[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 |
---|
| 11 | // Last Modified By : Aaron B. Moss |
---|
| 12 | // Created On : Fri May 10 10:30:00 2019 |
---|
| 13 | // Update Count : 1 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #pragma once |
---|
| 17 | |
---|
| 18 | #include <cassert> |
---|
| 19 | #include <map> |
---|
[54e41b3] | 20 | #include <string> |
---|
[79f7875] | 21 | #include <utility> // for move |
---|
| 22 | #include <vector> |
---|
| 23 | |
---|
| 24 | #include "Fwd.hpp" // for UniqueId |
---|
[54e41b3] | 25 | #include "Label.hpp" |
---|
[79f7875] | 26 | #include "ParseNode.hpp" |
---|
[264e691] | 27 | #include "Visitor.hpp" |
---|
[79f7875] | 28 | |
---|
[f3cc5b6] | 29 | // Must be included in *all* AST classes; should be #undef'd at the end of the file |
---|
[acd80b4] | 30 | #define MUTATE_FRIEND template<typename node_t> friend node_t * mutate(const node_t * node); |
---|
[f3cc5b6] | 31 | |
---|
[20de6fb] | 32 | class ConverterOldToNew; |
---|
| 33 | |
---|
[79f7875] | 34 | namespace ast { |
---|
| 35 | |
---|
[6d51bd7] | 36 | /// Contains the ID of a declaration and a type that is derived from that declaration, |
---|
[79f7875] | 37 | /// but subject to decay-to-pointer and type parameter renaming |
---|
| 38 | struct ParamEntry { |
---|
| 39 | UniqueId decl; |
---|
| 40 | ptr<Type> actualType; |
---|
| 41 | ptr<Type> formalType; |
---|
| 42 | ptr<Expr> expr; |
---|
| 43 | |
---|
| 44 | ParamEntry() : decl( 0 ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {} |
---|
| 45 | ParamEntry( UniqueId id, Type* actual, Type* formal, Expr* e ) |
---|
| 46 | : decl( id ), actualType( actual ), formalType( formal ), expr( e ) {} |
---|
| 47 | }; |
---|
| 48 | |
---|
| 49 | /// Pre-resolution list of parameters to infer |
---|
| 50 | using ResnSlots = std::vector<UniqueId>; |
---|
| 51 | /// Post-resolution map of inferred parameters |
---|
| 52 | using InferredParams = std::map< UniqueId, ParamEntry >; |
---|
| 53 | |
---|
| 54 | /// Base node for expressions |
---|
| 55 | class Expr : public ParseNode { |
---|
| 56 | public: |
---|
| 57 | /// Saves space (~16 bytes) by combining ResnSlots and InferredParams |
---|
| 58 | struct InferUnion { |
---|
| 59 | enum { Empty, Slots, Params } mode; |
---|
| 60 | union data_t { |
---|
| 61 | char def; |
---|
| 62 | ResnSlots resnSlots; |
---|
| 63 | InferredParams inferParams; |
---|
| 64 | |
---|
| 65 | data_t() : def('\0') {} |
---|
| 66 | ~data_t() {} |
---|
| 67 | } data; |
---|
| 68 | |
---|
| 69 | /// initializes from other InferUnion |
---|
| 70 | void init_from( const InferUnion& o ) { |
---|
| 71 | switch ( o.mode ) { |
---|
| 72 | case Empty: return; |
---|
| 73 | case Slots: new(&data.resnSlots) ResnSlots{ o.data.resnSlots }; return; |
---|
| 74 | case Params: new(&data.inferParams) InferredParams{ o.data.inferParams }; return; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | |
---|
| 78 | /// initializes from other InferUnion (move semantics) |
---|
| 79 | void init_from( InferUnion&& o ) { |
---|
| 80 | switch ( o.mode ) { |
---|
| 81 | case Empty: return; |
---|
| 82 | case Slots: new(&data.resnSlots) ResnSlots{ std::move(o.data.resnSlots) }; return; |
---|
[6d51bd7] | 83 | case Params: |
---|
[79f7875] | 84 | new(&data.inferParams) InferredParams{ std::move(o.data.inferParams) }; return; |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | /// clears variant fields |
---|
| 89 | void reset() { |
---|
| 90 | switch( mode ) { |
---|
| 91 | case Empty: return; |
---|
| 92 | case Slots: data.resnSlots.~ResnSlots(); return; |
---|
| 93 | case Params: data.inferParams.~InferredParams(); return; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | InferUnion() : mode(Empty), data() {} |
---|
| 98 | InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); } |
---|
| 99 | InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); } |
---|
| 100 | InferUnion& operator= ( const InferUnion& ) = delete; |
---|
| 101 | InferUnion& operator= ( InferUnion&& ) = delete; |
---|
| 102 | ~InferUnion() { reset(); } |
---|
| 103 | |
---|
| 104 | ResnSlots& resnSlots() { |
---|
| 105 | switch (mode) { |
---|
| 106 | case Empty: new(&data.resnSlots) ResnSlots{}; mode = Slots; // fallthrough |
---|
| 107 | case Slots: return data.resnSlots; |
---|
| 108 | case Params: assert(!"Cannot return to resnSlots from Params"); |
---|
| 109 | } |
---|
[19e567dd] | 110 | return *((ResnSlots*)nullptr); |
---|
| 111 | } |
---|
| 112 | |
---|
| 113 | const ResnSlots& resnSlotsConst() const { |
---|
| 114 | if (mode == Slots) { |
---|
| 115 | return data.resnSlots; |
---|
| 116 | } |
---|
| 117 | assert(!"Mode was not already resnSlots"); |
---|
| 118 | return *((ResnSlots*)nullptr); |
---|
[79f7875] | 119 | } |
---|
| 120 | |
---|
| 121 | InferredParams& inferParams() { |
---|
| 122 | switch (mode) { |
---|
| 123 | case Slots: data.resnSlots.~ResnSlots(); // fallthrough |
---|
| 124 | case Empty: new(&data.inferParams) InferredParams{}; mode = Params; // fallthrough |
---|
| 125 | case Params: return data.inferParams; |
---|
| 126 | } |
---|
[19e567dd] | 127 | return *((InferredParams*)nullptr); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | const InferredParams& inferParamsConst() const { |
---|
| 131 | if (mode == Params) { |
---|
| 132 | return data.inferParams; |
---|
| 133 | } |
---|
| 134 | assert(!"Mode was not already Params"); |
---|
| 135 | return *((InferredParams*)nullptr); |
---|
[79f7875] | 136 | } |
---|
| 137 | }; |
---|
| 138 | |
---|
| 139 | ptr<Type> result; |
---|
| 140 | ptr<TypeSubstitution> env; |
---|
| 141 | InferUnion inferred; |
---|
| 142 | bool extension = false; |
---|
| 143 | |
---|
[54e41b3] | 144 | Expr( const CodeLocation & loc, const Type * res = nullptr ) |
---|
| 145 | : ParseNode( loc ), result( res ), env(), inferred() {} |
---|
[79f7875] | 146 | |
---|
[9e1d485] | 147 | Expr * set_extension( bool ex ) { extension = ex; return this; } |
---|
[79f7875] | 148 | |
---|
[69bafd2] | 149 | virtual const Expr * accept( Visitor & v ) const override = 0; |
---|
[79f7875] | 150 | private: |
---|
[23f99e1] | 151 | Expr * clone() const override = 0; |
---|
[f3cc5b6] | 152 | MUTATE_FRIEND |
---|
[264e691] | 153 | }; |
---|
| 154 | |
---|
[87701b6] | 155 | /// The application of a function to a set of parameters. |
---|
[54e41b3] | 156 | /// Post-resolver form of `UntypedExpr` |
---|
| 157 | class ApplicationExpr final : public Expr { |
---|
| 158 | public: |
---|
| 159 | ptr<Expr> func; |
---|
| 160 | std::vector<ptr<Expr>> args; |
---|
| 161 | |
---|
| 162 | ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} ); |
---|
| 163 | |
---|
| 164 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 165 | private: |
---|
| 166 | ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; } |
---|
[f3cc5b6] | 167 | MUTATE_FRIEND |
---|
[54e41b3] | 168 | }; |
---|
| 169 | |
---|
| 170 | /// The application of a function to a set of parameters, pre-overload resolution. |
---|
| 171 | class UntypedExpr final : public Expr { |
---|
| 172 | public: |
---|
| 173 | ptr<Expr> func; |
---|
| 174 | std::vector<ptr<Expr>> args; |
---|
| 175 | |
---|
| 176 | UntypedExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} ) |
---|
| 177 | : Expr( loc ), func( f ), args( std::move(as) ) {} |
---|
| 178 | |
---|
| 179 | /// Creates a new dereference expression |
---|
| 180 | static UntypedExpr * createDeref( const CodeLocation & loc, Expr * arg ); |
---|
| 181 | /// Creates a new assignment expression |
---|
| 182 | static UntypedExpr * createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ); |
---|
| 183 | |
---|
| 184 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 185 | private: |
---|
| 186 | UntypedExpr * clone() const override { return new UntypedExpr{ *this }; } |
---|
[f3cc5b6] | 187 | MUTATE_FRIEND |
---|
[54e41b3] | 188 | }; |
---|
| 189 | |
---|
| 190 | /// A name whose name is as-yet undetermined. |
---|
| 191 | /// May also be used to avoid name mangling in codegen phase. |
---|
| 192 | class NameExpr final : public Expr { |
---|
| 193 | public: |
---|
| 194 | std::string name; |
---|
| 195 | |
---|
| 196 | NameExpr( const CodeLocation & loc, const std::string & n ) : Expr( loc ), name( n ) {} |
---|
| 197 | |
---|
| 198 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 199 | private: |
---|
| 200 | NameExpr * clone() const override { return new NameExpr{ *this }; } |
---|
[f3cc5b6] | 201 | MUTATE_FRIEND |
---|
[54e41b3] | 202 | }; |
---|
| 203 | |
---|
| 204 | /// Address-of expression `&e` |
---|
| 205 | class AddressExpr final : public Expr { |
---|
| 206 | public: |
---|
| 207 | ptr<Expr> arg; |
---|
| 208 | |
---|
| 209 | AddressExpr( const CodeLocation & loc, const Expr * a ); |
---|
| 210 | |
---|
| 211 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 212 | private: |
---|
| 213 | AddressExpr * clone() const override { return new AddressExpr{ *this }; } |
---|
[f3cc5b6] | 214 | MUTATE_FRIEND |
---|
[54e41b3] | 215 | }; |
---|
| 216 | |
---|
| 217 | /// GCC &&label |
---|
| 218 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html |
---|
| 219 | class LabelAddressExpr final : public Expr { |
---|
| 220 | public: |
---|
| 221 | Label arg; |
---|
| 222 | |
---|
| 223 | LabelAddressExpr( const CodeLocation & loc, Label && a ); |
---|
| 224 | |
---|
| 225 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 226 | private: |
---|
| 227 | LabelAddressExpr * clone() const override { return new LabelAddressExpr{ *this }; } |
---|
[f3cc5b6] | 228 | MUTATE_FRIEND |
---|
[54e41b3] | 229 | }; |
---|
| 230 | |
---|
| 231 | /// Whether a cast existed in the program source or not |
---|
| 232 | enum GeneratedFlag { ExplicitCast, GeneratedCast }; |
---|
| 233 | |
---|
| 234 | /// A type cast, e.g. `(int)e` |
---|
| 235 | class CastExpr final : public Expr { |
---|
| 236 | public: |
---|
| 237 | ptr<Expr> arg; |
---|
| 238 | GeneratedFlag isGenerated; |
---|
| 239 | |
---|
[87701b6] | 240 | CastExpr( const CodeLocation & loc, const Expr * a, const Type * to, |
---|
[54e41b3] | 241 | GeneratedFlag g = GeneratedCast ) : Expr( loc, to ), arg( a ), isGenerated( g ) {} |
---|
| 242 | /// Cast-to-void |
---|
| 243 | CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast ); |
---|
| 244 | |
---|
| 245 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 246 | private: |
---|
| 247 | CastExpr * clone() const override { return new CastExpr{ *this }; } |
---|
[f3cc5b6] | 248 | MUTATE_FRIEND |
---|
[54e41b3] | 249 | }; |
---|
| 250 | |
---|
| 251 | /// A cast to "keyword types", e.g. `(thread &)t` |
---|
| 252 | class KeywordCastExpr final : public Expr { |
---|
| 253 | public: |
---|
| 254 | ptr<Expr> arg; |
---|
| 255 | enum Target { Coroutine, Thread, Monitor, NUMBER_OF_TARGETS } target; |
---|
| 256 | |
---|
| 257 | KeywordCastExpr( const CodeLocation & loc, const Expr * a, Target t ) |
---|
| 258 | : Expr( loc ), arg( a ), target( t ) {} |
---|
| 259 | |
---|
| 260 | /// Get a name for the target type |
---|
| 261 | const std::string& targetString() const; |
---|
| 262 | |
---|
| 263 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 264 | private: |
---|
| 265 | KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; } |
---|
[f3cc5b6] | 266 | MUTATE_FRIEND |
---|
[54e41b3] | 267 | }; |
---|
| 268 | |
---|
| 269 | /// A virtual dynamic cast, e.g. `(virtual exception)e` |
---|
| 270 | class VirtualCastExpr final : public Expr { |
---|
| 271 | public: |
---|
| 272 | ptr<Expr> arg; |
---|
| 273 | |
---|
| 274 | VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to ) |
---|
| 275 | : Expr( loc, to ), arg( a ) {} |
---|
| 276 | |
---|
| 277 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 278 | private: |
---|
| 279 | VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; } |
---|
[f3cc5b6] | 280 | MUTATE_FRIEND |
---|
[54e41b3] | 281 | }; |
---|
| 282 | |
---|
| 283 | /// A member selection operation before expression resolution, e.g. `q.p` |
---|
| 284 | class UntypedMemberExpr final : public Expr { |
---|
| 285 | public: |
---|
| 286 | ptr<Expr> member; |
---|
| 287 | ptr<Expr> aggregate; |
---|
| 288 | |
---|
| 289 | UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg ) |
---|
| 290 | : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); } |
---|
| 291 | |
---|
| 292 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 293 | private: |
---|
| 294 | UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; } |
---|
[f3cc5b6] | 295 | MUTATE_FRIEND |
---|
[54e41b3] | 296 | }; |
---|
| 297 | |
---|
| 298 | /// A member selection operation after expression resolution, e.g. `q.p` |
---|
| 299 | class MemberExpr final : public Expr { |
---|
| 300 | public: |
---|
| 301 | readonly<DeclWithType> member; |
---|
| 302 | ptr<Expr> aggregate; |
---|
| 303 | |
---|
| 304 | MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ); |
---|
| 305 | |
---|
| 306 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 307 | private: |
---|
| 308 | MemberExpr * clone() const override { return new MemberExpr{ *this }; } |
---|
[f3cc5b6] | 309 | MUTATE_FRIEND |
---|
[54e41b3] | 310 | }; |
---|
| 311 | |
---|
| 312 | /// A reference to a named variable. |
---|
| 313 | class VariableExpr final : public Expr { |
---|
| 314 | public: |
---|
| 315 | readonly<DeclWithType> var; |
---|
| 316 | |
---|
| 317 | VariableExpr( const CodeLocation & loc, const DeclWithType * v ); |
---|
| 318 | |
---|
| 319 | /// generates a function pointer for a given function |
---|
| 320 | static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl ); |
---|
| 321 | |
---|
| 322 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 323 | private: |
---|
| 324 | VariableExpr * clone() const override { return new VariableExpr{ *this }; } |
---|
[f3cc5b6] | 325 | MUTATE_FRIEND |
---|
[54e41b3] | 326 | }; |
---|
| 327 | |
---|
| 328 | /// A compile-time constant |
---|
| 329 | class ConstantExpr final : public Expr { |
---|
| 330 | union Val { |
---|
| 331 | unsigned long long ival; |
---|
| 332 | double dval; |
---|
[87701b6] | 333 | |
---|
[54e41b3] | 334 | Val( unsigned long long i ) : ival( i ) {} |
---|
| 335 | Val( double d ) : dval( d ) {} |
---|
| 336 | } val; |
---|
| 337 | public: |
---|
| 338 | std::string rep; |
---|
| 339 | |
---|
[87701b6] | 340 | ConstantExpr( |
---|
[54e41b3] | 341 | const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v ) |
---|
| 342 | : Expr( loc, ty ), val( v ), rep( r ) {} |
---|
| 343 | ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v ) |
---|
| 344 | : Expr( loc, ty ), val( v ), rep( r ) {} |
---|
[87701b6] | 345 | |
---|
[54e41b3] | 346 | /// Gets the value of this constant as an integer |
---|
| 347 | long long int intValue() const; |
---|
| 348 | /// Gets the value of this constant as floating point |
---|
| 349 | double floatValue() const; |
---|
| 350 | |
---|
| 351 | /// generates a boolean constant of the given bool |
---|
| 352 | static ConstantExpr * from_bool( const CodeLocation & loc, bool b ); |
---|
| 353 | /// generates a char constant of the given char |
---|
| 354 | static ConstantExpr * from_char( const CodeLocation & loc, char c ); |
---|
| 355 | /// generates an integer constant of the given int |
---|
| 356 | static ConstantExpr * from_int( const CodeLocation & loc, int i ); |
---|
| 357 | /// generates an integer constant of the given unsigned long int |
---|
| 358 | static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i ); |
---|
| 359 | /// generates a floating point constant of the given double |
---|
| 360 | static ConstantExpr * from_double( const CodeLocation & loc, double d ); |
---|
| 361 | /// generates an array of chars constant of the given string |
---|
| 362 | static ConstantExpr * from_string( const CodeLocation & loc, const std::string & s ); |
---|
| 363 | /// generates a null pointer value for the given type. void * if omitted. |
---|
| 364 | static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr ); |
---|
| 365 | |
---|
| 366 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 367 | private: |
---|
| 368 | ConstantExpr * clone() const override { return new ConstantExpr{ *this }; } |
---|
[f3cc5b6] | 369 | MUTATE_FRIEND |
---|
[54e41b3] | 370 | }; |
---|
| 371 | |
---|
| 372 | /// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4` |
---|
| 373 | class SizeofExpr final : public Expr { |
---|
| 374 | public: |
---|
| 375 | ptr<Expr> expr; |
---|
| 376 | ptr<Type> type; |
---|
| 377 | |
---|
| 378 | SizeofExpr( const CodeLocation & loc, const Expr * e ); |
---|
| 379 | SizeofExpr( const CodeLocation & loc, const Type * t ); |
---|
| 380 | // deliberately no disambiguating overload for nullptr_t |
---|
| 381 | |
---|
| 382 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 383 | private: |
---|
| 384 | SizeofExpr * clone() const override { return new SizeofExpr{ *this }; } |
---|
[f3cc5b6] | 385 | MUTATE_FRIEND |
---|
[54e41b3] | 386 | }; |
---|
| 387 | |
---|
| 388 | /// alignof expression, e.g. `alignof(int)`, `alignof 3+4` |
---|
| 389 | class AlignofExpr final : public Expr { |
---|
| 390 | public: |
---|
| 391 | ptr<Expr> expr; |
---|
| 392 | ptr<Type> type; |
---|
| 393 | |
---|
| 394 | AlignofExpr( const CodeLocation & loc, const Expr * e ); |
---|
| 395 | AlignofExpr( const CodeLocation & loc, const Type * t ); |
---|
| 396 | // deliberately no disambiguating overload for nullptr_t |
---|
| 397 | |
---|
| 398 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 399 | private: |
---|
| 400 | AlignofExpr * clone() const override { return new AlignofExpr{ *this }; } |
---|
[f3cc5b6] | 401 | MUTATE_FRIEND |
---|
[54e41b3] | 402 | }; |
---|
| 403 | |
---|
| 404 | /// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)` |
---|
| 405 | class UntypedOffsetofExpr final : public Expr { |
---|
| 406 | public: |
---|
| 407 | ptr<Type> type; |
---|
| 408 | std::string member; |
---|
| 409 | |
---|
| 410 | UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem ) |
---|
| 411 | : Expr( loc ), type( ty ), member( mem ) {} |
---|
| 412 | |
---|
| 413 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 414 | private: |
---|
| 415 | UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; } |
---|
[f3cc5b6] | 416 | MUTATE_FRIEND |
---|
[54e41b3] | 417 | }; |
---|
| 418 | |
---|
| 419 | /// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)` |
---|
| 420 | class OffsetofExpr final : public Expr { |
---|
| 421 | public: |
---|
| 422 | ptr<Type> type; |
---|
| 423 | readonly<DeclWithType> member; |
---|
| 424 | |
---|
| 425 | OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ); |
---|
| 426 | |
---|
| 427 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 428 | private: |
---|
| 429 | OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; } |
---|
[f3cc5b6] | 430 | MUTATE_FRIEND |
---|
[54e41b3] | 431 | }; |
---|
| 432 | |
---|
| 433 | /// a pack of field-offsets for a generic type |
---|
| 434 | class OffsetPackExpr final : public Expr { |
---|
| 435 | public: |
---|
| 436 | ptr<StructInstType> type; |
---|
| 437 | |
---|
| 438 | OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ); |
---|
| 439 | |
---|
| 440 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 441 | private: |
---|
| 442 | OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; } |
---|
[f3cc5b6] | 443 | MUTATE_FRIEND |
---|
[54e41b3] | 444 | }; |
---|
| 445 | |
---|
| 446 | /// Variants of short-circuiting logical expression |
---|
| 447 | enum LogicalFlag { OrExpr, AndExpr }; |
---|
| 448 | |
---|
| 449 | /// Short-circuiting boolean expression (`&&` or `||`) |
---|
| 450 | class LogicalExpr final : public Expr { |
---|
| 451 | public: |
---|
| 452 | ptr<Expr> arg1; |
---|
| 453 | ptr<Expr> arg2; |
---|
| 454 | LogicalFlag isAnd; |
---|
| 455 | |
---|
| 456 | LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ); |
---|
| 457 | |
---|
| 458 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 459 | private: |
---|
| 460 | LogicalExpr * clone() const override { return new LogicalExpr{ *this }; } |
---|
[f3cc5b6] | 461 | MUTATE_FRIEND |
---|
[54e41b3] | 462 | }; |
---|
| 463 | |
---|
| 464 | /// Three-argument conditional e.g. `p ? a : b` |
---|
| 465 | class ConditionalExpr final : public Expr { |
---|
| 466 | public: |
---|
| 467 | ptr<Expr> arg1; |
---|
| 468 | ptr<Expr> arg2; |
---|
| 469 | ptr<Expr> arg3; |
---|
| 470 | |
---|
| 471 | ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 ) |
---|
| 472 | : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {} |
---|
| 473 | |
---|
| 474 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 475 | private: |
---|
| 476 | ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; } |
---|
[f3cc5b6] | 477 | MUTATE_FRIEND |
---|
[54e41b3] | 478 | }; |
---|
| 479 | |
---|
| 480 | /// Comma expression e.g. `( a , b )` |
---|
| 481 | class CommaExpr final : public Expr { |
---|
| 482 | public: |
---|
| 483 | ptr<Expr> arg1; |
---|
| 484 | ptr<Expr> arg2; |
---|
| 485 | |
---|
[87701b6] | 486 | CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 ) |
---|
[54e41b3] | 487 | : Expr( loc ), arg1( a1 ), arg2( a2 ) {} |
---|
| 488 | |
---|
| 489 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 490 | private: |
---|
| 491 | CommaExpr * clone() const override { return new CommaExpr{ *this }; } |
---|
[f3cc5b6] | 492 | MUTATE_FRIEND |
---|
[54e41b3] | 493 | }; |
---|
| 494 | |
---|
[264e691] | 495 | /// A type used as an expression (e.g. a type generator parameter) |
---|
| 496 | class TypeExpr final : public Expr { |
---|
| 497 | public: |
---|
| 498 | ptr<Type> type; |
---|
| 499 | |
---|
| 500 | TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {} |
---|
| 501 | |
---|
[69bafd2] | 502 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[264e691] | 503 | private: |
---|
| 504 | TypeExpr * clone() const override { return new TypeExpr{ *this }; } |
---|
[f3cc5b6] | 505 | MUTATE_FRIEND |
---|
[79f7875] | 506 | }; |
---|
| 507 | |
---|
[54e41b3] | 508 | /// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`. |
---|
| 509 | /// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints |
---|
| 510 | class AsmExpr final : public Expr { |
---|
| 511 | public: |
---|
| 512 | ptr<Expr> inout; |
---|
[9b4f329] | 513 | ptr<Expr> constraint; |
---|
| 514 | ptr<Expr> operand; |
---|
| 515 | |
---|
| 516 | AsmExpr( const CodeLocation & loc, const Expr * io, const Expr * con, const Expr * op ) |
---|
| 517 | : Expr( loc ), inout( io ), constraint( con ), operand( op ) {} |
---|
| 518 | |
---|
| 519 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 520 | private: |
---|
| 521 | AsmExpr * clone() const override { return new AsmExpr{ *this }; } |
---|
| 522 | MUTATE_FRIEND |
---|
| 523 | }; |
---|
| 524 | |
---|
[17a0228a] | 525 | /// The application of a function to a set of parameters, along with a set of copy constructor |
---|
[9b4f329] | 526 | /// calls, one for each argument |
---|
| 527 | class ImplicitCopyCtorExpr final : public Expr { |
---|
| 528 | public: |
---|
| 529 | ptr<ApplicationExpr> callExpr; |
---|
| 530 | std::vector<ptr<ObjectDecl>> tempDecls; |
---|
| 531 | std::vector<ptr<ObjectDecl>> returnDecls; |
---|
[20de6fb] | 532 | std::vector<ptr<Expr>> dtors; |
---|
[9b4f329] | 533 | |
---|
| 534 | ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call ) |
---|
| 535 | : Expr( loc, call->result ), tempDecls(), returnDecls(), dtors() { assert( call ); } |
---|
| 536 | |
---|
| 537 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 538 | private: |
---|
| 539 | ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; } |
---|
| 540 | MUTATE_FRIEND |
---|
| 541 | }; |
---|
| 542 | |
---|
| 543 | /// Constructor in expression context, e.g. `int * x = alloc() { 42 };` |
---|
| 544 | class ConstructorExpr final : public Expr { |
---|
| 545 | public: |
---|
| 546 | ptr<Expr> callExpr; |
---|
| 547 | |
---|
| 548 | ConstructorExpr( const CodeLocation & loc, const Expr * call ); |
---|
| 549 | |
---|
| 550 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 551 | private: |
---|
| 552 | ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; } |
---|
| 553 | MUTATE_FRIEND |
---|
| 554 | }; |
---|
| 555 | |
---|
| 556 | /// A C99 compound literal, e.g. `(MyType){ a, b, c }` |
---|
| 557 | class CompoundLiteralExpr final : public Expr { |
---|
| 558 | public: |
---|
| 559 | ptr<Init> init; |
---|
| 560 | |
---|
| 561 | CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i ); |
---|
| 562 | |
---|
| 563 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 564 | private: |
---|
| 565 | CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; } |
---|
| 566 | MUTATE_FRIEND |
---|
| 567 | }; |
---|
| 568 | |
---|
| 569 | /// A range, e.g. `3 ... 5` or `1~10` |
---|
| 570 | class RangeExpr final : public Expr { |
---|
| 571 | public: |
---|
| 572 | ptr<Expr> low; |
---|
| 573 | ptr<Expr> high; |
---|
| 574 | |
---|
| 575 | RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h ) |
---|
| 576 | : Expr( loc ), low( l ), high( h ) {} |
---|
| 577 | |
---|
| 578 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 579 | private: |
---|
| 580 | RangeExpr * clone() const override { return new RangeExpr{ *this }; } |
---|
| 581 | MUTATE_FRIEND |
---|
| 582 | }; |
---|
| 583 | |
---|
| 584 | /// A tuple expression before resolution, e.g. `[a, b, c]` |
---|
| 585 | class UntypedTupleExpr final : public Expr { |
---|
| 586 | public: |
---|
| 587 | std::vector<ptr<Expr>> exprs; |
---|
| 588 | |
---|
| 589 | UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ) |
---|
| 590 | : Expr( loc ), exprs( std::move(xs) ) {} |
---|
| 591 | |
---|
| 592 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 593 | private: |
---|
| 594 | UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; } |
---|
| 595 | MUTATE_FRIEND |
---|
| 596 | }; |
---|
| 597 | |
---|
| 598 | /// A tuple expression after resolution, e.g. `[a, b, c]` |
---|
| 599 | class TupleExpr final : public Expr { |
---|
| 600 | public: |
---|
| 601 | std::vector<ptr<Expr>> exprs; |
---|
| 602 | |
---|
| 603 | TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ); |
---|
| 604 | |
---|
| 605 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 606 | private: |
---|
| 607 | TupleExpr * clone() const override { return new TupleExpr{ *this }; } |
---|
| 608 | MUTATE_FRIEND |
---|
| 609 | }; |
---|
| 610 | |
---|
| 611 | /// An element selection operation on a tuple value, e.g. `t.3` after analysis |
---|
| 612 | class TupleIndexExpr final : public Expr { |
---|
| 613 | public: |
---|
| 614 | ptr<Expr> tuple; |
---|
| 615 | unsigned index; |
---|
| 616 | |
---|
| 617 | TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i ); |
---|
| 618 | |
---|
| 619 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 620 | private: |
---|
| 621 | TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; } |
---|
| 622 | MUTATE_FRIEND |
---|
| 623 | }; |
---|
| 624 | |
---|
[17a0228a] | 625 | /// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression. |
---|
| 626 | /// multiple-assignment: both sides of the assignment have tuple type, |
---|
[9b4f329] | 627 | /// e.g. `[a, b, c] = [d, e, f];` |
---|
| 628 | /// mass-assignment: left-hand side has tuple type and right-hand side does not: |
---|
| 629 | /// e.g. `[a, b, c] = 42;` |
---|
| 630 | class TupleAssignExpr final : public Expr { |
---|
| 631 | public: |
---|
| 632 | ptr<StmtExpr> stmtExpr; |
---|
| 633 | |
---|
[17a0228a] | 634 | TupleAssignExpr( |
---|
| 635 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns, |
---|
[9b4f329] | 636 | std::vector<ptr<ObjectDecl>> && tempDecls ); |
---|
[17a0228a] | 637 | |
---|
[9b4f329] | 638 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
[20de6fb] | 639 | |
---|
| 640 | friend class ::ConverterOldToNew; |
---|
| 641 | |
---|
[9b4f329] | 642 | private: |
---|
| 643 | TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; } |
---|
[20de6fb] | 644 | TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s ); |
---|
| 645 | |
---|
[9b4f329] | 646 | MUTATE_FRIEND |
---|
| 647 | }; |
---|
| 648 | |
---|
| 649 | /// A GCC "statement expression", e.g. `({ int x = 5; x })` |
---|
| 650 | class StmtExpr final : public Expr { |
---|
| 651 | public: |
---|
| 652 | ptr<CompoundStmt> stmts; |
---|
| 653 | std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression |
---|
| 654 | std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s) |
---|
| 655 | |
---|
| 656 | StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ); |
---|
| 657 | |
---|
| 658 | /// Set the result type of this StmtExpr based on its body |
---|
| 659 | void computeResult(); |
---|
| 660 | |
---|
| 661 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 662 | private: |
---|
| 663 | StmtExpr * clone() const override { return new StmtExpr{ *this }; } |
---|
| 664 | MUTATE_FRIEND |
---|
| 665 | }; |
---|
| 666 | |
---|
| 667 | /// An expression which must only be evaluated once |
---|
| 668 | class UniqueExpr final : public Expr { |
---|
| 669 | static unsigned long long nextId; |
---|
| 670 | public: |
---|
| 671 | ptr<Expr> expr; |
---|
| 672 | ptr<ObjectDecl> object; |
---|
| 673 | ptr<VariableExpr> var; |
---|
| 674 | unsigned long long id; |
---|
| 675 | |
---|
| 676 | UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1 ); |
---|
| 677 | |
---|
| 678 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 679 | private: |
---|
| 680 | UniqueExpr * clone() const override { return new UniqueExpr{ *this }; } |
---|
| 681 | MUTATE_FRIEND |
---|
| 682 | }; |
---|
| 683 | |
---|
| 684 | /// One option for resolving an initializer expression |
---|
| 685 | struct InitAlternative { |
---|
| 686 | ptr<Type> type; |
---|
| 687 | ptr<Designation> designation; |
---|
| 688 | |
---|
| 689 | InitAlternative() = default; |
---|
| 690 | InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {} |
---|
| 691 | }; |
---|
| 692 | |
---|
| 693 | /// Pre-resolution initializer expression |
---|
| 694 | class UntypedInitExpr final : public Expr { |
---|
| 695 | public: |
---|
| 696 | ptr<Expr> expr; |
---|
| 697 | std::vector<InitAlternative> initAlts; |
---|
| 698 | |
---|
| 699 | UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::vector<InitAlternative> && as ) |
---|
| 700 | : Expr( loc ), expr( e ), initAlts( std::move(as) ) {} |
---|
| 701 | |
---|
| 702 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 703 | private: |
---|
| 704 | UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; } |
---|
| 705 | MUTATE_FRIEND |
---|
| 706 | }; |
---|
| 707 | |
---|
| 708 | /// Post-resolution initializer expression |
---|
| 709 | class InitExpr final : public Expr { |
---|
| 710 | public: |
---|
| 711 | ptr<Expr> expr; |
---|
| 712 | ptr<Designation> designation; |
---|
| 713 | |
---|
| 714 | InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des ) |
---|
| 715 | : Expr( loc, e->result ), expr( e ), designation( des ) {} |
---|
| 716 | |
---|
| 717 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 718 | private: |
---|
| 719 | InitExpr * clone() const override { return new InitExpr{ *this }; } |
---|
| 720 | MUTATE_FRIEND |
---|
| 721 | }; |
---|
| 722 | |
---|
| 723 | /// Expression containing a deleted identifier. |
---|
| 724 | /// Internal to resolver. |
---|
| 725 | class DeletedExpr final : public Expr { |
---|
| 726 | public: |
---|
| 727 | ptr<Expr> expr; |
---|
| 728 | readonly<Node> deleteStmt; |
---|
| 729 | |
---|
| 730 | DeletedExpr( const CodeLocation & loc, const Expr * e, const Node * del ) |
---|
| 731 | : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); } |
---|
| 732 | |
---|
| 733 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 734 | private: |
---|
| 735 | DeletedExpr * clone() const override { return new DeletedExpr{ *this }; } |
---|
| 736 | MUTATE_FRIEND |
---|
| 737 | }; |
---|
| 738 | |
---|
| 739 | /// Use of a default argument. |
---|
| 740 | /// Internal to resolver. |
---|
| 741 | class DefaultArgExpr final : public Expr { |
---|
| 742 | public: |
---|
| 743 | ptr<Expr> expr; |
---|
| 744 | |
---|
| 745 | DefaultArgExpr( const CodeLocation & loc, const Expr * e ) |
---|
| 746 | : Expr( loc, e->result ), expr( e ) { assert( e->result ); } |
---|
| 747 | |
---|
| 748 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 749 | private: |
---|
| 750 | DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; } |
---|
| 751 | MUTATE_FRIEND |
---|
| 752 | }; |
---|
| 753 | |
---|
| 754 | /// C11 _Generic expression |
---|
| 755 | class GenericExpr final : public Expr { |
---|
| 756 | public: |
---|
| 757 | /// One arm of the _Generic expr |
---|
| 758 | struct Association { |
---|
| 759 | ptr<Type> type; |
---|
| 760 | ptr<Expr> expr; |
---|
| 761 | |
---|
| 762 | Association() = default; |
---|
| 763 | // default case |
---|
| 764 | Association( const Expr * e ) : type(), expr( e ) {} |
---|
| 765 | // non-default case |
---|
| 766 | Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {} |
---|
| 767 | }; |
---|
| 768 | |
---|
| 769 | ptr<Expr> control; |
---|
| 770 | std::vector<Association> associations; |
---|
| 771 | |
---|
| 772 | GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns ) |
---|
| 773 | : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {} |
---|
| 774 | |
---|
| 775 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); } |
---|
| 776 | private: |
---|
| 777 | GenericExpr * clone() const override { return new GenericExpr{ *this }; } |
---|
| 778 | MUTATE_FRIEND |
---|
[54e41b3] | 779 | }; |
---|
[6d51bd7] | 780 | |
---|
[246c245] | 781 | |
---|
[79f7875] | 782 | } |
---|
| 783 | |
---|
[f3cc5b6] | 784 | #undef MUTATE_FRIEND |
---|
| 785 | |
---|
[79f7875] | 786 | // Local Variables: // |
---|
| 787 | // tab-width: 4 // |
---|
| 788 | // mode: c++ // |
---|
| 789 | // compile-command: "make install" // |
---|
| 790 | // End: // |
---|