source: src/AST/Expr.cpp@ 366f5cd

Last change on this file since 366f5cd was 366f5cd, checked in by Peter A. Buhr <pabuhr@…>, 22 hours ago

differentiate between C _Alignof and gcc alignof

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