gdscript_compiler.h 6.1 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) 2007-2018 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2018 Godot Engine contributors (cf. AUTHORS.md) */
  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_parser.h"
  34. class GDScriptCompiler {
  35. const GDScriptParser *parser;
  36. struct CodeGen {
  37. GDScript *script;
  38. const GDScriptParser::ClassNode *class_node;
  39. const GDScriptParser::FunctionNode *function_node;
  40. bool debug_stack;
  41. List<Map<StringName, int> > stack_id_stack;
  42. Map<StringName, int> stack_identifiers;
  43. List<GDScriptFunction::StackDebug> stack_debug;
  44. List<Map<StringName, int> > block_identifier_stack;
  45. Map<StringName, int> block_identifiers;
  46. void add_stack_identifier(const StringName &p_id, int p_stackpos) {
  47. stack_identifiers[p_id] = p_stackpos;
  48. if (debug_stack) {
  49. block_identifiers[p_id] = p_stackpos;
  50. GDScriptFunction::StackDebug sd;
  51. sd.added = true;
  52. sd.line = current_line;
  53. sd.identifier = p_id;
  54. sd.pos = p_stackpos;
  55. stack_debug.push_back(sd);
  56. }
  57. }
  58. void push_stack_identifiers() {
  59. stack_id_stack.push_back(stack_identifiers);
  60. if (debug_stack) {
  61. block_identifier_stack.push_back(block_identifiers);
  62. block_identifiers.clear();
  63. }
  64. }
  65. void pop_stack_identifiers() {
  66. stack_identifiers = stack_id_stack.back()->get();
  67. stack_id_stack.pop_back();
  68. if (debug_stack) {
  69. for (Map<StringName, int>::Element *E = block_identifiers.front(); E; E = E->next()) {
  70. GDScriptFunction::StackDebug sd;
  71. sd.added = false;
  72. sd.identifier = E->key();
  73. sd.line = current_line;
  74. sd.pos = E->get();
  75. stack_debug.push_back(sd);
  76. }
  77. block_identifiers = block_identifier_stack.back()->get();
  78. block_identifier_stack.pop_back();
  79. }
  80. }
  81. HashMap<Variant, int, VariantHasher, VariantComparator> constant_map;
  82. Map<StringName, int> name_map;
  83. int get_name_map_pos(const StringName &p_identifier) {
  84. int ret;
  85. if (!name_map.has(p_identifier)) {
  86. ret = name_map.size();
  87. name_map[p_identifier] = ret;
  88. } else {
  89. ret = name_map[p_identifier];
  90. }
  91. return ret;
  92. }
  93. int get_constant_pos(const Variant &p_constant) {
  94. if (constant_map.has(p_constant))
  95. return constant_map[p_constant];
  96. int pos = constant_map.size();
  97. constant_map[p_constant] = pos;
  98. return pos;
  99. }
  100. Vector<int> opcodes;
  101. void alloc_stack(int p_level) {
  102. if (p_level >= stack_max) stack_max = p_level + 1;
  103. }
  104. void alloc_call(int p_params) {
  105. if (p_params >= call_max) call_max = p_params;
  106. }
  107. int current_line;
  108. int stack_max;
  109. int call_max;
  110. };
  111. bool _is_class_member_property(CodeGen &codegen, const StringName &p_name);
  112. bool _is_class_member_property(GDScript *owner, const StringName &p_name);
  113. void _set_error(const String &p_error, const GDScriptParser::Node *p_node);
  114. bool _create_unary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level);
  115. bool _create_binary_operator(CodeGen &codegen, const GDScriptParser::OperatorNode *on, Variant::Operator op, int p_stack_level, bool p_initializer = false);
  116. int _parse_assign_right_expression(CodeGen &codegen, const GDScriptParser::OperatorNode *p_expression, int p_stack_level);
  117. int _parse_expression(CodeGen &codegen, const GDScriptParser::Node *p_expression, int p_stack_level, bool p_root = false, bool p_initializer = false);
  118. 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);
  119. Error _parse_function(GDScript *p_script, const GDScriptParser::ClassNode *p_class, const GDScriptParser::FunctionNode *p_func, bool p_for_ready = false);
  120. Error _parse_class(GDScript *p_script, GDScript *p_owner, const GDScriptParser::ClassNode *p_class, bool p_keep_state);
  121. int err_line;
  122. int err_column;
  123. StringName source;
  124. String error;
  125. public:
  126. Error compile(const GDScriptParser *p_parser, GDScript *p_script, bool p_keep_state = false);
  127. String get_error() const;
  128. int get_error_line() const;
  129. int get_error_column() const;
  130. GDScriptCompiler();
  131. };
  132. #endif // GDSCRIPT_COMPILER_H