[54e41b3] | 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.cpp -- |
---|
| 8 | // |
---|
| 9 | // Author : Aaron B. Moss |
---|
| 10 | // Created On : Wed May 15 17:00:00 2019 |
---|
[312029a] | 11 | // Last Modified By : Peter A. Buhr |
---|
[d76f32c] | 12 | // Created On : Thr Jun 13 13:38:00 2019 |
---|
[312029a] | 13 | // Update Count : 6 |
---|
[54e41b3] | 14 | // |
---|
| 15 | |
---|
| 16 | #include "Expr.hpp" |
---|
| 17 | |
---|
| 18 | #include <cassert> // for strict_dynamic_cast |
---|
| 19 | #include <string> // for to_string |
---|
| 20 | #include <vector> |
---|
| 21 | |
---|
[2890212] | 22 | #include "Copy.hpp" // for shallowCopy |
---|
[417117e] | 23 | #include "Eval.hpp" // for call |
---|
[d8938622] | 24 | #include "GenericSubstitution.hpp" |
---|
[cf32116] | 25 | #include "LinkageSpec.hpp" |
---|
[9b4f329] | 26 | #include "Stmt.hpp" |
---|
[54e41b3] | 27 | #include "Type.hpp" |
---|
[d8938622] | 28 | #include "TypeSubstitution.hpp" |
---|
[733074e] | 29 | #include "Common/utility.h" |
---|
[54e41b3] | 30 | #include "Common/SemanticError.h" |
---|
| 31 | #include "GenPoly/Lvalue.h" // for referencesPermissable |
---|
[cf32116] | 32 | #include "InitTweak/InitTweak.h" // for getFunction, getPointerBase |
---|
[54e41b3] | 33 | #include "ResolvExpr/typeops.h" // for extractResultType |
---|
[9b4f329] | 34 | #include "Tuples/Tuples.h" // for makeTupleType |
---|
[54e41b3] | 35 | |
---|
| 36 | namespace ast { |
---|
| 37 | |
---|
[cf32116] | 38 | namespace { |
---|
| 39 | std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"}; |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | // --- Expr |
---|
| 43 | bool Expr::get_lvalue() const { |
---|
| 44 | return false; |
---|
| 45 | } |
---|
| 46 | |
---|
[54e41b3] | 47 | // --- ApplicationExpr |
---|
| 48 | |
---|
[87701b6] | 49 | ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f, |
---|
| 50 | std::vector<ptr<Expr>> && as ) |
---|
| 51 | : Expr( loc ), func( f ), args( std::move(as) ) { |
---|
[54e41b3] | 52 | // ensure that `ApplicationExpr` result type is `FuncExpr` |
---|
| 53 | const PointerType * pt = strict_dynamic_cast< const PointerType * >( f->result.get() ); |
---|
| 54 | const FunctionType * fn = strict_dynamic_cast< const FunctionType * >( pt->base.get() ); |
---|
| 55 | |
---|
| 56 | result = ResolvExpr::extractResultType( fn ); |
---|
| 57 | assert( result ); |
---|
| 58 | } |
---|
| 59 | |
---|
[cf32116] | 60 | bool ApplicationExpr::get_lvalue() const { |
---|
| 61 | if ( const DeclWithType * func = InitTweak::getFunction( this ) ) { |
---|
| 62 | return func->linkage == Linkage::Intrinsic && lvalueFunctionNames.count( func->name ); |
---|
| 63 | } |
---|
| 64 | return false; |
---|
| 65 | } |
---|
| 66 | |
---|
[54e41b3] | 67 | // --- UntypedExpr |
---|
| 68 | |
---|
| 69 | UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) { |
---|
| 70 | assert( arg ); |
---|
| 71 | |
---|
[417117e] | 72 | UntypedExpr * ret = call( loc, "*?", arg ); |
---|
[54e41b3] | 73 | if ( const Type * ty = arg->result ) { |
---|
| 74 | const Type * base = InitTweak::getPointerBase( ty ); |
---|
| 75 | assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() ); |
---|
| 76 | |
---|
| 77 | if ( GenPoly::referencesPermissable() ) { |
---|
| 78 | // if references are still allowed in the AST, dereference returns a reference |
---|
| 79 | ret->result = new ReferenceType{ base }; |
---|
| 80 | } else { |
---|
[87701b6] | 81 | // references have been removed, in which case dereference returns an lvalue of the |
---|
[54e41b3] | 82 | // base type |
---|
[d76c588] | 83 | ret->result = base; |
---|
[54e41b3] | 84 | } |
---|
| 85 | } |
---|
| 86 | return ret; |
---|
| 87 | } |
---|
| 88 | |
---|
[cf32116] | 89 | bool UntypedExpr::get_lvalue() const { |
---|
| 90 | std::string fname = InitTweak::getFunctionName( this ); |
---|
| 91 | return lvalueFunctionNames.count( fname ); |
---|
| 92 | } |
---|
| 93 | |
---|
[54e41b3] | 94 | UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { |
---|
| 95 | assert( lhs && rhs ); |
---|
| 96 | |
---|
[417117e] | 97 | UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); |
---|
[54e41b3] | 98 | if ( lhs->result && rhs->result ) { |
---|
| 99 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
| 100 | // so the result is the type of the RHS |
---|
| 101 | ret->result = rhs->result; |
---|
| 102 | } |
---|
| 103 | return ret; |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | // --- AddressExpr |
---|
| 107 | |
---|
| 108 | // Address expressions are typed based on the following inference rules: |
---|
| 109 | // E : lvalue T &..& (n references) |
---|
| 110 | // &E : T *&..& (n references) |
---|
| 111 | // |
---|
| 112 | // E : T &..& (m references) |
---|
| 113 | // &E : T *&..& (m-1 references) |
---|
| 114 | |
---|
| 115 | namespace { |
---|
| 116 | /// The type of the address of a type. |
---|
| 117 | /// Caller is responsible for managing returned memory |
---|
| 118 | Type * addrType( const Type * type ) { |
---|
| 119 | if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( type ) ) { |
---|
| 120 | return new ReferenceType{ addrType( refType->base ), refType->qualifiers }; |
---|
| 121 | } else { |
---|
| 122 | return new PointerType{ type }; |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { |
---|
| 128 | if ( arg->result ) { |
---|
[cf32116] | 129 | if ( arg->get_lvalue() ) { |
---|
[54e41b3] | 130 | // lvalue, retains all levels of reference, and gains a pointer inside the references |
---|
| 131 | Type * res = addrType( arg->result ); |
---|
| 132 | result = res; |
---|
| 133 | } else { |
---|
| 134 | // taking address of non-lvalue, must be a reference, loses one layer of reference |
---|
[87701b6] | 135 | if ( const ReferenceType * refType = |
---|
[54e41b3] | 136 | dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { |
---|
| 137 | Type * res = addrType( refType->base ); |
---|
| 138 | result = res; |
---|
| 139 | } else { |
---|
[10a1225] | 140 | SemanticError( loc, arg->result.get(), |
---|
[54e41b3] | 141 | "Attempt to take address of non-lvalue expression: " ); |
---|
| 142 | } |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | } |
---|
| 146 | |
---|
| 147 | // --- LabelAddressExpr |
---|
| 148 | |
---|
| 149 | // label address always has type `void*` |
---|
[87701b6] | 150 | LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a ) |
---|
[54e41b3] | 151 | : Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {} |
---|
| 152 | |
---|
| 153 | // --- CastExpr |
---|
| 154 | |
---|
[87701b6] | 155 | CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g ) |
---|
[54e41b3] | 156 | : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} |
---|
| 157 | |
---|
[cf32116] | 158 | bool CastExpr::get_lvalue() const { |
---|
| 159 | // This is actually wrong by C, but it works with our current set-up. |
---|
| 160 | return arg->get_lvalue(); |
---|
| 161 | } |
---|
| 162 | |
---|
[54e41b3] | 163 | // --- KeywordCastExpr |
---|
| 164 | |
---|
[312029a] | 165 | const char * KeywordCastExpr::targetString() const { |
---|
| 166 | return AggregateDecl::aggrString( target ); |
---|
[54e41b3] | 167 | } |
---|
| 168 | |
---|
[cf32116] | 169 | // --- UntypedMemberExpr |
---|
| 170 | |
---|
| 171 | bool UntypedMemberExpr::get_lvalue() const { |
---|
| 172 | return aggregate->get_lvalue(); |
---|
| 173 | } |
---|
| 174 | |
---|
[54e41b3] | 175 | // --- MemberExpr |
---|
| 176 | |
---|
| 177 | MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ) |
---|
| 178 | : Expr( loc ), member( mem ), aggregate( agg ) { |
---|
| 179 | assert( member ); |
---|
| 180 | assert( aggregate ); |
---|
| 181 | assert( aggregate->result ); |
---|
| 182 | |
---|
[ae265b55] | 183 | // Deep copy on result type avoids mutation on transitively multiply referenced object. |
---|
| 184 | // |
---|
| 185 | // Example, adapted from parts of builtins and bootloader: |
---|
| 186 | // |
---|
| 187 | // forall(dtype T) |
---|
| 188 | // struct __Destructor { |
---|
| 189 | // T * object; |
---|
| 190 | // void (*dtor)(T *); |
---|
| 191 | // }; |
---|
| 192 | // |
---|
| 193 | // forall(dtype S) |
---|
| 194 | // void foo(__Destructor(S) &d) { |
---|
| 195 | // if (d.dtor) { // here |
---|
| 196 | // } |
---|
| 197 | // } |
---|
| 198 | // |
---|
| 199 | // Let e be the "d.dtor" guard espression, which is MemberExpr after resolve. Let d be the |
---|
| 200 | // declaration of member __Destructor.dtor (an ObjectDecl), as accessed via the top-level |
---|
| 201 | // declaration of __Destructor. Consider the types e.result and d.type. In the old AST, one |
---|
| 202 | // is a clone of the other. Ordinary new-AST use would set them up as a multiply-referenced |
---|
| 203 | // object. |
---|
| 204 | // |
---|
| 205 | // e.result: PointerType |
---|
| 206 | // .base: FunctionType |
---|
| 207 | // .params.front(): ObjectDecl, the anonymous parameter of type T* |
---|
| 208 | // .type: PointerType |
---|
| 209 | // .base: TypeInstType |
---|
| 210 | // let x = that |
---|
| 211 | // let y = similar, except start from d.type |
---|
| 212 | // |
---|
| 213 | // Consider two code lines down, genericSubstitution(...).apply(result). |
---|
| 214 | // |
---|
| 215 | // Applying this chosen-candidate's type substitution means modifying x, substituting |
---|
| 216 | // S for T. This mutation should affect x and not y. |
---|
| 217 | |
---|
| 218 | result = deepCopy(mem->get_type()); |
---|
| 219 | |
---|
[335f2d8] | 220 | // substitute aggregate generic parameters into member type |
---|
[60aaa51d] | 221 | genericSubstitution( aggregate->result ).apply( result ); |
---|
[3f3bfe5a] | 222 | // ensure appropriate restrictions from aggregate type |
---|
| 223 | add_qualifiers( result, aggregate->result->qualifiers ); |
---|
[54e41b3] | 224 | } |
---|
| 225 | |
---|
[ae265b55] | 226 | MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg, |
---|
| 227 | MemberExpr::NoOpConstruction overloadSelector ) |
---|
| 228 | : Expr( loc ), member( mem ), aggregate( agg ) { |
---|
| 229 | assert( member ); |
---|
| 230 | assert( aggregate ); |
---|
| 231 | assert( aggregate->result ); |
---|
| 232 | (void) overloadSelector; |
---|
| 233 | } |
---|
| 234 | |
---|
[cf32116] | 235 | bool MemberExpr::get_lvalue() const { |
---|
| 236 | // This is actually wrong by C, but it works with our current set-up. |
---|
| 237 | return true; |
---|
| 238 | } |
---|
| 239 | |
---|
[54e41b3] | 240 | // --- VariableExpr |
---|
| 241 | |
---|
[546e712] | 242 | VariableExpr::VariableExpr( const CodeLocation & loc ) |
---|
| 243 | : Expr( loc ), var( nullptr ) {} |
---|
| 244 | |
---|
[54e41b3] | 245 | VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) |
---|
| 246 | : Expr( loc ), var( v ) { |
---|
| 247 | assert( var ); |
---|
| 248 | assert( var->get_type() ); |
---|
[3f3bfe5a] | 249 | result = shallowCopy( var->get_type() ); |
---|
[54e41b3] | 250 | } |
---|
| 251 | |
---|
[cf32116] | 252 | bool VariableExpr::get_lvalue() const { |
---|
| 253 | // It isn't always an lvalue, but it is never an rvalue. |
---|
| 254 | return true; |
---|
| 255 | } |
---|
| 256 | |
---|
[87701b6] | 257 | VariableExpr * VariableExpr::functionPointer( |
---|
[54e41b3] | 258 | const CodeLocation & loc, const FunctionDecl * decl ) { |
---|
| 259 | // wrap usually-determined result type in a pointer |
---|
| 260 | VariableExpr * funcExpr = new VariableExpr{ loc, decl }; |
---|
| 261 | funcExpr->result = new PointerType{ funcExpr->result }; |
---|
| 262 | return funcExpr; |
---|
| 263 | } |
---|
| 264 | |
---|
| 265 | // --- ConstantExpr |
---|
| 266 | |
---|
| 267 | long long int ConstantExpr::intValue() const { |
---|
| 268 | if ( const BasicType * bty = result.as< BasicType >() ) { |
---|
| 269 | if ( bty->isInteger() ) { |
---|
[c36298d] | 270 | assert(ival); |
---|
| 271 | return ival.value(); |
---|
[54e41b3] | 272 | } |
---|
| 273 | } else if ( result.as< ZeroType >() ) { |
---|
| 274 | return 0; |
---|
| 275 | } else if ( result.as< OneType >() ) { |
---|
| 276 | return 1; |
---|
| 277 | } |
---|
| 278 | SemanticError( this, "Constant expression of non-integral type " ); |
---|
| 279 | } |
---|
| 280 | |
---|
| 281 | ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) { |
---|
[87701b6] | 282 | return new ConstantExpr{ |
---|
[54e41b3] | 283 | loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b }; |
---|
| 284 | } |
---|
| 285 | |
---|
| 286 | ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) { |
---|
[87701b6] | 287 | return new ConstantExpr{ |
---|
[54e41b3] | 288 | loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i }; |
---|
| 289 | } |
---|
| 290 | |
---|
| 291 | ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) { |
---|
[87701b6] | 292 | return new ConstantExpr{ |
---|
| 293 | loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ), |
---|
[54e41b3] | 294 | (unsigned long long)i }; |
---|
| 295 | } |
---|
| 296 | |
---|
| 297 | ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) { |
---|
| 298 | return new ConstantExpr{ |
---|
| 299 | loc, ptrType ? ptrType : new PointerType{ new VoidType{} }, "0", (unsigned long long)0 }; |
---|
| 300 | } |
---|
| 301 | |
---|
| 302 | // --- SizeofExpr |
---|
| 303 | |
---|
| 304 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e ) |
---|
| 305 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
| 306 | |
---|
| 307 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t ) |
---|
| 308 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
| 309 | |
---|
| 310 | // --- AlignofExpr |
---|
| 311 | |
---|
| 312 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e ) |
---|
| 313 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
| 314 | |
---|
| 315 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t ) |
---|
| 316 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
| 317 | |
---|
| 318 | // --- OffsetofExpr |
---|
| 319 | |
---|
| 320 | OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ) |
---|
| 321 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) { |
---|
| 322 | assert( type ); |
---|
| 323 | assert( member ); |
---|
| 324 | } |
---|
| 325 | |
---|
| 326 | // --- OffsetPackExpr |
---|
| 327 | |
---|
| 328 | OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ) |
---|
[87701b6] | 329 | : Expr( loc, new ArrayType{ |
---|
| 330 | new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim } |
---|
[54e41b3] | 331 | ), type( ty ) { |
---|
| 332 | assert( type ); |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | // --- LogicalExpr |
---|
| 336 | |
---|
[87701b6] | 337 | LogicalExpr::LogicalExpr( |
---|
[54e41b3] | 338 | const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ) |
---|
| 339 | : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} |
---|
| 340 | |
---|
[cf32116] | 341 | // --- CommaExpr |
---|
| 342 | bool CommaExpr::get_lvalue() const { |
---|
| 343 | // This is wrong by C, but the current implementation uses it. |
---|
| 344 | // (ex: Specialize, Lvalue and Box) |
---|
| 345 | return arg2->get_lvalue(); |
---|
| 346 | } |
---|
| 347 | |
---|
[9b4f329] | 348 | // --- ConstructorExpr |
---|
| 349 | |
---|
[10a1225] | 350 | ConstructorExpr::ConstructorExpr( const CodeLocation & loc, const Expr * call ) |
---|
[9b4f329] | 351 | : Expr( loc ), callExpr( call ) { |
---|
[10a1225] | 352 | // allow resolver to type a constructor used as an expression if it has the same type as its |
---|
[9b4f329] | 353 | // first argument |
---|
| 354 | assert( callExpr ); |
---|
| 355 | const Expr * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
| 356 | assert( arg ); |
---|
| 357 | result = arg->result; |
---|
| 358 | } |
---|
| 359 | |
---|
| 360 | // --- CompoundLiteralExpr |
---|
| 361 | |
---|
| 362 | CompoundLiteralExpr::CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i ) |
---|
| 363 | : Expr( loc ), init( i ) { |
---|
| 364 | assert( t && i ); |
---|
[d76c588] | 365 | result = t; |
---|
[9b4f329] | 366 | } |
---|
| 367 | |
---|
[cf32116] | 368 | bool CompoundLiteralExpr::get_lvalue() const { |
---|
| 369 | return true; |
---|
| 370 | } |
---|
| 371 | |
---|
[9b4f329] | 372 | // --- TupleExpr |
---|
| 373 | |
---|
| 374 | TupleExpr::TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ) |
---|
| 375 | : Expr( loc, Tuples::makeTupleType( xs ) ), exprs( xs ) {} |
---|
| 376 | |
---|
| 377 | // --- TupleIndexExpr |
---|
| 378 | |
---|
| 379 | TupleIndexExpr::TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i ) |
---|
| 380 | : Expr( loc ), tuple( t ), index( i ) { |
---|
[10a1225] | 381 | const TupleType * type = strict_dynamic_cast< const TupleType * >( tuple->result.get() ); |
---|
[9b4f329] | 382 | assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested " |
---|
| 383 | "index %d in expr %s", type->size(), index, toString( tuple ).c_str() ); |
---|
| 384 | // like MemberExpr, TupleIndexExpr is always an lvalue |
---|
[d76c588] | 385 | result = type->types[ index ]; |
---|
[9b4f329] | 386 | } |
---|
| 387 | |
---|
[cf32116] | 388 | bool TupleIndexExpr::get_lvalue() const { |
---|
| 389 | return tuple->get_lvalue(); |
---|
| 390 | } |
---|
| 391 | |
---|
[9b4f329] | 392 | // --- TupleAssignExpr |
---|
| 393 | |
---|
[10a1225] | 394 | TupleAssignExpr::TupleAssignExpr( |
---|
| 395 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns, |
---|
[9b4f329] | 396 | std::vector<ptr<ObjectDecl>> && tempDecls ) |
---|
| 397 | : Expr( loc, Tuples::makeTupleType( assigns ) ), stmtExpr() { |
---|
[10a1225] | 398 | // convert internally into a StmtExpr which contains the declarations and produces the tuple of |
---|
[9b4f329] | 399 | // the assignments |
---|
| 400 | std::list<ptr<Stmt>> stmts; |
---|
| 401 | for ( const ObjectDecl * obj : tempDecls ) { |
---|
| 402 | stmts.emplace_back( new DeclStmt{ loc, obj } ); |
---|
| 403 | } |
---|
| 404 | TupleExpr * tupleExpr = new TupleExpr{ loc, std::move(assigns) }; |
---|
| 405 | assert( tupleExpr->result ); |
---|
| 406 | stmts.emplace_back( new ExprStmt{ loc, tupleExpr } ); |
---|
| 407 | stmtExpr = new StmtExpr{ loc, new CompoundStmt{ loc, std::move(stmts) } }; |
---|
| 408 | } |
---|
| 409 | |
---|
[0b57626] | 410 | TupleAssignExpr::TupleAssignExpr( |
---|
[20de6fb] | 411 | const CodeLocation & loc, const Type * result, const StmtExpr * s ) |
---|
| 412 | : Expr( loc, result ), stmtExpr() { |
---|
| 413 | stmtExpr = s; |
---|
| 414 | } |
---|
| 415 | |
---|
[9b4f329] | 416 | // --- StmtExpr |
---|
| 417 | |
---|
[10a1225] | 418 | StmtExpr::StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ) |
---|
[9b4f329] | 419 | : Expr( loc ), stmts( ss ), returnDecls(), dtors() { computeResult(); } |
---|
| 420 | |
---|
| 421 | void StmtExpr::computeResult() { |
---|
| 422 | assert( stmts ); |
---|
| 423 | const std::list<ptr<Stmt>> & body = stmts->kids; |
---|
| 424 | if ( ! returnDecls.empty() ) { |
---|
[10a1225] | 425 | // prioritize return decl for result type, since if a return decl exists, then the StmtExpr |
---|
[9b4f329] | 426 | // is currently in an intermediate state where the body will always give a void result type |
---|
| 427 | result = returnDecls.front()->get_type(); |
---|
| 428 | } else if ( ! body.empty() ) { |
---|
| 429 | if ( const ExprStmt * exprStmt = body.back().as< ExprStmt >() ) { |
---|
| 430 | result = exprStmt->expr->result; |
---|
| 431 | } |
---|
| 432 | } |
---|
| 433 | // ensure a result type exists |
---|
| 434 | if ( ! result ) { result = new VoidType{}; } |
---|
| 435 | } |
---|
| 436 | |
---|
| 437 | // --- UniqueExpr |
---|
| 438 | |
---|
| 439 | unsigned long long UniqueExpr::nextId = 0; |
---|
| 440 | |
---|
[10a1225] | 441 | UniqueExpr::UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i ) |
---|
[d76f32c] | 442 | : Expr( loc, e->result ), expr( e ), id( i ) { |
---|
[9b4f329] | 443 | assert( expr ); |
---|
[10a1225] | 444 | if ( id == -1ull ) { |
---|
| 445 | assert( nextId != -1ull ); |
---|
[9b4f329] | 446 | id = nextId++; |
---|
| 447 | } |
---|
[54e41b3] | 448 | } |
---|
| 449 | |
---|
[10a1225] | 450 | } |
---|
| 451 | |
---|
[54e41b3] | 452 | // Local Variables: // |
---|
| 453 | // tab-width: 4 // |
---|
| 454 | // mode: c++ // |
---|
| 455 | // compile-command: "make install" // |
---|
[d76f32c] | 456 | // End: // |
---|