Branch data Line data Source code
1 : : // Copyright (C) 2020-2024 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 DERIVE_VISITOR_H
20 : : #define DERIVE_VISITOR_H
21 : :
22 : : #include "rust-ast-full.h"
23 : : #include "rust-ast-visitor.h"
24 : : #include "rust-ast-builder.h"
25 : : #include "rust-macro-builtins.h"
26 : :
27 : : namespace Rust {
28 : : namespace AST {
29 : :
30 : : /**
31 : : * The goal of this class is to accumulate and create the required items from a
32 : : * builtin `#[derive]` macro applied on a struct, enum or union.
33 : : */
34 : : class DeriveVisitor : public AST::ASTVisitor
35 : : {
36 : : public:
37 : : static std::unique_ptr<Item> derive (Item &item, const Attribute &derive,
38 : : BuiltinMacro to_derive);
39 : :
40 : : protected:
41 : : DeriveVisitor (location_t loc);
42 : :
43 : : location_t loc;
44 : : Builder builder;
45 : :
46 : : private:
47 : : // the 4 "allowed" visitors, which a derive-visitor can specify and override
48 : : virtual void visit_struct (StructStruct &struct_item) = 0;
49 : : virtual void visit_tuple (TupleStruct &tuple_item) = 0;
50 : : virtual void visit_enum (Enum &enum_item) = 0;
51 : : virtual void visit_union (Union &enum_item) = 0;
52 : :
53 : : // all visitors are final, so no deriving class can implement `derive` for
54 : : // anything other than structs, tuples, enums and unions
55 : :
56 : 15 : virtual void visit (StructStruct &struct_item) override final
57 : : {
58 : 15 : visit_struct (struct_item);
59 : 15 : }
60 : :
61 : 7 : virtual void visit (TupleStruct &tuple_struct) override final
62 : : {
63 : 7 : visit_tuple (tuple_struct);
64 : 7 : }
65 : :
66 : 0 : virtual void visit (Enum &enum_item) override final
67 : : {
68 : 0 : visit_enum (enum_item);
69 : 0 : }
70 : :
71 : 2 : virtual void visit (Union &union_item) override final
72 : : {
73 : 2 : visit_union (union_item);
74 : 2 : }
75 : :
76 : 0 : virtual void visit (Token &tok) override final{};
77 : 0 : virtual void visit (DelimTokenTree &delim_tok_tree) override final{};
78 : 0 : virtual void visit (AttrInputMetaItemContainer &input) override final{};
79 : 0 : virtual void visit (AttrInputMacro &expr) override final{};
80 : 0 : virtual void visit (IdentifierExpr &ident_expr) override final{};
81 : 0 : virtual void visit (Lifetime &lifetime) override final{};
82 : 0 : virtual void visit (LifetimeParam &lifetime_param) override final{};
83 : 0 : virtual void visit (ConstGenericParam &const_param) override final{};
84 : 0 : virtual void visit (PathInExpression &path) override final{};
85 : 0 : virtual void visit (TypePathSegment &segment) override final{};
86 : 0 : virtual void visit (TypePathSegmentGeneric &segment) override final{};
87 : 0 : virtual void visit (TypePathSegmentFunction &segment) override final{};
88 : 0 : virtual void visit (TypePath &path) override final{};
89 : 0 : virtual void visit (QualifiedPathInExpression &path) override final{};
90 : 0 : virtual void visit (QualifiedPathInType &path) override final{};
91 : 0 : virtual void visit (LiteralExpr &expr) override final{};
92 : 0 : virtual void visit (AttrInputLiteral &attr_input) override final{};
93 : 0 : virtual void visit (MetaItemLitExpr &meta_item) override final{};
94 : 0 : virtual void visit (MetaItemPathLit &meta_item) override final{};
95 : 0 : virtual void visit (BorrowExpr &expr) override final{};
96 : 0 : virtual void visit (DereferenceExpr &expr) override final{};
97 : 0 : virtual void visit (ErrorPropagationExpr &expr) override final{};
98 : 0 : virtual void visit (NegationExpr &expr) override final{};
99 : 0 : virtual void visit (ArithmeticOrLogicalExpr &expr) override final{};
100 : 0 : virtual void visit (ComparisonExpr &expr) override final{};
101 : 0 : virtual void visit (LazyBooleanExpr &expr) override final{};
102 : 0 : virtual void visit (TypeCastExpr &expr) override final{};
103 : 0 : virtual void visit (AssignmentExpr &expr) override final{};
104 : 0 : virtual void visit (CompoundAssignmentExpr &expr) override final{};
105 : 0 : virtual void visit (GroupedExpr &expr) override final{};
106 : 0 : virtual void visit (ArrayElemsValues &elems) override final{};
107 : 0 : virtual void visit (ArrayElemsCopied &elems) override final{};
108 : 0 : virtual void visit (ArrayExpr &expr) override final{};
109 : 0 : virtual void visit (ArrayIndexExpr &expr) override final{};
110 : 0 : virtual void visit (TupleExpr &expr) override final{};
111 : 0 : virtual void visit (TupleIndexExpr &expr) override final{};
112 : 0 : virtual void visit (StructExprStruct &expr) override final{};
113 : 0 : virtual void visit (StructExprFieldIdentifier &field) override final{};
114 : 0 : virtual void visit (StructExprFieldIdentifierValue &field) override final{};
115 : 0 : virtual void visit (StructExprFieldIndexValue &field) override final{};
116 : 0 : virtual void visit (StructExprStructFields &expr) override final{};
117 : 0 : virtual void visit (StructExprStructBase &expr) override final{};
118 : 0 : virtual void visit (CallExpr &expr) override final{};
119 : 0 : virtual void visit (MethodCallExpr &expr) override final{};
120 : 0 : virtual void visit (FieldAccessExpr &expr) override final{};
121 : 0 : virtual void visit (ClosureExprInner &expr) override final{};
122 : 0 : virtual void visit (BlockExpr &expr) override final{};
123 : 0 : virtual void visit (ClosureExprInnerTyped &expr) override final{};
124 : 0 : virtual void visit (ContinueExpr &expr) override final{};
125 : 0 : virtual void visit (BreakExpr &expr) override final{};
126 : 0 : virtual void visit (RangeFromToExpr &expr) override final{};
127 : 0 : virtual void visit (RangeFromExpr &expr) override final{};
128 : 0 : virtual void visit (RangeToExpr &expr) override final{};
129 : 0 : virtual void visit (RangeFullExpr &expr) override final{};
130 : 0 : virtual void visit (RangeFromToInclExpr &expr) override final{};
131 : 0 : virtual void visit (RangeToInclExpr &expr) override final{};
132 : 0 : virtual void visit (ReturnExpr &expr) override final{};
133 : 0 : virtual void visit (UnsafeBlockExpr &expr) override final{};
134 : 0 : virtual void visit (LoopExpr &expr) override final{};
135 : 0 : virtual void visit (WhileLoopExpr &expr) override final{};
136 : 0 : virtual void visit (WhileLetLoopExpr &expr) override final{};
137 : 0 : virtual void visit (ForLoopExpr &expr) override final{};
138 : 0 : virtual void visit (IfExpr &expr) override final{};
139 : 0 : virtual void visit (IfExprConseqElse &expr) override final{};
140 : 0 : virtual void visit (IfLetExpr &expr) override final{};
141 : 0 : virtual void visit (IfLetExprConseqElse &expr) override final{};
142 : 0 : virtual void visit (MatchExpr &expr) override final{};
143 : 0 : virtual void visit (AwaitExpr &expr) override final{};
144 : 0 : virtual void visit (AsyncBlockExpr &expr) override final{};
145 : 0 : virtual void visit (TypeParam ¶m) override final{};
146 : 0 : virtual void visit (LifetimeWhereClauseItem &item) override final{};
147 : 0 : virtual void visit (TypeBoundWhereClauseItem &item) override final{};
148 : 0 : virtual void visit (Module &module) override final{};
149 : 0 : virtual void visit (ExternCrate &crate) override final{};
150 : 0 : virtual void visit (UseTreeGlob &use_tree) override final{};
151 : 0 : virtual void visit (UseTreeList &use_tree) override final{};
152 : 0 : virtual void visit (UseTreeRebind &use_tree) override final{};
153 : 0 : virtual void visit (UseDeclaration &use_decl) override final{};
154 : 0 : virtual void visit (Function &function) override final{};
155 : 0 : virtual void visit (TypeAlias &type_alias) override final{};
156 : 0 : virtual void visit (EnumItem &item) override final{};
157 : 0 : virtual void visit (EnumItemTuple &item) override final{};
158 : 0 : virtual void visit (EnumItemStruct &item) override final{};
159 : 0 : virtual void visit (EnumItemDiscriminant &item) override final{};
160 : 0 : virtual void visit (ConstantItem &const_item) override final{};
161 : 0 : virtual void visit (StaticItem &static_item) override final{};
162 : 0 : virtual void visit (TraitItemConst &item) override final{};
163 : 0 : virtual void visit (TraitItemType &item) override final{};
164 : 0 : virtual void visit (Trait &trait) override final{};
165 : 0 : virtual void visit (InherentImpl &impl) override final{};
166 : 0 : virtual void visit (TraitImpl &impl) override final{};
167 : 0 : virtual void visit (ExternalTypeItem &type) override final{};
168 : 0 : virtual void visit (ExternalStaticItem &item) override final{};
169 : 0 : virtual void visit (ExternBlock &block) override final{};
170 : 0 : virtual void visit (MacroMatchFragment &match) override final{};
171 : 0 : virtual void visit (MacroMatchRepetition &match) override final{};
172 : 0 : virtual void visit (MacroMatcher &matcher) override final{};
173 : 0 : virtual void visit (MacroRulesDefinition &rules_def) override final{};
174 : 0 : virtual void visit (MacroInvocation ¯o_invoc) override final{};
175 : 0 : virtual void visit (MetaItemPath &meta_item) override final{};
176 : 0 : virtual void visit (MetaItemSeq &meta_item) override final{};
177 : 0 : virtual void visit (MetaWord &meta_item) override final{};
178 : 0 : virtual void visit (MetaNameValueStr &meta_item) override final{};
179 : 0 : virtual void visit (MetaListPaths &meta_item) override final{};
180 : 0 : virtual void visit (MetaListNameValueStr &meta_item) override final{};
181 : 0 : virtual void visit (LiteralPattern &pattern) override final{};
182 : 0 : virtual void visit (IdentifierPattern &pattern) override final{};
183 : 0 : virtual void visit (WildcardPattern &pattern) override final{};
184 : 0 : virtual void visit (RestPattern &pattern) override final{};
185 : 0 : virtual void visit (RangePatternBoundLiteral &bound) override final{};
186 : 0 : virtual void visit (RangePatternBoundPath &bound) override final{};
187 : 0 : virtual void visit (RangePatternBoundQualPath &bound) override final{};
188 : 0 : virtual void visit (RangePattern &pattern) override final{};
189 : 0 : virtual void visit (ReferencePattern &pattern) override final{};
190 : 0 : virtual void visit (StructPatternFieldTuplePat &field) override final{};
191 : 0 : virtual void visit (StructPatternFieldIdentPat &field) override final{};
192 : 0 : virtual void visit (StructPatternFieldIdent &field) override final{};
193 : 0 : virtual void visit (StructPattern &pattern) override final{};
194 : 0 : virtual void visit (TupleStructItemsNoRange &tuple_items) override final{};
195 : 0 : virtual void visit (TupleStructItemsRange &tuple_items) override final{};
196 : 0 : virtual void visit (TupleStructPattern &pattern) override final{};
197 : 0 : virtual void visit (TuplePatternItemsMultiple &tuple_items) override final{};
198 : 0 : virtual void visit (TuplePatternItemsRanged &tuple_items) override final{};
199 : 0 : virtual void visit (TuplePattern &pattern) override final{};
200 : 0 : virtual void visit (GroupedPattern &pattern) override final{};
201 : 0 : virtual void visit (SlicePattern &pattern) override final{};
202 : 0 : virtual void visit (AltPattern &pattern) override final{};
203 : 0 : virtual void visit (EmptyStmt &stmt) override final{};
204 : 0 : virtual void visit (LetStmt &stmt) override final{};
205 : 0 : virtual void visit (ExprStmt &stmt) override final{};
206 : 0 : virtual void visit (TraitBound &bound) override final{};
207 : 0 : virtual void visit (ImplTraitType &type) override final{};
208 : 0 : virtual void visit (TraitObjectType &type) override final{};
209 : 0 : virtual void visit (ParenthesisedType &type) override final{};
210 : 0 : virtual void visit (ImplTraitTypeOneBound &type) override final{};
211 : 0 : virtual void visit (TraitObjectTypeOneBound &type) override final{};
212 : 0 : virtual void visit (TupleType &type) override final{};
213 : 0 : virtual void visit (NeverType &type) override final{};
214 : 0 : virtual void visit (RawPointerType &type) override final{};
215 : 0 : virtual void visit (ReferenceType &type) override final{};
216 : 0 : virtual void visit (ArrayType &type) override final{};
217 : 0 : virtual void visit (SliceType &type) override final{};
218 : 0 : virtual void visit (InferredType &type) override final{};
219 : 0 : virtual void visit (BareFunctionType &type) override final{};
220 : 0 : virtual void visit (SelfParam ¶m) override final{};
221 : 0 : virtual void visit (FunctionParam ¶m) override final{};
222 : 0 : virtual void visit (VariadicParam ¶m) override final{};
223 : 0 : virtual void visit (FormatArgs ¶m) override final{};
224 : : };
225 : :
226 : : } // namespace AST
227 : : } // namespace Rust
228 : :
229 : : #endif // DERIVE_VISITOR_H
|