1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (info@qt.nokia.com)
10 ** GNU Lesser General Public License Usage
12 ** This file may be used under the terms of the GNU Lesser General Public
13 ** License version 2.1 as published by the Free Software Foundation and
14 ** appearing in the file LICENSE.LGPL included in the packaging of this file.
15 ** Please review the following information to ensure the GNU Lesser General
16 ** Public License version 2.1 requirements will be met:
17 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
19 ** In addition, as a special exception, Nokia gives you certain additional
20 ** rights. These rights are described in the Nokia Qt LGPL Exception
21 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
25 ** Alternatively, this file may be used in accordance with the terms and
26 ** conditions contained in a signed written agreement between you and Nokia.
28 ** If you have questions regarding the use of this file, please contact
29 ** Nokia at qt-info@nokia.com.
31 **************************************************************************/
33 #ifndef CPLUSPLUS_LOOKUPCONTEXT_H
34 #define CPLUSPLUS_LOOKUPCONTEXT_H
36 #include "CppDocument.h"
37 #include "LookupItem.h"
38 #include <FullySpecifiedType.h>
40 #include <SymbolVisitor.h>
43 #include <QtCore/QSet>
51 class CPLUSPLUS_EXPORT ClassOrNamespace
54 ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent);
56 const TemplateNameId *templateId() const;
57 ClassOrNamespace *parent() const;
58 QList<ClassOrNamespace *> usings() const;
59 QList<Enum *> enums() const;
60 QList<Symbol *> symbols() const;
62 ClassOrNamespace *globalNamespace() const;
64 QList<LookupItem> lookup(const Name *name);
65 QList<LookupItem> find(const Name *name);
67 ClassOrNamespace *lookupType(const Name *name);
68 ClassOrNamespace *findType(const Name *name);
75 ClassOrNamespace *findOrCreateType(const Name *name);
77 void addTodo(Symbol *symbol);
78 void addSymbol(Symbol *symbol);
79 void addEnum(Enum *e);
80 void addUsing(ClassOrNamespace *u);
81 void addNestedType(const Name *alias, ClassOrNamespace *e);
83 QList<LookupItem> lookup_helper(const Name *name, bool searchInEnclosingScope);
85 void lookup_helper(const Name *name, ClassOrNamespace *binding,
86 QList<LookupItem> *result,
87 QSet<ClassOrNamespace *> *processed,
88 const TemplateNameId *templateId);
90 ClassOrNamespace *lookupType_helper(const Name *name, QSet<ClassOrNamespace *> *processed,
91 bool searchInEnclosingScope);
93 ClassOrNamespace *nestedType(const Name *name) const;
96 struct CompareName: std::binary_function<const Name *, const Name *, bool> {
97 bool operator()(const Name *name, const Name *other) const;
101 typedef std::map<const Name *, ClassOrNamespace *, CompareName> Table;
102 CreateBindings *_factory;
103 ClassOrNamespace *_parent;
104 QList<Symbol *> _symbols;
105 QList<ClassOrNamespace *> _usings;
106 Table _classOrNamespaces;
107 QList<Enum *> _enums;
108 QList<Symbol *> _todo;
110 // it's an instantiation.
111 const TemplateNameId *_templateId;
113 friend class CreateBindings;
116 class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
118 Q_DISABLE_COPY(CreateBindings)
121 CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control);
122 virtual ~CreateBindings();
124 /// Returns the binding for the global namespace.
125 ClassOrNamespace *globalNamespace() const;
127 /// Finds the binding associated to the given symbol.
128 ClassOrNamespace *lookupType(Symbol *symbol);
129 ClassOrNamespace *lookupType(const QList<const Name *> &path);
131 /// Returns the Control that must be used to create temporary symbols.
133 QSharedPointer<Control> control() const;
135 /// Searches in \a scope for symbols with the given \a name.
136 /// Store the result in \a results.
138 void lookupInScope(const Name *name, Scope *scope, QList<LookupItem> *result,
139 const TemplateNameId *templateId, ClassOrNamespace *binding);
141 /// Create bindings for the symbols reachable from \a rootSymbol.
143 void process(Symbol *rootSymbol, ClassOrNamespace *classOrNamespace);
145 /// Create an empty ClassOrNamespace binding with the given \a parent.
147 ClassOrNamespace *allocClassOrNamespace(ClassOrNamespace *parent);
150 using SymbolVisitor::visit;
152 /// Change the current ClassOrNamespace binding.
153 ClassOrNamespace *switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace);
155 /// Enters the ClassOrNamespace binding associated with the given \a symbol.
156 ClassOrNamespace *enterClassOrNamespaceBinding(Symbol *symbol);
158 /// Enters a ClassOrNamespace binding for the given \a symbol in the global
159 /// namespace binding.
160 ClassOrNamespace *enterGlobalClassOrNamespace(Symbol *symbol);
162 /// Creates bindings for the given \a document.
163 void process(Document::Ptr document);
165 /// Creates bindings for the symbols reachable from the \a root symbol.
166 void process(Symbol *root);
168 virtual bool visit(Template *templ);
169 virtual bool visit(Namespace *ns);
170 virtual bool visit(Class *klass);
171 virtual bool visit(ForwardClassDeclaration *klass);
172 virtual bool visit(Enum *e);
173 virtual bool visit(Declaration *decl);
174 virtual bool visit(Function *);
175 virtual bool visit(BaseClass *b);
176 virtual bool visit(UsingNamespaceDirective *u);
177 virtual bool visit(UsingDeclaration *u);
178 virtual bool visit(NamespaceAlias *a);
180 virtual bool visit(ObjCClass *klass);
181 virtual bool visit(ObjCBaseClass *b);
182 virtual bool visit(ObjCForwardClassDeclaration *klass);
183 virtual bool visit(ObjCProtocol *proto);
184 virtual bool visit(ObjCBaseProtocol *b);
185 virtual bool visit(ObjCForwardProtocolDeclaration *proto);
186 virtual bool visit(ObjCMethod *);
190 QSharedPointer<Control> _control;
191 QSet<Namespace *> _processed;
192 QList<ClassOrNamespace *> _entities;
193 ClassOrNamespace *_globalNamespace;
194 ClassOrNamespace *_currentClassOrNamespace;
197 class CPLUSPLUS_EXPORT LookupContext
202 LookupContext(Document::Ptr thisDocument,
203 const Snapshot &snapshot);
205 LookupContext(Document::Ptr expressionDocument,
206 Document::Ptr thisDocument,
207 const Snapshot &snapshot);
209 LookupContext(const LookupContext &other);
210 LookupContext &operator = (const LookupContext &other);
212 Document::Ptr expressionDocument() const;
213 Document::Ptr thisDocument() const;
214 Document::Ptr document(const QString &fileName) const;
215 Snapshot snapshot() const;
217 ClassOrNamespace *globalNamespace() const;
219 QList<LookupItem> lookup(const Name *name, Scope *scope) const;
220 ClassOrNamespace *lookupType(const Name *name, Scope *scope) const;
221 ClassOrNamespace *lookupType(Symbol *symbol) const;
222 ClassOrNamespace *lookupParent(Symbol *symbol) const;
225 QSharedPointer<CreateBindings> bindings() const;
228 void setBindings(QSharedPointer<CreateBindings> bindings);
230 QSharedPointer<Control> control() const; // ### deprecate
232 static QList<const Name *> fullyQualifiedName(Symbol *symbol);
233 static QList<const Name *> path(Symbol *symbol);
235 const Name *minimalName(const Name *name, Scope *source,
236 ClassOrNamespace *target) const;
239 // The current expression.
240 Document::Ptr _expressionDocument;
242 // The current document.
243 Document::Ptr _thisDocument;
249 mutable QSharedPointer<CreateBindings> _bindings;
251 QSharedPointer<Control> _control;
254 } // namespace CPlusPlus
256 #endif // CPLUSPLUS_LOOKUPCONTEXT_H