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 | std::vector<ptr<ObjectDecl>> tempDecls;
|
---|
533 | std::vector<ptr<ObjectDecl>> returnDecls;
|
---|
534 | std::vector<ptr<Expr>> dtors;
|
---|
535 |
|
---|
536 | ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
|
---|
537 | : Expr( loc, call->result ), tempDecls(), returnDecls(), dtors() { assert( call ); }
|
---|
538 |
|
---|
539 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
540 | private:
|
---|
541 | ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
|
---|
542 | MUTATE_FRIEND
|
---|
543 | };
|
---|
544 |
|
---|
545 | /// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
|
---|
546 | class ConstructorExpr final : public Expr {
|
---|
547 | public:
|
---|
548 | ptr<Expr> callExpr;
|
---|
549 |
|
---|
550 | ConstructorExpr( const CodeLocation & loc, const Expr * call );
|
---|
551 |
|
---|
552 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
553 | private:
|
---|
554 | ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
|
---|
555 | MUTATE_FRIEND
|
---|
556 | };
|
---|
557 |
|
---|
558 | /// A C99 compound literal, e.g. `(MyType){ a, b, c }`
|
---|
559 | class CompoundLiteralExpr final : public Expr {
|
---|
560 | public:
|
---|
561 | ptr<Init> init;
|
---|
562 |
|
---|
563 | CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
|
---|
564 |
|
---|
565 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
566 | private:
|
---|
567 | CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
|
---|
568 | MUTATE_FRIEND
|
---|
569 | };
|
---|
570 |
|
---|
571 | /// A range, e.g. `3 ... 5` or `1~10`
|
---|
572 | class RangeExpr final : public Expr {
|
---|
573 | public:
|
---|
574 | ptr<Expr> low;
|
---|
575 | ptr<Expr> high;
|
---|
576 |
|
---|
577 | RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
|
---|
578 | : Expr( loc ), low( l ), high( h ) {}
|
---|
579 |
|
---|
580 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
581 | private:
|
---|
582 | RangeExpr * clone() const override { return new RangeExpr{ *this }; }
|
---|
583 | MUTATE_FRIEND
|
---|
584 | };
|
---|
585 |
|
---|
586 | /// A tuple expression before resolution, e.g. `[a, b, c]`
|
---|
587 | class UntypedTupleExpr final : public Expr {
|
---|
588 | public:
|
---|
589 | std::vector<ptr<Expr>> exprs;
|
---|
590 |
|
---|
591 | UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
|
---|
592 | : Expr( loc ), exprs( std::move(xs) ) {}
|
---|
593 |
|
---|
594 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
595 | private:
|
---|
596 | UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
|
---|
597 | MUTATE_FRIEND
|
---|
598 | };
|
---|
599 |
|
---|
600 | /// A tuple expression after resolution, e.g. `[a, b, c]`
|
---|
601 | class TupleExpr final : public Expr {
|
---|
602 | public:
|
---|
603 | std::vector<ptr<Expr>> exprs;
|
---|
604 |
|
---|
605 | TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
|
---|
606 |
|
---|
607 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
608 | private:
|
---|
609 | TupleExpr * clone() const override { return new TupleExpr{ *this }; }
|
---|
610 | MUTATE_FRIEND
|
---|
611 | };
|
---|
612 |
|
---|
613 | /// An element selection operation on a tuple value, e.g. `t.3` after analysis
|
---|
614 | class TupleIndexExpr final : public Expr {
|
---|
615 | public:
|
---|
616 | ptr<Expr> tuple;
|
---|
617 | unsigned index;
|
---|
618 |
|
---|
619 | TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
|
---|
620 |
|
---|
621 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
622 | private:
|
---|
623 | TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
|
---|
624 | MUTATE_FRIEND
|
---|
625 | };
|
---|
626 |
|
---|
627 | /// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
|
---|
628 | /// multiple-assignment: both sides of the assignment have tuple type,
|
---|
629 | /// e.g. `[a, b, c] = [d, e, f];`
|
---|
630 | /// mass-assignment: left-hand side has tuple type and right-hand side does not:
|
---|
631 | /// e.g. `[a, b, c] = 42;`
|
---|
632 | class TupleAssignExpr final : public Expr {
|
---|
633 | public:
|
---|
634 | ptr<StmtExpr> stmtExpr;
|
---|
635 |
|
---|
636 | TupleAssignExpr(
|
---|
637 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
|
---|
638 | std::vector<ptr<ObjectDecl>> && tempDecls );
|
---|
639 |
|
---|
640 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
641 |
|
---|
642 | friend class ::ConverterOldToNew;
|
---|
643 |
|
---|
644 | private:
|
---|
645 | TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
|
---|
646 | TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
|
---|
647 |
|
---|
648 | MUTATE_FRIEND
|
---|
649 | };
|
---|
650 |
|
---|
651 | /// A GCC "statement expression", e.g. `({ int x = 5; x })`
|
---|
652 | class StmtExpr final : public Expr {
|
---|
653 | public:
|
---|
654 | ptr<CompoundStmt> stmts;
|
---|
655 | std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
|
---|
656 | std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
|
---|
657 |
|
---|
658 | StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
|
---|
659 |
|
---|
660 | /// Set the result type of this StmtExpr based on its body
|
---|
661 | void computeResult();
|
---|
662 |
|
---|
663 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
664 | private:
|
---|
665 | StmtExpr * clone() const override { return new StmtExpr{ *this }; }
|
---|
666 | MUTATE_FRIEND
|
---|
667 | };
|
---|
668 |
|
---|
669 | /// An expression which must only be evaluated once
|
---|
670 | class UniqueExpr final : public Expr {
|
---|
671 | static unsigned long long nextId;
|
---|
672 | public:
|
---|
673 | ptr<Expr> expr;
|
---|
674 | ptr<ObjectDecl> object;
|
---|
675 | ptr<VariableExpr> var;
|
---|
676 | unsigned long long id;
|
---|
677 |
|
---|
678 | UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1 );
|
---|
679 |
|
---|
680 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
681 | private:
|
---|
682 | UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
|
---|
683 | MUTATE_FRIEND
|
---|
684 | };
|
---|
685 |
|
---|
686 | /// One option for resolving an initializer expression
|
---|
687 | struct InitAlternative {
|
---|
688 | ptr<Type> type;
|
---|
689 | ptr<Designation> designation;
|
---|
690 |
|
---|
691 | InitAlternative() = default;
|
---|
692 | InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
|
---|
693 | };
|
---|
694 |
|
---|
695 | /// Pre-resolution initializer expression
|
---|
696 | class UntypedInitExpr final : public Expr {
|
---|
697 | public:
|
---|
698 | ptr<Expr> expr;
|
---|
699 | std::vector<InitAlternative> initAlts;
|
---|
700 |
|
---|
701 | UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::vector<InitAlternative> && as )
|
---|
702 | : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
|
---|
703 |
|
---|
704 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
705 | private:
|
---|
706 | UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
|
---|
707 | MUTATE_FRIEND
|
---|
708 | };
|
---|
709 |
|
---|
710 | /// Post-resolution initializer expression
|
---|
711 | class InitExpr final : public Expr {
|
---|
712 | public:
|
---|
713 | ptr<Expr> expr;
|
---|
714 | ptr<Designation> designation;
|
---|
715 |
|
---|
716 | InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
|
---|
717 | : Expr( loc, e->result ), expr( e ), designation( des ) {}
|
---|
718 |
|
---|
719 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
720 | private:
|
---|
721 | InitExpr * clone() const override { return new InitExpr{ *this }; }
|
---|
722 | MUTATE_FRIEND
|
---|
723 | };
|
---|
724 |
|
---|
725 | /// Expression containing a deleted identifier.
|
---|
726 | /// Internal to resolver.
|
---|
727 | class DeletedExpr final : public Expr {
|
---|
728 | public:
|
---|
729 | ptr<Expr> expr;
|
---|
730 | readonly<Node> deleteStmt;
|
---|
731 |
|
---|
732 | DeletedExpr( const CodeLocation & loc, const Expr * e, const Node * del )
|
---|
733 | : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
|
---|
734 |
|
---|
735 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
736 | private:
|
---|
737 | DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
|
---|
738 | MUTATE_FRIEND
|
---|
739 | };
|
---|
740 |
|
---|
741 | /// Use of a default argument.
|
---|
742 | /// Internal to resolver.
|
---|
743 | class DefaultArgExpr final : public Expr {
|
---|
744 | public:
|
---|
745 | ptr<Expr> expr;
|
---|
746 |
|
---|
747 | DefaultArgExpr( const CodeLocation & loc, const Expr * e )
|
---|
748 | : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
|
---|
749 |
|
---|
750 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
751 | private:
|
---|
752 | DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
|
---|
753 | MUTATE_FRIEND
|
---|
754 | };
|
---|
755 |
|
---|
756 | /// C11 _Generic expression
|
---|
757 | class GenericExpr final : public Expr {
|
---|
758 | public:
|
---|
759 | /// One arm of the _Generic expr
|
---|
760 | struct Association {
|
---|
761 | ptr<Type> type;
|
---|
762 | ptr<Expr> expr;
|
---|
763 |
|
---|
764 | Association() = default;
|
---|
765 | // default case
|
---|
766 | Association( const Expr * e ) : type(), expr( e ) {}
|
---|
767 | // non-default case
|
---|
768 | Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
|
---|
769 | };
|
---|
770 |
|
---|
771 | ptr<Expr> control;
|
---|
772 | std::vector<Association> associations;
|
---|
773 |
|
---|
774 | GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
|
---|
775 | : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
|
---|
776 |
|
---|
777 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
778 | private:
|
---|
779 | GenericExpr * clone() const override { return new GenericExpr{ *this }; }
|
---|
780 | MUTATE_FRIEND
|
---|
781 | };
|
---|
782 |
|
---|
783 |
|
---|
784 | }
|
---|
785 |
|
---|
786 | #undef MUTATE_FRIEND
|
---|
787 |
|
---|
788 | // Local Variables: //
|
---|
789 | // tab-width: 4 //
|
---|
790 | // mode: c++ //
|
---|
791 | // compile-command: "make install" //
|
---|
792 | // End: //
|
---|