source: src/ResolvExpr/CastCost.cc @ c6b4432

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

Remove BaseSyntaxNode? and clean-up.

  • Property mode set to 100644
File size: 5.1 KB
Line 
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 : Andrew Beach
12// Last Modified On : Tue Oct  4 15:00:00 2019
13// Update Count     : 9
14//
15
16#include "CastCost.hpp"
17
18#include <cassert>                       // for assert
19
20#include "AST/Print.hpp"
21#include "AST/SymbolTable.hpp"
22#include "AST/Type.hpp"
23#include "AST/TypeEnvironment.hpp"
24#include "ConversionCost.h"              // for ConversionCost
25#include "Cost.h"                        // for Cost, Cost::infinity
26#include "ResolvExpr/ConversionCost.h"   // for conversionCost
27#include "ResolvExpr/PtrsCastable.hpp"   // for ptrsCastable
28#include "ResolvExpr/typeops.h"          // for ptrsCastable
29#include "ResolvExpr/Unify.h"            // for typesCompatibleIgnoreQualifiers
30
31#if 0
32#define PRINT(x) x
33#else
34#define PRINT(x)
35#endif
36
37namespace ResolvExpr {
38
39namespace {
40        struct CastCost_new : public ConversionCost_new {
41                using ConversionCost_new::previsit;
42                using ConversionCost_new::postvisit;
43
44                CastCost_new(
45                        const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab,
46                        const ast::TypeEnvironment & env, CostCalculation costFunc )
47                : ConversionCost_new( dst, srcIsLvalue, symtab, env, costFunc ) {}
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 {
55                                cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
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
63                                        && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
64                                ) {
65                                        cost = Cost::safe;
66                                } else {
67                                        ast::TypeEnvironment newEnv{ env };
68                                        if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
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        };
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 ); }
96} // anonymous namespace
97
98Cost castCost(
99        const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
100        const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
101) {
102        if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
103                if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
104                        // check cast cost against bound type, if present
105                        if ( eqvClass->bound ) {
106                                return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
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 ) {
114                                return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
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
128        if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
129                PRINT( std::cerr << "compatible!" << std::endl; )
130                if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
131                        return Cost::spec;
132                }
133                return Cost::zero;
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
139                return convertToReferenceCost(
140                        src, refType, srcIsLvalue, symtab, env, localPtrsCastable );
141        } else {
142                #warning cast on castCost artifact of having two functions, remove when port done
143                ast::Pass< CastCost_new > converter(
144                        dst, srcIsLvalue, symtab, env, localCastCost );
145                src->accept( converter );
146                return converter.core.cost;
147        }
148}
149
150} // namespace ResolvExpr
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.