source: src/ResolvExpr/Typeops.hpp @ 4db0140

Last change on this file since 4db0140 was c92bdcc, checked in by Andrew Beach <ajbeach@…>, 6 months ago

Updated the rest of the names in src/ (except for the generated files).

  • Property mode set to 100644
File size: 2.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.hpp --
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
22namespace ResolvExpr {
23
24class TypeEnvironment;
25
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
28template< typename InputIterator, typename OutputIterator >
29void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
30        typedef typename InputIterator::value_type SetType;
31        typedef typename std::vector< typename SetType::value_type > ListType;
32
33        if ( begin == end )     {
34                *out++ = ListType();
35                return;
36        } // if
37
38        InputIterator current = begin;
39        begin++;
40
41        std::vector< ListType > recursiveResult;
42        combos( begin, end, back_inserter( recursiveResult ) );
43
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;
50        }
51}
52
53/// Flatten tuple type into existing list of types.
54inline 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.
67inline 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
74template< typename Iter >
75const 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
88inline const ast::Type * tupleFromTypes(
89        const std::vector< ast::ptr< ast::Type > > & tys
90) {
91        return tupleFromTypes( tys.begin(), tys.end() );
92}
93
94} // namespace ResolvExpr
95
96namespace ast {
97        // in TypeEnvironment.cpp
98        bool isFtype( const ast::Type * type );
99} // namespace ast
100
101// Local Variables: //
102// tab-width: 4 //
103// mode: c++ //
104// compile-command: "make install" //
105// End: //
Note: See TracBrowser for help on using the repository browser.