[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 | // |
---|
[2463d0e] | 7 | // ConversionCost.h -- |
---|
[a32b204] | 8 | // |
---|
| 9 | // Author : Richard C. Bilson |
---|
| 10 | // Created On : Sun May 17 09:37:28 2015 |
---|
[fb2bde4] | 11 | // Last Modified By : Andrew Beach |
---|
[1d17939] | 12 | // Last Modified On : Wed Jul 29 16:12:00 2020 |
---|
[cf32116] | 13 | // Update Count : 7 |
---|
[a32b204] | 14 | // |
---|
| 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[0c6596f] | 18 | #include <functional> // for function |
---|
| 19 | |
---|
[ea6332d] | 20 | #include "Cost.h" // for Cost |
---|
[bd0b6b62] | 21 | |
---|
[fb2bde4] | 22 | #include "AST/Fwd.hpp" |
---|
| 23 | #include "AST/Pass.hpp" // for WithShortCircuiting |
---|
[bd0b6b62] | 24 | #include "Common/PassVisitor.h" |
---|
[ea6332d] | 25 | #include "SynTree/Visitor.h" // for Visitor |
---|
| 26 | #include "SynTree/SynTree.h" // for Visitor Nodes |
---|
| 27 | |
---|
| 28 | namespace SymTab { |
---|
[0c6596f] | 29 | class Indexer; |
---|
[ea6332d] | 30 | } // namespace SymTab |
---|
[51b7345] | 31 | |
---|
| 32 | namespace ResolvExpr { |
---|
[0c6596f] | 33 | class TypeEnvironment; |
---|
[ea6332d] | 34 | |
---|
[7d01cf44] | 35 | typedef std::function<Cost(const Type *, const Type *, bool, |
---|
| 36 | const SymTab::Indexer &, const TypeEnvironment &)> CostFunction; |
---|
| 37 | |
---|
[bd0b6b62] | 38 | struct ConversionCost : public WithShortCircuiting { |
---|
[a32b204] | 39 | public: |
---|
[7d01cf44] | 40 | ConversionCost( const Type * dest, bool srcIsLvalue, |
---|
| 41 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction ); |
---|
[2463d0e] | 42 | |
---|
[a32b204] | 43 | Cost get_cost() const { return cost; } |
---|
| 44 | |
---|
[7870799] | 45 | void previsit( const BaseSyntaxNode * ) { visit_children = false; } |
---|
[bd0b6b62] | 46 | |
---|
[7870799] | 47 | void postvisit( const VoidType * voidType ); |
---|
| 48 | void postvisit( const BasicType * basicType ); |
---|
| 49 | void postvisit( const PointerType * pointerType ); |
---|
| 50 | void postvisit( const ArrayType * arrayType ); |
---|
| 51 | void postvisit( const ReferenceType * refType ); |
---|
| 52 | void postvisit( const FunctionType * functionType ); |
---|
| 53 | void postvisit( const EnumInstType * aggregateUseType ); |
---|
| 54 | void postvisit( const TraitInstType * aggregateUseType ); |
---|
| 55 | void postvisit( const TypeInstType * aggregateUseType ); |
---|
| 56 | void postvisit( const TupleType * tupleType ); |
---|
| 57 | void postvisit( const VarArgsType * varArgsType ); |
---|
| 58 | void postvisit( const ZeroType * zeroType ); |
---|
| 59 | void postvisit( const OneType * oneType ); |
---|
[a32b204] | 60 | protected: |
---|
[7870799] | 61 | const Type * dest; |
---|
[7d01cf44] | 62 | bool srcIsLvalue; |
---|
[a32b204] | 63 | const SymTab::Indexer &indexer; |
---|
| 64 | Cost cost; |
---|
| 65 | const TypeEnvironment &env; |
---|
[721cd19f] | 66 | CostFunction costFunc; |
---|
[a32b204] | 67 | }; |
---|
[2463d0e] | 68 | |
---|
[7870799] | 69 | typedef std::function<int(const Type *, const Type *, const SymTab::Indexer &, const TypeEnvironment &)> PtrsFunction; |
---|
[7d01cf44] | 70 | Cost convertToReferenceCost( const Type * src, const ReferenceType * dest, bool srcIsLvalue, |
---|
| 71 | const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ); |
---|
[fb2bde4] | 72 | |
---|
| 73 | // Some function pointer types, differ in return type. |
---|
[cf32116] | 74 | using CostCalculation = std::function<Cost(const ast::Type *, const ast::Type *, bool, |
---|
[fb2bde4] | 75 | const ast::SymbolTable &, const ast::TypeEnvironment &)>; |
---|
[cf32116] | 76 | using PtrsCalculation = std::function<int(const ast::Type *, const ast::Type *, |
---|
[fb2bde4] | 77 | const ast::SymbolTable &, const ast::TypeEnvironment &)>; |
---|
| 78 | |
---|
[3c89751] | 79 | #warning when the old ConversionCost is removed, get ride of the _new suffix. |
---|
[fb2bde4] | 80 | class ConversionCost_new : public ast::WithShortCircuiting { |
---|
[3c89751] | 81 | protected: |
---|
[fb2bde4] | 82 | const ast::Type * dst; |
---|
[cf32116] | 83 | bool srcIsLvalue; |
---|
[fb2bde4] | 84 | const ast::SymbolTable & symtab; |
---|
| 85 | const ast::TypeEnvironment & env; |
---|
| 86 | CostCalculation costCalc; |
---|
| 87 | public: |
---|
[c15085d] | 88 | static size_t traceId; |
---|
[fb2bde4] | 89 | Cost cost; |
---|
| 90 | |
---|
[cf32116] | 91 | ConversionCost_new( const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, |
---|
[fb2bde4] | 92 | const ast::TypeEnvironment & env, CostCalculation costCalc ) : |
---|
[cf32116] | 93 | dst( dst ), srcIsLvalue( srcIsLvalue ), symtab( symtab ), env( env ), |
---|
| 94 | costCalc( costCalc ), cost( Cost::infinity ) |
---|
[fb2bde4] | 95 | {} |
---|
| 96 | |
---|
| 97 | void previsit( const ast::Node * ) { visit_children = false; } |
---|
| 98 | |
---|
| 99 | void postvisit( const ast::VoidType * voidType ); |
---|
| 100 | void postvisit( const ast::BasicType * basicType ); |
---|
| 101 | void postvisit( const ast::PointerType * pointerType ); |
---|
| 102 | void postvisit( const ast::ArrayType * arrayType ); |
---|
| 103 | void postvisit( const ast::ReferenceType * refType ); |
---|
| 104 | void postvisit( const ast::FunctionType * functionType ); |
---|
| 105 | void postvisit( const ast::EnumInstType * enumInstType ); |
---|
| 106 | void postvisit( const ast::TraitInstType * traitInstType ); |
---|
| 107 | void postvisit( const ast::TypeInstType * typeInstType ); |
---|
| 108 | void postvisit( const ast::TupleType * tupleType ); |
---|
| 109 | void postvisit( const ast::VarArgsType * varArgsType ); |
---|
| 110 | void postvisit( const ast::ZeroType * zeroType ); |
---|
| 111 | void postvisit( const ast::OneType * oneType ); |
---|
| 112 | }; |
---|
| 113 | |
---|
| 114 | Cost convertToReferenceCost( const ast::Type * src, const ast::ReferenceType * dest, |
---|
[cf32116] | 115 | bool srcIsLvalue, const ast::SymbolTable & indexer, const ast::TypeEnvironment & env, |
---|
| 116 | PtrsCalculation func ); |
---|
[fb2bde4] | 117 | |
---|
[51b7345] | 118 | } // namespace ResolvExpr |
---|
| 119 | |
---|
[a32b204] | 120 | // Local Variables: // |
---|
| 121 | // tab-width: 4 // |
---|
| 122 | // mode: c++ // |
---|
| 123 | // compile-command: "make install" // |
---|
| 124 | // End: // |
---|