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