File: declarationid.cpp

package info (click to toggle)
kdevelop 4%3A5.6.2-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 57,892 kB
  • sloc: cpp: 278,773; javascript: 3,558; python: 3,385; sh: 1,317; ansic: 689; xml: 273; php: 95; makefile: 40; lisp: 13; sed: 12
file content (234 lines) | stat: -rw-r--r-- 8,246 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/* This file is part of KDevelop
    Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Library General Public
   License version 2 as published by the Free Software Foundation.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Library General Public License for more details.

   You should have received a copy of the GNU Library General Public License
   along with this library; see the file COPYING.LIB.  If not, write to
   the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
   Boston, MA 02110-1301, USA.
 */

#include "declarationid.h"

#include "ducontext.h"
#include "topducontext.h"
#include "duchain.h"
#include "declaration.h"
#include "persistentsymboltable.h"
#include "instantiationinformation.h"

#include <util/convenientfreelist.h>

namespace KDevelop {
DeclarationId::DeclarationId(const IndexedQualifiedIdentifier& id, uint additionalId,
                             const IndexedInstantiationInformation& specialization)
    : m_indirectData{id, additionalId}
    , m_isDirect(false)
    , m_specialization(specialization)
{
}

DeclarationId::DeclarationId(const IndexedDeclaration& decl,
                             const IndexedInstantiationInformation& specialization)
    : m_directData(decl)
    , m_isDirect(true)
    , m_specialization(specialization)
{
}

DeclarationId::DeclarationId(const DeclarationId& rhs)
    : m_isDirect(rhs.m_isDirect)
    , m_specialization(rhs.m_specialization)
{
    if (!m_isDirect) {
        // IndexedQualifiedIdentifier doesn't like zero-initialization...
        new (&m_indirectData.identifier) IndexedQualifiedIdentifier(rhs.m_indirectData.identifier);
        m_indirectData.additionalIdentity = rhs.m_indirectData.additionalIdentity;
    } else {
        m_directData = rhs.m_directData;
    }
}

DeclarationId::~DeclarationId()
{
    if (!m_isDirect) {
        m_indirectData.~Indirect();
    }
}

DeclarationId& DeclarationId::operator=(const DeclarationId& rhs)
{
    if (&rhs == this)
        return *this;

    m_isDirect = rhs.m_isDirect;
    m_specialization = rhs.m_specialization;
    if (!m_isDirect) {
        m_indirectData = rhs.m_indirectData;
    } else {
        m_directData = rhs.m_directData;
    }
    return *this;
}

bool DeclarationId::isDirect() const
{
    return m_isDirect;
}

void DeclarationId::setSpecialization(const IndexedInstantiationInformation& spec)
{
    m_specialization = spec;
}

IndexedInstantiationInformation DeclarationId::specialization() const
{
    return m_specialization;
}

KDevVarLengthArray<Declaration*> DeclarationId::declarations(const TopDUContext* top) const
{
    KDevVarLengthArray<Declaration*> ret;

    if (m_isDirect == false) {
        //Find the declaration by its qualified identifier and additionalIdentity
        QualifiedIdentifier id(m_indirectData.identifier);

        if (top) {
            //Do filtering
            PersistentSymbolTable::FilteredDeclarationIterator filter =
                PersistentSymbolTable::self().filteredDeclarations(id, top->recursiveImportIndices());
            for (; filter; ++filter) {
                Declaration* decl = filter->data();
                if (decl && m_indirectData.additionalIdentity == decl->additionalIdentity()) {
                    //Hit
                    ret.append(decl);
                }
            }
        } else {
            //Just accept anything
            PersistentSymbolTable::Declarations decls = PersistentSymbolTable::self().declarations(id);
            PersistentSymbolTable::Declarations::Iterator decl = decls.iterator();
            for (; decl; ++decl) {
                const IndexedDeclaration& iDecl(*decl);

                ///@todo think this over once we don't pull in all imported top-context any more
                //Don't trigger loading of top-contexts from here, it will create a lot of problems
                if ((!DUChain::self()->isInMemory(iDecl.topContextIndex())))
                    continue;

                Declaration* decl = iDecl.data();
                if (decl && m_indirectData.additionalIdentity == decl->additionalIdentity()) {
                    //Hit
                    ret.append(decl);
                }
            }
        }
    } else {
        Declaration* decl = m_directData.declaration();
        if (decl)
            ret.append(decl);
    }

    if (!ret.isEmpty() && m_specialization.index()) {
        KDevVarLengthArray<Declaration*> newRet;
        for (Declaration* decl : qAsConst(ret)) {
            Declaration* specialized = decl->specialize(m_specialization, top ? top : decl->topContext());
            if (specialized)
                newRet.append(specialized);
        }

        return newRet;
    }
    return ret;
}

Declaration* DeclarationId::declaration(const TopDUContext* top, bool instantiateIfRequired) const
{
    Declaration* ret = nullptr;

    if (m_isDirect == false) {
        //Find the declaration by its qualified identifier and additionalIdentity
        QualifiedIdentifier id(m_indirectData.identifier);

        if (top) {
            //Do filtering
            PersistentSymbolTable::FilteredDeclarationIterator filter =
                PersistentSymbolTable::self().filteredDeclarations(id, top->recursiveImportIndices());
            for (; filter; ++filter) {
                Declaration* decl = filter->data();
                if (decl && m_indirectData.additionalIdentity == decl->additionalIdentity()) {
                    //Hit
                    ret = decl;
                    if (!ret->isForwardDeclaration())
                        break;
                }
            }
        } else {
            //Just accept anything
            PersistentSymbolTable::Declarations decls = PersistentSymbolTable::self().declarations(id);
            PersistentSymbolTable::Declarations::Iterator decl = decls.iterator();
            for (; decl; ++decl) {
                const IndexedDeclaration& iDecl(*decl);

                ///@todo think this over once we don't pull in all imported top-context any more
                //Don't trigger loading of top-contexts from here, it will create a lot of problems
                if ((!DUChain::self()->isInMemory(iDecl.topContextIndex())))
                    continue;

                Declaration* decl = iDecl.data();
                if (decl && m_indirectData.additionalIdentity == decl->additionalIdentity()) {
                    //Hit
                    ret = decl;
                    if (!ret->isForwardDeclaration())
                        break;
                }
            }
        }
    } else {
        //Find the declaration by m_topContext and m_declaration
        ret = m_directData.declaration();
    }

    if (ret) {
        if (m_specialization.isValid()) {
            const TopDUContext* topContextForSpecialization = top;
            if (!instantiateIfRequired)
                topContextForSpecialization = nullptr; //If we don't want to instantiate new declarations, set the top-context to zero, so specialize(..) will only look-up
            else if (!topContextForSpecialization)
                topContextForSpecialization = ret->topContext();

            return ret->specialize(m_specialization, topContextForSpecialization);
        } else {
            return ret;
        }
    } else
        return nullptr;
}

QualifiedIdentifier DeclarationId::qualifiedIdentifier() const
{
    if (!m_isDirect) {
        QualifiedIdentifier baseIdentifier = m_indirectData.identifier.identifier();
        if (!m_specialization.index())
            return baseIdentifier;
        return m_specialization.information().applyToIdentifier(baseIdentifier);
    } else {
        Declaration* decl = declaration(nullptr);
        if (decl)
            return decl->qualifiedIdentifier();

        return QualifiedIdentifier(i18n("(unknown direct declaration)"));
    }

    return QualifiedIdentifier(i18n("(missing)")) + m_indirectData.identifier.identifier();
}
}