gdscript_parser.h 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679
  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. TypeNode *return_type = nullptr;
  754. SuiteNode *body = nullptr;
  755. bool is_abstract = false;
  756. bool is_static = false; // For lambdas it's determined in the analyzer.
  757. bool is_coroutine = false;
  758. Variant rpc_config;
  759. MethodInfo info;
  760. LambdaNode *source_lambda = nullptr;
  761. Vector<Variant> default_arg_values;
  762. #ifdef TOOLS_ENABLED
  763. MemberDocData doc_data;
  764. int min_local_doc_line = 0;
  765. String signature; // For autocompletion.
  766. #endif // TOOLS_ENABLED
  767. bool resolved_signature = false;
  768. bool resolved_body = false;
  769. FunctionNode() {
  770. type = FUNCTION;
  771. }
  772. };
  773. struct GetNodeNode : public ExpressionNode {
  774. String full_path;
  775. bool use_dollar = true;
  776. GetNodeNode() {
  777. type = GET_NODE;
  778. }
  779. };
  780. struct IdentifierNode : public ExpressionNode {
  781. StringName name;
  782. SuiteNode *suite = nullptr; // The block in which the identifier is used.
  783. enum Source {
  784. UNDEFINED_SOURCE,
  785. FUNCTION_PARAMETER,
  786. LOCAL_VARIABLE,
  787. LOCAL_CONSTANT,
  788. LOCAL_ITERATOR, // `for` loop iterator.
  789. LOCAL_BIND, // Pattern bind.
  790. MEMBER_VARIABLE,
  791. MEMBER_CONSTANT,
  792. MEMBER_FUNCTION,
  793. MEMBER_SIGNAL,
  794. MEMBER_CLASS,
  795. INHERITED_VARIABLE,
  796. STATIC_VARIABLE,
  797. };
  798. Source source = UNDEFINED_SOURCE;
  799. union {
  800. ParameterNode *parameter_source = nullptr;
  801. IdentifierNode *bind_source;
  802. VariableNode *variable_source;
  803. ConstantNode *constant_source;
  804. SignalNode *signal_source;
  805. FunctionNode *function_source;
  806. };
  807. bool function_source_is_static = false; // For non-GDScript scripts.
  808. FunctionNode *source_function = nullptr; // TODO: Rename to disambiguate `function_source`.
  809. int usages = 0; // Useful for binds/iterator variable.
  810. IdentifierNode() {
  811. type = IDENTIFIER;
  812. }
  813. };
  814. struct IfNode : public Node {
  815. ExpressionNode *condition = nullptr;
  816. SuiteNode *true_block = nullptr;
  817. SuiteNode *false_block = nullptr;
  818. IfNode() {
  819. type = IF;
  820. }
  821. };
  822. struct LambdaNode : public ExpressionNode {
  823. FunctionNode *function = nullptr;
  824. FunctionNode *parent_function = nullptr;
  825. LambdaNode *parent_lambda = nullptr;
  826. Vector<IdentifierNode *> captures;
  827. HashMap<StringName, int> captures_indices;
  828. bool use_self = false;
  829. bool has_name() const {
  830. return function && function->identifier;
  831. }
  832. LambdaNode() {
  833. type = LAMBDA;
  834. }
  835. };
  836. struct LiteralNode : public ExpressionNode {
  837. Variant value;
  838. LiteralNode() {
  839. type = LITERAL;
  840. }
  841. };
  842. struct MatchNode : public Node {
  843. ExpressionNode *test = nullptr;
  844. Vector<MatchBranchNode *> branches;
  845. MatchNode() {
  846. type = MATCH;
  847. }
  848. };
  849. struct MatchBranchNode : public Node {
  850. Vector<PatternNode *> patterns;
  851. SuiteNode *block = nullptr;
  852. bool has_wildcard = false;
  853. SuiteNode *guard_body = nullptr;
  854. MatchBranchNode() {
  855. type = MATCH_BRANCH;
  856. }
  857. };
  858. struct ParameterNode : public AssignableNode {
  859. ParameterNode() {
  860. type = PARAMETER;
  861. }
  862. };
  863. struct PassNode : public Node {
  864. PassNode() {
  865. type = PASS;
  866. }
  867. };
  868. struct PatternNode : public Node {
  869. enum Type {
  870. PT_LITERAL,
  871. PT_EXPRESSION,
  872. PT_BIND,
  873. PT_ARRAY,
  874. PT_DICTIONARY,
  875. PT_REST,
  876. PT_WILDCARD,
  877. };
  878. Type pattern_type = PT_LITERAL;
  879. union {
  880. LiteralNode *literal = nullptr;
  881. IdentifierNode *bind;
  882. ExpressionNode *expression;
  883. };
  884. Vector<PatternNode *> array;
  885. bool rest_used = false; // For array/dict patterns.
  886. struct Pair {
  887. ExpressionNode *key = nullptr;
  888. PatternNode *value_pattern = nullptr;
  889. };
  890. Vector<Pair> dictionary;
  891. HashMap<StringName, IdentifierNode *> binds;
  892. bool has_bind(const StringName &p_name);
  893. IdentifierNode *get_bind(const StringName &p_name);
  894. PatternNode() {
  895. type = PATTERN;
  896. }
  897. };
  898. struct PreloadNode : public ExpressionNode {
  899. ExpressionNode *path = nullptr;
  900. String resolved_path;
  901. Ref<Resource> resource;
  902. PreloadNode() {
  903. type = PRELOAD;
  904. }
  905. };
  906. struct ReturnNode : public Node {
  907. ExpressionNode *return_value = nullptr;
  908. bool void_return = false;
  909. ReturnNode() {
  910. type = RETURN;
  911. }
  912. };
  913. struct SelfNode : public ExpressionNode {
  914. ClassNode *current_class = nullptr;
  915. SelfNode() {
  916. type = SELF;
  917. }
  918. };
  919. struct SignalNode : public Node {
  920. IdentifierNode *identifier = nullptr;
  921. Vector<ParameterNode *> parameters;
  922. HashMap<StringName, int> parameters_indices;
  923. MethodInfo method_info;
  924. #ifdef TOOLS_ENABLED
  925. MemberDocData doc_data;
  926. #endif // TOOLS_ENABLED
  927. int usages = 0;
  928. SignalNode() {
  929. type = SIGNAL;
  930. }
  931. };
  932. struct SubscriptNode : public ExpressionNode {
  933. ExpressionNode *base = nullptr;
  934. union {
  935. ExpressionNode *index = nullptr;
  936. IdentifierNode *attribute;
  937. };
  938. bool is_attribute = false;
  939. SubscriptNode() {
  940. type = SUBSCRIPT;
  941. }
  942. };
  943. struct SuiteNode : public Node {
  944. SuiteNode *parent_block = nullptr;
  945. Vector<Node *> statements;
  946. struct Local {
  947. enum Type {
  948. UNDEFINED,
  949. CONSTANT,
  950. VARIABLE,
  951. PARAMETER,
  952. FOR_VARIABLE,
  953. PATTERN_BIND,
  954. };
  955. Type type = UNDEFINED;
  956. union {
  957. ConstantNode *constant = nullptr;
  958. VariableNode *variable;
  959. ParameterNode *parameter;
  960. IdentifierNode *bind;
  961. };
  962. StringName name;
  963. FunctionNode *source_function = nullptr;
  964. int start_line = 0, end_line = 0;
  965. int start_column = 0, end_column = 0;
  966. DataType get_datatype() const;
  967. String get_name() const;
  968. Local() {}
  969. Local(ConstantNode *p_constant, FunctionNode *p_source_function) {
  970. type = CONSTANT;
  971. constant = p_constant;
  972. name = p_constant->identifier->name;
  973. source_function = p_source_function;
  974. start_line = p_constant->start_line;
  975. end_line = p_constant->end_line;
  976. start_column = p_constant->start_column;
  977. end_column = p_constant->end_column;
  978. }
  979. Local(VariableNode *p_variable, FunctionNode *p_source_function) {
  980. type = VARIABLE;
  981. variable = p_variable;
  982. name = p_variable->identifier->name;
  983. source_function = p_source_function;
  984. start_line = p_variable->start_line;
  985. end_line = p_variable->end_line;
  986. start_column = p_variable->start_column;
  987. end_column = p_variable->end_column;
  988. }
  989. Local(ParameterNode *p_parameter, FunctionNode *p_source_function) {
  990. type = PARAMETER;
  991. parameter = p_parameter;
  992. name = p_parameter->identifier->name;
  993. source_function = p_source_function;
  994. start_line = p_parameter->start_line;
  995. end_line = p_parameter->end_line;
  996. start_column = p_parameter->start_column;
  997. end_column = p_parameter->end_column;
  998. }
  999. Local(IdentifierNode *p_identifier, FunctionNode *p_source_function) {
  1000. type = FOR_VARIABLE;
  1001. bind = p_identifier;
  1002. name = p_identifier->name;
  1003. source_function = p_source_function;
  1004. start_line = p_identifier->start_line;
  1005. end_line = p_identifier->end_line;
  1006. start_column = p_identifier->start_column;
  1007. end_column = p_identifier->end_column;
  1008. }
  1009. };
  1010. Local empty;
  1011. Vector<Local> locals;
  1012. HashMap<StringName, int> locals_indices;
  1013. FunctionNode *parent_function = nullptr;
  1014. IfNode *parent_if = nullptr;
  1015. bool has_return = false;
  1016. bool has_continue = false;
  1017. bool has_unreachable_code = false; // Just so warnings aren't given more than once per block.
  1018. bool is_in_loop = false; // The block is nested in a loop (directly or indirectly).
  1019. bool has_local(const StringName &p_name) const;
  1020. const Local &get_local(const StringName &p_name) const;
  1021. template <typename T>
  1022. void add_local(T *p_local, FunctionNode *p_source_function) {
  1023. locals_indices[p_local->identifier->name] = locals.size();
  1024. locals.push_back(Local(p_local, p_source_function));
  1025. }
  1026. void add_local(const Local &p_local) {
  1027. locals_indices[p_local.name] = locals.size();
  1028. locals.push_back(p_local);
  1029. }
  1030. SuiteNode() {
  1031. type = SUITE;
  1032. }
  1033. };
  1034. struct TernaryOpNode : public ExpressionNode {
  1035. // Only one ternary operation exists, so no abstraction here.
  1036. ExpressionNode *condition = nullptr;
  1037. ExpressionNode *true_expr = nullptr;
  1038. ExpressionNode *false_expr = nullptr;
  1039. TernaryOpNode() {
  1040. type = TERNARY_OPERATOR;
  1041. }
  1042. };
  1043. struct TypeNode : public Node {
  1044. Vector<IdentifierNode *> type_chain;
  1045. Vector<TypeNode *> container_types;
  1046. TypeNode *get_container_type_or_null(int p_index) const {
  1047. return p_index >= 0 && p_index < container_types.size() ? container_types[p_index] : nullptr;
  1048. }
  1049. TypeNode() {
  1050. type = TYPE;
  1051. }
  1052. };
  1053. struct TypeTestNode : public ExpressionNode {
  1054. ExpressionNode *operand = nullptr;
  1055. TypeNode *test_type = nullptr;
  1056. DataType test_datatype;
  1057. TypeTestNode() {
  1058. type = TYPE_TEST;
  1059. }
  1060. };
  1061. struct UnaryOpNode : public ExpressionNode {
  1062. enum OpType {
  1063. OP_POSITIVE,
  1064. OP_NEGATIVE,
  1065. OP_COMPLEMENT,
  1066. OP_LOGIC_NOT,
  1067. };
  1068. OpType operation = OP_POSITIVE;
  1069. Variant::Operator variant_op = Variant::OP_MAX;
  1070. ExpressionNode *operand = nullptr;
  1071. UnaryOpNode() {
  1072. type = UNARY_OPERATOR;
  1073. }
  1074. };
  1075. struct VariableNode : public AssignableNode {
  1076. enum PropertyStyle {
  1077. PROP_NONE,
  1078. PROP_INLINE,
  1079. PROP_SETGET,
  1080. };
  1081. PropertyStyle property = PROP_NONE;
  1082. union {
  1083. FunctionNode *setter = nullptr;
  1084. IdentifierNode *setter_pointer;
  1085. };
  1086. IdentifierNode *setter_parameter = nullptr;
  1087. union {
  1088. FunctionNode *getter = nullptr;
  1089. IdentifierNode *getter_pointer;
  1090. };
  1091. bool exported = false;
  1092. bool onready = false;
  1093. PropertyInfo export_info;
  1094. int assignments = 0;
  1095. bool is_static = false;
  1096. #ifdef TOOLS_ENABLED
  1097. MemberDocData doc_data;
  1098. #endif // TOOLS_ENABLED
  1099. VariableNode() {
  1100. type = VARIABLE;
  1101. }
  1102. };
  1103. struct WhileNode : public Node {
  1104. ExpressionNode *condition = nullptr;
  1105. SuiteNode *loop = nullptr;
  1106. WhileNode() {
  1107. type = WHILE;
  1108. }
  1109. };
  1110. enum CompletionType {
  1111. COMPLETION_NONE,
  1112. COMPLETION_ANNOTATION, // Annotation (following @).
  1113. COMPLETION_ANNOTATION_ARGUMENTS, // Annotation arguments hint.
  1114. COMPLETION_ASSIGN, // Assignment based on type (e.g. enum values).
  1115. COMPLETION_ATTRIBUTE, // After id.| to look for members.
  1116. COMPLETION_ATTRIBUTE_METHOD, // After id.| to look for methods.
  1117. 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).
  1118. COMPLETION_CALL_ARGUMENTS, // Complete with nodes, input actions, enum values (or usual expressions).
  1119. // TODO: COMPLETION_DECLARATION, // Potential declaration (var, const, func).
  1120. COMPLETION_GET_NODE, // Get node with $ notation.
  1121. COMPLETION_IDENTIFIER, // List available identifiers in scope.
  1122. COMPLETION_INHERIT_TYPE, // Type after extends. Exclude non-viable types (built-ins, enums, void). Includes subtypes using the argument index.
  1123. COMPLETION_METHOD, // List available methods in scope.
  1124. COMPLETION_OVERRIDE_METHOD, // Override implementation, also for native virtuals.
  1125. COMPLETION_PROPERTY_DECLARATION, // Property declaration (get, set).
  1126. COMPLETION_PROPERTY_DECLARATION_OR_TYPE, // Property declaration (get, set) or a type hint.
  1127. COMPLETION_PROPERTY_METHOD, // Property setter or getter (list available methods).
  1128. COMPLETION_RESOURCE_PATH, // For load/preload.
  1129. COMPLETION_SUBSCRIPT, // Inside id[|].
  1130. COMPLETION_SUPER_METHOD, // After super.
  1131. COMPLETION_TYPE_ATTRIBUTE, // Attribute in type name (Type.|).
  1132. COMPLETION_TYPE_NAME, // Name of type (after :).
  1133. COMPLETION_TYPE_NAME_OR_VOID, // Same as TYPE_NAME, but allows void (in function return type).
  1134. };
  1135. struct CompletionCall {
  1136. Node *call = nullptr;
  1137. int argument = -1;
  1138. };
  1139. struct CompletionContext {
  1140. CompletionType type = COMPLETION_NONE;
  1141. ClassNode *current_class = nullptr;
  1142. FunctionNode *current_function = nullptr;
  1143. SuiteNode *current_suite = nullptr;
  1144. int current_line = -1;
  1145. int current_argument = -1;
  1146. Variant::Type builtin_type = Variant::VARIANT_MAX;
  1147. Node *node = nullptr;
  1148. Object *base = nullptr;
  1149. GDScriptParser *parser = nullptr;
  1150. CompletionCall call;
  1151. };
  1152. private:
  1153. friend class GDScriptAnalyzer;
  1154. friend class GDScriptParserRef;
  1155. bool _is_tool = false;
  1156. String script_path;
  1157. bool for_completion = false;
  1158. bool parse_body = true;
  1159. bool panic_mode = false;
  1160. bool can_break = false;
  1161. bool can_continue = false;
  1162. List<bool> multiline_stack;
  1163. HashMap<String, Ref<GDScriptParserRef>> depended_parsers;
  1164. ClassNode *head = nullptr;
  1165. Node *list = nullptr;
  1166. List<ParserError> errors;
  1167. #ifdef DEBUG_ENABLED
  1168. struct PendingWarning {
  1169. const Node *source = nullptr;
  1170. GDScriptWarning::Code code = GDScriptWarning::WARNING_MAX;
  1171. bool treated_as_error = false;
  1172. Vector<String> symbols;
  1173. };
  1174. bool is_ignoring_warnings = false;
  1175. List<GDScriptWarning> warnings;
  1176. List<PendingWarning> pending_warnings;
  1177. HashSet<int> warning_ignored_lines[GDScriptWarning::WARNING_MAX];
  1178. int warning_ignore_start_lines[GDScriptWarning::WARNING_MAX];
  1179. HashSet<int> unsafe_lines;
  1180. #endif
  1181. GDScriptTokenizer *tokenizer = nullptr;
  1182. GDScriptTokenizer::Token previous;
  1183. GDScriptTokenizer::Token current;
  1184. ClassNode *current_class = nullptr;
  1185. FunctionNode *current_function = nullptr;
  1186. LambdaNode *current_lambda = nullptr;
  1187. SuiteNode *current_suite = nullptr;
  1188. CompletionContext completion_context;
  1189. List<CompletionCall> completion_call_stack;
  1190. bool in_lambda = false;
  1191. bool lambda_ended = false; // Marker for when a lambda ends, to apply an end of statement if needed.
  1192. typedef bool (GDScriptParser::*AnnotationAction)(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1193. struct AnnotationInfo {
  1194. enum TargetKind {
  1195. NONE = 0,
  1196. SCRIPT = 1 << 0,
  1197. CLASS = 1 << 1,
  1198. VARIABLE = 1 << 2,
  1199. CONSTANT = 1 << 3,
  1200. SIGNAL = 1 << 4,
  1201. FUNCTION = 1 << 5,
  1202. STATEMENT = 1 << 6,
  1203. STANDALONE = 1 << 7,
  1204. CLASS_LEVEL = CLASS | VARIABLE | CONSTANT | SIGNAL | FUNCTION,
  1205. };
  1206. uint32_t target_kind = 0; // Flags.
  1207. AnnotationAction apply = nullptr;
  1208. MethodInfo info;
  1209. };
  1210. static HashMap<StringName, AnnotationInfo> valid_annotations;
  1211. List<AnnotationNode *> annotation_stack;
  1212. typedef ExpressionNode *(GDScriptParser::*ParseFunction)(ExpressionNode *p_previous_operand, bool p_can_assign);
  1213. // Higher value means higher precedence (i.e. is evaluated first).
  1214. enum Precedence {
  1215. PREC_NONE,
  1216. PREC_ASSIGNMENT,
  1217. PREC_CAST,
  1218. PREC_TERNARY,
  1219. PREC_LOGIC_OR,
  1220. PREC_LOGIC_AND,
  1221. PREC_LOGIC_NOT,
  1222. PREC_CONTENT_TEST,
  1223. PREC_COMPARISON,
  1224. PREC_BIT_OR,
  1225. PREC_BIT_XOR,
  1226. PREC_BIT_AND,
  1227. PREC_BIT_SHIFT,
  1228. PREC_ADDITION_SUBTRACTION,
  1229. PREC_FACTOR,
  1230. PREC_SIGN,
  1231. PREC_BIT_NOT,
  1232. PREC_POWER,
  1233. PREC_TYPE_TEST,
  1234. PREC_AWAIT,
  1235. PREC_CALL,
  1236. PREC_ATTRIBUTE,
  1237. PREC_SUBSCRIPT,
  1238. PREC_PRIMARY,
  1239. };
  1240. struct ParseRule {
  1241. ParseFunction prefix = nullptr;
  1242. ParseFunction infix = nullptr;
  1243. Precedence precedence = PREC_NONE;
  1244. };
  1245. static ParseRule *get_rule(GDScriptTokenizer::Token::Type p_token_type);
  1246. List<Node *> nodes_in_progress;
  1247. void complete_extents(Node *p_node);
  1248. void update_extents(Node *p_node);
  1249. void reset_extents(Node *p_node, GDScriptTokenizer::Token p_token);
  1250. void reset_extents(Node *p_node, Node *p_from);
  1251. template <typename T>
  1252. T *alloc_node() {
  1253. T *node = memnew(T);
  1254. node->next = list;
  1255. list = node;
  1256. reset_extents(node, previous);
  1257. nodes_in_progress.push_back(node);
  1258. return node;
  1259. }
  1260. // Allocates a node for patching up the parse tree when an error occurred.
  1261. // Such nodes don't track their extents as they don't relate to actual tokens.
  1262. template <typename T>
  1263. T *alloc_recovery_node() {
  1264. T *node = memnew(T);
  1265. node->next = list;
  1266. list = node;
  1267. return node;
  1268. }
  1269. SuiteNode *alloc_recovery_suite() {
  1270. SuiteNode *suite = alloc_recovery_node<SuiteNode>();
  1271. suite->parent_block = current_suite;
  1272. suite->parent_function = current_function;
  1273. suite->is_in_loop = current_suite->is_in_loop;
  1274. return suite;
  1275. }
  1276. void clear();
  1277. void push_error(const String &p_message, const Node *p_origin = nullptr);
  1278. #ifdef DEBUG_ENABLED
  1279. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Vector<String> &p_symbols);
  1280. template <typename... Symbols>
  1281. void push_warning(const Node *p_source, GDScriptWarning::Code p_code, const Symbols &...p_symbols) {
  1282. push_warning(p_source, p_code, Vector<String>{ p_symbols... });
  1283. }
  1284. void apply_pending_warnings();
  1285. #endif
  1286. // Setting p_force to false will prevent the completion context from being update if a context was already set before.
  1287. // This should only be done when we push context before we consumed any tokens for the corresponding structure.
  1288. // See parse_precedence for an example.
  1289. void make_completion_context(CompletionType p_type, Node *p_node, int p_argument = -1, bool p_force = true);
  1290. void make_completion_context(CompletionType p_type, Variant::Type p_builtin_type, bool p_force = true);
  1291. // In some cases it might become necessary to alter the completion context after parsing a subexpression.
  1292. // For example to not override COMPLETE_CALL_ARGUMENTS with COMPLETION_NONE from string literals.
  1293. void override_completion_context(const Node *p_for_node, CompletionType p_type, Node *p_node, int p_argument = -1);
  1294. void push_completion_call(Node *p_call);
  1295. void pop_completion_call();
  1296. void set_last_completion_call_arg(int p_argument);
  1297. GDScriptTokenizer::Token advance();
  1298. bool match(GDScriptTokenizer::Token::Type p_token_type);
  1299. bool check(GDScriptTokenizer::Token::Type p_token_type) const;
  1300. bool consume(GDScriptTokenizer::Token::Type p_token_type, const String &p_error_message);
  1301. bool is_at_end() const;
  1302. bool is_statement_end_token() const;
  1303. bool is_statement_end() const;
  1304. void end_statement(const String &p_context);
  1305. void synchronize();
  1306. void push_multiline(bool p_state);
  1307. void pop_multiline();
  1308. // Main blocks.
  1309. void parse_program();
  1310. ClassNode *parse_class(bool p_is_abstract, bool p_is_static);
  1311. void parse_class_name();
  1312. void parse_extends();
  1313. void parse_class_body(bool p_first_is_abstract, bool p_is_multiline);
  1314. template <typename T>
  1315. 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);
  1316. SignalNode *parse_signal(bool p_is_abstract, bool p_is_static);
  1317. EnumNode *parse_enum(bool p_is_abstract, bool p_is_static);
  1318. ParameterNode *parse_parameter();
  1319. FunctionNode *parse_function(bool p_is_abstract, bool p_is_static);
  1320. void parse_function_signature(FunctionNode *p_function, SuiteNode *p_body, const String &p_type, int p_signature_start);
  1321. SuiteNode *parse_suite(const String &p_context, SuiteNode *p_suite = nullptr, bool p_for_lambda = false);
  1322. // Annotations
  1323. AnnotationNode *parse_annotation(uint32_t p_valid_targets);
  1324. 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);
  1325. bool validate_annotation_arguments(AnnotationNode *p_annotation);
  1326. void clear_unused_annotations();
  1327. bool tool_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1328. bool icon_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1329. bool static_unload_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1330. bool onready_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1331. template <PropertyHint t_hint, Variant::Type t_type>
  1332. bool export_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1333. bool export_storage_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1334. bool export_custom_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1335. bool export_tool_button_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1336. template <PropertyUsageFlags t_usage>
  1337. bool export_group_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1338. bool warning_ignore_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1339. bool warning_ignore_region_annotations(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1340. bool rpc_annotation(AnnotationNode *p_annotation, Node *p_target, ClassNode *p_class);
  1341. // Statements.
  1342. Node *parse_statement();
  1343. VariableNode *parse_variable(bool p_is_abstract, bool p_is_static);
  1344. VariableNode *parse_variable(bool p_is_abstract, bool p_is_static, bool p_allow_property);
  1345. VariableNode *parse_property(VariableNode *p_variable, bool p_need_indent);
  1346. void parse_property_getter(VariableNode *p_variable);
  1347. void parse_property_setter(VariableNode *p_variable);
  1348. ConstantNode *parse_constant(bool p_is_abstract, bool p_is_static);
  1349. AssertNode *parse_assert();
  1350. BreakNode *parse_break();
  1351. ContinueNode *parse_continue();
  1352. ForNode *parse_for();
  1353. IfNode *parse_if(const String &p_token = "if");
  1354. MatchNode *parse_match();
  1355. MatchBranchNode *parse_match_branch();
  1356. PatternNode *parse_match_pattern(PatternNode *p_root_pattern = nullptr);
  1357. WhileNode *parse_while();
  1358. // Expressions.
  1359. ExpressionNode *parse_expression(bool p_can_assign, bool p_stop_on_assign = false);
  1360. ExpressionNode *parse_precedence(Precedence p_precedence, bool p_can_assign, bool p_stop_on_assign = false);
  1361. ExpressionNode *parse_literal(ExpressionNode *p_previous_operand, bool p_can_assign);
  1362. LiteralNode *parse_literal();
  1363. ExpressionNode *parse_self(ExpressionNode *p_previous_operand, bool p_can_assign);
  1364. ExpressionNode *parse_identifier(ExpressionNode *p_previous_operand, bool p_can_assign);
  1365. IdentifierNode *parse_identifier();
  1366. ExpressionNode *parse_builtin_constant(ExpressionNode *p_previous_operand, bool p_can_assign);
  1367. ExpressionNode *parse_unary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1368. ExpressionNode *parse_binary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1369. ExpressionNode *parse_binary_not_in_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1370. ExpressionNode *parse_ternary_operator(ExpressionNode *p_previous_operand, bool p_can_assign);
  1371. ExpressionNode *parse_assignment(ExpressionNode *p_previous_operand, bool p_can_assign);
  1372. ExpressionNode *parse_array(ExpressionNode *p_previous_operand, bool p_can_assign);
  1373. ExpressionNode *parse_dictionary(ExpressionNode *p_previous_operand, bool p_can_assign);
  1374. ExpressionNode *parse_call(ExpressionNode *p_previous_operand, bool p_can_assign);
  1375. ExpressionNode *parse_get_node(ExpressionNode *p_previous_operand, bool p_can_assign);
  1376. ExpressionNode *parse_preload(ExpressionNode *p_previous_operand, bool p_can_assign);
  1377. ExpressionNode *parse_grouping(ExpressionNode *p_previous_operand, bool p_can_assign);
  1378. ExpressionNode *parse_cast(ExpressionNode *p_previous_operand, bool p_can_assign);
  1379. ExpressionNode *parse_await(ExpressionNode *p_previous_operand, bool p_can_assign);
  1380. ExpressionNode *parse_attribute(ExpressionNode *p_previous_operand, bool p_can_assign);
  1381. ExpressionNode *parse_subscript(ExpressionNode *p_previous_operand, bool p_can_assign);
  1382. ExpressionNode *parse_lambda(ExpressionNode *p_previous_operand, bool p_can_assign);
  1383. ExpressionNode *parse_type_test(ExpressionNode *p_previous_operand, bool p_can_assign);
  1384. ExpressionNode *parse_yield(ExpressionNode *p_previous_operand, bool p_can_assign);
  1385. ExpressionNode *parse_invalid_token(ExpressionNode *p_previous_operand, bool p_can_assign);
  1386. TypeNode *parse_type(bool p_allow_void = false);
  1387. #ifdef TOOLS_ENABLED
  1388. int max_script_doc_line = INT_MAX;
  1389. int min_member_doc_line = 1;
  1390. bool has_comment(int p_line, bool p_must_be_doc = false);
  1391. MemberDocData parse_doc_comment(int p_line, bool p_single_line = false);
  1392. ClassDocData parse_class_doc_comment(int p_line, bool p_single_line = false);
  1393. #endif // TOOLS_ENABLED
  1394. public:
  1395. Error parse(const String &p_source_code, const String &p_script_path, bool p_for_completion, bool p_parse_body = true);
  1396. Error parse_binary(const Vector<uint8_t> &p_binary, const String &p_script_path);
  1397. ClassNode *get_tree() const { return head; }
  1398. bool is_tool() const { return _is_tool; }
  1399. Ref<GDScriptParserRef> get_depended_parser_for(const String &p_path);
  1400. const HashMap<String, Ref<GDScriptParserRef>> &get_depended_parsers();
  1401. ClassNode *find_class(const String &p_qualified_name) const;
  1402. bool has_class(const GDScriptParser::ClassNode *p_class) const;
  1403. static Variant::Type get_builtin_type(const StringName &p_type); // Excluding `Variant::NIL` and `Variant::OBJECT`.
  1404. CompletionContext get_completion_context() const { return completion_context; }
  1405. void get_annotation_list(List<MethodInfo> *r_annotations) const;
  1406. bool annotation_exists(const String &p_annotation_name) const;
  1407. const List<ParserError> &get_errors() const { return errors; }
  1408. const List<String> get_dependencies() const {
  1409. // TODO: Keep track of deps.
  1410. return List<String>();
  1411. }
  1412. #ifdef DEBUG_ENABLED
  1413. const List<GDScriptWarning> &get_warnings() const { return warnings; }
  1414. const HashSet<int> &get_unsafe_lines() const { return unsafe_lines; }
  1415. int get_last_line_number() const { return current.end_line; }
  1416. #endif
  1417. #ifdef TOOLS_ENABLED
  1418. static HashMap<String, String> theme_color_names;
  1419. HashMap<int, GDScriptTokenizer::CommentData> comment_data;
  1420. #endif // TOOLS_ENABLED
  1421. GDScriptParser();
  1422. ~GDScriptParser();
  1423. #ifdef DEBUG_ENABLED
  1424. class TreePrinter {
  1425. int indent_level = 0;
  1426. String indent;
  1427. StringBuilder printed;
  1428. bool pending_indent = false;
  1429. void increase_indent();
  1430. void decrease_indent();
  1431. void push_line(const String &p_line = String());
  1432. void push_text(const String &p_text);
  1433. void print_annotation(const AnnotationNode *p_annotation);
  1434. void print_array(ArrayNode *p_array);
  1435. void print_assert(AssertNode *p_assert);
  1436. void print_assignment(AssignmentNode *p_assignment);
  1437. void print_await(AwaitNode *p_await);
  1438. void print_binary_op(BinaryOpNode *p_binary_op);
  1439. void print_call(CallNode *p_call);
  1440. void print_cast(CastNode *p_cast);
  1441. void print_class(ClassNode *p_class);
  1442. void print_constant(ConstantNode *p_constant);
  1443. void print_dictionary(DictionaryNode *p_dictionary);
  1444. void print_expression(ExpressionNode *p_expression);
  1445. void print_enum(EnumNode *p_enum);
  1446. void print_for(ForNode *p_for);
  1447. void print_function(FunctionNode *p_function, const String &p_context = "Function");
  1448. void print_get_node(GetNodeNode *p_get_node);
  1449. void print_if(IfNode *p_if, bool p_is_elif = false);
  1450. void print_identifier(IdentifierNode *p_identifier);
  1451. void print_lambda(LambdaNode *p_lambda);
  1452. void print_literal(LiteralNode *p_literal);
  1453. void print_match(MatchNode *p_match);
  1454. void print_match_branch(MatchBranchNode *p_match_branch);
  1455. void print_match_pattern(PatternNode *p_match_pattern);
  1456. void print_parameter(ParameterNode *p_parameter);
  1457. void print_preload(PreloadNode *p_preload);
  1458. void print_return(ReturnNode *p_return);
  1459. void print_self(SelfNode *p_self);
  1460. void print_signal(SignalNode *p_signal);
  1461. void print_statement(Node *p_statement);
  1462. void print_subscript(SubscriptNode *p_subscript);
  1463. void print_suite(SuiteNode *p_suite);
  1464. void print_ternary_op(TernaryOpNode *p_ternary_op);
  1465. void print_type(TypeNode *p_type);
  1466. void print_type_test(TypeTestNode *p_type_test);
  1467. void print_unary_op(UnaryOpNode *p_unary_op);
  1468. void print_variable(VariableNode *p_variable);
  1469. void print_while(WhileNode *p_while);
  1470. public:
  1471. void print_tree(const GDScriptParser &p_parser);
  1472. };
  1473. #endif // DEBUG_ENABLED
  1474. static void cleanup();
  1475. };