source: src/AST/Expr.hpp @ d5f4613

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since d5f4613 was 07d867b, checked in by Fangren Yu <f37yu@…>, 4 years ago

attempt to fix inferparams

  • Property mode set to 100644
File size: 26.3 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        ast::AggregateDecl::Aggregate target;
315
316        KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
317        : Expr( loc ), arg( a ), target( t ) {}
318
319        /// Get a name for the target type
320        const char * targetString() const;
321
322        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
323private:
324        KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
325        MUTATE_FRIEND
326};
327
328/// A virtual dynamic cast, e.g. `(virtual exception)e`
329class VirtualCastExpr final : public Expr {
330public:
331        ptr<Expr> arg;
332
333        VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to )
334        : Expr( loc, to ), arg( a ) {}
335
336        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
337private:
338        VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; }
339        MUTATE_FRIEND
340};
341
342/// A member selection operation before expression resolution, e.g. `q.p`
343class UntypedMemberExpr final : public Expr {
344public:
345        ptr<Expr> member;
346        ptr<Expr> aggregate;
347
348        UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg )
349        : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); }
350
351        bool get_lvalue() const final;
352
353        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
354private:
355        UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
356        MUTATE_FRIEND
357};
358
359/// A member selection operation after expression resolution, e.g. `q.p`
360class MemberExpr final : public Expr {
361public:
362        readonly<DeclWithType> member;
363        ptr<Expr> aggregate;
364
365        MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg );
366
367        bool get_lvalue() const final;
368
369        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
370private:
371        MemberExpr * clone() const override { return new MemberExpr{ *this }; }
372        MUTATE_FRIEND
373
374        // Custructor overload meant only for AST conversion
375        enum NoOpConstruction { NoOpConstructionChosen };
376        MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg,
377            NoOpConstruction overloadSelector );
378        friend class ::ConverterOldToNew;
379        friend class ::ConverterNewToOld;
380};
381
382/// A reference to a named variable.
383class VariableExpr final : public Expr {
384public:
385        readonly<DeclWithType> var;
386
387        VariableExpr( const CodeLocation & loc );
388        VariableExpr( const CodeLocation & loc, const DeclWithType * v );
389
390        bool get_lvalue() const final;
391
392        /// generates a function pointer for a given function
393        static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl );
394
395        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
396private:
397        VariableExpr * clone() const override { return new VariableExpr{ *this }; }
398        MUTATE_FRIEND
399};
400
401/// A compile-time constant.
402/// Mostly carries C-source text from parse to code-gen, without interpretation.  E.g. strings keep their outer quotes and never have backslashes interpreted.
403/// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
404class ConstantExpr final : public Expr {
405public:
406        // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
407        std::string rep;
408
409        ConstantExpr(
410                const CodeLocation & loc, const Type * ty, const std::string & r,
411                        std::optional<unsigned long long> i )
412        : Expr( loc, ty ), rep( r ), ival( i ) {}
413
414        /// Gets the integer value of this constant, if one is appropriate to its type.
415        /// Throws a SemanticError if the type is not appropriate for value-as-integer.
416        /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
417        long long int intValue() const;
418
419        /// generates a boolean constant of the given bool
420        static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
421        /// generates an integer constant of the given int
422        static ConstantExpr * from_int( const CodeLocation & loc, int i );
423        /// generates an integer constant of the given unsigned long int
424        static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
425        /// generates a null pointer value for the given type. void * if omitted.
426        static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
427
428        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
429private:
430        ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
431        MUTATE_FRIEND
432
433        std::optional<unsigned long long> ival;
434
435        // Intended only for legacy support of roundtripping the old AST.
436        // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
437        // In the old AST it's constExpr->constant.type
438        ptr<Type> underlyer;
439        friend class ::ConverterOldToNew;
440        friend class ::ConverterNewToOld;
441};
442
443/// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
444class SizeofExpr final : public Expr {
445public:
446        ptr<Expr> expr;
447        ptr<Type> type;
448
449        SizeofExpr( const CodeLocation & loc, const Expr * e );
450        SizeofExpr( const CodeLocation & loc, const Type * t );
451        // deliberately no disambiguating overload for nullptr_t
452
453        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
454private:
455        SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
456        MUTATE_FRIEND
457};
458
459/// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
460class AlignofExpr final : public Expr {
461public:
462        ptr<Expr> expr;
463        ptr<Type> type;
464
465        AlignofExpr( const CodeLocation & loc, const Expr * e );
466        AlignofExpr( const CodeLocation & loc, const Type * t );
467        // deliberately no disambiguating overload for nullptr_t
468
469        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
470private:
471        AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
472        MUTATE_FRIEND
473};
474
475/// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
476class UntypedOffsetofExpr final : public Expr {
477public:
478        ptr<Type> type;
479        std::string member;
480
481        UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
482        : Expr( loc ), type( ty ), member( mem ) {}
483
484        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
485private:
486        UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
487        MUTATE_FRIEND
488};
489
490/// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
491class OffsetofExpr final : public Expr {
492public:
493        ptr<Type> type;
494        readonly<DeclWithType> member;
495
496        OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
497
498        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
499private:
500        OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
501        MUTATE_FRIEND
502};
503
504/// a pack of field-offsets for a generic type
505class OffsetPackExpr final : public Expr {
506public:
507        ptr<StructInstType> type;
508
509        OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
510
511        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
512private:
513        OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
514        MUTATE_FRIEND
515};
516
517/// Variants of short-circuiting logical expression
518enum LogicalFlag { OrExpr, AndExpr };
519
520/// Short-circuiting boolean expression (`&&` or `||`)
521class LogicalExpr final : public Expr {
522public:
523        ptr<Expr> arg1;
524        ptr<Expr> arg2;
525        LogicalFlag isAnd;
526
527        LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
528
529        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
530private:
531        LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
532        MUTATE_FRIEND
533};
534
535/// Three-argument conditional e.g. `p ? a : b`
536class ConditionalExpr final : public Expr {
537public:
538        ptr<Expr> arg1;
539        ptr<Expr> arg2;
540        ptr<Expr> arg3;
541
542        ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
543        : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
544
545        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
546private:
547        ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
548        MUTATE_FRIEND
549};
550
551/// Comma expression e.g. `( a , b )`
552class CommaExpr final : public Expr {
553public:
554        ptr<Expr> arg1;
555        ptr<Expr> arg2;
556
557        CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
558        : Expr( loc ), arg1( a1 ), arg2( a2 ) {
559                this->result = a2->result;
560        }
561
562        bool get_lvalue() const final;
563
564        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
565private:
566        CommaExpr * clone() const override { return new CommaExpr{ *this }; }
567        MUTATE_FRIEND
568};
569
570/// A type used as an expression (e.g. a type generator parameter)
571class TypeExpr final : public Expr {
572public:
573        ptr<Type> type;
574
575        TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
576
577        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
578private:
579        TypeExpr * clone() const override { return new TypeExpr{ *this }; }
580        MUTATE_FRIEND
581};
582
583/// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
584/// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
585class AsmExpr final : public Expr {
586public:
587        std::string inout;
588        ptr<Expr> constraint;
589        ptr<Expr> operand;
590
591        AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
592        : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
593
594        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
595private:
596        AsmExpr * clone() const override { return new AsmExpr{ *this }; }
597        MUTATE_FRIEND
598};
599
600/// The application of a function to a set of parameters, along with a set of copy constructor
601/// calls, one for each argument
602class ImplicitCopyCtorExpr final : public Expr {
603public:
604        ptr<ApplicationExpr> callExpr;
605
606        ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
607        : Expr( loc, call->result ) { assert( call ); }
608
609        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
610private:
611        ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
612        MUTATE_FRIEND
613};
614
615/// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
616class ConstructorExpr final : public Expr {
617public:
618        ptr<Expr> callExpr;
619
620        ConstructorExpr( const CodeLocation & loc, const Expr * call );
621
622        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
623private:
624        ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
625        MUTATE_FRIEND
626};
627
628/// A C99 compound literal, e.g. `(MyType){ a, b, c }`
629class CompoundLiteralExpr final : public Expr {
630public:
631        ptr<Init> init;
632
633        CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
634
635        bool get_lvalue() const final;
636
637        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
638private:
639        CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
640        MUTATE_FRIEND
641};
642
643/// A range, e.g. `3 ... 5` or `1~10`
644class RangeExpr final : public Expr {
645public:
646        ptr<Expr> low;
647        ptr<Expr> high;
648
649        RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
650        : Expr( loc ), low( l ), high( h ) {}
651
652        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
653private:
654        RangeExpr * clone() const override { return new RangeExpr{ *this }; }
655        MUTATE_FRIEND
656};
657
658/// A tuple expression before resolution, e.g. `[a, b, c]`
659class UntypedTupleExpr final : public Expr {
660public:
661        std::vector<ptr<Expr>> exprs;
662
663        UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
664        : Expr( loc ), exprs( std::move(xs) ) {}
665
666        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
667private:
668        UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
669        MUTATE_FRIEND
670};
671
672/// A tuple expression after resolution, e.g. `[a, b, c]`
673class TupleExpr final : public Expr {
674public:
675        std::vector<ptr<Expr>> exprs;
676
677        TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
678
679        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
680private:
681        TupleExpr * clone() const override { return new TupleExpr{ *this }; }
682        MUTATE_FRIEND
683};
684
685/// An element selection operation on a tuple value, e.g. `t.3` after analysis
686class TupleIndexExpr final : public Expr {
687public:
688        ptr<Expr> tuple;
689        unsigned index;
690
691        TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
692
693        bool get_lvalue() const final;
694
695        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
696private:
697        TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
698        MUTATE_FRIEND
699};
700
701/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
702/// multiple-assignment: both sides of the assignment have tuple type,
703///     e.g. `[a, b, c] = [d, e, f];`
704/// mass-assignment: left-hand side has tuple type and right-hand side does not:
705///     e.g. `[a, b, c] = 42;`
706class TupleAssignExpr final : public Expr {
707public:
708        ptr<StmtExpr> stmtExpr;
709
710        TupleAssignExpr(
711                const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
712                std::vector<ptr<ObjectDecl>> && tempDecls );
713
714        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
715
716        friend class ::ConverterOldToNew;
717
718private:
719        TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
720    TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
721
722        MUTATE_FRIEND
723};
724
725/// A GCC "statement expression", e.g. `({ int x = 5; x })`
726class StmtExpr final : public Expr {
727public:
728        ptr<CompoundStmt> stmts;
729        std::vector<ptr<ObjectDecl>> returnDecls;  ///< return variable(s) for statement expression
730        std::vector<ptr<Expr>> dtors;              ///< destructor(s) for return variable(s)
731
732        StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
733
734        /// Set the result type of this StmtExpr based on its body
735        void computeResult();
736
737        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
738private:
739        StmtExpr * clone() const override { return new StmtExpr{ *this }; }
740        MUTATE_FRIEND
741};
742
743/// An expression which must only be evaluated once
744class UniqueExpr final : public Expr {
745        static unsigned long long nextId;
746public:
747        ptr<Expr> expr;
748        ptr<ObjectDecl> object;
749        ptr<VariableExpr> var;
750        unsigned long long id;
751
752        UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
753
754        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
755private:
756        UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
757        MUTATE_FRIEND
758};
759
760/// One option for resolving an initializer expression
761struct InitAlternative {
762        ptr<Type> type;
763        ptr<Designation> designation;
764
765        InitAlternative() = default;
766        InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
767};
768
769/// Pre-resolution initializer expression
770class UntypedInitExpr final : public Expr {
771public:
772        ptr<Expr> expr;
773        std::deque<InitAlternative> initAlts;
774
775        UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
776        : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
777
778        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
779private:
780        UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
781        MUTATE_FRIEND
782};
783
784/// Post-resolution initializer expression
785class InitExpr final : public Expr {
786public:
787        ptr<Expr> expr;
788        ptr<Designation> designation;
789
790        InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
791        : Expr( loc, e->result ), expr( e ), designation( des ) {}
792
793        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
794private:
795        InitExpr * clone() const override { return new InitExpr{ *this }; }
796        MUTATE_FRIEND
797};
798
799/// Expression containing a deleted identifier.
800/// Internal to resolver.
801class DeletedExpr final : public Expr {
802public:
803        ptr<Expr> expr;
804        readonly<Decl> deleteStmt;
805
806        DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
807        : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
808
809        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
810private:
811        DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
812        MUTATE_FRIEND
813};
814
815/// Use of a default argument.
816/// Internal to resolver.
817class DefaultArgExpr final : public Expr {
818public:
819        ptr<Expr> expr;
820
821        DefaultArgExpr( const CodeLocation & loc, const Expr * e )
822        : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
823
824        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
825private:
826        DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
827        MUTATE_FRIEND
828};
829
830/// C11 _Generic expression
831class GenericExpr final : public Expr {
832public:
833        /// One arm of the _Generic expr
834        struct Association {
835                ptr<Type> type;
836                ptr<Expr> expr;
837
838                Association() = default;
839                // default case
840                Association( const Expr * e ) : type(), expr( e ) {}
841                // non-default case
842                Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
843        };
844
845        ptr<Expr> control;
846        std::vector<Association> associations;
847
848        GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
849        : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
850
851        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
852private:
853        GenericExpr * clone() const override { return new GenericExpr{ *this }; }
854        MUTATE_FRIEND
855};
856
857
858}
859
860#undef MUTATE_FRIEND
861
862// Local Variables: //
863// tab-width: 4 //
864// mode: c++ //
865// compile-command: "make install" //
866// End: //
Note: See TracBrowser for help on using the repository browser.