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