[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 | // |
---|
[51b7345] | 15 | |
---|
| 16 | #include "GenPoly.h" |
---|
[ffad73a] | 17 | |
---|
[08fc48f] | 18 | #include <cassert> // for assertf, assert |
---|
| 19 | #include <iostream> // for operator<<, ostream, basic_os... |
---|
| 20 | #include <iterator> // for back_insert_iterator, back_in... |
---|
| 21 | #include <list> // for list, _List_iterator, list<>:... |
---|
| 22 | #include <typeindex> // for type_index |
---|
| 23 | #include <utility> // for pair |
---|
| 24 | #include <vector> // for vector |
---|
| 25 | |
---|
| 26 | #include "GenPoly/ErasableScopedMap.h" // for ErasableScopedMap<>::const_it... |
---|
| 27 | #include "ResolvExpr/typeops.h" // for flatten |
---|
| 28 | #include "SynTree/Constant.h" // for Constant |
---|
| 29 | #include "SynTree/Expression.h" // for Expression, TypeExpr, Constan... |
---|
| 30 | #include "SynTree/Type.h" // for Type, StructInstType, UnionIn... |
---|
| 31 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
[51b7345] | 32 | |
---|
[b1a6d6b] | 33 | using namespace std; |
---|
[51b7345] | 34 | |
---|
| 35 | namespace GenPoly { |
---|
[ffad73a] | 36 | namespace { |
---|
[0f889a77] | 37 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present |
---|
| 38 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { |
---|
| 39 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 40 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
[7350ff97] | 41 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
[0f889a77] | 42 | if ( isPolyType( paramType->get_type(), env ) ) return true; |
---|
| 43 | } |
---|
| 44 | return false; |
---|
| 45 | } |
---|
| 46 | |
---|
| 47 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present |
---|
[ffad73a] | 48 | bool hasPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
| 49 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 50 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
[5a3ac84] | 51 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
[ffad73a] | 52 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true; |
---|
| 53 | } |
---|
| 54 | return false; |
---|
| 55 | } |
---|
[3bb195cb] | 56 | |
---|
| 57 | /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present |
---|
| 58 | bool hasDynParams( 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 ); |
---|
[5a3ac84] | 61 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
[3bb195cb] | 62 | if ( isDynType( paramType->get_type(), tyVars, env ) ) return true; |
---|
| 63 | } |
---|
| 64 | return false; |
---|
| 65 | } |
---|
[5a3ac84] | 66 | |
---|
| 67 | /// Checks a parameter list for inclusion of polymorphic parameters; will substitute according to env if present |
---|
| 68 | bool includesPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) { |
---|
| 69 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 70 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
| 71 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
| 72 | if ( includesPolyType( paramType->get_type(), env ) ) return true; |
---|
| 73 | } |
---|
| 74 | return false; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | /// Checks a parameter list for inclusion of polymorphic parameters from tyVars; will substitute according to env if present |
---|
| 78 | bool includesPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
| 79 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) { |
---|
| 80 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param ); |
---|
| 81 | assertf(paramType, "Aggregate parameters should be type expressions"); |
---|
| 82 | if ( includesPolyType( paramType->get_type(), tyVars, env ) ) return true; |
---|
| 83 | } |
---|
| 84 | return false; |
---|
| 85 | } |
---|
[c2ad3c9] | 86 | } |
---|
[83de11e] | 87 | |
---|
[c2ad3c9] | 88 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) { |
---|
| 89 | if ( ! env ) return type; |
---|
| 90 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) { |
---|
| 91 | Type *newType = env->lookup( typeInst->get_name() ); |
---|
| 92 | if ( newType ) return newType; |
---|
[e24955a] | 93 | } |
---|
[c2ad3c9] | 94 | return type; |
---|
[ffad73a] | 95 | } |
---|
[0f889a77] | 96 | |
---|
| 97 | Type *isPolyType( Type *type, const TypeSubstitution *env ) { |
---|
[e24955a] | 98 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 99 | |
---|
[ca35c51] | 100 | if ( dynamic_cast< TypeInstType * >( type ) ) { |
---|
[0f889a77] | 101 | return type; |
---|
[5f61546] | 102 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 103 | return isPolyType( arrayType->base, env ); |
---|
[0f889a77] | 104 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 105 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type; |
---|
| 106 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 107 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type; |
---|
| 108 | } |
---|
| 109 | return 0; |
---|
| 110 | } |
---|
[540de412] | 111 | |
---|
[ffad73a] | 112 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
[e24955a] | 113 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 114 | |
---|
[e56cfdb0] | 115 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
[ffad73a] | 116 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) { |
---|
| 117 | return type; |
---|
[0f889a77] | 118 | } |
---|
[5f61546] | 119 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { |
---|
| 120 | return isPolyType( arrayType->base, tyVars, env ); |
---|
[ffad73a] | 121 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 122 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type; |
---|
| 123 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 124 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type; |
---|
| 125 | } |
---|
| 126 | return 0; |
---|
[01aeade] | 127 | } |
---|
[b1a6d6b] | 128 | |
---|
[33a7b6d] | 129 | ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
[3bb195cb] | 130 | type = replaceTypeInst( type, env ); |
---|
| 131 | |
---|
| 132 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) { |
---|
| 133 | auto var = tyVars.find( typeInst->get_name() ); |
---|
[2c57025] | 134 | if ( var != tyVars.end() && var->second.isComplete ) { |
---|
[33a7b6d] | 135 | return typeInst; |
---|
[3bb195cb] | 136 | } |
---|
| 137 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
[33a7b6d] | 138 | if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return structType; |
---|
[3bb195cb] | 139 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
[33a7b6d] | 140 | if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return unionType; |
---|
[3bb195cb] | 141 | } |
---|
| 142 | return 0; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) { |
---|
| 146 | if ( function->get_returnVals().empty() ) return 0; |
---|
[8c49c0e] | 147 | |
---|
[3bb195cb] | 148 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | ReferenceToType *isDynRet( FunctionType *function ) { |
---|
| 152 | if ( function->get_returnVals().empty() ) return 0; |
---|
| 153 | |
---|
[2c57025] | 154 | TyVarMap forallTypes( TypeDecl::Data{} ); |
---|
[3bb195cb] | 155 | makeTyVarMap( function, forallTypes ); |
---|
| 156 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes ); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) { |
---|
| 160 | // if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) { |
---|
| 161 | // return true; |
---|
| 162 | // } // if |
---|
| 163 | if ( isDynRet( adaptee, tyVars ) ) return true; |
---|
[8c49c0e] | 164 | |
---|
[3bb195cb] | 165 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) { |
---|
| 166 | // if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) { |
---|
| 167 | if ( isDynType( (*innerArg)->get_type(), tyVars ) ) { |
---|
| 168 | return true; |
---|
| 169 | } // if |
---|
| 170 | } // for |
---|
| 171 | return false; |
---|
| 172 | } |
---|
| 173 | |
---|
[0f889a77] | 174 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) { |
---|
[e24955a] | 175 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 176 | |
---|
[0f889a77] | 177 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 178 | return isPolyType( ptr->get_base(), env ); |
---|
[e24955a] | 179 | } |
---|
[0f889a77] | 180 | return 0; |
---|
| 181 | } |
---|
[540de412] | 182 | |
---|
[ffad73a] | 183 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
[e24955a] | 184 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 185 | |
---|
[ffad73a] | 186 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 187 | return isPolyType( ptr->get_base(), tyVars, env ); |
---|
[e24955a] | 188 | } |
---|
[ffad73a] | 189 | return 0; |
---|
[bdf1954] | 190 | } |
---|
| 191 | |
---|
[8488c715] | 192 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) { |
---|
| 193 | int dummy; |
---|
| 194 | if ( ! levels ) { levels = &dummy; } |
---|
| 195 | *levels = 0; |
---|
| 196 | |
---|
| 197 | while ( true ) { |
---|
[e24955a] | 198 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 199 | |
---|
[8488c715] | 200 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 201 | type = ptr->get_base(); |
---|
| 202 | ++(*levels); |
---|
| 203 | } else break; |
---|
[05d47278] | 204 | } |
---|
| 205 | |
---|
| 206 | return isPolyType( type, env ); |
---|
| 207 | } |
---|
[540de412] | 208 | |
---|
[8488c715] | 209 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) { |
---|
| 210 | int dummy; |
---|
| 211 | if ( ! levels ) { levels = &dummy; } |
---|
| 212 | *levels = 0; |
---|
| 213 | |
---|
| 214 | while ( true ) { |
---|
[e24955a] | 215 | type = replaceTypeInst( type, env ); |
---|
[83de11e] | 216 | |
---|
[8488c715] | 217 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) { |
---|
| 218 | type = ptr->get_base(); |
---|
| 219 | ++(*levels); |
---|
| 220 | } else break; |
---|
[05d47278] | 221 | } |
---|
| 222 | |
---|
| 223 | return isPolyType( type, tyVars, env ); |
---|
| 224 | } |
---|
| 225 | |
---|
[5a3ac84] | 226 | bool includesPolyType( Type *type, const TypeSubstitution *env ) { |
---|
| 227 | type = replaceTypeInst( type, env ); |
---|
| 228 | |
---|
| 229 | if ( dynamic_cast< TypeInstType * >( type ) ) { |
---|
| 230 | return true; |
---|
| 231 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) { |
---|
| 232 | if ( includesPolyType( pointerType->get_base(), env ) ) return true; |
---|
| 233 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 234 | if ( includesPolyParams( structType->get_parameters(), env ) ) return true; |
---|
| 235 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 236 | if ( includesPolyParams( unionType->get_parameters(), env ) ) return true; |
---|
| 237 | } |
---|
| 238 | return false; |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) { |
---|
| 242 | type = replaceTypeInst( type, env ); |
---|
| 243 | |
---|
| 244 | if ( TypeInstType *typeInstType = dynamic_cast< TypeInstType * >( type ) ) { |
---|
| 245 | if ( tyVars.find( typeInstType->get_name() ) != tyVars.end() ) { |
---|
| 246 | return true; |
---|
| 247 | } |
---|
| 248 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) { |
---|
| 249 | if ( includesPolyType( pointerType->get_base(), tyVars, env ) ) return true; |
---|
| 250 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) { |
---|
| 251 | if ( includesPolyParams( structType->get_parameters(), tyVars, env ) ) return true; |
---|
| 252 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) { |
---|
| 253 | if ( includesPolyParams( unionType->get_parameters(), tyVars, env ) ) return true; |
---|
| 254 | } |
---|
| 255 | return false; |
---|
| 256 | } |
---|
| 257 | |
---|
[7754cde] | 258 | FunctionType * getFunctionType( Type *ty ) { |
---|
| 259 | PointerType *ptrType; |
---|
| 260 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) { |
---|
| 261 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise |
---|
| 262 | } else { |
---|
| 263 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise |
---|
| 264 | } |
---|
| 265 | } |
---|
| 266 | |
---|
[8488c715] | 267 | VariableExpr * getBaseVar( Expression *expr, int *levels ) { |
---|
| 268 | int dummy; |
---|
| 269 | if ( ! levels ) { levels = &dummy; } |
---|
| 270 | *levels = 0; |
---|
| 271 | |
---|
| 272 | while ( true ) { |
---|
| 273 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) { |
---|
| 274 | return varExpr; |
---|
[d9f1b2d] | 275 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) { |
---|
| 276 | expr = memberExpr->get_aggregate(); |
---|
[8488c715] | 277 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) { |
---|
| 278 | expr = addressExpr->get_arg(); |
---|
| 279 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) { |
---|
| 280 | // look for compiler-inserted dereference operator |
---|
| 281 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() ); |
---|
| 282 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0; |
---|
| 283 | expr = *untypedExpr->begin_args(); |
---|
[540de412] | 284 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) { |
---|
| 285 | // copy constructors insert comma exprs, look at second argument which contains the variable |
---|
| 286 | expr = commaExpr->get_arg2(); |
---|
| 287 | continue; |
---|
[1aa4b71] | 288 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( expr ) ) { |
---|
| 289 | int lvl1; |
---|
| 290 | int lvl2; |
---|
| 291 | VariableExpr * var1 = getBaseVar( condExpr->get_arg2(), &lvl1 ); |
---|
| 292 | VariableExpr * var2 = getBaseVar( condExpr->get_arg3(), &lvl2 ); |
---|
| 293 | if ( lvl1 == lvl2 && var1 && var2 && var1->get_var() == var2->get_var() ) { |
---|
| 294 | *levels = lvl1; |
---|
| 295 | return var1; |
---|
| 296 | } |
---|
| 297 | break; |
---|
[8488c715] | 298 | } else break; |
---|
| 299 | |
---|
| 300 | ++(*levels); |
---|
| 301 | } |
---|
| 302 | |
---|
| 303 | return 0; |
---|
[05d47278] | 304 | } |
---|
| 305 | |
---|
[5a3ac84] | 306 | namespace { |
---|
| 307 | /// Checks if is a pointer to D |
---|
| 308 | template<typename D, typename B> |
---|
| 309 | bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; } |
---|
| 310 | |
---|
| 311 | /// Converts to a pointer to D without checking for safety |
---|
| 312 | template<typename D, typename B> |
---|
| 313 | inline D* as( B* p ) { return reinterpret_cast<D*>(p); } |
---|
| 314 | |
---|
| 315 | /// Flattens a declaration list |
---|
| 316 | template<typename Output> |
---|
| 317 | void flattenList( list< DeclarationWithType* > src, Output out ) { |
---|
| 318 | for ( DeclarationWithType* decl : src ) { |
---|
| 319 | ResolvExpr::flatten( decl->get_type(), out ); |
---|
| 320 | } |
---|
| 321 | } |
---|
| 322 | |
---|
| 323 | /// Flattens a list of types |
---|
| 324 | template<typename Output> |
---|
| 325 | void flattenList( list< Type* > src, Output out ) { |
---|
| 326 | for ( Type* ty : src ) { |
---|
| 327 | ResolvExpr::flatten( ty, out ); |
---|
| 328 | } |
---|
| 329 | } |
---|
| 330 | |
---|
| 331 | /// Checks if two lists of parameters are equal up to polymorphic substitution. |
---|
| 332 | bool paramListsPolyCompatible( const list< Expression* >& aparams, const list< Expression* >& bparams ) { |
---|
| 333 | if ( aparams.size() != bparams.size() ) return false; |
---|
| 334 | |
---|
| 335 | for ( list< Expression* >::const_iterator at = aparams.begin(), bt = bparams.begin(); |
---|
| 336 | at != aparams.end(); ++at, ++bt ) { |
---|
| 337 | TypeExpr *aparam = dynamic_cast< TypeExpr* >(*at); |
---|
| 338 | assertf(aparam, "Aggregate parameters should be type expressions"); |
---|
| 339 | TypeExpr *bparam = dynamic_cast< TypeExpr* >(*bt); |
---|
| 340 | assertf(bparam, "Aggregate parameters should be type expressions"); |
---|
| 341 | |
---|
[cccc534] | 342 | // xxx - might need to let VoidType be a wildcard here too; could have some voids |
---|
[5a3ac84] | 343 | // stuffed in for dtype-statics. |
---|
| 344 | // if ( is<VoidType>( aparam->get_type() ) || is<VoidType>( bparam->get_type() ) ) continue; |
---|
| 345 | if ( ! typesPolyCompatible( aparam->get_type(), bparam->get_type() ) ) return false; |
---|
| 346 | } |
---|
[cccc534] | 347 | |
---|
[5a3ac84] | 348 | return true; |
---|
| 349 | } |
---|
| 350 | } |
---|
| 351 | |
---|
| 352 | bool typesPolyCompatible( Type *a, Type *b ) { |
---|
| 353 | type_index aid{ typeid(*a) }; |
---|
| 354 | // polymorphic types always match |
---|
| 355 | if ( aid == type_index{typeid(TypeInstType)} ) return true; |
---|
[cccc534] | 356 | |
---|
[5a3ac84] | 357 | type_index bid{ typeid(*b) }; |
---|
| 358 | // polymorphic types always match |
---|
| 359 | if ( bid == type_index{typeid(TypeInstType)} ) return true; |
---|
[cccc534] | 360 | |
---|
[5a3ac84] | 361 | // can't match otherwise if different types |
---|
| 362 | if ( aid != bid ) return false; |
---|
| 363 | |
---|
| 364 | // recurse through type structure (conditions borrowed from Unify.cc) |
---|
| 365 | if ( aid == type_index{typeid(BasicType)} ) { |
---|
| 366 | return as<BasicType>(a)->get_kind() == as<BasicType>(b)->get_kind(); |
---|
| 367 | } else if ( aid == type_index{typeid(PointerType)} ) { |
---|
| 368 | PointerType *ap = as<PointerType>(a), *bp = as<PointerType>(b); |
---|
| 369 | |
---|
| 370 | // void pointers should match any other pointer type |
---|
| 371 | return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() ) |
---|
| 372 | || typesPolyCompatible( ap->get_base(), bp->get_base() ); |
---|
| 373 | } else if ( aid == type_index{typeid(ArrayType)} ) { |
---|
| 374 | ArrayType *aa = as<ArrayType>(a), *ba = as<ArrayType>(b); |
---|
| 375 | |
---|
| 376 | if ( aa->get_isVarLen() ) { |
---|
| 377 | if ( ! ba->get_isVarLen() ) return false; |
---|
| 378 | } else { |
---|
| 379 | if ( ba->get_isVarLen() ) return false; |
---|
| 380 | |
---|
| 381 | ConstantExpr *ad = dynamic_cast<ConstantExpr*>( aa->get_dimension() ); |
---|
| 382 | ConstantExpr *bd = dynamic_cast<ConstantExpr*>( ba->get_dimension() ); |
---|
[cccc534] | 383 | if ( ad && bd |
---|
[5a3ac84] | 384 | && ad->get_constant()->get_value() != bd->get_constant()->get_value() ) |
---|
| 385 | return false; |
---|
| 386 | } |
---|
| 387 | |
---|
| 388 | return typesPolyCompatible( aa->get_base(), ba->get_base() ); |
---|
| 389 | } else if ( aid == type_index{typeid(FunctionType)} ) { |
---|
| 390 | FunctionType *af = as<FunctionType>(a), *bf = as<FunctionType>(b); |
---|
| 391 | |
---|
| 392 | vector<Type*> aparams, bparams; |
---|
| 393 | flattenList( af->get_parameters(), back_inserter( aparams ) ); |
---|
| 394 | flattenList( bf->get_parameters(), back_inserter( bparams ) ); |
---|
| 395 | if ( aparams.size() != bparams.size() ) return false; |
---|
| 396 | |
---|
| 397 | vector<Type*> areturns, breturns; |
---|
| 398 | flattenList( af->get_returnVals(), back_inserter( areturns ) ); |
---|
| 399 | flattenList( bf->get_returnVals(), back_inserter( breturns ) ); |
---|
| 400 | if ( areturns.size() != breturns.size() ) return false; |
---|
| 401 | |
---|
| 402 | for ( unsigned i = 0; i < aparams.size(); ++i ) { |
---|
| 403 | if ( ! typesPolyCompatible( aparams[i], bparams[i] ) ) return false; |
---|
| 404 | } |
---|
| 405 | for ( unsigned i = 0; i < areturns.size(); ++i ) { |
---|
| 406 | if ( ! typesPolyCompatible( areturns[i], breturns[i] ) ) return false; |
---|
| 407 | } |
---|
| 408 | return true; |
---|
| 409 | } else if ( aid == type_index{typeid(StructInstType)} ) { |
---|
| 410 | StructInstType *aa = as<StructInstType>(a), *ba = as<StructInstType>(b); |
---|
| 411 | |
---|
| 412 | if ( aa->get_name() != ba->get_name() ) return false; |
---|
| 413 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() ); |
---|
| 414 | } else if ( aid == type_index{typeid(UnionInstType)} ) { |
---|
| 415 | UnionInstType *aa = as<UnionInstType>(a), *ba = as<UnionInstType>(b); |
---|
| 416 | |
---|
| 417 | if ( aa->get_name() != ba->get_name() ) return false; |
---|
| 418 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() ); |
---|
| 419 | } else if ( aid == type_index{typeid(EnumInstType)} ) { |
---|
| 420 | return as<EnumInstType>(a)->get_name() == as<EnumInstType>(b)->get_name(); |
---|
| 421 | } else if ( aid == type_index{typeid(TraitInstType)} ) { |
---|
| 422 | return as<TraitInstType>(a)->get_name() == as<TraitInstType>(b)->get_name(); |
---|
| 423 | } else if ( aid == type_index{typeid(TupleType)} ) { |
---|
| 424 | TupleType *at = as<TupleType>(a), *bt = as<TupleType>(b); |
---|
| 425 | |
---|
| 426 | vector<Type*> atypes, btypes; |
---|
| 427 | flattenList( at->get_types(), back_inserter( atypes ) ); |
---|
| 428 | flattenList( bt->get_types(), back_inserter( btypes ) ); |
---|
| 429 | if ( atypes.size() != btypes.size() ) return false; |
---|
| 430 | |
---|
| 431 | for ( unsigned i = 0; i < atypes.size(); ++i ) { |
---|
| 432 | if ( ! typesPolyCompatible( atypes[i], btypes[i] ) ) return false; |
---|
| 433 | } |
---|
| 434 | return true; |
---|
| 435 | } else return true; // VoidType, VarArgsType, ZeroType & OneType just need the same type |
---|
| 436 | } |
---|
| 437 | |
---|
[ae1b9ea] | 438 | bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, TypeSubstitution * env ) { |
---|
| 439 | // is parameter is not polymorphic, don't need to box |
---|
| 440 | if ( ! isPolyType( param, exprTyVars ) ) return false; |
---|
| 441 | Type * newType = arg->clone(); |
---|
| 442 | if ( env ) env->apply( newType ); |
---|
| 443 | std::unique_ptr<Type> manager( newType ); |
---|
| 444 | // if the argument's type is polymorphic, we don't need to box again! |
---|
| 445 | return ! isPolyType( newType ); |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, TypeSubstitution * env ) { |
---|
| 449 | FunctionType * function = getFunctionType( appExpr->function->result ); |
---|
| 450 | assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() ); |
---|
| 451 | TyVarMap exprTyVars( TypeDecl::Data{} ); |
---|
| 452 | makeTyVarMap( function, exprTyVars ); |
---|
| 453 | return needsBoxing( param, arg, exprTyVars, env ); |
---|
| 454 | } |
---|
| 455 | |
---|
[2c57025] | 456 | void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) { |
---|
[9b18044] | 457 | // xxx - should this actually be insert? |
---|
[2c57025] | 458 | tyVarMap[ tyVar->get_name() ] = TypeDecl::Data{ tyVar }; |
---|
| 459 | } |
---|
| 460 | |
---|
[aadc9a4] | 461 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) { |
---|
[8c49c0e] | 462 | for ( Type::ForallList::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) { |
---|
[aadc9a4] | 463 | assert( *tyVar ); |
---|
[2c57025] | 464 | addToTyVarMap( *tyVar, tyVarMap ); |
---|
[aadc9a4] | 465 | } |
---|
| 466 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) { |
---|
| 467 | makeTyVarMap( pointer->get_base(), tyVarMap ); |
---|
| 468 | } |
---|
| 469 | } |
---|
[540de412] | 470 | |
---|
[01aeade] | 471 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) { |
---|
| 472 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) { |
---|
| 473 | os << i->first << " (" << i->second << ") "; |
---|
| 474 | } // for |
---|
| 475 | os << std::endl; |
---|
| 476 | } |
---|
[ffad73a] | 477 | |
---|
[51b7345] | 478 | } // namespace GenPoly |
---|
[01aeade] | 479 | |
---|
[51587aa] | 480 | // Local Variables: // |
---|
| 481 | // tab-width: 4 // |
---|
| 482 | // mode: c++ // |
---|
| 483 | // compile-command: "make install" // |
---|
| 484 | // End: // |
---|