gdscript_analyzer.cpp 154 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967
  1. /*************************************************************************/
  2. /* gdscript_analyzer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /*************************************************************************/
  30. #include "gdscript_analyzer.h"
  31. #include "core/config/engine.h"
  32. #include "core/config/project_settings.h"
  33. #include "core/io/file_access.h"
  34. #include "core/io/resource_loader.h"
  35. #include "core/object/class_db.h"
  36. #include "core/object/script_language.h"
  37. #include "core/templates/hash_map.h"
  38. #include "gdscript.h"
  39. #include "gdscript_utility_functions.h"
  40. static MethodInfo info_from_utility_func(const StringName &p_function) {
  41. ERR_FAIL_COND_V(!Variant::has_utility_function(p_function), MethodInfo());
  42. MethodInfo info(p_function);
  43. if (Variant::has_utility_function_return_value(p_function)) {
  44. info.return_val.type = Variant::get_utility_function_return_type(p_function);
  45. if (info.return_val.type == Variant::NIL) {
  46. info.return_val.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  47. }
  48. }
  49. if (Variant::is_utility_function_vararg(p_function)) {
  50. info.flags |= METHOD_FLAG_VARARG;
  51. } else {
  52. for (int i = 0; i < Variant::get_utility_function_argument_count(p_function); i++) {
  53. PropertyInfo pi;
  54. #ifdef DEBUG_METHODS_ENABLED
  55. pi.name = Variant::get_utility_function_argument_name(p_function, i);
  56. #else
  57. pi.name = "arg" + itos(i + 1);
  58. #endif
  59. pi.type = Variant::get_utility_function_argument_type(p_function, i);
  60. if (pi.type == Variant::NIL) {
  61. pi.usage |= PROPERTY_USAGE_NIL_IS_VARIANT;
  62. }
  63. info.arguments.push_back(pi);
  64. }
  65. }
  66. return info;
  67. }
  68. static GDScriptParser::DataType make_callable_type(const MethodInfo &p_info) {
  69. GDScriptParser::DataType type;
  70. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  71. type.kind = GDScriptParser::DataType::BUILTIN;
  72. type.builtin_type = Variant::CALLABLE;
  73. type.is_constant = true;
  74. type.method_info = p_info;
  75. return type;
  76. }
  77. static GDScriptParser::DataType make_signal_type(const MethodInfo &p_info) {
  78. GDScriptParser::DataType type;
  79. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  80. type.kind = GDScriptParser::DataType::BUILTIN;
  81. type.builtin_type = Variant::SIGNAL;
  82. type.is_constant = true;
  83. type.method_info = p_info;
  84. return type;
  85. }
  86. static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_name) {
  87. GDScriptParser::DataType type;
  88. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  89. type.kind = GDScriptParser::DataType::NATIVE;
  90. type.builtin_type = Variant::OBJECT;
  91. type.is_constant = true;
  92. type.native_type = p_class_name;
  93. type.is_meta_type = true;
  94. return type;
  95. }
  96. static GDScriptParser::DataType make_native_enum_type(const StringName &p_native_class, const StringName &p_enum_name) {
  97. GDScriptParser::DataType type;
  98. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  99. type.kind = GDScriptParser::DataType::ENUM;
  100. type.builtin_type = Variant::OBJECT;
  101. type.is_constant = true;
  102. type.is_meta_type = true;
  103. List<StringName> enum_values;
  104. ClassDB::get_enum_constants(p_native_class, p_enum_name, &enum_values);
  105. for (const StringName &E : enum_values) {
  106. type.enum_values[E] = ClassDB::get_integer_constant(p_native_class, E);
  107. }
  108. return type;
  109. }
  110. static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) {
  111. GDScriptParser::DataType type;
  112. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  113. type.kind = GDScriptParser::DataType::BUILTIN;
  114. type.builtin_type = p_type;
  115. type.is_constant = true;
  116. type.is_meta_type = true;
  117. return type;
  118. }
  119. bool GDScriptAnalyzer::has_member_name_conflict_in_script_class(const StringName &p_member_name, const GDScriptParser::ClassNode *p_class) {
  120. if (p_class->members_indices.has(p_member_name)) {
  121. int index = p_class->members_indices[p_member_name];
  122. const GDScriptParser::ClassNode::Member *member = &p_class->members[index];
  123. if (member->type == GDScriptParser::ClassNode::Member::VARIABLE ||
  124. member->type == GDScriptParser::ClassNode::Member::CONSTANT ||
  125. member->type == GDScriptParser::ClassNode::Member::ENUM ||
  126. member->type == GDScriptParser::ClassNode::Member::ENUM_VALUE ||
  127. member->type == GDScriptParser::ClassNode::Member::CLASS ||
  128. member->type == GDScriptParser::ClassNode::Member::SIGNAL) {
  129. return true;
  130. }
  131. }
  132. return false;
  133. }
  134. bool GDScriptAnalyzer::has_member_name_conflict_in_native_type(const StringName &p_member_name, const StringName &p_native_type_string) {
  135. if (ClassDB::has_signal(p_native_type_string, p_member_name)) {
  136. return true;
  137. }
  138. if (ClassDB::has_property(p_native_type_string, p_member_name)) {
  139. return true;
  140. }
  141. if (ClassDB::has_integer_constant(p_native_type_string, p_member_name)) {
  142. return true;
  143. }
  144. return false;
  145. }
  146. Error GDScriptAnalyzer::check_native_member_name_conflict(const StringName &p_member_name, const GDScriptParser::Node *p_member_node, const StringName &p_native_type_string) {
  147. if (has_member_name_conflict_in_native_type(p_member_name, p_native_type_string)) {
  148. push_error(vformat(R"(Member "%s" redefined (original in native class '%s'))", p_member_name, p_native_type_string), p_member_node);
  149. return ERR_PARSE_ERROR;
  150. }
  151. if (class_exists(p_member_name)) {
  152. push_error(vformat(R"(The member "%s" shadows a native class.)", p_member_name), p_member_node);
  153. return ERR_PARSE_ERROR;
  154. }
  155. if (GDScriptParser::get_builtin_type(p_member_name) != Variant::VARIANT_MAX) {
  156. push_error(vformat(R"(The member "%s" cannot have the same name as a builtin type.)", p_member_name), p_member_node);
  157. return ERR_PARSE_ERROR;
  158. }
  159. return OK;
  160. }
  161. Error GDScriptAnalyzer::check_class_member_name_conflict(const GDScriptParser::ClassNode *p_class_node, const StringName &p_member_name, const GDScriptParser::Node *p_member_node) {
  162. const GDScriptParser::DataType *current_data_type = &p_class_node->base_type;
  163. while (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::CLASS) {
  164. GDScriptParser::ClassNode *current_class_node = current_data_type->class_type;
  165. if (has_member_name_conflict_in_script_class(p_member_name, current_class_node)) {
  166. push_error(vformat(R"(The member "%s" already exists in a parent class.)", p_member_name),
  167. p_member_node);
  168. return ERR_PARSE_ERROR;
  169. }
  170. current_data_type = &current_class_node->base_type;
  171. }
  172. if (current_data_type && current_data_type->kind == GDScriptParser::DataType::Kind::NATIVE) {
  173. if (current_data_type->native_type != StringName("")) {
  174. return check_native_member_name_conflict(
  175. p_member_name,
  176. p_member_node,
  177. current_data_type->native_type);
  178. }
  179. }
  180. return OK;
  181. }
  182. Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  183. if (p_class->base_type.is_set()) {
  184. // Already resolved
  185. return OK;
  186. }
  187. if (p_class == parser->head) {
  188. if (p_class->identifier) {
  189. p_class->fqcn = p_class->identifier->name;
  190. } else {
  191. p_class->fqcn = parser->script_path;
  192. }
  193. } else {
  194. p_class->fqcn = p_class->outer->fqcn + "::" + String(p_class->identifier->name);
  195. }
  196. if (p_class->identifier) {
  197. StringName class_name = p_class->identifier->name;
  198. if (class_exists(class_name)) {
  199. push_error(vformat(R"(Class "%s" hides a native class.)", class_name), p_class->identifier);
  200. } else if (ScriptServer::is_global_class(class_name) && (ScriptServer::get_global_class_path(class_name) != parser->script_path || p_class != parser->head)) {
  201. push_error(vformat(R"(Class "%s" hides a global script class.)", class_name), p_class->identifier);
  202. } else if (ProjectSettings::get_singleton()->has_autoload(class_name) && ProjectSettings::get_singleton()->get_autoload(class_name).is_singleton) {
  203. push_error(vformat(R"(Class "%s" hides an autoload singleton.)", class_name), p_class->identifier);
  204. }
  205. }
  206. GDScriptParser::DataType result;
  207. // Set datatype for class.
  208. GDScriptParser::DataType class_type;
  209. class_type.is_constant = true;
  210. class_type.is_meta_type = true;
  211. class_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  212. class_type.kind = GDScriptParser::DataType::CLASS;
  213. class_type.class_type = p_class;
  214. class_type.script_path = parser->script_path;
  215. class_type.builtin_type = Variant::OBJECT;
  216. p_class->set_datatype(class_type);
  217. if (!p_class->extends_used) {
  218. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  219. result.kind = GDScriptParser::DataType::NATIVE;
  220. result.native_type = "RefCounted";
  221. } else {
  222. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  223. GDScriptParser::DataType base;
  224. int extends_index = 0;
  225. if (!p_class->extends_path.is_empty()) {
  226. if (p_class->extends_path.is_relative_path()) {
  227. p_class->extends_path = class_type.script_path.get_base_dir().plus_file(p_class->extends_path).simplify_path();
  228. }
  229. Ref<GDScriptParserRef> parser = get_parser_for(p_class->extends_path);
  230. if (parser.is_null()) {
  231. push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class);
  232. return ERR_PARSE_ERROR;
  233. }
  234. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  235. if (err != OK) {
  236. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", p_class->extends_path), p_class);
  237. return err;
  238. }
  239. base = parser->get_parser()->head->get_datatype();
  240. } else {
  241. if (p_class->extends.is_empty()) {
  242. return ERR_PARSE_ERROR;
  243. }
  244. const StringName &name = p_class->extends[extends_index++];
  245. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  246. if (ScriptServer::is_global_class(name)) {
  247. String base_path = ScriptServer::get_global_class_path(name);
  248. if (base_path == parser->script_path) {
  249. base = parser->head->get_datatype();
  250. } else {
  251. Ref<GDScriptParserRef> parser = get_parser_for(base_path);
  252. if (parser.is_null()) {
  253. push_error(vformat(R"(Could not resolve super class "%s".)", name), p_class);
  254. return ERR_PARSE_ERROR;
  255. }
  256. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  257. if (err != OK) {
  258. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  259. return err;
  260. }
  261. base = parser->get_parser()->head->get_datatype();
  262. }
  263. } else if (ProjectSettings::get_singleton()->has_autoload(name) && ProjectSettings::get_singleton()->get_autoload(name).is_singleton) {
  264. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(name);
  265. if (info.path.get_extension().to_lower() != ".gd") {
  266. push_error(vformat(R"(Singleton %s is not a GDScript.)", info.name), p_class);
  267. return ERR_PARSE_ERROR;
  268. }
  269. Ref<GDScriptParserRef> parser = get_parser_for(info.path);
  270. if (parser.is_null()) {
  271. push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), p_class);
  272. return ERR_PARSE_ERROR;
  273. }
  274. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  275. if (err != OK) {
  276. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  277. return err;
  278. }
  279. } else if (class_exists(name) && ClassDB::can_instantiate(name)) {
  280. base.kind = GDScriptParser::DataType::NATIVE;
  281. base.native_type = name;
  282. } else {
  283. // Look for other classes in script.
  284. GDScriptParser::ClassNode *look_class = p_class;
  285. bool found = false;
  286. while (look_class != nullptr) {
  287. if (look_class->identifier && look_class->identifier->name == name) {
  288. if (!look_class->get_datatype().is_set()) {
  289. Error err = resolve_inheritance(look_class, false);
  290. if (err) {
  291. return err;
  292. }
  293. }
  294. base = look_class->get_datatype();
  295. found = true;
  296. break;
  297. }
  298. if (look_class->members_indices.has(name) && look_class->get_member(name).type == GDScriptParser::ClassNode::Member::CLASS) {
  299. GDScriptParser::ClassNode::Member member = look_class->get_member(name);
  300. if (!member.m_class->get_datatype().is_set()) {
  301. Error err = resolve_inheritance(member.m_class, false);
  302. if (err) {
  303. return err;
  304. }
  305. }
  306. base = member.m_class->get_datatype();
  307. found = true;
  308. break;
  309. }
  310. look_class = look_class->outer;
  311. }
  312. if (!found) {
  313. push_error(vformat(R"(Could not find base class "%s".)", name), p_class);
  314. return ERR_PARSE_ERROR;
  315. }
  316. }
  317. }
  318. for (int index = extends_index; index < p_class->extends.size(); index++) {
  319. if (base.kind != GDScriptParser::DataType::CLASS) {
  320. push_error(R"(Super type "%s" is not a GDScript. Cannot get nested types.)", p_class);
  321. return ERR_PARSE_ERROR;
  322. }
  323. // TODO: Extends could use identifier nodes. That way errors can be pointed out properly and it can be used here.
  324. GDScriptParser::IdentifierNode *id = parser->alloc_node<GDScriptParser::IdentifierNode>();
  325. id->name = p_class->extends[index];
  326. reduce_identifier_from_base(id, &base);
  327. GDScriptParser::DataType id_type = id->get_datatype();
  328. if (!id_type.is_set()) {
  329. push_error(vformat(R"(Could not find type "%s" under base "%s".)", id->name, base.to_string()), p_class);
  330. }
  331. base = id_type;
  332. }
  333. result = base;
  334. }
  335. if (!result.is_set()) {
  336. // TODO: More specific error messages.
  337. push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "<main>" : p_class->identifier->name), p_class);
  338. return ERR_PARSE_ERROR;
  339. }
  340. // Check for cyclic inheritance.
  341. const GDScriptParser::ClassNode *base_class = result.class_type;
  342. while (base_class) {
  343. if (base_class->fqcn == p_class->fqcn) {
  344. push_error("Cyclic inheritance.", p_class);
  345. return ERR_PARSE_ERROR;
  346. }
  347. base_class = base_class->base_type.class_type;
  348. }
  349. p_class->base_type = result;
  350. class_type.native_type = result.native_type;
  351. p_class->set_datatype(class_type);
  352. if (p_recursive) {
  353. for (int i = 0; i < p_class->members.size(); i++) {
  354. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  355. Error err = resolve_inheritance(p_class->members[i].m_class, true);
  356. if (err) {
  357. return err;
  358. }
  359. }
  360. }
  361. }
  362. return OK;
  363. }
  364. GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::TypeNode *p_type) {
  365. GDScriptParser::DataType result;
  366. if (p_type == nullptr) {
  367. result.kind = GDScriptParser::DataType::VARIANT;
  368. return result;
  369. }
  370. result.type_source = result.ANNOTATED_EXPLICIT;
  371. result.builtin_type = Variant::OBJECT;
  372. if (p_type->type_chain.is_empty()) {
  373. // void.
  374. result.kind = GDScriptParser::DataType::BUILTIN;
  375. result.builtin_type = Variant::NIL;
  376. p_type->set_datatype(result);
  377. return result;
  378. }
  379. StringName first = p_type->type_chain[0]->name;
  380. if (first == "Variant") {
  381. result.kind = GDScriptParser::DataType::VARIANT;
  382. if (p_type->type_chain.size() > 1) {
  383. push_error(R"("Variant" type don't contain nested types.)", p_type->type_chain[1]);
  384. return GDScriptParser::DataType();
  385. }
  386. return result;
  387. }
  388. if (first == "Object") {
  389. result.kind = GDScriptParser::DataType::NATIVE;
  390. result.native_type = "Object";
  391. if (p_type->type_chain.size() > 1) {
  392. push_error(R"("Object" type don't contain nested types.)", p_type->type_chain[1]);
  393. return GDScriptParser::DataType();
  394. }
  395. return result;
  396. }
  397. if (GDScriptParser::get_builtin_type(first) < Variant::VARIANT_MAX) {
  398. // Built-in types.
  399. if (p_type->type_chain.size() > 1) {
  400. push_error(R"(Built-in types don't contain nested types.)", p_type->type_chain[1]);
  401. return GDScriptParser::DataType();
  402. }
  403. result.kind = GDScriptParser::DataType::BUILTIN;
  404. result.builtin_type = GDScriptParser::get_builtin_type(first);
  405. if (result.builtin_type == Variant::ARRAY) {
  406. GDScriptParser::DataType container_type = resolve_datatype(p_type->container_type);
  407. if (container_type.kind != GDScriptParser::DataType::VARIANT) {
  408. result.set_container_element_type(container_type);
  409. }
  410. }
  411. } else if (class_exists(first)) {
  412. // Native engine classes.
  413. result.kind = GDScriptParser::DataType::NATIVE;
  414. result.native_type = first;
  415. } else if (ScriptServer::is_global_class(first)) {
  416. if (parser->script_path == ScriptServer::get_global_class_path(first)) {
  417. result = parser->head->get_datatype();
  418. } else {
  419. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(first));
  420. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  421. push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type);
  422. return GDScriptParser::DataType();
  423. }
  424. result = ref->get_parser()->head->get_datatype();
  425. }
  426. } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) {
  427. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first);
  428. Ref<GDScriptParserRef> ref = get_parser_for(autoload.path);
  429. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  430. push_error(vformat(R"(Could not parse singleton "%s" from "%s".)", first, autoload.path), p_type);
  431. return GDScriptParser::DataType();
  432. }
  433. result = ref->get_parser()->head->get_datatype();
  434. } else if (ClassDB::has_enum(parser->current_class->base_type.native_type, first)) {
  435. // Native enum in current class.
  436. result = make_native_enum_type(parser->current_class->base_type.native_type, first);
  437. } else {
  438. // Classes in current scope.
  439. GDScriptParser::ClassNode *script_class = parser->current_class;
  440. bool found = false;
  441. while (!found && script_class != nullptr) {
  442. if (script_class->identifier && script_class->identifier->name == first) {
  443. result = script_class->get_datatype();
  444. found = true;
  445. break;
  446. }
  447. if (script_class->members_indices.has(first)) {
  448. GDScriptParser::ClassNode::Member member = script_class->members[script_class->members_indices[first]];
  449. switch (member.type) {
  450. case GDScriptParser::ClassNode::Member::CLASS:
  451. result = member.m_class->get_datatype();
  452. found = true;
  453. break;
  454. case GDScriptParser::ClassNode::Member::ENUM:
  455. result = member.m_enum->get_datatype();
  456. found = true;
  457. break;
  458. case GDScriptParser::ClassNode::Member::CONSTANT:
  459. if (member.constant->get_datatype().is_meta_type) {
  460. result = member.constant->get_datatype();
  461. result.is_meta_type = false;
  462. found = true;
  463. break;
  464. } else if (Ref<Script>(member.constant->initializer->reduced_value).is_valid()) {
  465. Ref<GDScript> gdscript = member.constant->initializer->reduced_value;
  466. if (gdscript.is_valid()) {
  467. Ref<GDScriptParserRef> ref = get_parser_for(gdscript->get_path());
  468. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  469. push_error(vformat(R"(Could not parse script from "%s".)", gdscript->get_path()), p_type);
  470. return GDScriptParser::DataType();
  471. }
  472. result = ref->get_parser()->head->get_datatype();
  473. result.is_meta_type = false;
  474. } else {
  475. Ref<GDScript> script = member.constant->initializer->reduced_value;
  476. result.kind = GDScriptParser::DataType::SCRIPT;
  477. result.builtin_type = Variant::OBJECT;
  478. result.script_type = script;
  479. result.script_path = script->get_path();
  480. result.native_type = script->get_instance_base_type();
  481. }
  482. break;
  483. }
  484. [[fallthrough]];
  485. default:
  486. push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type);
  487. return GDScriptParser::DataType();
  488. }
  489. }
  490. script_class = script_class->outer;
  491. }
  492. }
  493. if (!result.is_set()) {
  494. push_error(vformat(R"("%s" was not found in the current scope.)", first), p_type);
  495. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  496. return result;
  497. }
  498. if (p_type->type_chain.size() > 1) {
  499. if (result.kind == GDScriptParser::DataType::CLASS) {
  500. for (int i = 1; i < p_type->type_chain.size(); i++) {
  501. GDScriptParser::DataType base = result;
  502. reduce_identifier_from_base(p_type->type_chain[i], &base);
  503. result = p_type->type_chain[i]->get_datatype();
  504. if (!result.is_set()) {
  505. push_error(vformat(R"(Could not find type "%s" under base "%s".)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]);
  506. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  507. return result;
  508. } else if (!result.is_meta_type) {
  509. push_error(vformat(R"(Member "%s" under base "%s" is not a valid type.)", p_type->type_chain[i]->name, base.to_string()), p_type->type_chain[1]);
  510. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  511. return result;
  512. }
  513. }
  514. } else if (result.kind == GDScriptParser::DataType::NATIVE) {
  515. // Only enums allowed for native.
  516. if (ClassDB::has_enum(result.native_type, p_type->type_chain[1]->name)) {
  517. if (p_type->type_chain.size() > 2) {
  518. push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]);
  519. } else {
  520. result = make_native_enum_type(result.native_type, p_type->type_chain[1]->name);
  521. }
  522. }
  523. } else {
  524. push_error(vformat(R"(Could not find nested type "%s" under base "%s".)", p_type->type_chain[1]->name, result.to_string()), p_type->type_chain[1]);
  525. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  526. return result;
  527. }
  528. }
  529. if (result.builtin_type != Variant::ARRAY && p_type->container_type != nullptr) {
  530. push_error("Only arrays can specify the collection element type.", p_type);
  531. }
  532. p_type->set_datatype(result);
  533. return result;
  534. }
  535. void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class) {
  536. if (p_class->resolved_interface) {
  537. return;
  538. }
  539. p_class->resolved_interface = true;
  540. GDScriptParser::ClassNode *previous_class = parser->current_class;
  541. parser->current_class = p_class;
  542. for (int i = 0; i < p_class->members.size(); i++) {
  543. GDScriptParser::ClassNode::Member member = p_class->members[i];
  544. switch (member.type) {
  545. case GDScriptParser::ClassNode::Member::VARIABLE: {
  546. check_class_member_name_conflict(p_class, member.variable->identifier->name, member.variable);
  547. GDScriptParser::DataType datatype;
  548. datatype.kind = GDScriptParser::DataType::VARIANT;
  549. datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  550. GDScriptParser::DataType specified_type;
  551. if (member.variable->datatype_specifier != nullptr) {
  552. specified_type = resolve_datatype(member.variable->datatype_specifier);
  553. specified_type.is_meta_type = false;
  554. }
  555. if (member.variable->initializer != nullptr) {
  556. member.variable->set_datatype(datatype); // Allow recursive usage.
  557. reduce_expression(member.variable->initializer);
  558. if ((member.variable->infer_datatype || (member.variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && member.variable->initializer->type == GDScriptParser::Node::ARRAY) {
  559. // Typed array.
  560. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.variable->initializer);
  561. // Can only infer typed array if it has elements.
  562. if ((member.variable->infer_datatype && array->elements.size() > 0) || member.variable->datatype_specifier != nullptr) {
  563. update_array_literal_element_type(specified_type, array);
  564. }
  565. }
  566. datatype = member.variable->initializer->get_datatype();
  567. if (datatype.type_source != GDScriptParser::DataType::UNDETECTED) {
  568. datatype.type_source = GDScriptParser::DataType::INFERRED;
  569. }
  570. }
  571. if (member.variable->datatype_specifier != nullptr) {
  572. datatype = specified_type;
  573. if (member.variable->initializer != nullptr) {
  574. if (!is_type_compatible(datatype, member.variable->initializer->get_datatype(), true)) {
  575. // Try reverse test since it can be a masked subtype.
  576. if (!is_type_compatible(member.variable->initializer->get_datatype(), datatype, true)) {
  577. push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", member.variable->initializer->get_datatype().to_string(), datatype.to_string()), member.variable->initializer);
  578. } else {
  579. // TODO: Add warning.
  580. mark_node_unsafe(member.variable->initializer);
  581. member.variable->use_conversion_assign = true;
  582. }
  583. } else if (datatype.builtin_type == Variant::INT && member.variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  584. #ifdef DEBUG_ENABLED
  585. parser->push_warning(member.variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  586. #endif
  587. }
  588. if (member.variable->initializer->get_datatype().is_variant()) {
  589. // TODO: Warn unsafe assign.
  590. mark_node_unsafe(member.variable->initializer);
  591. member.variable->use_conversion_assign = true;
  592. }
  593. }
  594. } else if (member.variable->infer_datatype) {
  595. if (member.variable->initializer == nullptr) {
  596. push_error(vformat(R"(Cannot infer the type of "%s" variable because there's no default value.)", member.variable->identifier->name), member.variable->identifier);
  597. } else if (!datatype.is_set() || datatype.has_no_type()) {
  598. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value doesn't have a set type.)", member.variable->identifier->name), member.variable->initializer);
  599. } else if (datatype.is_variant()) {
  600. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is Variant. Use explicit "Variant" type if this is intended.)", member.variable->identifier->name), member.variable->initializer);
  601. } else if (datatype.builtin_type == Variant::NIL) {
  602. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
  603. }
  604. datatype.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  605. }
  606. datatype.is_constant = false;
  607. member.variable->set_datatype(datatype);
  608. // Apply annotations.
  609. for (GDScriptParser::AnnotationNode *&E : member.variable->annotations) {
  610. E->apply(parser, member.variable);
  611. }
  612. } break;
  613. case GDScriptParser::ClassNode::Member::CONSTANT: {
  614. check_class_member_name_conflict(p_class, member.constant->identifier->name, member.constant);
  615. reduce_expression(member.constant->initializer);
  616. GDScriptParser::DataType specified_type;
  617. if (member.constant->datatype_specifier != nullptr) {
  618. specified_type = resolve_datatype(member.constant->datatype_specifier);
  619. specified_type.is_meta_type = false;
  620. }
  621. GDScriptParser::DataType datatype;
  622. if (member.constant->initializer) {
  623. datatype = member.constant->initializer->get_datatype();
  624. if (member.constant->initializer->type == GDScriptParser::Node::ARRAY) {
  625. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(member.constant->initializer);
  626. const_fold_array(array);
  627. // Can only infer typed array if it has elements.
  628. if (array->elements.size() > 0 || (member.constant->datatype_specifier != nullptr && specified_type.has_container_element_type())) {
  629. update_array_literal_element_type(specified_type, array);
  630. }
  631. } else if (member.constant->initializer->type == GDScriptParser::Node::DICTIONARY) {
  632. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(member.constant->initializer));
  633. }
  634. if (!member.constant->initializer->is_constant) {
  635. push_error(R"(Initializer for a constant must be a constant expression.)", member.constant->initializer);
  636. }
  637. if (member.constant->datatype_specifier != nullptr) {
  638. datatype = specified_type;
  639. if (!is_type_compatible(datatype, member.constant->initializer->get_datatype(), true)) {
  640. push_error(vformat(R"(Value of type "%s" cannot be initialized to constant of type "%s".)", member.constant->initializer->get_datatype().to_string(), datatype.to_string()), member.constant->initializer);
  641. } else if (datatype.builtin_type == Variant::INT && member.constant->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  642. #ifdef DEBUG_ENABLED
  643. parser->push_warning(member.constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  644. #endif
  645. }
  646. }
  647. }
  648. datatype.is_constant = true;
  649. member.constant->set_datatype(datatype);
  650. // Apply annotations.
  651. for (GDScriptParser::AnnotationNode *&E : member.constant->annotations) {
  652. E->apply(parser, member.constant);
  653. }
  654. } break;
  655. case GDScriptParser::ClassNode::Member::SIGNAL: {
  656. check_class_member_name_conflict(p_class, member.signal->identifier->name, member.signal);
  657. for (int j = 0; j < member.signal->parameters.size(); j++) {
  658. GDScriptParser::DataType signal_type = resolve_datatype(member.signal->parameters[j]->datatype_specifier);
  659. signal_type.is_meta_type = false;
  660. member.signal->parameters[j]->set_datatype(signal_type);
  661. }
  662. // TODO: Make MethodInfo from signal.
  663. GDScriptParser::DataType signal_type;
  664. signal_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  665. signal_type.kind = GDScriptParser::DataType::BUILTIN;
  666. signal_type.builtin_type = Variant::SIGNAL;
  667. member.signal->set_datatype(signal_type);
  668. // Apply annotations.
  669. for (GDScriptParser::AnnotationNode *&E : member.signal->annotations) {
  670. E->apply(parser, member.signal);
  671. }
  672. } break;
  673. case GDScriptParser::ClassNode::Member::ENUM: {
  674. check_class_member_name_conflict(p_class, member.m_enum->identifier->name, member.m_enum);
  675. GDScriptParser::DataType enum_type;
  676. enum_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  677. enum_type.kind = GDScriptParser::DataType::ENUM;
  678. enum_type.builtin_type = Variant::DICTIONARY;
  679. enum_type.enum_type = member.m_enum->identifier->name;
  680. enum_type.native_type = p_class->fqcn + "." + member.m_enum->identifier->name;
  681. enum_type.is_meta_type = true;
  682. enum_type.is_constant = true;
  683. // Enums can't be nested, so we can safely override this.
  684. current_enum = member.m_enum;
  685. for (int j = 0; j < member.m_enum->values.size(); j++) {
  686. GDScriptParser::EnumNode::Value &element = member.m_enum->values.write[j];
  687. if (element.custom_value) {
  688. reduce_expression(element.custom_value);
  689. if (!element.custom_value->is_constant) {
  690. push_error(R"(Enum values must be constant.)", element.custom_value);
  691. } else if (element.custom_value->reduced_value.get_type() != Variant::INT) {
  692. push_error(R"(Enum values must be integers.)", element.custom_value);
  693. } else {
  694. element.value = element.custom_value->reduced_value;
  695. element.resolved = true;
  696. }
  697. } else {
  698. if (element.index > 0) {
  699. element.value = element.parent_enum->values[element.index - 1].value + 1;
  700. } else {
  701. element.value = 0;
  702. }
  703. element.resolved = true;
  704. }
  705. enum_type.enum_values[element.identifier->name] = element.value;
  706. }
  707. current_enum = nullptr;
  708. member.m_enum->set_datatype(enum_type);
  709. // Apply annotations.
  710. for (GDScriptParser::AnnotationNode *&E : member.m_enum->annotations) {
  711. E->apply(parser, member.m_enum);
  712. }
  713. } break;
  714. case GDScriptParser::ClassNode::Member::FUNCTION:
  715. resolve_function_signature(member.function);
  716. break;
  717. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  718. if (member.enum_value.custom_value) {
  719. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.custom_value);
  720. current_enum = member.enum_value.parent_enum;
  721. reduce_expression(member.enum_value.custom_value);
  722. current_enum = nullptr;
  723. if (!member.enum_value.custom_value->is_constant) {
  724. push_error(R"(Enum values must be constant.)", member.enum_value.custom_value);
  725. } else if (member.enum_value.custom_value->reduced_value.get_type() != Variant::INT) {
  726. push_error(R"(Enum values must be integers.)", member.enum_value.custom_value);
  727. } else {
  728. member.enum_value.value = member.enum_value.custom_value->reduced_value;
  729. member.enum_value.resolved = true;
  730. }
  731. } else {
  732. check_class_member_name_conflict(p_class, member.enum_value.identifier->name, member.enum_value.parent_enum);
  733. if (member.enum_value.index > 0) {
  734. member.enum_value.value = member.enum_value.parent_enum->values[member.enum_value.index - 1].value + 1;
  735. } else {
  736. member.enum_value.value = 0;
  737. }
  738. member.enum_value.resolved = true;
  739. }
  740. // Also update the original references.
  741. member.enum_value.parent_enum->values.write[member.enum_value.index] = member.enum_value;
  742. p_class->members.write[i].enum_value = member.enum_value;
  743. } break;
  744. case GDScriptParser::ClassNode::Member::CLASS:
  745. check_class_member_name_conflict(p_class, member.m_class->identifier->name, member.m_class);
  746. break;
  747. case GDScriptParser::ClassNode::Member::UNDEFINED:
  748. ERR_PRINT("Trying to resolve undefined member.");
  749. break;
  750. }
  751. }
  752. // Recurse nested classes.
  753. for (int i = 0; i < p_class->members.size(); i++) {
  754. GDScriptParser::ClassNode::Member member = p_class->members[i];
  755. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  756. continue;
  757. }
  758. resolve_class_interface(member.m_class);
  759. }
  760. parser->current_class = previous_class;
  761. }
  762. void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class) {
  763. if (p_class->resolved_body) {
  764. return;
  765. }
  766. p_class->resolved_body = true;
  767. GDScriptParser::ClassNode *previous_class = parser->current_class;
  768. parser->current_class = p_class;
  769. // Do functions and properties now.
  770. for (int i = 0; i < p_class->members.size(); i++) {
  771. GDScriptParser::ClassNode::Member member = p_class->members[i];
  772. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  773. resolve_function_body(member.function);
  774. // Apply annotations.
  775. for (GDScriptParser::AnnotationNode *&E : member.function->annotations) {
  776. E->apply(parser, member.function);
  777. }
  778. } else if (member.type == GDScriptParser::ClassNode::Member::VARIABLE && member.variable->property != GDScriptParser::VariableNode::PROP_NONE) {
  779. if (member.variable->property == GDScriptParser::VariableNode::PROP_INLINE) {
  780. if (member.variable->getter != nullptr) {
  781. member.variable->getter->set_datatype(member.variable->datatype);
  782. resolve_function_body(member.variable->getter);
  783. }
  784. if (member.variable->setter != nullptr) {
  785. if (member.variable->setter->parameters.size() > 0) {
  786. member.variable->setter->parameters[0]->datatype_specifier = member.variable->datatype_specifier;
  787. }
  788. resolve_function_signature(member.variable->setter);
  789. resolve_function_body(member.variable->setter);
  790. }
  791. }
  792. }
  793. }
  794. parser->current_class = previous_class;
  795. // Recurse nested classes.
  796. for (int i = 0; i < p_class->members.size(); i++) {
  797. GDScriptParser::ClassNode::Member member = p_class->members[i];
  798. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  799. continue;
  800. }
  801. resolve_class_body(member.m_class);
  802. }
  803. // Check unused variables and datatypes of property getters and setters.
  804. for (int i = 0; i < p_class->members.size(); i++) {
  805. GDScriptParser::ClassNode::Member member = p_class->members[i];
  806. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  807. #ifdef DEBUG_ENABLED
  808. if (member.variable->usages == 0 && String(member.variable->identifier->name).begins_with("_")) {
  809. parser->push_warning(member.variable->identifier, GDScriptWarning::UNUSED_PRIVATE_CLASS_VARIABLE, member.variable->identifier->name);
  810. }
  811. #endif
  812. if (member.variable->property == GDScriptParser::VariableNode::PROP_SETGET) {
  813. GDScriptParser::FunctionNode *getter_function = nullptr;
  814. GDScriptParser::FunctionNode *setter_function = nullptr;
  815. bool has_valid_getter = false;
  816. bool has_valid_setter = false;
  817. if (member.variable->getter_pointer != nullptr) {
  818. if (p_class->has_function(member.variable->getter_pointer->name)) {
  819. getter_function = p_class->get_member(member.variable->getter_pointer->name).function;
  820. }
  821. if (getter_function == nullptr) {
  822. push_error(vformat(R"(Getter "%s" not found.)", member.variable->getter_pointer->name), member.variable);
  823. } else if (getter_function->parameters.size() != 0 || getter_function->datatype.has_no_type()) {
  824. push_error(vformat(R"(Function "%s" cannot be used as getter because of its signature.)", getter_function->identifier->name), member.variable);
  825. } else if (!is_type_compatible(member.variable->datatype, getter_function->datatype, true)) {
  826. push_error(vformat(R"(Function with return type "%s" cannot be used as getter for a property of type "%s".)", getter_function->datatype.to_string(), member.variable->datatype.to_string()), member.variable);
  827. } else {
  828. has_valid_getter = true;
  829. #ifdef DEBUG_ENABLED
  830. if (member.variable->datatype.builtin_type == Variant::INT && getter_function->datatype.builtin_type == Variant::FLOAT) {
  831. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  832. }
  833. #endif
  834. }
  835. }
  836. if (member.variable->setter_pointer != nullptr) {
  837. if (p_class->has_function(member.variable->setter_pointer->name)) {
  838. setter_function = p_class->get_member(member.variable->setter_pointer->name).function;
  839. }
  840. if (setter_function == nullptr) {
  841. push_error(vformat(R"(Setter "%s" not found.)", member.variable->setter_pointer->name), member.variable);
  842. } else if (setter_function->parameters.size() != 1) {
  843. push_error(vformat(R"(Function "%s" cannot be used as setter because of its signature.)", setter_function->identifier->name), member.variable);
  844. } else if (!is_type_compatible(member.variable->datatype, setter_function->parameters[0]->datatype, true)) {
  845. push_error(vformat(R"(Function with argument type "%s" cannot be used as setter for a property of type "%s".)", setter_function->parameters[0]->datatype.to_string(), member.variable->datatype.to_string()), member.variable);
  846. } else {
  847. has_valid_setter = true;
  848. #ifdef DEBUG_ENABLED
  849. if (member.variable->datatype.builtin_type == Variant::INT && setter_function->return_type->datatype.builtin_type == Variant::FLOAT) {
  850. parser->push_warning(member.variable, GDScriptWarning::NARROWING_CONVERSION);
  851. }
  852. #endif
  853. }
  854. }
  855. if (member.variable->datatype.is_variant() && has_valid_getter && has_valid_setter) {
  856. if (!is_type_compatible(getter_function->datatype, setter_function->parameters[0]->datatype, true)) {
  857. push_error(vformat(R"(Getter with type "%s" cannot be used along with setter of type "%s".)", getter_function->datatype.to_string(), setter_function->parameters[0]->datatype.to_string()), member.variable);
  858. }
  859. }
  860. }
  861. }
  862. }
  863. }
  864. void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node) {
  865. ERR_FAIL_COND_MSG(p_node == nullptr, "Trying to resolve type of a null node.");
  866. switch (p_node->type) {
  867. case GDScriptParser::Node::NONE:
  868. break; // Unreachable.
  869. case GDScriptParser::Node::CLASS:
  870. resolve_class_interface(static_cast<GDScriptParser::ClassNode *>(p_node));
  871. resolve_class_body(static_cast<GDScriptParser::ClassNode *>(p_node));
  872. break;
  873. case GDScriptParser::Node::CONSTANT:
  874. resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node));
  875. break;
  876. case GDScriptParser::Node::FOR:
  877. resolve_for(static_cast<GDScriptParser::ForNode *>(p_node));
  878. break;
  879. case GDScriptParser::Node::FUNCTION:
  880. resolve_function_signature(static_cast<GDScriptParser::FunctionNode *>(p_node));
  881. resolve_function_body(static_cast<GDScriptParser::FunctionNode *>(p_node));
  882. break;
  883. case GDScriptParser::Node::IF:
  884. resolve_if(static_cast<GDScriptParser::IfNode *>(p_node));
  885. break;
  886. case GDScriptParser::Node::SUITE:
  887. resolve_suite(static_cast<GDScriptParser::SuiteNode *>(p_node));
  888. break;
  889. case GDScriptParser::Node::VARIABLE:
  890. resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node));
  891. break;
  892. case GDScriptParser::Node::WHILE:
  893. resolve_while(static_cast<GDScriptParser::WhileNode *>(p_node));
  894. break;
  895. case GDScriptParser::Node::ANNOTATION:
  896. resolve_annotation(static_cast<GDScriptParser::AnnotationNode *>(p_node));
  897. break;
  898. case GDScriptParser::Node::ASSERT:
  899. resolve_assert(static_cast<GDScriptParser::AssertNode *>(p_node));
  900. break;
  901. case GDScriptParser::Node::MATCH:
  902. resolve_match(static_cast<GDScriptParser::MatchNode *>(p_node));
  903. break;
  904. case GDScriptParser::Node::MATCH_BRANCH:
  905. resolve_match_branch(static_cast<GDScriptParser::MatchBranchNode *>(p_node), nullptr);
  906. break;
  907. case GDScriptParser::Node::PARAMETER:
  908. resolve_parameter(static_cast<GDScriptParser::ParameterNode *>(p_node));
  909. break;
  910. case GDScriptParser::Node::PATTERN:
  911. resolve_match_pattern(static_cast<GDScriptParser::PatternNode *>(p_node), nullptr);
  912. break;
  913. case GDScriptParser::Node::RETURN:
  914. resolve_return(static_cast<GDScriptParser::ReturnNode *>(p_node));
  915. break;
  916. case GDScriptParser::Node::TYPE:
  917. resolve_datatype(static_cast<GDScriptParser::TypeNode *>(p_node));
  918. break;
  919. // Resolving expression is the same as reducing them.
  920. case GDScriptParser::Node::ARRAY:
  921. case GDScriptParser::Node::ASSIGNMENT:
  922. case GDScriptParser::Node::AWAIT:
  923. case GDScriptParser::Node::BINARY_OPERATOR:
  924. case GDScriptParser::Node::CALL:
  925. case GDScriptParser::Node::CAST:
  926. case GDScriptParser::Node::DICTIONARY:
  927. case GDScriptParser::Node::GET_NODE:
  928. case GDScriptParser::Node::IDENTIFIER:
  929. case GDScriptParser::Node::LAMBDA:
  930. case GDScriptParser::Node::LITERAL:
  931. case GDScriptParser::Node::PRELOAD:
  932. case GDScriptParser::Node::SELF:
  933. case GDScriptParser::Node::SUBSCRIPT:
  934. case GDScriptParser::Node::TERNARY_OPERATOR:
  935. case GDScriptParser::Node::UNARY_OPERATOR:
  936. reduce_expression(static_cast<GDScriptParser::ExpressionNode *>(p_node));
  937. break;
  938. case GDScriptParser::Node::BREAK:
  939. case GDScriptParser::Node::BREAKPOINT:
  940. case GDScriptParser::Node::CONTINUE:
  941. case GDScriptParser::Node::ENUM:
  942. case GDScriptParser::Node::PASS:
  943. case GDScriptParser::Node::SIGNAL:
  944. // Nothing to do.
  945. break;
  946. }
  947. }
  948. void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_annotation) {
  949. // TODO: Add second validation function for annotations, so they can use checked types.
  950. }
  951. void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function) {
  952. if (p_function->resolved_signature) {
  953. return;
  954. }
  955. p_function->resolved_signature = true;
  956. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  957. parser->current_function = p_function;
  958. for (int i = 0; i < p_function->parameters.size(); i++) {
  959. resolve_parameter(p_function->parameters[i]);
  960. #ifdef DEBUG_ENABLED
  961. if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) {
  962. parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name);
  963. }
  964. is_shadowing(p_function->parameters[i]->identifier, "function parameter");
  965. #endif // DEBUG_ENABLED
  966. #ifdef TOOLS_ENABLED
  967. if (p_function->parameters[i]->default_value && p_function->parameters[i]->default_value->is_constant) {
  968. p_function->default_arg_values.push_back(p_function->parameters[i]->default_value->reduced_value);
  969. }
  970. #endif // TOOLS_ENABLED
  971. }
  972. if (p_function->identifier->name == "_init") {
  973. // Constructor.
  974. GDScriptParser::DataType return_type = parser->current_class->get_datatype();
  975. return_type.is_meta_type = false;
  976. p_function->set_datatype(return_type);
  977. if (p_function->return_type) {
  978. GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);
  979. if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {
  980. push_error("Constructor cannot have an explicit return type.", p_function->return_type);
  981. }
  982. }
  983. } else {
  984. GDScriptParser::DataType return_type = resolve_datatype(p_function->return_type);
  985. p_function->set_datatype(return_type);
  986. }
  987. parser->current_function = previous_function;
  988. }
  989. void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) {
  990. if (p_function->resolved_body) {
  991. return;
  992. }
  993. p_function->resolved_body = true;
  994. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  995. parser->current_function = p_function;
  996. resolve_suite(p_function->body);
  997. GDScriptParser::DataType return_type = p_function->body->get_datatype();
  998. if (p_function->get_datatype().has_no_type() && return_type.is_set()) {
  999. // Use the suite inferred type if return isn't explicitly set.
  1000. return_type.type_source = GDScriptParser::DataType::INFERRED;
  1001. p_function->set_datatype(p_function->body->get_datatype());
  1002. } else if (p_function->get_datatype().is_hard_type() && (p_function->get_datatype().kind != GDScriptParser::DataType::BUILTIN || p_function->get_datatype().builtin_type != Variant::NIL)) {
  1003. if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) {
  1004. push_error(R"(Not all code paths return a value.)", p_function);
  1005. }
  1006. }
  1007. parser->current_function = previous_function;
  1008. }
  1009. void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) {
  1010. if (p_statement == nullptr) {
  1011. return;
  1012. }
  1013. switch (p_statement->type) {
  1014. case GDScriptParser::Node::IF:
  1015. case GDScriptParser::Node::FOR:
  1016. case GDScriptParser::Node::MATCH:
  1017. case GDScriptParser::Node::PATTERN:
  1018. case GDScriptParser::Node::RETURN:
  1019. case GDScriptParser::Node::WHILE:
  1020. // Use return or nested suite type as this suite type.
  1021. if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) {
  1022. // Mixed types.
  1023. // TODO: This could use the common supertype instead.
  1024. p_suite->datatype.kind = GDScriptParser::DataType::VARIANT;
  1025. p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  1026. } else {
  1027. p_suite->set_datatype(p_statement->get_datatype());
  1028. p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
  1029. }
  1030. break;
  1031. default:
  1032. break;
  1033. }
  1034. }
  1035. void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
  1036. for (int i = 0; i < p_suite->statements.size(); i++) {
  1037. GDScriptParser::Node *stmt = p_suite->statements[i];
  1038. resolve_node(stmt);
  1039. decide_suite_type(p_suite, stmt);
  1040. }
  1041. }
  1042. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  1043. reduce_expression(p_if->condition);
  1044. resolve_suite(p_if->true_block);
  1045. p_if->set_datatype(p_if->true_block->get_datatype());
  1046. if (p_if->false_block != nullptr) {
  1047. resolve_suite(p_if->false_block);
  1048. decide_suite_type(p_if, p_if->false_block);
  1049. }
  1050. }
  1051. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  1052. bool list_resolved = false;
  1053. // Optimize constant range() call to not allocate an array.
  1054. // Use int, Vector2, Vector3 instead, which also can be used as range iterators.
  1055. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  1056. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  1057. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1058. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1059. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  1060. if (callee->name == "range") {
  1061. list_resolved = true;
  1062. if (call->arguments.size() < 1) {
  1063. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  1064. } else if (call->arguments.size() > 3) {
  1065. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  1066. } else {
  1067. // Now we can optimize it.
  1068. bool all_is_constant = true;
  1069. Vector<Variant> args;
  1070. args.resize(call->arguments.size());
  1071. for (int i = 0; i < call->arguments.size(); i++) {
  1072. reduce_expression(call->arguments[i]);
  1073. if (!call->arguments[i]->is_constant) {
  1074. all_is_constant = false;
  1075. } else if (all_is_constant) {
  1076. args.write[i] = call->arguments[i]->reduced_value;
  1077. }
  1078. GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype();
  1079. if (!arg_type.is_variant()) {
  1080. if (arg_type.kind != GDScriptParser::DataType::BUILTIN) {
  1081. all_is_constant = false;
  1082. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]);
  1083. } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) {
  1084. all_is_constant = false;
  1085. push_error(vformat(R"*(Invalid argument for "range()" call. Argument %d should be int or float but "%s" was given.)*", i + 1, arg_type.to_string()), call->arguments[i]);
  1086. }
  1087. }
  1088. }
  1089. Variant reduced;
  1090. if (all_is_constant) {
  1091. switch (args.size()) {
  1092. case 1:
  1093. reduced = args[0];
  1094. break;
  1095. case 2:
  1096. reduced = Vector2i(args[0], args[1]);
  1097. break;
  1098. case 3:
  1099. reduced = Vector3i(args[0], args[1], args[2]);
  1100. break;
  1101. }
  1102. p_for->list->is_constant = true;
  1103. p_for->list->reduced_value = reduced;
  1104. }
  1105. }
  1106. if (p_for->list->is_constant) {
  1107. p_for->list->set_datatype(type_from_variant(p_for->list->reduced_value, p_for->list));
  1108. } else {
  1109. GDScriptParser::DataType list_type;
  1110. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1111. list_type.kind = GDScriptParser::DataType::BUILTIN;
  1112. list_type.builtin_type = Variant::ARRAY;
  1113. p_for->list->set_datatype(list_type);
  1114. }
  1115. }
  1116. }
  1117. }
  1118. GDScriptParser::DataType variable_type;
  1119. if (list_resolved) {
  1120. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1121. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1122. variable_type.builtin_type = Variant::INT; // Can this ever be a float or something else?
  1123. p_for->variable->set_datatype(variable_type);
  1124. } else if (p_for->list) {
  1125. resolve_node(p_for->list);
  1126. if (p_for->list->datatype.has_container_element_type()) {
  1127. variable_type = p_for->list->datatype.get_container_element_type();
  1128. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1129. } else if (p_for->list->datatype.is_typed_container_type()) {
  1130. variable_type = p_for->list->datatype.get_typed_container_type();
  1131. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1132. } else {
  1133. // Last resort
  1134. // TODO: Must other cases be handled? Must we mark as unsafe?
  1135. variable_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1136. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1137. }
  1138. }
  1139. if (p_for->variable) {
  1140. p_for->variable->set_datatype(variable_type);
  1141. }
  1142. resolve_suite(p_for->loop);
  1143. p_for->set_datatype(p_for->loop->get_datatype());
  1144. #ifdef DEBUG_ENABLED
  1145. if (p_for->variable) {
  1146. is_shadowing(p_for->variable, R"("for" iterator variable)");
  1147. }
  1148. #endif
  1149. }
  1150. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  1151. resolve_node(p_while->condition);
  1152. resolve_suite(p_while->loop);
  1153. p_while->set_datatype(p_while->loop->get_datatype());
  1154. }
  1155. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) {
  1156. GDScriptParser::DataType type;
  1157. type.kind = GDScriptParser::DataType::VARIANT; // By default.
  1158. GDScriptParser::DataType specified_type;
  1159. if (p_variable->datatype_specifier != nullptr) {
  1160. specified_type = resolve_datatype(p_variable->datatype_specifier);
  1161. specified_type.is_meta_type = false;
  1162. }
  1163. if (p_variable->initializer != nullptr) {
  1164. reduce_expression(p_variable->initializer);
  1165. if ((p_variable->infer_datatype || (p_variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && p_variable->initializer->type == GDScriptParser::Node::ARRAY) {
  1166. // Typed array.
  1167. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_variable->initializer);
  1168. // Can only infer typed array if it has elements.
  1169. if ((p_variable->infer_datatype && array->elements.size() > 0) || p_variable->datatype_specifier != nullptr) {
  1170. update_array_literal_element_type(specified_type, array);
  1171. }
  1172. }
  1173. type = p_variable->initializer->get_datatype();
  1174. if (p_variable->infer_datatype) {
  1175. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1176. if (type.has_no_type()) {
  1177. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value does not have a set type.)", p_variable->identifier->name), p_variable->initializer);
  1178. } else if (type.is_variant()) {
  1179. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is a variant. Use explicit "Variant" type if this is intended.)", p_variable->identifier->name), p_variable->initializer);
  1180. } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1181. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_variable->identifier->name), p_variable->initializer);
  1182. }
  1183. } else {
  1184. type.type_source = GDScriptParser::DataType::INFERRED;
  1185. }
  1186. #ifdef DEBUG_ENABLED
  1187. if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1188. parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_variable->initializer)->function_name);
  1189. }
  1190. #endif
  1191. }
  1192. if (p_variable->datatype_specifier != nullptr) {
  1193. type = specified_type;
  1194. type.is_meta_type = false;
  1195. if (p_variable->initializer != nullptr) {
  1196. if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true)) {
  1197. // Try reverse test since it can be a masked subtype.
  1198. if (!is_type_compatible(p_variable->initializer->get_datatype(), type, true)) {
  1199. push_error(vformat(R"(Value of type "%s" cannot be assigned to a variable of type "%s".)", p_variable->initializer->get_datatype().to_string(), type.to_string()), p_variable->initializer);
  1200. } else {
  1201. // TODO: Add warning.
  1202. mark_node_unsafe(p_variable->initializer);
  1203. p_variable->use_conversion_assign = true;
  1204. }
  1205. #ifdef DEBUG_ENABLED
  1206. } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  1207. parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1208. #endif
  1209. }
  1210. if (p_variable->initializer->get_datatype().is_variant()) {
  1211. // TODO: Warn unsafe assign.
  1212. mark_node_unsafe(p_variable->initializer);
  1213. p_variable->use_conversion_assign = true;
  1214. }
  1215. }
  1216. } else if (p_variable->infer_datatype) {
  1217. if (type.has_no_type()) {
  1218. push_error(vformat(R"(Cannot infer the type of variable "%s" because the initial value doesn't have a set type.)", p_variable->identifier->name), p_variable->identifier);
  1219. }
  1220. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1221. }
  1222. type.is_constant = false;
  1223. p_variable->set_datatype(type);
  1224. #ifdef DEBUG_ENABLED
  1225. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  1226. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  1227. } else if (p_variable->assignments == 0) {
  1228. parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
  1229. }
  1230. is_shadowing(p_variable->identifier, "variable");
  1231. #endif
  1232. }
  1233. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) {
  1234. GDScriptParser::DataType type;
  1235. if (p_constant->initializer != nullptr) {
  1236. reduce_expression(p_constant->initializer);
  1237. if (p_constant->initializer->type == GDScriptParser::Node::ARRAY) {
  1238. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_constant->initializer));
  1239. } else if (p_constant->initializer->type == GDScriptParser::Node::DICTIONARY) {
  1240. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_constant->initializer));
  1241. }
  1242. if (!p_constant->initializer->is_constant) {
  1243. push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer);
  1244. }
  1245. type = p_constant->initializer->get_datatype();
  1246. #ifdef DEBUG_ENABLED
  1247. if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1248. parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_constant->initializer)->function_name);
  1249. }
  1250. #endif
  1251. }
  1252. if (p_constant->datatype_specifier != nullptr) {
  1253. GDScriptParser::DataType explicit_type = resolve_datatype(p_constant->datatype_specifier);
  1254. explicit_type.is_meta_type = false;
  1255. if (!is_type_compatible(explicit_type, type)) {
  1256. push_error(vformat(R"(Assigned value for constant "%s" has type %s which is not compatible with defined type %s.)", p_constant->identifier->name, type.to_string(), explicit_type.to_string()), p_constant->initializer);
  1257. #ifdef DEBUG_ENABLED
  1258. } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) {
  1259. parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1260. #endif
  1261. }
  1262. type = explicit_type;
  1263. } else if (p_constant->infer_datatype) {
  1264. if (type.has_no_type()) {
  1265. push_error(vformat(R"(Cannot infer the type of constant "%s" because the initial value doesn't have a set type.)", p_constant->identifier->name), p_constant->identifier);
  1266. }
  1267. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1268. }
  1269. type.is_constant = true;
  1270. p_constant->set_datatype(type);
  1271. #ifdef DEBUG_ENABLED
  1272. if (p_constant->usages == 0) {
  1273. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1274. }
  1275. is_shadowing(p_constant->identifier, "constant");
  1276. #endif
  1277. }
  1278. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1279. reduce_expression(p_assert->condition);
  1280. if (p_assert->message != nullptr) {
  1281. reduce_expression(p_assert->message);
  1282. if (!p_assert->message->is_constant || p_assert->message->reduced_value.get_type() != Variant::STRING) {
  1283. push_error(R"(Expected constant string for assert error message.)", p_assert->message);
  1284. }
  1285. }
  1286. p_assert->set_datatype(p_assert->condition->get_datatype());
  1287. #ifdef DEBUG_ENABLED
  1288. if (p_assert->condition->is_constant) {
  1289. if (p_assert->condition->reduced_value.booleanize()) {
  1290. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1291. } else {
  1292. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1293. }
  1294. }
  1295. #endif
  1296. }
  1297. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1298. reduce_expression(p_match->test);
  1299. for (int i = 0; i < p_match->branches.size(); i++) {
  1300. resolve_match_branch(p_match->branches[i], p_match->test);
  1301. decide_suite_type(p_match, p_match->branches[i]);
  1302. }
  1303. }
  1304. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1305. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1306. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1307. }
  1308. resolve_suite(p_match_branch->block);
  1309. decide_suite_type(p_match_branch, p_match_branch->block);
  1310. }
  1311. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  1312. if (p_match_pattern == nullptr) {
  1313. return;
  1314. }
  1315. GDScriptParser::DataType result;
  1316. switch (p_match_pattern->pattern_type) {
  1317. case GDScriptParser::PatternNode::PT_LITERAL:
  1318. if (p_match_pattern->literal) {
  1319. reduce_literal(p_match_pattern->literal);
  1320. result = p_match_pattern->literal->get_datatype();
  1321. }
  1322. break;
  1323. case GDScriptParser::PatternNode::PT_EXPRESSION:
  1324. if (p_match_pattern->expression) {
  1325. reduce_expression(p_match_pattern->expression);
  1326. if (!p_match_pattern->expression->is_constant) {
  1327. push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression);
  1328. }
  1329. result = p_match_pattern->expression->get_datatype();
  1330. }
  1331. break;
  1332. case GDScriptParser::PatternNode::PT_BIND:
  1333. if (p_match_test != nullptr) {
  1334. result = p_match_test->get_datatype();
  1335. } else {
  1336. result.kind = GDScriptParser::DataType::VARIANT;
  1337. }
  1338. p_match_pattern->bind->set_datatype(result);
  1339. #ifdef DEBUG_ENABLED
  1340. is_shadowing(p_match_pattern->bind, "pattern bind");
  1341. if (p_match_pattern->bind->usages == 0) {
  1342. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNASSIGNED_VARIABLE, p_match_pattern->bind->name);
  1343. }
  1344. #endif
  1345. break;
  1346. case GDScriptParser::PatternNode::PT_ARRAY:
  1347. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  1348. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  1349. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  1350. }
  1351. result = p_match_pattern->get_datatype();
  1352. break;
  1353. case GDScriptParser::PatternNode::PT_DICTIONARY:
  1354. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  1355. if (p_match_pattern->dictionary[i].key) {
  1356. reduce_expression(p_match_pattern->dictionary[i].key);
  1357. if (!p_match_pattern->dictionary[i].key->is_constant) {
  1358. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->expression);
  1359. }
  1360. }
  1361. if (p_match_pattern->dictionary[i].value_pattern) {
  1362. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  1363. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  1364. }
  1365. }
  1366. result = p_match_pattern->get_datatype();
  1367. break;
  1368. case GDScriptParser::PatternNode::PT_WILDCARD:
  1369. case GDScriptParser::PatternNode::PT_REST:
  1370. result.kind = GDScriptParser::DataType::VARIANT;
  1371. break;
  1372. }
  1373. p_match_pattern->set_datatype(result);
  1374. }
  1375. void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) {
  1376. GDScriptParser::DataType result;
  1377. result.kind = GDScriptParser::DataType::VARIANT;
  1378. if (p_parameter->default_value != nullptr) {
  1379. reduce_expression(p_parameter->default_value);
  1380. result = p_parameter->default_value->get_datatype();
  1381. if (p_parameter->infer_datatype) {
  1382. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1383. } else {
  1384. result.type_source = GDScriptParser::DataType::INFERRED;
  1385. }
  1386. result.is_constant = false;
  1387. }
  1388. if (p_parameter->datatype_specifier != nullptr) {
  1389. result = resolve_datatype(p_parameter->datatype_specifier);
  1390. result.is_meta_type = false;
  1391. if (p_parameter->default_value != nullptr) {
  1392. if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) {
  1393. push_error(vformat(R"(Type of default value for parameter "%s" (%s) is not compatible with parameter type (%s).)", p_parameter->identifier->name, p_parameter->default_value->get_datatype().to_string(), p_parameter->datatype_specifier->get_datatype().to_string()), p_parameter->default_value);
  1394. } else if (p_parameter->default_value->get_datatype().is_variant()) {
  1395. mark_node_unsafe(p_parameter);
  1396. }
  1397. }
  1398. }
  1399. if (result.builtin_type == Variant::Type::NIL && result.type_source == GDScriptParser::DataType::ANNOTATED_INFERRED && p_parameter->datatype_specifier == nullptr) {
  1400. push_error(vformat(R"(Could not infer the type of the variable "%s" because the initial value is "null".)", p_parameter->identifier->name), p_parameter->default_value);
  1401. }
  1402. p_parameter->set_datatype(result);
  1403. }
  1404. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  1405. GDScriptParser::DataType result;
  1406. GDScriptParser::DataType expected_type;
  1407. bool has_expected_type = false;
  1408. if (parser->current_function != nullptr) {
  1409. expected_type = parser->current_function->get_datatype();
  1410. has_expected_type = true;
  1411. }
  1412. if (p_return->return_value != nullptr) {
  1413. reduce_expression(p_return->return_value);
  1414. if (p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1415. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1416. if (has_expected_type && expected_type.has_container_element_type() && p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1417. update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
  1418. }
  1419. }
  1420. result = p_return->return_value->get_datatype();
  1421. } else {
  1422. // Return type is null by default.
  1423. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1424. result.kind = GDScriptParser::DataType::BUILTIN;
  1425. result.builtin_type = Variant::NIL;
  1426. result.is_constant = true;
  1427. }
  1428. if (has_expected_type) {
  1429. expected_type.is_meta_type = false;
  1430. if (expected_type.is_hard_type()) {
  1431. if (!is_type_compatible(expected_type, result)) {
  1432. // Try other way. Okay but not safe.
  1433. if (!is_type_compatible(result, expected_type)) {
  1434. push_error(vformat(R"(Cannot return value of type "%s" because the function return type is "%s".)", result.to_string(), expected_type.to_string()), p_return);
  1435. } else {
  1436. // TODO: Add warning.
  1437. mark_node_unsafe(p_return);
  1438. }
  1439. #ifdef DEBUG_ENABLED
  1440. } else if (expected_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  1441. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  1442. } else if (result.is_variant()) {
  1443. mark_node_unsafe(p_return);
  1444. #endif
  1445. }
  1446. }
  1447. }
  1448. p_return->set_datatype(result);
  1449. }
  1450. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression) {
  1451. // This one makes some magic happen.
  1452. if (p_expression == nullptr) {
  1453. return;
  1454. }
  1455. if (p_expression->reduced) {
  1456. // Don't do this more than once.
  1457. return;
  1458. }
  1459. p_expression->reduced = true;
  1460. switch (p_expression->type) {
  1461. case GDScriptParser::Node::ARRAY:
  1462. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  1463. break;
  1464. case GDScriptParser::Node::ASSIGNMENT:
  1465. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  1466. break;
  1467. case GDScriptParser::Node::AWAIT:
  1468. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  1469. break;
  1470. case GDScriptParser::Node::BINARY_OPERATOR:
  1471. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  1472. break;
  1473. case GDScriptParser::Node::CALL:
  1474. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression));
  1475. break;
  1476. case GDScriptParser::Node::CAST:
  1477. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  1478. break;
  1479. case GDScriptParser::Node::DICTIONARY:
  1480. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  1481. break;
  1482. case GDScriptParser::Node::GET_NODE:
  1483. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  1484. break;
  1485. case GDScriptParser::Node::IDENTIFIER:
  1486. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  1487. break;
  1488. case GDScriptParser::Node::LAMBDA:
  1489. reduce_lambda(static_cast<GDScriptParser::LambdaNode *>(p_expression));
  1490. break;
  1491. case GDScriptParser::Node::LITERAL:
  1492. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  1493. break;
  1494. case GDScriptParser::Node::PRELOAD:
  1495. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  1496. break;
  1497. case GDScriptParser::Node::SELF:
  1498. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  1499. break;
  1500. case GDScriptParser::Node::SUBSCRIPT:
  1501. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  1502. break;
  1503. case GDScriptParser::Node::TERNARY_OPERATOR:
  1504. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression));
  1505. break;
  1506. case GDScriptParser::Node::UNARY_OPERATOR:
  1507. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  1508. break;
  1509. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  1510. case GDScriptParser::Node::NONE:
  1511. case GDScriptParser::Node::ANNOTATION:
  1512. case GDScriptParser::Node::ASSERT:
  1513. case GDScriptParser::Node::BREAK:
  1514. case GDScriptParser::Node::BREAKPOINT:
  1515. case GDScriptParser::Node::CLASS:
  1516. case GDScriptParser::Node::CONSTANT:
  1517. case GDScriptParser::Node::CONTINUE:
  1518. case GDScriptParser::Node::ENUM:
  1519. case GDScriptParser::Node::FOR:
  1520. case GDScriptParser::Node::FUNCTION:
  1521. case GDScriptParser::Node::IF:
  1522. case GDScriptParser::Node::MATCH:
  1523. case GDScriptParser::Node::MATCH_BRANCH:
  1524. case GDScriptParser::Node::PARAMETER:
  1525. case GDScriptParser::Node::PASS:
  1526. case GDScriptParser::Node::PATTERN:
  1527. case GDScriptParser::Node::RETURN:
  1528. case GDScriptParser::Node::SIGNAL:
  1529. case GDScriptParser::Node::SUITE:
  1530. case GDScriptParser::Node::TYPE:
  1531. case GDScriptParser::Node::VARIABLE:
  1532. case GDScriptParser::Node::WHILE:
  1533. ERR_FAIL_MSG("Reaching unreachable case");
  1534. }
  1535. }
  1536. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  1537. for (int i = 0; i < p_array->elements.size(); i++) {
  1538. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  1539. reduce_expression(element);
  1540. }
  1541. // It's array in any case.
  1542. GDScriptParser::DataType arr_type;
  1543. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1544. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  1545. arr_type.builtin_type = Variant::ARRAY;
  1546. arr_type.is_constant = true;
  1547. p_array->set_datatype(arr_type);
  1548. }
  1549. // When an array literal is stored (or passed as function argument) to a typed context, we then assume the array is typed.
  1550. // This function determines which type is that (if any).
  1551. void GDScriptAnalyzer::update_array_literal_element_type(const GDScriptParser::DataType &p_base_type, GDScriptParser::ArrayNode *p_array_literal) {
  1552. GDScriptParser::DataType array_type = p_array_literal->get_datatype();
  1553. if (p_array_literal->elements.size() == 0) {
  1554. // Empty array literal, just make the same type as the storage.
  1555. array_type.set_container_element_type(p_base_type.get_container_element_type());
  1556. } else {
  1557. // Check if elements match.
  1558. bool all_same_type = true;
  1559. bool all_have_type = true;
  1560. GDScriptParser::DataType element_type;
  1561. for (int i = 0; i < p_array_literal->elements.size(); i++) {
  1562. if (i == 0) {
  1563. element_type = p_array_literal->elements[0]->get_datatype();
  1564. } else {
  1565. GDScriptParser::DataType this_element_type = p_array_literal->elements[i]->get_datatype();
  1566. if (this_element_type.has_no_type()) {
  1567. all_same_type = false;
  1568. all_have_type = false;
  1569. break;
  1570. } else if (element_type != this_element_type) {
  1571. if (!is_type_compatible(element_type, this_element_type, false)) {
  1572. if (is_type_compatible(this_element_type, element_type, false)) {
  1573. // This element is a super-type to the previous type, so we use the super-type.
  1574. element_type = this_element_type;
  1575. } else {
  1576. // It's incompatible.
  1577. all_same_type = false;
  1578. break;
  1579. }
  1580. }
  1581. }
  1582. }
  1583. }
  1584. if (all_same_type) {
  1585. array_type.set_container_element_type(element_type);
  1586. } else if (all_have_type) {
  1587. push_error(vformat(R"(Variant array is not compatible with an array of type "%s".)", p_base_type.get_container_element_type().to_string()), p_array_literal);
  1588. }
  1589. }
  1590. // Update the type on the value itself.
  1591. p_array_literal->set_datatype(array_type);
  1592. }
  1593. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  1594. reduce_expression(p_assignment->assignee);
  1595. reduce_expression(p_assignment->assigned_value);
  1596. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  1597. return;
  1598. }
  1599. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  1600. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1601. if (assignee_type.has_container_element_type() && p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY) {
  1602. update_array_literal_element_type(assignee_type, static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value));
  1603. }
  1604. GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype();
  1605. if (assignee_type.is_constant) {
  1606. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  1607. }
  1608. if (!assignee_type.is_variant() && assigned_value_type.is_hard_type()) {
  1609. bool compatible = true;
  1610. GDScriptParser::DataType op_type = assigned_value_type;
  1611. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  1612. op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value);
  1613. }
  1614. if (compatible) {
  1615. compatible = is_type_compatible(assignee_type, op_type, true);
  1616. if (!compatible) {
  1617. if (assignee_type.is_hard_type()) {
  1618. // Try reverse test since it can be a masked subtype.
  1619. if (!is_type_compatible(op_type, assignee_type, true)) {
  1620. push_error(vformat(R"(Cannot assign a value of type "%s" to a target of type "%s".)", assigned_value_type.to_string(), assignee_type.to_string()), p_assignment->assigned_value);
  1621. } else {
  1622. // TODO: Add warning.
  1623. mark_node_unsafe(p_assignment);
  1624. p_assignment->use_conversion_assign = true;
  1625. }
  1626. } else {
  1627. // TODO: Warning in this case.
  1628. mark_node_unsafe(p_assignment);
  1629. }
  1630. }
  1631. } else {
  1632. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment);
  1633. }
  1634. }
  1635. if (assignee_type.has_no_type() || assigned_value_type.is_variant()) {
  1636. mark_node_unsafe(p_assignment);
  1637. if (assignee_type.is_hard_type()) {
  1638. p_assignment->use_conversion_assign = true;
  1639. }
  1640. }
  1641. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1642. // Change source type so it's not wrongly detected later.
  1643. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  1644. switch (identifier->source) {
  1645. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  1646. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1647. if (!id_type.is_hard_type()) {
  1648. id_type.kind = GDScriptParser::DataType::VARIANT;
  1649. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1650. identifier->variable_source->set_datatype(id_type);
  1651. }
  1652. } break;
  1653. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: {
  1654. GDScriptParser::DataType id_type = identifier->parameter_source->get_datatype();
  1655. if (!id_type.is_hard_type()) {
  1656. id_type.kind = GDScriptParser::DataType::VARIANT;
  1657. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1658. identifier->parameter_source->set_datatype(id_type);
  1659. }
  1660. } break;
  1661. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  1662. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1663. if (!id_type.is_hard_type()) {
  1664. id_type.kind = GDScriptParser::DataType::VARIANT;
  1665. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1666. identifier->variable_source->set_datatype(id_type);
  1667. }
  1668. } break;
  1669. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  1670. GDScriptParser::DataType id_type = identifier->bind_source->get_datatype();
  1671. if (!id_type.is_hard_type()) {
  1672. id_type.kind = GDScriptParser::DataType::VARIANT;
  1673. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1674. identifier->variable_source->set_datatype(id_type);
  1675. }
  1676. } break;
  1677. default:
  1678. // Nothing to do.
  1679. break;
  1680. }
  1681. }
  1682. #ifdef DEBUG_ENABLED
  1683. if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_value_type.kind == GDScriptParser::DataType::BUILTIN && assigned_value_type.builtin_type == Variant::NIL) {
  1684. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_assignment->assigned_value)->function_name);
  1685. } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) {
  1686. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  1687. }
  1688. #endif
  1689. }
  1690. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  1691. if (p_await->to_await == nullptr) {
  1692. GDScriptParser::DataType await_type;
  1693. await_type.kind = GDScriptParser::DataType::VARIANT;
  1694. p_await->set_datatype(await_type);
  1695. return;
  1696. }
  1697. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  1698. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  1699. } else {
  1700. reduce_expression(p_await->to_await);
  1701. }
  1702. p_await->is_constant = p_await->to_await->is_constant;
  1703. p_await->reduced_value = p_await->to_await->reduced_value;
  1704. GDScriptParser::DataType awaiting_type = p_await->to_await->get_datatype();
  1705. p_await->set_datatype(awaiting_type);
  1706. #ifdef DEBUG_ENABLED
  1707. if (!awaiting_type.is_coroutine && awaiting_type.builtin_type != Variant::SIGNAL) {
  1708. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  1709. }
  1710. #endif
  1711. }
  1712. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  1713. reduce_expression(p_binary_op->left_operand);
  1714. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) {
  1715. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_binary_op->right_operand), true);
  1716. } else {
  1717. reduce_expression(p_binary_op->right_operand);
  1718. }
  1719. // TODO: Right operand must be a valid type with the `is` operator. Need to check here.
  1720. GDScriptParser::DataType left_type;
  1721. if (p_binary_op->left_operand) {
  1722. left_type = p_binary_op->left_operand->get_datatype();
  1723. }
  1724. GDScriptParser::DataType right_type;
  1725. if (p_binary_op->right_operand) {
  1726. right_type = p_binary_op->right_operand->get_datatype();
  1727. }
  1728. if (!left_type.is_set() || !right_type.is_set()) {
  1729. return;
  1730. }
  1731. #ifdef DEBUG_ENABLED
  1732. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1733. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1734. }
  1735. #endif
  1736. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1737. p_binary_op->is_constant = true;
  1738. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1739. bool valid = false;
  1740. Variant::evaluate(p_binary_op->variant_op, p_binary_op->left_operand->reduced_value, p_binary_op->right_operand->reduced_value, p_binary_op->reduced_value, valid);
  1741. if (!valid) {
  1742. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1743. push_error(vformat(R"(%s in operator %s.)", p_binary_op->reduced_value, Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op);
  1744. } else {
  1745. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  1746. Variant::get_operator_name(p_binary_op->variant_op),
  1747. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1748. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1749. p_binary_op);
  1750. }
  1751. }
  1752. } else {
  1753. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1754. GDScriptParser::DataType test_type = right_type;
  1755. test_type.is_meta_type = false;
  1756. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1757. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1758. p_binary_op->reduced_value = false;
  1759. } else {
  1760. p_binary_op->reduced_value = true;
  1761. }
  1762. } else {
  1763. ERR_PRINT("Parser bug: unknown binary operation.");
  1764. }
  1765. }
  1766. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  1767. return;
  1768. }
  1769. GDScriptParser::DataType result;
  1770. if (left_type.is_variant() || right_type.is_variant()) {
  1771. // Cannot infer type because one operand can be anything.
  1772. result.kind = GDScriptParser::DataType::VARIANT;
  1773. mark_node_unsafe(p_binary_op);
  1774. } else {
  1775. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1776. bool valid = false;
  1777. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  1778. if (!valid) {
  1779. push_error(vformat(R"(Invalid operands "%s" and "%s" for "%s" operator.)", left_type.to_string(), right_type.to_string(), Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op);
  1780. }
  1781. } else {
  1782. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1783. GDScriptParser::DataType test_type = right_type;
  1784. test_type.is_meta_type = false;
  1785. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1786. // Test reverse as well to consider for subtypes.
  1787. if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) {
  1788. if (p_binary_op->left_operand->get_datatype().is_hard_type()) {
  1789. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)", p_binary_op->left_operand->get_datatype().to_string(), test_type.to_string()), p_binary_op->left_operand);
  1790. } else {
  1791. // TODO: Warning.
  1792. mark_node_unsafe(p_binary_op);
  1793. }
  1794. }
  1795. }
  1796. // "is" operator is always a boolean anyway.
  1797. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1798. result.kind = GDScriptParser::DataType::BUILTIN;
  1799. result.builtin_type = Variant::BOOL;
  1800. } else {
  1801. ERR_PRINT("Parser bug: unknown binary operation.");
  1802. }
  1803. }
  1804. }
  1805. p_binary_op->set_datatype(result);
  1806. }
  1807. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool is_await) {
  1808. bool all_is_constant = true;
  1809. Map<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  1810. for (int i = 0; i < p_call->arguments.size(); i++) {
  1811. reduce_expression(p_call->arguments[i]);
  1812. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  1813. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  1814. }
  1815. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  1816. }
  1817. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  1818. GDScriptParser::DataType call_type;
  1819. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  1820. // Call to name directly.
  1821. StringName function_name = p_call->function_name;
  1822. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  1823. if (builtin_type < Variant::VARIANT_MAX) {
  1824. // Is a builtin constructor.
  1825. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1826. call_type.kind = GDScriptParser::DataType::BUILTIN;
  1827. call_type.builtin_type = builtin_type;
  1828. if (builtin_type == Variant::OBJECT) {
  1829. call_type.kind = GDScriptParser::DataType::NATIVE;
  1830. call_type.native_type = function_name; // "Object".
  1831. }
  1832. bool safe_to_fold = true;
  1833. switch (builtin_type) {
  1834. // Those are stored by reference so not suited for compile-time construction.
  1835. // Because in this case they would be the same reference in all constructed values.
  1836. case Variant::OBJECT:
  1837. case Variant::DICTIONARY:
  1838. case Variant::ARRAY:
  1839. case Variant::PACKED_BYTE_ARRAY:
  1840. case Variant::PACKED_INT32_ARRAY:
  1841. case Variant::PACKED_INT64_ARRAY:
  1842. case Variant::PACKED_FLOAT32_ARRAY:
  1843. case Variant::PACKED_FLOAT64_ARRAY:
  1844. case Variant::PACKED_STRING_ARRAY:
  1845. case Variant::PACKED_VECTOR2_ARRAY:
  1846. case Variant::PACKED_VECTOR3_ARRAY:
  1847. case Variant::PACKED_COLOR_ARRAY:
  1848. safe_to_fold = false;
  1849. break;
  1850. default:
  1851. break;
  1852. }
  1853. if (all_is_constant && safe_to_fold) {
  1854. // Construct here.
  1855. Vector<const Variant *> args;
  1856. for (int i = 0; i < p_call->arguments.size(); i++) {
  1857. args.push_back(&(p_call->arguments[i]->reduced_value));
  1858. }
  1859. Callable::CallError err;
  1860. Variant value;
  1861. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  1862. switch (err.error) {
  1863. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  1864. push_error(vformat(R"(Invalid argument for %s constructor: argument %d should be %s but is %s.)", Variant::get_type_name(builtin_type), err.argument + 1,
  1865. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  1866. p_call->arguments[err.argument]);
  1867. break;
  1868. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1869. String signature = Variant::get_type_name(builtin_type) + "(";
  1870. for (int i = 0; i < p_call->arguments.size(); i++) {
  1871. if (i > 0) {
  1872. signature += ", ";
  1873. }
  1874. signature += p_call->arguments[i]->get_datatype().to_string();
  1875. }
  1876. signature += ")";
  1877. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  1878. } break;
  1879. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1880. push_error(vformat(R"(Too many arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call);
  1881. break;
  1882. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1883. push_error(vformat(R"(Too few arguments for %s constructor. Received %d but expected %d.)", Variant::get_type_name(builtin_type), p_call->arguments.size(), err.expected), p_call);
  1884. break;
  1885. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1886. break; // Can't happen in a builtin constructor.
  1887. case Callable::CallError::CALL_OK:
  1888. p_call->is_constant = true;
  1889. p_call->reduced_value = value;
  1890. break;
  1891. }
  1892. } else {
  1893. // TODO: Check constructors without constants.
  1894. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  1895. if (p_call->arguments.size() == 1) {
  1896. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  1897. if (arg_type.is_variant()) {
  1898. mark_node_unsafe(p_call->arguments[0]);
  1899. } else {
  1900. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  1901. // Okay.
  1902. p_call->set_datatype(call_type);
  1903. return;
  1904. }
  1905. }
  1906. }
  1907. List<MethodInfo> constructors;
  1908. Variant::get_constructor_list(builtin_type, &constructors);
  1909. bool match = false;
  1910. for (const MethodInfo &info : constructors) {
  1911. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  1912. continue;
  1913. }
  1914. if (p_call->arguments.size() > info.arguments.size()) {
  1915. continue;
  1916. }
  1917. bool types_match = true;
  1918. for (int i = 0; i < p_call->arguments.size(); i++) {
  1919. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  1920. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  1921. types_match = false;
  1922. break;
  1923. #ifdef DEBUG_ENABLED
  1924. } else {
  1925. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT) {
  1926. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  1927. }
  1928. #endif
  1929. }
  1930. }
  1931. if (types_match) {
  1932. match = true;
  1933. call_type = type_from_property(info.return_val);
  1934. break;
  1935. }
  1936. }
  1937. if (!match) {
  1938. String signature = Variant::get_type_name(builtin_type) + "(";
  1939. for (int i = 0; i < p_call->arguments.size(); i++) {
  1940. if (i > 0) {
  1941. signature += ", ";
  1942. }
  1943. signature += p_call->arguments[i]->get_datatype().to_string();
  1944. }
  1945. signature += ")";
  1946. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  1947. }
  1948. }
  1949. p_call->set_datatype(call_type);
  1950. return;
  1951. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  1952. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  1953. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  1954. // Can call on compilation.
  1955. Vector<const Variant *> args;
  1956. for (int i = 0; i < p_call->arguments.size(); i++) {
  1957. args.push_back(&(p_call->arguments[i]->reduced_value));
  1958. }
  1959. Variant value;
  1960. Callable::CallError err;
  1961. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  1962. switch (err.error) {
  1963. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1964. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  1965. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  1966. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  1967. p_call->arguments[err.argument]);
  1968. } break;
  1969. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  1970. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  1971. break;
  1972. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1973. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  1974. break;
  1975. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1976. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  1977. break;
  1978. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1979. break; // Can't happen in a builtin constructor.
  1980. case Callable::CallError::CALL_OK:
  1981. p_call->is_constant = true;
  1982. p_call->reduced_value = value;
  1983. break;
  1984. }
  1985. } else {
  1986. validate_call_arg(function_info, p_call);
  1987. }
  1988. p_call->set_datatype(type_from_property(function_info.return_val));
  1989. return;
  1990. } else if (Variant::has_utility_function(function_name)) {
  1991. MethodInfo function_info = info_from_utility_func(function_name);
  1992. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  1993. // Can call on compilation.
  1994. Vector<const Variant *> args;
  1995. for (int i = 0; i < p_call->arguments.size(); i++) {
  1996. args.push_back(&(p_call->arguments[i]->reduced_value));
  1997. }
  1998. Variant value;
  1999. Callable::CallError err;
  2000. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  2001. switch (err.error) {
  2002. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2003. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  2004. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2005. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  2006. p_call->arguments[err.argument]);
  2007. } break;
  2008. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2009. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2010. break;
  2011. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2012. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2013. break;
  2014. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2015. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", function_name, err.expected, p_call->arguments.size()), p_call);
  2016. break;
  2017. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2018. break; // Can't happen in a builtin constructor.
  2019. case Callable::CallError::CALL_OK:
  2020. p_call->is_constant = true;
  2021. p_call->reduced_value = value;
  2022. break;
  2023. }
  2024. } else {
  2025. validate_call_arg(function_info, p_call);
  2026. }
  2027. p_call->set_datatype(type_from_property(function_info.return_val));
  2028. return;
  2029. }
  2030. }
  2031. GDScriptParser::DataType base_type;
  2032. call_type.kind = GDScriptParser::DataType::VARIANT;
  2033. bool is_self = false;
  2034. if (p_call->is_super) {
  2035. base_type = parser->current_class->base_type;
  2036. base_type.is_meta_type = false;
  2037. is_self = true;
  2038. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2039. base_type = parser->current_class->get_datatype();
  2040. base_type.is_meta_type = false;
  2041. is_self = true;
  2042. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2043. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  2044. if (subscript->base == nullptr) {
  2045. // Invalid syntax, error already set on parser.
  2046. p_call->set_datatype(call_type);
  2047. mark_node_unsafe(p_call);
  2048. return;
  2049. }
  2050. if (!subscript->is_attribute) {
  2051. // Invalid call. Error already sent in parser.
  2052. // TODO: Could check if Callable here.
  2053. p_call->set_datatype(call_type);
  2054. mark_node_unsafe(p_call);
  2055. return;
  2056. }
  2057. if (subscript->attribute == nullptr) {
  2058. // Invalid call. Error already sent in parser.
  2059. p_call->set_datatype(call_type);
  2060. mark_node_unsafe(p_call);
  2061. return;
  2062. }
  2063. GDScriptParser::IdentifierNode *base_id = nullptr;
  2064. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2065. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2066. }
  2067. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2068. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2069. } else {
  2070. reduce_expression(subscript->base);
  2071. base_type = subscript->base->get_datatype();
  2072. }
  2073. } else {
  2074. // Invalid call. Error already sent in parser.
  2075. // TODO: Could check if Callable here too.
  2076. p_call->set_datatype(call_type);
  2077. mark_node_unsafe(p_call);
  2078. return;
  2079. }
  2080. bool is_static = false;
  2081. bool is_vararg = false;
  2082. int default_arg_count = 0;
  2083. GDScriptParser::DataType return_type;
  2084. List<GDScriptParser::DataType> par_types;
  2085. if (get_function_signature(p_call, base_type, p_call->function_name, return_type, par_types, default_arg_count, is_static, is_vararg)) {
  2086. // If the function require typed arrays we must make literals be typed.
  2087. for (const KeyValue<int, GDScriptParser::ArrayNode *> &E : arrays) {
  2088. int index = E.key;
  2089. if (index < par_types.size() && par_types[index].has_container_element_type()) {
  2090. update_array_literal_element_type(par_types[index], E.value);
  2091. }
  2092. }
  2093. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  2094. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  2095. push_error(vformat(R"*(Cannot call non-static function "%s()" from static function "%s()".)*", p_call->function_name, parser->current_function->identifier->name), p_call->callee);
  2096. } else if (!is_self && base_type.is_meta_type && !is_static) {
  2097. base_type.is_meta_type = false; // For `to_string()`.
  2098. push_error(vformat(R"*(Cannot call non-static function "%s()" on the class "%s" directly. Make an instance instead.)*", p_call->function_name, base_type.to_string()), p_call->callee);
  2099. } else if (is_self && !is_static && !lambda_stack.is_empty()) {
  2100. push_error(vformat(R"*(Cannot call non-static function "%s()" from a lambda function.)*", p_call->function_name), p_call->callee);
  2101. }
  2102. call_type = return_type;
  2103. } else {
  2104. // Check if the name exists as something else.
  2105. bool found = false;
  2106. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  2107. GDScriptParser::IdentifierNode *callee_id;
  2108. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2109. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  2110. } else {
  2111. // Can only be attribute.
  2112. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  2113. }
  2114. if (callee_id) {
  2115. reduce_identifier_from_base(callee_id, &base_type);
  2116. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  2117. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  2118. found = true;
  2119. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  2120. push_error(vformat(R"*(Name "%s" is a Callable. You can call it with "%s.call()" instead.)*", p_call->function_name, p_call->function_name), p_call->callee);
  2121. } else {
  2122. push_error(vformat(R"*(Name "%s" called as a function but is a "%s".)*", p_call->function_name, callee_datatype.to_string()), p_call->callee);
  2123. }
  2124. #ifdef DEBUG_ENABLED
  2125. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  2126. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  2127. mark_node_unsafe(p_call);
  2128. #endif
  2129. }
  2130. }
  2131. }
  2132. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  2133. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  2134. push_error(vformat(R"*(Function "%s()" not found in base %s.)*", p_call->function_name, base_name), p_call->is_super ? p_call : p_call->callee);
  2135. }
  2136. }
  2137. if (call_type.is_coroutine && !is_await) {
  2138. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call->callee);
  2139. }
  2140. p_call->set_datatype(call_type);
  2141. }
  2142. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  2143. reduce_expression(p_cast->operand);
  2144. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  2145. if (!cast_type.is_set()) {
  2146. return;
  2147. }
  2148. cast_type.is_meta_type = false; // The casted value won't be a type name.
  2149. p_cast->set_datatype(cast_type);
  2150. if (!cast_type.is_variant()) {
  2151. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  2152. if (!op_type.is_variant()) {
  2153. bool valid = false;
  2154. if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  2155. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  2156. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  2157. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  2158. }
  2159. if (!valid) {
  2160. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  2161. }
  2162. }
  2163. } else {
  2164. mark_node_unsafe(p_cast);
  2165. }
  2166. #ifdef DEBUG_ENABLED
  2167. if (p_cast->operand->get_datatype().is_variant()) {
  2168. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  2169. mark_node_unsafe(p_cast);
  2170. }
  2171. #endif
  2172. // TODO: Perform cast on constants.
  2173. }
  2174. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2175. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
  2176. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2177. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2178. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  2179. reduce_expression(element.key);
  2180. }
  2181. reduce_expression(element.value);
  2182. if (element.key->is_constant) {
  2183. if (elements.has(element.key->reduced_value)) {
  2184. push_error(vformat(R"(Key "%s" was already used in this dictionary (at line %d).)", element.key->reduced_value, elements[element.key->reduced_value]->start_line), element.key);
  2185. } else {
  2186. elements[element.key->reduced_value] = element.value;
  2187. }
  2188. }
  2189. }
  2190. // It's dictionary in any case.
  2191. GDScriptParser::DataType dict_type;
  2192. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2193. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  2194. dict_type.builtin_type = Variant::DICTIONARY;
  2195. dict_type.is_constant = true;
  2196. p_dictionary->set_datatype(dict_type);
  2197. }
  2198. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  2199. GDScriptParser::DataType result;
  2200. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2201. result.kind = GDScriptParser::DataType::NATIVE;
  2202. result.native_type = "Node";
  2203. result.builtin_type = Variant::OBJECT;
  2204. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, result.native_type)) {
  2205. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  2206. } else if (!lambda_stack.is_empty()) {
  2207. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") inside a lambda. Use a captured variable instead.)*", p_get_node);
  2208. }
  2209. p_get_node->set_datatype(result);
  2210. }
  2211. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  2212. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(p_class_name));
  2213. Error err = ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2214. if (err) {
  2215. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  2216. GDScriptParser::DataType type;
  2217. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2218. type.kind = GDScriptParser::DataType::VARIANT;
  2219. return type;
  2220. }
  2221. GDScriptParser::DataType type;
  2222. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2223. type.kind = GDScriptParser::DataType::CLASS;
  2224. type.builtin_type = Variant::OBJECT;
  2225. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  2226. type.class_type = ref->get_parser()->head;
  2227. type.script_path = ref->get_parser()->script_path;
  2228. type.is_constant = true;
  2229. type.is_meta_type = true;
  2230. return type;
  2231. }
  2232. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  2233. GDScriptParser::DataType base;
  2234. if (p_base == nullptr) {
  2235. base = type_from_metatype(parser->current_class->get_datatype());
  2236. } else {
  2237. base = *p_base;
  2238. }
  2239. const StringName &name = p_identifier->name;
  2240. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  2241. if (base.is_meta_type) {
  2242. bool valid = true;
  2243. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  2244. if (valid) {
  2245. p_identifier->is_constant = true;
  2246. p_identifier->reduced_value = result;
  2247. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  2248. } else if (base.is_hard_type()) {
  2249. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  2250. }
  2251. } else {
  2252. switch (base.builtin_type) {
  2253. case Variant::NIL: {
  2254. if (base.is_hard_type()) {
  2255. push_error(vformat(R"(Invalid get index "%s" on base Nil)", name), p_identifier);
  2256. }
  2257. return;
  2258. }
  2259. case Variant::DICTIONARY: {
  2260. GDScriptParser::DataType dummy;
  2261. dummy.kind = GDScriptParser::DataType::VARIANT;
  2262. p_identifier->set_datatype(dummy);
  2263. return;
  2264. }
  2265. default: {
  2266. Callable::CallError temp;
  2267. Variant dummy;
  2268. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  2269. List<PropertyInfo> properties;
  2270. dummy.get_property_list(&properties);
  2271. for (const PropertyInfo &prop : properties) {
  2272. if (prop.name == name) {
  2273. p_identifier->set_datatype(type_from_property(prop));
  2274. return;
  2275. }
  2276. }
  2277. if (base.is_hard_type()) {
  2278. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  2279. }
  2280. }
  2281. }
  2282. }
  2283. return;
  2284. }
  2285. if (base.kind == GDScriptParser::DataType::ENUM) {
  2286. if (base.is_meta_type) {
  2287. if (base.enum_values.has(name)) {
  2288. p_identifier->is_constant = true;
  2289. p_identifier->reduced_value = base.enum_values[name];
  2290. GDScriptParser::DataType result;
  2291. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2292. result.kind = GDScriptParser::DataType::ENUM_VALUE;
  2293. result.builtin_type = base.builtin_type;
  2294. result.native_type = base.native_type;
  2295. result.enum_type = name;
  2296. p_identifier->set_datatype(result);
  2297. } else {
  2298. // Consider as a Dictionary
  2299. GDScriptParser::DataType dummy;
  2300. dummy.kind = GDScriptParser::DataType::VARIANT;
  2301. p_identifier->set_datatype(dummy);
  2302. }
  2303. } else {
  2304. push_error(R"(Cannot get property from enum value.)", p_identifier);
  2305. }
  2306. return;
  2307. }
  2308. GDScriptParser::ClassNode *base_class = base.class_type;
  2309. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  2310. while (base_class != nullptr) {
  2311. if (base_class->identifier && base_class->identifier->name == name) {
  2312. p_identifier->set_datatype(base_class->get_datatype());
  2313. return;
  2314. }
  2315. if (base_class->has_member(name)) {
  2316. const GDScriptParser::ClassNode::Member &member = base_class->get_member(name);
  2317. p_identifier->set_datatype(member.get_datatype());
  2318. switch (member.type) {
  2319. case GDScriptParser::ClassNode::Member::CONSTANT:
  2320. // For out-of-order resolution:
  2321. reduce_expression(member.constant->initializer);
  2322. p_identifier->is_constant = true;
  2323. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2324. p_identifier->set_datatype(member.constant->initializer->get_datatype());
  2325. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2326. p_identifier->constant_source = member.constant;
  2327. break;
  2328. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2329. p_identifier->is_constant = true;
  2330. p_identifier->reduced_value = member.enum_value.value;
  2331. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2332. break;
  2333. case GDScriptParser::ClassNode::Member::VARIABLE:
  2334. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  2335. p_identifier->variable_source = member.variable;
  2336. member.variable->usages += 1;
  2337. break;
  2338. case GDScriptParser::ClassNode::Member::FUNCTION:
  2339. resolve_function_signature(member.function);
  2340. p_identifier->set_datatype(make_callable_type(member.function->info));
  2341. break;
  2342. case GDScriptParser::ClassNode::Member::CLASS:
  2343. // For out-of-order resolution:
  2344. resolve_class_interface(member.m_class);
  2345. p_identifier->set_datatype(member.m_class->get_datatype());
  2346. break;
  2347. default:
  2348. break; // Type already set.
  2349. }
  2350. return;
  2351. }
  2352. // Check outer constants.
  2353. // TODO: Allow outer static functions.
  2354. GDScriptParser::ClassNode *outer = base_class->outer;
  2355. while (outer != nullptr) {
  2356. if (outer->has_member(name)) {
  2357. const GDScriptParser::ClassNode::Member &member = outer->get_member(name);
  2358. switch (member.type) {
  2359. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2360. // TODO: Make sure loops won't cause problem. And make special error message for those.
  2361. // For out-of-order resolution:
  2362. reduce_expression(member.constant->initializer);
  2363. p_identifier->set_datatype(member.get_datatype());
  2364. p_identifier->is_constant = true;
  2365. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2366. return;
  2367. } break;
  2368. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2369. p_identifier->set_datatype(member.get_datatype());
  2370. p_identifier->is_constant = true;
  2371. p_identifier->reduced_value = member.enum_value.value;
  2372. return;
  2373. } break;
  2374. case GDScriptParser::ClassNode::Member::ENUM: {
  2375. p_identifier->set_datatype(member.get_datatype());
  2376. p_identifier->is_constant = false;
  2377. return;
  2378. } break;
  2379. default:
  2380. break;
  2381. }
  2382. }
  2383. outer = outer->outer;
  2384. }
  2385. base_class = base_class->base_type.class_type;
  2386. }
  2387. // Check native members.
  2388. const StringName &native = base.native_type;
  2389. if (class_exists(native)) {
  2390. MethodInfo method_info;
  2391. if (ClassDB::has_property(native, name)) {
  2392. StringName getter_name = ClassDB::get_property_getter(native, name);
  2393. MethodBind *getter = ClassDB::get_method(native, getter_name);
  2394. if (getter != nullptr) {
  2395. p_identifier->set_datatype(type_from_property(getter->get_return_info()));
  2396. }
  2397. return;
  2398. }
  2399. if (ClassDB::get_method_info(native, name, &method_info)) {
  2400. // Method is callable.
  2401. p_identifier->set_datatype(make_callable_type(method_info));
  2402. return;
  2403. }
  2404. if (ClassDB::get_signal(native, name, &method_info)) {
  2405. // Signal is a type too.
  2406. p_identifier->set_datatype(make_signal_type(method_info));
  2407. return;
  2408. }
  2409. if (ClassDB::has_enum(native, name)) {
  2410. p_identifier->set_datatype(make_native_enum_type(native, name));
  2411. return;
  2412. }
  2413. bool valid = false;
  2414. int int_constant = ClassDB::get_integer_constant(native, name, &valid);
  2415. if (valid) {
  2416. p_identifier->is_constant = true;
  2417. p_identifier->reduced_value = int_constant;
  2418. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  2419. return;
  2420. }
  2421. }
  2422. }
  2423. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  2424. // TODO: This is opportunity to further infer types.
  2425. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  2426. if (current_enum) {
  2427. for (int i = 0; i < current_enum->values.size(); i++) {
  2428. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  2429. if (element.identifier->name == p_identifier->name) {
  2430. GDScriptParser::DataType type;
  2431. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2432. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM_VALUE : GDScriptParser::DataType::BUILTIN;
  2433. type.builtin_type = Variant::INT;
  2434. type.is_constant = true;
  2435. if (element.parent_enum->identifier) {
  2436. type.enum_type = element.parent_enum->identifier->name;
  2437. }
  2438. p_identifier->set_datatype(type);
  2439. if (element.resolved) {
  2440. p_identifier->is_constant = true;
  2441. p_identifier->reduced_value = element.value;
  2442. } else {
  2443. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  2444. }
  2445. return; // Found anyway.
  2446. }
  2447. }
  2448. }
  2449. bool found_source = false;
  2450. // Check if identifier is local.
  2451. // If that's the case, the declaration already was solved before.
  2452. switch (p_identifier->source) {
  2453. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  2454. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  2455. found_source = true;
  2456. break;
  2457. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  2458. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2459. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  2460. p_identifier->is_constant = true;
  2461. // TODO: Constant should have a value on the node itself.
  2462. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  2463. found_source = true;
  2464. break;
  2465. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2466. p_identifier->variable_source->usages++;
  2467. [[fallthrough]];
  2468. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  2469. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  2470. found_source = true;
  2471. break;
  2472. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  2473. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  2474. found_source = true;
  2475. break;
  2476. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  2477. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  2478. result.is_constant = true;
  2479. p_identifier->set_datatype(result);
  2480. found_source = true;
  2481. } break;
  2482. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  2483. break;
  2484. }
  2485. // Not a local, so check members.
  2486. if (!found_source) {
  2487. reduce_identifier_from_base(p_identifier);
  2488. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  2489. // Found.
  2490. found_source = true;
  2491. }
  2492. }
  2493. if (found_source) {
  2494. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  2495. // Only capture locals and members and enum values. Constants are still accessible from the lambda using the script reference.
  2496. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT || lambda_stack.is_empty()) {
  2497. return;
  2498. }
  2499. GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
  2500. while (function_test != nullptr && function_test != p_identifier->source_function && function_test->source_lambda != nullptr && !function_test->source_lambda->captures_indices.has(p_identifier->name)) {
  2501. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  2502. function_test->source_lambda->captures.push_back(p_identifier);
  2503. function_test = function_test->source_lambda->parent_function;
  2504. }
  2505. return;
  2506. }
  2507. StringName name = p_identifier->name;
  2508. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2509. // Check globals. We make an exception for Variant::OBJECT because it's the base class for
  2510. // non-builtin types so we allow doing e.g. Object.new()
  2511. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  2512. if (builtin_type != Variant::OBJECT && builtin_type < Variant::VARIANT_MAX) {
  2513. if (can_be_builtin) {
  2514. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  2515. return;
  2516. } else {
  2517. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2518. }
  2519. }
  2520. if (class_exists(name)) {
  2521. p_identifier->set_datatype(make_native_meta_type(name));
  2522. return;
  2523. }
  2524. if (ScriptServer::is_global_class(name)) {
  2525. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  2526. return;
  2527. }
  2528. // Try singletons.
  2529. // Do this before globals because this might be a singleton loading another one before it's compiled.
  2530. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  2531. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  2532. if (autoload.is_singleton) {
  2533. // Singleton exists, so it's at least a Node.
  2534. GDScriptParser::DataType result;
  2535. result.kind = GDScriptParser::DataType::NATIVE;
  2536. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2537. if (autoload.path.to_lower().ends_with(GDScriptLanguage::get_singleton()->get_extension())) {
  2538. Ref<GDScriptParserRef> parser = get_parser_for(autoload.path);
  2539. if (parser.is_valid()) {
  2540. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2541. if (err == OK) {
  2542. result = type_from_metatype(parser->get_parser()->head->get_datatype());
  2543. }
  2544. }
  2545. }
  2546. result.is_constant = true;
  2547. p_identifier->set_datatype(result);
  2548. return;
  2549. }
  2550. }
  2551. if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) {
  2552. int idx = GDScriptLanguage::get_singleton()->get_global_map()[name];
  2553. Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  2554. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2555. p_identifier->is_constant = true;
  2556. p_identifier->reduced_value = constant;
  2557. return;
  2558. }
  2559. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) {
  2560. Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name];
  2561. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2562. p_identifier->is_constant = true;
  2563. p_identifier->reduced_value = constant;
  2564. return;
  2565. }
  2566. // Not found.
  2567. // Check if it's a builtin function.
  2568. if (GDScriptUtilityFunctions::function_exists(name)) {
  2569. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2570. } else {
  2571. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2572. }
  2573. GDScriptParser::DataType dummy;
  2574. dummy.kind = GDScriptParser::DataType::VARIANT;
  2575. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2576. }
  2577. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  2578. // Lambda is always a Callable.
  2579. GDScriptParser::DataType lambda_type;
  2580. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  2581. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  2582. lambda_type.builtin_type = Variant::CALLABLE;
  2583. p_lambda->set_datatype(lambda_type);
  2584. if (p_lambda->function == nullptr) {
  2585. return;
  2586. }
  2587. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  2588. parser->current_function = p_lambda->function;
  2589. lambda_stack.push_back(p_lambda);
  2590. for (int i = 0; i < p_lambda->function->parameters.size(); i++) {
  2591. resolve_parameter(p_lambda->function->parameters[i]);
  2592. }
  2593. resolve_suite(p_lambda->function->body);
  2594. int captures_amount = p_lambda->captures.size();
  2595. if (captures_amount > 0) {
  2596. // Create space for lambda parameters.
  2597. // At the beginning to not mess with optional parameters.
  2598. int param_count = p_lambda->function->parameters.size();
  2599. p_lambda->function->parameters.resize(param_count + captures_amount);
  2600. for (int i = param_count - 1; i >= 0; i--) {
  2601. p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
  2602. p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  2603. }
  2604. // Add captures as extra parameters at the beginning.
  2605. for (int i = 0; i < p_lambda->captures.size(); i++) {
  2606. GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
  2607. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  2608. capture_param->identifier = capture;
  2609. capture_param->usages = capture->usages;
  2610. capture_param->set_datatype(capture->get_datatype());
  2611. p_lambda->function->parameters.write[i] = capture_param;
  2612. p_lambda->function->parameters_indices[capture->name] = i;
  2613. }
  2614. }
  2615. lambda_stack.pop_back();
  2616. parser->current_function = previous_function;
  2617. }
  2618. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2619. p_literal->reduced_value = p_literal->value;
  2620. p_literal->is_constant = true;
  2621. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  2622. }
  2623. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2624. if (!p_preload->path) {
  2625. return;
  2626. }
  2627. reduce_expression(p_preload->path);
  2628. if (!p_preload->path->is_constant) {
  2629. push_error("Preloaded path must be a constant string.", p_preload->path);
  2630. return;
  2631. }
  2632. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2633. push_error("Preloaded path must be a constant string.", p_preload->path);
  2634. } else {
  2635. p_preload->resolved_path = p_preload->path->reduced_value;
  2636. // TODO: Save this as script dependency.
  2637. if (p_preload->resolved_path.is_relative_path()) {
  2638. p_preload->resolved_path = parser->script_path.get_base_dir().plus_file(p_preload->resolved_path);
  2639. }
  2640. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2641. if (!FileAccess::exists(p_preload->resolved_path)) {
  2642. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2643. } else {
  2644. // TODO: Don't load if validating: use completion cache.
  2645. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2646. if (p_preload->resource.is_null()) {
  2647. push_error(vformat(R"(Could not p_preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2648. }
  2649. }
  2650. }
  2651. p_preload->is_constant = true;
  2652. p_preload->reduced_value = p_preload->resource;
  2653. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  2654. }
  2655. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2656. p_self->is_constant = false;
  2657. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2658. }
  2659. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2660. if (p_subscript->base == nullptr) {
  2661. return;
  2662. }
  2663. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2664. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  2665. } else {
  2666. reduce_expression(p_subscript->base);
  2667. }
  2668. GDScriptParser::DataType result_type;
  2669. if (p_subscript->is_attribute) {
  2670. if (p_subscript->attribute == nullptr) {
  2671. return;
  2672. }
  2673. if (p_subscript->base->is_constant) {
  2674. // Just try to get it.
  2675. bool valid = false;
  2676. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  2677. if (!valid) {
  2678. push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index);
  2679. } else {
  2680. p_subscript->is_constant = true;
  2681. p_subscript->reduced_value = value;
  2682. result_type = type_from_variant(value, p_subscript);
  2683. }
  2684. result_type.kind = GDScriptParser::DataType::VARIANT;
  2685. } else {
  2686. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2687. if (base_type.is_variant() || !base_type.is_hard_type()) {
  2688. result_type.kind = GDScriptParser::DataType::VARIANT;
  2689. mark_node_unsafe(p_subscript);
  2690. } else {
  2691. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  2692. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  2693. if (attr_type.is_set()) {
  2694. result_type = attr_type;
  2695. p_subscript->is_constant = p_subscript->attribute->is_constant;
  2696. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  2697. } else {
  2698. if (base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2699. push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute);
  2700. #ifdef DEBUG_ENABLED
  2701. } else {
  2702. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  2703. #endif
  2704. }
  2705. result_type.kind = GDScriptParser::DataType::VARIANT;
  2706. }
  2707. }
  2708. }
  2709. } else {
  2710. if (p_subscript->index == nullptr) {
  2711. return;
  2712. }
  2713. reduce_expression(p_subscript->index);
  2714. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  2715. // Just try to get it.
  2716. bool valid = false;
  2717. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  2718. if (!valid) {
  2719. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  2720. } else {
  2721. p_subscript->is_constant = true;
  2722. p_subscript->reduced_value = value;
  2723. result_type = type_from_variant(value, p_subscript);
  2724. }
  2725. result_type.kind = GDScriptParser::DataType::VARIANT;
  2726. } else {
  2727. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2728. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  2729. if (base_type.is_variant()) {
  2730. result_type.kind = GDScriptParser::DataType::VARIANT;
  2731. mark_node_unsafe(p_subscript);
  2732. } else {
  2733. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2734. // Check if indexing is valid.
  2735. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  2736. if (!error) {
  2737. switch (base_type.builtin_type) {
  2738. // Expect int or real as index.
  2739. case Variant::PACKED_BYTE_ARRAY:
  2740. case Variant::PACKED_COLOR_ARRAY:
  2741. case Variant::PACKED_FLOAT32_ARRAY:
  2742. case Variant::PACKED_FLOAT64_ARRAY:
  2743. case Variant::PACKED_INT32_ARRAY:
  2744. case Variant::PACKED_INT64_ARRAY:
  2745. case Variant::PACKED_STRING_ARRAY:
  2746. case Variant::PACKED_VECTOR2_ARRAY:
  2747. case Variant::PACKED_VECTOR3_ARRAY:
  2748. case Variant::ARRAY:
  2749. case Variant::STRING:
  2750. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  2751. break;
  2752. // Expect String only.
  2753. case Variant::RECT2:
  2754. case Variant::RECT2I:
  2755. case Variant::PLANE:
  2756. case Variant::QUATERNION:
  2757. case Variant::AABB:
  2758. case Variant::OBJECT:
  2759. error = index_type.builtin_type != Variant::STRING;
  2760. break;
  2761. // Expect String or number.
  2762. case Variant::BASIS:
  2763. case Variant::VECTOR2:
  2764. case Variant::VECTOR2I:
  2765. case Variant::VECTOR3:
  2766. case Variant::VECTOR3I:
  2767. case Variant::TRANSFORM2D:
  2768. case Variant::TRANSFORM3D:
  2769. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  2770. index_type.builtin_type != Variant::STRING;
  2771. break;
  2772. // Expect String or int.
  2773. case Variant::COLOR:
  2774. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  2775. break;
  2776. // Don't support indexing, but we will check it later.
  2777. case Variant::RID:
  2778. case Variant::BOOL:
  2779. case Variant::CALLABLE:
  2780. case Variant::FLOAT:
  2781. case Variant::INT:
  2782. case Variant::NIL:
  2783. case Variant::NODE_PATH:
  2784. case Variant::SIGNAL:
  2785. case Variant::STRING_NAME:
  2786. break;
  2787. // Here for completeness.
  2788. case Variant::DICTIONARY:
  2789. case Variant::VARIANT_MAX:
  2790. break;
  2791. }
  2792. if (error) {
  2793. push_error(vformat(R"(Invalid index type "%s" for a base of type "%s".)", index_type.to_string(), base_type.to_string()), p_subscript->index);
  2794. }
  2795. }
  2796. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2797. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  2798. push_error(vformat(R"(Only String or StringName can be used as index for type "%s", but received a "%s".)", base_type.to_string(), index_type.to_string()), p_subscript->index);
  2799. }
  2800. }
  2801. // Check resulting type if possible.
  2802. result_type.builtin_type = Variant::NIL;
  2803. result_type.kind = GDScriptParser::DataType::BUILTIN;
  2804. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  2805. if (base_type.kind != GDScriptParser::DataType::BUILTIN) {
  2806. base_type.builtin_type = Variant::OBJECT;
  2807. }
  2808. switch (base_type.builtin_type) {
  2809. // Can't index at all.
  2810. case Variant::RID:
  2811. case Variant::BOOL:
  2812. case Variant::CALLABLE:
  2813. case Variant::FLOAT:
  2814. case Variant::INT:
  2815. case Variant::NIL:
  2816. case Variant::NODE_PATH:
  2817. case Variant::SIGNAL:
  2818. case Variant::STRING_NAME:
  2819. result_type.kind = GDScriptParser::DataType::VARIANT;
  2820. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  2821. break;
  2822. // Return int.
  2823. case Variant::PACKED_BYTE_ARRAY:
  2824. case Variant::PACKED_INT32_ARRAY:
  2825. case Variant::PACKED_INT64_ARRAY:
  2826. case Variant::VECTOR2I:
  2827. case Variant::VECTOR3I:
  2828. result_type.builtin_type = Variant::INT;
  2829. break;
  2830. // Return float.
  2831. case Variant::PACKED_FLOAT32_ARRAY:
  2832. case Variant::PACKED_FLOAT64_ARRAY:
  2833. case Variant::VECTOR2:
  2834. case Variant::VECTOR3:
  2835. case Variant::QUATERNION:
  2836. result_type.builtin_type = Variant::FLOAT;
  2837. break;
  2838. // Return Color.
  2839. case Variant::PACKED_COLOR_ARRAY:
  2840. result_type.builtin_type = Variant::COLOR;
  2841. break;
  2842. // Return String.
  2843. case Variant::PACKED_STRING_ARRAY:
  2844. case Variant::STRING:
  2845. result_type.builtin_type = Variant::STRING;
  2846. break;
  2847. // Return Vector2.
  2848. case Variant::PACKED_VECTOR2_ARRAY:
  2849. case Variant::TRANSFORM2D:
  2850. case Variant::RECT2:
  2851. result_type.builtin_type = Variant::VECTOR2;
  2852. break;
  2853. // Return Vector2I.
  2854. case Variant::RECT2I:
  2855. result_type.builtin_type = Variant::VECTOR2I;
  2856. break;
  2857. // Return Vector3.
  2858. case Variant::PACKED_VECTOR3_ARRAY:
  2859. case Variant::AABB:
  2860. case Variant::BASIS:
  2861. result_type.builtin_type = Variant::VECTOR3;
  2862. break;
  2863. // Depends on the index.
  2864. case Variant::TRANSFORM3D:
  2865. case Variant::PLANE:
  2866. case Variant::COLOR:
  2867. case Variant::DICTIONARY:
  2868. case Variant::OBJECT:
  2869. result_type.kind = GDScriptParser::DataType::VARIANT;
  2870. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2871. break;
  2872. // Can have an element type.
  2873. case Variant::ARRAY:
  2874. if (base_type.has_container_element_type()) {
  2875. result_type = base_type.get_container_element_type();
  2876. result_type.type_source = base_type.type_source;
  2877. } else {
  2878. result_type.kind = GDScriptParser::DataType::VARIANT;
  2879. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2880. }
  2881. break;
  2882. // Here for completeness.
  2883. case Variant::VARIANT_MAX:
  2884. break;
  2885. }
  2886. }
  2887. }
  2888. }
  2889. p_subscript->set_datatype(result_type);
  2890. }
  2891. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  2892. reduce_expression(p_ternary_op->condition);
  2893. reduce_expression(p_ternary_op->true_expr);
  2894. reduce_expression(p_ternary_op->false_expr);
  2895. GDScriptParser::DataType result;
  2896. if (p_ternary_op->condition && p_ternary_op->condition->is_constant && p_ternary_op->true_expr->is_constant && p_ternary_op->false_expr && p_ternary_op->false_expr->is_constant) {
  2897. p_ternary_op->is_constant = true;
  2898. if (p_ternary_op->condition->reduced_value.booleanize()) {
  2899. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  2900. } else {
  2901. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  2902. }
  2903. }
  2904. GDScriptParser::DataType true_type;
  2905. if (p_ternary_op->true_expr) {
  2906. true_type = p_ternary_op->true_expr->get_datatype();
  2907. } else {
  2908. true_type.kind = GDScriptParser::DataType::VARIANT;
  2909. }
  2910. GDScriptParser::DataType false_type;
  2911. if (p_ternary_op->false_expr) {
  2912. false_type = p_ternary_op->false_expr->get_datatype();
  2913. } else {
  2914. false_type.kind = GDScriptParser::DataType::VARIANT;
  2915. }
  2916. if (true_type.is_variant() || false_type.is_variant()) {
  2917. result.kind = GDScriptParser::DataType::VARIANT;
  2918. } else {
  2919. result = true_type;
  2920. if (!is_type_compatible(true_type, false_type)) {
  2921. result = false_type;
  2922. if (!is_type_compatible(false_type, true_type)) {
  2923. result.type_source = GDScriptParser::DataType::UNDETECTED;
  2924. result.kind = GDScriptParser::DataType::VARIANT;
  2925. #ifdef DEBUG_ENABLED
  2926. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  2927. #endif
  2928. }
  2929. }
  2930. }
  2931. p_ternary_op->set_datatype(result);
  2932. }
  2933. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  2934. reduce_expression(p_unary_op->operand);
  2935. GDScriptParser::DataType result;
  2936. if (p_unary_op->operand == nullptr) {
  2937. result.kind = GDScriptParser::DataType::VARIANT;
  2938. p_unary_op->set_datatype(result);
  2939. return;
  2940. }
  2941. if (p_unary_op->operand->is_constant) {
  2942. p_unary_op->is_constant = true;
  2943. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  2944. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  2945. } else if (p_unary_op->operand->get_datatype().is_variant()) {
  2946. result.kind = GDScriptParser::DataType::VARIANT;
  2947. mark_node_unsafe(p_unary_op);
  2948. } else {
  2949. bool valid = false;
  2950. result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), valid, p_unary_op);
  2951. if (!valid) {
  2952. push_error(vformat(R"(Invalid operand of type "%s" for unary operator "%s".)", p_unary_op->operand->get_datatype().to_string(), Variant::get_operator_name(p_unary_op->variant_op)), p_unary_op->operand);
  2953. }
  2954. }
  2955. p_unary_op->set_datatype(result);
  2956. }
  2957. void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
  2958. bool all_is_constant = true;
  2959. for (int i = 0; i < p_array->elements.size(); i++) {
  2960. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  2961. all_is_constant = all_is_constant && element->is_constant;
  2962. if (!all_is_constant) {
  2963. return;
  2964. }
  2965. }
  2966. Array array;
  2967. array.resize(p_array->elements.size());
  2968. for (int i = 0; i < p_array->elements.size(); i++) {
  2969. array[i] = p_array->elements[i]->reduced_value;
  2970. }
  2971. p_array->is_constant = true;
  2972. p_array->reduced_value = array;
  2973. }
  2974. void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2975. bool all_is_constant = true;
  2976. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2977. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2978. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  2979. if (!all_is_constant) {
  2980. return;
  2981. }
  2982. }
  2983. Dictionary dict;
  2984. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2985. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2986. dict[element.key->reduced_value] = element.value->reduced_value;
  2987. }
  2988. p_dictionary->is_constant = true;
  2989. p_dictionary->reduced_value = dict;
  2990. }
  2991. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  2992. GDScriptParser::DataType result;
  2993. result.is_constant = true;
  2994. result.kind = GDScriptParser::DataType::BUILTIN;
  2995. result.builtin_type = p_value.get_type();
  2996. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  2997. if (p_value.get_type() == Variant::OBJECT) {
  2998. Object *obj = p_value;
  2999. if (!obj) {
  3000. return GDScriptParser::DataType();
  3001. }
  3002. result.native_type = obj->get_class_name();
  3003. Ref<Script> scr = p_value; // Check if value is a script itself.
  3004. if (scr.is_valid()) {
  3005. result.is_meta_type = true;
  3006. } else {
  3007. result.is_meta_type = false;
  3008. scr = obj->get_script();
  3009. }
  3010. if (scr.is_valid()) {
  3011. if (scr->is_valid()) {
  3012. result.script_type = scr;
  3013. result.script_path = scr->get_path();
  3014. Ref<GDScript> gds = scr;
  3015. if (gds.is_valid()) {
  3016. result.kind = GDScriptParser::DataType::CLASS;
  3017. // This might be an inner class, so we want to get the parser for the root.
  3018. // But still get the inner class from that tree.
  3019. GDScript *current = gds.ptr();
  3020. List<StringName> class_chain;
  3021. while (current->_owner) {
  3022. // Push to front so it's in reverse.
  3023. class_chain.push_front(current->name);
  3024. current = current->_owner;
  3025. }
  3026. Ref<GDScriptParserRef> ref = get_parser_for(current->get_path());
  3027. if (ref.is_null()) {
  3028. push_error("Could not find script in path.", p_source);
  3029. GDScriptParser::DataType error_type;
  3030. error_type.kind = GDScriptParser::DataType::VARIANT;
  3031. return error_type;
  3032. }
  3033. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  3034. GDScriptParser::ClassNode *found = ref->get_parser()->head;
  3035. // It should be okay to assume this exists, since we have a complete script already.
  3036. for (const StringName &E : class_chain) {
  3037. found = found->get_member(E).m_class;
  3038. }
  3039. result.class_type = found;
  3040. result.script_path = ref->get_parser()->script_path;
  3041. } else {
  3042. result.kind = GDScriptParser::DataType::SCRIPT;
  3043. }
  3044. result.native_type = scr->get_instance_base_type();
  3045. } else {
  3046. push_error(vformat(R"(Constant value uses script from "%s" which is loaded but not compiled.)", scr->get_path()), p_source);
  3047. result.kind = GDScriptParser::DataType::VARIANT;
  3048. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3049. result.is_meta_type = false;
  3050. }
  3051. } else {
  3052. result.kind = GDScriptParser::DataType::NATIVE;
  3053. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  3054. result.is_meta_type = true;
  3055. }
  3056. }
  3057. }
  3058. return result;
  3059. }
  3060. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  3061. GDScriptParser::DataType result = p_meta_type;
  3062. result.is_meta_type = false;
  3063. result.is_constant = false;
  3064. return result;
  3065. }
  3066. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  3067. GDScriptParser::DataType result;
  3068. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3069. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  3070. // Variant
  3071. result.kind = GDScriptParser::DataType::VARIANT;
  3072. return result;
  3073. }
  3074. result.builtin_type = p_property.type;
  3075. if (p_property.type == Variant::OBJECT) {
  3076. result.kind = GDScriptParser::DataType::NATIVE;
  3077. result.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  3078. } else {
  3079. result.kind = GDScriptParser::DataType::BUILTIN;
  3080. result.builtin_type = p_property.type;
  3081. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  3082. // Check element type.
  3083. StringName elem_type_name = p_property.hint_string;
  3084. GDScriptParser::DataType elem_type;
  3085. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3086. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  3087. if (elem_builtin_type < Variant::VARIANT_MAX) {
  3088. // Builtin type.
  3089. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  3090. elem_type.builtin_type = elem_builtin_type;
  3091. } else if (class_exists(elem_type_name)) {
  3092. elem_type.kind = GDScriptParser::DataType::NATIVE;
  3093. elem_type.builtin_type = Variant::OBJECT;
  3094. elem_type.native_type = p_property.hint_string;
  3095. } else if (ScriptServer::is_global_class(elem_type_name)) {
  3096. // Just load this as it shouldn't be a GDScript.
  3097. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  3098. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  3099. elem_type.builtin_type = Variant::OBJECT;
  3100. elem_type.native_type = script->get_instance_base_type();
  3101. elem_type.script_type = script;
  3102. } else {
  3103. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  3104. }
  3105. result.set_container_element_type(elem_type);
  3106. }
  3107. }
  3108. return result;
  3109. }
  3110. bool GDScriptAnalyzer::get_function_signature(GDScriptParser::CallNode *p_source, GDScriptParser::DataType p_base_type, const StringName &p_function, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) {
  3111. r_static = false;
  3112. r_vararg = false;
  3113. r_default_arg_count = 0;
  3114. StringName function_name = p_function;
  3115. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  3116. // Construct a base type to get methods.
  3117. Callable::CallError err;
  3118. Variant dummy;
  3119. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  3120. if (err.error != Callable::CallError::CALL_OK) {
  3121. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  3122. }
  3123. List<MethodInfo> methods;
  3124. dummy.get_method_list(&methods);
  3125. for (const MethodInfo &E : methods) {
  3126. if (E.name == p_function) {
  3127. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3128. r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name);
  3129. return true;
  3130. }
  3131. }
  3132. return false;
  3133. }
  3134. bool is_constructor = (p_base_type.is_meta_type || (p_source->callee && p_source->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_function == StaticCString::create("new");
  3135. if (is_constructor) {
  3136. function_name = "_init";
  3137. r_static = true;
  3138. }
  3139. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  3140. GDScriptParser::FunctionNode *found_function = nullptr;
  3141. while (found_function == nullptr && base_class != nullptr) {
  3142. if (base_class->has_member(function_name)) {
  3143. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3144. // TODO: If this is Callable it can have a better error message.
  3145. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  3146. return false;
  3147. }
  3148. found_function = base_class->get_member(function_name).function;
  3149. }
  3150. base_class = base_class->base_type.class_type;
  3151. }
  3152. if (found_function != nullptr) {
  3153. r_static = is_constructor || found_function->is_static;
  3154. for (int i = 0; i < found_function->parameters.size(); i++) {
  3155. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  3156. if (found_function->parameters[i]->default_value != nullptr) {
  3157. r_default_arg_count++;
  3158. }
  3159. }
  3160. r_return_type = found_function->get_datatype();
  3161. r_return_type.is_meta_type = false;
  3162. r_return_type.is_coroutine = found_function->is_coroutine;
  3163. return true;
  3164. }
  3165. Ref<Script> base_script = p_base_type.script_type;
  3166. while (base_script.is_valid() && base_script->is_valid()) {
  3167. MethodInfo info = base_script->get_method_info(function_name);
  3168. if (!(info == MethodInfo())) {
  3169. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3170. }
  3171. base_script = base_script->get_base_script();
  3172. }
  3173. // If the base is a script, it might be trying to access members of the Script class itself.
  3174. if (p_base_type.is_meta_type && !is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  3175. MethodInfo info;
  3176. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  3177. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  3178. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3179. }
  3180. }
  3181. StringName base_native = p_base_type.native_type;
  3182. #ifdef DEBUG_ENABLED
  3183. if (base_native != StringName()) {
  3184. // Empty native class might happen in some Script implementations.
  3185. // Just ignore it.
  3186. if (!class_exists(base_native)) {
  3187. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  3188. }
  3189. }
  3190. #endif
  3191. if (is_constructor) {
  3192. // Native types always have a default constructor.
  3193. r_return_type = p_base_type;
  3194. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3195. r_return_type.is_meta_type = false;
  3196. return true;
  3197. }
  3198. MethodInfo info;
  3199. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  3200. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3201. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  3202. r_static = true;
  3203. }
  3204. return valid;
  3205. }
  3206. return false;
  3207. }
  3208. bool GDScriptAnalyzer::function_signature_from_info(const MethodInfo &p_info, GDScriptParser::DataType &r_return_type, List<GDScriptParser::DataType> &r_par_types, int &r_default_arg_count, bool &r_static, bool &r_vararg) {
  3209. r_return_type = type_from_property(p_info.return_val);
  3210. r_default_arg_count = p_info.default_arguments.size();
  3211. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  3212. for (const PropertyInfo &E : p_info.arguments) {
  3213. r_par_types.push_back(type_from_property(E));
  3214. }
  3215. return true;
  3216. }
  3217. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  3218. List<GDScriptParser::DataType> arg_types;
  3219. for (const PropertyInfo &E : p_method.arguments) {
  3220. arg_types.push_back(type_from_property(E));
  3221. }
  3222. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  3223. }
  3224. bool GDScriptAnalyzer::validate_call_arg(const List<GDScriptParser::DataType> &p_par_types, int p_default_args_count, bool p_is_vararg, const GDScriptParser::CallNode *p_call) {
  3225. bool valid = true;
  3226. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  3227. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", p_call->function_name, p_par_types.size() - p_default_args_count, p_call->arguments.size()), p_call);
  3228. valid = false;
  3229. }
  3230. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  3231. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", p_call->function_name, p_par_types.size(), p_call->arguments.size()), p_call->arguments[p_par_types.size()]);
  3232. valid = false;
  3233. }
  3234. for (int i = 0; i < p_call->arguments.size(); i++) {
  3235. if (i >= p_par_types.size()) {
  3236. // Already on vararg place.
  3237. break;
  3238. }
  3239. GDScriptParser::DataType par_type = p_par_types[i];
  3240. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  3241. if (arg_type.is_variant()) {
  3242. // Argument can be anything, so this is unsafe.
  3243. mark_node_unsafe(p_call->arguments[i]);
  3244. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  3245. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  3246. mark_node_unsafe(p_call);
  3247. if (!is_type_compatible(arg_type, par_type)) {
  3248. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  3249. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  3250. p_call->arguments[i]);
  3251. valid = false;
  3252. }
  3253. #ifdef DEBUG_ENABLED
  3254. } else {
  3255. if (par_type.kind == GDScriptParser::DataType::BUILTIN && par_type.builtin_type == Variant::INT && arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == Variant::FLOAT) {
  3256. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  3257. }
  3258. #endif
  3259. }
  3260. }
  3261. return valid;
  3262. }
  3263. #ifdef DEBUG_ENABLED
  3264. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  3265. const StringName &name = p_local->name;
  3266. GDScriptParser::DataType base = parser->current_class->get_datatype();
  3267. GDScriptParser::ClassNode *base_class = base.class_type;
  3268. while (base_class != nullptr) {
  3269. if (base_class->has_member(name)) {
  3270. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE, p_context, p_local->name, base_class->get_member(name).get_type_name(), itos(base_class->get_member(name).get_line()));
  3271. return true;
  3272. }
  3273. base_class = base_class->base_type.class_type;
  3274. }
  3275. StringName base_native = base.native_type;
  3276. ERR_FAIL_COND_V_MSG(!class_exists(base_native), false, "Non-existent native base class.");
  3277. StringName parent = base_native;
  3278. while (parent != StringName()) {
  3279. if (ClassDB::has_method(parent, name, true)) {
  3280. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  3281. return true;
  3282. } else if (ClassDB::has_signal(parent, name, true)) {
  3283. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  3284. return true;
  3285. } else if (ClassDB::has_property(parent, name, true)) {
  3286. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  3287. return true;
  3288. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  3289. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  3290. return true;
  3291. } else if (ClassDB::has_enum(parent, name, true)) {
  3292. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  3293. return true;
  3294. }
  3295. parent = ClassDB::get_parent_class(parent);
  3296. }
  3297. return false;
  3298. }
  3299. #endif
  3300. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  3301. // Unary version.
  3302. GDScriptParser::DataType nil_type;
  3303. nil_type.builtin_type = Variant::NIL;
  3304. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  3305. }
  3306. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid, const GDScriptParser::Node *p_source) {
  3307. GDScriptParser::DataType result;
  3308. result.kind = GDScriptParser::DataType::VARIANT;
  3309. Variant::Type a_type = p_a.builtin_type;
  3310. Variant::Type b_type = p_b.builtin_type;
  3311. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  3312. if (op_eval == nullptr) {
  3313. r_valid = false;
  3314. return result;
  3315. }
  3316. r_valid = true;
  3317. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3318. result.kind = GDScriptParser::DataType::BUILTIN;
  3319. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3320. return result;
  3321. }
  3322. // TODO: Add safe/unsafe return variable (for variant cases)
  3323. bool GDScriptAnalyzer::is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion) const {
  3324. // These return "true" so it doesn't affect users negatively.
  3325. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  3326. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  3327. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  3328. // Variant can receive anything.
  3329. return true;
  3330. }
  3331. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  3332. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  3333. return true;
  3334. }
  3335. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  3336. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  3337. if (!valid && p_allow_implicit_conversion) {
  3338. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  3339. }
  3340. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  3341. // Enum value is also integer.
  3342. valid = true;
  3343. }
  3344. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  3345. // Check the element type.
  3346. if (p_target.has_container_element_type()) {
  3347. if (!p_source.has_container_element_type()) {
  3348. // TODO: Maybe this is valid but unsafe?
  3349. // Variant array can't be appended to typed array.
  3350. valid = false;
  3351. } else {
  3352. valid = is_type_compatible(p_target.get_container_element_type(), p_source.get_container_element_type(), false);
  3353. }
  3354. }
  3355. }
  3356. return valid;
  3357. }
  3358. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  3359. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  3360. return true;
  3361. }
  3362. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  3363. if (p_source.native_type == p_target.native_type) {
  3364. return true;
  3365. }
  3366. }
  3367. if (p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  3368. if (p_source.native_type == p_target.native_type && p_target.enum_values.has(p_source.enum_type)) {
  3369. return true;
  3370. }
  3371. }
  3372. return false;
  3373. }
  3374. // From here on the target type is an object, so we have to test polymorphism.
  3375. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  3376. // null is acceptable in object.
  3377. return true;
  3378. }
  3379. StringName src_native;
  3380. Ref<Script> src_script;
  3381. const GDScriptParser::ClassNode *src_class = nullptr;
  3382. switch (p_source.kind) {
  3383. case GDScriptParser::DataType::NATIVE:
  3384. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  3385. // Non-native class cannot be supertype of native.
  3386. return false;
  3387. }
  3388. if (p_source.is_meta_type) {
  3389. src_native = GDScriptNativeClass::get_class_static();
  3390. } else {
  3391. src_native = p_source.native_type;
  3392. }
  3393. break;
  3394. case GDScriptParser::DataType::SCRIPT:
  3395. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  3396. // A script type cannot be a subtype of a GDScript class.
  3397. return false;
  3398. }
  3399. if (p_source.is_meta_type) {
  3400. src_native = p_source.script_type->get_class_name();
  3401. } else {
  3402. src_script = p_source.script_type;
  3403. src_native = src_script->get_instance_base_type();
  3404. }
  3405. break;
  3406. case GDScriptParser::DataType::CLASS:
  3407. if (p_source.is_meta_type) {
  3408. src_native = GDScript::get_class_static();
  3409. } else {
  3410. src_class = p_source.class_type;
  3411. const GDScriptParser::ClassNode *base = src_class;
  3412. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  3413. base = base->base_type.class_type;
  3414. }
  3415. src_native = base->base_type.native_type;
  3416. src_script = base->base_type.script_type;
  3417. }
  3418. break;
  3419. case GDScriptParser::DataType::VARIANT:
  3420. case GDScriptParser::DataType::BUILTIN:
  3421. case GDScriptParser::DataType::ENUM:
  3422. case GDScriptParser::DataType::ENUM_VALUE:
  3423. case GDScriptParser::DataType::UNRESOLVED:
  3424. break; // Already solved before.
  3425. }
  3426. switch (p_target.kind) {
  3427. case GDScriptParser::DataType::NATIVE: {
  3428. if (p_target.is_meta_type) {
  3429. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  3430. }
  3431. return ClassDB::is_parent_class(src_native, p_target.native_type);
  3432. }
  3433. case GDScriptParser::DataType::SCRIPT:
  3434. if (p_target.is_meta_type) {
  3435. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  3436. }
  3437. while (src_script.is_valid()) {
  3438. if (src_script == p_target.script_type) {
  3439. return true;
  3440. }
  3441. src_script = src_script->get_base_script();
  3442. }
  3443. return false;
  3444. case GDScriptParser::DataType::CLASS:
  3445. if (p_target.is_meta_type) {
  3446. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  3447. }
  3448. while (src_class != nullptr) {
  3449. if (src_class->fqcn == p_target.class_type->fqcn) {
  3450. return true;
  3451. }
  3452. src_class = src_class->base_type.class_type;
  3453. }
  3454. return false;
  3455. case GDScriptParser::DataType::VARIANT:
  3456. case GDScriptParser::DataType::BUILTIN:
  3457. case GDScriptParser::DataType::ENUM:
  3458. case GDScriptParser::DataType::ENUM_VALUE:
  3459. case GDScriptParser::DataType::UNRESOLVED:
  3460. break; // Already solved before.
  3461. }
  3462. return false;
  3463. }
  3464. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  3465. mark_node_unsafe(p_origin);
  3466. parser->push_error(p_message, p_origin);
  3467. }
  3468. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  3469. #ifdef DEBUG_ENABLED
  3470. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  3471. parser->unsafe_lines.insert(i);
  3472. }
  3473. #endif
  3474. }
  3475. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  3476. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  3477. }
  3478. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  3479. Ref<GDScriptParserRef> ref;
  3480. if (depended_parsers.has(p_path)) {
  3481. ref = depended_parsers[p_path];
  3482. } else {
  3483. Error err = OK;
  3484. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  3485. depended_parsers[p_path] = ref;
  3486. }
  3487. return ref;
  3488. }
  3489. Error GDScriptAnalyzer::resolve_inheritance() {
  3490. return resolve_inheritance(parser->head);
  3491. }
  3492. Error GDScriptAnalyzer::resolve_interface() {
  3493. resolve_class_interface(parser->head);
  3494. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3495. }
  3496. Error GDScriptAnalyzer::resolve_body() {
  3497. resolve_class_body(parser->head);
  3498. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3499. }
  3500. Error GDScriptAnalyzer::resolve_program() {
  3501. resolve_class_interface(parser->head);
  3502. resolve_class_body(parser->head);
  3503. List<String> parser_keys;
  3504. depended_parsers.get_key_list(&parser_keys);
  3505. for (const String &E : parser_keys) {
  3506. if (depended_parsers[E].is_null()) {
  3507. return ERR_PARSE_ERROR;
  3508. }
  3509. depended_parsers[E]->raise_status(GDScriptParserRef::FULLY_SOLVED);
  3510. }
  3511. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3512. }
  3513. Error GDScriptAnalyzer::analyze() {
  3514. parser->errors.clear();
  3515. Error err = resolve_inheritance(parser->head);
  3516. if (err) {
  3517. return err;
  3518. }
  3519. return resolve_program();
  3520. }
  3521. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  3522. parser = p_parser;
  3523. }