Commit d9527680 authored by Roberto Raggi's avatar Roberto Raggi

Try to use the new LookupContext.

parent 66a9ef07
......@@ -282,14 +282,14 @@ void FindUsages::checkExpression(unsigned startToken, unsigned endToken)
// qDebug() << "*** check expression:" << expression;
TypeOfExpression typeofExpression;
typeofExpression.setSnapshot(_snapshot);
typeofExpression.init(_doc, _snapshot);
unsigned line, column;
getTokenStartPosition(startToken, &line, &column);
Symbol *lastVisibleSymbol = _doc->findSymbolAt(line, column);
const QList<LookupItem> results = typeofExpression(expression, _doc, lastVisibleSymbol,
TypeOfExpression::Preprocess);
const QList<LookupItem> results = typeofExpression(expression, lastVisibleSymbol,
TypeOfExpression::Preprocess);
QList<Symbol *> candidates;
......
......@@ -47,10 +47,10 @@ namespace {
class ApplySubstitution
{
public:
ApplySubstitution(const DeprecatedLookupContext &context, Symbol *symbol, const GenTemplateInstance::Substitution &substitution);
ApplySubstitution(Control *control, Symbol *symbol, const GenTemplateInstance::Substitution &substitution);
~ApplySubstitution();
Control *control() const { return context.control(); }
inline Control *control() const { return _control; }
FullySpecifiedType apply(const Name *name);
FullySpecifiedType apply(const FullySpecifiedType &type);
......@@ -309,16 +309,16 @@ private:
};
public: // attributes
DeprecatedLookupContext context;
Control *_control;
Symbol *symbol;
GenTemplateInstance::Substitution substitution;
ApplyToType applyToType;
ApplyToName applyToName;
};
ApplySubstitution::ApplySubstitution(const DeprecatedLookupContext &context, Symbol *symbol,
ApplySubstitution::ApplySubstitution(Control *control, Symbol *symbol,
const GenTemplateInstance::Substitution &substitution)
: context(context), symbol(symbol),
: _control(control), symbol(symbol),
substitution(substitution),
applyToType(this), applyToName(this)
{ }
......@@ -363,17 +363,17 @@ FullySpecifiedType ApplySubstitution::applySubstitution(int index) const
} // end of anonymous namespace
GenTemplateInstance::GenTemplateInstance(const DeprecatedLookupContext &context, const Substitution &substitution)
GenTemplateInstance::GenTemplateInstance(Control *control, const Substitution &substitution)
: _symbol(0),
_context(context),
_control(control),
_substitution(substitution)
{ }
FullySpecifiedType GenTemplateInstance::operator()(Symbol *symbol)
{
ApplySubstitution o(_context, symbol, _substitution);
ApplySubstitution o(_control, symbol, _substitution);
return o.apply(symbol->type());
}
Control *GenTemplateInstance::control() const
{ return _context.control(); }
{ return _control; }
......@@ -47,7 +47,7 @@ public:
typedef QList< QPair<const Identifier *, FullySpecifiedType> > Substitution;
public:
GenTemplateInstance(const DeprecatedLookupContext &context, const Substitution &substitution);
GenTemplateInstance(Control *control, const Substitution &substitution);
FullySpecifiedType operator()(Symbol *symbol);
......@@ -55,7 +55,7 @@ public:
private:
Symbol *_symbol;
DeprecatedLookupContext _context;
Control *_control;
const Substitution _substitution;
};
......
......@@ -148,6 +148,16 @@ ClassOrNamespace *LookupContext::classOrNamespace(const Name *name, Symbol *last
return classOrNamespace(name, lastVisibleSymbol);
}
QList<Symbol *> LookupContext::lookup(const Name *name, Symbol *lastVisibleSymbol) const
{
Scope *scope = _thisDocument->globalSymbols();
if (lastVisibleSymbol && lastVisibleSymbol->scope())
scope = lastVisibleSymbol->scope();
return lookup(name, scope);
}
QList<Symbol *> LookupContext::lookup(const Name *name, Scope *scope) const
{
QList<Symbol *> candidates;
......@@ -168,19 +178,15 @@ QList<Symbol *> LookupContext::lookup(const Name *name, Scope *scope) const
Symbol *member = scope->symbolAt(index);
if (UsingNamespaceDirective *u = member->asUsingNamespaceDirective()) {
Namespace *enclosingNamespace = u->enclosingNamespaceScope()->owner()->asNamespace();
//qDebug() << "*** enclosing namespace:" << enclosingNamespace;
Q_ASSERT(enclosingNamespace != 0);
ClassOrNamespace *b = bindings()->findClassOrNamespace(enclosingNamespace);
//qDebug() << "**** binding:" << b;
Q_ASSERT(b != 0);
if (ClassOrNamespace *uu = b->lookupClassOrNamespace(u->name())) {
candidates = uu->lookup(name);
if (! candidates.isEmpty())
return candidates;
if (Namespace *enclosingNamespace = u->enclosingNamespaceScope()->owner()->asNamespace()) {
if (ClassOrNamespace *b = bindings()->findClassOrNamespace(enclosingNamespace)) {
if (ClassOrNamespace *uu = b->lookupClassOrNamespace(u->name())) {
candidates = uu->lookup(name);
if (! candidates.isEmpty())
return candidates;
}
}
}
}
}
......
......@@ -174,6 +174,7 @@ public:
Document::Ptr document(const QString &fileName) const;
Snapshot snapshot() const;
QList<Symbol *> lookup(const Name *name, Symbol *lastVisibleSymbol) const;
QList<Symbol *> lookup(const Name *name, Scope *scope) const;
ClassOrNamespace *globalNamespace() const;
......@@ -188,7 +189,7 @@ public:
/// \internal
void setBindings(QSharedPointer<CreateBindings> bindings);
Q_DECL_DEPRECATED Control *control() const;
Control *control() const; // ### deprecate
private:
Control *_control;
......
This diff is collapsed.
......@@ -30,7 +30,7 @@
#ifndef CPLUSPLUS_RESOLVEEXPRESSION_H
#define CPLUSPLUS_RESOLVEEXPRESSION_H
#include "DeprecatedLookupContext.h"
#include "LookupContext.h"
#include <ASTVisitor.h>
#include <Semantic.h>
......@@ -41,27 +41,30 @@ namespace CPlusPlus {
class CPLUSPLUS_EXPORT ResolveExpression: protected ASTVisitor
{
public:
ResolveExpression(const DeprecatedLookupContext &context);
ResolveExpression(Symbol *lastVisibleSymbol, const LookupContext &context);
ResolveExpression(Scope *scope, const LookupContext &context);
virtual ~ResolveExpression();
QList<LookupItem> operator()(ExpressionAST *ast);
QList<LookupItem> resolveMemberExpression(const QList<LookupItem> &baseResults,
unsigned accessOp,
const Name *memberName,
bool *replacedDotOperator = 0) const;
unsigned accessOp,
const Name *memberName,
bool *replacedDotOperator = 0) const;
QList<LookupItem> resolveBaseExpression(const QList<LookupItem> &baseResults,
int accessOp,
bool *replacedDotOperator = 0) const;
int accessOp,
bool *replacedDotOperator = 0) const;
QList<LookupItem> resolveMember(const Name *memberName, Class *klass,
const Name *className = 0) const;
Q_DECL_DEPRECATED QList<LookupItem> resolveMember(const Name *memberName, Class *klass,
const Name *className = 0) const;
QList<LookupItem> resolveMember(const Name *memberName, ObjCClass *klass) const;
Q_DECL_DEPRECATED QList<LookupItem> resolveMember(const Name *memberName, ObjCClass *klass) const;
protected:
QList<LookupItem> switchResults(const QList<LookupItem> &symbols);
FullySpecifiedType instantiate(const Name *className, Symbol *candidate) const;
void thisObject();
void addResult(const FullySpecifiedType &ty, Symbol *symbol = 0);
......@@ -113,44 +116,15 @@ protected:
// Objective-C expressions
virtual bool visit(ObjCMessageExpressionAST *ast);
QList<Scope *> visibleScopes(const LookupItem &result) const;
private:
DeprecatedLookupContext _context;
Symbol *_lastVisibleSymbol;
Scope *_scope;
LookupContext _context;
Semantic sem;
QList<LookupItem> _results;
Symbol *_declSymbol;
};
class CPLUSPLUS_EXPORT ResolveClass
{
public:
ResolveClass();
QList<Symbol *> operator()(const Name *name,
const LookupItem &p,
const DeprecatedLookupContext &context);
private:
QList<Symbol *> resolveClass(const Name *name,
const LookupItem &p,
const DeprecatedLookupContext &context);
private:
QList<LookupItem> _blackList;
};
class CPLUSPLUS_EXPORT ResolveObjCClass
{
public:
ResolveObjCClass();
QList<Symbol *> operator()(const Name *name,
const LookupItem &p,
const DeprecatedLookupContext &context);
};
} // end of namespace CPlusPlus
#endif // CPLUSPLUS_RESOLVEEXPRESSION_H
......@@ -29,7 +29,7 @@
#include "TypeOfExpression.h"
#include <TranslationUnit.h>
#include "DeprecatedLookupContext.h"
#include "LookupContext.h"
#include "ResolveExpression.h"
#include "pp.h"
......@@ -39,44 +39,59 @@
using namespace CPlusPlus;
TypeOfExpression::TypeOfExpression():
m_ast(0)
m_ast(0),
m_lastVisibleSymbol(0)
{
}
Snapshot TypeOfExpression::snapshot() const
void TypeOfExpression::reset()
{
return m_snapshot;
m_thisDocument.clear();
m_snapshot = Snapshot();
m_ast = 0;
m_lastVisibleSymbol = 0;
m_lookupContext = LookupContext();
m_bindings.clear();
m_environment.clear();
}
void TypeOfExpression::setSnapshot(const Snapshot &documents)
void TypeOfExpression::init(Document::Ptr thisDocument, const Snapshot &snapshot,
QSharedPointer<CreateBindings> bindings)
{
m_snapshot = documents;
m_lookupContext = DeprecatedLookupContext();
m_thisDocument = thisDocument;
m_snapshot = snapshot;
m_ast = 0;
m_lastVisibleSymbol = 0;
m_lookupContext = LookupContext();
m_bindings = bindings;
m_environment.clear();
}
QList<LookupItem> TypeOfExpression::operator()(const QString &expression,
Document::Ptr document,
Symbol *lastVisibleSymbol,
PreprocessMode mode)
Symbol *lastVisibleSymbol,
PreprocessMode mode)
{
QString code = expression;
if (mode == Preprocess)
code = preprocessedExpression(expression, m_snapshot, document);
code = preprocessedExpression(expression);
Document::Ptr expressionDoc = documentForExpression(code);
expressionDoc->check();
m_ast = extractExpressionAST(expressionDoc);
m_lookupContext = DeprecatedLookupContext(lastVisibleSymbol, expressionDoc,
document, m_snapshot);
m_lastVisibleSymbol = lastVisibleSymbol;
m_lookupContext = LookupContext(expressionDoc, m_thisDocument, m_snapshot);
m_lookupContext.setBindings(m_bindings);
ResolveExpression resolveExpression(m_lookupContext);
ResolveExpression resolveExpression(lastVisibleSymbol, m_lookupContext);
return resolveExpression(m_ast);
}
QString TypeOfExpression::preprocess(const QString &expression,
Document::Ptr document) const
QString TypeOfExpression::preprocess(const QString &expression) const
{
return preprocessedExpression(expression, m_snapshot, document);
return preprocessedExpression(expression);
}
ExpressionAST *TypeOfExpression::ast() const
......@@ -84,7 +99,12 @@ ExpressionAST *TypeOfExpression::ast() const
return m_ast;
}
const DeprecatedLookupContext &TypeOfExpression::lookupContext() const
Symbol *TypeOfExpression::lastVisibleSymbol() const
{
return m_lastVisibleSymbol;
}
const LookupContext &TypeOfExpression::lookupContext() const
{
return m_lookupContext;
}
......@@ -112,37 +132,35 @@ Document::Ptr TypeOfExpression::documentForExpression(const QString &expression)
return doc;
}
void TypeOfExpression::processEnvironment(Snapshot documents,
Document::Ptr doc, Environment *env,
void TypeOfExpression::processEnvironment(Document::Ptr doc, Environment *env,
QSet<QString> *processed) const
{
if (! doc)
return;
if (processed->contains(doc->fileName()))
return;
processed->insert(doc->fileName());
foreach (const Document::Include &incl, doc->includes()) {
processEnvironment(documents,
documents.document(incl.fileName()),
env, processed);
if (doc && ! processed->contains(doc->fileName())) {
processed->insert(doc->fileName());
foreach (const Document::Include &incl, doc->includes())
processEnvironment(m_snapshot.document(incl.fileName()), env, processed);
foreach (const Macro &macro, doc->definedMacros())
env->bind(macro);
}
foreach (const Macro &macro, doc->definedMacros())
env->bind(macro);
}
QString TypeOfExpression::preprocessedExpression(const QString &expression,
Snapshot documents,
Document::Ptr thisDocument) const
QString TypeOfExpression::preprocessedExpression(const QString &expression) const
{
if (expression.trimmed().isEmpty())
return expression;
Environment env;
QSet<QString> processed;
processEnvironment(documents, thisDocument,
&env, &processed);
if (! m_environment) {
Environment *env = new Environment(); // ### cache the environment.
QSet<QString> processed;
processEnvironment(m_thisDocument, env, &processed);
m_environment = QSharedPointer<Environment>(env);
}
const QByteArray code = expression.toUtf8();
Preprocessor preproc(0, &env);
Preprocessor preproc(0, m_environment.data());
const QByteArray preprocessedCode = preproc("<expression>", code);
return QString::fromUtf8(preprocessedCode.constData(), preprocessedCode.size());
}
......@@ -30,7 +30,8 @@
#define CPLUSPLUS_TYPEOFEXPRESSION_H
#include "CppDocument.h"
#include "DeprecatedLookupContext.h"
#include "LookupContext.h"
#include "PreprocessorEnvironment.h"
#include <ASTfwd.h>
#include <QtCore/QMap>
......@@ -44,11 +45,11 @@ class Macro;
class CPLUSPLUS_EXPORT TypeOfExpression
{
Q_DISABLE_COPY(TypeOfExpression)
public:
TypeOfExpression();
Snapshot snapshot() const;
/**
* Sets the documents used to evaluate expressions. Should be set before
* calling this functor.
......@@ -56,7 +57,10 @@ public:
* Also clears the lookup context, so can be used to make sure references
* to the documents previously used are removed.
*/
void setSnapshot(const Snapshot &documents);
void init(Document::Ptr thisDocument, const Snapshot &snapshot,
QSharedPointer<CreateBindings> bindings = QSharedPointer<CreateBindings>());
void reset();
enum PreprocessMode {
NoPreprocess,
......@@ -72,14 +76,13 @@ public:
* has been made!
*
* @param expression The expression to evaluate.
* @param document The document the expression is part of.
* @param lastVisibleSymbol The last visible symbol in the document.
*/
QList<LookupItem> operator()(const QString &expression, Document::Ptr document,
Symbol *lastVisibleSymbol,
PreprocessMode mode = NoPreprocess);
QList<LookupItem> operator()(const QString &expression,
Symbol *lastVisibleSymbol,
PreprocessMode mode = NoPreprocess);
QString preprocess(const QString &expression, Document::Ptr document) const;
QString preprocess(const QString &expression) const;
/**
* Returns the AST of the last evaluated expression.
......@@ -89,7 +92,8 @@ public:
/**
* Returns the lookup context of the last evaluated expression.
*/
const DeprecatedLookupContext &lookupContext() const;
const LookupContext &lookupContext() const;
Symbol *lastVisibleSymbol() const;
ExpressionAST *expressionAST() const;
......@@ -97,17 +101,19 @@ private:
ExpressionAST *extractExpressionAST(Document::Ptr doc) const;
Document::Ptr documentForExpression(const QString &expression) const;
void processEnvironment(CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr doc, CPlusPlus::Environment *env,
void processEnvironment(Document::Ptr doc, Environment *env,
QSet<QString> *processed) const;
QString preprocessedExpression(const QString &expression,
CPlusPlus::Snapshot documents,
CPlusPlus::Document::Ptr thisDocument) const;
QString preprocessedExpression(const QString &expression) const;
private:
Document::Ptr m_thisDocument;
Snapshot m_snapshot;
QSharedPointer<CreateBindings> m_bindings;
ExpressionAST *m_ast;
DeprecatedLookupContext m_lookupContext;
Symbol *m_lastVisibleSymbol;
LookupContext m_lookupContext;
mutable QSharedPointer<Environment> m_environment;
};
} // namespace CPlusPlus
......
......@@ -560,7 +560,7 @@ protected:
} // end of anonymous namespace
static const QualifiedNameId *qualifiedNameIdForSymbol(Symbol *s, const DeprecatedLookupContext &context)
static const QualifiedNameId *qualifiedNameIdForSymbol(Symbol *s, Control *control)
{
const Name *symbolName = s->name();
if (! symbolName)
......@@ -591,7 +591,7 @@ static const QualifiedNameId *qualifiedNameIdForSymbol(Symbol *s, const Deprecat
names.append(symbolName);
}
return context.control()->qualifiedNameId(names.constData(), names.size());
return control->qualifiedNameId(names.constData(), names.size());
}
CPPEditorEditable::CPPEditorEditable(CPPEditor *editor)
......@@ -869,13 +869,12 @@ CPlusPlus::Symbol *CPPEditor::findCanonicalSymbol(const QTextCursor &cursor,
// qDebug() << "code:" << code;
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(snapshot);
typeOfExpression.init(doc, snapshot);
Symbol *lastVisibleSymbol = doc->findSymbolAt(line, col);
const QList<LookupItem> results = typeOfExpression(code, doc,
lastVisibleSymbol,
TypeOfExpression::Preprocess);
const QList<LookupItem> results = typeOfExpression(code, lastVisibleSymbol,
TypeOfExpression::Preprocess);
NamespaceBindingPtr glo = bind(doc, snapshot);
Symbol *canonicalSymbol = DeprecatedLookupContext::canonicalSymbol(results, glo.data());
......@@ -1255,13 +1254,10 @@ void CPPEditor::switchDeclarationDefinition()
}
if (f) {
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(m_modelManager->snapshot());
QList<LookupItem> resolvedSymbols = typeOfExpression(QString(), doc, lastSymbol);
const DeprecatedLookupContext &context = typeOfExpression.lookupContext();
LookupContext context(doc, snapshot);
const QualifiedNameId *q = qualifiedNameIdForSymbol(f, context);
QList<Symbol *> symbols = context.resolve(q);
const QualifiedNameId *q = qualifiedNameIdForSymbol(f, context.control());
const QList<Symbol *> symbols = context.lookup(q, lastSymbol); // ### FIXME
Symbol *declaration = 0;
foreach (declaration, symbols) {
......@@ -1438,9 +1434,9 @@ CPPEditor::Link CPPEditor::findLinkAt(const QTextCursor &cursor,
const QString expression = expressionUnderCursor(tc);
TypeOfExpression typeOfExpression;
typeOfExpression.setSnapshot(snapshot);
typeOfExpression.init(doc, snapshot);
QList<LookupItem> resolvedSymbols =
typeOfExpression(expression, doc, lastSymbol);
typeOfExpression(expression, lastSymbol);
if (!resolvedSymbols.isEmpty()) {
LookupItem result = skipForwardDeclarations(resolvedSymbols);
......
......@@ -180,7 +180,8 @@ static QString buildHelpId(Symbol *symbol, const Name *name)
// ### move me
static FullySpecifiedType resolve(const FullySpecifiedType &ty,
const DeprecatedLookupContext &context,
const LookupContext &context,
Symbol *lastVisibleSymbol,
Symbol **resolvedSymbol,
const Name **resolvedName)
{
......@@ -188,22 +189,25 @@ static FullySpecifiedType resolve(const FullySpecifiedType &ty,
if (const PointerType *ptrTy = ty->asPointerType()) {
return control->pointerType(resolve(ptrTy->elementType(), context,
lastVisibleSymbol,
resolvedSymbol, resolvedName));
} else if (const ReferenceType *refTy = ty->asReferenceType()) {
return control->referenceType(resolve(refTy->elementType(), context,
lastVisibleSymbol,
resolvedSymbol, resolvedName));
} else if (const PointerToMemberType *ptrToMemTy = ty->asPointerToMemberType()) {
return control->pointerToMemberType(ptrToMemTy->memberName(),
resolve(ptrToMemTy->elementType(), context,
lastVisibleSymbol,
resolvedSymbol, resolvedName));
} else if (const NamedType *namedTy = ty->asNamedType()) {
if (resolvedName)
*resolvedName = namedTy->name();
const QList<Symbol *> candidates = context.resolve(namedTy->name());
const QList<Symbol *> candidates = context.lookup(namedTy->name(), lastVisibleSymbol);
foreach (Symbol *c, candidates) {
if (c->isClass() || c->isEnum()) {
......@@ -284,7 +288,7 @@ void CppHoverHandler::updateHelpIdAndTooltip(TextEditor::ITextEditor *editor, in
Symbol *lastSymbol = doc->findSymbolAt(line, column);
TypeOfExpression typeOfExpression;