gdscript_analyzer.cpp 172 KB

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