2
0

gdscript_analyzer.cpp 201 KB

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