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