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