| [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 | 
|---|
| [51b73452] | 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 | 
|---|
| [51b73452] | 31 |  | 
|---|
|  | 32 | namespace ResolvExpr { | 
|---|
| [0c6596f] | 33 | class TypeEnvironment; | 
|---|
| [ea6332d] | 34 |  | 
|---|
| [fed6a0f] | 35 | Cost conversionCost( | 
|---|
|  | 36 | const Type * src, const Type * dest, bool srcIsLvalue, | 
|---|
|  | 37 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
|  | 38 |  | 
|---|
| [7d01cf44] | 39 | typedef std::function<Cost(const Type *, const Type *, bool, | 
|---|
|  | 40 | const SymTab::Indexer &, const TypeEnvironment &)> CostFunction; | 
|---|
|  | 41 |  | 
|---|
| [bd0b6b62] | 42 | struct ConversionCost : public WithShortCircuiting { | 
|---|
| [a32b204] | 43 | public: | 
|---|
| [7d01cf44] | 44 | ConversionCost( const Type * dest, bool srcIsLvalue, | 
|---|
|  | 45 | const SymTab::Indexer &indexer, const TypeEnvironment &env, CostFunction ); | 
|---|
| [2463d0e] | 46 |  | 
|---|
| [a32b204] | 47 | Cost get_cost() const { return cost; } | 
|---|
|  | 48 |  | 
|---|
| [7870799] | 49 | void previsit( const BaseSyntaxNode * ) { visit_children = false; } | 
|---|
| [bd0b6b62] | 50 |  | 
|---|
| [7870799] | 51 | void postvisit( const VoidType * voidType ); | 
|---|
|  | 52 | void postvisit( const BasicType * basicType ); | 
|---|
|  | 53 | void postvisit( const PointerType * pointerType ); | 
|---|
|  | 54 | void postvisit( const ArrayType * arrayType ); | 
|---|
|  | 55 | void postvisit( const ReferenceType * refType ); | 
|---|
|  | 56 | void postvisit( const FunctionType * functionType ); | 
|---|
|  | 57 | void postvisit( const EnumInstType * aggregateUseType ); | 
|---|
|  | 58 | void postvisit( const TraitInstType * aggregateUseType ); | 
|---|
|  | 59 | void postvisit( const TypeInstType * aggregateUseType ); | 
|---|
|  | 60 | void postvisit( const TupleType * tupleType ); | 
|---|
|  | 61 | void postvisit( const VarArgsType * varArgsType ); | 
|---|
|  | 62 | void postvisit( const ZeroType * zeroType ); | 
|---|
|  | 63 | void postvisit( const OneType * oneType ); | 
|---|
| [a32b204] | 64 | protected: | 
|---|
| [7870799] | 65 | const Type * dest; | 
|---|
| [7d01cf44] | 66 | bool srcIsLvalue; | 
|---|
| [a32b204] | 67 | const SymTab::Indexer &indexer; | 
|---|
|  | 68 | Cost cost; | 
|---|
|  | 69 | const TypeEnvironment &env; | 
|---|
| [721cd19f] | 70 | CostFunction costFunc; | 
|---|
| [fc134a48] | 71 | private: | 
|---|
|  | 72 | // refactor for code resue | 
|---|
|  | 73 | void conversionCostFromBasicToBasic( const BasicType * src, const BasicType* dest ); | 
|---|
| [a32b204] | 74 | }; | 
|---|
| [2463d0e] | 75 |  | 
|---|
| [7870799] | 76 | typedef std::function<int(const Type *, const Type *, const SymTab::Indexer &, const TypeEnvironment &)> PtrsFunction; | 
|---|
| [7d01cf44] | 77 | Cost convertToReferenceCost( const Type * src, const ReferenceType * dest, bool srcIsLvalue, | 
|---|
|  | 78 | const SymTab::Indexer & indexer, const TypeEnvironment & env, PtrsFunction func ); | 
|---|
| [fb2bde4] | 79 |  | 
|---|
|  | 80 | // Some function pointer types, differ in return type. | 
|---|
| [cf32116] | 81 | using CostCalculation = std::function<Cost(const ast::Type *, const ast::Type *, bool, | 
|---|
| [fb2bde4] | 82 | const ast::SymbolTable &, const ast::TypeEnvironment &)>; | 
|---|
| [cf32116] | 83 | using PtrsCalculation = std::function<int(const ast::Type *, const ast::Type *, | 
|---|
| [fb2bde4] | 84 | const ast::SymbolTable &, const ast::TypeEnvironment &)>; | 
|---|
|  | 85 |  | 
|---|
| [fed6a0f] | 86 | Cost conversionCost( | 
|---|
|  | 87 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, | 
|---|
|  | 88 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); | 
|---|
|  | 89 |  | 
|---|
|  | 90 | Cost convertToReferenceCost( const ast::Type * src, const ast::ReferenceType * dest, | 
|---|
|  | 91 | bool srcIsLvalue, const ast::SymbolTable & indexer, const ast::TypeEnvironment & env, | 
|---|
|  | 92 | PtrsCalculation func ); | 
|---|
|  | 93 |  | 
|---|
| [3c89751] | 94 | #warning when the old ConversionCost is removed, get ride of the _new suffix. | 
|---|
| [fb2bde4] | 95 | class ConversionCost_new : public ast::WithShortCircuiting { | 
|---|
| [3c89751] | 96 | protected: | 
|---|
| [fb2bde4] | 97 | const ast::Type * dst; | 
|---|
| [cf32116] | 98 | bool srcIsLvalue; | 
|---|
| [fb2bde4] | 99 | const ast::SymbolTable & symtab; | 
|---|
|  | 100 | const ast::TypeEnvironment & env; | 
|---|
|  | 101 | CostCalculation costCalc; | 
|---|
|  | 102 | public: | 
|---|
| [c15085d] | 103 | static size_t traceId; | 
|---|
| [fb2bde4] | 104 | Cost cost; | 
|---|
| [e6b42e7] | 105 | Cost result() { return cost; } | 
|---|
| [fb2bde4] | 106 |  | 
|---|
| [cf32116] | 107 | ConversionCost_new( const ast::Type * dst, bool srcIsLvalue, const ast::SymbolTable & symtab, | 
|---|
| [fb2bde4] | 108 | const ast::TypeEnvironment & env, CostCalculation costCalc ) : | 
|---|
| [cf32116] | 109 | dst( dst ), srcIsLvalue( srcIsLvalue ), symtab( symtab ), env( env ), | 
|---|
|  | 110 | costCalc( costCalc ), cost( Cost::infinity ) | 
|---|
| [fb2bde4] | 111 | {} | 
|---|
|  | 112 |  | 
|---|
|  | 113 | void previsit( const ast::Node * ) { visit_children = false; } | 
|---|
|  | 114 |  | 
|---|
|  | 115 | void postvisit( const ast::VoidType * voidType ); | 
|---|
|  | 116 | void postvisit( const ast::BasicType * basicType ); | 
|---|
|  | 117 | void postvisit( const ast::PointerType * pointerType ); | 
|---|
|  | 118 | void postvisit( const ast::ArrayType * arrayType ); | 
|---|
|  | 119 | void postvisit( const ast::ReferenceType * refType ); | 
|---|
|  | 120 | void postvisit( const ast::FunctionType * functionType ); | 
|---|
|  | 121 | void postvisit( const ast::EnumInstType * enumInstType ); | 
|---|
|  | 122 | void postvisit( const ast::TraitInstType * traitInstType ); | 
|---|
|  | 123 | void postvisit( const ast::TypeInstType * typeInstType ); | 
|---|
|  | 124 | void postvisit( const ast::TupleType * tupleType ); | 
|---|
|  | 125 | void postvisit( const ast::VarArgsType * varArgsType ); | 
|---|
|  | 126 | void postvisit( const ast::ZeroType * zeroType ); | 
|---|
|  | 127 | void postvisit( const ast::OneType * oneType ); | 
|---|
| [fc134a48] | 128 | private: | 
|---|
|  | 129 | // refactor for code resue | 
|---|
|  | 130 | void conversionCostFromBasicToBasic( const ast::BasicType * src, const ast::BasicType* dest ); | 
|---|
| [fb2bde4] | 131 | }; | 
|---|
|  | 132 |  | 
|---|
| [51b73452] | 133 | } // namespace ResolvExpr | 
|---|
|  | 134 |  | 
|---|
| [a32b204] | 135 | // Local Variables: // | 
|---|
|  | 136 | // tab-width: 4 // | 
|---|
|  | 137 | // mode: c++ // | 
|---|
|  | 138 | // compile-command: "make install" // | 
|---|
|  | 139 | // End: // | 
|---|