1
/* This file is part of KDevelop
2
Copyright 2006 Roberto Raggi <roberto@kdevelop.org>
3
Copyright 2006-2008 Hamish Rodda <rodda@kde.org>
4
Copyright 2007-2008 David Nolden <david.nolden.kdevelop@art-master.de>
6
This library is free software; you can redistribute it and/or
7
modify it under the terms of the GNU Library General Public
8
License version 2 as published by the Free Software Foundation.
10
This library is distributed in the hope that it will be useful,
11
but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public License
16
along with this library; see the file COPYING.LIB. If not, write to
17
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
Boston, MA 02110-1301, USA.
21
#ifndef CONTEXTBUILDER_H
22
#define CONTEXTBUILDER_H
24
#include "default_visitor.h"
26
#include <QtCore/QSet>
28
#include "cppducontext.h"
30
#include <language/duchain/builders/abstractcontextbuilder.h>
31
#include <language/duchain/duchainpointer.h>
32
#include <language/duchain/duchainlock.h>
33
#include <language/duchain/identifier.h>
34
#include <language/duchain/ducontext.h>
35
#include <ksharedptr.h>
36
#include "cppduchainexport.h"
37
#include "cppeditorintegrator.h"
38
#include <language/duchain/classdeclaration.h>
39
#include "environmentmanager.h"
41
//Uncomment this to debug what happens to context ranges when new ones are inserted
42
//#define DEBUG_CONTEXT_RANGES
52
class CppEditorIntegrator;
56
namespace KTextEditor { class Range; }
59
class EnvironmentFile;
60
typedef KSharedPtr<EnvironmentFile> EnvironmentFilePointer;
63
struct KDEVCPPDUCHAIN_EXPORT LineContextPair {
64
LineContextPair( KDevelop::TopDUContext* _context, int _sourceLine ) : context(_context), sourceLine(_sourceLine), temporary(false) {
66
ReferencedTopDUContext context;
68
bool temporary; //When this flag is set, the import will be added in a special way that is faster
71
typedef QList<LineContextPair> IncludeFileList;
73
///@return Whether @param context is contained as a context in @param lineContexts
74
bool KDEVCPPDUCHAIN_EXPORT containsContext( const QList<LineContextPair>& lineContexts, TopDUContext* context );
75
///@return Whether a context in @param lineContexts imports the context @param context
76
bool KDEVCPPDUCHAIN_EXPORT importsContext( const QList<LineContextPair>& lineContexts, TopDUContext* context );
78
///Removes @param context from the list
79
void KDEVCPPDUCHAIN_EXPORT removeContext( QList<LineContextPair>& lineContexts, TopDUContext* context );
81
class ContextBuilderBase : public KDevelop::AbstractContextBuilder<AST, NameAST>
86
* A class which iterates the AST to identify contexts.
88
class KDEVCPPDUCHAIN_EXPORT ContextBuilder: public ContextBuilderBase, protected DefaultVisitor
90
friend class IdentifierVerifier;
94
ContextBuilder(ParseSession* session);
95
ContextBuilder(CppEditorIntegrator* editor);
96
virtual ~ContextBuilder ();
98
void setEditor(CppEditorIntegrator* editor, bool ownsEditorIntegrator);
101
* Builds or updates a proxy-context that represents a content-context under a different environment.
102
* The top-context is guaranteed to import "content" as first import, eventually all imports are cleared.
105
KDevelop::TopDUContext* buildProxyContextFromContent(Cpp::EnvironmentFilePointer file, const TopDUContextPointer& content, const TopDUContextPointer& updateContext);
108
* Compile either a context-definition chain, or add uses to an existing
111
* \param includes contexts to reference from the top context. The list may be changed by this function.
112
* \param removeOldImports Should old imports that are not in the includes-list be removed?
114
KDevelop::ReferencedTopDUContext buildContexts(Cpp::EnvironmentFilePointer file, AST *node, IncludeFileList* includes = 0, const ReferencedTopDUContext& updateContext = ReferencedTopDUContext(), bool removeOldImports = true);
117
* Build.an independent du-context based on a given parent-context. Such a context may be used for expression-parsing,
118
* but should be deleted as fast as possible because it keeps a reference to an independent context.
120
* Warning: the resulting context should be deleted some time. Before deleting it, the du-chain must be locked.
121
* Warning: The new context is added as a child to the parent-context.
122
* \param url A temporary url that can be used to identify this context @todo remove this
124
* \param parent Context that will be used as parent for this context
126
// KDevelop::DUContext* buildSubContexts(const HashedString& url, AST *node, KDevelop::DUContext* parent = 0);
128
inline CppEditorIntegrator* editor() const { return static_cast<CppEditorIntegrator*>(ContextBuilderBase::editor()); }
130
//If this flag is enabled, only publically visible declarations/contexts are computed
131
void setOnlyComputeVisible(bool onlyVisible);
132
//If this flag is enabled, declarations and contexts are computed in a very simplified form (without types, without template- or function-contexts, etc.)
133
void setComputeSimplified(bool simplified);
134
//If this flag is set, the top-context will be empty, without any contexts or declarations
135
void setComputeEmpty(bool empty);
137
void setMapAst(bool mapAst);
140
QualifiedIdentifier identifierForNode(NameAST* id);
141
void identifierForNode(NameAST* id, QualifiedIdentifier& target);
142
virtual void startVisiting( AST* node );
143
virtual void setContextOnNode( AST* node, DUContext* ctx );
144
virtual DUContext* contextFromNode( AST* node );
145
virtual KTextEditor::Range editorFindRange( AST* fromRange, AST* toRange );
146
virtual KTextEditor::Range editorFindRangeForContext( AST* fromRange, AST* toRange );
147
virtual DUContext* newContext(const SimpleRange& range);
150
* Compile an identifier for the specified NameAST \a id.
152
* \note this reference will only be valid until the next time the function
153
* is called, so you need to create a copy (store as non-reference).
154
* @param typeSpecifier a pointer that will eventually be filled with a type-specifier that can be found in the name(for example the return-type of a cast-operator)
155
* @param target Place where the identifier will be written.
157
void identifierForNode(NameAST* id, TypeSpecifierAST** typeSpecifier, QualifiedIdentifier& target);
159
virtual void addBaseType( KDevelop::BaseClassInstance base, BaseSpecifierAST *node );
161
///Open/close prefix contexts around the class specifier that make the qualified identifier
162
///of the class Declaration match, because Declarations have only unqualified names.
163
///The prefix-context will also import the context of the specific class-declaration, so the visibility matches.
164
///@param id should be the whole identifier. A prefix-context will only be created if it
165
///has more than 1 element.
166
void openPrefixContext(AST* ast, const QualifiedIdentifier& id, const SimpleCursor& pos);
167
void closePrefixContext(const QualifiedIdentifier& id);
169
//The returned context may be zero, while the identifier valid
170
QPair<DUContext*, QualifiedIdentifier> findPrefixContext(const QualifiedIdentifier& id, KDevelop::SimpleCursor pos);
172
// Split up visitors created for subclasses to use
173
/// Visits the type specifier and init declarator for a function.
174
virtual void visitFunctionDeclaration (FunctionDefinitionAST *);
175
virtual void visitPostSimpleDeclaration(SimpleDeclarationAST*);
177
virtual void visitTemplateDeclaration(TemplateDeclarationAST *);
179
// Normal overridden visitors
180
virtual void visitInitDeclarator(InitDeclaratorAST *node);
181
virtual void visitDeclarator(DeclaratorAST *node);
182
virtual void visitNamespace(NamespaceAST *);
183
virtual void visitEnumSpecifier(EnumSpecifierAST* node);
184
virtual void visitClassSpecifier(ClassSpecifierAST *);
185
virtual void visitTypedef(TypedefAST *);
186
virtual void visitFunctionDefinition(FunctionDefinitionAST *);
187
virtual void visitCompoundStatement(CompoundStatementAST *);
188
virtual void visitSimpleDeclaration(SimpleDeclarationAST *);
189
virtual void visitName(NameAST *);
190
virtual void visitUsing(UsingAST*);
191
virtual void visitExpressionOrDeclarationStatement(ExpressionOrDeclarationStatementAST*);
192
virtual void visitForStatement(ForStatementAST*);
193
virtual void visitIfStatement(IfStatementAST*);
194
virtual void visitDoStatement(DoStatementAST*);
195
virtual void visitTryBlockStatement(TryBlockStatementAST*);
196
virtual void visitCatchStatement(CatchStatementAST*);
197
virtual void createTypeForDeclarator(DeclaratorAST *node);
198
virtual void createTypeForInitializer(InitializerAST *node);
199
virtual void closeTypeForInitializer(InitializerAST *node);
200
virtual void closeTypeForDeclarator(DeclaratorAST *node);
201
virtual void visitParameterDeclarationClause(ParameterDeclarationClauseAST* node);
203
void queueImportedContext(DUContext* context) {
204
DUChainReadLocker lock(DUChain::lock());
205
m_importedParentContexts << DUContext::Import(context, currentContext());
208
virtual void setInSymbolTable(DUContext* context);
210
virtual void classContextOpened(ClassSpecifierAST *node, DUContext* context);
212
virtual void preVisitSimpleDeclaration(SimpleDeclarationAST *);
214
//DUChain must not be locked
215
void createUserProblem(AST* node, QString problem);
217
//Opens a context of size 0, starting at the given node
218
KDevelop::DUContext* openContextEmpty(AST* range, KDevelop::DUContext::ContextType type);
220
KDevelop::DUContext* openContextInternal(const KDevelop::SimpleRange& range, KDevelop::DUContext::ContextType type, const KDevelop::QualifiedIdentifier& identifier);
222
bool createContextIfNeeded(AST* node, const QVector<KDevelop::DUContext::Import>& importedParentContexts);
223
bool createContextIfNeeded(AST* node, KDevelop::DUContext* importedParentContext);
224
void addImportedContexts();
226
int templateDeclarationDepth() const {
227
return m_templateDeclarationDepth;
231
NameCompiler* m_nameCompiler;
233
bool m_inFunctionDefinition;
236
int m_templateDeclarationDepth;
237
uint m_typeSpecifierWithoutInitDeclarators; //Start-token of the last opened type-specifier without init-declarators (Only filled in specific cases)
239
QualifiedIdentifier m_openingFunctionBody; //Identifier of the currently opened function body, or empty.
241
bool m_onlyComputeVisible;
242
bool m_onlyComputeSimplified;
245
#ifdef DEBUG_CONTEXT_RANGES
247
QHash<KDevelop::DUContext*, KDevelop::SimpleRange> m_contextRanges;
250
QVector<KDevelop::DUContext::Import> m_importedParentContexts;
251
QStack< QVector<KDevelop::DUContext::Import> > m_tryParentContexts;
252
InitializerAST* m_currentInitializer;
254
/// AST - DUChain/Uses mapping variables
258
#endif // CONTEXTBUILDER_H