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
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        operator bool() {return declptr;}
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:
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         */
75        struct InferUnion {
76                // mode is now unused
77                enum { Empty, Slots, Params } mode;
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                        }
89                } data;
90
91                /// initializes from other InferUnion
92                void init_from( const InferUnion& o ) {
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);
98                        }
99                }
100
101                /// initializes from other InferUnion (move semantics)
102                void init_from( InferUnion&& o ) {
103                        data.resnSlots = o.data.resnSlots;
104                        data.inferParams = o.data.inferParams;
105                        o.data.resnSlots = nullptr;
106                        o.data.inferParams = nullptr;
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;
114
115                bool hasSlots() const { return data.resnSlots; }
116                bool hasParams() const { return data.inferParams; }
117
118                ResnSlots& resnSlots() {
119                        if (!data.resnSlots) {
120                                data.resnSlots = new ResnSlots();
121                        }
122                        return *data.resnSlots;
123                }
124
125                const ResnSlots& resnSlots() const {
126                        if (data.resnSlots) {
127                                return *data.resnSlots;
128                        }
129                        assertf(false, "Mode was not already resnSlots");
130                        abort();
131                }
132
133                InferredParams& inferParams() {
134                        if (!data.inferParams) {
135                                data.inferParams = new InferredParams();
136                        }
137                        return *data.inferParams;
138                }
139
140                const InferredParams& inferParams() const {
141                        if (data.inferParams) {
142                                return *data.inferParams;
143                        }
144                        assertf(false, "Mode was not already Params");
145                        abort();
146                }
147
148                void set_inferParams( InferredParams * ps ) {
149                        delete data.resnSlots;
150                        data.resnSlots = nullptr;
151                        delete data.inferParams;
152                        data.inferParams = ps;
153                }
154
155                /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode
156                /// and the other is in `Params`.
157                void splice( InferUnion && o ) {
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;
166                                }
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                        }
182                }
183        };
184
185        ptr<Type> result;
186        ptr<TypeSubstitution> env;
187        InferUnion inferred;
188        bool extension = false;
189
190        Expr( const CodeLocation & loc, const Type * res = nullptr )
191        : ParseNode( loc ), result( res ), env(), inferred() {}
192
193        Expr * set_extension( bool ex ) { extension = ex; return this; }
194        virtual bool get_lvalue() const;
195
196        virtual const Expr * accept( Visitor & v ) const override = 0;
197private:
198        Expr * clone() const override = 0;
199        MUTATE_FRIEND
200};
201
202/// The application of a function to a set of parameters.
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
211        bool get_lvalue() const final;
212
213        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
214private:
215        ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; }
216        MUTATE_FRIEND
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
228        bool get_lvalue() const final;
229
230        /// Creates a new dereference expression
231        static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg );
232        /// Creates a new assignment expression
233        static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs );
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 );
237
238        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
239private:
240        UntypedExpr * clone() const override { return new UntypedExpr{ *this }; }
241        MUTATE_FRIEND
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 }; }
255        MUTATE_FRIEND
256};
257
258class QualifiedNameExpr final : public Expr {
259public:
260        ptr<Decl> type_decl;
261        std::string name;
262
263        QualifiedNameExpr( const CodeLocation & loc, const Decl * d, const std::string & n ) 
264        : Expr( loc ), type_decl( d ), name( n ) {}
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
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
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
298        /// Generate AddressExpr wrapping given expression at same location
299        AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {}
300
301        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
302private:
303        AddressExpr * clone() const override { return new AddressExpr{ *this }; }
304        MUTATE_FRIEND
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 }; }
318        MUTATE_FRIEND
319};
320
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
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
338        enum CastKind {
339                Default, // C
340                Coerce, // reinterpret cast
341                Return  // overload selection
342        };
343
344        CastKind kind = Default;
345
346        CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
347                GeneratedFlag g = GeneratedCast, CastKind kind = Default ) : Expr( loc, to ), arg( a ), isGenerated( g ), kind( kind ) {}
348        /// Cast-to-void
349        CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast, CastKind kind = Default );
350
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
357        bool get_lvalue() const final;
358
359        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
360private:
361        CastExpr * clone() const override { return new CastExpr{ *this }; }
362        MUTATE_FRIEND
363};
364
365/// A cast to "keyword types", e.g. `(thread &)t`
366class KeywordCastExpr final : public Expr {
367public:
368        ptr<Expr> arg;
369        struct Concrete {
370                std::string field;
371                std::string getter;
372
373                Concrete() = default;
374                Concrete(const Concrete &) = default;
375        };
376        ast::AggregateDecl::Aggregate target;
377        Concrete concrete_target;
378
379
380        KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
381        : Expr( loc ), arg( a ), target( t ) {}
382
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
386        /// Get a name for the target type
387        const char * targetString() const;
388
389        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
390private:
391        KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
392        MUTATE_FRIEND
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 }; }
406        MUTATE_FRIEND
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
418        bool get_lvalue() const final;
419
420        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
421private:
422        UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
423        MUTATE_FRIEND
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
434        bool get_lvalue() const final;
435
436        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
437private:
438        MemberExpr * clone() const override { return new MemberExpr{ *this }; }
439        MUTATE_FRIEND
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;
447};
448
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.
452class ConstantExpr final : public Expr {
453public:
454        // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
455        std::string rep;
456
457        ConstantExpr(
458                const CodeLocation & loc, const Type * ty, const std::string & r,
459                        std::optional<unsigned long long> i )
460        : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
461
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.
465        long long int intValue() const;
466
467        /// Generates a boolean constant of the given bool.
468        static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
469        /// Generates an integer constant of the given int.
470        static ConstantExpr * from_int( const CodeLocation & loc, int i );
471        /// Generates an integer constant of the given unsigned long int.
472        static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
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.
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 }; }
481        MUTATE_FRIEND
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;
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 }; }
506        MUTATE_FRIEND
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 }; }
522        MUTATE_FRIEND
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 }; }
537        MUTATE_FRIEND
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 }; }
551        MUTATE_FRIEND
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 }; }
564        MUTATE_FRIEND
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 }; }
582        MUTATE_FRIEND
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 }; }
598        MUTATE_FRIEND
599};
600
601/// Comma expression e.g. `( a , b )`
602class CommaExpr final : public Expr {
603public:
604        ptr<Expr> arg1;
605        ptr<Expr> arg2;
606
607        CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
608        : Expr( loc ), arg1( a1 ), arg2( a2 ) {
609                this->result = a2->result;
610        }
611
612        bool get_lvalue() const final;
613
614        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
615private:
616        CommaExpr * clone() const override { return new CommaExpr{ *this }; }
617        MUTATE_FRIEND
618};
619
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
627        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
628private:
629        TypeExpr * clone() const override { return new TypeExpr{ *this }; }
630        MUTATE_FRIEND
631};
632
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
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:
650        std::string inout;
651        ptr<Expr> constraint;
652        ptr<Expr> operand;
653
654        AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
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
663/// The application of a function to a set of parameters, along with a set of copy constructor
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 )
670        : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
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
698        bool get_lvalue() const final;
699
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
756        bool get_lvalue() const final;
757
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
764/// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
765/// multiple-assignment: both sides of the assignment have tuple type,
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
773        TupleAssignExpr(
774                const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
775                std::vector<ptr<ObjectDecl>> && tempDecls );
776
777        const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
778
779        friend class ::ConverterOldToNew;
780
781private:
782        TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
783    TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
784
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
795        readonly<ExprStmt> resultExpr;
796
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
809class UniqueExpr final : public Expr {
810        static unsigned long long nextId;
811public:
812        ptr<Expr> expr;
813        readonly<ObjectDecl> object;
814        ptr<VariableExpr> var;
815        unsigned long long id;
816
817        UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
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;
838        std::deque<InitAlternative> initAlts;
839
840        UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
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;
869        readonly<Decl> deleteStmt;
870
871        DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
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
920};
921
922
923}
924
925#undef MUTATE_FRIEND
926
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.