1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
|
/*
Copyright 2007 David Nolden <david.nolden.kdevelop@art-master.de>
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License version 2 as published by the Free Software Foundation.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "overloadresolution.h"
#include "cppduchain/typeutils.h"
#include <language/duchain/ducontext.h>
#include <language/duchain/declaration.h>
#include <language/duchain/indexedstring.h>
#include <language/duchain/classfunctiondeclaration.h>
#include "cppduchain/cpptypes.h"
#include "viablefunctions.h"
#include "templatedeclaration.h"
#include "templateparameterdeclaration.h"
#include "typeutils.h"
#include <QtAlgorithms>
#include "adlhelper.h"
#include "typeconversion.h"
#include <language/duchain/persistentsymboltable.h>
using namespace Cpp;
using namespace KDevelop;
const bool allowADL = true;
// uncomment to get debugging info on ADL - very expensive on parsing
// #define DEBUG_ADL
#define ifDebugOverloadResolution(x)
// #define ifDebugOverloadResolution(x) x
OverloadResolver::OverloadResolver( DUContextPointer context, TopDUContextPointer topContext, Constness constness, bool forceIsInstance )
: m_context( context )
, m_topContext( topContext )
, m_worstConversionRank( NoMatch )
, m_forceIsInstance( forceIsInstance )
, m_constness(constness)
{
}
Declaration* OverloadResolver::resolveConstructor( const ParameterList& params, bool implicit, bool noUserDefinedConversion )
{
if ( !m_context || !m_topContext )
return 0;
QList<Declaration*> goodDeclarations;
Identifier id = m_context->localScopeIdentifier().last();
id.clearTemplateIdentifiers();
QList<Declaration*> declarations = m_context->findLocalDeclarations( id, CursorInRevision::invalid(), m_topContext.data(), AbstractType::Ptr(), DUContext::OnlyFunctions );
for ( QList<Declaration*>::iterator it = declarations.begin(); it != declarations.end(); ++it )
{
if (( *it )->indexedType() )
{
FunctionType::Ptr function = ( *it )->abstractType().cast<FunctionType>();
ClassFunctionDeclaration* functionDeclaration = dynamic_cast<ClassFunctionDeclaration*>( *it );
//Q_ASSERT();
if ( functionDeclaration /*&& functionDeclaration->isConstructor()*/ ) //Test not needed, because name == classname
{
if ( function->indexedArgumentsSize() >= static_cast<uint>(params.parameters.size()) )
{
if ( !implicit || !functionDeclaration->isExplicit() )
goodDeclarations << *it;
}
}
}
}
// no ADL possible when resolving constructors
return resolveList( params, goodDeclarations, noUserDefinedConversion );
}
Declaration* OverloadResolver::resolve( const ParameterList& params, const QualifiedIdentifier& functionName, bool noUserDefinedConversion )
{
if ( !m_context || !m_topContext )
return 0;
QList<Declaration*> declarations = m_context->findDeclarations( functionName, CursorInRevision::invalid(), AbstractType::Ptr(), m_topContext.data() );
// without ADL findDeclarations may fail so skip ADL there and do it here
Declaration * resolvedDecl = resolveList( params, declarations, noUserDefinedConversion );
if (!resolvedDecl && functionName.count() == 1) {
// start ADL lookup
QList<Declaration*> adlDecls = computeADLCandidates( params, functionName );
resolvedDecl = resolveList( params, adlDecls, noUserDefinedConversion );
#ifdef DEBUG_ADL
if (resolvedDecl)
kDebug() << "ADL found " << resolvedDecl->toString();
else
kDebug() << "ADL failed";
#endif
}
return resolvedDecl;
}
uint OverloadResolver::worstConversionRank()
{
return m_worstConversionRank;
}
void OverloadResolver::expandDeclarations( const QList<Declaration*>& declarations, QSet<Declaration*>& newDeclarations )
{
for ( QList<Declaration*>::const_iterator it = declarations.constBegin(); it != declarations.constEnd(); ++it )
{
Declaration* decl = *it;
if ( CppClassType::Ptr klass = TypeUtils::realType( decl->abstractType(), m_topContext.data() ).cast<CppClassType>() )
{
if ( decl->kind() == Declaration::Instance || m_forceIsInstance )
{
//Instances of classes should be substituted with their operator() members
QList<Declaration*> decls;
TypeUtils::getMemberFunctions( klass, m_topContext.data(), decls, "operator()", klass->modifiers() & AbstractType::ConstModifier );
foreach( Declaration* decl, decls )
newDeclarations.insert( decl );
}
else
{
//Classes should be substituted with their constructors
foreach( Declaration* decl, TypeUtils::getConstructors( klass, m_topContext.data() ) ) {
newDeclarations.insert( decl );
}
}
}
else
{
newDeclarations.insert( *it );
}
}
}
void OverloadResolver::expandDeclarations( const QList<QPair<OverloadResolver::ParameterList, Declaration*> >& declarations, QHash<Declaration*, OverloadResolver::ParameterList>& newDeclarations )
{
for ( QList<QPair<OverloadResolver::ParameterList, Declaration*> >::const_iterator it = declarations.constBegin(); it != declarations.constEnd(); ++it )
{
QPair<OverloadResolver::ParameterList, Declaration*> decl = *it;
if ( CppClassType::Ptr klass = TypeUtils::realType( decl.second->abstractType(), m_topContext.data() ).cast<CppClassType>() )
{
if ( decl.second->kind() == Declaration::Instance || m_forceIsInstance )
{
//Instances of classes should be substituted with their operator() members
QList<Declaration*> functions;
TypeUtils::getMemberFunctions( klass, m_topContext.data(), functions, "operator()", klass->modifiers() & AbstractType::ConstModifier );
foreach( Declaration* f, functions )
newDeclarations.insert( f, decl.first );
}
else
{
//Classes should be substituted with their constructors
foreach( Declaration* f, TypeUtils::getConstructors( klass, m_topContext.data() ) ) {
newDeclarations.insert( f, decl.first );
}
}
}
else
{
newDeclarations.insert( it->second, it->first );
}
}
}
Declaration* OverloadResolver::resolveList( const ParameterList& params, const QList<Declaration*>& declarations, bool noUserDefinedConversion )
{
if ( !m_context || !m_topContext )
return 0;
///Iso c++ draft 13.3.3
m_worstConversionRank = ExactMatch;
///First step: Replace class-instances with operator() functions, and pure classes with their constructors
QSet<Declaration*> newDeclarations;
expandDeclarations( declarations, newDeclarations );
///Second step: Find best viable function
ViableFunction bestViableFunction( m_topContext.data() );
for ( QSet<Declaration*>::const_iterator it = newDeclarations.constBegin(); it != newDeclarations.constEnd(); ++it )
{
Declaration* decl = applyImplicitTemplateParameters( params, *it );
ifDebugOverloadResolution(qDebug() << (*it)->toString() << decl; )
if ( !decl )
continue;
ViableFunction viable( m_topContext.data(), decl, m_constness, noUserDefinedConversion );
viable.matchParameters( params );
ifDebugOverloadResolution(qDebug() << decl->toString() << viable.isBetter(bestViableFunction); )
if ( viable.isBetter( bestViableFunction ) )
{
bestViableFunction = viable;
m_worstConversionRank = bestViableFunction.worstConversion();
}
}
if ( bestViableFunction.isViable() )
return bestViableFunction.declaration().data();
else {
return 0;
}
}
QList< ViableFunction > OverloadResolver::resolveListOffsetted( const ParameterList& params, const QList<QPair<OverloadResolver::ParameterList, Declaration*> >& declarations, bool partial )
{
if ( !m_context || !m_topContext )
return QList<ViableFunction>();
///Iso c++ draft 13.3.3
m_worstConversionRank = ExactMatch;
///First step: Replace class-instances with operator() functions, and pure classes with their constructors
QHash<Declaration*, OverloadResolver::ParameterList> newDeclarations;
expandDeclarations( declarations, newDeclarations );
///Second step: Find best viable function
QList<ViableFunction> viableFunctions;
for ( QHash<Declaration*, OverloadResolver::ParameterList>::const_iterator it = newDeclarations.constBegin(); it != newDeclarations.constEnd(); ++it )
{
ParameterList mergedParams = it.value();
mergedParams.parameters += params.parameters;
Declaration* decl = applyImplicitTemplateParameters( mergedParams, it.key() );
ifDebugOverloadResolution(qDebug() << it.key()->toString() << decl; )
if ( !decl )
continue;
if (decl->isExplicitlyDeleted())
continue;
ViableFunction viable( m_topContext.data(), decl, m_constness );
viable.matchParameters( mergedParams, partial );
viableFunctions << viable;
}
qSort( viableFunctions );
return viableFunctions;
}
ViableFunction OverloadResolver::resolveListViable( const ParameterList& params, const QList<QPair<OverloadResolver::ParameterList, Declaration*> >& declarations, bool partial )
{
if ( !m_context || !m_topContext )
return ViableFunction();
ifDebugOverloadResolution(qDebug() << "resolveListViable" << params; )
///Iso c++ draft 13.3.3
m_worstConversionRank = ExactMatch;
///First step: Replace class-instances with operator() functions, and pure classes with their constructors
QHash<Declaration*, OverloadResolver::ParameterList> newDeclarations;
expandDeclarations( declarations, newDeclarations );
///Second step: Find best viable function
ViableFunction bestViableFunction(m_topContext.data());
for ( QHash<Declaration*, OverloadResolver::ParameterList>::const_iterator it = newDeclarations.constBegin(); it != newDeclarations.constEnd(); ++it )
{
ParameterList mergedParams = it.value();
mergedParams.parameters += params.parameters;
Declaration* decl = applyImplicitTemplateParameters( mergedParams, it.key() );
ifDebugOverloadResolution(qDebug() << it.key()->toString() << decl; )
if ( !decl )
continue;
ViableFunction viable( m_topContext.data(), decl, m_constness );
viable.matchParameters( mergedParams, partial );
ifDebugOverloadResolution(qDebug() << decl->toString() << viable.isBetter(bestViableFunction); )
if ( viable.isBetter( bestViableFunction ) )
{
bestViableFunction = viable;
m_worstConversionRank = bestViableFunction.worstConversion();
}
}
return bestViableFunction;
}
Declaration* OverloadResolver::applyImplicitTemplateParameters( const ParameterList& params, Declaration* declaration ) const
{
TemplateDeclaration* tempDecl = dynamic_cast<TemplateDeclaration*>( declaration );
if ( !tempDecl )
return declaration;
KDevelop::DUContext* templateContext = tempDecl->templateParameterContext();
if ( !templateContext )
{
//May be just within a template, but without own template parameters
return declaration;
}
FunctionType::Ptr functionType = declaration->type<FunctionType>();
if ( !functionType )
{
kDebug( 9007 ) << "Template function has no function type";
return declaration;
}
const IndexedType* arguments( functionType->indexedArguments() );
if ( static_cast<uint>(params.parameters.count()) > functionType->indexedArgumentsSize() )
return declaration;
//templateContext contains the template-parameters that we need to find instantiations for
QMap<IndexedString, AbstractType::Ptr> instantiatedParameters; //Here we store the values assigned to each template-parameter
foreach( Declaration* decl, templateContext->localDeclarations() )
{
CppTemplateParameterType::Ptr paramType = decl->abstractType().cast<CppTemplateParameterType>();
if ( paramType ) //Parameters that are not of type CppTemplateParameterType are already assigned.
{
instantiatedParameters[decl->identifier().identifier()] = AbstractType::Ptr();
}
}
if ( instantiatedParameters.isEmpty() )
return declaration; //All parameters already have a type assigned
for ( int a = 0; a < params.parameters.count(); a++ )
matchParameterTypes( params.parameters[a].type, arguments[a].abstractType(), instantiatedParameters );
bool allInstantiated = true;
for ( QMap<IndexedString, AbstractType::Ptr>::const_iterator it = instantiatedParameters.constBegin(); it != instantiatedParameters.constEnd(); ++it )
if ( !( *it ) )
{
allInstantiated = false;
break;
}
if ( allInstantiated )
{
//We have new template-parameters at hand, we can specialize now.
Cpp::InstantiationInformation instantiateWith( tempDecl->instantiatedWith().information() );
instantiateWith.templateParametersList().clear();
foreach( Declaration* decl, templateContext->localDeclarations() )
{
AbstractType::Ptr type;
CppTemplateParameterType::Ptr paramType = decl->abstractType().cast<CppTemplateParameterType>();
if ( paramType ) //Take the type we have assigned.
type = instantiatedParameters[decl->identifier().identifier()];
else
type = decl->abstractType(); //Take the type that was available already earlier
instantiateWith.addTemplateParameter( type );
}
return tempDecl->instantiate( instantiateWith, m_topContext.data() );
}
return declaration;
}
inline uint incrementIfSuccessful( uint val )
{
if ( val )
return 1 + val;
return 0;
}
uint OverloadResolver::matchParameterTypes( const AbstractType::Ptr& argumentType, const AbstractType::Ptr& parameterType, QMap<IndexedString, AbstractType::Ptr>& instantiatedTypes, bool keepValue ) const
{
if ( !argumentType && !parameterType )
return 1;
if ( !argumentType || !parameterType )
return 0;
ifDebugOverloadResolution( kDebug() << "matching" << argumentType->toString() << "to" << parameterType->toString(); )
if ( instantiatedTypes.isEmpty() )
return 1;
DelayedType::Ptr delayed = parameterType.cast<DelayedType>();
if ( delayed )
return incrementIfSuccessful( matchParameterTypes( argumentType, delayed->identifier(), instantiatedTypes , keepValue ) );
///In case of references on both sides, match the target-types
ReferenceType::Ptr argumentRef = argumentType.cast<ReferenceType>();
ReferenceType::Ptr parameterRef = parameterType.cast<ReferenceType>();
if ( argumentRef && parameterRef )
return incrementIfSuccessful( matchParameterTypes( argumentRef->baseType(), parameterRef->baseType(), instantiatedTypes, keepValue ) );
else if ( argumentRef )
return incrementIfSuccessful( matchParameterTypes( argumentRef->baseType(), parameterType, instantiatedTypes, keepValue ) );
else if ( parameterRef )
return incrementIfSuccessful( matchParameterTypes( argumentType, parameterRef->baseType(), instantiatedTypes, keepValue ) );
///In case of pointers on both sides, match the target-types
PointerType::Ptr argumentPointer = argumentType.cast<PointerType>();
PointerType::Ptr parameterPointer = parameterType.cast<PointerType>();
if ( argumentPointer && parameterPointer && (( argumentPointer->modifiers() & AbstractType::ConstModifier ) == ( parameterPointer->modifiers() & AbstractType::ConstModifier ) ) )
return incrementIfSuccessful( matchParameterTypes( argumentPointer->baseType(), parameterPointer->baseType(), instantiatedTypes, keepValue ) );
///In case of arrays on both sides, match the target-types
ArrayType::Ptr argumentArray = argumentType.cast<ArrayType>();
ArrayType::Ptr parameterArray = parameterType.cast<ArrayType>();
if ( argumentArray && parameterArray && (( argumentArray->modifiers() & AbstractType::ConstModifier ) == ( parameterArray->modifiers() & AbstractType::ConstModifier ) ) ) {
return incrementIfSuccessful( matchParameterTypes( argumentArray->elementType(), parameterArray->elementType(), instantiatedTypes, keepValue ) );
}
if ( CppTemplateParameterType::Ptr templateParam = parameterType.cast<CppTemplateParameterType>() )
{
///@todo Allow template-parameters with even more template-parameters declared
//Directly assign argumentType to the template parameter
Declaration* decl = templateParam->declaration( m_topContext.data() );
if ( decl )
{
IndexedString id = decl->identifier().identifier();
if ( instantiatedTypes[id].isNull() )
{
instantiatedTypes[id] = argumentType;
return 1;
}
else if ( instantiatedTypes[id]->equals( argumentType.unsafeData() ) )
{
return 1;
}
else
{
//Mismatch, another type was already assigned
return 0;
}
}
}
///Match assigned template-parameters, for example when matching QList<int> to QList<T>, assign int to T.
const IdentifiedType* identifiedArgument = dynamic_cast<const IdentifiedType*>( argumentType.unsafeData() );
const IdentifiedType* identifiedParameter = dynamic_cast<const IdentifiedType*>( parameterType.unsafeData() );
if ( identifiedArgument && identifiedParameter )
{
Declaration* argumentDeclaration = identifiedArgument->declaration( m_topContext.data() );
Declaration* parameterDeclaration = identifiedParameter->declaration( m_topContext.data() );
TemplateDeclaration* argumentTemplateDeclaration = dynamic_cast<TemplateDeclaration*>( argumentDeclaration );
TemplateDeclaration* parameterTemplateDeclaration = dynamic_cast<TemplateDeclaration*>( parameterDeclaration );
if ( !argumentTemplateDeclaration || !parameterTemplateDeclaration )
{
return (int)(argumentDeclaration == parameterDeclaration);
}
if ( argumentTemplateDeclaration->instantiatedFrom() == parameterTemplateDeclaration->instantiatedFrom() && argumentTemplateDeclaration->instantiatedFrom() )
{
InstantiationInformation argumentInstantiatedWith = argumentTemplateDeclaration->instantiatedWith().information();
InstantiationInformation parameterInstantiatedWith = parameterTemplateDeclaration->instantiatedWith().information();
if ( argumentInstantiatedWith.templateParametersSize() != parameterInstantiatedWith.templateParametersSize() )
return 0;
uint matchDepth = 1;
for ( uint a = 0; a < argumentInstantiatedWith.templateParametersSize(); ++a )
{
uint localMatchDepth = matchParameterTypes( argumentInstantiatedWith.templateParameters()[a].abstractType(), parameterInstantiatedWith.templateParameters()[a].abstractType(), instantiatedTypes, keepValue );
if ( !localMatchDepth )
return 0;
matchDepth += localMatchDepth;
}
return matchDepth;
}
}
return 1;
}
QList<Declaration *> OverloadResolver::computeADLCandidates( const ParameterList& params, const QualifiedIdentifier& identifier )
{
if(!allowADL || identifier.count() != 1 )
return QList<Declaration *>();
// Don't try to do ADL if there are delayed/unresolved types involved,
// because then we cannot get a proper match as to ViableFunction anyway
foreach( const Parameter & param, params.parameters ) {
if( fastCast<DelayedType*>(param.type.unsafeData()) ) {
ifDebugOverloadResolution(qDebug() << "Skipping ADL due to delayed types" << identifier << params; )
return QList<Declaration *>();
}
}
ADLHelper adlHelper( m_context, m_topContext );
foreach( const Parameter & param, params.parameters )
adlHelper.addArgument( param );
QSet<QualifiedIdentifier> adlNamespaces = adlHelper.associatedNamespaces();
#ifdef DEBUG_ADL
foreach( QualifiedIdentifier ns, adlNamespaces )
{
kDebug() << " ADL found namespace: " << ns.toString();
}
#endif
QList<Declaration*> adlDecls;
#ifdef DEBUG_ADL
kDebug() << " ADL candidates for: " << identifier << params << params.parameters.size();
#endif
foreach( QualifiedIdentifier adlFunctionName, adlNamespaces )
{
adlFunctionName += identifier;
// By using DeclarationId, we prevent a lot of complex logic which we don't require, as we
// already have the fully qualified scope.
PersistentSymbolTable::FilteredDeclarationIterator decls =
PersistentSymbolTable::self().getFilteredDeclarations(IndexedQualifiedIdentifier(adlFunctionName), m_topContext->recursiveImportIndices());
for(; decls; ++decls)
{
Declaration* decl = decls->data();
if(decl && decl->isFunctionDeclaration()) {
adlDecls << decl;
#ifdef DEBUG_ADL
kDebug() << " ADL candidate: " << adlFunctionName << decl->toString();
#endif
}
}
}
return adlDecls;
}
AbstractType::Ptr getContainerType( AbstractType::Ptr type, int depth, TopDUContext* topContext )
{
for ( int a = 0; a < depth; a++ )
{
AbstractType::Ptr real = TypeUtils::realType( type, topContext );
IdentifiedType* idType = dynamic_cast<IdentifiedType*>( real.unsafeData() );
if ( !idType )
return AbstractType::Ptr();
Declaration* decl = idType->declaration(topContext);
if(!decl || !decl->context())
return AbstractType::Ptr();
Declaration* containerDecl = decl->context()->owner();
if ( containerDecl )
type = containerDecl->abstractType();
else
return AbstractType::Ptr();
}
return type;
}
uint OverloadResolver::matchParameterTypes( AbstractType::Ptr argumentType, const IndexedTypeIdentifier& parameterType, QMap<IndexedString, AbstractType::Ptr>& instantiatedTypes, bool keepValue ) const
{
ifDebugOverloadResolution( kDebug() << "1 matching" << argumentType->toString() << "to" << parameterType.toString() << parameterType.pointerDepth(); )
if ( !argumentType )
return 1;
if ( instantiatedTypes.isEmpty() )
return 1;
QualifiedIdentifier parameterQid( parameterType.identifier().identifier() );
if ( parameterQid.isEmpty() )
return 1;
{
ReferenceType::Ptr argumentRef = argumentType.cast<ReferenceType>();
if ( argumentRef && parameterType.isReference() )
argumentType = argumentRef->baseType();
else if ( parameterType.isReference() )
return 0; //Reference on right side, but not on left
}
{
PointerType::Ptr argumentPointer = argumentType.cast<PointerType>();
int cnt = 0; ///@todo correct ordering of the pointers and their constnesses
while ( argumentPointer && cnt < parameterType.pointerDepth() )
{
++cnt;
argumentType = argumentPointer->baseType();
argumentPointer = argumentType.cast<PointerType>();
}
if ( cnt != parameterType.pointerDepth() || !argumentType )
{
return 0; //Do not have the needed count of pointers
}
}
uint matchDepth = 1;
if (( argumentType->modifiers() & AbstractType::ConstModifier ) && parameterType.isConstant() )
++matchDepth;
for ( int a = 0; a < parameterQid.count(); ++a )
{
///@todo Think about this
AbstractType::Ptr pType = getContainerType( argumentType, parameterQid.count() - a - 1, m_topContext.data() );
uint localDepth = matchParameterTypes( pType, parameterQid.at( a ), instantiatedTypes, keepValue );
// if(!localDepth)
// return 0;
matchDepth += localDepth;
}
return matchDepth;
}
uint OverloadResolver::matchParameterTypes( AbstractType::Ptr argumentType, const Identifier& parameterType, QMap<IndexedString, AbstractType::Ptr>& instantiatedTypes, bool keepValue ) const
{
ifDebugOverloadResolution( kDebug() << "2 matching" << argumentType->toString() << "to" << parameterType.toString(); )
if ( !argumentType )
return 1;
if ( instantiatedTypes.isEmpty() )
return 1;
if ( instantiatedTypes.contains( parameterType.identifier() ) )
{
if ( !keepValue ) //Extract the actual type without the value
if ( ConstantIntegralType::Ptr integral = argumentType.cast<ConstantIntegralType>() )
argumentType = AbstractType::Ptr( new IntegralType( *integral ) );
instantiatedTypes[parameterType.identifier()] = argumentType;
return 1;
}
IdentifiedType* identifiedArgument = dynamic_cast<IdentifiedType*>( argumentType.unsafeData() );
if ( !identifiedArgument )
return 0;
if ( identifiedArgument->qualifiedIdentifier().last().identifier() != parameterType.identifier() )
{
return 0;
}
TemplateDeclaration* argumentTemplateDeclaration = dynamic_cast<TemplateDeclaration*>( identifiedArgument->declaration( m_topContext.data() ) );
uint matchDepth = 1;
if ( argumentTemplateDeclaration && parameterType.templateIdentifiersCount() )
{
DUContext* argumentTemplateDeclarationContext = argumentTemplateDeclaration->templateParameterContext();
if ( argumentTemplateDeclarationContext )
{
int matchLength = argumentTemplateDeclarationContext->localDeclarations().count();
if ( parameterType.templateIdentifiersCount() < static_cast<uint>(matchLength) )
matchLength = parameterType.templateIdentifiersCount();
for ( int a = 0; a < matchLength; ++a )
matchDepth += matchParameterTypes( argumentTemplateDeclarationContext->localDeclarations()[a]->abstractType(), parameterType.templateIdentifier( a ), instantiatedTypes, keepValue );
}
else
{
kDebug( 9007 ) << "Template-declaration missing template-parameter context";
}
}
return matchDepth;
}
///Version of matchParameters that would be correct if we used CppTemplateParameterType* instead of DelayedType. Maybe we should doing that some time, it's more correct, and more complicated.
// bool OverloadResolver::matchParameterTypes(AbstractType* _argumentType, AbstractType* _parameterType, QMap<CppTemplateParameterType*, AbstractType::Ptr>& instantiatedTypes) const
// {
// if(!_argumentType || !_parameterType)
// return true;
// if(instantiatedTypes.isEmpty())
// return true;
//
// AbstractType::Ptr argumentType = TypeUtils::resolvedType(_argumentType, m_topContext.data());
// AbstractType::Ptr parameterType = TypeUtils::resolvedType(_parameterType, m_topContext.data());
//
// CppTemplateParameterType* temp = dynamic_cast<CppTemplateParameterType*>(parameterType.data());
// if(temp) {
// if( instantiatedTypes.contains(temp) ) {
// if( instantiatedTypes[temp].data() == 0 ) {
// instantiatedTypes[temp] = argumentType;
// }else{
// ///@todo compare if the type already instantiated for this parameter is the same as argumentType, and else fail.
// }
// }
// return true;
// }
//
// ///Match assigned template-parameters, for example when matching QList<int> to QList<T>, assign int to T.
// IdentifiedType* identifiedArgument = dynamic_cast<IdentifiedType*>(argumentType.data());
// IdentifiedType* identifiedParameter = dynamic_cast<IdentifiedType*>(parameterType.data());
//
// if( identifiedArgument && identifiedParameter )
// {
// if( identifiedArgument->identifier() == identifiedParameter->identifier() )
// {
// TemplateDeclaration* argumentTemplateDeclaration = dynamic_cast<TemplateDeclaration*>(identifiedArgument->declaration());
// TemplateDeclaration* parameterTemplateDeclaration = dynamic_cast<TemplateDeclaration*>(identifiedParameter->declaration());
//
// if( argumentTemplateDeclaration && parameterTemplateDeclaration )
// {
// DUContext* argumentTemplateDeclarationContext = argumentTemplateDeclaration->templateParameterContext();
// DUContext* parameterTemplateDeclarationContext = parameterTemplateDeclaration->templateParameterContext();
//
// if( argumentTemplateDeclarationContext->localDeclarations().count() == parameterTemplateDeclarationContext->localDeclarations().count() ) {
// for( int a = 0; a < argumentTemplateDeclarationContext->localDeclarations().count(); ++a )
// matchParameterTypes( argumentTemplateDeclarationContext->localDeclarations()[a]->abstractType().data(), parameterTemplateDeclarationContext->localDeclarations()[a]->abstractType().data(), instantiatedTypes );
// }
// }
// }
// }
//
// ///In case of references on both sides, match the target-types
// ReferenceType* argumentRef = dynamic_cast<ReferenceType*>(argumentType.data());
// ReferenceType* parameterRef = dynamic_cast<ReferenceType*>(parameterType.data());
//
// if( argumentRef && parameterRef )
// return matchParameterTypes( argumentRef->baseType().data(), parameterRef->baseType().data(), instantiatedTypes );
//
// ///In case of pointers on both sides, match the target-types
// PointerType* argumentPointer = dynamic_cast<PointerType*>(argumentType.data());
// PointerType* parameterPointer = dynamic_cast<PointerType*>(parameterType.data());
//
// if( argumentPointer && parameterPointer )
// return matchParameterTypes( argumentPointer->baseType().data(), parameterPointer->baseType().data(), instantiatedTypes );
//
// return true;
// }
QDebug operator<<(QDebug s, const Cpp::OverloadResolver::Parameter& param)
{
return s << param.toString();
}
QDebug operator<<(QDebug s, const Cpp::OverloadResolver::ParameterList& params)
{
return s << params.parameters;
}
|