Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #ifndef RUST_AST_BASE_H
20 : #define RUST_AST_BASE_H
21 : // Base for AST used in gccrs, basically required by all specific ast things
22 :
23 : #include "rust-system.h"
24 : #include "rust-hir-map.h"
25 : #include "rust-token.h"
26 : #include "rust-location.h"
27 : #include "rust-diagnostics.h"
28 : #include "rust-keyword-values.h"
29 : #include "rust-cloneable.h"
30 : #include "rust-ast-identifier.h"
31 : #include "rust-raw-path.h"
32 :
33 : namespace Rust {
34 : // TODO: remove typedefs and make actual types for these
35 : typedef int TupleIndex;
36 : struct Session;
37 : struct MacroExpander;
38 :
39 : namespace AST {
40 : // foward decl: ast visitor
41 : class ASTVisitor;
42 : using AttrVec = std::vector<Attribute>;
43 :
44 35532853 : class Visitable
45 : {
46 : public:
47 18963026 : virtual ~Visitable () = default;
48 : virtual void accept_vis (ASTVisitor &vis) = 0;
49 : };
50 :
51 : /**
52 : * Base function for reconstructing and asserting that the new NodeId is
53 : * different from the old NodeId. It then wraps the given pointer into a unique
54 : * pointer and returns it.
55 : */
56 : template <typename T>
57 : std::unique_ptr<T>
58 920 : reconstruct_base (const T *instance)
59 : {
60 920 : auto *reconstructed = instance->reconstruct_impl ();
61 :
62 920 : rust_assert (reconstructed->get_node_id () != instance->get_node_id ());
63 :
64 920 : return std::unique_ptr<T> (reconstructed);
65 : }
66 :
67 : /**
68 : * Reconstruct multiple items in a vector
69 : */
70 : template <typename T>
71 : std::vector<std::unique_ptr<T>>
72 834 : reconstruct_vec (const std::vector<std::unique_ptr<T>> &to_reconstruct)
73 : {
74 834 : std::vector<std::unique_ptr<T>> reconstructed;
75 834 : reconstructed.reserve (to_reconstruct.size ());
76 :
77 1634 : for (const auto &elt : to_reconstruct)
78 800 : reconstructed.emplace_back (std::unique_ptr<T> (elt->reconstruct_impl ()));
79 :
80 834 : return reconstructed;
81 : }
82 :
83 : // Delimiter types - used in macros and whatever.
84 : enum DelimType
85 : {
86 : PARENS,
87 : SQUARE,
88 : CURLY
89 : };
90 :
91 : // forward decl for use in token tree method
92 : class Token;
93 :
94 : // A tree of tokens (or a single token) - abstract base class
95 38267892 : class TokenTree : public Visitable
96 : {
97 : public:
98 3150240 : virtual ~TokenTree () {}
99 :
100 : // Unique pointer custom clone function
101 72531891 : std::unique_ptr<TokenTree> clone_token_tree () const
102 : {
103 72531891 : return std::unique_ptr<TokenTree> (clone_token_tree_impl ());
104 : }
105 :
106 : virtual std::string as_string () const = 0;
107 :
108 : /* Converts token tree to a flat token stream. Tokens must be pointer to
109 : * avoid mutual dependency with Token. */
110 : virtual std::vector<std::unique_ptr<Token>> to_token_stream () const = 0;
111 :
112 : protected:
113 : // pure virtual clone implementation
114 : virtual TokenTree *clone_token_tree_impl () const = 0;
115 : };
116 :
117 : // Abstract base class for a macro match
118 16955087 : class MacroMatch : public Visitable
119 : {
120 : public:
121 : enum MacroMatchType
122 : {
123 : Fragment,
124 : Repetition,
125 : Matcher,
126 : Tok
127 : };
128 :
129 8033 : virtual ~MacroMatch () {}
130 :
131 : virtual std::string as_string () const = 0;
132 : virtual location_t get_match_locus () const = 0;
133 :
134 : // Unique pointer custom clone function
135 98964 : std::unique_ptr<MacroMatch> clone_macro_match () const
136 : {
137 98964 : return std::unique_ptr<MacroMatch> (clone_macro_match_impl ());
138 : }
139 :
140 : virtual MacroMatchType get_macro_match_type () const = 0;
141 :
142 : protected:
143 : // pure virtual clone implementation
144 : virtual MacroMatch *clone_macro_match_impl () const = 0;
145 : };
146 :
147 : // A token is a kind of token tree (except delimiter tokens)
148 : class Token : public TokenTree, public MacroMatch
149 : {
150 : // A token is a kind of token tree (except delimiter tokens)
151 : // A token is a kind of MacroMatch (except $ and delimiter tokens)
152 :
153 : const_TokenPtr tok_ref;
154 :
155 : /* new idea: wrapper around const_TokenPtr used for heterogeneuous storage
156 : * in token trees. rather than convert back and forth when parsing macros,
157 : * just wrap it. */
158 :
159 : public:
160 : // Unique pointer custom clone function
161 97393528 : std::unique_ptr<Token> clone_token () const
162 : {
163 97393528 : return std::unique_ptr<Token> (clone_token_impl ());
164 : }
165 :
166 : // Constructor from lexer const_TokenPtr
167 24656670 : Token (const_TokenPtr lexer_tok_ptr) : tok_ref (std::move (lexer_tok_ptr)) {}
168 :
169 96452419 : bool is_string_lit () const
170 : {
171 96452419 : switch (get_id ())
172 : {
173 : case STRING_LITERAL:
174 : case BYTE_STRING_LITERAL:
175 : case RAW_STRING_LITERAL:
176 : case C_STRING_LITERAL:
177 : return true;
178 74584098 : default:
179 74584098 : return false;
180 : }
181 : }
182 :
183 : std::string as_string () const override;
184 7933 : location_t get_match_locus () const override
185 : {
186 7933 : return tok_ref->get_locus ();
187 : };
188 :
189 : void accept_vis (ASTVisitor &vis) override;
190 :
191 : // Return copy of itself but in token stream form.
192 : std::vector<std::unique_ptr<Token>> to_token_stream () const override;
193 :
194 120935476 : TokenId get_id () const { return tok_ref->get_id (); }
195 17 : bool should_have_str () const { return tok_ref->should_have_str (); }
196 2755643 : const std::string &get_str () const { return tok_ref->get_str (); }
197 :
198 101527 : location_t get_locus () const { return tok_ref->get_locus (); }
199 :
200 394 : PrimitiveCoreType get_type_hint () const { return tok_ref->get_type_hint (); }
201 :
202 : // Get a new token pointer copy.
203 48749030 : const_TokenPtr get_tok_ptr () const { return tok_ref; }
204 :
205 1076018 : MacroMatchType get_macro_match_type () const override
206 : {
207 1076018 : return MacroMatchType::Tok;
208 : }
209 :
210 : protected:
211 : // No virtual for now as not polymorphic but can be in future
212 196105782 : /*virtual*/ Token *clone_token_impl () const { return new Token (*this); }
213 :
214 : /* Use covariance to implement clone function as returning this object
215 : * rather than base */
216 70588462 : Token *clone_token_tree_impl () const final override
217 : {
218 70588462 : return clone_token_impl ();
219 : }
220 :
221 : /* Use covariance to implement clone function as returning this object
222 : * rather than base */
223 0 : Token *clone_macro_match_impl () const final override
224 : {
225 0 : return clone_token_impl ();
226 : }
227 : };
228 :
229 : // A literal - value with a type. Used in LiteralExpr and LiteralPattern.
230 1513468 : struct Literal
231 : {
232 : public:
233 : enum LitType
234 : {
235 : CHAR,
236 : STRING,
237 : BYTE,
238 : BYTE_STRING,
239 : RAW_STRING,
240 : C_STRING,
241 : INT,
242 : FLOAT,
243 : BOOL,
244 : ERROR
245 : };
246 :
247 : private:
248 : /* TODO: maybe make subclasses of each type of literal with their typed
249 : * values (or generics) */
250 : std::string value_as_string;
251 : LitType type;
252 : PrimitiveCoreType type_hint;
253 :
254 : public:
255 337077748 : std::string as_string () const { return value_as_string; }
256 :
257 1963872 : LitType get_lit_type () const { return type; }
258 :
259 1856301 : PrimitiveCoreType get_type_hint () const { return type_hint; }
260 :
261 3872217 : Literal (std::string value_as_string, LitType type,
262 : PrimitiveCoreType type_hint)
263 3872217 : : value_as_string (std::move (value_as_string)), type (type),
264 3872217 : type_hint (type_hint)
265 : {}
266 :
267 0 : static Literal create_error ()
268 : {
269 0 : return Literal ("", ERROR, PrimitiveCoreType::CORETYPE_UNKNOWN);
270 : }
271 :
272 : // Returns whether literal is in an invalid state.
273 132675892 : bool is_error () const { return type == ERROR; }
274 : };
275 :
276 : /* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr
277 : * to be defined */
278 : class AttrInputLiteral;
279 :
280 : /* TODO: move applicable stuff into here or just don't include it because
281 : * nothing uses it A segment of a path (maybe) */
282 2935364 : class PathSegment
283 : {
284 : public:
285 435166 : virtual ~PathSegment () {}
286 :
287 : virtual std::string as_string () const = 0;
288 :
289 : // TODO: add visitor here?
290 : };
291 :
292 : // A segment of a simple path without generic or type arguments
293 2496615 : class SimplePathSegment : public PathSegment
294 : {
295 : std::string segment_name;
296 : location_t locus;
297 : NodeId node_id;
298 :
299 : // only allow identifiers, "super", "self", "crate", or "$crate"
300 : public:
301 : // TODO: put checks in constructor to enforce this rule?
302 1261024 : SimplePathSegment (std::string segment_name, location_t locus)
303 2522048 : : segment_name (std::move (segment_name)), locus (locus),
304 1261024 : node_id (Analysis::Mappings::get ().get_next_node_id ())
305 1261024 : {}
306 :
307 : /* Returns whether simple path segment is in an invalid state (currently, if
308 : * empty). */
309 194566236 : bool is_error () const { return segment_name.empty (); }
310 :
311 : // Creates an error SimplePathSegment
312 : static SimplePathSegment create_error ()
313 : {
314 : return SimplePathSegment (std::string (""), UNDEF_LOCATION);
315 : }
316 :
317 : std::string as_string () const override;
318 :
319 1473463 : location_t get_locus () const { return locus; }
320 957441 : NodeId get_node_id () const { return node_id; }
321 29882029 : const std::string &get_segment_name () const { return segment_name; }
322 34413 : bool is_super_path_seg () const
323 : {
324 34413 : return as_string ().compare (Values::Keywords::SUPER) == 0;
325 : }
326 34929 : bool is_crate_path_seg () const
327 : {
328 34929 : return as_string ().compare (Values::Keywords::CRATE) == 0;
329 : }
330 200855 : bool is_lower_self_seg () const
331 : {
332 200855 : return as_string ().compare (Values::Keywords::SELF) == 0;
333 : }
334 31692 : bool is_big_self () const
335 : {
336 31692 : return as_string ().compare (Values::Keywords::SELF_ALIAS) == 0;
337 : }
338 : };
339 :
340 : // A simple path without generic or type arguments
341 42960081 : class SimplePath : public RawPath<SimplePathSegment>
342 : {
343 : location_t locus;
344 : NodeId node_id;
345 :
346 : public:
347 : // Constructor
348 4944673 : explicit SimplePath (std::vector<SimplePathSegment> path_segments,
349 : bool has_opening_scope_resolution = false,
350 : location_t locus = UNDEF_LOCATION)
351 : : RawPath (std::move (path_segments), has_opening_scope_resolution),
352 4944673 : locus (locus), node_id (Analysis::Mappings::get ().get_next_node_id ())
353 4944673 : {}
354 :
355 1637 : explicit SimplePath (Identifier ident)
356 1637 : : RawPath ({SimplePathSegment (ident.as_string (), ident.get_locus ())},
357 : false),
358 1637 : locus (ident.get_locus ()),
359 6548 : node_id (Analysis::Mappings::get ().get_next_node_id ())
360 1637 : {}
361 :
362 : // Creates an empty SimplePath.
363 3704713 : static SimplePath create_empty ()
364 : {
365 3704713 : return SimplePath (std::vector<SimplePathSegment> ());
366 : }
367 :
368 : const std::string as_string () const;
369 :
370 519021 : location_t get_locus () const { return locus; }
371 5216 : NodeId get_node_id () const { return node_id; }
372 :
373 : // does this need visitor if not polymorphic? probably not
374 :
375 : // path-to-string comparison operator
376 118215378 : bool operator== (const std::string &rhs) const
377 : {
378 118215378 : return !opening_scope_resolution && segments.size () == 1
379 236410298 : && segments[0].as_string () == rhs;
380 : }
381 :
382 : /* Creates a single-segment SimplePath from a string. This will not check to
383 : * ensure that this is a valid identifier in path, so be careful. Also, this
384 : * will have no location data.
385 : * TODO have checks? */
386 9263 : static SimplePath from_str (std::string str, location_t locus)
387 : {
388 9263 : std::vector<AST::SimplePathSegment> single_segments
389 18526 : = {AST::SimplePathSegment (std::move (str), locus)};
390 9263 : return SimplePath (std::move (single_segments), false, locus);
391 9263 : }
392 : };
393 :
394 : // path-to-string inverse comparison operator
395 : inline bool
396 14885 : operator!= (const SimplePath &lhs, const std::string &rhs)
397 : {
398 14885 : return !(lhs == rhs);
399 : }
400 :
401 : // forward decl for Attribute
402 : class AttrInput;
403 :
404 : // Visibility of item - if the item has it, then it is some form of public
405 1741092 : struct Visibility
406 : {
407 : public:
408 : enum VisType
409 : {
410 : PRIV,
411 : PUB,
412 : PUB_CRATE,
413 : PUB_SELF,
414 : PUB_SUPER,
415 : PUB_IN_PATH
416 : };
417 :
418 : private:
419 : VisType vis_type;
420 : // Only assigned if vis_type is IN_PATH
421 : SimplePath in_path;
422 : location_t locus;
423 :
424 3601811 : Visibility (VisType vis_type, SimplePath in_path, location_t locus)
425 2822877 : : vis_type (vis_type), in_path (std::move (in_path)), locus (locus)
426 : {}
427 :
428 : public:
429 276811 : VisType get_vis_type () const { return vis_type; }
430 :
431 : // Returns whether a visibility has a path
432 41919760 : bool has_path () const { return vis_type >= PUB_CRATE; }
433 :
434 : // Returns whether visibility is public or not.
435 5826 : bool is_public () const { return vis_type != PRIV; }
436 :
437 12417 : location_t get_locus () const { return locus; }
438 :
439 : // Unique pointer custom clone function
440 : /*std::unique_ptr<Visibility> clone_visibility() const {
441 : return std::unique_ptr<Visibility>(clone_visibility_impl());
442 : }*/
443 :
444 : /* TODO: think of a way to only allow valid Visibility states - polymorphism
445 : * is one idea but may be too resource-intensive. */
446 :
447 : // Creates a public visibility with no further features/arguments.
448 : // empty?
449 56993 : static Visibility create_public (location_t pub_vis_location)
450 : {
451 56993 : return Visibility (PUB, SimplePath::create_empty (), pub_vis_location);
452 : }
453 :
454 : // Creates a public visibility with crate-relative paths
455 1771 : static Visibility create_crate (location_t crate_tok_location,
456 : location_t crate_vis_location)
457 : {
458 1771 : return Visibility (PUB_CRATE,
459 1771 : SimplePath::from_str (Values::Keywords::CRATE,
460 : crate_tok_location),
461 1771 : crate_vis_location);
462 : }
463 :
464 : // Creates a public visibility with self-relative paths
465 0 : static Visibility create_self (location_t self_tok_location,
466 : location_t self_vis_location)
467 : {
468 0 : return Visibility (PUB_SELF,
469 0 : SimplePath::from_str (Values::Keywords::SELF,
470 : self_tok_location),
471 0 : self_vis_location);
472 : }
473 :
474 : // Creates a public visibility with parent module-relative paths
475 780 : static Visibility create_super (location_t super_tok_location,
476 : location_t super_vis_location)
477 : {
478 780 : return Visibility (PUB_SUPER,
479 780 : SimplePath::from_str (Values::Keywords::SUPER,
480 : super_tok_location),
481 780 : super_vis_location);
482 : }
483 :
484 : // Creates a private visibility
485 3542205 : static Visibility create_private ()
486 : {
487 3542205 : return Visibility (PRIV, SimplePath::create_empty (), UNDEF_LOCATION);
488 : }
489 :
490 : // Creates a public visibility with a given path or whatever.
491 62 : static Visibility create_in_path (SimplePath in_path,
492 : location_t in_path_vis_location)
493 : {
494 62 : return Visibility (PUB_IN_PATH, std::move (in_path), in_path_vis_location);
495 : }
496 :
497 : std::string as_string () const;
498 2600 : const SimplePath &get_path () const { return in_path; }
499 529372 : SimplePath &get_path () { return in_path; }
500 :
501 : protected:
502 : // Clone function implementation - not currently virtual but may be if
503 : // polymorphism used
504 : /*virtual*/ Visibility *clone_visibility_impl () const
505 : {
506 : return new Visibility (*this);
507 : }
508 : };
509 :
510 : // aka Attr
511 : // Attribute AST representation
512 : class Attribute : public Visitable
513 : {
514 : SimplePath path;
515 :
516 : // bool has_attr_input;
517 : std::unique_ptr<AttrInput> attr_input;
518 :
519 : location_t locus;
520 :
521 : bool inner_attribute;
522 :
523 : NodeId node_id;
524 :
525 : // TODO: maybe a variable storing whether attr input is parsed or not
526 :
527 : public:
528 : // Returns whether Attribute has AttrInput
529 193546394 : bool has_attr_input () const { return attr_input != nullptr; }
530 :
531 : // Constructor has pointer AttrInput for polymorphism reasons
532 707589 : Attribute (SimplePath path, std::unique_ptr<AttrInput> input,
533 : location_t locus = UNDEF_LOCATION, bool inner_attribute = false)
534 707589 : : path (std::move (path)), attr_input (std::move (input)), locus (locus),
535 707589 : inner_attribute (inner_attribute),
536 707589 : node_id (Analysis::Mappings::get ().get_next_node_id ())
537 707589 : {}
538 :
539 : bool is_derive () const;
540 :
541 : std::vector<std::reference_wrapper<AST::SimplePath>> get_traits_to_derive ();
542 :
543 : // default destructor
544 13906113 : ~Attribute () = default;
545 :
546 : // no point in being defined inline as requires virtual call anyway
547 : Attribute (const Attribute &other);
548 :
549 : // no point in being defined inline as requires virtual call anyway
550 : Attribute &operator= (const Attribute &other);
551 :
552 : // default move semantics
553 2058408 : Attribute (Attribute &&other) = default;
554 16869 : Attribute &operator= (Attribute &&other) = default;
555 :
556 : // Unique pointer custom clone function
557 : std::unique_ptr<Attribute> clone_attribute () const
558 : {
559 : return std::unique_ptr<Attribute> (clone_attribute_impl ());
560 : }
561 :
562 : // Creates an empty attribute (which is invalid)
563 38761 : static Attribute create_empty ()
564 : {
565 38761 : return Attribute (SimplePath::create_empty (), nullptr);
566 : }
567 :
568 : // Returns whether the attribute is considered an "empty" attribute.
569 8356 : bool is_empty () const { return attr_input == nullptr && path.is_empty (); }
570 :
571 : // Returns whether the attribute has no input
572 43699 : bool empty_input () const { return !attr_input; }
573 :
574 76152 : location_t get_locus () const { return locus; }
575 :
576 141616121 : AttrInput &get_attr_input () const { return *attr_input; }
577 :
578 10593 : void set_attr_input (std::unique_ptr<AST::AttrInput> input)
579 : {
580 10593 : attr_input = std::move (input);
581 : }
582 :
583 : /* e.g.:
584 : #![crate_type = "lib"]
585 : #[test]
586 : #[cfg(target_os = "linux")]
587 : #[allow(non_camel_case_types)]
588 : #![allow(unused_variables)]
589 : */
590 :
591 : // Full built-in attribute list:
592 : /* cfg
593 : * cfg_attr
594 : * test
595 : * ignore
596 : * should_panic
597 : * derive
598 : * macro_export
599 : * macro_use
600 : * proc_macro
601 : * proc_macro_derive
602 : * proc_macro_attribute
603 : * allow
604 : * warn
605 : * deny
606 : * forbid
607 : * deprecated
608 : * must_use
609 : * link
610 : * link_name
611 : * no_link
612 : * repr
613 : * crate_type
614 : * no_main
615 : * export_name
616 : * link_section
617 : * no_mangle
618 : * used
619 : * crate_name
620 : * inline
621 : * cold
622 : * no_builtins
623 : * target_feature
624 : * doc
625 : * no_std
626 : * no_implicit_prelude
627 : * path
628 : * recursion_limit
629 : * type_length_limit
630 : * panic_handler
631 : * global_allocator
632 : * windows_subsystem
633 : * feature */
634 :
635 : std::string as_string () const;
636 :
637 28368 : bool is_inner_attribute () const { return inner_attribute; }
638 :
639 : void accept_vis (ASTVisitor &vis) override;
640 :
641 30240700 : const SimplePath &get_path () const { return path; }
642 198820484 : SimplePath &get_path () { return path; }
643 :
644 : NodeId get_node_id () { return node_id; }
645 :
646 : // Call to parse attribute body to meta item syntax.
647 : void parse_attr_to_meta_item ();
648 :
649 : /* Determines whether cfg predicate is true and item with attribute should
650 : * not be stripped. Attribute body must already be parsed to meta item. */
651 : bool check_cfg_predicate (const Session &session) const;
652 :
653 : // Returns whether body has been parsed to meta item form or not.
654 : bool is_parsed_to_meta_item () const;
655 :
656 : /* Returns any attributes generated from cfg_attr attributes. Attribute body
657 : * must already be parsed to meta item. */
658 : std::vector<Attribute> separate_cfg_attrs () const;
659 :
660 : protected:
661 : // not virtual as currently no subclasses of Attribute, but could be in
662 : // future
663 : /*virtual*/ Attribute *clone_attribute_impl () const
664 : {
665 : return new Attribute (*this);
666 : }
667 : };
668 :
669 : // Attribute body - abstract base class
670 8632680 : class AttrInput : public Visitable
671 : {
672 : public:
673 : enum AttrInputType
674 : {
675 : EXPR,
676 : LITERAL,
677 : META_ITEM,
678 : TOKEN_TREE,
679 : };
680 :
681 3150240 : virtual ~AttrInput () {}
682 :
683 : // Unique pointer custom clone function
684 11331590 : std::unique_ptr<AttrInput> clone_attr_input () const
685 : {
686 11331590 : return std::unique_ptr<AttrInput> (clone_attr_input_impl ());
687 : }
688 :
689 : virtual std::string as_string () const = 0;
690 :
691 : virtual bool check_cfg_predicate (const Session &session) const = 0;
692 :
693 : // Parse attribute input to meta item, if possible
694 0 : virtual AttrInput *parse_to_meta_item () const { return nullptr; }
695 :
696 0 : virtual std::vector<Attribute> separate_cfg_attrs () const { return {}; }
697 :
698 : // Returns whether attr input has been parsed to meta item syntax.
699 : virtual bool is_meta_item () const = 0;
700 :
701 : virtual AttrInputType get_attr_input_type () const = 0;
702 :
703 : protected:
704 : // pure virtual clone implementation
705 : virtual AttrInput *clone_attr_input_impl () const = 0;
706 : };
707 :
708 : // Forward decl - defined in rust-macro.h
709 : class MetaNameValueStr;
710 :
711 : // abstract base meta item inner class
712 94 : class MetaItemInner : public Visitable
713 : {
714 : protected:
715 : // pure virtual as MetaItemInner
716 : virtual MetaItemInner *clone_meta_item_inner_impl () const = 0;
717 :
718 : public:
719 : enum class Kind
720 : {
721 : LitExpr,
722 : MetaItem,
723 : };
724 :
725 : // Unique pointer custom clone function
726 92231 : std::unique_ptr<MetaItemInner> clone_meta_item_inner () const
727 : {
728 92231 : return std::unique_ptr<MetaItemInner> (clone_meta_item_inner_impl ());
729 : }
730 :
731 : virtual Kind get_kind () = 0;
732 :
733 : virtual ~MetaItemInner ();
734 :
735 : virtual location_t get_locus () const = 0;
736 :
737 : virtual std::string as_string () const = 0;
738 :
739 : /* HACK: used to simplify parsing - creates a copy of that type, or returns
740 : * null */
741 : virtual std::unique_ptr<MetaNameValueStr> to_meta_name_value_str () const;
742 :
743 : // HACK: used to simplify parsing - same thing
744 0 : virtual SimplePath to_path_item () const
745 : {
746 0 : return SimplePath::create_empty ();
747 : }
748 :
749 0 : virtual Attribute to_attribute () const { return Attribute::create_empty (); }
750 :
751 : virtual bool check_cfg_predicate (const Session &session) const = 0;
752 :
753 1182 : virtual bool is_key_value_pair () const { return false; }
754 : };
755 :
756 : // Container used to store MetaItems as AttrInput (bridge-ish kinda thing)
757 : class AttrInputMetaItemContainer : public AttrInput
758 : {
759 : std::vector<std::unique_ptr<MetaItemInner>> items;
760 :
761 : public:
762 68837 : AttrInputMetaItemContainer (std::vector<std::unique_ptr<MetaItemInner>> items)
763 68837 : : items (std::move (items))
764 : {}
765 :
766 : // copy constructor with vector clone
767 49983 : AttrInputMetaItemContainer (const AttrInputMetaItemContainer &other)
768 49983 : {
769 49983 : items.reserve (other.items.size ());
770 104469 : for (const auto &e : other.items)
771 54486 : items.push_back (e->clone_meta_item_inner ());
772 49983 : }
773 :
774 : // copy assignment operator with vector clone
775 : AttrInputMetaItemContainer &
776 : operator= (const AttrInputMetaItemContainer &other)
777 : {
778 : AttrInput::operator= (other);
779 :
780 : items.reserve (other.items.size ());
781 : for (const auto &e : other.items)
782 : items.push_back (e->clone_meta_item_inner ());
783 :
784 : return *this;
785 : }
786 :
787 : // default move constructors
788 : AttrInputMetaItemContainer (AttrInputMetaItemContainer &&other) = default;
789 : AttrInputMetaItemContainer &operator= (AttrInputMetaItemContainer &&other)
790 : = default;
791 :
792 : std::string as_string () const override;
793 :
794 : void accept_vis (ASTVisitor &vis) override;
795 :
796 : bool check_cfg_predicate (const Session &session) const override;
797 :
798 34311 : AttrInputType get_attr_input_type () const final override
799 : {
800 34311 : return AttrInput::AttrInputType::META_ITEM;
801 : }
802 :
803 : // Clones this object.
804 : std::unique_ptr<AttrInputMetaItemContainer>
805 : clone_attr_input_meta_item_container () const
806 : {
807 : return std::unique_ptr<AttrInputMetaItemContainer> (
808 : clone_attr_input_meta_item_container_impl ());
809 : }
810 :
811 : std::vector<Attribute> separate_cfg_attrs () const override;
812 :
813 150535 : bool is_meta_item () const override { return true; }
814 :
815 : // TODO: this mutable getter seems dodgy
816 382702 : std::vector<std::unique_ptr<MetaItemInner>> &get_items () { return items; }
817 645 : const std::vector<std::unique_ptr<MetaItemInner>> &get_items () const
818 : {
819 9388 : return items;
820 : }
821 :
822 : protected:
823 : // Use covariance to implement clone function as returning this type
824 49982 : AttrInputMetaItemContainer *clone_attr_input_impl () const final override
825 : {
826 49982 : return clone_attr_input_meta_item_container_impl ();
827 : }
828 :
829 49982 : AttrInputMetaItemContainer *clone_attr_input_meta_item_container_impl () const
830 : {
831 49982 : return new AttrInputMetaItemContainer (*this);
832 : }
833 : };
834 :
835 : // A token tree with delimiters
836 3131930 : class DelimTokenTree : public TokenTree, public AttrInput
837 : {
838 : DelimType delim_type;
839 : std::vector<std::unique_ptr<TokenTree>> token_trees;
840 : location_t locus;
841 :
842 : protected:
843 5489462 : DelimTokenTree *clone_delim_tok_tree_impl () const
844 : {
845 5489462 : return new DelimTokenTree (*this);
846 : }
847 :
848 : /* Use covariance to implement clone function as returning a DelimTokenTree
849 : * object */
850 0 : DelimTokenTree *clone_attr_input_impl () const final override
851 : {
852 0 : return clone_delim_tok_tree_impl ();
853 : }
854 :
855 : /* Use covariance to implement clone function as returning a DelimTokenTree
856 : * object */
857 1943429 : DelimTokenTree *clone_token_tree_impl () const final override
858 : {
859 1943429 : return clone_delim_tok_tree_impl ();
860 : }
861 :
862 : public:
863 1506120 : DelimTokenTree (DelimType delim_type,
864 : std::vector<std::unique_ptr<TokenTree>> token_trees
865 : = std::vector<std::unique_ptr<TokenTree>> (),
866 : location_t locus = UNDEF_LOCATION)
867 1506120 : : delim_type (delim_type), token_trees (std::move (token_trees)),
868 1506107 : locus (locus)
869 : {}
870 :
871 : // Copy constructor with vector clone
872 9473720 : DelimTokenTree (DelimTokenTree const &other)
873 9473720 : : delim_type (other.delim_type), locus (other.locus)
874 : {
875 9473720 : token_trees.clear ();
876 9473720 : token_trees.reserve (other.token_trees.size ());
877 81817688 : for (const auto &e : other.token_trees)
878 72343968 : token_trees.push_back (e->clone_token_tree ());
879 9473720 : }
880 :
881 : // overloaded assignment operator with vector clone
882 11036 : DelimTokenTree &operator= (DelimTokenTree const &other)
883 : {
884 11036 : delim_type = other.delim_type;
885 11036 : locus = other.locus;
886 :
887 11036 : token_trees.clear ();
888 11036 : token_trees.reserve (other.token_trees.size ());
889 198959 : for (const auto &e : other.token_trees)
890 187923 : token_trees.push_back (e->clone_token_tree ());
891 :
892 11036 : return *this;
893 : }
894 :
895 : // move constructors
896 1552821 : DelimTokenTree (DelimTokenTree &&other) = default;
897 : DelimTokenTree &operator= (DelimTokenTree &&other) = default;
898 :
899 13 : static DelimTokenTree create_empty () { return DelimTokenTree (PARENS); }
900 :
901 : std::string as_string () const override;
902 :
903 : void accept_vis (ASTVisitor &vis) override;
904 :
905 0 : bool check_cfg_predicate (const Session &) const override
906 : {
907 : // this should never be called - should be converted first
908 0 : rust_assert (false);
909 : return false;
910 : }
911 :
912 : AttrInputMetaItemContainer *parse_to_meta_item () const override;
913 :
914 : std::vector<std::unique_ptr<Token>> to_token_stream () const override;
915 :
916 : std::unique_ptr<DelimTokenTree> clone_delim_token_tree () const
917 : {
918 : return std::unique_ptr<DelimTokenTree> (clone_delim_tok_tree_impl ());
919 : }
920 :
921 35520 : bool is_meta_item () const override { return false; }
922 :
923 4999747 : AttrInputType get_attr_input_type () const final override
924 : {
925 4999747 : return AttrInput::AttrInputType::TOKEN_TREE;
926 : }
927 :
928 : std::vector<std::unique_ptr<TokenTree>> &get_token_trees ()
929 : {
930 32334102 : return token_trees;
931 : }
932 :
933 : const std::vector<std::unique_ptr<TokenTree>> &get_token_trees () const
934 : {
935 0 : return token_trees;
936 : }
937 :
938 1317784 : DelimType get_delim_type () const { return delim_type; }
939 850106 : location_t get_locus () const { return locus; }
940 : };
941 :
942 : /* Forward decl - definition moved to rust-expr.h as it requires LiteralExpr
943 : * to be defined */
944 : class AttrInputLiteral;
945 :
946 : // abstract base meta item class
947 173810 : class MetaItem : public MetaItemInner
948 : {
949 : public:
950 : enum class ItemKind
951 : {
952 : Path,
953 : Word,
954 : NameValueStr,
955 : PathExpr,
956 : Seq,
957 : ListPaths,
958 : ListNameValueStr,
959 : };
960 :
961 13264 : MetaItemInner::Kind get_kind () override
962 : {
963 13264 : return MetaItemInner::Kind::MetaItem;
964 : }
965 :
966 : virtual ItemKind get_item_kind () const = 0;
967 : };
968 :
969 : // Forward decl - defined in rust-expr.h
970 : class MetaItemLitExpr;
971 :
972 : // Forward decl - defined in rust-expr.h
973 : class MetaItemPathExpr;
974 :
975 : // Forward decl - defined in rust-macro.h
976 : class MetaItemPath;
977 :
978 : // Forward decl - defined in rust-macro.h
979 : class MetaItemSeq;
980 :
981 : // Forward decl - defined in rust-macro.h
982 : class MetaWord;
983 :
984 : // Forward decl - defined in rust-macro.h
985 : class MetaListPaths;
986 :
987 : // Forward decl - defined in rust-macro.h
988 : class MetaListNameValueStr;
989 :
990 : /* Base statement abstract class. Note that most "statements" are not allowed
991 : * in top-level module scope - only a subclass of statements called "items"
992 : * are. */
993 7650 : class Stmt : public Visitable
994 : {
995 : public:
996 : enum class Kind
997 : {
998 : Empty,
999 : Item,
1000 : Let,
1001 : Expr,
1002 : MacroInvocation,
1003 : };
1004 :
1005 : // Unique pointer custom clone function
1006 379591 : std::unique_ptr<Stmt> clone_stmt () const
1007 : {
1008 379591 : return std::unique_ptr<Stmt> (clone_stmt_impl ());
1009 : }
1010 :
1011 : virtual ~Stmt () {}
1012 :
1013 : virtual std::string as_string () const = 0;
1014 :
1015 : virtual location_t get_locus () const = 0;
1016 :
1017 : virtual void mark_for_strip () = 0;
1018 : virtual bool is_marked_for_strip () const = 0;
1019 :
1020 : // TODO: put this in a virtual base class?
1021 11481052 : virtual NodeId get_node_id () const { return node_id; }
1022 :
1023 : virtual Kind get_stmt_kind () = 0;
1024 :
1025 : // TODO: Can we remove these two?
1026 : virtual bool is_item () const = 0;
1027 30456 : virtual bool is_expr () const { return false; }
1028 :
1029 38 : virtual void add_semicolon () {}
1030 :
1031 : protected:
1032 5302350 : Stmt () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1033 :
1034 : // Clone function implementation as pure virtual method
1035 : virtual Stmt *clone_stmt_impl () const = 0;
1036 :
1037 : NodeId node_id;
1038 : };
1039 :
1040 : // Rust "item" AST node (declaration of top-level/module-level allowed stuff)
1041 3312989 : class Item : public Stmt
1042 : {
1043 : public:
1044 : enum class Kind
1045 : {
1046 : MacroRulesDefinition,
1047 : MacroInvocation,
1048 : Module,
1049 : ExternCrate,
1050 : UseDeclaration,
1051 : Function,
1052 : TypeAlias,
1053 : TraitAlias,
1054 : Struct,
1055 : EnumItem,
1056 : Enum,
1057 : Union,
1058 : ConstantItem,
1059 : StaticItem,
1060 : Trait,
1061 : Impl,
1062 : ExternBlock,
1063 : };
1064 :
1065 : virtual Kind get_item_kind () const = 0;
1066 :
1067 : // Unique pointer custom clone function
1068 502536 : std::unique_ptr<Item> clone_item () const
1069 : {
1070 502536 : return std::unique_ptr<Item> (clone_item_impl ());
1071 : }
1072 :
1073 : /* Adds crate names to the vector passed by reference, if it can
1074 : * (polymorphism). TODO: remove, unused. */
1075 : virtual void
1076 0 : add_crate_name (std::vector<std::string> &names ATTRIBUTE_UNUSED) const
1077 0 : {}
1078 :
1079 317634 : Stmt::Kind get_stmt_kind () final { return Stmt::Kind::Item; }
1080 :
1081 : // FIXME: ARTHUR: Is it okay to have removed that final? Is it *required*
1082 : // behavior that we have items that can also be expressions?
1083 0 : bool is_item () const override { return true; }
1084 :
1085 : virtual std::vector<Attribute> &get_outer_attrs () = 0;
1086 : virtual const std::vector<Attribute> &get_outer_attrs () const = 0;
1087 :
1088 3418703 : virtual bool has_outer_attrs () const { return !get_outer_attrs ().empty (); }
1089 :
1090 : protected:
1091 : // Clone function implementation as pure virtual method
1092 : virtual Item *clone_item_impl () const = 0;
1093 :
1094 : /* Save having to specify two clone methods in derived classes by making
1095 : * statement clone return item clone. Hopefully won't affect performance too
1096 : * much. */
1097 41925 : Item *clone_stmt_impl () const final override { return clone_item_impl (); }
1098 : };
1099 :
1100 15810 : class GlobContainer
1101 : {
1102 : public:
1103 : enum class Kind
1104 : {
1105 : Crate,
1106 : Module,
1107 : Enum,
1108 : };
1109 :
1110 : virtual Kind get_glob_container_kind () const = 0;
1111 : };
1112 :
1113 : // Item that supports visibility - abstract base class
1114 : class VisItem : public Item
1115 : {
1116 : Visibility visibility;
1117 : std::vector<Attribute> outer_attrs;
1118 :
1119 : protected:
1120 : // Visibility constructor
1121 271800 : VisItem (Visibility visibility,
1122 : std::vector<Attribute> outer_attrs = std::vector<Attribute> ())
1123 271800 : : visibility (std::move (visibility)), outer_attrs (std::move (outer_attrs))
1124 271800 : {}
1125 :
1126 : // Visibility copy constructor
1127 1215964 : VisItem (VisItem const &other)
1128 1215964 : : visibility (other.visibility), outer_attrs (other.outer_attrs)
1129 1215964 : {}
1130 :
1131 : // Overload assignment operator to clone
1132 0 : VisItem &operator= (VisItem const &other)
1133 : {
1134 0 : visibility = other.visibility;
1135 0 : outer_attrs = other.outer_attrs;
1136 :
1137 0 : return *this;
1138 : }
1139 :
1140 : // move constructors
1141 7520 : VisItem (VisItem &&other) = default;
1142 : VisItem &operator= (VisItem &&other) = default;
1143 :
1144 : public:
1145 : /* Does the item have some kind of public visibility (non-default
1146 : * visibility)? */
1147 5826 : bool has_visibility () const { return visibility.is_public (); }
1148 :
1149 : std::string as_string () const override;
1150 :
1151 : // TODO: this mutable getter seems really dodgy. Think up better way.
1152 40508609 : Visibility &get_visibility () { return visibility; }
1153 : const Visibility &get_visibility () const { return visibility; }
1154 :
1155 68257958 : std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
1156 3319101 : const std::vector<Attribute> &get_outer_attrs () const override
1157 : {
1158 3319101 : return outer_attrs;
1159 : }
1160 :
1161 : virtual Item::Kind get_item_kind () const override = 0;
1162 : };
1163 :
1164 : // forward decl of ExprWithoutBlock
1165 : class ExprWithoutBlock;
1166 :
1167 : // Base expression AST node - abstract
1168 1219942 : class Expr : public Visitable
1169 : {
1170 : public:
1171 : enum class Kind
1172 : {
1173 : PathInExpression,
1174 : QualifiedPathInExpression,
1175 : Literal,
1176 : Operator,
1177 : Grouped,
1178 : Array,
1179 : ArrayIndex,
1180 : Tuple,
1181 : TupleIndex,
1182 : Struct,
1183 : Call,
1184 : MethodCall,
1185 : FieldAccess,
1186 : Closure,
1187 : Block,
1188 : ConstExpr,
1189 : ConstBlock,
1190 : Continue,
1191 : Break,
1192 : Range,
1193 : Box,
1194 : Return,
1195 : UnsafeBlock,
1196 : Loop,
1197 : If,
1198 : IfLet,
1199 : Match,
1200 : Await,
1201 : AsyncBlock,
1202 : InlineAsm,
1203 : LlvmInlineAsm,
1204 : Identifier,
1205 : FormatArgs,
1206 : OffsetOf,
1207 : MacroInvocation,
1208 : Borrow,
1209 : Dereference,
1210 : ErrorPropagation,
1211 : Negation,
1212 : ArithmeticOrLogical,
1213 : Comparison,
1214 : LazyBoolean,
1215 : TypeCast,
1216 : Assignment,
1217 : CompoundAssignment,
1218 : Try,
1219 : };
1220 :
1221 : virtual Kind get_expr_kind () const = 0;
1222 :
1223 : // Unique pointer custom clone function
1224 1413 : std::unique_ptr<Expr> clone_expr () const
1225 : {
1226 33818633 : return std::unique_ptr<Expr> (clone_expr_impl ());
1227 : }
1228 :
1229 : /* TODO: public methods that could be useful:
1230 : * - get_type() - returns type of expression. set_type() may also be useful
1231 : * for some?
1232 : * - evaluate() - evaluates expression if constant? can_evaluate()? */
1233 :
1234 : virtual std::string as_string () const = 0;
1235 :
1236 10573 : virtual ~Expr () {}
1237 :
1238 : virtual location_t get_locus () const = 0;
1239 :
1240 21610 : virtual bool is_literal () const { return false; }
1241 :
1242 : // HACK: strictly not needed, but faster than full downcast clone
1243 : virtual bool is_expr_without_block () const = 0;
1244 :
1245 : virtual void mark_for_strip () = 0;
1246 : virtual bool is_marked_for_strip () const = 0;
1247 :
1248 137527475 : virtual NodeId get_node_id () const { return node_id; }
1249 :
1250 0 : virtual void set_node_id (NodeId id) { node_id = id; }
1251 :
1252 : virtual std::vector<Attribute> &get_outer_attrs () = 0;
1253 :
1254 : // TODO: think of less hacky way to implement this kind of thing
1255 : // Sets outer attributes.
1256 : virtual void set_outer_attrs (std::vector<Attribute>) = 0;
1257 :
1258 : protected:
1259 : // Constructor
1260 14462487 : Expr () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1261 :
1262 : // Clone function implementation as pure virtual method
1263 : virtual Expr *clone_expr_impl () const = 0;
1264 :
1265 : NodeId node_id;
1266 : };
1267 :
1268 : // AST node for an expression without an accompanying block - abstract
1269 16451827 : class ExprWithoutBlock : public Expr
1270 : {
1271 : protected:
1272 : // pure virtual clone implementation
1273 : virtual ExprWithoutBlock *clone_expr_without_block_impl () const = 0;
1274 :
1275 : /* Save having to specify two clone methods in derived classes by making
1276 : * expr clone return exprwithoutblock clone. Hopefully won't affect
1277 : * performance too much. */
1278 33311532 : ExprWithoutBlock *clone_expr_impl () const final override
1279 : {
1280 33311532 : return clone_expr_without_block_impl ();
1281 : }
1282 :
1283 1154981 : bool is_expr_without_block () const final override { return true; };
1284 :
1285 : public:
1286 : // Unique pointer custom clone function
1287 135 : std::unique_ptr<ExprWithoutBlock> clone_expr_without_block () const
1288 : {
1289 135 : return std::unique_ptr<ExprWithoutBlock> (clone_expr_without_block_impl ());
1290 : }
1291 : };
1292 :
1293 : /* HACK: IdentifierExpr, delete when figure out identifier vs expr problem in
1294 : * Pratt parser */
1295 : /* Alternatively, identifiers could just be represented as single-segment
1296 : * paths
1297 : */
1298 : class IdentifierExpr : public ExprWithoutBlock
1299 : {
1300 : std::vector<Attribute> outer_attrs;
1301 : Identifier ident;
1302 : location_t locus;
1303 :
1304 : public:
1305 1110103 : IdentifierExpr (Identifier ident, std::vector<Attribute> outer_attrs,
1306 : location_t locus)
1307 2220206 : : outer_attrs (std::move (outer_attrs)), ident (std::move (ident)),
1308 1110103 : locus (locus)
1309 1110103 : {}
1310 :
1311 12 : std::string as_string () const override { return ident.as_string (); }
1312 :
1313 3324292 : location_t get_locus () const override final { return locus; }
1314 :
1315 2049236 : Identifier get_ident () const { return ident; }
1316 :
1317 : void accept_vis (ASTVisitor &vis) override;
1318 :
1319 : // Clones this object.
1320 : std::unique_ptr<IdentifierExpr> clone_identifier_expr () const
1321 : {
1322 : return std::unique_ptr<IdentifierExpr> (clone_identifier_expr_impl ());
1323 : }
1324 :
1325 : // "Error state" if ident is empty, so base stripping on this.
1326 0 : void mark_for_strip () override { ident = {""}; }
1327 76382977 : bool is_marked_for_strip () const override { return ident.empty (); }
1328 :
1329 : const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; }
1330 474179237 : std::vector<Attribute> &get_outer_attrs () override { return outer_attrs; }
1331 :
1332 0 : void set_outer_attrs (std::vector<Attribute> new_attrs) override
1333 : {
1334 0 : outer_attrs = std::move (new_attrs);
1335 0 : }
1336 :
1337 1009143 : Expr::Kind get_expr_kind () const override { return Expr::Kind::Identifier; }
1338 :
1339 : protected:
1340 : // Clone method implementation
1341 7215129 : IdentifierExpr *clone_expr_without_block_impl () const final override
1342 : {
1343 7215129 : return clone_identifier_expr_impl ();
1344 : }
1345 :
1346 7215129 : IdentifierExpr *clone_identifier_expr_impl () const
1347 : {
1348 7215129 : return new IdentifierExpr (*this);
1349 : }
1350 : };
1351 :
1352 : // Pattern base AST node
1353 6648179 : class Pattern : public Visitable
1354 : {
1355 : public:
1356 : enum class Kind
1357 : {
1358 : Literal,
1359 : Identifier,
1360 : Wildcard,
1361 : Rest,
1362 : Range,
1363 : Reference,
1364 : Struct,
1365 : TupleStruct,
1366 : Tuple,
1367 : Grouped,
1368 : Slice,
1369 : Alt,
1370 : Path,
1371 : MacroInvocation,
1372 : };
1373 :
1374 : // Unique pointer custom clone function
1375 4252619 : std::unique_ptr<Pattern> clone_pattern () const
1376 : {
1377 4252619 : return std::unique_ptr<Pattern> (clone_pattern_impl ());
1378 : }
1379 :
1380 : virtual Kind get_pattern_kind () = 0;
1381 :
1382 : // possible virtual methods: is_refutable()
1383 :
1384 : virtual ~Pattern () {}
1385 :
1386 : virtual std::string as_string () const = 0;
1387 :
1388 : // as only one kind of pattern can be stripped, have default of nothing
1389 0 : virtual void mark_for_strip () {}
1390 36355855 : virtual bool is_marked_for_strip () const { return false; }
1391 :
1392 : virtual location_t get_locus () const = 0;
1393 : virtual NodeId get_node_id () const = 0;
1394 :
1395 : protected:
1396 : // Clone pattern implementation as pure virtual method
1397 : virtual Pattern *clone_pattern_impl () const = 0;
1398 : };
1399 :
1400 : // forward decl for Type
1401 : class TraitBound;
1402 :
1403 : // Base class for types as represented in AST - abstract
1404 4210387 : class Type : public Visitable
1405 : {
1406 : public:
1407 : enum Kind
1408 : {
1409 : MacroInvocation,
1410 : TypePath,
1411 : QualifiedPathInType,
1412 : ImplTrait,
1413 : TraitObject,
1414 : Parenthesised,
1415 : ImplTraitTypeOneBound,
1416 : TraitObjectTypeOneBound,
1417 : Tuple,
1418 : Never,
1419 : RawPointer,
1420 : Reference,
1421 : Array,
1422 : Slice,
1423 : Inferred,
1424 : BareFunction,
1425 : };
1426 :
1427 : virtual Kind get_type_kind () const = 0;
1428 :
1429 : // Unique pointer custom clone function
1430 2588627 : std::unique_ptr<Type> clone_type () const
1431 : {
1432 2588627 : return std::unique_ptr<Type> (clone_type_impl ());
1433 : }
1434 :
1435 : // Similar to `clone_type`, but generates a new instance of the node with a
1436 : // different NodeId
1437 869 : std::unique_ptr<Type> reconstruct () const { return reconstruct_base (this); }
1438 :
1439 : // virtual destructor
1440 1240701 : virtual ~Type () {}
1441 :
1442 : virtual std::string as_string () const = 0;
1443 :
1444 : /* HACK: convert to trait bound. Virtual method overriden by classes that
1445 : * enable this. */
1446 78 : virtual TraitBound *to_trait_bound (bool) const { return nullptr; }
1447 : /* as pointer, shouldn't require definition beforehand, only forward
1448 : * declaration. */
1449 :
1450 : // as only two kinds of types can be stripped, have default of nothing
1451 0 : virtual void mark_for_strip () {}
1452 7935367 : virtual bool is_marked_for_strip () const { return false; }
1453 :
1454 : virtual location_t get_locus () const = 0;
1455 :
1456 : // TODO: put this in a virtual base class?
1457 4580296 : virtual NodeId get_node_id () const { return node_id; }
1458 : virtual Type *reconstruct_impl () const = 0;
1459 :
1460 : protected:
1461 5006359 : Type () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1462 4227 : Type (NodeId node_id) : node_id (node_id) {}
1463 :
1464 : // Clone and reconstruct function implementations as pure virtual methods
1465 : virtual Type *clone_type_impl () const = 0;
1466 :
1467 : NodeId node_id;
1468 : };
1469 :
1470 : // A type without parentheses? - abstract
1471 5194829 : class TypeNoBounds : public Type
1472 : {
1473 : public:
1474 : // Unique pointer custom clone function
1475 659068 : std::unique_ptr<TypeNoBounds> clone_type_no_bounds () const
1476 : {
1477 659068 : return std::unique_ptr<TypeNoBounds> (clone_type_no_bounds_impl ());
1478 : }
1479 :
1480 50 : std::unique_ptr<TypeNoBounds> reconstruct () const
1481 : {
1482 50 : return reconstruct_base (this);
1483 : }
1484 :
1485 : virtual TypeNoBounds *reconstruct_impl () const override = 0;
1486 :
1487 : protected:
1488 : // Clone function implementation as pure virtual method
1489 : virtual TypeNoBounds *clone_type_no_bounds_impl () const = 0;
1490 :
1491 : /* Save having to specify two clone methods in derived classes by making
1492 : * type clone return typenobounds clone. Hopefully won't affect performance
1493 : * too much. */
1494 2584400 : TypeNoBounds *clone_type_impl () const final override
1495 : {
1496 2584400 : return clone_type_no_bounds_impl ();
1497 : }
1498 :
1499 5005346 : TypeNoBounds () : Type () {}
1500 : };
1501 :
1502 : /* Abstract base class representing a type param bound - Lifetime and
1503 : * TraitBound extends it */
1504 : class TypeParamBound : public Visitable
1505 : {
1506 : public:
1507 : enum TypeParamBoundType
1508 : {
1509 : TRAIT,
1510 : LIFETIME
1511 : };
1512 :
1513 156050 : virtual ~TypeParamBound () {}
1514 :
1515 : // Unique pointer custom clone function
1516 130366 : std::unique_ptr<TypeParamBound> clone_type_param_bound () const
1517 : {
1518 130366 : return std::unique_ptr<TypeParamBound> (clone_type_param_bound_impl ());
1519 : }
1520 :
1521 0 : std::unique_ptr<TypeParamBound> reconstruct () const
1522 : {
1523 0 : return reconstruct_base (this);
1524 : }
1525 :
1526 : virtual std::string as_string () const = 0;
1527 :
1528 152211 : NodeId get_node_id () const { return node_id; }
1529 :
1530 : virtual location_t get_locus () const = 0;
1531 :
1532 : virtual TypeParamBoundType get_bound_type () const = 0;
1533 :
1534 : virtual TypeParamBound *reconstruct_impl () const = 0;
1535 :
1536 : protected:
1537 : // Clone function implementation as pure virtual method
1538 : virtual TypeParamBound *clone_type_param_bound_impl () const = 0;
1539 :
1540 : TypeParamBound () : node_id (Analysis::Mappings::get ().get_next_node_id ())
1541 : {}
1542 1966 : TypeParamBound (NodeId node_id) : node_id (node_id) {}
1543 :
1544 : NodeId node_id;
1545 : };
1546 :
1547 : // Represents a lifetime (and is also a kind of type param bound)
1548 396774 : class Lifetime : public TypeParamBound
1549 : {
1550 : public:
1551 : enum LifetimeType
1552 : {
1553 : NAMED, // corresponds to LIFETIME_OR_LABEL
1554 : STATIC, // corresponds to 'static
1555 : WILDCARD // corresponds to '_
1556 : };
1557 :
1558 : private:
1559 : LifetimeType lifetime_type;
1560 : std::string lifetime_name;
1561 : location_t locus;
1562 : NodeId node_id;
1563 :
1564 : public:
1565 : // Constructor
1566 234774 : Lifetime (LifetimeType type, std::string name = std::string (),
1567 : location_t locus = UNDEF_LOCATION)
1568 234774 : : TypeParamBound (Analysis::Mappings::get ().get_next_node_id ()),
1569 234774 : lifetime_type (type), lifetime_name (std::move (name)), locus (locus)
1570 234774 : {}
1571 :
1572 3464 : Lifetime (NodeId id, LifetimeType type, std::string name = std::string (),
1573 : location_t locus = UNDEF_LOCATION)
1574 3464 : : TypeParamBound (id), lifetime_type (type),
1575 6928 : lifetime_name (std::move (name)), locus (locus)
1576 : {}
1577 :
1578 199243 : static Lifetime elided () { return Lifetime (WILDCARD, ""); }
1579 :
1580 : // Returns true if the lifetime is in an error state.
1581 : std::string as_string () const override;
1582 :
1583 : void accept_vis (ASTVisitor &vis) override;
1584 :
1585 140877 : LifetimeType get_lifetime_type () const { return lifetime_type; }
1586 :
1587 140819 : location_t get_locus () const override final { return locus; }
1588 :
1589 538222 : std::string get_lifetime_name () const { return lifetime_name; }
1590 :
1591 159 : TypeParamBoundType get_bound_type () const override
1592 : {
1593 159 : return TypeParamBound::TypeParamBoundType::LIFETIME;
1594 : }
1595 :
1596 : protected:
1597 : /* Use covariance to implement clone function as returning this object
1598 : * rather than base */
1599 3464 : Lifetime *clone_type_param_bound_impl () const override
1600 : {
1601 6928 : return new Lifetime (node_id, lifetime_type, lifetime_name, locus);
1602 : }
1603 0 : Lifetime *reconstruct_impl () const override
1604 : {
1605 0 : return new Lifetime (lifetime_type, lifetime_name, locus);
1606 : }
1607 : };
1608 :
1609 : /* Base generic parameter in AST. Abstract - can be represented by a Lifetime
1610 : * or Type param */
1611 : class GenericParam : public Visitable
1612 : {
1613 : public:
1614 : enum class Kind
1615 : {
1616 : Lifetime,
1617 : Type,
1618 : Const,
1619 : };
1620 :
1621 : virtual ~GenericParam () {}
1622 :
1623 : // Unique pointer custom clone function
1624 366924 : std::unique_ptr<GenericParam> clone_generic_param () const
1625 : {
1626 366924 : return std::unique_ptr<GenericParam> (clone_generic_param_impl ());
1627 : }
1628 :
1629 : virtual std::string as_string () const = 0;
1630 :
1631 : virtual location_t get_locus () const = 0;
1632 :
1633 : virtual Kind get_kind () const = 0;
1634 :
1635 6586139 : NodeId get_node_id () const { return node_id; }
1636 :
1637 : protected:
1638 12884 : GenericParam () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1639 400801 : GenericParam (NodeId node_id) : node_id (node_id) {}
1640 :
1641 : // Clone function implementation as pure virtual method
1642 : virtual GenericParam *clone_generic_param_impl () const = 0;
1643 :
1644 : NodeId node_id;
1645 : };
1646 :
1647 : // A lifetime generic parameter (as opposed to a type generic parameter)
1648 : class LifetimeParam : public GenericParam
1649 : {
1650 : Lifetime lifetime;
1651 : std::vector<Lifetime> lifetime_bounds;
1652 : AST::AttrVec outer_attrs;
1653 : location_t locus;
1654 :
1655 : public:
1656 0 : Lifetime get_lifetime () const { return lifetime; }
1657 :
1658 1390044 : Lifetime &get_lifetime () { return lifetime; }
1659 :
1660 1730206 : AST::AttrVec &get_outer_attrs () { return outer_attrs; }
1661 :
1662 : // Returns whether the lifetime param has any lifetime bounds.
1663 182 : bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); }
1664 :
1665 1378919 : std::vector<Lifetime> &get_lifetime_bounds () { return lifetime_bounds; }
1666 :
1667 : const std::vector<Lifetime> &get_lifetime_bounds () const
1668 : {
1669 0 : return lifetime_bounds;
1670 : }
1671 :
1672 : // Returns whether the lifetime param has an outer attribute.
1673 0 : bool has_outer_attribute () const { return !outer_attrs.empty (); }
1674 :
1675 : // Constructor
1676 10332 : LifetimeParam (Lifetime lifetime, std::vector<Lifetime> lifetime_bounds,
1677 : AST::AttrVec outer_attrs, location_t locus)
1678 10332 : : lifetime (std::move (lifetime)),
1679 10332 : lifetime_bounds (std::move (lifetime_bounds)),
1680 10332 : outer_attrs (std::move (outer_attrs)), locus (locus)
1681 10332 : {}
1682 :
1683 : std::string as_string () const override;
1684 :
1685 : void accept_vis (ASTVisitor &vis) override;
1686 :
1687 10943 : location_t get_locus () const override final { return locus; }
1688 :
1689 29174 : Kind get_kind () const override final { return Kind::Lifetime; }
1690 :
1691 : protected:
1692 : /* Use covariance to implement clone function as returning this object
1693 : * rather than base */
1694 51855 : LifetimeParam *clone_generic_param_impl () const override
1695 : {
1696 51855 : return new LifetimeParam (*this);
1697 : }
1698 : };
1699 :
1700 772745 : class AssociatedItem : public Visitable
1701 : {
1702 : protected:
1703 : // Clone function implementation as pure virtual method
1704 : virtual AssociatedItem *clone_associated_item_impl () const = 0;
1705 :
1706 : public:
1707 : virtual ~AssociatedItem () {}
1708 :
1709 653966 : std::unique_ptr<AssociatedItem> clone_associated_item () const
1710 : {
1711 653966 : return std::unique_ptr<AssociatedItem> (clone_associated_item_impl ());
1712 : }
1713 :
1714 : virtual std::string as_string () const = 0;
1715 :
1716 : virtual void mark_for_strip () = 0;
1717 : virtual bool is_marked_for_strip () const = 0;
1718 :
1719 : virtual location_t get_locus () const = 0;
1720 :
1721 : virtual NodeId get_node_id () const = 0;
1722 : };
1723 :
1724 : // Item used in trait declarations - abstract base class
1725 : class TraitItem : public AssociatedItem
1726 : {
1727 : protected:
1728 3315331 : TraitItem (location_t locus)
1729 3315331 : : node_id (Analysis::Mappings::get ().get_next_node_id ()),
1730 6630662 : vis (Visibility::create_private ()), locus (locus)
1731 3315331 : {}
1732 :
1733 1180 : TraitItem (Visibility vis, location_t locus)
1734 1180 : : node_id (Analysis::Mappings::get ().get_next_node_id ()), vis (vis),
1735 1180 : locus (locus)
1736 1180 : {}
1737 :
1738 : // Clone function implementation as pure virtual method
1739 : virtual TraitItem *clone_associated_item_impl () const override = 0;
1740 :
1741 : NodeId node_id;
1742 : Visibility vis;
1743 : location_t locus;
1744 :
1745 : public:
1746 : // Unique pointer custom clone function
1747 : std::unique_ptr<TraitItem> clone_trait_item () const
1748 : {
1749 : return std::unique_ptr<TraitItem> (clone_associated_item_impl ());
1750 : }
1751 :
1752 27756 : NodeId get_node_id () const override { return node_id; }
1753 15780 : location_t get_locus () const override { return locus; }
1754 : };
1755 :
1756 : // Abstract base class for an item used inside an extern block
1757 : class ExternalItem : public Visitable
1758 : {
1759 : public:
1760 15 : ExternalItem () : node_id (Analysis::Mappings::get ().get_next_node_id ()) {}
1761 :
1762 3995893 : ExternalItem (NodeId node_id) : node_id (node_id) {}
1763 :
1764 : virtual ~ExternalItem () {}
1765 :
1766 : // Unique pointer custom clone function
1767 29586 : std::unique_ptr<ExternalItem> clone_external_item () const
1768 : {
1769 29586 : return std::unique_ptr<ExternalItem> (clone_external_item_impl ());
1770 : }
1771 :
1772 : virtual std::string as_string () const = 0;
1773 :
1774 : virtual void mark_for_strip () = 0;
1775 : virtual bool is_marked_for_strip () const = 0;
1776 :
1777 514532 : virtual NodeId get_node_id () const { return node_id; }
1778 :
1779 : protected:
1780 : // Clone function implementation as pure virtual method
1781 : virtual ExternalItem *clone_external_item_impl () const = 0;
1782 :
1783 : NodeId node_id;
1784 : };
1785 :
1786 : /* Data structure to store the data used in macro invocations and macro
1787 : * invocations with semicolons. */
1788 : struct MacroInvocData
1789 : {
1790 : private:
1791 : SimplePath path;
1792 : DelimTokenTree token_tree;
1793 :
1794 : // One way of parsing the macro. Probably not applicable for all macros.
1795 : std::vector<std::unique_ptr<MetaItemInner>> parsed_items;
1796 : bool parsed_to_meta_item = false;
1797 : MacroExpander *expander = nullptr;
1798 :
1799 : public:
1800 : std::string as_string () const;
1801 :
1802 513927 : MacroInvocData (SimplePath path, DelimTokenTree token_tree)
1803 513927 : : path (std::move (path)), token_tree (std::move (token_tree))
1804 513927 : {}
1805 :
1806 : // Copy constructor with vector clone
1807 3313849 : MacroInvocData (const MacroInvocData &other)
1808 3313849 : : path (other.path), token_tree (other.token_tree),
1809 3313849 : parsed_to_meta_item (other.parsed_to_meta_item)
1810 : {
1811 3313849 : parsed_items.reserve (other.parsed_items.size ());
1812 3313849 : for (const auto &e : other.parsed_items)
1813 0 : parsed_items.push_back (e->clone_meta_item_inner ());
1814 3313849 : }
1815 :
1816 : // Copy assignment operator with vector clone
1817 : MacroInvocData &operator= (const MacroInvocData &other)
1818 : {
1819 : path = other.path;
1820 : token_tree = other.token_tree;
1821 : parsed_to_meta_item = other.parsed_to_meta_item;
1822 : expander = other.expander;
1823 :
1824 : parsed_items.reserve (other.parsed_items.size ());
1825 : for (const auto &e : other.parsed_items)
1826 : parsed_items.push_back (e->clone_meta_item_inner ());
1827 :
1828 : return *this;
1829 : }
1830 :
1831 : // Move constructors
1832 551297 : MacroInvocData (MacroInvocData &&other) = default;
1833 : MacroInvocData &operator= (MacroInvocData &&other) = default;
1834 :
1835 : // Invalid if path is empty, so base stripping on that.
1836 144 : void mark_for_strip () { path = SimplePath::create_empty (); }
1837 1122648 : bool is_marked_for_strip () const { return path.is_empty (); }
1838 :
1839 : // Returns whether the macro has been parsed already.
1840 727 : bool is_parsed () const { return parsed_to_meta_item; }
1841 : // TODO: update on other ways of parsing it
1842 :
1843 : // TODO: this mutable getter seems kinda dodgy
1844 1835499 : DelimTokenTree &get_delim_tok_tree () { return token_tree; }
1845 : const DelimTokenTree &get_delim_tok_tree () const { return token_tree; }
1846 :
1847 : // Set the delim token tree of a macro invocation
1848 11036 : void set_delim_tok_tree (DelimTokenTree tree) { token_tree = tree; }
1849 :
1850 : // TODO: this mutable getter seems kinda dodgy
1851 1824961 : SimplePath &get_path () { return path; }
1852 : const SimplePath &get_path () const { return path; }
1853 :
1854 473874 : void set_expander (MacroExpander *new_expander) { expander = new_expander; }
1855 21420 : MacroExpander *get_expander ()
1856 : {
1857 21420 : rust_assert (expander);
1858 21420 : return expander;
1859 : }
1860 :
1861 : void
1862 727 : set_meta_item_output (std::vector<std::unique_ptr<MetaItemInner>> new_items)
1863 : {
1864 727 : parsed_items = std::move (new_items);
1865 : }
1866 : std::vector<std::unique_ptr<MetaItemInner>> &get_meta_items ()
1867 : {
1868 : return parsed_items;
1869 : }
1870 : const std::vector<std::unique_ptr<MetaItemInner>> &get_meta_items () const
1871 : {
1872 : return parsed_items;
1873 : }
1874 : };
1875 :
1876 : class SingleASTNode : public Visitable
1877 : {
1878 : public:
1879 : enum class Kind
1880 : {
1881 : Expr,
1882 : Item,
1883 : Stmt,
1884 : Extern,
1885 : Assoc,
1886 : Type,
1887 : Pattern,
1888 : };
1889 :
1890 : private:
1891 : Kind kind;
1892 :
1893 : // FIXME make this a union
1894 : std::unique_ptr<Expr> expr;
1895 : std::unique_ptr<Item> item;
1896 : std::unique_ptr<Stmt> stmt;
1897 : std::unique_ptr<ExternalItem> external_item;
1898 : std::unique_ptr<AssociatedItem> assoc_item;
1899 : std::unique_ptr<Type> type;
1900 : std::unique_ptr<Pattern> pattern;
1901 :
1902 : public:
1903 429246 : SingleASTNode (std::unique_ptr<Expr> expr)
1904 429246 : : kind (Kind::Expr), expr (std::move (expr))
1905 : {}
1906 :
1907 70222 : SingleASTNode (std::unique_ptr<Item> item)
1908 70222 : : kind (Kind::Item), item (std::move (item))
1909 : {}
1910 :
1911 2044 : SingleASTNode (std::unique_ptr<Stmt> stmt)
1912 2044 : : kind (Kind::Stmt), stmt (std::move (stmt))
1913 : {}
1914 :
1915 3 : SingleASTNode (std::unique_ptr<ExternalItem> item)
1916 3 : : kind (Kind::Extern), external_item (std::move (item))
1917 : {}
1918 :
1919 17108 : SingleASTNode (std::unique_ptr<AssociatedItem> item)
1920 17108 : : kind (Kind::Assoc), assoc_item (std::move (item))
1921 : {}
1922 :
1923 3773 : SingleASTNode (std::unique_ptr<Type> type)
1924 3773 : : kind (Kind::Type), type (std::move (type))
1925 : {}
1926 :
1927 4 : SingleASTNode (std::unique_ptr<Pattern> pattern)
1928 4 : : kind (Kind::Pattern), pattern (std::move (pattern))
1929 : {}
1930 :
1931 : SingleASTNode (SingleASTNode const &other);
1932 :
1933 : SingleASTNode operator= (SingleASTNode const &other);
1934 :
1935 158113 : SingleASTNode (SingleASTNode &&other) = default;
1936 : SingleASTNode &operator= (SingleASTNode &&other) = default;
1937 :
1938 818070 : Kind get_kind () const { return kind; }
1939 :
1940 : std::unique_ptr<Expr> &get_expr ()
1941 : {
1942 : rust_assert (kind == Kind::Expr);
1943 : return expr;
1944 : }
1945 :
1946 0 : std::unique_ptr<Item> &get_item ()
1947 : {
1948 0 : rust_assert (kind == Kind::Item);
1949 0 : return item;
1950 : }
1951 :
1952 : std::unique_ptr<Stmt> &get_stmt ()
1953 : {
1954 : rust_assert (kind == Kind::Stmt);
1955 : return stmt;
1956 : }
1957 :
1958 : /**
1959 : * Access the inner nodes and take ownership of them.
1960 : * You can only call these functions once per node
1961 : */
1962 :
1963 3433 : std::unique_ptr<Stmt> take_stmt ()
1964 : {
1965 3433 : rust_assert (!is_error ());
1966 3433 : return std::move (stmt);
1967 : }
1968 :
1969 405259 : std::unique_ptr<Expr> take_expr ()
1970 : {
1971 405259 : rust_assert (!is_error ());
1972 405259 : return std::move (expr);
1973 : }
1974 :
1975 70221 : std::unique_ptr<Item> take_item ()
1976 : {
1977 70221 : rust_assert (!is_error ());
1978 70221 : return std::move (item);
1979 : }
1980 :
1981 3 : std::unique_ptr<ExternalItem> take_external_item ()
1982 : {
1983 3 : rust_assert (!is_error ());
1984 3 : return std::move (external_item);
1985 : }
1986 :
1987 17108 : std::unique_ptr<AssociatedItem> take_assoc_item ()
1988 : {
1989 17108 : rust_assert (!is_error ());
1990 17108 : return std::move (assoc_item);
1991 : }
1992 :
1993 3773 : std::unique_ptr<Type> take_type ()
1994 : {
1995 3773 : rust_assert (!is_error ());
1996 3773 : return std::move (type);
1997 : }
1998 :
1999 3 : std::unique_ptr<Pattern> take_pattern ()
2000 : {
2001 3 : rust_assert (!is_error ());
2002 3 : return std::move (pattern);
2003 : }
2004 :
2005 : void accept_vis (ASTVisitor &vis) override;
2006 :
2007 : bool is_error ();
2008 :
2009 : std::string as_string () const;
2010 : };
2011 :
2012 : // A crate AST object - holds all the data for a single compilation unit
2013 : struct Crate final : public GlobContainer
2014 : {
2015 : std::vector<Attribute> inner_attrs;
2016 : // dodgy spacing required here
2017 : /* TODO: is it better to have a vector of items here or a module (implicit
2018 : * top-level one)? */
2019 : std::vector<std::unique_ptr<Item>> items;
2020 :
2021 : NodeId node_id;
2022 :
2023 : public:
2024 : // Constructor
2025 5478 : Crate (std::vector<std::unique_ptr<Item>> items,
2026 : std::vector<Attribute> inner_attrs)
2027 5478 : : inner_attrs (std::move (inner_attrs)), items (std::move (items)),
2028 5478 : node_id (Analysis::Mappings::get ().get_next_node_id ())
2029 5478 : {}
2030 :
2031 : // Copy constructor with vector clone
2032 5289 : Crate (Crate const &other)
2033 5289 : : inner_attrs (other.inner_attrs), node_id (other.node_id)
2034 : {
2035 5289 : items.reserve (other.items.size ());
2036 28056 : for (const auto &e : other.items)
2037 22767 : items.push_back (e->clone_item ());
2038 5289 : }
2039 :
2040 5408 : ~Crate () = default;
2041 :
2042 : // Overloaded assignment operator with vector clone
2043 : Crate &operator= (Crate const &other)
2044 : {
2045 : inner_attrs = other.inner_attrs;
2046 : node_id = other.node_id;
2047 :
2048 : items.reserve (other.items.size ());
2049 : for (const auto &e : other.items)
2050 : items.push_back (e->clone_item ());
2051 :
2052 : return *this;
2053 : }
2054 :
2055 : // Move constructors
2056 : Crate (Crate &&other) = default;
2057 : Crate &operator= (Crate &&other) = default;
2058 :
2059 : // Get crate representation as string (e.g. for debugging).
2060 : std::string as_string () const;
2061 :
2062 : // Delete all crate information, e.g. if fails cfg.
2063 1 : void strip_crate ()
2064 : {
2065 1 : inner_attrs.clear ();
2066 1 : inner_attrs.shrink_to_fit ();
2067 :
2068 1 : items.clear ();
2069 1 : items.shrink_to_fit ();
2070 : // TODO: is this the best way to do this?
2071 1 : }
2072 :
2073 : NodeId inject_extern_crate (std::string name);
2074 : void inject_inner_attribute (Attribute attribute);
2075 :
2076 167091 : NodeId get_node_id () const { return node_id; }
2077 : const std::vector<Attribute> &get_inner_attrs () const { return inner_attrs; }
2078 129714 : std::vector<Attribute> &get_inner_attrs () { return inner_attrs; }
2079 :
2080 : std::vector<std::unique_ptr<AST::Item>> take_items ()
2081 : {
2082 : return std::move (items);
2083 : }
2084 :
2085 : void set_items (std::vector<std::unique_ptr<AST::Item>> &&new_items)
2086 : {
2087 : items = std::move (new_items);
2088 : }
2089 :
2090 5340 : GlobContainer::Kind get_glob_container_kind () const override
2091 : {
2092 5340 : return GlobContainer::Kind::Crate;
2093 : }
2094 : };
2095 :
2096 : } // namespace AST
2097 :
2098 : template <> struct CloneableDelegate<std::unique_ptr<AST::Pattern>>
2099 : {
2100 : static std::unique_ptr<AST::Pattern>
2101 1290187 : clone (const std::unique_ptr<AST::Pattern> &other)
2102 : {
2103 1290187 : if (other == nullptr)
2104 881158 : return nullptr;
2105 : else
2106 409029 : return other->clone_pattern ();
2107 : }
2108 : };
2109 :
2110 : } // namespace Rust
2111 :
2112 : namespace std {
2113 : template <> struct less<Rust::Identifier>
2114 : {
2115 : bool operator() (const Rust::Identifier &lhs,
2116 : const Rust::Identifier &rhs) const
2117 : {
2118 : return lhs.as_string () < rhs.as_string ();
2119 : }
2120 : };
2121 :
2122 : template <> struct hash<Rust::Identifier>
2123 : {
2124 : std::size_t operator() (const Rust::Identifier &k) const
2125 : {
2126 : using std::hash;
2127 : using std::size_t;
2128 : using std::string;
2129 :
2130 : return hash<string> () (k.as_string ()) ^ (hash<int> () (k.get_locus ()));
2131 : }
2132 : };
2133 :
2134 : } // namespace std
2135 :
2136 : #endif
|