source: src/ResolvExpr/CastCost.cpp@ 8d26b7a

Last change on this file since 8d26b7a was c92bdcc, checked in by Andrew Beach <ajbeach@…>, 17 months ago

Updated the rest of the names in src/ (except for the generated files).

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