gdscript_parser.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682
  1. /**************************************************************************/
  2. /* gdscript_parser.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. #pragma once
  31. #include "gdscript_cache.h"
  32. #include "gdscript_tokenizer.h"
  33. #ifdef DEBUG_ENABLED
  34. #include "gdscript_warning.h"
  35. #endif
  36. #include "core/io/resource.h"
  37. #include "core/object/ref_counted.h"
  38. #include "core/object/script_language.h"
  39. #include "core/string/string_name.h"
  40. #include "core/string/ustring.h"
  41. #include "core/templates/hash_map.h"
  42. #include "core/templates/list.h"
  43. #include "core/templates/vector.h"
  44. #include "core/variant/variant.h"
  45. #ifdef DEBUG_ENABLED
  46. #include "core/string/string_builder.h"
  47. #endif
  48. class GDScriptParser {
  49. struct AnnotationInfo;
  50. public:
  51. // Forward-declare all parser nodes, to avoid ordering issues.
  52. struct AnnotationNode;
  53. struct ArrayNode;
  54. struct AssertNode;
  55. struct AssignableNode;
  56. struct AssignmentNode;
  57. struct AwaitNode;
  58. struct BinaryOpNode;
  59. struct BreakNode;
  60. struct BreakpointNode;
  61. struct CallNode;
  62. struct CastNode;
  63. struct ClassNode;
  64. struct ConstantNode;
  65. struct ContinueNode;
  66. struct DictionaryNode;
  67. struct EnumNode;
  68. struct ExpressionNode;
  69. struct ForNode;
  70. struct FunctionNode;
  71. struct GetNodeNode;
  72. struct IdentifierNode;
  73. struct IfNode;
  74. struct LambdaNode;
  75. struct LiteralNode;
  76. struct MatchNode;
  77. struct MatchBranchNode;
  78. struct ParameterNode;
  79. struct PassNode;
  80. struct PatternNode;
  81. struct PreloadNode;
  82. struct ReturnNode;
  83. struct SelfNode;
  84. struct SignalNode;
  85. struct SubscriptNode;
  86. struct SuiteNode;
  87. struct TernaryOpNode;
  88. struct TypeNode;
  89. struct TypeTestNode;
  90. struct UnaryOpNode;
  91. struct VariableNode;
  92. struct WhileNode;
  93. class DataType {
  94. public:
  95. Vector<DataType> container_element_types;
  96. enum Kind {
  97. BUILTIN,
  98. NATIVE,
  99. SCRIPT,
  100. CLASS, // GDScript.
  101. ENUM, // Enumeration.
  102. VARIANT, // Can be any type.
  103. RESOLVING, // Currently resolving.
  104. UNRESOLVED,
  105. };
  106. Kind kind = UNRESOLVED;
  107. enum TypeSource {
  108. UNDETECTED, // Can be any type.
  109. INFERRED, // Has inferred type, but still dynamic.
  110. ANNOTATED_EXPLICIT, // Has a specific type annotated.
  111. ANNOTATED_INFERRED, // Has a static type but comes from the assigned value.
  112. };
  113. TypeSource type_source = UNDETECTED;
  114. bool is_constant = false;
  115. bool is_read_only = false;
  116. bool is_meta_type = false;
  117. bool is_pseudo_type = false; // For global names that can't be used standalone.
  118. bool is_coroutine = false; // For function calls.
  119. Variant::Type builtin_type = Variant::NIL;
  120. StringName native_type;
  121. StringName enum_type; // Enum name or the value name in an enum.
  122. Ref<Script> script_type;
  123. String script_path;
  124. ClassNode *class_type = nullptr;
  125. MethodInfo method_info; // For callable/signals.
  126. HashMap<StringName, int64_t> enum_values; // For enums.
  127. _FORCE_INLINE_ bool is_set() const { return kind != RESOLVING && kind != UNRESOLVED; }
  128. _FORCE_INLINE_ bool is_resolving() const { return kind == RESOLVING; }
  129. _FORCE_INLINE_ bool has_no_type() const { return type_source == UNDETECTED; }
  130. _FORCE_INLINE_ bool is_variant() const { return kind == VARIANT || kind == RESOLVING || kind == UNRESOLVED; }
  131. _FORCE_INLINE_ bool is_hard_type() const { return type_source > INFERRED; }
  132. String to_string() const;
  133. _FORCE_INLINE_ String to_string_strict() const { return is_hard_type() ? to_string() : "Variant"; }
  134. PropertyInfo to_property_info(const String &p_name) const;
  135. _FORCE_INLINE_ static DataType get_variant_type() { // Default DataType for container elements.
  136. DataType datatype;
  137. datatype.kind = VARIANT;
  138. datatype.type_source = INFERRED;
  139. return datatype;
  140. }
  141. _FORCE_INLINE_ void set_container_element_type(int p_index, const DataType &p_type) {
  142. ERR_FAIL_COND(p_index < 0);
  143. while (p_index >= container_element_types.size()) {
  144. container_element_types.push_back(get_variant_type());
  145. }
  146. container_element_types.write[p_index] = DataType(p_type);
  147. }
  148. _FORCE_INLINE_ int get_container_element_type_count() const {
  149. return container_element_types.size();
  150. }
  151. _FORCE_INLINE_ DataType get_container_element_type(int p_index) const {
  152. ERR_FAIL_INDEX_V(p_index, container_element_types.size(), get_variant_type());
  153. return container_element_types[p_index];
  154. }
  155. _FORCE_INLINE_ DataType get_container_element_type_or_variant(int p_index) const {
  156. if (p_index < 0 || p_index >= container_element_types.size()) {
  157. return get_variant_type();
  158. }
  159. return container_element_types[p_index];
  160. }
  161. _FORCE_INLINE_ bool has_container_element_type(int p_index) const {
  162. return p_index >= 0 && p_index < container_element_types.size();
  163. }
  164. _FORCE_INLINE_ bool has_container_element_types() const {
  165. return !container_element_types.is_empty();
  166. }
  167. bool is_typed_container_type() const;
  168. GDScriptParser::DataType get_typed_container_type() const;
  169. bool can_reference(const DataType &p_other) const;
  170. bool operator==(const DataType &p_other) const {
  171. if (type_source == UNDETECTED || p_other.type_source == UNDETECTED) {
  172. return true; // Can be considered equal for parsing purposes.
  173. }
  174. if (type_source == INFERRED || p_other.type_source == INFERRED) {
  175. return true; // Can be considered equal for parsing purposes.
  176. }
  177. if (kind != p_other.kind) {
  178. return false;
  179. }
  180. switch (kind) {
  181. case VARIANT:
  182. return true; // All variants are the same.
  183. case BUILTIN:
  184. return builtin_type == p_other.builtin_type;
  185. case NATIVE:
  186. case ENUM: // Enums use native_type to identify the enum and its base class.
  187. return native_type == p_other.native_type;
  188. case SCRIPT:
  189. return script_type == p_other.script_type;
  190. case CLASS:
  191. return class_type == p_other.class_type || class_type->fqcn == p_other.class_type->fqcn;
  192. case RESOLVING:
  193. case UNRESOLVED:
  194. break;
  195. }
  196. return false;
  197. }
  198. bool operator!=(const DataType &p_other) const {
  199. return !(*this == p_other);
  200. }
  201. void operator=(const DataType &p_other) {
  202. kind = p_other.kind;
  203. type_source = p_other.type_source;
  204. is_read_only = p_other.is_read_only;
  205. is_constant = p_other.is_constant;
  206. is_meta_type = p_other.is_meta_type;
  207. is_pseudo_type = p_other.is_pseudo_type;
  208. is_coroutine = p_other.is_coroutine;
  209. builtin_type = p_other.builtin_type;
  210. native_type = p_other.native_type;
  211. enum_type = p_other.enum_type;
  212. script_type = p_other.script_type;
  213. script_path = p_other.script_path;
  214. class_type = p_other.class_type;
  215. method_info = p_other.method_info;
  216. enum_values = p_other.enum_values;
  217. container_element_types = p_other.container_element_types;
  218. }
  219. DataType() = default;
  220. DataType(const DataType &p_other) {
  221. *this = p_other;
  222. }
  223. ~DataType() {}
  224. };
  225. struct ParserError {
  226. // TODO: Do I really need a "type"?
  227. // enum Type {
  228. // NO_ERROR,
  229. // EMPTY_FILE,
  230. // CLASS_NAME_USED_TWICE,
  231. // EXTENDS_USED_TWICE,
  232. // EXPECTED_END_STATEMENT,
  233. // };
  234. // Type type = NO_ERROR;
  235. String message;
  236. int line = 0, column = 0;
  237. };
  238. #ifdef TOOLS_ENABLED
  239. struct ClassDocData {
  240. String brief;
  241. String description;
  242. Vector<Pair<String, String>> tutorials;
  243. bool is_deprecated = false;
  244. String deprecated_message;
  245. bool is_experimental = false;
  246. String experimental_message;
  247. };
  248. struct MemberDocData {
  249. String description;
  250. bool is_deprecated = false;
  251. String deprecated_message;
  252. bool is_experimental = false;
  253. String experimental_message;
  254. };
  255. #endif // TOOLS_ENABLED
  256. struct Node {
  257. enum Type {
  258. NONE,
  259. ANNOTATION,
  260. ARRAY,
  261. ASSERT,
  262. ASSIGNMENT,
  263. AWAIT,
  264. BINARY_OPERATOR,
  265. BREAK,
  266. BREAKPOINT,
  267. CALL,
  268. CAST,
  269. CLASS,
  270. CONSTANT,
  271. CONTINUE,
  272. DICTIONARY,
  273. ENUM,
  274. FOR,
  275. FUNCTION,
  276. GET_NODE,
  277. IDENTIFIER,
  278. IF,
  279. LAMBDA,
  280. LITERAL,
  281. MATCH,
  282. MATCH_BRANCH,
  283. PARAMETER,
  284. PASS,
  285. PATTERN,
  286. PRELOAD,
  287. RETURN,
  288. SELF,
  289. SIGNAL,
  290. SUBSCRIPT,
  291. SUITE,
  292. TERNARY_OPERATOR,
  293. TYPE,
  294. TYPE_TEST,
  295. UNARY_OPERATOR,
  296. VARIABLE,
  297. WHILE,
  298. };
  299. Type type = NONE;
  300. int start_line = 0, end_line = 0;
  301. int start_column = 0, end_column = 0;
  302. Node *next = nullptr;
  303. List<AnnotationNode *> annotations;
  304. DataType datatype;
  305. virtual DataType get_datatype() const { return datatype; }
  306. virtual void set_datatype(const DataType &p_datatype) { datatype = p_datatype; }
  307. virtual bool is_expression() const { return false; }
  308. virtual ~Node() {}
  309. };
  310. struct ExpressionNode : public Node {
  311. // Base type for all expression kinds.
  312. bool reduced = false;
  313. bool is_constant = false;
  314. Variant reduced_value;
  315. virtual bool is_expression() const override { return true; }
  316. virtual ~ExpressionNode() {}
  317. protected:
  318. ExpressionNode() {}
  319. };
  320. struct AnnotationNode : public Node {
  321. StringName name;
  322. Vector<ExpressionNode *> arguments;
  323. Vector<Variant> resolved_arguments;
  324. /** Information of the annotation. Might be null for unknown annotations. */
  325. AnnotationInfo *info = nullptr;
  326. PropertyInfo export_info;
  327. bool is_resolved = false;
  328. bool is_applied = false;
  329. bool apply(GDScriptParser *p_this, Node *p_target, ClassNode *p_class);
  330. bool applies_to(uint32_t p_target_kinds) const;
  331. AnnotationNode() {
  332. type = ANNOTATION;
  333. }
  334. };
  335. struct ArrayNode : public ExpressionNode {
  336. Vector<ExpressionNode *> elements;
  337. ArrayNode() {
  338. type = ARRAY;
  339. }
  340. };
  341. struct AssertNode : public Node {
  342. ExpressionNode *condition = nullptr;
  343. ExpressionNode *message = nullptr;
  344. AssertNode() {
  345. type = ASSERT;
  346. }
  347. };
  348. struct AssignableNode : public Node {
  349. IdentifierNode *identifier = nullptr;
  350. ExpressionNode *initializer = nullptr;
  351. TypeNode *datatype_specifier = nullptr;
  352. bool infer_datatype = false;
  353. bool use_conversion_assign = false;
  354. int usages = 0;
  355. virtual ~AssignableNode() {}
  356. protected:
  357. AssignableNode() {}
  358. };
  359. struct AssignmentNode : public ExpressionNode {
  360. // Assignment is not really an expression but it's easier to parse as if it were.
  361. enum Operation {
  362. OP_NONE,
  363. OP_ADDITION,
  364. OP_SUBTRACTION,
  365. OP_MULTIPLICATION,
  366. OP_DIVISION,
  367. OP_MODULO,
  368. OP_POWER,
  369. OP_BIT_SHIFT_LEFT,
  370. OP_BIT_SHIFT_RIGHT,
  371. OP_BIT_AND,
  372. OP_BIT_OR,
  373. OP_BIT_XOR,
  374. };
  375. Operation operation = OP_NONE;
  376. Variant::Operator variant_op = Variant::OP_MAX;
  377. ExpressionNode *assignee = nullptr;
  378. ExpressionNode *assigned_value = nullptr;
  379. bool use_conversion_assign = false;
  380. AssignmentNode() {
  381. type = ASSIGNMENT;
  382. }
  383. };
  384. struct AwaitNode : public ExpressionNode {
  385. ExpressionNode *to_await = nullptr;
  386. AwaitNode() {
  387. type = AWAIT;
  388. }
  389. };
  390. struct BinaryOpNode : public ExpressionNode {
  391. enum OpType {
  392. OP_ADDITION,
  393. OP_SUBTRACTION,
  394. OP_MULTIPLICATION,
  395. OP_DIVISION,
  396. OP_MODULO,
  397. OP_POWER,
  398. OP_BIT_LEFT_SHIFT,
  399. OP_BIT_RIGHT_SHIFT,
  400. OP_BIT_AND,
  401. OP_BIT_OR,
  402. OP_BIT_XOR,
  403. OP_LOGIC_AND,
  404. OP_LOGIC_OR,
  405. OP_CONTENT_TEST,
  406. OP_COMP_EQUAL,
  407. OP_COMP_NOT_EQUAL,
  408. OP_COMP_LESS,
  409. OP_COMP_LESS_EQUAL,
  410. OP_COMP_GREATER,
  411. OP_COMP_GREATER_EQUAL,
  412. };
  413. OpType operation = OpType::OP_ADDITION;
  414. Variant::Operator variant_op = Variant::OP_MAX;
  415. ExpressionNode *left_operand = nullptr;
  416. ExpressionNode *right_operand = nullptr;
  417. BinaryOpNode() {
  418. type = BINARY_OPERATOR;
  419. }
  420. };
  421. struct BreakNode : public Node {
  422. BreakNode() {
  423. type = BREAK;
  424. }
  425. };
  426. struct BreakpointNode : public Node {
  427. BreakpointNode() {
  428. type = BREAKPOINT;
  429. }
  430. };
  431. struct CallNode : public ExpressionNode {
  432. ExpressionNode *callee = nullptr;
  433. Vector<ExpressionNode *> arguments;
  434. StringName function_name;
  435. bool is_super = false;
  436. bool is_static = false;
  437. CallNode() {
  438. type = CALL;
  439. }
  440. Type get_callee_type() const {
  441. if (callee == nullptr) {
  442. return Type::NONE;
  443. } else {
  444. return callee->type;
  445. }
  446. }
  447. };
  448. struct CastNode : public ExpressionNode {
  449. ExpressionNode *operand = nullptr;
  450. TypeNode *cast_type = nullptr;
  451. CastNode() {
  452. type = CAST;
  453. }
  454. };
  455. struct EnumNode : public Node {
  456. struct Value {
  457. IdentifierNode *identifier = nullptr;
  458. ExpressionNode *custom_value = nullptr;
  459. EnumNode *parent_enum = nullptr;
  460. int index = -1;
  461. bool resolved = false;
  462. int64_t value = 0;
  463. int line = 0;
  464. int start_column = 0;
  465. int end_column = 0;
  466. #ifdef TOOLS_ENABLED
  467. MemberDocData doc_data;
  468. #endif // TOOLS_ENABLED
  469. };
  470. IdentifierNode *identifier = nullptr;
  471. Vector<Value> values;
  472. Variant dictionary;
  473. #ifdef TOOLS_ENABLED
  474. MemberDocData doc_data;
  475. #endif // TOOLS_ENABLED
  476. EnumNode() {
  477. type = ENUM;
  478. }
  479. };
  480. struct ClassNode : public Node {
  481. struct Member {
  482. enum Type {
  483. UNDEFINED,
  484. CLASS,
  485. CONSTANT,
  486. FUNCTION,
  487. SIGNAL,
  488. VARIABLE,
  489. ENUM,
  490. ENUM_VALUE, // For unnamed enums.
  491. GROUP, // For member grouping.
  492. };
  493. Type type = UNDEFINED;
  494. union {
  495. ClassNode *m_class = nullptr;
  496. ConstantNode *constant;
  497. FunctionNode *function;
  498. SignalNode *signal;
  499. VariableNode *variable;
  500. EnumNode *m_enum;
  501. AnnotationNode *annotation;
  502. };
  503. EnumNode::Value enum_value;
  504. String get_name() const {
  505. switch (type) {
  506. case UNDEFINED:
  507. return "<undefined member>";
  508. case CLASS:
  509. // All class-type members have an id.
  510. return m_class->identifier->name;
  511. case CONSTANT:
  512. return constant->identifier->name;
  513. case FUNCTION:
  514. return function->identifier->name;
  515. case SIGNAL:
  516. return signal->identifier->name;
  517. case VARIABLE:
  518. return variable->identifier->name;
  519. case ENUM:
  520. // All enum-type members have an id.
  521. return m_enum->identifier->name;
  522. case ENUM_VALUE:
  523. return enum_value.identifier->name;
  524. case GROUP:
  525. return annotation->export_info.name;
  526. }
  527. return "";
  528. }
  529. String get_type_name() const {
  530. switch (type) {
  531. case UNDEFINED:
  532. return "???";
  533. case CLASS:
  534. return "class";
  535. case CONSTANT:
  536. return "constant";
  537. case FUNCTION:
  538. return "function";
  539. case SIGNAL:
  540. return "signal";
  541. case VARIABLE:
  542. return "variable";
  543. case ENUM:
  544. return "enum";
  545. case ENUM_VALUE:
  546. return "enum value";
  547. case GROUP:
  548. return "group";
  549. }
  550. return "";
  551. }
  552. int get_line() const {
  553. switch (type) {
  554. case CLASS:
  555. return m_class->start_line;
  556. case CONSTANT:
  557. return constant->start_line;
  558. case FUNCTION:
  559. return function->start_line;
  560. case VARIABLE:
  561. return variable->start_line;
  562. case ENUM_VALUE:
  563. return enum_value.line;
  564. case ENUM:
  565. return m_enum->start_line;
  566. case SIGNAL:
  567. return signal->start_line;
  568. case GROUP:
  569. return annotation->start_line;
  570. case UNDEFINED:
  571. ERR_FAIL_V_MSG(-1, "Reaching undefined member type.");
  572. }
  573. ERR_FAIL_V_MSG(-1, "Reaching unhandled type.");
  574. }
  575. DataType get_datatype() const {
  576. switch (type) {
  577. case CLASS:
  578. return m_class->get_datatype();
  579. case CONSTANT:
  580. return constant->get_datatype();
  581. case FUNCTION:
  582. return function->get_datatype();
  583. case VARIABLE:
  584. return variable->get_datatype();
  585. case ENUM:
  586. return m_enum->get_datatype();
  587. case ENUM_VALUE:
  588. return enum_value.identifier->get_datatype();
  589. case SIGNAL:
  590. return signal->get_datatype();
  591. case GROUP:
  592. return DataType();
  593. case UNDEFINED:
  594. return DataType();
  595. }
  596. ERR_FAIL_V_MSG(DataType(), "Reaching unhandled type.");
  597. }
  598. Node *get_source_node() const {
  599. switch (type) {
  600. case CLASS:
  601. return m_class;
  602. case CONSTANT:
  603. return constant;
  604. case FUNCTION:
  605. return function;
  606. case VARIABLE:
  607. return variable;
  608. case ENUM:
  609. return m_enum;
  610. case ENUM_VALUE:
  611. return enum_value.identifier;
  612. case SIGNAL:
  613. return signal;
  614. case GROUP:
  615. return annotation;
  616. case UNDEFINED:
  617. return nullptr;
  618. }
  619. ERR_FAIL_V_MSG(nullptr, "Reaching unhandled type.");
  620. }
  621. Member() {}
  622. Member(ClassNode *p_class) {
  623. type = CLASS;
  624. m_class = p_class;
  625. }
  626. Member(ConstantNode *p_constant) {
  627. type = CONSTANT;
  628. constant = p_constant;
  629. }
  630. Member(VariableNode *p_variable) {
  631. type = VARIABLE;
  632. variable = p_variable;
  633. }
  634. Member(SignalNode *p_signal) {
  635. type = SIGNAL;
  636. signal = p_signal;
  637. }
  638. Member(FunctionNode *p_function) {
  639. type = FUNCTION;
  640. function = p_function;
  641. }
  642. Member(EnumNode *p_enum) {
  643. type = ENUM;
  644. m_enum = p_enum;
  645. }
  646. Member(const EnumNode::Value &p_enum_value) {
  647. type = ENUM_VALUE;
  648. enum_value = p_enum_value;
  649. }
  650. Member(AnnotationNode *p_annotation) {
  651. type = GROUP;
  652. annotation = p_annotation;
  653. }
  654. };
  655. IdentifierNode *identifier = nullptr;
  656. String icon_path;
  657. String simplified_icon_path;
  658. Vector<Member> members;
  659. HashMap<StringName, int> members_indices;
  660. ClassNode *outer = nullptr;
  661. bool extends_used = false;
  662. bool onready_used = false;
  663. bool is_abstract = false;
  664. bool has_static_data = false;
  665. bool annotated_static_unload = false;
  666. String extends_path;
  667. Vector<IdentifierNode *> extends; // List for indexing: extends A.B.C
  668. DataType base_type;
  669. String fqcn; // Fully-qualified class name. Identifies uniquely any class in the project.
  670. #ifdef TOOLS_ENABLED
  671. ClassDocData doc_data;
  672. // EnumValue docs are parsed after itself, so we need a method to add/modify the doc property later.
  673. void set_enum_value_doc_data(const StringName &p_name, const MemberDocData &p_doc_data) {
  674. ERR_FAIL_INDEX(members_indices[p_name], members.size());
  675. members.write[members_indices[p_name]].enum_value.doc_data = p_doc_data;
  676. }
  677. #endif // TOOLS_ENABLED
  678. bool resolved_interface = false;
  679. bool resolved_body = false;
  680. StringName get_global_name() const {
  681. return (outer == nullptr && identifier != nullptr) ? identifier->name : StringName();
  682. }
  683. Member get_member(const StringName &p_name) const {
  684. return members[members_indices[p_name]];
  685. }
  686. bool has_member(const StringName &p_name) const {
  687. return members_indices.has(p_name);
  688. }
  689. bool has_function(const StringName &p_name) const {
  690. return has_member(p_name) && members[members_indices[p_name]].type == Member::FUNCTION;
  691. }
  692. template <typename T>
  693. void add_member(T *p_member_node) {
  694. members_indices[p_member_node->identifier->name] = members.size();
  695. members.push_back(Member(p_member_node));
  696. }
  697. void add_member(const EnumNode::Value &p_enum_value) {
  698. members_indices[p_enum_value.identifier->name] = members.size();
  699. members.push_back(Member(p_enum_value));
  700. }
  701. void add_member_group(AnnotationNode *p_annotation_node) {
  702. // Avoid name conflict. See GH-78252.
  703. StringName name = vformat("@group_%d_%s", members.size(), p_annotation_node->export_info.name);
  704. members_indices[name] = members.size();
  705. members.push_back(Member(p_annotation_node));
  706. }
  707. ClassNode() {
  708. type = CLASS;
  709. }
  710. };
  711. struct ConstantNode : public AssignableNode {
  712. #ifdef TOOLS_ENABLED
  713. MemberDocData doc_data;
  714. #endif // TOOLS_ENABLED
  715. ConstantNode() {
  716. type = CONSTANT;
  717. }
  718. };
  719. struct ContinueNode : public Node {
  720. ContinueNode() {
  721. type = CONTINUE;
  722. }
  723. };
  724. struct DictionaryNode : public ExpressionNode {
  725. struct Pair {
  726. ExpressionNode *key = nullptr;
  727. ExpressionNode *value = nullptr;
  728. };
  729. Vector<Pair> elements;
  730. enum Style {
  731. LUA_TABLE,
  732. PYTHON_DICT,
  733. };
  734. Style style = PYTHON_DICT;
  735. DictionaryNode() {
  736. type = DICTIONARY;
  737. }
  738. };
  739. struct ForNode : public Node {
  740. IdentifierNode *variable = nullptr;
  741. TypeNode *datatype_specifier = nullptr;
  742. bool use_conversion_assign = false;
  743. ExpressionNode *list = nullptr;
  744. SuiteNode *loop = nullptr;
  745. ForNode() {
  746. type = FOR;
  747. }
  748. };
  749. struct FunctionNode : public Node {
  750. IdentifierNode *identifier = nullptr;
  751. Vector<ParameterNode *> parameters;
  752. HashMap<StringName, int> parameters_indices;
  753. ParameterNode *rest_parameter = nullptr;
  754. TypeNode *return_type = nullptr;
  755. SuiteNode *body = nullptr;
  756. bool is_abstract = false;
  757. bool is_static = false; // For lambdas it's determined in the analyzer.
  758. bool is_coroutine = false;
  759. Variant rpc_config;
  760. MethodInfo info;
  761. LambdaNode *source_lambda = nullptr;
  762. Vector<Variant> default_arg_values;
  763. #ifdef TOOLS_ENABLED
  764. MemberDocData doc_data;
  765. int min_local_doc_line = 0;
  766. String signature; // For autocompletion.
  767. #endif // TOOLS_ENABLED
  768. bool resolved_signature = false;
  769. bool resolved_body = false;
  770. _FORCE_INLINE_ bool is_vararg() const { return rest_parameter != nullptr; }
  771. FunctionNode() {
  772. type = FUNCTION;
  773. }
  774. };
  775. struct GetNodeNode : public ExpressionNode {
  776. String full_path;
  777. bool use_dollar = true;
  778. GetNodeNode() {
  779. type = GET_NODE;
  780. }
  781. };
  782. struct IdentifierNode : public ExpressionNode {
  783. StringName name;
  784. SuiteNode *suite = nullptr; // The block in which the identifier is used.
  785. enum Source {
  786. UNDEFINED_SOURCE,
  787. FUNCTION_PARAMETER,
  788. LOCAL_VARIABLE,
  789. LOCAL_CONSTANT,
  790. LOCAL_ITERATOR, // `for` loop iterator.
  791. LOCAL_BIND, // Pattern bind.
  792. MEMBER_VARIABLE,
  793. MEMBER_CONSTANT,
  794. MEMBER_FUNCTION,
  795. MEMBER_SIGNAL,
  796. MEMBER_CLASS,
  797. INHERITED_VARIABLE,
  798. STATIC_VARIABLE,
  799. };
  800. Source source = UNDEFINED_SOURCE;
  801. union {
  802. ParameterNode *parameter_source = nullptr;
  803. IdentifierNode *bind_source;
  804. VariableNode *variable_source;
  805. ConstantNode *constant_source;
  806. SignalNode *signal_source;
  807. FunctionNode *function_source;
  808. };
  809. bool function_source_is_static = false; // For non-GDScript scripts.
  810. FunctionNode *source_function = nullptr; // TODO: Rename to disambiguate `function_source`.
  811. int usages = 0; // Useful for binds/iterator variable.
  812. IdentifierNode() {
  813. type = IDENTIFIER;
  814. }
  815. };
  816. struct IfNode : public Node {
  817. ExpressionNode *condition = nullptr;
  818. SuiteNode *true_block = nullptr;
  819. SuiteNode *false_block = nullptr;
  820. IfNode() {
  821. type = IF;
  822. }
  823. };
  824. struct LambdaNode : public ExpressionNode {
  825. FunctionNode *function = nullptr;
  826. FunctionNode *parent_function = nullptr;
  827. LambdaNode *parent_lambda = nullptr;
  828. Vector<IdentifierNode *> captures;
  829. HashMap<StringName, int> captures_indices;
  830. bool use_self = false;
  831. bool has_name() const {
  832. return function && function->identifier;
  833. }
  834. LambdaNode() {
  835. type = LAMBDA;
  836. }
  837. };
  838. struct LiteralNode : public ExpressionNode {
  839. Variant value;
  840. LiteralNode() {
  841. type = LITERAL;
  842. }
  843. };
  844. struct MatchNode : public Node {
  845. ExpressionNode *test = nullptr;
  846. Vector<MatchBranchNode *> branches;
  847. MatchNode() {
  848. type = MATCH;
  849. }
  850. };
  851. struct MatchBranchNode : public Node {
  852. Vector<PatternNode *> patterns;
  853. SuiteNode *block = nullptr;
  854. bool has_wildcard = false;
  855. SuiteNode *guard_body = nullptr;
  856. MatchBranchNode() {
  857. type = MATCH_BRANCH;
  858. }
  859. };
  860. struct ParameterNode : public AssignableNode {
  861. ParameterNode() {
  862. type = PARAMETER;
  863. }
  864. };
  865. struct PassNode : public Node {
  866. PassNode() {
  867. type = PASS;
  868. }
  869. };
  870. struct PatternNode : public Node {
  871. enum Type {
  872. PT_LITERAL,
  873. PT_EXPRESSION,
  874. PT_BIND,
  875. PT_ARRAY,
  876. PT_DICTIONARY,
  877. PT_REST,
  878. PT_WILDCARD,
  879. };
  880. Type pattern_type = PT_LITERAL;
  881. union {
  882. LiteralNode *literal = nullptr;
  883. IdentifierNode *bind;
  884. ExpressionNode *expression;
  885. };
  886. Vector<PatternNode *> array;
  887. bool rest_used = false; // For array/dict patterns.
  888. struct Pair {
  889. ExpressionNode *key = nullptr;
  890. PatternNode *value_pattern = nullptr;
  891. };
  892. Vector<Pair> dictionary;
  893. HashMap<StringName, IdentifierNode *> binds;
  894. bool has_bind(const StringName &p_name);
  895. IdentifierNode *get_bind(const StringName &p_name);
  896. PatternNode() {
  897. type = PATTERN;
  898. }
  899. };
  900. struct PreloadNode : public ExpressionNode {
  901. ExpressionNode *path = nullptr;
  902. String resolved_path;
  903. Ref<Resource> resource;
  904. PreloadNode() {
  905. type = PRELOAD;
  906. }
  907. };
  908. struct ReturnNode : public Node {
  909. ExpressionNode *return_value = nullptr;
  910. bool void_return = false;
  911. ReturnNode() {
  912. type = RETURN;
  913. }
  914. };
  915. struct SelfNode : public ExpressionNode {
  916. ClassNode *current_class = nullptr;
  917. SelfNode() {
  918. type = SELF;
  919. }
  920. };
  921. struct SignalNode : public Node {
  922. IdentifierNode *identifier = nullptr;
  923. Vector<ParameterNode *> parameters;
  924. HashMap<StringName, int> parameters_indices;
  925. MethodInfo method_info;
  926. #ifdef TOOLS_ENABLED
  927. MemberDocData doc_data;
  928. #endif // TOOLS_ENABLED
  929. int usages = 0;
  930. SignalNode() {
  931. type = SIGNAL;
  932. }
  933. };
  934. struct SubscriptNode : public ExpressionNode {
  935. ExpressionNode *base = nullptr;
  936. union {
  937. ExpressionNode *index = nullptr;
  938. IdentifierNode *attribute;
  939. };
  940. bool is_attribute = false;
  941. SubscriptNode() {
  942. type = SUBSCRIPT;
  943. }
  944. };
  945. struct SuiteNode : public Node {
  946. SuiteNode *parent_block = nullptr;
  947. Vector<Node *> statements;
  948. struct Local {
  949. enum Type {
  950. UNDEFINED,
  951. CONSTANT,
  952. VARIABLE,
  953. PARAMETER,
  954. FOR_VARIABLE,
  955. PATTERN_BIND,
  956. };
  957. Type type = UNDEFINED;
  958. union {
  959. ConstantNode *constant = nullptr;
  960. VariableNode *variable;
  961. ParameterNode *parameter;
  962. IdentifierNode *bind;
  963. };
  964. StringName name;
  965. FunctionNode *source_function = nullptr;
  966. int start_line = 0, end_line = 0;
  967. int start_column = 0, end_column = 0;
  968. DataType get_datatype() const;
  969. String get_name() const;
  970. Local() {}
  971. Local(ConstantNode *p_constant, FunctionNode *p_source_function) {
  972. type = CONSTANT;
  973. constant = p_constant;
  974. name = p_constant->identifier->name;
  975. source_function = p_source_function;
  976. start_line = p_constant->start_line;
  977. end_line = p_constant->end_line;
  978. start_column = p_constant->start_column;
  979. end_column = p_constant->end_column;
  980. }
  981. Local(VariableNode *p_variable, FunctionNode *p_source_function) {
  982. type = VARIABLE;
  983. variable = p_variable;
  984. name = p_variable->identifier->name;
  985. source_function = p_source_function;
  986. start_line = p_variable->start_line;
  987. end_line = p_variable->end_line;
  988. start_column = p_variable->start_column;
  989. end_column = p_variable->end_column;
  990. }
  991. Local(ParameterNode *p_parameter, FunctionNode *p_source_function) {
  992. type = PARAMETER;
  993. parameter = p_parameter;
  994. name = p_parameter->identifier->name;
  995. source_function = p_source_function;
  996. start_line = p_parameter->start_line;
  997. end_line = p_parameter->end_line;
  998. start_column = p_parameter->start_column;
  999. end_column = p_parameter->end_column;
  1000. }
  1001. Local(IdentifierNode *p_identifier, FunctionNode *p_source_function) {
  1002. type = FOR_VARIABLE;
  1003. bind = p_identifier;
  1004. name = p_identifier->name;
  1005. source_function = p_source_function;
  1006. start_line = p_identifier->start_line;
  1007. end_line = p_identifier->end_line;
  1008. start_column = p_identifier->start_column;
  1009. end_column = p_identifier->end_column;
  1010. }
  1011. };
  1012. Local empty;
  1013. Vector<Local> locals;
  1014. HashMap<StringName, int> locals_indices;
  1015. FunctionNode *parent_function = nullptr;
  1016. IfNode *parent_if = nullptr;
  1017. bool has_return = false;
  1018. bool has_continue = false;
  1019. bool has_unreachable_code = false; // Just so warnings aren't given more than once per block.
  1020. bool is_in_loop = false; // The block is nested in a loop (directly or indirectly).
  1021. bool has_local(const StringName &p_name) const;
  1022. const Local &get_local(const StringName &p_name) const;
  1023. template <typename T>
  1024. void add_local(T *p_local, FunctionNode *p_source_function) {
  1025. locals_indices[p_local->identifier->name] = locals.size();
  1026. locals.push_back(Local(p_local, p_source_function));
  1027. }
  1028. void add_local(const Local &p_local) {
  1029. locals_indices[p_local.name] = locals.size();
  1030. locals.push_back(p_local);
  1031. }
  1032. SuiteNode() {
  1033. type = SUITE;
  1034. }
  1035. };
  1036. struct TernaryOpNode : public ExpressionNode {
  1037. // Only one ternary operation exists, so no abstraction here.
  1038. ExpressionNode *condition = nullptr;
  1039. ExpressionNode *true_expr = nullptr;
  1040. ExpressionNode *false_expr = nullptr;
  1041. TernaryOpNode() {
  1042. type = TERNARY_OPERATOR;
  1043. }
  1044. };
  1045. struct TypeNode : public Node {
  1046. Vector<IdentifierNode *> type_chain;
  1047. Vector<TypeNode *> container_types;
  1048. TypeNode *get_container_type_or_null(int p_index) const {
  1049. return p_index >= 0 && p_index < container_types.size() ? container_types[p_index] : nullptr;
  1050. }
  1051. TypeNode() {
  1052. type = TYPE;
  1053. }
  1054. };
  1055. struct TypeTestNode : public ExpressionNode {
  1056. ExpressionNode *operand = nullptr;
  1057. TypeNode *test_type = nullptr;
  1058. DataType test_datatype;
  1059. TypeTestNode() {
  1060. type = TYPE_TEST;
  1061. }
  1062. };
  1063. struct UnaryOpNode : public ExpressionNode {
  1064. enum OpType {
  1065. OP_POSITIVE,
  1066. OP_NEGATIVE,
  1067. OP_COMPLEMENT,
  1068. OP_LOGIC_NOT,
  1069. };
  1070. OpType operation = OP_POSITIVE;
  1071. Variant::Operator variant_op = Variant::OP_MAX;
  1072. ExpressionNode *operand = nullptr;
  1073. UnaryOpNode() {
  1074. type = UNARY_OPERATOR;
  1075. }
  1076. };
  1077. struct VariableNode : public AssignableNode {
  1078. enum PropertyStyle {
  1079. PROP_NONE,
  1080. PROP_INLINE,
  1081. PROP_SETGET,
  1082. };
  1083. PropertyStyle property = PROP_NONE;
  1084. union {
  1085. FunctionNode *setter = nullptr;
  1086. IdentifierNode *setter_pointer;
  1087. };
  1088. IdentifierNode *setter_parameter = nullptr;
  1089. union {
  1090. FunctionNode *getter = nullptr;
  1091. IdentifierNode *getter_pointer;
  1092. };
  1093. bool exported = false;
  1094. bool onready = false;
  1095. PropertyInfo export_info;
  1096. int assignments = 0;
  1097. bool is_static = false;
  1098. #ifdef TOOLS_ENABLED
  1099. MemberDocData doc_data;
  1100. #endif // TOOLS_ENABLED
  1101. VariableNode() {
  1102. type = VARIABLE;
  1103. }
  1104. };
  1105. struct WhileNode : public Node {
  1106. ExpressionNode *condition = nullptr;
  1107. SuiteNode *loop = nullptr;
  1108. WhileNode() {
  1109. type = WHILE;
  1110. }
  1111. };
  1112. enum CompletionType {
  1113. COMPLETION_NONE,
  1114. COMPLETION_ANNOTATION, // Annotation (following @).
  1115. COMPLETION_ANNOTATION_ARGUMENTS, // Annotation arguments hint.
  1116. COMPLETION_ASSIGN, // Assignment based on type (e.g. enum values).
  1117. COMPLETION_ATTRIBUTE, // After id.| to look for members.
  1118. COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods.
  1119. COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD, // Constants inside a built-in type (e.g. Color.BLUE) or static methods (e.g. Color.html).
  1120. COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions).
  1121. // TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func).
  1122. COMPLETION_GET_NODE, // Get node with $ notation.
  1123. COMPLETION_IDENTIFIER, // List available identifiers in scope.
  1124. COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.
  1125. COMPLETION_METHOD, // List available methods in scope.
  1126. COMPLETION_OVERRIDE_METHOD, // Override implementation, also for native virtuals.
  1127. COMPLETION_PROPERTY_DECLARATION, // Property declaration (get, set).
  1128. COMPLETION_PROPERTY_DECLARATION_OR_TYPE, // Property declaration (get, set) or a type hint.
  1129. COMPLETION_PROPERTY_METHOD, // Property setter or getter (list available methods).
  1130. COMPLETION_RESOURCE_PATH, // For load/preload.
  1131. COMPLETION_SUBSCRIPT, // Inside id[|].
  1132. COMPLETION_SUPER_METHOD, // After super.
  1133. COMPLETION_TYPE_ATTRIBUTE, // Attribute in type name (Type.|).
  1134. COMPLETION_TYPE_NAME, // Name of type (after :).
  1135. COMPLETION_TYPE_NAME_OR_VOID, // Same as TYPE_NAME, but allows void (in function return type).
  1136. };
  1137. struct CompletionCall {
  1138. Node *call = nullptr;
  1139. int argument = -1;
  1140. };
  1141. struct CompletionContext {
  1142. CompletionType type = COMPLETION_NONE;
  1143. ClassNode *current_class = nullptr;
  1144. FunctionNode *current_function = nullptr;
  1145. SuiteNode *current_suite = nullptr;
  1146. int current_line = -1;
  1147. int current_argument = -1;
  1148. Variant::Type builtin_type = Variant::VARIANT_MAX;
  1149. Node *node = nullptr;
  1150. Object *base = nullptr;
  1151. GDScriptParser *parser = nullptr;
  1152. CompletionCall call;
  1153. };
  1154. private:
  1155. friend class GDScriptAnalyzer;
  1156. friend class GDScriptParserRef;
  1157. bool _is_tool = false;
  1158. String script_path;
  1159. bool for_completion = false;
  1160. bool parse_body = true;
  1161. bool panic_mode = false;
  1162. bool can_break = false;
  1163. bool can_continue = false;
  1164. List<bool> multiline_stack;
  1165. HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
  1166. ClassNode *head = nullptr;
  1167. Node *list = nullptr;
  1168. List<ParserError> errors;
  1169. #ifdef DEBUG_ENABLED
  1170. struct PendingWarning {
  1171. const Node *source = nullptr;
  1172. GDScriptWarning::Code code = GDScriptWarning::WARNING_MAX;
  1173. bool treated_as_error = false;
  1174. Vector<String> symbols;
  1175. };
  1176. bool is_ignoring_warnings = false;
  1177. List<GDScriptWarning> warnings;
  1178. List<PendingWarning> pending_warnings;
  1179. HashSet<int> warning_ignored_lines[GDScriptWarning::WARNING_MAX];
  1180. int warning_ignore_start_lines[GDScriptWarning::WARNING_MAX];
  1181. HashSet<int> unsafe_lines;
  1182. #endif
  1183. GDScriptTokenizer *tokenizer = nullptr;
  1184. GDScriptTokenizer::Token previous;
  1185. GDScriptTokenizer::Token current;
  1186. ClassNode *current_class = nullptr;
  1187. FunctionNode *current_function = nullptr;
  1188. LambdaNode *current_lambda = nullptr;
  1189. SuiteNode *current_suite = nullptr;
  1190. CompletionContext completion_context;
  1191. List<CompletionCall> completion_call_stack;
  1192. bool in_lambda = false;
  1193. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
  1194. typedef bool (GDScriptParser::*AnnotationAction)(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1195. struct AnnotationInfo {
  1196. enum TargetKind {
  1197. NONE = 0,
  1198. SCRIPT = 1 << 0,
  1199. CLASS = 1 << 1,
  1200. VARIABLE = 1 << 2,
  1201. CONSTANT = 1 << 3,
  1202. SIGNAL = 1 << 4,
  1203. FUNCTION = 1 << 5,
  1204. STATEMENT = 1 << 6,
  1205. STANDALONE = 1 << 7,
  1206. CLASS_LEVEL = CLASS | VARIABLE | CONSTANT | SIGNAL | FUNCTION,
  1207. };
  1208. uint32_t target_kind = 0; // Flags.
  1209. AnnotationAction apply = nullptr;
  1210. MethodInfo info;
  1211. };
  1212. static HashMap<StringName, AnnotationInfo> valid_annotations;
  1213. List<AnnotationNode *> annotation_stack;
  1214. typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
  1215. // Higher value means higher precedence (i.e. is evaluated first).
  1216. enum Precedence {
  1217. PREC_NONE,
  1218. PREC_ASSIGNMENT,
  1219. PREC_CAST,
  1220. PREC_TERNARY,
  1221. PREC_LOGIC_OR,
  1222. PREC_LOGIC_AND,
  1223. PREC_LOGIC_NOT,
  1224. PREC_CONTENT_TEST,
  1225. PREC_COMPARISON,
  1226. PREC_BIT_OR,
  1227. PREC_BIT_XOR,
  1228. PREC_BIT_AND,
  1229. PREC_BIT_SHIFT,
  1230. PREC_ADDITION_SUBTRACTION,
  1231. PREC_FACTOR,
  1232. PREC_SIGN,
  1233. PREC_BIT_NOT,
  1234. PREC_POWER,
  1235. PREC_TYPE_TEST,
  1236. PREC_AWAIT,
  1237. PREC_CALL,
  1238. PREC_ATTRIBUTE,
  1239. PREC_SUBSCRIPT,
  1240. PREC_PRIMARY,
  1241. };
  1242. struct ParseRule {
  1243. ParseFunction prefix = nullptr;
  1244. ParseFunction infix = nullptr;
  1245. Precedence precedence = PREC_NONE;
  1246. };
  1247. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1248. List<Node *> nodes_in_progress;
  1249. void complete_extents(Node *p_node);
  1250. void update_extents(Node *p_node);
  1251. void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
  1252. void reset_extents(Node *p_node, Node *p_from);
  1253. template <typename T>
  1254. T *alloc_node() {
  1255. T *node = memnew(T);
  1256. node->next = list;
  1257. list = node;
  1258. reset_extents(node, previous);
  1259. nodes_in_progress.push_back(node);
  1260. return node;
  1261. }
  1262. // Allocates a node for patching up the parse tree when an error occurred.
  1263. // Such nodes don't track their extents as they don't relate to actual tokens.
  1264. template <typename T>
  1265. T *alloc_recovery_node() {
  1266. T *node = memnew(T);
  1267. node->next = list;
  1268. list = node;
  1269. return node;
  1270. }
  1271. SuiteNode *alloc_recovery_suite() {
  1272. SuiteNode *suite = alloc_recovery_node<SuiteNode>();
  1273. suite->parent_block = current_suite;
  1274. suite->parent_function = current_function;
  1275. suite->is_in_loop = current_suite->is_in_loop;
  1276. return suite;
  1277. }
  1278. void clear();
  1279. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1280. #ifdef DEBUG_ENABLED
  1281. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1282. template <typename... Symbols>
  1283. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
  1284. push_warning(p_source, p_code, Vector<String>{ p_symbols... });
  1285. }
  1286. void apply_pending_warnings();
  1287. #endif
  1288. // Setting p_force to false will prevent the completion context from being update if a context was already set before.
  1289. // This should only be done when we push context before we consumed any tokens for the corresponding structure.
  1290. // See parse_precedence for an example.
  1291. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = true);
  1292. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = true);
  1293. // In some cases it might become necessary to alter the completion context after parsing a subexpression.
  1294. // For example to not override COMPLETE_CALL_ARGUMENTS with COMPLETION_NONE from string literals.
  1295. void override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument = -1);
  1296. void push_completion_call(Node *p_call);
  1297. void pop_completion_call();
  1298. void set_last_completion_call_arg(int p_argument);
  1299. GDScriptTokenizer::Token advance();
  1300. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1301. bool check(GDScriptTokenizer::Token::Type p_token_type) const;
  1302. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1303. bool is_at_end() const;
  1304. bool is_statement_end_token() const;
  1305. bool is_statement_end() const;
  1306. void end_statement(const String &p_context);
  1307. void synchronize();
  1308. void push_multiline(bool p_state);
  1309. void pop_multiline();
  1310. // Main blocks.
  1311. void parse_program();
  1312. ClassNode *parse_class(bool p_is_abstract, bool p_is_static);
  1313. void parse_class_name();
  1314. void parse_extends();
  1315. void parse_class_body(bool p_first_is_abstract, bool p_is_multiline);
  1316. template <typename T>
  1317. void parse_class_member(T *(GDScriptParser::*p_parse_function)(bool, bool), AnnotationInfo::TargetKind p_target, const String &p_member_kind, bool p_is_abstract = false, bool p_is_static = false);
  1318. SignalNode *parse_signal(bool p_is_abstract, bool p_is_static);
  1319. EnumNode *parse_enum(bool p_is_abstract, bool p_is_static);
  1320. ParameterNode *parse_parameter();
  1321. FunctionNode *parse_function(bool p_is_abstract, bool p_is_static);
  1322. void parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start);
  1323. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
  1324. // Annotations
  1325. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1326. static bool register_annotation(const MethodInfo &p_info, uint32_t p_target_kinds, AnnotationAction p_apply, const Vector<Variant> &p_default_arguments = Vector<Variant>(), bool p_is_vararg = false);
  1327. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1328. void clear_unused_annotations();
  1329. bool tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1330. bool icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1331. bool static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1332. bool onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1333. template <PropertyHint t_hint, Variant::Type t_type>
  1334. bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1335. bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1336. bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1337. bool export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1338. template <PropertyUsageFlags t_usage>
  1339. bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1340. bool warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1341. bool warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1342. bool rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1343. // Statements.
  1344. Node *parse_statement();
  1345. VariableNode *parse_variable(bool p_is_abstract, bool p_is_static);
  1346. VariableNode *parse_variable(bool p_is_abstract, bool p_is_static, bool p_allow_property);
  1347. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1348. void parse_property_getter(VariableNode *p_variable);
  1349. void parse_property_setter(VariableNode *p_variable);
  1350. ConstantNode *parse_constant(bool p_is_abstract, bool p_is_static);
  1351. AssertNode *parse_assert();
  1352. BreakNode *parse_break();
  1353. ContinueNode *parse_continue();
  1354. ForNode *parse_for();
  1355. IfNode *parse_if(const String &p_token = "if");
  1356. MatchNode *parse_match();
  1357. MatchBranchNode *parse_match_branch();
  1358. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1359. WhileNode *parse_while();
  1360. // Expressions.
  1361. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1362. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1363. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1364. LiteralNode *parse_literal();
  1365. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1366. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1367. IdentifierNode *parse_identifier();
  1368. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1369. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1370. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1371. ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1372. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1373. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1374. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1375. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1376. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1377. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1378. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1379. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1380. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1381. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1382. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1383. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1384. ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
  1385. ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
  1386. ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
  1387. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1388. TypeNode *parse_type(bool p_allow_void = false);
  1389. #ifdef TOOLS_ENABLED
  1390. int max_script_doc_line = INT_MAX;
  1391. int min_member_doc_line = 1;
  1392. bool has_comment(int p_line, bool p_must_be_doc = false);
  1393. MemberDocData parse_doc_comment(int p_line, bool p_single_line = false);
  1394. ClassDocData parse_class_doc_comment(int p_line, bool p_single_line = false);
  1395. #endif // TOOLS_ENABLED
  1396. public:
  1397. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body = true);
  1398. Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path);
  1399. ClassNode *get_tree() const { return head; }
  1400. bool is_tool() const { return _is_tool; }
  1401. Ref<GDScriptParserRef> get_depended_parser_for(const String &p_path);
  1402. const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
  1403. ClassNode *find_class(const String &p_qualified_name) const;
  1404. bool has_class(const GDScriptParser::ClassNode *p_class) const;
  1405. static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.
  1406. CompletionContext get_completion_context() const { return completion_context; }
  1407. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1408. bool annotation_exists(const String &p_annotation_name) const;
  1409. const List<ParserError> &get_errors() const { return errors; }
  1410. const List<String> get_dependencies() const {
  1411. // TODO: Keep track of deps.
  1412. return List<String>();
  1413. }
  1414. #ifdef DEBUG_ENABLED
  1415. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1416. const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
  1417. int get_last_line_number() const { return current.end_line; }
  1418. #endif
  1419. #ifdef TOOLS_ENABLED
  1420. static HashMap<String, String> theme_color_names;
  1421. HashMap<int, GDScriptTokenizer::CommentData> comment_data;
  1422. #endif // TOOLS_ENABLED
  1423. GDScriptParser();
  1424. ~GDScriptParser();
  1425. #ifdef DEBUG_ENABLED
  1426. class TreePrinter {
  1427. int indent_level = 0;
  1428. String indent;
  1429. StringBuilder printed;
  1430. bool pending_indent = false;
  1431. void increase_indent();
  1432. void decrease_indent();
  1433. void push_line(const String &p_line = String());
  1434. void push_text(const String &p_text);
  1435. void print_annotation(const AnnotationNode *p_annotation);
  1436. void print_array(ArrayNode *p_array);
  1437. void print_assert(AssertNode *p_assert);
  1438. void print_assignment(AssignmentNode *p_assignment);
  1439. void print_await(AwaitNode *p_await);
  1440. void print_binary_op(BinaryOpNode *p_binary_op);
  1441. void print_call(CallNode *p_call);
  1442. void print_cast(CastNode *p_cast);
  1443. void print_class(ClassNode *p_class);
  1444. void print_constant(ConstantNode *p_constant);
  1445. void print_dictionary(DictionaryNode *p_dictionary);
  1446. void print_expression(ExpressionNode *p_expression);
  1447. void print_enum(EnumNode *p_enum);
  1448. void print_for(ForNode *p_for);
  1449. void print_function(FunctionNode *p_function, const String &p_context = "Function");
  1450. void print_get_node(GetNodeNode *p_get_node);
  1451. void print_if(IfNode *p_if, bool p_is_elif = false);
  1452. void print_identifier(IdentifierNode *p_identifier);
  1453. void print_lambda(LambdaNode *p_lambda);
  1454. void print_literal(LiteralNode *p_literal);
  1455. void print_match(MatchNode *p_match);
  1456. void print_match_branch(MatchBranchNode *p_match_branch);
  1457. void print_match_pattern(PatternNode *p_match_pattern);
  1458. void print_parameter(ParameterNode *p_parameter);
  1459. void print_preload(PreloadNode *p_preload);
  1460. void print_return(ReturnNode *p_return);
  1461. void print_self(SelfNode *p_self);
  1462. void print_signal(SignalNode *p_signal);
  1463. void print_statement(Node *p_statement);
  1464. void print_subscript(SubscriptNode *p_subscript);
  1465. void print_suite(SuiteNode *p_suite);
  1466. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1467. void print_type(TypeNode *p_type);
  1468. void print_type_test(TypeTestNode *p_type_test);
  1469. void print_unary_op(UnaryOpNode *p_unary_op);
  1470. void print_variable(VariableNode *p_variable);
  1471. void print_while(WhileNode *p_while);
  1472. public:
  1473. void print_tree(const GDScriptParser &p_parser);
  1474. };
  1475. #endif // DEBUG_ENABLED
  1476. static void cleanup();
  1477. };