Add AbstractDeclarationNavigationContext, and move the html-method from
[kdevelopdvcssupport.git] / language / duchain / specializationstore.cpp
blob73126d648aac64f0637ab166581e72340249dac5
1 /* This file is part of KDevelop
2 Copyright 2008 David Nolden <david.nolden.kdevelop@art-master.de>
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.
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.
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.
19 #include "specializationstore.h"
20 #include "declarationid.h"
21 #include "ducontext.h"
23 namespace KDevelop {
25 SpecializationStore::SpecializationStore() {
28 SpecializationStore::~SpecializationStore() {
31 SpecializationStore& SpecializationStore::self() {
32 static SpecializationStore store;
33 return store;
36 void SpecializationStore::set(DeclarationId declaration, uint specialization) {
37 Q_ASSERT(specialization >> 16);
38 m_specializations[declaration] = specialization;
41 uint SpecializationStore::get(DeclarationId declaration) {
42 QHash<DeclarationId, uint>::const_iterator it = m_specializations.find(declaration);
43 if(it != m_specializations.end())
44 return *it;
45 else
46 return 0;
49 void SpecializationStore::clear(DeclarationId declaration) {
50 QHash<DeclarationId, uint>::iterator it = m_specializations.find(declaration);
51 if(it != m_specializations.end())
52 m_specializations.erase(it);
55 void SpecializationStore::clear() {
56 m_specializations.clear();
59 Declaration* SpecializationStore::applySpecialization(KDevelop::Declaration* declaration, KDevelop::TopDUContext* source, bool recursive) {
60 if(!declaration)
61 return 0;
63 uint specialization = get(declaration->id());
64 if(specialization)
65 return declaration->specialize(specialization, source);
67 if(declaration->context() && recursive) {
69 //Find a parent that has a specialization, and specialize this with the info and required depth
70 int depth = 0;
71 DUContext* ctx = declaration->context();
72 uint specialization = 0;
73 while(ctx && !specialization) {
74 if(ctx->owner())
75 specialization = get(ctx->owner()->id());
76 ++depth;
77 ctx = ctx->parentContext();
80 if(specialization)
81 return declaration->specialize(specialization, source, depth);
84 return declaration;
87 DUContext* SpecializationStore::applySpecialization(KDevelop::DUContext* context, KDevelop::TopDUContext* source, bool recursive) {
88 if(!context)
89 return 0;
91 if(Declaration* declaration = context->owner())
92 return applySpecialization(declaration, source, recursive)->internalContext();
94 if(context->parentContext() && recursive) {
95 //Find a parent that has a specialization, and specialize this with the info and required depth
96 int depth = 0;
97 DUContext* ctx = context->parentContext();
98 uint specialization = 0;
99 while(ctx && !specialization) {
100 if(ctx->owner())
101 specialization = get(ctx->owner()->id());
102 ++depth;
103 ctx = ctx->parentContext();
106 if(specialization)
107 return context->specialize(specialization, source, depth);
110 return context;