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

kdevplatform/language/duchain

  • sources
  • kfour-appscomplete
  • kdevelop
  • kdevplatform
  • language
  • duchain
codemodel.cpp
Go to the documentation of this file.
1 /* This file is part of KDevelop
2  Copyright 2008 David Nolden <[email protected]>
3 
4  This library is free software; you can redistribute it and/or
5  modify it under the terms of the GNU Library General Public
6  License version 2 as published by the Free Software Foundation.
7 
8  This library is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11  Library General Public License for more details.
12 
13  You should have received a copy of the GNU Library General Public License
14  along with this library; see the file COPYING.LIB. If not, write to
15  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
16  Boston, MA 02110-1301, USA.
17  */
18 
19 #include "codemodel.h"
20 
21 #include "appendedlist.h"
22 #include <debug.h>
23 #include <serialization/itemrepository.h>
24 #include "identifier.h"
25 #include <serialization/indexedstring.h>
26 #include <serialization/referencecounting.h>
27 #include <util/embeddedfreetree.h>
28 
29 #define ifDebug(x)
30 
31 namespace KDevelop {
32 class CodeModelItemHandler
33 {
34 public:
35  static int leftChild(const CodeModelItem& m_data)
36  {
37  return ( int )m_data.referenceCount;
38  }
39  static void setLeftChild(CodeModelItem& m_data, int child)
40  {
41  m_data.referenceCount = ( uint )child;
42  }
43  static int rightChild(const CodeModelItem& m_data)
44  {
45  return ( int )m_data.uKind;
46  }
47  static void setRightChild(CodeModelItem& m_data, int child)
48  {
49  m_data.uKind = ( uint )child;
50  }
51  //Copies this item into the given one
52  static void copyTo(const CodeModelItem& m_data, CodeModelItem& data)
53  {
54  data = m_data;
55  }
56 
57  static void createFreeItem(CodeModelItem& data)
58  {
59  data = CodeModelItem();
60  data.referenceCount = (uint) - 1;
61  data.uKind = (uint) - 1;
62  }
63 
64  static bool isFree(const CodeModelItem& m_data)
65  {
66  return !m_data.id.isValid();
67  }
68 
69  static const CodeModelItem& data(const CodeModelItem& m_data)
70  {
71  return m_data;
72  }
73 
74  static bool equals(const CodeModelItem& m_data, const CodeModelItem& rhs)
75  {
76  return m_data.id == rhs.id;
77  }
78 };
79 
80 DEFINE_LIST_MEMBER_HASH(CodeModelRepositoryItem, items, CodeModelItem)
81 
82 class CodeModelRepositoryItem
83 {
84 public:
85  CodeModelRepositoryItem()
86  {
87  initializeAppendedLists();
88  }
89  CodeModelRepositoryItem(const CodeModelRepositoryItem& rhs, bool dynamic = true) : file(rhs.file)
90  , centralFreeItem(rhs.centralFreeItem)
91  {
92  initializeAppendedLists(dynamic);
93  copyListsFrom(rhs);
94  }
95 
96  ~CodeModelRepositoryItem()
97  {
98  freeAppendedLists();
99  }
100 
101  CodeModelRepositoryItem& operator=(const CodeModelRepositoryItem& rhs) = delete;
102 
103  unsigned int hash() const
104  {
105  //We only compare the declaration. This allows us implementing a map, although the item-repository
106  //originally represents a set.
107  return file.index();
108  }
109 
110  uint itemSize() const
111  {
112  return dynamicSize();
113  }
114 
115  uint classSize() const
116  {
117  return sizeof(CodeModelRepositoryItem);
118  }
119 
120  IndexedString file;
121  int centralFreeItem = -1;
122 
123  START_APPENDED_LISTS(CodeModelRepositoryItem);
124  APPENDED_LIST_FIRST(CodeModelRepositoryItem, CodeModelItem, items);
125  END_APPENDED_LISTS(CodeModelRepositoryItem, items);
126 };
127 
128 class CodeModelRequestItem
129 {
130 public:
131 
132  CodeModelRequestItem(const CodeModelRepositoryItem& item) : m_item(item)
133  {
134  }
135  enum {
136  AverageSize = 30 //This should be the approximate average size of an Item
137  };
138 
139  unsigned int hash() const
140  {
141  return m_item.hash();
142  }
143 
144  uint itemSize() const
145  {
146  return m_item.itemSize();
147  }
148 
149  void createItem(CodeModelRepositoryItem* item) const
150  {
151  Q_ASSERT(shouldDoDUChainReferenceCounting(item));
152  Q_ASSERT(shouldDoDUChainReferenceCounting(reinterpret_cast<char*>(item) + (itemSize() - 1)));
153  new (item) CodeModelRepositoryItem(m_item, false);
154  }
155 
156  static void destroy(CodeModelRepositoryItem* item, KDevelop::AbstractItemRepository&)
157  {
158  Q_ASSERT(shouldDoDUChainReferenceCounting(item));
159 // Q_ASSERT(shouldDoDUChainReferenceCounting(((char*)item) + (itemSize()-1)));
160  item->~CodeModelRepositoryItem();
161  }
162 
163  static bool persistent(const CodeModelRepositoryItem* item)
164  {
165  Q_UNUSED(item);
166  return true;
167  }
168 
169  bool equals(const CodeModelRepositoryItem* item) const
170  {
171  return m_item.file == item->file;
172  }
173 
174  const CodeModelRepositoryItem& m_item;
175 };
176 
177 class CodeModelPrivate
178 {
179 public:
180 
181  CodeModelPrivate() : m_repository(QStringLiteral("Code Model"))
182  {
183  }
184  //Maps declaration-ids to items
185  // mutable as things like findIndex are not const
186  mutable ItemRepository<CodeModelRepositoryItem, CodeModelRequestItem> m_repository;
187 };
188 
189 CodeModel::CodeModel()
190  : d_ptr(new CodeModelPrivate())
191 {
192 }
193 
194 CodeModel::~CodeModel() = default;
195 
196 void CodeModel::addItem(const IndexedString& file, const IndexedQualifiedIdentifier& id, CodeModelItem::Kind kind)
197 {
198  Q_D(CodeModel);
199 
200  ifDebug(qCDebug(LANGUAGE) << "addItem" << file.str() << id.identifier().toString() << id.index; )
201 
202  if (!id.isValid())
203  return;
204  CodeModelRepositoryItem item;
205  item.file = file;
206  CodeModelRequestItem request(item);
207 
208  uint index = d->m_repository.findIndex(item);
209 
210  CodeModelItem newItem;
211  newItem.id = id;
212  newItem.kind = kind;
213  newItem.referenceCount = 1;
214 
215  if (index) {
216  const CodeModelRepositoryItem* oldItem = d->m_repository.itemFromIndex(index);
217  EmbeddedTreeAlgorithms<CodeModelItem, CodeModelItemHandler> alg(oldItem->items(),
218  oldItem->itemsSize(), oldItem->centralFreeItem);
219 
220  int listIndex = alg.indexOf(newItem);
221 
222  QMutexLocker lock(d->m_repository.mutex());
223 
224  DynamicItem<CodeModelRepositoryItem, true> editableItem = d->m_repository.dynamicItemFromIndex(index);
225  auto* items = const_cast<CodeModelItem*>(editableItem->items());
226 
227  if (listIndex != -1) {
228  //Only update the reference-count
229  ++items[listIndex].referenceCount;
230  items[listIndex].kind = kind;
231  return;
232  } else {
233  //Add the item to the list
234  EmbeddedTreeAddItem<CodeModelItem, CodeModelItemHandler> add(items,
235  editableItem->itemsSize(), editableItem->centralFreeItem, newItem);
236 
237  if (add.newItemCount() != editableItem->itemsSize()) {
238  //The data needs to be transferred into a bigger list. That list is within "item".
239 
240  item.itemsList().resize(add.newItemCount());
241  add.transferData(item.itemsList().data(), item.itemsList().size(), &item.centralFreeItem);
242 
243  d->m_repository.deleteItem(index);
244  } else {
245  //We're fine: The item fits into the existing list.
246  return;
247  }
248  }
249  } else {
250  //We're creating a new index
251  item.itemsList().append(newItem);
252  }
253 
254  Q_ASSERT(!d->m_repository.findIndex(request));
255 
256  //This inserts the changed item
257  const uint newIndex = d->m_repository.index(request);
258  Q_UNUSED(newIndex);
259  ifDebug(qCDebug(LANGUAGE) << "new index" << newIndex; )
260 
261  Q_ASSERT(d->m_repository.findIndex(request));
262 }
263 
264 void CodeModel::updateItem(const IndexedString& file, const IndexedQualifiedIdentifier& id, CodeModelItem::Kind kind)
265 {
266  Q_D(CodeModel);
267 
268  ifDebug(qCDebug(LANGUAGE) << file.str() << id.identifier().toString() << kind; )
269 
270  if (!id.isValid())
271  return;
272 
273  CodeModelRepositoryItem item;
274  item.file = file;
275  CodeModelRequestItem request(item);
276 
277  CodeModelItem newItem;
278  newItem.id = id;
279  newItem.kind = kind;
280  newItem.referenceCount = 1;
281 
282  uint index = d->m_repository.findIndex(item);
283 
284  if (index) {
285  //Check whether the item is already in the mapped list, else copy the list into the new created item
286  QMutexLocker lock(d->m_repository.mutex());
287  DynamicItem<CodeModelRepositoryItem, true> oldItem = d->m_repository.dynamicItemFromIndex(index);
288 
289  EmbeddedTreeAlgorithms<CodeModelItem, CodeModelItemHandler> alg(oldItem->items(),
290  oldItem->itemsSize(), oldItem->centralFreeItem);
291  int listIndex = alg.indexOf(newItem);
292  Q_ASSERT(listIndex != -1);
293 
294  auto* items = const_cast<CodeModelItem*>(oldItem->items());
295 
296  Q_ASSERT(items[listIndex].id == id);
297  items[listIndex].kind = kind;
298 
299  return;
300  }
301 
302  Q_ASSERT(0); //The updated item as not in the symbol table!
303 }
304 
305 void CodeModel::removeItem(const IndexedString& file, const IndexedQualifiedIdentifier& id)
306 //void CodeModel::removeDeclaration(const QualifiedIdentifier& id, const IndexedDeclaration& declaration)
307 {
308  Q_D(CodeModel);
309 
310  if (!id.isValid())
311  return;
312 
313  ifDebug(qCDebug(LANGUAGE) << "removeItem" << file.str() << id.identifier().toString(); )
314  CodeModelRepositoryItem item;
315  item.file = file;
316  CodeModelRequestItem request(item);
317 
318  uint index = d->m_repository.findIndex(item);
319 
320  if (index) {
321  CodeModelItem searchItem;
322  searchItem.id = id;
323 
324  QMutexLocker lock(d->m_repository.mutex());
325  DynamicItem<CodeModelRepositoryItem, true> oldItem = d->m_repository.dynamicItemFromIndex(index);
326 
327  EmbeddedTreeAlgorithms<CodeModelItem, CodeModelItemHandler> alg(oldItem->items(),
328  oldItem->itemsSize(), oldItem->centralFreeItem);
329 
330  int listIndex = alg.indexOf(searchItem);
331  if (listIndex == -1)
332  return;
333 
334  auto* items = const_cast<CodeModelItem*>(oldItem->items());
335 
336  --items[listIndex].referenceCount;
337 
338  if (oldItem->items()[listIndex].referenceCount)
339  return; //Nothing to remove, there's still a reference-count left
340 
341  //We have reduced the reference-count to zero, so remove the item from the list
342 
343  EmbeddedTreeRemoveItem<CodeModelItem, CodeModelItemHandler> remove(items,
344  oldItem->itemsSize(), oldItem->centralFreeItem, searchItem);
345 
346  uint newItemCount = remove.newItemCount();
347  if (newItemCount != oldItem->itemsSize()) {
348  if (newItemCount == 0) {
349  //Has become empty, delete the item
350  d->m_repository.deleteItem(index);
351 
352  return;
353  } else {
354  //Make smaller
355  item.itemsList().resize(newItemCount);
356  remove.transferData(item.itemsList().data(), item.itemsSize(), &item.centralFreeItem);
357 
358  //Delete the old list
359  d->m_repository.deleteItem(index);
360  //Add the new list
361  d->m_repository.index(request);
362  return;
363  }
364  }
365  }
366 }
367 
368 void CodeModel::items(const IndexedString& file, uint& count, const CodeModelItem*& items) const
369 {
370  Q_D(const CodeModel);
371 
372  ifDebug(qCDebug(LANGUAGE) << "items" << file.str(); )
373 
374  CodeModelRepositoryItem item;
375  item.file = file;
376  CodeModelRequestItem request(item);
377 
378  uint index = d->m_repository.findIndex(item);
379 
380  if (index) {
381  const CodeModelRepositoryItem* repositoryItem = d->m_repository.itemFromIndex(index);
382  ifDebug(qCDebug(LANGUAGE) << "found index" << index << repositoryItem->itemsSize(); )
383  count = repositoryItem->itemsSize();
384  items = repositoryItem->items();
385  } else {
386  ifDebug(qCDebug(LANGUAGE) << "found no index"; )
387  count = 0;
388  items = nullptr;
389  }
390 }
391 
392 CodeModel& CodeModel::self()
393 {
394  static CodeModel ret;
395  return ret;
396 }
397 }
KDevelop::CodeModel::addItem
void addItem(const IndexedString &file, const IndexedQualifiedIdentifier &id, CodeModelItem::Kind kind)
There can only be one item for each identifier.
Definition: codemodel.cpp:196
KDevelop::CodeModelItem::kind
Kind kind
Definition: codemodel.h:53
KDevelop::CodeModel::CodeModel
CodeModel()
Definition: codemodel.cpp:189
DEFINE_LIST_MEMBER_HASH
#define DEFINE_LIST_MEMBER_HASH(container, member, type)
Definition: appendedlist.h:218
APPENDED_LIST_FIRST
#define APPENDED_LIST_FIRST(container, type, name)
Definition: appendedlist.h:321
KDevelop::CodeModelItem::referenceCount
uint referenceCount
Definition: codemodel.h:51
appendedlist.h
KDevelop::IndexedQualifiedIdentifier
A helper-class to store an identifier by index in a type-safe way.
Definition: identifier.h:95
ifDebug
#define ifDebug(x)
Definition: codemodel.cpp:29
identifier.h
KDevelop::CodeModelItem::id
IndexedQualifiedIdentifier id
Definition: codemodel.h:50
KDevelop::CodeModel::removeItem
void removeItem(const IndexedString &file, const IndexedQualifiedIdentifier &id)
Definition: codemodel.cpp:305
KDevelop::CodeModel::self
static CodeModel & self()
Definition: codemodel.cpp:392
KDevelop::CodeModelItem
Definition: codemodel.h:35
END_APPENDED_LISTS
#define END_APPENDED_LISTS(container, predecessor)
Definition: appendedlist.h:358
KDevelop::CodeModelItem::Kind
Kind
Definition: codemodel.h:41
KDevelop::CodeModel::~CodeModel
~CodeModel()
KDevelop::CodeModel
Persistent store that efficiently holds a list of identifiers and their kind for each declaration-str...
Definition: codemodel.h:66
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
QMutexLocker
codemodel.h
START_APPENDED_LISTS
#define START_APPENDED_LISTS(container)
use this if the class does not have a base class that also uses appended lists
Definition: appendedlist.h:250
KDevelop::CodeModel::updateItem
void updateItem(const IndexedString &file, const IndexedQualifiedIdentifier &id, CodeModelItem::Kind kind)
Updates the kind for the given item.
Definition: codemodel.cpp:264
KDevelop::CodeModel::items
void items(const IndexedString &file, uint &count, const CodeModelItem *&items) const
Retrieves all the global identifiers for a file-name in an efficient way.
Definition: codemodel.cpp:368
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Fri Apr 16 2021 23:30:10 by doxygen 1.8.16 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevplatform/language/duchain

Skip menu "kdevplatform/language/duchain"
  • 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