| 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 "AST/Node.hpp"
 | 
|---|
| 21 | #include "AST/Type.hpp"
 | 
|---|
| 22 | #include "SynTree/SynTree.h"
 | 
|---|
| 23 | #include "SynTree/Type.h"
 | 
|---|
| 24 | #include "SymTab/Indexer.h"
 | 
|---|
| 25 | #include "Cost.h"
 | 
|---|
| 26 | #include "TypeEnvironment.h"
 | 
|---|
| 27 | 
 | 
|---|
| 28 | namespace ResolvExpr {
 | 
|---|
| 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 |         // in AdjustExprType.cc
 | 
|---|
| 57 |         /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function
 | 
|---|
| 58 |         void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 59 | 
 | 
|---|
| 60 |         /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function using empty TypeEnvironment and Indexer
 | 
|---|
| 61 |         void adjustExprType( Type *& type );
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         template< typename ForwardIterator >
 | 
|---|
| 64 |         void adjustExprTypeList( ForwardIterator begin, ForwardIterator end, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
| 65 |                 while ( begin != end ) {
 | 
|---|
| 66 |                         adjustExprType( *begin++, env, indexer );
 | 
|---|
| 67 |                 } // while
 | 
|---|
| 68 |         }
 | 
|---|
| 69 | 
 | 
|---|
| 70 |         // in CastCost.cc
 | 
|---|
| 71 |         Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 72 | 
 | 
|---|
| 73 |         // in ConversionCost.cc
 | 
|---|
| 74 |         Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 75 | 
 | 
|---|
| 76 |         // in AlternativeFinder.cc
 | 
|---|
| 77 |         Cost computeConversionCost( Type *actualType, Type *formalType, 
 | 
|---|
| 78 |                 const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 79 | 
 | 
|---|
| 80 |         // in PtrsAssignable.cc
 | 
|---|
| 81 |         int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env );
 | 
|---|
| 82 | 
 | 
|---|
| 83 |         // in PtrsCastable.cc
 | 
|---|
| 84 |         int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 85 | 
 | 
|---|
| 86 |         // in Unify.cc
 | 
|---|
| 87 |         bool isFtype( Type *type );
 | 
|---|
| 88 |         bool typesCompatible( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 89 |         bool typesCompatibleIgnoreQualifiers( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 90 | 
 | 
|---|
| 91 |         inline bool typesCompatible( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
 | 
|---|
| 92 |                 TypeEnvironment env;
 | 
|---|
| 93 |                 return typesCompatible( t1, t2, indexer, env );
 | 
|---|
| 94 |         }
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         inline bool typesCompatibleIgnoreQualifiers( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
 | 
|---|
| 97 |                 TypeEnvironment env;
 | 
|---|
| 98 |                 return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env );
 | 
|---|
| 99 |         }
 | 
|---|
| 100 | 
 | 
|---|
| 101 |         /// creates the type represented by the list of returnVals in a FunctionType. The caller owns the return value.
 | 
|---|
| 102 |         Type * extractResultType( FunctionType * functionType );
 | 
|---|
| 103 |         /// Creates or extracts the type represented by the list of returns in a `FunctionType`.
 | 
|---|
| 104 |         ast::ptr<ast::Type> extractResultType( const ast::FunctionType * func );
 | 
|---|
| 105 | 
 | 
|---|
| 106 |         // in CommonType.cc
 | 
|---|
| 107 |         Type * commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
 | 
|---|
| 108 | 
 | 
|---|
| 109 |         // in PolyCost.cc
 | 
|---|
| 110 |         int polyCost( Type *type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 111 | 
 | 
|---|
| 112 |         // in SpecCost.cc
 | 
|---|
| 113 |         int specCost( Type *type );
 | 
|---|
| 114 | 
 | 
|---|
| 115 |         // in Occurs.cc
 | 
|---|
| 116 |         bool occurs( Type *type, std::string varName, const TypeEnvironment &env );
 | 
|---|
| 117 | 
 | 
|---|
| 118 |         template<typename Iter> 
 | 
|---|
| 119 |         bool occursIn( Type* ty, Iter begin, Iter end, const TypeEnvironment &env ) {
 | 
|---|
| 120 |                 while ( begin != end ) {
 | 
|---|
| 121 |                         if ( occurs( ty, *begin, env ) ) return true;
 | 
|---|
| 122 |                         ++begin;
 | 
|---|
| 123 |                 }
 | 
|---|
| 124 |                 return false;
 | 
|---|
| 125 |         }
 | 
|---|
| 126 | 
 | 
|---|
| 127 |         // in AlternativeFinder.cc
 | 
|---|
| 128 |         void referenceToRvalueConversion( Expression *& expr, Cost & cost );
 | 
|---|
| 129 | 
 | 
|---|
| 130 |         // flatten tuple type into list of types
 | 
|---|
| 131 |         template< typename OutputIterator >
 | 
|---|
| 132 |         void flatten( Type * type, OutputIterator out ) {
 | 
|---|
| 133 |                 if ( TupleType * tupleType = dynamic_cast< TupleType * >( type ) ) {
 | 
|---|
| 134 |                         for ( Type * t : tupleType->get_types() ) {
 | 
|---|
| 135 |                                 flatten( t, out );
 | 
|---|
| 136 |                         }
 | 
|---|
| 137 |                 } else {
 | 
|---|
| 138 |                         *out++ = type->clone();
 | 
|---|
| 139 |                 }
 | 
|---|
| 140 |         }
 | 
|---|
| 141 | } // namespace ResolvExpr
 | 
|---|
| 142 | 
 | 
|---|
| 143 | // Local Variables: //
 | 
|---|
| 144 | // tab-width: 4 //
 | 
|---|
| 145 | // mode: c++ //
 | 
|---|
| 146 | // compile-command: "make install" //
 | 
|---|
| 147 | // End: //
 | 
|---|