[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;
|
---|
[20a5977] | 339 | enum Kind { Integer, FloatingPoint, String } kind;
|
---|
[54e41b3] | 340 |
|
---|
[87701b6] | 341 | ConstantExpr(
|
---|
[20a5977] | 342 | const CodeLocation & loc, const Type * ty, const std::string & r, unsigned long long v,
|
---|
| 343 | Kind k = Integer )
|
---|
| 344 | : Expr( loc, ty ), val( v ), rep( r ), kind( k ) {}
|
---|
[54e41b3] | 345 | ConstantExpr( const CodeLocation & loc, const Type * ty, const std::string & r, double v )
|
---|
[20a5977] | 346 | : Expr( loc, ty ), val( v ), rep( r ), kind( FloatingPoint ) {}
|
---|
[87701b6] | 347 |
|
---|
[54e41b3] | 348 | /// Gets the value of this constant as an integer
|
---|
| 349 | long long int intValue() const;
|
---|
| 350 | /// Gets the value of this constant as floating point
|
---|
| 351 | double floatValue() const;
|
---|
| 352 |
|
---|
| 353 | /// generates a boolean constant of the given bool
|
---|
| 354 | static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
|
---|
| 355 | /// generates a char constant of the given char
|
---|
| 356 | static ConstantExpr * from_char( const CodeLocation & loc, char c );
|
---|
| 357 | /// generates an integer constant of the given int
|
---|
| 358 | static ConstantExpr * from_int( const CodeLocation & loc, int i );
|
---|
| 359 | /// generates an integer constant of the given unsigned long int
|
---|
| 360 | static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
|
---|
| 361 | /// generates a floating point constant of the given double
|
---|
| 362 | static ConstantExpr * from_double( const CodeLocation & loc, double d );
|
---|
| 363 | /// generates an array of chars constant of the given string
|
---|
| 364 | static ConstantExpr * from_string( const CodeLocation & loc, const std::string & s );
|
---|
| 365 | /// generates a null pointer value for the given type. void * if omitted.
|
---|
| 366 | static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
|
---|
| 367 |
|
---|
| 368 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 369 | private:
|
---|
| 370 | ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
|
---|
[f3cc5b6] | 371 | MUTATE_FRIEND
|
---|
[54e41b3] | 372 | };
|
---|
| 373 |
|
---|
| 374 | /// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
|
---|
| 375 | class SizeofExpr final : public Expr {
|
---|
| 376 | public:
|
---|
| 377 | ptr<Expr> expr;
|
---|
| 378 | ptr<Type> type;
|
---|
| 379 |
|
---|
| 380 | SizeofExpr( const CodeLocation & loc, const Expr * e );
|
---|
| 381 | SizeofExpr( const CodeLocation & loc, const Type * t );
|
---|
| 382 | // deliberately no disambiguating overload for nullptr_t
|
---|
| 383 |
|
---|
| 384 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 385 | private:
|
---|
| 386 | SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
|
---|
[f3cc5b6] | 387 | MUTATE_FRIEND
|
---|
[54e41b3] | 388 | };
|
---|
| 389 |
|
---|
| 390 | /// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
|
---|
| 391 | class AlignofExpr final : public Expr {
|
---|
| 392 | public:
|
---|
| 393 | ptr<Expr> expr;
|
---|
| 394 | ptr<Type> type;
|
---|
| 395 |
|
---|
| 396 | AlignofExpr( const CodeLocation & loc, const Expr * e );
|
---|
| 397 | AlignofExpr( const CodeLocation & loc, const Type * t );
|
---|
| 398 | // deliberately no disambiguating overload for nullptr_t
|
---|
| 399 |
|
---|
| 400 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 401 | private:
|
---|
| 402 | AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
|
---|
[f3cc5b6] | 403 | MUTATE_FRIEND
|
---|
[54e41b3] | 404 | };
|
---|
| 405 |
|
---|
| 406 | /// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
|
---|
| 407 | class UntypedOffsetofExpr final : public Expr {
|
---|
| 408 | public:
|
---|
| 409 | ptr<Type> type;
|
---|
| 410 | std::string member;
|
---|
| 411 |
|
---|
| 412 | UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
|
---|
| 413 | : Expr( loc ), type( ty ), member( mem ) {}
|
---|
| 414 |
|
---|
| 415 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 416 | private:
|
---|
| 417 | UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
|
---|
[f3cc5b6] | 418 | MUTATE_FRIEND
|
---|
[54e41b3] | 419 | };
|
---|
| 420 |
|
---|
| 421 | /// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
|
---|
| 422 | class OffsetofExpr final : public Expr {
|
---|
| 423 | public:
|
---|
| 424 | ptr<Type> type;
|
---|
| 425 | readonly<DeclWithType> member;
|
---|
| 426 |
|
---|
| 427 | OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
|
---|
| 428 |
|
---|
| 429 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 430 | private:
|
---|
| 431 | OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
|
---|
[f3cc5b6] | 432 | MUTATE_FRIEND
|
---|
[54e41b3] | 433 | };
|
---|
| 434 |
|
---|
| 435 | /// a pack of field-offsets for a generic type
|
---|
| 436 | class OffsetPackExpr final : public Expr {
|
---|
| 437 | public:
|
---|
| 438 | ptr<StructInstType> type;
|
---|
| 439 |
|
---|
| 440 | OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
|
---|
| 441 |
|
---|
| 442 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 443 | private:
|
---|
| 444 | OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
|
---|
[f3cc5b6] | 445 | MUTATE_FRIEND
|
---|
[54e41b3] | 446 | };
|
---|
| 447 |
|
---|
| 448 | /// Variants of short-circuiting logical expression
|
---|
| 449 | enum LogicalFlag { OrExpr, AndExpr };
|
---|
| 450 |
|
---|
| 451 | /// Short-circuiting boolean expression (`&&` or `||`)
|
---|
| 452 | class LogicalExpr final : public Expr {
|
---|
| 453 | public:
|
---|
| 454 | ptr<Expr> arg1;
|
---|
| 455 | ptr<Expr> arg2;
|
---|
| 456 | LogicalFlag isAnd;
|
---|
| 457 |
|
---|
| 458 | LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
|
---|
| 459 |
|
---|
| 460 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 461 | private:
|
---|
| 462 | LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
|
---|
[f3cc5b6] | 463 | MUTATE_FRIEND
|
---|
[54e41b3] | 464 | };
|
---|
| 465 |
|
---|
| 466 | /// Three-argument conditional e.g. `p ? a : b`
|
---|
| 467 | class ConditionalExpr final : public Expr {
|
---|
| 468 | public:
|
---|
| 469 | ptr<Expr> arg1;
|
---|
| 470 | ptr<Expr> arg2;
|
---|
| 471 | ptr<Expr> arg3;
|
---|
| 472 |
|
---|
| 473 | ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
|
---|
| 474 | : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
|
---|
| 475 |
|
---|
| 476 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 477 | private:
|
---|
| 478 | ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
|
---|
[f3cc5b6] | 479 | MUTATE_FRIEND
|
---|
[54e41b3] | 480 | };
|
---|
| 481 |
|
---|
| 482 | /// Comma expression e.g. `( a , b )`
|
---|
| 483 | class CommaExpr final : public Expr {
|
---|
| 484 | public:
|
---|
| 485 | ptr<Expr> arg1;
|
---|
| 486 | ptr<Expr> arg2;
|
---|
| 487 |
|
---|
[87701b6] | 488 | CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
|
---|
[54e41b3] | 489 | : Expr( loc ), arg1( a1 ), arg2( a2 ) {}
|
---|
| 490 |
|
---|
| 491 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 492 | private:
|
---|
| 493 | CommaExpr * clone() const override { return new CommaExpr{ *this }; }
|
---|
[f3cc5b6] | 494 | MUTATE_FRIEND
|
---|
[54e41b3] | 495 | };
|
---|
| 496 |
|
---|
[264e691] | 497 | /// A type used as an expression (e.g. a type generator parameter)
|
---|
| 498 | class TypeExpr final : public Expr {
|
---|
| 499 | public:
|
---|
| 500 | ptr<Type> type;
|
---|
| 501 |
|
---|
| 502 | TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
|
---|
| 503 |
|
---|
[69bafd2] | 504 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[264e691] | 505 | private:
|
---|
| 506 | TypeExpr * clone() const override { return new TypeExpr{ *this }; }
|
---|
[f3cc5b6] | 507 | MUTATE_FRIEND
|
---|
[79f7875] | 508 | };
|
---|
| 509 |
|
---|
[54e41b3] | 510 | /// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
|
---|
| 511 | /// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
|
---|
| 512 | class AsmExpr final : public Expr {
|
---|
| 513 | public:
|
---|
| 514 | ptr<Expr> inout;
|
---|
[9b4f329] | 515 | ptr<Expr> constraint;
|
---|
| 516 | ptr<Expr> operand;
|
---|
| 517 |
|
---|
| 518 | AsmExpr( const CodeLocation & loc, const Expr * io, const Expr * con, const Expr * op )
|
---|
| 519 | : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
|
---|
| 520 |
|
---|
| 521 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 522 | private:
|
---|
| 523 | AsmExpr * clone() const override { return new AsmExpr{ *this }; }
|
---|
| 524 | MUTATE_FRIEND
|
---|
| 525 | };
|
---|
| 526 |
|
---|
[17a0228a] | 527 | /// The application of a function to a set of parameters, along with a set of copy constructor
|
---|
[9b4f329] | 528 | /// calls, one for each argument
|
---|
| 529 | class ImplicitCopyCtorExpr final : public Expr {
|
---|
| 530 | public:
|
---|
| 531 | ptr<ApplicationExpr> callExpr;
|
---|
| 532 |
|
---|
| 533 | ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
|
---|
[d908563] | 534 | : Expr( loc, call->result ) { assert( call ); }
|
---|
[9b4f329] | 535 |
|
---|
| 536 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 537 | private:
|
---|
| 538 | ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
|
---|
| 539 | MUTATE_FRIEND
|
---|
| 540 | };
|
---|
| 541 |
|
---|
| 542 | /// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
|
---|
| 543 | class ConstructorExpr final : public Expr {
|
---|
| 544 | public:
|
---|
| 545 | ptr<Expr> callExpr;
|
---|
| 546 |
|
---|
| 547 | ConstructorExpr( const CodeLocation & loc, const Expr * call );
|
---|
| 548 |
|
---|
| 549 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 550 | private:
|
---|
| 551 | ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
|
---|
| 552 | MUTATE_FRIEND
|
---|
| 553 | };
|
---|
| 554 |
|
---|
| 555 | /// A C99 compound literal, e.g. `(MyType){ a, b, c }`
|
---|
| 556 | class CompoundLiteralExpr final : public Expr {
|
---|
| 557 | public:
|
---|
| 558 | ptr<Init> init;
|
---|
| 559 |
|
---|
| 560 | CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
|
---|
| 561 |
|
---|
| 562 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 563 | private:
|
---|
| 564 | CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
|
---|
| 565 | MUTATE_FRIEND
|
---|
| 566 | };
|
---|
| 567 |
|
---|
| 568 | /// A range, e.g. `3 ... 5` or `1~10`
|
---|
| 569 | class RangeExpr final : public Expr {
|
---|
| 570 | public:
|
---|
| 571 | ptr<Expr> low;
|
---|
| 572 | ptr<Expr> high;
|
---|
| 573 |
|
---|
| 574 | RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
|
---|
| 575 | : Expr( loc ), low( l ), high( h ) {}
|
---|
| 576 |
|
---|
| 577 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 578 | private:
|
---|
| 579 | RangeExpr * clone() const override { return new RangeExpr{ *this }; }
|
---|
| 580 | MUTATE_FRIEND
|
---|
| 581 | };
|
---|
| 582 |
|
---|
| 583 | /// A tuple expression before resolution, e.g. `[a, b, c]`
|
---|
| 584 | class UntypedTupleExpr final : public Expr {
|
---|
| 585 | public:
|
---|
| 586 | std::vector<ptr<Expr>> exprs;
|
---|
| 587 |
|
---|
| 588 | UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
|
---|
| 589 | : Expr( loc ), exprs( std::move(xs) ) {}
|
---|
| 590 |
|
---|
| 591 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 592 | private:
|
---|
| 593 | UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
|
---|
| 594 | MUTATE_FRIEND
|
---|
| 595 | };
|
---|
| 596 |
|
---|
| 597 | /// A tuple expression after resolution, e.g. `[a, b, c]`
|
---|
| 598 | class TupleExpr final : public Expr {
|
---|
| 599 | public:
|
---|
| 600 | std::vector<ptr<Expr>> exprs;
|
---|
| 601 |
|
---|
| 602 | TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
|
---|
| 603 |
|
---|
| 604 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 605 | private:
|
---|
| 606 | TupleExpr * clone() const override { return new TupleExpr{ *this }; }
|
---|
| 607 | MUTATE_FRIEND
|
---|
| 608 | };
|
---|
| 609 |
|
---|
| 610 | /// An element selection operation on a tuple value, e.g. `t.3` after analysis
|
---|
| 611 | class TupleIndexExpr final : public Expr {
|
---|
| 612 | public:
|
---|
| 613 | ptr<Expr> tuple;
|
---|
| 614 | unsigned index;
|
---|
| 615 |
|
---|
| 616 | TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
|
---|
| 617 |
|
---|
| 618 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 619 | private:
|
---|
| 620 | TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
|
---|
| 621 | MUTATE_FRIEND
|
---|
| 622 | };
|
---|
| 623 |
|
---|
[17a0228a] | 624 | /// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
|
---|
| 625 | /// multiple-assignment: both sides of the assignment have tuple type,
|
---|
[9b4f329] | 626 | /// e.g. `[a, b, c] = [d, e, f];`
|
---|
| 627 | /// mass-assignment: left-hand side has tuple type and right-hand side does not:
|
---|
| 628 | /// e.g. `[a, b, c] = 42;`
|
---|
| 629 | class TupleAssignExpr final : public Expr {
|
---|
| 630 | public:
|
---|
| 631 | ptr<StmtExpr> stmtExpr;
|
---|
| 632 |
|
---|
[17a0228a] | 633 | TupleAssignExpr(
|
---|
| 634 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
|
---|
[9b4f329] | 635 | std::vector<ptr<ObjectDecl>> && tempDecls );
|
---|
[17a0228a] | 636 |
|
---|
[9b4f329] | 637 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[20de6fb] | 638 |
|
---|
| 639 | friend class ::ConverterOldToNew;
|
---|
| 640 |
|
---|
[9b4f329] | 641 | private:
|
---|
| 642 | TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
|
---|
[20de6fb] | 643 | TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
|
---|
| 644 |
|
---|
[9b4f329] | 645 | MUTATE_FRIEND
|
---|
| 646 | };
|
---|
| 647 |
|
---|
| 648 | /// A GCC "statement expression", e.g. `({ int x = 5; x })`
|
---|
| 649 | class StmtExpr final : public Expr {
|
---|
| 650 | public:
|
---|
| 651 | ptr<CompoundStmt> stmts;
|
---|
| 652 | std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
|
---|
| 653 | std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
|
---|
| 654 |
|
---|
| 655 | StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
|
---|
| 656 |
|
---|
| 657 | /// Set the result type of this StmtExpr based on its body
|
---|
| 658 | void computeResult();
|
---|
| 659 |
|
---|
| 660 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 661 | private:
|
---|
| 662 | StmtExpr * clone() const override { return new StmtExpr{ *this }; }
|
---|
| 663 | MUTATE_FRIEND
|
---|
| 664 | };
|
---|
| 665 |
|
---|
| 666 | /// An expression which must only be evaluated once
|
---|
| 667 | class UniqueExpr final : public Expr {
|
---|
| 668 | static unsigned long long nextId;
|
---|
| 669 | public:
|
---|
| 670 | ptr<Expr> expr;
|
---|
| 671 | ptr<ObjectDecl> object;
|
---|
| 672 | ptr<VariableExpr> var;
|
---|
| 673 | unsigned long long id;
|
---|
| 674 |
|
---|
| 675 | UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1 );
|
---|
| 676 |
|
---|
| 677 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 678 | private:
|
---|
| 679 | UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
|
---|
| 680 | MUTATE_FRIEND
|
---|
| 681 | };
|
---|
| 682 |
|
---|
| 683 | /// One option for resolving an initializer expression
|
---|
| 684 | struct InitAlternative {
|
---|
| 685 | ptr<Type> type;
|
---|
| 686 | ptr<Designation> designation;
|
---|
| 687 |
|
---|
| 688 | InitAlternative() = default;
|
---|
| 689 | InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
|
---|
| 690 | };
|
---|
| 691 |
|
---|
| 692 | /// Pre-resolution initializer expression
|
---|
| 693 | class UntypedInitExpr final : public Expr {
|
---|
| 694 | public:
|
---|
| 695 | ptr<Expr> expr;
|
---|
| 696 | std::vector<InitAlternative> initAlts;
|
---|
| 697 |
|
---|
| 698 | UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::vector<InitAlternative> && as )
|
---|
| 699 | : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
|
---|
| 700 |
|
---|
| 701 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 702 | private:
|
---|
| 703 | UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
|
---|
| 704 | MUTATE_FRIEND
|
---|
| 705 | };
|
---|
| 706 |
|
---|
| 707 | /// Post-resolution initializer expression
|
---|
| 708 | class InitExpr final : public Expr {
|
---|
| 709 | public:
|
---|
| 710 | ptr<Expr> expr;
|
---|
| 711 | ptr<Designation> designation;
|
---|
| 712 |
|
---|
| 713 | InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
|
---|
| 714 | : Expr( loc, e->result ), expr( e ), designation( des ) {}
|
---|
| 715 |
|
---|
| 716 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 717 | private:
|
---|
| 718 | InitExpr * clone() const override { return new InitExpr{ *this }; }
|
---|
| 719 | MUTATE_FRIEND
|
---|
| 720 | };
|
---|
| 721 |
|
---|
| 722 | /// Expression containing a deleted identifier.
|
---|
| 723 | /// Internal to resolver.
|
---|
| 724 | class DeletedExpr final : public Expr {
|
---|
| 725 | public:
|
---|
| 726 | ptr<Expr> expr;
|
---|
| 727 | readonly<Node> deleteStmt;
|
---|
| 728 |
|
---|
| 729 | DeletedExpr( const CodeLocation & loc, const Expr * e, const Node * del )
|
---|
| 730 | : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
|
---|
| 731 |
|
---|
| 732 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 733 | private:
|
---|
| 734 | DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
|
---|
| 735 | MUTATE_FRIEND
|
---|
| 736 | };
|
---|
| 737 |
|
---|
| 738 | /// Use of a default argument.
|
---|
| 739 | /// Internal to resolver.
|
---|
| 740 | class DefaultArgExpr final : public Expr {
|
---|
| 741 | public:
|
---|
| 742 | ptr<Expr> expr;
|
---|
| 743 |
|
---|
| 744 | DefaultArgExpr( const CodeLocation & loc, const Expr * e )
|
---|
| 745 | : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
|
---|
| 746 |
|
---|
| 747 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 748 | private:
|
---|
| 749 | DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
|
---|
| 750 | MUTATE_FRIEND
|
---|
| 751 | };
|
---|
| 752 |
|
---|
| 753 | /// C11 _Generic expression
|
---|
| 754 | class GenericExpr final : public Expr {
|
---|
| 755 | public:
|
---|
| 756 | /// One arm of the _Generic expr
|
---|
| 757 | struct Association {
|
---|
| 758 | ptr<Type> type;
|
---|
| 759 | ptr<Expr> expr;
|
---|
| 760 |
|
---|
| 761 | Association() = default;
|
---|
| 762 | // default case
|
---|
| 763 | Association( const Expr * e ) : type(), expr( e ) {}
|
---|
| 764 | // non-default case
|
---|
| 765 | Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
|
---|
| 766 | };
|
---|
| 767 |
|
---|
| 768 | ptr<Expr> control;
|
---|
| 769 | std::vector<Association> associations;
|
---|
| 770 |
|
---|
| 771 | GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
|
---|
| 772 | : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
|
---|
| 773 |
|
---|
| 774 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 775 | private:
|
---|
| 776 | GenericExpr * clone() const override { return new GenericExpr{ *this }; }
|
---|
| 777 | MUTATE_FRIEND
|
---|
[54e41b3] | 778 | };
|
---|
[6d51bd7] | 779 |
|
---|
[246c245] | 780 |
|
---|
[79f7875] | 781 | }
|
---|
| 782 |
|
---|
[f3cc5b6] | 783 | #undef MUTATE_FRIEND
|
---|
| 784 |
|
---|
[79f7875] | 785 | // Local Variables: //
|
---|
| 786 | // tab-width: 4 //
|
---|
| 787 | // mode: c++ //
|
---|
| 788 | // compile-command: "make install" //
|
---|
| 789 | // End: //
|
---|