| 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 | // PtrsCastable.cc -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 11:48:00 2015 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr | 
|---|
| 12 | // Last Modified On : Wed Mar  2 17:36:18 2016 | 
|---|
| 13 | // Update Count     : 8 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #include "Common/PassVisitor.h" | 
|---|
| 17 | #include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment | 
|---|
| 18 | #include "SymTab/Indexer.h"              // for Indexer | 
|---|
| 19 | #include "SynTree/Declaration.h"         // for TypeDecl, TypeDecl::Kind::Ftype | 
|---|
| 20 | #include "SynTree/Type.h"                // for TypeInstType, Type, BasicType | 
|---|
| 21 | #include "SynTree/Visitor.h"             // for Visitor | 
|---|
| 22 | #include "typeops.h"                     // for ptrsAssignable | 
|---|
| 23 |  | 
|---|
| 24 | namespace ResolvExpr { | 
|---|
| 25 | struct PtrsCastable : public WithShortCircuiting  { | 
|---|
| 26 | public: | 
|---|
| 27 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); | 
|---|
| 28 |  | 
|---|
| 29 | int get_result() const { return result; } | 
|---|
| 30 |  | 
|---|
| 31 | void previsit( Type * ) { visit_children = false; } | 
|---|
| 32 |  | 
|---|
| 33 | void postvisit( VoidType * voidType ); | 
|---|
| 34 | void postvisit( BasicType * basicType ); | 
|---|
| 35 | void postvisit( PointerType * pointerType ); | 
|---|
| 36 | void postvisit( ArrayType * arrayType ); | 
|---|
| 37 | void postvisit( FunctionType * functionType ); | 
|---|
| 38 | void postvisit( StructInstType * inst ); | 
|---|
| 39 | void postvisit( UnionInstType * inst ); | 
|---|
| 40 | void postvisit( EnumInstType * inst ); | 
|---|
| 41 | void postvisit( TraitInstType * inst ); | 
|---|
| 42 | void postvisit( TypeInstType * inst ); | 
|---|
| 43 | void postvisit( TupleType * tupleType ); | 
|---|
| 44 | void postvisit( VarArgsType * varArgsType ); | 
|---|
| 45 | void postvisit( ZeroType * zeroType ); | 
|---|
| 46 | void postvisit( OneType * oneType ); | 
|---|
| 47 | private: | 
|---|
| 48 | Type *dest; | 
|---|
| 49 | int result; | 
|---|
| 50 | const TypeEnvironment &env; | 
|---|
| 51 | const SymTab::Indexer &indexer; | 
|---|
| 52 | }; | 
|---|
| 53 |  | 
|---|
| 54 | namespace { | 
|---|
| 55 | int objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { | 
|---|
| 56 | if ( dynamic_cast< FunctionType* >( src ) ) { | 
|---|
| 57 | return -1; | 
|---|
| 58 | } else if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) { | 
|---|
| 59 | EqvClass eqvClass; | 
|---|
| 60 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { | 
|---|
| 61 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { | 
|---|
| 62 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { | 
|---|
| 63 | return -1; | 
|---|
| 64 | } // if | 
|---|
| 65 | } //if | 
|---|
| 66 | } else if ( env.lookup( typeInst->get_name(), eqvClass ) ) { | 
|---|
| 67 | if ( eqvClass.data.kind == TypeDecl::Ftype ) { | 
|---|
| 68 | return -1; | 
|---|
| 69 | } // if | 
|---|
| 70 | } // if | 
|---|
| 71 | } //if | 
|---|
| 72 | return 1; | 
|---|
| 73 | } | 
|---|
| 74 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { | 
|---|
| 75 | return -1 * objectCast( src, env, indexer );  // reverse the sense of objectCast | 
|---|
| 76 | } | 
|---|
| 77 | } | 
|---|
| 78 |  | 
|---|
| 79 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { | 
|---|
| 80 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { | 
|---|
| 81 | EqvClass eqvClass; | 
|---|
| 82 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { | 
|---|
| 83 | // xxx - should this be ptrsCastable? | 
|---|
| 84 | return ptrsAssignable( src, eqvClass.type, env ); | 
|---|
| 85 | } // if | 
|---|
| 86 | } // if | 
|---|
| 87 | if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
| 88 | return objectCast( src, env, indexer ); | 
|---|
| 89 | } else { | 
|---|
| 90 | PassVisitor<PtrsCastable> ptrs( dest, env, indexer ); | 
|---|
| 91 | src->accept( ptrs ); | 
|---|
| 92 | return ptrs.pass.get_result(); | 
|---|
| 93 | } // if | 
|---|
| 94 | } | 
|---|
| 95 |  | 
|---|
| 96 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) | 
|---|
| 97 | : dest( dest ), result( 0 ), env( env ), indexer( indexer )     { | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | void PtrsCastable::postvisit( VoidType * ) { | 
|---|
| 101 | result = objectCast( dest, env, indexer ); | 
|---|
| 102 | } | 
|---|
| 103 |  | 
|---|
| 104 | void PtrsCastable::postvisit( BasicType * ) { | 
|---|
| 105 | result = objectCast( dest, env, indexer ); | 
|---|
| 106 | } | 
|---|
| 107 |  | 
|---|
| 108 | void PtrsCastable::postvisit( PointerType * ) { | 
|---|
| 109 | result = objectCast( dest, env, indexer ); | 
|---|
| 110 | } | 
|---|
| 111 |  | 
|---|
| 112 | void PtrsCastable::postvisit( ArrayType * ) { | 
|---|
| 113 | result = objectCast( dest, env, indexer ); | 
|---|
| 114 | } | 
|---|
| 115 |  | 
|---|
| 116 | void PtrsCastable::postvisit( FunctionType * ) { | 
|---|
| 117 | // result = -1; | 
|---|
| 118 | result = functionCast( dest, env, indexer ); | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | void PtrsCastable::postvisit( StructInstType * ) { | 
|---|
| 122 | result = objectCast( dest, env, indexer ); | 
|---|
| 123 | } | 
|---|
| 124 |  | 
|---|
| 125 | void PtrsCastable::postvisit( UnionInstType * ) { | 
|---|
| 126 | result = objectCast( dest, env, indexer ); | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | void PtrsCastable::postvisit( EnumInstType * ) { | 
|---|
| 130 | if ( dynamic_cast< EnumInstType* >( dest ) ) { | 
|---|
| 131 | result = 1; | 
|---|
| 132 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
| 133 | if ( bt->get_kind() == BasicType::SignedInt ) { | 
|---|
| 134 | result = 0; | 
|---|
| 135 | } else { | 
|---|
| 136 | result = 1; | 
|---|
| 137 | } | 
|---|
| 138 | } else { | 
|---|
| 139 | result = objectCast( dest, env, indexer ); | 
|---|
| 140 | } | 
|---|
| 141 | } | 
|---|
| 142 |  | 
|---|
| 143 | void PtrsCastable::postvisit( TraitInstType * ) {} | 
|---|
| 144 |  | 
|---|
| 145 | void PtrsCastable::postvisit(TypeInstType *inst) { | 
|---|
| 146 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; | 
|---|
| 147 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; | 
|---|
| 148 | } | 
|---|
| 149 |  | 
|---|
| 150 | void PtrsCastable::postvisit( TupleType * ) { | 
|---|
| 151 | result = objectCast( dest, env, indexer ); | 
|---|
| 152 | } | 
|---|
| 153 |  | 
|---|
| 154 | void PtrsCastable::postvisit( VarArgsType * ) { | 
|---|
| 155 | result = objectCast( dest, env, indexer ); | 
|---|
| 156 | } | 
|---|
| 157 |  | 
|---|
| 158 | void PtrsCastable::postvisit( ZeroType * ) { | 
|---|
| 159 | result = objectCast( dest, env, indexer ); | 
|---|
| 160 | } | 
|---|
| 161 |  | 
|---|
| 162 | void PtrsCastable::postvisit( OneType * ) { | 
|---|
| 163 | result = objectCast( dest, env, indexer ); | 
|---|
| 164 | } | 
|---|
| 165 | } // namespace ResolvExpr | 
|---|
| 166 |  | 
|---|
| 167 | // Local Variables: // | 
|---|
| 168 | // tab-width: 4 // | 
|---|
| 169 | // mode: c++ // | 
|---|
| 170 | // compile-command: "make install" // | 
|---|
| 171 | // End: // | 
|---|