source: src/ResolvExpr/CastCost.cc@ f5ec35a

Last change on this file since f5ec35a was c6b4432, checked in by Andrew Beach <ajbeach@…>, 2 years ago

Remove BaseSyntaxNode and clean-up.

  • Property mode set to 100644
File size: 5.1 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/typeops.h" // for ptrsCastable
29#include "ResolvExpr/Unify.h" // for typesCompatibleIgnoreQualifiers
[51b73452]30
[150ec33]31#if 0
32#define PRINT(x) x
33#else
34#define PRINT(x)
35#endif
[51b73452]36
37namespace ResolvExpr {
[c8e4d2f8]38
[3c89751]39namespace {
40 struct CastCost_new : public ConversionCost_new {
41 using ConversionCost_new::previsit;
42 using ConversionCost_new::postvisit;
43
[7870799]44 CastCost_new(
[cf32116]45 const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab,
[3c89751]46 const ast::TypeEnvironment & env, CostCalculation costFunc )
[cf32116]47 : ConversionCost_new( dst, srcIsLvalue, symtab, env, costFunc ) {}
[3c89751]48
49 void postvisit( const ast::BasicType * basicType ) {
50 auto ptr = dynamic_cast< const ast::PointerType * >( dst );
51 if ( ptr && basicType->isInteger() ) {
52 // needed for, e.g. unsigned long => void *
53 cost = Cost::unsafe;
54 } else {
[cf32116]55 cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
[3c89751]56 }
57 }
58
59 void postvisit( const ast::PointerType * pointerType ) {
60 if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) {
61 if (
62 pointerType->qualifiers <= ptr->qualifiers
[251ce80]63 && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
[3c89751]64 ) {
65 cost = Cost::safe;
66 } else {
67 ast::TypeEnvironment newEnv{ env };
[361bf01]68 if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
[3c89751]69 newEnv.add( wParams->forall );
70 }
71 int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv );
72 if ( castResult > 0 ) {
73 cost = Cost::safe;
74 } else if ( castResult < 0 ) {
75 cost = Cost::infinity;
76 }
77 }
78 } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) {
79 if ( basic->isInteger() ) {
80 // necessary for, e.g. void * => unsigned long
81 cost = Cost::unsafe;
82 }
83 }
84 }
85 };
[cf32116]86
87 #warning For overload resolution between the two versions.
88 int localPtrsCastable(const ast::Type * t1, const ast::Type * t2,
89 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ) {
90 return ptrsCastable( t1, t2, symtab, env );
91 }
92 Cost localCastCost(
93 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
94 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
95 ) { return castCost( src, dst, srcIsLvalue, symtab, env ); }
[3c89751]96} // anonymous namespace
97
[7870799]98Cost castCost(
[cf32116]99 const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
100 const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
[3c89751]101) {
102 if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
[3e5dd913]103 if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
[3c89751]104 // check cast cost against bound type, if present
105 if ( eqvClass->bound ) {
[cf32116]106 return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
[3c89751]107 } else {
108 return Cost::infinity;
109 }
110 } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) {
111 // all typedefs should be gone by now
112 auto type = strict_dynamic_cast< const ast::TypeDecl * >( named );
113 if ( type->base ) {
[cf32116]114 return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
[3c89751]115 }
116 }
117 }
118
119 PRINT(
120 std::cerr << "castCost ::: src is ";
121 ast::print( std::cerr, src );
122 std::cerr << std::endl << "dest is ";
123 ast::print( std::cerr, dst );
124 std::cerr << std::endl << "env is" << std::endl;
125 ast::print( std::cerr, env, 2 );
126 )
127
[251ce80]128 if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
[3c89751]129 PRINT( std::cerr << "compatible!" << std::endl; )
[46da46b]130 if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
131 return Cost::spec;
132 }
[c8e4d2f8]133 return Cost::zero;
[3c89751]134 } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
135 return Cost::safe;
136 } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) {
137 PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
138 #warning cast on ptrsCastable artifact of having two functions, remove when port done
[7870799]139 return convertToReferenceCost(
[cf32116]140 src, refType, srcIsLvalue, symtab, env, localPtrsCastable );
[3c89751]141 } else {
142 #warning cast on castCost artifact of having two functions, remove when port done
[cf32116]143 ast::Pass< CastCost_new > converter(
144 dst, srcIsLvalue, symtab, env, localCastCost );
[3c89751]145 src->accept( converter );
[7ff3e522]146 return converter.core.cost;
[c8e4d2f8]147 }
[3c89751]148}
149
[51b73452]150} // namespace ResolvExpr
[a32b204]151
152// Local Variables: //
153// tab-width: 4 //
154// mode: c++ //
155// compile-command: "make install" //
156// End: //
Note: See TracBrowser for help on using the repository browser.