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