source: src/AST/Expr.hpp @ 722c4831

ADTarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 722c4831 was 20de6fb, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

finished draft past of converting expressions

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