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 : 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.h" // for ConversionCost |
---|
25 | #include "Cost.h" // for Cost, Cost::infinity |
---|
26 | #include "ResolvExpr/ConversionCost.h" // for conversionCost |
---|
27 | #include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable |
---|
28 | #include "ResolvExpr/Unify.h" // 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 | namespace { |
---|
39 | struct CastCost : public ConversionCost { |
---|
40 | using ConversionCost::previsit; |
---|
41 | using ConversionCost::postvisit; |
---|
42 | |
---|
43 | CastCost( |
---|
44 | const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
45 | const ast::TypeEnvironment & env, CostCalculation costFunc ) |
---|
46 | : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {} |
---|
47 | |
---|
48 | void postvisit( const ast::BasicType * basicType ) { |
---|
49 | auto ptr = dynamic_cast< const ast::PointerType * >( dst ); |
---|
50 | if ( ptr && basicType->isInteger() ) { |
---|
51 | // needed for, e.g. unsigned long => void * |
---|
52 | cost = Cost::unsafe; |
---|
53 | } else { |
---|
54 | cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env ); |
---|
55 | } |
---|
56 | } |
---|
57 | |
---|
58 | void postvisit( const ast::PointerType * pointerType ) { |
---|
59 | if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) { |
---|
60 | if ( |
---|
61 | pointerType->qualifiers <= ptr->qualifiers |
---|
62 | && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env ) |
---|
63 | ) { |
---|
64 | cost = Cost::safe; |
---|
65 | } else { |
---|
66 | ast::TypeEnvironment newEnv{ env }; |
---|
67 | if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) { |
---|
68 | newEnv.add( wParams->forall ); |
---|
69 | } |
---|
70 | int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv ); |
---|
71 | if ( castResult > 0 ) { |
---|
72 | cost = Cost::safe; |
---|
73 | } else if ( castResult < 0 ) { |
---|
74 | cost = Cost::infinity; |
---|
75 | } |
---|
76 | } |
---|
77 | } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) { |
---|
78 | if ( basic->isInteger() ) { |
---|
79 | // necessary for, e.g. void * => unsigned long |
---|
80 | cost = Cost::unsafe; |
---|
81 | } |
---|
82 | } |
---|
83 | } |
---|
84 | }; |
---|
85 | |
---|
86 | } // anonymous namespace |
---|
87 | |
---|
88 | Cost castCost( |
---|
89 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
90 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env |
---|
91 | ) { |
---|
92 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) { |
---|
93 | if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) { |
---|
94 | // check cast cost against bound type, if present |
---|
95 | if ( eqvClass->bound ) { |
---|
96 | return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env ); |
---|
97 | } else { |
---|
98 | return Cost::infinity; |
---|
99 | } |
---|
100 | } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) { |
---|
101 | // all typedefs should be gone by now |
---|
102 | auto type = strict_dynamic_cast< const ast::TypeDecl * >( named ); |
---|
103 | if ( type->base ) { |
---|
104 | return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe; |
---|
105 | } |
---|
106 | } |
---|
107 | } |
---|
108 | |
---|
109 | PRINT( |
---|
110 | std::cerr << "castCost ::: src is "; |
---|
111 | ast::print( std::cerr, src ); |
---|
112 | std::cerr << std::endl << "dest is "; |
---|
113 | ast::print( std::cerr, dst ); |
---|
114 | std::cerr << std::endl << "env is" << std::endl; |
---|
115 | ast::print( std::cerr, env, 2 ); |
---|
116 | ) |
---|
117 | |
---|
118 | if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) { |
---|
119 | PRINT( std::cerr << "compatible!" << std::endl; ) |
---|
120 | if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) { |
---|
121 | return Cost::spec; |
---|
122 | } |
---|
123 | return Cost::zero; |
---|
124 | } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) { |
---|
125 | return Cost::safe; |
---|
126 | } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) { |
---|
127 | PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; ) |
---|
128 | return convertToReferenceCost( |
---|
129 | src, refType, srcIsLvalue, symtab, env, ptrsCastable ); |
---|
130 | } else { |
---|
131 | ast::Pass< CastCost > converter( |
---|
132 | dst, srcIsLvalue, symtab, env, castCost ); |
---|
133 | src->accept( converter ); |
---|
134 | return converter.core.cost; |
---|
135 | } |
---|
136 | } |
---|
137 | |
---|
138 | } // namespace ResolvExpr |
---|
139 | |
---|
140 | // Local Variables: // |
---|
141 | // tab-width: 4 // |
---|
142 | // mode: c++ // |
---|
143 | // compile-command: "make install" // |
---|
144 | // End: // |
---|