LookupContext.h 8.59 KB
Newer Older
hjk's avatar
hjk committed
1
/****************************************************************************
2
**
Eike Ziller's avatar
Eike Ziller committed
3
4
** Copyright (C) 2015 The Qt Company Ltd.
** Contact: http://www.qt.io/licensing
5
**
hjk's avatar
hjk committed
6
** This file is part of Qt Creator.
7
**
hjk's avatar
hjk committed
8
9
10
11
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
Eike Ziller's avatar
Eike Ziller committed
12
13
** a written agreement between you and The Qt Company.  For licensing terms and
** conditions see http://www.qt.io/terms-conditions.  For further information
Eike Ziller's avatar
Eike Ziller committed
14
** use the contact form at http://www.qt.io/contact-us.
15
16
**
** GNU Lesser General Public License Usage
hjk's avatar
hjk committed
17
** Alternatively, this file may be used under the terms of the GNU Lesser
Eike Ziller's avatar
Eike Ziller committed
18
19
20
21
22
23
** General Public License version 2.1 or version 3 as published by the Free
** Software Foundation and appearing in the file LICENSE.LGPLv21 and
** LICENSE.LGPLv3 included in the packaging of this file.  Please review the
** following information to ensure the GNU Lesser General Public License
** requirements will be met: https://www.gnu.org/licenses/lgpl.html and
** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
hjk's avatar
hjk committed
24
**
Eike Ziller's avatar
Eike Ziller committed
25
26
** In addition, as a special exception, The Qt Company gives you certain additional
** rights.  These rights are described in The Qt Company LGPL Exception
con's avatar
con committed
27
28
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
**
hjk's avatar
hjk committed
29
****************************************************************************/
30
31
32
33
34
35

#ifndef CPLUSPLUS_LOOKUPCONTEXT_H
#define CPLUSPLUS_LOOKUPCONTEXT_H

#include "CppDocument.h"
#include "LookupItem.h"
36
#include "AlreadyConsideredClassContainer.h"
37
38
39
40
41
42
43

#include <cplusplus/FullySpecifiedType.h>
#include <cplusplus/Type.h>
#include <cplusplus/SymbolVisitor.h>
#include <cplusplus/Control.h>
#include <cplusplus/Name.h>

44
#include <QSet>
45
46
#include <QMap>

47
48
#include <map>
#include <functional>
49
50
51

namespace CPlusPlus {

52
53
54
55
56
57
58
59
60
namespace Internal {
struct FullyQualifiedName
{
    QList<const Name *> fqn;

    FullyQualifiedName(const QList<const Name *> &fqn)
        : fqn(fqn)
    {}
};
61
class LookupScopePrivate;
Orgad Shaneh's avatar
Orgad Shaneh committed
62
class Instantiator;
63
64
} // namespace Internal;

65
66
class CreateBindings;

67
class CPLUSPLUS_EXPORT LookupScope
68
{
69
    Q_DISABLE_COPY(LookupScope)
70

71
    LookupScope(CreateBindings *factory, LookupScope *parent);
72
73

public:
74
    ~LookupScope();
75

76
    LookupScope *instantiationOrigin() const;
77

78
79
    LookupScope *parent() const;
    QList<LookupScope *> usings() const;
80
    QList<Enum *> unscopedEnums() const;
81
82
    QList<Symbol *> symbols() const;

83
84
    QList<LookupItem> lookup(const Name *name);
    QList<LookupItem> find(const Name *name);
85

86
87
88
89
    LookupScope *lookupType(const Name *name);
    LookupScope *lookupType(const Name *name, Block *block);
    LookupScope *findType(const Name *name);
    LookupScope *findBlock(Block *block);
90

91
    /// The class this LookupScope is based on.
Orgad Shaneh's avatar
Orgad Shaneh committed
92
    Class *rootClass() const;
93

94
private:
95
    Internal::LookupScopePrivate *d;
96

97
    friend class Internal::LookupScopePrivate;
Orgad Shaneh's avatar
Orgad Shaneh committed
98
    friend class Internal::Instantiator;
99
100
101
102
103
104
105
106
    friend class CreateBindings;
};

class CPLUSPLUS_EXPORT CreateBindings: protected SymbolVisitor
{
    Q_DISABLE_COPY(CreateBindings)

public:
107
    CreateBindings(Document::Ptr thisDocument, const Snapshot &snapshot);
108
109
    virtual ~CreateBindings();

110
    /// Returns the binding for the global namespace.
111
    LookupScope *globalNamespace() const;
112

113
    /// Finds the binding associated to the given symbol.
114
115
    LookupScope *lookupType(Symbol *symbol, LookupScope *enclosingBinding = 0);
    LookupScope *lookupType(const QList<const Name *> &path, LookupScope *enclosingBinding = 0);
116
117

    /// Returns the Control that must be used to create temporary symbols.
118
    /// \internal
119
120
    QSharedPointer<Control> control() const
    { return _control; }
121

122
123
124
125
126
    bool expandTemplates() const
    { return _expandTemplates; }
    void setExpandTemplates(bool expandTemplates)
    { _expandTemplates = expandTemplates; }

127
128
    /// Searches in \a scope for symbols with the given \a name.
    /// Store the result in \a results.
129
    /// \internal
130
    void lookupInScope(const Name *name, Scope *scope, QList<LookupItem> *result,
131
                       LookupScope *binding = 0);
132

133
    /// Create bindings for the symbols reachable from \a rootSymbol.
134
    /// \internal
135
    void process(Symbol *rootSymbol, LookupScope *lookupScope);
136

137
    /// Create an empty LookupScope binding with the given \a parent.
138
    /// \internal
139
    LookupScope *allocLookupScope(LookupScope *parent);
140

141
142
143
protected:
    using SymbolVisitor::visit;

144
145
    /// Change the current LookupScope binding.
    LookupScope *switchCurrentLookupScope(LookupScope *lookupScope);
146

147
148
    /// Enters the LookupScope binding associated with the given \a symbol.
    LookupScope *enterLookupScopeBinding(Symbol *symbol);
149

150
    /// Enters a LookupScope binding for the given \a symbol in the global
151
    /// namespace binding.
152
    LookupScope *enterGlobalLookupScope(Symbol *symbol);
153
154
155
156
157
158

    /// 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);
159

160
    virtual bool visit(Template *templ);
161
162
163
164
165
    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);
166
167
168
    virtual bool visit(Function *function);
    virtual bool visit(Block *block);

169
170
    virtual bool visit(BaseClass *b);
    virtual bool visit(UsingNamespaceDirective *u);
Roberto Raggi's avatar
Roberto Raggi committed
171
    virtual bool visit(UsingDeclaration *u);
172
173
174
175
176
177
178
179
180
181
182
    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:
183
184
185
    Symbol *instantiateTemplateFunction(const TemplateNameId *instantiation,
                                        Template *specialization) const;

186
    Snapshot _snapshot;
187
    QSharedPointer<Control> _control;
188
    QSet<Namespace *> _processed;
189
190
191
    QList<LookupScope *> _entities;
    LookupScope *_globalNamespace;
    LookupScope *_currentLookupScope;
192
    bool _expandTemplates;
193
194
195
196
197
198
199
200
201
202
203
204
};

class CPLUSPLUS_EXPORT LookupContext
{
public:
    LookupContext();

    LookupContext(Document::Ptr thisDocument,
                  const Snapshot &snapshot);

    LookupContext(Document::Ptr expressionDocument,
                  Document::Ptr thisDocument,
205
206
                  const Snapshot &snapshot,
                  QSharedPointer<CreateBindings> bindings = QSharedPointer<CreateBindings>());
207
208
209
210
211
212
213
214
215

    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;

216
    LookupScope *globalNamespace() const;
217

218
    QList<LookupItem> lookup(const Name *name, Scope *scope) const;
219
220
    LookupScope *lookupType(const Name *name, Scope *scope,
                                 LookupScope *enclosingBinding = 0,
221
222
                                 QSet<const Declaration *> typedefsBeingResolved
                                    = QSet<const Declaration *>()) const;
223
224
225
    LookupScope *lookupType(Symbol *symbol,
                                 LookupScope *enclosingBinding = 0) const;
    LookupScope *lookupParent(Symbol *symbol) const;
226
227

    /// \internal
228
229
    QSharedPointer<CreateBindings> bindings() const
    { return _bindings; }
230

231
    static QList<const Name *> fullyQualifiedName(Symbol *symbol);
232
    static QList<const Name *> path(Symbol *symbol);
233

234
    static const Name *minimalName(Symbol *symbol, LookupScope *target, Control *control);
235

236
    void setExpandTemplates(bool expandTemplates)
237
238
239
240
241
    {
        if (_bindings)
            _bindings->setExpandTemplates(expandTemplates);
        m_expandTemplates = expandTemplates;
    }
242

243
private:
244
    QList<LookupItem> lookupByUsing(const Name *name, LookupScope *bindingScope) const;
245

246
247
248
249
250
251
252
253
254
255
    // The current expression.
    Document::Ptr _expressionDocument;

    // The current document.
    Document::Ptr _thisDocument;

    // All documents.
    Snapshot _snapshot;

    // Bindings
256
    QSharedPointer<CreateBindings> _bindings;
257

258
    bool m_expandTemplates;
259
260
};

261
262
263
264
bool CPLUSPLUS_EXPORT compareFullyQualifiedName(const QList<const Name *> &path,
                                                const QList<const Name *> &other);


265
} // namespace CPlusPlus
266
267

#endif // CPLUSPLUS_LOOKUPCONTEXT_H