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