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 26 12:12:00 2019 |
---|
13 | // Update Count : 3 |
---|
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 "Copy.hpp" // for shallowCopy |
---|
23 | #include "Eval.hpp" // for call |
---|
24 | #include "GenericSubstitution.hpp" |
---|
25 | #include "LinkageSpec.hpp" |
---|
26 | #include "Stmt.hpp" |
---|
27 | #include "Type.hpp" |
---|
28 | #include "TypeSubstitution.hpp" |
---|
29 | #include "Common/utility.h" |
---|
30 | #include "Common/SemanticError.h" |
---|
31 | #include "GenPoly/Lvalue.h" // for referencesPermissable |
---|
32 | #include "InitTweak/InitTweak.h" // for getFunction, getPointerBase |
---|
33 | #include "ResolvExpr/typeops.h" // for extractResultType |
---|
34 | #include "Tuples/Tuples.h" // for makeTupleType |
---|
35 | |
---|
36 | namespace ast { |
---|
37 | |
---|
38 | namespace { |
---|
39 | std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"}; |
---|
40 | } |
---|
41 | |
---|
42 | // --- Expr |
---|
43 | bool Expr::get_lvalue() const { |
---|
44 | return false; |
---|
45 | } |
---|
46 | |
---|
47 | // --- ApplicationExpr |
---|
48 | |
---|
49 | ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f, |
---|
50 | std::vector<ptr<Expr>> && as ) |
---|
51 | : Expr( loc ), func( f ), args( std::move(as) ) { |
---|
52 | // ensure that `ApplicationExpr` result type is `FuncExpr` |
---|
53 | const PointerType * pt = strict_dynamic_cast< const PointerType * >( f->result.get() ); |
---|
54 | const FunctionType * fn = strict_dynamic_cast< const FunctionType * >( pt->base.get() ); |
---|
55 | |
---|
56 | result = ResolvExpr::extractResultType( fn ); |
---|
57 | assert( result ); |
---|
58 | } |
---|
59 | |
---|
60 | bool ApplicationExpr::get_lvalue() const { |
---|
61 | if ( const DeclWithType * func = InitTweak::getFunction( this ) ) { |
---|
62 | return func->linkage == Linkage::Intrinsic && lvalueFunctionNames.count( func->name ); |
---|
63 | } |
---|
64 | return false; |
---|
65 | } |
---|
66 | |
---|
67 | // --- UntypedExpr |
---|
68 | |
---|
69 | UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) { |
---|
70 | assert( arg ); |
---|
71 | |
---|
72 | UntypedExpr * ret = call( loc, "*?", arg ); |
---|
73 | if ( const Type * ty = arg->result ) { |
---|
74 | const Type * base = InitTweak::getPointerBase( ty ); |
---|
75 | assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() ); |
---|
76 | |
---|
77 | if ( GenPoly::referencesPermissable() ) { |
---|
78 | // if references are still allowed in the AST, dereference returns a reference |
---|
79 | ret->result = new ReferenceType{ base }; |
---|
80 | } else { |
---|
81 | // references have been removed, in which case dereference returns an lvalue of the |
---|
82 | // base type |
---|
83 | ret->result = base; |
---|
84 | add_qualifiers( ret->result, CV::Lvalue ); |
---|
85 | } |
---|
86 | } |
---|
87 | return ret; |
---|
88 | } |
---|
89 | |
---|
90 | bool UntypedExpr::get_lvalue() const { |
---|
91 | std::string fname = InitTweak::getFunctionName( this ); |
---|
92 | return lvalueFunctionNames.count( fname ); |
---|
93 | } |
---|
94 | |
---|
95 | UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { |
---|
96 | assert( lhs && rhs ); |
---|
97 | |
---|
98 | UntypedExpr * ret = call( loc, "?=?", lhs, rhs ); |
---|
99 | if ( lhs->result && rhs->result ) { |
---|
100 | // if both expressions are typed, assumes that this assignment is a C bitwise assignment, |
---|
101 | // so the result is the type of the RHS |
---|
102 | ret->result = rhs->result; |
---|
103 | } |
---|
104 | return ret; |
---|
105 | } |
---|
106 | |
---|
107 | // --- AddressExpr |
---|
108 | |
---|
109 | // Address expressions are typed based on the following inference rules: |
---|
110 | // E : lvalue T &..& (n references) |
---|
111 | // &E : T *&..& (n references) |
---|
112 | // |
---|
113 | // E : T &..& (m references) |
---|
114 | // &E : T *&..& (m-1 references) |
---|
115 | |
---|
116 | namespace { |
---|
117 | /// The type of the address of a type. |
---|
118 | /// Caller is responsible for managing returned memory |
---|
119 | Type * addrType( const Type * type ) { |
---|
120 | if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( type ) ) { |
---|
121 | return new ReferenceType{ addrType( refType->base ), refType->qualifiers }; |
---|
122 | } else { |
---|
123 | return new PointerType{ type }; |
---|
124 | } |
---|
125 | } |
---|
126 | } |
---|
127 | |
---|
128 | AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { |
---|
129 | if ( arg->result ) { |
---|
130 | if ( arg->get_lvalue() ) { |
---|
131 | // lvalue, retains all levels of reference, and gains a pointer inside the references |
---|
132 | Type * res = addrType( arg->result ); |
---|
133 | res->set_lvalue( false ); // result of & is never an lvalue |
---|
134 | result = res; |
---|
135 | } else { |
---|
136 | // taking address of non-lvalue, must be a reference, loses one layer of reference |
---|
137 | if ( const ReferenceType * refType = |
---|
138 | dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { |
---|
139 | Type * res = addrType( refType->base ); |
---|
140 | res->set_lvalue( false ); // result of & is never an lvalue |
---|
141 | result = res; |
---|
142 | } else { |
---|
143 | SemanticError( loc, arg->result.get(), |
---|
144 | "Attempt to take address of non-lvalue expression: " ); |
---|
145 | } |
---|
146 | } |
---|
147 | } |
---|
148 | } |
---|
149 | |
---|
150 | // --- LabelAddressExpr |
---|
151 | |
---|
152 | // label address always has type `void*` |
---|
153 | LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a ) |
---|
154 | : Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {} |
---|
155 | |
---|
156 | // --- CastExpr |
---|
157 | |
---|
158 | CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g ) |
---|
159 | : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} |
---|
160 | |
---|
161 | bool CastExpr::get_lvalue() const { |
---|
162 | // This is actually wrong by C, but it works with our current set-up. |
---|
163 | return arg->get_lvalue(); |
---|
164 | } |
---|
165 | |
---|
166 | // --- KeywordCastExpr |
---|
167 | |
---|
168 | const std::string & KeywordCastExpr::targetString() const { |
---|
169 | static const std::string targetStrs[] = { |
---|
170 | "coroutine", "thread", "monitor" |
---|
171 | }; |
---|
172 | static_assert( |
---|
173 | (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS), |
---|
174 | "Each KeywordCastExpr::Target should have a corresponding string representation" |
---|
175 | ); |
---|
176 | return targetStrs[(unsigned long)target]; |
---|
177 | } |
---|
178 | |
---|
179 | // --- UntypedMemberExpr |
---|
180 | |
---|
181 | bool UntypedMemberExpr::get_lvalue() const { |
---|
182 | return aggregate->get_lvalue(); |
---|
183 | } |
---|
184 | |
---|
185 | // --- MemberExpr |
---|
186 | |
---|
187 | MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ) |
---|
188 | : Expr( loc ), member( mem ), aggregate( agg ) { |
---|
189 | assert( member ); |
---|
190 | assert( aggregate ); |
---|
191 | assert( aggregate->result ); |
---|
192 | |
---|
193 | // Deep copy on result type avoids mutation on transitively multiply referenced object. |
---|
194 | // |
---|
195 | // Example, adapted from parts of builtins and bootloader: |
---|
196 | // |
---|
197 | // forall(dtype T) |
---|
198 | // struct __Destructor { |
---|
199 | // T * object; |
---|
200 | // void (*dtor)(T *); |
---|
201 | // }; |
---|
202 | // |
---|
203 | // forall(dtype S) |
---|
204 | // void foo(__Destructor(S) &d) { |
---|
205 | // if (d.dtor) { // here |
---|
206 | // } |
---|
207 | // } |
---|
208 | // |
---|
209 | // Let e be the "d.dtor" guard espression, which is MemberExpr after resolve. Let d be the |
---|
210 | // declaration of member __Destructor.dtor (an ObjectDecl), as accessed via the top-level |
---|
211 | // declaration of __Destructor. Consider the types e.result and d.type. In the old AST, one |
---|
212 | // is a clone of the other. Ordinary new-AST use would set them up as a multiply-referenced |
---|
213 | // object. |
---|
214 | // |
---|
215 | // e.result: PointerType |
---|
216 | // .base: FunctionType |
---|
217 | // .params.front(): ObjectDecl, the anonymous parameter of type T* |
---|
218 | // .type: PointerType |
---|
219 | // .base: TypeInstType |
---|
220 | // let x = that |
---|
221 | // let y = similar, except start from d.type |
---|
222 | // |
---|
223 | // Consider two code lines down, genericSubstitution(...).apply(result). |
---|
224 | // |
---|
225 | // Applying this chosen-candidate's type substitution means modifying x, substituting |
---|
226 | // S for T. This mutation should affect x and not y. |
---|
227 | |
---|
228 | result = deepCopy(mem->get_type()); |
---|
229 | |
---|
230 | // substitute aggregate generic parameters into member type |
---|
231 | genericSubstitution( aggregate->result ).apply( result ); |
---|
232 | // ensure lvalue and appropriate restrictions from aggregate type |
---|
233 | add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue ); |
---|
234 | } |
---|
235 | |
---|
236 | MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg, |
---|
237 | MemberExpr::NoOpConstruction overloadSelector ) |
---|
238 | : Expr( loc ), member( mem ), aggregate( agg ) { |
---|
239 | assert( member ); |
---|
240 | assert( aggregate ); |
---|
241 | assert( aggregate->result ); |
---|
242 | (void) overloadSelector; |
---|
243 | } |
---|
244 | |
---|
245 | bool MemberExpr::get_lvalue() const { |
---|
246 | // This is actually wrong by C, but it works with our current set-up. |
---|
247 | return true; |
---|
248 | } |
---|
249 | |
---|
250 | // --- VariableExpr |
---|
251 | |
---|
252 | VariableExpr::VariableExpr( const CodeLocation & loc ) |
---|
253 | : Expr( loc ), var( nullptr ) {} |
---|
254 | |
---|
255 | VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) |
---|
256 | : Expr( loc ), var( v ) { |
---|
257 | assert( var ); |
---|
258 | assert( var->get_type() ); |
---|
259 | auto r = shallowCopy( var->get_type() ); |
---|
260 | r->qualifiers |= CV::Lvalue; |
---|
261 | result = r; |
---|
262 | } |
---|
263 | |
---|
264 | bool VariableExpr::get_lvalue() const { |
---|
265 | // It isn't always an lvalue, but it is never an rvalue. |
---|
266 | return true; |
---|
267 | } |
---|
268 | |
---|
269 | VariableExpr * VariableExpr::functionPointer( |
---|
270 | const CodeLocation & loc, const FunctionDecl * decl ) { |
---|
271 | // wrap usually-determined result type in a pointer |
---|
272 | VariableExpr * funcExpr = new VariableExpr{ loc, decl }; |
---|
273 | funcExpr->result = new PointerType{ funcExpr->result }; |
---|
274 | return funcExpr; |
---|
275 | } |
---|
276 | |
---|
277 | // --- ConstantExpr |
---|
278 | |
---|
279 | long long int ConstantExpr::intValue() const { |
---|
280 | if ( const BasicType * bty = result.as< BasicType >() ) { |
---|
281 | if ( bty->isInteger() ) { |
---|
282 | assert(ival); |
---|
283 | return ival.value(); |
---|
284 | } |
---|
285 | } else if ( result.as< ZeroType >() ) { |
---|
286 | return 0; |
---|
287 | } else if ( result.as< OneType >() ) { |
---|
288 | return 1; |
---|
289 | } |
---|
290 | SemanticError( this, "Constant expression of non-integral type " ); |
---|
291 | } |
---|
292 | |
---|
293 | ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) { |
---|
294 | return new ConstantExpr{ |
---|
295 | loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b }; |
---|
296 | } |
---|
297 | |
---|
298 | ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) { |
---|
299 | return new ConstantExpr{ |
---|
300 | loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i }; |
---|
301 | } |
---|
302 | |
---|
303 | ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) { |
---|
304 | return new ConstantExpr{ |
---|
305 | loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ), |
---|
306 | (unsigned long long)i }; |
---|
307 | } |
---|
308 | |
---|
309 | ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) { |
---|
310 | return new ConstantExpr{ |
---|
311 | loc, ptrType ? ptrType : new PointerType{ new VoidType{} }, "0", (unsigned long long)0 }; |
---|
312 | } |
---|
313 | |
---|
314 | // --- SizeofExpr |
---|
315 | |
---|
316 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e ) |
---|
317 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
318 | |
---|
319 | SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t ) |
---|
320 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
321 | |
---|
322 | // --- AlignofExpr |
---|
323 | |
---|
324 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e ) |
---|
325 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} |
---|
326 | |
---|
327 | AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t ) |
---|
328 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} |
---|
329 | |
---|
330 | // --- OffsetofExpr |
---|
331 | |
---|
332 | OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ) |
---|
333 | : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) { |
---|
334 | assert( type ); |
---|
335 | assert( member ); |
---|
336 | } |
---|
337 | |
---|
338 | // --- OffsetPackExpr |
---|
339 | |
---|
340 | OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ) |
---|
341 | : Expr( loc, new ArrayType{ |
---|
342 | new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim } |
---|
343 | ), type( ty ) { |
---|
344 | assert( type ); |
---|
345 | } |
---|
346 | |
---|
347 | // --- LogicalExpr |
---|
348 | |
---|
349 | LogicalExpr::LogicalExpr( |
---|
350 | const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ) |
---|
351 | : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} |
---|
352 | |
---|
353 | // --- CommaExpr |
---|
354 | bool CommaExpr::get_lvalue() const { |
---|
355 | // This is wrong by C, but the current implementation uses it. |
---|
356 | // (ex: Specialize, Lvalue and Box) |
---|
357 | return arg2->get_lvalue(); |
---|
358 | } |
---|
359 | |
---|
360 | // --- ConstructorExpr |
---|
361 | |
---|
362 | ConstructorExpr::ConstructorExpr( const CodeLocation & loc, const Expr * call ) |
---|
363 | : Expr( loc ), callExpr( call ) { |
---|
364 | // allow resolver to type a constructor used as an expression if it has the same type as its |
---|
365 | // first argument |
---|
366 | assert( callExpr ); |
---|
367 | const Expr * arg = InitTweak::getCallArg( callExpr, 0 ); |
---|
368 | assert( arg ); |
---|
369 | result = arg->result; |
---|
370 | } |
---|
371 | |
---|
372 | // --- CompoundLiteralExpr |
---|
373 | |
---|
374 | CompoundLiteralExpr::CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i ) |
---|
375 | : Expr( loc ), init( i ) { |
---|
376 | assert( t && i ); |
---|
377 | result = t; |
---|
378 | add_qualifiers( result, CV::Lvalue ); |
---|
379 | } |
---|
380 | |
---|
381 | bool CompoundLiteralExpr::get_lvalue() const { |
---|
382 | return true; |
---|
383 | } |
---|
384 | |
---|
385 | // --- TupleExpr |
---|
386 | |
---|
387 | TupleExpr::TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs ) |
---|
388 | : Expr( loc, Tuples::makeTupleType( xs ) ), exprs( xs ) {} |
---|
389 | |
---|
390 | // --- TupleIndexExpr |
---|
391 | |
---|
392 | TupleIndexExpr::TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i ) |
---|
393 | : Expr( loc ), tuple( t ), index( i ) { |
---|
394 | const TupleType * type = strict_dynamic_cast< const TupleType * >( tuple->result.get() ); |
---|
395 | assertf( type->size() > index, "TupleIndexExpr index out of bounds: tuple size %d, requested " |
---|
396 | "index %d in expr %s", type->size(), index, toString( tuple ).c_str() ); |
---|
397 | // like MemberExpr, TupleIndexExpr is always an lvalue |
---|
398 | result = type->types[ index ]; |
---|
399 | add_qualifiers( result, CV::Lvalue ); |
---|
400 | } |
---|
401 | |
---|
402 | bool TupleIndexExpr::get_lvalue() const { |
---|
403 | return tuple->get_lvalue(); |
---|
404 | } |
---|
405 | |
---|
406 | // --- TupleAssignExpr |
---|
407 | |
---|
408 | TupleAssignExpr::TupleAssignExpr( |
---|
409 | const CodeLocation & loc, std::vector<ptr<Expr>> && assigns, |
---|
410 | std::vector<ptr<ObjectDecl>> && tempDecls ) |
---|
411 | : Expr( loc, Tuples::makeTupleType( assigns ) ), stmtExpr() { |
---|
412 | // convert internally into a StmtExpr which contains the declarations and produces the tuple of |
---|
413 | // the assignments |
---|
414 | std::list<ptr<Stmt>> stmts; |
---|
415 | for ( const ObjectDecl * obj : tempDecls ) { |
---|
416 | stmts.emplace_back( new DeclStmt{ loc, obj } ); |
---|
417 | } |
---|
418 | TupleExpr * tupleExpr = new TupleExpr{ loc, std::move(assigns) }; |
---|
419 | assert( tupleExpr->result ); |
---|
420 | stmts.emplace_back( new ExprStmt{ loc, tupleExpr } ); |
---|
421 | stmtExpr = new StmtExpr{ loc, new CompoundStmt{ loc, std::move(stmts) } }; |
---|
422 | } |
---|
423 | |
---|
424 | TupleAssignExpr::TupleAssignExpr( |
---|
425 | const CodeLocation & loc, const Type * result, const StmtExpr * s ) |
---|
426 | : Expr( loc, result ), stmtExpr() { |
---|
427 | stmtExpr = s; |
---|
428 | } |
---|
429 | |
---|
430 | // --- StmtExpr |
---|
431 | |
---|
432 | StmtExpr::StmtExpr( const CodeLocation & loc, const CompoundStmt * ss ) |
---|
433 | : Expr( loc ), stmts( ss ), returnDecls(), dtors() { computeResult(); } |
---|
434 | |
---|
435 | void StmtExpr::computeResult() { |
---|
436 | assert( stmts ); |
---|
437 | const std::list<ptr<Stmt>> & body = stmts->kids; |
---|
438 | if ( ! returnDecls.empty() ) { |
---|
439 | // prioritize return decl for result type, since if a return decl exists, then the StmtExpr |
---|
440 | // is currently in an intermediate state where the body will always give a void result type |
---|
441 | result = returnDecls.front()->get_type(); |
---|
442 | } else if ( ! body.empty() ) { |
---|
443 | if ( const ExprStmt * exprStmt = body.back().as< ExprStmt >() ) { |
---|
444 | result = exprStmt->expr->result; |
---|
445 | } |
---|
446 | } |
---|
447 | // ensure a result type exists |
---|
448 | if ( ! result ) { result = new VoidType{}; } |
---|
449 | } |
---|
450 | |
---|
451 | // --- UniqueExpr |
---|
452 | |
---|
453 | unsigned long long UniqueExpr::nextId = 0; |
---|
454 | |
---|
455 | UniqueExpr::UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i ) |
---|
456 | : Expr( loc, e->result ), expr( e ), id( i ) { |
---|
457 | assert( expr ); |
---|
458 | if ( id == -1ull ) { |
---|
459 | assert( nextId != -1ull ); |
---|
460 | id = nextId++; |
---|
461 | } |
---|
462 | } |
---|
463 | |
---|
464 | } |
---|
465 | |
---|
466 | // Local Variables: // |
---|
467 | // tab-width: 4 // |
---|
468 | // mode: c++ // |
---|
469 | // compile-command: "make install" // |
---|
470 | // End: // |
---|