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