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