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