[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
|
---|
[3606fe4] | 11 | // Last Modified By : Andrew Beach
|
---|
[63d1ebe] | 12 | // Last Modified On : Mon Oct 24 15:19:00 2022
|
---|
| 13 | // Update Count : 17
|
---|
[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 |
|
---|
[a0d1f1c] | 26 | #include "AST/Expr.hpp"
|
---|
[d76c588] | 27 | #include "AST/Type.hpp"
|
---|
[a0d1f1c] | 28 | #include "AST/TypeSubstitution.hpp"
|
---|
[08fc48f] | 29 | #include "GenPoly/ErasableScopedMap.h" // for ErasableScopedMap<>::const_it...
|
---|
| 30 | #include "ResolvExpr/typeops.h" // for flatten
|
---|
| 31 | #include "SynTree/Constant.h" // for Constant
|
---|
| 32 | #include "SynTree/Expression.h" // for Expression, TypeExpr, Constan...
|
---|
| 33 | #include "SynTree/Type.h" // for Type, StructInstType, UnionIn...
|
---|
| 34 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
[51b73452] | 35 |
|
---|
[b1a6d6b] | 36 | using namespace std;
|
---|
[51b73452] | 37 |
|
---|
| 38 | namespace GenPoly {
|
---|
[ffad73a] | 39 | namespace {
|
---|
[0f889a77] | 40 | /// Checks a parameter list for polymorphic parameters; will substitute according to env if present
|
---|
| 41 | bool hasPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
|
---|
| 42 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 43 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
[7350ff97] | 44 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
[0f889a77] | 45 | if ( isPolyType( paramType->get_type(), env ) ) return true;
|
---|
| 46 | }
|
---|
| 47 | return false;
|
---|
| 48 | }
|
---|
| 49 |
|
---|
[490fb92e] | 50 | bool hasPolyParams( const std::vector<ast::ptr<ast::Expr>> & params, const ast::TypeSubstitution * env) {
|
---|
| 51 | for (auto ¶m : params) {
|
---|
| 52 | auto paramType = param.strict_as<ast::TypeExpr>();
|
---|
| 53 | if (isPolyType(paramType->type, env)) return true;
|
---|
| 54 | }
|
---|
| 55 | return false;
|
---|
| 56 | }
|
---|
| 57 |
|
---|
[0f889a77] | 58 | /// Checks a parameter list for polymorphic parameters from tyVars; will substitute according to env if present
|
---|
[ffad73a] | 59 | bool hasPolyParams( 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");
|
---|
[ffad73a] | 63 | if ( isPolyType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
| 64 | }
|
---|
| 65 | return false;
|
---|
| 66 | }
|
---|
[3bb195cb] | 67 |
|
---|
| 68 | /// Checks a parameter list for dynamic-layout parameters from tyVars; will substitute according to env if present
|
---|
| 69 | bool hasDynParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 70 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 71 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
[5a3ac84] | 72 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
[3bb195cb] | 73 | if ( isDynType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
| 74 | }
|
---|
| 75 | return false;
|
---|
| 76 | }
|
---|
[5a3ac84] | 77 |
|
---|
[c8837e5] | 78 | bool hasDynParams(
|
---|
| 79 | const std::vector<ast::ptr<ast::Expr>> & params,
|
---|
| 80 | const TypeVarMap & typeVars,
|
---|
| 81 | const ast::TypeSubstitution * subst ) {
|
---|
| 82 | for ( ast::ptr<ast::Expr> const & paramExpr : params ) {
|
---|
| 83 | auto param = paramExpr.as<ast::TypeExpr>();
|
---|
| 84 | assertf( param, "Aggregate parameters should be type expressions." );
|
---|
| 85 | if ( isDynType( param->type.get(), typeVars, subst ) ) {
|
---|
[3606fe4] | 86 | return true;
|
---|
| 87 | }
|
---|
| 88 | }
|
---|
| 89 | return false;
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[5a3ac84] | 92 | /// Checks a parameter list for inclusion of polymorphic parameters; will substitute according to env if present
|
---|
| 93 | bool includesPolyParams( std::list< Expression* >& params, const TypeSubstitution *env ) {
|
---|
| 94 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 95 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 96 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
| 97 | if ( includesPolyType( paramType->get_type(), env ) ) return true;
|
---|
| 98 | }
|
---|
| 99 | return false;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
| 102 | /// Checks a parameter list for inclusion of polymorphic parameters from tyVars; will substitute according to env if present
|
---|
| 103 | bool includesPolyParams( std::list< Expression* >& params, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 104 | for ( std::list< Expression* >::iterator param = params.begin(); param != params.end(); ++param ) {
|
---|
| 105 | TypeExpr *paramType = dynamic_cast< TypeExpr* >( *param );
|
---|
| 106 | assertf(paramType, "Aggregate parameters should be type expressions");
|
---|
| 107 | if ( includesPolyType( paramType->get_type(), tyVars, env ) ) return true;
|
---|
| 108 | }
|
---|
| 109 | return false;
|
---|
| 110 | }
|
---|
[c2ad3c9] | 111 | }
|
---|
[83de11e] | 112 |
|
---|
[c2ad3c9] | 113 | Type* replaceTypeInst( Type* type, const TypeSubstitution* env ) {
|
---|
| 114 | if ( ! env ) return type;
|
---|
| 115 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType* >( type ) ) {
|
---|
| 116 | Type *newType = env->lookup( typeInst->get_name() );
|
---|
| 117 | if ( newType ) return newType;
|
---|
[e24955a] | 118 | }
|
---|
[c2ad3c9] | 119 | return type;
|
---|
[ffad73a] | 120 | }
|
---|
[0f889a77] | 121 |
|
---|
[4da152a] | 122 | const Type* replaceTypeInst( const Type* type, const TypeSubstitution* env ) {
|
---|
| 123 | if ( ! env ) return type;
|
---|
| 124 | if ( auto typeInst = dynamic_cast< const TypeInstType* >( type ) ) {
|
---|
| 125 | Type *newType = env->lookup( typeInst->get_name() );
|
---|
| 126 | if ( newType ) return newType;
|
---|
| 127 | }
|
---|
| 128 | return type;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[490fb92e] | 131 | const ast::Type * replaceTypeInst(const ast::Type * type, const ast::TypeSubstitution * env) {
|
---|
| 132 | if (!env) return type;
|
---|
[75f6a5f] | 133 | if ( auto typeInst = dynamic_cast<const ast::TypeInstType*>(type) ) {
|
---|
[3e5dd913] | 134 | auto newType = env->lookup(typeInst);
|
---|
[490fb92e] | 135 | if (newType) return newType;
|
---|
| 136 | }
|
---|
| 137 | return type;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[0f889a77] | 140 | Type *isPolyType( Type *type, const TypeSubstitution *env ) {
|
---|
[e24955a] | 141 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 142 |
|
---|
[ca35c51] | 143 | if ( dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[0f889a77] | 144 | return type;
|
---|
[5f61546] | 145 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 146 | return isPolyType( arrayType->base, env );
|
---|
[0f889a77] | 147 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 148 | if ( hasPolyParams( structType->get_parameters(), env ) ) return type;
|
---|
| 149 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 150 | if ( hasPolyParams( unionType->get_parameters(), env ) ) return type;
|
---|
| 151 | }
|
---|
| 152 | return 0;
|
---|
| 153 | }
|
---|
[540de412] | 154 |
|
---|
[490fb92e] | 155 | const ast::Type * isPolyType(const ast::Type * type, const ast::TypeSubstitution * env) {
|
---|
| 156 | type = replaceTypeInst( type, env );
|
---|
| 157 |
|
---|
| 158 | if ( dynamic_cast< const ast::TypeInstType * >( type ) ) {
|
---|
| 159 | return type;
|
---|
| 160 | } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
|
---|
| 161 | return isPolyType( arrayType->base, env );
|
---|
| 162 | } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
|
---|
| 163 | if ( hasPolyParams( structType->params, env ) ) return type;
|
---|
| 164 | } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
|
---|
| 165 | if ( hasPolyParams( unionType->params, env ) ) return type;
|
---|
| 166 | }
|
---|
| 167 | return 0;
|
---|
| 168 | }
|
---|
| 169 |
|
---|
[ffad73a] | 170 | Type *isPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
[e24955a] | 171 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 172 |
|
---|
[e56cfdb0] | 173 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
[ffad73a] | 174 | if ( tyVars.find( typeInst->get_name() ) != tyVars.end() ) {
|
---|
| 175 | return type;
|
---|
[0f889a77] | 176 | }
|
---|
[5f61546] | 177 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) {
|
---|
| 178 | return isPolyType( arrayType->base, tyVars, env );
|
---|
[ffad73a] | 179 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 180 | if ( hasPolyParams( structType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 181 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 182 | if ( hasPolyParams( unionType->get_parameters(), tyVars, env ) ) return type;
|
---|
| 183 | }
|
---|
| 184 | return 0;
|
---|
[01aeade] | 185 | }
|
---|
[b1a6d6b] | 186 |
|
---|
[490fb92e] | 187 | const ast::Type * isPolyType(const ast::Type * type, const TyVarMap & tyVars, const ast::TypeSubstitution * env) {
|
---|
| 188 | type = replaceTypeInst( type, env );
|
---|
| 189 |
|
---|
| 190 | if ( auto typeInst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
|
---|
[3e5dd913] | 191 | return tyVars.find(typeInst->typeString()) != tyVars.end() ? type : nullptr;
|
---|
[490fb92e] | 192 | } else if ( auto arrayType = dynamic_cast< const ast::ArrayType * >( type ) ) {
|
---|
| 193 | return isPolyType( arrayType->base, env );
|
---|
| 194 | } else if ( auto structType = dynamic_cast< const ast::StructInstType* >( type ) ) {
|
---|
| 195 | if ( hasPolyParams( structType->params, env ) ) return type;
|
---|
| 196 | } else if ( auto unionType = dynamic_cast< const ast::UnionInstType* >( type ) ) {
|
---|
| 197 | if ( hasPolyParams( unionType->params, env ) ) return type;
|
---|
| 198 | }
|
---|
| 199 | return nullptr;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
[c8837e5] | 202 | const ast::Type * isPolyType( const ast::Type * type,
|
---|
| 203 | const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
|
---|
| 204 | type = replaceTypeInst( type, subst );
|
---|
| 205 |
|
---|
| 206 | if ( auto inst = dynamic_cast< const ast::TypeInstType * >( type ) ) {
|
---|
[63d1ebe] | 207 | if ( typeVars.find( *inst ) != typeVars.end() ) return type;
|
---|
[c8837e5] | 208 | } else if ( auto array = dynamic_cast< const ast::ArrayType * >( type ) ) {
|
---|
| 209 | return isPolyType( array->base, subst );
|
---|
| 210 | } else if ( auto sue = dynamic_cast< const ast::StructInstType * >( type ) ) {
|
---|
| 211 | if ( hasPolyParams( sue->params, subst ) ) return type;
|
---|
| 212 | } else if ( auto sue = dynamic_cast< const ast::UnionInstType * >( type ) ) {
|
---|
| 213 | if ( hasPolyParams( sue->params, subst ) ) return type;
|
---|
| 214 | }
|
---|
| 215 | return nullptr;
|
---|
| 216 | }
|
---|
| 217 |
|
---|
[33a7b6d] | 218 | ReferenceToType *isDynType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
[3bb195cb] | 219 | type = replaceTypeInst( type, env );
|
---|
| 220 |
|
---|
| 221 | if ( TypeInstType *typeInst = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 222 | auto var = tyVars.find( typeInst->get_name() );
|
---|
[2c57025] | 223 | if ( var != tyVars.end() && var->second.isComplete ) {
|
---|
[33a7b6d] | 224 | return typeInst;
|
---|
[3bb195cb] | 225 | }
|
---|
| 226 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
[33a7b6d] | 227 | if ( hasDynParams( structType->get_parameters(), tyVars, env ) ) return structType;
|
---|
[3bb195cb] | 228 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
[33a7b6d] | 229 | if ( hasDynParams( unionType->get_parameters(), tyVars, env ) ) return unionType;
|
---|
[3bb195cb] | 230 | }
|
---|
| 231 | return 0;
|
---|
| 232 | }
|
---|
| 233 |
|
---|
[c8837e5] | 234 | const ast::BaseInstType * isDynType(
|
---|
| 235 | const ast::Type * type, const TypeVarMap & typeVars,
|
---|
| 236 | const ast::TypeSubstitution * subst ) {
|
---|
| 237 | type = replaceTypeInst( type, subst );
|
---|
| 238 |
|
---|
| 239 | if ( auto inst = dynamic_cast<ast::TypeInstType const *>( type ) ) {
|
---|
[63d1ebe] | 240 | auto var = typeVars.find( *inst );
|
---|
[c8837e5] | 241 | if ( var != typeVars.end() && var->second.isComplete ) {
|
---|
[75f6a5f] | 242 | return inst;
|
---|
[3606fe4] | 243 | }
|
---|
[c8837e5] | 244 | } else if ( auto inst = dynamic_cast<ast::StructInstType const *>( type ) ) {
|
---|
| 245 | if ( hasDynParams( inst->params, typeVars, subst ) ) {
|
---|
| 246 | return inst;
|
---|
| 247 | }
|
---|
| 248 | } else if ( auto inst = dynamic_cast<ast::UnionInstType const *>( type ) ) {
|
---|
| 249 | if ( hasDynParams( inst->params, typeVars, subst ) ) {
|
---|
| 250 | return inst;
|
---|
| 251 | }
|
---|
[3606fe4] | 252 | }
|
---|
[c8837e5] | 253 | return nullptr;
|
---|
| 254 | }
|
---|
[3606fe4] | 255 |
|
---|
[3bb195cb] | 256 | ReferenceToType *isDynRet( FunctionType *function, const TyVarMap &forallTypes ) {
|
---|
| 257 | if ( function->get_returnVals().empty() ) return 0;
|
---|
[8c49c0e] | 258 |
|
---|
[3bb195cb] | 259 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
|
---|
| 260 | }
|
---|
| 261 |
|
---|
[c8837e5] | 262 | const ast::BaseInstType *isDynRet(
|
---|
| 263 | const ast::FunctionType * type, const TypeVarMap & typeVars ) {
|
---|
| 264 | if ( type->returns.empty() ) return nullptr;
|
---|
| 265 |
|
---|
| 266 | return isDynType( type->returns.front(), typeVars );
|
---|
| 267 | }
|
---|
| 268 |
|
---|
[3bb195cb] | 269 | ReferenceToType *isDynRet( FunctionType *function ) {
|
---|
| 270 | if ( function->get_returnVals().empty() ) return 0;
|
---|
| 271 |
|
---|
[2c57025] | 272 | TyVarMap forallTypes( TypeDecl::Data{} );
|
---|
[3bb195cb] | 273 | makeTyVarMap( function, forallTypes );
|
---|
| 274 | return (ReferenceToType*)isDynType( function->get_returnVals().front()->get_type(), forallTypes );
|
---|
| 275 | }
|
---|
| 276 |
|
---|
| 277 | bool needsAdapter( FunctionType *adaptee, const TyVarMap &tyVars ) {
|
---|
| 278 | // if ( ! adaptee->get_returnVals().empty() && isPolyType( adaptee->get_returnVals().front()->get_type(), tyVars ) ) {
|
---|
| 279 | // return true;
|
---|
| 280 | // } // if
|
---|
| 281 | if ( isDynRet( adaptee, tyVars ) ) return true;
|
---|
[8c49c0e] | 282 |
|
---|
[3bb195cb] | 283 | for ( std::list< DeclarationWithType* >::const_iterator innerArg = adaptee->get_parameters().begin(); innerArg != adaptee->get_parameters().end(); ++innerArg ) {
|
---|
| 284 | // if ( isPolyType( (*innerArg)->get_type(), tyVars ) ) {
|
---|
| 285 | if ( isDynType( (*innerArg)->get_type(), tyVars ) ) {
|
---|
| 286 | return true;
|
---|
| 287 | } // if
|
---|
| 288 | } // for
|
---|
| 289 | return false;
|
---|
| 290 | }
|
---|
| 291 |
|
---|
[c8837e5] | 292 | bool needsAdapter(
|
---|
| 293 | ast::FunctionType const * adaptee, const TypeVarMap & typeVars ) {
|
---|
| 294 | if ( isDynRet( adaptee, typeVars ) ) return true;
|
---|
| 295 |
|
---|
| 296 | for ( auto param : adaptee->params ) {
|
---|
| 297 | if ( isDynType( param, typeVars ) ) {
|
---|
| 298 | return true;
|
---|
| 299 | }
|
---|
| 300 | }
|
---|
| 301 | return false;
|
---|
| 302 | }
|
---|
| 303 |
|
---|
[0f889a77] | 304 | Type *isPolyPtr( Type *type, const TypeSubstitution *env ) {
|
---|
[e24955a] | 305 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 306 |
|
---|
[0f889a77] | 307 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 308 | return isPolyType( ptr->get_base(), env );
|
---|
[e24955a] | 309 | }
|
---|
[0f889a77] | 310 | return 0;
|
---|
| 311 | }
|
---|
[540de412] | 312 |
|
---|
[ffad73a] | 313 | Type *isPolyPtr( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
[e24955a] | 314 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 315 |
|
---|
[ffad73a] | 316 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 317 | return isPolyType( ptr->get_base(), tyVars, env );
|
---|
[e24955a] | 318 | }
|
---|
[ffad73a] | 319 | return 0;
|
---|
[bdf1954] | 320 | }
|
---|
| 321 |
|
---|
[8488c715] | 322 | Type * hasPolyBase( Type *type, int *levels, const TypeSubstitution *env ) {
|
---|
| 323 | int dummy;
|
---|
| 324 | if ( ! levels ) { levels = &dummy; }
|
---|
| 325 | *levels = 0;
|
---|
| 326 |
|
---|
| 327 | while ( true ) {
|
---|
[e24955a] | 328 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 329 |
|
---|
[8488c715] | 330 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 331 | type = ptr->get_base();
|
---|
| 332 | ++(*levels);
|
---|
| 333 | } else break;
|
---|
[05d47278] | 334 | }
|
---|
| 335 |
|
---|
| 336 | return isPolyType( type, env );
|
---|
| 337 | }
|
---|
[540de412] | 338 |
|
---|
[8488c715] | 339 | Type * hasPolyBase( Type *type, const TyVarMap &tyVars, int *levels, const TypeSubstitution *env ) {
|
---|
| 340 | int dummy;
|
---|
| 341 | if ( ! levels ) { levels = &dummy; }
|
---|
| 342 | *levels = 0;
|
---|
| 343 |
|
---|
| 344 | while ( true ) {
|
---|
[e24955a] | 345 | type = replaceTypeInst( type, env );
|
---|
[83de11e] | 346 |
|
---|
[8488c715] | 347 | if ( PointerType *ptr = dynamic_cast< PointerType *>( type ) ) {
|
---|
| 348 | type = ptr->get_base();
|
---|
| 349 | ++(*levels);
|
---|
| 350 | } else break;
|
---|
[05d47278] | 351 | }
|
---|
| 352 |
|
---|
| 353 | return isPolyType( type, tyVars, env );
|
---|
| 354 | }
|
---|
| 355 |
|
---|
[c8837e5] | 356 | ast::Type const * hasPolyBase(
|
---|
| 357 | ast::Type const * type, const TypeVarMap & typeVars,
|
---|
| 358 | int * levels, const ast::TypeSubstitution * subst ) {
|
---|
| 359 | int level_count = 0;
|
---|
| 360 |
|
---|
| 361 | while ( true ) {
|
---|
| 362 | type = replaceTypeInst( type, subst );
|
---|
| 363 |
|
---|
| 364 | if ( auto ptr = dynamic_cast<ast::PointerType const *>( type ) ) {
|
---|
| 365 | type = ptr->base;
|
---|
| 366 | ++level_count;
|
---|
| 367 | } else {
|
---|
| 368 | break;
|
---|
| 369 | }
|
---|
| 370 | }
|
---|
| 371 |
|
---|
| 372 | if ( nullptr != levels ) { *levels = level_count; }
|
---|
| 373 | return isPolyType( type, typeVars, subst );
|
---|
| 374 | }
|
---|
| 375 |
|
---|
[5a3ac84] | 376 | bool includesPolyType( Type *type, const TypeSubstitution *env ) {
|
---|
| 377 | type = replaceTypeInst( type, env );
|
---|
| 378 |
|
---|
| 379 | if ( dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 380 | return true;
|
---|
| 381 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
|
---|
| 382 | if ( includesPolyType( pointerType->get_base(), env ) ) return true;
|
---|
| 383 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 384 | if ( includesPolyParams( structType->get_parameters(), env ) ) return true;
|
---|
| 385 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 386 | if ( includesPolyParams( unionType->get_parameters(), env ) ) return true;
|
---|
| 387 | }
|
---|
| 388 | return false;
|
---|
| 389 | }
|
---|
| 390 |
|
---|
| 391 | bool includesPolyType( Type *type, const TyVarMap &tyVars, const TypeSubstitution *env ) {
|
---|
| 392 | type = replaceTypeInst( type, env );
|
---|
| 393 |
|
---|
| 394 | if ( TypeInstType *typeInstType = dynamic_cast< TypeInstType * >( type ) ) {
|
---|
| 395 | if ( tyVars.find( typeInstType->get_name() ) != tyVars.end() ) {
|
---|
| 396 | return true;
|
---|
| 397 | }
|
---|
| 398 | } else if ( PointerType *pointerType = dynamic_cast< PointerType* >( type ) ) {
|
---|
| 399 | if ( includesPolyType( pointerType->get_base(), tyVars, env ) ) return true;
|
---|
| 400 | } else if ( StructInstType *structType = dynamic_cast< StructInstType* >( type ) ) {
|
---|
| 401 | if ( includesPolyParams( structType->get_parameters(), tyVars, env ) ) return true;
|
---|
| 402 | } else if ( UnionInstType *unionType = dynamic_cast< UnionInstType* >( type ) ) {
|
---|
| 403 | if ( includesPolyParams( unionType->get_parameters(), tyVars, env ) ) return true;
|
---|
| 404 | }
|
---|
| 405 | return false;
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[7754cde] | 408 | FunctionType * getFunctionType( Type *ty ) {
|
---|
| 409 | PointerType *ptrType;
|
---|
| 410 | if ( ( ptrType = dynamic_cast< PointerType* >( ty ) ) ) {
|
---|
| 411 | return dynamic_cast< FunctionType* >( ptrType->get_base() ); // pointer if FunctionType, NULL otherwise
|
---|
| 412 | } else {
|
---|
| 413 | return dynamic_cast< FunctionType* >( ty ); // pointer if FunctionType, NULL otherwise
|
---|
| 414 | }
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[d76c588] | 417 | const ast::FunctionType * getFunctionType( const ast::Type * ty ) {
|
---|
| 418 | if ( auto pty = dynamic_cast< const ast::PointerType * >( ty ) ) {
|
---|
| 419 | return pty->base.as< ast::FunctionType >();
|
---|
| 420 | } else {
|
---|
| 421 | return dynamic_cast< const ast::FunctionType * >( ty );
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[8488c715] | 425 | VariableExpr * getBaseVar( Expression *expr, int *levels ) {
|
---|
| 426 | int dummy;
|
---|
| 427 | if ( ! levels ) { levels = &dummy; }
|
---|
| 428 | *levels = 0;
|
---|
| 429 |
|
---|
| 430 | while ( true ) {
|
---|
| 431 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( expr ) ) {
|
---|
| 432 | return varExpr;
|
---|
[d9f1b2d] | 433 | } else if ( MemberExpr *memberExpr = dynamic_cast< MemberExpr* >( expr ) ) {
|
---|
| 434 | expr = memberExpr->get_aggregate();
|
---|
[8488c715] | 435 | } else if ( AddressExpr *addressExpr = dynamic_cast< AddressExpr* >( expr ) ) {
|
---|
| 436 | expr = addressExpr->get_arg();
|
---|
| 437 | } else if ( UntypedExpr *untypedExpr = dynamic_cast< UntypedExpr* >( expr ) ) {
|
---|
| 438 | // look for compiler-inserted dereference operator
|
---|
| 439 | NameExpr *fn = dynamic_cast< NameExpr* >( untypedExpr->get_function() );
|
---|
| 440 | if ( ! fn || fn->get_name() != std::string("*?") ) return 0;
|
---|
| 441 | expr = *untypedExpr->begin_args();
|
---|
[540de412] | 442 | } else if ( CommaExpr *commaExpr = dynamic_cast< CommaExpr* >( expr ) ) {
|
---|
| 443 | // copy constructors insert comma exprs, look at second argument which contains the variable
|
---|
| 444 | expr = commaExpr->get_arg2();
|
---|
| 445 | continue;
|
---|
[1aa4b71] | 446 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( expr ) ) {
|
---|
| 447 | int lvl1;
|
---|
| 448 | int lvl2;
|
---|
| 449 | VariableExpr * var1 = getBaseVar( condExpr->get_arg2(), &lvl1 );
|
---|
| 450 | VariableExpr * var2 = getBaseVar( condExpr->get_arg3(), &lvl2 );
|
---|
| 451 | if ( lvl1 == lvl2 && var1 && var2 && var1->get_var() == var2->get_var() ) {
|
---|
| 452 | *levels = lvl1;
|
---|
| 453 | return var1;
|
---|
| 454 | }
|
---|
| 455 | break;
|
---|
[8488c715] | 456 | } else break;
|
---|
| 457 |
|
---|
| 458 | ++(*levels);
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | return 0;
|
---|
[05d47278] | 462 | }
|
---|
| 463 |
|
---|
[5a3ac84] | 464 | namespace {
|
---|
| 465 | /// Checks if is a pointer to D
|
---|
| 466 | template<typename D, typename B>
|
---|
| 467 | bool is( const B* p ) { return type_index{typeid(D)} == type_index{typeid(*p)}; }
|
---|
| 468 |
|
---|
| 469 | /// Converts to a pointer to D without checking for safety
|
---|
| 470 | template<typename D, typename B>
|
---|
| 471 | inline D* as( B* p ) { return reinterpret_cast<D*>(p); }
|
---|
| 472 |
|
---|
[3606fe4] | 473 | template<typename D, typename B>
|
---|
| 474 | inline D const * as( B const * p ) {
|
---|
| 475 | return reinterpret_cast<D const *>( p );
|
---|
| 476 | }
|
---|
| 477 |
|
---|
[5a3ac84] | 478 | /// Flattens a declaration list
|
---|
| 479 | template<typename Output>
|
---|
| 480 | void flattenList( list< DeclarationWithType* > src, Output out ) {
|
---|
| 481 | for ( DeclarationWithType* decl : src ) {
|
---|
| 482 | ResolvExpr::flatten( decl->get_type(), out );
|
---|
| 483 | }
|
---|
| 484 | }
|
---|
| 485 |
|
---|
| 486 | /// Flattens a list of types
|
---|
| 487 | template<typename Output>
|
---|
| 488 | void flattenList( list< Type* > src, Output out ) {
|
---|
| 489 | for ( Type* ty : src ) {
|
---|
| 490 | ResolvExpr::flatten( ty, out );
|
---|
| 491 | }
|
---|
| 492 | }
|
---|
| 493 |
|
---|
[7b5694d] | 494 | /// Flattens a list of types.
|
---|
| 495 | // There is another flattenList in Unify.
|
---|
[3606fe4] | 496 | void flattenList( vector<ast::ptr<ast::Type>> const & src,
|
---|
| 497 | vector<ast::ptr<ast::Type>> & out ) {
|
---|
| 498 | for ( auto const & type : src ) {
|
---|
| 499 | ResolvExpr::flatten( type, out );
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 |
|
---|
[5a3ac84] | 503 | /// Checks if two lists of parameters are equal up to polymorphic substitution.
|
---|
| 504 | bool paramListsPolyCompatible( const list< Expression* >& aparams, const list< Expression* >& bparams ) {
|
---|
| 505 | if ( aparams.size() != bparams.size() ) return false;
|
---|
| 506 |
|
---|
| 507 | for ( list< Expression* >::const_iterator at = aparams.begin(), bt = bparams.begin();
|
---|
| 508 | at != aparams.end(); ++at, ++bt ) {
|
---|
| 509 | TypeExpr *aparam = dynamic_cast< TypeExpr* >(*at);
|
---|
| 510 | assertf(aparam, "Aggregate parameters should be type expressions");
|
---|
| 511 | TypeExpr *bparam = dynamic_cast< TypeExpr* >(*bt);
|
---|
| 512 | assertf(bparam, "Aggregate parameters should be type expressions");
|
---|
| 513 |
|
---|
[cccc534] | 514 | // xxx - might need to let VoidType be a wildcard here too; could have some voids
|
---|
[5a3ac84] | 515 | // stuffed in for dtype-statics.
|
---|
| 516 | // if ( is<VoidType>( aparam->get_type() ) || is<VoidType>( bparam->get_type() ) ) continue;
|
---|
| 517 | if ( ! typesPolyCompatible( aparam->get_type(), bparam->get_type() ) ) return false;
|
---|
| 518 | }
|
---|
[cccc534] | 519 |
|
---|
[5a3ac84] | 520 | return true;
|
---|
| 521 | }
|
---|
[3606fe4] | 522 |
|
---|
| 523 | bool paramListsPolyCompatible(
|
---|
| 524 | std::vector<ast::ptr<ast::Expr>> const & lparams,
|
---|
| 525 | std::vector<ast::ptr<ast::Expr>> const & rparams ) {
|
---|
| 526 | if ( lparams.size() != rparams.size() ) {
|
---|
| 527 | return false;
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | for ( auto lparam = lparams.begin(), rparam = rparams.begin() ;
|
---|
| 531 | lparam != lparams.end() ; ++lparam, ++rparam ) {
|
---|
| 532 | ast::TypeExpr const * lexpr = lparam->as<ast::TypeExpr>();
|
---|
| 533 | assertf( lexpr, "Aggregate parameters should be type expressions" );
|
---|
| 534 | ast::TypeExpr const * rexpr = rparam->as<ast::TypeExpr>();
|
---|
| 535 | assertf( rexpr, "Aggregate parameters should be type expressions" );
|
---|
| 536 |
|
---|
| 537 | // xxx - might need to let VoidType be a wildcard here too; could have some voids
|
---|
| 538 | // stuffed in for dtype-statics.
|
---|
| 539 | // if ( is<VoidType>( lexpr->type() ) || is<VoidType>( bparam->get_type() ) ) continue;
|
---|
| 540 | if ( !typesPolyCompatible( lexpr->type, rexpr->type ) ) {
|
---|
| 541 | return false;
|
---|
| 542 | }
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | return true;
|
---|
| 546 | }
|
---|
[5a3ac84] | 547 | }
|
---|
| 548 |
|
---|
| 549 | bool typesPolyCompatible( Type *a, Type *b ) {
|
---|
| 550 | type_index aid{ typeid(*a) };
|
---|
| 551 | // polymorphic types always match
|
---|
| 552 | if ( aid == type_index{typeid(TypeInstType)} ) return true;
|
---|
[cccc534] | 553 |
|
---|
[5a3ac84] | 554 | type_index bid{ typeid(*b) };
|
---|
| 555 | // polymorphic types always match
|
---|
| 556 | if ( bid == type_index{typeid(TypeInstType)} ) return true;
|
---|
[cccc534] | 557 |
|
---|
[5a3ac84] | 558 | // can't match otherwise if different types
|
---|
| 559 | if ( aid != bid ) return false;
|
---|
| 560 |
|
---|
| 561 | // recurse through type structure (conditions borrowed from Unify.cc)
|
---|
| 562 | if ( aid == type_index{typeid(BasicType)} ) {
|
---|
| 563 | return as<BasicType>(a)->get_kind() == as<BasicType>(b)->get_kind();
|
---|
| 564 | } else if ( aid == type_index{typeid(PointerType)} ) {
|
---|
| 565 | PointerType *ap = as<PointerType>(a), *bp = as<PointerType>(b);
|
---|
| 566 |
|
---|
| 567 | // void pointers should match any other pointer type
|
---|
| 568 | return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() )
|
---|
| 569 | || typesPolyCompatible( ap->get_base(), bp->get_base() );
|
---|
[682dcae] | 570 | } else if ( aid == type_index{typeid(ReferenceType)} ) {
|
---|
| 571 | ReferenceType *ap = as<ReferenceType>(a), *bp = as<ReferenceType>(b);
|
---|
| 572 | return is<VoidType>( ap->get_base() ) || is<VoidType>( bp->get_base() )
|
---|
| 573 | || typesPolyCompatible( ap->get_base(), bp->get_base() );
|
---|
[5a3ac84] | 574 | } else if ( aid == type_index{typeid(ArrayType)} ) {
|
---|
| 575 | ArrayType *aa = as<ArrayType>(a), *ba = as<ArrayType>(b);
|
---|
| 576 |
|
---|
| 577 | if ( aa->get_isVarLen() ) {
|
---|
| 578 | if ( ! ba->get_isVarLen() ) return false;
|
---|
| 579 | } else {
|
---|
| 580 | if ( ba->get_isVarLen() ) return false;
|
---|
| 581 |
|
---|
| 582 | ConstantExpr *ad = dynamic_cast<ConstantExpr*>( aa->get_dimension() );
|
---|
| 583 | ConstantExpr *bd = dynamic_cast<ConstantExpr*>( ba->get_dimension() );
|
---|
[cccc534] | 584 | if ( ad && bd
|
---|
[5a3ac84] | 585 | && ad->get_constant()->get_value() != bd->get_constant()->get_value() )
|
---|
| 586 | return false;
|
---|
| 587 | }
|
---|
| 588 |
|
---|
| 589 | return typesPolyCompatible( aa->get_base(), ba->get_base() );
|
---|
| 590 | } else if ( aid == type_index{typeid(FunctionType)} ) {
|
---|
| 591 | FunctionType *af = as<FunctionType>(a), *bf = as<FunctionType>(b);
|
---|
| 592 |
|
---|
| 593 | vector<Type*> aparams, bparams;
|
---|
| 594 | flattenList( af->get_parameters(), back_inserter( aparams ) );
|
---|
| 595 | flattenList( bf->get_parameters(), back_inserter( bparams ) );
|
---|
| 596 | if ( aparams.size() != bparams.size() ) return false;
|
---|
| 597 |
|
---|
| 598 | vector<Type*> areturns, breturns;
|
---|
| 599 | flattenList( af->get_returnVals(), back_inserter( areturns ) );
|
---|
| 600 | flattenList( bf->get_returnVals(), back_inserter( breturns ) );
|
---|
| 601 | if ( areturns.size() != breturns.size() ) return false;
|
---|
| 602 |
|
---|
| 603 | for ( unsigned i = 0; i < aparams.size(); ++i ) {
|
---|
| 604 | if ( ! typesPolyCompatible( aparams[i], bparams[i] ) ) return false;
|
---|
| 605 | }
|
---|
| 606 | for ( unsigned i = 0; i < areturns.size(); ++i ) {
|
---|
| 607 | if ( ! typesPolyCompatible( areturns[i], breturns[i] ) ) return false;
|
---|
| 608 | }
|
---|
| 609 | return true;
|
---|
| 610 | } else if ( aid == type_index{typeid(StructInstType)} ) {
|
---|
| 611 | StructInstType *aa = as<StructInstType>(a), *ba = as<StructInstType>(b);
|
---|
| 612 |
|
---|
| 613 | if ( aa->get_name() != ba->get_name() ) return false;
|
---|
| 614 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
|
---|
| 615 | } else if ( aid == type_index{typeid(UnionInstType)} ) {
|
---|
| 616 | UnionInstType *aa = as<UnionInstType>(a), *ba = as<UnionInstType>(b);
|
---|
| 617 |
|
---|
| 618 | if ( aa->get_name() != ba->get_name() ) return false;
|
---|
| 619 | return paramListsPolyCompatible( aa->get_parameters(), ba->get_parameters() );
|
---|
| 620 | } else if ( aid == type_index{typeid(EnumInstType)} ) {
|
---|
| 621 | return as<EnumInstType>(a)->get_name() == as<EnumInstType>(b)->get_name();
|
---|
| 622 | } else if ( aid == type_index{typeid(TraitInstType)} ) {
|
---|
| 623 | return as<TraitInstType>(a)->get_name() == as<TraitInstType>(b)->get_name();
|
---|
| 624 | } else if ( aid == type_index{typeid(TupleType)} ) {
|
---|
| 625 | TupleType *at = as<TupleType>(a), *bt = as<TupleType>(b);
|
---|
| 626 |
|
---|
| 627 | vector<Type*> atypes, btypes;
|
---|
| 628 | flattenList( at->get_types(), back_inserter( atypes ) );
|
---|
| 629 | flattenList( bt->get_types(), back_inserter( btypes ) );
|
---|
| 630 | if ( atypes.size() != btypes.size() ) return false;
|
---|
| 631 |
|
---|
| 632 | for ( unsigned i = 0; i < atypes.size(); ++i ) {
|
---|
| 633 | if ( ! typesPolyCompatible( atypes[i], btypes[i] ) ) return false;
|
---|
| 634 | }
|
---|
| 635 | return true;
|
---|
| 636 | } else return true; // VoidType, VarArgsType, ZeroType & OneType just need the same type
|
---|
| 637 | }
|
---|
| 638 |
|
---|
[3606fe4] | 639 | bool typesPolyCompatible( ast::Type const * lhs, ast::Type const * rhs ) {
|
---|
| 640 | type_index const lid = typeid(*lhs);
|
---|
| 641 |
|
---|
| 642 | // Polymorphic types always match:
|
---|
| 643 | if ( type_index(typeid(ast::TypeInstType)) == lid ) return true;
|
---|
| 644 |
|
---|
| 645 | type_index const rid = typeid(*rhs);
|
---|
| 646 | if ( type_index(typeid(ast::TypeInstType)) == rid ) return true;
|
---|
| 647 |
|
---|
| 648 | // All other types only match if they are the same type:
|
---|
| 649 | if ( lid != rid ) return false;
|
---|
| 650 |
|
---|
| 651 | // So remaining types can be examined case by case.
|
---|
| 652 | // Recurse through type structure (conditions borrowed from Unify.cc).
|
---|
| 653 |
|
---|
| 654 | if ( type_index(typeid(ast::BasicType)) == lid ) {
|
---|
| 655 | return as<ast::BasicType>(lhs)->kind == as<ast::BasicType>(rhs)->kind;
|
---|
| 656 | } else if ( type_index(typeid(ast::PointerType)) == lid ) {
|
---|
| 657 | ast::PointerType const * l = as<ast::PointerType>(lhs);
|
---|
| 658 | ast::PointerType const * r = as<ast::PointerType>(rhs);
|
---|
| 659 |
|
---|
| 660 | // void pointers should match any other pointer type.
|
---|
| 661 | return is<ast::VoidType>( l->base.get() )
|
---|
| 662 | || is<ast::VoidType>( r->base.get() )
|
---|
| 663 | || typesPolyCompatible( l->base.get(), r->base.get() );
|
---|
| 664 | } else if ( type_index(typeid(ast::ReferenceType)) == lid ) {
|
---|
| 665 | ast::ReferenceType const * l = as<ast::ReferenceType>(lhs);
|
---|
| 666 | ast::ReferenceType const * r = as<ast::ReferenceType>(rhs);
|
---|
| 667 |
|
---|
| 668 | // void references should match any other reference type.
|
---|
| 669 | return is<ast::VoidType>( l->base.get() )
|
---|
| 670 | || is<ast::VoidType>( r->base.get() )
|
---|
| 671 | || typesPolyCompatible( l->base.get(), r->base.get() );
|
---|
| 672 | } else if ( type_index(typeid(ast::ArrayType)) == lid ) {
|
---|
| 673 | ast::ArrayType const * l = as<ast::ArrayType>(lhs);
|
---|
| 674 | ast::ArrayType const * r = as<ast::ArrayType>(rhs);
|
---|
| 675 |
|
---|
| 676 | if ( l->isVarLen ) {
|
---|
| 677 | if ( !r->isVarLen ) return false;
|
---|
| 678 | } else {
|
---|
| 679 | if ( r->isVarLen ) return false;
|
---|
| 680 |
|
---|
| 681 | auto lc = l->dimension.as<ast::ConstantExpr>();
|
---|
| 682 | auto rc = r->dimension.as<ast::ConstantExpr>();
|
---|
| 683 | if ( lc && rc && lc->intValue() != rc->intValue() ) {
|
---|
| 684 | return false;
|
---|
| 685 | }
|
---|
| 686 | }
|
---|
| 687 |
|
---|
| 688 | return typesPolyCompatible( l->base.get(), r->base.get() );
|
---|
| 689 | } else if ( type_index(typeid(ast::FunctionType)) == lid ) {
|
---|
| 690 | ast::FunctionType const * l = as<ast::FunctionType>(lhs);
|
---|
| 691 | ast::FunctionType const * r = as<ast::FunctionType>(rhs);
|
---|
| 692 |
|
---|
| 693 | std::vector<ast::ptr<ast::Type>> lparams, rparams;
|
---|
| 694 | flattenList( l->params, lparams );
|
---|
| 695 | flattenList( r->params, rparams );
|
---|
| 696 | if ( lparams.size() != rparams.size() ) return false;
|
---|
| 697 | for ( unsigned i = 0; i < lparams.size(); ++i ) {
|
---|
| 698 | if ( !typesPolyCompatible( lparams[i], rparams[i] ) ) return false;
|
---|
| 699 | }
|
---|
| 700 |
|
---|
| 701 | std::vector<ast::ptr<ast::Type>> lrets, rrets;
|
---|
| 702 | flattenList( l->returns, lrets );
|
---|
| 703 | flattenList( r->returns, rrets );
|
---|
| 704 | if ( lrets.size() != rrets.size() ) return false;
|
---|
| 705 | for ( unsigned i = 0; i < lrets.size(); ++i ) {
|
---|
| 706 | if ( !typesPolyCompatible( lrets[i], rrets[i] ) ) return false;
|
---|
| 707 | }
|
---|
| 708 | return true;
|
---|
| 709 | } else if ( type_index(typeid(ast::StructInstType)) == lid ) {
|
---|
| 710 | ast::StructInstType const * l = as<ast::StructInstType>(lhs);
|
---|
| 711 | ast::StructInstType const * r = as<ast::StructInstType>(rhs);
|
---|
| 712 |
|
---|
| 713 | if ( l->name != r->name ) return false;
|
---|
| 714 | return paramListsPolyCompatible( l->params, r->params );
|
---|
| 715 | } else if ( type_index(typeid(ast::UnionInstType)) == lid ) {
|
---|
| 716 | ast::UnionInstType const * l = as<ast::UnionInstType>(lhs);
|
---|
| 717 | ast::UnionInstType const * r = as<ast::UnionInstType>(rhs);
|
---|
| 718 |
|
---|
| 719 | if ( l->name != r->name ) return false;
|
---|
| 720 | return paramListsPolyCompatible( l->params, r->params );
|
---|
| 721 | } else if ( type_index(typeid(ast::EnumInstType)) == lid ) {
|
---|
| 722 | ast::EnumInstType const * l = as<ast::EnumInstType>(lhs);
|
---|
| 723 | ast::EnumInstType const * r = as<ast::EnumInstType>(rhs);
|
---|
| 724 |
|
---|
| 725 | return l->name == r->name;
|
---|
| 726 | } else if ( type_index(typeid(ast::TraitInstType)) == lid ) {
|
---|
| 727 | ast::TraitInstType const * l = as<ast::TraitInstType>(lhs);
|
---|
| 728 | ast::TraitInstType const * r = as<ast::TraitInstType>(rhs);
|
---|
| 729 |
|
---|
| 730 | return l->name == r->name;
|
---|
| 731 | } else if ( type_index(typeid(ast::TupleType)) == lid ) {
|
---|
| 732 | ast::TupleType const * l = as<ast::TupleType>(lhs);
|
---|
| 733 | ast::TupleType const * r = as<ast::TupleType>(rhs);
|
---|
| 734 |
|
---|
| 735 | std::vector<ast::ptr<ast::Type>> ltypes, rtypes;
|
---|
| 736 | flattenList( l->types, ( ltypes ) );
|
---|
| 737 | flattenList( r->types, ( rtypes ) );
|
---|
| 738 | if ( ltypes.size() != rtypes.size() ) return false;
|
---|
| 739 |
|
---|
| 740 | for ( unsigned i = 0 ; i < ltypes.size() ; ++i ) {
|
---|
| 741 | if ( !typesPolyCompatible( ltypes[i], rtypes[i] ) ) return false;
|
---|
| 742 | }
|
---|
| 743 | return true;
|
---|
| 744 | // The remaining types (VoidType, VarArgsType, ZeroType & OneType)
|
---|
| 745 | // have no variation so will always be equal.
|
---|
| 746 | } else {
|
---|
| 747 | return true;
|
---|
| 748 | }
|
---|
| 749 | }
|
---|
| 750 |
|
---|
[02fdb8e] | 751 | bool needsBoxing( Type * param, Type * arg, const TyVarMap &exprTyVars, const TypeSubstitution * env ) {
|
---|
[ae1b9ea] | 752 | // is parameter is not polymorphic, don't need to box
|
---|
| 753 | if ( ! isPolyType( param, exprTyVars ) ) return false;
|
---|
| 754 | Type * newType = arg->clone();
|
---|
| 755 | if ( env ) env->apply( newType );
|
---|
| 756 | std::unique_ptr<Type> manager( newType );
|
---|
| 757 | // if the argument's type is polymorphic, we don't need to box again!
|
---|
| 758 | return ! isPolyType( newType );
|
---|
| 759 | }
|
---|
| 760 |
|
---|
[c8837e5] | 761 | bool needsBoxing( const ast::Type * param, const ast::Type * arg,
|
---|
| 762 | const TypeVarMap & typeVars, const ast::TypeSubstitution * subst ) {
|
---|
| 763 | // Don't need to box if the parameter is not polymorphic.
|
---|
| 764 | if ( !isPolyType( param, typeVars ) ) return false;
|
---|
| 765 |
|
---|
| 766 | ast::ptr<ast::Type> newType = arg;
|
---|
| 767 | if ( subst ) {
|
---|
| 768 | int count = subst->apply( newType );
|
---|
| 769 | (void)count;
|
---|
[490fb92e] | 770 | }
|
---|
[c8837e5] | 771 | // Only need to box if the argument is not also polymorphic.
|
---|
| 772 | return !isPolyType( newType );
|
---|
| 773 | }
|
---|
[490fb92e] | 774 |
|
---|
[02fdb8e] | 775 | bool needsBoxing( Type * param, Type * arg, ApplicationExpr * appExpr, const TypeSubstitution * env ) {
|
---|
[ae1b9ea] | 776 | FunctionType * function = getFunctionType( appExpr->function->result );
|
---|
| 777 | assertf( function, "ApplicationExpr has non-function type: %s", toString( appExpr->function->result ).c_str() );
|
---|
| 778 | TyVarMap exprTyVars( TypeDecl::Data{} );
|
---|
| 779 | makeTyVarMap( function, exprTyVars );
|
---|
| 780 | return needsBoxing( param, arg, exprTyVars, env );
|
---|
| 781 | }
|
---|
| 782 |
|
---|
[c8837e5] | 783 | bool needsBoxing(
|
---|
| 784 | const ast::Type * param, const ast::Type * arg,
|
---|
| 785 | const ast::ApplicationExpr * expr,
|
---|
| 786 | const ast::TypeSubstitution * subst ) {
|
---|
| 787 | const ast::FunctionType * function = getFunctionType( expr->func->result );
|
---|
| 788 | assertf( function, "ApplicationExpr has non-function type: %s", toString( expr->func->result ).c_str() );
|
---|
[93c10de] | 789 | TypeVarMap exprTyVars = { ast::TypeData() };
|
---|
[c8837e5] | 790 | makeTypeVarMap( function, exprTyVars );
|
---|
| 791 | return needsBoxing( param, arg, exprTyVars, subst );
|
---|
| 792 | }
|
---|
[490fb92e] | 793 |
|
---|
[2c57025] | 794 | void addToTyVarMap( TypeDecl * tyVar, TyVarMap &tyVarMap ) {
|
---|
[7ba1324] | 795 | tyVarMap.insert( tyVar->name, TypeDecl::Data{ tyVar } );
|
---|
[2c57025] | 796 | }
|
---|
| 797 |
|
---|
[c8837e5] | 798 | void addToTypeVarMap( const ast::TypeInstType * type, TypeVarMap & typeVars ) {
|
---|
[93c10de] | 799 | typeVars.insert( *type, ast::TypeData( type->base ) );
|
---|
[c8837e5] | 800 | }
|
---|
[490fb92e] | 801 |
|
---|
[aadc9a4] | 802 | void makeTyVarMap( Type *type, TyVarMap &tyVarMap ) {
|
---|
[8c49c0e] | 803 | for ( Type::ForallList::const_iterator tyVar = type->get_forall().begin(); tyVar != type->get_forall().end(); ++tyVar ) {
|
---|
[aadc9a4] | 804 | assert( *tyVar );
|
---|
[2c57025] | 805 | addToTyVarMap( *tyVar, tyVarMap );
|
---|
[aadc9a4] | 806 | }
|
---|
| 807 | if ( PointerType *pointer = dynamic_cast< PointerType* >( type ) ) {
|
---|
| 808 | makeTyVarMap( pointer->get_base(), tyVarMap );
|
---|
| 809 | }
|
---|
| 810 | }
|
---|
[540de412] | 811 |
|
---|
[c8837e5] | 812 | void makeTypeVarMap( const ast::Type * type, TypeVarMap & typeVars ) {
|
---|
| 813 | if ( auto func = dynamic_cast<ast::FunctionType const *>( type ) ) {
|
---|
| 814 | for ( auto & typeVar : func->forall ) {
|
---|
| 815 | assert( typeVar );
|
---|
| 816 | addToTypeVarMap( typeVar, typeVars );
|
---|
[490fb92e] | 817 | }
|
---|
| 818 | }
|
---|
[c8837e5] | 819 | if ( auto pointer = dynamic_cast<ast::PointerType const *>( type ) ) {
|
---|
| 820 | makeTypeVarMap( pointer->base, typeVars );
|
---|
| 821 | }
|
---|
| 822 | }
|
---|
[490fb92e] | 823 |
|
---|
[01aeade] | 824 | void printTyVarMap( std::ostream &os, const TyVarMap &tyVarMap ) {
|
---|
| 825 | for ( TyVarMap::const_iterator i = tyVarMap.begin(); i != tyVarMap.end(); ++i ) {
|
---|
| 826 | os << i->first << " (" << i->second << ") ";
|
---|
| 827 | } // for
|
---|
| 828 | os << std::endl;
|
---|
| 829 | }
|
---|
[ffad73a] | 830 |
|
---|
[51b73452] | 831 | } // namespace GenPoly
|
---|
[01aeade] | 832 |
|
---|
[51587aa] | 833 | // Local Variables: //
|
---|
| 834 | // tab-width: 4 //
|
---|
| 835 | // mode: c++ //
|
---|
| 836 | // compile-command: "make install" //
|
---|
| 837 | // End: //
|
---|