gdscript_analyzer.cpp 166 KB

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