source: src/ResolvExpr/typeops.h@ 418882af

ADT ast-experimental
Last change on this file since 418882af was 5bf3976, checked in by Andrew Beach <ajbeach@…>, 3 years ago

Header Clean-Up: Created new headers for new AST typeops and moved declarations.

  • Property mode set to 100644
File size: 3.7 KB
Line 
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 "Cost.h"
21#include "TypeEnvironment.h"
22#include "WidenMode.h"
23#include "AST/Fwd.hpp"
24#include "AST/Node.hpp"
25#include "AST/SymbolTable.hpp"
26#include "AST/Type.hpp"
27#include "AST/TypeEnvironment.hpp"
28#include "SynTree/SynTree.h"
29#include "SynTree/Type.h"
30
31namespace SymTab {
32 class Indexer;
33}
34
35namespace ResolvExpr {
36 // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by
37 // picking one element out of each set
38 template< typename InputIterator, typename OutputIterator >
39 void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
40 typedef typename InputIterator::value_type SetType;
41 typedef typename std::vector< typename SetType::value_type > ListType;
42
43 if ( begin == end ) {
44 *out++ = ListType();
45 return;
46 } // if
47
48 InputIterator current = begin;
49 begin++;
50
51 std::vector< ListType > recursiveResult;
52 combos( begin, end, back_inserter( recursiveResult ) );
53
54 for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) {
55 ListType result;
56 std::back_insert_iterator< ListType > inserter = back_inserter( result );
57 *inserter++ = j;
58 std::copy( i.begin(), i.end(), inserter );
59 *out++ = result;
60 }
61 }
62
63 // in Occurs.cc
64 bool occurs( const Type * type, const std::string & varName, const TypeEnvironment & env );
65 // new AST version in TypeEnvironment.cpp (only place it was used in old AST)
66
67 template<typename Iter>
68 bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment & env ) {
69 while ( begin != end ) {
70 if ( occurs( ty, *begin, env ) ) return true;
71 ++begin;
72 }
73 return false;
74 }
75
76 /// flatten tuple type into list of types
77 template< typename OutputIterator >
78 void flatten( Type * type, OutputIterator out ) {
79 if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
80 for ( Type * t : tupleType->get_types() ) {
81 flatten( t, out );
82 }
83 } else {
84 *out++ = type->clone();
85 }
86 }
87
88 /// flatten tuple type into existing list of types
89 inline void flatten(
90 const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out
91 ) {
92 if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) {
93 for ( const ast::Type * t : tupleType->types ) {
94 flatten( t, out );
95 }
96 } else {
97 out.emplace_back( type );
98 }
99 }
100
101 /// flatten tuple type into list of types
102 inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) {
103 std::vector< ast::ptr< ast::Type > > out;
104 out.reserve( type->size() );
105 flatten( type, out );
106 return out;
107 }
108
109 template< typename Iter >
110 const ast::Type * tupleFromTypes( Iter crnt, Iter end ) {
111 std::vector< ast::ptr< ast::Type > > types;
112 while ( crnt != end ) {
113 // it is guaranteed that a ttype variable will be bound to a flat tuple, so ensure
114 // that this results in a flat tuple
115 flatten( *crnt, types );
116
117 ++crnt;
118 }
119
120 return new ast::TupleType{ std::move(types) };
121 }
122
123 inline const ast::Type * tupleFromTypes(
124 const std::vector< ast::ptr< ast::Type > > & tys
125 ) {
126 return tupleFromTypes( tys.begin(), tys.end() );
127 }
128
129 // in TypeEnvironment.cc
130 bool isFtype( const Type * type );
131} // namespace ResolvExpr
132
133namespace ast {
134 // in TypeEnvironment.cpp
135 bool isFtype( const ast::Type * type );
136} // namespace ast
137
138// Local Variables: //
139// tab-width: 4 //
140// mode: c++ //
141// compile-command: "make install" //
142// End: //
Note: See TracBrowser for help on using the repository browser.