| [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 | 
|---|
| [ca35c51] | 11 | // Last Modified By : Peter A. Buhr | 
|---|
|  | 12 | // Last Modified On : Wed Jun 29 21:45:53 2016 | 
|---|
|  | 13 | // Update Count     : 14 | 
|---|
| [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 { | 
|---|
| [ffad73a] | 25 | namespace { | 
|---|
| [0f889a77] | 26 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present | 
|---|
|  | 27 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { | 
|---|
|  | 28 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 29 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 30 | assert(paramType && "Aggregate parameters should be type expressions"); | 
|---|
|  | 31 | if ( isPolyType( paramType->get_type(), env ) ) return true; | 
|---|
|  | 32 | } | 
|---|
|  | 33 | return false; | 
|---|
|  | 34 | } | 
|---|
|  | 35 |  | 
|---|
|  | 36 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present | 
|---|
| [ffad73a] | 37 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 38 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 39 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 40 | assert(paramType && "Aggregate parameters should be type expressions"); | 
|---|
|  | 41 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; | 
|---|
|  | 42 | } | 
|---|
|  | 43 | return false; | 
|---|
|  | 44 | } | 
|---|
| [3bb195cb] | 45 |  | 
|---|
|  | 46 | /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present | 
|---|
|  | 47 | bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 48 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { | 
|---|
|  | 49 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); | 
|---|
|  | 50 | assert(paramType && "Aggregate parameters should be type expressions"); | 
|---|
|  | 51 | if ( isDynType( paramType->get_type(), tyVars, env ) ) return true; | 
|---|
|  | 52 | } | 
|---|
|  | 53 | return false; | 
|---|
|  | 54 | } | 
|---|
| [c2ad3c9] | 55 | } | 
|---|
| [83de11e] | 56 |  | 
|---|
| [c2ad3c9] | 57 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { | 
|---|
|  | 58 | if ( ! env ) return type; | 
|---|
|  | 59 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { | 
|---|
|  | 60 | Type *newType = env->lookup( typeInst->get_name() ); | 
|---|
|  | 61 | if ( newType ) return newType; | 
|---|
| [e24955a] | 62 | } | 
|---|
| [c2ad3c9] | 63 | return type; | 
|---|
| [ffad73a] | 64 | } | 
|---|
| [0f889a77] | 65 |  | 
|---|
|  | 66 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 67 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 68 |  | 
|---|
| [ca35c51] | 69 | if ( dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
| [0f889a77] | 70 | return type; | 
|---|
|  | 71 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 72 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; | 
|---|
|  | 73 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 74 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; | 
|---|
|  | 75 | } | 
|---|
|  | 76 | return 0; | 
|---|
|  | 77 | } | 
|---|
| [540de412] | 78 |  | 
|---|
| [ffad73a] | 79 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 80 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 81 |  | 
|---|
| [e56cfdb0] | 82 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
| [ffad73a] | 83 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { | 
|---|
|  | 84 | return type; | 
|---|
| [0f889a77] | 85 | } | 
|---|
| [ffad73a] | 86 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 87 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 88 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 89 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 90 | } | 
|---|
|  | 91 | return 0; | 
|---|
| [01aeade] | 92 | } | 
|---|
| [b1a6d6b] | 93 |  | 
|---|
| [3bb195cb] | 94 | Type *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
|  | 95 | type = replaceTypeInst( type, env ); | 
|---|
|  | 96 |  | 
|---|
|  | 97 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { | 
|---|
|  | 98 | auto var = tyVars.find( typeInst->get_name() ); | 
|---|
|  | 99 | if ( var != tyVars.end() && var->second == TypeDecl::Any ) { | 
|---|
|  | 100 | return type; | 
|---|
|  | 101 | } | 
|---|
|  | 102 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { | 
|---|
|  | 103 | if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 104 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { | 
|---|
|  | 105 | if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return type; | 
|---|
|  | 106 | } | 
|---|
|  | 107 | return 0; | 
|---|
|  | 108 | } | 
|---|
|  | 109 |  | 
|---|
|  | 110 | ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) { | 
|---|
|  | 111 | if ( function->get_returnVals().empty() ) return 0; | 
|---|
|  | 112 |  | 
|---|
|  | 113 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); | 
|---|
|  | 114 | } | 
|---|
|  | 115 |  | 
|---|
|  | 116 | ReferenceToType *isDynRet( FunctionType *function ) { | 
|---|
|  | 117 | if ( function->get_returnVals().empty() ) return 0; | 
|---|
|  | 118 |  | 
|---|
|  | 119 | TyVarMap forallTypes( (TypeDecl::Kind)-1 ); | 
|---|
|  | 120 | makeTyVarMap( function, forallTypes ); | 
|---|
|  | 121 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); | 
|---|
|  | 122 | } | 
|---|
|  | 123 |  | 
|---|
|  | 124 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { | 
|---|
|  | 125 | //              if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { | 
|---|
|  | 126 | //                      return true; | 
|---|
|  | 127 | //              } // if | 
|---|
|  | 128 | if ( isDynRet( adaptee, tyVars ) ) return true; | 
|---|
|  | 129 |  | 
|---|
|  | 130 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { | 
|---|
|  | 131 | //                      if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { | 
|---|
|  | 132 | if ( isDynType( (*innerArg)->get_type(), tyVars ) ) { | 
|---|
|  | 133 | return true; | 
|---|
|  | 134 | } // if | 
|---|
|  | 135 | } // for | 
|---|
|  | 136 | return false; | 
|---|
|  | 137 | } | 
|---|
|  | 138 |  | 
|---|
| [0f889a77] | 139 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 140 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 141 |  | 
|---|
| [0f889a77] | 142 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 143 | return isPolyType( ptr->get_base(), env ); | 
|---|
| [e24955a] | 144 | } | 
|---|
| [0f889a77] | 145 | return 0; | 
|---|
|  | 146 | } | 
|---|
| [540de412] | 147 |  | 
|---|
| [ffad73a] | 148 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { | 
|---|
| [e24955a] | 149 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 150 |  | 
|---|
| [ffad73a] | 151 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 152 | return isPolyType( ptr->get_base(), tyVars, env ); | 
|---|
| [e24955a] | 153 | } | 
|---|
| [ffad73a] | 154 | return 0; | 
|---|
| [bdf1954] | 155 | } | 
|---|
|  | 156 |  | 
|---|
| [8488c715] | 157 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 158 | int dummy; | 
|---|
|  | 159 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 160 | *levels = 0; | 
|---|
|  | 161 |  | 
|---|
|  | 162 | while ( true ) { | 
|---|
| [e24955a] | 163 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 164 |  | 
|---|
| [8488c715] | 165 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 166 | type = ptr->get_base(); | 
|---|
|  | 167 | ++(*levels); | 
|---|
|  | 168 | } else break; | 
|---|
| [05d47278] | 169 | } | 
|---|
|  | 170 |  | 
|---|
|  | 171 | return isPolyType( type, env ); | 
|---|
|  | 172 | } | 
|---|
| [540de412] | 173 |  | 
|---|
| [8488c715] | 174 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { | 
|---|
|  | 175 | int dummy; | 
|---|
|  | 176 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 177 | *levels = 0; | 
|---|
|  | 178 |  | 
|---|
|  | 179 | while ( true ) { | 
|---|
| [e24955a] | 180 | type = replaceTypeInst( type, env ); | 
|---|
| [83de11e] | 181 |  | 
|---|
| [8488c715] | 182 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { | 
|---|
|  | 183 | type = ptr->get_base(); | 
|---|
|  | 184 | ++(*levels); | 
|---|
|  | 185 | } else break; | 
|---|
| [05d47278] | 186 | } | 
|---|
|  | 187 |  | 
|---|
|  | 188 | return isPolyType( type, tyVars, env ); | 
|---|
|  | 189 | } | 
|---|
|  | 190 |  | 
|---|
| [7754cde] | 191 | FunctionType * getFunctionType( Type *ty ) { | 
|---|
|  | 192 | PointerType *ptrType; | 
|---|
|  | 193 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { | 
|---|
|  | 194 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 195 | } else { | 
|---|
|  | 196 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise | 
|---|
|  | 197 | } | 
|---|
|  | 198 | } | 
|---|
|  | 199 |  | 
|---|
| [8488c715] | 200 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { | 
|---|
|  | 201 | int dummy; | 
|---|
|  | 202 | if ( ! levels ) { levels = &dummy; } | 
|---|
|  | 203 | *levels = 0; | 
|---|
|  | 204 |  | 
|---|
|  | 205 | while ( true ) { | 
|---|
|  | 206 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { | 
|---|
|  | 207 | return varExpr; | 
|---|
| [d9f1b2d] | 208 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { | 
|---|
|  | 209 | expr = memberExpr->get_aggregate(); | 
|---|
| [8488c715] | 210 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { | 
|---|
|  | 211 | expr = addressExpr->get_arg(); | 
|---|
|  | 212 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { | 
|---|
|  | 213 | // look for compiler-inserted dereference operator | 
|---|
|  | 214 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); | 
|---|
|  | 215 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; | 
|---|
|  | 216 | expr = *untypedExpr->begin_args(); | 
|---|
| [540de412] | 217 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { | 
|---|
|  | 218 | // copy constructors insert comma exprs, look at second argument which contains the variable | 
|---|
|  | 219 | expr = commaExpr->get_arg2(); | 
|---|
|  | 220 | continue; | 
|---|
| [8488c715] | 221 | } else break; | 
|---|
|  | 222 |  | 
|---|
|  | 223 | ++(*levels); | 
|---|
|  | 224 | } | 
|---|
|  | 225 |  | 
|---|
|  | 226 | return 0; | 
|---|
| [05d47278] | 227 | } | 
|---|
|  | 228 |  | 
|---|
| [aadc9a4] | 229 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { | 
|---|
|  | 230 | for ( std::list< TypeDecl* >::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { | 
|---|
|  | 231 | assert( *tyVar ); | 
|---|
|  | 232 | tyVarMap[ (*tyVar)->get_name() ] = (*tyVar)->get_kind(); | 
|---|
|  | 233 | } | 
|---|
|  | 234 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { | 
|---|
|  | 235 | makeTyVarMap( pointer->get_base(), tyVarMap ); | 
|---|
|  | 236 | } | 
|---|
|  | 237 | } | 
|---|
| [540de412] | 238 |  | 
|---|
| [01aeade] | 239 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { | 
|---|
|  | 240 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { | 
|---|
|  | 241 | os << i->first << " (" << i->second << ") "; | 
|---|
|  | 242 | } // for | 
|---|
|  | 243 | os << std::endl; | 
|---|
|  | 244 | } | 
|---|
| [ffad73a] | 245 |  | 
|---|
| [51b73452] | 246 | } // namespace GenPoly | 
|---|
| [01aeade] | 247 |  | 
|---|
| [51587aa] | 248 | // Local Variables: // | 
|---|
|  | 249 | // tab-width: 4 // | 
|---|
|  | 250 | // mode: c++ // | 
|---|
|  | 251 | // compile-command: "make install" // | 
|---|
|  | 252 | // End: // | 
|---|