[51b7345] | 1 | /* |
---|
| 2 | * This file is part of the Cforall project |
---|
| 3 | * |
---|
| 4 | * $Id: PtrsCastable.cc,v 1.5 2005/08/29 20:14:16 rcbilson Exp $ |
---|
| 5 | * |
---|
| 6 | */ |
---|
| 7 | |
---|
| 8 | #include "typeops.h" |
---|
| 9 | #include "SynTree/Type.h" |
---|
| 10 | #include "SynTree/Declaration.h" |
---|
| 11 | #include "SynTree/Visitor.h" |
---|
| 12 | #include "SymTab/Indexer.h" |
---|
| 13 | |
---|
| 14 | |
---|
| 15 | namespace ResolvExpr { |
---|
| 16 | |
---|
| 17 | class PtrsCastable : public Visitor |
---|
| 18 | { |
---|
| 19 | public: |
---|
| 20 | PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ); |
---|
| 21 | |
---|
| 22 | int get_result() const { return result; } |
---|
| 23 | |
---|
| 24 | virtual void visit(VoidType *voidType); |
---|
| 25 | virtual void visit(BasicType *basicType); |
---|
| 26 | virtual void visit(PointerType *pointerType); |
---|
| 27 | virtual void visit(ArrayType *arrayType); |
---|
| 28 | virtual void visit(FunctionType *functionType); |
---|
| 29 | virtual void visit(StructInstType *inst); |
---|
| 30 | virtual void visit(UnionInstType *inst); |
---|
| 31 | virtual void visit(EnumInstType *inst); |
---|
| 32 | virtual void visit(ContextInstType *inst); |
---|
| 33 | virtual void visit(TypeInstType *inst); |
---|
| 34 | virtual void visit(TupleType *tupleType); |
---|
| 35 | |
---|
| 36 | private: |
---|
| 37 | Type *dest; |
---|
| 38 | int result; |
---|
| 39 | const TypeEnvironment &env; |
---|
| 40 | const SymTab::Indexer &indexer; |
---|
| 41 | }; |
---|
| 42 | |
---|
| 43 | int |
---|
| 44 | objectCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
| 45 | { |
---|
| 46 | if( dynamic_cast< FunctionType* >( src ) ) { |
---|
| 47 | return -1; |
---|
| 48 | } else if( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( src ) ) { |
---|
| 49 | EqvClass eqvClass; |
---|
| 50 | if( NamedTypeDecl *ntDecl = indexer.lookupType( typeInst->get_name() ) ) { |
---|
| 51 | if( TypeDecl *tyDecl = dynamic_cast< TypeDecl* >( ntDecl ) ) { |
---|
| 52 | if( tyDecl->get_kind() == TypeDecl::Ftype ) { |
---|
| 53 | return -1; |
---|
| 54 | } |
---|
| 55 | } |
---|
| 56 | } else if( env.lookup( typeInst->get_name(), eqvClass ) ) { |
---|
| 57 | if( eqvClass.kind == TypeDecl::Ftype ) { |
---|
| 58 | return -1; |
---|
| 59 | } |
---|
| 60 | } |
---|
| 61 | } |
---|
| 62 | return 1; |
---|
| 63 | } |
---|
| 64 | |
---|
| 65 | int |
---|
| 66 | ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
| 67 | { |
---|
| 68 | if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 69 | EqvClass eqvClass; |
---|
| 70 | if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
| 71 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
| 72 | } |
---|
| 73 | } |
---|
| 74 | if( dynamic_cast< VoidType* >( dest ) ) { |
---|
| 75 | return objectCast( src, env, indexer ); |
---|
| 76 | } else { |
---|
| 77 | PtrsCastable ptrs( dest, env, indexer ); |
---|
| 78 | src->accept( ptrs ); |
---|
| 79 | return ptrs.get_result(); |
---|
| 80 | } |
---|
| 81 | } |
---|
| 82 | |
---|
| 83 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) |
---|
| 84 | : dest( dest ), result( 0 ), env( env ), indexer( indexer ) |
---|
| 85 | { |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | void |
---|
| 89 | PtrsCastable::visit(VoidType *voidType) |
---|
| 90 | { |
---|
| 91 | result = objectCast( dest, env, indexer ); |
---|
| 92 | } |
---|
| 93 | |
---|
| 94 | void |
---|
| 95 | PtrsCastable::visit(BasicType *basicType) |
---|
| 96 | { |
---|
| 97 | result = objectCast( dest, env, indexer ); |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | void |
---|
| 101 | PtrsCastable::visit(PointerType *pointerType) |
---|
| 102 | { |
---|
| 103 | result = objectCast( dest, env, indexer ); |
---|
| 104 | } |
---|
| 105 | |
---|
| 106 | void |
---|
| 107 | PtrsCastable::visit(ArrayType *arrayType) |
---|
| 108 | { |
---|
| 109 | result = objectCast( dest, env, indexer ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | void |
---|
| 113 | PtrsCastable::visit(FunctionType *functionType) |
---|
| 114 | { |
---|
| 115 | result = -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | void |
---|
| 119 | PtrsCastable::visit(StructInstType *inst) |
---|
| 120 | { |
---|
| 121 | result = objectCast( dest, env, indexer ); |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | void |
---|
| 125 | PtrsCastable::visit(UnionInstType *inst) |
---|
| 126 | { |
---|
| 127 | result = objectCast( dest, env, indexer ); |
---|
| 128 | } |
---|
| 129 | |
---|
| 130 | void |
---|
| 131 | PtrsCastable::visit(EnumInstType *inst) |
---|
| 132 | { |
---|
| 133 | if( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
| 134 | result = 1; |
---|
| 135 | } else if( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
| 136 | if( bt->get_kind() == BasicType::SignedInt ) { |
---|
| 137 | result = 0; |
---|
| 138 | } else { |
---|
| 139 | result = 1; |
---|
| 140 | } |
---|
| 141 | } else { |
---|
| 142 | result = objectCast( dest, env, indexer ); |
---|
| 143 | } |
---|
| 144 | } |
---|
| 145 | |
---|
| 146 | void |
---|
| 147 | PtrsCastable::visit(ContextInstType *inst) |
---|
| 148 | { |
---|
| 149 | // I definitely don't think we should be doing anything here |
---|
| 150 | } |
---|
| 151 | |
---|
| 152 | void |
---|
| 153 | PtrsCastable::visit(TypeInstType *inst) |
---|
| 154 | { |
---|
| 155 | result = objectCast( inst, env, indexer ) && objectCast( dest, env, indexer ) ? 1 : -1; |
---|
| 156 | } |
---|
| 157 | |
---|
| 158 | void |
---|
| 159 | PtrsCastable::visit(TupleType *tupleType) |
---|
| 160 | { |
---|
| 161 | result = objectCast( dest, env, indexer ); |
---|
| 162 | } |
---|
| 163 | |
---|
| 164 | } // namespace ResolvExpr |
---|