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