source: src/AST/Expr.hpp@ 8c0d801

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 8c0d801 was 546e712, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Fix for 1 bug of N

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