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