source: src/AST/Expr.hpp @ 4eb43fa

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4eb43fa was 4eb43fa, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Merge branch 'new-ast' of plg.uwaterloo.ca:software/cfa/cfa-cc into new-ast

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