~ubuntu-branches/debian/sid/kdevelop/sid

« back to all changes in this revision

Viewing changes to languages/cpp/cppduchain/contextbuilder.h

  • Committer: Bazaar Package Importer
  • Author(s): Jeremy Lainé
  • Date: 2010-05-05 07:21:55 UTC
  • mfrom: (1.2.3 upstream) (5.1.2 squeeze)
  • Revision ID: james.westby@ubuntu.com-20100505072155-h78lx19pu04sbhtn
Tags: 4:4.0.0-2
* Upload to unstable (Closes: #579947, #481832).
* Acknowledge obsolete NMU fixes (Closes: #562410, #546961).

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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>
 
5
 
 
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.
 
9
 
 
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.
 
14
 
 
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.
 
19
*/
 
20
 
 
21
#ifndef CONTEXTBUILDER_H
 
22
#define CONTEXTBUILDER_H
 
23
 
 
24
#include "default_visitor.h"
 
25
 
 
26
#include <QtCore/QSet>
 
27
 
 
28
#include "cppducontext.h"
 
29
 
 
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"
 
40
 
 
41
//Uncomment this to debug what happens to context ranges when new ones are inserted
 
42
//#define DEBUG_CONTEXT_RANGES
 
43
 
 
44
namespace KDevelop
 
45
{
 
46
class DUChain;
 
47
class DUChainBase;
 
48
class DUContext;
 
49
class TopDUContext;
 
50
}
 
51
 
 
52
class CppEditorIntegrator;
 
53
class ParseSession;
 
54
class NameCompiler;
 
55
 
 
56
namespace KTextEditor { class Range; }
 
57
 
 
58
namespace Cpp {
 
59
  class EnvironmentFile;
 
60
  typedef KSharedPtr<EnvironmentFile> EnvironmentFilePointer;
 
61
}
 
62
 
 
63
struct KDEVCPPDUCHAIN_EXPORT LineContextPair {
 
64
  LineContextPair( KDevelop::TopDUContext* _context, int _sourceLine ) : context(_context), sourceLine(_sourceLine), temporary(false) {
 
65
  }
 
66
  ReferencedTopDUContext context;
 
67
  int sourceLine;
 
68
  bool temporary; //When this flag is set, the import will be added in a special way that is faster
 
69
};
 
70
 
 
71
typedef QList<LineContextPair> IncludeFileList;
 
72
 
 
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 );
 
77
 
 
78
///Removes @param context from the list
 
79
void KDEVCPPDUCHAIN_EXPORT removeContext( QList<LineContextPair>& lineContexts, TopDUContext* context );
 
80
 
 
81
class ContextBuilderBase : public KDevelop::AbstractContextBuilder<AST, NameAST>
 
82
{
 
83
};
 
84
 
 
85
/**
 
86
 * A class which iterates the AST to identify contexts.
 
87
 */
 
88
class KDEVCPPDUCHAIN_EXPORT  ContextBuilder: public ContextBuilderBase, protected DefaultVisitor
 
89
{
 
90
  friend class IdentifierVerifier;
 
91
 
 
92
public:
 
93
  ContextBuilder();
 
94
  ContextBuilder(ParseSession* session);
 
95
  ContextBuilder(CppEditorIntegrator* editor);
 
96
  virtual ~ContextBuilder ();
 
97
 
 
98
  void setEditor(CppEditorIntegrator* editor, bool ownsEditorIntegrator);
 
99
 
 
100
  /**
 
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.
 
103
   * */
 
104
 
 
105
  KDevelop::TopDUContext* buildProxyContextFromContent(Cpp::EnvironmentFilePointer file, const TopDUContextPointer& content, const TopDUContextPointer& updateContext);
 
106
 
 
107
  /**
 
108
   * Compile either a context-definition chain, or add uses to an existing
 
109
   * chain.
 
110
   *
 
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?
 
113
   */
 
114
  KDevelop::ReferencedTopDUContext buildContexts(Cpp::EnvironmentFilePointer file, AST *node, IncludeFileList* includes = 0, const ReferencedTopDUContext& updateContext = ReferencedTopDUContext(), bool removeOldImports = true);
 
115
 
 
116
  /**
 
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.
 
119
   *
 
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
 
123
   *
 
124
   * \param parent Context that will be used as parent for this context
 
125
   */
 
126
//   KDevelop::DUContext* buildSubContexts(const HashedString& url, AST *node, KDevelop::DUContext* parent = 0);
 
127
 
 
128
  inline CppEditorIntegrator* editor() const { return static_cast<CppEditorIntegrator*>(ContextBuilderBase::editor()); }
 
129
 
 
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);
 
136
  
 
137
  void setMapAst(bool mapAst);
 
138
  
 
139
protected:
 
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);
 
