source: src/SymTab/TypeEquality.cc @ 711eee5

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 711eee5 was 540ddb7d, checked in by Aaron Moss <a3moss@…>, 8 years ago

Add qualifiers to VarArgsType?

  • Property mode set to 100644
File size: 6.0 KB
Line 
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
26namespace 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                virtual void visit( VarArgsType *varArgsType );
44
45                void handleQualifiers( Type * t );
46
47                Type * other;
48                bool vlaErr;
49        };
50
51        bool typeEquals( Type * t1, Type * t2, bool vlaErr ) {
52                TypeEquality teq( t2, vlaErr );
53                t1->accept( teq );
54                return teq.result;
55        }
56
57        void TypeEquality::handleQualifiers( Type * t ) {
58                result = result && t->get_qualifiers() == other->get_qualifiers();
59        }
60
61        void TypeEquality::visit( VoidType *voidType ) {
62                handleQualifiers( voidType );
63                if ( ! dynamic_cast< VoidType * >( other ) ) {
64                        result = false;
65                }
66        }
67
68        void TypeEquality::visit( BasicType *basicType ) {
69                handleQualifiers( basicType );
70                if ( BasicType * bt = dynamic_cast< BasicType * >( other ) ) {
71                        result = result && basicType->get_kind() == bt->get_kind(); 
72                } else {
73                        result = false;
74                }
75        }
76
77        void TypeEquality::visit( PointerType *pointerType ) {
78                handleQualifiers( pointerType );
79                if ( PointerType * pt = dynamic_cast< PointerType * >( other ) ) {
80                        other = pt->get_base();
81                        pointerType->get_base()->accept( *this );
82                } else {
83                        result = false;
84                }
85        }
86
87        void TypeEquality::visit( ArrayType *arrayType ) {
88                handleQualifiers( arrayType );
89
90                if ( ArrayType * at = dynamic_cast< ArrayType * >( other ) ) {
91                        // to be equal, array types must both be VLA or both not VLA
92                        // and must both have a dimension expression or not have a dimension
93                        result = result && arrayType->get_isVarLen() == at->get_isVarLen()
94                                && ((arrayType->get_dimension() != 0 && at->get_dimension() != 0)
95                                        || (arrayType->get_dimension() == 0 && at->get_dimension() == 0));
96
97                        if ( vlaErr ) {
98                                // useful for comparing typedef types - in this case, we
99                                // want types to appear distinct if either is a VLA type
100                                if ( arrayType->get_isVarLen() || at->get_isVarLen() ) {
101                                        result = false;
102                                }
103                        }
104
105                        if ( ! arrayType->get_isVarLen() && ! at->get_isVarLen() &&
106                                arrayType->get_dimension() != 0 && at->get_dimension() != 0 ) {
107                                ConstantExpr * ce1 = dynamic_cast< ConstantExpr * >( arrayType->get_dimension() );
108                                ConstantExpr * ce2 = dynamic_cast< ConstantExpr * >( at->get_dimension() );
109                                assert(ce1 && ce2);
110
111                                Constant * c1 = ce1->get_constant();
112                                Constant * c2 = ce2->get_constant();
113
114                                result = result && c1->get_value() == c2->get_value();
115                        }
116
117                        other = at->get_base();
118                        arrayType->get_base()->accept( *this );
119                } else {
120                        result = false;
121                }
122        }
123
124        void TypeEquality::visit( FunctionType *funcType ) {
125                handleQualifiers( funcType );
126
127                if ( FunctionType * ft = dynamic_cast< FunctionType * >( other ) ) {
128                        // function types must have the same number of return types
129                        // and parameters to be equivalent
130                        result = result && funcType->get_returnVals().size() == ft->get_returnVals().size()
131                                && funcType->get_parameters().size() == ft->get_parameters().size()
132                                && funcType->get_isVarArgs() == ft->get_isVarArgs();
133
134                        std::list< DeclarationWithType * >::iterator it1, it2;
135
136                        // return types must be equivalent
137                        it1 = funcType->get_returnVals().begin();
138                        it2 = ft->get_returnVals().begin();
139                        for ( ; it1 != funcType->get_returnVals().end(); ++it1, ++it2 ) {
140                                if ( ! result ) return;
141                                other = (*it2)->get_type();
142                                (*it1)->get_type()->accept( *this );
143                        }
144
145                        // parameter types must be equivalent
146                        it1 = funcType->get_parameters().begin(); 
147                        it2 = ft->get_parameters().begin();
148                        for ( ; it1 != funcType->get_parameters().end(); ++it1, ++it2 ) {
149                                if ( ! result ) return;
150                                other = (*it2)->get_type();
151                                (*it1)->get_type()->accept( *this );
152                        }
153                } else {
154                        result = false;
155                }
156        }
157
158        // aggregate types only need to have the same name
159        void TypeEquality::visit( StructInstType *structInst )  {
160                handleQualifiers( structInst );
161                if ( StructInstType * st = dynamic_cast< StructInstType * >( other ) ) {
162                        result = result && structInst->get_name() == st->get_name();
163                } else {
164                        result = false;
165                }
166        }
167
168        void TypeEquality::visit( UnionInstType *unionInst ) {
169                handleQualifiers( unionInst );
170                if ( UnionInstType * ut = dynamic_cast< UnionInstType * >( other ) ) {
171                        result = result && unionInst->get_name() == ut->get_name();
172                } else {
173                        result = false;
174                }
175        }
176
177        void TypeEquality::visit( EnumInstType *enumInst ) {
178                handleQualifiers( enumInst );
179                if ( EnumInstType * et = dynamic_cast< EnumInstType * >( other ) ) {
180                        result = result && enumInst->get_name() == et->get_name();
181                } else {
182                        result = false;
183                }
184        }
185
186        void TypeEquality::visit( TypeInstType *typeInst ) {
187                handleQualifiers( typeInst );
188                if ( TypeInstType * tt = dynamic_cast< TypeInstType * >( other ) ) {
189                        result = result && typeInst->get_name() == tt->get_name();
190                } else {
191                        result = false;
192                }
193        }
194
195        void TypeEquality::visit( VarArgsType *varArgsType ) {
196                handleQualifiers( varArgsType );
197                if ( ! dynamic_cast< VarArgsType * >( other ) ) {
198                        result = false;
199                }
200        }
201} // namespace SymTab
Note: See TracBrowser for help on using the repository browser.