source: src/ResolvExpr/CommonType.cc@ 33f5b57

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum with_gc
Last change on this file since 33f5b57 was 9d1e3f7, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Minor updates

  • Property mode set to 100644
File size: 29.8 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// 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
36namespace 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 ][ 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 */
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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, },
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 },
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, },
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, },
204 };
205
206 CommonType::CommonType( Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars )
207 : result( 0 ), type2( type2 ), widenFirst( widenFirst ), widenSecond( widenSecond ), indexer( indexer ), env( env ), openVars( openVars ) {
208 }
209
210 void CommonType::postvisit( VoidType * ) {}
211
212 void CommonType::postvisit( BasicType *basicType ) {
213 if ( BasicType *otherBasic = dynamic_cast< BasicType* >( type2 ) ) {
214 BasicType::Kind newType = combinedType[ basicType->get_kind() ][ otherBasic->get_kind() ];
215 if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= otherBasic->get_qualifiers() ) || widenFirst ) && ( ( newType == otherBasic->get_kind() && basicType->get_qualifiers() <= otherBasic->get_qualifiers() ) || widenSecond ) ) {
216 result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType );
217 } // if
218 } else if ( dynamic_cast< EnumInstType * > ( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
219 // use signed int in lieu of the enum/zero/one type
220 BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ];
221 if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) {
222 result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType );
223 } // if
224 } // if
225 }
226
227 template< typename Pointer >
228 void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) {
229 if ( TypeInstType* var = dynamic_cast< TypeInstType* >( otherPointer->get_base() ) ) {
230 OpenVarSet::const_iterator entry = openVars.find( var->get_name() );
231 if ( entry != openVars.end() ) {
232 AssertionSet need, have;
233 WidenMode widen( widenFirst, widenSecond );
234 if ( entry != openVars.end() && ! bindVar(var, voidPointer->get_base(), entry->second, env, need, have, openVars, widen, indexer ) ) return;
235 }
236 }
237 result = voidPointer->clone();
238 result->get_qualifiers() |= otherPointer->get_qualifiers();
239 }
240
241 void CommonType::postvisit( PointerType *pointerType ) {
242 if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
243 // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl;
244 if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) {
245 getCommonWithVoidPointer( otherPointer, pointerType );
246 } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) {
247 getCommonWithVoidPointer( pointerType, otherPointer );
248 } else if ( ( pointerType->get_base()->get_qualifiers() >= otherPointer->get_base()->get_qualifiers() || widenFirst )
249 && ( pointerType->get_base()->get_qualifiers() <= otherPointer->get_base()->get_qualifiers() || widenSecond ) ) {
250 // std::cerr << "middle case" << std::endl;
251 Type::Qualifiers tq1 = pointerType->get_base()->get_qualifiers(), tq2 = otherPointer->get_base()->get_qualifiers();
252 pointerType->get_base()->get_qualifiers() = Type::Qualifiers();
253 otherPointer->get_base()->get_qualifiers() = Type::Qualifiers();
254 AssertionSet have, need;
255 OpenVarSet newOpen( openVars );
256 if ( unifyExact( pointerType->get_base(), otherPointer->get_base(), env, have, need, newOpen, indexer ) ) {
257 // std::cerr << "unifyExact success" << std::endl;
258 if ( tq1 < tq2 ) {
259 result = pointerType->clone();
260 } else {
261 result = otherPointer->clone();
262 } // if
263 result->get_qualifiers() = tq1 | tq2;
264 } else {
265 /// std::cerr << "place for ptr-to-type" << std::endl;
266 } // if
267 pointerType->get_base()->get_qualifiers() = tq1;
268 otherPointer->get_base()->get_qualifiers() = tq2;
269 } // if
270 } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
271 result = pointerType->clone();
272 result->get_qualifiers() |= type2->get_qualifiers();
273 } // if
274 }
275
276 void CommonType::postvisit( ArrayType * ) {}
277
278 void CommonType::postvisit( ReferenceType *refType ) {
279 if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
280 // std::cerr << "commonType: both references: " << refType << " / " << otherRef << std::endl;
281 // 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;
282 if ( widenFirst && dynamic_cast< VoidType* >( otherRef->get_base() ) && ! isFtype(refType->get_base()) ) {
283 getCommonWithVoidPointer( otherRef, refType );
284 } else if ( widenSecond && dynamic_cast< VoidType* >( refType->get_base() ) && ! isFtype(otherRef->get_base()) ) {
285 getCommonWithVoidPointer( refType, otherRef );
286 } else if ( ( refType->get_base()->get_qualifiers() >= otherRef->get_base()->get_qualifiers() || widenFirst )
287 && ( refType->get_base()->get_qualifiers() <= otherRef->get_base()->get_qualifiers() || widenSecond ) ) {
288 // std::cerr << "middle case" << std::endl;
289 Type::Qualifiers tq1 = refType->get_base()->get_qualifiers(), tq2 = otherRef->get_base()->get_qualifiers();
290 refType->get_base()->get_qualifiers() = Type::Qualifiers();
291 otherRef->get_base()->get_qualifiers() = Type::Qualifiers();
292 AssertionSet have, need;
293 OpenVarSet newOpen( openVars );
294 if ( unifyExact( refType->get_base(), otherRef->get_base(), env, have, need, newOpen, indexer ) ) {
295 if ( tq1 < tq2 ) {
296 result = refType->clone();
297 } else {
298 result = otherRef->clone();
299 } // if
300 result->get_qualifiers() = tq1 | tq2;
301 } else {
302 /// std::cerr << "place for ptr-to-type" << std::endl;
303 } // if
304 refType->get_base()->get_qualifiers() = tq1;
305 otherRef->get_base()->get_qualifiers() = tq2;
306 } // if
307 } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
308 result = refType->clone();
309 result->get_qualifiers() |= type2->get_qualifiers();
310 } // if
311 }
312
313 void CommonType::postvisit( FunctionType * ) {}
314 void CommonType::postvisit( StructInstType * ) {}
315 void CommonType::postvisit( UnionInstType * ) {}
316
317 void CommonType::postvisit( EnumInstType *enumInstType ) {
318 if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
319 // reuse BasicType, EnumInstType code by swapping type2 with enumInstType
320 result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars );
321 } // if
322 }
323
324 void CommonType::postvisit( TraitInstType * ) {
325 }
326
327 void CommonType::postvisit( TypeInstType *inst ) {
328 if ( widenFirst ) {
329 NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
330 if ( nt ) {
331 TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt );
332 if ( type->get_base() ) {
333 Type::Qualifiers tq1 = inst->get_qualifiers(), tq2 = type2->get_qualifiers();
334 AssertionSet have, need;
335 OpenVarSet newOpen( openVars );
336 type2->get_qualifiers() = Type::Qualifiers();
337 type->get_base()->get_qualifiers() = tq1;
338 if ( unifyExact( type->get_base(), type2, env, have, need, newOpen, indexer ) ) {
339 result = type2->clone();
340 result->get_qualifiers() = tq1 | tq2;
341 } // if
342 type2->get_qualifiers() = tq2;
343 type->get_base()->get_qualifiers() = Type::Qualifiers();
344 } // if
345 } // if
346 } // if
347 }
348
349 void CommonType::postvisit( TupleType * ) {}
350 void CommonType::postvisit( VarArgsType * ) {}
351
352 void CommonType::postvisit( ZeroType *zeroType ) {
353 if ( widenFirst ) {
354 if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< PointerType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
355 if ( widenSecond || zeroType->get_qualifiers() <= type2->get_qualifiers() ) {
356 result = type2->clone();
357 result->get_qualifiers() |= zeroType->get_qualifiers();
358 }
359 } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) {
360 result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt );
361 result->get_qualifiers() |= type2->get_qualifiers();
362 }
363 }
364 }
365
366 void CommonType::postvisit( OneType *oneType ) {
367 if ( widenFirst ) {
368 if ( dynamic_cast< BasicType* >( type2 ) || dynamic_cast< EnumInstType* >( type2 ) ) {
369 if ( widenSecond || oneType->get_qualifiers() <= type2->get_qualifiers() ) {
370 result = type2->clone();
371 result->get_qualifiers() |= oneType->get_qualifiers();
372 }
373 } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
374 result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt );
375 result->get_qualifiers() |= type2->get_qualifiers();
376 }
377 }
378 }
379} // namespace ResolvExpr
380
381// Local Variables: //
382// tab-width: 4 //
383// mode: c++ //
384// compile-command: "make install" //
385// End: //
Note: See TracBrowser for help on using the repository browser.