[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 |
---|
[d76f32c] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Created On : Thr Jun 13 13:38:00 2019 |
---|
| 13 | // Update Count : 2 |
---|
[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 | |
---|
[417117e] | 22 | #include "Eval.hpp" // for call |
---|
[d8938622] | 23 | #include "GenericSubstitution.hpp" |
---|
[9b4f329] | 24 | #include "Stmt.hpp" |
---|
[54e41b3] | 25 | #include "Type.hpp" |
---|
[d8938622] | 26 | #include "TypeSubstitution.hpp" |
---|
[733074e] | 27 | #include "Common/utility.h" |
---|
[54e41b3] | 28 | #include "Common/SemanticError.h" |
---|
| 29 | #include "GenPoly/Lvalue.h" // for referencesPermissable |
---|
| 30 | #include "InitTweak/InitTweak.h" // for getPointerBase |
---|
| 31 | #include "ResolvExpr/typeops.h" // for extractResultType |
---|
[9b4f329] | 32 | #include "Tuples/Tuples.h" // for makeTupleType |
---|
[54e41b3] | 33 | |
---|
| 34 | namespace ast { |
---|
| 35 | |
---|
| 36 | // --- ApplicationExpr |
---|
| 37 | |
---|
[87701b6] | 38 | ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f, |
---|
| 39 | std::vector<ptr<Expr>> && as ) |
---|
| 40 | : Expr( loc ), func( f ), args( std::move(as) ) { |
---|
[54e41b3] | 41 | // ensure that `ApplicationExpr` result type is `FuncExpr` |
---|
| 42 | const PointerType * pt = strict_dynamic_cast< const PointerType * >( f->result.get() ); |
---|
| 43 | const FunctionType * fn = strict_dynamic_cast< const FunctionType * >( pt->base.get() ); |
---|
| 44 | |
---|
| 45 | result = ResolvExpr::extractResultType( fn ); |
---|
| 46 | assert( result ); |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | // --- UntypedExpr |
---|
| 50 | |
---|
| 51 | UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) { |
---|
| 52 | assert( arg ); |
---|
| 53 | |
---|
[417117e] | 54 | UntypedExpr * ret = call( loc, "*?", arg ); |
---|
[54e41b3] | 55 | if ( const Type * ty = arg->result ) { |
---|
| 56 | const Type * base = InitTweak::getPointerBase( ty ); |
---|
| 57 | assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() ); |
---|
| 58 | |
---|
| 59 | if ( GenPoly::referencesPermissable() ) { |
---|
| 60 | // if references are still allowed in the AST, dereference returns a reference |
---|
| 61 | ret->result = new ReferenceType{ base }; |
---|
| 62 | } else { |
---|
[87701b6] | 63 | // references have been removed, in which case dereference returns an lvalue of the |
---|
[54e41b3] | 64 | // base type |
---|
[d76c588] | 65 | ret->result = base; |
---|
[ee574a2] | 66 | add_qualifiers( ret->result, CV::Lvalue ); |
---|
[54e41b3] | 67 | } |
---|
| 68 | } |
---|
| 69 | return ret; |
---|
| 70 | } |
---|
| 71 | |
---|
| 72 | UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { |
---|
| 73 | assert( lhs && rhs ); |
---|
| 74 | |
---|
[417117e] | 75 | UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); |
---|
[54e41b3] | 76 | if ( lhs->result && rhs->result ) { |
---|
| 77 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
| 78 | // so the result is the type of the RHS |
---|
| 79 | ret->result = rhs->result; |
---|
| 80 | } |
---|
| 81 | return ret; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | // --- AddressExpr |
---|
| 85 | |
---|
| 86 | // Address expressions are typed based on the following inference rules: |
---|
| 87 | // E : lvalue T &..& (n references) |
---|
| 88 | // &E : T *&..& (n references) |
---|
| 89 | // |
---|
| 90 | // E : T &..& (m references) |
---|
| 91 | // &E : T *&..& (m-1 references) |
---|
| 92 | |
---|
| 93 | namespace { |
---|
| 94 | /// The type of the address of a type. |
---|
| 95 | /// Caller is responsible for managing returned memory |
---|
| 96 | Type * addrType( const Type * type ) { |
---|
| 97 | if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( type ) ) { |
---|
| 98 | return new ReferenceType{ addrType( refType->base ), refType->qualifiers }; |
---|
| 99 | } else { |
---|
| 100 | return new PointerType{ type }; |
---|
| 101 | } |
---|
| 102 | } |
---|
| 103 | } |
---|
| 104 | |
---|
| 105 | AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { |
---|
| 106 | if ( arg->result ) { |
---|
| 107 | if ( arg->result->is_lvalue() ) { |
---|
| 108 | // lvalue, retains all levels of reference, and gains a pointer inside the references |
---|
| 109 | Type * res = addrType( arg->result ); |
---|
| 110 | res->set_lvalue( false ); // result of & is never an lvalue |
---|
| 111 | result = res; |
---|
| 112 | } else { |
---|
| 113 | // taking address of non-lvalue, must be a reference, loses one layer of reference |
---|
[87701b6] | 114 | if ( const ReferenceType * refType = |
---|
[54e41b3] | 115 | dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { |
---|
| 116 | Type * res = addrType( refType->base ); |
---|
| 117 | res->set_lvalue( false ); // result of & is never an lvalue |
---|
| 118 | result = res; |
---|
| 119 | } else { |
---|
[10a1225] | 120 | SemanticError( loc, arg->result.get(), |
---|
[54e41b3] | 121 | "Attempt to take address of non-lvalue expression: " ); |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | // --- LabelAddressExpr |
---|
| 128 | |
---|
| 129 | // label address always has type `void*` |
---|
[87701b6] | 130 | LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a ) |
---|
[54e41b3] | 131 | : Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {} |
---|
| 132 | |
---|
| 133 | // --- CastExpr |
---|
| 134 | |
---|
[87701b6] | 135 | CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g ) |
---|
[54e41b3] | 136 | : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} |
---|
| 137 | |
---|
| 138 | // --- KeywordCastExpr |
---|
| 139 | |
---|
| 140 | const std::string & KeywordCastExpr::targetString() const { |
---|
| 141 | static const std::string targetStrs[] = { |
---|
| 142 | "coroutine", "thread", "monitor" |
---|
| 143 | }; |
---|
| 144 | static_assert( |
---|
| 145 | (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS), |
---|
| 146 | "Each KeywordCastExpr::Target should have a corresponding string representation" |
---|
| 147 | ); |
---|
| 148 | return targetStrs[(unsigned long)target]; |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | // --- MemberExpr |
---|
| 152 | |
---|
| 153 | MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ) |
---|
| 154 | : Expr( loc ), member( mem ), aggregate( agg ) { |
---|
| 155 | assert( member ); |
---|
| 156 | assert( aggregate ); |
---|
| 157 | assert( aggregate->result ); |
---|
| 158 | |
---|
[335f2d8] | 159 | // take ownership of member type |
---|
| 160 | result = mem->get_type(); |
---|
| 161 | // substitute aggregate generic parameters into member type |
---|
[60aaa51d] | 162 | genericSubstitution( aggregate->result ).apply( result ); |
---|
[335f2d8] | 163 | // ensure lvalue and appropriate restrictions from aggregate type |
---|
[ee574a2] | 164 | add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue ); |
---|
[54e41b3] | 165 | } |
---|
| 166 | |
---|
| 167 | // --- VariableExpr |
---|
| 168 | |
---|
[546e712] | 169 | VariableExpr::VariableExpr( const CodeLocation & loc ) |
---|
| 170 | : Expr( loc ), var( nullptr ) {} |
---|
| 171 | |
---|
[54e41b3] | 172 | VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) |
---|
| 173 | : Expr( loc ), var( v ) { |
---|
| 174 | assert( var ); |
---|
| 175 | assert( var->get_type() ); |
---|
[d76c588] | 176 | result = var->get_type(); |
---|
[ee574a2] | 177 | add_qualifiers( result, CV::Lvalue ); |
---|
[54e41b3] | 178 | } |
---|
| 179 | |
---|
[87701b6] | 180 | VariableExpr * VariableExpr::functionPointer( |
---|
[54e41b3] | 181 | const CodeLocation & loc, const FunctionDecl * decl ) { |
---|
| 182 | // wrap usually-determined result type in a pointer |
---|
| 183 | VariableExpr * funcExpr = new VariableExpr{ loc, decl }; |
---|
| 184 | funcExpr->result = new PointerType{ funcExpr->result }; |
---|
| 185 | return funcExpr; |
---|
| 186 | } |
---|
| 187 | |
---|
| 188 | // --- ConstantExpr |
---|
| 189 | |
---|
| 190 | long long int ConstantExpr::intValue() const { |
---|
| 191 | if ( const BasicType * bty = result.as< BasicType >() ) { |
---|
| 192 | if ( bty->isInteger() ) { |
---|
[c36298d] | 193 | assert(ival); |
---|
| 194 | return ival.value(); |
---|
[54e41b3] | 195 | } |
---|
| 196 | } else if ( result.as< ZeroType >() ) { |
---|
| 197 | return 0; |
---|
| 198 | } else if ( result.as< OneType >() ) { |
---|
| 199 | return 1; |
---|
| 200 | } |
---|
| 201 | SemanticError( this, "Constant expression of non-integral type " ); |
---|
| 202 | } |
---|
| 203 | |
---|
| 204 | ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) { |
---|
[87701b6] | 205 | return new ConstantExpr{ |
---|
[54e41b3] | 206 | loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b }; |
---|
| 207 | } |
---|
| 208 | |
---|
| 209 | ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) { |
---|
[87701b6] | 210 | return new ConstantExpr{ |
---|
[54e41b3] | 211 | loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i }; |
---|
| 212 | } |
---|
| 213 | |
---|
| 214 | ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) { |
---|
[87701b6] | 215 | return new ConstantExpr{ |
---|
| 216 | loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ), |
---|
[54e41b3] | 217 | (unsigned long long)i }; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) { |
---|
| 221 | return new ConstantExpr{ |
---|
| 222 | loc, ptrType ? ptrType : new PointerType{ new VoidType{} }, "0", (unsigned long long)0 }; |
---|
| 223 | } |
---|
| 224 | |
---|
| 225 | // --- SizeofExpr |
---|
| 226 | |
---|
| 227 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e ) |
---|
| 228 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
| 229 | |
---|
| 230 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t ) |
---|
| 231 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
| 232 | |
---|
| 233 | // --- AlignofExpr |
---|
| 234 | |
---|
| 235 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e ) |
---|
| 236 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
| 237 | |
---|
| 238 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t ) |
---|
| 239 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
| 240 | |
---|
| 241 | // --- OffsetofExpr |
---|
| 242 | |
---|
| 243 | OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ) |
---|
| 244 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) { |
---|
| 245 | assert( type ); |
---|
| 246 | assert( member ); |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | // --- OffsetPackExpr |
---|
| 250 | |
---|
| 251 | OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ) |
---|
[87701b6] | 252 | : Expr( loc, new ArrayType{ |
---|
| 253 | new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim } |
---|
[54e41b3] | 254 | ), type( ty ) { |
---|
| 255 | assert( type ); |
---|
| 256 | } |
---|
| 257 | |
---|
| 258 | // --- LogicalExpr |
---|
| 259 | |
---|
[87701b6] | 260 | LogicalExpr::LogicalExpr( |
---|
[54e41b3] | 261 | const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ) |
---|
| 262 | : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} |
---|
| 263 | |
---|
[9b4f329] | 264 | // --- ConstructorExpr |
---|
| 265 | |
---|
[10a1225] | 266 | ConstructorExpr::ConstructorExpr( const CodeLocation & loc, const Expr * call ) |
---|
[9b4f329] | 267 | : Expr( loc ), callExpr( call ) { |
---|
[10a1225] | 268 | // allow resolver to type a constructor used as an expression if it has the same type as its |
---|
[9b4f329] | 269 | // first argument |
---|
| 270 | assert( callExpr ); |
---|
| 271 | const Expr * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
| 272 | assert( arg ); |
---|
| 273 | result = arg->result; |
---|
| 274 | } |
---|
| 275 | |
---|
| 276 | // --- CompoundLiteralExpr |
---|
| 277 | |
---|
| 278 | CompoundLiteralExpr::CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i ) |
---|
| 279 | : Expr( loc ), init( i ) { |
---|
| 280 | assert( t && i ); |
---|
[d76c588] | 281 | result = t; |
---|
[ee574a2] | 282 | add_qualifiers( result, CV::Lvalue ); |
---|
[9b4f329] | 283 | } |
---|
| 284 | |
---|
| 285 | // --- TupleExpr |
---|
| 286 | |
---|
| 287 | TupleExpr::TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ) |
---|
| 288 | : Expr( loc, Tuples::makeTupleType( xs ) ), exprs( xs ) {} |
---|
| 289 | |
---|
| 290 | // --- TupleIndexExpr |
---|
| 291 | |
---|
| 292 | TupleIndexExpr::TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i ) |
---|
| 293 | : Expr( loc ), tuple( t ), index( i ) { |
---|
[10a1225] | 294 | const TupleType * type = strict_dynamic_cast< const TupleType * >( tuple->result.get() ); |
---|
[9b4f329] | 295 | assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested " |
---|
| 296 | "index %d in expr %s", type->size(), index, toString( tuple ).c_str() ); |
---|
| 297 | // like MemberExpr, TupleIndexExpr is always an lvalue |
---|
[d76c588] | 298 | result = type->types[ index ]; |
---|
[ee574a2] | 299 | add_qualifiers( result, CV::Lvalue ); |
---|
[9b4f329] | 300 | } |
---|
| 301 | |
---|
| 302 | // --- TupleAssignExpr |
---|
| 303 | |
---|
[10a1225] | 304 | TupleAssignExpr::TupleAssignExpr( |
---|
| 305 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns, |
---|
[9b4f329] | 306 | std::vector<ptr<ObjectDecl>> && tempDecls ) |
---|
| 307 | : Expr( loc, Tuples::makeTupleType( assigns ) ), stmtExpr() { |
---|
[10a1225] | 308 | // convert internally into a StmtExpr which contains the declarations and produces the tuple of |
---|
[9b4f329] | 309 | // the assignments |
---|
| 310 | std::list<ptr<Stmt>> stmts; |
---|
| 311 | for ( const ObjectDecl * obj : tempDecls ) { |
---|
| 312 | stmts.emplace_back( new DeclStmt{ loc, obj } ); |
---|
| 313 | } |
---|
| 314 | TupleExpr * tupleExpr = new TupleExpr{ loc, std::move(assigns) }; |
---|
| 315 | assert( tupleExpr->result ); |
---|
| 316 | stmts.emplace_back( new ExprStmt{ loc, tupleExpr } ); |
---|
| 317 | stmtExpr = new StmtExpr{ loc, new CompoundStmt{ loc, std::move(stmts) } }; |
---|
| 318 | } |
---|
| 319 | |
---|
[0b57626] | 320 | TupleAssignExpr::TupleAssignExpr( |
---|
[20de6fb] | 321 | const CodeLocation & loc, const Type * result, const StmtExpr * s ) |
---|
| 322 | : Expr( loc, result ), stmtExpr() { |
---|
| 323 | stmtExpr = s; |
---|
| 324 | } |
---|
| 325 | |
---|
[9b4f329] | 326 | // --- StmtExpr |
---|
| 327 | |
---|
[10a1225] | 328 | StmtExpr::StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ) |
---|
[9b4f329] | 329 | : Expr( loc ), stmts( ss ), returnDecls(), dtors() { computeResult(); } |
---|
| 330 | |
---|
| 331 | void StmtExpr::computeResult() { |
---|
| 332 | assert( stmts ); |
---|
| 333 | const std::list<ptr<Stmt>> & body = stmts->kids; |
---|
| 334 | if ( ! returnDecls.empty() ) { |
---|
[10a1225] | 335 | // prioritize return decl for result type, since if a return decl exists, then the StmtExpr |
---|
[9b4f329] | 336 | // is currently in an intermediate state where the body will always give a void result type |
---|
| 337 | result = returnDecls.front()->get_type(); |
---|
| 338 | } else if ( ! body.empty() ) { |
---|
| 339 | if ( const ExprStmt * exprStmt = body.back().as< ExprStmt >() ) { |
---|
| 340 | result = exprStmt->expr->result; |
---|
| 341 | } |
---|
| 342 | } |
---|
| 343 | // ensure a result type exists |
---|
| 344 | if ( ! result ) { result = new VoidType{}; } |
---|
| 345 | } |
---|
| 346 | |
---|
| 347 | // --- UniqueExpr |
---|
| 348 | |
---|
| 349 | unsigned long long UniqueExpr::nextId = 0; |
---|
| 350 | |
---|
[10a1225] | 351 | UniqueExpr::UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i ) |
---|
[d76f32c] | 352 | : Expr( loc, e->result ), expr( e ), id( i ) { |
---|
[9b4f329] | 353 | assert( expr ); |
---|
[10a1225] | 354 | if ( id == -1ull ) { |
---|
| 355 | assert( nextId != -1ull ); |
---|
[9b4f329] | 356 | id = nextId++; |
---|
| 357 | } |
---|
[54e41b3] | 358 | } |
---|
| 359 | |
---|
[10a1225] | 360 | } |
---|
| 361 | |
---|
[54e41b3] | 362 | // Local Variables: // |
---|
| 363 | // tab-width: 4 // |
---|
| 364 | // mode: c++ // |
---|
| 365 | // compile-command: "make install" // |
---|
[d76f32c] | 366 | // End: // |
---|