// // 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. // // PtrsAssignable.cc -- // // Author : Richard C. Bilson // Created On : Sun May 17 11:44:11 2015 // Last Modified By : Andrew // Last Modified On : Mon Jun 24 15:29:00 2019 // Update Count : 9 // #include "typeops.h" #include "AST/Pass.hpp" #include "AST/Type.hpp" #include "AST/TypeEnvironment.hpp" #include "Common/PassVisitor.h" #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment #include "SynTree/Type.h" // for TypeInstType, Type, BasicType #include "SynTree/Visitor.h" // for Visitor namespace ResolvExpr { struct PtrsAssignable : public WithShortCircuiting { PtrsAssignable( Type *dest, const TypeEnvironment &env ); int get_result() const { return result; } void previsit( Type * ) { visit_children = false; } void postvisit( VoidType * voidType ); void postvisit( BasicType * basicType ); void postvisit( PointerType * pointerType ); void postvisit( ArrayType * arrayType ); void postvisit( FunctionType * functionType ); void postvisit( StructInstType * inst ); void postvisit( UnionInstType * inst ); void postvisit( EnumInstType * inst ); void postvisit( TraitInstType * inst ); void postvisit( TypeInstType * inst ); void postvisit( TupleType * tupleType ); void postvisit( VarArgsType * varArgsType ); void postvisit( ZeroType * zeroType ); void postvisit( OneType * oneType ); private: Type *dest; int result; const TypeEnvironment &env; }; int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { // std::cerr << "assignable: " << src << " | " << dest << std::endl; if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { if ( const EqvClass *eqvClass = env.lookup( destAsTypeInst->get_name() ) ) { return ptrsAssignable( src, eqvClass->type, env ); } // if } // if if ( dynamic_cast< VoidType* >( dest ) ) { // void * = T * for any T is unsafe // xxx - this should be safe, but that currently breaks the build return -1; } else { PassVisitor ptrs( dest, env ); src->accept( ptrs ); return ptrs.pass.get_result(); } // if } PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} void PtrsAssignable::postvisit( VoidType * ) { // T * = void * is disallowed - this is a change from C, where any // void * can be assigned or passed to a non-void pointer without a cast. } void PtrsAssignable::postvisit( __attribute__((unused)) BasicType *basicType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) PointerType *pointerType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) ArrayType *arrayType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) FunctionType *functionType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) StructInstType *inst ) {} void PtrsAssignable::postvisit( __attribute__((unused)) UnionInstType *inst ) {} void PtrsAssignable::postvisit( EnumInstType * ) { if ( dynamic_cast< BasicType* >( dest ) ) { // int * = E *, etc. is safe. This isn't technically correct, as each // enum has one basic type that it is compatible with, an that type can // differ from enum to enum. Without replicating GCC's internal logic, // there is no way to know which type this particular enum is compatible // with, so punt on this for now. result = 1; } } void PtrsAssignable::postvisit( __attribute__((unused)) TraitInstType *inst ) {} void PtrsAssignable::postvisit( TypeInstType *inst ) { if ( const EqvClass *eqvClass = env.lookup( inst->get_name() ) ) { if ( eqvClass->type ) { // T * = S * for any S depends on the type bound to T result = ptrsAssignable( eqvClass->type, dest, env ); } } // if } void PtrsAssignable::postvisit( __attribute__((unused)) TupleType *tupleType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) VarArgsType *varArgsType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) ZeroType *zeroType ) {} void PtrsAssignable::postvisit( __attribute__((unused)) OneType *oneType ) {} // TODO: Get rid of the `_new` suffix when the old version is removed. struct PtrsAssignable_new : public ast::WithShortCircuiting { const ast::Type * dst; const ast::TypeEnvironment & typeEnv; int result; PtrsAssignable_new( const ast::Type * dst, const ast::TypeEnvironment & env ) : dst( dst ), typeEnv( env ), result( 0 ) {} void previsit( Type * ) { visit_children = false; } void postvisit( const ast::EnumInstType * ) { if ( dynamic_cast< const ast::BasicType * >( dst ) ) { // int * = E *, etc. is safe. This isn't technically correct, as each // enum has one basic type that it is compatible with, an that type can // differ from enum to enum. Without replicating GCC's internal logic, // there is no way to know which type this particular enum is compatible // with, so punt on this for now. result = 1; } } void postvisit( const ast::TypeInstType * inst ) { if ( const ast::EqvClass * eqv = typeEnv.lookup( inst->name ) ) { if ( eqv->bound ) { // T * = S * for any S depends on the type bound to T result = ptrsAssignable( eqv->bound, dst, typeEnv ); } } } }; int ptrsAssignable( const ast::Type * src, const ast::Type * dst, const ast::TypeEnvironment & env ) { if ( const ast::TypeInstType * dstAsInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { if ( const ast::EqvClass * eqv = env.lookup( dstAsInst->name ) ) { return ptrsAssignable( src, eqv->bound, env ); } } if ( dynamic_cast< const ast::VoidType * >( dst ) ) { return -1; } else { ast::Pass visitor( dst, env ); src->accept( visitor ); return visitor.pass.result; } // see ticket #136 (this should be able to replace the visitor). #if 0 if ( const ast::TypeInstType * dstAsTypeInst = dynamic_cast< const ast::TypeInstType* >( dst ) ) { if ( const ast::EqvClass * eqv = env.lookup( dstAsTypeInst->get_name() ) ) { return ptrsAssignable( src, eqv->type, env ); } // if } // if if ( dynamic_cast< VoidType* >( dst ) ) { // void * = T * for any T is unsafe // xxx - this should be safe, but that currently breaks the build return -1; } else if ( dynamic_cast< EnumInstType * >( src ) ) { if ( dynamic_cast< BasicType * >( dst ) ) { // int * = E *, etc. is safe. This isn't technically correct, as each // enum has one basic type that it is compatible with, an that type can // differ from enum to enum. Without replicating GCC's internal logic, // there is no way to know which type this particular enum is compatible // with, so punt on this for now. return 1; } } else if ( const ast::TypeInstType * typeInstType = dynamic_cast< const ast::TypeInstType * >( src ) ) { if ( const ast::EqvClass * eqv = env.lookup( typeInstType->name ) ) { if ( eqv->bound ) { // T * = S * for any S depends on the type bound to T return ptrsAssignable( eqv->bound, dst, env ); } } } return 0; #endif } } // namespace ResolvExpr // Local Variables: // // tab-width: 4 // // mode: c++ // // compile-command: "make install" // // End: //