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