source: src/AST/Expr.hpp @ 234b1cb

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 234b1cb was b69233a, checked in by Aaron Moss <a3moss@…>, 5 years ago

Port assertion satisfaction to new AST

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