[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 | // |
---|
[c92bdcc] | 7 | // Typeops.hpp -- |
---|
[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" |
---|
[6f096d2] | 21 | |
---|
[51b7345] | 22 | namespace ResolvExpr { |
---|
[906e24d] | 23 | |
---|
[13de4478] | 24 | class TypeEnvironment; |
---|
[f474e91] | 25 | |
---|
[13de4478] | 26 | // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by |
---|
| 27 | // picking one element out of each set |
---|
| 28 | template< typename InputIterator, typename OutputIterator > |
---|
| 29 | void combos( InputIterator begin, InputIterator end, OutputIterator out ) { |
---|
| 30 | typedef typename InputIterator::value_type SetType; |
---|
| 31 | typedef typename std::vector< typename SetType::value_type > ListType; |
---|
[ee574a2] | 32 | |
---|
[13de4478] | 33 | if ( begin == end ) { |
---|
| 34 | *out++ = ListType(); |
---|
| 35 | return; |
---|
| 36 | } // if |
---|
[ef1da0e2] | 37 | |
---|
[13de4478] | 38 | InputIterator current = begin; |
---|
| 39 | begin++; |
---|
[ef1da0e2] | 40 | |
---|
[13de4478] | 41 | std::vector< ListType > recursiveResult; |
---|
| 42 | combos( begin, end, back_inserter( recursiveResult ) ); |
---|
[ef1da0e2] | 43 | |
---|
[13de4478] | 44 | for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) { |
---|
| 45 | ListType result; |
---|
| 46 | std::back_insert_iterator< ListType > inserter = back_inserter( result ); |
---|
| 47 | *inserter++ = j; |
---|
| 48 | std::copy( i.begin(), i.end(), inserter ); |
---|
| 49 | *out++ = result; |
---|
[ef1da0e2] | 50 | } |
---|
[13de4478] | 51 | } |
---|
| 52 | |
---|
| 53 | /// Flatten tuple type into existing list of types. |
---|
| 54 | inline void flatten( |
---|
| 55 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out |
---|
| 56 | ) { |
---|
| 57 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { |
---|
| 58 | for ( const ast::Type * t : tupleType->types ) { |
---|
| 59 | flatten( t, out ); |
---|
| 60 | } |
---|
| 61 | } else { |
---|
| 62 | out.emplace_back( type ); |
---|
| 63 | } |
---|
| 64 | } |
---|
| 65 | |
---|
| 66 | /// Flatten tuple type into list of types. |
---|
| 67 | inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { |
---|
| 68 | std::vector< ast::ptr< ast::Type > > out; |
---|
| 69 | out.reserve( type->size() ); |
---|
| 70 | flatten( type, out ); |
---|
| 71 | return out; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | template< typename Iter > |
---|
| 75 | const ast::Type * tupleFromTypes( Iter crnt, Iter end ) { |
---|
| 76 | std::vector< ast::ptr< ast::Type > > types; |
---|
| 77 | while ( crnt != end ) { |
---|
| 78 | // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure |
---|
| 79 | // that this results in a flat tuple |
---|
| 80 | flatten( *crnt, types ); |
---|
| 81 | |
---|
| 82 | ++crnt; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | return new ast::TupleType( std::move(types) ); |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | inline const ast::Type * tupleFromTypes( |
---|
| 89 | const std::vector< ast::ptr< ast::Type > > & tys |
---|
| 90 | ) { |
---|
| 91 | return tupleFromTypes( tys.begin(), tys.end() ); |
---|
| 92 | } |
---|
| 93 | |
---|
[51b7345] | 94 | } // namespace ResolvExpr |
---|
| 95 | |
---|
[ee574a2] | 96 | namespace ast { |
---|
| 97 | // in TypeEnvironment.cpp |
---|
| 98 | bool isFtype( const ast::Type * type ); |
---|
| 99 | } // namespace ast |
---|
| 100 | |
---|
[a32b204] | 101 | // Local Variables: // |
---|
| 102 | // tab-width: 4 // |
---|
| 103 | // mode: c++ // |
---|
| 104 | // compile-command: "make install" // |
---|
| 105 | // End: // |
---|