| 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 : Sun May 17 07:33:11 2015
 | 
|---|
| 13 | // Update Count     : 2
 | 
|---|
| 14 | //
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #ifndef TYPEOPS_H
 | 
|---|
| 17 | #define TYPEOPS_H
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "SynTree/SynTree.h"
 | 
|---|
| 20 | #include "SynTree/Type.h"
 | 
|---|
| 21 | #include "SymTab/Indexer.h"
 | 
|---|
| 22 | #include "Cost.h"
 | 
|---|
| 23 | #include "TypeEnvironment.h"
 | 
|---|
| 24 | 
 | 
|---|
| 25 | namespace ResolvExpr {
 | 
|---|
| 26 |         // combos: takes a list of sets and returns a set of lists representing every possible way of forming a list by
 | 
|---|
| 27 |         // picking one element out of each set
 | 
|---|
| 28 |         template< typename InputIterator, typename OutputIterator >
 | 
|---|
| 29 |         void combos( InputIterator begin, InputIterator end, OutputIterator out ) {
 | 
|---|
| 30 |                 typedef typename InputIterator::value_type SetType;
 | 
|---|
| 31 |                 typedef typename std::list< typename SetType::value_type > ListType;
 | 
|---|
| 32 |   
 | 
|---|
| 33 |                 if ( begin == end )     {
 | 
|---|
| 34 |                         *out++ = ListType();
 | 
|---|
| 35 |                         return;
 | 
|---|
| 36 |                 } // if
 | 
|---|
| 37 |   
 | 
|---|
| 38 |                 InputIterator current = begin;
 | 
|---|
| 39 |                 begin++;
 | 
|---|
| 40 | 
 | 
|---|
| 41 |                 std::list< ListType > recursiveResult;
 | 
|---|
| 42 |                 combos( begin, end, back_inserter( recursiveResult ) );
 | 
|---|
| 43 |   
 | 
|---|
| 44 |                 for ( typename std::list< ListType >::const_iterator i = recursiveResult.begin(); i != recursiveResult.end(); ++i ) {
 | 
|---|
| 45 |                         for ( typename ListType::const_iterator j = current->begin(); j != current->end(); ++j ) {
 | 
|---|
| 46 |                                 ListType result;
 | 
|---|
| 47 |                                 std::back_insert_iterator< ListType > inserter = back_inserter( result );
 | 
|---|
| 48 |                                 *inserter++ = *j;
 | 
|---|
| 49 |                                 std::copy( i->begin(), i->end(), inserter );
 | 
|---|
| 50 |                                 *out++ = result;
 | 
|---|
| 51 |                         } // for
 | 
|---|
| 52 |                 } // for
 | 
|---|
| 53 |         }
 | 
|---|
| 54 |   
 | 
|---|
| 55 |         // in AdjustExprType.cc
 | 
|---|
| 56 |         /// Replaces array types with the equivalent pointer, and function types with a pointer-to-function
 | 
|---|
| 57 |         void adjustExprType( Type *&type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 58 | 
 | 
|---|
| 59 |         template< typename ForwardIterator >
 | 
|---|
| 60 |         void adjustExprTypeList( ForwardIterator begin, ForwardIterator end, const TypeEnvironment &env, const SymTab::Indexer &indexer ) {
 | 
|---|
| 61 |                 while ( begin != end ) {
 | 
|---|
| 62 |                         adjustExprType( *begin++, env, indexer );
 | 
|---|
| 63 |                 } // while
 | 
|---|
| 64 |         }
 | 
|---|
| 65 | 
 | 
|---|
| 66 |         // in CastCost.cc
 | 
|---|
| 67 |         Cost castCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 68 | 
 | 
|---|
| 69 |         template< typename SrcIterator, typename DestIterator >
 | 
|---|
| 70 |         Cost castCostList( SrcIterator srcBegin, SrcIterator srcEnd, DestIterator destBegin, DestIterator destEnd, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
 | 
|---|
| 71 |                 Cost ret;
 | 
|---|
| 72 |                 if ( destBegin == destEnd ) {
 | 
|---|
| 73 |                         if ( srcBegin == srcEnd ) {
 | 
|---|
| 74 |                                 return Cost::zero;
 | 
|---|
| 75 |                         } else {
 | 
|---|
| 76 |                                 return Cost( 0, 0, 1 );
 | 
|---|
| 77 |                         } // if
 | 
|---|
| 78 |                 } // if
 | 
|---|
| 79 |                 while ( srcBegin != srcEnd && destBegin != destEnd ) {
 | 
|---|
| 80 |                         Cost thisCost = castCost( *srcBegin++, *destBegin++, indexer, env );
 | 
|---|
| 81 |                         if ( thisCost == Cost::infinity ) {
 | 
|---|
| 82 |                                 return Cost::infinity;
 | 
|---|
| 83 |                         } // if
 | 
|---|
| 84 |                         ret += thisCost;
 | 
|---|
| 85 |                 } // while
 | 
|---|
| 86 |                 if ( srcBegin == srcEnd && destBegin == destEnd ) {
 | 
|---|
| 87 |                         return ret;
 | 
|---|
| 88 |                 } else {
 | 
|---|
| 89 |                         return Cost::infinity;
 | 
|---|
| 90 |                 } // if
 | 
|---|
| 91 |         }
 | 
|---|
| 92 | 
 | 
|---|
| 93 |         // in ConversionCost.cc
 | 
|---|
| 94 |         Cost conversionCost( Type *src, Type *dest, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 95 | 
 | 
|---|
| 96 |         template< typename SrcIterator, typename DestIterator >
 | 
|---|
| 97 |         Cost conversionCostList( SrcIterator srcBegin, SrcIterator srcEnd, DestIterator destBegin, DestIterator destEnd, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
 | 
|---|
| 98 |                 Cost ret;
 | 
|---|
| 99 |                 while ( srcBegin != srcEnd && destBegin != destEnd ) {
 | 
|---|
| 100 |                         Cost thisCost = conversionCost( *srcBegin++, *destBegin++, indexer, env );
 | 
|---|
| 101 |                         if ( thisCost == Cost::infinity ) {
 | 
|---|
| 102 |                                 return Cost::infinity;
 | 
|---|
| 103 |                         } // if
 | 
|---|
| 104 |                         ret += thisCost;
 | 
|---|
| 105 |                 } // while
 | 
|---|
| 106 |                 if ( srcBegin == srcEnd && destBegin == destEnd ) {
 | 
|---|
| 107 |                         return ret;
 | 
|---|
| 108 |                 } else {
 | 
|---|
| 109 |                         return Cost::infinity;
 | 
|---|
| 110 |                 } // if
 | 
|---|
| 111 |         }
 | 
|---|
| 112 | 
 | 
|---|
| 113 |         // in PtrsAssignable.cc
 | 
|---|
| 114 |         int ptrsAssignable( Type *src, Type *dest, const TypeEnvironment &env );
 | 
|---|
| 115 | 
 | 
|---|
| 116 |         // in PtrsCastable.cc
 | 
|---|
| 117 |         int ptrsCastable( Type *src, Type *dest, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 118 | 
 | 
|---|
| 119 |         // in Unify.cc
 | 
|---|
| 120 |         bool isFtype( Type *type, const SymTab::Indexer &indexer );
 | 
|---|
| 121 |         bool typesCompatible( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 122 |         bool typesCompatibleIgnoreQualifiers( Type *, Type *, const SymTab::Indexer &indexer, const TypeEnvironment &env );
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         inline bool typesCompatible( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
 | 
|---|
| 125 |                 TypeEnvironment env;
 | 
|---|
| 126 |                 return typesCompatible( t1, t2, indexer, env );
 | 
|---|
| 127 |         }
 | 
|---|
| 128 | 
 | 
|---|
| 129 |         inline bool typesCompatibleIgnoreQualifiers( Type *t1, Type *t2, const SymTab::Indexer &indexer ) {
 | 
|---|
| 130 |                 TypeEnvironment env;
 | 
|---|
| 131 |                 return typesCompatibleIgnoreQualifiers( t1, t2, indexer, env );
 | 
|---|
| 132 |         }
 | 
|---|
| 133 | 
 | 
|---|
| 134 |         template< typename Container1, typename Container2 >
 | 
|---|
| 135 |         bool typesCompatibleList( Container1 &c1, Container2 &c2, const SymTab::Indexer &indexer, const TypeEnvironment &env ) {
 | 
|---|
| 136 |                 typename Container1::iterator i1 = c1.begin();
 | 
|---|
| 137 |                 typename Container2::iterator i2 = c2.begin();
 | 
|---|
| 138 |                 for ( ; i1 != c1.end() && i2 != c2.end(); ++i1, ++i2 ) {
 | 
|---|
| 139 |                         if ( ! typesCompatible( *i1, *i2, indexer ) ) {
 | 
|---|
| 140 |                                 return false;
 | 
|---|
| 141 |                         } // if
 | 
|---|
| 142 |                 }
 | 
|---|
| 143 |                 return ( i1 == c1.end() ) && ( i2 == c2.end() );
 | 
|---|
| 144 |         }
 | 
|---|
| 145 | 
 | 
|---|
| 146 |         // in CommonType.cc
 | 
|---|
| 147 |         Type *commonType( Type *type1, Type *type2, bool widenFirst, bool widenSecond, const SymTab::Indexer &indexer, TypeEnvironment &env, const OpenVarSet &openVars );
 | 
|---|
| 148 | 
 | 
|---|
| 149 |         // in PolyCost.cc
 | 
|---|
| 150 |         int polyCost( Type *type, const TypeEnvironment &env, const SymTab::Indexer &indexer );
 | 
|---|
| 151 | 
 | 
|---|
| 152 |         // in Occurs.cc
 | 
|---|
| 153 |         bool occurs( Type *type, std::string varName, const TypeEnvironment &env );
 | 
|---|
| 154 | } // namespace ResolvExpr
 | 
|---|
| 155 | 
 | 
|---|
| 156 | #endif // TYPEOPS_H
 | 
|---|
| 157 | 
 | 
|---|
| 158 | // Local Variables: //
 | 
|---|
| 159 | // tab-width: 4 //
 | 
|---|
| 160 | // mode: c++ //
 | 
|---|
| 161 | // compile-command: "make install" //
 | 
|---|
| 162 | // End: //
 | 
|---|