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 | #include "SynTree/Type.h" |
---|
22 | |
---|
23 | namespace SymTab { |
---|
24 | class Indexer; |
---|
25 | } |
---|
26 | |
---|
27 | namespace ResolvExpr { |
---|
28 | class TypeEnvironment; |
---|
29 | |
---|
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; |
---|
35 | typedef typename std::vector< typename SetType::value_type > ListType; |
---|
36 | |
---|
37 | if ( begin == end ) { |
---|
38 | *out++ = ListType(); |
---|
39 | return; |
---|
40 | } // if |
---|
41 | |
---|
42 | InputIterator current = begin; |
---|
43 | begin++; |
---|
44 | |
---|
45 | std::vector< ListType > recursiveResult; |
---|
46 | combos( begin, end, back_inserter( recursiveResult ) ); |
---|
47 | |
---|
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 | } |
---|
55 | } |
---|
56 | |
---|
57 | // in Occurs.cc |
---|
58 | bool occurs( const Type * type, const std::string & varName, const TypeEnvironment & env ); |
---|
59 | // new AST version in TypeEnvironment.cpp (only place it was used in old AST) |
---|
60 | |
---|
61 | template<typename Iter> |
---|
62 | bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment & env ) { |
---|
63 | while ( begin != end ) { |
---|
64 | if ( occurs( ty, *begin, env ) ) return true; |
---|
65 | ++begin; |
---|
66 | } |
---|
67 | return false; |
---|
68 | } |
---|
69 | |
---|
70 | /// flatten tuple type into list of types |
---|
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 { |
---|
78 | *out++ = type->clone(); |
---|
79 | } |
---|
80 | } |
---|
81 | |
---|
82 | /// flatten tuple type into existing list of types |
---|
83 | inline void flatten( |
---|
84 | const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out |
---|
85 | ) { |
---|
86 | if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) { |
---|
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 |
---|
96 | inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) { |
---|
97 | std::vector< ast::ptr< ast::Type > > out; |
---|
98 | out.reserve( type->size() ); |
---|
99 | flatten( type, out ); |
---|
100 | return out; |
---|
101 | } |
---|
102 | |
---|
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 | |
---|
123 | // in TypeEnvironment.cc |
---|
124 | bool isFtype( const Type * type ); |
---|
125 | } // namespace ResolvExpr |
---|
126 | |
---|
127 | namespace ast { |
---|
128 | // in TypeEnvironment.cpp |
---|
129 | bool isFtype( const ast::Type * type ); |
---|
130 | } // namespace ast |
---|
131 | |
---|
132 | // Local Variables: // |
---|
133 | // tab-width: 4 // |
---|
134 | // mode: c++ // |
---|
135 | // compile-command: "make install" // |
---|
136 | // End: // |
---|