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 | ReferenceType * refType1 = dynamic_cast< ReferenceType * >( type1 ); |
---|
84 | ReferenceType * refType2 = dynamic_cast< ReferenceType * >( type2 ); |
---|
85 | if ( (refType1 || refType2) && (! refType1 || ! refType2) ) { |
---|
86 | // handle the case where exactly one of the types is a reference type specially |
---|
87 | if ( refType1 ) { |
---|
88 | return handleReference( refType1, type2, widenFirst, widenSecond, indexer, env, openVars ); |
---|
89 | } else if ( refType2 ) { |
---|
90 | return handleReference( refType2, type1, widenSecond, widenFirst, indexer, env, openVars ); |
---|
91 | } |
---|
92 | } |
---|
93 | |
---|
94 | type1->accept( visitor ); |
---|
95 | Type *result = visitor.get_result(); |
---|
96 | if ( ! result ) { |
---|
97 | // this appears to be handling for opaque type declarations |
---|
98 | if ( widenSecond ) { |
---|
99 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) { |
---|
100 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) { |
---|
101 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
102 | if ( type->get_base() ) { |
---|
103 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
104 | AssertionSet have, need; |
---|
105 | OpenVarSet newOpen( openVars ); |
---|
106 | type1->get_qualifiers() = Type::Qualifiers(); |
---|
107 | type->get_base()->get_qualifiers() = tq1; |
---|
108 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
109 | result = type1->clone(); |
---|
110 | result->get_qualifiers() = tq1 | tq2; |
---|
111 | } // if |
---|
112 | type1->get_qualifiers() = tq1; |
---|
113 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
114 | } // if |
---|
115 | } // if |
---|
116 | } // if |
---|
117 | } // if |
---|
118 | } // if |
---|
119 | #ifdef DEBUG |
---|
120 | std::cout << "============= commonType" << std::endl << "type1 is "; |
---|
121 | type1->print( std::cout ); |
---|
122 | std::cout << " type2 is "; |
---|
123 | type2->print( std::cout ); |
---|
124 | if ( result ) { |
---|
125 | std::cout << " common type is "; |
---|
126 | result->print( std::cout ); |
---|
127 | } else { |
---|
128 | std::cout << " no common type"; |
---|
129 | } // if |
---|
130 | std::cout << std::endl; |
---|
131 | #endif |
---|
132 | return result; |
---|
133 | } |
---|
134 | |
---|
135 | static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] = |
---|
136 | { |
---|
137 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary */ |
---|
138 | /* 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 }, |
---|
139 | /* 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 }, |
---|
140 | /* 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 }, |
---|
141 | /* 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 }, |
---|
142 | /* 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 }, |
---|
143 | /* 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 }, |
---|
144 | /* 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 }, |
---|
145 | /* 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 }, |
---|
146 | /* 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 }, |
---|
147 | /* 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 }, |
---|
148 | /* 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 }, |
---|
149 | /* 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 }, |
---|
150 | /* 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 }, |
---|
151 | /* 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 }, |
---|
152 | /* 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 }, |
---|
153 | /* 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 }, |
---|
154 | /* 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 }, |
---|
155 | /* 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 }, |
---|
156 | /* 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 }, |
---|
157 | /* 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 }, |
---|
158 | /* 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 } |
---|
159 | }; |
---|
160 | |
---|
161 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) |
---|
162 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) { |
---|
163 | } |
---|
164 | |
---|
165 | void CommonType::visit( __attribute((unused)) VoidType *voidType ) {} |
---|
166 | |
---|
167 | void CommonType::visit( BasicType *basicType ) { |
---|
168 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) { |
---|
169 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ]; |
---|
170 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) { |
---|
171 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType ); |
---|
172 | } // if |
---|
173 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
174 | // use signed int in lieu of the enum/zero/one type |
---|
175 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ]; |
---|
176 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) { |
---|
177 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType ); |
---|
178 | } // if |
---|
179 | } // if |
---|
180 | } |
---|
181 | |
---|
182 | template< typename Pointer > |
---|
183 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) { |
---|
184 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) { |
---|
185 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() ); |
---|
186 | if ( entry != openVars.end() ) { |
---|
187 | AssertionSet need, have; |
---|
188 | WidenMode widen( widenFirst, widenSecond ); |
---|
189 | if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return; |
---|
190 | } |
---|
191 | } |
---|
192 | result = voidPointer->clone(); |
---|
193 | result->get_qualifiers() |= otherPointer->get_qualifiers(); |
---|
194 | } |
---|
195 | |
---|
196 | void CommonType::visit( PointerType *pointerType ) { |
---|
197 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) { |
---|
198 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) { |
---|
199 | getCommonWithVoidPointer( otherPointer, pointerType ); |
---|
200 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) { |
---|
201 | getCommonWithVoidPointer( pointerType, otherPointer ); |
---|
202 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst ) |
---|
203 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
204 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers(); |
---|
205 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
206 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
207 | AssertionSet have, need; |
---|
208 | OpenVarSet newOpen( openVars ); |
---|
209 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
210 | if ( tq1 < tq2 ) { |
---|
211 | result = pointerType->clone(); |
---|
212 | } else { |
---|
213 | result = otherPointer->clone(); |
---|
214 | } // if |
---|
215 | result->get_qualifiers() = tq1 | tq2; |
---|
216 | } else { |
---|
217 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
218 | } // if |
---|
219 | pointerType->get_base()->get_qualifiers() = tq1; |
---|
220 | otherPointer->get_base()->get_qualifiers() = tq2; |
---|
221 | } // if |
---|
222 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
223 | result = pointerType->clone(); |
---|
224 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
225 | } // if |
---|
226 | } |
---|
227 | |
---|
228 | void CommonType::visit( __attribute((unused)) ArrayType *arrayType ) {} |
---|
229 | |
---|
230 | void CommonType::visit( ReferenceType *refType ) { |
---|
231 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) { |
---|
232 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) { |
---|
233 | getCommonWithVoidPointer( otherRef, refType ); |
---|
234 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) { |
---|
235 | getCommonWithVoidPointer( refType, otherRef ); |
---|
236 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) |
---|
237 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) { |
---|
238 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers(); |
---|
239 | refType->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
240 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
241 | AssertionSet have, need; |
---|
242 | OpenVarSet newOpen( openVars ); |
---|
243 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) { |
---|
244 | if ( tq1 < tq2 ) { |
---|
245 | result = refType->clone(); |
---|
246 | } else { |
---|
247 | result = otherRef->clone(); |
---|
248 | } // if |
---|
249 | result->get_qualifiers() = tq1 | tq2; |
---|
250 | } else { |
---|
251 | /// std::cout << "place for ptr-to-type" << std::endl; |
---|
252 | } // if |
---|
253 | refType->get_base()->get_qualifiers() = tq1; |
---|
254 | otherRef->get_base()->get_qualifiers() = tq2; |
---|
255 | } // if |
---|
256 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
257 | result = refType->clone(); |
---|
258 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
259 | } // if |
---|
260 | } |
---|
261 | |
---|
262 | void CommonType::visit( __attribute((unused)) FunctionType *functionType ) {} |
---|
263 | void CommonType::visit( __attribute((unused)) StructInstType *aggregateUseType ) {} |
---|
264 | void CommonType::visit( __attribute((unused)) UnionInstType *aggregateUseType ) {} |
---|
265 | |
---|
266 | void CommonType::visit( EnumInstType *enumInstType ) { |
---|
267 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) { |
---|
268 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType |
---|
269 | ValueGuard< Type * > temp( type2 ); |
---|
270 | type2 = enumInstType; |
---|
271 | temp.old->accept( *this ); |
---|
272 | } // if |
---|
273 | } |
---|
274 | |
---|
275 | void CommonType::visit( __attribute((unused)) TraitInstType *aggregateUseType ) { |
---|
276 | } |
---|
277 | |
---|
278 | void CommonType::visit( TypeInstType *inst ) { |
---|
279 | if ( widenFirst ) { |
---|
280 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ); |
---|
281 | if ( nt ) { |
---|
282 | TypeDecl *type = safe_dynamic_cast< TypeDecl* >( nt ); |
---|
283 | if ( type->get_base() ) { |
---|
284 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers(); |
---|
285 | AssertionSet have, need; |
---|
286 | OpenVarSet newOpen( openVars ); |
---|
287 | type2->get_qualifiers() = Type::Qualifiers(); |
---|
288 | type->get_base()->get_qualifiers() = tq1; |
---|
289 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) { |
---|
290 | result = type2->clone(); |
---|
291 | result->get_qualifiers() = tq1 | tq2; |
---|
292 | } // if |
---|
293 | type2->get_qualifiers() = tq2; |
---|
294 | type->get_base()->get_qualifiers() = Type::Qualifiers(); |
---|
295 | } // if |
---|
296 | } // if |
---|
297 | } // if |
---|
298 | } |
---|
299 | |
---|
300 | void CommonType::visit( __attribute((unused)) TupleType *tupleType ) {} |
---|
301 | void CommonType::visit( __attribute((unused)) VarArgsType *varArgsType ) {} |
---|
302 | |
---|
303 | void CommonType::visit( ZeroType *zeroType ) { |
---|
304 | if ( widenFirst ) { |
---|
305 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
306 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
307 | result = type2->clone(); |
---|
308 | result->get_qualifiers() |= zeroType->get_qualifiers(); |
---|
309 | } |
---|
310 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) { |
---|
311 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt ); |
---|
312 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
313 | } |
---|
314 | } |
---|
315 | } |
---|
316 | |
---|
317 | void CommonType::visit( OneType *oneType ) { |
---|
318 | if ( widenFirst ) { |
---|
319 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) { |
---|
320 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) { |
---|
321 | result = type2->clone(); |
---|
322 | result->get_qualifiers() |= oneType->get_qualifiers(); |
---|
323 | } |
---|
324 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) { |
---|
325 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt ); |
---|
326 | result->get_qualifiers() |= type2->get_qualifiers(); |
---|
327 | } |
---|
328 | } |
---|
329 | } |
---|
330 | } // namespace ResolvExpr |
---|
331 | |
---|
332 | // Local Variables: // |
---|
333 | // tab-width: 4 // |
---|
334 | // mode: c++ // |
---|
335 | // compile-command: "make install" // |
---|
336 | // End: // |
---|