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 | // CastCost.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Sun May 17 06:57:43 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue Feb 2 15:34:36 2016
|
---|
13 | // Update Count : 7
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include <cassert> // for assert
|
---|
17 |
|
---|
18 | #include "ConversionCost.h" // for ConversionCost
|
---|
19 | #include "Cost.h" // for Cost, Cost::infinity
|
---|
20 | #include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass
|
---|
21 | #include "SymTab/Indexer.h" // for Indexer
|
---|
22 | #include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl
|
---|
23 | #include "SynTree/Type.h" // for PointerType, Type, TypeInstType
|
---|
24 | #include "typeops.h" // for typesCompatibleIgnoreQualifiers
|
---|
25 |
|
---|
26 | #if 0
|
---|
27 | #define PRINT(x) x
|
---|
28 | #else
|
---|
29 | #define PRINT(x)
|
---|
30 | #endif
|
---|
31 |
|
---|
32 | namespace ResolvExpr {
|
---|
33 | struct CastCost : public ConversionCost {
|
---|
34 | public:
|
---|
35 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
|
---|
36 |
|
---|
37 | using ConversionCost::previsit;
|
---|
38 | using ConversionCost::postvisit;
|
---|
39 | void postvisit( BasicType * basicType );
|
---|
40 | void postvisit( PointerType * pointerType );
|
---|
41 | };
|
---|
42 |
|
---|
43 | Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
44 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
45 | if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
|
---|
46 | if ( eqvClass->type ) {
|
---|
47 | return castCost( src, eqvClass->type, indexer, env );
|
---|
48 | } else {
|
---|
49 | return Cost::infinity;
|
---|
50 | }
|
---|
51 | } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) {
|
---|
52 | // all typedefs should be gone by this point
|
---|
53 | TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
|
---|
54 | if ( type->base ) {
|
---|
55 | return castCost( src, type->base, indexer, env ) + Cost::safe;
|
---|
56 | } // if
|
---|
57 | } // if
|
---|
58 | } // if
|
---|
59 |
|
---|
60 | PRINT(
|
---|
61 | std::cerr << "castCost ::: src is ";
|
---|
62 | src->print( std::cerr );
|
---|
63 | std::cerr << std::endl << "dest is ";
|
---|
64 | dest->print( std::cerr );
|
---|
65 | std::cerr << std::endl << "env is" << std::endl;
|
---|
66 | env.print( std::cerr, 8 );
|
---|
67 | )
|
---|
68 |
|
---|
69 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
|
---|
70 | PRINT( std::cerr << "compatible!" << std::endl; )
|
---|
71 | return Cost::zero;
|
---|
72 | } else if ( dynamic_cast< VoidType* >( dest ) ) {
|
---|
73 | return Cost::safe;
|
---|
74 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
|
---|
75 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
|
---|
76 | return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
|
---|
77 | return ptrsCastable( t1, t2, env, indexer );
|
---|
78 | });
|
---|
79 | } else {
|
---|
80 | #warning cast on castCost artifact of having two functions, remove when port done
|
---|
81 | PassVisitor<CastCost> converter(
|
---|
82 | dest, indexer, env,
|
---|
83 | (Cost (*)( Type *, Type *, const SymTab::Indexer &, const TypeEnvironment & ))
|
---|
84 | castCost );
|
---|
85 | src->accept( converter );
|
---|
86 | if ( converter.pass.get_cost() == Cost::infinity ) {
|
---|
87 | return Cost::infinity;
|
---|
88 | } else {
|
---|
89 | // xxx - why are we adding cost 0 here?
|
---|
90 | return converter.pass.get_cost() + Cost::zero;
|
---|
91 | } // if
|
---|
92 | } // if
|
---|
93 | }
|
---|
94 |
|
---|
95 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
|
---|
96 | : ConversionCost( dest, indexer, env, costFunc ) {
|
---|
97 | }
|
---|
98 |
|
---|
99 | void CastCost::postvisit( BasicType *basicType ) {
|
---|
100 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
|
---|
101 | if ( destAsPointer && basicType->isInteger() ) {
|
---|
102 | // necessary for, e.g. unsigned long => void*
|
---|
103 | cost = Cost::unsafe;
|
---|
104 | } else {
|
---|
105 | cost = conversionCost( basicType, dest, indexer, env );
|
---|
106 | } // if
|
---|
107 | }
|
---|
108 |
|
---|
109 | void CastCost::postvisit( PointerType *pointerType ) {
|
---|
110 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
|
---|
111 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
|
---|
112 | cost = Cost::safe;
|
---|
113 | } else {
|
---|
114 | TypeEnvironment newEnv( env );
|
---|
115 | newEnv.add( pointerType->forall );
|
---|
116 | newEnv.add( pointerType->base->forall );
|
---|
117 | int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
|
---|
118 | if ( castResult > 0 ) {
|
---|
119 | cost = Cost::safe;
|
---|
120 | } else if ( castResult < 0 ) {
|
---|
121 | cost = Cost::infinity;
|
---|
122 | } // if
|
---|
123 | } // if
|
---|
124 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
|
---|
125 | if ( destAsBasic->isInteger() ) {
|
---|
126 | // necessary for, e.g. void* => unsigned long
|
---|
127 | cost = Cost::unsafe;
|
---|
128 | } // if
|
---|
129 | }
|
---|
130 | }
|
---|
131 |
|
---|
132 | Cost castCost(
|
---|
133 | const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
|
---|
134 | const ast::TypeEnvironment & env
|
---|
135 | ) {
|
---|
136 | #warning unimplmented
|
---|
137 | (void)src; (void)dst; (void)symtab; (void)env;
|
---|
138 | assert(false);
|
---|
139 | return Cost::zero;
|
---|
140 | }
|
---|
141 | } // namespace ResolvExpr
|
---|
142 |
|
---|
143 | // Local Variables: //
|
---|
144 | // tab-width: 4 //
|
---|
145 | // mode: c++ //
|
---|
146 | // compile-command: "make install" //
|
---|
147 | // End: //
|
---|