source: src/ResolvExpr/CommonType.cc @ 10a97ad

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumwith_gc
Last change on this file since 10a97ad was 53452de, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Convert CommonType? to PassVisitor?

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