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