[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 | // |
---|
| 7 | // PtrsAssignable.cc -- |
---|
| 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 11:44:11 2015 |
---|
[4040425] | 11 | // Last Modified By : Peter A. Buhr |
---|
| 12 | // Last Modified On : Wed Mar 2 17:36:05 2016 |
---|
| 13 | // Update Count : 8 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[51b7345] | 16 | #include "typeops.h" |
---|
| 17 | #include "SynTree/Type.h" |
---|
| 18 | #include "SynTree/Declaration.h" |
---|
| 19 | #include "SynTree/Visitor.h" |
---|
| 20 | |
---|
| 21 | |
---|
| 22 | namespace ResolvExpr { |
---|
[a32b204] | 23 | class PtrsAssignable : public Visitor { |
---|
| 24 | public: |
---|
| 25 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
| 26 | |
---|
| 27 | int get_result() const { return result; } |
---|
| 28 | |
---|
| 29 | virtual void visit( VoidType *voidType ); |
---|
| 30 | virtual void visit( BasicType *basicType ); |
---|
| 31 | virtual void visit( PointerType *pointerType ); |
---|
| 32 | virtual void visit( ArrayType *arrayType ); |
---|
| 33 | virtual void visit( FunctionType *functionType ); |
---|
| 34 | virtual void visit( StructInstType *inst ); |
---|
| 35 | virtual void visit( UnionInstType *inst ); |
---|
| 36 | virtual void visit( EnumInstType *inst ); |
---|
[4040425] | 37 | virtual void visit( TraitInstType *inst ); |
---|
[a32b204] | 38 | virtual void visit( TypeInstType *inst ); |
---|
| 39 | virtual void visit( TupleType *tupleType ); |
---|
[44b7088] | 40 | virtual void visit( VarArgsType *varArgsType ); |
---|
[a32b204] | 41 | private: |
---|
| 42 | Type *dest; |
---|
| 43 | int result; |
---|
| 44 | const TypeEnvironment &env; |
---|
| 45 | }; |
---|
| 46 | |
---|
| 47 | int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) { |
---|
| 48 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 49 | EqvClass eqvClass; |
---|
| 50 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
| 51 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
| 52 | } // if |
---|
| 53 | } // if |
---|
| 54 | if ( dynamic_cast< VoidType* >( dest ) ) { |
---|
| 55 | return 1; |
---|
| 56 | } else { |
---|
| 57 | PtrsAssignable ptrs( dest, env ); |
---|
| 58 | src->accept( ptrs ); |
---|
| 59 | return ptrs.get_result(); |
---|
| 60 | } // if |
---|
[c11e31c] | 61 | } |
---|
[a32b204] | 62 | |
---|
| 63 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) : dest( dest ), result( 0 ), env( env ) { |
---|
[c11e31c] | 64 | } |
---|
| 65 | |
---|
[a32b204] | 66 | void PtrsAssignable::visit( VoidType *voidType ) { |
---|
| 67 | if ( dynamic_cast< FunctionType* >( dest ) ) { |
---|
| 68 | result = 0; |
---|
| 69 | } else { |
---|
| 70 | result = -1; |
---|
| 71 | } // if |
---|
| 72 | } |
---|
[c11e31c] | 73 | |
---|
[a32b204] | 74 | void PtrsAssignable::visit( BasicType *basicType ) { |
---|
[c11e31c] | 75 | } |
---|
| 76 | |
---|
[a32b204] | 77 | void PtrsAssignable::visit( PointerType *pointerType ) { |
---|
| 78 | } |
---|
[c11e31c] | 79 | |
---|
[a32b204] | 80 | void PtrsAssignable::visit( ArrayType *arrayType ) { |
---|
| 81 | } |
---|
[c11e31c] | 82 | |
---|
[a32b204] | 83 | void PtrsAssignable::visit( FunctionType *functionType ) { |
---|
| 84 | result = -1; |
---|
| 85 | } |
---|
[c11e31c] | 86 | |
---|
[a32b204] | 87 | void PtrsAssignable::visit( StructInstType *inst ) { |
---|
| 88 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
| 89 | } |
---|
[c11e31c] | 90 | |
---|
[a32b204] | 91 | void PtrsAssignable::visit( UnionInstType *inst ) { |
---|
| 92 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
| 93 | } |
---|
[c11e31c] | 94 | |
---|
[a32b204] | 95 | void PtrsAssignable::visit( EnumInstType *inst ) { |
---|
| 96 | if ( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
| 97 | result = 1; |
---|
| 98 | } else if ( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
| 99 | result = bt->get_kind() == BasicType::SignedInt; |
---|
| 100 | } |
---|
| 101 | } |
---|
[c11e31c] | 102 | |
---|
[4040425] | 103 | void PtrsAssignable::visit( TraitInstType *inst ) { |
---|
[a32b204] | 104 | // I definitely don't think we should be doing anything here |
---|
[c11e31c] | 105 | } |
---|
[a32b204] | 106 | |
---|
| 107 | void PtrsAssignable::visit( TypeInstType *inst ) { |
---|
| 108 | EqvClass eqvClass; |
---|
[1521de20] | 109 | if ( env.lookup( inst->get_name(), eqvClass ) && eqvClass.type ) { |
---|
[a32b204] | 110 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
| 111 | } else { |
---|
| 112 | result = 0; |
---|
| 113 | } // if |
---|
[c11e31c] | 114 | } |
---|
| 115 | |
---|
[a32b204] | 116 | void PtrsAssignable::visit( TupleType *tupleType ) { |
---|
[51b7345] | 117 | /// // This code doesn't belong here, but it might be useful somewhere else |
---|
[c11e31c] | 118 | /// if ( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
[51b7345] | 119 | /// int ret = 0; |
---|
| 120 | /// std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
| 121 | /// std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
[a32b204] | 122 | /// while ( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
[51b7345] | 123 | /// int assignResult = ptrsAssignable( *srcIt++, *destIt++ ); |
---|
[c11e31c] | 124 | /// if ( assignResult == 0 ) { |
---|
[51b7345] | 125 | /// result = assignResult; |
---|
| 126 | /// return; |
---|
| 127 | /// } else if ( assignResult < 0 ) { |
---|
| 128 | /// ret = -1; |
---|
| 129 | /// } else if ( ret > 0 ) { |
---|
| 130 | /// ret += assignResult; |
---|
| 131 | /// } |
---|
| 132 | /// } |
---|
[c11e31c] | 133 | /// if ( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) { |
---|
[51b7345] | 134 | /// result = ret; |
---|
| 135 | /// } else { |
---|
| 136 | /// result = 0; |
---|
| 137 | /// } |
---|
| 138 | /// } |
---|
[a32b204] | 139 | } |
---|
[44b7088] | 140 | |
---|
| 141 | void PtrsAssignable::visit( VarArgsType *varArgsType ) { |
---|
| 142 | } |
---|
[51b7345] | 143 | } // namespace ResolvExpr |
---|
[a32b204] | 144 | |
---|
| 145 | // Local Variables: // |
---|
| 146 | // tab-width: 4 // |
---|
| 147 | // mode: c++ // |
---|
| 148 | // compile-command: "make install" // |
---|
| 149 | // End: // |
---|