source: src/AST/Expr.hpp@ 90ce35aa

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

Added setting of result in Comma expression.
Added asserts in candidate finder to catch null pointers earlier.
ForAll substituter now properly uses ShallowCopy.
Added missing makefile.in

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