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