gdscript_analyzer.cpp 196 KB

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