1 | // |
---|
2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo |
---|
3 | // |
---|
4 | // The contents of this file are covered under the licence agreement in the |
---|
5 | // file "LICENCE" distributed with Cforall. |
---|
6 | // |
---|
7 | // Expr.hpp -- |
---|
8 | // |
---|
9 | // Author : Aaron B. Moss |
---|
10 | // Created On : Fri May 10 10:30:00 2019 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Created On : Fri May 10 10:30:00 2019 |
---|
13 | // Update Count : 7 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <cassert> |
---|
19 | #include <deque> |
---|
20 | #include <map> |
---|
21 | #include <string> |
---|
22 | #include <utility> // for move |
---|
23 | #include <vector> |
---|
24 | #include <optional> |
---|
25 | |
---|
26 | #include "Fwd.hpp" // for UniqueId |
---|
27 | #include "Label.hpp" |
---|
28 | #include "Decl.hpp" |
---|
29 | #include "ParseNode.hpp" |
---|
30 | #include "Visitor.hpp" |
---|
31 | |
---|
32 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
33 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); |
---|
34 | |
---|
35 | class ConverterOldToNew; |
---|
36 | class ConverterNewToOld; |
---|
37 | |
---|
38 | namespace ast { |
---|
39 | |
---|
40 | /// Contains the ID of a declaration and a type that is derived from that declaration, |
---|
41 | /// but subject to decay-to-pointer and type parameter renaming |
---|
42 | struct ParamEntry { |
---|
43 | UniqueId decl; |
---|
44 | ptr<Decl> declptr; |
---|
45 | ptr<Type> actualType; |
---|
46 | ptr<Type> formalType; |
---|
47 | ptr<Expr> expr; |
---|
48 | |
---|
49 | ParamEntry() : decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {} |
---|
50 | ParamEntry( |
---|
51 | UniqueId id, const Decl * declptr, const Type * actual, const Type * formal, |
---|
52 | const Expr * e ) |
---|
53 | : decl( id ), declptr( declptr ), actualType( actual ), formalType( formal ), expr( e ) {} |
---|
54 | }; |
---|
55 | |
---|
56 | /// Pre-resolution list of parameters to infer |
---|
57 | using ResnSlots = std::vector<UniqueId>; |
---|
58 | /// Post-resolution map of inferred parameters |
---|
59 | using InferredParams = std::map< UniqueId, ParamEntry >; |
---|
60 | |
---|
61 | /// Base node for expressions |
---|
62 | class Expr : public ParseNode { |
---|
63 | public: |
---|
64 | /// Saves space (~16 bytes) by combining ResnSlots and InferredParams |
---|
65 | struct InferUnion { |
---|
66 | enum { Empty, Slots, Params } mode; |
---|
67 | union data_t { |
---|
68 | char def; |
---|
69 | ResnSlots resnSlots; |
---|
70 | InferredParams inferParams; |
---|
71 | |
---|
72 | data_t() : def('\0') {} |
---|
73 | ~data_t() {} |
---|
74 | } data; |
---|
75 | |
---|
76 | /// initializes from other InferUnion |
---|
77 | void init_from( const InferUnion& o ) { |
---|
78 | switch ( o.mode ) { |
---|
79 | case Empty: return; |
---|
80 | case Slots: new(&data.resnSlots) ResnSlots{ o.data.resnSlots }; return; |
---|
81 | case Params: new(&data.inferParams) InferredParams{ o.data.inferParams }; return; |
---|
82 | } |
---|
83 | } |
---|
84 | |
---|
85 | /// initializes from other InferUnion (move semantics) |
---|
86 | void init_from( InferUnion&& o ) { |
---|
87 | switch ( o.mode ) { |
---|
88 | case Empty: return; |
---|
89 | case Slots: new(&data.resnSlots) ResnSlots{ std::move(o.data.resnSlots) }; return; |
---|
90 | case Params: |
---|
91 | new(&data.inferParams) InferredParams{ std::move(o.data.inferParams) }; return; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | /// clears variant fields |
---|
96 | void reset() { |
---|
97 | switch( mode ) { |
---|
98 | case Empty: return; |
---|
99 | case Slots: data.resnSlots.~ResnSlots(); return; |
---|
100 | case Params: data.inferParams.~InferredParams(); return; |
---|
101 | } |
---|
102 | } |
---|
103 | |
---|
104 | InferUnion() : mode(Empty), data() {} |
---|
105 | InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); } |
---|
106 | InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); } |
---|
107 | InferUnion& operator= ( const InferUnion& ) = delete; |
---|
108 | InferUnion& operator= ( InferUnion&& ) = delete; |
---|
109 | ~InferUnion() { reset(); } |
---|
110 | |
---|
111 | ResnSlots& resnSlots() { |
---|
112 | switch (mode) { |
---|
113 | case Empty: new(&data.resnSlots) ResnSlots{}; mode = Slots; // fallthrough |
---|
114 | case Slots: return data.resnSlots; |
---|
115 | case Params: assertf(false, "Cannot return to resnSlots from Params"); abort(); |
---|
116 | } |
---|
117 | assertf(false, "unreachable"); |
---|
118 | } |
---|
119 | |
---|
120 | const ResnSlots& resnSlots() const { |
---|
121 | if (mode == Slots) { |
---|
122 | return data.resnSlots; |
---|
123 | } |
---|
124 | assertf(false, "Mode was not already resnSlots"); |
---|
125 | abort(); |
---|
126 | } |
---|
127 | |
---|
128 | InferredParams& inferParams() { |
---|
129 | switch (mode) { |
---|
130 | case Slots: data.resnSlots.~ResnSlots(); // fallthrough |
---|
131 | case Empty: new(&data.inferParams) InferredParams{}; mode = Params; // fallthrough |
---|
132 | case Params: return data.inferParams; |
---|
133 | } |
---|
134 | assertf(false, "unreachable"); |
---|
135 | } |
---|
136 | |
---|
137 | const InferredParams& inferParams() const { |
---|
138 | if (mode == Params) { |
---|
139 | return data.inferParams; |
---|
140 | } |
---|
141 | assertf(false, "Mode was not already Params"); |
---|
142 | abort(); |
---|
143 | } |
---|
144 | |
---|
145 | void set_inferParams( InferredParams && ps ) { |
---|
146 | switch(mode) { |
---|
147 | case Slots: |
---|
148 | data.resnSlots.~ResnSlots(); |
---|
149 | // fallthrough |
---|
150 | case Empty: |
---|
151 | new(&data.inferParams) InferredParams{ std::move( ps ) }; |
---|
152 | mode = Params; |
---|
153 | break; |
---|
154 | case Params: |
---|
155 | data.inferParams = std::move( ps ); |
---|
156 | break; |
---|
157 | } |
---|
158 | } |
---|
159 | |
---|
160 | /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode |
---|
161 | /// and the other is in `Params`. |
---|
162 | void splice( InferUnion && o ) { |
---|
163 | if ( o.mode == Empty ) return; |
---|
164 | if ( mode == Empty ) { init_from( o ); return; } |
---|
165 | assert( mode == o.mode && "attempt to splice incompatible InferUnion" ); |
---|
166 | |
---|
167 | if ( mode == Slots ){ |
---|
168 | data.resnSlots.insert( |
---|
169 | data.resnSlots.end(), o.data.resnSlots.begin(), o.data.resnSlots.end() ); |
---|
170 | } else if ( mode == Params ) { |
---|
171 | for ( const auto & p : o.data.inferParams ) { |
---|
172 | data.inferParams[p.first] = std::move(p.second); |
---|
173 | } |
---|
174 | } else assertf(false, "invalid mode"); |
---|
175 | } |
---|
176 | }; |
---|
177 | |
---|
178 | ptr<Type> result; |
---|
179 | ptr<TypeSubstitution> env; |
---|
180 | InferUnion inferred; |
---|
181 | bool extension = false; |
---|
182 | |
---|
183 | Expr( const CodeLocation & loc, const Type * res = nullptr ) |
---|
184 | : ParseNode( loc ), result( res ), env(), inferred() {} |
---|
185 | |
---|
186 | Expr * set_extension( bool ex ) { extension = ex; return this; } |
---|
187 | |
---|
188 | virtual const Expr * accept( Visitor & v ) const override = 0; |
---|
189 | private: |
---|
190 | Expr * clone() const override = 0; |
---|
191 | MUTATE_FRIEND |
---|
192 | }; |
---|
193 | |
---|
194 | /// The application of a function to a set of parameters. |
---|
195 | /// Post-resolver form of `UntypedExpr` |
---|
196 | class ApplicationExpr final : public Expr { |
---|
197 | public: |
---|
198 | ptr<Expr> func; |
---|
199 | std::vector<ptr<Expr>> args; |
---|
200 | |
---|
201 | ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} ); |
---|
202 | |
---|
203 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
204 | private: |
---|
205 | ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; } |
---|
206 | MUTATE_FRIEND |
---|
207 | }; |
---|
208 | |
---|
209 | /// The application of a function to a set of parameters, pre-overload resolution. |
---|
210 | class UntypedExpr final : public Expr { |
---|
211 | public: |
---|
212 | ptr<Expr> func; |
---|
213 | std::vector<ptr<Expr>> args; |
---|
214 | |
---|
215 | UntypedExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} ) |
---|
216 | : Expr( loc ), func( f ), args( std::move(as) ) {} |
---|
217 | |
---|
218 | /// Creates a new dereference expression |
---|
219 | static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg ); |
---|
220 | /// Creates a new assignment expression |
---|
221 | static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ); |
---|
222 | |
---|
223 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
224 | private: |
---|
225 | UntypedExpr * clone() const override { return new UntypedExpr{ *this }; } |
---|
226 | MUTATE_FRIEND |
---|
227 | }; |
---|
228 | |
---|
229 | /// A name whose name is as-yet undetermined. |
---|
230 | /// May also be used to avoid name mangling in codegen phase. |
---|
231 | class NameExpr final : public Expr { |
---|
232 | public: |
---|
233 | std::string name; |
---|
234 | |
---|
235 | NameExpr( const CodeLocation & loc, const std::string & n ) : Expr( loc ), name( n ) {} |
---|
236 | |
---|
237 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
238 | private: |
---|
239 | NameExpr * clone() const override { return new NameExpr{ *this }; } |
---|
240 | MUTATE_FRIEND |
---|
241 | }; |
---|
242 | |
---|
243 | /// Address-of expression `&e` |
---|
244 | class AddressExpr final : public Expr { |
---|
245 | public: |
---|
246 | ptr<Expr> arg; |
---|
247 | |
---|
248 | AddressExpr( const CodeLocation & loc, const Expr * a ); |
---|
249 | |
---|
250 | /// Generate AddressExpr wrapping given expression at same location |
---|
251 | AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {} |
---|
252 | |
---|
253 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
254 | private: |
---|
255 | AddressExpr * clone() const override { return new AddressExpr{ *this }; } |
---|
256 | MUTATE_FRIEND |
---|
257 | }; |
---|
258 | |
---|
259 | /// GCC &&label |
---|
260 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html |
---|
261 | class LabelAddressExpr final : public Expr { |
---|
262 | public: |
---|
263 | Label arg; |
---|
264 | |
---|
265 | LabelAddressExpr( const CodeLocation & loc, Label && a ); |
---|
266 | |
---|
267 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
268 | private: |
---|
269 | LabelAddressExpr * clone() const override { return new LabelAddressExpr{ *this }; } |
---|
270 | MUTATE_FRIEND |
---|
271 | }; |
---|
272 | |
---|
273 | /// Whether a cast existed in the program source or not |
---|
274 | enum GeneratedFlag { ExplicitCast, GeneratedCast }; |
---|
275 | |
---|
276 | /// A type cast, e.g. `(int)e` |
---|
277 | class CastExpr final : public Expr { |
---|
278 | public: |
---|
279 | ptr<Expr> arg; |
---|
280 | GeneratedFlag isGenerated; |
---|
281 | |
---|
282 | CastExpr( const CodeLocation & loc, const Expr * a, const Type * to, |
---|
283 | GeneratedFlag g = GeneratedCast ) : Expr( loc, to ), arg( a ), isGenerated( g ) {} |
---|
284 | /// Cast-to-void |
---|
285 | CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast ); |
---|
286 | |
---|
287 | /// Wrap a cast expression around an existing expression (always generated) |
---|
288 | CastExpr( const Expr * a, const Type * to ) : CastExpr( a->location, a, to, GeneratedCast ) {} |
---|
289 | |
---|
290 | /// Wrap a cast-to-void expression around an existing expression (always generated) |
---|
291 | CastExpr( const Expr * a ) : CastExpr( a->location, a, GeneratedCast ) {} |
---|
292 | |
---|
293 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
294 | private: |
---|
295 | CastExpr * clone() const override { return new CastExpr{ *this }; } |
---|
296 | MUTATE_FRIEND |
---|
297 | }; |
---|
298 | |
---|
299 | /// A cast to "keyword types", e.g. `(thread &)t` |
---|
300 | class KeywordCastExpr final : public Expr { |
---|
301 | public: |
---|
302 | ptr<Expr> arg; |
---|
303 | ast::AggregateDecl::Aggregate target; |
---|
304 | |
---|
305 | KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t ) |
---|
306 | : Expr( loc ), arg( a ), target( t ) {} |
---|
307 | |
---|
308 | /// Get a name for the target type |
---|
309 | const char * targetString() const; |
---|
310 | |
---|
311 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
312 | private: |
---|
313 | KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; } |
---|
314 | MUTATE_FRIEND |
---|
315 | }; |
---|
316 | |
---|
317 | /// A virtual dynamic cast, e.g. `(virtual exception)e` |
---|
318 | class VirtualCastExpr final : public Expr { |
---|
319 | public: |
---|
320 | ptr<Expr> arg; |
---|
321 | |
---|
322 | VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to ) |
---|
323 | : Expr( loc, to ), arg( a ) {} |
---|
324 | |
---|
325 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
326 | private: |
---|
327 | VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; } |
---|
328 | MUTATE_FRIEND |
---|
329 | }; |
---|
330 | |
---|
331 | /// A member selection operation before expression resolution, e.g. `q.p` |
---|
332 | class UntypedMemberExpr final : public Expr { |
---|
333 | public: |
---|
334 | ptr<Expr> member; |
---|
335 | ptr<Expr> aggregate; |
---|
336 | |
---|
337 | UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg ) |
---|
338 | : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); } |
---|
339 | |
---|
340 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
341 | private: |
---|
342 | UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; } |
---|
343 | MUTATE_FRIEND |
---|
344 | }; |
---|
345 | |
---|
346 | /// A member selection operation after expression resolution, e.g. `q.p` |
---|
347 | class MemberExpr final : public Expr { |
---|
348 | public: |
---|
349 | readonly<DeclWithType> member; |
---|
350 | ptr<Expr> aggregate; |
---|
351 | |
---|
352 | MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ); |
---|
353 | |
---|
354 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
355 | private: |
---|
356 | MemberExpr * clone() const override { return new MemberExpr{ *this }; } |
---|
357 | MUTATE_FRIEND |
---|
358 | }; |
---|
359 | |
---|
360 | /// A reference to a named variable. |
---|
361 | class VariableExpr final : public Expr { |
---|
362 | public: |
---|
363 | readonly<DeclWithType> var; |
---|
364 | |
---|
365 | VariableExpr( const CodeLocation & loc ); |
---|
366 | VariableExpr( const CodeLocation & loc, const DeclWithType * v ); |
---|
367 | |
---|
368 | /// generates a function pointer for a given function |
---|
369 | static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl ); |
---|
370 | |
---|
371 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
372 | private: |
---|
373 | VariableExpr * clone() const override { return new VariableExpr{ *this }; } |
---|
374 | MUTATE_FRIEND |
---|
375 | }; |
---|
376 | |
---|
377 | /// A compile-time constant. |
---|
378 | /// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted. |
---|
379 | /// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array. |
---|
380 | class ConstantExpr final : public Expr { |
---|
381 | public: |
---|
382 | // Representation of this constant, as it occurs in .cfa source and .cfa.cc result. |
---|
383 | std::string rep; |
---|
384 | |
---|
385 | ConstantExpr( |
---|
386 | const CodeLocation & loc, const Type * ty, const std::string & r, |
---|
387 | std::optional<unsigned long long> i ) |
---|
388 | : Expr( loc, ty ), rep( r ), ival( i ) {} |
---|
389 | |
---|
390 | /// Gets the integer value of this constant, if one is appropriate to its type. |
---|
391 | /// Throws a SemanticError if the type is not appropriate for value-as-integer. |
---|
392 | /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor. |
---|
393 | long long int intValue() const; |
---|
394 | |
---|
395 | /// generates a boolean constant of the given bool |
---|
396 | static ConstantExpr * from_bool( const CodeLocation & loc, bool b ); |
---|
397 | /// generates an integer constant of the given int |
---|
398 | static ConstantExpr * from_int( const CodeLocation & loc, int i ); |
---|
399 | /// generates an integer constant of the given unsigned long int |
---|
400 | static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i ); |
---|
401 | /// generates a null pointer value for the given type. void * if omitted. |
---|
402 | static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr ); |
---|
403 | |
---|
404 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
405 | private: |
---|
406 | ConstantExpr * clone() const override { return new ConstantExpr{ *this }; } |
---|
407 | MUTATE_FRIEND |
---|
408 | |
---|
409 | std::optional<unsigned long long> ival; |
---|
410 | |
---|
411 | // Intended only for legacy support of roundtripping the old AST. |
---|
412 | // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression. |
---|
413 | // In the old AST it's constExpr->constant.type |
---|
414 | ptr<Type> underlyer; |
---|
415 | friend class ::ConverterOldToNew; |
---|
416 | friend class ::ConverterNewToOld; |
---|
417 | }; |
---|
418 | |
---|
419 | /// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4` |
---|
420 | class SizeofExpr final : public Expr { |
---|
421 | public: |
---|
422 | ptr<Expr> expr; |
---|
423 | ptr<Type> type; |
---|
424 | |
---|
425 | SizeofExpr( const CodeLocation & loc, const Expr * e ); |
---|
426 | SizeofExpr( const CodeLocation & loc, const Type * t ); |
---|
427 | // deliberately no disambiguating overload for nullptr_t |
---|
428 | |
---|
429 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
430 | private: |
---|
431 | SizeofExpr * clone() const override { return new SizeofExpr{ *this }; } |
---|
432 | MUTATE_FRIEND |
---|
433 | }; |
---|
434 | |
---|
435 | /// alignof expression, e.g. `alignof(int)`, `alignof 3+4` |
---|
436 | class AlignofExpr final : public Expr { |
---|
437 | public: |
---|
438 | ptr<Expr> expr; |
---|
439 | ptr<Type> type; |
---|
440 | |
---|
441 | AlignofExpr( const CodeLocation & loc, const Expr * e ); |
---|
442 | AlignofExpr( const CodeLocation & loc, const Type * t ); |
---|
443 | // deliberately no disambiguating overload for nullptr_t |
---|
444 | |
---|
445 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
446 | private: |
---|
447 | AlignofExpr * clone() const override { return new AlignofExpr{ *this }; } |
---|
448 | MUTATE_FRIEND |
---|
449 | }; |
---|
450 | |
---|
451 | /// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)` |
---|
452 | class UntypedOffsetofExpr final : public Expr { |
---|
453 | public: |
---|
454 | ptr<Type> type; |
---|
455 | std::string member; |
---|
456 | |
---|
457 | UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem ) |
---|
458 | : Expr( loc ), type( ty ), member( mem ) {} |
---|
459 | |
---|
460 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
461 | private: |
---|
462 | UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; } |
---|
463 | MUTATE_FRIEND |
---|
464 | }; |
---|
465 | |
---|
466 | /// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)` |
---|
467 | class OffsetofExpr final : public Expr { |
---|
468 | public: |
---|
469 | ptr<Type> type; |
---|
470 | readonly<DeclWithType> member; |
---|
471 | |
---|
472 | OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ); |
---|
473 | |
---|
474 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
475 | private: |
---|
476 | OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; } |
---|
477 | MUTATE_FRIEND |
---|
478 | }; |
---|
479 | |
---|
480 | /// a pack of field-offsets for a generic type |
---|
481 | class OffsetPackExpr final : public Expr { |
---|
482 | public: |
---|
483 | ptr<StructInstType> type; |
---|
484 | |
---|
485 | OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ); |
---|
486 | |
---|
487 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
488 | private: |
---|
489 | OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; } |
---|
490 | MUTATE_FRIEND |
---|
491 | }; |
---|
492 | |
---|
493 | /// Variants of short-circuiting logical expression |
---|
494 | enum LogicalFlag { OrExpr, AndExpr }; |
---|
495 | |
---|
496 | /// Short-circuiting boolean expression (`&&` or `||`) |
---|
497 | class LogicalExpr final : public Expr { |
---|
498 | public: |
---|
499 | ptr<Expr> arg1; |
---|
500 | ptr<Expr> arg2; |
---|
501 | LogicalFlag isAnd; |
---|
502 | |
---|
503 | LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ); |
---|
504 | |
---|
505 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
506 | private: |
---|
507 | LogicalExpr * clone() const override { return new LogicalExpr{ *this }; } |
---|
508 | MUTATE_FRIEND |
---|
509 | }; |
---|
510 | |
---|
511 | /// Three-argument conditional e.g. `p ? a : b` |
---|
512 | class ConditionalExpr final : public Expr { |
---|
513 | public: |
---|
514 | ptr<Expr> arg1; |
---|
515 | ptr<Expr> arg2; |
---|
516 | ptr<Expr> arg3; |
---|
517 | |
---|
518 | ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 ) |
---|
519 | : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {} |
---|
520 | |
---|
521 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
522 | private: |
---|
523 | ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; } |
---|
524 | MUTATE_FRIEND |
---|
525 | }; |
---|
526 | |
---|
527 | /// Comma expression e.g. `( a , b )` |
---|
528 | class CommaExpr final : public Expr { |
---|
529 | public: |
---|
530 | ptr<Expr> arg1; |
---|
531 | ptr<Expr> arg2; |
---|
532 | |
---|
533 | CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 ) |
---|
534 | : Expr( loc ), arg1( a1 ), arg2( a2 ) {} |
---|
535 | |
---|
536 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
537 | private: |
---|
538 | CommaExpr * clone() const override { return new CommaExpr{ *this }; } |
---|
539 | MUTATE_FRIEND |
---|
540 | }; |
---|
541 | |
---|
542 | /// A type used as an expression (e.g. a type generator parameter) |
---|
543 | class TypeExpr final : public Expr { |
---|
544 | public: |
---|
545 | ptr<Type> type; |
---|
546 | |
---|
547 | TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {} |
---|
548 | |
---|
549 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
550 | private: |
---|
551 | TypeExpr * clone() const override { return new TypeExpr{ *this }; } |
---|
552 | MUTATE_FRIEND |
---|
553 | }; |
---|
554 | |
---|
555 | /// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`. |
---|
556 | /// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints |
---|
557 | class AsmExpr final : public Expr { |
---|
558 | public: |
---|
559 | std::string inout; |
---|
560 | ptr<Expr> constraint; |
---|
561 | ptr<Expr> operand; |
---|
562 | |
---|
563 | AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op ) |
---|
564 | : Expr( loc ), inout( io ), constraint( con ), operand( op ) {} |
---|
565 | |
---|
566 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
567 | private: |
---|
568 | AsmExpr * clone() const override { return new AsmExpr{ *this }; } |
---|
569 | MUTATE_FRIEND |
---|
570 | }; |
---|
571 | |
---|
572 | /// The application of a function to a set of parameters, along with a set of copy constructor |
---|
573 | /// calls, one for each argument |
---|
574 | class ImplicitCopyCtorExpr final : public Expr { |
---|
575 | public: |
---|
576 | ptr<ApplicationExpr> callExpr; |
---|
577 | |
---|
578 | ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call ) |
---|
579 | : Expr( loc, call->result ) { assert( call ); } |
---|
580 | |
---|
581 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
582 | private: |
---|
583 | ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; } |
---|
584 | MUTATE_FRIEND |
---|
585 | }; |
---|
586 | |
---|
587 | /// Constructor in expression context, e.g. `int * x = alloc() { 42 };` |
---|
588 | class ConstructorExpr final : public Expr { |
---|
589 | public: |
---|
590 | ptr<Expr> callExpr; |
---|
591 | |
---|
592 | ConstructorExpr( const CodeLocation & loc, const Expr * call ); |
---|
593 | |
---|
594 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
595 | private: |
---|
596 | ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; } |
---|
597 | MUTATE_FRIEND |
---|
598 | }; |
---|
599 | |
---|
600 | /// A C99 compound literal, e.g. `(MyType){ a, b, c }` |
---|
601 | class CompoundLiteralExpr final : public Expr { |
---|
602 | public: |
---|
603 | ptr<Init> init; |
---|
604 | |
---|
605 | CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i ); |
---|
606 | |
---|
607 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
608 | private: |
---|
609 | CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; } |
---|
610 | MUTATE_FRIEND |
---|
611 | }; |
---|
612 | |
---|
613 | /// A range, e.g. `3 ... 5` or `1~10` |
---|
614 | class RangeExpr final : public Expr { |
---|
615 | public: |
---|
616 | ptr<Expr> low; |
---|
617 | ptr<Expr> high; |
---|
618 | |
---|
619 | RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h ) |
---|
620 | : Expr( loc ), low( l ), high( h ) {} |
---|
621 | |
---|
622 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
623 | private: |
---|
624 | RangeExpr * clone() const override { return new RangeExpr{ *this }; } |
---|
625 | MUTATE_FRIEND |
---|
626 | }; |
---|
627 | |
---|
628 | /// A tuple expression before resolution, e.g. `[a, b, c]` |
---|
629 | class UntypedTupleExpr final : public Expr { |
---|
630 | public: |
---|
631 | std::vector<ptr<Expr>> exprs; |
---|
632 | |
---|
633 | UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ) |
---|
634 | : Expr( loc ), exprs( std::move(xs) ) {} |
---|
635 | |
---|
636 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
637 | private: |
---|
638 | UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; } |
---|
639 | MUTATE_FRIEND |
---|
640 | }; |
---|
641 | |
---|
642 | /// A tuple expression after resolution, e.g. `[a, b, c]` |
---|
643 | class TupleExpr final : public Expr { |
---|
644 | public: |
---|
645 | std::vector<ptr<Expr>> exprs; |
---|
646 | |
---|
647 | TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ); |
---|
648 | |
---|
649 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
650 | private: |
---|
651 | TupleExpr * clone() const override { return new TupleExpr{ *this }; } |
---|
652 | MUTATE_FRIEND |
---|
653 | }; |
---|
654 | |
---|
655 | /// An element selection operation on a tuple value, e.g. `t.3` after analysis |
---|
656 | class TupleIndexExpr final : public Expr { |
---|
657 | public: |
---|
658 | ptr<Expr> tuple; |
---|
659 | unsigned index; |
---|
660 | |
---|
661 | TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i ); |
---|
662 | |
---|
663 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
664 | private: |
---|
665 | TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; } |
---|
666 | MUTATE_FRIEND |
---|
667 | }; |
---|
668 | |
---|
669 | /// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression. |
---|
670 | /// multiple-assignment: both sides of the assignment have tuple type, |
---|
671 | /// e.g. `[a, b, c] = [d, e, f];` |
---|
672 | /// mass-assignment: left-hand side has tuple type and right-hand side does not: |
---|
673 | /// e.g. `[a, b, c] = 42;` |
---|
674 | class TupleAssignExpr final : public Expr { |
---|
675 | public: |
---|
676 | ptr<StmtExpr> stmtExpr; |
---|
677 | |
---|
678 | TupleAssignExpr( |
---|
679 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns, |
---|
680 | std::vector<ptr<ObjectDecl>> && tempDecls ); |
---|
681 | |
---|
682 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
683 | |
---|
684 | friend class ::ConverterOldToNew; |
---|
685 | |
---|
686 | private: |
---|
687 | TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; } |
---|
688 | TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s ); |
---|
689 | |
---|
690 | MUTATE_FRIEND |
---|
691 | }; |
---|
692 | |
---|
693 | /// A GCC "statement expression", e.g. `({ int x = 5; x })` |
---|
694 | class StmtExpr final : public Expr { |
---|
695 | public: |
---|
696 | ptr<CompoundStmt> stmts; |
---|
697 | std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression |
---|
698 | std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s) |
---|
699 | |
---|
700 | StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ); |
---|
701 | |
---|
702 | /// Set the result type of this StmtExpr based on its body |
---|
703 | void computeResult(); |
---|
704 | |
---|
705 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
706 | private: |
---|
707 | StmtExpr * clone() const override { return new StmtExpr{ *this }; } |
---|
708 | MUTATE_FRIEND |
---|
709 | }; |
---|
710 | |
---|
711 | /// An expression which must only be evaluated once |
---|
712 | class UniqueExpr final : public Expr { |
---|
713 | static unsigned long long nextId; |
---|
714 | public: |
---|
715 | ptr<Expr> expr; |
---|
716 | ptr<ObjectDecl> object; |
---|
717 | ptr<VariableExpr> var; |
---|
718 | unsigned long long id; |
---|
719 | |
---|
720 | UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull ); |
---|
721 | |
---|
722 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
723 | private: |
---|
724 | UniqueExpr * clone() const override { return new UniqueExpr{ *this }; } |
---|
725 | MUTATE_FRIEND |
---|
726 | }; |
---|
727 | |
---|
728 | /// One option for resolving an initializer expression |
---|
729 | struct InitAlternative { |
---|
730 | ptr<Type> type; |
---|
731 | ptr<Designation> designation; |
---|
732 | |
---|
733 | InitAlternative() = default; |
---|
734 | InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {} |
---|
735 | }; |
---|
736 | |
---|
737 | /// Pre-resolution initializer expression |
---|
738 | class UntypedInitExpr final : public Expr { |
---|
739 | public: |
---|
740 | ptr<Expr> expr; |
---|
741 | std::deque<InitAlternative> initAlts; |
---|
742 | |
---|
743 | UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as ) |
---|
744 | : Expr( loc ), expr( e ), initAlts( std::move(as) ) {} |
---|
745 | |
---|
746 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
747 | private: |
---|
748 | UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; } |
---|
749 | MUTATE_FRIEND |
---|
750 | }; |
---|
751 | |
---|
752 | /// Post-resolution initializer expression |
---|
753 | class InitExpr final : public Expr { |
---|
754 | public: |
---|
755 | ptr<Expr> expr; |
---|
756 | ptr<Designation> designation; |
---|
757 | |
---|
758 | InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des ) |
---|
759 | : Expr( loc, e->result ), expr( e ), designation( des ) {} |
---|
760 | |
---|
761 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
762 | private: |
---|
763 | InitExpr * clone() const override { return new InitExpr{ *this }; } |
---|
764 | MUTATE_FRIEND |
---|
765 | }; |
---|
766 | |
---|
767 | /// Expression containing a deleted identifier. |
---|
768 | /// Internal to resolver. |
---|
769 | class DeletedExpr final : public Expr { |
---|
770 | public: |
---|
771 | ptr<Expr> expr; |
---|
772 | readonly<Decl> deleteStmt; |
---|
773 | |
---|
774 | DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del ) |
---|
775 | : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); } |
---|
776 | |
---|
777 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
778 | private: |
---|
779 | DeletedExpr * clone() const override { return new DeletedExpr{ *this }; } |
---|
780 | MUTATE_FRIEND |
---|
781 | }; |
---|
782 | |
---|
783 | /// Use of a default argument. |
---|
784 | /// Internal to resolver. |
---|
785 | class DefaultArgExpr final : public Expr { |
---|
786 | public: |
---|
787 | ptr<Expr> expr; |
---|
788 | |
---|
789 | DefaultArgExpr( const CodeLocation & loc, const Expr * e ) |
---|
790 | : Expr( loc, e->result ), expr( e ) { assert( e->result ); } |
---|
791 | |
---|
792 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
793 | private: |
---|
794 | DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; } |
---|
795 | MUTATE_FRIEND |
---|
796 | }; |
---|
797 | |
---|
798 | /// C11 _Generic expression |
---|
799 | class GenericExpr final : public Expr { |
---|
800 | public: |
---|
801 | /// One arm of the _Generic expr |
---|
802 | struct Association { |
---|
803 | ptr<Type> type; |
---|
804 | ptr<Expr> expr; |
---|
805 | |
---|
806 | Association() = default; |
---|
807 | // default case |
---|
808 | Association( const Expr * e ) : type(), expr( e ) {} |
---|
809 | // non-default case |
---|
810 | Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {} |
---|
811 | }; |
---|
812 | |
---|
813 | ptr<Expr> control; |
---|
814 | std::vector<Association> associations; |
---|
815 | |
---|
816 | GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns ) |
---|
817 | : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {} |
---|
818 | |
---|
819 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
820 | private: |
---|
821 | GenericExpr * clone() const override { return new GenericExpr{ *this }; } |
---|
822 | MUTATE_FRIEND |
---|
823 | }; |
---|
824 | |
---|
825 | |
---|
826 | } |
---|
827 | |
---|
828 | #undef MUTATE_FRIEND |
---|
829 | |
---|
830 | // Local Variables: // |
---|
831 | // tab-width: 4 // |
---|
832 | // mode: c++ // |
---|
833 | // compile-command: "make install" // |
---|
834 | // End: // |
---|