source: src/AST/Expr.hpp @ 46da46b

ast-experimental
Last change on this file since 46da46b was 46da46b, checked in by Fangren Yu <f37yu@…>, 13 months ago

current progress

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