[51b7345] | 1 | /* |
---|
| 2 | * This file is part of the Cforall project |
---|
| 3 | * |
---|
| 4 | * $Id: PtrsAssignable.cc,v 1.3 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 | |
---|
| 13 | |
---|
| 14 | namespace ResolvExpr { |
---|
| 15 | |
---|
| 16 | class PtrsAssignable : public Visitor |
---|
| 17 | { |
---|
| 18 | public: |
---|
| 19 | PtrsAssignable( Type *dest, const TypeEnvironment &env ); |
---|
| 20 | |
---|
| 21 | int get_result() const { return result; } |
---|
| 22 | |
---|
| 23 | virtual void visit(VoidType *voidType); |
---|
| 24 | virtual void visit(BasicType *basicType); |
---|
| 25 | virtual void visit(PointerType *pointerType); |
---|
| 26 | virtual void visit(ArrayType *arrayType); |
---|
| 27 | virtual void visit(FunctionType *functionType); |
---|
| 28 | virtual void visit(StructInstType *inst); |
---|
| 29 | virtual void visit(UnionInstType *inst); |
---|
| 30 | virtual void visit(EnumInstType *inst); |
---|
| 31 | virtual void visit(ContextInstType *inst); |
---|
| 32 | virtual void visit(TypeInstType *inst); |
---|
| 33 | virtual void visit(TupleType *tupleType); |
---|
| 34 | |
---|
| 35 | private: |
---|
| 36 | Type *dest; |
---|
| 37 | int result; |
---|
| 38 | const TypeEnvironment &env; |
---|
| 39 | }; |
---|
| 40 | |
---|
| 41 | int |
---|
| 42 | ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env ) |
---|
| 43 | { |
---|
| 44 | if( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) { |
---|
| 45 | EqvClass eqvClass; |
---|
| 46 | if( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) { |
---|
| 47 | return ptrsAssignable( src, eqvClass.type, env ); |
---|
| 48 | } |
---|
| 49 | } |
---|
| 50 | if( dynamic_cast< VoidType* >( dest ) ) { |
---|
| 51 | return 1; |
---|
| 52 | } else { |
---|
| 53 | PtrsAssignable ptrs( dest, env ); |
---|
| 54 | src->accept( ptrs ); |
---|
| 55 | return ptrs.get_result(); |
---|
| 56 | } |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | PtrsAssignable::PtrsAssignable( Type *dest, const TypeEnvironment &env ) |
---|
| 60 | : dest( dest ), result( 0 ), env( env ) |
---|
| 61 | { |
---|
| 62 | } |
---|
| 63 | |
---|
| 64 | void |
---|
| 65 | PtrsAssignable::visit(VoidType *voidType) |
---|
| 66 | { |
---|
| 67 | if( dynamic_cast< FunctionType* >( dest ) ) { |
---|
| 68 | result = 0; |
---|
| 69 | } else { |
---|
| 70 | result = -1; |
---|
| 71 | } |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | void |
---|
| 75 | PtrsAssignable::visit(BasicType *basicType) |
---|
| 76 | { |
---|
| 77 | } |
---|
| 78 | |
---|
| 79 | void |
---|
| 80 | PtrsAssignable::visit(PointerType *pointerType) |
---|
| 81 | { |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | void |
---|
| 85 | PtrsAssignable::visit(ArrayType *arrayType) |
---|
| 86 | { |
---|
| 87 | } |
---|
| 88 | |
---|
| 89 | void |
---|
| 90 | PtrsAssignable::visit(FunctionType *functionType) |
---|
| 91 | { |
---|
| 92 | result = -1; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | void |
---|
| 96 | PtrsAssignable::visit(StructInstType *inst) |
---|
| 97 | { |
---|
| 98 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
| 99 | } |
---|
| 100 | |
---|
| 101 | void |
---|
| 102 | PtrsAssignable::visit(UnionInstType *inst) |
---|
| 103 | { |
---|
| 104 | // I don't think we should be doing anything here, but I'm willing to admit that I might be wrong |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | void |
---|
| 108 | PtrsAssignable::visit(EnumInstType *inst) |
---|
| 109 | { |
---|
| 110 | if( dynamic_cast< EnumInstType* >( inst ) ) { |
---|
| 111 | result = 1; |
---|
| 112 | } else if( BasicType *bt = dynamic_cast< BasicType* >( inst ) ) { |
---|
| 113 | result = bt->get_kind() == BasicType::SignedInt; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | void |
---|
| 118 | PtrsAssignable::visit(ContextInstType *inst) |
---|
| 119 | { |
---|
| 120 | // I definitely don't think we should be doing anything here |
---|
| 121 | } |
---|
| 122 | |
---|
| 123 | void |
---|
| 124 | PtrsAssignable::visit(TypeInstType *inst) |
---|
| 125 | { |
---|
| 126 | EqvClass eqvClass; |
---|
| 127 | if( env.lookup( inst->get_name(), eqvClass ) ) { |
---|
| 128 | result = ptrsAssignable( eqvClass.type, dest, env ); |
---|
| 129 | } else { |
---|
| 130 | result = 0; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | void |
---|
| 135 | PtrsAssignable::visit(TupleType *tupleType) |
---|
| 136 | { |
---|
| 137 | /// // This code doesn't belong here, but it might be useful somewhere else |
---|
| 138 | /// if( TupleType *destAsTuple = dynamic_cast< TupleType* >( dest ) ) { |
---|
| 139 | /// int ret = 0; |
---|
| 140 | /// std::list< Type* >::const_iterator srcIt = tupleType->get_types().begin(); |
---|
| 141 | /// std::list< Type* >::const_iterator destIt = destAsTuple->get_types().begin(); |
---|
| 142 | /// while( srcIt != tupleType->get_types().end() && destIt != destAsTuple->get_types().end() ) { |
---|
| 143 | /// int assignResult = ptrsAssignable( *srcIt++, *destIt++ ); |
---|
| 144 | /// if( assignResult == 0 ) { |
---|
| 145 | /// result = assignResult; |
---|
| 146 | /// return; |
---|
| 147 | /// } else if ( assignResult < 0 ) { |
---|
| 148 | /// ret = -1; |
---|
| 149 | /// } else if ( ret > 0 ) { |
---|
| 150 | /// ret += assignResult; |
---|
| 151 | /// } |
---|
| 152 | /// } |
---|
| 153 | /// if( srcIt == tupleType->get_types().end() && destIt == destAsTuple->get_types().end() ) { |
---|
| 154 | /// result = ret; |
---|
| 155 | /// } else { |
---|
| 156 | /// result = 0; |
---|
| 157 | /// } |
---|
| 158 | /// } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | } // namespace ResolvExpr |
---|