source: src/AST/Expr.cpp@ c671112

ADT arm-eh ast-experimental cleanup-dtors enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since c671112 was 54e41b3, checked in by Aaron Moss <a3moss@…>, 6 years ago

Add first half of ast::Expr subclasses

  • Property mode set to 100644
File size: 9.3 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
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
22#include "Type.hpp"
23#include "Common/SemanticError.h"
24#include "GenPoly/Lvalue.h" // for referencesPermissable
25#include "InitTweak/InitTweak.h" // for getPointerBase
26#include "ResolvExpr/typeops.h" // for extractResultType
27
28namespace ast {
29
30// --- ApplicationExpr
31
32ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f,
33 std::vector<ptr<Expr>> && as )
34: Expr( loc ), func( f ), args( std::move(args) ) {
35 // ensure that `ApplicationExpr` result type is `FuncExpr`
36 const PointerType * pt = strict_dynamic_cast< const PointerType * >( f->result.get() );
37 const FunctionType * fn = strict_dynamic_cast< const FunctionType * >( pt->base.get() );
38
39 result = ResolvExpr::extractResultType( fn );
40 assert( result );
41}
42
43// --- UntypedExpr
44
45UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {
46 assert( arg );
47
48 UntypedExpr * ret = new UntypedExpr{
49 loc, new NameExpr{loc, "*?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ arg } }
50 };
51 if ( const Type * ty = arg->result ) {
52 const Type * base = InitTweak::getPointerBase( ty );
53 assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() );
54
55 if ( GenPoly::referencesPermissable() ) {
56 // if references are still allowed in the AST, dereference returns a reference
57 ret->result = new ReferenceType{ base };
58 } else {
59 // references have been removed, in which case dereference returns an lvalue of the
60 // base type
61 ret->result.set_and_mutate( base )->set_lvalue( true );
62 }
63 }
64 return ret;
65}
66
67UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) {
68 assert( lhs && rhs );
69
70 UntypedExpr * ret = new UntypedExpr{
71 loc, new NameExpr{loc, "?=?"}, std::vector<ptr<Expr>>{ ptr<Expr>{ lhs }, ptr<Expr>{ rhs } }
72 };
73 if ( lhs->result && rhs->result ) {
74 // if both expressions are typed, assumes that this assignment is a C bitwise assignment,
75 // so the result is the type of the RHS
76 ret->result = rhs->result;
77 }
78 return ret;
79}
80
81// --- AddressExpr
82
83// Address expressions are typed based on the following inference rules:
84// E : lvalue T &..& (n references)
85// &E : T *&..& (n references)
86//
87// E : T &..& (m references)
88// &E : T *&..& (m-1 references)
89
90namespace {
91 /// The type of the address of a type.
92 /// Caller is responsible for managing returned memory
93 Type * addrType( const Type * type ) {
94 if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( type ) ) {
95 CV::Qualifiers quals = refType->qualifiers;
96 return new ReferenceType{ addrType( refType->base ), refType->qualifiers };
97 } else {
98 return new PointerType{ type };
99 }
100 }
101}
102
103AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) {
104 if ( arg->result ) {
105 if ( arg->result->is_lvalue() ) {
106 // lvalue, retains all levels of reference, and gains a pointer inside the references
107 Type * res = addrType( arg->result );
108 res->set_lvalue( false ); // result of & is never an lvalue
109 result = res;
110 } else {
111 // taking address of non-lvalue, must be a reference, loses one layer of reference
112 if ( const ReferenceType * refType =
113 dynamic_cast< const ReferenceType * >( arg->result.get() ) ) {
114 Type * res = addrType( refType->base );
115 res->set_lvalue( false ); // result of & is never an lvalue
116 result = res;
117 } else {
118 SemanticError( loc, arg->result,
119 "Attempt to take address of non-lvalue expression: " );
120 }
121 }
122 }
123}
124
125// --- LabelAddressExpr
126
127// label address always has type `void*`
128LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a )
129: Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {}
130
131// --- CastExpr
132
133CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g )
134: Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {}
135
136// --- KeywordCastExpr
137
138const std::string & KeywordCastExpr::targetString() const {
139 static const std::string targetStrs[] = {
140 "coroutine", "thread", "monitor"
141 };
142 static_assert(
143 (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS),
144 "Each KeywordCastExpr::Target should have a corresponding string representation"
145 );
146 return targetStrs[(unsigned long)target];
147}
148
149// --- MemberExpr
150
151MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg )
152: Expr( loc ), member( mem ), aggregate( agg ) {
153 assert( member );
154 assert( aggregate );
155 assert( aggregate->result );
156
157 assert(!"unimplemented; need TypeSubstitution, genericSubstitution");
158}
159
160// --- VariableExpr
161
162VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v )
163: Expr( loc ), var( v ) {
164 assert( var );
165 assert( var->get_type() );
166 result.set_and_mutate( var->get_type() )->set_lvalue( true );
167}
168
169VariableExpr * VariableExpr::functionPointer(
170 const CodeLocation & loc, const FunctionDecl * decl ) {
171 // wrap usually-determined result type in a pointer
172 VariableExpr * funcExpr = new VariableExpr{ loc, decl };
173 funcExpr->result = new PointerType{ funcExpr->result };
174 return funcExpr;
175}
176
177// --- ConstantExpr
178
179long long int ConstantExpr::intValue() const {
180 if ( const BasicType * bty = result.as< BasicType >() ) {
181 if ( bty->isInteger() ) {
182 return val.ival;
183 }
184 } else if ( result.as< ZeroType >() ) {
185 return 0;
186 } else if ( result.as< OneType >() ) {
187 return 1;
188 }
189 SemanticError( this, "Constant expression of non-integral type " );
190}
191
192double ConstantExpr::floatValue() const {
193 if ( const BasicType * bty = result.as< BasicType >() ) {
194 if ( ! bty->isInteger() ) {
195 return val.dval;
196 }
197 }
198 SemanticError( this, "Constant expression of non-floating-point type " );
199}
200
201ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) {
202 return new ConstantExpr{
203 loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b };
204}
205
206ConstantExpr * ConstantExpr::from_char( const CodeLocation & loc, char c ) {
207 return new ConstantExpr{
208 loc, new BasicType{ BasicType::Char }, std::to_string( c ), (unsigned long long)c };
209}
210
211ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) {
212 return new ConstantExpr{
213 loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i };
214}
215
216ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) {
217 return new ConstantExpr{
218 loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ),
219 (unsigned long long)i };
220}
221
222ConstantExpr * ConstantExpr::from_double( const CodeLocation & loc, double d ) {
223 return new ConstantExpr{ loc, new BasicType{ BasicType::Double }, std::to_string( d ), d };
224}
225
226ConstantExpr * ConstantExpr::from_string( const CodeLocation & loc, const std::string & s ) {
227 return new ConstantExpr{
228 loc,
229 new ArrayType{
230 new BasicType{ BasicType::Char, CV::Const },
231 ConstantExpr::from_int( loc, s.size() + 1 /* null terminator */ ),
232 FixedLen, DynamicDim },
233 std::string{"\""} + s + "\"",
234 (unsigned long long)0 };
235}
236
237ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) {
238 return new ConstantExpr{
239 loc, ptrType ? ptrType : new PointerType{ new VoidType{} }, "0", (unsigned long long)0 };
240}
241
242// --- SizeofExpr
243
244SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e )
245: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
246
247SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t )
248: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
249
250// --- AlignofExpr
251
252AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e )
253: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
254
255AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
256: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
257
258// --- UntypedOffsetofExpr
259
260UntypedOffsetofExpr::UntypedOffsetofExpr(
261 const CodeLocation & loc, const Type * ty, const std::string & mem )
262: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) {
263 assert( type );
264}
265
266// --- OffsetofExpr
267
268OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem )
269: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) {
270 assert( type );
271 assert( member );
272}
273
274// --- OffsetPackExpr
275
276OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty )
277: Expr( loc, new ArrayType{
278 new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim }
279), type( ty ) {
280 assert( type );
281}
282
283// --- LogicalExpr
284
285LogicalExpr::LogicalExpr(
286 const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia )
287: Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {}
288
289}
290
291// Local Variables: //
292// tab-width: 4 //
293// mode: c++ //
294// compile-command: "make install" //
295// End: //
Note: See TracBrowser for help on using the repository browser.