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 | // CommonType.cc -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 06:59:27 2015 |
---|
11 | // Last Modified By : Peter A. Buhr |
---|
12 | // Last Modified On : Thu Mar 16 16:24:31 2017 |
---|
13 | // Update Count : 7 |
---|
14 | // |
---|
15 | |
---|
16 | #include "typeops.h" |
---|
17 | #include "SynTree/Type.h" |
---|
18 | #include "Unify.h" |
---|
19 | |
---|
20 | /// #define DEBUG |
---|
21 | |
---|
22 | namespace ResolvExpr { |
---|
23 | class CommonType : public Visitor { |
---|
24 | public: |
---|
25 | CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ); |
---|
26 | Type *get_result() const { return result; } |
---|
27 | private: |
---|
28 | virtual void visit( VoidType *voidType ); |
---|
29 | virtual void visit( BasicType *basicType ); |
---|
30 | virtual void visit( PointerType *pointerType ); |
---|
31 | virtual void visit( ArrayType *arrayType ); |
---|
32 | virtual void visit( ReferenceType *refType ); |
---|
33 | virtual void visit( FunctionType *functionType ); |
---|
34 | virtual void visit( StructInstType *aggregateUseType ); |
---|
35 | virtual void visit( UnionInstType *aggregateUseType ); |
---|
36 | virtual void visit( EnumInstType *aggregateUseType ); |
---|
37 | virtual void visit( TraitInstType *aggregateUseType ); |
---|
38 | virtual void visit( TypeInstType *aggregateUseType ); |
---|
39 | virtual void visit( TupleType *tupleType ); |
---|
40 | virtual void visit( VarArgsType *varArgsType ); |
---|
41 | virtual void visit( ZeroType *zeroType ); |
---|
42 | virtual void visit( OneType *oneType ); |
---|
43 | |
---|
44 | template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ); |
---|
45 | template< typename RefType > void handleRefType( RefType *inst, Type *other ); |
---|
46 | |
---|
47 | Type *result; |
---|
48 | Type *type2; // inherited |
---|
49 | bool widenFirst, widenSecond; |
---|
50 | const SymTab::Indexer &indexer; |
---|
51 | TypeEnvironment &env; |
---|
52 | const OpenVarSet &openVars; |
---|
53 | }; |
---|
54 | |
---|
55 | Type * handleReference( ReferenceType * refType, Type * other, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) { |
---|
56 | Type * result = nullptr, * common = nullptr; |
---|
57 | AssertionSet have, need; |
---|
58 | OpenVarSet newOpen( openVars ); |
---|
59 | // need unify to bind type variables |
---|
60 | if ( unify( refType->get_base(), other, env, have, need, newOpen, indexer, common ) ) { |
---|
61 | // std::cerr << "unify success" << std::endl; |
---|
62 | if ( widenSecond ) { |
---|
63 | if ( widenFirst || other->get_qualifiers() <= refType->get_qualifiers() ) { |
---|
64 | result = new ReferenceType( refType->get_qualifiers(), common ); // refType->clone(); |
---|
65 | result->get_qualifiers() |= other->get_qualifiers(); |
---|
66 | } |
---|
67 | } else if ( widenFirst ) { |
---|
68 | if ( widenSecond || refType->get_qualifiers() <= other->get_qualifiers() ) { |
---|
69 | result = common; |
---|
70 | result->get_qualifiers() |= refType->get_qualifiers(); |
---|
71 | } |
---|
72 | } |
---|
73 | } else { |
---|
74 | // std::cerr << "exact unify failed: " << refType << " " << other << std::endl; |
---|
75 | } |
---|
76 | // std::cerr << "common type of reference [" << refType << "] and non-reference [" << other << "] is [" << result << "]" << std::endl; |
---|
77 | return result; |
---|
78 | } |
---|
79 | |
---|
80 | Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) { |
---|
81 | CommonType visitor( type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
82 | |
---|
83 | int depth1 = type1->referenceDepth(); |
---|
84 | int depth2 = type2->referenceDepth(); |
---|
85 | if ( depth1 > 0 || depth2 > 0 ) { |
---|
86 | int diff = depth1-depth2; |
---|
87 | // TODO: should it be possible for commonType to generate complicated conversions? I would argue no, only conversions that involve types of the same reference level or a difference of 1 should be allowed. |
---|
88 | if ( diff > 1 || diff < -1 ) return nullptr; |
---|
89 | |
---|
90 | // special case where one type has a reference depth of 1 larger than the other |
---|
91 | if ( diff > 0 ) { |
---|
92 | return handleReference( safe_dynamic_cast<ReferenceType *>( type1 ), type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
93 | } else if ( diff < 0 ) { |
---|
94 | return handleReference( safe_dynamic_cast<ReferenceType *>( type2 ), type1, widenSecond, widenFirst, indexer, env, openVars ); |
---|
95 | } |
---|
96 | // otherwise, both are reference types of the same depth and this is handled by the CommonType visitor. |
---|
97 | } |
---|
98 | |
---|
99 | type1->accept( visitor ); |
---|
100 | Type *result = visitor.get_result(); |
---|
101 | if ( ! result ) { |
---|
102 | // this appears to be handling for opaque type declarations |
---|
103 | if ( widenSecond ) { |
---|
104 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) { |
---|
105 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) { |
---|
106 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
107 | if ( type->get_base() ) { |
---|
108 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
109 | AssertionSet have, need; |
---|
110 | OpenVarSet newOpen( openVars ); |
---|
111 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
112 | type->get_base()->get_qualifiers() = tq1; |
---|
113 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
114 | result = type1->clone(); |
---|
115 | result->get_qualifiers() = tq1 | tq2; |
---|
116 | } // if |
---|
117 | type1->get_qualifiers() = tq1; |
---|
118 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
119 | } // if |
---|
120 | } // if |
---|
121 | } // if |
---|
122 | } // if |
---|
123 | } // if |
---|
124 | #ifdef DEBUG |
---|
125 | std::cout << "============= commonType" << std::endl << "type1 is "; |
---|
126 | type1->print( std::cout ); |
---|
127 | std::cout << " type2 is "; |
---|
128 | type2->print( std::cout ); |
---|
129 | if ( result ) { |
---|
130 | std::cout << " common type is "; |
---|
131 | result->print( std::cout ); |
---|
132 | } else { |
---|
133 | std::cout << " no common type"; |
---|
134 | } // if |
---|
135 | std::cout << std::endl; |
---|
136 | #endif |
---|
137 | return result; |
---|
138 | } |
---|
139 | |
---|
140 | static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = |
---|
141 | { |
---|
142 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary */ |
---|
143 | /* Bool */ { BasicType::Bool, BasicType::Char, BasicType::SignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
144 | /* Char */ { BasicType::Char, BasicType::Char, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
145 | /* SignedChar */ { BasicType::SignedChar, BasicType::UnsignedChar, BasicType::SignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
146 | /* UnsignedChar */ { BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::UnsignedChar, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
147 | /* ShortSignedInt */ { BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortSignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
148 | /* ShortUnsignedInt */ { BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::ShortUnsignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
149 | /* SignedInt */ { BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::SignedInt, BasicType::UnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
150 | /* UnsignedInt */ { BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::UnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
151 | /* LongSignedInt */ { BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongSignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
152 | /* LongUnsignedInt */ { BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongUnsignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
153 | /* LongLongSignedInt */ { BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongSignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
154 | /* LongLongUnsignedInt */ { BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::LongLongUnsignedInt, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
155 | /* Float */ { BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
156 | /* Double */ { BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::Double, BasicType::LongDouble, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
157 | /* LongDouble */ { BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDouble, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex }, |
---|
158 | /* FloatComplex */ { BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
159 | /* DoubleComplex */ { BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex }, |
---|
160 | /* LongDoubleComplex */ { BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex }, |
---|
161 | /* FloatImaginary */ { BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary }, |
---|
162 | /* DoubleImaginary */ { BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::DoubleImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary }, |
---|
163 | /* LongDoubleImaginary */ { BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary } |
---|
164 | }; |
---|
165 | |
---|
166 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) |
---|
167 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { |
---|
168 | } |
---|
169 | |
---|
170 | void CommonType::visit( __attribute((unused)) VoidType *voidType ) {} |
---|
171 | |
---|
172 | void CommonType::visit( BasicType *basicType ) { |
---|
173 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
174 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; |
---|
175 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { |
---|
176 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); |
---|
177 | } // if |
---|
178 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
179 | // use signed int in lieu of the enum/zero/one type |
---|
180 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; |
---|
181 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { |
---|
182 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); |
---|
183 | } // if |
---|
184 | } // if |
---|
185 | } |
---|
186 | |
---|
187 | template< typename Pointer > |
---|
188 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { |
---|
189 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { |
---|
190 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); |
---|
191 | if ( entry != openVars.end() ) { |
---|
192 | AssertionSet need, have; |
---|
193 | WidenMode widen( widenFirst, widenSecond ); |
---|
194 | if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return; |
---|
195 | } |
---|
196 | } |
---|
197 | result = voidPointer->clone(); |
---|
198 | result->get_qualifiers() |= otherPointer->get_qualifiers(); |
---|
199 | } |
---|
200 | |
---|
201 | void CommonType::visit( PointerType *pointerType ) { |
---|
202 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
203 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { |
---|
204 | getCommonWithVoidPointer( otherPointer, pointerType ); |
---|
205 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { |
---|
206 | getCommonWithVoidPointer( pointerType, otherPointer ); |
---|
207 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) |
---|
208 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
209 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); |
---|
210 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
211 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
212 | AssertionSet have, need; |
---|
213 | OpenVarSet newOpen( openVars ); |
---|
214 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
215 | if ( tq1 < tq2 ) { |
---|
216 | result = pointerType->clone(); |
---|
217 | } else { |
---|
218 | result = otherPointer->clone(); |
---|
219 | } // if |
---|
220 | result->get_qualifiers() = tq1 | tq2; |
---|
221 | } else { |
---|
222 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
223 | } // if |
---|
224 | pointerType->get_base()->get_qualifiers() = tq1; |
---|
225 | otherPointer->get_base()->get_qualifiers() = tq2; |
---|
226 | } // if |
---|
227 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
228 | result = pointerType->clone(); |
---|
229 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
230 | } // if |
---|
231 | } |
---|
232 | |
---|
233 | void CommonType::visit( __attribute((unused)) ArrayType *arrayType ) {} |
---|
234 | |
---|
235 | void CommonType::visit( ReferenceType *refType ) { |
---|
236 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { |
---|
237 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { |
---|
238 | getCommonWithVoidPointer( otherRef, refType ); |
---|
239 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { |
---|
240 | getCommonWithVoidPointer( refType, otherRef ); |
---|
241 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) |
---|
242 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
243 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); |
---|
244 | refType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
245 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
246 | AssertionSet have, need; |
---|
247 | OpenVarSet newOpen( openVars ); |
---|
248 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
249 | if ( tq1 < tq2 ) { |
---|
250 | result = refType->clone(); |
---|
251 | } else { |
---|
252 | result = otherRef->clone(); |
---|
253 | } // if |
---|
254 | result->get_qualifiers() = tq1 | tq2; |
---|
255 | } else { |
---|
256 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
257 | } // if |
---|
258 | refType->get_base()->get_qualifiers() = tq1; |
---|
259 | otherRef->get_base()->get_qualifiers() = tq2; |
---|
260 | } // if |
---|
261 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
262 | result = refType->clone(); |
---|
263 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
264 | } // if |
---|
265 | } |
---|
266 | |
---|
267 | void CommonType::visit( __attribute((unused)) FunctionType *functionType ) {} |
---|
268 | void CommonType::visit( __attribute((unused)) StructInstType *aggregateUseType ) {} |
---|
269 | void CommonType::visit( __attribute((unused)) UnionInstType *aggregateUseType ) {} |
---|
270 | |
---|
271 | void CommonType::visit( EnumInstType *enumInstType ) { |
---|
272 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
273 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType |
---|
274 | ValueGuard< Type * > temp( type2 ); |
---|
275 | type2 = enumInstType; |
---|
276 | temp.old->accept( *this ); |
---|
277 | } // if |
---|
278 | } |
---|
279 | |
---|
280 | void CommonType::visit( __attribute((unused)) TraitInstType *aggregateUseType ) { |
---|
281 | } |
---|
282 | |
---|
283 | void CommonType::visit( TypeInstType *inst ) { |
---|
284 | if ( widenFirst ) { |
---|
285 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); |
---|
286 | if ( nt ) { |
---|
287 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
288 | if ( type->get_base() ) { |
---|
289 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
290 | AssertionSet have, need; |
---|
291 | OpenVarSet newOpen( openVars ); |
---|
292 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
293 | type->get_base()->get_qualifiers() = tq1; |
---|
294 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { |
---|
295 | result = type2->clone(); |
---|
296 | result->get_qualifiers() = tq1 | tq2; |
---|
297 | } // if |
---|
298 | type2->get_qualifiers() = tq2; |
---|
299 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
300 | } // if |
---|
301 | } // if |
---|
302 | } // if |
---|
303 | } |
---|
304 | |
---|
305 | void CommonType::visit( __attribute((unused)) TupleType *tupleType ) {} |
---|
306 | void CommonType::visit( __attribute((unused)) VarArgsType *varArgsType ) {} |
---|
307 | |
---|
308 | void CommonType::visit( ZeroType *zeroType ) { |
---|
309 | if ( widenFirst ) { |
---|
310 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
311 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
312 | result = type2->clone(); |
---|
313 | result->get_qualifiers() |= zeroType->get_qualifiers(); |
---|
314 | } |
---|
315 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { |
---|
316 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); |
---|
317 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
318 | } |
---|
319 | } |
---|
320 | } |
---|
321 | |
---|
322 | void CommonType::visit( OneType *oneType ) { |
---|
323 | if ( widenFirst ) { |
---|
324 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
325 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
326 | result = type2->clone(); |
---|
327 | result->get_qualifiers() |= oneType->get_qualifiers(); |
---|
328 | } |
---|
329 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
330 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); |
---|
331 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | } // namespace ResolvExpr |
---|
336 | |
---|
337 | // Local Variables: // |
---|
338 | // tab-width: 4 // |
---|
339 | // mode: c++ // |
---|
340 | // compile-command: "make install" // |
---|
341 | // End: // |
---|