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