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