source: src/AST/Expr.cpp @ cf32116

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since cf32116 was cf32116, checked in by Andrew Beach <ajbeach@…>, 4 years ago

Implemented expression based lvalue resolution on new ast.

  • Property mode set to 100644
File size: 14.5 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
[d76f32c]11// Last Modified By : Andrew Beach
[cf32116]12// Created On       : Thr Jun 26 12:12:00 2019
13// Update Count     : 3
[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
[417117e]23#include "Eval.hpp"                // for call
[d8938622]24#include "GenericSubstitution.hpp"
[cf32116]25#include "LinkageSpec.hpp"
[9b4f329]26#include "Stmt.hpp"
[54e41b3]27#include "Type.hpp"
[d8938622]28#include "TypeSubstitution.hpp"
[733074e]29#include "Common/utility.h"
[54e41b3]30#include "Common/SemanticError.h"
31#include "GenPoly/Lvalue.h"        // for referencesPermissable
[cf32116]32#include "InitTweak/InitTweak.h"   // for getFunction, getPointerBase
[54e41b3]33#include "ResolvExpr/typeops.h"    // for extractResultType
[9b4f329]34#include "Tuples/Tuples.h"         // for makeTupleType
[54e41b3]35
36namespace ast {
37
[cf32116]38namespace {
39        std::set<std::string> const lvalueFunctionNames = {"*?", "?[?]"};
40}
41
42// --- Expr
43bool Expr::get_lvalue() const {
44        return false;
45}
46
[54e41b3]47// --- ApplicationExpr
48
[87701b6]49ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f,
50        std::vector<ptr<Expr>> && as )
51: Expr( loc ), func( f ), args( std::move(as) ) {
[54e41b3]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
[cf32116]60bool 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
[54e41b3]67// --- UntypedExpr
68
69UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) {
70        assert( arg );
71
[417117e]72        UntypedExpr * ret = call( loc, "*?", arg );
[54e41b3]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 {
[87701b6]81                        // references have been removed, in which case dereference returns an lvalue of the
[54e41b3]82                        // base type
[d76c588]83                        ret->result = base;
[ee574a2]84                        add_qualifiers( ret->result, CV::Lvalue );
[54e41b3]85                }
86        }
87        return ret;
88}
89
[cf32116]90bool UntypedExpr::get_lvalue() const {
91        std::string fname = InitTweak::getFunctionName( this );
92        return lvalueFunctionNames.count( fname );
93}
94
[54e41b3]95UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) {
96        assert( lhs && rhs );
97
[417117e]98        UntypedExpr * ret = call( loc, "?=?", lhs, rhs );
[54e41b3]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
116namespace {
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
128AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) {
129        if ( arg->result ) {
[cf32116]130                if ( arg->get_lvalue() ) {
[54e41b3]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
[87701b6]137                        if ( const ReferenceType * refType =
[54e41b3]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 {
[10a1225]143                                SemanticError( loc, arg->result.get(),
[54e41b3]144                                        "Attempt to take address of non-lvalue expression: " );
145                        }
146                }
147        }
148}
149
150// --- LabelAddressExpr
151
152// label address always has type `void*`
[87701b6]153LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a )
[54e41b3]154: Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {}
155
156// --- CastExpr
157
[87701b6]158CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g )
[54e41b3]159: Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {}
160
[cf32116]161bool 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
[54e41b3]166// --- KeywordCastExpr
167
168const 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
[cf32116]179// --- UntypedMemberExpr
180
181bool UntypedMemberExpr::get_lvalue() const {
182        return aggregate->get_lvalue();
183}
184
[54e41b3]185// --- MemberExpr
186
187MemberExpr::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
[ae265b55]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
[335f2d8]230        // substitute aggregate generic parameters into member type
[60aaa51d]231        genericSubstitution( aggregate->result ).apply( result );
[335f2d8]232        // ensure lvalue and appropriate restrictions from aggregate type
[ee574a2]233        add_qualifiers( result, aggregate->result->qualifiers | CV::Lvalue );
[54e41b3]234}
235
[ae265b55]236MemberExpr::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
[cf32116]245bool MemberExpr::get_lvalue() const {
246        // This is actually wrong by C, but it works with our current set-up.
247        return true;
248}
249
[54e41b3]250// --- VariableExpr
251
[546e712]252VariableExpr::VariableExpr( const CodeLocation & loc )
253: Expr( loc ), var( nullptr ) {}
254
[54e41b3]255VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v )
256: Expr( loc ), var( v ) {
257        assert( var );
258        assert( var->get_type() );
[2890212]259        auto r = shallowCopy( var->get_type() );
260        r->qualifiers |= CV::Lvalue;
261        result = r;
[54e41b3]262}
263
[cf32116]264bool VariableExpr::get_lvalue() const {
265        // It isn't always an lvalue, but it is never an rvalue.
266        return true;
267}
268
[87701b6]269VariableExpr * VariableExpr::functionPointer(
[54e41b3]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
279long long int ConstantExpr::intValue() const {
280        if ( const BasicType * bty = result.as< BasicType >() ) {
281                if ( bty->isInteger() ) {
[c36298d]282                        assert(ival);
283                        return ival.value();
[54e41b3]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
293ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) {
[87701b6]294        return new ConstantExpr{
[54e41b3]295                loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b };
296}
297
298ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) {
[87701b6]299        return new ConstantExpr{
[54e41b3]300                loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i };
301}
302
303ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) {
[87701b6]304        return new ConstantExpr{
305                loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ),
[54e41b3]306                (unsigned long long)i };
307}
308
309ConstantExpr * 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
316SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e )
317: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
318
319SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t )
320: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
321
322// --- AlignofExpr
323
324AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e )
325: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {}
326
327AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t )
328: Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {}
329
330// --- OffsetofExpr
331
332OffsetofExpr::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
340OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty )
[87701b6]341: Expr( loc, new ArrayType{
342        new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim }
[54e41b3]343), type( ty ) {
344        assert( type );
345}
346
347// --- LogicalExpr
348
[87701b6]349LogicalExpr::LogicalExpr(
[54e41b3]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
[cf32116]353// --- CommaExpr
354bool 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
[9b4f329]360// --- ConstructorExpr
361
[10a1225]362ConstructorExpr::ConstructorExpr( const CodeLocation & loc, const Expr * call )
[9b4f329]363: Expr( loc ), callExpr( call ) {
[10a1225]364        // allow resolver to type a constructor used as an expression if it has the same type as its
[9b4f329]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
374CompoundLiteralExpr::CompoundLiteralExpr( const CodeLocation & loc, const Type * t, const Init * i )
375: Expr( loc ), init( i ) {
376        assert( t && i );
[d76c588]377        result = t;
[ee574a2]378        add_qualifiers( result, CV::Lvalue );
[9b4f329]379}
380
[cf32116]381bool CompoundLiteralExpr::get_lvalue() const {
382        return true;
383}
384
[9b4f329]385// --- TupleExpr
386
387TupleExpr::TupleExpr( const CodeLocation & loc, std::vector<ptr<Expr>> && xs )
388: Expr( loc, Tuples::makeTupleType( xs ) ), exprs( xs ) {}
389
390// --- TupleIndexExpr
391
392TupleIndexExpr::TupleIndexExpr( const CodeLocation & loc, const Expr * t, unsigned i )
393: Expr( loc ), tuple( t ), index( i ) {
[10a1225]394        const TupleType * type = strict_dynamic_cast< const TupleType * >( tuple->result.get() );
[9b4f329]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
[d76c588]398        result = type->types[ index ];
[ee574a2]399        add_qualifiers( result, CV::Lvalue );
[9b4f329]400}
401
[cf32116]402bool TupleIndexExpr::get_lvalue() const {
403        return tuple->get_lvalue();
404}
405
[9b4f329]406// --- TupleAssignExpr
407
[10a1225]408TupleAssignExpr::TupleAssignExpr(
409        const CodeLocation & loc, std::vector<ptr<Expr>> && assigns,
[9b4f329]410        std::vector<ptr<ObjectDecl>> && tempDecls )
411: Expr( loc, Tuples::makeTupleType( assigns ) ), stmtExpr() {
[10a1225]412        // convert internally into a StmtExpr which contains the declarations and produces the tuple of
[9b4f329]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
[0b57626]424TupleAssignExpr::TupleAssignExpr(
[20de6fb]425        const CodeLocation & loc, const Type * result, const StmtExpr * s )
426: Expr( loc, result ), stmtExpr() {
427        stmtExpr = s;
428}
429
[9b4f329]430// --- StmtExpr
431
[10a1225]432StmtExpr::StmtExpr( const CodeLocation & loc, const CompoundStmt * ss )
[9b4f329]433: Expr( loc ), stmts( ss ), returnDecls(), dtors() { computeResult(); }
434
435void StmtExpr::computeResult() {
436        assert( stmts );
437        const std::list<ptr<Stmt>> & body = stmts->kids;
438        if ( ! returnDecls.empty() ) {
[10a1225]439                // prioritize return decl for result type, since if a return decl exists, then the StmtExpr
[9b4f329]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
453unsigned long long UniqueExpr::nextId = 0;
454
[10a1225]455UniqueExpr::UniqueExpr( const CodeLocation & loc, const Expr * e, unsigned long long i )
[d76f32c]456: Expr( loc, e->result ), expr( e ), id( i ) {
[9b4f329]457        assert( expr );
[10a1225]458        if ( id == -1ull ) {
459                assert( nextId != -1ull );
[9b4f329]460                id = nextId++;
461        }
[54e41b3]462}
463
[10a1225]464}
465
[54e41b3]466// Local Variables: //
467// tab-width: 4 //
468// mode: c++ //
469// compile-command: "make install" //
[d76f32c]470// End: //
Note: See TracBrowser for help on using the repository browser.