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