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