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.cpp --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Sun May 17 06:57:43 2015
|
---|
11 | // Last Modified By : Andrew Beach
|
---|
12 | // Last Modified On : Tue Oct 4 15:00:00 2019
|
---|
13 | // Update Count : 9
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "CastCost.hpp"
|
---|
17 |
|
---|
18 | #include <cassert> // for assert
|
---|
19 |
|
---|
20 | #include "AST/Print.hpp"
|
---|
21 | #include "AST/SymbolTable.hpp"
|
---|
22 | #include "AST/Type.hpp"
|
---|
23 | #include "AST/TypeEnvironment.hpp"
|
---|
24 | #include "ConversionCost.hpp" // for ConversionCost
|
---|
25 | #include "Cost.hpp" // for Cost, Cost::infinity
|
---|
26 | #include "ResolvExpr/ConversionCost.hpp" // for conversionCost
|
---|
27 | #include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable
|
---|
28 | #include "ResolvExpr/Unify.hpp" // for typesCompatibleIgnoreQualifiers
|
---|
29 |
|
---|
30 | #if 0
|
---|
31 | #define PRINT(x) x
|
---|
32 | #else
|
---|
33 | #define PRINT(x)
|
---|
34 | #endif
|
---|
35 |
|
---|
36 | namespace ResolvExpr {
|
---|
37 |
|
---|
38 | Cost castCost(
|
---|
39 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
|
---|
40 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
|
---|
41 | );
|
---|
42 |
|
---|
43 | namespace {
|
---|
44 | struct CastCost : public ConversionCost {
|
---|
45 | using ConversionCost::previsit;
|
---|
46 | using ConversionCost::postvisit;
|
---|
47 |
|
---|
48 | CastCost(
|
---|
49 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab,
|
---|
50 | const ast::TypeEnvironment & env, CostCalculation costFunc )
|
---|
51 | : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {}
|
---|
52 |
|
---|
53 | void postvisit( const ast::EnumInstType * enumInst ) {
|
---|
54 | cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env );
|
---|
55 | if ( enumInst->base->isTyped() ) {
|
---|
56 | auto baseConversionCost =
|
---|
57 | castCost( enumInst->base->base, dst, srcIsLvalue, symtab, env );
|
---|
58 | cost = baseConversionCost < cost? baseConversionCost: cost;
|
---|
59 | }
|
---|
60 | static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
|
---|
61 | Cost intCost = costCalc( integer, dst, srcIsLvalue, symtab, env );
|
---|
62 | intCost.incSafe();
|
---|
63 | cost = intCost < cost? intCost: cost;
|
---|
64 | }
|
---|
65 |
|
---|
66 | void postvisit( const ast::BasicType * basicType ) {
|
---|
67 | auto ptr = dynamic_cast< const ast::PointerType * >( dst );
|
---|
68 | if ( ptr && basicType->isInteger() ) {
|
---|
69 | // needed for, e.g. unsigned long => void *
|
---|
70 | cost = Cost::unsafe;
|
---|
71 | } else {
|
---|
72 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
|
---|
73 | if ( Cost::unsafe < cost ) {
|
---|
74 | if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
|
---|
75 | cost = Cost::unsafe;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | }
|
---|
79 | }
|
---|
80 |
|
---|
81 | void postvisit( const ast::ZeroType * zero ) {
|
---|
82 | cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );
|
---|
83 | if ( Cost::unsafe < cost ) {
|
---|
84 | if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
|
---|
85 | cost = Cost::unsafe;
|
---|
86 | }
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | void postvisit( const ast::OneType * one ) {
|
---|
91 | cost = conversionCost( one, dst, srcIsLvalue, symtab, env );
|
---|
92 | if ( Cost::unsafe < cost ) {
|
---|
93 | if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
|
---|
94 | cost = Cost::unsafe;
|
---|
95 | }
|
---|
96 | }
|
---|
97 | }
|
---|
98 |
|
---|
99 | void postvisit( const ast::PointerType * pointerType ) {
|
---|
100 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) {
|
---|
101 | if (
|
---|
102 | pointerType->qualifiers <= ptr->qualifiers
|
---|
103 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
|
---|
104 | ) {
|
---|
105 | cost = Cost::safe;
|
---|
106 | } else {
|
---|
107 | ast::TypeEnvironment newEnv{ env };
|
---|
108 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
|
---|
109 | newEnv.add( wParams->forall );
|
---|
110 | }
|
---|
111 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv );
|
---|
112 | if ( castResult > 0 ) {
|
---|
113 | cost = Cost::safe;
|
---|
114 | } else if ( castResult < 0 ) {
|
---|
115 | cost = Cost::infinity;
|
---|
116 | }
|
---|
117 | }
|
---|
118 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) {
|
---|
119 | if ( basic->isInteger() ) {
|
---|
120 | // necessary for, e.g. void * => unsigned long
|
---|
121 | cost = Cost::unsafe;
|
---|
122 | }
|
---|
123 | }
|
---|
124 | }
|
---|
125 | };
|
---|
126 |
|
---|
127 | } // anonymous namespace
|
---|
128 |
|
---|
129 | Cost castCost(
|
---|
130 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
|
---|
131 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
|
---|
132 | ) {
|
---|
133 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
|
---|
134 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
|
---|
135 | // check cast cost against bound type, if present
|
---|
136 | if ( eqvClass->bound ) {
|
---|
137 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
|
---|
138 | } else {
|
---|
139 | return Cost::infinity;
|
---|
140 | }
|
---|
141 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) {
|
---|
142 | // all typedefs should be gone by now
|
---|
143 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named );
|
---|
144 | if ( type->base ) {
|
---|
145 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
|
---|
146 | }
|
---|
147 | }
|
---|
148 | }
|
---|
149 |
|
---|
150 | PRINT(
|
---|
151 | std::cerr << "castCost ::: src is ";
|
---|
152 | ast::print( std::cerr, src );
|
---|
153 | std::cerr << std::endl << "dest is ";
|
---|
154 | ast::print( std::cerr, dst );
|
---|
155 | std::cerr << std::endl << "env is" << std::endl;
|
---|
156 | ast::print( std::cerr, env, 2 );
|
---|
157 | )
|
---|
158 |
|
---|
159 | if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
|
---|
160 | PRINT( std::cerr << "compatible!" << std::endl; )
|
---|
161 | if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
|
---|
162 | return Cost::spec;
|
---|
163 | }
|
---|
164 | return Cost::zero;
|
---|
165 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
|
---|
166 | return Cost::safe;
|
---|
167 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) {
|
---|
168 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
|
---|
169 | return convertToReferenceCost(
|
---|
170 | src, refType, srcIsLvalue, symtab, env, ptrsCastable );
|
---|
171 | } else {
|
---|
172 | ast::Pass< CastCost > converter(
|
---|
173 | dst, srcIsLvalue, symtab, env, castCost );
|
---|
174 | src->accept( converter );
|
---|
175 | return converter.core.cost;
|
---|
176 | }
|
---|
177 | }
|
---|
178 |
|
---|
179 | } // namespace ResolvExpr
|
---|
180 |
|
---|
181 | // Local Variables: //
|
---|
182 | // tab-width: 4 //
|
---|
183 | // mode: c++ //
|
---|
184 | // compile-command: "make install" //
|
---|
185 | // End: //
|
---|