[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 |
---|
[7d01cf44] | 11 | // Last Modified By : Andrew Beach |
---|
[cf32116] | 12 | // Last Modified On : Tue Oct 1 09:45:00 2019 |
---|
| 13 | // Update Count : 6 |
---|
[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 |
---|
[7d01cf44] | 82 | Cost castCost( const Type * src, const Type * dest, bool srcIsLvalue, |
---|
| 83 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); |
---|
[7870799] | 84 | Cost castCost( |
---|
[cf32116] | 85 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
| 86 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); |
---|
[a32b204] | 87 | |
---|
| 88 | // in ConversionCost.cc |
---|
[7d01cf44] | 89 | Cost conversionCost( const Type * src, const Type * dest, bool srcIsLvalue, |
---|
| 90 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); |
---|
[7870799] | 91 | Cost conversionCost( |
---|
[cf32116] | 92 | const ast::Type * src, const ast::Type * dst, bool srcIsLvalue, |
---|
| 93 | const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); |
---|
[a32b204] | 94 | |
---|
[fbecee5] | 95 | // in AlternativeFinder.cc |
---|
[7d01cf44] | 96 | Cost computeConversionCost( Type * actualType, Type * formalType, bool actualIsLvalue, |
---|
[6f096d2] | 97 | const SymTab::Indexer & indexer, const TypeEnvironment & env ); |
---|
[fbecee5] | 98 | |
---|
[a32b204] | 99 | // in PtrsAssignable.cc |
---|
[6f096d2] | 100 | int ptrsAssignable( const Type * src, const Type * dest, const TypeEnvironment & env ); |
---|
[fb2bde4] | 101 | int ptrsAssignable( const ast::Type * src, const ast::Type * dst, |
---|
| 102 | const ast::TypeEnvironment & env ); |
---|
[a32b204] | 103 | |
---|
| 104 | // in PtrsCastable.cc |
---|
[6f096d2] | 105 | int ptrsCastable( const Type * src, const Type * dest, const TypeEnvironment & env, const SymTab::Indexer & indexer ); |
---|
[7870799] | 106 | int ptrsCastable( |
---|
| 107 | const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab, |
---|
[3c89751] | 108 | const ast::TypeEnvironment & env ); |
---|
[a32b204] | 109 | |
---|
| 110 | // in Unify.cc |
---|
[6f096d2] | 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 ); |
---|
[a32b204] | 113 | |
---|
[6f096d2] | 114 | inline bool typesCompatible( const Type * t1, const Type * t2, const SymTab::Indexer & indexer ) { |
---|
[a32b204] | 115 | TypeEnvironment env; |
---|
| 116 | return typesCompatible( t1, t2, indexer, env ); |
---|
| 117 | } |
---|
| 118 | |
---|
[6f096d2] | 119 | inline bool typesCompatibleIgnoreQualifiers( const Type * t1, const Type * t2, const SymTab::Indexer & indexer ) { |
---|
[a32b204] | 120 | TypeEnvironment env; |
---|
| 121 | return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env ); |
---|
| 122 | } |
---|
| 123 | |
---|
[7870799] | 124 | bool typesCompatible( |
---|
| 125 | const ast::Type *, const ast::Type *, const ast::SymbolTable & symtab = {}, |
---|
[d76c588] | 126 | const ast::TypeEnvironment & env = {} ); |
---|
[7870799] | 127 | |
---|
[d76c588] | 128 | bool typesCompatibleIgnoreQualifiers( |
---|
[7870799] | 129 | const ast::Type *, const ast::Type *, const ast::SymbolTable &, |
---|
[d76c588] | 130 | const ast::TypeEnvironment & env = {} ); |
---|
| 131 | |
---|
[906e24d] | 132 | /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value. |
---|
| 133 | Type * extractResultType( FunctionType * functionType ); |
---|
[54e41b3] | 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 ); |
---|
[906e24d] | 136 | |
---|
[a32b204] | 137 | // in CommonType.cc |
---|
[6f096d2] | 138 | Type * commonType( Type * type1, Type * type2, bool widenFirst, bool widenSecond, const SymTab::Indexer & indexer, TypeEnvironment & env, const OpenVarSet & openVars ); |
---|
[ee574a2] | 139 | ast::ptr< ast::Type > commonType( |
---|
[ef1da0e2] | 140 | const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2, |
---|
| 141 | ast::TypeEnvironment & env, ast::AssertionSet & need, ast::AssertionSet & have, |
---|
| 142 | const ast::OpenVarSet & open, WidenMode widen, const ast::SymbolTable & symtab |
---|
| 143 | ); |
---|
| 144 | // in Unify.cc |
---|
| 145 | std::vector< ast::ptr< ast::Type > > flattenList( |
---|
| 146 | const std::vector< ast::ptr< ast::Type > > & src, ast::TypeEnvironment & env |
---|
| 147 | ); |
---|
[a32b204] | 148 | |
---|
| 149 | // in PolyCost.cc |
---|
[6f096d2] | 150 | int polyCost( Type * type, const TypeEnvironment & env, const SymTab::Indexer & indexer ); |
---|
[7870799] | 151 | int polyCost( |
---|
[9d5089e] | 152 | const ast::Type * type, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env ); |
---|
[a32b204] | 153 | |
---|
[1dd1bd2] | 154 | // in SpecCost.cc |
---|
[6f096d2] | 155 | int specCost( Type * type ); |
---|
[9d5089e] | 156 | int specCost( const ast::Type * type ); |
---|
[1dd1bd2] | 157 | |
---|
[a32b204] | 158 | // in Occurs.cc |
---|
[85dac33] | 159 | bool occurs( const Type * type, const std::string & varName, const TypeEnvironment & env ); |
---|
[d76c588] | 160 | // new AST version in TypeEnvironment.cpp (only place it was used in old AST) |
---|
[906e24d] | 161 | |
---|
[7870799] | 162 | template<typename Iter> |
---|
[6f096d2] | 163 | bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment & env ) { |
---|
[9ad2f9f] | 164 | while ( begin != end ) { |
---|
| 165 | if ( occurs( ty, *begin, env ) ) return true; |
---|
| 166 | ++begin; |
---|
| 167 | } |
---|
| 168 | return false; |
---|
| 169 | } |
---|
| 170 | |
---|
[6d2f993] | 171 | // in AlternativeFinder.cc |
---|
[a181494] | 172 | void referenceToRvalueConversion( Expression *& expr, Cost & cost ); |
---|
[9d5089e] | 173 | // in CandidateFinder.cpp |
---|
[d76c588] | 174 | const ast::Expr * referenceToRvalueConversion( const ast::Expr * expr, Cost & cost ); |
---|
[6d2f993] | 175 | |
---|
[f474e91] | 176 | /// flatten tuple type into list of types |
---|
[906e24d] | 177 | template< typename OutputIterator > |
---|
| 178 | void flatten( Type * type, OutputIterator out ) { |
---|
| 179 | if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) { |
---|
| 180 | for ( Type * t : tupleType->get_types() ) { |
---|
| 181 | flatten( t, out ); |
---|
| 182 | } |
---|
| 183 | } else { |
---|
[6c3a988f] | 184 | *out++ = type->clone(); |
---|
[906e24d] | 185 | } |
---|
| 186 | } |
---|
[f474e91] | 187 | |
---|
| 188 | /// flatten tuple type into existing list of types |
---|
[ef1da0e2] | 189 | inline void flatten( |
---|
[7870799] | 190 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out |
---|
[f474e91] | 191 | ) { |
---|
[7870799] | 192 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { |
---|
[f474e91] | 193 | for ( const ast::Type * t : tupleType->types ) { |
---|
| 194 | flatten( t, out ); |
---|
| 195 | } |
---|
| 196 | } else { |
---|
| 197 | out.emplace_back( type ); |
---|
| 198 | } |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | /// flatten tuple type into list of types |
---|
[ef1da0e2] | 202 | inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { |
---|
[f474e91] | 203 | std::vector< ast::ptr< ast::Type > > out; |
---|
| 204 | out.reserve( type->size() ); |
---|
| 205 | flatten( type, out ); |
---|
| 206 | return out; |
---|
| 207 | } |
---|
[ee574a2] | 208 | |
---|
[ef1da0e2] | 209 | template< typename Iter > |
---|
| 210 | const ast::Type * tupleFromTypes( Iter crnt, Iter end ) { |
---|
| 211 | std::vector< ast::ptr< ast::Type > > types; |
---|
| 212 | while ( crnt != end ) { |
---|
| 213 | // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure |
---|
| 214 | // that this results in a flat tuple |
---|
| 215 | flatten( *crnt, types ); |
---|
| 216 | |
---|
| 217 | ++crnt; |
---|
| 218 | } |
---|
| 219 | |
---|
| 220 | |
---|
| 221 | return new ast::TupleType{ std::move(types) }; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | inline const ast::Type * tupleFromTypes( |
---|
| 225 | const std::vector< ast::ptr< ast::Type > > & tys |
---|
| 226 | ) { |
---|
| 227 | return tupleFromTypes( tys.begin(), tys.end() ); |
---|
| 228 | } |
---|
| 229 | |
---|
| 230 | |
---|
| 231 | |
---|
[ee574a2] | 232 | // in TypeEnvironment.cc |
---|
[85dac33] | 233 | bool isFtype( const Type * type ); |
---|
[51b7345] | 234 | } // namespace ResolvExpr |
---|
| 235 | |
---|
[ee574a2] | 236 | namespace ast { |
---|
| 237 | // in TypeEnvironment.cpp |
---|
| 238 | bool isFtype( const ast::Type * type ); |
---|
| 239 | } // namespace ast |
---|
| 240 | |
---|
[a32b204] | 241 | // Local Variables: // |
---|
| 242 | // tab-width: 4 // |
---|
| 243 | // mode: c++ // |
---|
| 244 | // compile-command: "make install" // |
---|
| 245 | // End: // |
---|