gdscript_codegen.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. /**************************************************************************/
  2. /* gdscript_codegen.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_CODEGEN_H
  31. #define GDSCRIPT_CODEGEN_H
  32. #include "gdscript_function.h"
  33. #include "gdscript_utility_functions.h"
  34. #include "core/string/string_name.h"
  35. #include "core/variant/variant.h"
  36. class GDScriptCodeGenerator {
  37. public:
  38. struct Address {
  39. enum AddressMode {
  40. SELF,
  41. CLASS,
  42. MEMBER,
  43. CONSTANT,
  44. LOCAL_VARIABLE,
  45. FUNCTION_PARAMETER,
  46. TEMPORARY,
  47. NIL,
  48. };
  49. AddressMode mode = NIL;
  50. uint32_t address = 0;
  51. GDScriptDataType type;
  52. Address() {}
  53. Address(AddressMode p_mode, const GDScriptDataType &p_type = GDScriptDataType()) {
  54. mode = p_mode;
  55. type = p_type;
  56. }
  57. Address(AddressMode p_mode, uint32_t p_address, const GDScriptDataType &p_type = GDScriptDataType()) {
  58. mode = p_mode;
  59. address = p_address;
  60. type = p_type;
  61. }
  62. };
  63. virtual uint32_t add_parameter(const StringName &p_name, bool p_is_optional, const GDScriptDataType &p_type) = 0;
  64. virtual uint32_t add_local(const StringName &p_name, const GDScriptDataType &p_type) = 0;
  65. virtual uint32_t add_local_constant(const StringName &p_name, const Variant &p_constant) = 0;
  66. virtual uint32_t add_or_get_constant(const Variant &p_constant) = 0;
  67. virtual uint32_t add_or_get_name(const StringName &p_name) = 0;
  68. virtual uint32_t add_temporary(const GDScriptDataType &p_type) = 0;
  69. virtual void pop_temporary() = 0;
  70. virtual void clean_temporaries() = 0;
  71. virtual void start_parameters() = 0;
  72. virtual void end_parameters() = 0;
  73. virtual void start_block() = 0;
  74. virtual void end_block() = 0;
  75. virtual void write_start(GDScript *p_script, const StringName &p_function_name, bool p_static, Variant p_rpc_config, const GDScriptDataType &p_return_type) = 0;
  76. virtual GDScriptFunction *write_end() = 0;
  77. #ifdef DEBUG_ENABLED
  78. virtual void set_signature(const String &p_signature) = 0;
  79. #endif
  80. virtual void set_initial_line(int p_line) = 0;
  81. virtual void write_type_adjust(const Address &p_target, Variant::Type p_new_type) = 0;
  82. virtual void write_unary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand) = 0;
  83. virtual void write_binary_operator(const Address &p_target, Variant::Operator p_operator, const Address &p_left_operand, const Address &p_right_operand) = 0;
  84. virtual void write_type_test(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) = 0;
  85. virtual void write_and_left_operand(const Address &p_left_operand) = 0;
  86. virtual void write_and_right_operand(const Address &p_right_operand) = 0;
  87. virtual void write_end_and(const Address &p_target) = 0;
  88. virtual void write_or_left_operand(const Address &p_left_operand) = 0;
  89. virtual void write_or_right_operand(const Address &p_right_operand) = 0;
  90. virtual void write_end_or(const Address &p_target) = 0;
  91. virtual void write_start_ternary(const Address &p_target) = 0;
  92. virtual void write_ternary_condition(const Address &p_condition) = 0;
  93. virtual void write_ternary_true_expr(const Address &p_expr) = 0;
  94. virtual void write_ternary_false_expr(const Address &p_expr) = 0;
  95. virtual void write_end_ternary() = 0;
  96. virtual void write_set(const Address &p_target, const Address &p_index, const Address &p_source) = 0;
  97. virtual void write_get(const Address &p_target, const Address &p_index, const Address &p_source) = 0;
  98. virtual void write_set_named(const Address &p_target, const StringName &p_name, const Address &p_source) = 0;
  99. virtual void write_get_named(const Address &p_target, const StringName &p_name, const Address &p_source) = 0;
  100. virtual void write_set_member(const Address &p_value, const StringName &p_name) = 0;
  101. virtual void write_get_member(const Address &p_target, const StringName &p_name) = 0;
  102. virtual void write_set_static_variable(const Address &p_value, const Address &p_class, int p_index) = 0;
  103. virtual void write_get_static_variable(const Address &p_target, const Address &p_class, int p_index) = 0;
  104. virtual void write_assign(const Address &p_target, const Address &p_source) = 0;
  105. virtual void write_assign_with_conversion(const Address &p_target, const Address &p_source) = 0;
  106. virtual void write_assign_true(const Address &p_target) = 0;
  107. virtual void write_assign_false(const Address &p_target) = 0;
  108. virtual void write_assign_default_parameter(const Address &dst, const Address &src, bool p_use_conversion) = 0;
  109. virtual void write_store_global(const Address &p_dst, int p_global_index) = 0;
  110. virtual void write_store_named_global(const Address &p_dst, const StringName &p_global) = 0;
  111. virtual void write_cast(const Address &p_target, const Address &p_source, const GDScriptDataType &p_type) = 0;
  112. virtual void write_call(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  113. virtual void write_super_call(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  114. virtual void write_call_async(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  115. virtual void write_call_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) = 0;
  116. virtual void write_call_gdscript_utility(const Address &p_target, const StringName &p_function, const Vector<Address> &p_arguments) = 0;
  117. virtual void write_call_builtin_type(const Address &p_target, const Address &p_base, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) = 0;
  118. virtual void write_call_builtin_type_static(const Address &p_target, Variant::Type p_type, const StringName &p_method, const Vector<Address> &p_arguments) = 0;
  119. virtual void write_call_native_static(const Address &p_target, const StringName &p_class, const StringName &p_method, const Vector<Address> &p_arguments) = 0;
  120. virtual void write_call_method_bind(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) = 0;
  121. virtual void write_call_method_bind_validated(const Address &p_target, const Address &p_base, MethodBind *p_method, const Vector<Address> &p_arguments) = 0;
  122. virtual void write_call_self(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  123. virtual void write_call_self_async(const Address &p_target, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  124. virtual void write_call_script_function(const Address &p_target, const Address &p_base, const StringName &p_function_name, const Vector<Address> &p_arguments) = 0;
  125. virtual void write_lambda(const Address &p_target, GDScriptFunction *p_function, const Vector<Address> &p_captures, bool p_use_self) = 0;
  126. virtual void write_construct(const Address &p_target, Variant::Type p_type, const Vector<Address> &p_arguments) = 0;
  127. virtual void write_construct_array(const Address &p_target, const Vector<Address> &p_arguments) = 0;
  128. virtual void write_construct_typed_array(const Address &p_target, const GDScriptDataType &p_element_type, const Vector<Address> &p_arguments) = 0;
  129. virtual void write_construct_dictionary(const Address &p_target, const Vector<Address> &p_arguments) = 0;
  130. virtual void write_await(const Address &p_target, const Address &p_operand) = 0;
  131. virtual void write_if(const Address &p_condition) = 0;
  132. virtual void write_else() = 0;
  133. virtual void write_endif() = 0;
  134. virtual void write_jump_if_shared(const Address &p_value) = 0;
  135. virtual void write_end_jump_if_shared() = 0;
  136. virtual void start_for(const GDScriptDataType &p_iterator_type, const GDScriptDataType &p_list_type) = 0;
  137. virtual void write_for_assignment(const Address &p_list) = 0;
  138. virtual void write_for(const Address &p_variable, bool p_use_conversion) = 0;
  139. virtual void write_endfor() = 0;
  140. virtual void start_while_condition() = 0; // Used to allow a jump to the expression evaluation.
  141. virtual void write_while(const Address &p_condition) = 0;
  142. virtual void write_endwhile() = 0;
  143. virtual void write_break() = 0;
  144. virtual void write_continue() = 0;
  145. virtual void write_breakpoint() = 0;
  146. virtual void write_newline(int p_line) = 0;
  147. virtual void write_return(const Address &p_return_value) = 0;
  148. virtual void write_assert(const Address &p_test, const Address &p_message) = 0;
  149. virtual ~GDScriptCodeGenerator() {}
  150. };
  151. #endif // GDSCRIPT_CODEGEN_H