gdscript_compiler.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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 "core/set.h"
  33. #include "gdscript.h"
  34. #include "gdscript_parser.h"
  35. class GDScriptCompiler {
  36. const GDScriptParser *parser;
  37. Set<GDScript *> parsed_classes;
  38. Set<GDScript *> parsing_classes;
  39. GDScript *main_script;
  40. struct CodeGen {
  41. GDScript *script;
  42. const GDScriptParser::ClassNode *class_node;
  43. const GDScriptParser::FunctionNode *function_node;
  44. bool debug_stack;
  45. List<Map<StringName, int>> stack_id_stack;
  46. Map<StringName, int> stack_identifiers;
  47. List<GDScriptFunction::StackDebug> stack_debug;
  48. List<Map<StringName, int>> block_identifier_stack;
  49. Map<StringName, int> block_identifiers;
  50. void add_stack_identifier(const StringName &p_id, int p_stackpos) {
  51. stack_identifiers[p_id] = p_stackpos;
  52. if (debug_stack) {
  53. block_identifiers[p_id] = p_stackpos;
  54. GDScriptFunction::StackDebug sd;
  55. sd.added = true;
  56. sd.line = current_line;
  57. sd.identifier = p_id;
  58. sd.pos = p_stackpos;
  59. stack_debug.push_back(sd);
  60. }
  61. }
  62. void push_stack_identifiers() {
  63. stack_id_stack.push_back(stack_identifiers);
  64. if (debug_stack) {
  65. block_identifier_stack.push_back(block_identifiers);
  66. block_identifiers.clear();
  67. }
  68. }
  69. void pop_stack_identifiers() {
  70. stack_identifiers = stack_id_stack.back()->get();
  71. stack_id_stack.pop_back();
  72. if (debug_stack) {
  73. for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
  74. GDScriptFunction::StackDebug sd;
  75. sd.added = false;
  76. sd.identifier = E->key();
  77. sd.line = current_line;
  78. sd.pos = E->get();
  79. stack_debug.push_back(sd);
  80. }
  81. block_identifiers = block_identifier_stack.back()->get();
  82. block_identifier_stack.pop_back();
  83. }
  84. }
  85. HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
  86. Map<StringName, int> name_map;
  87. #ifdef TOOLS_ENABLED
  88. Vector<StringName> named_globals;
  89. #endif
  90. int get_name_map_pos(const StringName &p_identifier) {
  91. int ret;
  92. if (!name_map.has(p_identifier)) {
  93. ret = name_map.size();
  94. name_map[p_identifier] = ret;
  95. } else {
  96. ret = name_map[p_identifier];
  97. }
  98. return ret;
  99. }
  100. int get_constant_pos(const Variant &p_constant) {
  101. if (constant_map.has(p_constant)) {
  102. return constant_map[p_constant];
  103. }
  104. int pos = constant_map.size();
  105. constant_map[p_constant] = pos;
  106. return pos;
  107. }
  108. Vector<int> opcodes;
  109. void alloc_stack(int p_level) {
  110. if (p_level >= stack_max) {
  111. stack_max = p_level + 1;
  112. }
  113. }
  114. void alloc_call(int p_params) {
  115. if (p_params >= call_max) {
  116. call_max = p_params;
  117. }
  118. }
  119. int current_line;
  120. int stack_max;
  121. int call_max;
  122. };
  123. bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
  124. bool _is_class_member_property(GDScript *owner, const StringName &p_name);
  125. void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
  126. bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level);
  127. bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false, int p_index_addr = 0);
  128. GDScriptDataType _gdtype_from_datatype(const GDScriptParser::DataType &p_datatype, GDScript *p_owner = nullptr) const;
  129. int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level, int p_index_addr = 0);
  130. int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false, int p_index_addr = 0);
  131. Error _parse_block(CodeGen &codegen, const GDScriptParser::BlockNode *p_block, int p_stack_level = 0, int p_break_addr = -1, int p_continue_addr = -1);
  132. Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
  133. Error _parse_class_level(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  134. Error _parse_class_blocks(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  135. void _make_scripts(GDScript *p_script, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  136. int err_line;
  137. int err_column;
  138. StringName source;
  139. String error;
  140. public:
  141. Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
  142. String get_error() const;
  143. int get_error_line() const;
  144. int get_error_column() const;
  145. GDScriptCompiler();
  146. };
  147. #endif // GDSCRIPT_COMPILER_H