Add new GDScript type checker
This commit is contained in:
parent
17cd6347ba
commit
9a76ab8b6a
File diff suppressed because it is too large
Load Diff
|
@ -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<String, Ref<GDScriptParserRef>> 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<GDScriptParser::DataType> &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<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg);
|
||||
bool validate_call_arg(const List<GDScriptParser::DataType> &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<GDScriptParserRef> get_parser_for(const String &p_path);
|
||||
|
||||
public:
|
||||
Error resolve_inheritance();
|
||||
Error resolve_interface();
|
||||
Error resolve_body();
|
||||
Error analyze();
|
||||
|
||||
GDScriptAnalyzer(GDScriptParser *p_parser);
|
||||
|
|
|
@ -137,7 +137,7 @@ GDScriptDataType GDScriptCompiler::_gdtype_from_datatype(const GDScriptParser::D
|
|||
} break;
|
||||
case GDScriptParser::DataType::CLASS: {
|
||||
// Locate class by constructing the path to it and following that path
|
||||
GDScriptParser::ClassNode *class_type = p_datatype.gdscript_type;
|
||||
GDScriptParser::ClassNode *class_type = p_datatype.class_type;
|
||||
if (class_type) {
|
||||
List<StringName> names;
|
||||
while (class_type->outer) {
|
||||
|
@ -233,6 +233,10 @@ int GDScriptCompiler::_parse_assign_right_expression(CodeGen &codegen, const GDS
|
|||
}
|
||||
|
||||
int GDScriptCompiler::_parse_expression(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_expression, int p_stack_level, bool p_root, bool p_initializer, int p_index_addr) {
|
||||
if (p_expression->is_constant) {
|
||||
return codegen.get_constant_pos(p_expression->reduced_value);
|
||||
}
|
||||
|
||||
switch (p_expression->type) {
|
||||
//should parse variable declaration and adjust stack accordingly...
|
||||
case GDScriptParser::Node::IDENTIFIER: {
|
||||
|
@ -2578,14 +2582,14 @@ Error GDScriptCompiler::_parse_class_level(GDScript *p_script, const GDScriptPar
|
|||
p_script->_base = base.ptr();
|
||||
p_script->member_indices = base->member_indices;
|
||||
|
||||
if (p_class->base_type.kind == GDScriptParser::DataType::CLASS && p_class->base_type.gdscript_type != nullptr) {
|
||||
if (p_class->base_type.kind == GDScriptParser::DataType::CLASS && p_class->base_type.class_type != nullptr) {
|
||||
if (!parsed_classes.has(p_script->_base)) {
|
||||
if (parsing_classes.has(p_script->_base)) {
|
||||
String class_name = p_class->identifier ? p_class->identifier->name : "<main>";
|
||||
_set_error("Cyclic class reference for '" + class_name + "'.", p_class);
|
||||
return ERR_PARSE_ERROR;
|
||||
}
|
||||
Error err = _parse_class_level(p_script->_base, p_class->base_type.gdscript_type, p_keep_state);
|
||||
Error err = _parse_class_level(p_script->_base, p_class->base_type.class_type, p_keep_state);
|
||||
if (err) {
|
||||
return err;
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@
|
|||
#include "core/engine.h"
|
||||
#include "core/global_constants.h"
|
||||
#include "core/os/file_access.h"
|
||||
#include "gdscript_analyzer.h"
|
||||
#include "gdscript_compiler.h"
|
||||
#include "gdscript_parser.h"
|
||||
#include "gdscript_tokenizer.h"
|
||||
|
@ -130,6 +131,7 @@ static void get_function_names_recursively(const GDScriptParser::ClassNode *p_cl
|
|||
|
||||
bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &r_col_error, String &r_test_error, const String &p_path, List<String> *r_functions, List<ScriptLanguage::Warning> *r_warnings, Set<int> *r_safe_lines) const {
|
||||
GDScriptParser parser;
|
||||
GDScriptAnalyzer analyzer(&parser);
|
||||
|
||||
Error err = parser.parse(p_script, p_path, false);
|
||||
#ifdef DEBUG_ENABLED
|
||||
|
@ -146,6 +148,9 @@ bool GDScriptLanguage::validate(const String &p_script, int &r_line_error, int &
|
|||
// }
|
||||
// }
|
||||
#endif
|
||||
if (err == OK) {
|
||||
err = analyzer.analyze();
|
||||
}
|
||||
if (err) {
|
||||
GDScriptParser::ParserError parse_error = parser.get_errors().front()->get();
|
||||
r_line_error = parse_error.line;
|
||||
|
|
|
@ -146,12 +146,14 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
|
|||
if (p_arg_count < m_count) { \
|
||||
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS; \
|
||||
r_error.argument = m_count; \
|
||||
r_error.expected = m_count; \
|
||||
r_ret = Variant(); \
|
||||
return; \
|
||||
} \
|
||||
if (p_arg_count > m_count) { \
|
||||
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS; \
|
||||
r_error.argument = m_count; \
|
||||
r_error.expected = m_count; \
|
||||
r_ret = Variant(); \
|
||||
return; \
|
||||
}
|
||||
|
@ -897,6 +899,7 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
|
|||
case 0: {
|
||||
r_error.error = Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS;
|
||||
r_error.argument = 1;
|
||||
r_error.expected = 1;
|
||||
r_ret = Variant();
|
||||
|
||||
} break;
|
||||
|
@ -1001,6 +1004,7 @@ void GDScriptFunctions::call(Function p_func, const Variant **p_args, int p_arg_
|
|||
default: {
|
||||
r_error.error = Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS;
|
||||
r_error.argument = 3;
|
||||
r_error.expected = 3;
|
||||
r_ret = Variant();
|
||||
|
||||
} break;
|
||||
|
|
|
@ -1894,6 +1894,8 @@ GDScriptParser::ExpressionNode *GDScriptParser::parse_await(ExpressionNode *p_pr
|
|||
AwaitNode *await = alloc_node<AwaitNode>();
|
||||
await->to_await = parse_precedence(PREC_AWAIT, false);
|
||||
|
||||
current_function->is_coroutine = true;
|
||||
|
||||
return await;
|
||||
}
|
||||
|
||||
|
|
|
@ -127,6 +127,7 @@ public:
|
|||
_FORCE_INLINE_ bool is_set() const { return kind != UNRESOLVED; }
|
||||
_FORCE_INLINE_ bool has_no_type() const { return type_source == UNDETECTED; }
|
||||
_FORCE_INLINE_ bool is_variant() const { return kind == VARIANT; }
|
||||
_FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }
|
||||
String to_string() const;
|
||||
|
||||
bool operator==(const DataType &p_other) const {
|
||||
|
@ -591,6 +592,7 @@ public:
|
|||
TypeNode *return_type = nullptr;
|
||||
SuiteNode *body = nullptr;
|
||||
bool is_static = false;
|
||||
bool is_coroutine = false;
|
||||
MultiplayerAPI::RPCMode rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED;
|
||||
|
||||
bool resolved_signature = false;
|
||||
|
@ -620,7 +622,8 @@ public:
|
|||
LOCAL_VARIABLE,
|
||||
LOCAL_ITERATOR, // `for` loop iterator.
|
||||
LOCAL_BIND, // Pattern bind.
|
||||
// TODO: Add higher sources to help compiling?
|
||||
MEMBER_VARIABLE,
|
||||
MEMBER_CONSTANT,
|
||||
};
|
||||
Source source = UNDEFINED_SOURCE;
|
||||
|
||||
|
|
Loading…
Reference in New Issue