// // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo // // The contents of this file are covered under the licence agreement in the // file "LICENCE" distributed with Cforall. // // Expr.cpp -- // // Author : Aaron B. Moss // Created On : Wed May 15 17:00:00 2019 // Last Modified By : Aaron B. Moss // Created On : Wed May 15 17:00:00 2019 // Update Count : 1 // #include "Expr.hpp" #include // for strict_dynamic_cast #include // for to_string #include #include "Type.hpp" #include "Common/SemanticError.h" #include "GenPoly/Lvalue.h" // for referencesPermissable #include "InitTweak/InitTweak.h" // for getPointerBase #include "ResolvExpr/typeops.h" // for extractResultType namespace ast { // --- ApplicationExpr ApplicationExpr::ApplicationExpr( const CodeLocation & loc, const Expr * f, std::vector> && as ) : Expr( loc ), func( f ), args( std::move(args) ) { // ensure that `ApplicationExpr` result type is `FuncExpr` const PointerType * pt = strict_dynamic_cast< const PointerType * >( f->result.get() ); const FunctionType * fn = strict_dynamic_cast< const FunctionType * >( pt->base.get() ); result = ResolvExpr::extractResultType( fn ); assert( result ); } // --- UntypedExpr UntypedExpr * UntypedExpr::createDeref( const CodeLocation & loc, Expr * arg ) { assert( arg ); UntypedExpr * ret = new UntypedExpr{ loc, new NameExpr{loc, "*?"}, std::vector>{ ptr{ arg } } }; if ( const Type * ty = arg->result ) { const Type * base = InitTweak::getPointerBase( ty ); assertf( base, "expected pointer type in dereference (type was %s)", toString( ty ).c_str() ); if ( GenPoly::referencesPermissable() ) { // if references are still allowed in the AST, dereference returns a reference ret->result = new ReferenceType{ base }; } else { // references have been removed, in which case dereference returns an lvalue of the // base type ret->result.set_and_mutate( base )->set_lvalue( true ); } } return ret; } UntypedExpr * UntypedExpr::createAssign( const CodeLocation & loc, Expr * lhs, Expr * rhs ) { assert( lhs && rhs ); UntypedExpr * ret = new UntypedExpr{ loc, new NameExpr{loc, "?=?"}, std::vector>{ ptr{ lhs }, ptr{ rhs } } }; if ( lhs->result && rhs->result ) { // if both expressions are typed, assumes that this assignment is a C bitwise assignment, // so the result is the type of the RHS ret->result = rhs->result; } return ret; } // --- AddressExpr // Address expressions are typed based on the following inference rules: // E : lvalue T &..& (n references) // &E : T *&..& (n references) // // E : T &..& (m references) // &E : T *&..& (m-1 references) namespace { /// The type of the address of a type. /// Caller is responsible for managing returned memory Type * addrType( const Type * type ) { if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( type ) ) { CV::Qualifiers quals = refType->qualifiers; return new ReferenceType{ addrType( refType->base ), refType->qualifiers }; } else { return new PointerType{ type }; } } } AddressExpr::AddressExpr( const CodeLocation & loc, const Expr * a ) : Expr( loc ), arg( a ) { if ( arg->result ) { if ( arg->result->is_lvalue() ) { // lvalue, retains all levels of reference, and gains a pointer inside the references Type * res = addrType( arg->result ); res->set_lvalue( false ); // result of & is never an lvalue result = res; } else { // taking address of non-lvalue, must be a reference, loses one layer of reference if ( const ReferenceType * refType = dynamic_cast< const ReferenceType * >( arg->result.get() ) ) { Type * res = addrType( refType->base ); res->set_lvalue( false ); // result of & is never an lvalue result = res; } else { SemanticError( loc, arg->result, "Attempt to take address of non-lvalue expression: " ); } } } } // --- LabelAddressExpr // label address always has type `void*` LabelAddressExpr::LabelAddressExpr( const CodeLocation & loc, Label && a ) : Expr( loc, new PointerType{ new VoidType{} } ), arg( a ) {} // --- CastExpr CastExpr::CastExpr( const CodeLocation & loc, const Expr * a, GeneratedFlag g ) : Expr( loc, new VoidType{} ), arg( a ), isGenerated( g ) {} // --- KeywordCastExpr const std::string & KeywordCastExpr::targetString() const { static const std::string targetStrs[] = { "coroutine", "thread", "monitor" }; static_assert( (sizeof(targetStrs) / sizeof(targetStrs[0])) == ((unsigned long)NUMBER_OF_TARGETS), "Each KeywordCastExpr::Target should have a corresponding string representation" ); return targetStrs[(unsigned long)target]; } // --- MemberExpr MemberExpr::MemberExpr( const CodeLocation & loc, const DeclWithType * mem, const Expr * agg ) : Expr( loc ), member( mem ), aggregate( agg ) { assert( member ); assert( aggregate ); assert( aggregate->result ); assert(!"unimplemented; need TypeSubstitution, genericSubstitution"); } // --- VariableExpr VariableExpr::VariableExpr( const CodeLocation & loc, const DeclWithType * v ) : Expr( loc ), var( v ) { assert( var ); assert( var->get_type() ); result.set_and_mutate( var->get_type() )->set_lvalue( true ); } VariableExpr * VariableExpr::functionPointer( const CodeLocation & loc, const FunctionDecl * decl ) { // wrap usually-determined result type in a pointer VariableExpr * funcExpr = new VariableExpr{ loc, decl }; funcExpr->result = new PointerType{ funcExpr->result }; return funcExpr; } // --- ConstantExpr long long int ConstantExpr::intValue() const { if ( const BasicType * bty = result.as< BasicType >() ) { if ( bty->isInteger() ) { return val.ival; } } else if ( result.as< ZeroType >() ) { return 0; } else if ( result.as< OneType >() ) { return 1; } SemanticError( this, "Constant expression of non-integral type " ); } double ConstantExpr::floatValue() const { if ( const BasicType * bty = result.as< BasicType >() ) { if ( ! bty->isInteger() ) { return val.dval; } } SemanticError( this, "Constant expression of non-floating-point type " ); } ConstantExpr * ConstantExpr::from_bool( const CodeLocation & loc, bool b ) { return new ConstantExpr{ loc, new BasicType{ BasicType::Bool }, b ? "1" : "0", (unsigned long long)b }; } ConstantExpr * ConstantExpr::from_char( const CodeLocation & loc, char c ) { return new ConstantExpr{ loc, new BasicType{ BasicType::Char }, std::to_string( c ), (unsigned long long)c }; } ConstantExpr * ConstantExpr::from_int( const CodeLocation & loc, int i ) { return new ConstantExpr{ loc, new BasicType{ BasicType::SignedInt }, std::to_string( i ), (unsigned long long)i }; } ConstantExpr * ConstantExpr::from_ulong( const CodeLocation & loc, unsigned long i ) { return new ConstantExpr{ loc, new BasicType{ BasicType::LongUnsignedInt }, std::to_string( i ), (unsigned long long)i }; } ConstantExpr * ConstantExpr::from_double( const CodeLocation & loc, double d ) { return new ConstantExpr{ loc, new BasicType{ BasicType::Double }, std::to_string( d ), d }; } ConstantExpr * ConstantExpr::from_string( const CodeLocation & loc, const std::string & s ) { return new ConstantExpr{ loc, new ArrayType{ new BasicType{ BasicType::Char, CV::Const }, ConstantExpr::from_int( loc, s.size() + 1 /* null terminator */ ), FixedLen, DynamicDim }, std::string{"\""} + s + "\"", (unsigned long long)0 }; } ConstantExpr * ConstantExpr::null( const CodeLocation & loc, const Type * ptrType ) { return new ConstantExpr{ loc, ptrType ? ptrType : new PointerType{ new VoidType{} }, "0", (unsigned long long)0 }; } // --- SizeofExpr SizeofExpr::SizeofExpr( const CodeLocation & loc, const Expr * e ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} SizeofExpr::SizeofExpr( const CodeLocation & loc, const Type * t ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} // --- AlignofExpr AlignofExpr::AlignofExpr( const CodeLocation & loc, const Expr * e ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( e ), type( nullptr ) {} AlignofExpr::AlignofExpr( const CodeLocation & loc, const Type * t ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), expr( nullptr ), type( t ) {} // --- UntypedOffsetofExpr UntypedOffsetofExpr::UntypedOffsetofExpr( const CodeLocation & loc, const Type * ty, const std::string & mem ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) { assert( type ); } // --- OffsetofExpr OffsetofExpr::OffsetofExpr( const CodeLocation & loc, const Type * ty, const DeclWithType * mem ) : Expr( loc, new BasicType{ BasicType::LongUnsignedInt } ), type( ty ), member( mem ) { assert( type ); assert( member ); } // --- OffsetPackExpr OffsetPackExpr::OffsetPackExpr( const CodeLocation & loc, const StructInstType * ty ) : Expr( loc, new ArrayType{ new BasicType{ BasicType::LongUnsignedInt }, nullptr, FixedLen, DynamicDim } ), type( ty ) { assert( type ); } // --- LogicalExpr LogicalExpr::LogicalExpr( const CodeLocation & loc, const Expr * a1, const Expr * a2, LogicalFlag ia ) : Expr( loc, new BasicType{ BasicType::SignedInt } ), arg1( a1 ), arg2( a2 ), isAnd( ia ) {} } // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //