| [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 | // | 
|---|
| [540de412] | 7 | // GenPoly.cc -- | 
|---|
| [51587aa] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Mon May 18 07:44:20 2015 | 
|---|
| [540de412] | 11 | // Last Modified By : Rob Schluntz | 
|---|
| [d9f1b2d] | 12 | // Last Modified On : Wed May 25 13:39:21 2016 | 
|---|
| [4389966] | 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 | } | 
|---|
| [83de11e] | 66 |  | 
|---|
| [e24955a] | 67 | /// Replaces a TypeInstType by its referrent in the environment, if applicable | 
|---|
|  | 68 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { | 
|---|
|  | 69 | if ( ! env ) return type; | 
|---|
|  | 70 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { | 
|---|
|  | 71 | Type *newType = env->lookup( typeInst->get_name() ); | 
|---|
|  | 72 | if ( newType ) return newType; | 
|---|
|  | 73 | } | 
|---|
|  | 74 | return type; | 
|---|
|  | 75 | } | 
|---|
| [ffad73a] | 76 | } | 
|---|
| [0f889a77] | 77 |  | 
|---|
|  | 78 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 79 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 80 |  | 
|---|
| [0f889a77] | 81 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 82 | return type; | 
|---|
|  | 83 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 84 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; | 
|---|
|  | 85 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 86 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; | 
|---|
|  | 87 | } | 
|---|
|  | 88 | return 0; | 
|---|
|  | 89 | } | 
|---|
| [540de412] | 90 |  | 
|---|
| [ffad73a] | 91 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 92 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 93 |  | 
|---|
| [e56cfdb0] | 94 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
| [ffad73a] | 95 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { | 
|---|
|  | 96 | return type; | 
|---|
| [0f889a77] | 97 | } | 
|---|
| [ffad73a] | 98 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 99 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 100 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 101 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 102 | } | 
|---|
|  | 103 | return 0; | 
|---|
| [01aeade] | 104 | } | 
|---|
| [b1a6d6b] | 105 |  | 
|---|
| [0f889a77] | 106 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 107 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 108 |  | 
|---|
| [0f889a77] | 109 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 110 | return isPolyType( ptr->get_base(), env ); | 
|---|
| [e24955a] | 111 | } | 
|---|
| [0f889a77] | 112 | return 0; | 
|---|
|  | 113 | } | 
|---|
| [540de412] | 114 |  | 
|---|
| [ffad73a] | 115 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 116 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 117 |  | 
|---|
| [ffad73a] | 118 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 119 | return isPolyType( ptr->get_base(), tyVars, env ); | 
|---|
| [e24955a] | 120 | } | 
|---|
| [ffad73a] | 121 | return 0; | 
|---|
| [bdf1954] | 122 | } | 
|---|
|  | 123 |  | 
|---|
| [8488c715] | 124 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 125 | int dummy; | 
|---|
|  | 126 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 127 | *levels = 0; | 
|---|
|  | 128 |  | 
|---|
|  | 129 | while ( true ) { | 
|---|
| [e24955a] | 130 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 131 |  | 
|---|
| [8488c715] | 132 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 133 | type = ptr->get_base(); | 
|---|
|  | 134 | ++(*levels); | 
|---|
|  | 135 | } else break; | 
|---|
| [05d47278] | 136 | } | 
|---|
|  | 137 |  | 
|---|
|  | 138 | return isPolyType( type, env ); | 
|---|
|  | 139 | } | 
|---|
| [540de412] | 140 |  | 
|---|
| [8488c715] | 141 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 142 | int dummy; | 
|---|
|  | 143 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 144 | *levels = 0; | 
|---|
|  | 145 |  | 
|---|
|  | 146 | while ( true ) { | 
|---|
| [e24955a] | 147 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 148 |  | 
|---|
| [8488c715] | 149 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 150 | type = ptr->get_base(); | 
|---|
|  | 151 | ++(*levels); | 
|---|
|  | 152 | } else break; | 
|---|
| [05d47278] | 153 | } | 
|---|
|  | 154 |  | 
|---|
|  | 155 | return isPolyType( type, tyVars, env ); | 
|---|
|  | 156 | } | 
|---|
|  | 157 |  | 
|---|
| [7754cde] | 158 | FunctionType * getFunctionType( Type *ty ) { | 
|---|
|  | 159 | PointerType *ptrType; | 
|---|
|  | 160 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { | 
|---|
|  | 161 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 162 | } else { | 
|---|
|  | 163 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 164 | } | 
|---|
|  | 165 | } | 
|---|
|  | 166 |  | 
|---|
| [8488c715] | 167 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { | 
|---|
|  | 168 | int dummy; | 
|---|
|  | 169 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 170 | *levels = 0; | 
|---|
|  | 171 |  | 
|---|
|  | 172 | while ( true ) { | 
|---|
|  | 173 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { | 
|---|
|  | 174 | return varExpr; | 
|---|
| [d9f1b2d] | 175 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { | 
|---|
|  | 176 | expr = memberExpr->get_aggregate(); | 
|---|
| [8488c715] | 177 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { | 
|---|
|  | 178 | expr = addressExpr->get_arg(); | 
|---|
|  | 179 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { | 
|---|
|  | 180 | // look for compiler-inserted dereference operator | 
|---|
|  | 181 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); | 
|---|
|  | 182 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; | 
|---|
|  | 183 | expr = *untypedExpr->begin_args(); | 
|---|
| [540de412] | 184 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { | 
|---|
|  | 185 | // copy constructors insert comma exprs, look at second argument which contains the variable | 
|---|
|  | 186 | expr = commaExpr->get_arg2(); | 
|---|
|  | 187 | continue; | 
|---|
| [8488c715] | 188 | } else break; | 
|---|
|  | 189 |  | 
|---|
|  | 190 | ++(*levels); | 
|---|
|  | 191 | } | 
|---|
|  | 192 |  | 
|---|
|  | 193 | return 0; | 
|---|
| [05d47278] | 194 | } | 
|---|
|  | 195 |  | 
|---|
| [aadc9a4] | 196 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { | 
|---|
|  | 197 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { | 
|---|
|  | 198 | assert( *tyVar ); | 
|---|
|  | 199 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); | 
|---|
|  | 200 | } | 
|---|
|  | 201 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { | 
|---|
|  | 202 | makeTyVarMap( pointer->get_base(), tyVarMap ); | 
|---|
|  | 203 | } | 
|---|
|  | 204 | } | 
|---|
| [540de412] | 205 |  | 
|---|
| [01aeade] | 206 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { | 
|---|
|  | 207 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { | 
|---|
|  | 208 | os << i->first << " (" << i->second << ") "; | 
|---|
|  | 209 | } // for | 
|---|
|  | 210 | os << std::endl; | 
|---|
|  | 211 | } | 
|---|
| [ffad73a] | 212 |  | 
|---|
| [51b73452] | 213 | } // namespace GenPoly | 
|---|
| [01aeade] | 214 |  | 
|---|
| [51587aa] | 215 | // Local Variables: // | 
|---|
|  | 216 | // tab-width: 4 // | 
|---|
|  | 217 | // mode: c++ // | 
|---|
|  | 218 | // compile-command: "make install" // | 
|---|
|  | 219 | // End: // | 
|---|