gdscript_analyzer.cpp 173 KB

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