| [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
 | 
|---|
| [df9317bd] | 11 | // Last Modified By : Andrew
 | 
|---|
 | 12 | // Last Modified On : Mon Jun 24 15:29:00 2019
 | 
|---|
 | 13 | // Update Count     : 9
 | 
|---|
| [a32b204] | 14 | //
 | 
|---|
 | 15 | 
 | 
|---|
| [df9317bd] | 16 | #include "typeops.h"
 | 
|---|
 | 17 | 
 | 
|---|
 | 18 | #include "AST/Pass.hpp"
 | 
|---|
 | 19 | #include "AST/Type.hpp"
 | 
|---|
 | 20 | #include "AST/TypeEnvironment.hpp"
 | 
|---|
| [f07c1e6] | 21 | #include "Common/PassVisitor.h"
 | 
|---|
| [ea6332d] | 22 | #include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
 | 
|---|
 | 23 | #include "SynTree/Type.h"                // for TypeInstType, Type, BasicType
 | 
|---|
 | 24 | #include "SynTree/Visitor.h"             // for Visitor
 | 
|---|
| [51b73452] | 25 | 
 | 
|---|
 | 26 | 
 | 
|---|
 | 27 | namespace ResolvExpr {
 | 
|---|
| [f07c1e6] | 28 |         struct PtrsAssignable : public WithShortCircuiting {
 | 
|---|
| [7870799] | 29 |                 PtrsAssignable( const Type * dest, const TypeEnvironment &env );
 | 
|---|
| [a32b204] | 30 | 
 | 
|---|
 | 31 |                 int get_result() const { return result; }
 | 
|---|
 | 32 | 
 | 
|---|
| [7870799] | 33 |                 void previsit( const Type * ) { visit_children = false; }
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 |                 void postvisit( const VoidType * voidType );
 | 
|---|
 | 36 |                 void postvisit( const BasicType * basicType );
 | 
|---|
 | 37 |                 void postvisit( const PointerType * pointerType );
 | 
|---|
 | 38 |                 void postvisit( const ArrayType * arrayType );
 | 
|---|
 | 39 |                 void postvisit( const FunctionType * functionType );
 | 
|---|
 | 40 |                 void postvisit( const StructInstType * inst );
 | 
|---|
 | 41 |                 void postvisit( const UnionInstType * inst );
 | 
|---|
 | 42 |                 void postvisit( const EnumInstType * inst );
 | 
|---|
 | 43 |                 void postvisit( const TraitInstType * inst );
 | 
|---|
 | 44 |                 void postvisit( const TypeInstType * inst );
 | 
|---|
 | 45 |                 void postvisit( const TupleType * tupleType );
 | 
|---|
 | 46 |                 void postvisit( const VarArgsType * varArgsType );
 | 
|---|
 | 47 |                 void postvisit( const ZeroType * zeroType );
 | 
|---|
 | 48 |                 void postvisit( const OneType * oneType );
 | 
|---|
| [a32b204] | 49 |           private:
 | 
|---|
| [7870799] | 50 |                 const Type * dest;
 | 
|---|
| [a32b204] | 51 |                 int result;
 | 
|---|
 | 52 |                 const TypeEnvironment &env;
 | 
|---|
 | 53 |         };
 | 
|---|
 | 54 | 
 | 
|---|
| [7870799] | 55 |         int ptrsAssignable( const Type *src, const Type * dest, const TypeEnvironment &env ) {
 | 
|---|
| [b0837e4] | 56 |                 // std::cerr << "assignable: " << src << " | " << dest << std::endl;
 | 
|---|
| [7870799] | 57 |                 if ( const TypeInstType * destAsTypeInst = dynamic_cast< const TypeInstType* >( dest ) ) {
 | 
|---|
 | 58 |                         if ( const EqvClass * eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
 | 
|---|
| [00ac42e] | 59 |                                 return ptrsAssignable( src, eqvClass->type, env );
 | 
|---|
| [a32b204] | 60 |                         } // if
 | 
|---|
 | 61 |                 } // if
 | 
|---|
| [7870799] | 62 |                 if ( dynamic_cast< const VoidType* >( dest ) ) {
 | 
|---|
| [b0837e4] | 63 |                         // void * = T * for any T is unsafe
 | 
|---|
 | 64 |                         // xxx - this should be safe, but that currently breaks the build
 | 
|---|
 | 65 |                         return -1;
 | 
|---|
| [a32b204] | 66 |                 } else {
 | 
|---|
| [f07c1e6] | 67 |                         PassVisitor<PtrsAssignable> ptrs( dest, env );
 | 
|---|
| [a32b204] | 68 |                         src->accept( ptrs );
 | 
|---|
| [f07c1e6] | 69 |                         return ptrs.pass.get_result();
 | 
|---|
| [a32b204] | 70 |                 } // if
 | 
|---|
| [c11e31c] | 71 |         }
 | 
|---|
| [a32b204] | 72 | 
 | 
|---|
| [7870799] | 73 |         PtrsAssignable::PtrsAssignable( const Type * dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) {}
 | 
|---|
| [c11e31c] | 74 | 
 | 
|---|
| [7870799] | 75 |         void PtrsAssignable::postvisit( const VoidType * ) {
 | 
|---|
| [4d5e57b] | 76 |                 // T * = void * is disallowed - this is a change from C, where any
 | 
|---|
 | 77 |                 // void * can be assigned or passed to a non-void pointer without a cast.
 | 
|---|
| [a32b204] | 78 |         }
 | 
|---|
| [c11e31c] | 79 | 
 | 
|---|
| [7870799] | 80 |         void PtrsAssignable::postvisit( const BasicType * ) {}
 | 
|---|
 | 81 |         void PtrsAssignable::postvisit( const PointerType * ) {}
 | 
|---|
 | 82 |         void PtrsAssignable::postvisit( const ArrayType * ) {}
 | 
|---|
 | 83 |         void PtrsAssignable::postvisit( const FunctionType * ) {}
 | 
|---|
| [c11e31c] | 84 | 
 | 
|---|
| [7870799] | 85 |         void PtrsAssignable::postvisit( const StructInstType * ) {}
 | 
|---|
 | 86 |         void PtrsAssignable::postvisit( const UnionInstType * ) {}
 | 
|---|
| [c11e31c] | 87 | 
 | 
|---|
| [7870799] | 88 |         void PtrsAssignable::postvisit( const EnumInstType * ) {
 | 
|---|
 | 89 |                 if ( dynamic_cast< const BasicType* >( dest ) ) {
 | 
|---|
| [b0837e4] | 90 |                         // int * = E *, etc. is safe. This isn't technically correct, as each
 | 
|---|
 | 91 |                         // enum has one basic type that it is compatible with, an that type can
 | 
|---|
 | 92 |                         // differ from enum to enum. Without replicating GCC's internal logic,
 | 
|---|
 | 93 |                         // there is no way to know which type this particular enum is compatible
 | 
|---|
 | 94 |                         // with, so punt on this for now.
 | 
|---|
| [a32b204] | 95 |                         result = 1;
 | 
|---|
 | 96 |                 }
 | 
|---|
 | 97 |         }
 | 
|---|
| [c11e31c] | 98 | 
 | 
|---|
| [7870799] | 99 |         void PtrsAssignable::postvisit(  const TraitInstType * ) {}
 | 
|---|
 | 100 |         void PtrsAssignable::postvisit( const TypeInstType * inst ) {
 | 
|---|
 | 101 |                 if ( const EqvClass * eqvClass = env.lookup( inst->name ) ) {
 | 
|---|
| [00ac42e] | 102 |                         if ( eqvClass->type ) {
 | 
|---|
 | 103 |                                 // T * = S * for any S depends on the type bound to T
 | 
|---|
 | 104 |                                 result = ptrsAssignable( eqvClass->type, dest, env );
 | 
|---|
 | 105 |                         }
 | 
|---|
| [a32b204] | 106 |                 } // if
 | 
|---|
| [c11e31c] | 107 |         }
 | 
|---|
 | 108 | 
 | 
|---|
| [7870799] | 109 |         void PtrsAssignable::postvisit( const TupleType * ) {}
 | 
|---|
 | 110 |         void PtrsAssignable::postvisit( const VarArgsType * ) {}
 | 
|---|
 | 111 |         void PtrsAssignable::postvisit( const ZeroType * ) {}
 | 
|---|
 | 112 |         void PtrsAssignable::postvisit( const OneType * ) {}
 | 
|---|
| [5ccb10d] | 113 | 
 | 
|---|
| [df9317bd] | 114 | // TODO: Get rid of the `_new` suffix when the old version is removed.
 | 
|---|
 | 115 | struct PtrsAssignable_new : public ast::WithShortCircuiting {
 | 
|---|
 | 116 |         const ast::Type * dst;
 | 
|---|
 | 117 |         const ast::TypeEnvironment & typeEnv;
 | 
|---|
 | 118 |         int result;
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |         PtrsAssignable_new( const ast::Type * dst, const ast::TypeEnvironment & env ) :
 | 
|---|
 | 121 |                 dst( dst ), typeEnv( env ), result( 0 ) {}
 | 
|---|
 | 122 | 
 | 
|---|
 | 123 |         void previsit( Type * ) { visit_children = false; }
 | 
|---|
 | 124 | 
 | 
|---|
 | 125 |         void postvisit( const ast::EnumInstType * ) {
 | 
|---|
 | 126 |                 if ( dynamic_cast< const ast::BasicType * >( dst ) ) {
 | 
|---|
 | 127 |                         // int * = E *, etc. is safe. This isn't technically correct, as each
 | 
|---|
 | 128 |                         // enum has one basic type that it is compatible with, an that type can
 | 
|---|
 | 129 |                         // differ from enum to enum. Without replicating GCC's internal logic,
 | 
|---|
 | 130 |                         // there is no way to know which type this particular enum is compatible
 | 
|---|
 | 131 |                         // with, so punt on this for now.
 | 
|---|
 | 132 |                         result = 1;
 | 
|---|
 | 133 |                 }
 | 
|---|
 | 134 |         }
 | 
|---|
 | 135 |         void postvisit( const ast::TypeInstType * inst ) {
 | 
|---|
| [3e5dd913] | 136 |                 if ( const ast::EqvClass * eqv = typeEnv.lookup( *inst ) ) {
 | 
|---|
| [df9317bd] | 137 |                         if ( eqv->bound ) {
 | 
|---|
 | 138 |                                 // T * = S * for any S depends on the type bound to T
 | 
|---|
 | 139 |                                 result = ptrsAssignable( eqv->bound, dst, typeEnv );
 | 
|---|
 | 140 |                         }
 | 
|---|
 | 141 |                 }
 | 
|---|
 | 142 |         }
 | 
|---|
 | 143 | };
 | 
|---|
 | 144 | 
 | 
|---|
| [fb2bde4] | 145 | int ptrsAssignable( const ast::Type * src, const ast::Type * dst,
 | 
|---|
 | 146 |                 const ast::TypeEnvironment & env ) {
 | 
|---|
| [df9317bd] | 147 |         if ( const ast::TypeInstType * dstAsInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
 | 
|---|
| [3e5dd913] | 148 |                 if ( const ast::EqvClass * eqv = env.lookup( *dstAsInst ) ) {
 | 
|---|
| [df9317bd] | 149 |                         return ptrsAssignable( src, eqv->bound, env );
 | 
|---|
 | 150 |                 }
 | 
|---|
 | 151 |         }
 | 
|---|
 | 152 |         if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
 | 
|---|
 | 153 |                 return -1;
 | 
|---|
 | 154 |         } else {
 | 
|---|
 | 155 |                 ast::Pass<PtrsAssignable_new> visitor( dst, env );
 | 
|---|
 | 156 |                 src->accept( visitor );
 | 
|---|
| [7ff3e522] | 157 |                 return visitor.core.result;
 | 
|---|
| [df9317bd] | 158 |         }
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 | // see ticket #136 (this should be able to replace the visitor).
 | 
|---|
 | 161 | #if 0
 | 
|---|
 | 162 |         if ( const ast::TypeInstType * dstAsTypeInst =
 | 
|---|
 | 163 |                         dynamic_cast< const ast::TypeInstType* >( dst ) ) {
 | 
|---|
 | 164 |                 if ( const ast::EqvClass * eqv = env.lookup( dstAsTypeInst->get_name() ) ) {
 | 
|---|
 | 165 |                         return ptrsAssignable( src, eqv->type, env );
 | 
|---|
 | 166 |                 } // if
 | 
|---|
 | 167 |         } // if
 | 
|---|
 | 168 |         if ( dynamic_cast< VoidType* >( dst ) ) {
 | 
|---|
 | 169 |                 // void * = T * for any T is unsafe
 | 
|---|
 | 170 |                 // xxx - this should be safe, but that currently breaks the build
 | 
|---|
 | 171 |                 return -1;
 | 
|---|
 | 172 |         } else if ( dynamic_cast< EnumInstType * >( src ) ) {
 | 
|---|
 | 173 |                 if ( dynamic_cast< BasicType * >( dst ) ) {
 | 
|---|
 | 174 |                         // int * = E *, etc. is safe. This isn't technically correct, as each
 | 
|---|
 | 175 |                         // enum has one basic type that it is compatible with, an that type can
 | 
|---|
 | 176 |                         // differ from enum to enum. Without replicating GCC's internal logic,
 | 
|---|
 | 177 |                         // there is no way to know which type this particular enum is compatible
 | 
|---|
 | 178 |                         // with, so punt on this for now.
 | 
|---|
 | 179 |                         return 1;
 | 
|---|
 | 180 |                 }
 | 
|---|
 | 181 |         } else if ( const ast::TypeInstType * typeInstType =
 | 
|---|
 | 182 |                         dynamic_cast< const ast::TypeInstType * >( src ) ) {
 | 
|---|
 | 183 |                 if ( const ast::EqvClass * eqv = env.lookup( typeInstType->name ) ) {
 | 
|---|
 | 184 |                         if ( eqv->bound ) {
 | 
|---|
 | 185 |                                 // T * = S * for any S depends on the type bound to T
 | 
|---|
 | 186 |                                 return ptrsAssignable( eqv->bound, dst, env );
 | 
|---|
 | 187 |                         }
 | 
|---|
 | 188 |                 }
 | 
|---|
 | 189 |         }
 | 
|---|
| [fb2bde4] | 190 |         return 0;
 | 
|---|
| [df9317bd] | 191 | #endif
 | 
|---|
| [fb2bde4] | 192 | }
 | 
|---|
 | 193 | 
 | 
|---|
| [51b73452] | 194 | } // namespace ResolvExpr
 | 
|---|
| [a32b204] | 195 | 
 | 
|---|
 | 196 | // Local Variables: //
 | 
|---|
 | 197 | // tab-width: 4 //
 | 
|---|
 | 198 | // mode: c++ //
 | 
|---|
 | 199 | // compile-command: "make install" //
 | 
|---|
 | 200 | // End: //
 | 
|---|