source: src/ResolvExpr/CommonType.cc @ 34dcc474

new-envwith_gc
Last change on this file since 34dcc474 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
RevLine 
[a32b204]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//
[a436947]7// CommonType.cc --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 06:59:27 2015
11// Last Modified By : Peter A. Buhr
[201aeb9]12// Last Modified On : Mon Sep 25 15:18:17 2017
13// Update Count     : 9
[a32b204]14//
[51b7345]15
[e3e16bc]16#include <cassert>                       // for strict_dynamic_cast
[ea6332d]17#include <map>                           // for _Rb_tree_const_iterator
18#include <utility>                       // for pair
19
[53452de]20#include "Common/PassVisitor.h"
[ea6332d]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
[51b7345]28
[5ccb10d]29// #define DEBUG
[51b7345]30
31namespace ResolvExpr {
[53452de]32        struct CommonType : public WithShortCircuiting {
[a32b204]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
[53452de]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:
[65cec25]55                template< typename Pointer > void getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer );
56                template< typename RefType > void handleRefType( RefType *inst, Type *other );
[a32b204]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
[000178a]66        Type * handleReference( Type * t1, Type * t2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment & env, const OpenVarSet &openVars ) {
67                Type * common = nullptr;
[65cec25]68                AssertionSet have, need;
69                OpenVarSet newOpen( openVars );
70                // need unify to bind type variables
[000178a]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;
[65cec25]78                        }
79                }
[000178a]80                // std::cerr << "exact unify failed: " << t1 << " " << t2 << std::endl;
81                return nullptr;
[65cec25]82        }
83
[a32b204]84        Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars ) {
[53452de]85                PassVisitor<CommonType> visitor( type2, widenFirst, widenSecond, indexer, env, openVars );
[65cec25]86
[e6cee92]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
[000178a]95                        if ( diff > 0 || diff < 0 ) {
[74cdfb1]96                                // std::cerr << "reference depth diff: " << diff << std::endl;
[000178a]97                                Type * result = nullptr;
[74cdfb1]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 );
[000178a]103                                        result = handleReference( ref1->base, type2, widenFirst, widenSecond, indexer, env, openVars );
104                                } else {
[74cdfb1]105                                        // deeper on the right
106                                        assert( ref2 );
[000178a]107                                        result = handleReference( type1, ref2->base, widenFirst, widenSecond, indexer, env, openVars );
108                                }
[74cdfb1]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                                }
[000178a]114                                // std::cerr << "common type of reference [" << type1 << "] and [" << type2 << "] is [" << result << "]" << std::endl;
115                                return result;
[65cec25]116                        }
[e6cee92]117                        // otherwise, both are reference types of the same depth and this is handled by the CommonType visitor.
[65cec25]118                }
119
[a32b204]120                type1->accept( visitor );
[53452de]121                Type *result = visitor.pass.get_result();
[a32b204]122                if ( ! result ) {
[2c57025]123                        // this appears to be handling for opaque type declarations
[a32b204]124                        if ( widenSecond ) {
[2c57025]125                                if ( TypeInstType *inst = dynamic_cast< TypeInstType* >( type2 ) ) {
126                                        if ( NamedTypeDecl *nt = indexer.lookupType( inst->get_name() ) ) {
[e3e16bc]127                                                TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt );
[a32b204]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();
[6f95000]136                                                                result->get_qualifiers() = tq1 | tq2;
[a32b204]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
[51b7345]145#ifdef DEBUG
[c93bc28]146                std::cerr << "============= commonType" << std::endl << "type1 is ";
147                type1->print( std::cerr );
148                std::cerr << " type2 is ";
149                type2->print( std::cerr );
[a32b204]150                if ( result ) {
[c93bc28]151                        std::cerr << " common type is ";
152                        result->print( std::cerr );
[a32b204]153                } else {
[c93bc28]154                        std::cerr << " no common type";
[a32b204]155                } // if
[c93bc28]156                std::cerr << std::endl;
[51b7345]157#endif
[a32b204]158                return result;
159        }
[51b7345]160
[a32b204]161        static const BasicType::Kind combinedType[ BasicType::NUMBER_OF_BASIC_TYPES ][ BasicType::NUMBER_OF_BASIC_TYPES ] =
162        {
[201aeb9]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, },
[a32b204]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
[53452de]193        void CommonType::postvisit( VoidType * ) {}
[a32b204]194
[53452de]195        void CommonType::postvisit( BasicType *basicType ) {
[a32b204]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 ) ) {
[6f95000]199                                result = new BasicType( basicType->get_qualifiers() | otherBasic->get_qualifiers(), newType );
[a32b204]200                        } // if
[89e6ffc]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
[a436947]203                        BasicType::Kind newType = combinedType[ basicType->get_kind() ][ BasicType::SignedInt ];
[89e6ffc]204                        if ( ( ( newType == basicType->get_kind() && basicType->get_qualifiers() >= type2->get_qualifiers() ) || widenFirst ) && ( ( newType != basicType->get_kind() && basicType->get_qualifiers() <= type2->get_qualifiers() ) || widenSecond ) ) {
[6f95000]205                                result = new BasicType( basicType->get_qualifiers() | type2->get_qualifiers(), newType );
[a436947]206                        } // if
[a32b204]207                } // if
208        }
209
[65cec25]210        template< typename Pointer >
211        void CommonType::getCommonWithVoidPointer( Pointer* voidPointer, Pointer* otherPointer ) {
[a2a77af]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                        }
[2c57025]219                }
[a2a77af]220                result = voidPointer->clone();
[6f95000]221                result->get_qualifiers() |= otherPointer->get_qualifiers();
[2c57025]222        }
223
[53452de]224        void CommonType::postvisit( PointerType *pointerType ) {
[a32b204]225                if ( PointerType *otherPointer = dynamic_cast< PointerType* >( type2 ) ) {
[c93bc28]226                        // std::cerr << "commonType: two pointers: " << pointerType << " / " << otherPointer << std::endl;
[d7dc824]227                        if ( widenFirst && dynamic_cast< VoidType* >( otherPointer->get_base() ) && ! isFtype(pointerType->get_base()) ) {
[a2a77af]228                                getCommonWithVoidPointer( otherPointer, pointerType );
[d7dc824]229                        } else if ( widenSecond && dynamic_cast< VoidType* >( pointerType->get_base() ) && ! isFtype(otherPointer->get_base()) ) {
[a2a77af]230                                getCommonWithVoidPointer( pointerType, otherPointer );
[a32b204]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 ) ) {
[c93bc28]233                                // std::cerr << "middle case" << std::endl;
[a32b204]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 ) ) {
[c93bc28]240                                        // std::cerr << "unifyExact success" << std::endl;
[a32b204]241                                        if ( tq1 < tq2 ) {
242                                                result = pointerType->clone();
243                                        } else {
244                                                result = otherPointer->clone();
245                                        } // if
[6f95000]246                                        result->get_qualifiers() = tq1 | tq2;
[a32b204]247                                } else {
[c93bc28]248                                        /// std::cerr << "place for ptr-to-type" << std::endl;
[a32b204]249                                } // if
250                                pointerType->get_base()->get_qualifiers() = tq1;
251                                otherPointer->get_base()->get_qualifiers() = tq2;
252                        } // if
[89e6ffc]253                } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
254                        result = pointerType->clone();
[6f95000]255                        result->get_qualifiers() |= type2->get_qualifiers();
[a32b204]256                } // if
257        }
258
[53452de]259        void CommonType::postvisit( ArrayType * ) {}
[65cec25]260
[53452de]261        void CommonType::postvisit( ReferenceType *refType ) {
[65cec25]262                if ( ReferenceType *otherRef = dynamic_cast< ReferenceType* >( type2 ) ) {
[c93bc28]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;
[65cec25]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 ) ) {
[c93bc28]271                                // std::cerr << "middle case" << std::endl;
[65cec25]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 {
[c93bc28]285                                        /// std::cerr << "place for ptr-to-type" << std::endl;
[65cec25]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
[53452de]296        void CommonType::postvisit( FunctionType * ) {}
297        void CommonType::postvisit( StructInstType * ) {}
298        void CommonType::postvisit( UnionInstType * ) {}
[a32b204]299
[53452de]300        void CommonType::postvisit( EnumInstType *enumInstType ) {
[89e6ffc]301                if ( dynamic_cast< BasicType * >( type2 ) || dynamic_cast< ZeroType* >( type2 ) || dynamic_cast< OneType* >( type2 ) ) {
[a436947]302                        // reuse BasicType, EnumInstType code by swapping type2 with enumInstType
[53452de]303                        result = commonType( type2, enumInstType, widenSecond, widenFirst, indexer, env, openVars );
[a436947]304                } // if
[a32b204]305        }
306
[53452de]307        void CommonType::postvisit( TraitInstType * ) {
[a32b204]308        }
309
[53452de]310        void CommonType::postvisit( TypeInstType *inst ) {
[a32b204]311                if ( widenFirst ) {
312                        NamedTypeDecl *nt = indexer.lookupType( inst->get_name() );
313                        if ( nt ) {
[e3e16bc]314                                TypeDecl *type = strict_dynamic_cast< TypeDecl* >( nt );
[a32b204]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();
[6f95000]323                                                result->get_qualifiers() = tq1 | tq2;
[a32b204]324                                        } // if
325                                        type2->get_qualifiers() = tq2;
326                                        type->get_base()->get_qualifiers() = Type::Qualifiers();
327                                } // if
328                        } // if
329                } // if
330        }
331
[53452de]332        void CommonType::postvisit( TupleType * ) {}
333        void CommonType::postvisit( VarArgsType * ) {}
[89e6ffc]334
[53452de]335        void CommonType::postvisit( ZeroType *zeroType ) {
[89e6ffc]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();
[6f95000]340                                        result->get_qualifiers() |= zeroType->get_qualifiers();
[89e6ffc]341                                }
[4cb935e]342                        } else if ( widenSecond && dynamic_cast< OneType* >( type2 ) ) {
343                                result = new BasicType( zeroType->get_qualifiers(), BasicType::SignedInt );
[6f95000]344                                result->get_qualifiers() |= type2->get_qualifiers();
[89e6ffc]345                        }
346                }
347        }
348
[53452de]349        void CommonType::postvisit( OneType *oneType ) {
[89e6ffc]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();
[6f95000]354                                        result->get_qualifiers() |= oneType->get_qualifiers();
[89e6ffc]355                                }
[4cb935e]356                        } else if ( widenSecond && dynamic_cast< ZeroType* >( type2 ) ) {
357                                result = new BasicType( oneType->get_qualifiers(), BasicType::SignedInt );
[6f95000]358                                result->get_qualifiers() |= type2->get_qualifiers();
[89e6ffc]359                        }
360                }
361        }
[51b7345]362} // namespace ResolvExpr
[a32b204]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.