| [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 | } | 
|---|
| [1d29d46] | 70 | int functionCast( Type *src, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { | 
|---|
|  | 71 | return -1 * objectCast( src, env, indexer );  // reverse the sense of objectCast | 
|---|
|  | 72 | } | 
|---|
| [a32b204] | 73 |  | 
|---|
|  | 74 | int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) { | 
|---|
|  | 75 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { | 
|---|
|  | 76 | EqvClass eqvClass; | 
|---|
|  | 77 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { | 
|---|
|  | 78 | return ptrsAssignable( src, eqvClass.type, env ); | 
|---|
|  | 79 | } // if | 
|---|
|  | 80 | } // if | 
|---|
|  | 81 | if ( dynamic_cast< VoidType* >( dest ) ) { | 
|---|
|  | 82 | return objectCast( src, env, indexer ); | 
|---|
|  | 83 | } else { | 
|---|
|  | 84 | PtrsCastable ptrs( dest, env, indexer ); | 
|---|
|  | 85 | src->accept( ptrs ); | 
|---|
|  | 86 | return ptrs.get_result(); | 
|---|
|  | 87 | } // if | 
|---|
|  | 88 | } | 
|---|
|  | 89 |  | 
|---|
|  | 90 | PtrsCastable::PtrsCastable( Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer ) | 
|---|
|  | 91 | : dest( dest ), result( 0 ), env( env ), indexer( indexer )     { | 
|---|
|  | 92 | } | 
|---|
|  | 93 |  | 
|---|
| [7e003011] | 94 | void PtrsCastable::visit( __attribute__((unused)) VoidType *voidType) { | 
|---|
| [a32b204] | 95 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 96 | } | 
|---|
|  | 97 |  | 
|---|
| [7e003011] | 98 | void PtrsCastable::visit( __attribute__((unused)) BasicType *basicType) { | 
|---|
| [a32b204] | 99 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 100 | } | 
|---|
|  | 101 |  | 
|---|
| [7e003011] | 102 | void PtrsCastable::visit( __attribute__((unused)) PointerType *pointerType) { | 
|---|
| [a32b204] | 103 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 104 | } | 
|---|
|  | 105 |  | 
|---|
| [7e003011] | 106 | void PtrsCastable::visit( __attribute__((unused)) ArrayType *arrayType) { | 
|---|
| [a32b204] | 107 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
| [7e003011] | 110 | void PtrsCastable::visit( __attribute__((unused)) FunctionType *functionType) { | 
|---|
| [1d29d46] | 111 | // result = -1; | 
|---|
|  | 112 | result = functionCast( dest, env, indexer ); | 
|---|
| [a32b204] | 113 | } | 
|---|
|  | 114 |  | 
|---|
| [7e003011] | 115 | void PtrsCastable::visit( __attribute__((unused)) StructInstType *inst) { | 
|---|
| [a32b204] | 116 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 117 | } | 
|---|
|  | 118 |  | 
|---|
| [7e003011] | 119 | void PtrsCastable::visit( __attribute__((unused)) UnionInstType *inst) { | 
|---|
| [a32b204] | 120 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 121 | } | 
|---|
|  | 122 |  | 
|---|
| [7e003011] | 123 | void PtrsCastable::visit( __attribute__((unused)) EnumInstType *inst) { | 
|---|
| [931dd12] | 124 | if ( dynamic_cast< EnumInstType* >( dest ) ) { | 
|---|
| [a32b204] | 125 | result = 1; | 
|---|
| [931dd12] | 126 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( dest ) ) { | 
|---|
| [a32b204] | 127 | if ( bt->get_kind() == BasicType::SignedInt ) { | 
|---|
|  | 128 | result = 0; | 
|---|
|  | 129 | } else { | 
|---|
|  | 130 | result = 1; | 
|---|
|  | 131 | } | 
|---|
|  | 132 | } else { | 
|---|
|  | 133 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 134 | } | 
|---|
|  | 135 | } | 
|---|
|  | 136 |  | 
|---|
| [af397ef8] | 137 | void PtrsCastable::visit( __attribute__((unused)) TraitInstType *inst ) {} | 
|---|
| [a32b204] | 138 |  | 
|---|
|  | 139 | void PtrsCastable::visit(TypeInstType *inst) { | 
|---|
| [1d29d46] | 140 | //result = objectCast( inst, env, indexer ) > 0 && objectCast( dest, env, indexer ) > 0 ? 1 : -1; | 
|---|
|  | 141 | result = objectCast( inst, env, indexer ) == objectCast( dest, env, indexer ) ? 1 : -1; | 
|---|
| [a32b204] | 142 | } | 
|---|
|  | 143 |  | 
|---|
| [7e003011] | 144 | void PtrsCastable::visit( __attribute__((unused)) TupleType *tupleType) { | 
|---|
| [a32b204] | 145 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 146 | } | 
|---|
| [44b7088] | 147 |  | 
|---|
| [7e003011] | 148 | void PtrsCastable::visit( __attribute__((unused)) VarArgsType *varArgsType) { | 
|---|
| [44b7088] | 149 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 150 | } | 
|---|
| [89e6ffc] | 151 |  | 
|---|
| [7e003011] | 152 | void PtrsCastable::visit( __attribute__((unused)) ZeroType *zeroType) { | 
|---|
| [89e6ffc] | 153 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 154 | } | 
|---|
|  | 155 |  | 
|---|
| [7e003011] | 156 | void PtrsCastable::visit( __attribute__((unused)) OneType *oneType) { | 
|---|
| [89e6ffc] | 157 | result = objectCast( dest, env, indexer ); | 
|---|
|  | 158 | } | 
|---|
| [51b73452] | 159 | } // namespace ResolvExpr | 
|---|
| [a32b204] | 160 |  | 
|---|
|  | 161 | // Local Variables: // | 
|---|
|  | 162 | // tab-width: 4 // | 
|---|
|  | 163 | // mode: c++ // | 
|---|
|  | 164 | // compile-command: "make install" // | 
|---|
|  | 165 | // End: // | 
|---|