Commit 653ca110 authored by Roberto Raggi's avatar Roberto Raggi
Browse files

Removed DeprecatedLookupContext.

parent 54e09a18
......@@ -34,8 +34,6 @@
#include <NameVisitor.h>
#include <FullySpecifiedType.h>
#include "DeprecatedLookupContext.h"
#include <QtCore/QList>
#include <QtCore/QPair>
......
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2010 Nokia Corporation and/or its subsidiary(-ies).
**
** Contact: Nokia Corporation (qt-info@nokia.com)
**
** Commercial Usage
**
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
** GNU Lesser General Public License Usage
**
** Alternatively, this file may be used under the terms of the GNU Lesser
** General Public License version 2.1 as published by the Free Software
** Foundation and appearing in the file LICENSE.LGPL included in the
** packaging of this file. Please review the following information to
** ensure the GNU Lesser General Public License version 2.1 requirements
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at http://qt.nokia.com/contact.
**
**************************************************************************/
#include "DeprecatedLookupContext.h"
#include "ResolveExpression.h"
#include "Overview.h"
#include <CoreTypes.h>
#include <Symbols.h>
#include <Literals.h>
#include <Names.h>
#include <Scope.h>
#include <Control.h>
#include <QtDebug>
using namespace CPlusPlus;
/////////////////////////////////////////////////////////////////////
// LookupContext
/////////////////////////////////////////////////////////////////////
DeprecatedLookupContext::DeprecatedLookupContext(Control *control)
: _control(control),
_symbol(0)
{ }
DeprecatedLookupContext::DeprecatedLookupContext(Symbol *symbol,
Document::Ptr expressionDocument,
Document::Ptr thisDocument,
const Snapshot &snapshot)
: _symbol(symbol),
_expressionDocument(expressionDocument),
_thisDocument(thisDocument),
_snapshot(snapshot)
{
_control = _expressionDocument->control();
_visibleScopes = buildVisibleScopes();
}
bool DeprecatedLookupContext::isValid() const
{ return _control != 0; }
Control *DeprecatedLookupContext::control() const
{ return _control; }
Symbol *DeprecatedLookupContext::symbol() const
{ return _symbol; }
Document::Ptr DeprecatedLookupContext::expressionDocument() const
{ return _expressionDocument; }
Document::Ptr DeprecatedLookupContext::thisDocument() const
{ return _thisDocument; }
Document::Ptr DeprecatedLookupContext::document(const QString &fileName) const
{ return _snapshot.document(fileName); }
Snapshot DeprecatedLookupContext::snapshot() const
{ return _snapshot; }
bool DeprecatedLookupContext::maybeValidSymbol(Symbol *symbol,
ResolveMode mode,
const QList<Symbol *> &candidates)
{
if (((mode & ResolveNamespace) && symbol->isNamespace()) ||
((mode & ResolveClass) && symbol->isClass()) ||
((mode & ResolveObjCClass) && symbol->isObjCClass()) ||
((mode & ResolveObjCProtocol) && symbol->isObjCProtocol()) ||
(mode & ResolveSymbol)) {
return ! candidates.contains(symbol);
}
return false;
}
QList<Scope *> DeprecatedLookupContext::resolveNestedNameSpecifier(const QualifiedNameId *q,
const QList<Scope *> &visibleScopes) const
{
QList<Symbol *> candidates;
QList<Scope *> scopes = visibleScopes;
for (unsigned i = 0; i < q->nameCount() - 1; ++i) {
const Name *name = q->nameAt(i);
candidates = resolveClassOrNamespace(name, scopes);
if (candidates.isEmpty())
break;
scopes.clear();
foreach (Symbol *candidate, candidates) {
ScopedSymbol *scoped = candidate->asScopedSymbol();
Scope *members = scoped->members();
if (! scopes.contains(members))
scopes.append(members);
}
}
return scopes;
}
QList<Symbol *> DeprecatedLookupContext::resolveQualifiedNameId(const QualifiedNameId *q,
const QList<Scope *> &visibleScopes,
ResolveMode mode) const
{
QList<Symbol *> candidates;
if (true || mode & ResolveClass) {
for (int i = 0; i < visibleScopes.size(); ++i) {
Scope *scope = visibleScopes.at(i);
for (Symbol *symbol = scope->lookat(q); symbol; symbol = symbol->next()) {
if (! symbol->name())
continue;
else if (! symbol->isClass())
continue;
const QualifiedNameId *qq = symbol->name()->asQualifiedNameId();
if (! qq)
continue;
else if (! maybeValidSymbol(symbol, mode, candidates))
continue;
if (! q->unqualifiedNameId()->isEqualTo(qq->unqualifiedNameId()))
continue;
else if (qq->nameCount() == q->nameCount()) {
unsigned j = 0;
for (; j < q->nameCount(); ++j) {
const Name *classOrNamespaceName1 = q->nameAt(j);
const Name *classOrNamespaceName2 = qq->nameAt(j);
if (! classOrNamespaceName1->isEqualTo(classOrNamespaceName2))
break;
}
if (j == q->nameCount())
candidates.append(symbol);
}
}
}
}
QList<Scope *> scopes;
if (q->nameCount() == 1)
scopes = visibleScopes; // ### handle global scope lookup
else
scopes = resolveNestedNameSpecifier(q, visibleScopes);
QList<Scope *> expanded;
foreach (Scope *scope, scopes) {
expanded.append(scope);
for (unsigned i = 0; i < scope->symbolCount(); ++i) {
Symbol *member = scope->symbolAt(i);
if (ScopedSymbol *scopedSymbol = member->asScopedSymbol())
expandEnumOrAnonymousSymbol(scopedSymbol, &expanded);
}
}
candidates += resolve(q->unqualifiedNameId(), expanded, mode);
return candidates;
}
QList<Symbol *> DeprecatedLookupContext::resolveOperatorNameId(const OperatorNameId *opId,
const QList<Scope *> &visibleScopes,
ResolveMode) const
{
QList<Symbol *> candidates;
for (int scopeIndex = 0; scopeIndex < visibleScopes.size(); ++scopeIndex) {
Scope *scope = visibleScopes.at(scopeIndex);
for (Symbol *symbol = scope->lookat(opId->kind()); symbol; symbol = symbol->next()) {
if (! opId->isEqualTo(symbol->name()))
continue;
if (! candidates.contains(symbol))
candidates.append(symbol);
}
}
return candidates;
}
QList<Symbol *> DeprecatedLookupContext::resolve(const Name *name, const QList<Scope *> &visibleScopes,
ResolveMode mode) const
{
QList<Symbol *> candidates;
if (!name)
return candidates; // nothing to do, the symbol is anonymous.
else if (const QualifiedNameId *q = name->asQualifiedNameId())
return resolveQualifiedNameId(q, visibleScopes, mode);
else if (const OperatorNameId *opId = name->asOperatorNameId())
return resolveOperatorNameId(opId, visibleScopes, mode);
else if (const Identifier *id = name->identifier()) {
for (int scopeIndex = 0; scopeIndex < visibleScopes.size(); ++scopeIndex) {
Scope *scope = visibleScopes.at(scopeIndex);
for (Symbol *symbol = scope->lookat(id); symbol; symbol = symbol->next()) {
if (! symbol->name())
continue; // nothing to do, the symbol is anonymous.
else if (! maybeValidSymbol(symbol, mode, candidates))
continue; // skip it, we're not looking for this kind of symbols
else if (const Identifier *symbolId = symbol->identifier()) {
if (! symbolId->isEqualTo(id))
continue; // skip it, the symbol's id is not compatible with this lookup.
}
if (const QualifiedNameId *q = symbol->name()->asQualifiedNameId()) {
if (name->isDestructorNameId() != q->unqualifiedNameId()->isDestructorNameId())
continue;
else if (q->nameCount() > 1) {
const Name *classOrNamespaceName = control()->qualifiedNameId(q->names(),
q->nameCount() - 1);
if (const Identifier *classOrNamespaceNameId = identifier(classOrNamespaceName)) {
if (classOrNamespaceNameId->isEqualTo(id))
continue;
}
const QList<Symbol *> resolvedClassOrNamespace =
resolveClassOrNamespace(classOrNamespaceName, visibleScopes);
bool good = false;
foreach (Symbol *classOrNamespace, resolvedClassOrNamespace) {
ScopedSymbol *scoped = classOrNamespace->asScopedSymbol();
if (visibleScopes.contains(scoped->members())) {
good = true;
break;
}
}
if (! good)
continue;
}
} else if (symbol->name()->isDestructorNameId() != name->isDestructorNameId()) {
// ### FIXME: this is wrong!
continue;
}
if (! candidates.contains(symbol))
candidates.append(symbol);
}
}
}
return candidates;
}
const Identifier *DeprecatedLookupContext::identifier(const Name *name) const
{
if (name)
return name->identifier();
return 0;
}
void DeprecatedLookupContext::buildVisibleScopes_helper(Document::Ptr doc, QList<Scope *> *scopes,
QSet<QString> *processed)
{
if (doc && ! processed->contains(doc->fileName())) {
processed->insert(doc->fileName());
if (doc->globalSymbolCount())
scopes->append(doc->globalSymbols());
foreach (const Document::Include &incl, doc->includes()) {
buildVisibleScopes_helper(_snapshot.document(incl.fileName()),
scopes, processed);
}
}
}
QList<Scope *> DeprecatedLookupContext::buildVisibleScopes()
{
QList<Scope *> scopes;
if (_symbol) {
Scope *scope = _symbol->scope();
if (Function *fun = _symbol->asFunction())
scope = fun->members(); // handle ctor initializers.
for (; scope; scope = scope->enclosingScope()) {
if (scope == _thisDocument->globalSymbols())
break;
scopes.append(scope);
}
}
QSet<QString> processed;
buildVisibleScopes_helper(_thisDocument, &scopes, &processed);
while (true) {
QList<Scope *> expandedScopes;
expand(scopes, &expandedScopes);
if (expandedScopes.size() == scopes.size())
return expandedScopes;
scopes = expandedScopes;
}
return scopes;
}
QList<Scope *> DeprecatedLookupContext::visibleScopes(const LookupItem &result) const
{ return visibleScopes(result.declaration()); }
QList<Scope *> DeprecatedLookupContext::visibleScopes(Symbol *symbol) const
{
QList<Scope *> scopes;
if (symbol) {
for (Scope *scope = symbol->scope(); scope; scope = scope->enclosingScope())
scopes.append(scope);
}
scopes += visibleScopes();
scopes = expand(scopes);
return scopes;
}
void DeprecatedLookupContext::expandEnumOrAnonymousSymbol(ScopedSymbol *scopedSymbol,
QList<Scope *> *expandedScopes) const
{
if (! scopedSymbol || expandedScopes->contains(scopedSymbol->members()))
return;
Scope *members = scopedSymbol->members();
if (scopedSymbol->isEnum())
expandedScopes->append(members);
else if (! scopedSymbol->name() && (scopedSymbol->isClass() || scopedSymbol->isNamespace())) {
// anonymous class or namespace
expandedScopes->append(members);
for (unsigned i = 0; i < members->symbolCount(); ++i) {
Symbol *member = members->symbolAt(i);
if (ScopedSymbol *nested = member->asScopedSymbol()) {
expandEnumOrAnonymousSymbol(nested, expandedScopes);
}
}
}
}
QList<Scope *> DeprecatedLookupContext::expand(const QList<Scope *> &scopes) const
{
QList<Scope *> expanded;
expand(scopes, &expanded);
return expanded;
}
void DeprecatedLookupContext::expand(const QList<Scope *> &scopes, QList<Scope *> *expandedScopes) const
{
for (int i = 0; i < scopes.size(); ++i) {
expand(scopes.at(i), scopes, expandedScopes);
}
}
void DeprecatedLookupContext::expandNamespace(Namespace *ns,
const QList<Scope *> &visibleScopes,
QList<Scope *> *expandedScopes) const
{
//qDebug() << "*** expand namespace:" << ns->fileName() << ns->line() << ns->column();
if (Scope *encl = ns->enclosingNamespaceScope())
expand(encl, visibleScopes, expandedScopes);
if (const Name *nsName = ns->name()) {
const QList<Symbol *> namespaceList = resolveNamespace(nsName, visibleScopes);
foreach (Symbol *otherNs, namespaceList) {
if (otherNs == ns)
continue;
expand(otherNs->asNamespace()->members(), visibleScopes, expandedScopes);
}
}
for (unsigned i = 0; i < ns->memberCount(); ++i) { // ### make me fast
Symbol *symbol = ns->memberAt(i);
if (Namespace *otherNs = symbol->asNamespace()) {
if (! otherNs->name()) {
expand(otherNs->members(), visibleScopes, expandedScopes);
}
} else if (UsingNamespaceDirective *u = symbol->asUsingNamespaceDirective()) {
const QList<Symbol *> candidates = resolveNamespace(u->name(), visibleScopes);
for (int j = 0; j < candidates.size(); ++j) {
expand(candidates.at(j)->asNamespace()->members(),
visibleScopes, expandedScopes);
}
} else if (Enum *e = symbol->asEnum()) {
expand(e->members(), visibleScopes, expandedScopes);
}
}
}
void DeprecatedLookupContext::expandClass(Class *klass,
const QList<Scope *> &visibleScopes,
QList<Scope *> *expandedScopes) const
{
for (TemplateParameters *params = klass->templateParameters(); params; params = params->previous())
expand(params->scope(), visibleScopes, expandedScopes);
for (unsigned i = 0; i < klass->memberCount(); ++i) {
Symbol *symbol = klass->memberAt(i);
if (Class *nestedClass = symbol->asClass()) {
if (! nestedClass->name()) {
expand(nestedClass->members(), visibleScopes, expandedScopes);
}
} else if (Enum *e = symbol->asEnum()) {
expand(e->members(), visibleScopes, expandedScopes);
}
}
if (klass->baseClassCount()) {
QList<Scope *> classVisibleScopes = visibleScopes;
for (Scope *scope = klass->scope(); scope; scope = scope->enclosingScope()) {
if (scope->isNamespaceScope()) {
Namespace *enclosingNamespace = scope->owner()->asNamespace();
if (enclosingNamespace->name()) {
const QList<Symbol *> nsList = resolveNamespace(enclosingNamespace->name(),
visibleScopes);
foreach (Symbol *ns, nsList) {
expand(ns->asNamespace()->members(), classVisibleScopes,
&classVisibleScopes);
}
}
}
}
for (unsigned i = 0; i < klass->baseClassCount(); ++i) {
BaseClass *baseClass = klass->baseClassAt(i);
const Name *baseClassName = baseClass->name();
const QList<Symbol *> baseClassCandidates = resolveClass(baseClassName,
classVisibleScopes);
for (int j = 0; j < baseClassCandidates.size(); ++j) {
if (Class *baseClassSymbol = baseClassCandidates.at(j)->asClass())
expand(baseClassSymbol->members(), visibleScopes, expandedScopes);
}
}
}
}
void DeprecatedLookupContext::expandBlock(Block *blockSymbol,
const QList<Scope *> &visibleScopes,
QList<Scope *> *expandedScopes) const
{
for (unsigned i = 0; i < blockSymbol->memberCount(); ++i) {
Symbol *symbol = blockSymbol->memberAt(i);
if (UsingNamespaceDirective *u = symbol->asUsingNamespaceDirective()) {
const QList<Symbol *> candidates = resolveNamespace(u->name(),
visibleScopes);
for (int j = 0; j < candidates.size(); ++j) {
expand(candidates.at(j)->asNamespace()->members(),
visibleScopes, expandedScopes);
}
}
}
}
void DeprecatedLookupContext::expandFunction(Function *function,
const QList<Scope *> &visibleScopes,
QList<Scope *> *expandedScopes) const
{
for (TemplateParameters *params = function->templateParameters(); params; params = params->previous())
expand(params->scope(), visibleScopes, expandedScopes);
if (! expandedScopes->contains(function->arguments()))
expandedScopes->append(function->arguments());
if (const QualifiedNameId *q = function->name()->asQualifiedNameId()) {
const Name *nestedNameSpec = 0;
if (q->nameCount() == 1)
nestedNameSpec = q->nameAt(0);
else
nestedNameSpec = control()->qualifiedNameId(q->names(), q->nameCount() - 1,
q->isGlobal());
const QList<Symbol *> candidates = resolveClassOrNamespace(nestedNameSpec, visibleScopes);
for (int j = 0; j < candidates.size(); ++j) {
if (ScopedSymbol *scopedSymbol = candidates.at(j)->asScopedSymbol())
expand(scopedSymbol->members(), visibleScopes, expandedScopes);
}
}
}
void DeprecatedLookupContext::expandObjCMethod(ObjCMethod *method,
const QList<Scope *> &,
QList<Scope *> *expandedScopes) const
{
if (! expandedScopes->contains(method->arguments()))
expandedScopes->append(method->arguments());
}
void DeprecatedLookupContext::expandObjCClass(ObjCClass *klass,
const QList<Scope *> &visibleScopes,
QList<Scope *> *expandedScopes) const
{
{// expand other @interfaces, @implementations and categories for this class:
const QList<Symbol *> classList = resolveObjCClass(klass->name(), visibleScopes);
foreach (Symbol *otherClass, classList) {
if (otherClass == klass)
continue;
expand(otherClass->asObjCClass()->members(), visibleScopes, expandedScopes);
}
}
// expand definitions in the currect class:
for (unsigned i = 0; i < klass->memberCount(); ++i) {
Symbol *symbol = klass->memberAt(i);
if (Class *nestedClass = symbol->asClass()) {
if (! nestedClass->name()) {
expand(nestedClass->members(), visibleScopes, expandedScopes);
}
} else if (Enum *e = symbol->asEnum()) {
expand(e->members(), visibleScopes, expandedScopes);
}
}
// expand the base class:
if (ObjCBaseClass *baseClass = klass->baseClass()) {
const Name *baseClassName = baseClass->name();
const QList<Symbol *> baseClassCandidates = resolveObjCClass(baseClassName,
visibleScopes);
for (int j = 0; j < baseClassCandidates.size(); ++j) {
if (ObjCClass *baseClassSymbol = baseClassCandidates.at(j)->asObjCClass())
expand(baseClassSymbol->members(), visibleScopes, expandedScopes);
}
}
// expand the protocols:
for (unsigned i = 0; i < klass->protocolCount(); ++i) {
const Name *protocolName = klass->protocolAt(i)->name();
const QList<Symbol *> protocolCandidates = resolveObjCProtocol(protocolName, visibleScopes