source: src/AST/Expr.hpp @ 99da267

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 99da267 was 99da267, checked in by Michael Brooks <mlbrooks@…>, 5 years ago

Running a deep-copy on FunctionType? at RenameVars? time. This manual action addresses the currently-problematic occurrence of 'the transitivity problem.' Resolution of bootloader (and thus builtins) is now completing. The change on RenameVars?.cc makes this happen. Changes on AST/*.hpp finish making the deep-copy framework compile.

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