source: src/AST/Expr.cpp@ 1f1c102

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since 1f1c102 was 417117e, checked in by Aaron Moss <a3moss@…>, 6 years ago

Assorted cleanup

  • Property mode set to 100644
File size: 11.6 KB
Line 
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 : Andrew Beach
12// Created On : Thr Jun 13 13:38:00 2019
13// Update Count : 2
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 "Eval.hpp" // for call
23#include "GenericSubstitution.hpp"
24#include "Stmt.hpp"
25#include "Type.hpp"
26#include "TypeSubstitution.hpp"
27#include "Common/utility.h"
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
32#include "Tuples/Tuples.h" // for makeTupleType
33
34namespace ast {
35
36// --- ApplicationExpr
37
38ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f,
39 std::vector<ptr<Expr>> && as )
40: Expr( loc ), func( f ), args( std::move(as) ) {
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
51UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {
52 assert( arg );
53
54 UntypedExpr * ret = call( loc, "*?", arg );
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 {
63 // references have been removed, in which case dereference returns an lvalue of the
64 // base type
65 ret->result = base;
66 add_qualifiers( ret->result, CV::Lvalue );
67 }
68 }
69 return ret;
70}
71
72UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) {
73 assert( lhs && rhs );
74
75 UntypedExpr * ret = call( loc, "?=?", lhs, rhs );
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
93namespace {
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
105AddressExpr::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
114 if ( const ReferenceType * refType =
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 {
120 SemanticError( loc, arg->result.get(),
121 "Attempt to take address of non-lvalue expression: " );
122 }
123 }
124 }
125}
126
127// --- LabelAddressExpr
128
129// label address always has type `void*`
130LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a )
131: Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {}
132
133// --- CastExpr
134
135CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g )
136: Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {}
137
138// --- KeywordCastExpr
139
140const 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
153MemberExpr::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
159 // take ownership of member type
160 result = mem->get_type();
161 // substitute aggregate generic parameters into member type
162 genericSubstitution( aggregate->result ).apply( result );
163 // ensure lvalue and appropriate restrictions from aggregate type
164 add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue );
165}
166
167// --- VariableExpr
168
169VariableExpr::VariableExpr( const CodeLocation & loc )
170: Expr( loc ), var( nullptr ) {}
171
172VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v )
173: Expr( loc ), var( v ) {
174 assert( var );
175 assert( var->get_type() );
176 result = var->get_type();
177 add_qualifiers( result, CV::Lvalue );
178}
179
180VariableExpr * VariableExpr::functionPointer(
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
190long long int ConstantExpr::intValue() const {
191 if ( const BasicType * bty = result.as< BasicType >() ) {
192 if ( bty->isInteger() ) {
193 assert(ival);
194 return ival.value();
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
204ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) {
205 return new ConstantExpr{
206 loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b };
207}
208
209ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) {
210 return new ConstantExpr{
211 loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i };
212}
213
214ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) {
215 return new ConstantExpr{
216 loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ),
217 (unsigned long long)i };
218}
219
220ConstantExpr * 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
227SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e )
228: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
229
230SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t )
231: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
232
233// --- AlignofExpr
234
235AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e )
236: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
237
238AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
239: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
240
241// --- OffsetofExpr
242
243OffsetofExpr::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
251OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty )
252: Expr( loc, new ArrayType{
253 new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim }
254), type( ty ) {
255 assert( type );
256}
257
258// --- LogicalExpr
259
260LogicalExpr::LogicalExpr(
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
264// --- ConstructorExpr
265
266ConstructorExpr::ConstructorExpr( const CodeLocation & loc, const Expr * call )
267: Expr( loc ), callExpr( call ) {
268 // allow resolver to type a constructor used as an expression if it has the same type as its
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
278CompoundLiteralExpr::CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i )
279: Expr( loc ), init( i ) {
280 assert( t && i );
281 result = t;
282 add_qualifiers( result, CV::Lvalue );
283}
284
285// --- TupleExpr
286
287TupleExpr::TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
288: Expr( loc, Tuples::makeTupleType( xs ) ), exprs( xs ) {}
289
290// --- TupleIndexExpr
291
292TupleIndexExpr::TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i )
293: Expr( loc ), tuple( t ), index( i ) {
294 const TupleType * type = strict_dynamic_cast< const TupleType * >( tuple->result.get() );
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
298 result = type->types[ index ];
299 add_qualifiers( result, CV::Lvalue );
300}
301
302// --- TupleAssignExpr
303
304TupleAssignExpr::TupleAssignExpr(
305 const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
306 std::vector<ptr<ObjectDecl>> && tempDecls )
307: Expr( loc, Tuples::makeTupleType( assigns ) ), stmtExpr() {
308 // convert internally into a StmtExpr which contains the declarations and produces the tuple of
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
320TupleAssignExpr::TupleAssignExpr(
321 const CodeLocation & loc, const Type * result, const StmtExpr * s )
322: Expr( loc, result ), stmtExpr() {
323 stmtExpr = s;
324}
325
326// --- StmtExpr
327
328StmtExpr::StmtExpr( const CodeLocation & loc, const CompoundStmt * ss )
329: Expr( loc ), stmts( ss ), returnDecls(), dtors() { computeResult(); }
330
331void StmtExpr::computeResult() {
332 assert( stmts );
333 const std::list<ptr<Stmt>> & body = stmts->kids;
334 if ( ! returnDecls.empty() ) {
335 // prioritize return decl for result type, since if a return decl exists, then the StmtExpr
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
349unsigned long long UniqueExpr::nextId = 0;
350
351UniqueExpr::UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i )
352: Expr( loc, e->result ), expr( e ), id( i ) {
353 assert( expr );
354 if ( id == -1ull ) {
355 assert( nextId != -1ull );
356 id = nextId++;
357 }
358}
359
360}
361
362// Local Variables: //
363// tab-width: 4 //
364// mode: c++ //
365// compile-command: "make install" //
366// End: //
Note: See TracBrowser for help on using the repository browser.