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

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 TYPEBUILDER_H
0008 #define TYPEBUILDER_H
0009 
0010 #include "contextbuilder.h"
0011 
0012 #include <language/duchain/builders/abstracttypebuilder.h>
0013 
0014 #include <language/duchain/types/functiontype.h>
0015 #include <language/duchain/declaration.h>
0016 #include <language/duchain/identifier.h>
0017 
0018 namespace Php
0019 {
0020 
0021 typedef KDevelop::AbstractTypeBuilder<AstNode, IdentifierAst, ContextBuilder> TypeBuilderBase;
0022 
0023 /**
0024  * Create types from an AstNode tree.
0025  *
0026  * \note This builder overrides visitDeclarator, in order to support
0027  * array types; parent classes will not have
0028  * their visitDeclarator function called.
0029  */
0030 class KDEVPHPDUCHAIN_EXPORT TypeBuilder: public TypeBuilderBase
0031 {
0032 public:
0033     TypeBuilder();
0034     ~TypeBuilder() override;
0035 
0036 protected:
0037     void visitClassDeclarationStatement(ClassDeclarationStatementAst* node) override;
0038     void visitInterfaceDeclarationStatement(InterfaceDeclarationStatementAst* node) override;
0039     void visitTraitDeclarationStatement(TraitDeclarationStatementAst* node) override;
0040     void visitClassStatement(ClassStatementAst *node) override;
0041     void visitClassVariable(ClassVariableAst *node) override;
0042     void visitConstantDeclaration(ConstantDeclarationAst* node) override;
0043     void visitClassConstantDeclaration(ClassConstantDeclarationAst* node) override;
0044     void visitParameter(ParameterAst *node) override;
0045     void visitFunctionDeclarationStatement(FunctionDeclarationStatementAst* node) override;
0046     void visitClosure(ClosureAst* node) override;
0047 
0048     void visitStatement(StatementAst* node) override;
0049     void visitAssignmentExpression(AssignmentExpressionAst* node) override;
0050     void visitStaticVar(StaticVarAst *node) override;
0051     void visitCatchItem(CatchItemAst *node) override;
0052     void visitVarExpression(VarExpressionAst *node) override;
0053 
0054     /// The declaration builder implements this and updates
0055     /// the type of the current declaration
0056     virtual void updateCurrentType();
0057 
0058     KDevelop::AbstractType::Ptr getTypeForNode(AstNode* node);
0059 
0060     /**
0061      * Opens the given context type, and sets it to be the current context type.
0062      */
0063     void openContextType(const KDevelop::AbstractType::Ptr& type)
0064     {
0065         m_contextTypes.append(type);
0066     }
0067 
0068     /**
0069      * Close the current context type.
0070      */
0071     void closeContextType()
0072     {
0073         // And the reference will be lost...
0074         m_contextTypes.pop();
0075     }
0076 
0077     /**
0078      * Retrieve the type of the current context.
0079      *
0080      * \returns the abstract type of the current context.
0081      */
0082     inline KDevelop::AbstractType::Ptr currentContextType()
0083     {
0084         if (m_contextTypes.isEmpty()) {
0085             return KDevelop::AbstractType::Ptr();
0086         } else {
0087             return m_contextTypes.top();
0088         }
0089     }
0090 
0091   /// Determine if the type builder has a type for the current context. \returns true if there is a current context type, else returns false.
0092   inline bool hasCurrentContextType() { return !m_contextTypes.isEmpty(); }
0093 
0094 private:
0095     KDevelop::FunctionType::Ptr m_currentFunctionType;
0096     QList<KDevelop::AbstractType::Ptr> m_currentFunctionParams;
0097     KDevelop::Stack<KDevelop::AbstractType::Ptr> m_contextTypes;
0098 
0099     bool m_gotTypeFromDocComment;
0100     bool m_gotReturnTypeFromDocComment;
0101     bool m_gotTypeFromTypeHint;
0102 
0103     KDevelop::AbstractType::Ptr injectParseType(QString type, AstNode* node);
0104     KDevelop::AbstractType::Ptr parseType(QString type, AstNode* node);
0105     KDevelop::AbstractType::Ptr parseSimpleType(QString type, AstNode* node);
0106     KDevelop::AbstractType::Ptr parseDocComment(AstNode* node, const QString& docCommentName);
0107     QList<KDevelop::AbstractType::Ptr> parseDocCommentParams(AstNode* node);
0108 };
0109 
0110 }
0111 
0112 #endif // TYPEBUILDER_H
0113