OSDN Git Service

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