source: src/ResolvExpr/ConversionCost.cc @ cdcddfe1

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpersistent-indexerpthread-emulationqualifiedEnum
Last change on this file since cdcddfe1 was cdcddfe1, checked in by Peter A. Buhr <pabuhr@…>, 5 years ago

add signedness to cost model and _FloatNN

  • Property mode set to 100644
File size: 34.7 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// ConversionCost.cc --
[a32b204]8//
9// Author           : Richard C. Bilson
10// Created On       : Sun May 17 07:06:19 2015
11// Last Modified By : Peter A. Buhr
[cdcddfe1]12// Last Modified On : Fri Feb  8 09:48:39 2019
13// Update Count     : 19
[a32b204]14//
[51b7345]15
16#include "ConversionCost.h"
[ea6332d]17
18#include <cassert>                       // for assert
19#include <list>                          // for list, list<>::const_iterator
20#include <string>                        // for operator==, string
21
22#include "ResolvExpr/Cost.h"             // for Cost
23#include "ResolvExpr/TypeEnvironment.h"  // for EqvClass, TypeEnvironment
24#include "SymTab/Indexer.h"              // for Indexer
25#include "SynTree/Declaration.h"         // for TypeDecl, NamedTypeDecl
26#include "SynTree/Type.h"                // for Type, BasicType, TypeInstType
27#include "typeops.h"                     // for typesCompatibleIgnoreQualifiers
[51b7345]28
29namespace ResolvExpr {
[cdcddfe1]30        const Cost Cost::zero =      Cost{  0,  0,  0,  0,  0,  0,  0 };
31        const Cost Cost::infinity =  Cost{ -1, -1, -1, -1, -1,  1, -1 };
32        const Cost Cost::unsafe =    Cost{  1,  0,  0,  0,  0,  0,  0 };
33        const Cost Cost::poly =      Cost{  0,  1,  0,  0,  0,  0,  0 };
34        const Cost Cost::safe =      Cost{  0,  0,  1,  0,  0,  0,  0 };
35        const Cost Cost::sign =      Cost{  0,  0,  0,  1,  0,  0,  0 };
36        const Cost Cost::var =       Cost{  0,  0,  0,  0,  1,  0,  0 };
37        const Cost Cost::spec =      Cost{  0,  0,  0,  0,  0, -1,  0 };
38        const Cost Cost::reference = Cost{  0,  0,  0,  0,  0,  0,  1 };
[b46e3bd]39
[5ccb10d]40#if 0
41#define PRINT(x) x
42#else
43#define PRINT(x)
44#endif
[a32b204]45        Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
46                if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
[eb0aedb]47                        PRINT( std::cerr << "type inst " << destAsTypeInst->name; )
[00ac42e]48                        if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->name ) ) {
49                                if ( eqvClass->type ) {
50                                        return conversionCost( src, eqvClass->type, indexer, env );
[0b150ec]51                                } else {
52                                        return Cost::infinity;
53                                }
[00ac42e]54                        } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->name ) ) {
[5ccb10d]55                                PRINT( std::cerr << " found" << std::endl; )
[a32b204]56                                TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
57                                // all typedefs should be gone by this point
58                                assert( type );
[eb0aedb]59                                if ( type->base ) {
60                                        return conversionCost( src, type->base, indexer, env ) + Cost::safe;
[a32b204]61                                } // if
62                        } // if
[5ccb10d]63                        PRINT( std::cerr << " not found" << std::endl; )
[a32b204]64                } // if
[5ccb10d]65                PRINT(
66                        std::cerr << "src is ";
67                        src->print( std::cerr );
68                        std::cerr << std::endl << "dest is ";
69                        dest->print( std::cerr );
70                        std::cerr << std::endl << "env is" << std::endl;
71                        env.print( std::cerr, 8 );
72                )
[a32b204]73                if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
[5ccb10d]74                        PRINT( std::cerr << "compatible!" << std::endl; )
[89be1c68]75                        return Cost::zero;
[a32b204]76                } else if ( dynamic_cast< VoidType* >( dest ) ) {
[89be1c68]77                        return Cost::safe;
[2463d0e]78                } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
[5ccb10d]79                        PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
[721cd19f]80                        return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer &, const TypeEnvironment & env ){
[0c6596f]81                                return ptrsAssignable( t1, t2, env );
82                        });
[a32b204]83                } else {
[bd0b6b62]84                        PassVisitor<ConversionCost> converter( dest, indexer, env, conversionCost );
[a32b204]85                        src->accept( converter );
[bd0b6b62]86                        if ( converter.pass.get_cost() == Cost::infinity ) {
[a32b204]87                                return Cost::infinity;
88                        } else {
[bd0b6b62]89                                return converter.pass.get_cost() + Cost::zero;
[a32b204]90                        } // if
91                } // if
92        }
93
[0c6596f]94        Cost convertToReferenceCost( Type * src, Type * dest, int diff, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) {
[6e027d6]95                PRINT( std::cerr << "convert to reference cost... diff " << diff << " " << src << " / " << dest << std::endl; )
[89be1c68]96                if ( diff > 0 ) {
97                        // TODO: document this
[eb0aedb]98                        Cost cost = convertToReferenceCost( strict_dynamic_cast< ReferenceType * >( src )->base, dest, diff-1, indexer, env, func );
[7ebaa56]99                        cost.incReference();
[89be1c68]100                        return cost;
101                } else if ( diff < -1 ) {
102                        // TODO: document this
[eb0aedb]103                        Cost cost = convertToReferenceCost( src, strict_dynamic_cast< ReferenceType * >( dest )->base, diff+1, indexer, env, func );
[7ebaa56]104                        cost.incReference();
[89be1c68]105                        return cost;
106                } else if ( diff == 0 ) {
107                        ReferenceType * srcAsRef = dynamic_cast< ReferenceType * >( src );
108                        ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest );
109                        if ( srcAsRef && destAsRef ) { // pointer-like conversions between references
[5ccb10d]110                                PRINT( std::cerr << "converting between references" << std::endl; )
[eb0aedb]111                                Type::Qualifiers tq1 = srcAsRef->base->get_qualifiers();
112                                Type::Qualifiers tq2 = destAsRef->base->get_qualifiers();
113                                if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( srcAsRef->base, destAsRef->base, indexer, env ) ) {
[6e027d6]114                                        PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; )
115                                        if ( tq1 == tq2 ) {
116                                                // types are the same
117                                                return Cost::zero;
118                                        } else {
119                                                // types are the same, except otherPointer has more qualifiers
120                                                return Cost::safe;
121                                        }
[89be1c68]122                                } else {  // xxx - this discards reference qualifiers from consideration -- reducing qualifiers is a safe conversion; is this right?
[721cd19f]123                                        int assignResult = func( srcAsRef->base, destAsRef->base, indexer, env );
[5ccb10d]124                                        PRINT( std::cerr << "comparing references: " << assignResult << " " << srcAsRef << " " << destAsRef << std::endl; )
[b0837e4]125                                        if ( assignResult > 0 ) {
[89be1c68]126                                                return Cost::safe;
[b0837e4]127                                        } else if ( assignResult < 0 ) {
[89be1c68]128                                                return Cost::unsafe;
129                                        } // if
[2463d0e]130                                } // if
131                        } else {
[5ccb10d]132                                PRINT( std::cerr << "reference to rvalue conversion" << std::endl; )
[bd0b6b62]133                                PassVisitor<ConversionCost> converter( dest, indexer, env, conversionCost );
[89be1c68]134                                src->accept( converter );
[bd0b6b62]135                                return converter.pass.get_cost();
[89be1c68]136                        } // if
[2463d0e]137                } else {
[89be1c68]138                        ReferenceType * destAsRef = dynamic_cast< ReferenceType * >( dest );
139                        assert( diff == -1 && destAsRef );
[108f3cdb]140                        PRINT( std::cerr << "dest is: " << dest << " / src is: " << src << std::endl; )
[eb0aedb]141                        if ( typesCompatibleIgnoreQualifiers( src, destAsRef->base, indexer, env ) ) {
[5ccb10d]142                                PRINT( std::cerr << "converting compatible base type" << std::endl; )
[89be1c68]143                                if ( src->get_lvalue() ) {
[5ccb10d]144                                        PRINT(
145                                                std::cerr << "lvalue to reference conversion" << std::endl;
146                                                std::cerr << src << " => " << destAsRef << std::endl;
147                                        )
[89be1c68]148                                        // lvalue-to-reference conversion:  cv lvalue T => cv T &
[eb0aedb]149                                        if ( src->get_qualifiers() == destAsRef->base->get_qualifiers() ) {
[7ebaa56]150                                                return Cost::reference; // cost needs to be non-zero to add cast
[eb0aedb]151                                        } if ( src->get_qualifiers() < destAsRef->base->get_qualifiers() ) {
[7ebaa56]152                                                return Cost::safe; // cost needs to be higher than previous cast to differentiate adding qualifiers vs. keeping same
[89be1c68]153                                        } else {
154                                                return Cost::unsafe;
155                                        } // if
[eb0aedb]156                                } else if ( destAsRef->base->get_const() ) {
[5ccb10d]157                                        PRINT( std::cerr << "rvalue to const ref conversion" << std::endl; )
[89be1c68]158                                        // rvalue-to-const-reference conversion: T => const T &
159                                        return Cost::safe;
160                                } else {
[5ccb10d]161                                        PRINT( std::cerr << "rvalue to non-const reference conversion" << std::endl; )
[89be1c68]162                                        // rvalue-to-reference conversion: T => T &
163                                        return Cost::unsafe;
164                                } // if
165                        } // if
[5ccb10d]166                        PRINT( std::cerr << "attempting to convert from incompatible base type -- fail" << std::endl; )
[2463d0e]167                }
168                return Cost::infinity;
169        }
170
[0c6596f]171        Cost convertToReferenceCost( Type * src, ReferenceType * dest, const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ) {
[89be1c68]172                int sdepth = src->referenceDepth(), ddepth = dest->referenceDepth();
[eddb399]173                Cost cost = convertToReferenceCost( src, dest, sdepth-ddepth, indexer, env, func );
174                PRINT( std::cerr << "convertToReferenceCost result: " << cost << std::endl; )
175                return cost;
[89be1c68]176        }
177
[721cd19f]178        ConversionCost::ConversionCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
179                : dest( dest ), indexer( indexer ), cost( Cost::infinity ), env( env ), costFunc( costFunc ) {
[a32b204]180        }
[cdcddfe1]181#if 0
[51b7345]182/*
[a32b204]183            Old
184            ===
185           Double
186             |
187           Float
188             |
189           ULong
190           /   \
191        UInt    Long
192           \   /
193            Int
194             |
195           Ushort
196             |
197           Short
198             |
199           Uchar
200           /   \
201        Schar   Char
202
203                                New
204                                ===
205                       +-----LongDoubleComplex--+
206           LongDouble--+          |             +-LongDoubleImag
207             |         +---DoubleComplex---+         |
208           Double------+        |          +----DoubleImag
209             |           +-FloatComplex-+            |
210           Float---------+              +-------FloatImag
211             |
212          ULongLong
213             |
214          LongLong
215             |
216           ULong
217           /   \
218        UInt    Long
219           \   /
220            Int
221             |
222           Ushort
223             |
224           Short
225             |
226           Uchar
227           /   \
228        Schar   Char
229           \   /
230            Bool
[51b7345]231*/
232
[4ee3b0c1]233        static const int costMatrix[][ BasicType::NUMBER_OF_BASIC_TYPES ] = {
234        /* Src \ Dest:  Bool    Char    SChar   UChar   Short   UShort  Int     UInt    Long    ULong   LLong   ULLong  Float   Double  LDbl    FCplex  DCplex  LDCplex FImag   DImag   LDImag  I128,   U128, F80, F128 */
235                /* Bool */      { 0,    1,              1,              2,              3,              4,              5,              6,              6,              7,              8,              9,              12,             13,             14,             12,             13,             14,             -1,             -1,             -1,             10,             11,       14,   15},
236                /* Char */      { -1,   0,              -1,             1,              2,              3,              4,              5,              5,              6,              7,              8,              11,             12,             13,             11,             12,             13,             -1,             -1,             -1,             9,              10,       13,   14},
237                /* SChar */ { -1,       -1,             0,              1,              2,              3,              4,              5,              5,              6,              7,              8,              11,             12,             13,             11,             12,             13,             -1,             -1,             -1,             9,              10,       13,   14},
238                /* UChar */ { -1,       -1,             -1,             0,              1,              2,              3,              4,              4,              5,              6,              7,              10,             11,             12,             10,             11,             12,             -1,             -1,             -1,             8,              9,        12,   13},
239                /* Short */ { -1,       -1,             -1,             -1,             0,              1,              2,              3,              3,              4,              5,              6,              9,              10,             11,             9,              10,             11,             -1,             -1,             -1,             7,              8,        11,   12},
240                /* UShort */{ -1,       -1,             -1,             -1,             -1,             0,              1,              2,              2,              3,              4,              5,              8,              9,              10,             8,              9,              10,             -1,             -1,             -1,             6,              7,        10,   11},
241                /* Int */       { -1,   -1,             -1,             -1,             -1,             -1,             0,              1,              1,              2,              3,              4,              7,              8,              9,              7,              8,              9,              -1,             -1,             -1,             5,              6,        9,    10},
242                /* UInt */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             1,              2,              3,              6,              7,              8,              6,              7,              8,              -1,             -1,             -1,             4,              5,        8,    9},
243                /* Long */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              3,              6,              7,              8,              6,              7,              8,              -1,             -1,             -1,             4,              5,        8,    9},
244                /* ULong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              5,              6,              7,              5,              6,              7,              -1,             -1,             -1,             3,              4,        7,    8},
245                /* LLong */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              4,              5,              6,              4,              5,              6,              -1,             -1,             -1,             2,              3,        6,    7},
246                /* ULLong */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              3,              4,              5,              3,              4,              5,              -1,             -1,             -1,             1,              2,        5,    6},
[201aeb9]247
[4ee3b0c1]248                /* Float */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              1,              2,              3,              -1,             -1,             -1,             -1,             -1,       2,    3},
249                /* Double */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             1,              2,              -1,             -1,             -1,             -1,             -1,       1,    2},
250                /* LDbl */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             1,              -1,             -1,             -1,             -1,             -1,       -1,   1},
251                /* FCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              2,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
252                /* DCplex */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              1,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
253                /* LDCplex */{ -1,      -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             0,              -1,             -1,             -1,             -1,             -1,       -1,   -1},
254                /* FImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              0,              1,              2,              -1,             -1,       -1,   -1},
255                /* DImag */ { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              -1,             0,              1,              -1,             -1,       -1,   -1},
256                /* LDImag */{ -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             0,              -1,             -1,       -1,   -1},
[201aeb9]257
[4ee3b0c1]258                /* I128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             2,              3,              4,              3,              4,              5,              -1,             -1,             -1,             0,              1,        4,    4},
259                /* U128 */  { -1,       -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              2,              3,              2,              3,              4,              -1,             -1,             -1,             -1,             0,        3,    3},
260
261                /* F80 */       { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             1,              -1,             -1,             -1,             -1,             -1,       0,    1},
262                /* F128 */      { -1,   -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             -1,             1,              -1,             -1,             -1,             -1,             -1,       -1,   0},
[a32b204]263        };
[4ee3b0c1]264        static_assert(
265                sizeof(costMatrix)/sizeof(costMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES,
266                "Each basic type kind should have a corresponding row in the cost matrix"
267        );
[cdcddfe1]268#endif
[4ee3b0c1]269
[cdcddfe1]270        /* EXTENDED INTEGRAL RANK HIERARCHY (root to leaves)
271                    _Bool
272        Char    SignedChar    UnsignedChar
273                ShortSignedInt    ShortUnsignedInt
274                SignedInt    UnsignedInt
275                LongSignedInt    LongUnsignedInt
276                LongLongSignedInt    LongLongUnsignedInt
277                SignedInt128    UnsignedInt128
278                _Float16    _Float16Complex
279                _Float32    _Float32Complex
280                Float    FloatComplex
281                _Float32x    _Float32xComplex
282                _Float64    _Float64Complex
283                Double    DoubleComplex
284                _Float64x    _Float64xComplex
285                    __float80
286                _Float128    _Float128Complex
287                    __float128
288                LongDouble    LongDoubleComplex
289                _Float128x    _Float128xComplex
290        */
291
292        static const int costMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // path length from root to node
293                /*          B    C   SC   UC   SI  SUI    I   UI   LI  LUI  LLI LLUI   IB  UIB  _FH  _FH   _F  _FC    F   FC  _FX _FXC   FD _FDC    D   DC F80X _FDXC  F80  _FB _FLDC   FB   LD  LDC _FBX _FLDXC */
294                /*     B*/  0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  14,  15,  15,  16,  17,  16,  18,  17, 
295                /*     C*/ -1,   0,   1,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  14,  15,  16,  15,  17,  16, 
296                /*    SC*/ -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  14,  15,  16,  15,  17,  16, 
297                /*    UC*/ -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  14,  15,  16,  15,  17,  16, 
298                /*    SI*/ -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  15,  14,  16,  15, 
299                /*   SUI*/ -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  13,  14,  15,  14,  16,  15, 
300                /*     I*/ -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  14,  13,  15,  14, 
301                /*    UI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  12,  13,  14,  13,  15,  14, 
302                /*    LI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  13,  12,  14,  13, 
303                /*   LUI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  11,  12,  13,  12,  14,  13, 
304                /*   LLI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  12,  11,  13,  12, 
305                /*  LLUI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  10,  11,  12,  11,  13,  12, 
306                /*    IB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  11,  10,  12,  11, 
307                /*   UIB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,   9,  10,  11,  10,  12,  11, 
308                /*   _FH*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   8,   9,  10,   9,  11,  10, 
309                /*   _FH*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,   2,  -1,   3,  -1,   4,  -1,   5,  -1,   6,  -1,  -1,   7,  -1,  -1,   8,  -1,   9, 
310                /*    _F*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   7,   8,   9,   8,  10,   9, 
311                /*   _FC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,   2,  -1,   3,  -1,   4,  -1,   5,  -1,  -1,   6,  -1,  -1,   7,  -1,   8, 
312                /*     F*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   6,   7,   8,   7,   9,   8, 
313                /*    FC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,   2,  -1,   3,  -1,   4,  -1,  -1,   5,  -1,  -1,   6,  -1,   7, 
314                /*   _FX*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   5,   6,   7,   6,   8,   7, 
315                /*  _FXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,   2,  -1,   3,  -1,  -1,   4,  -1,  -1,   5,  -1,   6, 
316                /*    FD*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   4,   5,   6,   5,   7,   6, 
317                /*  _FDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,   2,  -1,  -1,   3,  -1,  -1,   4,  -1,   5, 
318                /*     D*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3,   4,   5,   4,   6,   5, 
319                /*    DC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1,  -1,  -1,   2,  -1,  -1,   3,  -1,   4, 
320                /*  F80X*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   4,   3,   5,   4, 
321                /* _FDXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,  -1,   1,  -1,  -1,   2,  -1,   3, 
322                /*   F80*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   1,   0,   1,   2,   2,   3,   3,   4,   4, 
323                /*   _FB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2,   2,   3,   3, 
324                /* _FLDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,  -1,   1,  -1,   2, 
325                /*    FB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   1,   0,   1,   2,   2,   3, 
326                /*    LD*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   1,   2, 
327                /*   LDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   1, 
328                /*  _FBX*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1, 
329                /*_FLDXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0, 
330        };
331        static_assert(
332                sizeof(costMatrix)/sizeof(costMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES,
333                "Missing row in the cost matrix"
334        );
335
336        static const int signMatrix[BasicType::NUMBER_OF_BASIC_TYPES][BasicType::NUMBER_OF_BASIC_TYPES] = { // number of sign changes in safe conversion
337                /*          B    C   SC   UC   SI  SUI    I   UI   LI  LUI  LLI LLUI   IB  UIB  _FH  _FH   _F  _FC    F   FC  _FX _FXC   FD _FDC    D   DC F80X _FDXC  F80  _FB _FLDC   FB   LD  LDC _FBX _FLDXC */
338                /*     B*/  0,   0,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
339                /*     C*/ -1,   0,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
340                /*    SC*/ -1,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
341                /*    UC*/ -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
342                /*    SI*/ -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
343                /*   SUI*/ -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
344                /*     I*/ -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
345                /*    UI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
346                /*    LI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
347                /*   LUI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
348                /*   LLI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
349                /*  LLUI*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
350                /*    IB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
351                /*   UIB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
352                /*   _FH*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
353                /*   _FH*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
354                /*    _F*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
355                /*   _FC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
356                /*     F*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
357                /*    FC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
358                /*   _FX*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
359                /*  _FXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
360                /*    FD*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
361                /*  _FDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
362                /*     D*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
363                /*    DC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
364                /*  F80X*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
365                /* _FDXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
366                /*   F80*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0,   0,   0, 
367                /*   _FB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0,   0, 
368                /* _FLDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,  -1,   0,  -1,   0, 
369                /*    FB*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0,   0,   0, 
370                /*    LD*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0,   0,   0, 
371                /*   LDC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,  -1,   0, 
372                /*  _FBX*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0,   0, 
373                /*_FLDXC*/ -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,  -1,   0, 
374        };
375        static_assert(
376                sizeof(signMatrix)/sizeof(signMatrix[0][0]) == BasicType::NUMBER_OF_BASIC_TYPES*BasicType::NUMBER_OF_BASIC_TYPES,
377                "Missing row in the sign matrix"
378        );
[a32b204]379
[bd0b6b62]380        void ConversionCost::postvisit( VoidType * ) {
[a32b204]381                cost = Cost::infinity;
382        }
383
[bd0b6b62]384        void ConversionCost::postvisit(BasicType *basicType) {
[a32b204]385                if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
386                        int tableResult = costMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ];
387                        if ( tableResult == -1 ) {
[89be1c68]388                                cost = Cost::unsafe;
[a32b204]389                        } else {
[89be1c68]390                                cost = Cost::zero;
391                                cost.incSafe( tableResult );
[cdcddfe1]392                                cost.incSign( signMatrix[ basicType->get_kind() ][ destAsBasic->get_kind() ] );
[a32b204]393                        } // if
[a436947]394                } else if ( dynamic_cast< EnumInstType *>( dest ) ) {
395                        // xxx - not positive this is correct, but appears to allow casting int => enum
[89be1c68]396                        cost = Cost::unsafe;
[89e6ffc]397                } // if
[98278b3a]398                // no cases for zero_t/one_t because it should not be possible to convert int, etc. to zero_t/one_t.
[a32b204]399        }
400
[bd0b6b62]401        void ConversionCost::postvisit( PointerType * pointerType ) {
[a32b204]402                if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
[6e027d6]403                        PRINT( std::cerr << pointerType << " ===> " << destAsPtr << std::endl; )
[eb0aedb]404                        Type::Qualifiers tq1 = pointerType->base->get_qualifiers();
405                        Type::Qualifiers tq2 = destAsPtr->base->get_qualifiers();
406                        if ( tq1 <= tq2 && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
[6e027d6]407                                PRINT( std::cerr << " :: compatible and good qualifiers" << std::endl; )
[cb43451]408                                if ( tq1 == tq2 ) {
409                                        // types are the same
410                                        cost = Cost::zero;
411                                } else {
412                                        // types are the same, except otherPointer has more qualifiers
413                                        cost = Cost::safe;
[cdcddfe1]414                                } // if
[b8b075cd]415                        } else {
[b0837e4]416                                int assignResult = ptrsAssignable( pointerType->base, destAsPtr->base, env );
[5ccb10d]417                                PRINT( std::cerr << " :: " << assignResult << std::endl; )
[b8b075cd]418                                if ( assignResult > 0 && tq1 <= tq2 ) {
419                                        // xxx - want the case where qualifiers are added to be more expensive than the case where qualifiers are the same. Is 1 safe vs. 2 safe correct?
420                                        if ( tq1 == tq2 ) {
421                                                cost = Cost::safe;
422                                        } else if ( tq1 < tq2 ) {
423                                                cost = Cost::safe+Cost::safe;
424                                        }
[b0837e4]425                                } else if ( assignResult < 0 ) {
[89be1c68]426                                        cost = Cost::unsafe;
[a32b204]427                                } // if
[cb43451]428                                // assignResult == 0 means Cost::Infinity
[a32b204]429                        } // if
[98278b3a]430                        // case case for zero_t because it should not be possible to convert pointers to zero_t.
[a32b204]431                } // if
432        }
433
[bd0b6b62]434        void ConversionCost::postvisit( ArrayType * ) {}
[2463d0e]435
[bd0b6b62]436        void ConversionCost::postvisit( ReferenceType * refType ) {
[2463d0e]437                // Note: dest can never be a reference, since it would have been caught in an earlier check
438                assert( ! dynamic_cast< ReferenceType * >( dest ) );
439                // convert reference to rvalue: cv T1 & => T2
440                // recursively compute conversion cost from T1 to T2.
441                // cv can be safely dropped because of 'implicit dereference' behavior.
[721cd19f]442                cost = costFunc( refType->base, dest, indexer, env );
[150ec33]443                if ( refType->base->get_qualifiers() == dest->get_qualifiers() ) {
[cb43451]444                        cost.incReference();  // prefer exact qualifiers
[150ec33]445                } else if ( refType->base->get_qualifiers() < dest->get_qualifiers() ) {
[cb43451]446                        cost.incSafe(); // then gaining qualifiers
447                } else {
448                        cost.incUnsafe(); // lose qualifiers as last resort
449                }
[5ccb10d]450                PRINT( std::cerr << refType << " ==> " << dest << " " << cost << std::endl; )
[2463d0e]451        }
452
[bd0b6b62]453        void ConversionCost::postvisit( FunctionType * ) {}
[a32b204]454
[bd0b6b62]455        void ConversionCost::postvisit( StructInstType * inst ) {
[a32b204]456                if ( StructInstType *destAsInst = dynamic_cast< StructInstType* >( dest ) ) {
[150ec33]457                        if ( inst->name == destAsInst->name ) {
[a32b204]458                                cost = Cost::zero;
459                        } // if
460                } // if
461        }
462
[bd0b6b62]463        void ConversionCost::postvisit( UnionInstType * inst ) {
[150ec33]464                if ( UnionInstType *destAsInst = dynamic_cast< UnionInstType* >( dest ) ) {
465                        if ( inst->name == destAsInst->name ) {
[a32b204]466                                cost = Cost::zero;
467                        } // if
468                } // if
469        }
470
[bd0b6b62]471        void ConversionCost::postvisit( EnumInstType * ) {
[a32b204]472                static Type::Qualifiers q;
473                static BasicType integer( q, BasicType::SignedInt );
[721cd19f]474                cost = costFunc( &integer, dest, indexer, env );  // safe if dest >= int
[89be1c68]475                if ( cost < Cost::unsafe ) {
[a32b204]476                        cost.incSafe();
477                } // if
478        }
479
[bd0b6b62]480        void ConversionCost::postvisit( TraitInstType * ) {}
[a32b204]481
[bd0b6b62]482        void ConversionCost::postvisit( TypeInstType *inst ) {
[00ac42e]483                if ( const EqvClass *eqvClass = env.lookup( inst->name ) ) {
484                        cost = costFunc( eqvClass->type, dest, indexer, env );
[a32b204]485                } else if ( TypeInstType *destAsInst = dynamic_cast< TypeInstType* >( dest ) ) {
[eb0aedb]486                        if ( inst->name == destAsInst->name ) {
[a32b204]487                                cost = Cost::zero;
488                        }
[00ac42e]489                } else if ( NamedTypeDecl *namedType = indexer.lookupType( inst->name ) ) {
[a32b204]490                        TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
491                        // all typedefs should be gone by this point
492                        assert( type );
[eb0aedb]493                        if ( type->base ) {
[721cd19f]494                                cost = costFunc( type->base, dest, indexer, env ) + Cost::safe;
[a32b204]495                        } // if
496                } // if
497        }
498
[bd0b6b62]499        void ConversionCost::postvisit( TupleType * tupleType ) {
[89be1c68]500                Cost c = Cost::zero;
[b0837e4]501                if ( TupleType * destAsTuple = dynamic_cast< TupleType * >( dest ) ) {
[eb0aedb]502                        std::list< Type * >::const_iterator srcIt = tupleType->types.begin();
503                        std::list< Type * >::const_iterator destIt = destAsTuple->types.begin();
504                        while ( srcIt != tupleType->types.end() && destIt != destAsTuple->types.end() ) {
[721cd19f]505                                Cost newCost = costFunc( *srcIt++, *destIt++, indexer, env );
[a32b204]506                                if ( newCost == Cost::infinity ) {
507                                        return;
508                                } // if
509                                c += newCost;
510                        } // while
[eb0aedb]511                        if ( destIt != destAsTuple->types.end() ) {
[a32b204]512                                cost = Cost::infinity;
513                        } else {
514                                cost = c;
515                        } // if
516                } // if
517        }
[44b7088]518
[bd0b6b62]519        void ConversionCost::postvisit( VarArgsType * ) {
[90c3b1c]520                if ( dynamic_cast< VarArgsType* >( dest ) ) {
[44b7088]521                        cost = Cost::zero;
522                }
523        }
[89e6ffc]524
[bd0b6b62]525        void ConversionCost::postvisit( ZeroType * ) {
[b0837e4]526                if ( dynamic_cast< ZeroType * >( dest ) ) {
[89e6ffc]527                        cost = Cost::zero;
528                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
529                        // copied from visit(BasicType*) for signed int, but +1 for safe conversions
530                        int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
531                        if ( tableResult == -1 ) {
[89be1c68]532                                cost = Cost::unsafe;
[89e6ffc]533                        } else {
[89be1c68]534                                cost = Cost::zero;
535                                cost.incSafe( tableResult + 1 );
[cdcddfe1]536                                cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] );
537                        } // if
[89e6ffc]538                } else if ( dynamic_cast< PointerType* >( dest ) ) {
[89be1c68]539                        cost = Cost::safe;
[cdcddfe1]540                } // if
[89e6ffc]541        }
542
[bd0b6b62]543        void ConversionCost::postvisit( OneType * ) {
[b0837e4]544                if ( dynamic_cast< OneType * >( dest ) ) {
[89e6ffc]545                        cost = Cost::zero;
546                } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
547                        // copied from visit(BasicType*) for signed int, but +1 for safe conversions
548                        int tableResult = costMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ];
549                        if ( tableResult == -1 ) {
[89be1c68]550                                cost = Cost::unsafe;
[89e6ffc]551                        } else {
[89be1c68]552                                cost = Cost::zero;
553                                cost.incSafe( tableResult + 1 );
[cdcddfe1]554                                cost.incSign( signMatrix[ BasicType::SignedInt ][ destAsBasic->get_kind() ] );
555                        } // if
556                } // if
[89e6ffc]557        }
[51b7345]558} // namespace ResolvExpr
[a32b204]559
560// Local Variables: //
561// tab-width: 4 //
562// mode: c++ //
563// compile-command: "make install" //
564// End: //
Note: See TracBrowser for help on using the repository browser.