[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 | |
---|
[ea6332d] | 16 | #include "ResolvExpr/TypeEnvironment.h" // for EqvClass, TypeEnvironment |
---|
| 17 | #include "SynTree/Type.h" // for TypeInstType, Type, BasicType |
---|
| 18 | #include "SynTree/Visitor.h" // for Visitor |
---|
[51b7345] | 19 | |
---|
| 20 | |
---|
| 21 | namespace ResolvExpr { |
---|
[a32b204] | 22 | class PtrsAssignable : public Visitor { |
---|
| 23 | public: |
---|
| 24 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
| 25 | |
---|
| 26 | int get_result() const { return result; } |
---|
| 27 | |
---|
| 28 | virtual void visit( VoidType *voidType ); |
---|
| 29 | virtual void visit( BasicType *basicType ); |
---|
| 30 | virtual void visit( PointerType *pointerType ); |
---|
| 31 | virtual void visit( ArrayType *arrayType ); |
---|
| 32 | virtual void visit( FunctionType *functionType ); |
---|
| 33 | virtual void visit( StructInstType *inst ); |
---|
| 34 | virtual void visit( UnionInstType *inst ); |
---|
| 35 | virtual void visit( EnumInstType *inst ); |
---|
[4040425] | 36 | virtual void visit( TraitInstType *inst ); |
---|
[a32b204] | 37 | virtual void visit( TypeInstType *inst ); |
---|
| 38 | virtual void visit( TupleType *tupleType ); |
---|
[44b7088] | 39 | virtual void visit( VarArgsType *varArgsType ); |
---|
[89e6ffc] | 40 | virtual void visit( ZeroType *zeroType ); |
---|
| 41 | virtual void visit( OneType *oneType ); |
---|
[a32b204] | 42 | private: |
---|
| 43 | Type *dest; |
---|
| 44 | int result; |
---|
| 45 | const TypeEnvironment &env; |
---|
| 46 | }; |
---|
| 47 | |
---|
| 48 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
[b0837e4] | 49 | // std::cerr << "assignable: " << src << " | " << dest << std::endl; |
---|
[a32b204] | 50 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 51 | EqvClass eqvClass; |
---|
| 52 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
| 53 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
| 54 | } // if |
---|
| 55 | } // if |
---|
| 56 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
[b0837e4] | 57 | // void * = T * for any T is unsafe |
---|
| 58 | // xxx - this should be safe, but that currently breaks the build |
---|
| 59 | return -1; |
---|
[a32b204] | 60 | } else { |
---|
| 61 | PtrsAssignable ptrs( dest, env ); |
---|
| 62 | src->accept( ptrs ); |
---|
| 63 | return ptrs.get_result(); |
---|
| 64 | } // if |
---|
[c11e31c] | 65 | } |
---|
[a32b204] | 66 | |
---|
[7e003011] | 67 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {} |
---|
[c11e31c] | 68 | |
---|
[7e003011] | 69 | void PtrsAssignable::visit( __attribute((unused)) VoidType *voidType ) { |
---|
[b0837e4] | 70 | if ( ! dynamic_cast< FunctionType* >( dest ) ) { |
---|
| 71 | // T * = void * is safe for any T that is not a function type. |
---|
| 72 | // xxx - this should be unsafe... |
---|
| 73 | result = 1; |
---|
[a32b204] | 74 | } // if |
---|
| 75 | } |
---|
[c11e31c] | 76 | |
---|
[7e003011] | 77 | void PtrsAssignable::visit( __attribute__((unused)) BasicType *basicType ) {} |
---|
| 78 | void PtrsAssignable::visit( __attribute__((unused)) PointerType *pointerType ) {} |
---|
| 79 | void PtrsAssignable::visit( __attribute__((unused)) ArrayType *arrayType ) {} |
---|
[b0837e4] | 80 | void PtrsAssignable::visit( __attribute__((unused)) FunctionType *functionType ) {} |
---|
[c11e31c] | 81 | |
---|
[7e003011] | 82 | void PtrsAssignable::visit( __attribute__((unused)) StructInstType *inst ) {} |
---|
| 83 | void PtrsAssignable::visit( __attribute__((unused)) UnionInstType *inst ) {} |
---|
[c11e31c] | 84 | |
---|
[5ccb10d] | 85 | void PtrsAssignable::visit( EnumInstType * ) { |
---|
[b0837e4] | 86 | if ( dynamic_cast< BasicType* >( dest ) ) { |
---|
| 87 | // int * = E *, etc. is safe. This isn't technically correct, as each |
---|
| 88 | // enum has one basic type that it is compatible with, an that type can |
---|
| 89 | // differ from enum to enum. Without replicating GCC's internal logic, |
---|
| 90 | // there is no way to know which type this particular enum is compatible |
---|
| 91 | // with, so punt on this for now. |
---|
[a32b204] | 92 | result = 1; |
---|
| 93 | } |
---|
| 94 | } |
---|
[c11e31c] | 95 | |
---|
[7e003011] | 96 | void PtrsAssignable::visit( __attribute__((unused)) TraitInstType *inst ) {} |
---|
[a32b204] | 97 | void PtrsAssignable::visit( TypeInstType *inst ) { |
---|
| 98 | EqvClass eqvClass; |
---|
[1521de20] | 99 | if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) { |
---|
[b0837e4] | 100 | // T * = S * for any S depends on the type bound to T |
---|
[a32b204] | 101 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
| 102 | } // if |
---|
[c11e31c] | 103 | } |
---|
| 104 | |
---|
[7e003011] | 105 | void PtrsAssignable::visit( __attribute__((unused)) TupleType *tupleType ) {} |
---|
| 106 | void PtrsAssignable::visit( __attribute__((unused)) VarArgsType *varArgsType ) {} |
---|
| 107 | void PtrsAssignable::visit( __attribute__((unused)) ZeroType *zeroType ) {} |
---|
| 108 | void PtrsAssignable::visit( __attribute__((unused)) OneType *oneType ) {} |
---|
[5ccb10d] | 109 | |
---|
[51b7345] | 110 | } // namespace ResolvExpr |
---|
[a32b204] | 111 | |
---|
| 112 | // Local Variables: // |
---|
| 113 | // tab-width: 4 // |
---|
| 114 | // mode: c++ // |
---|
| 115 | // compile-command: "make install" // |
---|
| 116 | // End: // |
---|