| [a32b204] | 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 | // | 
|---|
| [5ccb10d] | 7 | // PtrsAssignable.cc -- | 
|---|
| [a32b204] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 11:44:11 2015 | 
|---|
| [4040425] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Wed Mar  2 17:36:05 2016 | 
|---|
|  | 13 | // Update Count     : 8 | 
|---|
| [a32b204] | 14 | // | 
|---|
|  | 15 |  | 
|---|
| [f07c1e6] | 16 | #include "Common/PassVisitor.h" | 
|---|
| [ea6332d] | 17 | #include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment | 
|---|
|  | 18 | #include "SynTree/Type.h"                // for TypeInstType, Type, BasicType | 
|---|
|  | 19 | #include "SynTree/Visitor.h"             // for Visitor | 
|---|
| [51b73452] | 20 |  | 
|---|
|  | 21 |  | 
|---|
|  | 22 | namespace ResolvExpr { | 
|---|
| [f07c1e6] | 23 | struct PtrsAssignable : public WithShortCircuiting { | 
|---|
| [a32b204] | 24 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); | 
|---|
|  | 25 |  | 
|---|
|  | 26 | int get_result() const { return result; } | 
|---|
|  | 27 |  | 
|---|
| [f07c1e6] | 28 | void previsit( Type * ) { visit_children = false; } | 
|---|
|  | 29 |  | 
|---|
|  | 30 | void postvisit( VoidType * voidType ); | 
|---|
|  | 31 | void postvisit( BasicType * basicType ); | 
|---|
|  | 32 | void postvisit( PointerType * pointerType ); | 
|---|
|  | 33 | void postvisit( ArrayType * arrayType ); | 
|---|
|  | 34 | void postvisit( FunctionType * functionType ); | 
|---|
|  | 35 | void postvisit( StructInstType * inst ); | 
|---|
|  | 36 | void postvisit( UnionInstType * inst ); | 
|---|
|  | 37 | void postvisit( EnumInstType * inst ); | 
|---|
|  | 38 | void postvisit( TraitInstType * inst ); | 
|---|
|  | 39 | void postvisit( TypeInstType * inst ); | 
|---|
|  | 40 | void postvisit( TupleType * tupleType ); | 
|---|
|  | 41 | void postvisit( VarArgsType * varArgsType ); | 
|---|
|  | 42 | void postvisit( ZeroType * zeroType ); | 
|---|
|  | 43 | void postvisit( OneType * oneType ); | 
|---|
| [a32b204] | 44 | private: | 
|---|
|  | 45 | Type *dest; | 
|---|
|  | 46 | int result; | 
|---|
|  | 47 | const TypeEnvironment &env; | 
|---|
|  | 48 | }; | 
|---|
|  | 49 |  | 
|---|
|  | 50 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { | 
|---|
| [b0837e4] | 51 | // std::cerr << "assignable: " << src << " | " << dest << std::endl; | 
|---|
| [a32b204] | 52 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { | 
|---|
| [00ac42e] | 53 | if ( const EqvClass *eqvClass = env.lookup( destAsTypeInst->get_name() ) ) { | 
|---|
|  | 54 | return ptrsAssignable( src, eqvClass->type, env ); | 
|---|
| [a32b204] | 55 | } // if | 
|---|
|  | 56 | } // if | 
|---|
|  | 57 | if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
| [b0837e4] | 58 | // void * = T * for any T is unsafe | 
|---|
|  | 59 | // xxx - this should be safe, but that currently breaks the build | 
|---|
|  | 60 | return -1; | 
|---|
| [a32b204] | 61 | } else { | 
|---|
| [f07c1e6] | 62 | PassVisitor<PtrsAssignable> ptrs( dest, env ); | 
|---|
| [a32b204] | 63 | src->accept( ptrs ); | 
|---|
| [f07c1e6] | 64 | return ptrs.pass.get_result(); | 
|---|
| [a32b204] | 65 | } // if | 
|---|
| [c11e31c] | 66 | } | 
|---|
| [a32b204] | 67 |  | 
|---|
| [7e003011] | 68 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} | 
|---|
| [c11e31c] | 69 |  | 
|---|
| [f07c1e6] | 70 | void PtrsAssignable::postvisit( VoidType * ) { | 
|---|
| [4d5e57b] | 71 | // T * = void * is disallowed - this is a change from C, where any | 
|---|
|  | 72 | // void * can be assigned or passed to a non-void pointer without a cast. | 
|---|
| [a32b204] | 73 | } | 
|---|
| [c11e31c] | 74 |  | 
|---|
| [f07c1e6] | 75 | void PtrsAssignable::postvisit( __attribute__((unused)) BasicType *basicType ) {} | 
|---|
|  | 76 | void PtrsAssignable::postvisit( __attribute__((unused)) PointerType *pointerType ) {} | 
|---|
|  | 77 | void PtrsAssignable::postvisit( __attribute__((unused)) ArrayType *arrayType ) {} | 
|---|
|  | 78 | void PtrsAssignable::postvisit( __attribute__((unused)) FunctionType *functionType ) {} | 
|---|
| [c11e31c] | 79 |  | 
|---|
| [f07c1e6] | 80 | void PtrsAssignable::postvisit(  __attribute__((unused)) StructInstType *inst ) {} | 
|---|
|  | 81 | void PtrsAssignable::postvisit(  __attribute__((unused)) UnionInstType *inst ) {} | 
|---|
| [c11e31c] | 82 |  | 
|---|
| [f07c1e6] | 83 | void PtrsAssignable::postvisit( EnumInstType * ) { | 
|---|
| [b0837e4] | 84 | if ( dynamic_cast< BasicType* >( dest ) ) { | 
|---|
|  | 85 | // int * = E *, etc. is safe. This isn't technically correct, as each | 
|---|
|  | 86 | // enum has one basic type that it is compatible with, an that type can | 
|---|
|  | 87 | // differ from enum to enum. Without replicating GCC's internal logic, | 
|---|
|  | 88 | // there is no way to know which type this particular enum is compatible | 
|---|
|  | 89 | // with, so punt on this for now. | 
|---|
| [a32b204] | 90 | result = 1; | 
|---|
|  | 91 | } | 
|---|
|  | 92 | } | 
|---|
| [c11e31c] | 93 |  | 
|---|
| [f07c1e6] | 94 | void PtrsAssignable::postvisit(  __attribute__((unused)) TraitInstType *inst ) {} | 
|---|
|  | 95 | void PtrsAssignable::postvisit( TypeInstType *inst ) { | 
|---|
| [00ac42e] | 96 | if ( const EqvClass *eqvClass = env.lookup( inst->get_name() ) ) { | 
|---|
|  | 97 | if ( eqvClass->type ) { | 
|---|
|  | 98 | // T * = S * for any S depends on the type bound to T | 
|---|
|  | 99 | result = ptrsAssignable( eqvClass->type, dest, env ); | 
|---|
|  | 100 | } | 
|---|
| [a32b204] | 101 | } // if | 
|---|
| [c11e31c] | 102 | } | 
|---|
|  | 103 |  | 
|---|
| [f07c1e6] | 104 | void PtrsAssignable::postvisit(  __attribute__((unused)) TupleType *tupleType ) {} | 
|---|
|  | 105 | void PtrsAssignable::postvisit(  __attribute__((unused)) VarArgsType *varArgsType ) {} | 
|---|
|  | 106 | void PtrsAssignable::postvisit(  __attribute__((unused)) ZeroType *zeroType ) {} | 
|---|
|  | 107 | void PtrsAssignable::postvisit(  __attribute__((unused)) OneType *oneType ) {} | 
|---|
| [5ccb10d] | 108 |  | 
|---|
| [51b73452] | 109 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 110 |  | 
|---|
|  | 111 | // Local Variables: // | 
|---|
|  | 112 | // tab-width: 4 // | 
|---|
|  | 113 | // mode: c++ // | 
|---|
|  | 114 | // compile-command: "make install" // | 
|---|
|  | 115 | // End: // | 
|---|