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

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

Implemented KeywordCast? in CandidateFinder? of new AST.

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