source: src/AST/Expr.hpp @ c36298d

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

Fixed handling of "literals.cfa" string-detail test cases by simplifying constant analysis. Now a ConstantExpr? is a minial passthrough from parser to code generator, with special-case analysis only for integer values. Awareness of how to build a string-constant type is back in ExpressionNode?.cc; now, this knowlede is only needed there. AST conversion no longer specializes string-int-float constants; it just converts types and passes values through. Unused constant API features are removed, notably from-to-float and from-string.

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