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