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
|
/*
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 "overloadresolutionhelper.h"
#include <kdebug.h>
#include <language/duchain/declaration.h>
#include <language/duchain/ducontext.h>
#include <language/duchain/types/identifiedtype.h>
#include <cpptypes.h>
#include "typeutils.h"
#include "viablefunctions.h"
#include "cppduchain.h"
#include "adlhelper.h"
using namespace KDevelop;
using namespace Cpp;
///@todo Decide whether this should be enabled or disabled (by performance considerations)
bool useADLForOperators = true;
OverloadResolutionFunction::OverloadResolutionFunction() : matchedArguments(0) {
}
OverloadResolutionFunction::OverloadResolutionFunction( int _matchedArguments, const ViableFunction& _viable ) : matchedArguments(_matchedArguments), function(_viable) {
}
OverloadResolutionHelper::OverloadResolutionHelper(const KDevelop::DUContextPointer& context, const KDevelop::TopDUContextPointer& topContext)
: m_context(context)
, m_topContext(topContext)
, m_isOperator(false)
, m_constness(OverloadResolver::Unknown)
{
}
void OverloadResolutionHelper::setFunctionNameForADL(const KDevelop::QualifiedIdentifier& identifierForADL)
{
m_identifierForADL = identifierForADL;
}
void OverloadResolutionHelper::setOperator( const OverloadResolver::Parameter& base )
{
m_baseType = base;
m_isOperator = true;
}
void OverloadResolutionHelper::setFunctions( const QList<Declaration*>& functions )
{
foreach( Declaration* decl, functions )
m_declarations << DeclarationWithArgument( OverloadResolver::ParameterList(), decl ); //Insert with argument-offset zero
}
void OverloadResolutionHelper::setKnownParameters( const OverloadResolver::ParameterList& parameters )
{
m_knownParameters = parameters;
}
void OverloadResolutionHelper::setConstness( OverloadResolver::Constness constness )
{
m_constness = constness;
}
void OverloadResolutionHelper::initializeResolver(OverloadResolver& resolv)
{
if( m_isOperator ) {
Q_ASSERT(!m_identifierForADL.isEmpty());
///Search for member operators
AbstractType::Ptr real( TypeUtils::realType(m_baseType.type, m_context->topContext()) );
if( dynamic_cast<CppClassType*>( real.unsafeData() ) )
{
IdentifiedType* idType = dynamic_cast<IdentifiedType*>( real.unsafeData() );
if( idType && idType->declaration(m_context->topContext()) ) {
DUContext* ctx = idType->declaration(m_context->topContext())->logicalInternalContext(m_context->topContext());
if( ctx ) {
QList<Declaration*> decls = Cpp::findLocalDeclarations( ctx, m_identifierForADL.first(), m_context->topContext() );
foreach( Declaration* decl, decls )
m_declarations << DeclarationWithArgument( OverloadResolver::ParameterList(), decl );
} else {
log( "no internal context found" );
}
} else {
log( "type is not identified" );
}
}
///Search for static global operators
QList<Declaration*> decls = m_context->findDeclarations(m_identifierForADL);
foreach( Declaration* decl, decls ) {
FunctionType::Ptr fun = decl->abstractType().cast<FunctionType>();
if( fun && (fun->arguments().size() == 1 || fun->arguments().size() == 2) )
m_declarations << DeclarationWithArgument( m_baseType, decl );
}
///If no global or class member operators found try ADL for namespaced operators
if (m_declarations.isEmpty() && useADLForOperators) {
OverloadResolver::ParameterList params;
params.parameters << m_baseType;
params.parameters += m_knownParameters.parameters;
foreach( Declaration* decl, resolv.computeADLCandidates( params, m_identifierForADL ) ) {
FunctionType::Ptr fun = decl->abstractType().cast<FunctionType>();
if( fun && (fun->arguments().size() == 1 || fun->arguments().size() == 2) )
m_declarations << DeclarationWithArgument( m_baseType, decl );
}
}
}else{
//m_declarations should already be set by setFunctions(..)
}
foreach( const DeclarationWithArgument& decl, m_declarations )
m_argumentCountMap[decl.second] = decl.first.parameters.size();
// log( "functions given to overload-resolution:" );
// foreach( const DeclarationWithArgument& declaration, m_declarations )
// log( declaration.second->toString() );
// log("parameters given to overload-resolution:");
// lock.unlock();
// foreach( const ExpressionEvaluationResult& result, m_knownArgumentTypes ) {
// log( result.toString() );
// }
// lock.lock();
}
QList<OverloadResolutionFunction> OverloadResolutionHelper::resolveToList(bool partial)
{
OverloadResolver resolv( m_context, m_topContext, m_constness );
QList< ViableFunction > viableFunctions;
viableFunctions = resolv.resolveListOffsetted( m_knownParameters, m_declarations, partial );
// also retrieve names by ADL if partial argument list (only used by code completion)
// and even in strict mode if normal lookup failed
if (partial || viableFunctions.empty() || !viableFunctions[0].isViable()) {
QList<Declaration*> adlDecls = resolv.computeADLCandidates( m_knownParameters, m_identifierForADL );
if (!adlDecls.empty()) {
QList< DeclarationWithArgument > adlDeclsWithArguments;
foreach(Declaration * decl, adlDecls) {
adlDeclsWithArguments << DeclarationWithArgument( OverloadResolver::ParameterList(), decl ); // see setFunctions
}
viableFunctions += resolv.resolveListOffsetted( m_knownParameters, adlDeclsWithArguments, partial );
}
}
qSort( viableFunctions );
QList<OverloadResolutionFunction> ret;
foreach( const ViableFunction& function, viableFunctions )
{
if( function.declaration() && function.declaration()->abstractType() )
ret << OverloadResolutionFunction( m_argumentCountMap[function.declaration().data()] + m_knownParameters.parameters.size(), function );
}
return ret;
}
ViableFunction OverloadResolutionHelper::resolve(bool forceInstance)
{
OverloadResolver resolv( m_context, m_topContext, m_constness, forceInstance );
initializeResolver(resolv);
ViableFunction ret = resolv.resolveListViable( m_knownParameters, m_declarations );
// also retrieve names by ADL if partial argument list (only used by code completion)
// and even in strict mode if normal lookup failed
if (!ret.isViable()) {
QList<Declaration*> adlDecls = resolv.computeADLCandidates( m_knownParameters, m_identifierForADL );
if (!adlDecls.empty()) {
QList< DeclarationWithArgument > adlDeclsWithArguments;
foreach(Declaration * decl, adlDecls)
adlDeclsWithArguments << DeclarationWithArgument( OverloadResolver::ParameterList(), decl ); // see setFunctions
ret = resolv.resolveListViable( m_knownParameters, adlDeclsWithArguments );
}
}
return ret;
}
void OverloadResolutionHelper::log(const QString& str) const
{
kDebug(9007) << "OverloadResolutionHelper: " << str;
}
|