[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 | // |
---|
[2c57025] | 7 | // PtrsCastable.cc -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 11:48:00 2015 |
---|
[4040425] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Wed Mar 2 17:36:18 2016 |
---|
| 13 | // Update Count : 8 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[12145b9] | 16 | #include "Common/PassVisitor.h" |
---|
[ea6332d] | 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 |
---|
[51b7345] | 23 | |
---|
| 24 | namespace ResolvExpr { |
---|
[12145b9] | 25 | struct PtrsCastable : public WithShortCircuiting { |
---|
[a32b204] | 26 | public: |
---|
| 27 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
[2c57025] | 28 | |
---|
[a32b204] | 29 | int get_result() const { return result; } |
---|
| 30 | |
---|
[12145b9] | 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 ); |
---|
[a32b204] | 47 | private: |
---|
| 48 | Type *dest; |
---|
| 49 | int result; |
---|
| 50 | const TypeEnvironment &env; |
---|
| 51 | const SymTab::Indexer &indexer; |
---|
| 52 | }; |
---|
| 53 | |
---|
[b0837e4] | 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 ) { |
---|
[a32b204] | 68 | return -1; |
---|
| 69 | } // if |
---|
| 70 | } // if |
---|
[b0837e4] | 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 | } |
---|
[1d29d46] | 77 | } |
---|
[a32b204] | 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 ) ) { |
---|
[12145b9] | 83 | // xxx - should this be ptrsCastable? |
---|
[a32b204] | 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 { |
---|
[12145b9] | 90 | PassVisitor<PtrsCastable> ptrs( dest, env, indexer ); |
---|
[a32b204] | 91 | src->accept( ptrs ); |
---|
[12145b9] | 92 | return ptrs.pass.get_result(); |
---|
[a32b204] | 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 | |
---|
[12145b9] | 100 | void PtrsCastable::postvisit( VoidType * ) { |
---|
[a32b204] | 101 | result = objectCast( dest, env, indexer ); |
---|
| 102 | } |
---|
| 103 | |
---|
[12145b9] | 104 | void PtrsCastable::postvisit( BasicType * ) { |
---|
[a32b204] | 105 | result = objectCast( dest, env, indexer ); |
---|
| 106 | } |
---|
| 107 | |
---|
[12145b9] | 108 | void PtrsCastable::postvisit( PointerType * ) { |
---|
[a32b204] | 109 | result = objectCast( dest, env, indexer ); |
---|
| 110 | } |
---|
| 111 | |
---|
[12145b9] | 112 | void PtrsCastable::postvisit( ArrayType * ) { |
---|
[a32b204] | 113 | result = objectCast( dest, env, indexer ); |
---|
| 114 | } |
---|
| 115 | |
---|
[12145b9] | 116 | void PtrsCastable::postvisit( FunctionType * ) { |
---|
[1d29d46] | 117 | // result = -1; |
---|
| 118 | result = functionCast( dest, env, indexer ); |
---|
[a32b204] | 119 | } |
---|
| 120 | |
---|
[12145b9] | 121 | void PtrsCastable::postvisit( StructInstType * ) { |
---|
[a32b204] | 122 | result = objectCast( dest, env, indexer ); |
---|
| 123 | } |
---|
| 124 | |
---|
[12145b9] | 125 | void PtrsCastable::postvisit( UnionInstType * ) { |
---|
[a32b204] | 126 | result = objectCast( dest, env, indexer ); |
---|
| 127 | } |
---|
| 128 | |
---|
[12145b9] | 129 | void PtrsCastable::postvisit( EnumInstType * ) { |
---|
[931dd12] | 130 | if ( dynamic_cast< EnumInstType* >( dest ) ) { |
---|
[a32b204] | 131 | result = 1; |
---|
[931dd12] | 132 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { |
---|
[a32b204] | 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 | |
---|
[12145b9] | 143 | void PtrsCastable::postvisit( TraitInstType * ) {} |
---|
[a32b204] | 144 | |
---|
[12145b9] | 145 | void PtrsCastable::postvisit(TypeInstType *inst) { |
---|
[1d29d46] | 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; |
---|
[a32b204] | 148 | } |
---|
| 149 | |
---|
[12145b9] | 150 | void PtrsCastable::postvisit( TupleType * ) { |
---|
[a32b204] | 151 | result = objectCast( dest, env, indexer ); |
---|
| 152 | } |
---|
[44b7088] | 153 | |
---|
[12145b9] | 154 | void PtrsCastable::postvisit( VarArgsType * ) { |
---|
[44b7088] | 155 | result = objectCast( dest, env, indexer ); |
---|
| 156 | } |
---|
[89e6ffc] | 157 | |
---|
[12145b9] | 158 | void PtrsCastable::postvisit( ZeroType * ) { |
---|
[89e6ffc] | 159 | result = objectCast( dest, env, indexer ); |
---|
| 160 | } |
---|
| 161 | |
---|
[12145b9] | 162 | void PtrsCastable::postvisit( OneType * ) { |
---|
[89e6ffc] | 163 | result = objectCast( dest, env, indexer ); |
---|
| 164 | } |
---|
[51b7345] | 165 | } // namespace ResolvExpr |
---|
[a32b204] | 166 | |
---|
| 167 | // Local Variables: // |
---|
| 168 | // tab-width: 4 // |
---|
| 169 | // mode: c++ // |
---|
| 170 | // compile-command: "make install" // |
---|
| 171 | // End: // |
---|