source: src/ResolvExpr/typeops.h @ 27316b4

Last change on this file since 27316b4 was c6b4432, checked in by Andrew Beach <ajbeach@…>, 12 months ago

Remove BaseSyntaxNode? and clean-up.

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