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