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