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 : Mon Sep 25 15:18:17 2017
|
---|
13 | // Update Count : 9
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cassert> // for strict_dynamic_cast
|
---|
17 | #include <map> // for _Rb_tree_const_iterator
|
---|
18 | #include <utility> // for pair
|
---|
19 |
|
---|
20 | #include "Common/PassVisitor.h"
|
---|
21 | #include "ResolvExpr/TypeEnvironment.h" // for OpenVarSet, AssertionSet
|
---|
22 | #include "SymTab/Indexer.h" // for Indexer
|
---|
23 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl (ptr...
|
---|
24 | #include "SynTree/Type.h" // for BasicType, BasicType::Kind::...
|
---|
25 | #include "SynTree/Visitor.h" // for Visitor
|
---|
26 | #include "Unify.h" // for unifyExact, bindVar, WidenMode
|
---|
27 | #include "typeops.h" // for isFtype
|
---|
28 |
|
---|
29 | // #define DEBUG
|
---|
30 | #ifdef DEBUG
|
---|
31 | #define PRINT(x) x
|
---|
32 | #else
|
---|
33 | #define PRINT(x)
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | namespace ResolvExpr {
|
---|
37 | struct CommonType : public WithShortCircuiting {
|
---|
38 | CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
|
---|
39 | Type *get_result() const { return result; }
|
---|
40 |
|
---|
41 | void previsit( BaseSyntaxNode * ) { visit_children = false; }
|
---|
42 |
|
---|
43 | void postvisit( VoidType * voidType );
|
---|
44 | void postvisit( BasicType * basicType );
|
---|
45 | void postvisit( PointerType * pointerType );
|
---|
46 | void postvisit( ArrayType * arrayType );
|
---|
47 | void postvisit( ReferenceType * refType );
|
---|
48 | void postvisit( FunctionType * functionType );
|
---|
49 | void postvisit( StructInstType * aggregateUseType );
|
---|
50 | void postvisit( UnionInstType * aggregateUseType );
|
---|
51 | void postvisit( EnumInstType * aggregateUseType );
|
---|
52 | void postvisit( TraitInstType * aggregateUseType );
|
---|
53 | void postvisit( TypeInstType * aggregateUseType );
|
---|
54 | void postvisit( TupleType * tupleType );
|
---|
55 | void postvisit( VarArgsType * varArgsType );
|
---|
56 | void postvisit( ZeroType * zeroType );
|
---|
57 | void postvisit( OneType * oneType );
|
---|
58 |
|
---|
59 | private:
|
---|
60 | template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer );
|
---|
61 | template< typename RefType > void handleRefType( RefType *inst, Type *other );
|
---|
62 |
|
---|
63 | Type *result;
|
---|
64 | Type *type2; // inherited
|
---|
65 | bool widenFirst, widenSecond;
|
---|
66 | const SymTab::Indexer &indexer;
|
---|
67 | TypeEnvironment &env;
|
---|
68 | const OpenVarSet &openVars;
|
---|
69 | };
|
---|
70 |
|
---|
71 | Type * handleReference( Type * t1, Type * t2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) {
|
---|
72 | Type * common = nullptr;
|
---|
73 | AssertionSet have, need;
|
---|
74 | OpenVarSet newOpen( openVars );
|
---|
75 | // need unify to bind type variables
|
---|
76 | if ( unify( t1, t2, env, have, need, newOpen, indexer, common ) ) {
|
---|
77 | PRINT(
|
---|
78 | std::cerr << "unify success: " << widenFirst << " " << widenSecond << std::endl;
|
---|
79 | )
|
---|
80 | if ( (widenFirst || t2->get_qualifiers() <= t1->get_qualifiers()) && (widenSecond || t1->get_qualifiers() <= t2->get_qualifiers()) ) {
|
---|
81 | PRINT(
|
---|
82 | std::cerr << "widen okay" << std::endl;
|
---|
83 | )
|
---|
84 | common->get_qualifiers() |= t1->get_qualifiers();
|
---|
85 | common->get_qualifiers() |= t2->get_qualifiers();
|
---|
86 | return common;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | PRINT(
|
---|
90 | std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl;
|
---|
91 | )
|
---|
92 | return nullptr;
|
---|
93 | }
|
---|
94 |
|
---|
95 | Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) {
|
---|
96 | PassVisitor<CommonType> visitor( type2, widenFirst, widenSecond, indexer, env, openVars );
|
---|
97 |
|
---|
98 | int depth1 = type1->referenceDepth();
|
---|
99 | int depth2 = type2->referenceDepth();
|
---|
100 | if ( depth1 > 0 || depth2 > 0 ) {
|
---|
101 | int diff = depth1-depth2;
|
---|
102 | // 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.
|
---|
103 | // if ( diff > 1 || diff < -1 ) return nullptr;
|
---|
104 |
|
---|
105 | // special case where one type has a reference depth of 1 larger than the other
|
---|
106 | if ( diff > 0 || diff < 0 ) {
|
---|
107 | PRINT(
|
---|
108 | std::cerr << "reference depth diff: " << diff << std::endl;
|
---|
109 | )
|
---|
110 | Type * result = nullptr;
|
---|
111 | ReferenceType * ref1 = dynamic_cast< ReferenceType * >( type1 );
|
---|
112 | ReferenceType * ref2 = dynamic_cast< ReferenceType * >( type2 );
|
---|
113 | if ( diff > 0 ) {
|
---|
114 | // deeper on the left
|
---|
115 | assert( ref1 );
|
---|
116 | result = handleReference( ref1->base, type2, widenFirst, widenSecond, indexer, env, openVars );
|
---|
117 | } else {
|
---|
118 | // deeper on the right
|
---|
119 | assert( ref2 );
|
---|
120 | result = handleReference( type1, ref2->base, widenFirst, widenSecond, indexer, env, openVars );
|
---|
121 | }
|
---|
122 | if ( result && ref1 ) {
|
---|
123 | // formal is reference, so result should be reference
|
---|
124 | PRINT(
|
---|
125 | std::cerr << "formal is reference; result should be reference" << std::endl;
|
---|
126 | )
|
---|
127 | result = new ReferenceType( ref1->get_qualifiers(), result );
|
---|
128 | }
|
---|
129 | PRINT(
|
---|
130 | std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is [" << result << "]" << std::endl;
|
---|
131 | )
|
---|
132 | return result;
|
---|
133 | }
|
---|
134 | // otherwise, both are reference types of the same depth and this is handled by the CommonType visitor.
|
---|
135 | }
|
---|
136 |
|
---|
137 | type1->accept( visitor );
|
---|
138 | Type *result = visitor.pass.get_result();
|
---|
139 | if ( ! result ) {
|
---|
140 | // this appears to be handling for opaque type declarations
|
---|
141 | if ( widenSecond ) {
|
---|
142 | if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) {
|
---|
143 | if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) {
|
---|
144 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt );
|
---|
145 | if ( type->get_base() ) {
|
---|
146 | Type::Qualifiers tq1 = type1->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
147 | AssertionSet have, need;
|
---|
148 | OpenVarSet newOpen( openVars );
|
---|
149 | type1->get_qualifiers() = Type::Qualifiers();
|
---|
150 | type->get_base()->get_qualifiers() = tq1;
|
---|
151 | if ( unifyExact( type1, type->get_base(), env, have, need, newOpen, indexer ) ) {
|
---|
152 | result = type1->clone();
|
---|
153 | result->get_qualifiers() = tq1 | tq2;
|
---|
154 | } // if
|
---|
155 | type1->get_qualifiers() = tq1;
|
---|
156 | type->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
157 | } // if
|
---|
158 | } // if
|
---|
159 | } // if
|
---|
160 | } // if
|
---|
161 | } // if
|
---|
162 | #ifdef DEBUG
|
---|
163 | std::cerr << "============= commonType" << std::endl << "type1 is ";
|
---|
164 | type1->print( std::cerr );
|
---|
165 | std::cerr << " type2 is ";
|
---|
166 | type2->print( std::cerr );
|
---|
167 | if ( result ) {
|
---|
168 | std::cerr << " common type is ";
|
---|
169 | result->print( std::cerr );
|
---|
170 | } else {
|
---|
171 | std::cerr << " no common type";
|
---|
172 | } // if
|
---|
173 | std::cerr << std::endl;
|
---|
174 | #endif
|
---|
175 | return result;
|
---|
176 | }
|
---|
177 |
|
---|
178 | static const BasicType::Kind combinedType[][ BasicType::NUMBER_OF_BASIC_TYPES ] =
|
---|
179 | {
|
---|
180 | /* Bool Char SignedChar UnsignedChar ShortSignedInt ShortUnsignedInt SignedInt UnsignedInt LongSignedInt LongUnsignedInt LongLongSignedInt LongLongUnsignedInt Float Double LongDouble FloatComplex DoubleComplex LongDoubleComplex FloatImaginary DoubleImaginary LongDoubleImaginary SignedInt128 UnsignedInt128 Float80 Float128 */
|
---|
181 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
182 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
183 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
184 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
185 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
186 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
187 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
188 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
189 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
190 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
191 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
192 | /* 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, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128 },
|
---|
193 | /* 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, BasicType::Float, BasicType::Float, BasicType::Float80, BasicType::Float128 },
|
---|
194 | /* 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, BasicType::Double, BasicType::Double, BasicType::Float80, BasicType::Float128 },
|
---|
195 | /* 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, BasicType::LongDouble, BasicType::LongDouble, BasicType::BasicType::LongDouble, BasicType::Float128 },
|
---|
196 | /* 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, BasicType::FloatComplex, BasicType::FloatComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, },
|
---|
197 | /* 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, BasicType::DoubleComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex },
|
---|
198 | /* 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, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, },
|
---|
199 | /* 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, BasicType::FloatImaginary, BasicType::FloatImaginary, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, },
|
---|
200 | /* 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, BasicType::DoubleImaginary, BasicType::DoubleImaginary, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, },
|
---|
201 | /* 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, BasicType::LongDoubleImaginary, BasicType::LongDoubleImaginary, },
|
---|
202 | /* SignedInt128 */ { BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::SignedInt128, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::SignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128, },
|
---|
203 | /* UnsignedInt128 */ { BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::Float, BasicType::Double, BasicType::LongDouble, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::FloatComplex, BasicType::DoubleComplex, BasicType::LongDoubleComplex, BasicType::UnsignedInt128, BasicType::UnsignedInt128, BasicType::Float80, BasicType::Float128, },
|
---|
204 | /* Float80 */ { BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::LongDouble, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::Float80, BasicType::Float80, BasicType::Float80, BasicType::Float128 },
|
---|
205 | /* Float128 */ { BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::LongDoubleComplex, BasicType::Float128, BasicType::Float128, BasicType::Float128, BasicType::Float128 },
|
---|
206 | };
|
---|
207 | static_assert(
|
---|
208 | sizeof(combinedType)/sizeof(combinedType[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES,
|
---|
209 | "Each basic type kind should have a corresponding row in the combined type matrix"
|
---|
210 | );
|
---|
211 |
|
---|
212 | CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars )
|
---|
213 | : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) {
|
---|
214 | }
|
---|
215 |
|
---|
216 | void CommonType::postvisit( VoidType * ) {}
|
---|
217 |
|
---|
218 | void CommonType::postvisit( BasicType *basicType ) {
|
---|
219 | if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
|
---|
220 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ];
|
---|
221 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) {
|
---|
222 | result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType );
|
---|
223 | } // if
|
---|
224 | } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
|
---|
225 | // use signed int in lieu of the enum/zero/one type
|
---|
226 | BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ];
|
---|
227 | if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) {
|
---|
228 | result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType );
|
---|
229 | } // if
|
---|
230 | } // if
|
---|
231 | }
|
---|
232 |
|
---|
233 | template< typename Pointer >
|
---|
234 | void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) {
|
---|
235 | if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) {
|
---|
236 | OpenVarSet::const_iterator entry = openVars.find( var->get_name() );
|
---|
237 | if ( entry != openVars.end() ) {
|
---|
238 | AssertionSet need, have;
|
---|
239 | WidenMode widen( widenFirst, widenSecond );
|
---|
240 | if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return;
|
---|
241 | }
|
---|
242 | }
|
---|
243 | result = voidPointer->clone();
|
---|
244 | result->get_qualifiers() |= otherPointer->get_qualifiers();
|
---|
245 | }
|
---|
246 |
|
---|
247 | void CommonType::postvisit( PointerType *pointerType ) {
|
---|
248 | if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
|
---|
249 | // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl;
|
---|
250 | if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) {
|
---|
251 | getCommonWithVoidPointer( otherPointer, pointerType );
|
---|
252 | } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) {
|
---|
253 | getCommonWithVoidPointer( pointerType, otherPointer );
|
---|
254 | } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst )
|
---|
255 | && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) {
|
---|
256 | // std::cerr << "middle case" << std::endl;
|
---|
257 | Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers();
|
---|
258 | pointerType->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
259 | otherPointer->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
260 | AssertionSet have, need;
|
---|
261 | OpenVarSet newOpen( openVars );
|
---|
262 | if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) {
|
---|
263 | // std::cerr << "unifyExact success" << std::endl;
|
---|
264 | if ( tq1 < tq2 ) {
|
---|
265 | result = pointerType->clone();
|
---|
266 | } else {
|
---|
267 | result = otherPointer->clone();
|
---|
268 | } // if
|
---|
269 | result->get_qualifiers() = tq1 | tq2;
|
---|
270 | } else {
|
---|
271 | /// std::cerr << "place for ptr-to-type" << std::endl;
|
---|
272 | } // if
|
---|
273 | pointerType->get_base()->get_qualifiers() = tq1;
|
---|
274 | otherPointer->get_base()->get_qualifiers() = tq2;
|
---|
275 | } // if
|
---|
276 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
|
---|
277 | result = pointerType->clone();
|
---|
278 | result->get_qualifiers() |= type2->get_qualifiers();
|
---|
279 | } // if
|
---|
280 | }
|
---|
281 |
|
---|
282 | void CommonType::postvisit( ArrayType * ) {}
|
---|
283 |
|
---|
284 | void CommonType::postvisit( ReferenceType *refType ) {
|
---|
285 | if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
|
---|
286 | // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl;
|
---|
287 | // std::cerr << ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst ) << (refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond) << std::endl;
|
---|
288 | if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) {
|
---|
289 | getCommonWithVoidPointer( otherRef, refType );
|
---|
290 | } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) {
|
---|
291 | getCommonWithVoidPointer( refType, otherRef );
|
---|
292 | } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst )
|
---|
293 | && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) {
|
---|
294 | // std::cerr << "middle case" << std::endl;
|
---|
295 | Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers();
|
---|
296 | refType->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
297 | otherRef->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
298 | AssertionSet have, need;
|
---|
299 | OpenVarSet newOpen( openVars );
|
---|
300 | if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) {
|
---|
301 | if ( tq1 < tq2 ) {
|
---|
302 | result = refType->clone();
|
---|
303 | } else {
|
---|
304 | result = otherRef->clone();
|
---|
305 | } // if
|
---|
306 | result->get_qualifiers() = tq1 | tq2;
|
---|
307 | } else {
|
---|
308 | /// std::cerr << "place for ptr-to-type" << std::endl;
|
---|
309 | } // if
|
---|
310 | refType->get_base()->get_qualifiers() = tq1;
|
---|
311 | otherRef->get_base()->get_qualifiers() = tq2;
|
---|
312 | } // if
|
---|
313 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
|
---|
314 | result = refType->clone();
|
---|
315 | result->get_qualifiers() |= type2->get_qualifiers();
|
---|
316 | } // if
|
---|
317 | }
|
---|
318 |
|
---|
319 | void CommonType::postvisit( FunctionType * ) {}
|
---|
320 | void CommonType::postvisit( StructInstType * ) {}
|
---|
321 | void CommonType::postvisit( UnionInstType * ) {}
|
---|
322 |
|
---|
323 | void CommonType::postvisit( EnumInstType *enumInstType ) {
|
---|
324 | if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
|
---|
325 | // reuse BasicType, EnumInstType code by swapping type2 with enumInstType
|
---|
326 | result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars );
|
---|
327 | } // if
|
---|
328 | }
|
---|
329 |
|
---|
330 | void CommonType::postvisit( TraitInstType * ) {
|
---|
331 | }
|
---|
332 |
|
---|
333 | void CommonType::postvisit( TypeInstType *inst ) {
|
---|
334 | if ( widenFirst ) {
|
---|
335 | NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
|
---|
336 | if ( nt ) {
|
---|
337 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt );
|
---|
338 | if ( type->get_base() ) {
|
---|
339 | Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers();
|
---|
340 | AssertionSet have, need;
|
---|
341 | OpenVarSet newOpen( openVars );
|
---|
342 | type2->get_qualifiers() = Type::Qualifiers();
|
---|
343 | type->get_base()->get_qualifiers() = tq1;
|
---|
344 | if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) {
|
---|
345 | result = type2->clone();
|
---|
346 | result->get_qualifiers() = tq1 | tq2;
|
---|
347 | } // if
|
---|
348 | type2->get_qualifiers() = tq2;
|
---|
349 | type->get_base()->get_qualifiers() = Type::Qualifiers();
|
---|
350 | } // if
|
---|
351 | } // if
|
---|
352 | } // if
|
---|
353 | }
|
---|
354 |
|
---|
355 | void CommonType::postvisit( TupleType * ) {}
|
---|
356 | void CommonType::postvisit( VarArgsType * ) {}
|
---|
357 |
|
---|
358 | void CommonType::postvisit( ZeroType *zeroType ) {
|
---|
359 | if ( widenFirst ) {
|
---|
360 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
|
---|
361 | if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) {
|
---|
362 | result = type2->clone();
|
---|
363 | result->get_qualifiers() |= zeroType->get_qualifiers();
|
---|
364 | }
|
---|
365 | } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) {
|
---|
366 | result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt );
|
---|
367 | result->get_qualifiers() |= type2->get_qualifiers();
|
---|
368 | }
|
---|
369 | }
|
---|
370 | }
|
---|
371 |
|
---|
372 | void CommonType::postvisit( OneType *oneType ) {
|
---|
373 | if ( widenFirst ) {
|
---|
374 | if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
|
---|
375 | if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) {
|
---|
376 | result = type2->clone();
|
---|
377 | result->get_qualifiers() |= oneType->get_qualifiers();
|
---|
378 | }
|
---|
379 | } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
|
---|
380 | result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt );
|
---|
381 | result->get_qualifiers() |= type2->get_qualifiers();
|
---|
382 | }
|
---|
383 | }
|
---|
384 | }
|
---|
385 | } // namespace ResolvExpr
|
---|
386 |
|
---|
387 | // Local Variables: //
|
---|
388 | // tab-width: 4 //
|
---|
389 | // mode: c++ //
|
---|
390 | // compile-command: "make install" //
|
---|
391 | // End: //
|
---|