gdscript_analyzer.cpp 171 KB

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