gdscript_compiler.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /**************************************************************************/
  2. /* gdscript_compiler.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef GDSCRIPT_COMPILER_H
  31. #define GDSCRIPT_COMPILER_H
  32. #include "gdscript.h"
  33. #include "gdscript_codegen.h"
  34. #include "gdscript_function.h"
  35. #include "gdscript_parser.h"
  36. #include "core/templates/hash_set.h"
  37. class GDScriptCompiler {
  38. const GDScriptParser *parser = nullptr;
  39. HashSet<GDScript *> parsed_classes;
  40. HashSet<GDScript *> parsing_classes;
  41. GDScript *main_script = nullptr;
  42. struct FunctionLambdaInfo {
  43. GDScriptFunction *function = nullptr;
  44. GDScriptFunction *parent = nullptr;
  45. GDScript *script = nullptr;
  46. StringName name;
  47. int line = 0;
  48. int index = 0;
  49. int depth = 0;
  50. //uint64_t code_hash;
  51. //int code_size;
  52. int capture_count = 0;
  53. bool use_self = false;
  54. int arg_count = 0;
  55. int default_arg_count = 0;
  56. //Vector<GDScriptDataType> argument_types;
  57. //GDScriptDataType return_type;
  58. Vector<FunctionLambdaInfo> sublambdas;
  59. };
  60. struct ScriptLambdaInfo {
  61. Vector<FunctionLambdaInfo> implicit_initializer_info;
  62. Vector<FunctionLambdaInfo> implicit_ready_info;
  63. Vector<FunctionLambdaInfo> static_initializer_info;
  64. HashMap<StringName, Vector<FunctionLambdaInfo>> member_function_infos;
  65. Vector<FunctionLambdaInfo> other_function_infos;
  66. HashMap<StringName, ScriptLambdaInfo> subclass_info;
  67. };
  68. struct CodeGen {
  69. GDScript *script = nullptr;
  70. const GDScriptParser::ClassNode *class_node = nullptr;
  71. const GDScriptParser::FunctionNode *function_node = nullptr;
  72. StringName function_name;
  73. GDScriptCodeGenerator *generator = nullptr;
  74. HashMap<StringName, GDScriptCodeGenerator::Address> parameters;
  75. HashMap<StringName, GDScriptCodeGenerator::Address> locals;
  76. List<HashMap<StringName, GDScriptCodeGenerator::Address>> locals_stack;
  77. bool is_static = false;
  78. GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  79. uint32_t addr = generator->add_local(p_name, p_type);
  80. locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
  81. return locals[p_name];
  82. }
  83. GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) {
  84. uint32_t addr = generator->add_local_constant(p_name, p_value);
  85. locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr);
  86. return locals[p_name];
  87. }
  88. GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) {
  89. uint32_t addr = generator->add_temporary(p_type);
  90. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type);
  91. }
  92. GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
  93. GDScriptDataType type;
  94. type.has_type = true;
  95. type.kind = GDScriptDataType::BUILTIN;
  96. type.builtin_type = p_constant.get_type();
  97. if (type.builtin_type == Variant::OBJECT) {
  98. Object *obj = p_constant;
  99. if (obj) {
  100. type.kind = GDScriptDataType::NATIVE;
  101. type.native_type = obj->get_class_name();
  102. Ref<Script> scr = obj->get_script();
  103. if (scr.is_valid()) {
  104. type.script_type = scr.ptr();
  105. Ref<GDScript> gdscript = scr;
  106. if (gdscript.is_valid()) {
  107. type.kind = GDScriptDataType::GDSCRIPT;
  108. } else {
  109. type.kind = GDScriptDataType::SCRIPT;
  110. }
  111. }
  112. } else {
  113. type.builtin_type = Variant::NIL;
  114. }
  115. }
  116. uint32_t addr = generator->add_or_get_constant(p_constant);
  117. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
  118. }
  119. void start_block() {
  120. HashMap<StringName, GDScriptCodeGenerator::Address> old_locals = locals;
  121. locals_stack.push_back(old_locals);
  122. generator->start_block();
  123. }
  124. void end_block() {
  125. locals = locals_stack.back()->get();
  126. locals_stack.pop_back();
  127. generator->end_block();
  128. }
  129. };
  130. bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
  131. bool _is_class_member_property(GDScript *owner, const StringName &p_name);
  132. bool _is_local_or_parameter(CodeGen &codegen, const StringName &p_name);
  133. void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
  134. Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::BinaryOpNode *on, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
  135. Error _create_binary_operator(CodeGen &codegen, const GDScriptParser::ExpressionNode *p_left_operand, const GDScriptParser::ExpressionNode *p_right_operand, Variant::Operator op, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
  136. GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype = true);
  137. GDScriptCodeGenerator::Address _parse_assign_right_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::AssignmentNode *p_assignmentint, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
  138. GDScriptCodeGenerator::Address _parse_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::ExpressionNode *p_expression, bool p_root = false, bool p_initializer = false, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
  139. GDScriptCodeGenerator::Address _parse_match_pattern(CodeGen &codegen, Error &r_error, const GDScriptParser::PatternNode *p_pattern, const GDScriptCodeGenerator::Address &p_value_addr, const GDScriptCodeGenerator::Address &p_type_addr, const GDScriptCodeGenerator::Address &p_previous_test, bool p_is_first, bool p_is_nested);
  140. List<GDScriptCodeGenerator::Address> _add_locals_in_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
  141. void _clear_addresses(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_addresses);
  142. Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true, bool p_reset_locals = true);
  143. GDScriptFunction *_parse_function(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false, bool p_for_lambda = false);
  144. GDScriptFunction *_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class);
  145. Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
  146. Error _prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  147. Error _compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  148. FunctionLambdaInfo _get_function_replacement_info(GDScriptFunction *p_func, int p_index = -1, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
  149. Vector<FunctionLambdaInfo> _get_function_lambda_replacement_info(GDScriptFunction *p_func, int p_depth = 0, GDScriptFunction *p_parent_func = nullptr);
  150. ScriptLambdaInfo _get_script_lambda_replacement_info(GDScript *p_script);
  151. bool _do_function_infos_match(const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
  152. void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const FunctionLambdaInfo &p_old_info, const FunctionLambdaInfo *p_new_info);
  153. void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const Vector<FunctionLambdaInfo> &p_old_infos, const Vector<FunctionLambdaInfo> *p_new_infos);
  154. void _get_function_ptr_replacements(HashMap<GDScriptFunction *, GDScriptFunction *> &r_replacements, const ScriptLambdaInfo &p_old_info, const ScriptLambdaInfo *p_new_info);
  155. int err_line = 0;
  156. int err_column = 0;
  157. StringName source;
  158. String error;
  159. GDScriptParser::ExpressionNode *awaited_node = nullptr;
  160. bool has_static_data = false;
  161. public:
  162. static void convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node);
  163. static void make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  164. Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
  165. String get_error() const;
  166. int get_error_line() const;
  167. int get_error_column() const;
  168. GDScriptCompiler();
  169. };
  170. #endif // GDSCRIPT_COMPILER_H