diff options
author | Andreas Kling <kling@serenityos.org> | 2023-02-20 00:39:57 +0100 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2023-02-21 00:54:04 +0100 |
commit | 70d7fb6a0358ed26f54d5b3c4b3f80500853d876 (patch) | |
tree | 1390305358d09405312e0a0d9c865dd18a8c6d5e /Userland/Libraries/LibCpp | |
parent | eb4a69c377362f04c74ef3d81658b591e611b405 (diff) | |
download | serenity-70d7fb6a0358ed26f54d5b3c4b3f80500853d876.zip |
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.
Diffstat (limited to 'Userland/Libraries/LibCpp')
-rw-r--r-- | Userland/Libraries/LibCpp/AST.cpp | 28 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/AST.h | 308 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Parser.cpp | 108 | ||||
-rw-r--r-- | Userland/Libraries/LibCpp/Parser.h | 76 |
4 files changed, 260 insertions, 260 deletions
diff --git a/Userland/Libraries/LibCpp/AST.cpp b/Userland/Libraries/LibCpp/AST.cpp index 36b2bd25f1..74c1685145 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<Declaration> FunctionDeclaration::declarations() const +NonnullRefPtrVector<Declaration const> FunctionDeclaration::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> 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<Declaration> FunctionDefinition::declarations() const +NonnullRefPtrVector<Declaration const> FunctionDefinition::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> 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<Declaration> StructOrClassDeclaration::declarations() const +NonnullRefPtrVector<Declaration const> StructOrClassDeclaration::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> 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<Declaration> Statement::declarations() const +NonnullRefPtrVector<Declaration const> Statement::declarations() const { if (is_declaration()) { - NonnullRefPtrVector<Declaration> vec; + NonnullRefPtrVector<Declaration const> vec; auto const& decl = static_cast<Declaration const&>(*this); vec.empend(const_cast<Declaration&>(decl)); return vec; @@ -469,9 +469,9 @@ NonnullRefPtrVector<Declaration> Statement::declarations() const return {}; } -NonnullRefPtrVector<Declaration> ForStatement::declarations() const +NonnullRefPtrVector<Declaration const> ForStatement::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> declarations; if (m_init) declarations.extend(m_init->declarations()); if (m_body) @@ -479,9 +479,9 @@ NonnullRefPtrVector<Declaration> ForStatement::declarations() const return declarations; } -NonnullRefPtrVector<Declaration> BlockStatement::declarations() const +NonnullRefPtrVector<Declaration const> BlockStatement::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> declarations; for (auto& statement : m_statements) { declarations.extend(statement.declarations()); } @@ -508,9 +508,9 @@ void IfStatement::dump(FILE* output, size_t indent) const } } -NonnullRefPtrVector<Declaration> IfStatement::declarations() const +NonnullRefPtrVector<Declaration const> IfStatement::declarations() const { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> 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 b0ec6c8de9..0e8be683d3 100644 --- a/Userland/Libraries/LibCpp/AST.h +++ b/Userland/Libraries/LibCpp/AST.h @@ -36,7 +36,7 @@ public: template<typename T> 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<Declaration> declarations() const { return {}; } + virtual NonnullRefPtrVector<Declaration const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + ASTNode(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> m_start; Optional<Position> 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<Declaration> declarations() const override { return m_declarations; } + virtual NonnullRefPtrVector<Declaration const> declarations() const override { return m_declarations; } - TranslationUnit(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + TranslationUnit(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } - void set_declarations(NonnullRefPtrVector<Declaration>&& declarations) { m_declarations = move(declarations); } + void set_declarations(NonnullRefPtrVector<Declaration const>&& declarations) { m_declarations = move(declarations); } private: - NonnullRefPtrVector<Declaration> m_declarations; + NonnullRefPtrVector<Declaration const> 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<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; protected: - Statement(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + Statement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Declaration>(parent())->is_struct_or_class(); } Name const* name() const { return m_name; } StringView full_name() const; - void set_name(RefPtr<Name> name) { m_name = move(name); } + void set_name(RefPtr<Name const> name) { m_name = move(name); } protected: - Declaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + Declaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } - RefPtr<Name> m_name; + RefPtr<Name const> m_name; mutable Optional<DeprecatedString> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + InvalidDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<FunctionDefinition> definition() { return m_definition; } + RefPtr<FunctionDefinition const> definition() { return m_definition; } - FunctionDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + FunctionDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - virtual NonnullRefPtrVector<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; Vector<StringView> const& qualifiers() const { return m_qualifiers; } void set_qualifiers(Vector<StringView> const& qualifiers) { m_qualifiers = qualifiers; } Type const* return_type() const { return m_return_type.ptr(); } - void set_return_type(RefPtr<Type> const& return_type) { m_return_type = return_type; } - NonnullRefPtrVector<Parameter> const& parameters() const { return m_parameters; } - void set_parameters(NonnullRefPtrVector<Parameter> const& parameters) { m_parameters = parameters; } + void set_return_type(RefPtr<Type const> const& return_type) { m_return_type = return_type; } + NonnullRefPtrVector<Parameter const> const& parameters() const { return m_parameters; } + void set_parameters(NonnullRefPtrVector<Parameter const> const& parameters) { m_parameters = parameters; } FunctionDefinition const* definition() const { return m_definition.ptr(); } - void set_definition(RefPtr<FunctionDefinition>&& definition) { m_definition = move(definition); } + void set_definition(RefPtr<FunctionDefinition const>&& definition) { m_definition = move(definition); } private: Vector<StringView> m_qualifiers; - RefPtr<Type> m_return_type; - NonnullRefPtrVector<Parameter> m_parameters; - RefPtr<FunctionDefinition> m_definition; + RefPtr<Type const> m_return_type; + NonnullRefPtrVector<Parameter const> m_parameters; + RefPtr<FunctionDefinition const> 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>&& type) { m_type = move(type); } + void set_type(RefPtr<Type const>&& type) { m_type = move(type); } Type const* type() const { return m_type.ptr(); } protected: - VariableOrParameterDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + VariableOrParameterDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - RefPtr<Type> m_type; + RefPtr<Type const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename, RefPtr<Name> name) + Parameter(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename, RefPtr<Name const> name) : VariableOrParameterDeclaration(parent, start, end, filename) { m_name = name; @@ -237,7 +237,7 @@ public: void set_qualifiers(Vector<StringView>&& qualifiers) { m_qualifiers = move(qualifiers); } protected: - Type(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + Type(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + NamedType(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } Name const* name() const { return m_name.ptr(); } - void set_name(RefPtr<Name>&& name) { m_name = move(name); } + void set_name(RefPtr<Name const>&& name) { m_name = move(name); } private: - RefPtr<Name> m_name; + RefPtr<Name const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + Pointer(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } Type const* pointee() const { return m_pointee.ptr(); } - void set_pointee(RefPtr<Type>&& pointee) { m_pointee = move(pointee); } + void set_pointee(RefPtr<Type const>&& pointee) { m_pointee = move(pointee); } private: - RefPtr<Type> m_pointee; + RefPtr<Type const> m_pointee; }; class Reference : public Type { @@ -297,14 +297,14 @@ public: Rvalue, }; - Reference(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename, Kind kind) + Reference(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Type>&& pointee) { m_referenced_type = move(pointee); } + void set_referenced_type(RefPtr<Type const>&& 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + FunctionType(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Type(parent, start, end, filename) { } void set_return_type(Type& type) { m_return_type = type; } - void set_parameters(NonnullRefPtrVector<Parameter> parameters) { m_parameters = move(parameters); } + void set_parameters(NonnullRefPtrVector<Parameter const> parameters) { m_parameters = move(parameters); } private: - RefPtr<Type> m_return_type; - NonnullRefPtrVector<Parameter> m_parameters; + RefPtr<Type const> m_return_type; + NonnullRefPtrVector<Parameter const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + FunctionDefinition(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : ASTNode(parent, start, end, filename) { } - virtual NonnullRefPtrVector<Declaration> declarations() const override; - NonnullRefPtrVector<Statement> const& statements() { return m_statements; } - void add_statement(NonnullRefPtr<Statement>&& statement) { m_statements.append(move(statement)); } + virtual NonnullRefPtrVector<Declaration const> declarations() const override; + NonnullRefPtrVector<Statement const> const& statements() { return m_statements; } + void add_statement(NonnullRefPtr<Statement const>&& statement) { m_statements.append(move(statement)); } private: - NonnullRefPtrVector<Statement> m_statements; + NonnullRefPtrVector<Statement const> m_statements; }; class InvalidStatement : public Statement { public: virtual ~InvalidStatement() override = default; virtual StringView class_name() const override { return "InvalidStatement"sv; } - InvalidStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + InvalidStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + Expression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + InvalidExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + VariableDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& initial_value) { m_initial_value = move(initial_value); } + void set_initial_value(RefPtr<Expression const>&& initial_value) { m_initial_value = move(initial_value); } private: - RefPtr<Expression> m_initial_value; + RefPtr<Expression const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename, StringView name) + Identifier(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename, StringView name) : Expression(parent, start, end, filename) , m_name(name) { } - Identifier(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + Identifier(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + Name(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Identifier>&& name) { m_name = move(name); } - NonnullRefPtrVector<Identifier> const& scope() const { return m_scope; } - void set_scope(NonnullRefPtrVector<Identifier> scope) { m_scope = move(scope); } - void add_to_scope(NonnullRefPtr<Identifier>&& part) { m_scope.append(move(part)); } + void set_name(RefPtr<Identifier const>&& name) { m_name = move(name); } + NonnullRefPtrVector<Identifier const> const& scope() const { return m_scope; } + void set_scope(NonnullRefPtrVector<Identifier const> scope) { m_scope = move(scope); } + void add_to_scope(NonnullRefPtr<Identifier const>&& part) { m_scope.append(move(part)); } private: - RefPtr<Identifier> m_name; - NonnullRefPtrVector<Identifier> m_scope; + RefPtr<Identifier const> m_name; + NonnullRefPtrVector<Identifier const> m_scope; mutable Optional<DeprecatedString> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + TemplatizedName(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Name(parent, start, end, filename) { } - void add_template_argument(NonnullRefPtr<Type>&& type) { m_template_arguments.append(move(type)); } + void add_template_argument(NonnullRefPtr<Type const>&& type) { m_template_arguments.append(move(type)); } private: - NonnullRefPtrVector<Type> m_template_arguments; + NonnullRefPtrVector<Type const> m_template_arguments; mutable Optional<DeprecatedString> 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<Position> start, Optional<Position> end, DeprecatedString const& filename, StringView value) + NumericLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + NullPointerLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename, bool value) + BooleanLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + BinaryExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& e) { m_lhs = move(e); } + void set_lhs(RefPtr<Expression const>&& e) { m_lhs = move(e); } Expression const* rhs() const { return m_rhs.ptr(); } - void set_rhs(RefPtr<Expression>&& e) { m_rhs = move(e); } + void set_rhs(RefPtr<Expression const>&& e) { m_rhs = move(e); } private: BinaryOp m_op; - RefPtr<Expression> m_lhs; - RefPtr<Expression> m_rhs; + RefPtr<Expression const> m_lhs; + RefPtr<Expression const> m_rhs; }; enum class AssignmentOp { @@ -571,7 +571,7 @@ enum class AssignmentOp { class AssignmentExpression : public Expression { public: - AssignmentExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + AssignmentExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& e) { m_lhs = move(e); } + void set_lhs(RefPtr<Expression const>&& e) { m_lhs = move(e); } Expression const* rhs() const { return m_rhs; } - void set_rhs(RefPtr<Expression>&& e) { m_rhs = move(e); } + void set_rhs(RefPtr<Expression const>&& e) { m_rhs = move(e); } private: AssignmentOp m_op {}; - RefPtr<Expression> m_lhs; - RefPtr<Expression> m_rhs; + RefPtr<Expression const> m_lhs; + RefPtr<Expression const> m_rhs; }; class FunctionCall : public Expression { public: - FunctionCall(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + FunctionCall(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& callee) { m_callee = move(callee); } + void set_callee(RefPtr<Expression const>&& callee) { m_callee = move(callee); } - void add_argument(NonnullRefPtr<Expression>&& arg) { m_arguments.append(move(arg)); } - NonnullRefPtrVector<Expression> const& arguments() const { return m_arguments; } + void add_argument(NonnullRefPtr<Expression const>&& arg) { m_arguments.append(move(arg)); } + NonnullRefPtrVector<Expression const> const& arguments() const { return m_arguments; } private: - RefPtr<Expression> m_callee; - NonnullRefPtrVector<Expression> m_arguments; + RefPtr<Expression const> m_callee; + NonnullRefPtrVector<Expression const> m_arguments; }; class StringLiteral final : public Expression { public: - StringLiteral(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + StringLiteral(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + ReturnStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& value) { m_value = move(value); } + void set_value(RefPtr<Expression const>&& value) { m_value = move(value); } private: - RefPtr<Expression> m_value; + RefPtr<Expression const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + EnumDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression> value = nullptr) { m_entries.append({ entry, move(value) }); } + void add_entry(StringView entry, RefPtr<Expression const> value = nullptr) { m_entries.append({ entry, move(value) }); } private: Type m_type { Type::RegularEnum }; struct EnumerationEntry { StringView name; - RefPtr<Expression> value; + RefPtr<Expression const> value; }; Vector<EnumerationEntry> 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<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; enum class Type { Struct, Class }; - StructOrClassDeclaration(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename, StructOrClassDeclaration::Type type) + StructOrClassDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename, StructOrClassDeclaration::Type type) : Declaration(parent, start, end, filename) , m_type(type) { } - NonnullRefPtrVector<Declaration> const& members() const { return m_members; } - void set_members(NonnullRefPtrVector<Declaration>&& members) { m_members = move(members); } + NonnullRefPtrVector<Declaration const> const& members() const { return m_members; } + void set_members(NonnullRefPtrVector<Declaration const>&& members) { m_members = move(members); } - NonnullRefPtrVector<Name> const& baseclasses() const { return m_baseclasses; } - void set_baseclasses(NonnullRefPtrVector<Name>&& baseclasses) { m_baseclasses = move(baseclasses); } + NonnullRefPtrVector<Name const> const& baseclasses() const { return m_baseclasses; } + void set_baseclasses(NonnullRefPtrVector<Name const>&& baseclasses) { m_baseclasses = move(baseclasses); } private: StructOrClassDeclaration::Type m_type; - NonnullRefPtrVector<Declaration> m_members; - NonnullRefPtrVector<Name> m_baseclasses; + NonnullRefPtrVector<Declaration const> m_members; + NonnullRefPtrVector<Name const> m_baseclasses; }; enum class UnaryOp { @@ -726,7 +726,7 @@ enum class UnaryOp { class UnaryExpression : public Expression { public: - UnaryExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + UnaryExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& e) { m_lhs = move(e); } + void set_lhs(RefPtr<Expression const>&& e) { m_lhs = move(e); } private: UnaryOp m_op; - RefPtr<Expression> m_lhs; + RefPtr<Expression const> m_lhs; }; class MemberExpression : public Expression { public: - MemberExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + MemberExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& object) { m_object = move(object); } + void set_object(RefPtr<Expression const>&& object) { m_object = move(object); } Expression const* property() const { return m_property.ptr(); } - void set_property(RefPtr<Expression>&& property) { m_property = move(property); } + void set_property(RefPtr<Expression const>&& property) { m_property = move(property); } private: - RefPtr<Expression> m_object; - RefPtr<Expression> m_property; + RefPtr<Expression const> m_object; + RefPtr<Expression const> m_property; }; class ForStatement : public Statement { public: - ForStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + ForStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; - void set_init(RefPtr<VariableDeclaration>&& init) { m_init = move(init); } - void set_test(RefPtr<Expression>&& test) { m_test = move(test); } - void set_update(RefPtr<Expression>&& update) { m_update = move(update); } - void set_body(RefPtr<Statement>&& body) { m_body = move(body); } + void set_init(RefPtr<VariableDeclaration const>&& init) { m_init = move(init); } + void set_test(RefPtr<Expression const>&& test) { m_test = move(test); } + void set_update(RefPtr<Expression const>&& update) { m_update = move(update); } + void set_body(RefPtr<Statement const>&& body) { m_body = move(body); } Statement const* body() const { return m_body.ptr(); } private: - RefPtr<VariableDeclaration> m_init; - RefPtr<Expression> m_test; - RefPtr<Expression> m_update; - RefPtr<Statement> m_body; + RefPtr<VariableDeclaration const> m_init; + RefPtr<Expression const> m_test; + RefPtr<Expression const> m_update; + RefPtr<Statement const> m_body; }; class BlockStatement final : public Statement { public: - BlockStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + BlockStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; - void add_statement(NonnullRefPtr<Statement>&& statement) { m_statements.append(move(statement)); } + void add_statement(NonnullRefPtr<Statement const>&& statement) { m_statements.append(move(statement)); } private: - NonnullRefPtrVector<Statement> m_statements; + NonnullRefPtrVector<Statement const> m_statements; }; class Comment final : public Statement { public: - Comment(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + Comment(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Statement(parent, start, end, filename) { } @@ -823,7 +823,7 @@ public: class IfStatement : public Statement { public: - IfStatement(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + IfStatement(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Declaration> declarations() const override; + virtual NonnullRefPtrVector<Declaration const> declarations() const override; - void set_predicate(RefPtr<Expression>&& predicate) { m_predicate = move(predicate); } - void set_then_statement(RefPtr<Statement>&& then) { m_then = move(then); } - void set_else_statement(RefPtr<Statement>&& _else) { m_else = move(_else); } + void set_predicate(RefPtr<Expression const>&& predicate) { m_predicate = move(predicate); } + void set_then_statement(RefPtr<Statement const>&& then) { m_then = move(then); } + void set_else_statement(RefPtr<Statement const>&& _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<Expression> m_predicate; - RefPtr<Statement> m_then; - RefPtr<Statement> m_else; + RefPtr<Expression const> m_predicate; + RefPtr<Statement const> m_then; + RefPtr<Statement const> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + NamespaceDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } - virtual NonnullRefPtrVector<Declaration> declarations() const override { return m_declarations; } - void add_declaration(NonnullRefPtr<Declaration>&& declaration) { m_declarations.append(move(declaration)); } + virtual NonnullRefPtrVector<Declaration const> declarations() const override { return m_declarations; } + void add_declaration(NonnullRefPtr<Declaration const>&& declaration) { m_declarations.append(move(declaration)); } private: - NonnullRefPtrVector<Declaration> m_declarations; + NonnullRefPtrVector<Declaration const> m_declarations; }; class CppCastExpression : public Expression { public: - CppCastExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + CppCastExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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>&& type) { m_type = move(type); } - void set_expression(NonnullRefPtr<Expression>&& e) { m_expression = move(e); } + void set_type(NonnullRefPtr<Type const>&& type) { m_type = move(type); } + void set_expression(NonnullRefPtr<Expression const>&& e) { m_expression = move(e); } private: StringView m_cast_type; - RefPtr<Type> m_type; - RefPtr<Expression> m_expression; + RefPtr<Type const> m_type; + RefPtr<Expression const> m_expression; }; class CStyleCastExpression : public Expression { public: - CStyleCastExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + CStyleCastExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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>&& type) { m_type = move(type); } - void set_expression(NonnullRefPtr<Expression>&& e) { m_expression = move(e); } + void set_type(NonnullRefPtr<Type const>&& type) { m_type = move(type); } + void set_expression(NonnullRefPtr<Expression const>&& e) { m_expression = move(e); } private: - RefPtr<Type> m_type; - RefPtr<Expression> m_expression; + RefPtr<Type const> m_type; + RefPtr<Expression const> m_expression; }; class SizeofExpression : public Expression { public: - SizeofExpression(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + SizeofExpression(ASTNode const* parent, Optional<Position> start, Optional<Position> 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>&& type) { m_type = move(type); } + void set_type(RefPtr<Type const>&& type) { m_type = move(type); } private: - RefPtr<Type> m_type; + RefPtr<Type const> m_type; }; class BracedInitList : public Expression { public: - BracedInitList(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + BracedInitList(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Expression>&& exp) { m_expressions.append(move(exp)); } + void add_expression(NonnullRefPtr<Expression const>&& exp) { m_expressions.append(move(exp)); } private: - NonnullRefPtrVector<Expression> m_expressions; + NonnullRefPtrVector<Expression const> m_expressions; }; class DummyAstNode : public ASTNode { public: - DummyAstNode(ASTNode* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) + DummyAstNode(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + Constructor(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + Destructor(ASTNode const* parent, Optional<Position> start, Optional<Position> 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<Position> start, Optional<Position> end, DeprecatedString const& filename) + UsingNamespaceDeclaration(ASTNode const* parent, Optional<Position> start, Optional<Position> end, DeprecatedString const& filename) : Declaration(parent, start, end, filename) { } diff --git a/Userland/Libraries/LibCpp/Parser.cpp b/Userland/Libraries/LibCpp/Parser.cpp index 89ed2a79e2..a091b64085 100644 --- a/Userland/Libraries/LibCpp/Parser.cpp +++ b/Userland/Libraries/LibCpp/Parser.cpp @@ -37,9 +37,9 @@ NonnullRefPtr<TranslationUnit> Parser::parse() return unit; } -NonnullRefPtrVector<Declaration> Parser::parse_declarations_in_translation_unit(ASTNode& parent) +NonnullRefPtrVector<Declaration const> Parser::parse_declarations_in_translation_unit(ASTNode const& parent) { - NonnullRefPtrVector<Declaration> declarations; + NonnullRefPtrVector<Declaration const> declarations; while (!eof()) { auto declaration = parse_single_declaration_in_translation_unit(parent); if (declaration) { @@ -52,7 +52,7 @@ NonnullRefPtrVector<Declaration> Parser::parse_declarations_in_translation_unit( return declarations; } -RefPtr<Declaration> Parser::parse_single_declaration_in_translation_unit(ASTNode& parent) +RefPtr<Declaration const> Parser::parse_single_declaration_in_translation_unit(ASTNode const& parent) { while (!eof()) { if (match_comment()) { @@ -74,7 +74,7 @@ RefPtr<Declaration> Parser::parse_single_declaration_in_translation_unit(ASTNode return {}; } -NonnullRefPtr<Declaration> Parser::parse_declaration(ASTNode& parent, DeclarationType declaration_type) +NonnullRefPtr<Declaration const> Parser::parse_declaration(ASTNode const& parent, DeclarationType declaration_type) { switch (declaration_type) { case DeclarationType::Function: @@ -99,7 +99,7 @@ NonnullRefPtr<Declaration> Parser::parse_declaration(ASTNode& parent, Declaratio } } -NonnullRefPtr<FunctionDeclaration> Parser::parse_function_declaration(ASTNode& parent) +NonnullRefPtr<FunctionDeclaration const> Parser::parse_function_declaration(ASTNode const& parent) { auto func = create_ast_node<FunctionDeclaration>(parent, position(), {}); @@ -120,7 +120,7 @@ NonnullRefPtr<FunctionDeclaration> 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<FunctionDefinition> body; + RefPtr<FunctionDefinition const> body; Position func_end {}; if (peek(Token::Type::LeftCurly).has_value()) { body = parse_function_definition(*func); @@ -137,7 +137,7 @@ NonnullRefPtr<FunctionDeclaration> Parser::parse_function_declaration(ASTNode& p return func; } -NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& parent) +NonnullRefPtr<FunctionDefinition const> Parser::parse_function_definition(ASTNode const& parent) { LOG_SCOPE(); auto func = create_ast_node<FunctionDefinition>(parent, position(), {}); @@ -151,7 +151,7 @@ NonnullRefPtr<FunctionDefinition> Parser::parse_function_definition(ASTNode& par return func; } -NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent) +NonnullRefPtr<Statement const> Parser::parse_statement(ASTNode const& parent) { LOG_SCOPE(); ArmedScopeGuard consume_semicolon([this]() { @@ -190,7 +190,7 @@ NonnullRefPtr<Statement> Parser::parse_statement(ASTNode& parent) } } -NonnullRefPtr<Comment> Parser::parse_comment(ASTNode& parent) +NonnullRefPtr<Comment const> Parser::parse_comment(ASTNode const& parent) { auto comment = create_ast_node<Comment>(parent, position(), {}); consume(Token::Type::Comment); @@ -203,7 +203,7 @@ bool Parser::match_block_statement() return peek().type() == Token::Type::LeftCurly; } -NonnullRefPtr<BlockStatement> Parser::parse_block_statement(ASTNode& parent) +NonnullRefPtr<BlockStatement const> Parser::parse_block_statement(ASTNode const& parent) { LOG_SCOPE(); auto block_statement = create_ast_node<BlockStatement>(parent, position(), {}); @@ -259,13 +259,13 @@ bool Parser::match_template_arguments() return peek().type() == Token::Type::Greater; } -NonnullRefPtrVector<Type> Parser::parse_template_arguments(ASTNode& parent) +NonnullRefPtrVector<Type const> Parser::parse_template_arguments(ASTNode const& parent) { LOG_SCOPE(); consume(Token::Type::Less); - NonnullRefPtrVector<Type> template_arguments; + NonnullRefPtrVector<Type const> 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<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& parent, bool expect_semicolon) +NonnullRefPtr<VariableDeclaration const> Parser::parse_variable_declaration(ASTNode const& parent, bool expect_semicolon) { LOG_SCOPE(); auto var = create_ast_node<VariableDeclaration>(parent, position(), {}); @@ -320,7 +320,7 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& p } var->set_type(parse_type(var)); auto name = parse_name(*var); - RefPtr<Expression> initial_value; + RefPtr<Expression const> initial_value; if (match(Token::Type::Equals)) { consume(Token::Type::Equals); @@ -341,7 +341,7 @@ NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration(ASTNode& p return var; } -NonnullRefPtr<Expression> Parser::parse_expression(ASTNode& parent) +NonnullRefPtr<Expression const> Parser::parse_expression(ASTNode const& parent) { LOG_SCOPE(); auto expression = parse_primary_expression(parent); @@ -350,7 +350,7 @@ NonnullRefPtr<Expression> Parser::parse_expression(ASTNode& parent) return expression; } - NonnullRefPtrVector<Expression> secondary_expressions; + NonnullRefPtrVector<Expression const> secondary_expressions; while (match_secondary_expression()) { // FIXME: Handle operator precedence @@ -359,7 +359,7 @@ NonnullRefPtr<Expression> 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<Expression&>(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<Expression> Parser::parse_primary_expression(ASTNode& parent) +NonnullRefPtr<Expression const> 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<UnaryExpression> Parser::parse_unary_expression(ASTNode& parent) +NonnullRefPtr<UnaryExpression const> Parser::parse_unary_expression(ASTNode const& parent) { auto unary_exp = create_ast_node<UnaryExpression>(parent, position(), {}); auto op_token = consume(); @@ -504,7 +504,7 @@ NonnullRefPtr<UnaryExpression> Parser::parse_unary_expression(ASTNode& parent) return unary_exp; } -NonnullRefPtr<Expression> Parser::parse_literal(ASTNode& parent) +NonnullRefPtr<Expression const> Parser::parse_literal(ASTNode const& parent) { switch (peek().type()) { case Token::Type::Integer: { @@ -532,7 +532,7 @@ NonnullRefPtr<Expression> Parser::parse_literal(ASTNode& parent) } } -NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs) +NonnullRefPtr<Expression const> Parser::parse_secondary_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs) { LOG_SCOPE(); switch (peek().type()) { @@ -559,7 +559,7 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, No case Token::Type::Dot: { consume(); auto exp = create_ast_node<MemberExpression>(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast<Expression&>(*lhs).set_parent(*exp); exp->set_object(move(lhs)); auto identifier_token = consume(Token::Type::Identifier); exp->set_property(create_ast_node<Identifier>(*exp, identifier_token.start(), identifier_token.end(), identifier_token.text())); @@ -569,7 +569,7 @@ NonnullRefPtr<Expression> Parser::parse_secondary_expression(ASTNode& parent, No case Token::Type::LeftParen: { consume(); auto func = create_ast_node<FunctionCall>(parent, lhs->start(), {}); - lhs->set_parent(*func); + const_cast<Expression&>(*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<Expression> Parser::parse_secondary_expression(ASTNode& parent, No } } -NonnullRefPtr<BinaryExpression> Parser::parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp op) +NonnullRefPtr<BinaryExpression const> Parser::parse_binary_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs, BinaryOp op) { consume(); // Operator auto exp = create_ast_node<BinaryExpression>(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast<Expression&>(*lhs).set_parent(*exp); exp->set_op(op); exp->set_lhs(move(lhs)); auto rhs = parse_expression(exp); @@ -601,11 +601,11 @@ NonnullRefPtr<BinaryExpression> Parser::parse_binary_expression(ASTNode& parent, return exp; } -NonnullRefPtr<AssignmentExpression> Parser::parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp op) +NonnullRefPtr<AssignmentExpression const> Parser::parse_assignment_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs, AssignmentOp op) { consume(); // Operator auto exp = create_ast_node<AssignmentExpression>(parent, lhs->start(), {}); - lhs->set_parent(*exp); + const_cast<Expression&>(*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<NonnullRefPtrVector<Parameter>> Parser::parse_parameter_list(ASTNode& parent) +Optional<NonnullRefPtrVector<Parameter const>> Parser::parse_parameter_list(ASTNode const& parent) { LOG_SCOPE(); - NonnullRefPtrVector<Parameter> parameters; + NonnullRefPtrVector<Parameter const> parameters; while (peek().type() != Token::Type::RightParen && !eof()) { if (match_ellipsis()) { auto param = create_ast_node<Parameter>(parent, position(), {}, RefPtr<Name> {}); @@ -764,13 +764,13 @@ Optional<NonnullRefPtrVector<Parameter>> Parser::parse_parameter_list(ASTNode& p } else { auto type = parse_type(parent); - RefPtr<Name> name; + RefPtr<Name const> name; if (match_name()) { name = parse_name(parent); } auto param = create_ast_node<Parameter>(parent, type->start(), !name.is_null() ? name->end() : type->end(), name); - type->set_parent(*param.ptr()); + const_cast<Type&>(*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<ASTNode> Parser::node_at(Position pos) const +RefPtr<ASTNode const> Parser::node_at(Position pos) const { VERIFY(m_saved_states.is_empty()); auto index = index_of_node_at(pos); @@ -1029,7 +1029,7 @@ Vector<CodeComprehension::TodoEntry> Parser::get_todo_entries() const return ret; } -NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent) +NonnullRefPtr<StringLiteral const> Parser::parse_string_literal(ASTNode const& parent) { LOG_SCOPE(); Optional<size_t> start_token_index; @@ -1063,7 +1063,7 @@ NonnullRefPtr<StringLiteral> Parser::parse_string_literal(ASTNode& parent) return string_literal; } -NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent) +NonnullRefPtr<ReturnStatement const> Parser::parse_return_statement(ASTNode const& parent) { LOG_SCOPE(); auto return_statement = create_ast_node<ReturnStatement>(parent, position(), {}); @@ -1075,7 +1075,7 @@ NonnullRefPtr<ReturnStatement> Parser::parse_return_statement(ASTNode& parent) return return_statement; } -NonnullRefPtr<EnumDeclaration> Parser::parse_enum_declaration(ASTNode& parent) +NonnullRefPtr<EnumDeclaration const> Parser::parse_enum_declaration(ASTNode const& parent) { LOG_SCOPE(); auto enum_decl = create_ast_node<EnumDeclaration>(parent, position(), {}); @@ -1093,7 +1093,7 @@ NonnullRefPtr<EnumDeclaration> 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<Expression> value; + RefPtr<Expression const> 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<StructOrClassDeclaration> Parser::parse_class_declaration(ASTNode& parent) +NonnullRefPtr<StructOrClassDeclaration const> Parser::parse_class_declaration(ASTNode const& parent) { LOG_SCOPE(); @@ -1155,7 +1155,7 @@ NonnullRefPtr<StructOrClassDeclaration> Parser::parse_class_declaration(ASTNode& auto has_final = match_keyword("final"); - NonnullRefPtrVector<Name> baseclasses; + NonnullRefPtrVector<Name const> baseclasses; // FIXME: Don't ignore this. if (peek(has_final ? 1 : 0).type() == Token::Type::Colon) { @@ -1186,7 +1186,7 @@ NonnullRefPtr<StructOrClassDeclaration> Parser::parse_class_declaration(ASTNode& return decl; } -NonnullRefPtr<BooleanLiteral> Parser::parse_boolean_literal(ASTNode& parent) +NonnullRefPtr<BooleanLiteral const> 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<Type> Parser::parse_type(ASTNode& parent) +NonnullRefPtr<Type const> Parser::parse_type(ASTNode const& parent) { LOG_SCOPE(); @@ -1284,7 +1284,7 @@ NonnullRefPtr<Type> Parser::parse_type(ASTNode& parent) return type; } -NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent) +NonnullRefPtr<ForStatement const> Parser::parse_for_statement(ASTNode const& parent) { LOG_SCOPE(); auto for_statement = create_ast_node<ForStatement>(parent, position(), {}); @@ -1308,7 +1308,7 @@ NonnullRefPtr<ForStatement> Parser::parse_for_statement(ASTNode& parent) return for_statement; } -NonnullRefPtr<IfStatement> Parser::parse_if_statement(ASTNode& parent) +NonnullRefPtr<IfStatement const> Parser::parse_if_statement(ASTNode const& parent) { LOG_SCOPE(); auto if_statement = create_ast_node<IfStatement>(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<NamespaceDeclaration> Parser::parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace) +NonnullRefPtr<NamespaceDeclaration const> Parser::parse_namespace_declaration(ASTNode const& parent, bool is_nested_namespace) { auto namespace_decl = create_ast_node<NamespaceDeclaration>(parent, position(), {}); @@ -1431,7 +1431,7 @@ bool Parser::match_name() return type == Token::Type::Identifier || type == Token::Type::KnownType; } -NonnullRefPtr<Name> Parser::parse_name(ASTNode& parent) +NonnullRefPtr<Name const> Parser::parse_name(ASTNode const& parent) { LOG_SCOPE(); NonnullRefPtr<Name> name_node = create_ast_node<Name>(parent, position(), {}); @@ -1504,7 +1504,7 @@ bool Parser::match_c_style_cast_expression() return true; } -NonnullRefPtr<CStyleCastExpression> Parser::parse_c_style_cast_expression(ASTNode& parent) +NonnullRefPtr<CStyleCastExpression const> Parser::parse_c_style_cast_expression(ASTNode const& parent) { auto parse_exp = create_ast_node<CStyleCastExpression>(parent, position(), {}); @@ -1517,7 +1517,7 @@ NonnullRefPtr<CStyleCastExpression> Parser::parse_c_style_cast_expression(ASTNod return parse_exp; } -NonnullRefPtr<CppCastExpression> Parser::parse_cpp_cast_expression(ASTNode& parent) +NonnullRefPtr<CppCastExpression const> Parser::parse_cpp_cast_expression(ASTNode const& parent) { auto cast_expression = create_ast_node<CppCastExpression>(parent, position(), {}); @@ -1541,7 +1541,7 @@ bool Parser::match_sizeof_expression() return match_keyword("sizeof"); } -NonnullRefPtr<SizeofExpression> Parser::parse_sizeof_expression(ASTNode& parent) +NonnullRefPtr<SizeofExpression const> Parser::parse_sizeof_expression(ASTNode const& parent) { auto exp = create_ast_node<SizeofExpression>(parent, position(), {}); consume(Token::Type::Keyword); @@ -1557,7 +1557,7 @@ bool Parser::match_braced_init_list() return match(Token::Type::LeftCurly); } -NonnullRefPtr<BracedInitList> Parser::parse_braced_init_list(ASTNode& parent) +NonnullRefPtr<BracedInitList const> Parser::parse_braced_init_list(ASTNode const& parent) { auto init_list = create_ast_node<BracedInitList>(parent, position(), {}); @@ -1569,11 +1569,11 @@ NonnullRefPtr<BracedInitList> Parser::parse_braced_init_list(ASTNode& parent) init_list->set_end(position()); return init_list; } -NonnullRefPtrVector<Declaration> Parser::parse_class_members(StructOrClassDeclaration& parent) +NonnullRefPtrVector<Declaration const> Parser::parse_class_members(StructOrClassDeclaration& parent) { auto class_name = parent.full_name(); - NonnullRefPtrVector<Declaration> members; + NonnullRefPtrVector<Declaration const> 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<FunctionDefinition> body; + RefPtr<FunctionDefinition const> 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<Constructor> Parser::parse_constructor(ASTNode& parent) +NonnullRefPtr<Constructor const> Parser::parse_constructor(ASTNode const& parent) { auto ctor = create_ast_node<Constructor>(parent, position(), {}); parse_constructor_or_destructor_impl(*ctor, CtorOrDtor::Ctor); return ctor; } -NonnullRefPtr<Destructor> Parser::parse_destructor(ASTNode& parent) +NonnullRefPtr<Destructor const> Parser::parse_destructor(ASTNode const& parent) { auto ctor = create_ast_node<Destructor>(parent, position(), {}); parse_constructor_or_destructor_impl(*ctor, CtorOrDtor::Dtor); @@ -1723,7 +1723,7 @@ bool Parser::match_using_namespace_declaration() return true; } -NonnullRefPtr<UsingNamespaceDeclaration> Parser::parse_using_namespace_declaration(ASTNode& parent) +NonnullRefPtr<UsingNamespaceDeclaration const> Parser::parse_using_namespace_declaration(ASTNode const& parent) { auto decl = create_ast_node<UsingNamespaceDeclaration>(parent, position(), {}); diff --git a/Userland/Libraries/LibCpp/Parser.h b/Userland/Libraries/LibCpp/Parser.h index 41684f08e9..349d2eb0b1 100644 --- a/Userland/Libraries/LibCpp/Parser.h +++ b/Userland/Libraries/LibCpp/Parser.h @@ -25,7 +25,7 @@ public: NonnullRefPtr<TranslationUnit> parse(); bool eof() const; - RefPtr<ASTNode> node_at(Position) const; + RefPtr<ASTNode const> node_at(Position) const; Optional<size_t> index_of_node_at(Position) const; Optional<Token> token_at(Position) const; Optional<size_t> index_of_token_at(Position) const; @@ -83,45 +83,45 @@ private: bool match_destructor(StringView class_name); bool match_using_namespace_declaration(); - Optional<NonnullRefPtrVector<Parameter>> parse_parameter_list(ASTNode& parent); + Optional<NonnullRefPtrVector<Parameter const>> parse_parameter_list(ASTNode const& parent); Optional<Token> consume_whitespace(); void consume_preprocessor(); - NonnullRefPtr<Declaration> parse_declaration(ASTNode& parent, DeclarationType); - NonnullRefPtr<FunctionDeclaration> parse_function_declaration(ASTNode& parent); - NonnullRefPtr<FunctionDefinition> parse_function_definition(ASTNode& parent); - NonnullRefPtr<Statement> parse_statement(ASTNode& parent); - NonnullRefPtr<VariableDeclaration> parse_variable_declaration(ASTNode& parent, bool expect_semicolon = true); - NonnullRefPtr<Expression> parse_expression(ASTNode& parent); - NonnullRefPtr<Expression> parse_primary_expression(ASTNode& parent); - NonnullRefPtr<Expression> parse_secondary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs); - NonnullRefPtr<StringLiteral> parse_string_literal(ASTNode& parent); - NonnullRefPtr<ReturnStatement> parse_return_statement(ASTNode& parent); - NonnullRefPtr<EnumDeclaration> parse_enum_declaration(ASTNode& parent); - NonnullRefPtr<StructOrClassDeclaration> parse_class_declaration(ASTNode& parent); - NonnullRefPtr<Expression> parse_literal(ASTNode& parent); - NonnullRefPtr<UnaryExpression> parse_unary_expression(ASTNode& parent); - NonnullRefPtr<BooleanLiteral> parse_boolean_literal(ASTNode& parent); - NonnullRefPtr<Type> parse_type(ASTNode& parent); - NonnullRefPtr<BinaryExpression> parse_binary_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, BinaryOp); - NonnullRefPtr<AssignmentExpression> parse_assignment_expression(ASTNode& parent, NonnullRefPtr<Expression> lhs, AssignmentOp); - NonnullRefPtr<ForStatement> parse_for_statement(ASTNode& parent); - NonnullRefPtr<BlockStatement> parse_block_statement(ASTNode& parent); - NonnullRefPtr<Comment> parse_comment(ASTNode& parent); - NonnullRefPtr<IfStatement> parse_if_statement(ASTNode& parent); - NonnullRefPtr<NamespaceDeclaration> parse_namespace_declaration(ASTNode& parent, bool is_nested_namespace = false); - NonnullRefPtrVector<Declaration> parse_declarations_in_translation_unit(ASTNode& parent); - RefPtr<Declaration> parse_single_declaration_in_translation_unit(ASTNode& parent); - NonnullRefPtrVector<Type> parse_template_arguments(ASTNode& parent); - NonnullRefPtr<Name> parse_name(ASTNode& parent); - NonnullRefPtr<CppCastExpression> parse_cpp_cast_expression(ASTNode& parent); - NonnullRefPtr<SizeofExpression> parse_sizeof_expression(ASTNode& parent); - NonnullRefPtr<BracedInitList> parse_braced_init_list(ASTNode& parent); - NonnullRefPtr<CStyleCastExpression> parse_c_style_cast_expression(ASTNode& parent); - NonnullRefPtrVector<Declaration> parse_class_members(StructOrClassDeclaration& parent); - NonnullRefPtr<Constructor> parse_constructor(ASTNode& parent); - NonnullRefPtr<Destructor> parse_destructor(ASTNode& parent); - NonnullRefPtr<UsingNamespaceDeclaration> parse_using_namespace_declaration(ASTNode& parent); + NonnullRefPtr<Declaration const> parse_declaration(ASTNode const& parent, DeclarationType); + NonnullRefPtr<FunctionDeclaration const> parse_function_declaration(ASTNode const& parent); + NonnullRefPtr<FunctionDefinition const> parse_function_definition(ASTNode const& parent); + NonnullRefPtr<Statement const> parse_statement(ASTNode const& parent); + NonnullRefPtr<VariableDeclaration const> parse_variable_declaration(ASTNode const& parent, bool expect_semicolon = true); + NonnullRefPtr<Expression const> parse_expression(ASTNode const& parent); + NonnullRefPtr<Expression const> parse_primary_expression(ASTNode const& parent); + NonnullRefPtr<Expression const> parse_secondary_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs); + NonnullRefPtr<StringLiteral const> parse_string_literal(ASTNode const& parent); + NonnullRefPtr<ReturnStatement const> parse_return_statement(ASTNode const& parent); + NonnullRefPtr<EnumDeclaration const> parse_enum_declaration(ASTNode const& parent); + NonnullRefPtr<StructOrClassDeclaration const> parse_class_declaration(ASTNode const& parent); + NonnullRefPtr<Expression const> parse_literal(ASTNode const& parent); + NonnullRefPtr<UnaryExpression const> parse_unary_expression(ASTNode const& parent); + NonnullRefPtr<BooleanLiteral const> parse_boolean_literal(ASTNode const& parent); + NonnullRefPtr<Type const> parse_type(ASTNode const& parent); + NonnullRefPtr<BinaryExpression const> parse_binary_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs, BinaryOp); + NonnullRefPtr<AssignmentExpression const> parse_assignment_expression(ASTNode const& parent, NonnullRefPtr<Expression const> lhs, AssignmentOp); + NonnullRefPtr<ForStatement const> parse_for_statement(ASTNode const& parent); + NonnullRefPtr<BlockStatement const> parse_block_statement(ASTNode const& parent); + NonnullRefPtr<Comment const> parse_comment(ASTNode const& parent); + NonnullRefPtr<IfStatement const> parse_if_statement(ASTNode const& parent); + NonnullRefPtr<NamespaceDeclaration const> parse_namespace_declaration(ASTNode const& parent, bool is_nested_namespace = false); + NonnullRefPtrVector<Declaration const> parse_declarations_in_translation_unit(ASTNode const& parent); + RefPtr<Declaration const> parse_single_declaration_in_translation_unit(ASTNode const& parent); + NonnullRefPtrVector<Type const> parse_template_arguments(ASTNode const& parent); + NonnullRefPtr<Name const> parse_name(ASTNode const& parent); + NonnullRefPtr<CppCastExpression const> parse_cpp_cast_expression(ASTNode const& parent); + NonnullRefPtr<SizeofExpression const> parse_sizeof_expression(ASTNode const& parent); + NonnullRefPtr<BracedInitList const> parse_braced_init_list(ASTNode const& parent); + NonnullRefPtr<CStyleCastExpression const> parse_c_style_cast_expression(ASTNode const& parent); + NonnullRefPtrVector<Declaration const> parse_class_members(StructOrClassDeclaration& parent); + NonnullRefPtr<Constructor const> parse_constructor(ASTNode const& parent); + NonnullRefPtr<Destructor const> parse_destructor(ASTNode const& parent); + NonnullRefPtr<UsingNamespaceDeclaration const> parse_using_namespace_declaration(ASTNode const& parent); bool match(Token::Type); Token consume(Token::Type); @@ -145,7 +145,7 @@ private: template<class T, class... Args> NonnullRefPtr<T> - create_ast_node(ASTNode& parent, Position const& start, Optional<Position> end, Args&&... args) + create_ast_node(ASTNode const& parent, Position const& start, Optional<Position> end, Args&&... args) { auto node = adopt_ref(*new T(&parent, start, end, m_filename, forward<Args>(args)...)); |