[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" |
---|
[982f95d] | 17 | #include "ResolvExpr/TypeEnvironment.h" // for ClassRef, TypeEnvironment |
---|
[ea6332d] | 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 | if ( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
| 60 | if ( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
| 61 | if ( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
| 62 | return -1; |
---|
| 63 | } // if |
---|
| 64 | } //if |
---|
[982f95d] | 65 | } else if ( ClassRef eqvClass = env.lookup( typeInst->get_name() ) ) { |
---|
| 66 | if ( eqvClass.get_bound().data.kind == TypeDecl::Ftype ) { |
---|
[a32b204] | 67 | return -1; |
---|
| 68 | } // if |
---|
| 69 | } // if |
---|
[b0837e4] | 70 | } //if |
---|
| 71 | return 1; |
---|
| 72 | } |
---|
| 73 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
| 74 | return -1 * objectCast( src, env, indexer ); // reverse the sense of objectCast |
---|
| 75 | } |
---|
[1d29d46] | 76 | } |
---|
[a32b204] | 77 | |
---|
| 78 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { |
---|
| 79 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
[982f95d] | 80 | if ( ClassRef eqvClass = env.lookup( destAsTypeInst->get_name() ) ) { |
---|
[12145b9] | 81 | // xxx - should this be ptrsCastable? |
---|
[982f95d] | 82 | return ptrsAssignable( src, eqvClass.get_bound().type, env ); |
---|
[a32b204] | 83 | } // if |
---|
| 84 | } // if |
---|
| 85 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
| 86 | return objectCast( src, env, indexer ); |
---|
| 87 | } else { |
---|
[12145b9] | 88 | PassVisitor<PtrsCastable> ptrs( dest, env, indexer ); |
---|
[a32b204] | 89 | src->accept( ptrs ); |
---|
[12145b9] | 90 | return ptrs.pass.get_result(); |
---|
[a32b204] | 91 | } // if |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
| 95 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) { |
---|
| 96 | } |
---|
| 97 | |
---|
[12145b9] | 98 | void PtrsCastable::postvisit( VoidType * ) { |
---|
[a32b204] | 99 | result = objectCast( dest, env, indexer ); |
---|
| 100 | } |
---|
| 101 | |
---|
[12145b9] | 102 | void PtrsCastable::postvisit( BasicType * ) { |
---|
[a32b204] | 103 | result = objectCast( dest, env, indexer ); |
---|
| 104 | } |
---|
| 105 | |
---|
[12145b9] | 106 | void PtrsCastable::postvisit( PointerType * ) { |
---|
[a32b204] | 107 | result = objectCast( dest, env, indexer ); |
---|
| 108 | } |
---|
| 109 | |
---|
[12145b9] | 110 | void PtrsCastable::postvisit( ArrayType * ) { |
---|
[a32b204] | 111 | result = objectCast( dest, env, indexer ); |
---|
| 112 | } |
---|
| 113 | |
---|
[12145b9] | 114 | void PtrsCastable::postvisit( FunctionType * ) { |
---|
[1d29d46] | 115 | // result = -1; |
---|
| 116 | result = functionCast( dest, env, indexer ); |
---|
[a32b204] | 117 | } |
---|
| 118 | |
---|
[12145b9] | 119 | void PtrsCastable::postvisit( StructInstType * ) { |
---|
[a32b204] | 120 | result = objectCast( dest, env, indexer ); |
---|
| 121 | } |
---|
| 122 | |
---|
[12145b9] | 123 | void PtrsCastable::postvisit( UnionInstType * ) { |
---|
[a32b204] | 124 | result = objectCast( dest, env, indexer ); |
---|
| 125 | } |
---|
| 126 | |
---|
[12145b9] | 127 | void PtrsCastable::postvisit( EnumInstType * ) { |
---|
[931dd12] | 128 | if ( dynamic_cast< EnumInstType* >( dest ) ) { |
---|
[a32b204] | 129 | result = 1; |
---|
[931dd12] | 130 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { |
---|
[a32b204] | 131 | if ( bt->get_kind() == BasicType::SignedInt ) { |
---|
| 132 | result = 0; |
---|
| 133 | } else { |
---|
| 134 | result = 1; |
---|
| 135 | } |
---|
| 136 | } else { |
---|
| 137 | result = objectCast( dest, env, indexer ); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
[12145b9] | 141 | void PtrsCastable::postvisit( TraitInstType * ) {} |
---|
[a32b204] | 142 | |
---|
[12145b9] | 143 | void PtrsCastable::postvisit(TypeInstType *inst) { |
---|
[1d29d46] | 144 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; |
---|
| 145 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; |
---|
[a32b204] | 146 | } |
---|
| 147 | |
---|
[12145b9] | 148 | void PtrsCastable::postvisit( TupleType * ) { |
---|
[a32b204] | 149 | result = objectCast( dest, env, indexer ); |
---|
| 150 | } |
---|
[44b7088] | 151 | |
---|
[12145b9] | 152 | void PtrsCastable::postvisit( VarArgsType * ) { |
---|
[44b7088] | 153 | result = objectCast( dest, env, indexer ); |
---|
| 154 | } |
---|
[89e6ffc] | 155 | |
---|
[12145b9] | 156 | void PtrsCastable::postvisit( ZeroType * ) { |
---|
[89e6ffc] | 157 | result = objectCast( dest, env, indexer ); |
---|
| 158 | } |
---|
| 159 | |
---|
[12145b9] | 160 | void PtrsCastable::postvisit( OneType * ) { |
---|
[89e6ffc] | 161 | result = objectCast( dest, env, indexer ); |
---|
| 162 | } |
---|
[51b7345] | 163 | } // namespace ResolvExpr |
---|
[a32b204] | 164 | |
---|
| 165 | // Local Variables: // |
---|
| 166 | // tab-width: 4 // |
---|
| 167 | // mode: c++ // |
---|
| 168 | // compile-command: "make install" // |
---|
| 169 | // End: // |
---|