1 /**************************************************************************
3 ** This file is part of Qt Creator
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
11 ** This file contains pre-release code and may not be distributed.
12 ** You may use this file in accordance with the terms and conditions
13 ** contained in the Technology Preview License Agreement accompanying
16 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights. These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
32 **************************************************************************/
34 #ifndef CPLUSPLUS_LOOKUPCONTEXT_H
35 #define CPLUSPLUS_LOOKUPCONTEXT_H
37 #include "CppDocument.h"
38 #include "LookupItem.h"
39 #include <FullySpecifiedType.h>
41 #include <SymbolVisitor.h>
44 #include <QtCore/QSet>
52 class CPLUSPLUS_EXPORT ClassOrNamespace
55 ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent);
57 const TemplateNameId *templateId() const;
58 ClassOrNamespace *parent() const;
59 QList<ClassOrNamespace *> usings() const;
60 QList<Enum *> enums() const;
61 QList<Symbol *> symbols() const;
63 ClassOrNamespace *globalNamespace() const;
65 QList<LookupItem> lookup(const Name *name);
66 QList<LookupItem> find(const Name *name);
68 ClassOrNamespace *lookupType(const Name *name);
69 ClassOrNamespace *findType(const Name *name);
76 ClassOrNamespace *findOrCreateType(const Name *name);
78 void addTodo(Symbol *symbol);
79 void addSymbol(Symbol *symbol);
80 void addEnum(Enum *e);
81 void addUsing(ClassOrNamespace *u);
82 void addNestedType(const Name *alias, ClassOrNamespace *e);
84 QList<LookupItem> lookup_helper(const Name *name, bool searchInEnclosingScope);
86 void lookup_helper(const Name *name, ClassOrNamespace *binding,
87 QList<LookupItem> *result,
88 QSet<ClassOrNamespace *> *processed,
89 const TemplateNameId *templateId);
91 ClassOrNamespace *lookupType_helper(const Name *name, QSet<ClassOrNamespace *> *processed,
92 bool searchInEnclosingScope);
94 ClassOrNamespace *nestedType(const Name *name) const;
97 struct CompareName: std::binary_function<const Name *, const Name *, bool> {
98 bool operator()(const Name *name, const Name *other) const;
102 typedef std::map<const Name *, ClassOrNamespace *, CompareName> Table;
103 CreateBindings *_factory;
104 ClassOrNamespace *_parent;
105 QList<Symbol *> _symbols;
106 QList<ClassOrNamespace *> _usings;
107 Table _classOrNamespaces;
108 QList<Enum *> _enums;
109 QList<Symbol *> _todo;
111 // it's an instantiation.
112 const TemplateNameId *_templateId;
114 friend class CreateBindings;
117 class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
119 Q_DISABLE_COPY(CreateBindings)
122 CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control);
123 virtual ~CreateBindings();
125 /// Returns the binding for the global namespace.
126 ClassOrNamespace *globalNamespace() const;
128 /// Finds the binding associated to the given symbol.
129 ClassOrNamespace *lookupType(Symbol *symbol);
130 ClassOrNamespace *lookupType(const QList<const Name *> &path);
132 /// Returns the Control that must be used to create temporary symbols.
134 QSharedPointer<Control> control() const;
136 /// Searches in \a scope for symbols with the given \a name.
137 /// Store the result in \a results.
139 void lookupInScope(const Name *name, Scope *scope, QList<LookupItem> *result,
140 const TemplateNameId *templateId, ClassOrNamespace *binding);
142 /// Create bindings for the symbols reachable from \a rootSymbol.
144 void process(Symbol *rootSymbol, ClassOrNamespace *classOrNamespace);
146 /// Create an empty ClassOrNamespace binding with the given \a parent.
148 ClassOrNamespace *allocClassOrNamespace(ClassOrNamespace *parent);
151 using SymbolVisitor::visit;
153 /// Change the current ClassOrNamespace binding.
154 ClassOrNamespace *switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace);
156 /// Enters the ClassOrNamespace binding associated with the given \a symbol.
157 ClassOrNamespace *enterClassOrNamespaceBinding(Symbol *symbol);
159 /// Enters a ClassOrNamespace binding for the given \a symbol in the global
160 /// namespace binding.
161 ClassOrNamespace *enterGlobalClassOrNamespace(Symbol *symbol);
163 /// Creates bindings for the given \a document.
164 void process(Document::Ptr document);
166 /// Creates bindings for the symbols reachable from the \a root symbol.
167 void process(Symbol *root);
169 virtual bool visit(Template *templ);
170 virtual bool visit(Namespace *ns);
171 virtual bool visit(Class *klass);
172 virtual bool visit(ForwardClassDeclaration *klass);
173 virtual bool visit(Enum *e);
174 virtual bool visit(Declaration *decl);
175 virtual bool visit(Function *);
176 virtual bool visit(BaseClass *b);
177 virtual bool visit(UsingNamespaceDirective *u);
178 virtual bool visit(UsingDeclaration *u);
179 virtual bool visit(NamespaceAlias *a);
181 virtual bool visit(ObjCClass *klass);
182 virtual bool visit(ObjCBaseClass *b);
183 virtual bool visit(ObjCForwardClassDeclaration *klass);
184 virtual bool visit(ObjCProtocol *proto);
185 virtual bool visit(ObjCBaseProtocol *b);
186 virtual bool visit(ObjCForwardProtocolDeclaration *proto);
187 virtual bool visit(ObjCMethod *);
191 QSharedPointer<Control> _control;
192 QSet<Namespace *> _processed;
193 QList<ClassOrNamespace *> _entities;
194 ClassOrNamespace *_globalNamespace;
195 ClassOrNamespace *_currentClassOrNamespace;
198 class CPLUSPLUS_EXPORT LookupContext
203 LookupContext(Document::Ptr thisDocument,
204 const Snapshot &snapshot);
206 LookupContext(Document::Ptr expressionDocument,
207 Document::Ptr thisDocument,
208 const Snapshot &snapshot);
210 LookupContext(const LookupContext &other);
211 LookupContext &operator = (const LookupContext &other);
213 Document::Ptr expressionDocument() const;
214 Document::Ptr thisDocument() const;
215 Document::Ptr document(const QString &fileName) const;
216 Snapshot snapshot() const;
218 ClassOrNamespace *globalNamespace() const;
220 QList<LookupItem> lookup(const Name *name, Scope *scope) const;
221 ClassOrNamespace *lookupType(const Name *name, Scope *scope) const;
222 ClassOrNamespace *lookupType(Symbol *symbol) const;
223 ClassOrNamespace *lookupParent(Symbol *symbol) const;
226 QSharedPointer<CreateBindings> bindings() const;
229 void setBindings(QSharedPointer<CreateBindings> bindings);
231 QSharedPointer<Control> control() const; // ### deprecate
233 static QList<const Name *> fullyQualifiedName(Symbol *symbol);
234 static QList<const Name *> path(Symbol *symbol);
236 const Name *minimalName(const Name *name, Scope *source,
237 ClassOrNamespace *target) const;
240 // The current expression.
241 Document::Ptr _expressionDocument;
243 // The current document.
244 Document::Ptr _thisDocument;
250 mutable QSharedPointer<CreateBindings> _bindings;
252 QSharedPointer<Control> _control;
255 } // namespace CPlusPlus
257 #endif // CPLUSPLUS_LOOKUPCONTEXT_H