Serenity Operating System

LibJS: rename JS::DeclarationType => JS::DeclarationKind

Many other parsers call it with this name.

Also Type can be confusing in this context since the DeclarationType is
not the type (number, string, etc.) of the variables that are being
declared by the VariableDeclaration.

authored by

Emanuele Torre and committed by
Andreas Kling
38dfd046 44f81611

+37 -37
+11 -11
Libraries/LibJS/AST.cpp
··· 202 202 { 203 203 RefPtr<BlockStatement> wrapper; 204 204 205 - if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_type() != DeclarationType::Var) { 205 + if (m_init && m_init->is_variable_declaration() && static_cast<const VariableDeclaration*>(m_init.ptr())->declaration_kind() != DeclarationKind::Var) { 206 206 wrapper = create_ast_node<BlockStatement>(); 207 207 interpreter.enter_scope(*wrapper, {}, ScopeType::Block); 208 208 } ··· 799 799 Value VariableDeclaration::execute(Interpreter& interpreter) const 800 800 { 801 801 for (auto& declarator : m_declarations) { 802 - interpreter.declare_variable(declarator.id().string(), m_declaration_type); 802 + interpreter.declare_variable(declarator.id().string(), m_declaration_kind); 803 803 if (auto* init = declarator.init()) { 804 804 auto initalizer_result = init->execute(interpreter); 805 805 if (interpreter.exception()) ··· 818 818 819 819 void VariableDeclaration::dump(int indent) const 820 820 { 821 - const char* declaration_type_string = nullptr; 822 - switch (m_declaration_type) { 823 - case DeclarationType::Let: 824 - declaration_type_string = "Let"; 821 + const char* declaration_kind_string = nullptr; 822 + switch (m_declaration_kind) { 823 + case DeclarationKind::Let: 824 + declaration_kind_string = "Let"; 825 825 break; 826 - case DeclarationType::Var: 827 - declaration_type_string = "Var"; 826 + case DeclarationKind::Var: 827 + declaration_kind_string = "Var"; 828 828 break; 829 - case DeclarationType::Const: 830 - declaration_type_string = "Const"; 829 + case DeclarationKind::Const: 830 + declaration_kind_string = "Const"; 831 831 break; 832 832 } 833 833 834 834 ASTNode::dump(indent); 835 835 print_indent(indent + 1); 836 - printf("%s\n", declaration_type_string); 836 + printf("%s\n", declaration_kind_string); 837 837 838 838 for (auto& declarator : m_declarations) 839 839 declarator.dump(indent + 1);
+5 -5
Libraries/LibJS/AST.h
··· 591 591 bool m_prefixed; 592 592 }; 593 593 594 - enum class DeclarationType { 594 + enum class DeclarationKind { 595 595 Var, 596 596 Let, 597 597 Const, ··· 620 620 621 621 class VariableDeclaration : public Declaration { 622 622 public: 623 - VariableDeclaration(DeclarationType declaration_type, NonnullRefPtrVector<VariableDeclarator> declarations) 624 - : m_declaration_type(declaration_type) 623 + VariableDeclaration(DeclarationKind declaration_kind, NonnullRefPtrVector<VariableDeclarator> declarations) 624 + : m_declaration_kind(declaration_kind) 625 625 , m_declarations(move(declarations)) 626 626 { 627 627 } 628 628 629 629 virtual bool is_variable_declaration() const override { return true; } 630 - DeclarationType declaration_type() const { return m_declaration_type; } 630 + DeclarationKind declaration_kind() const { return m_declaration_kind; } 631 631 632 632 virtual Value execute(Interpreter&) const override; 633 633 virtual void dump(int indent) const override; ··· 635 635 private: 636 636 virtual const char* class_name() const override { return "VariableDeclaration"; } 637 637 638 - DeclarationType m_declaration_type; 638 + DeclarationKind m_declaration_kind; 639 639 NonnullRefPtrVector<VariableDeclarator> m_declarations; 640 640 }; 641 641
+1 -1
Libraries/LibJS/Forward.h
··· 52 52 class Shape; 53 53 class Statement; 54 54 class Value; 55 - enum class DeclarationType; 55 + enum class DeclarationKind; 56 56 57 57 struct Argument; 58 58
+13 -13
Libraries/LibJS/Interpreter.cpp
··· 90 90 91 91 void Interpreter::enter_scope(const ScopeNode& scope_node, ArgumentVector arguments, ScopeType scope_type) 92 92 { 93 - HashMap<FlyString, Variable> scope_variables_with_declaration_type; 93 + HashMap<FlyString, Variable> scope_variables_with_declaration_kind; 94 94 for (auto& argument : arguments) { 95 - scope_variables_with_declaration_type.set(argument.name, { argument.value, DeclarationType::Var }); 95 + scope_variables_with_declaration_kind.set(argument.name, { argument.value, DeclarationKind::Var }); 96 96 } 97 - m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_type) }); 97 + m_scope_stack.append({ scope_type, scope_node, move(scope_variables_with_declaration_kind) }); 98 98 } 99 99 100 100 void Interpreter::exit_scope(const ScopeNode& scope_node) ··· 110 110 m_unwind_until = ScopeType::None; 111 111 } 112 112 113 - void Interpreter::declare_variable(const FlyString& name, DeclarationType declaration_type) 113 + void Interpreter::declare_variable(const FlyString& name, DeclarationKind declaration_kind) 114 114 { 115 - switch (declaration_type) { 116 - case DeclarationType::Var: 115 + switch (declaration_kind) { 116 + case DeclarationKind::Var: 117 117 for (ssize_t i = m_scope_stack.size() - 1; i >= 0; --i) { 118 118 auto& scope = m_scope_stack.at(i); 119 119 if (scope.type == ScopeType::Function) { 120 - if (scope.variables.get(name).has_value() && scope.variables.get(name).value().declaration_type != DeclarationType::Var) 120 + if (scope.variables.get(name).has_value() && scope.variables.get(name).value().declaration_kind != DeclarationKind::Var) 121 121 ASSERT_NOT_REACHED(); 122 122 123 - scope.variables.set(move(name), { js_undefined(), declaration_type }); 123 + scope.variables.set(move(name), { js_undefined(), declaration_kind }); 124 124 return; 125 125 } 126 126 } 127 127 128 128 global_object().put(move(name), js_undefined()); 129 129 break; 130 - case DeclarationType::Let: 131 - case DeclarationType::Const: 130 + case DeclarationKind::Let: 131 + case DeclarationKind::Const: 132 132 if (m_scope_stack.last().variables.get(name).has_value()) 133 133 ASSERT_NOT_REACHED(); 134 134 135 - m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_type }); 135 + m_scope_stack.last().variables.set(move(name), { js_undefined(), declaration_kind }); 136 136 break; 137 137 } 138 138 } ··· 144 144 145 145 auto possible_match = scope.variables.get(name); 146 146 if (possible_match.has_value()) { 147 - if (!first_assignment && possible_match.value().declaration_type == DeclarationType::Const) 147 + if (!first_assignment && possible_match.value().declaration_kind == DeclarationKind::Const) 148 148 ASSERT_NOT_REACHED(); 149 149 150 - scope.variables.set(move(name), { move(value), possible_match.value().declaration_type }); 150 + scope.variables.set(move(name), { move(value), possible_match.value().declaration_kind }); 151 151 return; 152 152 } 153 153 }
+2 -2
Libraries/LibJS/Interpreter.h
··· 48 48 49 49 struct Variable { 50 50 Value value; 51 - DeclarationType declaration_type; 51 + DeclarationKind declaration_kind; 52 52 }; 53 53 54 54 struct ScopeFrame { ··· 95 95 96 96 Optional<Value> get_variable(const FlyString& name); 97 97 void set_variable(const FlyString& name, Value, bool first_assignment = false); 98 - void declare_variable(const FlyString& name, DeclarationType); 98 + void declare_variable(const FlyString& name, DeclarationKind); 99 99 100 100 void gather_roots(Badge<Heap>, HashTable<Cell*>&); 101 101
+5 -5
Libraries/LibJS/Parser.cpp
··· 658 658 659 659 NonnullRefPtr<VariableDeclaration> Parser::parse_variable_declaration() 660 660 { 661 - DeclarationType declaration_type; 661 + DeclarationKind declaration_kind; 662 662 663 663 switch (m_parser_state.m_current_token.type()) { 664 664 case TokenType::Var: 665 - declaration_type = DeclarationType::Var; 665 + declaration_kind = DeclarationKind::Var; 666 666 consume(TokenType::Var); 667 667 break; 668 668 case TokenType::Let: 669 - declaration_type = DeclarationType::Let; 669 + declaration_kind = DeclarationKind::Let; 670 670 consume(TokenType::Let); 671 671 break; 672 672 case TokenType::Const: 673 - declaration_type = DeclarationType::Const; 673 + declaration_kind = DeclarationKind::Const; 674 674 consume(TokenType::Const); 675 675 break; 676 676 default: ··· 692 692 } 693 693 break; 694 694 } 695 - return create_ast_node<VariableDeclaration>(declaration_type, move(declarations)); 695 + return create_ast_node<VariableDeclaration>(declaration_kind, move(declarations)); 696 696 } 697 697 698 698 NonnullRefPtr<ThrowStatement> Parser::parse_throw_statement()