148
  
 
149
  /**
 
150
   * Compile an identifier for the specified NameAST \a id.
 
151
   *
 
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.
 
156
   */
 
157
  void identifierForNode(NameAST* id, TypeSpecifierAST** typeSpecifier, QualifiedIdentifier& target);
 
158
 
 
159
  virtual void addBaseType( KDevelop::BaseClassInstance base, BaseSpecifierAST *node );
 
160
  
 
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);
 
168
  
 
169
  //The returned context may be zero, while the identifier valid
 
170
  QPair<DUContext*, QualifiedIdentifier> findPrefixContext(const QualifiedIdentifier& id, KDevelop::SimpleCursor pos);
 
171
  
 
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*);
 
176
 
 
177
  virtual void visitTemplateDeclaration(TemplateDeclarationAST *);
 
178
 
 
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);
 
202
 
 
203
  void queueImportedContext(DUContext* context) {
 
204
    DUChainReadLocker lock(DUChain::lock());
 
205
    m_importedParentContexts << DUContext::Import(context, currentContext());
 
206
  }
 
207
  
 
208
  virtual void setInSymbolTable(DUContext* context);
 
209
  
 
210
  virtual void classContextOpened(ClassSpecifierAST *node, DUContext* context);
 
211
  
 
212
  virtual void preVisitSimpleDeclaration(SimpleDeclarationAST *);
 
213
 
 
214
  //DUChain must not be locked
 
215
  void createUserProblem(AST* node, QString problem);
 
216
 
 
217
  //Opens a context of size 0, starting at the given node
 
218
  KDevelop::DUContext* openContextEmpty(AST* range, KDevelop::DUContext::ContextType type);
 
219
 
 
220
  KDevelop::DUContext* openContextInternal(const KDevelop::SimpleRange& range, KDevelop::DUContext::ContextType type, const KDevelop::QualifiedIdentifier& identifier);
 
221
 
 
222
  bool createContextIfNeeded(AST* node, const QVector<KDevelop::DUContext::Import>& importedParentContexts);
 
223
  bool createContextIfNeeded(AST* node, KDevelop::DUContext* importedParentContext);
 
224
  void addImportedContexts();
 
225
 
 
226
  int templateDeclarationDepth() const {
 
227
    return m_templateDeclarationDepth;
 
228
  }
 
229
 
 
230
  // Variables
 
231
  NameCompiler* m_nameCompiler;
 
232
 
 
233
  bool m_inFunctionDefinition;
 
234
  bool smart() const;
 
235
 
 
236
  int m_templateDeclarationDepth;
 
237
  uint m_typeSpecifierWithoutInitDeclarators; //Start-token of the last opened type-specifier without init-declarators (Only filled in specific cases)
 
238
 
 
239
  QualifiedIdentifier m_openingFunctionBody; //Identifier of the currently opened function body, or empty.
 
240
 
 
241
  bool m_onlyComputeVisible;
 
242
  bool m_onlyComputeSimplified;
 
243
  bool m_computeEmpty;
 
244
 
 
245
#ifdef DEBUG_CONTEXT_RANGES
 
246
  void checkRanges();
 
247
  QHash<KDevelop::DUContext*, KDevelop::SimpleRange> m_contextRanges;
 
248
#endif
 
249
 
 
250
  QVector<KDevelop::DUContext::Import> m_importedParentContexts;
 
251
  QStack< QVector<KDevelop::DUContext::Import> > m_tryParentContexts;
 
252
  InitializerAST* m_currentInitializer;
 
253
  
 
254
  /// AST - DUChain/Uses mapping variables
 
255
  bool m_mapAst;
 
256
};
 
257
 
 
258
#endif // CONTEXTBUILDER_H
 
259