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