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