gdscript_analyzer.cpp 181 KB

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