[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 |
---|
[5bf3976] | 12 | // Last Modified On : Wed Jan 18 11:54:00 2023 |
---|
| 13 | // Update Count : 7 |
---|
[a32b204] | 14 | // |
---|
[51b7345] | 15 | |
---|
[6b0b624] | 16 | #pragma once |
---|
[51b7345] | 17 | |
---|
[bd4f2e9] | 18 | #include <vector> |
---|
| 19 | |
---|
[54e41b3] | 20 | #include "AST/Type.hpp" |
---|
[51b7345] | 21 | #include "SynTree/Type.h" |
---|
[6f096d2] | 22 | |
---|
| 23 | namespace SymTab { |
---|
| 24 | class Indexer; |
---|
| 25 | } |
---|
[51b7345] | 26 | |
---|
| 27 | namespace ResolvExpr { |
---|
[a0d1f1c] | 28 | class TypeEnvironment; |
---|
| 29 | |
---|
[a32b204] | 30 | // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by |
---|
| 31 | // picking one element out of each set |
---|
| 32 | template< typename InputIterator, typename OutputIterator > |
---|
| 33 | void combos( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
| 34 | typedef typename InputIterator::value_type SetType; |
---|
[bd4f2e9] | 35 | typedef typename std::vector< typename SetType::value_type > ListType; |
---|
[906e24d] | 36 | |
---|
[a32b204] | 37 | if ( begin == end ) { |
---|
| 38 | *out++ = ListType(); |
---|
| 39 | return; |
---|
| 40 | } // if |
---|
[906e24d] | 41 | |
---|
[a32b204] | 42 | InputIterator current = begin; |
---|
| 43 | begin++; |
---|
[51b7345] | 44 | |
---|
[bd4f2e9] | 45 | std::vector< ListType > recursiveResult; |
---|
[a32b204] | 46 | combos( begin, end, back_inserter( recursiveResult ) ); |
---|
[906e24d] | 47 | |
---|
[bd4f2e9] | 48 | for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { |
---|
| 49 | ListType result; |
---|
| 50 | std::back_insert_iterator< ListType > inserter = back_inserter( result ); |
---|
| 51 | *inserter++ = j; |
---|
| 52 | std::copy( i.begin(), i.end(), inserter ); |
---|
| 53 | *out++ = result; |
---|
| 54 | } |
---|
[a32b204] | 55 | } |
---|
[906e24d] | 56 | |
---|
[a32b204] | 57 | // in Occurs.cc |
---|
[85dac33] | 58 | bool occurs( const Type * type, const std::string & varName, const TypeEnvironment & env ); |
---|
[d76c588] | 59 | // new AST version in TypeEnvironment.cpp (only place it was used in old AST) |
---|
[906e24d] | 60 | |
---|
[7870799] | 61 | template<typename Iter> |
---|
[6f096d2] | 62 | bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment & env ) { |
---|
[9ad2f9f] | 63 | while ( begin != end ) { |
---|
| 64 | if ( occurs( ty, *begin, env ) ) return true; |
---|
| 65 | ++begin; |
---|
| 66 | } |
---|
| 67 | return false; |
---|
| 68 | } |
---|
| 69 | |
---|
[f474e91] | 70 | /// flatten tuple type into list of types |
---|
[906e24d] | 71 | template< typename OutputIterator > |
---|
| 72 | void flatten( Type * type, OutputIterator out ) { |
---|
| 73 | if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) { |
---|
| 74 | for ( Type * t : tupleType->get_types() ) { |
---|
| 75 | flatten( t, out ); |
---|
| 76 | } |
---|
| 77 | } else { |
---|
[6c3a988f] | 78 | *out++ = type->clone(); |
---|
[906e24d] | 79 | } |
---|
| 80 | } |
---|
[f474e91] | 81 | |
---|
| 82 | /// flatten tuple type into existing list of types |
---|
[ef1da0e2] | 83 | inline void flatten( |
---|
[7870799] | 84 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out |
---|
[f474e91] | 85 | ) { |
---|
[7870799] | 86 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { |
---|
[f474e91] | 87 | for ( const ast::Type * t : tupleType->types ) { |
---|
| 88 | flatten( t, out ); |
---|
| 89 | } |
---|
| 90 | } else { |
---|
| 91 | out.emplace_back( type ); |
---|
| 92 | } |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | /// flatten tuple type into list of types |
---|
[ef1da0e2] | 96 | inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { |
---|
[f474e91] | 97 | std::vector< ast::ptr< ast::Type > > out; |
---|
| 98 | out.reserve( type->size() ); |
---|
| 99 | flatten( type, out ); |
---|
| 100 | return out; |
---|
| 101 | } |
---|
[ee574a2] | 102 | |
---|
[ef1da0e2] | 103 | template< typename Iter > |
---|
| 104 | const ast::Type * tupleFromTypes( Iter crnt, Iter end ) { |
---|
| 105 | std::vector< ast::ptr< ast::Type > > types; |
---|
| 106 | while ( crnt != end ) { |
---|
| 107 | // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure |
---|
| 108 | // that this results in a flat tuple |
---|
| 109 | flatten( *crnt, types ); |
---|
| 110 | |
---|
| 111 | ++crnt; |
---|
| 112 | } |
---|
| 113 | |
---|
| 114 | return new ast::TupleType{ std::move(types) }; |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | inline const ast::Type * tupleFromTypes( |
---|
| 118 | const std::vector< ast::ptr< ast::Type > > & tys |
---|
| 119 | ) { |
---|
| 120 | return tupleFromTypes( tys.begin(), tys.end() ); |
---|
| 121 | } |
---|
| 122 | |
---|
[ee574a2] | 123 | // in TypeEnvironment.cc |
---|
[85dac33] | 124 | bool isFtype( const Type * type ); |
---|
[51b7345] | 125 | } // namespace ResolvExpr |
---|
| 126 | |
---|
[ee574a2] | 127 | namespace ast { |
---|
| 128 | // in TypeEnvironment.cpp |
---|
| 129 | bool isFtype( const ast::Type * type ); |
---|
| 130 | } // namespace ast |
---|
| 131 | |
---|
[a32b204] | 132 | // Local Variables: // |
---|
| 133 | // tab-width: 4 // |
---|
| 134 | // mode: c++ // |
---|
| 135 | // compile-command: "make install" // |
---|
| 136 | // End: // |
---|