Files
qt-creator/src/libs/cplusplus/LookupContext.h

263 lines
8.5 KiB
C
Raw Normal View History

2010-05-05 10:28:32 +02:00
/**************************************************************************
**
** This file is part of Qt Creator
**
** Copyright (c) 2012 Nokia Corporation and/or its subsidiary(-ies).
2010-05-05 10:28:32 +02:00
**
** Contact: http://www.qt-project.org/
2010-05-05 10:28:32 +02:00
**
**
** GNU Lesser General Public License Usage
**
2011-04-13 08:42:33 +02:00
** 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.
2010-05-05 10:28:32 +02:00
**
2010-12-17 16:01:08 +01:00
** In addition, as a special exception, Nokia gives you certain additional
2011-04-13 08:42:33 +02:00
** rights. These rights are described in the Nokia Qt LGPL Exception
2010-12-17 16:01:08 +01:00
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
2011-04-13 08:42:33 +02:00
** Other Usage
**
** Alternatively, this file may be used in accordance with the terms and
** conditions contained in a signed written agreement between you and Nokia.
**
2010-05-05 10:28:32 +02:00
**
**************************************************************************/
#ifndef CPLUSPLUS_LOOKUPCONTEXT_H
#define CPLUSPLUS_LOOKUPCONTEXT_H
#include "CppDocument.h"
#include "LookupItem.h"
#include <FullySpecifiedType.h>
#include <Type.h>
#include <SymbolVisitor.h>
#include <Control.h>
#include <Name.h>
#include <QSet>
#include <map>
#include <functional>
2010-05-05 10:28:32 +02:00
namespace CPlusPlus {
class CreateBindings;
class CPLUSPLUS_EXPORT ClassOrNamespace
{
public:
ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent);
2010-05-12 16:04:43 +02:00
const TemplateNameId *templateId() const;
ClassOrNamespace *instantiationOrigin() const;
ClassOrNamespace *parent() const;
2010-05-05 10:28:32 +02:00
QList<ClassOrNamespace *> usings() const;
QList<Enum *> enums() const;
QList<Symbol *> symbols() const;
ClassOrNamespace *globalNamespace() const;
QList<LookupItem> lookup(const Name *name);
QList<LookupItem> find(const Name *name);
2010-05-05 10:28:32 +02:00
ClassOrNamespace *lookupType(const Name *name);
ClassOrNamespace *findType(const Name *name);
2010-05-05 10:28:32 +02:00
private:
/// \internal
void flush();
/// \internal
ClassOrNamespace *findOrCreateType(const Name *name, ClassOrNamespace *origin = 0);
2010-05-05 10:28:32 +02:00
void addTodo(Symbol *symbol);
void addSymbol(Symbol *symbol);
void addEnum(Enum *e);
void addUsing(ClassOrNamespace *u);
2010-05-14 13:49:16 +02:00
void addNestedType(const Name *alias, ClassOrNamespace *e);
2010-05-05 10:28:32 +02:00
QList<LookupItem> lookup_helper(const Name *name, bool searchInEnclosingScope);
2010-05-05 10:28:32 +02:00
void lookup_helper(const Name *name, ClassOrNamespace *binding,
QList<LookupItem> *result,
QSet<ClassOrNamespace *> *processed,
const TemplateNameId *templateId);
2010-05-05 10:28:32 +02:00
ClassOrNamespace *lookupType_helper(const Name *name, QSet<ClassOrNamespace *> *processed,
bool searchInEnclosingScope, ClassOrNamespace *origin);
ClassOrNamespace *nestedType(const Name *name, ClassOrNamespace *origin);
2010-05-05 10:28:32 +02:00
private:
struct CompareName: std::binary_function<const Name *, const Name *, bool> {
bool operator()(const Name *name, const Name *other) const;
};
2010-05-05 10:28:32 +02:00
private:
typedef std::map<const Name *, ClassOrNamespace *, CompareName> Table;
2010-05-05 10:28:32 +02:00
CreateBindings *_factory;
ClassOrNamespace *_parent;
QList<Symbol *> _symbols;
QList<ClassOrNamespace *> _usings;
Table _classOrNamespaces;
2010-05-05 10:28:32 +02:00
QList<Enum *> _enums;
QList<Symbol *> _todo;
QSharedPointer<Control> _control;
2010-05-05 10:28:32 +02:00
// it's an instantiation.
const TemplateNameId *_templateId;
ClassOrNamespace *_instantiationOrigin;
2010-05-05 10:28:32 +02:00
friend class CreateBindings;
};
class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
{
Q_DISABLE_COPY(CreateBindings)
public:
CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control);
2010-05-05 10:28:32 +02:00
virtual ~CreateBindings();
2010-05-11 10:12:49 +02:00
/// Returns the binding for the global namespace.
2010-05-05 10:28:32 +02:00
ClassOrNamespace *globalNamespace() const;
2010-05-11 10:12:49 +02:00
/// Finds the binding associated to the given symbol.
2010-05-14 14:00:56 +02:00
ClassOrNamespace *lookupType(Symbol *symbol);
ClassOrNamespace *lookupType(const QList<const Name *> &path);
2010-05-11 10:12:49 +02:00
/// Returns the Control that must be used to create temporary symbols.
2010-05-05 10:28:32 +02:00
/// \internal
QSharedPointer<Control> control() const;
2010-05-05 10:28:32 +02:00
2010-05-11 10:12:49 +02:00
/// Searches in \a scope for symbols with the given \a name.
/// Store the result in \a results.
2010-05-05 10:28:32 +02:00
/// \internal
void lookupInScope(const Name *name, Scope *scope, QList<LookupItem> *result,
const TemplateNameId *templateId, ClassOrNamespace *binding);
2010-05-05 10:28:32 +02:00
2010-05-11 10:12:49 +02:00
/// Create bindings for the symbols reachable from \a rootSymbol.
/// \internal
2010-05-11 10:12:49 +02:00
void process(Symbol *rootSymbol, ClassOrNamespace *classOrNamespace);
2010-05-11 10:12:49 +02:00
/// Create an empty ClassOrNamespace binding with the given \a parent.
/// \internal
2010-05-11 10:03:30 +02:00
ClassOrNamespace *allocClassOrNamespace(ClassOrNamespace *parent);
2010-05-05 10:28:32 +02:00
protected:
using SymbolVisitor::visit;
2010-05-11 10:12:49 +02:00
/// Change the current ClassOrNamespace binding.
2010-05-11 10:03:30 +02:00
ClassOrNamespace *switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace);
2010-05-05 10:28:32 +02:00
2010-05-11 10:12:49 +02:00
/// Enters the ClassOrNamespace binding associated with the given \a symbol.
ClassOrNamespace *enterClassOrNamespaceBinding(Symbol *symbol);
/// Enters a ClassOrNamespace binding for the given \a symbol in the global
/// namespace binding.
ClassOrNamespace *enterGlobalClassOrNamespace(Symbol *symbol);
/// Creates bindings for the given \a document.
void process(Document::Ptr document);
/// Creates bindings for the symbols reachable from the \a root symbol.
void process(Symbol *root);
2010-05-05 10:28:32 +02:00
virtual bool visit(Template *templ);
2010-05-05 10:28:32 +02:00
virtual bool visit(Namespace *ns);
virtual bool visit(Class *klass);
virtual bool visit(ForwardClassDeclaration *klass);
virtual bool visit(Enum *e);
virtual bool visit(Declaration *decl);
virtual bool visit(Function *);
virtual bool visit(BaseClass *b);
virtual bool visit(UsingNamespaceDirective *u);
2010-05-27 16:33:18 +02:00
virtual bool visit(UsingDeclaration *u);
2010-05-05 10:28:32 +02:00
virtual bool visit(NamespaceAlias *a);
virtual bool visit(ObjCClass *klass);
virtual bool visit(ObjCBaseClass *b);
virtual bool visit(ObjCForwardClassDeclaration *klass);
virtual bool visit(ObjCProtocol *proto);
virtual bool visit(ObjCBaseProtocol *b);
virtual bool visit(ObjCForwardProtocolDeclaration *proto);
virtual bool visit(ObjCMethod *);
private:
Snapshot _snapshot;
QSharedPointer<Control> _control;
2010-05-05 10:28:32 +02:00
QSet<Namespace *> _processed;
QList<ClassOrNamespace *> _entities;
ClassOrNamespace *_globalNamespace;
ClassOrNamespace *_currentClassOrNamespace;
};
class CPLUSPLUS_EXPORT LookupContext
{
public:
LookupContext();
LookupContext(Document::Ptr thisDocument,
const Snapshot &snapshot);
LookupContext(Document::Ptr expressionDocument,
Document::Ptr thisDocument,
const Snapshot &snapshot);
LookupContext(const LookupContext &other);
LookupContext &operator = (const LookupContext &other);
Document::Ptr expressionDocument() const;
Document::Ptr thisDocument() const;
Document::Ptr document(const QString &fileName) const;
Snapshot snapshot() const;
ClassOrNamespace *globalNamespace() const;
QList<LookupItem> lookup(const Name *name, Scope *scope) const;
ClassOrNamespace *lookupType(const Name *name, Scope *scope) const;
ClassOrNamespace *lookupType(Symbol *symbol) const;
ClassOrNamespace *lookupParent(Symbol *symbol) const;
2010-05-05 10:28:32 +02:00
/// \internal
QSharedPointer<CreateBindings> bindings() const;
/// \internal
void setBindings(QSharedPointer<CreateBindings> bindings);
QSharedPointer<Control> control() const; // ### deprecate
2010-05-05 10:28:32 +02:00
static QList<const Name *> fullyQualifiedName(Symbol *symbol);
static QList<const Name *> path(Symbol *symbol);
static const Name *minimalName(Symbol *symbol, ClassOrNamespace *target, Control *control);
2010-05-05 10:28:32 +02:00
private:
// The current expression.
Document::Ptr _expressionDocument;
// The current document.
Document::Ptr _thisDocument;
// All documents.
Snapshot _snapshot;
// Bindings
mutable QSharedPointer<CreateBindings> _bindings;
QSharedPointer<Control> _control;
2010-05-05 10:28:32 +02:00
};
bool CPLUSPLUS_EXPORT compareName(const Name *name, const Name *other);
bool CPLUSPLUS_EXPORT compareFullyQualifiedName(const QList<const Name *> &path,
const QList<const Name *> &other);
} // namespace CPlusPlus
2010-05-05 10:28:32 +02:00
#endif // CPLUSPLUS_LOOKUPCONTEXT_H