source: src/ResolvExpr/typeops.h @ 4ae2364

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 4ae2364 was f474e91, checked in by Aaron Moss <a3moss@…>, 5 years ago

Port unification calculations to new AST

  • Property mode set to 100644
File size: 6.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        // in CastCost.cc
75        Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
76
77        // in ConversionCost.cc
78        Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
79
80        // in AlternativeFinder.cc
81        Cost computeConversionCost( Type *actualType, Type *formalType, 
82                const SymTab::Indexer &indexer, const TypeEnvironment &env );
83
84        // in PtrsAssignable.cc
85        int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env );
86
87        // in PtrsCastable.cc
88        int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
89
90        // in Unify.cc
91        bool isFtype( Type *type );
92        bool typesCompatible( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
93        bool typesCompatibleIgnoreQualifiers( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
94
95        inline bool typesCompatible( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
96                TypeEnvironment env;
97                return typesCompatible( t1, t2, indexer, env );
98        }
99
100        inline bool typesCompatibleIgnoreQualifiers( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
101                TypeEnvironment env;
102                return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env );
103        }
104
105        bool typesCompatible( 
106                const ast::Type *, const ast::Type *, const ast::SymbolTable &, 
107                const ast::TypeEnvironment & env = {} );
108       
109        bool typesCompatibleIgnoreQualifiers(
110                const ast::Type *, const ast::Type *, const ast::SymbolTable &, 
111                const ast::TypeEnvironment & env = {} );
112
113        /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value.
114        Type * extractResultType( FunctionType * functionType );
115        /// Creates or extracts the type represented by the list of returns in a `FunctionType`.
116        ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func );
117
118        // in CommonType.cc
119        Type * commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
120        const ast::Type * commonType(
121                const ast::Type * type1, const ast::Type * type2, WidenMode widen, 
122                const ast::SymbolTable & symtab, ast::TypeEnvironment & env, const ast::OpenVarSet & open );
123
124        // in PolyCost.cc
125        int polyCost( Type *type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
126
127        // in SpecCost.cc
128        int specCost( Type *type );
129
130        // in Occurs.cc
131        bool occurs( Type *type, std::string varName, const TypeEnvironment &env );
132        // new AST version in TypeEnvironment.cpp (only place it was used in old AST)
133
134        template<typename Iter> 
135        bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment &env ) {
136                while ( begin != end ) {
137                        if ( occurs( ty, *begin, env ) ) return true;
138                        ++begin;
139                }
140                return false;
141        }
142
143        // in AlternativeFinder.cc
144        void referenceToRvalueConversion( Expression *& expr, Cost & cost );
145        const ast::Expr * referenceToRvalueConversion( const ast::Expr * expr, Cost & cost );
146
147        /// flatten tuple type into list of types
148        template< typename OutputIterator >
149        void flatten( Type * type, OutputIterator out ) {
150                if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
151                        for ( Type * t : tupleType->get_types() ) {
152                                flatten( t, out );
153                        }
154                } else {
155                        *out++ = type->clone();
156                }
157        }
158
159        /// flatten tuple type into existing list of types
160        static inline void flatten( 
161                const ast::Type * type, std::vector< ast::ptr< ast::Type > > & out
162        ) {
163                if ( auto tupleType = dynamic_cast< const ast::TupleType * >( type ) ) {       
164                        for ( const ast::Type * t : tupleType->types ) {
165                                flatten( t, out );
166                        }
167                } else {
168                        out.emplace_back( type );
169                }
170        }
171
172        /// flatten tuple type into list of types
173        static inline std::vector< ast::ptr< ast::Type > > flatten( const ast::Type * type ) {
174                std::vector< ast::ptr< ast::Type > > out;
175                out.reserve( type->size() );
176                flatten( type, out );
177                return out;
178        }
179} // namespace ResolvExpr
180
181// Local Variables: //
182// tab-width: 4 //
183// mode: c++ //
184// compile-command: "make install" //
185// End: //
Note: See TracBrowser for help on using the repository browser.