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 | //
|
---|
7 | // CastCost.cc --
|
---|
8 | //
|
---|
9 | // Author : Richard C. Bilson
|
---|
10 | // Created On : Sun May 17 06:57:43 2015
|
---|
11 | // Last Modified By : Peter A. Buhr
|
---|
12 | // Last Modified On : Tue Feb 2 15:34:36 2016
|
---|
13 | // Update Count : 7
|
---|
14 | //
|
---|
15 |
|
---|
16 | #include "typeops.h"
|
---|
17 | #include "Cost.h"
|
---|
18 | #include "ConversionCost.h"
|
---|
19 | #include "SynTree/Type.h"
|
---|
20 | #include "SynTree/Visitor.h"
|
---|
21 | #include "SymTab/Indexer.h"
|
---|
22 |
|
---|
23 |
|
---|
24 | namespace ResolvExpr {
|
---|
25 | class CastCost : public ConversionCost {
|
---|
26 | public:
|
---|
27 | CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
|
---|
28 |
|
---|
29 | virtual void visit( BasicType *basicType );
|
---|
30 | virtual void visit( PointerType *pointerType );
|
---|
31 | };
|
---|
32 |
|
---|
33 | Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
|
---|
34 | if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
|
---|
35 | EqvClass eqvClass;
|
---|
36 | NamedTypeDecl *namedType;
|
---|
37 | if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
|
---|
38 | return castCost( src, eqvClass.type, indexer, env );
|
---|
39 | } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
|
---|
40 | TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
|
---|
41 | // all typedefs should be gone by this point
|
---|
42 | assert( type );
|
---|
43 | if ( type->get_base() ) {
|
---|
44 | return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
|
---|
45 | } // if
|
---|
46 | } // if
|
---|
47 | } // if
|
---|
48 | if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
|
---|
49 | return Cost( 0, 0, 0 );
|
---|
50 | } else if ( dynamic_cast< VoidType* >( dest ) ) {
|
---|
51 | return Cost( 0, 0, 1 );
|
---|
52 | } else {
|
---|
53 | CastCost converter( dest, indexer, env );
|
---|
54 | src->accept( converter );
|
---|
55 | if ( converter.get_cost() == Cost::infinity ) {
|
---|
56 | return Cost::infinity;
|
---|
57 | } else {
|
---|
58 | // xxx - why are we adding cost 0 here?
|
---|
59 | return converter.get_cost() + Cost( 0, 0, 0 );
|
---|
60 | } // if
|
---|
61 | } // if
|
---|
62 | }
|
---|
63 |
|
---|
64 | CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
|
---|
65 | : ConversionCost( dest, indexer, env ) {
|
---|
66 | }
|
---|
67 |
|
---|
68 | void CastCost::visit( BasicType *basicType ) {
|
---|
69 | PointerType *destAsPointer = dynamic_cast< PointerType* >( dest );
|
---|
70 | if ( destAsPointer && basicType->isInteger() ) {
|
---|
71 | // necessary for, e.g. unsigned long => void*
|
---|
72 | cost = Cost( 1, 0, 0 );
|
---|
73 | } else {
|
---|
74 | ConversionCost::visit( basicType );
|
---|
75 | } // if
|
---|
76 | }
|
---|
77 |
|
---|
78 | void CastCost::visit( PointerType *pointerType ) {
|
---|
79 | if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
|
---|
80 | if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
|
---|
81 | cost = Cost( 0, 0, 1 );
|
---|
82 | } else {
|
---|
83 | TypeEnvironment newEnv( env );
|
---|
84 | newEnv.add( pointerType->get_forall() );
|
---|
85 | newEnv.add( pointerType->get_base()->get_forall() );
|
---|
86 | int castResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
|
---|
87 | if ( castResult > 0 ) {
|
---|
88 | cost = Cost( 0, 0, 1 );
|
---|
89 | } else if ( castResult < 0 ) {
|
---|
90 | cost = Cost::infinity;
|
---|
91 | } // if
|
---|
92 | } // if
|
---|
93 | } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
|
---|
94 | if ( destAsBasic->isInteger() ) {
|
---|
95 | // necessary for, e.g. void* => unsigned long
|
---|
96 | cost = Cost( 1, 0, 0 );
|
---|
97 | } // if
|
---|
98 | }
|
---|
99 | }
|
---|
100 | } // namespace ResolvExpr
|
---|
101 |
|
---|
102 | // Local Variables: //
|
---|
103 | // tab-width: 4 //
|
---|
104 | // mode: c++ //
|
---|
105 | // compile-command: "make install" //
|
---|
106 | // End: //
|
---|