[79f7875] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
| 7 | // Expr.hpp --
|
---|
| 8 | //
|
---|
| 9 | // Author : Aaron B. Moss
|
---|
| 10 | // Created On : Fri May 10 10:30:00 2019
|
---|
[312029a] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[79f7875] | 12 | // Created On : Fri May 10 10:30:00 2019
|
---|
[312029a] | 13 | // Update Count : 7
|
---|
[79f7875] | 14 | //
|
---|
| 15 |
|
---|
| 16 | #pragma once
|
---|
| 17 |
|
---|
| 18 | #include <cassert>
|
---|
[60aaa51d] | 19 | #include <deque>
|
---|
[79f7875] | 20 | #include <map>
|
---|
[54e41b3] | 21 | #include <string>
|
---|
[79f7875] | 22 | #include <utility> // for move
|
---|
| 23 | #include <vector>
|
---|
[c36298d] | 24 | #include <optional>
|
---|
[79f7875] | 25 |
|
---|
| 26 | #include "Fwd.hpp" // for UniqueId
|
---|
[54e41b3] | 27 | #include "Label.hpp"
|
---|
[312029a] | 28 | #include "Decl.hpp"
|
---|
[79f7875] | 29 | #include "ParseNode.hpp"
|
---|
[264e691] | 30 | #include "Visitor.hpp"
|
---|
[79f7875] | 31 |
|
---|
[f3cc5b6] | 32 | // Must be included in *all* AST classes; should be #undef'd at the end of the file
|
---|
[99da267] | 33 | #define MUTATE_FRIEND \
|
---|
| 34 | template<typename node_t> friend node_t * mutate(const node_t * node); \
|
---|
| 35 | template<typename node_t> friend node_t * shallowCopy(const node_t * node);
|
---|
| 36 |
|
---|
[f3cc5b6] | 37 |
|
---|
[20de6fb] | 38 | class ConverterOldToNew;
|
---|
[c36298d] | 39 | class ConverterNewToOld;
|
---|
[20de6fb] | 40 |
|
---|
[79f7875] | 41 | namespace ast {
|
---|
| 42 |
|
---|
[6d51bd7] | 43 | /// Contains the ID of a declaration and a type that is derived from that declaration,
|
---|
[79f7875] | 44 | /// but subject to decay-to-pointer and type parameter renaming
|
---|
| 45 | struct ParamEntry {
|
---|
| 46 | UniqueId decl;
|
---|
[07d867b] | 47 | readonly<Decl> declptr;
|
---|
[79f7875] | 48 | ptr<Type> actualType;
|
---|
| 49 | ptr<Type> formalType;
|
---|
| 50 | ptr<Expr> expr;
|
---|
| 51 |
|
---|
[aaeacf4] | 52 | ParamEntry() : decl( 0 ), declptr( nullptr ), actualType( nullptr ), formalType( nullptr ), expr( nullptr ) {}
|
---|
[7870799] | 53 | ParamEntry(
|
---|
| 54 | UniqueId id, const Decl * declptr, const Type * actual, const Type * formal,
|
---|
[b69233ac] | 55 | const Expr * e )
|
---|
[aaeacf4] | 56 | : decl( id ), declptr( declptr ), actualType( actual ), formalType( formal ), expr( e ) {}
|
---|
[46da46b] | 57 |
|
---|
| 58 | operator bool() {return declptr;}
|
---|
[79f7875] | 59 | };
|
---|
| 60 |
|
---|
| 61 | /// Pre-resolution list of parameters to infer
|
---|
| 62 | using ResnSlots = std::vector<UniqueId>;
|
---|
| 63 | /// Post-resolution map of inferred parameters
|
---|
| 64 | using InferredParams = std::map< UniqueId, ParamEntry >;
|
---|
| 65 |
|
---|
| 66 | /// Base node for expressions
|
---|
| 67 | class Expr : public ParseNode {
|
---|
| 68 | public:
|
---|
[07d867b] | 69 | /*
|
---|
| 70 | * NOTE: the union approach is incorrect until the case of
|
---|
| 71 | * partial resolution in InferMatcher is eliminated.
|
---|
| 72 | * it is reverted to allow unresolved and resolved parameters
|
---|
| 73 | * to coexist in an expression node.
|
---|
| 74 | */
|
---|
[79f7875] | 75 | struct InferUnion {
|
---|
[07d867b] | 76 | // mode is now unused
|
---|
[79f7875] | 77 | enum { Empty, Slots, Params } mode;
|
---|
[07d867b] | 78 | struct data_t {
|
---|
| 79 | // char def;
|
---|
| 80 | ResnSlots * resnSlots;
|
---|
| 81 | InferredParams * inferParams;
|
---|
| 82 |
|
---|
| 83 | data_t(): resnSlots(nullptr), inferParams(nullptr) {}
|
---|
| 84 | data_t(const data_t &other) = delete;
|
---|
| 85 | ~data_t() {
|
---|
| 86 | delete resnSlots;
|
---|
| 87 | delete inferParams;
|
---|
| 88 | }
|
---|
[79f7875] | 89 | } data;
|
---|
| 90 |
|
---|
| 91 | /// initializes from other InferUnion
|
---|
| 92 | void init_from( const InferUnion& o ) {
|
---|
[07d867b] | 93 | if (o.data.resnSlots) {
|
---|
| 94 | data.resnSlots = new ResnSlots(*o.data.resnSlots);
|
---|
| 95 | }
|
---|
| 96 | if (o.data.inferParams) {
|
---|
| 97 | data.inferParams = new InferredParams(*o.data.inferParams);
|
---|
[79f7875] | 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /// initializes from other InferUnion (move semantics)
|
---|
| 102 | void init_from( InferUnion&& o ) {
|
---|
[07d867b] | 103 | data.resnSlots = o.data.resnSlots;
|
---|
| 104 | data.inferParams = o.data.inferParams;
|
---|
| 105 | o.data.resnSlots = nullptr;
|
---|
| 106 | o.data.inferParams = nullptr;
|
---|
[79f7875] | 107 | }
|
---|
| 108 |
|
---|
| 109 | InferUnion() : mode(Empty), data() {}
|
---|
| 110 | InferUnion( const InferUnion& o ) : mode( o.mode ), data() { init_from( o ); }
|
---|
| 111 | InferUnion( InferUnion&& o ) : mode( o.mode ), data() { init_from( std::move(o) ); }
|
---|
| 112 | InferUnion& operator= ( const InferUnion& ) = delete;
|
---|
| 113 | InferUnion& operator= ( InferUnion&& ) = delete;
|
---|
[07d867b] | 114 |
|
---|
| 115 | bool hasSlots() const { return data.resnSlots; }
|
---|
[b3a0df6] | 116 | bool hasParams() const { return data.inferParams; }
|
---|
[79f7875] | 117 |
|
---|
| 118 | ResnSlots& resnSlots() {
|
---|
[07d867b] | 119 | if (!data.resnSlots) {
|
---|
| 120 | data.resnSlots = new ResnSlots();
|
---|
[79f7875] | 121 | }
|
---|
[07d867b] | 122 | return *data.resnSlots;
|
---|
[19e567dd] | 123 | }
|
---|
| 124 |
|
---|
[60aaa51d] | 125 | const ResnSlots& resnSlots() const {
|
---|
[07d867b] | 126 | if (data.resnSlots) {
|
---|
| 127 | return *data.resnSlots;
|
---|
[19e567dd] | 128 | }
|
---|
[7870799] | 129 | assertf(false, "Mode was not already resnSlots");
|
---|
| 130 | abort();
|
---|
[79f7875] | 131 | }
|
---|
| 132 |
|
---|
| 133 | InferredParams& inferParams() {
|
---|
[07d867b] | 134 | if (!data.inferParams) {
|
---|
| 135 | data.inferParams = new InferredParams();
|
---|
[79f7875] | 136 | }
|
---|
[07d867b] | 137 | return *data.inferParams;
|
---|
[19e567dd] | 138 | }
|
---|
| 139 |
|
---|
[60aaa51d] | 140 | const InferredParams& inferParams() const {
|
---|
[07d867b] | 141 | if (data.inferParams) {
|
---|
| 142 | return *data.inferParams;
|
---|
[19e567dd] | 143 | }
|
---|
[7870799] | 144 | assertf(false, "Mode was not already Params");
|
---|
| 145 | abort();
|
---|
[79f7875] | 146 | }
|
---|
[60aaa51d] | 147 |
|
---|
[07d867b] | 148 | void set_inferParams( InferredParams * ps ) {
|
---|
| 149 | delete data.resnSlots;
|
---|
| 150 | data.resnSlots = nullptr;
|
---|
| 151 | delete data.inferParams;
|
---|
| 152 | data.inferParams = ps;
|
---|
[b69233ac] | 153 | }
|
---|
| 154 |
|
---|
[aaeacf4] | 155 | /// splices other InferUnion into this one. Will fail if one union is in `Slots` mode
|
---|
[60aaa51d] | 156 | /// and the other is in `Params`.
|
---|
| 157 | void splice( InferUnion && o ) {
|
---|
[07d867b] | 158 | if (o.data.resnSlots) {
|
---|
| 159 | if (data.resnSlots) {
|
---|
| 160 | data.resnSlots->insert(
|
---|
| 161 | data.resnSlots->end(), o.data.resnSlots->begin(), o.data.resnSlots->end() );
|
---|
| 162 | delete o.data.resnSlots;
|
---|
| 163 | }
|
---|
| 164 | else {
|
---|
| 165 | data.resnSlots = o.data.resnSlots;
|
---|
[60aaa51d] | 166 | }
|
---|
[07d867b] | 167 | o.data.resnSlots = nullptr;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
| 170 | if (o.data.inferParams) {
|
---|
| 171 | if (data.inferParams) {
|
---|
| 172 | for ( const auto & p : *o.data.inferParams ) {
|
---|
| 173 | (*data.inferParams)[p.first] = std::move(p.second);
|
---|
| 174 | }
|
---|
| 175 | delete o.data.inferParams;
|
---|
| 176 | }
|
---|
| 177 | else {
|
---|
| 178 | data.inferParams = o.data.inferParams;
|
---|
| 179 | }
|
---|
| 180 | o.data.inferParams = nullptr;
|
---|
| 181 | }
|
---|
[60aaa51d] | 182 | }
|
---|
[79f7875] | 183 | };
|
---|
| 184 |
|
---|
| 185 | ptr<Type> result;
|
---|
| 186 | ptr<TypeSubstitution> env;
|
---|
| 187 | InferUnion inferred;
|
---|
| 188 | bool extension = false;
|
---|
| 189 |
|
---|
[54e41b3] | 190 | Expr( const CodeLocation & loc, const Type * res = nullptr )
|
---|
| 191 | : ParseNode( loc ), result( res ), env(), inferred() {}
|
---|
[79f7875] | 192 |
|
---|
[9e1d485] | 193 | Expr * set_extension( bool ex ) { extension = ex; return this; }
|
---|
[cf32116] | 194 | virtual bool get_lvalue() const;
|
---|
[79f7875] | 195 |
|
---|
[69bafd2] | 196 | virtual const Expr * accept( Visitor & v ) const override = 0;
|
---|
[79f7875] | 197 | private:
|
---|
[23f99e1] | 198 | Expr * clone() const override = 0;
|
---|
[f3cc5b6] | 199 | MUTATE_FRIEND
|
---|
[264e691] | 200 | };
|
---|
| 201 |
|
---|
[87701b6] | 202 | /// The application of a function to a set of parameters.
|
---|
[54e41b3] | 203 | /// Post-resolver form of `UntypedExpr`
|
---|
| 204 | class ApplicationExpr final : public Expr {
|
---|
| 205 | public:
|
---|
| 206 | ptr<Expr> func;
|
---|
| 207 | std::vector<ptr<Expr>> args;
|
---|
| 208 |
|
---|
| 209 | ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} );
|
---|
| 210 |
|
---|
[cf32116] | 211 | bool get_lvalue() const final;
|
---|
| 212 |
|
---|
[54e41b3] | 213 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 214 | private:
|
---|
| 215 | ApplicationExpr * clone() const override { return new ApplicationExpr{ *this }; }
|
---|
[f3cc5b6] | 216 | MUTATE_FRIEND
|
---|
[54e41b3] | 217 | };
|
---|
| 218 |
|
---|
| 219 | /// The application of a function to a set of parameters, pre-overload resolution.
|
---|
| 220 | class UntypedExpr final : public Expr {
|
---|
| 221 | public:
|
---|
| 222 | ptr<Expr> func;
|
---|
| 223 | std::vector<ptr<Expr>> args;
|
---|
| 224 |
|
---|
| 225 | UntypedExpr( const CodeLocation & loc, const Expr * f, std::vector<ptr<Expr>> && as = {} )
|
---|
| 226 | : Expr( loc ), func( f ), args( std::move(as) ) {}
|
---|
| 227 |
|
---|
[cf32116] | 228 | bool get_lvalue() const final;
|
---|
| 229 |
|
---|
[54e41b3] | 230 | /// Creates a new dereference expression
|
---|
[490fb92e] | 231 | static UntypedExpr * createDeref( const CodeLocation & loc, const Expr * arg );
|
---|
[54e41b3] | 232 | /// Creates a new assignment expression
|
---|
[490fb92e] | 233 | static UntypedExpr * createAssign( const CodeLocation & loc, const Expr * lhs, const Expr * rhs );
|
---|
[e6cf857f] | 234 | /// Creates a new call of a variable.
|
---|
| 235 | static UntypedExpr * createCall( const CodeLocation & loc,
|
---|
| 236 | const std::string & name, std::vector<ptr<Expr>> && args );
|
---|
[54e41b3] | 237 |
|
---|
| 238 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 239 | private:
|
---|
| 240 | UntypedExpr * clone() const override { return new UntypedExpr{ *this }; }
|
---|
[f3cc5b6] | 241 | MUTATE_FRIEND
|
---|
[54e41b3] | 242 | };
|
---|
| 243 |
|
---|
| 244 | /// A name whose name is as-yet undetermined.
|
---|
| 245 | /// May also be used to avoid name mangling in codegen phase.
|
---|
| 246 | class NameExpr final : public Expr {
|
---|
| 247 | public:
|
---|
| 248 | std::string name;
|
---|
| 249 |
|
---|
| 250 | NameExpr( const CodeLocation & loc, const std::string & n ) : Expr( loc ), name( n ) {}
|
---|
| 251 |
|
---|
| 252 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 253 | private:
|
---|
| 254 | NameExpr * clone() const override { return new NameExpr{ *this }; }
|
---|
[f3cc5b6] | 255 | MUTATE_FRIEND
|
---|
[54e41b3] | 256 | };
|
---|
| 257 |
|
---|
[94c98f0e] | 258 | /// A name qualified by a namespace or type.
|
---|
[b0d9ff7] | 259 | class QualifiedNameExpr final : public Expr {
|
---|
| 260 | public:
|
---|
| 261 | ptr<Decl> type_decl;
|
---|
| 262 | std::string name;
|
---|
| 263 |
|
---|
[94c98f0e] | 264 | QualifiedNameExpr( const CodeLocation & loc, const Decl * d, const std::string & n )
|
---|
[5408b59] | 265 | : Expr( loc ), type_decl( d ), name( n ) {}
|
---|
[b0d9ff7] | 266 |
|
---|
| 267 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 268 | private:
|
---|
| 269 | QualifiedNameExpr * clone() const override { return new QualifiedNameExpr{ *this }; }
|
---|
| 270 | MUTATE_FRIEND
|
---|
| 271 | };
|
---|
| 272 |
|
---|
[d5631b3] | 273 | /// A reference to a named variable.
|
---|
| 274 | class VariableExpr final : public Expr {
|
---|
| 275 | public:
|
---|
| 276 | readonly<DeclWithType> var;
|
---|
| 277 |
|
---|
| 278 | VariableExpr( const CodeLocation & loc );
|
---|
| 279 | VariableExpr( const CodeLocation & loc, const DeclWithType * v );
|
---|
| 280 |
|
---|
| 281 | bool get_lvalue() const final;
|
---|
| 282 |
|
---|
| 283 | /// generates a function pointer for a given function
|
---|
| 284 | static VariableExpr * functionPointer( const CodeLocation & loc, const FunctionDecl * decl );
|
---|
| 285 |
|
---|
| 286 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 287 | private:
|
---|
| 288 | VariableExpr * clone() const override { return new VariableExpr{ *this }; }
|
---|
| 289 | MUTATE_FRIEND
|
---|
| 290 | };
|
---|
| 291 |
|
---|
[54e41b3] | 292 | /// Address-of expression `&e`
|
---|
| 293 | class AddressExpr final : public Expr {
|
---|
| 294 | public:
|
---|
| 295 | ptr<Expr> arg;
|
---|
| 296 |
|
---|
| 297 | AddressExpr( const CodeLocation & loc, const Expr * a );
|
---|
| 298 |
|
---|
[b8524ca] | 299 | /// Generate AddressExpr wrapping given expression at same location
|
---|
| 300 | AddressExpr( const Expr * a ) : AddressExpr( a->location, a ) {}
|
---|
| 301 |
|
---|
[54e41b3] | 302 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 303 | private:
|
---|
| 304 | AddressExpr * clone() const override { return new AddressExpr{ *this }; }
|
---|
[f3cc5b6] | 305 | MUTATE_FRIEND
|
---|
[54e41b3] | 306 | };
|
---|
| 307 |
|
---|
| 308 | /// GCC &&label
|
---|
| 309 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.2/gcc/Labels-as-Values.html
|
---|
| 310 | class LabelAddressExpr final : public Expr {
|
---|
| 311 | public:
|
---|
| 312 | Label arg;
|
---|
| 313 |
|
---|
| 314 | LabelAddressExpr( const CodeLocation & loc, Label && a );
|
---|
| 315 |
|
---|
| 316 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 317 | private:
|
---|
| 318 | LabelAddressExpr * clone() const override { return new LabelAddressExpr{ *this }; }
|
---|
[f3cc5b6] | 319 | MUTATE_FRIEND
|
---|
[54e41b3] | 320 | };
|
---|
| 321 |
|
---|
[bb87dd0] | 322 | /// Inidicates whether the cast is introduced by the CFA type system.
|
---|
| 323 | /// GeneratedCast for casts that the resolver introduces to force a return type
|
---|
| 324 | /// ExplicitCast for casts from user code
|
---|
| 325 | /// ExplicitCast for casts from desugaring advanced CFA features into simpler CFA
|
---|
| 326 | /// example
|
---|
| 327 | /// int * p; // declaration
|
---|
| 328 | /// (float *) p; // use, with subject cast
|
---|
| 329 | /// subject cast being GeneratedCast means we are considering an interpretation with a type mismatch
|
---|
| 330 | /// subject cast being ExplicitCast means someone in charge wants it that way
|
---|
[54e41b3] | 331 | enum GeneratedFlag { ExplicitCast, GeneratedCast };
|
---|
| 332 |
|
---|
| 333 | /// A type cast, e.g. `(int)e`
|
---|
| 334 | class CastExpr final : public Expr {
|
---|
| 335 | public:
|
---|
| 336 | ptr<Expr> arg;
|
---|
| 337 | GeneratedFlag isGenerated;
|
---|
| 338 |
|
---|
[46da46b] | 339 | enum CastKind {
|
---|
| 340 | Default, // C
|
---|
| 341 | Coerce, // reinterpret cast
|
---|
| 342 | Return // overload selection
|
---|
| 343 | };
|
---|
| 344 |
|
---|
| 345 | CastKind kind = Default;
|
---|
| 346 |
|
---|
[87701b6] | 347 | CastExpr( const CodeLocation & loc, const Expr * a, const Type * to,
|
---|
[46da46b] | 348 | GeneratedFlag g = GeneratedCast, CastKind kind = Default ) : Expr( loc, to ), arg( a ), isGenerated( g ), kind( kind ) {}
|
---|
[54e41b3] | 349 | /// Cast-to-void
|
---|
[46da46b] | 350 | CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g = GeneratedCast, CastKind kind = Default );
|
---|
[54e41b3] | 351 |
|
---|
[b8524ca] | 352 | /// Wrap a cast expression around an existing expression (always generated)
|
---|
| 353 | CastExpr( const Expr * a, const Type * to ) : CastExpr( a->location, a, to, GeneratedCast ) {}
|
---|
| 354 |
|
---|
| 355 | /// Wrap a cast-to-void expression around an existing expression (always generated)
|
---|
| 356 | CastExpr( const Expr * a ) : CastExpr( a->location, a, GeneratedCast ) {}
|
---|
| 357 |
|
---|
[cf32116] | 358 | bool get_lvalue() const final;
|
---|
| 359 |
|
---|
[54e41b3] | 360 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 361 | private:
|
---|
| 362 | CastExpr * clone() const override { return new CastExpr{ *this }; }
|
---|
[f3cc5b6] | 363 | MUTATE_FRIEND
|
---|
[54e41b3] | 364 | };
|
---|
| 365 |
|
---|
| 366 | /// A cast to "keyword types", e.g. `(thread &)t`
|
---|
| 367 | class KeywordCastExpr final : public Expr {
|
---|
| 368 | public:
|
---|
| 369 | ptr<Expr> arg;
|
---|
[4ef08f7] | 370 | struct Concrete {
|
---|
| 371 | std::string field;
|
---|
| 372 | std::string getter;
|
---|
| 373 |
|
---|
| 374 | Concrete() = default;
|
---|
| 375 | Concrete(const Concrete &) = default;
|
---|
| 376 | };
|
---|
[312029a] | 377 | ast::AggregateDecl::Aggregate target;
|
---|
[4ef08f7] | 378 | Concrete concrete_target;
|
---|
| 379 |
|
---|
[54e41b3] | 380 |
|
---|
[312029a] | 381 | KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t )
|
---|
[54e41b3] | 382 | : Expr( loc ), arg( a ), target( t ) {}
|
---|
| 383 |
|
---|
[4ef08f7] | 384 | KeywordCastExpr( const CodeLocation & loc, const Expr * a, ast::AggregateDecl::Aggregate t, const Concrete & ct )
|
---|
| 385 | : Expr( loc ), arg( a ), target( t ), concrete_target( ct ) {}
|
---|
| 386 |
|
---|
[54e41b3] | 387 | /// Get a name for the target type
|
---|
[312029a] | 388 | const char * targetString() const;
|
---|
[54e41b3] | 389 |
|
---|
| 390 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 391 | private:
|
---|
| 392 | KeywordCastExpr * clone() const override { return new KeywordCastExpr{ *this }; }
|
---|
[f3cc5b6] | 393 | MUTATE_FRIEND
|
---|
[54e41b3] | 394 | };
|
---|
| 395 |
|
---|
| 396 | /// A virtual dynamic cast, e.g. `(virtual exception)e`
|
---|
| 397 | class VirtualCastExpr final : public Expr {
|
---|
| 398 | public:
|
---|
| 399 | ptr<Expr> arg;
|
---|
| 400 |
|
---|
| 401 | VirtualCastExpr( const CodeLocation & loc, const Expr * a, const Type * to )
|
---|
| 402 | : Expr( loc, to ), arg( a ) {}
|
---|
| 403 |
|
---|
| 404 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 405 | private:
|
---|
| 406 | VirtualCastExpr * clone() const override { return new VirtualCastExpr{ *this }; }
|
---|
[f3cc5b6] | 407 | MUTATE_FRIEND
|
---|
[54e41b3] | 408 | };
|
---|
| 409 |
|
---|
| 410 | /// A member selection operation before expression resolution, e.g. `q.p`
|
---|
| 411 | class UntypedMemberExpr final : public Expr {
|
---|
| 412 | public:
|
---|
| 413 | ptr<Expr> member;
|
---|
| 414 | ptr<Expr> aggregate;
|
---|
| 415 |
|
---|
| 416 | UntypedMemberExpr( const CodeLocation & loc, const Expr * mem, const Expr * agg )
|
---|
| 417 | : Expr( loc ), member( mem ), aggregate( agg ) { assert( aggregate ); }
|
---|
| 418 |
|
---|
[cf32116] | 419 | bool get_lvalue() const final;
|
---|
| 420 |
|
---|
[54e41b3] | 421 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 422 | private:
|
---|
| 423 | UntypedMemberExpr * clone() const override { return new UntypedMemberExpr{ *this }; }
|
---|
[f3cc5b6] | 424 | MUTATE_FRIEND
|
---|
[54e41b3] | 425 | };
|
---|
| 426 |
|
---|
| 427 | /// A member selection operation after expression resolution, e.g. `q.p`
|
---|
| 428 | class MemberExpr final : public Expr {
|
---|
| 429 | public:
|
---|
| 430 | readonly<DeclWithType> member;
|
---|
| 431 | ptr<Expr> aggregate;
|
---|
| 432 |
|
---|
| 433 | MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg );
|
---|
| 434 |
|
---|
[cf32116] | 435 | bool get_lvalue() const final;
|
---|
| 436 |
|
---|
[54e41b3] | 437 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 438 | private:
|
---|
| 439 | MemberExpr * clone() const override { return new MemberExpr{ *this }; }
|
---|
[f3cc5b6] | 440 | MUTATE_FRIEND
|
---|
[ae265b55] | 441 |
|
---|
| 442 | // Custructor overload meant only for AST conversion
|
---|
| 443 | enum NoOpConstruction { NoOpConstructionChosen };
|
---|
| 444 | MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg,
|
---|
| 445 | NoOpConstruction overloadSelector );
|
---|
| 446 | friend class ::ConverterOldToNew;
|
---|
| 447 | friend class ::ConverterNewToOld;
|
---|
[54e41b3] | 448 | };
|
---|
| 449 |
|
---|
[c36298d] | 450 | /// A compile-time constant.
|
---|
| 451 | /// Mostly carries C-source text from parse to code-gen, without interpretation. E.g. strings keep their outer quotes and never have backslashes interpreted.
|
---|
| 452 | /// Integer constants get special treatment, e.g. for verifying array operations, when an integer constant occurs as the length of an array.
|
---|
[54e41b3] | 453 | class ConstantExpr final : public Expr {
|
---|
| 454 | public:
|
---|
[c36298d] | 455 | // Representation of this constant, as it occurs in .cfa source and .cfa.cc result.
|
---|
[54e41b3] | 456 | std::string rep;
|
---|
| 457 |
|
---|
[87701b6] | 458 | ConstantExpr(
|
---|
[7870799] | 459 | const CodeLocation & loc, const Type * ty, const std::string & r,
|
---|
[c36298d] | 460 | std::optional<unsigned long long> i )
|
---|
[490fb92e] | 461 | : Expr( loc, ty ), rep( r ), ival( i ), underlyer(ty) {}
|
---|
[87701b6] | 462 |
|
---|
[c36298d] | 463 | /// Gets the integer value of this constant, if one is appropriate to its type.
|
---|
| 464 | /// Throws a SemanticError if the type is not appropriate for value-as-integer.
|
---|
| 465 | /// Suffers an assertion failure the type is appropriate but no integer value was supplied to the constructor.
|
---|
[54e41b3] | 466 | long long int intValue() const;
|
---|
| 467 |
|
---|
[b91bfde] | 468 | /// Generates a boolean constant of the given bool.
|
---|
[54e41b3] | 469 | static ConstantExpr * from_bool( const CodeLocation & loc, bool b );
|
---|
[b91bfde] | 470 | /// Generates an integer constant of the given int.
|
---|
[54e41b3] | 471 | static ConstantExpr * from_int( const CodeLocation & loc, int i );
|
---|
[b91bfde] | 472 | /// Generates an integer constant of the given unsigned long int.
|
---|
[54e41b3] | 473 | static ConstantExpr * from_ulong( const CodeLocation & loc, unsigned long i );
|
---|
[b91bfde] | 474 | /// Generates a string constant from the given string (char type, unquoted string).
|
---|
| 475 | static ConstantExpr * from_string( const CodeLocation & loc, const std::string & string );
|
---|
| 476 | /// Generates a null pointer value for the given type. void * if omitted.
|
---|
[54e41b3] | 477 | static ConstantExpr * null( const CodeLocation & loc, const Type * ptrType = nullptr );
|
---|
| 478 |
|
---|
| 479 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 480 | private:
|
---|
| 481 | ConstantExpr * clone() const override { return new ConstantExpr{ *this }; }
|
---|
[f3cc5b6] | 482 | MUTATE_FRIEND
|
---|
[c36298d] | 483 |
|
---|
| 484 | std::optional<unsigned long long> ival;
|
---|
| 485 |
|
---|
| 486 | // Intended only for legacy support of roundtripping the old AST.
|
---|
| 487 | // Captures the very-locally inferred type, before the resolver modifies the type of this ConstantExpression.
|
---|
| 488 | // In the old AST it's constExpr->constant.type
|
---|
| 489 | ptr<Type> underlyer;
|
---|
| 490 | friend class ::ConverterOldToNew;
|
---|
| 491 | friend class ::ConverterNewToOld;
|
---|
[54e41b3] | 492 | };
|
---|
| 493 |
|
---|
| 494 | /// sizeof expression, e.g. `sizeof(int)`, `sizeof 3+4`
|
---|
| 495 | class SizeofExpr final : public Expr {
|
---|
| 496 | public:
|
---|
| 497 | ptr<Expr> expr;
|
---|
| 498 | ptr<Type> type;
|
---|
| 499 |
|
---|
| 500 | SizeofExpr( const CodeLocation & loc, const Expr * e );
|
---|
| 501 | SizeofExpr( const CodeLocation & loc, const Type * t );
|
---|
| 502 | // deliberately no disambiguating overload for nullptr_t
|
---|
| 503 |
|
---|
| 504 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 505 | private:
|
---|
| 506 | SizeofExpr * clone() const override { return new SizeofExpr{ *this }; }
|
---|
[f3cc5b6] | 507 | MUTATE_FRIEND
|
---|
[54e41b3] | 508 | };
|
---|
| 509 |
|
---|
| 510 | /// alignof expression, e.g. `alignof(int)`, `alignof 3+4`
|
---|
| 511 | class AlignofExpr final : public Expr {
|
---|
| 512 | public:
|
---|
| 513 | ptr<Expr> expr;
|
---|
| 514 | ptr<Type> type;
|
---|
| 515 |
|
---|
| 516 | AlignofExpr( const CodeLocation & loc, const Expr * e );
|
---|
| 517 | AlignofExpr( const CodeLocation & loc, const Type * t );
|
---|
| 518 | // deliberately no disambiguating overload for nullptr_t
|
---|
| 519 |
|
---|
| 520 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 521 | private:
|
---|
| 522 | AlignofExpr * clone() const override { return new AlignofExpr{ *this }; }
|
---|
[f3cc5b6] | 523 | MUTATE_FRIEND
|
---|
[54e41b3] | 524 | };
|
---|
| 525 |
|
---|
| 526 | /// offsetof expression before resolver determines field, e.g. `offsetof(MyStruct, myfield)`
|
---|
| 527 | class UntypedOffsetofExpr final : public Expr {
|
---|
| 528 | public:
|
---|
| 529 | ptr<Type> type;
|
---|
| 530 | std::string member;
|
---|
| 531 |
|
---|
| 532 | UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem )
|
---|
| 533 | : Expr( loc ), type( ty ), member( mem ) {}
|
---|
| 534 |
|
---|
| 535 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 536 | private:
|
---|
| 537 | UntypedOffsetofExpr * clone() const override { return new UntypedOffsetofExpr{ *this }; }
|
---|
[f3cc5b6] | 538 | MUTATE_FRIEND
|
---|
[54e41b3] | 539 | };
|
---|
| 540 |
|
---|
| 541 | /// offsetof expression after resolver determines field, e.g. `offsetof(MyStruct, myfield)`
|
---|
| 542 | class OffsetofExpr final : public Expr {
|
---|
| 543 | public:
|
---|
| 544 | ptr<Type> type;
|
---|
| 545 | readonly<DeclWithType> member;
|
---|
| 546 |
|
---|
| 547 | OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem );
|
---|
| 548 |
|
---|
| 549 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 550 | private:
|
---|
| 551 | OffsetofExpr * clone() const override { return new OffsetofExpr{ *this }; }
|
---|
[f3cc5b6] | 552 | MUTATE_FRIEND
|
---|
[54e41b3] | 553 | };
|
---|
| 554 |
|
---|
| 555 | /// a pack of field-offsets for a generic type
|
---|
| 556 | class OffsetPackExpr final : public Expr {
|
---|
| 557 | public:
|
---|
| 558 | ptr<StructInstType> type;
|
---|
| 559 |
|
---|
| 560 | OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty );
|
---|
| 561 |
|
---|
| 562 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 563 | private:
|
---|
| 564 | OffsetPackExpr * clone() const override { return new OffsetPackExpr{ *this }; }
|
---|
[f3cc5b6] | 565 | MUTATE_FRIEND
|
---|
[54e41b3] | 566 | };
|
---|
| 567 |
|
---|
| 568 | /// Variants of short-circuiting logical expression
|
---|
| 569 | enum LogicalFlag { OrExpr, AndExpr };
|
---|
| 570 |
|
---|
| 571 | /// Short-circuiting boolean expression (`&&` or `||`)
|
---|
| 572 | class LogicalExpr final : public Expr {
|
---|
| 573 | public:
|
---|
| 574 | ptr<Expr> arg1;
|
---|
| 575 | ptr<Expr> arg2;
|
---|
| 576 | LogicalFlag isAnd;
|
---|
| 577 |
|
---|
| 578 | LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia );
|
---|
| 579 |
|
---|
| 580 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 581 | private:
|
---|
| 582 | LogicalExpr * clone() const override { return new LogicalExpr{ *this }; }
|
---|
[f3cc5b6] | 583 | MUTATE_FRIEND
|
---|
[54e41b3] | 584 | };
|
---|
| 585 |
|
---|
| 586 | /// Three-argument conditional e.g. `p ? a : b`
|
---|
| 587 | class ConditionalExpr final : public Expr {
|
---|
| 588 | public:
|
---|
| 589 | ptr<Expr> arg1;
|
---|
| 590 | ptr<Expr> arg2;
|
---|
| 591 | ptr<Expr> arg3;
|
---|
| 592 |
|
---|
| 593 | ConditionalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, const Expr * a3 )
|
---|
| 594 | : Expr( loc ), arg1( a1 ), arg2( a2 ), arg3( a3 ) {}
|
---|
| 595 |
|
---|
| 596 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 597 | private:
|
---|
| 598 | ConditionalExpr * clone() const override { return new ConditionalExpr{ *this }; }
|
---|
[f3cc5b6] | 599 | MUTATE_FRIEND
|
---|
[54e41b3] | 600 | };
|
---|
| 601 |
|
---|
| 602 | /// Comma expression e.g. `( a , b )`
|
---|
| 603 | class CommaExpr final : public Expr {
|
---|
| 604 | public:
|
---|
| 605 | ptr<Expr> arg1;
|
---|
| 606 | ptr<Expr> arg2;
|
---|
| 607 |
|
---|
[87701b6] | 608 | CommaExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2 )
|
---|
[4e13e2a] | 609 | : Expr( loc ), arg1( a1 ), arg2( a2 ) {
|
---|
| 610 | this->result = a2->result;
|
---|
| 611 | }
|
---|
[54e41b3] | 612 |
|
---|
[cf32116] | 613 | bool get_lvalue() const final;
|
---|
| 614 |
|
---|
[54e41b3] | 615 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 616 | private:
|
---|
| 617 | CommaExpr * clone() const override { return new CommaExpr{ *this }; }
|
---|
[f3cc5b6] | 618 | MUTATE_FRIEND
|
---|
[54e41b3] | 619 | };
|
---|
| 620 |
|
---|
[264e691] | 621 | /// A type used as an expression (e.g. a type generator parameter)
|
---|
| 622 | class TypeExpr final : public Expr {
|
---|
| 623 | public:
|
---|
| 624 | ptr<Type> type;
|
---|
| 625 |
|
---|
| 626 | TypeExpr( const CodeLocation & loc, const Type * t ) : Expr(loc), type(t) {}
|
---|
| 627 |
|
---|
[69bafd2] | 628 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[264e691] | 629 | private:
|
---|
| 630 | TypeExpr * clone() const override { return new TypeExpr{ *this }; }
|
---|
[f3cc5b6] | 631 | MUTATE_FRIEND
|
---|
[79f7875] | 632 | };
|
---|
| 633 |
|
---|
[94c98f0e] | 634 | /// A name that refers to a generic dimension parameter.
|
---|
[4ec9513] | 635 | class DimensionExpr final : public Expr {
|
---|
| 636 | public:
|
---|
| 637 | std::string name;
|
---|
| 638 |
|
---|
| 639 | DimensionExpr( const CodeLocation & loc, std::string name )
|
---|
| 640 | : Expr( loc ), name( name ) {}
|
---|
| 641 |
|
---|
| 642 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 643 | private:
|
---|
| 644 | DimensionExpr * clone() const override { return new DimensionExpr{ *this }; }
|
---|
| 645 | MUTATE_FRIEND
|
---|
| 646 | };
|
---|
| 647 |
|
---|
[54e41b3] | 648 | /// A GCC "asm constraint operand" used in an asm statement, e.g. `[output] "=f" (result)`.
|
---|
| 649 | /// https://gcc.gnu.org/onlinedocs/gcc-4.7.1/gcc/Machine-Constraints.html#Machine-Constraints
|
---|
| 650 | class AsmExpr final : public Expr {
|
---|
| 651 | public:
|
---|
[665f432] | 652 | std::string inout;
|
---|
[9b4f329] | 653 | ptr<Expr> constraint;
|
---|
| 654 | ptr<Expr> operand;
|
---|
| 655 |
|
---|
[665f432] | 656 | AsmExpr( const CodeLocation & loc, const std::string & io, const Expr * con, const Expr * op )
|
---|
[9b4f329] | 657 | : Expr( loc ), inout( io ), constraint( con ), operand( op ) {}
|
---|
| 658 |
|
---|
| 659 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 660 | private:
|
---|
| 661 | AsmExpr * clone() const override { return new AsmExpr{ *this }; }
|
---|
| 662 | MUTATE_FRIEND
|
---|
| 663 | };
|
---|
| 664 |
|
---|
[17a0228a] | 665 | /// The application of a function to a set of parameters, along with a set of copy constructor
|
---|
[9b4f329] | 666 | /// calls, one for each argument
|
---|
| 667 | class ImplicitCopyCtorExpr final : public Expr {
|
---|
| 668 | public:
|
---|
| 669 | ptr<ApplicationExpr> callExpr;
|
---|
| 670 |
|
---|
| 671 | ImplicitCopyCtorExpr( const CodeLocation& loc, const ApplicationExpr * call )
|
---|
[490fb92e] | 672 | : Expr( loc, call->result ), callExpr(call) { assert( call ); assert(call->result); }
|
---|
[9b4f329] | 673 |
|
---|
| 674 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 675 | private:
|
---|
| 676 | ImplicitCopyCtorExpr * clone() const override { return new ImplicitCopyCtorExpr{ *this }; }
|
---|
| 677 | MUTATE_FRIEND
|
---|
| 678 | };
|
---|
| 679 |
|
---|
| 680 | /// Constructor in expression context, e.g. `int * x = alloc() { 42 };`
|
---|
| 681 | class ConstructorExpr final : public Expr {
|
---|
| 682 | public:
|
---|
| 683 | ptr<Expr> callExpr;
|
---|
| 684 |
|
---|
| 685 | ConstructorExpr( const CodeLocation & loc, const Expr * call );
|
---|
| 686 |
|
---|
| 687 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 688 | private:
|
---|
| 689 | ConstructorExpr * clone() const override { return new ConstructorExpr{ *this }; }
|
---|
| 690 | MUTATE_FRIEND
|
---|
| 691 | };
|
---|
| 692 |
|
---|
| 693 | /// A C99 compound literal, e.g. `(MyType){ a, b, c }`
|
---|
| 694 | class CompoundLiteralExpr final : public Expr {
|
---|
| 695 | public:
|
---|
| 696 | ptr<Init> init;
|
---|
| 697 |
|
---|
| 698 | CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i );
|
---|
| 699 |
|
---|
[cf32116] | 700 | bool get_lvalue() const final;
|
---|
| 701 |
|
---|
[9b4f329] | 702 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 703 | private:
|
---|
| 704 | CompoundLiteralExpr * clone() const override { return new CompoundLiteralExpr{ *this }; }
|
---|
| 705 | MUTATE_FRIEND
|
---|
| 706 | };
|
---|
| 707 |
|
---|
| 708 | /// A range, e.g. `3 ... 5` or `1~10`
|
---|
| 709 | class RangeExpr final : public Expr {
|
---|
| 710 | public:
|
---|
| 711 | ptr<Expr> low;
|
---|
| 712 | ptr<Expr> high;
|
---|
| 713 |
|
---|
| 714 | RangeExpr( const CodeLocation & loc, const Expr * l, const Expr * h )
|
---|
| 715 | : Expr( loc ), low( l ), high( h ) {}
|
---|
| 716 |
|
---|
| 717 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 718 | private:
|
---|
| 719 | RangeExpr * clone() const override { return new RangeExpr{ *this }; }
|
---|
| 720 | MUTATE_FRIEND
|
---|
| 721 | };
|
---|
| 722 |
|
---|
| 723 | /// A tuple expression before resolution, e.g. `[a, b, c]`
|
---|
| 724 | class UntypedTupleExpr final : public Expr {
|
---|
| 725 | public:
|
---|
| 726 | std::vector<ptr<Expr>> exprs;
|
---|
| 727 |
|
---|
| 728 | UntypedTupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
|
---|
| 729 | : Expr( loc ), exprs( std::move(xs) ) {}
|
---|
| 730 |
|
---|
| 731 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 732 | private:
|
---|
| 733 | UntypedTupleExpr * clone() const override { return new UntypedTupleExpr{ *this }; }
|
---|
| 734 | MUTATE_FRIEND
|
---|
| 735 | };
|
---|
| 736 |
|
---|
| 737 | /// A tuple expression after resolution, e.g. `[a, b, c]`
|
---|
| 738 | class TupleExpr final : public Expr {
|
---|
| 739 | public:
|
---|
| 740 | std::vector<ptr<Expr>> exprs;
|
---|
| 741 |
|
---|
| 742 | TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs );
|
---|
| 743 |
|
---|
| 744 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 745 | private:
|
---|
| 746 | TupleExpr * clone() const override { return new TupleExpr{ *this }; }
|
---|
| 747 | MUTATE_FRIEND
|
---|
| 748 | };
|
---|
| 749 |
|
---|
| 750 | /// An element selection operation on a tuple value, e.g. `t.3` after analysis
|
---|
| 751 | class TupleIndexExpr final : public Expr {
|
---|
| 752 | public:
|
---|
| 753 | ptr<Expr> tuple;
|
---|
| 754 | unsigned index;
|
---|
| 755 |
|
---|
| 756 | TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i );
|
---|
| 757 |
|
---|
[cf32116] | 758 | bool get_lvalue() const final;
|
---|
| 759 |
|
---|
[9b4f329] | 760 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 761 | private:
|
---|
| 762 | TupleIndexExpr * clone() const override { return new TupleIndexExpr{ *this }; }
|
---|
| 763 | MUTATE_FRIEND
|
---|
| 764 | };
|
---|
| 765 |
|
---|
[17a0228a] | 766 | /// A multiple- or mass-assignment operation, or a tuple ctor/dtor expression.
|
---|
| 767 | /// multiple-assignment: both sides of the assignment have tuple type,
|
---|
[9b4f329] | 768 | /// e.g. `[a, b, c] = [d, e, f];`
|
---|
| 769 | /// mass-assignment: left-hand side has tuple type and right-hand side does not:
|
---|
| 770 | /// e.g. `[a, b, c] = 42;`
|
---|
| 771 | class TupleAssignExpr final : public Expr {
|
---|
| 772 | public:
|
---|
| 773 | ptr<StmtExpr> stmtExpr;
|
---|
| 774 |
|
---|
[17a0228a] | 775 | TupleAssignExpr(
|
---|
| 776 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
|
---|
[9b4f329] | 777 | std::vector<ptr<ObjectDecl>> && tempDecls );
|
---|
[17a0228a] | 778 |
|
---|
[9b4f329] | 779 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
[20de6fb] | 780 |
|
---|
| 781 | friend class ::ConverterOldToNew;
|
---|
| 782 |
|
---|
[9b4f329] | 783 | private:
|
---|
| 784 | TupleAssignExpr * clone() const override { return new TupleAssignExpr{ *this }; }
|
---|
[20de6fb] | 785 | TupleAssignExpr( const CodeLocation & loc, const Type * result, const StmtExpr * s );
|
---|
| 786 |
|
---|
[9b4f329] | 787 | MUTATE_FRIEND
|
---|
| 788 | };
|
---|
| 789 |
|
---|
| 790 | /// A GCC "statement expression", e.g. `({ int x = 5; x })`
|
---|
| 791 | class StmtExpr final : public Expr {
|
---|
| 792 | public:
|
---|
| 793 | ptr<CompoundStmt> stmts;
|
---|
| 794 | std::vector<ptr<ObjectDecl>> returnDecls; ///< return variable(s) for statement expression
|
---|
| 795 | std::vector<ptr<Expr>> dtors; ///< destructor(s) for return variable(s)
|
---|
| 796 |
|
---|
[490fb92e] | 797 | readonly<ExprStmt> resultExpr;
|
---|
| 798 |
|
---|
[9b4f329] | 799 | StmtExpr( const CodeLocation & loc, const CompoundStmt * ss );
|
---|
| 800 |
|
---|
| 801 | /// Set the result type of this StmtExpr based on its body
|
---|
| 802 | void computeResult();
|
---|
| 803 |
|
---|
| 804 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 805 | private:
|
---|
| 806 | StmtExpr * clone() const override { return new StmtExpr{ *this }; }
|
---|
| 807 | MUTATE_FRIEND
|
---|
| 808 | };
|
---|
| 809 |
|
---|
| 810 | /// An expression which must only be evaluated once
|
---|
[3249dd8b] | 811 | class UniqueExpr final : public Expr {
|
---|
[9b4f329] | 812 | static unsigned long long nextId;
|
---|
| 813 | public:
|
---|
| 814 | ptr<Expr> expr;
|
---|
[7edd5c1] | 815 | readonly<ObjectDecl> object;
|
---|
[9b4f329] | 816 | ptr<VariableExpr> var;
|
---|
| 817 | unsigned long long id;
|
---|
| 818 |
|
---|
[d76f32c] | 819 | UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i = -1ull );
|
---|
[9b4f329] | 820 |
|
---|
| 821 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 822 | private:
|
---|
| 823 | UniqueExpr * clone() const override { return new UniqueExpr{ *this }; }
|
---|
| 824 | MUTATE_FRIEND
|
---|
| 825 | };
|
---|
| 826 |
|
---|
| 827 | /// One option for resolving an initializer expression
|
---|
| 828 | struct InitAlternative {
|
---|
| 829 | ptr<Type> type;
|
---|
| 830 | ptr<Designation> designation;
|
---|
| 831 |
|
---|
| 832 | InitAlternative() = default;
|
---|
| 833 | InitAlternative( const Type * ty, const Designation * des ) : type( ty ), designation( des ) {}
|
---|
| 834 | };
|
---|
| 835 |
|
---|
| 836 | /// Pre-resolution initializer expression
|
---|
| 837 | class UntypedInitExpr final : public Expr {
|
---|
| 838 | public:
|
---|
| 839 | ptr<Expr> expr;
|
---|
[60aaa51d] | 840 | std::deque<InitAlternative> initAlts;
|
---|
[9b4f329] | 841 |
|
---|
[60aaa51d] | 842 | UntypedInitExpr( const CodeLocation & loc, const Expr * e, std::deque<InitAlternative> && as )
|
---|
[9b4f329] | 843 | : Expr( loc ), expr( e ), initAlts( std::move(as) ) {}
|
---|
| 844 |
|
---|
| 845 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 846 | private:
|
---|
| 847 | UntypedInitExpr * clone() const override { return new UntypedInitExpr{ *this }; }
|
---|
| 848 | MUTATE_FRIEND
|
---|
| 849 | };
|
---|
| 850 |
|
---|
| 851 | /// Post-resolution initializer expression
|
---|
| 852 | class InitExpr final : public Expr {
|
---|
| 853 | public:
|
---|
| 854 | ptr<Expr> expr;
|
---|
| 855 | ptr<Designation> designation;
|
---|
| 856 |
|
---|
| 857 | InitExpr( const CodeLocation & loc, const Expr * e, const Designation * des )
|
---|
| 858 | : Expr( loc, e->result ), expr( e ), designation( des ) {}
|
---|
| 859 |
|
---|
| 860 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 861 | private:
|
---|
| 862 | InitExpr * clone() const override { return new InitExpr{ *this }; }
|
---|
| 863 | MUTATE_FRIEND
|
---|
| 864 | };
|
---|
| 865 |
|
---|
| 866 | /// Expression containing a deleted identifier.
|
---|
| 867 | /// Internal to resolver.
|
---|
| 868 | class DeletedExpr final : public Expr {
|
---|
| 869 | public:
|
---|
| 870 | ptr<Expr> expr;
|
---|
[e67991f] | 871 | readonly<Decl> deleteStmt;
|
---|
[9b4f329] | 872 |
|
---|
[e67991f] | 873 | DeletedExpr( const CodeLocation & loc, const Expr * e, const Decl * del )
|
---|
[9b4f329] | 874 | : Expr( loc, e->result ), expr( e ), deleteStmt( del ) { assert( expr->result ); }
|
---|
| 875 |
|
---|
| 876 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 877 | private:
|
---|
| 878 | DeletedExpr * clone() const override { return new DeletedExpr{ *this }; }
|
---|
| 879 | MUTATE_FRIEND
|
---|
| 880 | };
|
---|
| 881 |
|
---|
| 882 | /// Use of a default argument.
|
---|
| 883 | /// Internal to resolver.
|
---|
| 884 | class DefaultArgExpr final : public Expr {
|
---|
| 885 | public:
|
---|
| 886 | ptr<Expr> expr;
|
---|
| 887 |
|
---|
| 888 | DefaultArgExpr( const CodeLocation & loc, const Expr * e )
|
---|
| 889 | : Expr( loc, e->result ), expr( e ) { assert( e->result ); }
|
---|
| 890 |
|
---|
| 891 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 892 | private:
|
---|
| 893 | DefaultArgExpr * clone() const override { return new DefaultArgExpr{ *this }; }
|
---|
| 894 | MUTATE_FRIEND
|
---|
| 895 | };
|
---|
| 896 |
|
---|
| 897 | /// C11 _Generic expression
|
---|
| 898 | class GenericExpr final : public Expr {
|
---|
| 899 | public:
|
---|
| 900 | /// One arm of the _Generic expr
|
---|
| 901 | struct Association {
|
---|
| 902 | ptr<Type> type;
|
---|
| 903 | ptr<Expr> expr;
|
---|
| 904 |
|
---|
| 905 | Association() = default;
|
---|
| 906 | // default case
|
---|
| 907 | Association( const Expr * e ) : type(), expr( e ) {}
|
---|
| 908 | // non-default case
|
---|
| 909 | Association( const Type * t, const Expr * e ) : type( t ), expr( e ) {}
|
---|
| 910 | };
|
---|
| 911 |
|
---|
| 912 | ptr<Expr> control;
|
---|
| 913 | std::vector<Association> associations;
|
---|
| 914 |
|
---|
| 915 | GenericExpr( const CodeLocation & loc, const Expr * ctrl, std::vector<Association> && assns )
|
---|
| 916 | : Expr( loc ), control( ctrl ), associations( std::move(assns) ) {}
|
---|
| 917 |
|
---|
| 918 | const Expr * accept( Visitor & v ) const override { return v.visit( this ); }
|
---|
| 919 | private:
|
---|
| 920 | GenericExpr * clone() const override { return new GenericExpr{ *this }; }
|
---|
| 921 | MUTATE_FRIEND
|
---|
[54e41b3] | 922 | };
|
---|
[6d51bd7] | 923 |
|
---|
[79f7875] | 924 | }
|
---|
| 925 |
|
---|
[f3cc5b6] | 926 | #undef MUTATE_FRIEND
|
---|
| 927 |
|
---|
[79f7875] | 928 | // Local Variables: //
|
---|
| 929 | // tab-width: 4 //
|
---|
| 930 | // mode: c++ //
|
---|
| 931 | // compile-command: "make install" //
|
---|
| 932 | // End: //
|
---|