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