From 5d6e8538065050d5f5579ec03cfa9e241811e062 Mon Sep 17 00:00:00 2001 From: George Marques Date: Fri, 1 May 2020 19:14:56 -0300 Subject: New GDScript tokenizer and parser Sometimes to fix something you have to break it first. This get GDScript mostly working with the new tokenizer and parser but a lot of things isn't working yet. It compiles and it's usable, and that should be enough for now. Don't worry: other huge commits will come after this. --- modules/gdscript/gdscript_analyzer.h | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 modules/gdscript/gdscript_analyzer.h (limited to 'modules/gdscript/gdscript_analyzer.h') diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h new file mode 100644 index 0000000000..33f036f11c --- /dev/null +++ b/modules/gdscript/gdscript_analyzer.h @@ -0,0 +1,52 @@ +/*************************************************************************/ +/* gdscript_analyzer.h */ +/*************************************************************************/ +/* This file is part of: */ +/* GODOT ENGINE */ +/* https://godotengine.org */ +/*************************************************************************/ +/* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md). */ +/* */ +/* Permission is hereby granted, free of charge, to any person obtaining */ +/* a copy of this software and associated documentation files (the */ +/* "Software"), to deal in the Software without restriction, including */ +/* without limitation the rights to use, copy, modify, merge, publish, */ +/* distribute, sublicense, and/or sell copies of the Software, and to */ +/* permit persons to whom the Software is furnished to do so, subject to */ +/* the following conditions: */ +/* */ +/* The above copyright notice and this permission notice shall be */ +/* included in all copies or substantial portions of the Software. */ +/* */ +/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */ +/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */ +/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/ +/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */ +/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */ +/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */ +/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +/*************************************************************************/ + +#ifndef GDSCRIPT_ANALYZER_H +#define GDSCRIPT_ANALYZER_H + +#include "gdscript_parser.h" + +class GDScriptAnalyzer { + GDScriptParser *parser = nullptr; + + Error resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive = true); + GDScriptParser::DataType resolve_datatype(const GDScriptParser::TypeNode *p_type); + + // This traverses the tree to resolve all TypeNodes. + Error resolve_datatypes(GDScriptParser::ClassNode *p_class); + +public: + Error resolve_inheritance(); + Error analyze(); + + GDScriptAnalyzer(GDScriptParser *p_parser); +}; + +#endif // GDSCRIPT_ANALYZER_H -- cgit v1.2.3 From 9a76ab8b6a787edd6e09be4b7833eaf8703cbff7 Mon Sep 17 00:00:00 2001 From: George Marques Date: Wed, 10 Jun 2020 19:53:25 -0300 Subject: Add new GDScript type checker --- modules/gdscript/gdscript_analyzer.h | 65 +++++++++++++++++++++++++++++++++++- 1 file changed, 64 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/gdscript_analyzer.h') diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h index 33f036f11c..2794e55b3d 100644 --- a/modules/gdscript/gdscript_analyzer.h +++ b/modules/gdscript/gdscript_analyzer.h @@ -31,19 +31,82 @@ #ifndef GDSCRIPT_ANALYZER_H #define GDSCRIPT_ANALYZER_H +#include "core/object.h" +#include "core/reference.h" +#include "core/set.h" +#include "gdscript_cache.h" #include "gdscript_parser.h" class GDScriptAnalyzer { GDScriptParser *parser = nullptr; + HashMap> depended_parsers; Error resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive = true); GDScriptParser::DataType resolve_datatype(const GDScriptParser::TypeNode *p_type); + void decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement); + // This traverses the tree to resolve all TypeNodes. - Error resolve_datatypes(GDScriptParser::ClassNode *p_class); + Error resolve_program(); + + void resolve_annotation(GDScriptParser::AnnotationNode *p_annotation); + void resolve_class_interface(GDScriptParser::ClassNode *p_class); + void resolve_class_body(GDScriptParser::ClassNode *p_class); + void resolve_function_signature(GDScriptParser::FunctionNode *p_function); + void resolve_function_body(GDScriptParser::FunctionNode *p_function); + void resolve_node(GDScriptParser::Node *p_node); + void resolve_suite(GDScriptParser::SuiteNode *p_suite); + void resolve_if(GDScriptParser::IfNode *p_if); + void resolve_for(GDScriptParser::ForNode *p_for); + void resolve_while(GDScriptParser::WhileNode *p_while); + void resolve_variable(GDScriptParser::VariableNode *p_variable); + void resolve_constant(GDScriptParser::ConstantNode *p_constant); + void resolve_assert(GDScriptParser::AssertNode *p_assert); + void resolve_match(GDScriptParser::MatchNode *p_match); + void resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test); + void resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test); + void resolve_pararameter(GDScriptParser::ParameterNode *p_parameter); + void resolve_return(GDScriptParser::ReturnNode *p_return); + + // Reduction functions. + void reduce_expression(GDScriptParser::ExpressionNode *p_expression); + void reduce_array(GDScriptParser::ArrayNode *p_array); + void reduce_assignment(GDScriptParser::AssignmentNode *p_assignment); + void reduce_await(GDScriptParser::AwaitNode *p_await); + void reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op); + void reduce_call(GDScriptParser::CallNode *p_call); + void reduce_cast(GDScriptParser::CastNode *p_cast); + void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary); + void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node); + void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier); + void reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base = nullptr); + void reduce_literal(GDScriptParser::LiteralNode *p_literal); + void reduce_preload(GDScriptParser::PreloadNode *p_preload); + void reduce_self(GDScriptParser::SelfNode *p_self); + void reduce_subscript(GDScriptParser::SubscriptNode *p_subscript); + void reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op); + void reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op); + + // Helpers. + GDScriptParser::DataType type_from_variant(const Variant &p_value); + GDScriptParser::DataType type_from_metatype(const GDScriptParser::DataType &p_meta_type) const; + GDScriptParser::DataType type_from_property(const PropertyInfo &p_property) const; + GDScriptParser::DataType make_global_class_meta_type(const StringName &p_class_name); + bool get_function_signature(GDScriptParser::Node *p_source, GDScriptParser::DataType base_type, const StringName &p_function, GDScriptParser::DataType &r_return_type, List &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg); + bool function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg); + bool validate_call_arg(const List &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call); + bool validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call); + GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid); + bool is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false) const; + void push_error(const String &p_message, const GDScriptParser::Node *p_origin); + void mark_node_unsafe(const GDScriptParser::Node *p_node); + bool class_exists(const StringName &p_class); + Ref get_parser_for(const String &p_path); public: Error resolve_inheritance(); + Error resolve_interface(); + Error resolve_body(); Error analyze(); GDScriptAnalyzer(GDScriptParser *p_parser); -- cgit v1.2.3 From 95c0909290508620bd330c2b4f7120c92cd73f0c Mon Sep 17 00:00:00 2001 From: George Marques Date: Thu, 11 Jun 2020 19:31:28 -0300 Subject: Add warning checks in GDScript analyzer Reenable checking those when validating code. --- modules/gdscript/gdscript_analyzer.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'modules/gdscript/gdscript_analyzer.h') diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h index 2794e55b3d..5de5c7ab85 100644 --- a/modules/gdscript/gdscript_analyzer.h +++ b/modules/gdscript/gdscript_analyzer.h @@ -42,7 +42,7 @@ class GDScriptAnalyzer { HashMap> depended_parsers; Error resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive = true); - GDScriptParser::DataType resolve_datatype(const GDScriptParser::TypeNode *p_type); + GDScriptParser::DataType resolve_datatype(GDScriptParser::TypeNode *p_type); void decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement); @@ -74,7 +74,7 @@ class GDScriptAnalyzer { void reduce_assignment(GDScriptParser::AssignmentNode *p_assignment); void reduce_await(GDScriptParser::AwaitNode *p_await); void reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op); - void reduce_call(GDScriptParser::CallNode *p_call); + void reduce_call(GDScriptParser::CallNode *p_call, bool is_await = false); void reduce_cast(GDScriptParser::CastNode *p_cast); void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary); void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node); @@ -96,6 +96,7 @@ class GDScriptAnalyzer { bool function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg); bool validate_call_arg(const List &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call); bool validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call); + bool is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context); GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid); bool is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false) const; void push_error(const String &p_message, const GDScriptParser::Node *p_origin); -- cgit v1.2.3 From dadfcd8aba8df696f7c608866a34b4e5ee55a0bf Mon Sep 17 00:00:00 2001 From: George Marques Date: Thu, 11 Jun 2020 21:49:58 -0300 Subject: Added support for enums to be used as types in GDScript --- modules/gdscript/gdscript_analyzer.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'modules/gdscript/gdscript_analyzer.h') diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h index 5de5c7ab85..b21a003e47 100644 --- a/modules/gdscript/gdscript_analyzer.h +++ b/modules/gdscript/gdscript_analyzer.h @@ -78,7 +78,7 @@ class GDScriptAnalyzer { void reduce_cast(GDScriptParser::CastNode *p_cast); void reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary); void reduce_get_node(GDScriptParser::GetNodeNode *p_get_node); - void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier); + void reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin = false); void reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base = nullptr); void reduce_literal(GDScriptParser::LiteralNode *p_literal); void reduce_preload(GDScriptParser::PreloadNode *p_preload); -- cgit v1.2.3 From a0f54cb95e3a0250df8ed6e6a54720c487034d97 Mon Sep 17 00:00:00 2001 From: George Marques Date: Wed, 15 Jul 2020 22:02:44 -0300 Subject: Wrap up GDScript 2.0 base implementation --- modules/gdscript/gdscript_analyzer.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'modules/gdscript/gdscript_analyzer.h') diff --git a/modules/gdscript/gdscript_analyzer.h b/modules/gdscript/gdscript_analyzer.h index b21a003e47..85183d3272 100644 --- a/modules/gdscript/gdscript_analyzer.h +++ b/modules/gdscript/gdscript_analyzer.h @@ -96,13 +96,15 @@ class GDScriptAnalyzer { bool function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg); bool validate_call_arg(const List &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call); bool validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call); - bool is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context); GDScriptParser::DataType get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid); bool is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion = false) const; void push_error(const String &p_message, const GDScriptParser::Node *p_origin); void mark_node_unsafe(const GDScriptParser::Node *p_node); bool class_exists(const StringName &p_class); Ref get_parser_for(const String &p_path); +#ifdef DEBUG_ENABLED + bool is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context); +#endif public: Error resolve_inheritance(); -- cgit v1.2.3