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