source: src/ResolvExpr/CastCost.cc@ f22b170b

Last change on this file since f22b170b was 0bd3faf, checked in by Andrew Beach <ajbeach@…>, 23 months ago

Removed forward declarations missed in the BaseSyntaxNode removal. Removed code and modified names to support two versions of the ast.

  • Property mode set to 100644
File size: 4.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//
[0b150ec]7// CastCost.cc --
[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"
[ea6332d]24#include "ConversionCost.h" // for ConversionCost
25#include "Cost.h" // for Cost, Cost::infinity
[5bf3976]26#include "ResolvExpr/ConversionCost.h" // for conversionCost
27#include "ResolvExpr/PtrsCastable.hpp" // for ptrsCastable
[e563edf]28#include "ResolvExpr/Unify.h" // 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 );
[3c89751]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
[251ce80]62 && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
[3c89751]63 ) {
64 cost = Cost::safe;
65 } else {
66 ast::TypeEnvironment newEnv{ env };
[361bf01]67 if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
[3c89751]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 };
[cf32116]85
[3c89751]86} // anonymous namespace
87
[7870799]88Cost castCost(
[cf32116]89 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
90 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
[3c89751]91) {
92 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
[3e5dd913]93 if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
[3c89751]94 // check cast cost against bound type, if present
95 if ( eqvClass->bound ) {
[cf32116]96 return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
[3c89751]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 ) {
[cf32116]104 return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
[3c89751]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
[251ce80]118 if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
[3c89751]119 PRINT( std::cerr << "compatible!" << std::endl; )
[46da46b]120 if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
121 return Cost::spec;
122 }
[c8e4d2f8]123 return Cost::zero;
[3c89751]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; )
[7870799]128 return convertToReferenceCost(
[0bd3faf]129 src, refType, srcIsLvalue, symtab, env, ptrsCastable );
[3c89751]130 } else {
[0bd3faf]131 ast::Pass< CastCost > converter(
132 dst, srcIsLvalue, symtab, env, castCost );
[3c89751]133 src->accept( converter );
[7ff3e522]134 return converter.core.cost;
[c8e4d2f8]135 }
[3c89751]136}
137
[51b73452]138} // namespace ResolvExpr
[a32b204]139
140// Local Variables: //
141// tab-width: 4 //
142// mode: c++ //
143// compile-command: "make install" //
144// End: //
Note: See TracBrowser for help on using the repository browser.