1 /*************************************************************************/ 2 /* gdscript_parser.h */ 3 /*************************************************************************/ 4 /* This file is part of: */ 5 /* GODOT ENGINE */ 6 /* https://godotengine.org */ 7 /*************************************************************************/ 8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */ 9 /* Copyright (c) 2014-2020 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 31 #ifndef GDSCRIPT_PARSER_H 32 #define GDSCRIPT_PARSER_H 33 34 #include "core/map.h" 35 #include "core/object.h" 36 #include "core/script_language.h" 37 #include "gdscript_functions.h" 38 #include "gdscript_tokenizer.h" 39 40 struct GDScriptDataType; 41 struct GDScriptWarning; 42 43 class GDScriptParser { 44 public: 45 struct ClassNode; 46 47 struct DataType { 48 enum { 49 BUILTIN, 50 NATIVE, 51 SCRIPT, 52 GDSCRIPT, 53 CLASS, 54 UNRESOLVED 55 } kind; 56 57 bool has_type; 58 bool is_constant; 59 bool is_meta_type; // Whether the value can be used as a type 60 bool infer_type; 61 bool may_yield; // For function calls 62 63 Variant::Type builtin_type; 64 StringName native_type; 65 Ref<Script> script_type; 66 ClassNode *class_type; 67 68 String to_string() const; 69 70 bool operator==(const DataType &other) const { 71 if (!has_type || !other.has_type) { 72 return true; // Can be considered equal for parsing purpose 73 } 74 if (kind != other.kind) { 75 return false; 76 } 77 switch (kind) { 78 case BUILTIN: { 79 return builtin_type == other.builtin_type; 80 } break; 81 case NATIVE: { 82 return native_type == other.native_type; 83 } break; 84 case GDSCRIPT: 85 case SCRIPT: { 86 return script_type == other.script_type; 87 } break; 88 case CLASS: { 89 return class_type == other.class_type; 90 } break; 91 case UNRESOLVED: { 92 } break; 93 } 94 return false; 95 } 96 DataTypeDataType97 DataType() : 98 kind(UNRESOLVED), 99 has_type(false), 100 is_constant(false), 101 is_meta_type(false), 102 infer_type(false), 103 may_yield(false), 104 builtin_type(Variant::NIL), 105 class_type(NULL) {} 106 }; 107 108 struct Node { 109 110 enum Type { 111 TYPE_CLASS, 112 TYPE_FUNCTION, 113 TYPE_BUILT_IN_FUNCTION, 114 TYPE_BLOCK, 115 TYPE_IDENTIFIER, 116 TYPE_TYPE, 117 TYPE_CONSTANT, 118 TYPE_ARRAY, 119 TYPE_DICTIONARY, 120 TYPE_SELF, 121 TYPE_OPERATOR, 122 TYPE_CONTROL_FLOW, 123 TYPE_LOCAL_VAR, 124 TYPE_CAST, 125 TYPE_ASSERT, 126 TYPE_BREAKPOINT, 127 TYPE_NEWLINE, 128 }; 129 130 Node *next; 131 int line; 132 int column; 133 Type type; 134 get_datatypeNode135 virtual DataType get_datatype() const { return DataType(); } set_datatypeNode136 virtual void set_datatype(const DataType &p_datatype) {} 137 ~NodeNode138 virtual ~Node() {} 139 }; 140 141 struct FunctionNode; 142 struct BlockNode; 143 struct ConstantNode; 144 struct LocalVarNode; 145 struct OperatorNode; 146 147 struct ClassNode : public Node { 148 149 bool tool; 150 StringName name; 151 bool extends_used; 152 bool classname_used; 153 StringName extends_file; 154 Vector<StringName> extends_class; 155 DataType base_type; 156 String icon_path; 157 158 struct Member { 159 PropertyInfo _export; 160 #ifdef TOOLS_ENABLED 161 Variant default_value; 162 #endif 163 StringName identifier; 164 DataType data_type; 165 StringName setter; 166 StringName getter; 167 int line; 168 Node *expression; 169 OperatorNode *initial_assignment; 170 MultiplayerAPI::RPCMode rpc_mode; 171 int usages; 172 }; 173 174 struct Constant { 175 Node *expression; 176 DataType type; 177 }; 178 179 struct Signal { 180 StringName name; 181 Vector<StringName> arguments; 182 int emissions; 183 int line; 184 }; 185 186 Vector<ClassNode *> subclasses; 187 Vector<Member> variables; 188 Map<StringName, Constant> constant_expressions; 189 Vector<FunctionNode *> functions; 190 Vector<FunctionNode *> static_functions; 191 Vector<Signal> _signals; 192 BlockNode *initializer; 193 BlockNode *ready; 194 ClassNode *owner; 195 //Vector<Node*> initializers; 196 int end_line; 197 ClassNodeClassNode198 ClassNode() { 199 tool = false; 200 type = TYPE_CLASS; 201 extends_used = false; 202 classname_used = false; 203 end_line = -1; 204 owner = NULL; 205 } 206 }; 207 208 struct FunctionNode : public Node { 209 210 bool _static; 211 MultiplayerAPI::RPCMode rpc_mode; 212 bool has_yield; 213 bool has_unreachable_code; 214 StringName name; 215 DataType return_type; 216 Vector<StringName> arguments; 217 Vector<DataType> argument_types; 218 Vector<Node *> default_values; 219 BlockNode *body; 220 #ifdef DEBUG_ENABLED 221 Vector<int> arguments_usage; 222 #endif // DEBUG_ENABLED 223 get_datatypeFunctionNode224 virtual DataType get_datatype() const { return return_type; } set_datatypeFunctionNode225 virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; } get_required_argument_countFunctionNode226 int get_required_argument_count() { return arguments.size() - default_values.size(); } 227 FunctionNodeFunctionNode228 FunctionNode() { 229 type = TYPE_FUNCTION; 230 _static = false; 231 rpc_mode = MultiplayerAPI::RPC_MODE_DISABLED; 232 has_yield = false; 233 has_unreachable_code = false; 234 } 235 }; 236 237 struct BlockNode : public Node { 238 239 ClassNode *parent_class; 240 BlockNode *parent_block; 241 List<Node *> statements; 242 Map<StringName, LocalVarNode *> variables; 243 bool has_return = false; 244 bool can_break = false; 245 bool can_continue = false; 246 247 Node *if_condition; //tiny hack to improve code completion on if () blocks 248 249 //the following is useful for code completion 250 List<BlockNode *> sub_blocks; 251 int end_line; BlockNodeBlockNode252 BlockNode() { 253 if_condition = NULL; 254 type = TYPE_BLOCK; 255 end_line = -1; 256 parent_block = NULL; 257 parent_class = NULL; 258 } 259 }; 260 261 struct TypeNode : public Node { 262 263 Variant::Type vtype; TypeNodeTypeNode264 TypeNode() { type = TYPE_TYPE; } 265 }; 266 struct BuiltInFunctionNode : public Node { 267 GDScriptFunctions::Function function; BuiltInFunctionNodeBuiltInFunctionNode268 BuiltInFunctionNode() { type = TYPE_BUILT_IN_FUNCTION; } 269 }; 270 271 struct IdentifierNode : public Node { 272 273 StringName name; 274 BlockNode *declared_block; // Simplify lookup by checking if it is declared locally 275 DataType datatype; get_datatypeIdentifierNode276 virtual DataType get_datatype() const { return datatype; } set_datatypeIdentifierNode277 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } IdentifierNodeIdentifierNode278 IdentifierNode() { 279 type = TYPE_IDENTIFIER; 280 declared_block = NULL; 281 } 282 }; 283 284 struct LocalVarNode : public Node { 285 286 StringName name; 287 Node *assign; 288 OperatorNode *assign_op; 289 int assignments; 290 int usages; 291 DataType datatype; get_datatypeLocalVarNode292 virtual DataType get_datatype() const { return datatype; } set_datatypeLocalVarNode293 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } LocalVarNodeLocalVarNode294 LocalVarNode() { 295 type = TYPE_LOCAL_VAR; 296 assign = NULL; 297 assign_op = NULL; 298 assignments = 0; 299 usages = 0; 300 } 301 }; 302 303 struct ConstantNode : public Node { 304 Variant value; 305 DataType datatype; get_datatypeConstantNode306 virtual DataType get_datatype() const { return datatype; } set_datatypeConstantNode307 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } ConstantNodeConstantNode308 ConstantNode() { type = TYPE_CONSTANT; } 309 }; 310 311 struct ArrayNode : public Node { 312 313 Vector<Node *> elements; 314 DataType datatype; get_datatypeArrayNode315 virtual DataType get_datatype() const { return datatype; } set_datatypeArrayNode316 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } ArrayNodeArrayNode317 ArrayNode() { 318 type = TYPE_ARRAY; 319 datatype.has_type = true; 320 datatype.kind = DataType::BUILTIN; 321 datatype.builtin_type = Variant::ARRAY; 322 } 323 }; 324 325 struct DictionaryNode : public Node { 326 327 struct Pair { 328 329 Node *key; 330 Node *value; 331 }; 332 333 Vector<Pair> elements; 334 DataType datatype; get_datatypeDictionaryNode335 virtual DataType get_datatype() const { return datatype; } set_datatypeDictionaryNode336 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } DictionaryNodeDictionaryNode337 DictionaryNode() { 338 type = TYPE_DICTIONARY; 339 datatype.has_type = true; 340 datatype.kind = DataType::BUILTIN; 341 datatype.builtin_type = Variant::DICTIONARY; 342 } 343 }; 344 345 struct SelfNode : public Node { SelfNodeSelfNode346 SelfNode() { type = TYPE_SELF; } 347 }; 348 349 struct OperatorNode : public Node { 350 enum Operator { 351 //call/constructor operator 352 OP_CALL, 353 OP_PARENT_CALL, 354 OP_YIELD, 355 OP_IS, 356 OP_IS_BUILTIN, 357 //indexing operator 358 OP_INDEX, 359 OP_INDEX_NAMED, 360 //unary operators 361 OP_NEG, 362 OP_POS, 363 OP_NOT, 364 OP_BIT_INVERT, 365 //binary operators (in precedence order) 366 OP_IN, 367 OP_EQUAL, 368 OP_NOT_EQUAL, 369 OP_LESS, 370 OP_LESS_EQUAL, 371 OP_GREATER, 372 OP_GREATER_EQUAL, 373 OP_AND, 374 OP_OR, 375 OP_ADD, 376 OP_SUB, 377 OP_MUL, 378 OP_DIV, 379 OP_MOD, 380 OP_SHIFT_LEFT, 381 OP_SHIFT_RIGHT, 382 OP_INIT_ASSIGN, 383 OP_ASSIGN, 384 OP_ASSIGN_ADD, 385 OP_ASSIGN_SUB, 386 OP_ASSIGN_MUL, 387 OP_ASSIGN_DIV, 388 OP_ASSIGN_MOD, 389 OP_ASSIGN_SHIFT_LEFT, 390 OP_ASSIGN_SHIFT_RIGHT, 391 OP_ASSIGN_BIT_AND, 392 OP_ASSIGN_BIT_OR, 393 OP_ASSIGN_BIT_XOR, 394 OP_BIT_AND, 395 OP_BIT_OR, 396 OP_BIT_XOR, 397 //ternary operators 398 OP_TERNARY_IF, 399 OP_TERNARY_ELSE, 400 }; 401 402 Operator op; 403 404 Vector<Node *> arguments; 405 DataType datatype; get_datatypeOperatorNode406 virtual DataType get_datatype() const { return datatype; } set_datatypeOperatorNode407 virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; } OperatorNodeOperatorNode408 OperatorNode() { type = TYPE_OPERATOR; } 409 }; 410 411 struct PatternNode : public Node { 412 413 enum PatternType { 414 PT_CONSTANT, 415 PT_BIND, 416 PT_DICTIONARY, 417 PT_ARRAY, 418 PT_IGNORE_REST, 419 PT_WILDCARD 420 }; 421 422 PatternType pt_type; 423 424 Node *constant; 425 StringName bind; 426 Map<ConstantNode *, PatternNode *> dictionary; 427 Vector<PatternNode *> array; 428 }; 429 430 struct PatternBranchNode : public Node { 431 Vector<PatternNode *> patterns; 432 BlockNode *body; 433 }; 434 435 struct MatchNode : public Node { 436 Node *val_to_match; 437 Vector<PatternBranchNode *> branches; 438 439 struct CompiledPatternBranch { 440 Node *compiled_pattern; 441 BlockNode *body; 442 }; 443 444 Vector<CompiledPatternBranch> compiled_pattern_branches; 445 }; 446 447 struct ControlFlowNode : public Node { 448 enum CFType { 449 CF_IF, 450 CF_FOR, 451 CF_WHILE, 452 CF_BREAK, 453 CF_CONTINUE, 454 CF_RETURN, 455 CF_MATCH 456 }; 457 458 CFType cf_type; 459 Vector<Node *> arguments; 460 BlockNode *body; 461 BlockNode *body_else; 462 463 MatchNode *match; 464 465 ControlFlowNode *_else; //used for if ControlFlowNodeControlFlowNode466 ControlFlowNode() { 467 type = TYPE_CONTROL_FLOW; 468 cf_type = CF_IF; 469 body = NULL; 470 body_else = NULL; 471 } 472 }; 473 474 struct CastNode : public Node { 475 Node *source_node; 476 DataType cast_type; 477 DataType return_type; get_datatypeCastNode478 virtual DataType get_datatype() const { return return_type; } set_datatypeCastNode479 virtual void set_datatype(const DataType &p_datatype) { return_type = p_datatype; } CastNodeCastNode480 CastNode() { type = TYPE_CAST; } 481 }; 482 483 struct AssertNode : public Node { 484 Node *condition; 485 Node *message; AssertNodeAssertNode486 AssertNode() : 487 condition(0), 488 message(0) { 489 type = TYPE_ASSERT; 490 } 491 }; 492 493 struct BreakpointNode : public Node { BreakpointNodeBreakpointNode494 BreakpointNode() { type = TYPE_BREAKPOINT; } 495 }; 496 497 struct NewLineNode : public Node { NewLineNodeNewLineNode498 NewLineNode() { type = TYPE_NEWLINE; } 499 }; 500 501 struct Expression { 502 503 bool is_op; 504 union { 505 OperatorNode::Operator op; 506 Node *node; 507 }; 508 }; 509 510 enum CompletionType { 511 COMPLETION_NONE, 512 COMPLETION_BUILT_IN_TYPE_CONSTANT, 513 COMPLETION_GET_NODE, 514 COMPLETION_FUNCTION, 515 COMPLETION_IDENTIFIER, 516 COMPLETION_EXTENDS, 517 COMPLETION_PARENT_FUNCTION, 518 COMPLETION_METHOD, 519 COMPLETION_CALL_ARGUMENTS, 520 COMPLETION_RESOURCE_PATH, 521 COMPLETION_INDEX, 522 COMPLETION_VIRTUAL_FUNC, 523 COMPLETION_YIELD, 524 COMPLETION_ASSIGN, 525 COMPLETION_TYPE_HINT, 526 COMPLETION_TYPE_HINT_INDEX, 527 }; 528 529 private: 530 GDScriptTokenizer *tokenizer; 531 532 Node *head; 533 Node *list; 534 template <class T> 535 T *alloc_node(); 536 537 bool validating; 538 bool for_completion; 539 int parenthesis; 540 bool error_set; 541 String error; 542 int error_line; 543 int error_column; 544 bool check_types; 545 bool dependencies_only; 546 List<String> dependencies; 547 #ifdef DEBUG_ENABLED 548 Set<int> *safe_lines; 549 #endif // DEBUG_ENABLED 550 551 #ifdef DEBUG_ENABLED 552 List<GDScriptWarning> warnings; 553 #endif // DEBUG_ENABLED 554 555 int pending_newline; 556 557 struct IndentLevel { 558 int indent; 559 int tabs; 560 is_mixedIndentLevel561 bool is_mixed(IndentLevel other) { 562 return ( 563 (indent == other.indent && tabs != other.tabs) || 564 (indent > other.indent && tabs < other.tabs) || 565 (indent < other.indent && tabs > other.tabs)); 566 } 567 IndentLevelIndentLevel568 IndentLevel() : 569 indent(0), 570 tabs(0) {} 571 IndentLevelIndentLevel572 IndentLevel(int p_indent, int p_tabs) : 573 indent(p_indent), 574 tabs(p_tabs) {} 575 }; 576 577 List<IndentLevel> indent_level; 578 579 String base_path; 580 String self_path; 581 582 ClassNode *current_class; 583 FunctionNode *current_function; 584 BlockNode *current_block; 585 586 bool _get_completable_identifier(CompletionType p_type, StringName &identifier); 587 void _make_completable_call(int p_arg); 588 589 CompletionType completion_type; 590 StringName completion_cursor; 591 Variant::Type completion_built_in_constant; 592 Node *completion_node; 593 ClassNode *completion_class; 594 FunctionNode *completion_function; 595 BlockNode *completion_block; 596 int completion_line; 597 int completion_argument; 598 bool completion_found; 599 bool completion_ident_is_call; 600 601 PropertyInfo current_export; 602 603 MultiplayerAPI::RPCMode rpc_mode; 604 605 void _set_error(const String &p_error, int p_line = -1, int p_column = -1); 606 #ifdef DEBUG_ENABLED 607 void _add_warning(int p_code, int p_line = -1, const String &p_symbol1 = String(), const String &p_symbol2 = String(), const String &p_symbol3 = String(), const String &p_symbol4 = String()); 608 void _add_warning(int p_code, int p_line, const Vector<String> &p_symbols); 609 #endif // DEBUG_ENABLED 610 bool _recover_from_completion(); 611 612 bool _parse_arguments(Node *p_parent, Vector<Node *> &p_args, bool p_static, bool p_can_codecomplete = false, bool p_parsing_constant = false); 613 bool _enter_indent_block(BlockNode *p_block = NULL); 614 bool _parse_newline(); 615 Node *_parse_expression(Node *p_parent, bool p_static, bool p_allow_assign = false, bool p_parsing_constant = false); 616 Node *_reduce_expression(Node *p_node, bool p_to_const = false); 617 Node *_parse_and_reduce_expression(Node *p_parent, bool p_static, bool p_reduce_const = false, bool p_allow_assign = false); 618 bool _reduce_export_var_type(Variant &p_value, int p_line = 0); 619 620 PatternNode *_parse_pattern(bool p_static); 621 void _parse_pattern_block(BlockNode *p_block, Vector<PatternBranchNode *> &p_branches, bool p_static); 622 void _transform_match_statment(MatchNode *p_match_statement); 623 void _generate_pattern(PatternNode *p_pattern, Node *p_node_to_match, Node *&p_resulting_node, Map<StringName, Node *> &p_bindings); 624 625 void _parse_block(BlockNode *p_block, bool p_static); 626 void _parse_extends(ClassNode *p_class); 627 void _parse_class(ClassNode *p_class); 628 bool _end_statement(); 629 void _set_end_statement_error(String p_name); 630 631 void _determine_inheritance(ClassNode *p_class, bool p_recursive = true); 632 bool _parse_type(DataType &r_type, bool p_can_be_void = false); 633 DataType _resolve_type(const DataType &p_source, int p_line); 634 DataType _type_from_variant(const Variant &p_value) const; 635 DataType _type_from_property(const PropertyInfo &p_property, bool p_nil_is_variant = true) const; 636 DataType _type_from_gdtype(const GDScriptDataType &p_gdtype) const; 637 DataType _get_operation_type(const Variant::Operator p_op, const DataType &p_a, const DataType &p_b, bool &r_valid) const; 638 Variant::Operator _get_variant_operation(const OperatorNode::Operator &p_op) const; 639 bool _get_function_signature(DataType &p_base_type, const StringName &p_function, DataType &r_return_type, List<DataType> &r_arg_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) const; 640 bool _get_member_type(const DataType &p_base_type, const StringName &p_member, DataType &r_member_type, bool *r_is_const = nullptr) const; 641 bool _is_type_compatible(const DataType &p_container, const DataType &p_expression, bool p_allow_implicit_conversion = false) const; 642 Node *_get_default_value_for_type(const DataType &p_type, int p_line = -1); 643 644 DataType _reduce_node_type(Node *p_node); 645 DataType _reduce_function_call_type(const OperatorNode *p_call); 646 DataType _reduce_identifier_type(const DataType *p_base_type, const StringName &p_identifier, int p_line, bool p_is_indexing); 647 void _check_class_level_types(ClassNode *p_class); 648 void _check_class_blocks_types(ClassNode *p_class); 649 void _check_function_types(FunctionNode *p_function); 650 void _check_block_types(BlockNode *p_block); _mark_line_as_safe(int p_line)651 _FORCE_INLINE_ void _mark_line_as_safe(int p_line) const { 652 #ifdef DEBUG_ENABLED 653 if (safe_lines) safe_lines->insert(p_line); 654 #endif // DEBUG_ENABLED 655 } _mark_line_as_unsafe(int p_line)656 _FORCE_INLINE_ void _mark_line_as_unsafe(int p_line) const { 657 #ifdef DEBUG_ENABLED 658 if (safe_lines) safe_lines->erase(p_line); 659 #endif // DEBUG_ENABLED 660 } 661 662 Error _parse(const String &p_base_path); 663 664 public: 665 bool has_error() const; 666 String get_error() const; 667 int get_error_line() const; 668 int get_error_column() const; 669 #ifdef DEBUG_ENABLED get_warnings()670 const List<GDScriptWarning> &get_warnings() const { return warnings; } 671 #endif // DEBUG_ENABLED 672 Error parse(const String &p_code, const String &p_base_path = "", bool p_just_validate = false, const String &p_self_path = "", bool p_for_completion = false, Set<int> *r_safe_lines = NULL, bool p_dependencies_only = false); 673 Error parse_bytecode(const Vector<uint8_t> &p_bytecode, const String &p_base_path = "", const String &p_self_path = ""); 674 675 bool is_tool_script() const; 676 const Node *get_parse_tree() const; 677 678 //completion info 679 680 CompletionType get_completion_type(); 681 StringName get_completion_cursor(); 682 int get_completion_line(); 683 Variant::Type get_completion_built_in_constant(); 684 Node *get_completion_node(); 685 ClassNode *get_completion_class(); 686 BlockNode *get_completion_block(); 687 FunctionNode *get_completion_function(); 688 int get_completion_argument_index(); 689 int get_completion_identifier_is_function(); 690 get_dependencies()691 const List<String> &get_dependencies() const { return dependencies; } 692 693 void clear(); 694 GDScriptParser(); 695 ~GDScriptParser(); 696 }; 697 698 #endif // GDSCRIPT_PARSER_H 699