source: src/AST/Expr.hpp@ fce4e31

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 fce4e31 was 7870799, checked in by Thierry Delisle <tdelisle@…>, 6 years ago

Cast cost and conversion cost now take constant parameters.
This required supporting visiting const node.
The PassVisitor can now visit const nodes but not when using the Indexer

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