• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdevelop API Reference
  • KDE Home
  • Contact Us
 

kdevelop/kdevplatform/language/codegen

  • extragear
  • kdevelop
  • kdevelop
  • kdevplatform
  • language
  • codegen
codegenerator.h
Go to the documentation of this file.
1 /*
2  This library is free software; you can redistribute it and/or
3  modify it under the terms of the GNU Library General Public
4  License version 2 as published by the Free Software Foundation.
5 
6  This library is distributed in the hope that it will be useful,
7  but WITHOUT ANY WARRANTY; without even the implied warranty of
8  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
9  Library General Public License for more details.
10 
11  You should have received a copy of the GNU Library General Public License
12  along with this library; see the file COPYING.LIB. If not, write to
13  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
14  Boston, MA 02110-1301, USA.
15  */
16 
17 #ifndef KDEVPLATFORM_CODEGENERATOR_H
18 #define KDEVPLATFORM_CODEGENERATOR_H
19 
20 #include <serialization/indexedstring.h>
21 #include "../duchain/topducontext.h"
22 #include "../duchain/duchain.h"
23 
24 #include "language/interfaces/iastcontainer.h"
25 #include <debug.h>
26 
27 namespace KDevelop {
28 template <class AstNode>
29 class AstChangeSet;
30 
31 class DUContext;
32 class DUChainChangeSet;
33 class DocumentChangeSet;
34 class DocumentRange;
35 class CodeGeneratorBasePrivate;
36 
52 class KDEVPLATFORMLANGUAGE_EXPORT CodeGeneratorBase
53 {
54 public:
55  CodeGeneratorBase();
56  virtual ~CodeGeneratorBase();
57 
58  enum State {
59  Precondition,
60  UserInput,
61  Processing,
62  Review,
63  Finished
64  };
65 
72  virtual bool checkPreconditions(DUContext* context, const DocumentRange& position) = 0;
73 
80  virtual bool gatherInformation() = 0;
81 
88  virtual bool process() = 0;
89 
90  const QString& errorText() const;
91 
92  // Implementation from kJob
93  bool execute();
94 
103  void autoGenerate(DUContext* context, const DocumentRange* range);
104 
108  DocumentChangeSet& documentChangeSet();
109 
110 protected:
111 
118  void addChangeSet(DUChainChangeSet* duChainChange);
119 
120  void addChangeSet(DocumentChangeSet& docChangeSet);
121 
125  void setErrorText(const QString& error);
126 
130  bool autoGeneration() const;
131 
135  void clearChangeSets();
136 
137 private:
138  bool displayChanges();
139 
140 private:
141  const QScopedPointer<class CodeGeneratorBasePrivate> d_ptr;
142  Q_DECLARE_PRIVATE(CodeGeneratorBase)
143 };
144 
155 template <typename AstContainer>
156 class CodeGenerator
157  : public CodeGeneratorBase
158 {
159 public:
160  ~CodeGenerator()
161  {
162  clearChangeSets();
163  }
164 
165 protected:
166 
168  using TopAstNode = typename AstContainer::TopAstNode ;
169  using LanguageChangeSet = AstChangeSet<TopAstNode>;
170 
174  TopAstNode* ast(const IndexedString& file)
175  {
176  return astContainer(file)->topAstNode();
177  }
178 
179  TopAstNode* ast(const TopDUContext& context)
180  {
181  return astContainer(context)->topAstNode();
182  }
183 
184  typename AstContainer::Ptr astContainer(const IndexedString& file)
185  {
186  if (!m_AstContainers.contains(file)) {
187  qCDebug(LANGUAGE) << "Ast requested for: " << file.str();
188 
189  TopDUContext* context = DUChain::self()->waitForUpdate(file, KDevelop::TopDUContext::AST).data();
190 
191  Q_ASSERT(context);
192  m_AstContainers[file] = AstContainer::Ptr::template staticCast<IAstContainer>(context->ast());
193  }
194 
195  return m_AstContainers[file];
196  }
197 
198  typename AstContainer::Ptr astContainer(const TopDUContext& context)
199  {
200  return astContainer(context.url());
201  }
202 
208  void addChangeSet(DUChainChangeSet* duChainChange)
209  {
210  CodeGeneratorBase::addChangeSet(duChainChange);
211  }
212 
213  void addChangeSet(DocumentChangeSet& doc)
214  {
215  CodeGeneratorBase::addChangeSet(doc);
216  }
217 
223  void addChangeSet(LanguageChangeSet* astChange);
224 
225  void clearChangeSets()
226  {
227  CodeGeneratorBase::clearChangeSets();
228  }
229 
233  bool autoGeneration() const
234  {
235  return CodeGeneratorBase::autoGeneration();
236  }
237 
241  void setErrorText(const QString& error)
242  {
243  CodeGeneratorBase::setErrorText(error);
244  }
245 
246 private:
247  using AstContainerMap = QMap<IndexedString, typename AstContainer::Ptr>;
248  AstContainerMap m_AstContainers;
249 };
250 }
251 
252 #endif // KDEVPLATFORM_CODEGENERATOR_H
KDevelop::CodeGeneratorBase::clearChangeSets
void clearChangeSets()
Clean up all the change sets that this generator is in charge of.
Definition: codegenerator.cpp:122
KDevelop::CodeGeneratorBase::setErrorText
void setErrorText(const QString &error)
Accessor for KJob's KJob::setErrorText.
Definition: codegenerator.cpp:115
KDevelop::CodeGeneratorBase::Precondition
Definition: codegenerator.h:59
QMap::contains
bool contains(const Key &key) const
KDevelop::CodeGeneratorBase::Processing
Definition: codegenerator.h:61
QMap< IndexedString, typename AstContainer::Ptr >
KDevelop::CodeGenerator::addChangeSet
void addChangeSet(DocumentChangeSet &doc)
Definition: codegenerator.h:213
KDevelop::CodeGenerator
Base class for Ast aware code generators.
Definition: codegenerator.h:156
KDevelop::CodeGeneratorBase::State
State
Definition: codegenerator.h:58
KDevelop::CodeGeneratorBase
Base class for generic code generators.
Definition: codegenerator.h:52
KDevelop::CodeGenerator::LanguageChangeSet
AstChangeSet< TopAstNode > LanguageChangeSet
Definition: codegenerator.h:169
KDevelop::DocumentChangeSet
Object representing an arbitrary set of changes to an arbitrary set of files that can be applied atom...
Definition: documentchangeset.h:64
KDevelop::CodeGeneratorBase::addChangeSet
void addChangeSet(DUChainChangeSet *duChainChange)
Generate text edits from duchain change set.
Definition: codegenerator.cpp:71
KDevelop::AstChangeSet
A set of changes to an AST.
Definition: astchangeset.h:26
QScopedPointer< class CodeGeneratorBasePrivate >
KDevelop::CodeGeneratorBase::UserInput
Definition: codegenerator.h:60
KDevelop::CodeGenerator::astContainer
AstContainer::Ptr astContainer(const TopDUContext &context)
Definition: codegenerator.h:198
QString
KDevelop::CodeGenerator::ast
TopAstNode * ast(const IndexedString &file)
Query an AST of a particular file.
Definition: codegenerator.h:174
KDevelop::CodeGenerator::TopAstNode
typename AstContainer::TopAstNode TopAstNode
Convenience definition of the TopAstNode that is contained by this AstContainer.
Definition: codegenerator.h:168
KDevelop::CodeGenerator::autoGeneration
bool autoGeneration() const
Inform the derived class if this generation is being performed without user interaction.
Definition: codegenerator.h:233
KDevelop::CodeGenerator::ast
TopAstNode * ast(const TopDUContext &context)
Definition: codegenerator.h:179
KDevelop::CodeGenerator::clearChangeSets
void clearChangeSets()
Definition: codegenerator.h:225
KDevelop::CodeGeneratorBase::autoGeneration
bool autoGeneration() const
Inform the derived class if this generation is being performed without user interaction.
Definition: codegenerator.cpp:108
KDevelop::CodeGenerator::setErrorText
void setErrorText(const QString &error)
Accessor for KJob's KJob::setErrorText.
Definition: codegenerator.h:241
KDevelop::CodeGenerator::addChangeSet
void addChangeSet(DUChainChangeSet *duChainChange)
Generate text edits from duchain / ast change set.
Definition: codegenerator.h:208
KDevelop::CodeGenerator::~CodeGenerator
~CodeGenerator()
Definition: codegenerator.h:160
KDevelop::DUChainChangeSet
A set of changes to a DUChain.
Definition: duchainchangeset.h:166
KDevelop::CodeGeneratorBase::Review
Definition: codegenerator.h:62
KDevelop::CodeGenerator::astContainer
AstContainer::Ptr astContainer(const IndexedString &file)
Definition: codegenerator.h:184
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Thu Dec 12 2019 03:33:20 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevelop/kdevplatform/language/codegen

Skip menu "kdevelop/kdevplatform/language/codegen"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdevelop API Reference

Skip menu "kdevelop API Reference"
  •   kdevplatform
  •     debugger
  •     documentation
  •     interfaces
  •     language
  •       assistant
  •       backgroundparser
  •       checks
  •       classmodel
  •       codecompletion
  •       codegen
  •       duchain
  •       editor
  •       highlighting
  •       interfaces
  •       util
  •     outputview
  •     project
  •     serialization
  •     shell
  •     sublime
  •     tests
  •     util
  •     vcs

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal