source: src/ResolvExpr/typeops.h @ da7454c

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since da7454c was fb2bde4, checked in by Andrew Beach <ajbeach@…>, 5 years ago

ConversionCost? has been ported to the new AST.

  • Property mode set to 100644
File size: 7.2 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 : Peter A. Buhr
12// Last Modified On : Fri Feb  8 09:30:34 2019
13// Update Count     : 4
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#include "SymTab/Indexer.h"
31
32namespace ResolvExpr {
33        // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by
34        // picking one element out of each set
35        template< typename InputIterator, typename OutputIterator >
36        void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
37                typedef typename InputIterator::value_type SetType;
38                typedef typename std::vector< typename SetType::value_type > ListType;
39
40                if ( begin == end )     {
41                        *out++ = ListType();
42                        return;
43                } // if
44
45                InputIterator current = begin;
46                begin++;
47
48                std::vector< ListType > recursiveResult;
49                combos( begin, end, back_inserter( recursiveResult ) );
50
51                for ( const auto& i : recursiveResult ) for ( const auto& j : *current ) {
52                        ListType result;
53                        std::back_insert_iterator< ListType > inserter = back_inserter( result );
54                        *inserter++ = j;
55                        std::copy( i.begin(), i.end(), inserter );
56                        *out++ = result;
57                }
58        }
59
60        // in AdjustExprType.cc
61        /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function
62        void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
63
64        /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function using empty TypeEnvironment and Indexer
65        void adjustExprType( Type *& type );
66
67        template< typename ForwardIterator >
68        void adjustExprTypeList( ForwardIterator begin, ForwardIterator end, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
69                while ( begin != end ) {
70                        adjustExprType( *begin++, env, indexer );
71                } // while
72        }
73
74        /// Replaces array types with equivalent pointer, and function types with a pointer-to-function
75        const ast::Type * adjustExprType( 
76                const ast::Type * type, const ast::TypeEnvironment & env, const ast::SymbolTable & symtab );
77
78        // in CastCost.cc
79        Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
80        Cost castCost( 
81                const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab, 
82                const ast::TypeEnvironment & env );
83
84        // in ConversionCost.cc
85        Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
86        Cost conversionCost( 
87                const ast::Type * src, const ast::Type * dst, const ast::SymbolTable & symtab, 
88                const ast::TypeEnvironment & env );
89
90        // in AlternativeFinder.cc
91        Cost computeConversionCost( Type *actualType, Type *formalType, 
92                const SymTab::Indexer &indexer, const TypeEnvironment &env );
93
94        // in PtrsAssignable.cc
95        int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env );
96        int ptrsAssignable( const ast::Type * src, const ast::Type * dst,
97                const ast::TypeEnvironment & env );
98
99        // in PtrsCastable.cc
100        int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
101
102        // in Unify.cc
103        bool typesCompatible( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
104        bool typesCompatibleIgnoreQualifiers( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
105
106        inline bool typesCompatible( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
107                TypeEnvironment env;
108                return typesCompatible( t1, t2, indexer, env );
109        }
110
111        inline bool typesCompatibleIgnoreQualifiers( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
112                TypeEnvironment env;
113                return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env );
114        }
115
116        bool typesCompatible( 
117                const ast::Type *, const ast::Type *, const ast::SymbolTable & symtab = {}, 
118                const ast::TypeEnvironment & env = {} );
119       
120        bool typesCompatibleIgnoreQualifiers(
121                const ast::Type *, const ast::Type *, const ast::SymbolTable &, 
122                const ast::TypeEnvironment & env = {} );
123
124        /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value.
125        Type * extractResultType( FunctionType * functionType );
126        /// Creates or extracts the type represented by the list of returns in a `FunctionType`.
127        ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func );
128
129        // in CommonType.cc
130        Type * commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
131        ast::ptr< ast::Type > commonType(
132                const ast::ptr< ast::Type > & type1, const ast::ptr< ast::Type > & type2, WidenMode widen, 
133                const ast::SymbolTable & symtab, ast::TypeEnvironment & env, const ast::OpenVarSet & open );
134
135        // in PolyCost.cc
136        int polyCost( Type *type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
137        int polyCost( 
138                const ast::Type * type, const ast::SymbolTable & symtab, const ast::TypeEnvironment & env );
139
140        // in SpecCost.cc
141        int specCost( Type *type );
142        int specCost( const ast::Type * type );
143
144        // in Occurs.cc
145        bool occurs( Type *type, std::string varName, const TypeEnvironment &env );
146        // new AST version in TypeEnvironment.cpp (only place it was used in old AST)
147
148        template<typename Iter> 
149        bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment &env ) {
150                while ( begin != end ) {
151                        if ( occurs( ty, *begin, env ) ) return true;
152                        ++begin;
153                }
154                return false;
155        }
156
157        // in AlternativeFinder.cc
158        void referenceToRvalueConversion( Expression *& expr, Cost & cost );
159        // in CandidateFinder.cpp
160        const ast::Expr * referenceToRvalueConversion( const ast::Expr * expr, Cost & cost );
161
162        /// flatten tuple type into list of types
163        template< typename OutputIterator >
164        void flatten( Type * type, OutputIterator out ) {
165                if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
166                        for ( Type * t : tupleType->get_types() ) {
167                                flatten( t, out );
168                        }
169                } else {
170                        *out++ = type->clone();
171                }
172        }
173
174        /// flatten tuple type into existing list of types
175        static inline void flatten( 
176                const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out
177        ) {
178                if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) {       
179                        for ( const ast::Type * t : tupleType->types ) {
180                                flatten( t, out );
181                        }
182                } else {
183                        out.emplace_back( type );
184                }
185        }
186
187        /// flatten tuple type into list of types
188        static inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) {
189                std::vector< ast::ptr< ast::Type > > out;
190                out.reserve( type->size() );
191                flatten( type, out );
192                return out;
193        }
194
195        // in TypeEnvironment.cc
196        bool isFtype( Type *type );
197} // namespace ResolvExpr
198
199namespace ast {
200        // in TypeEnvironment.cpp
201        bool isFtype( const ast::Type * type );
202} // namespace ast
203
204// Local Variables: //
205// tab-width: 4 //
206// mode: c++ //
207// compile-command: "make install" //
208// End: //
Note: See TracBrowser for help on using the repository browser.