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 | // |
---|
7 | // typeops.h -- |
---|
8 | // |
---|
9 | // Author : Richard C. Bilson |
---|
10 | // Created On : Sun May 17 07:28:22 2015 |
---|
11 | // Last Modified By : Andrew Beach |
---|
12 | // Last Modified On : Wed Jan 18 11:54:00 2023 |
---|
13 | // Update Count : 7 |
---|
14 | // |
---|
15 | |
---|
16 | #pragma once |
---|
17 | |
---|
18 | #include <vector> |
---|
19 | |
---|
20 | #include "AST/Type.hpp" |
---|
21 | |
---|
22 | namespace ResolvExpr { |
---|
23 | class TypeEnvironment; |
---|
24 | |
---|
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; |
---|
30 | typedef typename std::vector< typename SetType::value_type > ListType; |
---|
31 | |
---|
32 | if ( begin == end ) { |
---|
33 | *out++ = ListType(); |
---|
34 | return; |
---|
35 | } // if |
---|
36 | |
---|
37 | InputIterator current = begin; |
---|
38 | begin++; |
---|
39 | |
---|
40 | std::vector< ListType > recursiveResult; |
---|
41 | combos( begin, end, back_inserter( recursiveResult ) ); |
---|
42 | |
---|
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 | } |
---|
50 | } |
---|
51 | |
---|
52 | /// flatten tuple type into existing list of types |
---|
53 | inline void flatten( |
---|
54 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out |
---|
55 | ) { |
---|
56 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { |
---|
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 |
---|
66 | inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { |
---|
67 | std::vector< ast::ptr< ast::Type > > out; |
---|
68 | out.reserve( type->size() ); |
---|
69 | flatten( type, out ); |
---|
70 | return out; |
---|
71 | } |
---|
72 | |
---|
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 | } |
---|
92 | } // namespace ResolvExpr |
---|
93 | |
---|
94 | namespace ast { |
---|
95 | // in TypeEnvironment.cpp |
---|
96 | bool isFtype( const ast::Type * type ); |
---|
97 | } // namespace ast |
---|
98 | |
---|
99 | // Local Variables: // |
---|
100 | // tab-width: 4 // |
---|
101 | // mode: c++ // |
---|
102 | // compile-command: "make install" // |
---|
103 | // End: // |
---|