gdscript_compiler.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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 CodeGen {
  43. GDScript *script = nullptr;
  44. const GDScriptParser::ClassNode *class_node = nullptr;
  45. const GDScriptParser::FunctionNode *function_node = nullptr;
  46. StringName function_name;
  47. GDScriptCodeGenerator *generator = nullptr;
  48. HashMap<StringName, GDScriptCodeGenerator::Address> parameters;
  49. HashMap<StringName, GDScriptCodeGenerator::Address> locals;
  50. List<HashMap<StringName, GDScriptCodeGenerator::Address>> locals_stack;
  51. bool is_static = false;
  52. GDScriptCodeGenerator::Address add_local(const StringName &p_name, const GDScriptDataType &p_type) {
  53. uint32_t addr = generator->add_local(p_name, p_type);
  54. locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::LOCAL_VARIABLE, addr, p_type);
  55. return locals[p_name];
  56. }
  57. GDScriptCodeGenerator::Address add_local_constant(const StringName &p_name, const Variant &p_value) {
  58. uint32_t addr = generator->add_local_constant(p_name, p_value);
  59. locals[p_name] = GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr);
  60. return locals[p_name];
  61. }
  62. GDScriptCodeGenerator::Address add_temporary(const GDScriptDataType &p_type = GDScriptDataType()) {
  63. uint32_t addr = generator->add_temporary(p_type);
  64. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::TEMPORARY, addr, p_type);
  65. }
  66. GDScriptCodeGenerator::Address add_constant(const Variant &p_constant) {
  67. GDScriptDataType type;
  68. type.has_type = true;
  69. type.kind = GDScriptDataType::BUILTIN;
  70. type.builtin_type = p_constant.get_type();
  71. if (type.builtin_type == Variant::OBJECT) {
  72. Object *obj = p_constant;
  73. if (obj) {
  74. type.kind = GDScriptDataType::NATIVE;
  75. type.native_type = obj->get_class_name();
  76. Ref<Script> scr = obj->get_script();
  77. if (scr.is_valid()) {
  78. type.script_type = scr.ptr();
  79. Ref<GDScript> gdscript = scr;
  80. if (gdscript.is_valid()) {
  81. type.kind = GDScriptDataType::GDSCRIPT;
  82. } else {
  83. type.kind = GDScriptDataType::SCRIPT;
  84. }
  85. }
  86. } else {
  87. type.builtin_type = Variant::NIL;
  88. }
  89. }
  90. uint32_t addr = generator->add_or_get_constant(p_constant);
  91. return GDScriptCodeGenerator::Address(GDScriptCodeGenerator::Address::CONSTANT, addr, type);
  92. }
  93. void start_block() {
  94. HashMap<StringName, GDScriptCodeGenerator::Address> old_locals = locals;
  95. locals_stack.push_back(old_locals);
  96. generator->start_block();
  97. }
  98. void end_block() {
  99. locals = locals_stack.back()->get();
  100. locals_stack.pop_back();
  101. generator->end_block();
  102. }
  103. };
  104. bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
  105. bool _is_class_member_property(GDScript *owner, const StringName &p_name);
  106. bool _is_local_or_parameter(CodeGen &codegen, const StringName &p_name);
  107. void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
  108. 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());
  109. 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());
  110. GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner, bool p_handle_metatype = true);
  111. GDScriptCodeGenerator::Address _parse_assign_right_expression(CodeGen &codegen, Error &r_error, const GDScriptParser::AssignmentNode *p_assignmentint, const GDScriptCodeGenerator::Address &p_index_addr = GDScriptCodeGenerator::Address());
  112. 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());
  113. 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);
  114. List<GDScriptCodeGenerator::Address> _add_locals_in_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block);
  115. void _clear_addresses(CodeGen &codegen, const List<GDScriptCodeGenerator::Address> &p_addresses);
  116. Error _parse_block(CodeGen &codegen, const GDScriptParser::SuiteNode *p_block, bool p_add_locals = true, bool p_reset_locals = true);
  117. 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);
  118. GDScriptFunction *_make_static_initializer(Error &r_error, GDScript *p_script, const GDScriptParser::ClassNode *p_class);
  119. Error _parse_setter_getter(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::VariableNode *p_variable, bool p_is_setter);
  120. Error _prepare_compilation(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  121. Error _compile_class(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  122. int err_line = 0;
  123. int err_column = 0;
  124. StringName source;
  125. String error;
  126. GDScriptParser::ExpressionNode *awaited_node = nullptr;
  127. bool has_static_data = false;
  128. public:
  129. static void convert_to_initializer_type(Variant &p_variant, const GDScriptParser::VariableNode *p_node);
  130. static void make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  131. Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
  132. String get_error() const;
  133. int get_error_line() const;
  134. int get_error_column() const;
  135. GDScriptCompiler();
  136. };
  137. #endif // GDSCRIPT_COMPILER_H