• 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
definitions.cpp
Go to the documentation of this file.
1 /* This file is part of KDevelop
2  Copyright 2008 David Nolden <[email protected]>
3  Copyright 2014 Kevin Funk <[email protected]>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18  */
19 
20 #include "definitions.h"
21 
22 #include "appendedlist.h"
23 #include "declaration.h"
24 #include "declarationid.h"
25 #include "duchainpointer.h"
26 #include <serialization/indexedstring.h>
27 #include "serialization/itemrepository.h"
28 
29 namespace KDevelop {
30 
31 using TextStreamFunction = QTextStream& (*)(QTextStream&);
32 #if QT_VERSION >= QT_VERSION_CHECK(5, 14, 0)
33 constexpr TextStreamFunction endl = Qt::endl;
34 #else
35 constexpr TextStreamFunction endl = ::endl;
36 #endif
37 
38 
39 
40 DEFINE_LIST_MEMBER_HASH(DefinitionsItem, definitions, IndexedDeclaration)
41 
42 class DefinitionsItem
43 {
44 public:
45  DefinitionsItem()
46  {
47  initializeAppendedLists();
48  }
49  DefinitionsItem(const DefinitionsItem& rhs, bool dynamic = true) : declaration(rhs.declaration)
50  {
51  initializeAppendedLists(dynamic);
52  copyListsFrom(rhs);
53  }
54 
55  ~DefinitionsItem()
56  {
57  freeAppendedLists();
58  }
59 
60  DefinitionsItem& operator=(const DefinitionsItem& rhs) = delete;
61 
62  unsigned int hash() const
63  {
64  //We only compare the declaration. This allows us implementing a map, although the item-repository
65  //originally represents a set.
66  return declaration.hash();
67  }
68 
69  unsigned int itemSize() const
70  {
71  return dynamicSize();
72  }
73 
74  uint classSize() const
75  {
76  return sizeof(DefinitionsItem);
77  }
78 
79  DeclarationId declaration;
80 
81  START_APPENDED_LISTS(DefinitionsItem);
82  APPENDED_LIST_FIRST(DefinitionsItem, IndexedDeclaration, definitions);
83  END_APPENDED_LISTS(DefinitionsItem, definitions);
84 };
85 
86 class DefinitionsRequestItem
87 {
88 public:
89 
90  DefinitionsRequestItem(const DefinitionsItem& item) : m_item(item)
91  {
92  }
93  enum {
94  AverageSize = 30 //This should be the approximate average size of an Item
95  };
96 
97  unsigned int hash() const
98  {
99  return m_item.hash();
100  }
101 
102  uint itemSize() const
103  {
104  return m_item.itemSize();
105  }
106 
107  void createItem(DefinitionsItem* item) const
108  {
109  new (item) DefinitionsItem(m_item, false);
110  }
111 
112  static void destroy(DefinitionsItem* item, KDevelop::AbstractItemRepository&)
113  {
114  item->~DefinitionsItem();
115  }
116 
117  static bool persistent(const DefinitionsItem*)
118  {
119  return true;
120  }
121 
122  bool equals(const DefinitionsItem* item) const
123  {
124  return m_item.declaration == item->declaration;
125  }
126 
127  const DefinitionsItem& m_item;
128 };
129 
130 class DefinitionsVisitor
131 {
132 public:
133  DefinitionsVisitor(Definitions* _definitions, const QTextStream& _out)
134  : definitions(_definitions)
135  , out(_out)
136  {
137  }
138 
139  bool operator()(const DefinitionsItem* item)
140  {
141  QDebug qout(out.device());
142  auto id = item->declaration;
143  const auto allDefinitions = definitions->definitions(id);
144 
145  qout << "Definitions for" << id.qualifiedIdentifier() << endl;
146  for (const IndexedDeclaration& decl : allDefinitions) {
147  if (decl.data()) {
148  qout << " " << decl.data()->qualifiedIdentifier() << "in" << decl.data()->url().byteArray() << "at" <<
149  decl.data()->rangeInCurrentRevision() << endl;
150  }
151  }
152 
153  return true;
154  }
155 
156 private:
157  const Definitions* definitions;
158  const QTextStream& out;
159 };
160 
161 class DefinitionsPrivate
162 {
163 public:
164 
165  DefinitionsPrivate() : m_definitions(QStringLiteral("Definition Map"))
166  {
167  }
168  //Maps declaration-ids to definitions
169  // mutable as things like findIndex are not const
170  mutable ItemRepository<DefinitionsItem, DefinitionsRequestItem> m_definitions;
171 };
172 
173 Definitions::Definitions()
174  : d_ptr(new DefinitionsPrivate())
175 {
176 }
177 
178 Definitions::~Definitions() = default;
179 
180 void Definitions::addDefinition(const DeclarationId& id, const IndexedDeclaration& definition)
181 {
182  Q_D(Definitions);
183 
184  DefinitionsItem item;
185  item.declaration = id;
186  item.definitionsList().append(definition);
187  DefinitionsRequestItem request(item);
188 
189  uint index = d->m_definitions.findIndex(item);
190 
191  if (index) {
192  //Check whether the item is already in the mapped list, else copy the list into the new created item
193  const DefinitionsItem* oldItem = d->m_definitions.itemFromIndex(index);
194  for (unsigned int a = 0; a < oldItem->definitionsSize(); ++a) {
195  if (oldItem->definitions()[a] == definition)
196  return; //Already there
197  item.definitionsList().append(oldItem->definitions()[a]);
198  }
199 
200  d->m_definitions.deleteItem(index);
201  }
202 
203  //This inserts the changed item
204  d->m_definitions.index(request);
205 }
206 
207 void Definitions::removeDefinition(const DeclarationId& id, const IndexedDeclaration& definition)
208 {
209  Q_D(Definitions);
210 
211  DefinitionsItem item;
212  item.declaration = id;
213  DefinitionsRequestItem request(item);
214 
215  uint index = d->m_definitions.findIndex(item);
216 
217  if (index) {
218  //Check whether the item is already in the mapped list, else copy the list into the new created item
219  const DefinitionsItem* oldItem = d->m_definitions.itemFromIndex(index);
220  for (unsigned int a = 0; a < oldItem->definitionsSize(); ++a)
221  if (!(oldItem->definitions()[a] == definition))
222  item.definitionsList().append(oldItem->definitions()[a]);
223 
224  d->m_definitions.deleteItem(index);
225  Q_ASSERT(d->m_definitions.findIndex(item) == 0);
226 
227  //This inserts the changed item
228  if (item.definitionsSize() != 0)
229  d->m_definitions.index(request);
230  }
231 }
232 
233 KDevVarLengthArray<IndexedDeclaration> Definitions::definitions(const DeclarationId& id) const
234 {
235  Q_D(const Definitions);
236 
237  KDevVarLengthArray<IndexedDeclaration> ret;
238 
239  DefinitionsItem item;
240  item.declaration = id;
241  DefinitionsRequestItem request(item);
242 
243  uint index = d->m_definitions.findIndex(item);
244 
245  if (index) {
246  const DefinitionsItem* repositoryItem = d->m_definitions.itemFromIndex(index);
247  FOREACH_FUNCTION(const IndexedDeclaration &decl, repositoryItem->definitions)
248  ret.append(decl);
249  }
250 
251  return ret;
252 }
253 
254 void Definitions::dump(const QTextStream& out)
255 {
256  Q_D(Definitions);
257 
258  QMutexLocker lock(d->m_definitions.mutex());
259  DefinitionsVisitor v(this, out);
260  d->m_definitions.visitAllItems(v);
261 }
262 }
KDevelop::Definitions::addDefinition
void addDefinition(const DeclarationId &id, const IndexedDeclaration &definition)
Assigns.
Definition: definitions.cpp:180
duchainpointer.h
KDevelop::TextStreamFunction
QTextStream &(*)(QTextStream &) TextStreamFunction
Definition: definitions.cpp:31
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::endl
constexpr TextStreamFunction endl
Definition: definitions.cpp:33
KDevelop::Definitions
Global mapping of one Declaration-Ids to multiple Definitions, protected through DUChainLock.
Definition: definitions.h:40
KDevelop::Definitions::removeDefinition
void removeDefinition(const DeclarationId &id, const IndexedDeclaration &definition)
Definition: definitions.cpp:207
QDebug
appendedlist.h
KDevelop::Definitions::dump
void dump(const QTextStream &out)
Dump contents of the definitions repository to stream out.
Definition: definitions.cpp:254
KDevelop::IndexedDeclaration
Represents a declaration only by its global indices.
Definition: indexeddeclaration.h:33
FOREACH_FUNCTION
#define FOREACH_FUNCTION(item, container)
Foreach macro that takes a container and a function-name, and will iterate through the vector returne...
Definition: appendedlist.h:213
KDevelop::Definitions::~Definitions
~Definitions()
Destructor.
declaration.h
QTextStream
KDevVarLengthArray
KDevelop::DeclarationId
Allows clearly identifying a Declaration.
Definition: declarationid.h:50
definitions.h
declarationid.h
KDevelop::Definitions::definitions
KDevVarLengthArray< IndexedDeclaration > definitions(const DeclarationId &id) const
Gets all the known definitions assigned to id.
Definition: definitions.cpp:233
END_APPENDED_LISTS
#define END_APPENDED_LISTS(container, predecessor)
Definition: appendedlist.h:358
KDevelop
Definition: abstractfunctiondeclaration.cpp:27
KDevelop::Definitions::Definitions
Definitions()
Constructor.
Definition: definitions.cpp:173
QMutexLocker
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
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Wed Mar 3 2021 00:37:28 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