File indexing completed on 2024-05-05 16:41:05

0001 /*
0002     SPDX-FileCopyrightText: 2008 Niko Sams <niko.sams@gmail.com>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #ifndef CONTEXTBUILDER_H
0008 #define CONTEXTBUILDER_H
0009 
0010 #include <language/duchain/builders/abstractcontextbuilder.h>
0011 #include <language/duchain/problem.h>
0012 
0013 #include "phpdefaultvisitor.h"
0014 #include "phpduchainexport.h"
0015 #include "editorintegrator.h"
0016 #include "helper.h"
0017 
0018 namespace Php
0019 {
0020 class EditorIntegrator;
0021 class ParseSession;
0022 
0023 typedef KDevelop::AbstractContextBuilder<AstNode, IdentifierAst> ContextBuilderBase;
0024 
0025 /// first is the "pretty" identifier used for printing
0026 /// second comes the all-lowercase identifier used for storage
0027 typedef QPair<KDevelop::IndexedString, KDevelop::QualifiedIdentifier> IdentifierPair;
0028 
0029 class KDEVPHPDUCHAIN_EXPORT ContextBuilder: public ContextBuilderBase, public DefaultVisitor
0030 {
0031 
0032 public:
0033     ContextBuilder();
0034     ~ContextBuilder() override;
0035 
0036     KDevelop::ReferencedTopDUContext build(const KDevelop::IndexedString& url, AstNode* node,
0037             const KDevelop::ReferencedTopDUContext& updateContext
0038             = KDevelop::ReferencedTopDUContext()) override;
0039 
0040     bool hadUnresolvedIdentifiers() const;
0041 
0042     EditorIntegrator* editor() const;
0043 
0044 protected:
0045     KDevelop::DUContext* newContext(const KDevelop::RangeInRevision& range) override;
0046     KDevelop::TopDUContext* newTopContext(const KDevelop::RangeInRevision& range, KDevelop::ParsingEnvironmentFile* file = nullptr) override;
0047 
0048     void startVisiting(AstNode* node) override;
0049     void setContextOnNode(AstNode* node, KDevelop::DUContext* ctx) override;
0050     KDevelop::DUContext* contextFromNode(AstNode* node) override;
0051     KDevelop::RangeInRevision editorFindRange(AstNode* fromRange, AstNode* toRange = nullptr) override;
0052     /// Find Cursor for start of a node, useful to limit findLocalDeclarations() searches.
0053     KDevelop::CursorInRevision startPos( AstNode* node);
0054 
0055     KDevelop::QualifiedIdentifier identifierForNode(IdentifierAst* id) override;
0056     KDevelop::QualifiedIdentifier identifierForNode(SemiReservedIdentifierAst* id);
0057     KDevelop::QualifiedIdentifier identifierForNode(VariableIdentifierAst* id);
0058     IdentifierPair identifierPairForNode(IdentifierAst* id, bool isConstIdentifier = false);
0059     IdentifierPair identifierPairForNode(SemiReservedIdentifierAst* id);
0060     IdentifierPair identifierPairForNode(ReservedNonModifierIdentifierAst* id);
0061     QString stringForNode(IdentifierAst* node) const;
0062     QString stringForNode(SemiReservedIdentifierAst* node) const;
0063     QString stringForNode(ReservedNonModifierIdentifierAst* node) const;
0064     QString stringForNode(VariableIdentifierAst* node) const;
0065 
0066     void visitClassDeclarationStatement(ClassDeclarationStatementAst*) override;
0067     void visitInterfaceDeclarationStatement(InterfaceDeclarationStatementAst* node) override;
0068     void visitTraitDeclarationStatement(TraitDeclarationStatementAst* node) override;
0069     void visitClassStatement(ClassStatementAst *node) override;
0070     void visitFunctionDeclarationStatement(FunctionDeclarationStatementAst* node) override;
0071     void visitClosure(ClosureAst* node) override;
0072     void visitUnaryExpression(UnaryExpressionAst* node) override;
0073     void visitFunctionCallParameterListElement(FunctionCallParameterListElementAst* node) override;
0074 
0075     /**
0076      * don't overload in other builders, use @c openNamespace and @c closeNamespace instead.
0077      */
0078     void visitNamespaceDeclarationStatement(NamespaceDeclarationStatementAst* node) override;
0079     virtual void openNamespace(NamespaceDeclarationStatementAst* parent, IdentifierAst* node, const IdentifierPair& identifier, const KDevelop::RangeInRevision& range);
0080     virtual void closeNamespace(NamespaceDeclarationStatementAst* parent, IdentifierAst* node, const IdentifierPair& identifier);
0081 
0082     virtual void addBaseType(NamespacedIdentifierAst * identifier);
0083 
0084     virtual void classContextOpened(KDevelop::DUContext* context);
0085 
0086     /// Report @p errorMsg with the range of @p node
0087     /// @see void reportError(const QString& errorMsg, KDevelop::SimpleRange range);
0088     void reportError(const QString& errorMsg, AstNode* node,
0089                         KDevelop::IProblem::Severity severity = KDevelop::IProblem::Error);
0090     /// Report @p errorMsg with the range encompassing all nodes in @p nodes
0091     /// @see void reportError(const QString& errorMsg, KDevelop::SimpleRange range);
0092     void reportError(const QString& errorMsg, QList<AstNode*> nodes,
0093                         KDevelop::IProblem::Severity severity = KDevelop::IProblem::Error);
0094     /// Report @p errorMsg with range @p range
0095     void reportError(const QString& errorMsg, KDevelop::RangeInRevision range,
0096                         KDevelop::IProblem::Severity severity = KDevelop::IProblem::Error);
0097 
0098     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, IdentifierAst* node);
0099     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, SemiReservedIdentifierAst* node, DeclarationScope declarationScope = LocalScope);
0100     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType, VariableIdentifierAst* node);
0101     KDevelop::DeclarationPointer findDeclarationImport(DeclarationType declarationType,
0102                                                        const KDevelop::QualifiedIdentifier &identifier);
0103 
0104     /// internal functions file should not be checked for errors and can get some optimizations
0105     bool m_isInternalFunctions;
0106     /// Whether semantic problems should get reported
0107     bool m_reportErrors;
0108     ///TODO: push this into kdevplatform
0109     bool m_mapAst;
0110     bool m_hadUnresolvedIdentifiers;
0111 
0112     EditorIntegrator* m_editor;
0113 
0114 private:
0115     void closeNamespaces(NamespaceDeclarationStatementAst* namespaces);
0116     NamespaceDeclarationStatementAst* m_openNamespaces;
0117 
0118 };
0119 
0120 }
0121 
0122 #endif