source: src/ResolvExpr/CommonType.cc @ fc56cdbf

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 fc56cdbf was 5ccb10d, checked in by Rob Schluntz <rschlunt@…>, 7 years ago

Set reference size to base size, clean up debug code, remove more old-style NULLs from prelude

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