source: translator/ResolvExpr/CastCost.cc @ b87a5ed

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since b87a5ed was c11e31c, checked in by Peter A. Buhr <pabuhr@…>, 9 years ago

add inline and attribute qualifiers, cfa.y comment formatting, fix error message in isIntegralType

  • Property mode set to 100644
File size: 2.8 KB
Line 
1#include "typeops.h"
2#include "Cost.h"
3#include "ConversionCost.h"
4#include "SynTree/Type.h"
5#include "SynTree/Visitor.h"
6#include "SymTab/Indexer.h"
7
8
9namespace ResolvExpr {
10    class CastCost : public ConversionCost {
11      public:
12        CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
13 
14        virtual void visit( BasicType *basicType );
15        virtual void visit( PointerType *pointerType );
16    };
17
18    Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
19        if ( TypeInstType *destAsTypeInst = dynamic_cast< TypeInstType* >( dest ) ) {
20            EqvClass eqvClass;
21            NamedTypeDecl *namedType;
22            if ( env.lookup( destAsTypeInst->get_name(), eqvClass ) ) {
23                return castCost( src, eqvClass.type, indexer, env );
24            } else if ( ( namedType = indexer.lookupType( destAsTypeInst->get_name() ) ) ) {
25                TypeDecl *type = dynamic_cast< TypeDecl* >( namedType );
26                // all typedefs should be gone by this point
27                assert( type );
28                if ( type->get_base() ) {
29                    return castCost( src, type->get_base(), indexer, env ) + Cost( 0, 0, 1 );
30                } // if
31            } // if
32        } // if
33        if ( typesCompatibleIgnoreQualifiers( src, dest, indexer, env ) ) {
34            return Cost( 0, 0, 0 );
35        } else if ( dynamic_cast< VoidType* >( dest ) ) {
36            return Cost( 0, 0, 1 );
37        } else {
38            CastCost converter( dest, indexer, env );
39            src->accept( converter );
40            if ( converter.get_cost() == Cost::infinity ) {
41                return Cost::infinity;
42            } else {
43                return converter.get_cost() + Cost( 0, 0, 0 );
44            } // if
45        } // if
46    }
47
48    CastCost::CastCost( Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env )
49        : ConversionCost( dest, indexer, env ) {
50    }
51
52    void CastCost::visit( BasicType *basicType ) {
53        if ( dynamic_cast< PointerType* >( dest ) ) {
54            cost = Cost( 1, 0, 0 );
55        } else {
56            ConversionCost::visit( basicType );
57        } // if
58    }
59
60    void CastCost::visit( PointerType *pointerType ) {
61        if ( PointerType *destAsPtr = dynamic_cast< PointerType* >( dest ) ) {
62            if ( pointerType->get_qualifiers() <= destAsPtr->get_qualifiers() && typesCompatibleIgnoreQualifiers( pointerType->get_base(), destAsPtr->get_base(), indexer, env ) ) {
63                cost = Cost( 0, 0, 1 );
64            } else if ( BasicType *destAsBasic = dynamic_cast< BasicType* >( dest ) ) {
65                if ( destAsBasic->isInteger() ) {
66                    cost = Cost( 1, 0, 0 );
67                }
68            } else {
69                TypeEnvironment newEnv( env );
70                newEnv.add( pointerType->get_forall() );
71                newEnv.add( pointerType->get_base()->get_forall() );
72                int assignResult = ptrsCastable( pointerType->get_base(), destAsPtr->get_base(), newEnv, indexer );
73                if ( assignResult > 0 ) {
74                    cost = Cost( 0, 0, 1 );
75                } else if ( assignResult < 0 ) {
76                    cost = Cost( 1, 0, 0 );
77                } // if
78            } // if
79        } // if
80    }
81} // namespace ResolvExpr
Note: See TracBrowser for help on using the repository browser.