[51587aa] | 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 | //
|
---|
[01aeade] | 7 | // GenPoly.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[cf16f94] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[4389966] | 12 | // Last Modified On : Tue Dec 15 16:11:18 2015
|
---|
| 13 | // Update Count : 13
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include "GenPoly.h"
|
---|
[ffad73a] | 17 |
|
---|
| 18 | #include "SynTree/Expression.h"
|
---|
[51b73452] | 19 | #include "SynTree/Type.h"
|
---|
| 20 |
|
---|
[b1a6d6b] | 21 | #include <iostream>
|
---|
| 22 | using namespace std;
|
---|
[51b73452] | 23 |
|
---|
| 24 | namespace GenPoly {
|
---|
[e56cfdb0] | 25 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
[ffad73a] | 26 | if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
[e56cfdb0] | 27 | return true;
|
---|
| 28 | } // if
|
---|
| 29 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
|
---|
[ffad73a] | 30 | if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
|
---|
[e56cfdb0] | 31 | return true;
|
---|
| 32 | } // if
|
---|
| 33 | } // for
|
---|
| 34 | return false;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
[aadc9a4] | 37 | ReferenceToType *isPolyRet( FunctionType *function ) {
|
---|
[e56cfdb0] | 38 | if ( ! function->get_returnVals().empty() ) {
|
---|
[bfae637] | 39 | TyVarMap forallTypes( (TypeDecl::Kind)-1 );
|
---|
[aadc9a4] | 40 | makeTyVarMap( function, forallTypes );
|
---|
| 41 | return (ReferenceToType*)isPolyType( function->get_returnVals().front()->get_type(), forallTypes );
|
---|
[e56cfdb0] | 42 | } // if
|
---|
[aadc9a4] | 43 | return 0;
|
---|
[01aeade] | 44 | }
|
---|
[b1a6d6b] | 45 |
|
---|
[ffad73a] | 46 | namespace {
|
---|
[0f889a77] | 47 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
|
---|
| 48 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
|
---|
| 49 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 50 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 51 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
| 52 | if ( isPolyType( paramType->get_type(), env ) ) return true;
|
---|
| 53 | }
|
---|
| 54 | return false;
|
---|
| 55 | }
|
---|
| 56 |
|
---|
| 57 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
|
---|
[ffad73a] | 58 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 59 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 60 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 61 | assert(paramType && "Aggregate parameters should be type expressions");
|
---|
| 62 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
| 63 | }
|
---|
| 64 | return false;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
[0f889a77] | 67 |
|
---|
| 68 | Type *isPolyType( Type *type, const TypeSubstitution *env ) {
|
---|
| 69 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 70 | if ( env ) {
|
---|
| 71 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 72 | return isPolyType( newType, env );
|
---|
| 73 | } // if
|
---|
| 74 | } // if
|
---|
| 75 | return type;
|
---|
| 76 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 77 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
|
---|
| 78 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 79 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
|
---|
| 80 | }
|
---|
| 81 | return 0;
|
---|
| 82 | }
|
---|
[ffad73a] | 83 |
|
---|
| 84 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
[e56cfdb0] | 85 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[ffad73a] | 86 | if ( env ) {
|
---|
| 87 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 88 | return isPolyType( newType, tyVars, env );
|
---|
[0f889a77] | 89 | } // if
|
---|
[01aeade] | 90 | } // if
|
---|
[ffad73a] | 91 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
---|
| 92 | return type;
|
---|
[0f889a77] | 93 | }
|
---|
[ffad73a] | 94 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 95 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 96 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 97 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 98 | }
|
---|
| 99 | return 0;
|
---|
[01aeade] | 100 | }
|
---|
[b1a6d6b] | 101 |
|
---|
[0f889a77] | 102 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
|
---|
| 103 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 104 | return isPolyType( ptr->get_base(), env );
|
---|
| 105 | } else if ( env ) {
|
---|
| 106 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 107 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 108 | return isPolyPtr( newType, env );
|
---|
| 109 | } // if
|
---|
| 110 | } // if
|
---|
| 111 | } // if
|
---|
| 112 | return 0;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[ffad73a] | 115 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 116 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 117 | return isPolyType( ptr->get_base(), tyVars, env );
|
---|
| 118 | } else if ( env ) {
|
---|
| 119 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 120 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 121 | return isPolyPtr( newType, tyVars, env );
|
---|
| 122 | } // if
|
---|
| 123 | } // if
|
---|
| 124 | } // if
|
---|
| 125 | return 0;
|
---|
[bdf1954] | 126 | }
|
---|
| 127 |
|
---|
[8488c715] | 128 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
|
---|
| 129 | int dummy;
|
---|
| 130 | if ( ! levels ) { levels = &dummy; }
|
---|
| 131 | *levels = 0;
|
---|
| 132 |
|
---|
| 133 | while ( true ) {
|
---|
| 134 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 135 | type = ptr->get_base();
|
---|
| 136 | ++(*levels);
|
---|
| 137 | } else if ( env ) {
|
---|
| 138 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 139 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 140 | type = newType;
|
---|
| 141 | } else break;
|
---|
| 142 | } else break;
|
---|
| 143 | } else break;
|
---|
[05d47278] | 144 | }
|
---|
| 145 |
|
---|
| 146 | return isPolyType( type, env );
|
---|
| 147 | }
|
---|
| 148 |
|
---|
[8488c715] | 149 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
|
---|
| 150 | int dummy;
|
---|
| 151 | if ( ! levels ) { levels = &dummy; }
|
---|
| 152 | *levels = 0;
|
---|
| 153 |
|
---|
| 154 | while ( true ) {
|
---|
| 155 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 156 | type = ptr->get_base();
|
---|
| 157 | ++(*levels);
|
---|
| 158 | } else if ( env ) {
|
---|
| 159 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType *>( type ) ) {
|
---|
| 160 | if ( Type *newType = env->lookup( typeInst->get_name() ) ) {
|
---|
| 161 | type = newType;
|
---|
| 162 | } else break;
|
---|
| 163 | } else break;
|
---|
| 164 | } else break;
|
---|
[05d47278] | 165 | }
|
---|
| 166 |
|
---|
| 167 | return isPolyType( type, tyVars, env );
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[7754cde] | 170 | FunctionType * getFunctionType( Type *ty ) {
|
---|
| 171 | PointerType *ptrType;
|
---|
| 172 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
|
---|
| 173 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
|
---|
| 174 | } else {
|
---|
| 175 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
|
---|
| 176 | }
|
---|
| 177 | }
|
---|
| 178 |
|
---|
[8488c715] | 179 | VariableExpr * getBaseVar( Expression *expr, int *levels ) {
|
---|
| 180 | int dummy;
|
---|
| 181 | if ( ! levels ) { levels = &dummy; }
|
---|
| 182 | *levels = 0;
|
---|
| 183 |
|
---|
| 184 | while ( true ) {
|
---|
| 185 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
|
---|
| 186 | return varExpr;
|
---|
| 187 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
|
---|
| 188 | expr = addressExpr->get_arg();
|
---|
| 189 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
|
---|
| 190 | // look for compiler-inserted dereference operator
|
---|
| 191 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
|
---|
| 192 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
|
---|
| 193 | expr = *untypedExpr->begin_args();
|
---|
| 194 | } else break;
|
---|
| 195 |
|
---|
| 196 | ++(*levels);
|
---|
| 197 | }
|
---|
| 198 |
|
---|
| 199 | return 0;
|
---|
[05d47278] | 200 | }
|
---|
| 201 |
|
---|
[aadc9a4] | 202 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
|
---|
| 203 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
|
---|
| 204 | assert( *tyVar );
|
---|
| 205 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind();
|
---|
| 206 | }
|
---|
| 207 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
|
---|
| 208 | makeTyVarMap( pointer->get_base(), tyVarMap );
|
---|
| 209 | }
|
---|
| 210 | }
|
---|
| 211 |
|
---|
[01aeade] | 212 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
---|
| 213 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
---|
| 214 | os << i->first << " (" << i->second << ") ";
|
---|
| 215 | } // for
|
---|
| 216 | os << std::endl;
|
---|
| 217 | }
|
---|
[ffad73a] | 218 |
|
---|
[51b73452] | 219 | } // namespace GenPoly
|
---|
[01aeade] | 220 |
|
---|
[51587aa] | 221 | // Local Variables: //
|
---|
| 222 | // tab-width: 4 //
|
---|
| 223 | // mode: c++ //
|
---|
| 224 | // compile-command: "make install" //
|
---|
| 225 | // End: //
|
---|