source: src/ResolvExpr/CastCost.cpp@ 4117761

Last change on this file since 4117761 was 5ccc733, checked in by JiadaL <j82liang@…>, 17 months ago

Fix the bug that C style enum cannot to use as an lvalue

  • Property mode set to 100644
File size: 5.8 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
[85855b0]38Cost castCost(
39 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
40 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
41);
42
[3c89751]43namespace {
[0bd3faf]44 struct CastCost : public ConversionCost {
45 using ConversionCost::previsit;
46 using ConversionCost::postvisit;
[3c89751]47
[0bd3faf]48 CastCost(
[cf32116]49 const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab,
[3c89751]50 const ast::TypeEnvironment & env, CostCalculation costFunc )
[0bd3faf]51 : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {}
[3c89751]52
[85855b0]53 void postvisit( const ast::EnumInstType * enumInst ) {
54 cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env );
[d3aa55e9]55
[5ccc733]56 if ( !enumInst->base->isCfa ) {
[d3aa55e9]57 static ast::ptr<ast::BasicType> integer = { new ast::BasicType( ast::BasicKind::SignedInt ) };
58 Cost intCost = costCalc( integer, dst, srcIsLvalue, symtab, env );
59 cost = intCost < cost? intCost: cost;
[5ccc733]60 } else if ( enumInst->base->isTyped() ) {
[d3aa55e9]61 auto baseConversionCost =
62 castCost( enumInst->base->base, dst, srcIsLvalue, symtab, env );
63 cost = baseConversionCost < cost? baseConversionCost: cost;
64 }
[85855b0]65 }
66
[3c89751]67 void postvisit( const ast::BasicType * basicType ) {
68 auto ptr = dynamic_cast< const ast::PointerType * >( dst );
69 if ( ptr && basicType->isInteger() ) {
70 // needed for, e.g. unsigned long => void *
71 cost = Cost::unsafe;
72 } else {
[cf32116]73 cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
[eb7586e]74 if ( Cost::unsafe < cost ) {
[5ccc733]75 if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
76 cost = Cost::unsafe;
[eb7586e]77 }
78 }
79 }
80 }
81
82 void postvisit( const ast::ZeroType * zero ) {
83 cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );
84 if ( Cost::unsafe < cost ) {
[5ccc733]85 if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
86 cost = Cost::unsafe;
[eb7586e]87 }
[3c89751]88 }
[eb7586e]89 }
90
91 void postvisit( const ast::OneType * one ) {
92 cost = conversionCost( one, dst, srcIsLvalue, symtab, env );
93 if ( Cost::unsafe < cost ) {
[5ccc733]94 if ( dynamic_cast<const ast::EnumInstType *>(dst)) {
95 cost = Cost::unsafe;
[eb7586e]96 }
97 }
[3c89751]98 }
99
100 void postvisit( const ast::PointerType * pointerType ) {
101 if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) {
102 if (
103 pointerType->qualifiers <= ptr->qualifiers
[251ce80]104 && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
[3c89751]105 ) {
106 cost = Cost::safe;
107 } else {
108 ast::TypeEnvironment newEnv{ env };
[361bf01]109 if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
[3c89751]110 newEnv.add( wParams->forall );
111 }
112 int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv );
113 if ( castResult > 0 ) {
114 cost = Cost::safe;
115 } else if ( castResult < 0 ) {
116 cost = Cost::infinity;
117 }
118 }
119 } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) {
120 if ( basic->isInteger() ) {
121 // necessary for, e.g. void * => unsigned long
122 cost = Cost::unsafe;
123 }
124 }
125 }
126 };
[cf32116]127
[3c89751]128} // anonymous namespace
129
[7870799]130Cost castCost(
[cf32116]131 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
132 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
[3c89751]133) {
134 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
[3e5dd913]135 if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
[3c89751]136 // check cast cost against bound type, if present
137 if ( eqvClass->bound ) {
[cf32116]138 return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
[3c89751]139 } else {
140 return Cost::infinity;
141 }
142 } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) {
143 // all typedefs should be gone by now
144 auto type = strict_dynamic_cast< const ast::TypeDecl * >( named );
145 if ( type->base ) {
[cf32116]146 return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
[3c89751]147 }
148 }
149 }
150
151 PRINT(
152 std::cerr << "castCost ::: src is ";
153 ast::print( std::cerr, src );
154 std::cerr << std::endl << "dest is ";
155 ast::print( std::cerr, dst );
156 std::cerr << std::endl << "env is" << std::endl;
157 ast::print( std::cerr, env, 2 );
158 )
159
[251ce80]160 if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
[3c89751]161 PRINT( std::cerr << "compatible!" << std::endl; )
[46da46b]162 if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
163 return Cost::spec;
164 }
[c8e4d2f8]165 return Cost::zero;
[3c89751]166 } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
167 return Cost::safe;
168 } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) {
169 PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
[7870799]170 return convertToReferenceCost(
[0bd3faf]171 src, refType, srcIsLvalue, symtab, env, ptrsCastable );
[3c89751]172 } else {
[0bd3faf]173 ast::Pass< CastCost > converter(
174 dst, srcIsLvalue, symtab, env, castCost );
[3c89751]175 src->accept( converter );
[7ff3e522]176 return converter.core.cost;
[c8e4d2f8]177 }
[3c89751]178}
179
[51b73452]180} // namespace ResolvExpr
[a32b204]181
182// Local Variables: //
183// tab-width: 4 //
184// mode: c++ //
185// compile-command: "make install" //
186// End: //
Note: See TracBrowser for help on using the repository browser.