Add AbstractDeclarationNavigationContext, and move the html-method from
[kdevelopdvcssupport.git] / language / duchain / functiondefinition.cpp
blobc1d1a72af10b0840a53051d9c0f542b623be090e
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 "functiondefinition.h"
20 #include "duchainregister.h"
21 #include "definitions.h"
23 namespace KDevelop {
24 REGISTER_DUCHAIN_ITEM(FunctionDefinition);
26 FunctionDefinition::FunctionDefinition(FunctionDefinitionData& data) : FunctionDeclaration(data)
30 FunctionDefinition::FunctionDefinition(const SimpleRange& range, DUContext* context)
31 : FunctionDeclaration(*new FunctionDefinitionData, range)
33 d_func_dynamic()->setClassId(this);
34 if( context )
35 setContext( context );
38 FunctionDefinition::FunctionDefinition(const FunctionDefinition& rhs) : FunctionDeclaration(*new FunctionDefinitionData(*rhs.d_func())) {
41 FunctionDefinition::~FunctionDefinition() {
42 if(!topContext()->isOnDisk())
43 DUChain::definitions()->removeDefinition(d_func()->m_declaration, this);
46 Declaration* FunctionDefinition::declaration(TopDUContext* topContext) const
48 ENSURE_CAN_READ
50 KDevVarLengthArray<Declaration*> declarations = d_func()->m_declaration.getDeclarations(topContext ? topContext : this->topContext());
52 FOREACH_ARRAY(Declaration* decl, declarations) {
53 if(!dynamic_cast<FunctionDefinition*>(decl))
54 return decl;
57 return 0;
60 bool FunctionDefinition::hasDeclaration() const
62 return d_func()->m_declaration.isValid();
65 void FunctionDefinition::setDeclaration(Declaration* declaration)
67 ENSURE_CAN_WRITE
69 if(declaration) {
70 DUChain::definitions()->addDefinition(declaration->id(), this);
71 d_func_dynamic()->m_declaration = declaration->id();
72 }else{
73 if(d_func()->m_declaration.isValid()) {
74 DUChain::definitions()->removeDefinition(d_func()->m_declaration, this);
75 d_func_dynamic()->m_declaration = DeclarationId();
80 FunctionDefinition* FunctionDefinition::definition(const Declaration* decl)
82 ENSURE_CHAIN_READ_LOCKED
83 KDevVarLengthArray<IndexedDeclaration> allDefinitions = DUChain::definitions()->definitions(decl->id());
84 FOREACH_ARRAY(IndexedDeclaration decl, allDefinitions) {
85 if(decl.data()) ///@todo Find better ways of deciding which definition to use
86 return dynamic_cast<FunctionDefinition*>(decl.data());
88 return 0;
91 Declaration* FunctionDefinition::clonePrivate() const
93 return new FunctionDefinition(*new FunctionDefinitionData(*d_func()));