| 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 | // typeops.h -- | 
|---|
| 8 | // | 
|---|
| 9 | // Author           : Richard C. Bilson | 
|---|
| 10 | // Created On       : Sun May 17 07:28:22 2015 | 
|---|
| 11 | // Last Modified By : Andrew Beach | 
|---|
| 12 | // Last Modified On : Tue Oct  1 09:45:00 2019 | 
|---|
| 13 | // Update Count     : 6 | 
|---|
| 14 | // | 
|---|
| 15 |  | 
|---|
| 16 | #pragma once | 
|---|
| 17 |  | 
|---|
| 18 | #include <vector> | 
|---|
| 19 |  | 
|---|
| 20 | #include "Cost.h" | 
|---|
| 21 | #include "TypeEnvironment.h" | 
|---|
| 22 | #include "WidenMode.h" | 
|---|
| 23 | #include "AST/Fwd.hpp" | 
|---|
| 24 | #include "AST/Node.hpp" | 
|---|
| 25 | #include "AST/SymbolTable.hpp" | 
|---|
| 26 | #include "AST/Type.hpp" | 
|---|
| 27 | #include "AST/TypeEnvironment.hpp" | 
|---|
| 28 | #include "SynTree/SynTree.h" | 
|---|
| 29 | #include "SynTree/Type.h" | 
|---|
| 30 |  | 
|---|
| 31 | namespace SymTab { | 
|---|
| 32 | class Indexer; | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | namespace ResolvExpr { | 
|---|
| 36 | // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by | 
|---|
| 37 | // picking one element out of each set | 
|---|
| 38 | template< typename InputIterator, typename OutputIterator > | 
|---|
| 39 | void combos( InputIterator begin, InputIterator end, OutputIterator out ) { | 
|---|
| 40 | typedef typename InputIterator::value_type SetType; | 
|---|
| 41 | typedef typename std::vector< typename SetType::value_type > ListType; | 
|---|
| 42 |  | 
|---|
| 43 | if ( begin == end )     { | 
|---|
| 44 | *out++ = ListType(); | 
|---|
| 45 | return; | 
|---|
| 46 | } // if | 
|---|
| 47 |  | 
|---|
| 48 | InputIterator current = begin; | 
|---|
| 49 | begin++; | 
|---|
| 50 |  | 
|---|
| 51 | std::vector< ListType > recursiveResult; | 
|---|
| 52 | combos( begin, end, back_inserter( recursiveResult ) ); | 
|---|
| 53 |  | 
|---|
| 54 | for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { | 
|---|
| 55 | ListType result; | 
|---|
| 56 | std::back_insert_iterator< ListType > inserter = back_inserter( result ); | 
|---|
| 57 | *inserter++ = j; | 
|---|
| 58 | std::copy( i.begin(), i.end(), inserter ); | 
|---|
| 59 | *out++ = result; | 
|---|
| 60 | } | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | // in AdjustExprType.cc | 
|---|
| 64 | /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function | 
|---|
| 65 | void adjustExprType( Type *& type, const TypeEnvironment & env, const SymTab::Indexer & indexer ); | 
|---|
| 66 |  | 
|---|
| 67 | /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function using empty TypeEnvironment and Indexer | 
|---|
| 68 | void adjustExprType( Type *& type ); | 
|---|
| 69 |  | 
|---|
| 70 | template< typename ForwardIterator > | 
|---|
| 71 | void adjustExprTypeList( ForwardIterator begin, ForwardIterator end, const TypeEnvironment & env, const SymTab::Indexer & indexer ) { | 
|---|
| 72 | while ( begin != end ) { | 
|---|
| 73 | adjustExprType( *begin++, env, indexer ); | 
|---|
| 74 | } // while | 
|---|
| 75 | } | 
|---|
| 76 |  | 
|---|
| 77 | /// Replaces array types with equivalent pointer, and function types with a pointer-to-function | 
|---|
| 78 | const ast::Type * adjustExprType( | 
|---|
| 79 | const ast::Type * type, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab ); | 
|---|
| 80 |  | 
|---|
| 81 | // in CastCost.cc | 
|---|
| 82 | Cost castCost( const Type * src, const Type * dest, bool srcIsLvalue, | 
|---|
| 83 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
| 84 | Cost castCost( | 
|---|
| 85 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, | 
|---|
| 86 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); | 
|---|
| 87 |  | 
|---|
| 88 | // in ConversionCost.cc | 
|---|
| 89 | Cost conversionCost( const Type * src, const Type * dest, bool srcIsLvalue, | 
|---|
| 90 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
| 91 | Cost conversionCost( | 
|---|
| 92 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, | 
|---|
| 93 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); | 
|---|
| 94 |  | 
|---|
| 95 | // in AlternativeFinder.cc | 
|---|
| 96 | Cost computeConversionCost( Type * actualType, Type * formalType, bool actualIsLvalue, | 
|---|
| 97 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
| 98 |  | 
|---|
| 99 | // in PtrsAssignable.cc | 
|---|
| 100 | int ptrsAssignable( const Type * src, const Type * dest, const TypeEnvironment & env ); | 
|---|
| 101 | int ptrsAssignable( const ast::Type * src, const ast::Type * dst, | 
|---|
| 102 | const ast::TypeEnvironment & env ); | 
|---|
| 103 |  | 
|---|
| 104 | // in PtrsCastable.cc | 
|---|
| 105 | int ptrsCastable( const Type * src, const Type * dest, const TypeEnvironment & env, const SymTab::Indexer & indexer ); | 
|---|
| 106 | int ptrsCastable( | 
|---|
| 107 | const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab, | 
|---|
| 108 | const ast::TypeEnvironment & env ); | 
|---|
| 109 |  | 
|---|
| 110 | // in Unify.cc | 
|---|
| 111 | bool typesCompatible( const Type *, const Type *, const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
| 112 | bool typesCompatibleIgnoreQualifiers( const Type *, const Type *, const SymTab::Indexer & indexer, const TypeEnvironment & env ); | 
|---|
| 113 |  | 
|---|
| 114 | inline bool typesCompatible( const Type * t1, const Type * t2, const SymTab::Indexer & indexer ) { | 
|---|
| 115 | TypeEnvironment env; | 
|---|
| 116 | return typesCompatible( t1, t2, indexer, env ); | 
|---|
| 117 | } | 
|---|
| 118 |  | 
|---|
| 119 | inline bool typesCompatibleIgnoreQualifiers( const Type * t1, const Type * t2, const SymTab::Indexer & indexer ) { | 
|---|
| 120 | TypeEnvironment env; | 
|---|
| 121 | return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env ); | 
|---|
| 122 | } | 
|---|
| 123 |  | 
|---|
| 124 | bool typesCompatible( | 
|---|
| 125 | const ast::Type *, const ast::Type *, const ast::SymbolTable & symtab = {}, | 
|---|
| 126 | const ast::TypeEnvironment & env = {} ); | 
|---|
| 127 |  | 
|---|
| 128 | bool typesCompatibleIgnoreQualifiers( | 
|---|
| 129 | const ast::Type *, const ast::Type *, const ast::SymbolTable &, | 
|---|
| 130 | const ast::TypeEnvironment & env = {} ); | 
|---|
| 131 |  | 
|---|
| 132 | /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value. | 
|---|
| 133 | Type * extractResultType( FunctionType * functionType ); | 
|---|
| 134 | /// Creates or extracts the type represented by the list of returns in a `FunctionType`. | 
|---|
| 135 | ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func ); | 
|---|
| 136 |  | 
|---|
| 137 | // in CommonType.cc | 
|---|
| 138 | Type * commonType( Type * type1, Type * type2, bool widenFirst, bool widenSecond, const SymTab::Indexer & indexer, TypeEnvironment & env, const OpenVarSet & openVars ); | 
|---|
| 139 | ast::ptr< ast::Type > commonType( | 
|---|
| 140 | const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2, WidenMode widen, | 
|---|
| 141 | const ast::SymbolTable & symtab, ast::TypeEnvironment & env, const ast::OpenVarSet & open ); | 
|---|
| 142 |  | 
|---|
| 143 | // in PolyCost.cc | 
|---|
| 144 | int polyCost( Type * type, const TypeEnvironment & env, const SymTab::Indexer & indexer ); | 
|---|
| 145 | int polyCost( | 
|---|
| 146 | const ast::Type * type, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); | 
|---|
| 147 |  | 
|---|
| 148 | // in SpecCost.cc | 
|---|
| 149 | int specCost( Type * type ); | 
|---|
| 150 | int specCost( const ast::Type * type ); | 
|---|
| 151 |  | 
|---|
| 152 | // in Occurs.cc | 
|---|
| 153 | bool occurs( const Type * type, const std::string & varName, const TypeEnvironment & env ); | 
|---|
| 154 | // new AST version in TypeEnvironment.cpp (only place it was used in old AST) | 
|---|
| 155 |  | 
|---|
| 156 | template<typename Iter> | 
|---|
| 157 | bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment & env ) { | 
|---|
| 158 | while ( begin != end ) { | 
|---|
| 159 | if ( occurs( ty, *begin, env ) ) return true; | 
|---|
| 160 | ++begin; | 
|---|
| 161 | } | 
|---|
| 162 | return false; | 
|---|
| 163 | } | 
|---|
| 164 |  | 
|---|
| 165 | // in AlternativeFinder.cc | 
|---|
| 166 | void referenceToRvalueConversion( Expression *& expr, Cost & cost ); | 
|---|
| 167 | // in CandidateFinder.cpp | 
|---|
| 168 | const ast::Expr * referenceToRvalueConversion( const ast::Expr * expr, Cost & cost ); | 
|---|
| 169 |  | 
|---|
| 170 | /// flatten tuple type into list of types | 
|---|
| 171 | template< typename OutputIterator > | 
|---|
| 172 | void flatten( Type * type, OutputIterator out ) { | 
|---|
| 173 | if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) { | 
|---|
| 174 | for ( Type * t : tupleType->get_types() ) { | 
|---|
| 175 | flatten( t, out ); | 
|---|
| 176 | } | 
|---|
| 177 | } else { | 
|---|
| 178 | *out++ = type->clone(); | 
|---|
| 179 | } | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | /// flatten tuple type into existing list of types | 
|---|
| 183 | static inline void flatten( | 
|---|
| 184 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out | 
|---|
| 185 | ) { | 
|---|
| 186 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { | 
|---|
| 187 | for ( const ast::Type * t : tupleType->types ) { | 
|---|
| 188 | flatten( t, out ); | 
|---|
| 189 | } | 
|---|
| 190 | } else { | 
|---|
| 191 | out.emplace_back( type ); | 
|---|
| 192 | } | 
|---|
| 193 | } | 
|---|
| 194 |  | 
|---|
| 195 | /// flatten tuple type into list of types | 
|---|
| 196 | static inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { | 
|---|
| 197 | std::vector< ast::ptr< ast::Type > > out; | 
|---|
| 198 | out.reserve( type->size() ); | 
|---|
| 199 | flatten( type, out ); | 
|---|
| 200 | return out; | 
|---|
| 201 | } | 
|---|
| 202 |  | 
|---|
| 203 | // in TypeEnvironment.cc | 
|---|
| 204 | bool isFtype( const Type * type ); | 
|---|
| 205 | } // namespace ResolvExpr | 
|---|
| 206 |  | 
|---|
| 207 | namespace ast { | 
|---|
| 208 | // in TypeEnvironment.cpp | 
|---|
| 209 | bool isFtype( const ast::Type * type ); | 
|---|
| 210 | } // namespace ast | 
|---|
| 211 |  | 
|---|
| 212 | // Local Variables: // | 
|---|
| 213 | // tab-width: 4 // | 
|---|
| 214 | // mode: c++ // | 
|---|
| 215 | // compile-command: "make install" // | 
|---|
| 216 | // End: // | 
|---|