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

kdevelop/kdevplatform/language/duchain

  • extragear
  • kdevelop
  • kdevelop
  • kdevplatform
  • language
  • duchain
duchainbase.cpp
Go to the documentation of this file.
1 /* This is part of KDevelop
2  Copyright 2006 Hamish Rodda <[email protected]>
3  Copyright 2007/2008 David Nolden <[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 "duchainbase.h"
21 
22 #include <QMutexLocker>
23 #include <QThreadStorage>
24 
25 #include "duchainpointer.h"
26 #include "parsingenvironment.h"
27 #include <serialization/indexedstring.h>
28 #include "topducontext.h"
29 #include "duchainregister.h"
30 #include <util/foregroundlock.h>
31 #include <interfaces/icore.h>
32 #include <interfaces/ilanguagecontroller.h>
33 #include <backgroundparser/backgroundparser.h>
34 #include <backgroundparser/documentchangetracker.h>
35 
36 namespace KDevelop {
37 REGISTER_DUCHAIN_ITEM(DUChainBase);
38 
39 uint DUChainBaseData::classSize() const
40 {
41  return DUChainItemSystem::self().dataClassSize(*this);
42 }
43 
44 DUChainBase::DUChainBase(const RangeInRevision& range)
45  : d_ptr(new DUChainBaseData)
46 {
47  d_func_dynamic()->m_range = range;
48  d_func_dynamic()->setClassId(this);
49 }
50 
51 DUChainBase::DUChainBase(DUChainBaseData& dd, const RangeInRevision& range)
52  : d_ptr(&dd)
53 {
54  d_func_dynamic()->m_range = range;
55 }
56 
57 DUChainBase::DUChainBase(DUChainBaseData& dd)
58  : d_ptr(&dd)
59 {
60 }
61 
62 DUChainBase::DUChainBase(DUChainBase& rhs)
63  : d_ptr(new DUChainBaseData(*rhs.d_func()))
64 {
65  d_func_dynamic()->setClassId(this);
66 }
67 
68 IndexedString DUChainBase::url() const
69 {
70  TopDUContext* top = topContext();
71  if (top)
72  return top->TopDUContext::url();
73  else
74  return IndexedString();
75 }
76 
77 void DUChainBase::setData(DUChainBaseData* data, bool constructorCalled)
78 {
79  Q_ASSERT(data);
80  Q_ASSERT(d_ptr);
81 
82  if (constructorCalled)
83  KDevelop::DUChainItemSystem::self().callDestructor(static_cast<DUChainBaseData*>(d_ptr));
84 
85  if (d_ptr->m_dynamic) // If the data object isn't dynamic, then it is part of a central repository, and cannot be deleted here.
86  delete d_ptr;
87 
88  d_ptr = data;
89 }
90 
91 DUChainBase::~DUChainBase()
92 {
93  if (m_ptr)
94  m_ptr->m_base = nullptr;
95 
96  if (d_ptr->m_dynamic) {
97  KDevelop::DUChainItemSystem::self().callDestructor(d_ptr);
98  delete d_ptr;
99  d_ptr = nullptr;
100  }
101 }
102 
103 TopDUContext* DUChainBase::topContext() const
104 {
106  return nullptr;
107 }
108 
109 namespace {
110 QMutex weakPointerMutex;
111 }
112 
113 const QExplicitlySharedDataPointer<DUChainPointerData>& DUChainBase::weakPointer() const
114 {
115  if (!m_ptr) {
116  QMutexLocker lock(&weakPointerMutex); // The mutex is used to make sure we don't create m_ptr twice at the same time
117  m_ptr = new DUChainPointerData(const_cast<DUChainBase*>(this));
118  m_ptr->m_base = const_cast<DUChainBase*>(this);
119  }
120 
121  return m_ptr;
122 }
123 
124 void DUChainBase::rebuildDynamicData(DUContext* parent, uint ownIndex)
125 {
126  Q_UNUSED(parent)
127  Q_UNUSED(ownIndex)
128 }
129 
130 void DUChainBase::makeDynamic()
131 {
132  Q_ASSERT(d_ptr);
133  if (!d_func()->m_dynamic) {
134  Q_ASSERT(d_func()->classId);
135  DUChainBaseData* newData = DUChainItemSystem::self().cloneData(*d_func());
136  enableDUChainReferenceCounting(d_ptr,
137  DUChainItemSystem::self().dynamicSize(*static_cast<DUChainBaseData*>(d_ptr)));
138  //We don't delete the previous data, because it's embedded in the top-context when it isn't dynamic.
139  //However we do call the destructor, to keep semantic stuff like reference-counting within the data class working correctly.
140  KDevelop::DUChainItemSystem::self().callDestructor(static_cast<DUChainBaseData*>(d_ptr));
141  disableDUChainReferenceCounting(d_ptr);
142  d_ptr = newData;
143  Q_ASSERT(d_ptr);
144  Q_ASSERT(d_func()->m_dynamic);
145  Q_ASSERT(d_func()->classId);
146  }
147 }
148 
149 RangeInRevision DUChainBase::range() const
150 {
151  return d_func()->m_range;
152 }
153 
154 KTextEditor::Range DUChainBase::rangeInCurrentRevision() const
155 {
156  DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(url());
157 
158  if (tracker && topContext() && topContext()->parsingEnvironmentFile()) {
159  qint64 revision = topContext()->parsingEnvironmentFile()->modificationRevision().revision;
160  return tracker->transformToCurrentRevision(d_func()->m_range, revision);
161  }
162 
163  // If the document is not open, we can simply cast the range over, as no translation can be done
164  return d_func()->m_range.castToSimpleRange();
165 }
166 
167 PersistentMovingRange::Ptr DUChainBase::createRangeMoving() const
168 {
169  VERIFY_FOREGROUND_LOCKED
170  return PersistentMovingRange::Ptr(new PersistentMovingRange(rangeInCurrentRevision(), url()));
171 }
172 
173 CursorInRevision DUChainBase::transformToLocalRevision(const KTextEditor::Cursor& cursor) const
174 {
175  DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(url());
176 
177  if (tracker && topContext() && topContext()->parsingEnvironmentFile()) {
178  qint64 revision = topContext()->parsingEnvironmentFile()->modificationRevision().revision;
179  return tracker->transformToRevision(cursor, revision);
180  }
181 
182  return CursorInRevision::castFromSimpleCursor(cursor);
183 }
184 
185 RangeInRevision DUChainBase::transformToLocalRevision(const KTextEditor::Range& range) const
186 {
187  DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(url());
188 
189  if (tracker && topContext() && topContext()->parsingEnvironmentFile()) {
190  qint64 revision = topContext()->parsingEnvironmentFile()->modificationRevision().revision;
191  return tracker->transformToRevision(range, revision);
192  }
193 
194  return RangeInRevision::castFromSimpleRange(range);
195 }
196 
197 KTextEditor::Range DUChainBase::transformFromLocalRevision(const KDevelop::RangeInRevision& range) const
198 {
199  DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(url());
200 
201  if (tracker && topContext() && topContext()->parsingEnvironmentFile()) {
202  qint64 revision = topContext()->parsingEnvironmentFile()->modificationRevision().revision;
203  return tracker->transformToCurrentRevision(range, revision);
204  }
205 
206  return range.castToSimpleRange();
207 }
208 
209 KTextEditor::Cursor DUChainBase::transformFromLocalRevision(const KDevelop::CursorInRevision& cursor) const
210 {
211  DocumentChangeTracker* tracker = ICore::self()->languageController()->backgroundParser()->trackerForUrl(url());
212 
213  if (tracker && topContext() && topContext()->parsingEnvironmentFile()) {
214  qint64 revision = topContext()->parsingEnvironmentFile()->modificationRevision().revision;
215  return tracker->transformToCurrentRevision(cursor, revision);
216  }
217 
218  return cursor.castToSimpleCursor();
219 }
220 
221 void DUChainBase::setRange(const RangeInRevision& range)
222 {
223  d_func_dynamic()->m_range = range;
224 }
225 
226 QThreadStorage<bool> shouldCreateConstantDataStorage;
227 
228 bool& DUChainBaseData::shouldCreateConstantData()
229 {
230  return shouldCreateConstantDataStorage.localData();
231 }
232 }
KDevelop::DUChainBase::range
RangeInRevision range() const
Returns the range assigned to this object, in the document revision when this document was last parse...
Definition: duchainbase.cpp:149
QMutex
KDevelop::DUChainItemSystem::dataClassSize
uint dataClassSize(const DUChainBaseData &data) const
Returns the size of the derived class, not including dynamic data.
Definition: duchainregister.cpp:70
KDevelop::REGISTER_DUCHAIN_ITEM
REGISTER_DUCHAIN_ITEM(AliasDeclaration)
KDevelop::DUChainItemSystem::self
static DUChainItemSystem & self()
Access the static DUChainItemSystem instance.
Definition: duchainregister.cpp:86
KDevelop::DUChainBase::makeDynamic
void makeDynamic()
After this was called, the data-pointer is dynamic. It is cloned if needed.
Definition: duchainbase.cpp:130
KDevelop::shouldCreateConstantDataStorage
QThreadStorage< bool > shouldCreateConstantDataStorage
Definition: duchainbase.cpp:226
KDevelop::DUChainItemSystem::cloneData
DUChainBaseData * cloneData(const DUChainBaseData &data) const
Creates a dynamic copy of the given data.
Definition: duchainregister.cpp:40
KDevelop::DUChainBase::url
virtual IndexedString url() const
Definition: duchainbase.cpp:68
duchainpointer.h
KDevelop::DUChainBase::rangeInCurrentRevision
KTextEditor::Range rangeInCurrentRevision() const
Returns the range assigned to this object, transformed into the current revision of the document...
Definition: duchainbase.cpp:154
KDevelop::DUChainBase::~DUChainBase
virtual ~DUChainBase()
Destructor.
Definition: duchainbase.cpp:91
KDevelop::DUChainBaseData::shouldCreateConstantData
static bool & shouldCreateConstantData()
Used to decide whether a constructed item should create constant data.
Definition: duchainbase.cpp:228
KDevelop::TopDUContext
The top context in a definition-use chain for one source file.
Definition: topducontext.h:113
parsingenvironment.h
KDevelop::DUChainBase
Base class for definition-use chain objects.
Definition: duchainbase.h:131
KDevelop::DUChainBaseData::classSize
uint classSize() const
Definition: duchainbase.cpp:39
KDevelop::DUChainItemSystem::callDestructor
void callDestructor(DUChainBaseData *data) const
Calls the destructor, but does not delete anything.
Definition: duchainregister.cpp:49
KDevelop::DUChainBase::d_ptr
DUChainBaseData * d_ptr
Data pointer that is shared across all the inheritance hierarchy.
Definition: duchainbase.h:221
QThreadStorage
KDevelop::DUChainBase::setData
virtual void setData(DUChainBaseData *, bool constructorCalled=true)
This must only be used to change the storage-location or storage-kind(dynamic/constant) of the data...
Definition: duchainbase.cpp:77
KDevelop::DUChainBase::setRange
void setRange(const RangeInRevision &range)
Changes the range assigned to this object, in the document revision when this document is parsed...
Definition: duchainbase.cpp:221
topducontext.h
KDevelop::DUChainBase::weakPointer
const QExplicitlySharedDataPointer< DUChainPointerData > & weakPointer() const
Returns a special pointer that can be used to track the existence of a du-chain object across locking...
Definition: duchainbase.cpp:113
KDevelop::DUChainBase::transformToLocalRevision
CursorInRevision transformToLocalRevision(const KTextEditor::Cursor &cursor) const
Transforms the given cursor in the current document revision to its according position in the parsed ...
Definition: duchainbase.cpp:173
KDevelop::DUChainBase::DUChainBase
DUChainBase(const RangeInRevision &range)
Constructor.
Definition: duchainbase.cpp:44
QMutexLocker
duchainregister.h
KDevelop::DUChainPointerData
Whenever the du-chain is unlocked and locked again, any du-chain item may have been deleted in betwee...
Definition: duchainpointer.h:50
KDevelop::DUContext
A single context in source code, represented as a node in a directed acyclic graph.
Definition: ducontext.h:72
KDevelop::DUChainBaseData
Definition: duchainbase.h:59
KDevelop::DUChainBase::transformFromLocalRevision
KTextEditor::Cursor transformFromLocalRevision(const CursorInRevision &cursor) const
KDevelop::DUChainBase::createRangeMoving
PersistentMovingRange::Ptr createRangeMoving() const
Returns the range assigned to this object, transformed into the current revision of the document...
Definition: duchainbase.cpp:167
QExplicitlySharedDataPointer
Definition: topducontext.h:28
KDevelop::DUChainBase::rebuildDynamicData
virtual void rebuildDynamicData(DUContext *parent, uint ownIndex)
Called after loading to rebuild the dynamic data. If this is a context, this should recursively work ...
Definition: duchainbase.cpp:124
duchainbase.h
KDevelop::TopDUContext::parsingEnvironmentFile
QExplicitlySharedDataPointer< ParsingEnvironmentFile > parsingEnvironmentFile() const
Definition: topducontext.cpp:564
KDevelop::DUChainBase::topContext
virtual TopDUContext * topContext() const
Determine the top context to which this object belongs.
Definition: duchainbase.cpp:103
This file is part of the KDE documentation.
Documentation copyright © 1996-2019 The KDE developers.
Generated on Sat Dec 7 2019 04:28:06 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

kdevelop/kdevplatform/language/duchain

Skip menu "kdevelop/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