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