gdscript_analyzer.cpp 172 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486
  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_constant = p_assignable->type == GDScriptParser::Node::CONSTANT;
  1283. GDScriptParser::DataType specified_type;
  1284. bool has_specified_type = p_assignable->datatype_specifier != nullptr;
  1285. if (has_specified_type) {
  1286. specified_type = resolve_datatype(p_assignable->datatype_specifier);
  1287. specified_type.is_meta_type = false;
  1288. type = specified_type;
  1289. }
  1290. if (p_assignable->initializer != nullptr) {
  1291. reduce_expression(p_assignable->initializer);
  1292. if (p_assignable->initializer->type == GDScriptParser::Node::ARRAY) {
  1293. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_assignable->initializer);
  1294. if ((p_assignable->infer_datatype && array->elements.size() > 0) || (has_specified_type && specified_type.has_container_element_type())) {
  1295. update_array_literal_element_type(specified_type, array);
  1296. }
  1297. }
  1298. if (is_constant) {
  1299. if (p_assignable->initializer->type == GDScriptParser::Node::ARRAY) {
  1300. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_assignable->initializer));
  1301. } else if (p_assignable->initializer->type == GDScriptParser::Node::DICTIONARY) {
  1302. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_assignable->initializer));
  1303. }
  1304. if (!p_assignable->initializer->is_constant) {
  1305. push_error(vformat(R"(Assigned value for %s "%s" isn't a constant expression.)", p_kind, p_assignable->identifier->name), p_assignable->initializer);
  1306. }
  1307. }
  1308. GDScriptParser::DataType initializer_type = p_assignable->initializer->get_datatype();
  1309. if (p_assignable->infer_datatype) {
  1310. if (!initializer_type.is_set() || initializer_type.has_no_type()) {
  1311. 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);
  1312. } else if (initializer_type.is_variant() && !initializer_type.is_hard_type()) {
  1313. 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);
  1314. } else if (initializer_type.kind == GDScriptParser::DataType::BUILTIN && initializer_type.builtin_type == Variant::NIL && !is_constant) {
  1315. 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);
  1316. }
  1317. } else {
  1318. if (!initializer_type.is_set()) {
  1319. push_error(vformat(R"(Could not resolve type for %s "%s".)", p_kind, p_assignable->identifier->name), p_assignable->initializer);
  1320. }
  1321. }
  1322. if (!has_specified_type) {
  1323. type = initializer_type;
  1324. if (!type.is_set() || (type.is_hard_type() && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL && !is_constant)) {
  1325. type.kind = GDScriptParser::DataType::VARIANT;
  1326. }
  1327. if (p_assignable->infer_datatype || is_constant) {
  1328. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1329. } else {
  1330. type.type_source = GDScriptParser::DataType::INFERRED;
  1331. }
  1332. } else if (!specified_type.is_variant()) {
  1333. if (initializer_type.is_variant() || !initializer_type.is_hard_type()) {
  1334. mark_node_unsafe(p_assignable->initializer);
  1335. p_assignable->use_conversion_assign = true;
  1336. } else if (!is_type_compatible(specified_type, initializer_type, true, p_assignable->initializer)) {
  1337. if (!is_constant && is_type_compatible(initializer_type, specified_type, true, p_assignable->initializer)) {
  1338. mark_node_unsafe(p_assignable->initializer);
  1339. p_assignable->use_conversion_assign = true;
  1340. } else {
  1341. 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);
  1342. }
  1343. #ifdef DEBUG_ENABLED
  1344. } else if (specified_type.builtin_type == Variant::INT && initializer_type.builtin_type == Variant::FLOAT) {
  1345. parser->push_warning(p_assignable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1346. #endif
  1347. }
  1348. }
  1349. }
  1350. type.is_constant = is_constant;
  1351. p_assignable->set_datatype(type);
  1352. }
  1353. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable, bool p_is_local) {
  1354. static constexpr const char *kind = "variable";
  1355. resolve_assignable(p_variable, kind);
  1356. #ifdef DEBUG_ENABLED
  1357. if (p_is_local) {
  1358. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  1359. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  1360. } else if (p_variable->assignments == 0) {
  1361. parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
  1362. }
  1363. is_shadowing(p_variable->identifier, kind);
  1364. }
  1365. #endif
  1366. }
  1367. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant, bool p_is_local) {
  1368. static constexpr const char *kind = "constant";
  1369. resolve_assignable(p_constant, kind);
  1370. #ifdef DEBUG_ENABLED
  1371. if (p_is_local) {
  1372. if (p_constant->usages == 0) {
  1373. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1374. }
  1375. is_shadowing(p_constant->identifier, kind);
  1376. }
  1377. #endif
  1378. }
  1379. void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) {
  1380. static constexpr const char *kind = "parameter";
  1381. resolve_assignable(p_parameter, kind);
  1382. }
  1383. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  1384. reduce_expression(p_if->condition);
  1385. resolve_suite(p_if->true_block);
  1386. p_if->set_datatype(p_if->true_block->get_datatype());
  1387. if (p_if->false_block != nullptr) {
  1388. resolve_suite(p_if->false_block);
  1389. decide_suite_type(p_if, p_if->false_block);
  1390. }
  1391. }
  1392. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  1393. bool list_resolved = false;
  1394. // Optimize constant range() call to not allocate an array.
  1395. // Use int, Vector2i, Vector3i instead, which also can be used as range iterators.
  1396. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  1397. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  1398. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1399. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1400. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  1401. if (callee->name == "range") {
  1402. list_resolved = true;
  1403. if (call->arguments.size() < 1) {
  1404. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  1405. } else if (call->arguments.size() > 3) {
  1406. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  1407. } else {
  1408. // Now we can optimize it.
  1409. bool all_is_constant = true;
  1410. Vector<Variant> args;
  1411. args.resize(call->arguments.size());
  1412. for (int i = 0; i < call->arguments.size(); i++) {
  1413. reduce_expression(call->arguments[i]);
  1414. if (!call->arguments[i]->is_constant) {
  1415. all_is_constant = false;
  1416. } else if (all_is_constant) {
  1417. args.write[i] = call->arguments[i]->reduced_value;
  1418. }
  1419. GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype();
  1420. if (!arg_type.is_variant()) {
  1421. if (arg_type.kind != GDScriptParser::DataType::BUILTIN) {
  1422. all_is_constant = false;
  1423. 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]);
  1424. } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) {
  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. }
  1428. }
  1429. }
  1430. Variant reduced;
  1431. if (all_is_constant) {
  1432. switch (args.size()) {
  1433. case 1:
  1434. reduced = (int32_t)args[0];
  1435. break;
  1436. case 2:
  1437. reduced = Vector2i(args[0], args[1]);
  1438. break;
  1439. case 3:
  1440. reduced = Vector3i(args[0], args[1], args[2]);
  1441. break;
  1442. }
  1443. p_for->list->is_constant = true;
  1444. p_for->list->reduced_value = reduced;
  1445. }
  1446. }
  1447. if (p_for->list->is_constant) {
  1448. p_for->list->set_datatype(type_from_variant(p_for->list->reduced_value, p_for->list));
  1449. } else {
  1450. GDScriptParser::DataType list_type;
  1451. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1452. list_type.kind = GDScriptParser::DataType::BUILTIN;
  1453. list_type.builtin_type = Variant::ARRAY;
  1454. p_for->list->set_datatype(list_type);
  1455. }
  1456. }
  1457. }
  1458. }
  1459. GDScriptParser::DataType variable_type;
  1460. if (list_resolved) {
  1461. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1462. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1463. variable_type.builtin_type = Variant::INT; // Can this ever be a float or something else?
  1464. p_for->variable->set_datatype(variable_type);
  1465. } else if (p_for->list) {
  1466. resolve_node(p_for->list, false);
  1467. if (p_for->list->datatype.has_container_element_type()) {
  1468. variable_type = p_for->list->datatype.get_container_element_type();
  1469. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1470. } else if (p_for->list->datatype.is_typed_container_type()) {
  1471. variable_type = p_for->list->datatype.get_typed_container_type();
  1472. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1473. } else {
  1474. // Last resort
  1475. // TODO: Must other cases be handled? Must we mark as unsafe?
  1476. variable_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1477. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1478. }
  1479. }
  1480. if (p_for->variable) {
  1481. p_for->variable->set_datatype(variable_type);
  1482. }
  1483. resolve_suite(p_for->loop);
  1484. p_for->set_datatype(p_for->loop->get_datatype());
  1485. #ifdef DEBUG_ENABLED
  1486. if (p_for->variable) {
  1487. is_shadowing(p_for->variable, R"("for" iterator variable)");
  1488. }
  1489. #endif
  1490. }
  1491. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  1492. resolve_node(p_while->condition, false);
  1493. resolve_suite(p_while->loop);
  1494. p_while->set_datatype(p_while->loop->get_datatype());
  1495. }
  1496. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1497. reduce_expression(p_assert->condition);
  1498. if (p_assert->message != nullptr) {
  1499. reduce_expression(p_assert->message);
  1500. 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)) {
  1501. push_error(R"(Expected string for assert error message.)", p_assert->message);
  1502. }
  1503. }
  1504. p_assert->set_datatype(p_assert->condition->get_datatype());
  1505. #ifdef DEBUG_ENABLED
  1506. if (p_assert->condition->is_constant) {
  1507. if (p_assert->condition->reduced_value.booleanize()) {
  1508. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1509. } else {
  1510. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1511. }
  1512. }
  1513. #endif
  1514. }
  1515. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1516. reduce_expression(p_match->test);
  1517. for (int i = 0; i < p_match->branches.size(); i++) {
  1518. resolve_match_branch(p_match->branches[i], p_match->test);
  1519. decide_suite_type(p_match, p_match->branches[i]);
  1520. }
  1521. }
  1522. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1523. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1524. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1525. }
  1526. resolve_suite(p_match_branch->block);
  1527. decide_suite_type(p_match_branch, p_match_branch->block);
  1528. }
  1529. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  1530. if (p_match_pattern == nullptr) {
  1531. return;
  1532. }
  1533. GDScriptParser::DataType result;
  1534. switch (p_match_pattern->pattern_type) {
  1535. case GDScriptParser::PatternNode::PT_LITERAL:
  1536. if (p_match_pattern->literal) {
  1537. reduce_literal(p_match_pattern->literal);
  1538. result = p_match_pattern->literal->get_datatype();
  1539. }
  1540. break;
  1541. case GDScriptParser::PatternNode::PT_EXPRESSION:
  1542. if (p_match_pattern->expression) {
  1543. reduce_expression(p_match_pattern->expression);
  1544. if (!p_match_pattern->expression->is_constant) {
  1545. push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression);
  1546. }
  1547. result = p_match_pattern->expression->get_datatype();
  1548. }
  1549. break;
  1550. case GDScriptParser::PatternNode::PT_BIND:
  1551. if (p_match_test != nullptr) {
  1552. result = p_match_test->get_datatype();
  1553. } else {
  1554. result.kind = GDScriptParser::DataType::VARIANT;
  1555. }
  1556. p_match_pattern->bind->set_datatype(result);
  1557. #ifdef DEBUG_ENABLED
  1558. is_shadowing(p_match_pattern->bind, "pattern bind");
  1559. if (p_match_pattern->bind->usages == 0 && !String(p_match_pattern->bind->name).begins_with("_")) {
  1560. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNUSED_VARIABLE, p_match_pattern->bind->name);
  1561. }
  1562. #endif
  1563. break;
  1564. case GDScriptParser::PatternNode::PT_ARRAY:
  1565. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  1566. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  1567. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  1568. }
  1569. result = p_match_pattern->get_datatype();
  1570. break;
  1571. case GDScriptParser::PatternNode::PT_DICTIONARY:
  1572. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  1573. if (p_match_pattern->dictionary[i].key) {
  1574. reduce_expression(p_match_pattern->dictionary[i].key);
  1575. if (!p_match_pattern->dictionary[i].key->is_constant) {
  1576. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->dictionary[i].key);
  1577. }
  1578. }
  1579. if (p_match_pattern->dictionary[i].value_pattern) {
  1580. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  1581. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  1582. }
  1583. }
  1584. result = p_match_pattern->get_datatype();
  1585. break;
  1586. case GDScriptParser::PatternNode::PT_WILDCARD:
  1587. case GDScriptParser::PatternNode::PT_REST:
  1588. result.kind = GDScriptParser::DataType::VARIANT;
  1589. break;
  1590. }
  1591. p_match_pattern->set_datatype(result);
  1592. }
  1593. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  1594. GDScriptParser::DataType result;
  1595. GDScriptParser::DataType expected_type;
  1596. bool has_expected_type = false;
  1597. if (parser->current_function != nullptr) {
  1598. expected_type = parser->current_function->get_datatype();
  1599. has_expected_type = true;
  1600. }
  1601. if (p_return->return_value != nullptr) {
  1602. reduce_expression(p_return->return_value);
  1603. if (p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1604. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1605. if (has_expected_type && expected_type.has_container_element_type() && p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1606. update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
  1607. }
  1608. }
  1609. if (has_expected_type && expected_type.is_hard_type() && expected_type.kind == GDScriptParser::DataType::BUILTIN && expected_type.builtin_type == Variant::NIL) {
  1610. push_error("A void function cannot return a value.", p_return);
  1611. }
  1612. result = p_return->return_value->get_datatype();
  1613. } else {
  1614. // Return type is null by default.
  1615. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1616. result.kind = GDScriptParser::DataType::BUILTIN;
  1617. result.builtin_type = Variant::NIL;
  1618. result.is_constant = true;
  1619. }
  1620. if (has_expected_type) {
  1621. expected_type.is_meta_type = false;
  1622. if (expected_type.is_hard_type()) {
  1623. if (!is_type_compatible(expected_type, result)) {
  1624. // Try other way. Okay but not safe.
  1625. if (!is_type_compatible(result, expected_type)) {
  1626. 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);
  1627. } else {
  1628. // TODO: Add warning.
  1629. mark_node_unsafe(p_return);
  1630. }
  1631. #ifdef DEBUG_ENABLED
  1632. } else if (expected_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  1633. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  1634. } else if (result.is_variant()) {
  1635. mark_node_unsafe(p_return);
  1636. #endif
  1637. }
  1638. }
  1639. }
  1640. p_return->set_datatype(result);
  1641. }
  1642. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root) {
  1643. // This one makes some magic happen.
  1644. if (p_expression == nullptr) {
  1645. return;
  1646. }
  1647. if (p_expression->reduced) {
  1648. // Don't do this more than once.
  1649. return;
  1650. }
  1651. p_expression->reduced = true;
  1652. switch (p_expression->type) {
  1653. case GDScriptParser::Node::ARRAY:
  1654. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  1655. break;
  1656. case GDScriptParser::Node::ASSIGNMENT:
  1657. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  1658. break;
  1659. case GDScriptParser::Node::AWAIT:
  1660. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  1661. break;
  1662. case GDScriptParser::Node::BINARY_OPERATOR:
  1663. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  1664. break;
  1665. case GDScriptParser::Node::CALL:
  1666. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression), false, p_is_root);
  1667. break;
  1668. case GDScriptParser::Node::CAST:
  1669. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  1670. break;
  1671. case GDScriptParser::Node::DICTIONARY:
  1672. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  1673. break;
  1674. case GDScriptParser::Node::GET_NODE:
  1675. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  1676. break;
  1677. case GDScriptParser::Node::IDENTIFIER:
  1678. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  1679. break;
  1680. case GDScriptParser::Node::LAMBDA:
  1681. reduce_lambda(static_cast<GDScriptParser::LambdaNode *>(p_expression));
  1682. break;
  1683. case GDScriptParser::Node::LITERAL:
  1684. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  1685. break;
  1686. case GDScriptParser::Node::PRELOAD:
  1687. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  1688. break;
  1689. case GDScriptParser::Node::SELF:
  1690. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  1691. break;
  1692. case GDScriptParser::Node::SUBSCRIPT:
  1693. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  1694. break;
  1695. case GDScriptParser::Node::TERNARY_OPERATOR:
  1696. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression));
  1697. break;
  1698. case GDScriptParser::Node::UNARY_OPERATOR:
  1699. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  1700. break;
  1701. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  1702. case GDScriptParser::Node::NONE:
  1703. case GDScriptParser::Node::ANNOTATION:
  1704. case GDScriptParser::Node::ASSERT:
  1705. case GDScriptParser::Node::BREAK:
  1706. case GDScriptParser::Node::BREAKPOINT:
  1707. case GDScriptParser::Node::CLASS:
  1708. case GDScriptParser::Node::CONSTANT:
  1709. case GDScriptParser::Node::CONTINUE:
  1710. case GDScriptParser::Node::ENUM:
  1711. case GDScriptParser::Node::FOR:
  1712. case GDScriptParser::Node::FUNCTION:
  1713. case GDScriptParser::Node::IF:
  1714. case GDScriptParser::Node::MATCH:
  1715. case GDScriptParser::Node::MATCH_BRANCH:
  1716. case GDScriptParser::Node::PARAMETER:
  1717. case GDScriptParser::Node::PASS:
  1718. case GDScriptParser::Node::PATTERN:
  1719. case GDScriptParser::Node::RETURN:
  1720. case GDScriptParser::Node::SIGNAL:
  1721. case GDScriptParser::Node::SUITE:
  1722. case GDScriptParser::Node::TYPE:
  1723. case GDScriptParser::Node::VARIABLE:
  1724. case GDScriptParser::Node::WHILE:
  1725. ERR_FAIL_MSG("Reaching unreachable case");
  1726. }
  1727. }
  1728. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  1729. for (int i = 0; i < p_array->elements.size(); i++) {
  1730. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  1731. reduce_expression(element);
  1732. }
  1733. // It's array in any case.
  1734. GDScriptParser::DataType arr_type;
  1735. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1736. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  1737. arr_type.builtin_type = Variant::ARRAY;
  1738. arr_type.is_constant = true;
  1739. p_array->set_datatype(arr_type);
  1740. }
  1741. // When an array literal is stored (or passed as function argument) to a typed context, we then assume the array is typed.
  1742. // This function determines which type is that (if any).
  1743. void GDScriptAnalyzer::update_array_literal_element_type(const GDScriptParser::DataType &p_base_type, GDScriptParser::ArrayNode *p_array_literal) {
  1744. GDScriptParser::DataType array_type = p_array_literal->get_datatype();
  1745. if (p_array_literal->elements.size() == 0) {
  1746. // Empty array literal, just make the same type as the storage.
  1747. array_type.set_container_element_type(p_base_type.get_container_element_type());
  1748. } else {
  1749. // Check if elements match.
  1750. bool all_same_type = true;
  1751. bool all_have_type = true;
  1752. GDScriptParser::DataType element_type;
  1753. for (int i = 0; i < p_array_literal->elements.size(); i++) {
  1754. if (i == 0) {
  1755. element_type = p_array_literal->elements[0]->get_datatype();
  1756. } else {
  1757. GDScriptParser::DataType this_element_type = p_array_literal->elements[i]->get_datatype();
  1758. if (this_element_type.has_no_type()) {
  1759. all_same_type = false;
  1760. all_have_type = false;
  1761. break;
  1762. } else if (element_type != this_element_type) {
  1763. if (!is_type_compatible(element_type, this_element_type, false)) {
  1764. if (is_type_compatible(this_element_type, element_type, false)) {
  1765. // This element is a super-type to the previous type, so we use the super-type.
  1766. element_type = this_element_type;
  1767. } else {
  1768. // It's incompatible.
  1769. all_same_type = false;
  1770. break;
  1771. }
  1772. }
  1773. }
  1774. }
  1775. }
  1776. if (all_same_type) {
  1777. element_type.is_constant = false;
  1778. array_type.set_container_element_type(element_type);
  1779. } else if (all_have_type) {
  1780. 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);
  1781. }
  1782. }
  1783. // Update the type on the value itself.
  1784. p_array_literal->set_datatype(array_type);
  1785. }
  1786. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  1787. reduce_expression(p_assignment->assignee);
  1788. reduce_expression(p_assignment->assigned_value);
  1789. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  1790. return;
  1791. }
  1792. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  1793. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1794. if (assignee_type.has_container_element_type() && p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY) {
  1795. update_array_literal_element_type(assignee_type, static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value));
  1796. }
  1797. GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype();
  1798. if (assignee_type.is_constant) {
  1799. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  1800. }
  1801. bool compatible = true;
  1802. GDScriptParser::DataType op_type = assigned_value_type;
  1803. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  1804. op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value);
  1805. }
  1806. p_assignment->set_datatype(op_type);
  1807. if (assignee_type.is_hard_type() && !assignee_type.is_variant() && op_type.is_hard_type()) {
  1808. if (compatible) {
  1809. compatible = is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value);
  1810. if (!compatible) {
  1811. // Try reverse test since it can be a masked subtype.
  1812. if (!is_type_compatible(op_type, assignee_type, true)) {
  1813. 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);
  1814. } else {
  1815. // TODO: Add warning.
  1816. mark_node_unsafe(p_assignment);
  1817. p_assignment->use_conversion_assign = true;
  1818. }
  1819. }
  1820. } else {
  1821. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment);
  1822. }
  1823. } else if (assignee_type.is_hard_type() && !assignee_type.is_variant()) {
  1824. mark_node_unsafe(p_assignment);
  1825. p_assignment->use_conversion_assign = true;
  1826. } else {
  1827. mark_node_unsafe(p_assignment);
  1828. if (assignee_type.is_hard_type() && !assignee_type.is_variant()) {
  1829. p_assignment->use_conversion_assign = true;
  1830. }
  1831. }
  1832. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1833. // Change source type so it's not wrongly detected later.
  1834. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  1835. switch (identifier->source) {
  1836. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  1837. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1838. if (!id_type.is_hard_type()) {
  1839. id_type.kind = GDScriptParser::DataType::VARIANT;
  1840. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1841. identifier->variable_source->set_datatype(id_type);
  1842. }
  1843. } break;
  1844. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: {
  1845. GDScriptParser::DataType id_type = identifier->parameter_source->get_datatype();
  1846. if (!id_type.is_hard_type()) {
  1847. id_type.kind = GDScriptParser::DataType::VARIANT;
  1848. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1849. identifier->parameter_source->set_datatype(id_type);
  1850. }
  1851. } break;
  1852. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  1853. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1854. if (!id_type.is_hard_type()) {
  1855. id_type.kind = GDScriptParser::DataType::VARIANT;
  1856. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1857. identifier->variable_source->set_datatype(id_type);
  1858. }
  1859. } break;
  1860. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  1861. GDScriptParser::DataType id_type = identifier->bind_source->get_datatype();
  1862. if (!id_type.is_hard_type()) {
  1863. id_type.kind = GDScriptParser::DataType::VARIANT;
  1864. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1865. identifier->variable_source->set_datatype(id_type);
  1866. }
  1867. } break;
  1868. default:
  1869. // Nothing to do.
  1870. break;
  1871. }
  1872. }
  1873. #ifdef DEBUG_ENABLED
  1874. if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) {
  1875. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  1876. }
  1877. #endif
  1878. }
  1879. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  1880. if (p_await->to_await == nullptr) {
  1881. GDScriptParser::DataType await_type;
  1882. await_type.kind = GDScriptParser::DataType::VARIANT;
  1883. p_await->set_datatype(await_type);
  1884. return;
  1885. }
  1886. GDScriptParser::DataType awaiting_type;
  1887. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  1888. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  1889. awaiting_type = p_await->to_await->get_datatype();
  1890. } else {
  1891. reduce_expression(p_await->to_await);
  1892. }
  1893. if (p_await->to_await->is_constant) {
  1894. p_await->is_constant = p_await->to_await->is_constant;
  1895. p_await->reduced_value = p_await->to_await->reduced_value;
  1896. awaiting_type = p_await->to_await->get_datatype();
  1897. } else {
  1898. awaiting_type.kind = GDScriptParser::DataType::VARIANT;
  1899. awaiting_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1900. }
  1901. p_await->set_datatype(awaiting_type);
  1902. #ifdef DEBUG_ENABLED
  1903. awaiting_type = p_await->to_await->get_datatype();
  1904. if (!(awaiting_type.has_no_type() || awaiting_type.is_coroutine || awaiting_type.builtin_type == Variant::SIGNAL)) {
  1905. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  1906. }
  1907. #endif
  1908. }
  1909. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  1910. reduce_expression(p_binary_op->left_operand);
  1911. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) {
  1912. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_binary_op->right_operand), true);
  1913. } else {
  1914. reduce_expression(p_binary_op->right_operand);
  1915. }
  1916. // TODO: Right operand must be a valid type with the `is` operator. Need to check here.
  1917. GDScriptParser::DataType left_type;
  1918. if (p_binary_op->left_operand) {
  1919. left_type = p_binary_op->left_operand->get_datatype();
  1920. }
  1921. GDScriptParser::DataType right_type;
  1922. if (p_binary_op->right_operand) {
  1923. right_type = p_binary_op->right_operand->get_datatype();
  1924. }
  1925. if (!left_type.is_set() || !right_type.is_set()) {
  1926. return;
  1927. }
  1928. #ifdef DEBUG_ENABLED
  1929. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1930. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1931. }
  1932. #endif
  1933. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1934. p_binary_op->is_constant = true;
  1935. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1936. bool valid = false;
  1937. 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);
  1938. if (!valid) {
  1939. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1940. 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);
  1941. } else {
  1942. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  1943. Variant::get_operator_name(p_binary_op->variant_op),
  1944. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1945. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1946. p_binary_op);
  1947. }
  1948. }
  1949. } else {
  1950. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1951. GDScriptParser::DataType test_type = right_type;
  1952. test_type.is_meta_type = false;
  1953. if (!is_type_compatible(test_type, left_type, false)) {
  1954. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1955. p_binary_op->reduced_value = false;
  1956. } else {
  1957. p_binary_op->reduced_value = true;
  1958. }
  1959. } else {
  1960. ERR_PRINT("Parser bug: unknown binary operation.");
  1961. }
  1962. }
  1963. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  1964. return;
  1965. }
  1966. GDScriptParser::DataType result;
  1967. if (left_type.is_variant() || right_type.is_variant()) {
  1968. // Cannot infer type because one operand can be anything.
  1969. result.kind = GDScriptParser::DataType::VARIANT;
  1970. mark_node_unsafe(p_binary_op);
  1971. } else {
  1972. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1973. bool valid = false;
  1974. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  1975. if (!valid) {
  1976. 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);
  1977. }
  1978. } else {
  1979. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1980. GDScriptParser::DataType test_type = right_type;
  1981. test_type.is_meta_type = false;
  1982. if (!is_type_compatible(test_type, left_type, false)) {
  1983. // Test reverse as well to consider for subtypes.
  1984. if (!is_type_compatible(left_type, test_type, false)) {
  1985. if (left_type.is_hard_type()) {
  1986. 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);
  1987. } else {
  1988. // TODO: Warning.
  1989. mark_node_unsafe(p_binary_op);
  1990. }
  1991. }
  1992. }
  1993. // "is" operator is always a boolean anyway.
  1994. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1995. result.kind = GDScriptParser::DataType::BUILTIN;
  1996. result.builtin_type = Variant::BOOL;
  1997. } else {
  1998. ERR_PRINT("Parser bug: unknown binary operation.");
  1999. }
  2000. }
  2001. }
  2002. p_binary_op->set_datatype(result);
  2003. }
  2004. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await, bool p_is_root) {
  2005. bool all_is_constant = true;
  2006. HashMap<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  2007. for (int i = 0; i < p_call->arguments.size(); i++) {
  2008. reduce_expression(p_call->arguments[i]);
  2009. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  2010. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  2011. }
  2012. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  2013. }
  2014. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  2015. GDScriptParser::DataType call_type;
  2016. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  2017. // Call to name directly.
  2018. StringName function_name = p_call->function_name;
  2019. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  2020. if (builtin_type < Variant::VARIANT_MAX) {
  2021. // Is a builtin constructor.
  2022. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2023. call_type.kind = GDScriptParser::DataType::BUILTIN;
  2024. call_type.builtin_type = builtin_type;
  2025. if (builtin_type == Variant::OBJECT) {
  2026. call_type.kind = GDScriptParser::DataType::NATIVE;
  2027. call_type.native_type = function_name; // "Object".
  2028. }
  2029. bool safe_to_fold = true;
  2030. switch (builtin_type) {
  2031. // Those are stored by reference so not suited for compile-time construction.
  2032. // Because in this case they would be the same reference in all constructed values.
  2033. case Variant::OBJECT:
  2034. case Variant::DICTIONARY:
  2035. case Variant::ARRAY:
  2036. case Variant::PACKED_BYTE_ARRAY:
  2037. case Variant::PACKED_INT32_ARRAY:
  2038. case Variant::PACKED_INT64_ARRAY:
  2039. case Variant::PACKED_FLOAT32_ARRAY:
  2040. case Variant::PACKED_FLOAT64_ARRAY:
  2041. case Variant::PACKED_STRING_ARRAY:
  2042. case Variant::PACKED_VECTOR2_ARRAY:
  2043. case Variant::PACKED_VECTOR3_ARRAY:
  2044. case Variant::PACKED_COLOR_ARRAY:
  2045. safe_to_fold = false;
  2046. break;
  2047. default:
  2048. break;
  2049. }
  2050. if (all_is_constant && safe_to_fold) {
  2051. // Construct here.
  2052. Vector<const Variant *> args;
  2053. for (int i = 0; i < p_call->arguments.size(); i++) {
  2054. args.push_back(&(p_call->arguments[i]->reduced_value));
  2055. }
  2056. Callable::CallError err;
  2057. Variant value;
  2058. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  2059. switch (err.error) {
  2060. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  2061. 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,
  2062. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  2063. p_call->arguments[err.argument]);
  2064. break;
  2065. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  2066. String signature = Variant::get_type_name(builtin_type) + "(";
  2067. for (int i = 0; i < p_call->arguments.size(); i++) {
  2068. if (i > 0) {
  2069. signature += ", ";
  2070. }
  2071. signature += p_call->arguments[i]->get_datatype().to_string();
  2072. }
  2073. signature += ")";
  2074. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  2075. } break;
  2076. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2077. 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);
  2078. break;
  2079. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2080. 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);
  2081. break;
  2082. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2083. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2084. break; // Can't happen in a builtin constructor.
  2085. case Callable::CallError::CALL_OK:
  2086. p_call->is_constant = true;
  2087. p_call->reduced_value = value;
  2088. break;
  2089. }
  2090. } else {
  2091. // TODO: Check constructors without constants.
  2092. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  2093. if (p_call->arguments.size() == 1) {
  2094. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  2095. if (arg_type.is_variant()) {
  2096. mark_node_unsafe(p_call->arguments[0]);
  2097. } else {
  2098. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  2099. // Okay.
  2100. p_call->set_datatype(call_type);
  2101. return;
  2102. }
  2103. }
  2104. }
  2105. List<MethodInfo> constructors;
  2106. Variant::get_constructor_list(builtin_type, &constructors);
  2107. bool match = false;
  2108. for (const MethodInfo &info : constructors) {
  2109. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  2110. continue;
  2111. }
  2112. if (p_call->arguments.size() > info.arguments.size()) {
  2113. continue;
  2114. }
  2115. bool types_match = true;
  2116. for (int i = 0; i < p_call->arguments.size(); i++) {
  2117. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  2118. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  2119. types_match = false;
  2120. break;
  2121. #ifdef DEBUG_ENABLED
  2122. } else {
  2123. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT && builtin_type != Variant::INT) {
  2124. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  2125. }
  2126. #endif
  2127. }
  2128. }
  2129. if (types_match) {
  2130. match = true;
  2131. call_type = type_from_property(info.return_val);
  2132. break;
  2133. }
  2134. }
  2135. if (!match) {
  2136. String signature = Variant::get_type_name(builtin_type) + "(";
  2137. for (int i = 0; i < p_call->arguments.size(); i++) {
  2138. if (i > 0) {
  2139. signature += ", ";
  2140. }
  2141. signature += p_call->arguments[i]->get_datatype().to_string();
  2142. }
  2143. signature += ")";
  2144. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  2145. }
  2146. }
  2147. p_call->set_datatype(call_type);
  2148. return;
  2149. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  2150. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  2151. if (!p_is_root && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) {
  2152. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call);
  2153. }
  2154. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  2155. // Can call on compilation.
  2156. Vector<const Variant *> args;
  2157. for (int i = 0; i < p_call->arguments.size(); i++) {
  2158. args.push_back(&(p_call->arguments[i]->reduced_value));
  2159. }
  2160. Variant value;
  2161. Callable::CallError err;
  2162. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  2163. switch (err.error) {
  2164. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2165. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  2166. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2167. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  2168. p_call->arguments[err.argument]);
  2169. } break;
  2170. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2171. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2172. break;
  2173. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2174. 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);
  2175. break;
  2176. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2177. 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);
  2178. break;
  2179. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2180. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2181. break; // Can't happen in a builtin constructor.
  2182. case Callable::CallError::CALL_OK:
  2183. p_call->is_constant = true;
  2184. p_call->reduced_value = value;
  2185. break;
  2186. }
  2187. } else {
  2188. validate_call_arg(function_info, p_call);
  2189. }
  2190. p_call->set_datatype(type_from_property(function_info.return_val));
  2191. return;
  2192. } else if (Variant::has_utility_function(function_name)) {
  2193. MethodInfo function_info = info_from_utility_func(function_name);
  2194. if (!p_is_root && function_info.return_val.type == Variant::NIL && ((function_info.return_val.usage & PROPERTY_USAGE_NIL_IS_VARIANT) == 0)) {
  2195. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", function_name), p_call);
  2196. }
  2197. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  2198. // Can call on compilation.
  2199. Vector<const Variant *> args;
  2200. for (int i = 0; i < p_call->arguments.size(); i++) {
  2201. args.push_back(&(p_call->arguments[i]->reduced_value));
  2202. }
  2203. Variant value;
  2204. Callable::CallError err;
  2205. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  2206. switch (err.error) {
  2207. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2208. String expected_type_name;
  2209. if (err.argument < function_info.arguments.size()) {
  2210. expected_type_name = type_from_property(function_info.arguments[err.argument]).to_string();
  2211. } else {
  2212. expected_type_name = Variant::get_type_name((Variant::Type)err.expected);
  2213. }
  2214. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2215. expected_type_name, p_call->arguments[err.argument]->get_datatype().to_string()),
  2216. p_call->arguments[err.argument]);
  2217. } break;
  2218. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2219. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2220. break;
  2221. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2222. 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);
  2223. break;
  2224. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2225. 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);
  2226. break;
  2227. case Callable::CallError::CALL_ERROR_METHOD_NOT_CONST:
  2228. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2229. break; // Can't happen in a builtin constructor.
  2230. case Callable::CallError::CALL_OK:
  2231. p_call->is_constant = true;
  2232. p_call->reduced_value = value;
  2233. break;
  2234. }
  2235. } else {
  2236. validate_call_arg(function_info, p_call);
  2237. }
  2238. p_call->set_datatype(type_from_property(function_info.return_val));
  2239. return;
  2240. }
  2241. }
  2242. GDScriptParser::DataType base_type;
  2243. call_type.kind = GDScriptParser::DataType::VARIANT;
  2244. bool is_self = false;
  2245. if (p_call->is_super) {
  2246. base_type = parser->current_class->base_type;
  2247. base_type.is_meta_type = false;
  2248. is_self = true;
  2249. if (p_call->callee == nullptr && !lambda_stack.is_empty()) {
  2250. push_error("Cannot use `super()` inside a lambda.", p_call);
  2251. }
  2252. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2253. base_type = parser->current_class->get_datatype();
  2254. base_type.is_meta_type = false;
  2255. is_self = true;
  2256. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2257. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  2258. if (subscript->base == nullptr) {
  2259. // Invalid syntax, error already set on parser.
  2260. p_call->set_datatype(call_type);
  2261. mark_node_unsafe(p_call);
  2262. return;
  2263. }
  2264. if (!subscript->is_attribute) {
  2265. // Invalid call. Error already sent in parser.
  2266. // TODO: Could check if Callable here.
  2267. p_call->set_datatype(call_type);
  2268. mark_node_unsafe(p_call);
  2269. return;
  2270. }
  2271. if (subscript->attribute == nullptr) {
  2272. // Invalid call. Error already sent in parser.
  2273. p_call->set_datatype(call_type);
  2274. mark_node_unsafe(p_call);
  2275. return;
  2276. }
  2277. GDScriptParser::IdentifierNode *base_id = nullptr;
  2278. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2279. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2280. }
  2281. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2282. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2283. } else {
  2284. reduce_expression(subscript->base);
  2285. base_type = subscript->base->get_datatype();
  2286. }
  2287. } else {
  2288. // Invalid call. Error already sent in parser.
  2289. // TODO: Could check if Callable here too.
  2290. p_call->set_datatype(call_type);
  2291. mark_node_unsafe(p_call);
  2292. return;
  2293. }
  2294. bool is_static = false;
  2295. bool is_vararg = false;
  2296. int default_arg_count = 0;
  2297. GDScriptParser::DataType return_type;
  2298. List<GDScriptParser::DataType> par_types;
  2299. bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
  2300. 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)) {
  2301. // If the function require typed arrays we must make literals be typed.
  2302. for (const KeyValue<int, GDScriptParser::ArrayNode *> &E : arrays) {
  2303. int index = E.key;
  2304. if (index < par_types.size() && par_types[index].has_container_element_type()) {
  2305. update_array_literal_element_type(par_types[index], E.value);
  2306. }
  2307. }
  2308. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  2309. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) {
  2310. // Enum type is treated as a dictionary value for function calls.
  2311. base_type.is_meta_type = false;
  2312. }
  2313. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  2314. // Get the parent function above any lambda.
  2315. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  2316. while (parent_function->source_lambda) {
  2317. parent_function = parent_function->source_lambda->parent_function;
  2318. }
  2319. push_error(vformat(R"*(Cannot call non-static function "%s()" from static function "%s()".)*", p_call->function_name, parent_function->identifier->name), p_call);
  2320. } else if (!is_self && base_type.is_meta_type && !is_static) {
  2321. base_type.is_meta_type = false; // For `to_string()`.
  2322. 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);
  2323. } else if (is_self && !is_static) {
  2324. mark_lambda_use_self();
  2325. }
  2326. if (!p_is_root && return_type.is_hard_type() && return_type.kind == GDScriptParser::DataType::BUILTIN && return_type.builtin_type == Variant::NIL) {
  2327. push_error(vformat(R"*(Cannot get return value of call to "%s()" because it returns "void".)*", p_call->function_name), p_call);
  2328. }
  2329. #ifdef DEBUG_ENABLED
  2330. if (p_is_root && return_type.kind != GDScriptParser::DataType::UNRESOLVED && return_type.builtin_type != Variant::NIL) {
  2331. parser->push_warning(p_call, GDScriptWarning::RETURN_VALUE_DISCARDED, p_call->function_name);
  2332. }
  2333. if (is_static && !base_type.is_meta_type && !(is_self && parser->current_function != nullptr && parser->current_function->is_static)) {
  2334. String caller_type = String(base_type.native_type);
  2335. if (caller_type.is_empty()) {
  2336. caller_type = base_type.to_string();
  2337. }
  2338. parser->push_warning(p_call, GDScriptWarning::STATIC_CALLED_ON_INSTANCE, p_call->function_name, caller_type);
  2339. }
  2340. #endif // DEBUG_ENABLED
  2341. call_type = return_type;
  2342. } else {
  2343. bool found = false;
  2344. // Check if the name exists as something else.
  2345. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  2346. GDScriptParser::IdentifierNode *callee_id;
  2347. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2348. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  2349. } else {
  2350. // Can only be attribute.
  2351. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  2352. }
  2353. if (callee_id) {
  2354. reduce_identifier_from_base(callee_id, &base_type);
  2355. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  2356. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  2357. found = true;
  2358. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  2359. 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);
  2360. } else {
  2361. 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);
  2362. }
  2363. #ifdef DEBUG_ENABLED
  2364. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  2365. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  2366. mark_node_unsafe(p_call);
  2367. #endif
  2368. }
  2369. }
  2370. }
  2371. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  2372. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  2373. 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);
  2374. } else if (!found && (!p_call->is_super && base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::NATIVE && base_type.is_meta_type)) {
  2375. push_error(vformat(R"*(Static function "%s()" not found in base "%s".)*", p_call->function_name, base_type.native_type.operator String()), p_call);
  2376. }
  2377. }
  2378. if (call_type.is_coroutine && !p_is_await && !p_is_root) {
  2379. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call);
  2380. }
  2381. p_call->set_datatype(call_type);
  2382. }
  2383. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  2384. reduce_expression(p_cast->operand);
  2385. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  2386. if (!cast_type.is_set()) {
  2387. mark_node_unsafe(p_cast);
  2388. return;
  2389. }
  2390. cast_type = type_from_metatype(cast_type); // The casted value won't be a type name.
  2391. p_cast->set_datatype(cast_type);
  2392. if (!cast_type.is_variant()) {
  2393. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  2394. if (!op_type.is_variant()) {
  2395. bool valid = false;
  2396. if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2397. // Enum types are compatible between each other, so it's a safe cast.
  2398. valid = true;
  2399. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2400. // Convertint int to enum is always valid.
  2401. valid = true;
  2402. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  2403. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  2404. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  2405. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  2406. }
  2407. if (!valid) {
  2408. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  2409. }
  2410. }
  2411. } else {
  2412. mark_node_unsafe(p_cast);
  2413. }
  2414. #ifdef DEBUG_ENABLED
  2415. if (p_cast->operand->get_datatype().is_variant()) {
  2416. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  2417. mark_node_unsafe(p_cast);
  2418. }
  2419. #endif
  2420. // TODO: Perform cast on constants.
  2421. }
  2422. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2423. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, StringLikeVariantComparator> elements;
  2424. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2425. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2426. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  2427. reduce_expression(element.key);
  2428. }
  2429. reduce_expression(element.value);
  2430. if (element.key->is_constant) {
  2431. if (elements.has(element.key->reduced_value)) {
  2432. 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);
  2433. } else {
  2434. elements[element.key->reduced_value] = element.value;
  2435. }
  2436. }
  2437. }
  2438. // It's dictionary in any case.
  2439. GDScriptParser::DataType dict_type;
  2440. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2441. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  2442. dict_type.builtin_type = Variant::DICTIONARY;
  2443. dict_type.is_constant = true;
  2444. p_dictionary->set_datatype(dict_type);
  2445. }
  2446. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  2447. GDScriptParser::DataType result;
  2448. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2449. result.kind = GDScriptParser::DataType::NATIVE;
  2450. result.native_type = SNAME("Node");
  2451. result.builtin_type = Variant::OBJECT;
  2452. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, result.native_type)) {
  2453. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  2454. }
  2455. mark_lambda_use_self();
  2456. p_get_node->set_datatype(result);
  2457. }
  2458. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  2459. GDScriptParser::DataType type;
  2460. String path = ScriptServer::get_global_class_path(p_class_name);
  2461. String ext = path.get_extension();
  2462. if (ext == GDScriptLanguage::get_singleton()->get_extension()) {
  2463. Ref<GDScriptParserRef> ref = get_parser_for(path);
  2464. if (ref.is_null()) {
  2465. push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source);
  2466. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2467. type.kind = GDScriptParser::DataType::VARIANT;
  2468. return type;
  2469. }
  2470. Error err = ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  2471. if (err) {
  2472. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  2473. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2474. type.kind = GDScriptParser::DataType::VARIANT;
  2475. return type;
  2476. }
  2477. return ref->get_parser()->head->get_datatype();
  2478. } else {
  2479. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2480. type.kind = GDScriptParser::DataType::SCRIPT;
  2481. type.builtin_type = Variant::OBJECT;
  2482. type.script_type = ResourceLoader::load(path, "Script");
  2483. type.native_type = type.script_type->get_instance_base_type();
  2484. type.script_path = path;
  2485. type.is_constant = true;
  2486. type.is_meta_type = true;
  2487. return type;
  2488. }
  2489. }
  2490. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  2491. if (!p_identifier->get_datatype().has_no_type()) {
  2492. return;
  2493. }
  2494. GDScriptParser::DataType base;
  2495. if (p_base == nullptr) {
  2496. base = type_from_metatype(parser->current_class->get_datatype());
  2497. } else {
  2498. base = *p_base;
  2499. }
  2500. const StringName &name = p_identifier->name;
  2501. if (base.kind == GDScriptParser::DataType::ENUM) {
  2502. if (base.is_meta_type) {
  2503. if (base.enum_values.has(name)) {
  2504. p_identifier->is_constant = true;
  2505. p_identifier->reduced_value = base.enum_values[name];
  2506. GDScriptParser::DataType result;
  2507. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2508. result.kind = GDScriptParser::DataType::ENUM;
  2509. result.is_constant = true;
  2510. result.builtin_type = Variant::INT;
  2511. result.native_type = base.native_type;
  2512. result.enum_type = base.enum_type;
  2513. result.enum_values = base.enum_values;
  2514. p_identifier->set_datatype(result);
  2515. return;
  2516. } else {
  2517. // Consider as a Dictionary, so it can be anything.
  2518. // This will be evaluated in the next if block.
  2519. base.kind = GDScriptParser::DataType::BUILTIN;
  2520. base.builtin_type = Variant::DICTIONARY;
  2521. base.is_meta_type = false;
  2522. }
  2523. } else {
  2524. push_error(R"(Cannot get property from enum value.)", p_identifier);
  2525. return;
  2526. }
  2527. }
  2528. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  2529. if (base.is_meta_type) {
  2530. bool valid = true;
  2531. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  2532. if (valid) {
  2533. p_identifier->is_constant = true;
  2534. p_identifier->reduced_value = result;
  2535. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  2536. } else if (base.is_hard_type()) {
  2537. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  2538. }
  2539. } else {
  2540. switch (base.builtin_type) {
  2541. case Variant::NIL: {
  2542. if (base.is_hard_type()) {
  2543. push_error(vformat(R"(Invalid get index "%s" on base Nil)", name), p_identifier);
  2544. }
  2545. return;
  2546. }
  2547. case Variant::DICTIONARY: {
  2548. GDScriptParser::DataType dummy;
  2549. dummy.kind = GDScriptParser::DataType::VARIANT;
  2550. p_identifier->set_datatype(dummy);
  2551. return;
  2552. }
  2553. default: {
  2554. Callable::CallError temp;
  2555. Variant dummy;
  2556. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  2557. List<PropertyInfo> properties;
  2558. dummy.get_property_list(&properties);
  2559. for (const PropertyInfo &prop : properties) {
  2560. if (prop.name == name) {
  2561. p_identifier->set_datatype(type_from_property(prop));
  2562. return;
  2563. }
  2564. }
  2565. if (base.is_hard_type()) {
  2566. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  2567. }
  2568. }
  2569. }
  2570. }
  2571. return;
  2572. }
  2573. GDScriptParser::ClassNode *base_class = base.class_type;
  2574. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  2575. while (base_class != nullptr) {
  2576. if (base_class->identifier && base_class->identifier->name == name) {
  2577. p_identifier->set_datatype(base_class->get_datatype());
  2578. return;
  2579. }
  2580. if (base_class->has_member(name)) {
  2581. resolve_class_member(base_class, name, p_identifier);
  2582. GDScriptParser::ClassNode::Member member = base_class->get_member(name);
  2583. p_identifier->set_datatype(member.get_datatype());
  2584. switch (member.type) {
  2585. case GDScriptParser::ClassNode::Member::CONSTANT:
  2586. p_identifier->is_constant = true;
  2587. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2588. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2589. p_identifier->constant_source = member.constant;
  2590. break;
  2591. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2592. p_identifier->is_constant = true;
  2593. p_identifier->reduced_value = member.enum_value.value;
  2594. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2595. break;
  2596. case GDScriptParser::ClassNode::Member::ENUM:
  2597. p_identifier->is_constant = true;
  2598. p_identifier->reduced_value = member.m_enum->dictionary;
  2599. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2600. break;
  2601. case GDScriptParser::ClassNode::Member::VARIABLE:
  2602. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  2603. p_identifier->variable_source = member.variable;
  2604. member.variable->usages += 1;
  2605. break;
  2606. case GDScriptParser::ClassNode::Member::SIGNAL:
  2607. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_SIGNAL;
  2608. break;
  2609. case GDScriptParser::ClassNode::Member::FUNCTION:
  2610. p_identifier->set_datatype(make_callable_type(member.function->info));
  2611. break;
  2612. case GDScriptParser::ClassNode::Member::CLASS:
  2613. if (p_base != nullptr && p_base->is_constant) {
  2614. p_identifier->is_constant = true;
  2615. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2616. Error err = OK;
  2617. GDScript *scr = GDScriptCache::get_full_script(base.script_path, err).ptr();
  2618. ERR_FAIL_COND_MSG(err != OK, "Error while getting subscript full script.");
  2619. scr = scr->find_class(p_identifier->get_datatype().class_type->fqcn);
  2620. p_identifier->reduced_value = scr;
  2621. }
  2622. break;
  2623. default:
  2624. break; // Type already set.
  2625. }
  2626. return;
  2627. }
  2628. // Check outer constants.
  2629. // TODO: Allow outer static functions.
  2630. if (base_class->outer != nullptr) {
  2631. List<GDScriptParser::ClassNode *> script_classes;
  2632. get_class_node_current_scope_classes(base_class->outer, &script_classes);
  2633. for (GDScriptParser::ClassNode *script_class : script_classes) {
  2634. if (script_class->has_member(name)) {
  2635. resolve_class_member(script_class, name, p_identifier);
  2636. GDScriptParser::ClassNode::Member member = script_class->get_member(name);
  2637. switch (member.type) {
  2638. case GDScriptParser::ClassNode::Member::CONSTANT:
  2639. // TODO: Make sure loops won't cause problem. And make special error message for those.
  2640. p_identifier->set_datatype(member.get_datatype());
  2641. p_identifier->is_constant = true;
  2642. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2643. return;
  2644. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2645. p_identifier->set_datatype(member.get_datatype());
  2646. p_identifier->is_constant = true;
  2647. p_identifier->reduced_value = member.enum_value.value;
  2648. return;
  2649. case GDScriptParser::ClassNode::Member::ENUM:
  2650. p_identifier->set_datatype(member.get_datatype());
  2651. p_identifier->is_constant = true;
  2652. p_identifier->reduced_value = member.m_enum->dictionary;
  2653. return;
  2654. case GDScriptParser::ClassNode::Member::CLASS:
  2655. p_identifier->set_datatype(member.get_datatype());
  2656. return;
  2657. default:
  2658. break;
  2659. }
  2660. }
  2661. }
  2662. }
  2663. base_class = base_class->base_type.class_type;
  2664. }
  2665. // Check native members. No need for native class recursion because Node exposes all Object's properties.
  2666. const StringName &native = base.native_type;
  2667. if (class_exists(native)) {
  2668. MethodInfo method_info;
  2669. if (ClassDB::has_property(native, name)) {
  2670. StringName getter_name = ClassDB::get_property_getter(native, name);
  2671. MethodBind *getter = ClassDB::get_method(native, getter_name);
  2672. if (getter != nullptr) {
  2673. p_identifier->set_datatype(type_from_property(getter->get_return_info()));
  2674. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2675. }
  2676. return;
  2677. }
  2678. if (ClassDB::get_method_info(native, name, &method_info)) {
  2679. // Method is callable.
  2680. p_identifier->set_datatype(make_callable_type(method_info));
  2681. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2682. return;
  2683. }
  2684. if (ClassDB::get_signal(native, name, &method_info)) {
  2685. // Signal is a type too.
  2686. p_identifier->set_datatype(make_signal_type(method_info));
  2687. p_identifier->source = GDScriptParser::IdentifierNode::INHERITED_VARIABLE;
  2688. return;
  2689. }
  2690. if (ClassDB::has_enum(native, name)) {
  2691. p_identifier->set_datatype(make_native_enum_type(native, name));
  2692. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2693. return;
  2694. }
  2695. bool valid = false;
  2696. int64_t int_constant = ClassDB::get_integer_constant(native, name, &valid);
  2697. if (valid) {
  2698. p_identifier->is_constant = true;
  2699. p_identifier->reduced_value = int_constant;
  2700. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  2701. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2702. return;
  2703. }
  2704. }
  2705. }
  2706. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  2707. // TODO: This is opportunity to further infer types.
  2708. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  2709. if (current_enum) {
  2710. for (int i = 0; i < current_enum->values.size(); i++) {
  2711. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  2712. if (element.identifier->name == p_identifier->name) {
  2713. GDScriptParser::DataType type;
  2714. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2715. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM : GDScriptParser::DataType::BUILTIN;
  2716. type.builtin_type = Variant::INT;
  2717. type.is_constant = true;
  2718. if (element.parent_enum->identifier) {
  2719. type.enum_type = element.parent_enum->identifier->name;
  2720. }
  2721. p_identifier->set_datatype(type);
  2722. if (element.resolved) {
  2723. p_identifier->is_constant = true;
  2724. p_identifier->reduced_value = element.value;
  2725. } else {
  2726. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  2727. }
  2728. return; // Found anyway.
  2729. }
  2730. }
  2731. }
  2732. bool found_source = false;
  2733. // Check if identifier is local.
  2734. // If that's the case, the declaration already was solved before.
  2735. switch (p_identifier->source) {
  2736. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  2737. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  2738. found_source = true;
  2739. break;
  2740. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  2741. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2742. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  2743. p_identifier->is_constant = true;
  2744. // TODO: Constant should have a value on the node itself.
  2745. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  2746. found_source = true;
  2747. break;
  2748. case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
  2749. case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
  2750. mark_lambda_use_self();
  2751. break;
  2752. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2753. mark_lambda_use_self();
  2754. p_identifier->variable_source->usages++;
  2755. [[fallthrough]];
  2756. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  2757. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  2758. found_source = true;
  2759. break;
  2760. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  2761. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  2762. found_source = true;
  2763. break;
  2764. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  2765. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  2766. result.is_constant = true;
  2767. p_identifier->set_datatype(result);
  2768. found_source = true;
  2769. } break;
  2770. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  2771. break;
  2772. }
  2773. // Not a local, so check members.
  2774. if (!found_source) {
  2775. reduce_identifier_from_base(p_identifier);
  2776. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  2777. // Found.
  2778. found_source = true;
  2779. }
  2780. }
  2781. if (found_source) {
  2782. if ((p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) && parser->current_function && parser->current_function->is_static) {
  2783. // Get the parent function above any lambda.
  2784. GDScriptParser::FunctionNode *parent_function = parser->current_function;
  2785. while (parent_function->source_lambda) {
  2786. parent_function = parent_function->source_lambda->parent_function;
  2787. }
  2788. push_error(vformat(R"*(Cannot access instance variable "%s" from the static function "%s()".)*", p_identifier->name, parent_function->identifier->name), p_identifier);
  2789. }
  2790. if (!lambda_stack.is_empty()) {
  2791. // 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.
  2792. if (p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_VARIABLE || p_identifier->source == GDScriptParser::IdentifierNode::INHERITED_VARIABLE) {
  2793. mark_lambda_use_self();
  2794. return; // No need to capture.
  2795. }
  2796. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  2797. // Only capture locals and enum values. Constants are still accessible from the lambda using the script reference. If not, this method is done.
  2798. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT) {
  2799. return;
  2800. }
  2801. GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
  2802. // Make sure we aren't capturing variable in the same lambda.
  2803. // This also add captures for nested lambdas.
  2804. 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)) {
  2805. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  2806. function_test->source_lambda->captures.push_back(p_identifier);
  2807. function_test = function_test->source_lambda->parent_function;
  2808. }
  2809. }
  2810. return;
  2811. }
  2812. StringName name = p_identifier->name;
  2813. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2814. // Check globals. We make an exception for Variant::OBJECT because it's the base class for
  2815. // non-builtin types so we allow doing e.g. Object.new()
  2816. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  2817. if (builtin_type != Variant::OBJECT && builtin_type < Variant::VARIANT_MAX) {
  2818. if (can_be_builtin) {
  2819. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  2820. return;
  2821. } else {
  2822. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2823. }
  2824. }
  2825. if (class_exists(name)) {
  2826. p_identifier->set_datatype(make_native_meta_type(name));
  2827. return;
  2828. }
  2829. if (ScriptServer::is_global_class(name)) {
  2830. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  2831. return;
  2832. }
  2833. // Try singletons.
  2834. // Do this before globals because this might be a singleton loading another one before it's compiled.
  2835. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  2836. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  2837. if (autoload.is_singleton) {
  2838. // Singleton exists, so it's at least a Node.
  2839. GDScriptParser::DataType result;
  2840. result.kind = GDScriptParser::DataType::NATIVE;
  2841. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2842. if (ResourceLoader::get_resource_type(autoload.path) == "GDScript") {
  2843. Ref<GDScriptParserRef> singl_parser = get_parser_for(autoload.path);
  2844. if (singl_parser.is_valid()) {
  2845. Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  2846. if (err == OK) {
  2847. result = type_from_metatype(singl_parser->get_parser()->head->get_datatype());
  2848. }
  2849. }
  2850. } else if (ResourceLoader::get_resource_type(autoload.path) == "PackedScene") {
  2851. if (GDScriptLanguage::get_singleton()->has_any_global_constant(name)) {
  2852. Variant constant = GDScriptLanguage::get_singleton()->get_any_global_constant(name);
  2853. Node *node = Object::cast_to<Node>(constant);
  2854. if (node != nullptr) {
  2855. Ref<GDScript> scr = node->get_script();
  2856. if (scr.is_valid()) {
  2857. Ref<GDScriptParserRef> singl_parser = get_parser_for(scr->get_script_path());
  2858. if (singl_parser.is_valid()) {
  2859. Error err = singl_parser->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  2860. if (err == OK) {
  2861. result = type_from_metatype(singl_parser->get_parser()->head->get_datatype());
  2862. }
  2863. }
  2864. }
  2865. }
  2866. }
  2867. }
  2868. result.is_constant = true;
  2869. p_identifier->set_datatype(result);
  2870. return;
  2871. }
  2872. }
  2873. if (GDScriptLanguage::get_singleton()->has_any_global_constant(name)) {
  2874. Variant constant = GDScriptLanguage::get_singleton()->get_any_global_constant(name);
  2875. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2876. p_identifier->is_constant = true;
  2877. p_identifier->reduced_value = constant;
  2878. return;
  2879. }
  2880. // Not found.
  2881. // Check if it's a builtin function.
  2882. if (GDScriptUtilityFunctions::function_exists(name)) {
  2883. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2884. } else {
  2885. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2886. }
  2887. GDScriptParser::DataType dummy;
  2888. dummy.kind = GDScriptParser::DataType::VARIANT;
  2889. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2890. }
  2891. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  2892. // Lambda is always a Callable.
  2893. GDScriptParser::DataType lambda_type;
  2894. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  2895. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  2896. lambda_type.builtin_type = Variant::CALLABLE;
  2897. p_lambda->set_datatype(lambda_type);
  2898. if (p_lambda->function == nullptr) {
  2899. return;
  2900. }
  2901. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  2902. parser->current_function = p_lambda->function;
  2903. lambda_stack.push_back(p_lambda);
  2904. for (int i = 0; i < p_lambda->function->parameters.size(); i++) {
  2905. resolve_parameter(p_lambda->function->parameters[i]);
  2906. }
  2907. resolve_suite(p_lambda->function->body);
  2908. int captures_amount = p_lambda->captures.size();
  2909. if (captures_amount > 0) {
  2910. // Create space for lambda parameters.
  2911. // At the beginning to not mess with optional parameters.
  2912. int param_count = p_lambda->function->parameters.size();
  2913. p_lambda->function->parameters.resize(param_count + captures_amount);
  2914. for (int i = param_count - 1; i >= 0; i--) {
  2915. p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
  2916. p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  2917. }
  2918. // Add captures as extra parameters at the beginning.
  2919. for (int i = 0; i < p_lambda->captures.size(); i++) {
  2920. GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
  2921. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  2922. capture_param->identifier = capture;
  2923. capture_param->usages = capture->usages;
  2924. capture_param->set_datatype(capture->get_datatype());
  2925. p_lambda->function->parameters.write[i] = capture_param;
  2926. p_lambda->function->parameters_indices[capture->name] = i;
  2927. }
  2928. }
  2929. lambda_stack.pop_back();
  2930. parser->current_function = previous_function;
  2931. }
  2932. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2933. p_literal->reduced_value = p_literal->value;
  2934. p_literal->is_constant = true;
  2935. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  2936. }
  2937. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2938. if (!p_preload->path) {
  2939. return;
  2940. }
  2941. reduce_expression(p_preload->path);
  2942. if (!p_preload->path->is_constant) {
  2943. push_error("Preloaded path must be a constant string.", p_preload->path);
  2944. return;
  2945. }
  2946. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2947. push_error("Preloaded path must be a constant string.", p_preload->path);
  2948. } else {
  2949. p_preload->resolved_path = p_preload->path->reduced_value;
  2950. // TODO: Save this as script dependency.
  2951. if (p_preload->resolved_path.is_relative_path()) {
  2952. p_preload->resolved_path = parser->script_path.get_base_dir().path_join(p_preload->resolved_path);
  2953. }
  2954. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2955. if (!ResourceLoader::exists(p_preload->resolved_path)) {
  2956. Ref<FileAccess> file_check = FileAccess::create(FileAccess::ACCESS_RESOURCES);
  2957. if (file_check->file_exists(p_preload->resolved_path)) {
  2958. push_error(vformat(R"(Preload file "%s" has no resource loaders (unrecognized file extension).)", p_preload->resolved_path), p_preload->path);
  2959. } else {
  2960. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2961. }
  2962. } else {
  2963. // TODO: Don't load if validating: use completion cache.
  2964. // Must load GDScript and PackedScenes separately to permit cyclic references
  2965. // as ResourceLoader::load() detect and reject those.
  2966. if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "GDScript") {
  2967. Error err = OK;
  2968. Ref<GDScript> res = GDScriptCache::get_shallow_script(p_preload->resolved_path, err, parser->script_path);
  2969. p_preload->resource = res;
  2970. if (err != OK) {
  2971. push_error(vformat(R"(Could not preload resource script "%s".)", p_preload->resolved_path), p_preload->path);
  2972. }
  2973. } else if (ResourceLoader::get_resource_type(p_preload->resolved_path) == "PackedScene") {
  2974. Error err = OK;
  2975. Ref<PackedScene> res = GDScriptCache::get_packed_scene(p_preload->resolved_path, err, parser->script_path);
  2976. p_preload->resource = res;
  2977. if (err != OK) {
  2978. push_error(vformat(R"(Could not preload resource scene "%s".)", p_preload->resolved_path), p_preload->path);
  2979. }
  2980. } else {
  2981. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2982. if (p_preload->resource.is_null()) {
  2983. push_error(vformat(R"(Could not preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2984. }
  2985. }
  2986. }
  2987. }
  2988. p_preload->is_constant = true;
  2989. p_preload->reduced_value = p_preload->resource;
  2990. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  2991. }
  2992. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2993. p_self->is_constant = false;
  2994. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2995. mark_lambda_use_self();
  2996. }
  2997. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2998. if (p_subscript->base == nullptr) {
  2999. return;
  3000. }
  3001. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  3002. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  3003. } else {
  3004. reduce_expression(p_subscript->base);
  3005. if (p_subscript->base->type == GDScriptParser::Node::ARRAY) {
  3006. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_subscript->base));
  3007. } else if (p_subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  3008. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_subscript->base));
  3009. }
  3010. }
  3011. GDScriptParser::DataType result_type;
  3012. if (p_subscript->is_attribute) {
  3013. if (p_subscript->attribute == nullptr) {
  3014. return;
  3015. }
  3016. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  3017. bool valid = false;
  3018. // If the base is a metatype, use the analyzer instead.
  3019. if (p_subscript->base->is_constant && !base_type.is_meta_type) {
  3020. // Just try to get it.
  3021. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  3022. if (valid) {
  3023. p_subscript->is_constant = true;
  3024. p_subscript->reduced_value = value;
  3025. result_type = type_from_variant(value, p_subscript);
  3026. }
  3027. } else if (base_type.is_variant() || !base_type.is_hard_type()) {
  3028. valid = true;
  3029. result_type.kind = GDScriptParser::DataType::VARIANT;
  3030. mark_node_unsafe(p_subscript);
  3031. } else {
  3032. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  3033. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  3034. if (attr_type.is_set()) {
  3035. valid = true;
  3036. result_type = attr_type;
  3037. p_subscript->is_constant = p_subscript->attribute->is_constant;
  3038. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  3039. } else if (!base_type.is_meta_type || !base_type.is_constant) {
  3040. valid = base_type.kind != GDScriptParser::DataType::BUILTIN;
  3041. #ifdef DEBUG_ENABLED
  3042. if (valid) {
  3043. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  3044. }
  3045. #endif
  3046. result_type.kind = GDScriptParser::DataType::VARIANT;
  3047. }
  3048. }
  3049. if (!valid) {
  3050. 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);
  3051. result_type.kind = GDScriptParser::DataType::VARIANT;
  3052. }
  3053. } else {
  3054. if (p_subscript->index == nullptr) {
  3055. return;
  3056. }
  3057. reduce_expression(p_subscript->index);
  3058. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  3059. // Just try to get it.
  3060. bool valid = false;
  3061. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  3062. if (!valid) {
  3063. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  3064. } else {
  3065. p_subscript->is_constant = true;
  3066. p_subscript->reduced_value = value;
  3067. result_type = type_from_variant(value, p_subscript);
  3068. }
  3069. result_type.kind = GDScriptParser::DataType::VARIANT;
  3070. } else {
  3071. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  3072. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  3073. if (base_type.is_variant()) {
  3074. result_type.kind = GDScriptParser::DataType::VARIANT;
  3075. mark_node_unsafe(p_subscript);
  3076. } else {
  3077. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3078. // Check if indexing is valid.
  3079. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  3080. if (!error) {
  3081. switch (base_type.builtin_type) {
  3082. // Expect int or real as index.
  3083. case Variant::PACKED_BYTE_ARRAY:
  3084. case Variant::PACKED_COLOR_ARRAY:
  3085. case Variant::PACKED_FLOAT32_ARRAY:
  3086. case Variant::PACKED_FLOAT64_ARRAY:
  3087. case Variant::PACKED_INT32_ARRAY:
  3088. case Variant::PACKED_INT64_ARRAY:
  3089. case Variant::PACKED_STRING_ARRAY:
  3090. case Variant::PACKED_VECTOR2_ARRAY:
  3091. case Variant::PACKED_VECTOR3_ARRAY:
  3092. case Variant::ARRAY:
  3093. case Variant::STRING:
  3094. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  3095. break;
  3096. // Expect String only.
  3097. case Variant::RECT2:
  3098. case Variant::RECT2I:
  3099. case Variant::PLANE:
  3100. case Variant::QUATERNION:
  3101. case Variant::AABB:
  3102. case Variant::OBJECT:
  3103. error = index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3104. break;
  3105. // Expect String or number.
  3106. case Variant::BASIS:
  3107. case Variant::VECTOR2:
  3108. case Variant::VECTOR2I:
  3109. case Variant::VECTOR3:
  3110. case Variant::VECTOR3I:
  3111. case Variant::VECTOR4:
  3112. case Variant::VECTOR4I:
  3113. case Variant::TRANSFORM2D:
  3114. case Variant::TRANSFORM3D:
  3115. case Variant::PROJECTION:
  3116. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  3117. index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3118. break;
  3119. // Expect String or int.
  3120. case Variant::COLOR:
  3121. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME;
  3122. break;
  3123. // Don't support indexing, but we will check it later.
  3124. case Variant::RID:
  3125. case Variant::BOOL:
  3126. case Variant::CALLABLE:
  3127. case Variant::FLOAT:
  3128. case Variant::INT:
  3129. case Variant::NIL:
  3130. case Variant::NODE_PATH:
  3131. case Variant::SIGNAL:
  3132. case Variant::STRING_NAME:
  3133. break;
  3134. // Here for completeness.
  3135. case Variant::DICTIONARY:
  3136. case Variant::VARIANT_MAX:
  3137. break;
  3138. }
  3139. if (error) {
  3140. 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);
  3141. }
  3142. }
  3143. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  3144. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  3145. 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);
  3146. }
  3147. }
  3148. // Check resulting type if possible.
  3149. result_type.builtin_type = Variant::NIL;
  3150. result_type.kind = GDScriptParser::DataType::BUILTIN;
  3151. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  3152. if (base_type.kind != GDScriptParser::DataType::BUILTIN) {
  3153. base_type.builtin_type = Variant::OBJECT;
  3154. }
  3155. switch (base_type.builtin_type) {
  3156. // Can't index at all.
  3157. case Variant::RID:
  3158. case Variant::BOOL:
  3159. case Variant::CALLABLE:
  3160. case Variant::FLOAT:
  3161. case Variant::INT:
  3162. case Variant::NIL:
  3163. case Variant::NODE_PATH:
  3164. case Variant::SIGNAL:
  3165. case Variant::STRING_NAME:
  3166. result_type.kind = GDScriptParser::DataType::VARIANT;
  3167. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  3168. break;
  3169. // Return int.
  3170. case Variant::PACKED_BYTE_ARRAY:
  3171. case Variant::PACKED_INT32_ARRAY:
  3172. case Variant::PACKED_INT64_ARRAY:
  3173. case Variant::VECTOR2I:
  3174. case Variant::VECTOR3I:
  3175. case Variant::VECTOR4I:
  3176. result_type.builtin_type = Variant::INT;
  3177. break;
  3178. // Return float.
  3179. case Variant::PACKED_FLOAT32_ARRAY:
  3180. case Variant::PACKED_FLOAT64_ARRAY:
  3181. case Variant::VECTOR2:
  3182. case Variant::VECTOR3:
  3183. case Variant::VECTOR4:
  3184. case Variant::QUATERNION:
  3185. result_type.builtin_type = Variant::FLOAT;
  3186. break;
  3187. // Return Color.
  3188. case Variant::PACKED_COLOR_ARRAY:
  3189. result_type.builtin_type = Variant::COLOR;
  3190. break;
  3191. // Return String.
  3192. case Variant::PACKED_STRING_ARRAY:
  3193. case Variant::STRING:
  3194. result_type.builtin_type = Variant::STRING;
  3195. break;
  3196. // Return Vector2.
  3197. case Variant::PACKED_VECTOR2_ARRAY:
  3198. case Variant::TRANSFORM2D:
  3199. case Variant::RECT2:
  3200. result_type.builtin_type = Variant::VECTOR2;
  3201. break;
  3202. // Return Vector2I.
  3203. case Variant::RECT2I:
  3204. result_type.builtin_type = Variant::VECTOR2I;
  3205. break;
  3206. // Return Vector3.
  3207. case Variant::PACKED_VECTOR3_ARRAY:
  3208. case Variant::AABB:
  3209. case Variant::BASIS:
  3210. result_type.builtin_type = Variant::VECTOR3;
  3211. break;
  3212. // Depends on the index.
  3213. case Variant::TRANSFORM3D:
  3214. case Variant::PROJECTION:
  3215. case Variant::PLANE:
  3216. case Variant::COLOR:
  3217. case Variant::DICTIONARY:
  3218. case Variant::OBJECT:
  3219. result_type.kind = GDScriptParser::DataType::VARIANT;
  3220. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  3221. break;
  3222. // Can have an element type.
  3223. case Variant::ARRAY:
  3224. if (base_type.has_container_element_type()) {
  3225. result_type = base_type.get_container_element_type();
  3226. result_type.type_source = base_type.type_source;
  3227. } else {
  3228. result_type.kind = GDScriptParser::DataType::VARIANT;
  3229. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  3230. }
  3231. break;
  3232. // Here for completeness.
  3233. case Variant::VARIANT_MAX:
  3234. break;
  3235. }
  3236. }
  3237. }
  3238. }
  3239. p_subscript->set_datatype(result_type);
  3240. }
  3241. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  3242. reduce_expression(p_ternary_op->condition);
  3243. reduce_expression(p_ternary_op->true_expr);
  3244. reduce_expression(p_ternary_op->false_expr);
  3245. GDScriptParser::DataType result;
  3246. 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) {
  3247. p_ternary_op->is_constant = true;
  3248. if (p_ternary_op->condition->reduced_value.booleanize()) {
  3249. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  3250. } else {
  3251. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  3252. }
  3253. }
  3254. GDScriptParser::DataType true_type;
  3255. if (p_ternary_op->true_expr) {
  3256. true_type = p_ternary_op->true_expr->get_datatype();
  3257. } else {
  3258. true_type.kind = GDScriptParser::DataType::VARIANT;
  3259. }
  3260. GDScriptParser::DataType false_type;
  3261. if (p_ternary_op->false_expr) {
  3262. false_type = p_ternary_op->false_expr->get_datatype();
  3263. } else {
  3264. false_type.kind = GDScriptParser::DataType::VARIANT;
  3265. }
  3266. if (true_type.is_variant() || false_type.is_variant()) {
  3267. result.kind = GDScriptParser::DataType::VARIANT;
  3268. } else {
  3269. result = true_type;
  3270. if (!is_type_compatible(true_type, false_type)) {
  3271. result = false_type;
  3272. if (!is_type_compatible(false_type, true_type)) {
  3273. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3274. result.kind = GDScriptParser::DataType::VARIANT;
  3275. #ifdef DEBUG_ENABLED
  3276. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  3277. #endif
  3278. }
  3279. }
  3280. }
  3281. p_ternary_op->set_datatype(result);
  3282. }
  3283. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  3284. reduce_expression(p_unary_op->operand);
  3285. GDScriptParser::DataType result;
  3286. if (p_unary_op->operand == nullptr) {
  3287. result.kind = GDScriptParser::DataType::VARIANT;
  3288. p_unary_op->set_datatype(result);
  3289. return;
  3290. }
  3291. GDScriptParser::DataType operand_type = p_unary_op->operand->get_datatype();
  3292. if (p_unary_op->operand->is_constant) {
  3293. p_unary_op->is_constant = true;
  3294. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  3295. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  3296. }
  3297. if (operand_type.is_variant()) {
  3298. result.kind = GDScriptParser::DataType::VARIANT;
  3299. mark_node_unsafe(p_unary_op);
  3300. } else {
  3301. bool valid = false;
  3302. result = get_operation_type(p_unary_op->variant_op, operand_type, valid, p_unary_op);
  3303. if (!valid) {
  3304. 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);
  3305. }
  3306. }
  3307. p_unary_op->set_datatype(result);
  3308. }
  3309. void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
  3310. bool all_is_constant = true;
  3311. for (int i = 0; i < p_array->elements.size(); i++) {
  3312. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  3313. if (element->type == GDScriptParser::Node::ARRAY) {
  3314. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element));
  3315. } else if (element->type == GDScriptParser::Node::DICTIONARY) {
  3316. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element));
  3317. }
  3318. all_is_constant = all_is_constant && element->is_constant;
  3319. if (!all_is_constant) {
  3320. return;
  3321. }
  3322. }
  3323. Array array;
  3324. array.resize(p_array->elements.size());
  3325. for (int i = 0; i < p_array->elements.size(); i++) {
  3326. array[i] = p_array->elements[i]->reduced_value;
  3327. }
  3328. p_array->is_constant = true;
  3329. p_array->reduced_value = array;
  3330. }
  3331. void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  3332. bool all_is_constant = true;
  3333. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3334. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3335. if (element.value->type == GDScriptParser::Node::ARRAY) {
  3336. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(element.value));
  3337. } else if (element.value->type == GDScriptParser::Node::DICTIONARY) {
  3338. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(element.value));
  3339. }
  3340. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  3341. if (!all_is_constant) {
  3342. return;
  3343. }
  3344. }
  3345. Dictionary dict;
  3346. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3347. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3348. dict[element.key->reduced_value] = element.value->reduced_value;
  3349. }
  3350. p_dictionary->is_constant = true;
  3351. p_dictionary->reduced_value = dict;
  3352. }
  3353. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  3354. GDScriptParser::DataType result;
  3355. result.is_constant = true;
  3356. result.kind = GDScriptParser::DataType::BUILTIN;
  3357. result.builtin_type = p_value.get_type();
  3358. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  3359. if (p_value.get_type() == Variant::OBJECT) {
  3360. // Object is treated as a native type, not a builtin type.
  3361. result.kind = GDScriptParser::DataType::NATIVE;
  3362. Object *obj = p_value;
  3363. if (!obj) {
  3364. return GDScriptParser::DataType();
  3365. }
  3366. result.native_type = obj->get_class_name();
  3367. Ref<Script> scr = p_value; // Check if value is a script itself.
  3368. if (scr.is_valid()) {
  3369. result.is_meta_type = true;
  3370. } else {
  3371. result.is_meta_type = false;
  3372. scr = obj->get_script();
  3373. }
  3374. if (scr.is_valid()) {
  3375. Ref<GDScript> gds = scr;
  3376. if (gds.is_valid()) {
  3377. // This might be an inner class, so we want to get the parser for the root.
  3378. // But still get the inner class from that tree.
  3379. String script_path = gds->get_script_path();
  3380. Ref<GDScriptParserRef> ref = get_parser_for(script_path);
  3381. if (ref.is_null()) {
  3382. push_error(vformat(R"(Could not find script "%s".)", script_path), p_source);
  3383. GDScriptParser::DataType error_type;
  3384. error_type.kind = GDScriptParser::DataType::VARIANT;
  3385. return error_type;
  3386. }
  3387. Error err = ref->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  3388. GDScriptParser::ClassNode *found = nullptr;
  3389. if (err == OK) {
  3390. found = ref->get_parser()->find_class(gds->fully_qualified_name);
  3391. if (found != nullptr) {
  3392. err = resolve_class_inheritance(found, p_source);
  3393. }
  3394. }
  3395. if (err || found == nullptr) {
  3396. push_error(vformat(R"(Could not resolve script "%s".)", script_path), p_source);
  3397. GDScriptParser::DataType error_type;
  3398. error_type.kind = GDScriptParser::DataType::VARIANT;
  3399. return error_type;
  3400. }
  3401. result.kind = GDScriptParser::DataType::CLASS;
  3402. result.native_type = found->get_datatype().native_type;
  3403. result.class_type = found;
  3404. result.script_path = ref->get_parser()->script_path;
  3405. } else {
  3406. result.kind = GDScriptParser::DataType::SCRIPT;
  3407. result.native_type = scr->get_instance_base_type();
  3408. result.script_path = scr->get_path();
  3409. }
  3410. result.script_type = scr;
  3411. } else {
  3412. result.kind = GDScriptParser::DataType::NATIVE;
  3413. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  3414. result.is_meta_type = true;
  3415. }
  3416. }
  3417. }
  3418. return result;
  3419. }
  3420. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  3421. GDScriptParser::DataType result = p_meta_type;
  3422. result.is_meta_type = false;
  3423. result.is_constant = false;
  3424. if (p_meta_type.kind == GDScriptParser::DataType::ENUM) {
  3425. result.builtin_type = Variant::INT;
  3426. }
  3427. return result;
  3428. }
  3429. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  3430. GDScriptParser::DataType result;
  3431. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3432. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  3433. // Variant
  3434. result.kind = GDScriptParser::DataType::VARIANT;
  3435. return result;
  3436. }
  3437. result.builtin_type = p_property.type;
  3438. if (p_property.type == Variant::OBJECT) {
  3439. result.kind = GDScriptParser::DataType::NATIVE;
  3440. result.native_type = p_property.class_name == StringName() ? SNAME("Object") : p_property.class_name;
  3441. } else {
  3442. result.kind = GDScriptParser::DataType::BUILTIN;
  3443. result.builtin_type = p_property.type;
  3444. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  3445. // Check element type.
  3446. StringName elem_type_name = p_property.hint_string;
  3447. GDScriptParser::DataType elem_type;
  3448. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3449. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  3450. if (elem_builtin_type < Variant::VARIANT_MAX) {
  3451. // Builtin type.
  3452. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  3453. elem_type.builtin_type = elem_builtin_type;
  3454. } else if (class_exists(elem_type_name)) {
  3455. elem_type.kind = GDScriptParser::DataType::NATIVE;
  3456. elem_type.builtin_type = Variant::OBJECT;
  3457. elem_type.native_type = p_property.hint_string;
  3458. } else if (ScriptServer::is_global_class(elem_type_name)) {
  3459. // Just load this as it shouldn't be a GDScript.
  3460. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  3461. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  3462. elem_type.builtin_type = Variant::OBJECT;
  3463. elem_type.native_type = script->get_instance_base_type();
  3464. elem_type.script_type = script;
  3465. } else {
  3466. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  3467. }
  3468. elem_type.is_constant = false;
  3469. result.set_container_element_type(elem_type);
  3470. }
  3471. }
  3472. return result;
  3473. }
  3474. 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) {
  3475. r_static = false;
  3476. r_vararg = false;
  3477. r_default_arg_count = 0;
  3478. StringName function_name = p_function;
  3479. if (p_base_type.kind == GDScriptParser::DataType::ENUM) {
  3480. if (p_base_type.is_meta_type) {
  3481. // Enum type can be treated as a dictionary value.
  3482. p_base_type.kind = GDScriptParser::DataType::BUILTIN;
  3483. p_base_type.builtin_type = Variant::DICTIONARY;
  3484. p_base_type.is_meta_type = false;
  3485. } else {
  3486. push_error("Cannot call function on enum value.", p_source);
  3487. return false;
  3488. }
  3489. }
  3490. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  3491. // Construct a base type to get methods.
  3492. Callable::CallError err;
  3493. Variant dummy;
  3494. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  3495. if (err.error != Callable::CallError::CALL_OK) {
  3496. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  3497. }
  3498. List<MethodInfo> methods;
  3499. dummy.get_method_list(&methods);
  3500. for (const MethodInfo &E : methods) {
  3501. if (E.name == p_function) {
  3502. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3503. r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name);
  3504. return true;
  3505. }
  3506. }
  3507. return false;
  3508. }
  3509. if (p_is_constructor) {
  3510. function_name = "_init";
  3511. r_static = true;
  3512. }
  3513. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  3514. GDScriptParser::FunctionNode *found_function = nullptr;
  3515. while (found_function == nullptr && base_class != nullptr) {
  3516. if (base_class->has_member(function_name)) {
  3517. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3518. // TODO: If this is Callable it can have a better error message.
  3519. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  3520. return false;
  3521. }
  3522. resolve_class_member(base_class, function_name, p_source);
  3523. found_function = base_class->get_member(function_name).function;
  3524. }
  3525. resolve_class_inheritance(base_class, p_source);
  3526. base_class = base_class->base_type.class_type;
  3527. }
  3528. if (found_function != nullptr) {
  3529. r_static = p_is_constructor || found_function->is_static;
  3530. for (int i = 0; i < found_function->parameters.size(); i++) {
  3531. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  3532. if (found_function->parameters[i]->initializer != nullptr) {
  3533. r_default_arg_count++;
  3534. }
  3535. }
  3536. r_return_type = p_is_constructor ? p_base_type : found_function->get_datatype();
  3537. r_return_type.is_meta_type = false;
  3538. r_return_type.is_coroutine = found_function->is_coroutine;
  3539. return true;
  3540. }
  3541. Ref<Script> base_script = p_base_type.script_type;
  3542. while (base_script.is_valid() && base_script->has_method(function_name)) {
  3543. MethodInfo info = base_script->get_method_info(function_name);
  3544. if (!(info == MethodInfo())) {
  3545. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3546. }
  3547. base_script = base_script->get_base_script();
  3548. }
  3549. // If the base is a script, it might be trying to access members of the Script class itself.
  3550. if (p_base_type.is_meta_type && !p_is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  3551. MethodInfo info;
  3552. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  3553. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  3554. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3555. }
  3556. }
  3557. StringName base_native = p_base_type.native_type;
  3558. #ifdef DEBUG_ENABLED
  3559. if (base_native != StringName()) {
  3560. // Empty native class might happen in some Script implementations.
  3561. // Just ignore it.
  3562. if (!class_exists(base_native)) {
  3563. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  3564. }
  3565. }
  3566. #endif
  3567. if (p_is_constructor) {
  3568. // Native types always have a default constructor.
  3569. r_return_type = p_base_type;
  3570. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3571. r_return_type.is_meta_type = false;
  3572. return true;
  3573. }
  3574. MethodInfo info;
  3575. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  3576. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3577. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  3578. r_static = true;
  3579. }
  3580. return valid;
  3581. }
  3582. return false;
  3583. }
  3584. 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) {
  3585. r_return_type = type_from_property(p_info.return_val);
  3586. r_default_arg_count = p_info.default_arguments.size();
  3587. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  3588. r_static = (p_info.flags & METHOD_FLAG_STATIC) != 0;
  3589. for (const PropertyInfo &E : p_info.arguments) {
  3590. r_par_types.push_back(type_from_property(E));
  3591. }
  3592. return true;
  3593. }
  3594. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  3595. List<GDScriptParser::DataType> arg_types;
  3596. for (const PropertyInfo &E : p_method.arguments) {
  3597. arg_types.push_back(type_from_property(E));
  3598. }
  3599. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  3600. }
  3601. 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) {
  3602. bool valid = true;
  3603. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  3604. 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);
  3605. valid = false;
  3606. }
  3607. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  3608. 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()]);
  3609. valid = false;
  3610. }
  3611. for (int i = 0; i < p_call->arguments.size(); i++) {
  3612. if (i >= p_par_types.size()) {
  3613. // Already on vararg place.
  3614. break;
  3615. }
  3616. GDScriptParser::DataType par_type = p_par_types[i];
  3617. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  3618. if (arg_type.is_variant()) {
  3619. // Argument can be anything, so this is unsafe.
  3620. mark_node_unsafe(p_call->arguments[i]);
  3621. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  3622. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  3623. mark_node_unsafe(p_call);
  3624. if (!is_type_compatible(arg_type, par_type)) {
  3625. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  3626. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  3627. p_call->arguments[i]);
  3628. valid = false;
  3629. }
  3630. #ifdef DEBUG_ENABLED
  3631. } else {
  3632. 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) {
  3633. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  3634. }
  3635. #endif
  3636. }
  3637. }
  3638. return valid;
  3639. }
  3640. #ifdef DEBUG_ENABLED
  3641. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  3642. const StringName &name = p_local->name;
  3643. GDScriptParser::DataType base = parser->current_class->get_datatype();
  3644. GDScriptParser::ClassNode *base_class = base.class_type;
  3645. {
  3646. List<MethodInfo> gdscript_funcs;
  3647. GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);
  3648. for (MethodInfo &info : gdscript_funcs) {
  3649. if (info.name == name) {
  3650. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3651. return true;
  3652. }
  3653. }
  3654. if (Variant::has_utility_function(name)) {
  3655. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3656. return true;
  3657. } else if (ClassDB::class_exists(name)) {
  3658. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global class");
  3659. return true;
  3660. }
  3661. }
  3662. while (base_class != nullptr) {
  3663. if (base_class->has_member(name)) {
  3664. 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()));
  3665. return true;
  3666. }
  3667. base_class = base_class->base_type.class_type;
  3668. }
  3669. StringName parent = base.native_type;
  3670. while (parent != StringName()) {
  3671. ERR_FAIL_COND_V_MSG(!class_exists(parent), false, "Non-existent native base class.");
  3672. if (ClassDB::has_method(parent, name, true)) {
  3673. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  3674. return true;
  3675. } else if (ClassDB::has_signal(parent, name, true)) {
  3676. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  3677. return true;
  3678. } else if (ClassDB::has_property(parent, name, true)) {
  3679. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  3680. return true;
  3681. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  3682. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  3683. return true;
  3684. } else if (ClassDB::has_enum(parent, name, true)) {
  3685. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  3686. return true;
  3687. }
  3688. parent = ClassDB::get_parent_class(parent);
  3689. }
  3690. return false;
  3691. }
  3692. #endif
  3693. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  3694. // Unary version.
  3695. GDScriptParser::DataType nil_type;
  3696. nil_type.builtin_type = Variant::NIL;
  3697. nil_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3698. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  3699. }
  3700. 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) {
  3701. GDScriptParser::DataType result;
  3702. result.kind = GDScriptParser::DataType::VARIANT;
  3703. Variant::Type a_type = p_a.builtin_type;
  3704. Variant::Type b_type = p_b.builtin_type;
  3705. if (p_a.kind == GDScriptParser::DataType::ENUM) {
  3706. if (p_a.is_meta_type) {
  3707. a_type = Variant::DICTIONARY;
  3708. } else {
  3709. a_type = Variant::INT;
  3710. }
  3711. }
  3712. if (p_b.kind == GDScriptParser::DataType::ENUM) {
  3713. if (p_b.is_meta_type) {
  3714. b_type = Variant::DICTIONARY;
  3715. } else {
  3716. b_type = Variant::INT;
  3717. }
  3718. }
  3719. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  3720. bool hard_operation = p_a.is_hard_type() && p_b.is_hard_type();
  3721. bool validated = op_eval != nullptr;
  3722. if (hard_operation && !validated) {
  3723. r_valid = false;
  3724. return result;
  3725. } else if (hard_operation && validated) {
  3726. r_valid = true;
  3727. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3728. result.kind = GDScriptParser::DataType::BUILTIN;
  3729. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3730. } else if (!hard_operation && !validated) {
  3731. r_valid = true;
  3732. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3733. result.kind = GDScriptParser::DataType::VARIANT;
  3734. result.builtin_type = Variant::NIL;
  3735. } else if (!hard_operation && validated) {
  3736. r_valid = true;
  3737. result.type_source = GDScriptParser::DataType::INFERRED;
  3738. result.kind = GDScriptParser::DataType::BUILTIN;
  3739. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3740. }
  3741. return result;
  3742. }
  3743. // TODO: Add safe/unsafe return variable (for variant cases)
  3744. 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) {
  3745. // These return "true" so it doesn't affect users negatively.
  3746. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  3747. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  3748. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  3749. // Variant can receive anything.
  3750. return true;
  3751. }
  3752. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  3753. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  3754. return true;
  3755. }
  3756. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  3757. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  3758. if (!valid && p_allow_implicit_conversion) {
  3759. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  3760. }
  3761. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM && !p_source.is_meta_type) {
  3762. // Enum value is also integer.
  3763. valid = true;
  3764. }
  3765. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  3766. // Check the element type.
  3767. if (p_target.has_container_element_type()) {
  3768. if (!p_source.has_container_element_type()) {
  3769. // TODO: Maybe this is valid but unsafe?
  3770. // Variant array can't be appended to typed array.
  3771. valid = false;
  3772. } else {
  3773. valid = is_type_compatible(p_target.get_container_element_type(), p_source.get_container_element_type(), p_allow_implicit_conversion);
  3774. }
  3775. }
  3776. }
  3777. return valid;
  3778. }
  3779. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  3780. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  3781. #ifdef DEBUG_ENABLED
  3782. if (p_source_node) {
  3783. parser->push_warning(p_source_node, GDScriptWarning::INT_ASSIGNED_TO_ENUM);
  3784. }
  3785. #endif
  3786. return true;
  3787. }
  3788. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  3789. if (p_source.native_type == p_target.native_type) {
  3790. return true;
  3791. }
  3792. }
  3793. return false;
  3794. }
  3795. // From here on the target type is an object, so we have to test polymorphism.
  3796. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  3797. // null is acceptable in object.
  3798. return true;
  3799. }
  3800. StringName src_native;
  3801. Ref<Script> src_script;
  3802. const GDScriptParser::ClassNode *src_class = nullptr;
  3803. switch (p_source.kind) {
  3804. case GDScriptParser::DataType::NATIVE:
  3805. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  3806. // Non-native class cannot be supertype of native.
  3807. return false;
  3808. }
  3809. if (p_source.is_meta_type) {
  3810. src_native = GDScriptNativeClass::get_class_static();
  3811. } else {
  3812. src_native = p_source.native_type;
  3813. }
  3814. break;
  3815. case GDScriptParser::DataType::SCRIPT:
  3816. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  3817. // A script type cannot be a subtype of a GDScript class.
  3818. return false;
  3819. }
  3820. if (p_source.is_meta_type) {
  3821. src_native = p_source.script_type->get_class_name();
  3822. } else {
  3823. src_script = p_source.script_type;
  3824. src_native = src_script->get_instance_base_type();
  3825. }
  3826. break;
  3827. case GDScriptParser::DataType::CLASS:
  3828. if (p_source.is_meta_type) {
  3829. src_native = GDScript::get_class_static();
  3830. } else {
  3831. src_class = p_source.class_type;
  3832. const GDScriptParser::ClassNode *base = src_class;
  3833. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  3834. base = base->base_type.class_type;
  3835. }
  3836. src_native = base->base_type.native_type;
  3837. src_script = base->base_type.script_type;
  3838. }
  3839. break;
  3840. case GDScriptParser::DataType::VARIANT:
  3841. case GDScriptParser::DataType::BUILTIN:
  3842. case GDScriptParser::DataType::ENUM:
  3843. case GDScriptParser::DataType::RESOLVING:
  3844. case GDScriptParser::DataType::UNRESOLVED:
  3845. break; // Already solved before.
  3846. }
  3847. switch (p_target.kind) {
  3848. case GDScriptParser::DataType::NATIVE: {
  3849. if (p_target.is_meta_type) {
  3850. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  3851. }
  3852. return ClassDB::is_parent_class(src_native, p_target.native_type);
  3853. }
  3854. case GDScriptParser::DataType::SCRIPT:
  3855. if (p_target.is_meta_type) {
  3856. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  3857. }
  3858. while (src_script.is_valid()) {
  3859. if (src_script == p_target.script_type) {
  3860. return true;
  3861. }
  3862. src_script = src_script->get_base_script();
  3863. }
  3864. return false;
  3865. case GDScriptParser::DataType::CLASS:
  3866. if (p_target.is_meta_type) {
  3867. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  3868. }
  3869. while (src_class != nullptr) {
  3870. if (src_class->fqcn == p_target.class_type->fqcn) {
  3871. return true;
  3872. }
  3873. src_class = src_class->base_type.class_type;
  3874. }
  3875. return false;
  3876. case GDScriptParser::DataType::VARIANT:
  3877. case GDScriptParser::DataType::BUILTIN:
  3878. case GDScriptParser::DataType::ENUM:
  3879. case GDScriptParser::DataType::RESOLVING:
  3880. case GDScriptParser::DataType::UNRESOLVED:
  3881. break; // Already solved before.
  3882. }
  3883. return false;
  3884. }
  3885. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  3886. mark_node_unsafe(p_origin);
  3887. parser->push_error(p_message, p_origin);
  3888. }
  3889. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  3890. #ifdef DEBUG_ENABLED
  3891. if (p_node == nullptr) {
  3892. return;
  3893. }
  3894. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  3895. parser->unsafe_lines.insert(i);
  3896. }
  3897. #endif
  3898. }
  3899. void GDScriptAnalyzer::mark_lambda_use_self() {
  3900. for (GDScriptParser::LambdaNode *lambda : lambda_stack) {
  3901. lambda->use_self = true;
  3902. }
  3903. }
  3904. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  3905. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  3906. }
  3907. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  3908. Ref<GDScriptParserRef> ref;
  3909. if (depended_parsers.has(p_path)) {
  3910. ref = depended_parsers[p_path];
  3911. } else {
  3912. Error err = OK;
  3913. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  3914. if (ref.is_valid()) {
  3915. depended_parsers[p_path] = ref;
  3916. }
  3917. }
  3918. return ref;
  3919. }
  3920. Error GDScriptAnalyzer::resolve_inheritance() {
  3921. return resolve_class_inheritance(parser->head, true);
  3922. }
  3923. Error GDScriptAnalyzer::resolve_interface() {
  3924. resolve_class_interface(parser->head, true);
  3925. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3926. }
  3927. Error GDScriptAnalyzer::resolve_body() {
  3928. resolve_class_body(parser->head, true);
  3929. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3930. }
  3931. Error GDScriptAnalyzer::resolve_dependencies() {
  3932. for (KeyValue<String, Ref<GDScriptParserRef>> &K : depended_parsers) {
  3933. if (K.value.is_null()) {
  3934. return ERR_PARSE_ERROR;
  3935. }
  3936. K.value->raise_status(GDScriptParserRef::INHERITANCE_SOLVED);
  3937. }
  3938. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3939. }
  3940. Error GDScriptAnalyzer::analyze() {
  3941. parser->errors.clear();
  3942. Error err = OK;
  3943. err = resolve_inheritance();
  3944. if (err) {
  3945. return err;
  3946. }
  3947. resolve_interface();
  3948. resolve_body();
  3949. if (!parser->errors.is_empty()) {
  3950. return ERR_PARSE_ERROR;
  3951. }
  3952. return resolve_dependencies();
  3953. }
  3954. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  3955. parser = p_parser;
  3956. }