adapt to kdevplatform changes, use DeclarationPointer when the duchain might get...
[kdevelop:php.git] / duchain / builders / contextbuilder.h
1 /***************************************************************************
2  *   This file is part of KDevelop                                         *
3  *   Copyright 2008 Niko Sams <niko.sams@gmail.com>                        *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU Library General Public License as       *
7  *   published by the Free Software Foundation; either version 2 of the    *
8  *   License, or (at your option) any later version.                       *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU Library General Public     *
16  *   License along with this program; if not, write to the                 *
17  *   Free Software Foundation, Inc.,                                       *
18  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.         *
19  ***************************************************************************/
20
21 #ifndef CONTEXTBUILDER_H
22 #define CONTEXTBUILDER_H
23
24 #include "phpdefaultvisitor.h"
25 #include <language/duchain/builders/abstractcontextbuilder.h>
26
27 #include "phpduchainexport.h"
28 #include "editorintegrator.h"
29 #include "helper.h"
30
31
32 namespace Php
33 {
34 class EditorIntegrator;
35 class ParseSession;
36
37 typedef KDevelop::AbstractContextBuilder<AstNode, IdentifierAst> ContextBuilderBase;
38
39 /// first is the "pretty" identifier used for printing
40 /// second comes the all-lowercase identifier used for storage
41 typedef QPair<KDevelop::IndexedString, KDevelop::QualifiedIdentifier> IdentifierPair;
42
43 class KDEVPHPDUCHAIN_EXPORT ContextBuilder: public ContextBuilderBase, public DefaultVisitor
44 {
45
46 public:
47     ContextBuilder();
48     virtual ~ContextBuilder();
49
50     virtual KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString& url, AstNode* node,
51             KDevelop::ReferencedTopDUContext updateContext
52             = KDevelop::ReferencedTopDUContext());
53
54     bool hadUnresolvedIdentifiers() const;
55
56     EditorIntegrator* editor() const;
57
58 protected:
59     virtual KDevelop::DUContext* newContext(const KDevelop::RangeInRevision& range);
60     virtual KDevelop::TopDUContext* newTopContext(const KDevelop::RangeInRevision& range, KDevelop::ParsingEnvironmentFile* file = 0);
61
62     virtual void startVisiting(AstNode* node);
63     virtual void setContextOnNode(AstNode* node, KDevelop::DUContext* ctx);
64     virtual KDevelop::DUContext* contextFromNode(AstNode* node);
65     virtual KDevelop::RangeInRevision editorFindRange(AstNode* fromRange, AstNode* toRange);
66     /// Find Cursor for start of a node, useful to limit findLocalDeclarations() searches.
67     KDevelop::CursorInRevision startPos( AstNode* node);
68
69     virtual KDevelop::QualifiedIdentifier identifierForNode(IdentifierAst* id);
70     KDevelop::QualifiedIdentifier identifierForNode(VariableIdentifierAst* id);
71     IdentifierPair identifierPairForNode(IdentifierAst* id);
72     QString stringForNode(IdentifierAst* node) const;
73     QString stringForNode(VariableIdentifierAst* node) const;
74
75     virtual void visitClassDeclarationStatement(ClassDeclarationStatementAst*);
76     virtual void visitInterfaceDeclarationStatement(InterfaceDeclarationStatementAst* node);
77     virtual void visitClassStatement(ClassStatementAst *node);
78     virtual void visitFunctionDeclarationStatement(FunctionDeclarationStatementAst* node);
79     virtual void visitClosure(ClosureAst* node);
80     virtual void visitUnaryExpression(UnaryExpressionAst* node);
81     /**
82      * don't overload in other builders, use @c openNamespace and @c closeNamespace instead.
83      */
84     virtual void visitNamespaceDeclarationStatement(NamespaceDeclarationStatementAst* node);
85     virtual void openNamespace(NamespaceDeclarationStatementAst* parent, IdentifierAst* node, const IdentifierPair& identifier, const KDevelop::RangeInRevision& range);
86     virtual void closeNamespace(NamespaceDeclarationStatementAst* parent, IdentifierAst* node, const IdentifierPair& identifier);
87
88     virtual void addBaseType(NamespacedIdentifierAst * identifier);
89
90     virtual void classContextOpened(KDevelop::DUContext* context);
91
92     /// Report @p errorMsg with the range of @p node
93     /// @see void reportError(const QString& errorMsg, KDevelop::SimpleRange range);
94     void reportError(const QString& errorMsg, AstNode* node,
95                         KDevelop::ProblemData::Severity severity = KDevelop::ProblemData::Error);
96     /// Report @p errorMsg with the range encompassing all nodes in @p nodes
97     /// @see void reportError(const QString& errorMsg, KDevelop::SimpleRange range);
98     void reportError(const QString& errorMsg, QList<AstNode*> nodes,
99                         KDevelop::ProblemData::Severity severity = KDevelop::ProblemData::Error);
100     /// Report @p errorMsg with range @p range
101     void reportError(const QString& errorMsg, KDevelop::RangeInRevision range,
102                         KDevelop::ProblemData::Severity severity = KDevelop::ProblemData::Error);
103
104     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, IdentifierAst* node);
105     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, VariableIdentifierAst* node);
106     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, const KDevelop::QualifiedIdentifier &identifier, AstNode* node);
107
108     /// internal functions file should not be checked for errors and can get some optimizations
109     bool m_isInternalFunctions;
110     /// Whether semantic problems should get reported
111     bool m_reportErrors;
112     ///TODO: push this into kdevplatform
113     bool m_mapAst;
114     bool m_hadUnresolvedIdentifiers;
115
116     EditorIntegrator* m_editor;
117
118 private:
119     void closeNamespaces(NamespaceDeclarationStatementAst* namespaces);
120     NamespaceDeclarationStatementAst* m_openNamespaces;
121
122 };
123
124 }
125
126 #endif