source: src/ResolvExpr/CommonType.cc @ 62194cb

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since 62194cb was 000178a, checked in by Rob Schluntz <rschlunt@…>, 6 years ago

Refactor reference CommonType? code

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