OSDN Git Service

3a26b521e07e38d2209bda9d3d2dcc5c1192e2f7
[qt-creator-jp/qt-creator-jp.git] / src / libs / cplusplus / LookupContext.h
1 /**************************************************************************
2 **
3 ** This file is part of Qt Creator
4 **
5 ** Copyright (c) 2011 Nokia Corporation and/or its subsidiary(-ies).
6 **
7 ** Contact: Nokia Corporation (qt-info@nokia.com)
8 **
9 ** No Commercial Usage
10 **
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
14 ** this package.
15 **
16 ** GNU Lesser General Public License Usage
17 **
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.
24 **
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.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **************************************************************************/
33
34 #ifndef CPLUSPLUS_LOOKUPCONTEXT_H
35 #define CPLUSPLUS_LOOKUPCONTEXT_H
36
37 #include "CppDocument.h"
38 #include "LookupItem.h"
39 #include <FullySpecifiedType.h>
40 #include <Type.h>
41 #include <SymbolVisitor.h>
42 #include <Control.h>
43 #include <Name.h>
44 #include <QtCore/QSet>
45 #include <map>
46 #include <functional>
47
48 namespace CPlusPlus {
49
50 class CreateBindings;
51
52 class CPLUSPLUS_EXPORT ClassOrNamespace
53 {
54 public:
55     ClassOrNamespace(CreateBindings *factory, ClassOrNamespace *parent);
56
57     const TemplateNameId *templateId() const;
58     ClassOrNamespace *parent() const;
59     QList<ClassOrNamespace *> usings() const;
60     QList<Enum *> enums() const;
61     QList<Symbol *> symbols() const;
62
63     ClassOrNamespace *globalNamespace() const;
64
65     QList<LookupItem> lookup(const Name *name);
66     QList<LookupItem> find(const Name *name);
67
68     ClassOrNamespace *lookupType(const Name *name);
69     ClassOrNamespace *findType(const Name *name);
70
71 private:
72     /// \internal
73     void flush();
74
75     /// \internal
76     ClassOrNamespace *findOrCreateType(const Name *name);
77
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);
83
84     QList<LookupItem> lookup_helper(const Name *name, bool searchInEnclosingScope);
85
86     void lookup_helper(const Name *name, ClassOrNamespace *binding,
87                        QList<LookupItem> *result,
88                        QSet<ClassOrNamespace *> *processed,
89                        const TemplateNameId *templateId);
90
91     ClassOrNamespace *lookupType_helper(const Name *name, QSet<ClassOrNamespace *> *processed,
92                                         bool searchInEnclosingScope);
93
94     ClassOrNamespace *nestedType(const Name *name) const;
95
96 private:
97     struct CompareName: std::binary_function<const Name *, const Name *, bool> {
98         bool operator()(const Name *name, const Name *other) const;
99     };
100
101 private:
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;
110
111     // it's an instantiation.
112     const TemplateNameId *_templateId;
113
114     friend class CreateBindings;
115 };
116
117 class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
118 {
119     Q_DISABLE_COPY(CreateBindings)
120
121 public:
122     CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot, QSharedPointer<Control> control);
123     virtual ~CreateBindings();
124
125     /// Returns the binding for the global namespace.
126     ClassOrNamespace *globalNamespace() const;
127
128     /// Finds the binding associated to the given symbol.
129     ClassOrNamespace *lookupType(Symbol *symbol);
130     ClassOrNamespace *lookupType(const QList<const Name *> &path);
131
132     /// Returns the Control that must be used to create temporary symbols.
133     /// \internal
134     QSharedPointer<Control> control() const;
135
136     /// Searches in \a scope for symbols with the given \a name.
137     /// Store the result in \a results.
138     /// \internal
139     void lookupInScope(const Name *name, Scope *scope, QList<LookupItem> *result,
140                             const TemplateNameId *templateId, ClassOrNamespace *binding);
141
142     /// Create bindings for the symbols reachable from \a rootSymbol.
143     /// \internal
144     void process(Symbol *rootSymbol, ClassOrNamespace *classOrNamespace);
145
146     /// Create an empty ClassOrNamespace binding with the given \a parent.
147     /// \internal
148     ClassOrNamespace *allocClassOrNamespace(ClassOrNamespace *parent);
149
150 protected:
151     using SymbolVisitor::visit;
152
153     /// Change the current ClassOrNamespace binding.
154     ClassOrNamespace *switchCurrentClassOrNamespace(ClassOrNamespace *classOrNamespace);
155
156     /// Enters the ClassOrNamespace binding associated with the given \a symbol.
157     ClassOrNamespace *enterClassOrNamespaceBinding(Symbol *symbol);
158
159     /// Enters a ClassOrNamespace binding for the given \a symbol in the global
160     /// namespace binding.
161     ClassOrNamespace *enterGlobalClassOrNamespace(Symbol *symbol);
162
163     /// Creates bindings for the given \a document.
164     void process(Document::Ptr document);
165
166     /// Creates bindings for the symbols reachable from the \a root symbol.
167     void process(Symbol *root);
168
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);
180
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 *);
188
189 private:
190     Snapshot _snapshot;
191     QSharedPointer<Control> _control;
192     QSet<Namespace *> _processed;
193     QList<ClassOrNamespace *> _entities;
194     ClassOrNamespace *_globalNamespace;
195     ClassOrNamespace *_currentClassOrNamespace;
196 };
197
198 class CPLUSPLUS_EXPORT LookupContext
199 {
200 public:
201     LookupContext();
202
203     LookupContext(Document::Ptr thisDocument,
204                   const Snapshot &snapshot);
205
206     LookupContext(Document::Ptr expressionDocument,
207                   Document::Ptr thisDocument,
208                   const Snapshot &snapshot);
209
210     LookupContext(const LookupContext &other);
211     LookupContext &operator = (const LookupContext &other);
212
213     Document::Ptr expressionDocument() const;
214     Document::Ptr thisDocument() const;
215     Document::Ptr document(const QString &fileName) const;
216     Snapshot snapshot() const;
217
218     ClassOrNamespace *globalNamespace() const;
219
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;
224
225     /// \internal
226     QSharedPointer<CreateBindings> bindings() const;
227
228     /// \internal
229     void setBindings(QSharedPointer<CreateBindings> bindings);
230
231     QSharedPointer<Control> control() const; // ### deprecate
232
233     static QList<const Name *> fullyQualifiedName(Symbol *symbol);
234     static QList<const Name *> path(Symbol *symbol);
235
236     const Name *minimalName(const Name *name, Scope *source,
237                             ClassOrNamespace *target) const;
238
239 private:
240     // The current expression.
241     Document::Ptr _expressionDocument;
242
243     // The current document.
244     Document::Ptr _thisDocument;
245
246     // All documents.
247     Snapshot _snapshot;
248
249     // Bindings
250     mutable QSharedPointer<CreateBindings> _bindings;
251
252     QSharedPointer<Control> _control;
253 };
254
255 } // namespace CPlusPlus
256
257 #endif // CPLUSPLUS_LOOKUPCONTEXT_H