| 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 | // TypeEquality.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rob Schluntz
|
|---|
| 10 | // Created On : Tue Jul 07 16:28:29 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Mon Jul 20 14:16:11 2015
|
|---|
| 13 | // Update Count : 37
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <list>
|
|---|
| 17 | #include <iterator>
|
|---|
| 18 | #include "Validate.h"
|
|---|
| 19 | #include "SynTree/Visitor.h"
|
|---|
| 20 | #include "SynTree/Type.h"
|
|---|
| 21 | #include "SynTree/Statement.h"
|
|---|
| 22 | #include "SynTree/TypeSubstitution.h"
|
|---|
| 23 | #include "Indexer.h"
|
|---|
| 24 | #include "TypeEquality.h"
|
|---|
| 25 |
|
|---|
| 26 | namespace SymTab {
|
|---|
| 27 | class TypeEquality : public Visitor {
|
|---|
| 28 | public:
|
|---|
| 29 | TypeEquality( Type * other, bool vlaErr ) : result( true ), other( other ),
|
|---|
| 30 | vlaErr( vlaErr ) {}
|
|---|
| 31 | bool result;
|
|---|
| 32 |
|
|---|
| 33 | private:
|
|---|
| 34 | virtual void visit( FunctionType *funcType );
|
|---|
| 35 | virtual void visit( VoidType *voidType );
|
|---|
| 36 | virtual void visit( BasicType *basicType );
|
|---|
| 37 | virtual void visit( PointerType *pointerType );
|
|---|
| 38 | virtual void visit( ArrayType *arrayType );
|
|---|
| 39 | virtual void visit( StructInstType *structInst );
|
|---|
| 40 | virtual void visit( UnionInstType *unionInst );
|
|---|
| 41 | virtual void visit( EnumInstType *enumInst );
|
|---|
| 42 | virtual void visit( TypeInstType *typeInst );
|
|---|
| 43 |
|
|---|
| 44 | void handleQualifiers( Type * t );
|
|---|
| 45 |
|
|---|
| 46 | Type * other;
|
|---|
| 47 | bool vlaErr;
|
|---|
| 48 | };
|
|---|
| 49 |
|
|---|
| 50 | bool typeEquals( Type * t1, Type * t2, bool vlaErr ) {
|
|---|
| 51 | TypeEquality teq( t2, vlaErr );
|
|---|
| 52 | t1->accept( teq );
|
|---|
| 53 | return teq.result;
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | void TypeEquality::handleQualifiers( Type * t ) {
|
|---|
| 57 | result = result && t->get_qualifiers() == other->get_qualifiers();
|
|---|
| 58 | }
|
|---|
| 59 |
|
|---|
| 60 | void TypeEquality::visit( VoidType *voidType ) {
|
|---|
| 61 | handleQualifiers( voidType );
|
|---|
| 62 | if ( ! dynamic_cast< VoidType * >( other ) ) {
|
|---|
| 63 | result = false;
|
|---|
| 64 | }
|
|---|
| 65 | }
|
|---|
| 66 |
|
|---|
| 67 | void TypeEquality::visit( BasicType *basicType ) {
|
|---|
| 68 | handleQualifiers( basicType );
|
|---|
| 69 | if ( BasicType * bt = dynamic_cast< BasicType * >( other ) ) {
|
|---|
| 70 | result = result && basicType->get_kind() == bt->get_kind();
|
|---|
| 71 | } else {
|
|---|
| 72 | result = false;
|
|---|
| 73 | }
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | void TypeEquality::visit( PointerType *pointerType ) {
|
|---|
| 77 | handleQualifiers( pointerType );
|
|---|
| 78 | if ( PointerType * pt = dynamic_cast< PointerType * >( other ) ) {
|
|---|
| 79 | other = pt->get_base();
|
|---|
| 80 | pointerType->get_base()->accept( *this );
|
|---|
| 81 | } else {
|
|---|
| 82 | result = false;
|
|---|
| 83 | }
|
|---|
| 84 | }
|
|---|
| 85 |
|
|---|
| 86 | void TypeEquality::visit( ArrayType *arrayType ) {
|
|---|
| 87 | handleQualifiers( arrayType );
|
|---|
| 88 |
|
|---|
| 89 | if ( ArrayType * at = dynamic_cast< ArrayType * >( other ) ) {
|
|---|
| 90 | // to be equal, array types must both be VLA or both not VLA
|
|---|
| 91 | // and must both have a dimension expression or not have a dimension
|
|---|
| 92 | result = result && arrayType->get_isVarLen() == at->get_isVarLen()
|
|---|
| 93 | && ((arrayType->get_dimension() != 0 && at->get_dimension() != 0)
|
|---|
| 94 | || (arrayType->get_dimension() == 0 && at->get_dimension() == 0));
|
|---|
| 95 |
|
|---|
| 96 | if ( vlaErr ) {
|
|---|
| 97 | // useful for comparing typedef types - in this case, we
|
|---|
| 98 | // want types to appear distinct if either is a VLA type
|
|---|
| 99 | if ( arrayType->get_isVarLen() || at->get_isVarLen() ) {
|
|---|
| 100 | result = false;
|
|---|
| 101 | }
|
|---|
| 102 | }
|
|---|
| 103 |
|
|---|
| 104 | if ( ! arrayType->get_isVarLen() && ! at->get_isVarLen() &&
|
|---|
| 105 | arrayType->get_dimension() != 0 && at->get_dimension() != 0 ) {
|
|---|
| 106 | ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
|
|---|
| 107 | ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( at->get_dimension() );
|
|---|
| 108 | assert(ce1 && ce2);
|
|---|
| 109 |
|
|---|
| 110 | Constant * c1 = ce1->get_constant();
|
|---|
| 111 | Constant * c2 = ce2->get_constant();
|
|---|
| 112 |
|
|---|
| 113 | result = result && c1->get_value() == c2->get_value();
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | other = at->get_base();
|
|---|
| 117 | arrayType->get_base()->accept( *this );
|
|---|
| 118 | } else {
|
|---|
| 119 | result = false;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | void TypeEquality::visit( FunctionType *funcType ) {
|
|---|
| 124 | handleQualifiers( funcType );
|
|---|
| 125 |
|
|---|
| 126 | if ( FunctionType * ft = dynamic_cast< FunctionType * >( other ) ) {
|
|---|
| 127 | // function types must have the same number of return types
|
|---|
| 128 | // and parameters to be equivalent
|
|---|
| 129 | result = result && funcType->get_returnVals().size() == ft->get_returnVals().size()
|
|---|
| 130 | && funcType->get_parameters().size() == ft->get_parameters().size()
|
|---|
| 131 | && funcType->get_isVarArgs() == ft->get_isVarArgs();
|
|---|
| 132 |
|
|---|
| 133 | std::list< DeclarationWithType * >::iterator it1, it2;
|
|---|
| 134 |
|
|---|
| 135 | // return types must be equivalent
|
|---|
| 136 | it1 = funcType->get_returnVals().begin();
|
|---|
| 137 | it2 = ft->get_returnVals().begin();
|
|---|
| 138 | for ( ; it1 != funcType->get_returnVals().end(); ++it1, ++it2 ) {
|
|---|
| 139 | if ( ! result ) return;
|
|---|
| 140 | other = (*it2)->get_type();
|
|---|
| 141 | (*it1)->get_type()->accept( *this );
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | // parameter types must be equivalent
|
|---|
| 145 | it1 = funcType->get_parameters().begin();
|
|---|
| 146 | it2 = ft->get_parameters().begin();
|
|---|
| 147 | for ( ; it1 != funcType->get_parameters().end(); ++it1, ++it2 ) {
|
|---|
| 148 | if ( ! result ) return;
|
|---|
| 149 | other = (*it2)->get_type();
|
|---|
| 150 | (*it1)->get_type()->accept( *this );
|
|---|
| 151 | }
|
|---|
| 152 | } else {
|
|---|
| 153 | result = false;
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 |
|
|---|
| 157 | // aggregate types only need to have the same name
|
|---|
| 158 | void TypeEquality::visit( StructInstType *structInst ) {
|
|---|
| 159 | handleQualifiers( structInst );
|
|---|
| 160 | if ( StructInstType * st = dynamic_cast< StructInstType * >( other ) ) {
|
|---|
| 161 | result = result && structInst->get_name() == st->get_name();
|
|---|
| 162 | } else {
|
|---|
| 163 | result = false;
|
|---|
| 164 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | void TypeEquality::visit( UnionInstType *unionInst ) {
|
|---|
| 168 | handleQualifiers( unionInst );
|
|---|
| 169 | if ( UnionInstType * ut = dynamic_cast< UnionInstType * >( other ) ) {
|
|---|
| 170 | result = result && unionInst->get_name() == ut->get_name();
|
|---|
| 171 | } else {
|
|---|
| 172 | result = false;
|
|---|
| 173 | }
|
|---|
| 174 | }
|
|---|
| 175 |
|
|---|
| 176 | void TypeEquality::visit( EnumInstType *enumInst ) {
|
|---|
| 177 | handleQualifiers( enumInst );
|
|---|
| 178 | if ( EnumInstType * et = dynamic_cast< EnumInstType * >( other ) ) {
|
|---|
| 179 | result = result && enumInst->get_name() == et->get_name();
|
|---|
| 180 | } else {
|
|---|
| 181 | result = false;
|
|---|
| 182 | }
|
|---|
| 183 | }
|
|---|
| 184 |
|
|---|
| 185 | void TypeEquality::visit( TypeInstType *typeInst ) {
|
|---|
| 186 | handleQualifiers( typeInst );
|
|---|
| 187 | if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( other ) ) {
|
|---|
| 188 | result = result && typeInst->get_name() == tt->get_name();
|
|---|
| 189 | } else {
|
|---|
| 190 | result = false;
|
|---|
| 191 | }
|
|---|
| 192 | }
|
|---|
| 193 | } // namespace SymTab
|
|---|