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