source: src/ResolvExpr/CastCost.cpp @ f678c53b

Last change on this file since f678c53b was 85855b0, checked in by JiadaL <j82liang@…>, 6 weeks ago
  1. Implement enum cast; 2. Change valueE so that opague enum returns quasi_void; 3. change enum hiding interpretation and pass visiting scheme
  • Property mode set to 100644
File size: 5.5 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.cpp --
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.hpp"            // for ConversionCost
25#include "Cost.hpp"                      // for Cost, Cost::infinity
26#include "ResolvExpr/ConversionCost.hpp" // for conversionCost
27#include "ResolvExpr/PtrsCastable.hpp"   // for ptrsCastable
28#include "ResolvExpr/Unify.hpp"          // for typesCompatibleIgnoreQualifiers
29
30#if 0
31#define PRINT(x) x
32#else
33#define PRINT(x)
34#endif
35
36namespace ResolvExpr {
37
38Cost castCost(
39        const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
40        const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
41);
42
43namespace {
44        struct CastCost : public ConversionCost {
45                using ConversionCost::previsit;
46                using ConversionCost::postvisit;
47
48                CastCost(
49                        const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab,
50                        const ast::TypeEnvironment & env, CostCalculation costFunc )
51                : ConversionCost( dst, srcIsLvalue, symtab, env, costFunc ) {}
52
53                void postvisit( const ast::EnumInstType * enumInst ) {
54                        cost = conversionCost( enumInst, dst, srcIsLvalue, symtab, env );
55                }
56
57                void postvisit( const ast::BasicType * basicType ) {
58                        auto ptr = dynamic_cast< const ast::PointerType * >( dst );
59                        if ( ptr && basicType->isInteger() ) {
60                                // needed for, e.g. unsigned long => void *
61                                cost = Cost::unsafe;
62                        } else {
63                                cost = conversionCost( basicType, dst, srcIsLvalue, symtab, env );
64                                if ( Cost::unsafe < cost ) {
65                                        if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {
66                                                // Always explict cast only for typed enum
67                                                if (enumInst->base->isTyped) cost = Cost::unsafe;
68                                        }
69                                }
70                        }
71                }
72
73                void postvisit( const ast::ZeroType * zero ) {
74                        cost = conversionCost( zero, dst, srcIsLvalue, symtab, env );
75                        if ( Cost::unsafe < cost ) {
76                                if (auto enumInst =  dynamic_cast<const ast::EnumInstType *>(dst)) {
77                                        if (enumInst->base->isTyped) cost = Cost::unsafe;
78                                }
79                        }
80                }
81
82                void postvisit( const ast::OneType * one ) {
83                        cost = conversionCost( one, dst, srcIsLvalue, symtab, env );
84                        if ( Cost::unsafe < cost ) {
85                                if (auto enumInst = dynamic_cast<const ast::EnumInstType *>(dst)) {
86                                        if (enumInst->base->isTyped) cost = Cost::unsafe;
87                                }
88                        }
89                }
90
91                void postvisit( const ast::PointerType * pointerType ) {
92                        if ( auto ptr = dynamic_cast< const ast::PointerType * >( dst ) ) {
93                                if (
94                                        pointerType->qualifiers <= ptr->qualifiers
95                                        && typesCompatibleIgnoreQualifiers( pointerType->base, ptr->base, env )
96                                ) {
97                                        cost = Cost::safe;
98                                } else {
99                                        ast::TypeEnvironment newEnv{ env };
100                                        if ( auto wParams = pointerType->base.as< ast::FunctionType >() ) {
101                                                newEnv.add( wParams->forall );
102                                        }
103                                        int castResult = ptrsCastable( pointerType->base, ptr->base, symtab, newEnv );
104                                        if ( castResult > 0 ) {
105                                                cost = Cost::safe;
106                                        } else if ( castResult < 0 ) {
107                                                cost = Cost::infinity;
108                                        }
109                                }
110                        } else if ( auto basic = dynamic_cast< const ast::BasicType * >( dst ) ) {
111                                if ( basic->isInteger() ) {
112                                        // necessary for, e.g. void * => unsigned long
113                                        cost = Cost::unsafe;
114                                }
115                        }
116                }
117        };
118
119} // anonymous namespace
120
121Cost castCost(
122        const ast::Type * src, const ast::Type * dst, bool srcIsLvalue,
123        const ast::SymbolTable & symtab, const ast::TypeEnvironment & env
124) {
125        if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( dst ) ) {
126                if ( const ast::EqvClass * eqvClass = env.lookup( *typeInst ) ) {
127                        // check cast cost against bound type, if present
128                        if ( eqvClass->bound ) {
129                                return castCost( src, eqvClass->bound, srcIsLvalue, symtab, env );
130                        } else {
131                                return Cost::infinity;
132                        }
133                } else if ( const ast::NamedTypeDecl * named = symtab.lookupType( typeInst->name ) ) {
134                        // all typedefs should be gone by now
135                        auto type = strict_dynamic_cast< const ast::TypeDecl * >( named );
136                        if ( type->base ) {
137                                return castCost( src, type->base, srcIsLvalue, symtab, env ) + Cost::safe;
138                        }
139                }
140        }
141
142        PRINT(
143                std::cerr << "castCost ::: src is ";
144                ast::print( std::cerr, src );
145                std::cerr << std::endl << "dest is ";
146                ast::print( std::cerr, dst );
147                std::cerr << std::endl << "env is" << std::endl;
148                ast::print( std::cerr, env, 2 );
149        )
150
151        if ( typesCompatibleIgnoreQualifiers( src, dst, env ) ) {
152                PRINT( std::cerr << "compatible!" << std::endl; )
153                if (dynamic_cast<const ast::ZeroType *>(dst) || dynamic_cast<const ast::OneType *>(dst)) {
154                        return Cost::spec;
155                }
156                return Cost::zero;
157        } else if ( dynamic_cast< const ast::VoidType * >( dst ) ) {
158                return Cost::safe;
159        } else if ( auto refType = dynamic_cast< const ast::ReferenceType * >( dst ) ) {
160                PRINT( std::cerr << "conversionCost: dest is reference" << std::endl; )
161                return convertToReferenceCost(
162                        src, refType, srcIsLvalue, symtab, env, ptrsCastable );
163        } else {
164                ast::Pass< CastCost > converter(
165                        dst, srcIsLvalue, symtab, env, castCost );
166                src->accept( converter );
167                return converter.core.cost;
168        }
169}
170
171} // namespace ResolvExpr
172
173// Local Variables: //
174// tab-width: 4 //
175// mode: c++ //
176// compile-command: "make install" //
177// End: //
Note: See TracBrowser for help on using the repository browser.