source: src/AST/Expr.hpp@ 6896548

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 6896548 was 6896548, checked in by Michael Brooks <mlbrooks@…>, 6 years ago

Fixed convert-convert issues with strings, when conversion happens after resolve. Three specific issues fixed.

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