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