From 70d7fb6a0358ed26f54d5b3c4b3f80500853d876 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 20 Feb 2023 00:39:57 +0100 Subject: [PATCH] LibCpp: Make C++ AST (mostly) const-correct I cheated and used const_cast to avoid dealing with the strange pattern where we sometimes do a delayed reparenting of an AST node. --- .../Cpp/CppComprehensionEngine.cpp | 22 +- .../Cpp/CppComprehensionEngine.h | 12 +- Userland/Libraries/LibCpp/AST.cpp | 28 +- Userland/Libraries/LibCpp/AST.h | 308 +++++++++--------- Userland/Libraries/LibCpp/Parser.cpp | 108 +++--- Userland/Libraries/LibCpp/Parser.h | 76 ++--- 6 files changed, 277 insertions(+), 277 deletions(-) diff --git a/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.cpp b/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.cpp index 5b6780da198..e298a3515e0 100644 --- a/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.cpp +++ b/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.cpp @@ -331,7 +331,7 @@ Vector CppComprehensionEngine::properties_of_typ return properties; } -CppComprehensionEngine::Symbol CppComprehensionEngine::Symbol::create(StringView name, Vector const& scope, NonnullRefPtr declaration, IsLocal is_local) +CppComprehensionEngine::Symbol CppComprehensionEngine::Symbol::create(StringView name, Vector const& scope, NonnullRefPtr declaration, IsLocal is_local) { return { { name, scope }, move(declaration), is_local == IsLocal::Yes }; } @@ -416,7 +416,7 @@ Optional CppComprehensionEngine::find_declar return find_preprocessor_definition(document, identifier_position); } -RefPtr CppComprehensionEngine::find_declaration_of(DocumentData const& document, const GUI::TextPosition& identifier_position) +RefPtr CppComprehensionEngine::find_declaration_of(DocumentData const& document, const GUI::TextPosition& identifier_position) { auto node = document.parser().node_at(Cpp::Position { identifier_position.line(), identifier_position.column() }); if (!node) { @@ -509,7 +509,7 @@ static Optional get_target_declaration(ASTNode const& node, D return TargetDeclaration { TargetDeclaration::Type::Variable, name }; } -RefPtr CppComprehensionEngine::find_declaration_of(DocumentData const& document_data, ASTNode const& node) const +RefPtr CppComprehensionEngine::find_declaration_of(DocumentData const& document_data, ASTNode const& node) const { dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "find_declaration_of: {} ({})", document_data.parser().text_of_node(node), node.class_name()); @@ -669,15 +669,15 @@ Vector CppComprehensionEngine::scope_of_node(ASTNode const& node) co if (!parent->is_declaration()) return parent_scope; - auto& parent_decl = static_cast(*parent); + auto& parent_decl = static_cast(*parent); StringView containing_scope; if (parent_decl.is_namespace()) - containing_scope = static_cast(parent_decl).full_name(); + containing_scope = static_cast(parent_decl).full_name(); if (parent_decl.is_struct_or_class()) - containing_scope = static_cast(parent_decl).full_name(); + containing_scope = static_cast(parent_decl).full_name(); if (parent_decl.is_function()) - containing_scope = static_cast(parent_decl).full_name(); + containing_scope = static_cast(parent_decl).full_name(); parent_scope.append(containing_scope); return parent_scope; @@ -751,9 +751,9 @@ Optional> CppComprehensionEng return options; } -RefPtr CppComprehensionEngine::find_declaration_of(CppComprehensionEngine::DocumentData const& document, CppComprehensionEngine::SymbolName const& target_symbol_name) const +RefPtr CppComprehensionEngine::find_declaration_of(CppComprehensionEngine::DocumentData const& document, CppComprehensionEngine::SymbolName const& target_symbol_name) const { - RefPtr target_declaration; + RefPtr target_declaration; for_each_available_symbol(document, [&](Symbol const& symbol) { if (symbol.name == target_symbol_name) { target_declaration = symbol.declaration; @@ -834,7 +834,7 @@ Optional CppComprehensionEngine::ge dbgln_if(CPP_LANGUAGE_SERVER_DEBUG, "node type: {}", node->class_name()); - FunctionCall* call_node { nullptr }; + FunctionCall const* call_node { nullptr }; if (node->is_function_call()) { call_node = verify_cast(node.ptr()); @@ -880,7 +880,7 @@ Optional CppComprehensionEngine::ge Optional CppComprehensionEngine::get_function_params_hint( DocumentData const& document, - FunctionCall& call_node, + FunctionCall const& call_node, size_t argument_index) { Identifier const* callee = nullptr; diff --git a/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.h b/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.h index 8605540f079..e635afc2859 100644 --- a/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.h +++ b/Userland/Libraries/LibCodeComprehension/Cpp/CppComprehensionEngine.h @@ -47,7 +47,7 @@ private: struct Symbol { SymbolName name; - NonnullRefPtr declaration; + NonnullRefPtr declaration; // Local symbols are symbols that should not appear in a global symbol search. // For example, a variable that is declared inside a function will have is_local = true. @@ -57,7 +57,7 @@ private: No, Yes }; - static Symbol create(StringView name, Vector const& scope, NonnullRefPtr, IsLocal is_local); + static Symbol create(StringView name, Vector const& scope, NonnullRefPtr, IsLocal is_local); }; friend Traits; @@ -101,9 +101,9 @@ private: DeprecatedString type_of_property(DocumentData const&, Identifier const&) const; DeprecatedString type_of_variable(Identifier const&) const; bool is_property(ASTNode const&) const; - RefPtr find_declaration_of(DocumentData const&, ASTNode const&) const; - RefPtr find_declaration_of(DocumentData const&, SymbolName const&) const; - RefPtr find_declaration_of(DocumentData const&, const GUI::TextPosition& identifier_position); + RefPtr find_declaration_of(DocumentData const&, ASTNode const&) const; + RefPtr find_declaration_of(DocumentData const&, SymbolName const&) const; + RefPtr find_declaration_of(DocumentData const&, const GUI::TextPosition& identifier_position); enum class RecurseIntoScopes { No, @@ -134,7 +134,7 @@ private: Optional> try_autocomplete_name(DocumentData const&, ASTNode const&, Optional containing_token) const; Optional> try_autocomplete_include(DocumentData const&, Token include_path_token, Cpp::Position const& cursor_position) const; static bool is_symbol_available(Symbol const&, Vector const& current_scope, Vector const& reference_scope); - Optional get_function_params_hint(DocumentData const&, FunctionCall&, size_t argument_index); + Optional get_function_params_hint(DocumentData const&, FunctionCall const&, size_t argument_index); template void for_each_available_symbol(DocumentData const&, Func) const; diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp index 36b2bd25f14..74c16851455 100644 --- a/Userland/Libraries/LibCpp/AST.cpp +++ b/Userland/Libraries/LibCpp/AST.cpp @@ -55,9 +55,9 @@ void FunctionDeclaration::dump(FILE* output, size_t indent) const } } -NonnullRefPtrVector FunctionDeclaration::declarations() const +NonnullRefPtrVector FunctionDeclaration::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (auto& arg : m_parameters) { declarations.append(arg); } @@ -162,9 +162,9 @@ void FunctionDefinition::dump(FILE* output, size_t indent) const outln(output, "}}"); } -NonnullRefPtrVector FunctionDefinition::declarations() const +NonnullRefPtrVector FunctionDefinition::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (auto& statement : m_statements) { declarations.extend(statement.declarations()); } @@ -350,9 +350,9 @@ void StructOrClassDeclaration::dump(FILE* output, size_t indent) const member.dump(output, indent + 1); } } -NonnullRefPtrVector StructOrClassDeclaration::declarations() const +NonnullRefPtrVector StructOrClassDeclaration::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (auto& member : m_members) declarations.append(member); return declarations; @@ -458,10 +458,10 @@ void ForStatement::dump(FILE* output, size_t indent) const m_body->dump(output, indent + 1); } -NonnullRefPtrVector Statement::declarations() const +NonnullRefPtrVector Statement::declarations() const { if (is_declaration()) { - NonnullRefPtrVector vec; + NonnullRefPtrVector vec; auto const& decl = static_cast(*this); vec.empend(const_cast(decl)); return vec; @@ -469,9 +469,9 @@ NonnullRefPtrVector Statement::declarations() const return {}; } -NonnullRefPtrVector ForStatement::declarations() const +NonnullRefPtrVector ForStatement::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; if (m_init) declarations.extend(m_init->declarations()); if (m_body) @@ -479,9 +479,9 @@ NonnullRefPtrVector ForStatement::declarations() const return declarations; } -NonnullRefPtrVector BlockStatement::declarations() const +NonnullRefPtrVector BlockStatement::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; for (auto& statement : m_statements) { declarations.extend(statement.declarations()); } @@ -508,9 +508,9 @@ void IfStatement::dump(FILE* output, size_t indent) const } } -NonnullRefPtrVector IfStatement::declarations() const +NonnullRefPtrVector IfStatement::declarations() const { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; if (m_predicate) declarations.extend(m_predicate->declarations()); if (m_then) diff --git a/Userland/Libraries/LibCpp/AST.h b/Userland/Libraries/LibCpp/AST.h index b0ec6c8de9c..0e8be683d3c 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -36,7 +36,7 @@ public: template bool fast_is() const = delete; - ASTNode* parent() const { return m_parent; } + ASTNode const* parent() const { return m_parent; } Position start() const { VERIFY(m_start.has_value()); @@ -52,9 +52,9 @@ public: return m_filename; } void set_end(Position const& end) { m_end = end; } - void set_parent(ASTNode& parent) { m_parent = &parent; } + void set_parent(ASTNode const& parent) { m_parent = &parent; } - virtual NonnullRefPtrVector declarations() const { return {}; } + virtual NonnullRefPtrVector declarations() const { return {}; } virtual bool is_identifier() const { return false; } virtual bool is_member_expression() const { return false; } @@ -66,7 +66,7 @@ public: virtual bool is_dummy_node() const { return false; } protected: - ASTNode(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + ASTNode(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : m_parent(parent) , m_start(start) , m_end(end) @@ -75,7 +75,7 @@ protected: } private: - ASTNode* m_parent { nullptr }; + ASTNode const* m_parent { nullptr }; Optional m_start; Optional m_end; DeprecatedFlyString m_filename; @@ -87,17 +87,17 @@ public: virtual ~TranslationUnit() override = default; virtual StringView class_name() const override { return "TranslationUnit"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - virtual NonnullRefPtrVector declarations() const override { return m_declarations; } + virtual NonnullRefPtrVector declarations() const override { return m_declarations; } - TranslationUnit(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + TranslationUnit(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } - void set_declarations(NonnullRefPtrVector&& declarations) { m_declarations = move(declarations); } + void set_declarations(NonnullRefPtrVector&& declarations) { m_declarations = move(declarations); } private: - NonnullRefPtrVector m_declarations; + NonnullRefPtrVector m_declarations; }; class Statement : public ASTNode { @@ -105,10 +105,10 @@ public: virtual ~Statement() override = default; virtual StringView class_name() const override { return "Statement"sv; } - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; protected: - Statement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Statement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } @@ -129,15 +129,15 @@ public: bool is_member() const { return parent() != nullptr && parent()->is_declaration() && verify_cast(parent())->is_struct_or_class(); } Name const* name() const { return m_name; } StringView full_name() const; - void set_name(RefPtr name) { m_name = move(name); } + void set_name(RefPtr name) { m_name = move(name); } protected: - Declaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Declaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } - RefPtr m_name; + RefPtr m_name; mutable Optional m_full_name; }; @@ -146,7 +146,7 @@ class InvalidDeclaration : public Declaration { public: virtual ~InvalidDeclaration() override = default; virtual StringView class_name() const override { return "InvalidDeclaration"sv; } - InvalidDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + InvalidDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } @@ -160,28 +160,28 @@ public: virtual bool is_function() const override { return true; } virtual bool is_constructor() const { return false; } virtual bool is_destructor() const { return false; } - RefPtr definition() { return m_definition; } + RefPtr definition() { return m_definition; } - FunctionDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + FunctionDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; Vector const& qualifiers() const { return m_qualifiers; } void set_qualifiers(Vector const& qualifiers) { m_qualifiers = qualifiers; } Type const* return_type() const { return m_return_type.ptr(); } - void set_return_type(RefPtr const& return_type) { m_return_type = return_type; } - NonnullRefPtrVector const& parameters() const { return m_parameters; } - void set_parameters(NonnullRefPtrVector const& parameters) { m_parameters = parameters; } + void set_return_type(RefPtr const& return_type) { m_return_type = return_type; } + NonnullRefPtrVector const& parameters() const { return m_parameters; } + void set_parameters(NonnullRefPtrVector const& parameters) { m_parameters = parameters; } FunctionDefinition const* definition() const { return m_definition.ptr(); } - void set_definition(RefPtr&& definition) { m_definition = move(definition); } + void set_definition(RefPtr&& definition) { m_definition = move(definition); } private: Vector m_qualifiers; - RefPtr m_return_type; - NonnullRefPtrVector m_parameters; - RefPtr m_definition; + RefPtr m_return_type; + NonnullRefPtrVector m_parameters; + RefPtr m_definition; }; class VariableOrParameterDeclaration : public Declaration { @@ -189,16 +189,16 @@ public: virtual ~VariableOrParameterDeclaration() override = default; virtual bool is_variable_or_parameter_declaration() const override { return true; } - void set_type(RefPtr&& type) { m_type = move(type); } + void set_type(RefPtr&& type) { m_type = move(type); } Type const* type() const { return m_type.ptr(); } protected: - VariableOrParameterDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + VariableOrParameterDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - RefPtr m_type; + RefPtr m_type; }; class Parameter : public VariableOrParameterDeclaration { @@ -208,7 +208,7 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual bool is_parameter() const override { return true; } - Parameter(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, RefPtr name) + Parameter(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, RefPtr name) : VariableOrParameterDeclaration(parent, start, end, filename) { m_name = name; @@ -237,7 +237,7 @@ public: void set_qualifiers(Vector&& qualifiers) { m_qualifiers = move(qualifiers); } protected: - Type(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Type(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } @@ -254,16 +254,16 @@ public: virtual DeprecatedString to_deprecated_string() const override; virtual bool is_named_type() const override { return true; } - NamedType(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + NamedType(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } Name const* name() const { return m_name.ptr(); } - void set_name(RefPtr&& name) { m_name = move(name); } + void set_name(RefPtr&& name) { m_name = move(name); } private: - RefPtr m_name; + RefPtr m_name; }; class Pointer : public Type { @@ -273,16 +273,16 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual DeprecatedString to_deprecated_string() const override; - Pointer(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Pointer(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } Type const* pointee() const { return m_pointee.ptr(); } - void set_pointee(RefPtr&& pointee) { m_pointee = move(pointee); } + void set_pointee(RefPtr&& pointee) { m_pointee = move(pointee); } private: - RefPtr m_pointee; + RefPtr m_pointee; }; class Reference : public Type { @@ -297,14 +297,14 @@ public: Rvalue, }; - Reference(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, Kind kind) + Reference(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, Kind kind) : Type(parent, start, end, filename) , m_kind(kind) { } Type const* referenced_type() const { return m_referenced_type.ptr(); } - void set_referenced_type(RefPtr&& pointee) { m_referenced_type = move(pointee); } + void set_referenced_type(RefPtr&& pointee) { m_referenced_type = move(pointee); } Kind kind() const { return m_kind; } private: @@ -319,17 +319,17 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual DeprecatedString to_deprecated_string() const override; - FunctionType(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + FunctionType(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } void set_return_type(Type& type) { m_return_type = type; } - void set_parameters(NonnullRefPtrVector parameters) { m_parameters = move(parameters); } + void set_parameters(NonnullRefPtrVector parameters) { m_parameters = move(parameters); } private: - RefPtr m_return_type; - NonnullRefPtrVector m_parameters; + RefPtr m_return_type; + NonnullRefPtrVector m_parameters; }; class FunctionDefinition : public ASTNode { @@ -338,24 +338,24 @@ public: virtual StringView class_name() const override { return "FunctionDefinition"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - FunctionDefinition(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + FunctionDefinition(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } - virtual NonnullRefPtrVector declarations() const override; - NonnullRefPtrVector const& statements() { return m_statements; } - void add_statement(NonnullRefPtr&& statement) { m_statements.append(move(statement)); } + virtual NonnullRefPtrVector declarations() const override; + NonnullRefPtrVector const& statements() { return m_statements; } + void add_statement(NonnullRefPtr&& statement) { m_statements.append(move(statement)); } private: - NonnullRefPtrVector m_statements; + NonnullRefPtrVector m_statements; }; class InvalidStatement : public Statement { public: virtual ~InvalidStatement() override = default; virtual StringView class_name() const override { return "InvalidStatement"sv; } - InvalidStatement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + InvalidStatement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -367,7 +367,7 @@ public: virtual StringView class_name() const override { return "Expression"sv; } protected: - Expression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Expression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -377,7 +377,7 @@ class InvalidExpression : public Expression { public: virtual ~InvalidExpression() override = default; virtual StringView class_name() const override { return "InvalidExpression"sv; } - InvalidExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + InvalidExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -389,7 +389,7 @@ public: virtual StringView class_name() const override { return "VariableDeclaration"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - VariableDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + VariableDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : VariableOrParameterDeclaration(parent, start, end, filename) { } @@ -397,10 +397,10 @@ public: virtual bool is_variable_declaration() const override { return true; } Expression const* initial_value() const { return m_initial_value; } - void set_initial_value(RefPtr&& initial_value) { m_initial_value = move(initial_value); } + void set_initial_value(RefPtr&& initial_value) { m_initial_value = move(initial_value); } private: - RefPtr m_initial_value; + RefPtr m_initial_value; }; class Identifier : public Expression { @@ -409,12 +409,12 @@ public: virtual StringView class_name() const override { return "Identifier"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - Identifier(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, StringView name) + Identifier(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, StringView name) : Expression(parent, start, end, filename) , m_name(name) { } - Identifier(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Identifier(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Identifier(parent, start, end, filename, {}) { } @@ -436,21 +436,21 @@ public: virtual bool is_name() const override { return true; } virtual bool is_templatized() const { return false; } - Name(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Name(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } virtual StringView full_name() const; Identifier const* name() const { return m_name.ptr(); } - void set_name(RefPtr&& name) { m_name = move(name); } - NonnullRefPtrVector const& scope() const { return m_scope; } - void set_scope(NonnullRefPtrVector scope) { m_scope = move(scope); } - void add_to_scope(NonnullRefPtr&& part) { m_scope.append(move(part)); } + void set_name(RefPtr&& name) { m_name = move(name); } + NonnullRefPtrVector const& scope() const { return m_scope; } + void set_scope(NonnullRefPtrVector scope) { m_scope = move(scope); } + void add_to_scope(NonnullRefPtr&& part) { m_scope.append(move(part)); } private: - RefPtr m_name; - NonnullRefPtrVector m_scope; + RefPtr m_name; + NonnullRefPtrVector m_scope; mutable Optional m_full_name; }; @@ -461,15 +461,15 @@ public: virtual bool is_templatized() const override { return true; } virtual StringView full_name() const override; - TemplatizedName(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + TemplatizedName(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Name(parent, start, end, filename) { } - void add_template_argument(NonnullRefPtr&& type) { m_template_arguments.append(move(type)); } + void add_template_argument(NonnullRefPtr&& type) { m_template_arguments.append(move(type)); } private: - NonnullRefPtrVector m_template_arguments; + NonnullRefPtrVector m_template_arguments; mutable Optional m_full_name; }; @@ -479,7 +479,7 @@ public: virtual StringView class_name() const override { return "NumericLiteral"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - NumericLiteral(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, StringView value) + NumericLiteral(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, StringView value) : Expression(parent, start, end, filename) , m_value(value) { @@ -495,7 +495,7 @@ public: virtual StringView class_name() const override { return "NullPointerLiteral"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - NullPointerLiteral(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + NullPointerLiteral(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -507,7 +507,7 @@ public: virtual StringView class_name() const override { return "BooleanLiteral"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - BooleanLiteral(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, bool value) + BooleanLiteral(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, bool value) : Expression(parent, start, end, filename) , m_value(value) { @@ -541,7 +541,7 @@ enum class BinaryOp { class BinaryExpression : public Expression { public: - BinaryExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + BinaryExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -553,14 +553,14 @@ public: BinaryOp op() const { return m_op; } void set_op(BinaryOp op) { m_op = op; } Expression const* lhs() const { return m_lhs.ptr(); } - void set_lhs(RefPtr&& e) { m_lhs = move(e); } + void set_lhs(RefPtr&& e) { m_lhs = move(e); } Expression const* rhs() const { return m_rhs.ptr(); } - void set_rhs(RefPtr&& e) { m_rhs = move(e); } + void set_rhs(RefPtr&& e) { m_rhs = move(e); } private: BinaryOp m_op; - RefPtr m_lhs; - RefPtr m_rhs; + RefPtr m_lhs; + RefPtr m_rhs; }; enum class AssignmentOp { @@ -571,7 +571,7 @@ enum class AssignmentOp { class AssignmentExpression : public Expression { public: - AssignmentExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + AssignmentExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -583,19 +583,19 @@ public: AssignmentOp op() const { return m_op; } void set_op(AssignmentOp op) { m_op = op; } Expression const* lhs() const { return m_lhs; } - void set_lhs(RefPtr&& e) { m_lhs = move(e); } + void set_lhs(RefPtr&& e) { m_lhs = move(e); } Expression const* rhs() const { return m_rhs; } - void set_rhs(RefPtr&& e) { m_rhs = move(e); } + void set_rhs(RefPtr&& e) { m_rhs = move(e); } private: AssignmentOp m_op {}; - RefPtr m_lhs; - RefPtr m_rhs; + RefPtr m_lhs; + RefPtr m_rhs; }; class FunctionCall : public Expression { public: - FunctionCall(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + FunctionCall(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -606,19 +606,19 @@ public: virtual bool is_function_call() const override { return true; } Expression const* callee() const { return m_callee.ptr(); } - void set_callee(RefPtr&& callee) { m_callee = move(callee); } + void set_callee(RefPtr&& callee) { m_callee = move(callee); } - void add_argument(NonnullRefPtr&& arg) { m_arguments.append(move(arg)); } - NonnullRefPtrVector const& arguments() const { return m_arguments; } + void add_argument(NonnullRefPtr&& arg) { m_arguments.append(move(arg)); } + NonnullRefPtrVector const& arguments() const { return m_arguments; } private: - RefPtr m_callee; - NonnullRefPtrVector m_arguments; + RefPtr m_callee; + NonnullRefPtrVector m_arguments; }; class StringLiteral final : public Expression { public: - StringLiteral(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + StringLiteral(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -639,17 +639,17 @@ public: virtual ~ReturnStatement() override = default; virtual StringView class_name() const override { return "ReturnStatement"sv; } - ReturnStatement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + ReturnStatement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } virtual void dump(FILE* = stdout, size_t indent = 0) const override; Expression const* value() const { return m_value.ptr(); } - void set_value(RefPtr&& value) { m_value = move(value); } + void set_value(RefPtr&& value) { m_value = move(value); } private: - RefPtr m_value; + RefPtr m_value; }; class EnumDeclaration : public Declaration { @@ -659,7 +659,7 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual bool is_enum() const override { return true; } - EnumDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + EnumDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } @@ -670,13 +670,13 @@ public: }; void set_type(Type type) { m_type = type; } - void add_entry(StringView entry, RefPtr value = nullptr) { m_entries.append({ entry, move(value) }); } + void add_entry(StringView entry, RefPtr value = nullptr) { m_entries.append({ entry, move(value) }); } private: Type m_type { Type::RegularEnum }; struct EnumerationEntry { StringView name; - RefPtr value; + RefPtr value; }; Vector m_entries; }; @@ -689,29 +689,29 @@ public: virtual bool is_struct_or_class() const override { return true; } virtual bool is_struct() const override { return m_type == Type::Struct; } virtual bool is_class() const override { return m_type == Type::Class; } - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; enum class Type { Struct, Class }; - StructOrClassDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename, StructOrClassDeclaration::Type type) + StructOrClassDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename, StructOrClassDeclaration::Type type) : Declaration(parent, start, end, filename) , m_type(type) { } - NonnullRefPtrVector const& members() const { return m_members; } - void set_members(NonnullRefPtrVector&& members) { m_members = move(members); } + NonnullRefPtrVector const& members() const { return m_members; } + void set_members(NonnullRefPtrVector&& members) { m_members = move(members); } - NonnullRefPtrVector const& baseclasses() const { return m_baseclasses; } - void set_baseclasses(NonnullRefPtrVector&& baseclasses) { m_baseclasses = move(baseclasses); } + NonnullRefPtrVector const& baseclasses() const { return m_baseclasses; } + void set_baseclasses(NonnullRefPtrVector&& baseclasses) { m_baseclasses = move(baseclasses); } private: StructOrClassDeclaration::Type m_type; - NonnullRefPtrVector m_members; - NonnullRefPtrVector m_baseclasses; + NonnullRefPtrVector m_members; + NonnullRefPtrVector m_baseclasses; }; enum class UnaryOp { @@ -726,7 +726,7 @@ enum class UnaryOp { class UnaryExpression : public Expression { public: - UnaryExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + UnaryExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -736,16 +736,16 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; void set_op(UnaryOp op) { m_op = op; } - void set_lhs(RefPtr&& e) { m_lhs = move(e); } + void set_lhs(RefPtr&& e) { m_lhs = move(e); } private: UnaryOp m_op; - RefPtr m_lhs; + RefPtr m_lhs; }; class MemberExpression : public Expression { public: - MemberExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + MemberExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -756,18 +756,18 @@ public: virtual bool is_member_expression() const override { return true; } Expression const* object() const { return m_object.ptr(); } - void set_object(RefPtr&& object) { m_object = move(object); } + void set_object(RefPtr&& object) { m_object = move(object); } Expression const* property() const { return m_property.ptr(); } - void set_property(RefPtr&& property) { m_property = move(property); } + void set_property(RefPtr&& property) { m_property = move(property); } private: - RefPtr m_object; - RefPtr m_property; + RefPtr m_object; + RefPtr m_property; }; class ForStatement : public Statement { public: - ForStatement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + ForStatement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -776,24 +776,24 @@ public: virtual StringView class_name() const override { return "ForStatement"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; - void set_init(RefPtr&& init) { m_init = move(init); } - void set_test(RefPtr&& test) { m_test = move(test); } - void set_update(RefPtr&& update) { m_update = move(update); } - void set_body(RefPtr&& body) { m_body = move(body); } + void set_init(RefPtr&& init) { m_init = move(init); } + void set_test(RefPtr&& test) { m_test = move(test); } + void set_update(RefPtr&& update) { m_update = move(update); } + void set_body(RefPtr&& body) { m_body = move(body); } Statement const* body() const { return m_body.ptr(); } private: - RefPtr m_init; - RefPtr m_test; - RefPtr m_update; - RefPtr m_body; + RefPtr m_init; + RefPtr m_test; + RefPtr m_update; + RefPtr m_body; }; class BlockStatement final : public Statement { public: - BlockStatement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + BlockStatement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -802,17 +802,17 @@ public: virtual StringView class_name() const override { return "BlockStatement"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; - void add_statement(NonnullRefPtr&& statement) { m_statements.append(move(statement)); } + void add_statement(NonnullRefPtr&& statement) { m_statements.append(move(statement)); } private: - NonnullRefPtrVector m_statements; + NonnullRefPtrVector m_statements; }; class Comment final : public Statement { public: - Comment(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Comment(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -823,7 +823,7 @@ public: class IfStatement : public Statement { public: - IfStatement(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + IfStatement(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -831,19 +831,19 @@ public: virtual ~IfStatement() override = default; virtual StringView class_name() const override { return "IfStatement"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - virtual NonnullRefPtrVector declarations() const override; + virtual NonnullRefPtrVector declarations() const override; - void set_predicate(RefPtr&& predicate) { m_predicate = move(predicate); } - void set_then_statement(RefPtr&& then) { m_then = move(then); } - void set_else_statement(RefPtr&& _else) { m_else = move(_else); } + void set_predicate(RefPtr&& predicate) { m_predicate = move(predicate); } + void set_then_statement(RefPtr&& then) { m_then = move(then); } + void set_else_statement(RefPtr&& _else) { m_else = move(_else); } Statement const* then_statement() const { return m_then.ptr(); } Statement const* else_statement() const { return m_else.ptr(); } private: - RefPtr m_predicate; - RefPtr m_then; - RefPtr m_else; + RefPtr m_predicate; + RefPtr m_then; + RefPtr m_else; }; class NamespaceDeclaration : public Declaration { @@ -853,21 +853,21 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual bool is_namespace() const override { return true; } - NamespaceDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + NamespaceDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - virtual NonnullRefPtrVector declarations() const override { return m_declarations; } - void add_declaration(NonnullRefPtr&& declaration) { m_declarations.append(move(declaration)); } + virtual NonnullRefPtrVector declarations() const override { return m_declarations; } + void add_declaration(NonnullRefPtr&& declaration) { m_declarations.append(move(declaration)); } private: - NonnullRefPtrVector m_declarations; + NonnullRefPtrVector m_declarations; }; class CppCastExpression : public Expression { public: - CppCastExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + CppCastExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -877,18 +877,18 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; void set_cast_type(StringView cast_type) { m_cast_type = move(cast_type); } - void set_type(NonnullRefPtr&& type) { m_type = move(type); } - void set_expression(NonnullRefPtr&& e) { m_expression = move(e); } + void set_type(NonnullRefPtr&& type) { m_type = move(type); } + void set_expression(NonnullRefPtr&& e) { m_expression = move(e); } private: StringView m_cast_type; - RefPtr m_type; - RefPtr m_expression; + RefPtr m_type; + RefPtr m_expression; }; class CStyleCastExpression : public Expression { public: - CStyleCastExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + CStyleCastExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -897,17 +897,17 @@ public: virtual StringView class_name() const override { return "CStyleCastExpression"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - void set_type(NonnullRefPtr&& type) { m_type = move(type); } - void set_expression(NonnullRefPtr&& e) { m_expression = move(e); } + void set_type(NonnullRefPtr&& type) { m_type = move(type); } + void set_expression(NonnullRefPtr&& e) { m_expression = move(e); } private: - RefPtr m_type; - RefPtr m_expression; + RefPtr m_type; + RefPtr m_expression; }; class SizeofExpression : public Expression { public: - SizeofExpression(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + SizeofExpression(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -916,15 +916,15 @@ public: virtual StringView class_name() const override { return "SizeofExpression"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - void set_type(RefPtr&& type) { m_type = move(type); } + void set_type(RefPtr&& type) { m_type = move(type); } private: - RefPtr m_type; + RefPtr m_type; }; class BracedInitList : public Expression { public: - BracedInitList(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + BracedInitList(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Expression(parent, start, end, filename) { } @@ -933,15 +933,15 @@ public: virtual StringView class_name() const override { return "BracedInitList"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - void add_expression(NonnullRefPtr&& exp) { m_expressions.append(move(exp)); } + void add_expression(NonnullRefPtr&& exp) { m_expressions.append(move(exp)); } private: - NonnullRefPtrVector m_expressions; + NonnullRefPtrVector m_expressions; }; class DummyAstNode : public ASTNode { public: - DummyAstNode(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + DummyAstNode(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } @@ -957,7 +957,7 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual bool is_constructor() const override { return true; } - Constructor(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Constructor(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : FunctionDeclaration(parent, start, end, filename) { } @@ -970,7 +970,7 @@ public: virtual void dump(FILE* = stdout, size_t indent = 0) const override; virtual bool is_destructor() const override { return true; } - Destructor(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + Destructor(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : FunctionDeclaration(parent, start, end, filename) { } @@ -982,7 +982,7 @@ public: virtual StringView class_name() const override { return "UsingNamespaceDeclaration"sv; } virtual void dump(FILE* = stdout, size_t indent = 0) const override; - UsingNamespaceDeclaration(ASTNode* parent, Optional start, Optional end, DeprecatedString const& filename) + UsingNamespaceDeclaration(ASTNode const* parent, Optional start, Optional end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 89ed2a79e27..a091b64085a 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -37,9 +37,9 @@ NonnullRefPtr Parser::parse() return unit; } -NonnullRefPtrVector Parser::parse_declarations_in_translation_unit(ASTNode& parent) +NonnullRefPtrVector Parser::parse_declarations_in_translation_unit(ASTNode const& parent) { - NonnullRefPtrVector declarations; + NonnullRefPtrVector declarations; while (!eof()) { auto declaration = parse_single_declaration_in_translation_unit(parent); if (declaration) { @@ -52,7 +52,7 @@ NonnullRefPtrVector Parser::parse_declarations_in_translation_unit( return declarations; } -RefPtr Parser::parse_single_declaration_in_translation_unit(ASTNode& parent) +RefPtr Parser::parse_single_declaration_in_translation_unit(ASTNode const& parent) { while (!eof()) { if (match_comment()) { @@ -74,7 +74,7 @@ RefPtr Parser::parse_single_declaration_in_translation_unit(ASTNode return {}; } -NonnullRefPtr Parser::parse_declaration(ASTNode& parent, DeclarationType declaration_type) +NonnullRefPtr Parser::parse_declaration(ASTNode const& parent, DeclarationType declaration_type) { switch (declaration_type) { case DeclarationType::Function: @@ -99,7 +99,7 @@ NonnullRefPtr Parser::parse_declaration(ASTNode& parent, Declaratio } } -NonnullRefPtr Parser::parse_function_declaration(ASTNode& parent) +NonnullRefPtr Parser::parse_function_declaration(ASTNode const& parent) { auto func = create_ast_node(parent, position(), {}); @@ -120,7 +120,7 @@ NonnullRefPtr Parser::parse_function_declaration(ASTNode& p // FIXME: Note that this function is supposed to be a class member, and `this` has to be const, somehow. } - RefPtr body; + RefPtr body; Position func_end {}; if (peek(Token::Type::LeftCurly).has_value()) { body = parse_function_definition(*func); @@ -137,7 +137,7 @@ NonnullRefPtr Parser::parse_function_declaration(ASTNode& p return func; } -NonnullRefPtr Parser::parse_function_definition(ASTNode& parent) +NonnullRefPtr Parser::parse_function_definition(ASTNode const& parent) { LOG_SCOPE(); auto func = create_ast_node(parent, position(), {}); @@ -151,7 +151,7 @@ NonnullRefPtr Parser::parse_function_definition(ASTNode& par return func; } -NonnullRefPtr Parser::parse_statement(ASTNode& parent) +NonnullRefPtr Parser::parse_statement(ASTNode const& parent) { LOG_SCOPE(); ArmedScopeGuard consume_semicolon([this]() { @@ -190,7 +190,7 @@ NonnullRefPtr Parser::parse_statement(ASTNode& parent) } } -NonnullRefPtr Parser::parse_comment(ASTNode& parent) +NonnullRefPtr Parser::parse_comment(ASTNode const& parent) { auto comment = create_ast_node(parent, position(), {}); consume(Token::Type::Comment); @@ -203,7 +203,7 @@ bool Parser::match_block_statement() return peek().type() == Token::Type::LeftCurly; } -NonnullRefPtr Parser::parse_block_statement(ASTNode& parent) +NonnullRefPtr Parser::parse_block_statement(ASTNode const& parent) { LOG_SCOPE(); auto block_statement = create_ast_node(parent, position(), {}); @@ -259,13 +259,13 @@ bool Parser::match_template_arguments() return peek().type() == Token::Type::Greater; } -NonnullRefPtrVector Parser::parse_template_arguments(ASTNode& parent) +NonnullRefPtrVector Parser::parse_template_arguments(ASTNode const& parent) { LOG_SCOPE(); consume(Token::Type::Less); - NonnullRefPtrVector template_arguments; + NonnullRefPtrVector template_arguments; while (!eof() && peek().type() != Token::Type::Greater) { template_arguments.append(parse_type(parent)); } @@ -309,7 +309,7 @@ bool Parser::match_variable_declaration() return match(Token::Type::Semicolon); } -NonnullRefPtr Parser::parse_variable_declaration(ASTNode& parent, bool expect_semicolon) +NonnullRefPtr Parser::parse_variable_declaration(ASTNode const& parent, bool expect_semicolon) { LOG_SCOPE(); auto var = create_ast_node(parent, position(), {}); @@ -320,7 +320,7 @@ NonnullRefPtr Parser::parse_variable_declaration(ASTNode& p } var->set_type(parse_type(var)); auto name = parse_name(*var); - RefPtr initial_value; + RefPtr initial_value; if (match(Token::Type::Equals)) { consume(Token::Type::Equals); @@ -341,7 +341,7 @@ NonnullRefPtr Parser::parse_variable_declaration(ASTNode& p return var; } -NonnullRefPtr Parser::parse_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_expression(ASTNode const& parent) { LOG_SCOPE(); auto expression = parse_primary_expression(parent); @@ -350,7 +350,7 @@ NonnullRefPtr Parser::parse_expression(ASTNode& parent) return expression; } - NonnullRefPtrVector secondary_expressions; + NonnullRefPtrVector secondary_expressions; while (match_secondary_expression()) { // FIXME: Handle operator precedence @@ -359,7 +359,7 @@ NonnullRefPtr Parser::parse_expression(ASTNode& parent) } for (size_t i = 0; secondary_expressions.size() != 0 && i < secondary_expressions.size() - 1; ++i) { - secondary_expressions[i].set_parent(secondary_expressions[i + 1]); + const_cast(secondary_expressions[i]).set_parent(secondary_expressions[i + 1]); } return expression; @@ -402,7 +402,7 @@ bool Parser::match_secondary_expression() || type == Token::Type::LeftParen; } -NonnullRefPtr Parser::parse_primary_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_primary_expression(ASTNode const& parent) { LOG_SCOPE(); // TODO: remove eof() logic, should still work without it @@ -470,7 +470,7 @@ bool Parser::match_unary_expression() || type == Token::Type::And; } -NonnullRefPtr Parser::parse_unary_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_unary_expression(ASTNode const& parent) { auto unary_exp = create_ast_node(parent, position(), {}); auto op_token = consume(); @@ -504,7 +504,7 @@ NonnullRefPtr Parser::parse_unary_expression(ASTNode& parent) return unary_exp; } -NonnullRefPtr Parser::parse_literal(ASTNode& parent) +NonnullRefPtr Parser::parse_literal(ASTNode const& parent) { switch (peek().type()) { case Token::Type::Integer: { @@ -532,7 +532,7 @@ NonnullRefPtr Parser::parse_literal(ASTNode& parent) } } -NonnullRefPtr Parser::parse_secondary_expression(ASTNode& parent, NonnullRefPtr lhs) +NonnullRefPtr Parser::parse_secondary_expression(ASTNode const& parent, NonnullRefPtr lhs) { LOG_SCOPE(); switch (peek().type()) { @@ -559,7 +559,7 @@ NonnullRefPtr Parser::parse_secondary_expression(ASTNode& parent, No case Token::Type::Dot: { consume(); auto exp = create_ast_node(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast(*lhs).set_parent(*exp); exp->set_object(move(lhs)); auto identifier_token = consume(Token::Type::Identifier); exp->set_property(create_ast_node(*exp, identifier_token.start(), identifier_token.end(), identifier_token.text())); @@ -569,7 +569,7 @@ NonnullRefPtr Parser::parse_secondary_expression(ASTNode& parent, No case Token::Type::LeftParen: { consume(); auto func = create_ast_node(parent, lhs->start(), {}); - lhs->set_parent(*func); + const_cast(*lhs).set_parent(*func); func->set_callee(move(lhs)); while (peek().type() != Token::Type::RightParen && !eof()) { func->add_argument(parse_expression(*func)); @@ -588,11 +588,11 @@ NonnullRefPtr Parser::parse_secondary_expression(ASTNode& parent, No } } -NonnullRefPtr Parser::parse_binary_expression(ASTNode& parent, NonnullRefPtr lhs, BinaryOp op) +NonnullRefPtr Parser::parse_binary_expression(ASTNode const& parent, NonnullRefPtr lhs, BinaryOp op) { consume(); // Operator auto exp = create_ast_node(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast(*lhs).set_parent(*exp); exp->set_op(op); exp->set_lhs(move(lhs)); auto rhs = parse_expression(exp); @@ -601,11 +601,11 @@ NonnullRefPtr Parser::parse_binary_expression(ASTNode& parent, return exp; } -NonnullRefPtr Parser::parse_assignment_expression(ASTNode& parent, NonnullRefPtr lhs, AssignmentOp op) +NonnullRefPtr Parser::parse_assignment_expression(ASTNode const& parent, NonnullRefPtr lhs, AssignmentOp op) { consume(); // Operator auto exp = create_ast_node(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast(*lhs).set_parent(*exp); exp->set_op(op); exp->set_lhs(move(lhs)); auto rhs = parse_expression(exp); @@ -748,10 +748,10 @@ bool Parser::match_function_declaration() return false; } -Optional> Parser::parse_parameter_list(ASTNode& parent) +Optional> Parser::parse_parameter_list(ASTNode const& parent) { LOG_SCOPE(); - NonnullRefPtrVector parameters; + NonnullRefPtrVector parameters; while (peek().type() != Token::Type::RightParen && !eof()) { if (match_ellipsis()) { auto param = create_ast_node(parent, position(), {}, RefPtr {}); @@ -764,13 +764,13 @@ Optional> Parser::parse_parameter_list(ASTNode& p } else { auto type = parse_type(parent); - RefPtr name; + RefPtr name; if (match_name()) { name = parse_name(parent); } auto param = create_ast_node(parent, type->start(), !name.is_null() ? name->end() : type->end(), name); - type->set_parent(*param.ptr()); + const_cast(*type).set_parent(*param.ptr()); param->set_type(move(type)); parameters.append(move(param)); @@ -958,7 +958,7 @@ Position Parser::previous_token_end() const return m_tokens[m_state.token_index - 1].end(); } -RefPtr Parser::node_at(Position pos) const +RefPtr Parser::node_at(Position pos) const { VERIFY(m_saved_states.is_empty()); auto index = index_of_node_at(pos); @@ -1029,7 +1029,7 @@ Vector Parser::get_todo_entries() const return ret; } -NonnullRefPtr Parser::parse_string_literal(ASTNode& parent) +NonnullRefPtr Parser::parse_string_literal(ASTNode const& parent) { LOG_SCOPE(); Optional start_token_index; @@ -1063,7 +1063,7 @@ NonnullRefPtr Parser::parse_string_literal(ASTNode& parent) return string_literal; } -NonnullRefPtr Parser::parse_return_statement(ASTNode& parent) +NonnullRefPtr Parser::parse_return_statement(ASTNode const& parent) { LOG_SCOPE(); auto return_statement = create_ast_node(parent, position(), {}); @@ -1075,7 +1075,7 @@ NonnullRefPtr Parser::parse_return_statement(ASTNode& parent) return return_statement; } -NonnullRefPtr Parser::parse_enum_declaration(ASTNode& parent) +NonnullRefPtr Parser::parse_enum_declaration(ASTNode const& parent) { LOG_SCOPE(); auto enum_decl = create_ast_node(parent, position(), {}); @@ -1093,7 +1093,7 @@ NonnullRefPtr Parser::parse_enum_declaration(ASTNode& parent) consume(Token::Type::LeftCurly); while (!eof() && peek().type() != Token::Type::RightCurly) { auto name = text_of_token(consume(Token::Type::Identifier)); - RefPtr value; + RefPtr value; if (peek().type() == Token::Type::Equals) { consume(); value = parse_expression(enum_decl); @@ -1136,7 +1136,7 @@ bool Parser::match_keyword(DeprecatedString const& keyword) return true; } -NonnullRefPtr Parser::parse_class_declaration(ASTNode& parent) +NonnullRefPtr Parser::parse_class_declaration(ASTNode const& parent) { LOG_SCOPE(); @@ -1155,7 +1155,7 @@ NonnullRefPtr Parser::parse_class_declaration(ASTNode& auto has_final = match_keyword("final"); - NonnullRefPtrVector baseclasses; + NonnullRefPtrVector baseclasses; // FIXME: Don't ignore this. if (peek(has_final ? 1 : 0).type() == Token::Type::Colon) { @@ -1186,7 +1186,7 @@ NonnullRefPtr Parser::parse_class_declaration(ASTNode& return decl; } -NonnullRefPtr Parser::parse_boolean_literal(ASTNode& parent) +NonnullRefPtr Parser::parse_boolean_literal(ASTNode const& parent) { LOG_SCOPE(); auto token = consume(Token::Type::Keyword); @@ -1205,7 +1205,7 @@ bool Parser::match_boolean_literal() return text == "true" || text == "false"; } -NonnullRefPtr Parser::parse_type(ASTNode& parent) +NonnullRefPtr Parser::parse_type(ASTNode const& parent) { LOG_SCOPE(); @@ -1284,7 +1284,7 @@ NonnullRefPtr Parser::parse_type(ASTNode& parent) return type; } -NonnullRefPtr Parser::parse_for_statement(ASTNode& parent) +NonnullRefPtr Parser::parse_for_statement(ASTNode const& parent) { LOG_SCOPE(); auto for_statement = create_ast_node(parent, position(), {}); @@ -1308,7 +1308,7 @@ NonnullRefPtr Parser::parse_for_statement(ASTNode& parent) return for_statement; } -NonnullRefPtr Parser::parse_if_statement(ASTNode& parent) +NonnullRefPtr Parser::parse_if_statement(ASTNode const& parent) { LOG_SCOPE(); auto if_statement = create_ast_node(parent, position(), {}); @@ -1393,7 +1393,7 @@ bool Parser::match_ellipsis() return peek().type() == Token::Type::Dot && peek(1).type() == Token::Type::Dot && peek(2).type() == Token::Type::Dot; } -NonnullRefPtr Parser::parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace) +NonnullRefPtr Parser::parse_namespace_declaration(ASTNode const& parent, bool is_nested_namespace) { auto namespace_decl = create_ast_node(parent, position(), {}); @@ -1431,7 +1431,7 @@ bool Parser::match_name() return type == Token::Type::Identifier || type == Token::Type::KnownType; } -NonnullRefPtr Parser::parse_name(ASTNode& parent) +NonnullRefPtr Parser::parse_name(ASTNode const& parent) { LOG_SCOPE(); NonnullRefPtr name_node = create_ast_node(parent, position(), {}); @@ -1504,7 +1504,7 @@ bool Parser::match_c_style_cast_expression() return true; } -NonnullRefPtr Parser::parse_c_style_cast_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_c_style_cast_expression(ASTNode const& parent) { auto parse_exp = create_ast_node(parent, position(), {}); @@ -1517,7 +1517,7 @@ NonnullRefPtr Parser::parse_c_style_cast_expression(ASTNod return parse_exp; } -NonnullRefPtr Parser::parse_cpp_cast_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_cpp_cast_expression(ASTNode const& parent) { auto cast_expression = create_ast_node(parent, position(), {}); @@ -1541,7 +1541,7 @@ bool Parser::match_sizeof_expression() return match_keyword("sizeof"); } -NonnullRefPtr Parser::parse_sizeof_expression(ASTNode& parent) +NonnullRefPtr Parser::parse_sizeof_expression(ASTNode const& parent) { auto exp = create_ast_node(parent, position(), {}); consume(Token::Type::Keyword); @@ -1557,7 +1557,7 @@ bool Parser::match_braced_init_list() return match(Token::Type::LeftCurly); } -NonnullRefPtr Parser::parse_braced_init_list(ASTNode& parent) +NonnullRefPtr Parser::parse_braced_init_list(ASTNode const& parent) { auto init_list = create_ast_node(parent, position(), {}); @@ -1569,11 +1569,11 @@ NonnullRefPtr Parser::parse_braced_init_list(ASTNode& parent) init_list->set_end(position()); return init_list; } -NonnullRefPtrVector Parser::parse_class_members(StructOrClassDeclaration& parent) +NonnullRefPtrVector Parser::parse_class_members(StructOrClassDeclaration& parent) { auto class_name = parent.full_name(); - NonnullRefPtrVector members; + NonnullRefPtrVector members; while (!eof() && peek().type() != Token::Type::RightCurly) { if (match_access_specifier()) consume_access_specifier(); // FIXME: Do not ignore access specifiers @@ -1677,7 +1677,7 @@ void Parser::parse_constructor_or_destructor_impl(FunctionDeclaration& func, Cto // TODO: Parse =default, =delete. - RefPtr body; + RefPtr body; Position ctor_end {}; if (peek(Token::Type::LeftCurly).has_value()) { body = parse_function_definition(func); @@ -1693,14 +1693,14 @@ void Parser::parse_constructor_or_destructor_impl(FunctionDeclaration& func, Cto func.set_end(ctor_end); } -NonnullRefPtr Parser::parse_constructor(ASTNode& parent) +NonnullRefPtr Parser::parse_constructor(ASTNode const& parent) { auto ctor = create_ast_node(parent, position(), {}); parse_constructor_or_destructor_impl(*ctor, CtorOrDtor::Ctor); return ctor; } -NonnullRefPtr Parser::parse_destructor(ASTNode& parent) +NonnullRefPtr Parser::parse_destructor(ASTNode const& parent) { auto ctor = create_ast_node(parent, position(), {}); parse_constructor_or_destructor_impl(*ctor, CtorOrDtor::Dtor); @@ -1723,7 +1723,7 @@ bool Parser::match_using_namespace_declaration() return true; } -NonnullRefPtr Parser::parse_using_namespace_declaration(ASTNode& parent) +NonnullRefPtr Parser::parse_using_namespace_declaration(ASTNode const& parent) { auto decl = create_ast_node(parent, position(), {}); diff --git a/Userland/Libraries/LibCpp/Parser.h b/Userland/Libraries/LibCpp/Parser.h index 41684f08e9c..349d2eb0b13 100644 --- a/Userland/Libraries/LibCpp/Parser.h +++ b/Userland/Libraries/LibCpp/Parser.h @@ -25,7 +25,7 @@ public: NonnullRefPtr parse(); bool eof() const; - RefPtr node_at(Position) const; + RefPtr node_at(Position) const; Optional index_of_node_at(Position) const; Optional token_at(Position) const; Optional index_of_token_at(Position) const; @@ -83,45 +83,45 @@ private: bool match_destructor(StringView class_name); bool match_using_namespace_declaration(); - Optional> parse_parameter_list(ASTNode& parent); + Optional> parse_parameter_list(ASTNode const& parent); Optional consume_whitespace(); void consume_preprocessor(); - NonnullRefPtr parse_declaration(ASTNode& parent, DeclarationType); - NonnullRefPtr parse_function_declaration(ASTNode& parent); - NonnullRefPtr parse_function_definition(ASTNode& parent); - NonnullRefPtr parse_statement(ASTNode& parent); - NonnullRefPtr parse_variable_declaration(ASTNode& parent, bool expect_semicolon = true); - NonnullRefPtr parse_expression(ASTNode& parent); - NonnullRefPtr parse_primary_expression(ASTNode& parent); - NonnullRefPtr parse_secondary_expression(ASTNode& parent, NonnullRefPtr lhs); - NonnullRefPtr parse_string_literal(ASTNode& parent); - NonnullRefPtr parse_return_statement(ASTNode& parent); - NonnullRefPtr parse_enum_declaration(ASTNode& parent); - NonnullRefPtr parse_class_declaration(ASTNode& parent); - NonnullRefPtr parse_literal(ASTNode& parent); - NonnullRefPtr parse_unary_expression(ASTNode& parent); - NonnullRefPtr parse_boolean_literal(ASTNode& parent); - NonnullRefPtr parse_type(ASTNode& parent); - NonnullRefPtr parse_binary_expression(ASTNode& parent, NonnullRefPtr lhs, BinaryOp); - NonnullRefPtr parse_assignment_expression(ASTNode& parent, NonnullRefPtr lhs, AssignmentOp); - NonnullRefPtr parse_for_statement(ASTNode& parent); - NonnullRefPtr parse_block_statement(ASTNode& parent); - NonnullRefPtr parse_comment(ASTNode& parent); - NonnullRefPtr parse_if_statement(ASTNode& parent); - NonnullRefPtr parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace = false); - NonnullRefPtrVector parse_declarations_in_translation_unit(ASTNode& parent); - RefPtr parse_single_declaration_in_translation_unit(ASTNode& parent); - NonnullRefPtrVector parse_template_arguments(ASTNode& parent); - NonnullRefPtr parse_name(ASTNode& parent); - NonnullRefPtr parse_cpp_cast_expression(ASTNode& parent); - NonnullRefPtr parse_sizeof_expression(ASTNode& parent); - NonnullRefPtr parse_braced_init_list(ASTNode& parent); - NonnullRefPtr parse_c_style_cast_expression(ASTNode& parent); - NonnullRefPtrVector parse_class_members(StructOrClassDeclaration& parent); - NonnullRefPtr parse_constructor(ASTNode& parent); - NonnullRefPtr parse_destructor(ASTNode& parent); - NonnullRefPtr parse_using_namespace_declaration(ASTNode& parent); + NonnullRefPtr parse_declaration(ASTNode const& parent, DeclarationType); + NonnullRefPtr parse_function_declaration(ASTNode const& parent); + NonnullRefPtr parse_function_definition(ASTNode const& parent); + NonnullRefPtr parse_statement(ASTNode const& parent); + NonnullRefPtr parse_variable_declaration(ASTNode const& parent, bool expect_semicolon = true); + NonnullRefPtr parse_expression(ASTNode const& parent); + NonnullRefPtr parse_primary_expression(ASTNode const& parent); + NonnullRefPtr parse_secondary_expression(ASTNode const& parent, NonnullRefPtr lhs); + NonnullRefPtr parse_string_literal(ASTNode const& parent); + NonnullRefPtr parse_return_statement(ASTNode const& parent); + NonnullRefPtr parse_enum_declaration(ASTNode const& parent); + NonnullRefPtr parse_class_declaration(ASTNode const& parent); + NonnullRefPtr parse_literal(ASTNode const& parent); + NonnullRefPtr parse_unary_expression(ASTNode const& parent); + NonnullRefPtr parse_boolean_literal(ASTNode const& parent); + NonnullRefPtr parse_type(ASTNode const& parent); + NonnullRefPtr parse_binary_expression(ASTNode const& parent, NonnullRefPtr lhs, BinaryOp); + NonnullRefPtr parse_assignment_expression(ASTNode const& parent, NonnullRefPtr lhs, AssignmentOp); + NonnullRefPtr parse_for_statement(ASTNode const& parent); + NonnullRefPtr parse_block_statement(ASTNode const& parent); + NonnullRefPtr parse_comment(ASTNode const& parent); + NonnullRefPtr parse_if_statement(ASTNode const& parent); + NonnullRefPtr parse_namespace_declaration(ASTNode const& parent, bool is_nested_namespace = false); + NonnullRefPtrVector parse_declarations_in_translation_unit(ASTNode const& parent); + RefPtr parse_single_declaration_in_translation_unit(ASTNode const& parent); + NonnullRefPtrVector parse_template_arguments(ASTNode const& parent); + NonnullRefPtr parse_name(ASTNode const& parent); + NonnullRefPtr parse_cpp_cast_expression(ASTNode const& parent); + NonnullRefPtr parse_sizeof_expression(ASTNode const& parent); + NonnullRefPtr parse_braced_init_list(ASTNode const& parent); + NonnullRefPtr parse_c_style_cast_expression(ASTNode const& parent); + NonnullRefPtrVector parse_class_members(StructOrClassDeclaration& parent); + NonnullRefPtr parse_constructor(ASTNode const& parent); + NonnullRefPtr parse_destructor(ASTNode const& parent); + NonnullRefPtr parse_using_namespace_declaration(ASTNode const& parent); bool match(Token::Type); Token consume(Token::Type); @@ -145,7 +145,7 @@ private: template NonnullRefPtr - create_ast_node(ASTNode& parent, Position const& start, Optional end, Args&&... args) + create_ast_node(ASTNode const& parent, Position const& start, Optional end, Args&&... args) { auto node = adopt_ref(*new T(&parent, start, end, m_filename, forward(args)...));