source: src/AST/Expr.hpp @ c02cef1

ADTast-experimental
Last change on this file since c02cef1 was b0d9ff7, checked in by JiadaL <j82liang@…>, 21 months ago

Fix up the QualifiedNameExpr?. It should now work on both old AST and new AST. There are some known bugs to fix so make all-tests will fail.

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