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