source: src/ResolvExpr/CastCost.cc@ aca144e

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since aca144e was c8e4d2f8, checked in by Aaron Moss <a3moss@…>, 6 years ago

Start porting CastExpr resolution

  • 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
[52f85e0]11// Last Modified By : Peter A. Buhr
12// Last Modified On : Tue Feb 2 15:34:36 2016
13// Update Count : 7
[a32b204]14//
15
[ea6332d]16#include <cassert> // for assert
17
18#include "ConversionCost.h" // for ConversionCost
19#include "Cost.h" // for Cost, Cost::infinity
20#include "ResolvExpr/TypeEnvironment.h" // for TypeEnvironment, EqvClass
21#include "SymTab/Indexer.h" // for Indexer
22#include "SynTree/Declaration.h" // for TypeDecl, NamedTypeDecl
23#include "SynTree/Type.h" // for PointerType, Type, TypeInstType
24#include "typeops.h" // for typesCompatibleIgnoreQualifiers
[51b73452]25
[150ec33]26#if 0
27#define PRINT(x) x
28#else
29#define PRINT(x)
30#endif
[51b73452]31
32namespace ResolvExpr {
[bd0b6b62]33 struct CastCost : public ConversionCost {
[a32b204]34 public:
[721cd19f]35 CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc );
[0b150ec]36
[bd0b6b62]37 using ConversionCost::previsit;
38 using ConversionCost::postvisit;
39 void postvisit( BasicType * basicType );
40 void postvisit( PointerType * pointerType );
[a32b204]41 };
[51b73452]42
[a32b204]43 Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
44 if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
[00ac42e]45 if ( const EqvClass* eqvClass = env.lookup( destAsTypeInst->get_name() ) ) {
46 if ( eqvClass->type ) {
47 return castCost( src, eqvClass->type, indexer, env );
[0b150ec]48 } else {
49 return Cost::infinity;
50 }
[00ac42e]51 } else if ( NamedTypeDecl *namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) {
[a32b204]52 // all typedefs should be gone by this point
[982832e]53 TypeDecl *type = strict_dynamic_cast< TypeDecl* >( namedType );
[eb0aedb]54 if ( type->base ) {
55 return castCost( src, type->base, indexer, env ) + Cost::safe;
[a32b204]56 } // if
57 } // if
58 } // if
[150ec33]59
60 PRINT(
61 std::cerr << "castCost ::: src is ";
62 src->print( std::cerr );
63 std::cerr << std::endl << "dest is ";
64 dest->print( std::cerr );
65 std::cerr << std::endl << "env is" << std::endl;
66 env.print( std::cerr, 8 );
67 )
68
[a32b204]69 if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
[150ec33]70 PRINT( std::cerr << "compatible!" << std::endl; )
[89be1c68]71 return Cost::zero;
[a32b204]72 } else if ( dynamic_cast< VoidType* >( dest ) ) {
[89be1c68]73 return Cost::safe;
[2463d0e]74 } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * > ( dest ) ) {
[150ec33]75 PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
[721cd19f]76 return convertToReferenceCost( src, refType, indexer, env, [](Type * t1, Type * t2, const SymTab::Indexer & indexer, const TypeEnvironment & env ) {
[0c6596f]77 return ptrsCastable( t1, t2, env, indexer );
78 });
[a32b204]79 } else {
[c8e4d2f8]80 #warning cast on castCost artifact of having two functions, remove when port done
81 PassVisitor<CastCost> converter(
82 dest, indexer, env,
83 (Cost (*)( Type *, Type *, const SymTab::Indexer &, const TypeEnvironment & ))
84 castCost );
[a32b204]85 src->accept( converter );
[bd0b6b62]86 if ( converter.pass.get_cost() == Cost::infinity ) {
[a32b204]87 return Cost::infinity;
88 } else {
[1521de20]89 // xxx - why are we adding cost 0 here?
[bd0b6b62]90 return converter.pass.get_cost() + Cost::zero;
[a32b204]91 } // if
[c11e31c]92 } // if
[a32b204]93 }
[51b73452]94
[721cd19f]95 CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction costFunc )
96 : ConversionCost( dest, indexer, env, costFunc ) {
[a32b204]97 }
[51b73452]98
[bd0b6b62]99 void CastCost::postvisit( BasicType *basicType ) {
[543159b]100 PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
101 if ( destAsPointer && basicType->isInteger() ) {
[9fd97126]102 // necessary for, e.g. unsigned long => void*
[89be1c68]103 cost = Cost::unsafe;
[a32b204]104 } else {
[2463d0e]105 cost = conversionCost( basicType, dest, indexer, env );
[a32b204]106 } // if
107 }
[51b73452]108
[bd0b6b62]109 void CastCost::postvisit( PointerType *pointerType ) {
[a32b204]110 if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
[eb0aedb]111 if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->base, destAsPtr->base, indexer, env ) ) {
[89be1c68]112 cost = Cost::safe;
[a32b204]113 } else {
114 TypeEnvironment newEnv( env );
[eb0aedb]115 newEnv.add( pointerType->forall );
116 newEnv.add( pointerType->base->forall );
117 int castResult = ptrsCastable( pointerType->base, destAsPtr->base, newEnv, indexer );
[1521de20]118 if ( castResult > 0 ) {
[89be1c68]119 cost = Cost::safe;
[1521de20]120 } else if ( castResult < 0 ) {
[52f85e0]121 cost = Cost::infinity;
[a32b204]122 } // if
123 } // if
[543159b]124 } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
125 if ( destAsBasic->isInteger() ) {
[9fd97126]126 // necessary for, e.g. void* => unsigned long
[89be1c68]127 cost = Cost::unsafe;
[543159b]128 } // if
129 }
[a32b204]130 }
[c8e4d2f8]131
132 Cost castCost(
133 const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab,
134 const ast::TypeEnvironment & env
135 ) {
136 #warning unimplmented
137 (void)src; (void)dst; (void)symtab; (void)env;
138 assert(false);
139 return Cost::zero;
140 }
[51b73452]141} // namespace ResolvExpr
[a32b204]142
143// Local Variables: //
144// tab-width: 4 //
145// mode: c++ //
146// compile-command: "make install" //
147// End: //
Note: See TracBrowser for help on using the repository browser.