gdscript_analyzer.cpp 172 KB

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