gdscript_analyzer.cpp 162 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167
  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 initalizer 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. Set<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. Set<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. for (int i = 0; i < p_function->parameters.size(); i++) {
  989. resolve_parameter(p_function->parameters[i]);
  990. #ifdef DEBUG_ENABLED
  991. if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) {
  992. parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name);
  993. }
  994. is_shadowing(p_function->parameters[i]->identifier, "function parameter");
  995. #endif // DEBUG_ENABLED
  996. #ifdef TOOLS_ENABLED
  997. if (p_function->parameters[i]->default_value && p_function->parameters[i]->default_value->is_constant) {
  998. p_function->default_arg_values.push_back(p_function->parameters[i]->default_value->reduced_value);
  999. }
  1000. #endif // TOOLS_ENABLED
  1001. }
  1002. if (p_function->identifier->name == GDScriptLanguage::get_singleton()->strings._init) {
  1003. // Constructor.
  1004. GDScriptParser::DataType return_type = parser->current_class->get_datatype();
  1005. return_type.is_meta_type = false;
  1006. p_function->set_datatype(return_type);
  1007. if (p_function->return_type) {
  1008. GDScriptParser::DataType declared_return = resolve_datatype(p_function->return_type);
  1009. if (declared_return.kind != GDScriptParser::DataType::BUILTIN || declared_return.builtin_type != Variant::NIL) {
  1010. push_error("Constructor cannot have an explicit return type.", p_function->return_type);
  1011. }
  1012. }
  1013. } else {
  1014. GDScriptParser::DataType return_type = resolve_datatype(p_function->return_type);
  1015. p_function->set_datatype(return_type);
  1016. #ifdef TOOLS_ENABLED
  1017. // Check if the function signature matches the parent. If not it's an error since it breaks polymorphism.
  1018. // Not for the constructor which can vary in signature.
  1019. GDScriptParser::DataType base_type = parser->current_class->base_type;
  1020. GDScriptParser::DataType parent_return_type;
  1021. List<GDScriptParser::DataType> parameters_types;
  1022. int default_par_count = 0;
  1023. bool is_static = false;
  1024. bool is_vararg = false;
  1025. 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)) {
  1026. bool valid = p_function->is_static == is_static;
  1027. valid = valid && parent_return_type == p_function->get_datatype();
  1028. int par_count_diff = p_function->parameters.size() - parameters_types.size();
  1029. valid = valid && par_count_diff >= 0;
  1030. valid = valid && p_function->default_arg_values.size() >= default_par_count + par_count_diff;
  1031. int i = 0;
  1032. for (const GDScriptParser::DataType &par_type : parameters_types) {
  1033. valid = valid && par_type == p_function->parameters[i++]->get_datatype();
  1034. }
  1035. if (!valid) {
  1036. // Compute parent signature as a string to show in the error message.
  1037. String parent_signature = parent_return_type.is_hard_type() ? parent_return_type.to_string() : "Variant";
  1038. if (parent_signature == "null") {
  1039. parent_signature = "void";
  1040. }
  1041. parent_signature += " " + p_function->identifier->name.operator String() + "(";
  1042. int j = 0;
  1043. for (const GDScriptParser::DataType &par_type : parameters_types) {
  1044. if (j > 0) {
  1045. parent_signature += ", ";
  1046. }
  1047. String parameter = par_type.to_string();
  1048. if (parameter == "null") {
  1049. parameter = "Variant";
  1050. }
  1051. parent_signature += parameter;
  1052. if (j == parameters_types.size() - default_par_count) {
  1053. parent_signature += " = default";
  1054. }
  1055. j++;
  1056. }
  1057. parent_signature += ")";
  1058. push_error(vformat(R"(The function signature doesn't match the parent. Parent signature is "%s".)", parent_signature), p_function);
  1059. }
  1060. }
  1061. #endif
  1062. }
  1063. parser->current_function = previous_function;
  1064. }
  1065. void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) {
  1066. if (p_function->resolved_body) {
  1067. return;
  1068. }
  1069. p_function->resolved_body = true;
  1070. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  1071. parser->current_function = p_function;
  1072. resolve_suite(p_function->body);
  1073. GDScriptParser::DataType return_type = p_function->body->get_datatype();
  1074. if (p_function->get_datatype().has_no_type() && return_type.is_set()) {
  1075. // Use the suite inferred type if return isn't explicitly set.
  1076. return_type.type_source = GDScriptParser::DataType::INFERRED;
  1077. p_function->set_datatype(p_function->body->get_datatype());
  1078. } 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)) {
  1079. if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) {
  1080. push_error(R"(Not all code paths return a value.)", p_function);
  1081. }
  1082. }
  1083. parser->current_function = previous_function;
  1084. }
  1085. void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) {
  1086. if (p_statement == nullptr) {
  1087. return;
  1088. }
  1089. switch (p_statement->type) {
  1090. case GDScriptParser::Node::IF:
  1091. case GDScriptParser::Node::FOR:
  1092. case GDScriptParser::Node::MATCH:
  1093. case GDScriptParser::Node::PATTERN:
  1094. case GDScriptParser::Node::RETURN:
  1095. case GDScriptParser::Node::WHILE:
  1096. // Use return or nested suite type as this suite type.
  1097. if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) {
  1098. // Mixed types.
  1099. // TODO: This could use the common supertype instead.
  1100. p_suite->datatype.kind = GDScriptParser::DataType::VARIANT;
  1101. p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  1102. } else {
  1103. p_suite->set_datatype(p_statement->get_datatype());
  1104. p_suite->datatype.type_source = GDScriptParser::DataType::INFERRED;
  1105. }
  1106. break;
  1107. default:
  1108. break;
  1109. }
  1110. }
  1111. void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
  1112. for (int i = 0; i < p_suite->statements.size(); i++) {
  1113. GDScriptParser::Node *stmt = p_suite->statements[i];
  1114. for (GDScriptParser::AnnotationNode *&annotation : stmt->annotations) {
  1115. annotation->apply(parser, stmt);
  1116. }
  1117. #ifdef DEBUG_ENABLED
  1118. Set<uint32_t> previously_ignored = parser->ignored_warning_codes;
  1119. for (uint32_t ignored_warning : stmt->ignored_warnings) {
  1120. parser->ignored_warning_codes.insert(ignored_warning);
  1121. }
  1122. #endif // DEBUG_ENABLED
  1123. resolve_node(stmt);
  1124. #ifdef DEBUG_ENABLED
  1125. parser->ignored_warning_codes = previously_ignored;
  1126. #endif // DEBUG_ENABLED
  1127. decide_suite_type(p_suite, stmt);
  1128. }
  1129. }
  1130. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  1131. reduce_expression(p_if->condition);
  1132. resolve_suite(p_if->true_block);
  1133. p_if->set_datatype(p_if->true_block->get_datatype());
  1134. if (p_if->false_block != nullptr) {
  1135. resolve_suite(p_if->false_block);
  1136. decide_suite_type(p_if, p_if->false_block);
  1137. }
  1138. }
  1139. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  1140. bool list_resolved = false;
  1141. // Optimize constant range() call to not allocate an array.
  1142. // Use int, Vector2i, Vector3i instead, which also can be used as range iterators.
  1143. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  1144. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  1145. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1146. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1147. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  1148. if (callee->name == "range") {
  1149. list_resolved = true;
  1150. if (call->arguments.size() < 1) {
  1151. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  1152. } else if (call->arguments.size() > 3) {
  1153. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  1154. } else {
  1155. // Now we can optimize it.
  1156. bool all_is_constant = true;
  1157. Vector<Variant> args;
  1158. args.resize(call->arguments.size());
  1159. for (int i = 0; i < call->arguments.size(); i++) {
  1160. reduce_expression(call->arguments[i]);
  1161. if (!call->arguments[i]->is_constant) {
  1162. all_is_constant = false;
  1163. } else if (all_is_constant) {
  1164. args.write[i] = call->arguments[i]->reduced_value;
  1165. }
  1166. GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype();
  1167. if (!arg_type.is_variant()) {
  1168. if (arg_type.kind != GDScriptParser::DataType::BUILTIN) {
  1169. all_is_constant = false;
  1170. 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]);
  1171. } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) {
  1172. all_is_constant = false;
  1173. 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]);
  1174. }
  1175. }
  1176. }
  1177. Variant reduced;
  1178. if (all_is_constant) {
  1179. switch (args.size()) {
  1180. case 1:
  1181. reduced = args[0];
  1182. break;
  1183. case 2:
  1184. reduced = Vector2i(args[0], args[1]);
  1185. break;
  1186. case 3:
  1187. reduced = Vector3i(args[0], args[1], args[2]);
  1188. break;
  1189. }
  1190. p_for->list->is_constant = true;
  1191. p_for->list->reduced_value = reduced;
  1192. }
  1193. }
  1194. if (p_for->list->is_constant) {
  1195. p_for->list->set_datatype(type_from_variant(p_for->list->reduced_value, p_for->list));
  1196. } else {
  1197. GDScriptParser::DataType list_type;
  1198. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1199. list_type.kind = GDScriptParser::DataType::BUILTIN;
  1200. list_type.builtin_type = Variant::ARRAY;
  1201. p_for->list->set_datatype(list_type);
  1202. }
  1203. }
  1204. }
  1205. }
  1206. GDScriptParser::DataType variable_type;
  1207. if (list_resolved) {
  1208. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1209. variable_type.kind = GDScriptParser::DataType::BUILTIN;
  1210. variable_type.builtin_type = Variant::INT; // Can this ever be a float or something else?
  1211. p_for->variable->set_datatype(variable_type);
  1212. } else if (p_for->list) {
  1213. resolve_node(p_for->list);
  1214. if (p_for->list->datatype.has_container_element_type()) {
  1215. variable_type = p_for->list->datatype.get_container_element_type();
  1216. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1217. } else if (p_for->list->datatype.is_typed_container_type()) {
  1218. variable_type = p_for->list->datatype.get_typed_container_type();
  1219. variable_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1220. } else {
  1221. // Last resort
  1222. // TODO: Must other cases be handled? Must we mark as unsafe?
  1223. variable_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1224. variable_type.kind = GDScriptParser::DataType::VARIANT;
  1225. }
  1226. }
  1227. if (p_for->variable) {
  1228. p_for->variable->set_datatype(variable_type);
  1229. }
  1230. resolve_suite(p_for->loop);
  1231. p_for->set_datatype(p_for->loop->get_datatype());
  1232. #ifdef DEBUG_ENABLED
  1233. if (p_for->variable) {
  1234. is_shadowing(p_for->variable, R"("for" iterator variable)");
  1235. }
  1236. #endif
  1237. }
  1238. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  1239. resolve_node(p_while->condition);
  1240. resolve_suite(p_while->loop);
  1241. p_while->set_datatype(p_while->loop->get_datatype());
  1242. }
  1243. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) {
  1244. GDScriptParser::DataType type;
  1245. type.kind = GDScriptParser::DataType::VARIANT; // By default.
  1246. GDScriptParser::DataType specified_type;
  1247. if (p_variable->datatype_specifier != nullptr) {
  1248. specified_type = resolve_datatype(p_variable->datatype_specifier);
  1249. specified_type.is_meta_type = false;
  1250. }
  1251. if (p_variable->initializer != nullptr) {
  1252. reduce_expression(p_variable->initializer);
  1253. if ((p_variable->infer_datatype || (p_variable->datatype_specifier != nullptr && specified_type.has_container_element_type())) && p_variable->initializer->type == GDScriptParser::Node::ARRAY) {
  1254. // Typed array.
  1255. GDScriptParser::ArrayNode *array = static_cast<GDScriptParser::ArrayNode *>(p_variable->initializer);
  1256. // Can only infer typed array if it has elements.
  1257. if ((p_variable->infer_datatype && array->elements.size() > 0) || p_variable->datatype_specifier != nullptr) {
  1258. update_array_literal_element_type(specified_type, array);
  1259. }
  1260. }
  1261. type = p_variable->initializer->get_datatype();
  1262. if (p_variable->infer_datatype) {
  1263. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1264. if (type.has_no_type()) {
  1265. 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);
  1266. } else if (type.is_variant()) {
  1267. 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);
  1268. } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1269. 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);
  1270. }
  1271. } else {
  1272. type.type_source = GDScriptParser::DataType::INFERRED;
  1273. }
  1274. #ifdef DEBUG_ENABLED
  1275. if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1276. parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_variable->initializer)->function_name);
  1277. }
  1278. #endif
  1279. }
  1280. if (p_variable->datatype_specifier != nullptr) {
  1281. type = specified_type;
  1282. type.is_meta_type = false;
  1283. if (p_variable->initializer != nullptr) {
  1284. if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true, p_variable->initializer)) {
  1285. // Try reverse test since it can be a masked subtype.
  1286. if (!is_type_compatible(p_variable->initializer->get_datatype(), type, true, p_variable->initializer)) {
  1287. 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);
  1288. } else {
  1289. // TODO: Add warning.
  1290. mark_node_unsafe(p_variable->initializer);
  1291. p_variable->use_conversion_assign = true;
  1292. }
  1293. #ifdef DEBUG_ENABLED
  1294. } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  1295. parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1296. #endif
  1297. }
  1298. if (p_variable->initializer->get_datatype().is_variant() && !type.is_variant()) {
  1299. // TODO: Warn unsafe assign.
  1300. mark_node_unsafe(p_variable->initializer);
  1301. p_variable->use_conversion_assign = true;
  1302. }
  1303. }
  1304. } else if (p_variable->infer_datatype) {
  1305. if (type.has_no_type()) {
  1306. 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);
  1307. }
  1308. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1309. }
  1310. type.is_constant = false;
  1311. p_variable->set_datatype(type);
  1312. #ifdef DEBUG_ENABLED
  1313. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  1314. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  1315. } else if (p_variable->assignments == 0) {
  1316. parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
  1317. }
  1318. is_shadowing(p_variable->identifier, "variable");
  1319. #endif
  1320. }
  1321. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) {
  1322. GDScriptParser::DataType type;
  1323. if (p_constant->initializer != nullptr) {
  1324. reduce_expression(p_constant->initializer);
  1325. if (p_constant->initializer->type == GDScriptParser::Node::ARRAY) {
  1326. const_fold_array(static_cast<GDScriptParser::ArrayNode *>(p_constant->initializer));
  1327. } else if (p_constant->initializer->type == GDScriptParser::Node::DICTIONARY) {
  1328. const_fold_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_constant->initializer));
  1329. }
  1330. if (!p_constant->initializer->is_constant) {
  1331. push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer);
  1332. }
  1333. type = p_constant->initializer->get_datatype();
  1334. #ifdef DEBUG_ENABLED
  1335. if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  1336. parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_constant->initializer)->function_name);
  1337. }
  1338. #endif
  1339. }
  1340. if (p_constant->datatype_specifier != nullptr) {
  1341. GDScriptParser::DataType explicit_type = resolve_datatype(p_constant->datatype_specifier);
  1342. explicit_type.is_meta_type = false;
  1343. if (!is_type_compatible(explicit_type, type)) {
  1344. 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);
  1345. #ifdef DEBUG_ENABLED
  1346. } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) {
  1347. parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  1348. #endif
  1349. }
  1350. type = explicit_type;
  1351. } else if (p_constant->infer_datatype) {
  1352. if (type.has_no_type()) {
  1353. 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);
  1354. }
  1355. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1356. }
  1357. type.is_constant = true;
  1358. p_constant->set_datatype(type);
  1359. #ifdef DEBUG_ENABLED
  1360. if (p_constant->usages == 0) {
  1361. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1362. }
  1363. is_shadowing(p_constant->identifier, "constant");
  1364. #endif
  1365. }
  1366. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1367. reduce_expression(p_assert->condition);
  1368. if (p_assert->message != nullptr) {
  1369. reduce_expression(p_assert->message);
  1370. if (!p_assert->message->is_constant || p_assert->message->reduced_value.get_type() != Variant::STRING) {
  1371. push_error(R"(Expected constant string for assert error message.)", p_assert->message);
  1372. }
  1373. }
  1374. p_assert->set_datatype(p_assert->condition->get_datatype());
  1375. #ifdef DEBUG_ENABLED
  1376. if (p_assert->condition->is_constant) {
  1377. if (p_assert->condition->reduced_value.booleanize()) {
  1378. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1379. } else {
  1380. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1381. }
  1382. }
  1383. #endif
  1384. }
  1385. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1386. reduce_expression(p_match->test);
  1387. for (int i = 0; i < p_match->branches.size(); i++) {
  1388. resolve_match_branch(p_match->branches[i], p_match->test);
  1389. decide_suite_type(p_match, p_match->branches[i]);
  1390. }
  1391. }
  1392. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1393. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1394. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1395. }
  1396. resolve_suite(p_match_branch->block);
  1397. decide_suite_type(p_match_branch, p_match_branch->block);
  1398. }
  1399. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  1400. if (p_match_pattern == nullptr) {
  1401. return;
  1402. }
  1403. GDScriptParser::DataType result;
  1404. switch (p_match_pattern->pattern_type) {
  1405. case GDScriptParser::PatternNode::PT_LITERAL:
  1406. if (p_match_pattern->literal) {
  1407. reduce_literal(p_match_pattern->literal);
  1408. result = p_match_pattern->literal->get_datatype();
  1409. }
  1410. break;
  1411. case GDScriptParser::PatternNode::PT_EXPRESSION:
  1412. if (p_match_pattern->expression) {
  1413. reduce_expression(p_match_pattern->expression);
  1414. if (!p_match_pattern->expression->is_constant) {
  1415. push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression);
  1416. }
  1417. result = p_match_pattern->expression->get_datatype();
  1418. }
  1419. break;
  1420. case GDScriptParser::PatternNode::PT_BIND:
  1421. if (p_match_test != nullptr) {
  1422. result = p_match_test->get_datatype();
  1423. } else {
  1424. result.kind = GDScriptParser::DataType::VARIANT;
  1425. }
  1426. p_match_pattern->bind->set_datatype(result);
  1427. #ifdef DEBUG_ENABLED
  1428. is_shadowing(p_match_pattern->bind, "pattern bind");
  1429. if (p_match_pattern->bind->usages == 0) {
  1430. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNASSIGNED_VARIABLE, p_match_pattern->bind->name);
  1431. }
  1432. #endif
  1433. break;
  1434. case GDScriptParser::PatternNode::PT_ARRAY:
  1435. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  1436. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  1437. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  1438. }
  1439. result = p_match_pattern->get_datatype();
  1440. break;
  1441. case GDScriptParser::PatternNode::PT_DICTIONARY:
  1442. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  1443. if (p_match_pattern->dictionary[i].key) {
  1444. reduce_expression(p_match_pattern->dictionary[i].key);
  1445. if (!p_match_pattern->dictionary[i].key->is_constant) {
  1446. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->dictionary[i].key);
  1447. }
  1448. }
  1449. if (p_match_pattern->dictionary[i].value_pattern) {
  1450. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  1451. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  1452. }
  1453. }
  1454. result = p_match_pattern->get_datatype();
  1455. break;
  1456. case GDScriptParser::PatternNode::PT_WILDCARD:
  1457. case GDScriptParser::PatternNode::PT_REST:
  1458. result.kind = GDScriptParser::DataType::VARIANT;
  1459. break;
  1460. }
  1461. p_match_pattern->set_datatype(result);
  1462. }
  1463. void GDScriptAnalyzer::resolve_parameter(GDScriptParser::ParameterNode *p_parameter) {
  1464. GDScriptParser::DataType result;
  1465. result.kind = GDScriptParser::DataType::VARIANT;
  1466. if (p_parameter->default_value != nullptr) {
  1467. reduce_expression(p_parameter->default_value);
  1468. result = p_parameter->default_value->get_datatype();
  1469. if (p_parameter->infer_datatype) {
  1470. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1471. } else {
  1472. result.type_source = GDScriptParser::DataType::INFERRED;
  1473. }
  1474. result.is_constant = false;
  1475. }
  1476. if (p_parameter->datatype_specifier != nullptr) {
  1477. result = resolve_datatype(p_parameter->datatype_specifier);
  1478. result.is_meta_type = false;
  1479. if (p_parameter->default_value != nullptr) {
  1480. if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) {
  1481. 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);
  1482. } else if (p_parameter->default_value->get_datatype().is_variant()) {
  1483. mark_node_unsafe(p_parameter);
  1484. }
  1485. }
  1486. }
  1487. if (result.builtin_type == Variant::Type::NIL && result.type_source == GDScriptParser::DataType::ANNOTATED_INFERRED && p_parameter->datatype_specifier == nullptr) {
  1488. 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);
  1489. }
  1490. p_parameter->set_datatype(result);
  1491. }
  1492. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  1493. GDScriptParser::DataType result;
  1494. GDScriptParser::DataType expected_type;
  1495. bool has_expected_type = false;
  1496. if (parser->current_function != nullptr) {
  1497. expected_type = parser->current_function->get_datatype();
  1498. has_expected_type = true;
  1499. }
  1500. if (p_return->return_value != nullptr) {
  1501. reduce_expression(p_return->return_value);
  1502. if (p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1503. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1504. if (has_expected_type && expected_type.has_container_element_type() && p_return->return_value->type == GDScriptParser::Node::ARRAY) {
  1505. update_array_literal_element_type(expected_type, static_cast<GDScriptParser::ArrayNode *>(p_return->return_value));
  1506. }
  1507. }
  1508. result = p_return->return_value->get_datatype();
  1509. } else {
  1510. // Return type is null by default.
  1511. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1512. result.kind = GDScriptParser::DataType::BUILTIN;
  1513. result.builtin_type = Variant::NIL;
  1514. result.is_constant = true;
  1515. }
  1516. if (has_expected_type) {
  1517. expected_type.is_meta_type = false;
  1518. if (expected_type.is_hard_type()) {
  1519. if (!is_type_compatible(expected_type, result)) {
  1520. // Try other way. Okay but not safe.
  1521. if (!is_type_compatible(result, expected_type)) {
  1522. 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);
  1523. } else {
  1524. // TODO: Add warning.
  1525. mark_node_unsafe(p_return);
  1526. }
  1527. #ifdef DEBUG_ENABLED
  1528. } else if (expected_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  1529. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  1530. } else if (result.is_variant()) {
  1531. mark_node_unsafe(p_return);
  1532. #endif
  1533. }
  1534. }
  1535. }
  1536. p_return->set_datatype(result);
  1537. }
  1538. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression, bool p_is_root) {
  1539. // This one makes some magic happen.
  1540. if (p_expression == nullptr) {
  1541. return;
  1542. }
  1543. if (p_expression->reduced) {
  1544. // Don't do this more than once.
  1545. return;
  1546. }
  1547. p_expression->reduced = true;
  1548. switch (p_expression->type) {
  1549. case GDScriptParser::Node::ARRAY:
  1550. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  1551. break;
  1552. case GDScriptParser::Node::ASSIGNMENT:
  1553. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  1554. break;
  1555. case GDScriptParser::Node::AWAIT:
  1556. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  1557. break;
  1558. case GDScriptParser::Node::BINARY_OPERATOR:
  1559. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  1560. break;
  1561. case GDScriptParser::Node::CALL:
  1562. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression), p_is_root);
  1563. break;
  1564. case GDScriptParser::Node::CAST:
  1565. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  1566. break;
  1567. case GDScriptParser::Node::DICTIONARY:
  1568. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  1569. break;
  1570. case GDScriptParser::Node::GET_NODE:
  1571. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  1572. break;
  1573. case GDScriptParser::Node::IDENTIFIER:
  1574. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  1575. break;
  1576. case GDScriptParser::Node::LAMBDA:
  1577. reduce_lambda(static_cast<GDScriptParser::LambdaNode *>(p_expression));
  1578. break;
  1579. case GDScriptParser::Node::LITERAL:
  1580. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  1581. break;
  1582. case GDScriptParser::Node::PRELOAD:
  1583. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  1584. break;
  1585. case GDScriptParser::Node::SELF:
  1586. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  1587. break;
  1588. case GDScriptParser::Node::SUBSCRIPT:
  1589. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  1590. break;
  1591. case GDScriptParser::Node::TERNARY_OPERATOR:
  1592. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression));
  1593. break;
  1594. case GDScriptParser::Node::UNARY_OPERATOR:
  1595. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  1596. break;
  1597. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  1598. case GDScriptParser::Node::NONE:
  1599. case GDScriptParser::Node::ANNOTATION:
  1600. case GDScriptParser::Node::ASSERT:
  1601. case GDScriptParser::Node::BREAK:
  1602. case GDScriptParser::Node::BREAKPOINT:
  1603. case GDScriptParser::Node::CLASS:
  1604. case GDScriptParser::Node::CONSTANT:
  1605. case GDScriptParser::Node::CONTINUE:
  1606. case GDScriptParser::Node::ENUM:
  1607. case GDScriptParser::Node::FOR:
  1608. case GDScriptParser::Node::FUNCTION:
  1609. case GDScriptParser::Node::IF:
  1610. case GDScriptParser::Node::MATCH:
  1611. case GDScriptParser::Node::MATCH_BRANCH:
  1612. case GDScriptParser::Node::PARAMETER:
  1613. case GDScriptParser::Node::PASS:
  1614. case GDScriptParser::Node::PATTERN:
  1615. case GDScriptParser::Node::RETURN:
  1616. case GDScriptParser::Node::SIGNAL:
  1617. case GDScriptParser::Node::SUITE:
  1618. case GDScriptParser::Node::TYPE:
  1619. case GDScriptParser::Node::VARIABLE:
  1620. case GDScriptParser::Node::WHILE:
  1621. ERR_FAIL_MSG("Reaching unreachable case");
  1622. }
  1623. }
  1624. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  1625. for (int i = 0; i < p_array->elements.size(); i++) {
  1626. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  1627. reduce_expression(element);
  1628. }
  1629. // It's array in any case.
  1630. GDScriptParser::DataType arr_type;
  1631. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1632. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  1633. arr_type.builtin_type = Variant::ARRAY;
  1634. arr_type.is_constant = true;
  1635. p_array->set_datatype(arr_type);
  1636. }
  1637. // When an array literal is stored (or passed as function argument) to a typed context, we then assume the array is typed.
  1638. // This function determines which type is that (if any).
  1639. void GDScriptAnalyzer::update_array_literal_element_type(const GDScriptParser::DataType &p_base_type, GDScriptParser::ArrayNode *p_array_literal) {
  1640. GDScriptParser::DataType array_type = p_array_literal->get_datatype();
  1641. if (p_array_literal->elements.size() == 0) {
  1642. // Empty array literal, just make the same type as the storage.
  1643. array_type.set_container_element_type(p_base_type.get_container_element_type());
  1644. } else {
  1645. // Check if elements match.
  1646. bool all_same_type = true;
  1647. bool all_have_type = true;
  1648. GDScriptParser::DataType element_type;
  1649. for (int i = 0; i < p_array_literal->elements.size(); i++) {
  1650. if (i == 0) {
  1651. element_type = p_array_literal->elements[0]->get_datatype();
  1652. } else {
  1653. GDScriptParser::DataType this_element_type = p_array_literal->elements[i]->get_datatype();
  1654. if (this_element_type.has_no_type()) {
  1655. all_same_type = false;
  1656. all_have_type = false;
  1657. break;
  1658. } else if (element_type != this_element_type) {
  1659. if (!is_type_compatible(element_type, this_element_type, false)) {
  1660. if (is_type_compatible(this_element_type, element_type, false)) {
  1661. // This element is a super-type to the previous type, so we use the super-type.
  1662. element_type = this_element_type;
  1663. } else {
  1664. // It's incompatible.
  1665. all_same_type = false;
  1666. break;
  1667. }
  1668. }
  1669. }
  1670. }
  1671. }
  1672. if (all_same_type) {
  1673. element_type.is_constant = false;
  1674. array_type.set_container_element_type(element_type);
  1675. } else if (all_have_type) {
  1676. 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);
  1677. }
  1678. }
  1679. // Update the type on the value itself.
  1680. p_array_literal->set_datatype(array_type);
  1681. }
  1682. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  1683. reduce_expression(p_assignment->assignee);
  1684. reduce_expression(p_assignment->assigned_value);
  1685. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  1686. return;
  1687. }
  1688. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  1689. // Check if assigned value is an array literal, so we can make it a typed array too if appropriate.
  1690. if (assignee_type.has_container_element_type() && p_assignment->assigned_value->type == GDScriptParser::Node::ARRAY) {
  1691. update_array_literal_element_type(assignee_type, static_cast<GDScriptParser::ArrayNode *>(p_assignment->assigned_value));
  1692. }
  1693. GDScriptParser::DataType assigned_value_type = p_assignment->assigned_value->get_datatype();
  1694. if (assignee_type.is_constant) {
  1695. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  1696. }
  1697. bool compatible = true;
  1698. GDScriptParser::DataType op_type = assigned_value_type;
  1699. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  1700. op_type = get_operation_type(p_assignment->variant_op, assignee_type, assigned_value_type, compatible, p_assignment->assigned_value);
  1701. }
  1702. p_assignment->set_datatype(op_type);
  1703. if (!assignee_type.is_variant() && assigned_value_type.is_hard_type()) {
  1704. if (compatible) {
  1705. compatible = is_type_compatible(assignee_type, op_type, true, p_assignment->assigned_value);
  1706. if (!compatible) {
  1707. if (assignee_type.is_hard_type()) {
  1708. // Try reverse test since it can be a masked subtype.
  1709. if (!is_type_compatible(op_type, assignee_type, true, p_assignment->assigned_value)) {
  1710. 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);
  1711. } else {
  1712. // TODO: Add warning.
  1713. mark_node_unsafe(p_assignment);
  1714. p_assignment->use_conversion_assign = true;
  1715. }
  1716. } else {
  1717. // TODO: Warning in this case.
  1718. mark_node_unsafe(p_assignment);
  1719. }
  1720. }
  1721. } else {
  1722. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", assignee_type.to_string(), assigned_value_type.to_string()), p_assignment);
  1723. }
  1724. }
  1725. if (assignee_type.has_no_type() || assigned_value_type.is_variant()) {
  1726. mark_node_unsafe(p_assignment);
  1727. if (assignee_type.is_hard_type() && !assignee_type.is_variant()) {
  1728. p_assignment->use_conversion_assign = true;
  1729. }
  1730. }
  1731. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1732. // Change source type so it's not wrongly detected later.
  1733. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  1734. switch (identifier->source) {
  1735. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  1736. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1737. if (!id_type.is_hard_type()) {
  1738. id_type.kind = GDScriptParser::DataType::VARIANT;
  1739. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1740. identifier->variable_source->set_datatype(id_type);
  1741. }
  1742. } break;
  1743. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER: {
  1744. GDScriptParser::DataType id_type = identifier->parameter_source->get_datatype();
  1745. if (!id_type.is_hard_type()) {
  1746. id_type.kind = GDScriptParser::DataType::VARIANT;
  1747. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1748. identifier->parameter_source->set_datatype(id_type);
  1749. }
  1750. } break;
  1751. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  1752. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1753. if (!id_type.is_hard_type()) {
  1754. id_type.kind = GDScriptParser::DataType::VARIANT;
  1755. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1756. identifier->variable_source->set_datatype(id_type);
  1757. }
  1758. } break;
  1759. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  1760. GDScriptParser::DataType id_type = identifier->bind_source->get_datatype();
  1761. if (!id_type.is_hard_type()) {
  1762. id_type.kind = GDScriptParser::DataType::VARIANT;
  1763. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1764. identifier->variable_source->set_datatype(id_type);
  1765. }
  1766. } break;
  1767. default:
  1768. // Nothing to do.
  1769. break;
  1770. }
  1771. }
  1772. #ifdef DEBUG_ENABLED
  1773. if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_value_type.kind == GDScriptParser::DataType::BUILTIN && assigned_value_type.builtin_type == Variant::NIL) {
  1774. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_assignment->assigned_value)->function_name);
  1775. } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_value_type.builtin_type == Variant::FLOAT) {
  1776. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  1777. }
  1778. #endif
  1779. }
  1780. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  1781. if (p_await->to_await == nullptr) {
  1782. GDScriptParser::DataType await_type;
  1783. await_type.kind = GDScriptParser::DataType::VARIANT;
  1784. p_await->set_datatype(await_type);
  1785. return;
  1786. }
  1787. GDScriptParser::DataType awaiting_type;
  1788. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  1789. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  1790. awaiting_type = p_await->to_await->get_datatype();
  1791. } else {
  1792. reduce_expression(p_await->to_await);
  1793. }
  1794. if (p_await->to_await->is_constant) {
  1795. p_await->is_constant = p_await->to_await->is_constant;
  1796. p_await->reduced_value = p_await->to_await->reduced_value;
  1797. awaiting_type = p_await->to_await->get_datatype();
  1798. } else {
  1799. awaiting_type.kind = GDScriptParser::DataType::VARIANT;
  1800. awaiting_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1801. }
  1802. p_await->set_datatype(awaiting_type);
  1803. #ifdef DEBUG_ENABLED
  1804. if (!awaiting_type.is_coroutine && awaiting_type.builtin_type != Variant::SIGNAL) {
  1805. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  1806. }
  1807. #endif
  1808. }
  1809. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  1810. reduce_expression(p_binary_op->left_operand);
  1811. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) {
  1812. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_binary_op->right_operand), true);
  1813. } else {
  1814. reduce_expression(p_binary_op->right_operand);
  1815. }
  1816. // TODO: Right operand must be a valid type with the `is` operator. Need to check here.
  1817. GDScriptParser::DataType left_type;
  1818. if (p_binary_op->left_operand) {
  1819. left_type = p_binary_op->left_operand->get_datatype();
  1820. }
  1821. GDScriptParser::DataType right_type;
  1822. if (p_binary_op->right_operand) {
  1823. right_type = p_binary_op->right_operand->get_datatype();
  1824. }
  1825. if (!left_type.is_set() || !right_type.is_set()) {
  1826. return;
  1827. }
  1828. #ifdef DEBUG_ENABLED
  1829. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1830. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1831. }
  1832. #endif
  1833. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1834. p_binary_op->is_constant = true;
  1835. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1836. bool valid = false;
  1837. 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);
  1838. if (!valid) {
  1839. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1840. 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);
  1841. } else {
  1842. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  1843. Variant::get_operator_name(p_binary_op->variant_op),
  1844. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1845. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1846. p_binary_op);
  1847. }
  1848. }
  1849. } else {
  1850. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1851. GDScriptParser::DataType test_type = right_type;
  1852. test_type.is_meta_type = false;
  1853. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1854. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1855. p_binary_op->reduced_value = false;
  1856. } else {
  1857. p_binary_op->reduced_value = true;
  1858. }
  1859. } else {
  1860. ERR_PRINT("Parser bug: unknown binary operation.");
  1861. }
  1862. }
  1863. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  1864. return;
  1865. }
  1866. GDScriptParser::DataType result;
  1867. if (left_type.is_variant() || right_type.is_variant()) {
  1868. // Cannot infer type because one operand can be anything.
  1869. result.kind = GDScriptParser::DataType::VARIANT;
  1870. mark_node_unsafe(p_binary_op);
  1871. } else {
  1872. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1873. bool valid = false;
  1874. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  1875. if (!valid) {
  1876. 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);
  1877. }
  1878. } else {
  1879. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1880. GDScriptParser::DataType test_type = right_type;
  1881. test_type.is_meta_type = false;
  1882. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1883. // Test reverse as well to consider for subtypes.
  1884. if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) {
  1885. if (p_binary_op->left_operand->get_datatype().is_hard_type()) {
  1886. 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);
  1887. } else {
  1888. // TODO: Warning.
  1889. mark_node_unsafe(p_binary_op);
  1890. }
  1891. }
  1892. }
  1893. // "is" operator is always a boolean anyway.
  1894. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1895. result.kind = GDScriptParser::DataType::BUILTIN;
  1896. result.builtin_type = Variant::BOOL;
  1897. } else {
  1898. ERR_PRINT("Parser bug: unknown binary operation.");
  1899. }
  1900. }
  1901. }
  1902. p_binary_op->set_datatype(result);
  1903. }
  1904. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool p_is_await, bool p_is_root) {
  1905. bool all_is_constant = true;
  1906. Map<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  1907. for (int i = 0; i < p_call->arguments.size(); i++) {
  1908. reduce_expression(p_call->arguments[i]);
  1909. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  1910. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  1911. }
  1912. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  1913. }
  1914. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  1915. GDScriptParser::DataType call_type;
  1916. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  1917. // Call to name directly.
  1918. StringName function_name = p_call->function_name;
  1919. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  1920. if (builtin_type < Variant::VARIANT_MAX) {
  1921. // Is a builtin constructor.
  1922. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1923. call_type.kind = GDScriptParser::DataType::BUILTIN;
  1924. call_type.builtin_type = builtin_type;
  1925. if (builtin_type == Variant::OBJECT) {
  1926. call_type.kind = GDScriptParser::DataType::NATIVE;
  1927. call_type.native_type = function_name; // "Object".
  1928. }
  1929. bool safe_to_fold = true;
  1930. switch (builtin_type) {
  1931. // Those are stored by reference so not suited for compile-time construction.
  1932. // Because in this case they would be the same reference in all constructed values.
  1933. case Variant::OBJECT:
  1934. case Variant::DICTIONARY:
  1935. case Variant::ARRAY:
  1936. case Variant::PACKED_BYTE_ARRAY:
  1937. case Variant::PACKED_INT32_ARRAY:
  1938. case Variant::PACKED_INT64_ARRAY:
  1939. case Variant::PACKED_FLOAT32_ARRAY:
  1940. case Variant::PACKED_FLOAT64_ARRAY:
  1941. case Variant::PACKED_STRING_ARRAY:
  1942. case Variant::PACKED_VECTOR2_ARRAY:
  1943. case Variant::PACKED_VECTOR3_ARRAY:
  1944. case Variant::PACKED_COLOR_ARRAY:
  1945. safe_to_fold = false;
  1946. break;
  1947. default:
  1948. break;
  1949. }
  1950. if (all_is_constant && safe_to_fold) {
  1951. // Construct here.
  1952. Vector<const Variant *> args;
  1953. for (int i = 0; i < p_call->arguments.size(); i++) {
  1954. args.push_back(&(p_call->arguments[i]->reduced_value));
  1955. }
  1956. Callable::CallError err;
  1957. Variant value;
  1958. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  1959. switch (err.error) {
  1960. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  1961. 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,
  1962. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  1963. p_call->arguments[err.argument]);
  1964. break;
  1965. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1966. String signature = Variant::get_type_name(builtin_type) + "(";
  1967. for (int i = 0; i < p_call->arguments.size(); i++) {
  1968. if (i > 0) {
  1969. signature += ", ";
  1970. }
  1971. signature += p_call->arguments[i]->get_datatype().to_string();
  1972. }
  1973. signature += ")";
  1974. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  1975. } break;
  1976. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1977. 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);
  1978. break;
  1979. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1980. 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);
  1981. break;
  1982. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1983. break; // Can't happen in a builtin constructor.
  1984. case Callable::CallError::CALL_OK:
  1985. p_call->is_constant = true;
  1986. p_call->reduced_value = value;
  1987. break;
  1988. }
  1989. } else {
  1990. // TODO: Check constructors without constants.
  1991. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  1992. if (p_call->arguments.size() == 1) {
  1993. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  1994. if (arg_type.is_variant()) {
  1995. mark_node_unsafe(p_call->arguments[0]);
  1996. } else {
  1997. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  1998. // Okay.
  1999. p_call->set_datatype(call_type);
  2000. return;
  2001. }
  2002. }
  2003. }
  2004. List<MethodInfo> constructors;
  2005. Variant::get_constructor_list(builtin_type, &constructors);
  2006. bool match = false;
  2007. for (const MethodInfo &info : constructors) {
  2008. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  2009. continue;
  2010. }
  2011. if (p_call->arguments.size() > info.arguments.size()) {
  2012. continue;
  2013. }
  2014. bool types_match = true;
  2015. for (int i = 0; i < p_call->arguments.size(); i++) {
  2016. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  2017. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  2018. types_match = false;
  2019. break;
  2020. #ifdef DEBUG_ENABLED
  2021. } else {
  2022. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT) {
  2023. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  2024. }
  2025. #endif
  2026. }
  2027. }
  2028. if (types_match) {
  2029. match = true;
  2030. call_type = type_from_property(info.return_val);
  2031. break;
  2032. }
  2033. }
  2034. if (!match) {
  2035. String signature = Variant::get_type_name(builtin_type) + "(";
  2036. for (int i = 0; i < p_call->arguments.size(); i++) {
  2037. if (i > 0) {
  2038. signature += ", ";
  2039. }
  2040. signature += p_call->arguments[i]->get_datatype().to_string();
  2041. }
  2042. signature += ")";
  2043. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  2044. }
  2045. }
  2046. p_call->set_datatype(call_type);
  2047. return;
  2048. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  2049. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  2050. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  2051. // Can call on compilation.
  2052. Vector<const Variant *> args;
  2053. for (int i = 0; i < p_call->arguments.size(); i++) {
  2054. args.push_back(&(p_call->arguments[i]->reduced_value));
  2055. }
  2056. Variant value;
  2057. Callable::CallError err;
  2058. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  2059. switch (err.error) {
  2060. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2061. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  2062. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2063. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  2064. p_call->arguments[err.argument]);
  2065. } break;
  2066. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2067. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2068. break;
  2069. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2070. 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);
  2071. break;
  2072. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2073. 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);
  2074. break;
  2075. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2076. break; // Can't happen in a builtin constructor.
  2077. case Callable::CallError::CALL_OK:
  2078. p_call->is_constant = true;
  2079. p_call->reduced_value = value;
  2080. break;
  2081. }
  2082. } else {
  2083. validate_call_arg(function_info, p_call);
  2084. }
  2085. p_call->set_datatype(type_from_property(function_info.return_val));
  2086. return;
  2087. } else if (Variant::has_utility_function(function_name)) {
  2088. MethodInfo function_info = info_from_utility_func(function_name);
  2089. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  2090. // Can call on compilation.
  2091. Vector<const Variant *> args;
  2092. for (int i = 0; i < p_call->arguments.size(); i++) {
  2093. args.push_back(&(p_call->arguments[i]->reduced_value));
  2094. }
  2095. Variant value;
  2096. Callable::CallError err;
  2097. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  2098. switch (err.error) {
  2099. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  2100. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  2101. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  2102. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  2103. p_call->arguments[err.argument]);
  2104. } break;
  2105. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  2106. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  2107. break;
  2108. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  2109. 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);
  2110. break;
  2111. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  2112. 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);
  2113. break;
  2114. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  2115. break; // Can't happen in a builtin constructor.
  2116. case Callable::CallError::CALL_OK:
  2117. p_call->is_constant = true;
  2118. p_call->reduced_value = value;
  2119. break;
  2120. }
  2121. } else {
  2122. validate_call_arg(function_info, p_call);
  2123. }
  2124. p_call->set_datatype(type_from_property(function_info.return_val));
  2125. return;
  2126. }
  2127. }
  2128. GDScriptParser::DataType base_type;
  2129. call_type.kind = GDScriptParser::DataType::VARIANT;
  2130. bool is_self = false;
  2131. if (p_call->is_super) {
  2132. base_type = parser->current_class->base_type;
  2133. base_type.is_meta_type = false;
  2134. is_self = true;
  2135. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2136. base_type = parser->current_class->get_datatype();
  2137. base_type.is_meta_type = false;
  2138. is_self = true;
  2139. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2140. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  2141. if (subscript->base == nullptr) {
  2142. // Invalid syntax, error already set on parser.
  2143. p_call->set_datatype(call_type);
  2144. mark_node_unsafe(p_call);
  2145. return;
  2146. }
  2147. if (!subscript->is_attribute) {
  2148. // Invalid call. Error already sent in parser.
  2149. // TODO: Could check if Callable here.
  2150. p_call->set_datatype(call_type);
  2151. mark_node_unsafe(p_call);
  2152. return;
  2153. }
  2154. if (subscript->attribute == nullptr) {
  2155. // Invalid call. Error already sent in parser.
  2156. p_call->set_datatype(call_type);
  2157. mark_node_unsafe(p_call);
  2158. return;
  2159. }
  2160. GDScriptParser::IdentifierNode *base_id = nullptr;
  2161. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2162. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2163. }
  2164. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2165. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2166. } else {
  2167. reduce_expression(subscript->base);
  2168. base_type = subscript->base->get_datatype();
  2169. }
  2170. } else {
  2171. // Invalid call. Error already sent in parser.
  2172. // TODO: Could check if Callable here too.
  2173. p_call->set_datatype(call_type);
  2174. mark_node_unsafe(p_call);
  2175. return;
  2176. }
  2177. bool is_static = false;
  2178. bool is_vararg = false;
  2179. int default_arg_count = 0;
  2180. GDScriptParser::DataType return_type;
  2181. List<GDScriptParser::DataType> par_types;
  2182. bool is_constructor = (base_type.is_meta_type || (p_call->callee && p_call->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_call->function_name == SNAME("new");
  2183. 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)) {
  2184. // If the function require typed arrays we must make literals be typed.
  2185. for (const KeyValue<int, GDScriptParser::ArrayNode *> &E : arrays) {
  2186. int index = E.key;
  2187. if (index < par_types.size() && par_types[index].has_container_element_type()) {
  2188. update_array_literal_element_type(par_types[index], E.value);
  2189. }
  2190. }
  2191. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  2192. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type) {
  2193. // Enum type is treated as a dictionary value for function calls.
  2194. base_type.is_meta_type = false;
  2195. }
  2196. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  2197. push_error(vformat(R"*(Cannot call non-static function "%s()" from static function "%s()".)*", p_call->function_name, parser->current_function->identifier->name), p_call->callee);
  2198. } else if (!is_self && base_type.is_meta_type && !is_static) {
  2199. base_type.is_meta_type = false; // For `to_string()`.
  2200. 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->callee);
  2201. } else if (is_self && !is_static && !lambda_stack.is_empty()) {
  2202. push_error(vformat(R"*(Cannot call non-static function "%s()" from a lambda function.)*", p_call->function_name), p_call->callee);
  2203. }
  2204. call_type = return_type;
  2205. } else {
  2206. // Check if the name exists as something else.
  2207. bool found = false;
  2208. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  2209. GDScriptParser::IdentifierNode *callee_id;
  2210. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2211. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  2212. } else {
  2213. // Can only be attribute.
  2214. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  2215. }
  2216. if (callee_id) {
  2217. reduce_identifier_from_base(callee_id, &base_type);
  2218. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  2219. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  2220. found = true;
  2221. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  2222. 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);
  2223. } else {
  2224. 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);
  2225. }
  2226. #ifdef DEBUG_ENABLED
  2227. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  2228. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  2229. mark_node_unsafe(p_call);
  2230. #endif
  2231. }
  2232. }
  2233. }
  2234. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  2235. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  2236. 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);
  2237. }
  2238. }
  2239. if (call_type.is_coroutine && !p_is_await && !p_is_root) {
  2240. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call->callee);
  2241. }
  2242. p_call->set_datatype(call_type);
  2243. }
  2244. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  2245. reduce_expression(p_cast->operand);
  2246. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  2247. if (!cast_type.is_set()) {
  2248. mark_node_unsafe(p_cast);
  2249. return;
  2250. }
  2251. cast_type = type_from_metatype(cast_type); // The casted value won't be a type name.
  2252. p_cast->set_datatype(cast_type);
  2253. if (!cast_type.is_variant()) {
  2254. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  2255. if (!op_type.is_variant()) {
  2256. bool valid = false;
  2257. if (op_type.kind == GDScriptParser::DataType::ENUM && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2258. // Enum types are compatible between each other, so it's a safe cast.
  2259. valid = true;
  2260. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && op_type.builtin_type == Variant::INT && cast_type.kind == GDScriptParser::DataType::ENUM) {
  2261. // Convertint int to enum is always valid.
  2262. valid = true;
  2263. } else if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  2264. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  2265. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  2266. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  2267. }
  2268. if (!valid) {
  2269. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  2270. }
  2271. }
  2272. } else {
  2273. mark_node_unsafe(p_cast);
  2274. }
  2275. #ifdef DEBUG_ENABLED
  2276. if (p_cast->operand->get_datatype().is_variant()) {
  2277. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  2278. mark_node_unsafe(p_cast);
  2279. }
  2280. #endif
  2281. // TODO: Perform cast on constants.
  2282. }
  2283. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2284. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
  2285. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2286. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2287. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  2288. reduce_expression(element.key);
  2289. }
  2290. reduce_expression(element.value);
  2291. if (element.key->is_constant) {
  2292. if (elements.has(element.key->reduced_value)) {
  2293. 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);
  2294. } else {
  2295. elements[element.key->reduced_value] = element.value;
  2296. }
  2297. }
  2298. }
  2299. // It's dictionary in any case.
  2300. GDScriptParser::DataType dict_type;
  2301. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2302. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  2303. dict_type.builtin_type = Variant::DICTIONARY;
  2304. dict_type.is_constant = true;
  2305. p_dictionary->set_datatype(dict_type);
  2306. }
  2307. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  2308. GDScriptParser::DataType result;
  2309. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2310. result.kind = GDScriptParser::DataType::NATIVE;
  2311. result.native_type = SNAME("Node");
  2312. result.builtin_type = Variant::OBJECT;
  2313. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, result.native_type)) {
  2314. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  2315. } else if (!lambda_stack.is_empty()) {
  2316. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") inside a lambda. Use a captured variable instead.)*", p_get_node);
  2317. }
  2318. p_get_node->set_datatype(result);
  2319. }
  2320. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  2321. GDScriptParser::DataType type;
  2322. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(p_class_name));
  2323. if (ref.is_null()) {
  2324. push_error(vformat(R"(Could not find script for class "%s".)", p_class_name), p_source);
  2325. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2326. type.kind = GDScriptParser::DataType::VARIANT;
  2327. return type;
  2328. }
  2329. Error err = ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2330. if (err) {
  2331. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  2332. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2333. type.kind = GDScriptParser::DataType::VARIANT;
  2334. return type;
  2335. }
  2336. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2337. type.kind = GDScriptParser::DataType::CLASS;
  2338. type.builtin_type = Variant::OBJECT;
  2339. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  2340. type.class_type = ref->get_parser()->head;
  2341. type.script_path = ref->get_parser()->script_path;
  2342. type.is_constant = true;
  2343. type.is_meta_type = true;
  2344. return type;
  2345. }
  2346. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  2347. GDScriptParser::DataType base;
  2348. if (p_base == nullptr) {
  2349. base = type_from_metatype(parser->current_class->get_datatype());
  2350. } else {
  2351. base = *p_base;
  2352. }
  2353. const StringName &name = p_identifier->name;
  2354. if (base.kind == GDScriptParser::DataType::ENUM) {
  2355. if (base.is_meta_type) {
  2356. if (base.enum_values.has(name)) {
  2357. p_identifier->is_constant = true;
  2358. p_identifier->reduced_value = base.enum_values[name];
  2359. GDScriptParser::DataType result;
  2360. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2361. result.kind = GDScriptParser::DataType::ENUM;
  2362. result.is_constant = true;
  2363. result.builtin_type = Variant::INT;
  2364. result.native_type = base.native_type;
  2365. result.enum_type = base.enum_type;
  2366. p_identifier->set_datatype(result);
  2367. return;
  2368. } else {
  2369. // Consider as a Dictionary, so it can be anything.
  2370. // This will be evaluated in the next if block.
  2371. base.kind = GDScriptParser::DataType::BUILTIN;
  2372. base.builtin_type = Variant::DICTIONARY;
  2373. base.is_meta_type = false;
  2374. }
  2375. } else {
  2376. push_error(R"(Cannot get property from enum value.)", p_identifier);
  2377. return;
  2378. }
  2379. }
  2380. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  2381. if (base.is_meta_type) {
  2382. bool valid = true;
  2383. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  2384. if (valid) {
  2385. p_identifier->is_constant = true;
  2386. p_identifier->reduced_value = result;
  2387. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  2388. } else if (base.is_hard_type()) {
  2389. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  2390. }
  2391. } else {
  2392. switch (base.builtin_type) {
  2393. case Variant::NIL: {
  2394. if (base.is_hard_type()) {
  2395. push_error(vformat(R"(Invalid get index "%s" on base Nil)", name), p_identifier);
  2396. }
  2397. return;
  2398. }
  2399. case Variant::DICTIONARY: {
  2400. GDScriptParser::DataType dummy;
  2401. dummy.kind = GDScriptParser::DataType::VARIANT;
  2402. p_identifier->set_datatype(dummy);
  2403. return;
  2404. }
  2405. default: {
  2406. Callable::CallError temp;
  2407. Variant dummy;
  2408. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  2409. List<PropertyInfo> properties;
  2410. dummy.get_property_list(&properties);
  2411. for (const PropertyInfo &prop : properties) {
  2412. if (prop.name == name) {
  2413. p_identifier->set_datatype(type_from_property(prop));
  2414. return;
  2415. }
  2416. }
  2417. if (base.is_hard_type()) {
  2418. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  2419. }
  2420. }
  2421. }
  2422. }
  2423. return;
  2424. }
  2425. GDScriptParser::ClassNode *base_class = base.class_type;
  2426. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  2427. while (base_class != nullptr) {
  2428. if (base_class->identifier && base_class->identifier->name == name) {
  2429. p_identifier->set_datatype(base_class->get_datatype());
  2430. return;
  2431. }
  2432. if (base_class->has_member(name)) {
  2433. const GDScriptParser::ClassNode::Member &member = base_class->get_member(name);
  2434. p_identifier->set_datatype(member.get_datatype());
  2435. switch (member.type) {
  2436. case GDScriptParser::ClassNode::Member::CONSTANT:
  2437. // For out-of-order resolution:
  2438. reduce_expression(member.constant->initializer);
  2439. p_identifier->is_constant = true;
  2440. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2441. p_identifier->set_datatype(member.constant->initializer->get_datatype());
  2442. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2443. p_identifier->constant_source = member.constant;
  2444. break;
  2445. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2446. p_identifier->is_constant = true;
  2447. p_identifier->reduced_value = member.enum_value.value;
  2448. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2449. break;
  2450. case GDScriptParser::ClassNode::Member::VARIABLE:
  2451. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  2452. p_identifier->variable_source = member.variable;
  2453. member.variable->usages += 1;
  2454. break;
  2455. case GDScriptParser::ClassNode::Member::FUNCTION:
  2456. resolve_function_signature(member.function);
  2457. p_identifier->set_datatype(make_callable_type(member.function->info));
  2458. break;
  2459. case GDScriptParser::ClassNode::Member::CLASS:
  2460. // For out-of-order resolution:
  2461. resolve_class_interface(member.m_class);
  2462. p_identifier->set_datatype(member.m_class->get_datatype());
  2463. break;
  2464. default:
  2465. break; // Type already set.
  2466. }
  2467. return;
  2468. }
  2469. // Check outer constants.
  2470. // TODO: Allow outer static functions.
  2471. GDScriptParser::ClassNode *outer = base_class->outer;
  2472. while (outer != nullptr) {
  2473. if (outer->has_member(name)) {
  2474. const GDScriptParser::ClassNode::Member &member = outer->get_member(name);
  2475. switch (member.type) {
  2476. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2477. // TODO: Make sure loops won't cause problem. And make special error message for those.
  2478. // For out-of-order resolution:
  2479. reduce_expression(member.constant->initializer);
  2480. p_identifier->set_datatype(member.get_datatype());
  2481. p_identifier->is_constant = true;
  2482. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2483. return;
  2484. } break;
  2485. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2486. p_identifier->set_datatype(member.get_datatype());
  2487. p_identifier->is_constant = true;
  2488. p_identifier->reduced_value = member.enum_value.value;
  2489. return;
  2490. } break;
  2491. case GDScriptParser::ClassNode::Member::ENUM: {
  2492. p_identifier->set_datatype(member.get_datatype());
  2493. p_identifier->is_constant = false;
  2494. return;
  2495. } break;
  2496. case GDScriptParser::ClassNode::Member::CLASS: {
  2497. resolve_class_interface(member.m_class);
  2498. p_identifier->set_datatype(member.m_class->get_datatype());
  2499. return;
  2500. } break;
  2501. default:
  2502. break;
  2503. }
  2504. }
  2505. outer = outer->outer;
  2506. }
  2507. base_class = base_class->base_type.class_type;
  2508. }
  2509. // Check native members.
  2510. const StringName &native = base.native_type;
  2511. if (class_exists(native)) {
  2512. MethodInfo method_info;
  2513. if (ClassDB::has_property(native, name)) {
  2514. StringName getter_name = ClassDB::get_property_getter(native, name);
  2515. MethodBind *getter = ClassDB::get_method(native, getter_name);
  2516. if (getter != nullptr) {
  2517. p_identifier->set_datatype(type_from_property(getter->get_return_info()));
  2518. }
  2519. return;
  2520. }
  2521. if (ClassDB::get_method_info(native, name, &method_info)) {
  2522. // Method is callable.
  2523. p_identifier->set_datatype(make_callable_type(method_info));
  2524. return;
  2525. }
  2526. if (ClassDB::get_signal(native, name, &method_info)) {
  2527. // Signal is a type too.
  2528. p_identifier->set_datatype(make_signal_type(method_info));
  2529. return;
  2530. }
  2531. if (ClassDB::has_enum(native, name)) {
  2532. p_identifier->set_datatype(make_native_enum_type(native, name));
  2533. return;
  2534. }
  2535. bool valid = false;
  2536. int int_constant = ClassDB::get_integer_constant(native, name, &valid);
  2537. if (valid) {
  2538. p_identifier->is_constant = true;
  2539. p_identifier->reduced_value = int_constant;
  2540. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  2541. return;
  2542. }
  2543. }
  2544. }
  2545. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  2546. // TODO: This is opportunity to further infer types.
  2547. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  2548. if (current_enum) {
  2549. for (int i = 0; i < current_enum->values.size(); i++) {
  2550. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  2551. if (element.identifier->name == p_identifier->name) {
  2552. GDScriptParser::DataType type;
  2553. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2554. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM : GDScriptParser::DataType::BUILTIN;
  2555. type.builtin_type = Variant::INT;
  2556. type.is_constant = true;
  2557. if (element.parent_enum->identifier) {
  2558. type.enum_type = element.parent_enum->identifier->name;
  2559. }
  2560. p_identifier->set_datatype(type);
  2561. if (element.resolved) {
  2562. p_identifier->is_constant = true;
  2563. p_identifier->reduced_value = element.value;
  2564. } else {
  2565. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  2566. }
  2567. return; // Found anyway.
  2568. }
  2569. }
  2570. }
  2571. bool found_source = false;
  2572. // Check if identifier is local.
  2573. // If that's the case, the declaration already was solved before.
  2574. switch (p_identifier->source) {
  2575. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  2576. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  2577. found_source = true;
  2578. break;
  2579. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  2580. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2581. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  2582. p_identifier->is_constant = true;
  2583. // TODO: Constant should have a value on the node itself.
  2584. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  2585. found_source = true;
  2586. break;
  2587. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2588. p_identifier->variable_source->usages++;
  2589. [[fallthrough]];
  2590. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  2591. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  2592. found_source = true;
  2593. break;
  2594. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  2595. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  2596. found_source = true;
  2597. break;
  2598. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  2599. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  2600. result.is_constant = true;
  2601. p_identifier->set_datatype(result);
  2602. found_source = true;
  2603. } break;
  2604. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  2605. break;
  2606. }
  2607. // Not a local, so check members.
  2608. if (!found_source) {
  2609. reduce_identifier_from_base(p_identifier);
  2610. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  2611. // Found.
  2612. found_source = true;
  2613. }
  2614. }
  2615. if (found_source) {
  2616. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  2617. // Only capture locals and members and enum values. Constants are still accessible from the lambda using the script reference.
  2618. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT || lambda_stack.is_empty()) {
  2619. return;
  2620. }
  2621. GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
  2622. 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)) {
  2623. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  2624. function_test->source_lambda->captures.push_back(p_identifier);
  2625. function_test = function_test->source_lambda->parent_function;
  2626. }
  2627. return;
  2628. }
  2629. StringName name = p_identifier->name;
  2630. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2631. // Check globals. We make an exception for Variant::OBJECT because it's the base class for
  2632. // non-builtin types so we allow doing e.g. Object.new()
  2633. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  2634. if (builtin_type != Variant::OBJECT && builtin_type < Variant::VARIANT_MAX) {
  2635. if (can_be_builtin) {
  2636. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  2637. return;
  2638. } else {
  2639. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2640. }
  2641. }
  2642. if (class_exists(name)) {
  2643. p_identifier->set_datatype(make_native_meta_type(name));
  2644. return;
  2645. }
  2646. if (ScriptServer::is_global_class(name)) {
  2647. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  2648. return;
  2649. }
  2650. // Try singletons.
  2651. // Do this before globals because this might be a singleton loading another one before it's compiled.
  2652. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  2653. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  2654. if (autoload.is_singleton) {
  2655. // Singleton exists, so it's at least a Node.
  2656. GDScriptParser::DataType result;
  2657. result.kind = GDScriptParser::DataType::NATIVE;
  2658. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2659. if (autoload.path.to_lower().ends_with(GDScriptLanguage::get_singleton()->get_extension())) {
  2660. Ref<GDScriptParserRef> parser = get_parser_for(autoload.path);
  2661. if (parser.is_valid()) {
  2662. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2663. if (err == OK) {
  2664. result = type_from_metatype(parser->get_parser()->head->get_datatype());
  2665. }
  2666. }
  2667. }
  2668. result.is_constant = true;
  2669. p_identifier->set_datatype(result);
  2670. return;
  2671. }
  2672. }
  2673. if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) {
  2674. int idx = GDScriptLanguage::get_singleton()->get_global_map()[name];
  2675. Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  2676. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2677. p_identifier->is_constant = true;
  2678. p_identifier->reduced_value = constant;
  2679. return;
  2680. }
  2681. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) {
  2682. Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name];
  2683. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2684. p_identifier->is_constant = true;
  2685. p_identifier->reduced_value = constant;
  2686. return;
  2687. }
  2688. // Not found.
  2689. // Check if it's a builtin function.
  2690. if (GDScriptUtilityFunctions::function_exists(name)) {
  2691. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2692. } else {
  2693. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2694. }
  2695. GDScriptParser::DataType dummy;
  2696. dummy.kind = GDScriptParser::DataType::VARIANT;
  2697. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2698. }
  2699. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  2700. // Lambda is always a Callable.
  2701. GDScriptParser::DataType lambda_type;
  2702. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  2703. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  2704. lambda_type.builtin_type = Variant::CALLABLE;
  2705. p_lambda->set_datatype(lambda_type);
  2706. if (p_lambda->function == nullptr) {
  2707. return;
  2708. }
  2709. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  2710. parser->current_function = p_lambda->function;
  2711. lambda_stack.push_back(p_lambda);
  2712. for (int i = 0; i < p_lambda->function->parameters.size(); i++) {
  2713. resolve_parameter(p_lambda->function->parameters[i]);
  2714. }
  2715. resolve_suite(p_lambda->function->body);
  2716. int captures_amount = p_lambda->captures.size();
  2717. if (captures_amount > 0) {
  2718. // Create space for lambda parameters.
  2719. // At the beginning to not mess with optional parameters.
  2720. int param_count = p_lambda->function->parameters.size();
  2721. p_lambda->function->parameters.resize(param_count + captures_amount);
  2722. for (int i = param_count - 1; i >= 0; i--) {
  2723. p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
  2724. p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  2725. }
  2726. // Add captures as extra parameters at the beginning.
  2727. for (int i = 0; i < p_lambda->captures.size(); i++) {
  2728. GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
  2729. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  2730. capture_param->identifier = capture;
  2731. capture_param->usages = capture->usages;
  2732. capture_param->set_datatype(capture->get_datatype());
  2733. p_lambda->function->parameters.write[i] = capture_param;
  2734. p_lambda->function->parameters_indices[capture->name] = i;
  2735. }
  2736. }
  2737. lambda_stack.pop_back();
  2738. parser->current_function = previous_function;
  2739. }
  2740. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2741. p_literal->reduced_value = p_literal->value;
  2742. p_literal->is_constant = true;
  2743. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  2744. }
  2745. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2746. if (!p_preload->path) {
  2747. return;
  2748. }
  2749. reduce_expression(p_preload->path);
  2750. if (!p_preload->path->is_constant) {
  2751. push_error("Preloaded path must be a constant string.", p_preload->path);
  2752. return;
  2753. }
  2754. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2755. push_error("Preloaded path must be a constant string.", p_preload->path);
  2756. } else {
  2757. p_preload->resolved_path = p_preload->path->reduced_value;
  2758. // TODO: Save this as script dependency.
  2759. if (p_preload->resolved_path.is_relative_path()) {
  2760. p_preload->resolved_path = parser->script_path.get_base_dir().plus_file(p_preload->resolved_path);
  2761. }
  2762. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2763. if (!FileAccess::exists(p_preload->resolved_path)) {
  2764. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2765. } else {
  2766. // TODO: Don't load if validating: use completion cache.
  2767. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2768. if (p_preload->resource.is_null()) {
  2769. push_error(vformat(R"(Could not preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2770. }
  2771. }
  2772. }
  2773. p_preload->is_constant = true;
  2774. p_preload->reduced_value = p_preload->resource;
  2775. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  2776. }
  2777. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2778. p_self->is_constant = false;
  2779. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2780. }
  2781. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2782. if (p_subscript->base == nullptr) {
  2783. return;
  2784. }
  2785. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2786. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  2787. } else {
  2788. reduce_expression(p_subscript->base);
  2789. }
  2790. GDScriptParser::DataType result_type;
  2791. if (p_subscript->is_attribute) {
  2792. if (p_subscript->attribute == nullptr) {
  2793. return;
  2794. }
  2795. if (p_subscript->base->is_constant) {
  2796. // Just try to get it.
  2797. bool valid = false;
  2798. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  2799. if (!valid) {
  2800. push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index);
  2801. } else {
  2802. p_subscript->is_constant = true;
  2803. p_subscript->reduced_value = value;
  2804. result_type = type_from_variant(value, p_subscript);
  2805. }
  2806. result_type.kind = GDScriptParser::DataType::VARIANT;
  2807. } else {
  2808. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2809. if (base_type.is_variant() || !base_type.is_hard_type()) {
  2810. result_type.kind = GDScriptParser::DataType::VARIANT;
  2811. mark_node_unsafe(p_subscript);
  2812. } else {
  2813. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  2814. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  2815. if (attr_type.is_set()) {
  2816. result_type = attr_type;
  2817. p_subscript->is_constant = p_subscript->attribute->is_constant;
  2818. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  2819. } else {
  2820. if (base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2821. push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute);
  2822. #ifdef DEBUG_ENABLED
  2823. } else {
  2824. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  2825. #endif
  2826. }
  2827. result_type.kind = GDScriptParser::DataType::VARIANT;
  2828. }
  2829. }
  2830. }
  2831. } else {
  2832. if (p_subscript->index == nullptr) {
  2833. return;
  2834. }
  2835. reduce_expression(p_subscript->index);
  2836. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  2837. // Just try to get it.
  2838. bool valid = false;
  2839. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  2840. if (!valid) {
  2841. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  2842. } else {
  2843. p_subscript->is_constant = true;
  2844. p_subscript->reduced_value = value;
  2845. result_type = type_from_variant(value, p_subscript);
  2846. }
  2847. result_type.kind = GDScriptParser::DataType::VARIANT;
  2848. } else {
  2849. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2850. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  2851. if (base_type.is_variant()) {
  2852. result_type.kind = GDScriptParser::DataType::VARIANT;
  2853. mark_node_unsafe(p_subscript);
  2854. } else {
  2855. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2856. // Check if indexing is valid.
  2857. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  2858. if (!error) {
  2859. switch (base_type.builtin_type) {
  2860. // Expect int or real as index.
  2861. case Variant::PACKED_BYTE_ARRAY:
  2862. case Variant::PACKED_COLOR_ARRAY:
  2863. case Variant::PACKED_FLOAT32_ARRAY:
  2864. case Variant::PACKED_FLOAT64_ARRAY:
  2865. case Variant::PACKED_INT32_ARRAY:
  2866. case Variant::PACKED_INT64_ARRAY:
  2867. case Variant::PACKED_STRING_ARRAY:
  2868. case Variant::PACKED_VECTOR2_ARRAY:
  2869. case Variant::PACKED_VECTOR3_ARRAY:
  2870. case Variant::ARRAY:
  2871. case Variant::STRING:
  2872. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  2873. break;
  2874. // Expect String only.
  2875. case Variant::RECT2:
  2876. case Variant::RECT2I:
  2877. case Variant::PLANE:
  2878. case Variant::QUATERNION:
  2879. case Variant::AABB:
  2880. case Variant::OBJECT:
  2881. error = index_type.builtin_type != Variant::STRING;
  2882. break;
  2883. // Expect String or number.
  2884. case Variant::BASIS:
  2885. case Variant::VECTOR2:
  2886. case Variant::VECTOR2I:
  2887. case Variant::VECTOR3:
  2888. case Variant::VECTOR3I:
  2889. case Variant::TRANSFORM2D:
  2890. case Variant::TRANSFORM3D:
  2891. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  2892. index_type.builtin_type != Variant::STRING;
  2893. break;
  2894. // Expect String or int.
  2895. case Variant::COLOR:
  2896. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  2897. break;
  2898. // Don't support indexing, but we will check it later.
  2899. case Variant::RID:
  2900. case Variant::BOOL:
  2901. case Variant::CALLABLE:
  2902. case Variant::FLOAT:
  2903. case Variant::INT:
  2904. case Variant::NIL:
  2905. case Variant::NODE_PATH:
  2906. case Variant::SIGNAL:
  2907. case Variant::STRING_NAME:
  2908. break;
  2909. // Here for completeness.
  2910. case Variant::DICTIONARY:
  2911. case Variant::VARIANT_MAX:
  2912. break;
  2913. }
  2914. if (error) {
  2915. 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);
  2916. }
  2917. }
  2918. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2919. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  2920. 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);
  2921. }
  2922. }
  2923. // Check resulting type if possible.
  2924. result_type.builtin_type = Variant::NIL;
  2925. result_type.kind = GDScriptParser::DataType::BUILTIN;
  2926. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  2927. if (base_type.kind != GDScriptParser::DataType::BUILTIN) {
  2928. base_type.builtin_type = Variant::OBJECT;
  2929. }
  2930. switch (base_type.builtin_type) {
  2931. // Can't index at all.
  2932. case Variant::RID:
  2933. case Variant::BOOL:
  2934. case Variant::CALLABLE:
  2935. case Variant::FLOAT:
  2936. case Variant::INT:
  2937. case Variant::NIL:
  2938. case Variant::NODE_PATH:
  2939. case Variant::SIGNAL:
  2940. case Variant::STRING_NAME:
  2941. result_type.kind = GDScriptParser::DataType::VARIANT;
  2942. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  2943. break;
  2944. // Return int.
  2945. case Variant::PACKED_BYTE_ARRAY:
  2946. case Variant::PACKED_INT32_ARRAY:
  2947. case Variant::PACKED_INT64_ARRAY:
  2948. case Variant::VECTOR2I:
  2949. case Variant::VECTOR3I:
  2950. result_type.builtin_type = Variant::INT;
  2951. break;
  2952. // Return float.
  2953. case Variant::PACKED_FLOAT32_ARRAY:
  2954. case Variant::PACKED_FLOAT64_ARRAY:
  2955. case Variant::VECTOR2:
  2956. case Variant::VECTOR3:
  2957. case Variant::QUATERNION:
  2958. result_type.builtin_type = Variant::FLOAT;
  2959. break;
  2960. // Return Color.
  2961. case Variant::PACKED_COLOR_ARRAY:
  2962. result_type.builtin_type = Variant::COLOR;
  2963. break;
  2964. // Return String.
  2965. case Variant::PACKED_STRING_ARRAY:
  2966. case Variant::STRING:
  2967. result_type.builtin_type = Variant::STRING;
  2968. break;
  2969. // Return Vector2.
  2970. case Variant::PACKED_VECTOR2_ARRAY:
  2971. case Variant::TRANSFORM2D:
  2972. case Variant::RECT2:
  2973. result_type.builtin_type = Variant::VECTOR2;
  2974. break;
  2975. // Return Vector2I.
  2976. case Variant::RECT2I:
  2977. result_type.builtin_type = Variant::VECTOR2I;
  2978. break;
  2979. // Return Vector3.
  2980. case Variant::PACKED_VECTOR3_ARRAY:
  2981. case Variant::AABB:
  2982. case Variant::BASIS:
  2983. result_type.builtin_type = Variant::VECTOR3;
  2984. break;
  2985. // Depends on the index.
  2986. case Variant::TRANSFORM3D:
  2987. case Variant::PLANE:
  2988. case Variant::COLOR:
  2989. case Variant::DICTIONARY:
  2990. case Variant::OBJECT:
  2991. result_type.kind = GDScriptParser::DataType::VARIANT;
  2992. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2993. break;
  2994. // Can have an element type.
  2995. case Variant::ARRAY:
  2996. if (base_type.has_container_element_type()) {
  2997. result_type = base_type.get_container_element_type();
  2998. result_type.type_source = base_type.type_source;
  2999. } else {
  3000. result_type.kind = GDScriptParser::DataType::VARIANT;
  3001. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  3002. }
  3003. break;
  3004. // Here for completeness.
  3005. case Variant::VARIANT_MAX:
  3006. break;
  3007. }
  3008. }
  3009. }
  3010. }
  3011. p_subscript->set_datatype(result_type);
  3012. }
  3013. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  3014. reduce_expression(p_ternary_op->condition);
  3015. reduce_expression(p_ternary_op->true_expr);
  3016. reduce_expression(p_ternary_op->false_expr);
  3017. GDScriptParser::DataType result;
  3018. 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) {
  3019. p_ternary_op->is_constant = true;
  3020. if (p_ternary_op->condition->reduced_value.booleanize()) {
  3021. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  3022. } else {
  3023. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  3024. }
  3025. }
  3026. GDScriptParser::DataType true_type;
  3027. if (p_ternary_op->true_expr) {
  3028. true_type = p_ternary_op->true_expr->get_datatype();
  3029. } else {
  3030. true_type.kind = GDScriptParser::DataType::VARIANT;
  3031. }
  3032. GDScriptParser::DataType false_type;
  3033. if (p_ternary_op->false_expr) {
  3034. false_type = p_ternary_op->false_expr->get_datatype();
  3035. } else {
  3036. false_type.kind = GDScriptParser::DataType::VARIANT;
  3037. }
  3038. if (true_type.is_variant() || false_type.is_variant()) {
  3039. result.kind = GDScriptParser::DataType::VARIANT;
  3040. } else {
  3041. result = true_type;
  3042. if (!is_type_compatible(true_type, false_type)) {
  3043. result = false_type;
  3044. if (!is_type_compatible(false_type, true_type)) {
  3045. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3046. result.kind = GDScriptParser::DataType::VARIANT;
  3047. #ifdef DEBUG_ENABLED
  3048. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  3049. #endif
  3050. }
  3051. }
  3052. }
  3053. p_ternary_op->set_datatype(result);
  3054. }
  3055. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  3056. reduce_expression(p_unary_op->operand);
  3057. GDScriptParser::DataType result;
  3058. if (p_unary_op->operand == nullptr) {
  3059. result.kind = GDScriptParser::DataType::VARIANT;
  3060. p_unary_op->set_datatype(result);
  3061. return;
  3062. }
  3063. if (p_unary_op->operand->is_constant) {
  3064. p_unary_op->is_constant = true;
  3065. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  3066. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  3067. } else if (p_unary_op->operand->get_datatype().is_variant()) {
  3068. result.kind = GDScriptParser::DataType::VARIANT;
  3069. mark_node_unsafe(p_unary_op);
  3070. } else {
  3071. bool valid = false;
  3072. result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), valid, p_unary_op);
  3073. if (!valid) {
  3074. 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);
  3075. }
  3076. }
  3077. p_unary_op->set_datatype(result);
  3078. }
  3079. void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
  3080. bool all_is_constant = true;
  3081. for (int i = 0; i < p_array->elements.size(); i++) {
  3082. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  3083. all_is_constant = all_is_constant && element->is_constant;
  3084. if (!all_is_constant) {
  3085. return;
  3086. }
  3087. }
  3088. Array array;
  3089. array.resize(p_array->elements.size());
  3090. for (int i = 0; i < p_array->elements.size(); i++) {
  3091. array[i] = p_array->elements[i]->reduced_value;
  3092. }
  3093. p_array->is_constant = true;
  3094. p_array->reduced_value = array;
  3095. }
  3096. void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  3097. bool all_is_constant = true;
  3098. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3099. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3100. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  3101. if (!all_is_constant) {
  3102. return;
  3103. }
  3104. }
  3105. Dictionary dict;
  3106. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  3107. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  3108. dict[element.key->reduced_value] = element.value->reduced_value;
  3109. }
  3110. p_dictionary->is_constant = true;
  3111. p_dictionary->reduced_value = dict;
  3112. }
  3113. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  3114. GDScriptParser::DataType result;
  3115. result.is_constant = true;
  3116. result.kind = GDScriptParser::DataType::BUILTIN;
  3117. result.builtin_type = p_value.get_type();
  3118. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  3119. if (p_value.get_type() == Variant::OBJECT) {
  3120. Object *obj = p_value;
  3121. if (!obj) {
  3122. return GDScriptParser::DataType();
  3123. }
  3124. result.native_type = obj->get_class_name();
  3125. Ref<Script> scr = p_value; // Check if value is a script itself.
  3126. if (scr.is_valid()) {
  3127. result.is_meta_type = true;
  3128. } else {
  3129. result.is_meta_type = false;
  3130. scr = obj->get_script();
  3131. }
  3132. if (scr.is_valid()) {
  3133. if (scr->is_valid()) {
  3134. result.script_type = scr;
  3135. result.script_path = scr->get_path();
  3136. Ref<GDScript> gds = scr;
  3137. if (gds.is_valid()) {
  3138. result.kind = GDScriptParser::DataType::CLASS;
  3139. // This might be an inner class, so we want to get the parser for the root.
  3140. // But still get the inner class from that tree.
  3141. GDScript *current = gds.ptr();
  3142. List<StringName> class_chain;
  3143. while (current->_owner) {
  3144. // Push to front so it's in reverse.
  3145. class_chain.push_front(current->name);
  3146. current = current->_owner;
  3147. }
  3148. Ref<GDScriptParserRef> ref = get_parser_for(current->get_path());
  3149. if (ref.is_null()) {
  3150. push_error("Could not find script in path.", p_source);
  3151. GDScriptParser::DataType error_type;
  3152. error_type.kind = GDScriptParser::DataType::VARIANT;
  3153. return error_type;
  3154. }
  3155. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  3156. GDScriptParser::ClassNode *found = ref->get_parser()->head;
  3157. // It should be okay to assume this exists, since we have a complete script already.
  3158. for (const StringName &E : class_chain) {
  3159. found = found->get_member(E).m_class;
  3160. }
  3161. result.class_type = found;
  3162. result.script_path = ref->get_parser()->script_path;
  3163. } else {
  3164. result.kind = GDScriptParser::DataType::SCRIPT;
  3165. }
  3166. result.native_type = scr->get_instance_base_type();
  3167. } else {
  3168. push_error(vformat(R"(Constant value uses script from "%s" which is loaded but not compiled.)", scr->get_path()), p_source);
  3169. result.kind = GDScriptParser::DataType::VARIANT;
  3170. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3171. result.is_meta_type = false;
  3172. }
  3173. } else {
  3174. result.kind = GDScriptParser::DataType::NATIVE;
  3175. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  3176. result.is_meta_type = true;
  3177. }
  3178. }
  3179. }
  3180. return result;
  3181. }
  3182. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  3183. GDScriptParser::DataType result = p_meta_type;
  3184. result.is_meta_type = false;
  3185. result.is_constant = false;
  3186. if (p_meta_type.kind == GDScriptParser::DataType::ENUM) {
  3187. result.builtin_type = Variant::INT;
  3188. }
  3189. return result;
  3190. }
  3191. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  3192. GDScriptParser::DataType result;
  3193. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3194. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  3195. // Variant
  3196. result.kind = GDScriptParser::DataType::VARIANT;
  3197. return result;
  3198. }
  3199. result.builtin_type = p_property.type;
  3200. if (p_property.type == Variant::OBJECT) {
  3201. result.kind = GDScriptParser::DataType::NATIVE;
  3202. result.native_type = p_property.class_name == StringName() ? SNAME("Object") : p_property.class_name;
  3203. } else {
  3204. result.kind = GDScriptParser::DataType::BUILTIN;
  3205. result.builtin_type = p_property.type;
  3206. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  3207. // Check element type.
  3208. StringName elem_type_name = p_property.hint_string;
  3209. GDScriptParser::DataType elem_type;
  3210. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3211. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  3212. if (elem_builtin_type < Variant::VARIANT_MAX) {
  3213. // Builtin type.
  3214. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  3215. elem_type.builtin_type = elem_builtin_type;
  3216. } else if (class_exists(elem_type_name)) {
  3217. elem_type.kind = GDScriptParser::DataType::NATIVE;
  3218. elem_type.builtin_type = Variant::OBJECT;
  3219. elem_type.native_type = p_property.hint_string;
  3220. } else if (ScriptServer::is_global_class(elem_type_name)) {
  3221. // Just load this as it shouldn't be a GDScript.
  3222. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  3223. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  3224. elem_type.builtin_type = Variant::OBJECT;
  3225. elem_type.native_type = script->get_instance_base_type();
  3226. elem_type.script_type = script;
  3227. } else {
  3228. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  3229. }
  3230. elem_type.is_constant = false;
  3231. result.set_container_element_type(elem_type);
  3232. }
  3233. }
  3234. return result;
  3235. }
  3236. 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) {
  3237. r_static = false;
  3238. r_vararg = false;
  3239. r_default_arg_count = 0;
  3240. StringName function_name = p_function;
  3241. if (p_base_type.kind == GDScriptParser::DataType::ENUM) {
  3242. if (p_base_type.is_meta_type) {
  3243. // Enum type can be treated as a dictionary value.
  3244. p_base_type.kind = GDScriptParser::DataType::BUILTIN;
  3245. p_base_type.builtin_type = Variant::DICTIONARY;
  3246. p_base_type.is_meta_type = false;
  3247. } else {
  3248. push_error("Cannot call function on enum value.", p_source);
  3249. return false;
  3250. }
  3251. }
  3252. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  3253. // Construct a base type to get methods.
  3254. Callable::CallError err;
  3255. Variant dummy;
  3256. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  3257. if (err.error != Callable::CallError::CALL_OK) {
  3258. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  3259. }
  3260. List<MethodInfo> methods;
  3261. dummy.get_method_list(&methods);
  3262. for (const MethodInfo &E : methods) {
  3263. if (E.name == p_function) {
  3264. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3265. r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name);
  3266. return true;
  3267. }
  3268. }
  3269. return false;
  3270. }
  3271. if (p_is_constructor) {
  3272. function_name = "_init";
  3273. r_static = true;
  3274. }
  3275. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  3276. GDScriptParser::FunctionNode *found_function = nullptr;
  3277. while (found_function == nullptr && base_class != nullptr) {
  3278. if (base_class->has_member(function_name)) {
  3279. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3280. // TODO: If this is Callable it can have a better error message.
  3281. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  3282. return false;
  3283. }
  3284. found_function = base_class->get_member(function_name).function;
  3285. }
  3286. base_class = base_class->base_type.class_type;
  3287. }
  3288. if (found_function != nullptr) {
  3289. r_static = p_is_constructor || found_function->is_static;
  3290. for (int i = 0; i < found_function->parameters.size(); i++) {
  3291. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  3292. if (found_function->parameters[i]->default_value != nullptr) {
  3293. r_default_arg_count++;
  3294. }
  3295. }
  3296. r_return_type = found_function->get_datatype();
  3297. r_return_type.is_meta_type = false;
  3298. r_return_type.is_coroutine = found_function->is_coroutine;
  3299. return true;
  3300. }
  3301. Ref<Script> base_script = p_base_type.script_type;
  3302. while (base_script.is_valid() && base_script->is_valid()) {
  3303. MethodInfo info = base_script->get_method_info(function_name);
  3304. if (!(info == MethodInfo())) {
  3305. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3306. }
  3307. base_script = base_script->get_base_script();
  3308. }
  3309. // If the base is a script, it might be trying to access members of the Script class itself.
  3310. if (p_base_type.is_meta_type && !p_is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  3311. MethodInfo info;
  3312. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  3313. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  3314. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3315. }
  3316. }
  3317. StringName base_native = p_base_type.native_type;
  3318. #ifdef DEBUG_ENABLED
  3319. if (base_native != StringName()) {
  3320. // Empty native class might happen in some Script implementations.
  3321. // Just ignore it.
  3322. if (!class_exists(base_native)) {
  3323. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  3324. }
  3325. }
  3326. #endif
  3327. if (p_is_constructor) {
  3328. // Native types always have a default constructor.
  3329. r_return_type = p_base_type;
  3330. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3331. r_return_type.is_meta_type = false;
  3332. return true;
  3333. }
  3334. MethodInfo info;
  3335. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  3336. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3337. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  3338. r_static = true;
  3339. }
  3340. return valid;
  3341. }
  3342. return false;
  3343. }
  3344. 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) {
  3345. r_return_type = type_from_property(p_info.return_val);
  3346. r_default_arg_count = p_info.default_arguments.size();
  3347. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  3348. for (const PropertyInfo &E : p_info.arguments) {
  3349. r_par_types.push_back(type_from_property(E));
  3350. }
  3351. return true;
  3352. }
  3353. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  3354. List<GDScriptParser::DataType> arg_types;
  3355. for (const PropertyInfo &E : p_method.arguments) {
  3356. arg_types.push_back(type_from_property(E));
  3357. }
  3358. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  3359. }
  3360. 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) {
  3361. bool valid = true;
  3362. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  3363. 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);
  3364. valid = false;
  3365. }
  3366. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  3367. 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()]);
  3368. valid = false;
  3369. }
  3370. for (int i = 0; i < p_call->arguments.size(); i++) {
  3371. if (i >= p_par_types.size()) {
  3372. // Already on vararg place.
  3373. break;
  3374. }
  3375. GDScriptParser::DataType par_type = p_par_types[i];
  3376. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  3377. if (arg_type.is_variant()) {
  3378. // Argument can be anything, so this is unsafe.
  3379. mark_node_unsafe(p_call->arguments[i]);
  3380. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  3381. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  3382. mark_node_unsafe(p_call);
  3383. if (!is_type_compatible(arg_type, par_type)) {
  3384. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  3385. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  3386. p_call->arguments[i]);
  3387. valid = false;
  3388. }
  3389. #ifdef DEBUG_ENABLED
  3390. } else {
  3391. 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) {
  3392. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  3393. }
  3394. #endif
  3395. }
  3396. }
  3397. return valid;
  3398. }
  3399. #ifdef DEBUG_ENABLED
  3400. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  3401. const StringName &name = p_local->name;
  3402. GDScriptParser::DataType base = parser->current_class->get_datatype();
  3403. GDScriptParser::ClassNode *base_class = base.class_type;
  3404. {
  3405. List<MethodInfo> gdscript_funcs;
  3406. GDScriptLanguage::get_singleton()->get_public_functions(&gdscript_funcs);
  3407. for (MethodInfo &info : gdscript_funcs) {
  3408. if (info.name == name) {
  3409. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3410. return true;
  3411. }
  3412. }
  3413. if (Variant::has_utility_function(name)) {
  3414. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "built-in function");
  3415. return true;
  3416. } else if (ClassDB::class_exists(name)) {
  3417. parser->push_warning(p_local, GDScriptWarning::SHADOWED_GLOBAL_IDENTIFIER, p_context, name, "global class");
  3418. return true;
  3419. }
  3420. }
  3421. while (base_class != nullptr) {
  3422. if (base_class->has_member(name)) {
  3423. 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()));
  3424. return true;
  3425. }
  3426. base_class = base_class->base_type.class_type;
  3427. }
  3428. StringName base_native = base.native_type;
  3429. ERR_FAIL_COND_V_MSG(!class_exists(base_native), false, "Non-existent native base class.");
  3430. StringName parent = base_native;
  3431. while (parent != StringName()) {
  3432. if (ClassDB::has_method(parent, name, true)) {
  3433. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  3434. return true;
  3435. } else if (ClassDB::has_signal(parent, name, true)) {
  3436. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  3437. return true;
  3438. } else if (ClassDB::has_property(parent, name, true)) {
  3439. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  3440. return true;
  3441. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  3442. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  3443. return true;
  3444. } else if (ClassDB::has_enum(parent, name, true)) {
  3445. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  3446. return true;
  3447. }
  3448. parent = ClassDB::get_parent_class(parent);
  3449. }
  3450. return false;
  3451. }
  3452. #endif
  3453. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  3454. // Unary version.
  3455. GDScriptParser::DataType nil_type;
  3456. nil_type.builtin_type = Variant::NIL;
  3457. nil_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3458. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  3459. }
  3460. 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) {
  3461. GDScriptParser::DataType result;
  3462. result.kind = GDScriptParser::DataType::VARIANT;
  3463. Variant::Type a_type = p_a.builtin_type;
  3464. Variant::Type b_type = p_b.builtin_type;
  3465. if (p_a.kind == GDScriptParser::DataType::ENUM) {
  3466. if (p_a.is_meta_type) {
  3467. a_type = Variant::DICTIONARY;
  3468. } else {
  3469. a_type = Variant::INT;
  3470. }
  3471. }
  3472. if (p_b.kind == GDScriptParser::DataType::ENUM) {
  3473. if (p_b.is_meta_type) {
  3474. b_type = Variant::DICTIONARY;
  3475. } else {
  3476. b_type = Variant::INT;
  3477. }
  3478. }
  3479. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  3480. bool hard_operation = p_a.is_hard_type() && p_b.is_hard_type();
  3481. bool validated = op_eval != nullptr;
  3482. if (hard_operation && !validated) {
  3483. r_valid = false;
  3484. return result;
  3485. } else if (hard_operation && validated) {
  3486. r_valid = true;
  3487. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3488. result.kind = GDScriptParser::DataType::BUILTIN;
  3489. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3490. } else if (!hard_operation && !validated) {
  3491. r_valid = true;
  3492. result.type_source = GDScriptParser::DataType::UNDETECTED;
  3493. result.kind = GDScriptParser::DataType::VARIANT;
  3494. result.builtin_type = Variant::NIL;
  3495. } else if (!hard_operation && validated) {
  3496. r_valid = true;
  3497. result.type_source = GDScriptParser::DataType::INFERRED;
  3498. result.kind = GDScriptParser::DataType::BUILTIN;
  3499. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3500. }
  3501. return result;
  3502. }
  3503. // TODO: Add safe/unsafe return variable (for variant cases)
  3504. 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) {
  3505. // These return "true" so it doesn't affect users negatively.
  3506. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  3507. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  3508. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  3509. // Variant can receive anything.
  3510. return true;
  3511. }
  3512. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  3513. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  3514. return true;
  3515. }
  3516. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  3517. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  3518. if (!valid && p_allow_implicit_conversion) {
  3519. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  3520. }
  3521. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM && !p_source.is_meta_type) {
  3522. // Enum value is also integer.
  3523. valid = true;
  3524. }
  3525. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  3526. // Check the element type.
  3527. if (p_target.has_container_element_type()) {
  3528. if (!p_source.has_container_element_type()) {
  3529. // TODO: Maybe this is valid but unsafe?
  3530. // Variant array can't be appended to typed array.
  3531. valid = false;
  3532. } else {
  3533. valid = is_type_compatible(p_target.get_container_element_type(), p_source.get_container_element_type(), false);
  3534. }
  3535. }
  3536. }
  3537. return valid;
  3538. }
  3539. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  3540. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  3541. #ifdef DEBUG_ENABLED
  3542. if (p_source_node) {
  3543. parser->push_warning(p_source_node, GDScriptWarning::INT_ASSIGNED_TO_ENUM);
  3544. }
  3545. #endif
  3546. return true;
  3547. }
  3548. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  3549. if (p_source.native_type == p_target.native_type) {
  3550. return true;
  3551. }
  3552. }
  3553. return false;
  3554. }
  3555. // From here on the target type is an object, so we have to test polymorphism.
  3556. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  3557. // null is acceptable in object.
  3558. return true;
  3559. }
  3560. StringName src_native;
  3561. Ref<Script> src_script;
  3562. const GDScriptParser::ClassNode *src_class = nullptr;
  3563. switch (p_source.kind) {
  3564. case GDScriptParser::DataType::NATIVE:
  3565. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  3566. // Non-native class cannot be supertype of native.
  3567. return false;
  3568. }
  3569. if (p_source.is_meta_type) {
  3570. src_native = GDScriptNativeClass::get_class_static();
  3571. } else {
  3572. src_native = p_source.native_type;
  3573. }
  3574. break;
  3575. case GDScriptParser::DataType::SCRIPT:
  3576. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  3577. // A script type cannot be a subtype of a GDScript class.
  3578. return false;
  3579. }
  3580. if (p_source.is_meta_type) {
  3581. src_native = p_source.script_type->get_class_name();
  3582. } else {
  3583. src_script = p_source.script_type;
  3584. src_native = src_script->get_instance_base_type();
  3585. }
  3586. break;
  3587. case GDScriptParser::DataType::CLASS:
  3588. if (p_source.is_meta_type) {
  3589. src_native = GDScript::get_class_static();
  3590. } else {
  3591. src_class = p_source.class_type;
  3592. const GDScriptParser::ClassNode *base = src_class;
  3593. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  3594. base = base->base_type.class_type;
  3595. }
  3596. src_native = base->base_type.native_type;
  3597. src_script = base->base_type.script_type;
  3598. }
  3599. break;
  3600. case GDScriptParser::DataType::VARIANT:
  3601. case GDScriptParser::DataType::BUILTIN:
  3602. case GDScriptParser::DataType::ENUM:
  3603. case GDScriptParser::DataType::UNRESOLVED:
  3604. break; // Already solved before.
  3605. }
  3606. switch (p_target.kind) {
  3607. case GDScriptParser::DataType::NATIVE: {
  3608. if (p_target.is_meta_type) {
  3609. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  3610. }
  3611. return ClassDB::is_parent_class(src_native, p_target.native_type);
  3612. }
  3613. case GDScriptParser::DataType::SCRIPT:
  3614. if (p_target.is_meta_type) {
  3615. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  3616. }
  3617. while (src_script.is_valid()) {
  3618. if (src_script == p_target.script_type) {
  3619. return true;
  3620. }
  3621. src_script = src_script->get_base_script();
  3622. }
  3623. return false;
  3624. case GDScriptParser::DataType::CLASS:
  3625. if (p_target.is_meta_type) {
  3626. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  3627. }
  3628. while (src_class != nullptr) {
  3629. if (src_class->fqcn == p_target.class_type->fqcn) {
  3630. return true;
  3631. }
  3632. src_class = src_class->base_type.class_type;
  3633. }
  3634. return false;
  3635. case GDScriptParser::DataType::VARIANT:
  3636. case GDScriptParser::DataType::BUILTIN:
  3637. case GDScriptParser::DataType::ENUM:
  3638. case GDScriptParser::DataType::UNRESOLVED:
  3639. break; // Already solved before.
  3640. }
  3641. return false;
  3642. }
  3643. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  3644. mark_node_unsafe(p_origin);
  3645. parser->push_error(p_message, p_origin);
  3646. }
  3647. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  3648. #ifdef DEBUG_ENABLED
  3649. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  3650. parser->unsafe_lines.insert(i);
  3651. }
  3652. #endif
  3653. }
  3654. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  3655. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  3656. }
  3657. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  3658. Ref<GDScriptParserRef> ref;
  3659. if (depended_parsers.has(p_path)) {
  3660. ref = depended_parsers[p_path];
  3661. } else {
  3662. Error err = OK;
  3663. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  3664. if (ref.is_valid()) {
  3665. depended_parsers[p_path] = ref;
  3666. }
  3667. }
  3668. return ref;
  3669. }
  3670. Error GDScriptAnalyzer::resolve_inheritance() {
  3671. return resolve_inheritance(parser->head);
  3672. }
  3673. Error GDScriptAnalyzer::resolve_interface() {
  3674. resolve_class_interface(parser->head);
  3675. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3676. }
  3677. Error GDScriptAnalyzer::resolve_body() {
  3678. resolve_class_body(parser->head);
  3679. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3680. }
  3681. Error GDScriptAnalyzer::resolve_program() {
  3682. resolve_class_interface(parser->head);
  3683. resolve_class_body(parser->head);
  3684. List<String> parser_keys;
  3685. depended_parsers.get_key_list(&parser_keys);
  3686. for (const String &E : parser_keys) {
  3687. if (depended_parsers[E].is_null()) {
  3688. return ERR_PARSE_ERROR;
  3689. }
  3690. depended_parsers[E]->raise_status(GDScriptParserRef::FULLY_SOLVED);
  3691. }
  3692. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3693. }
  3694. Error GDScriptAnalyzer::analyze() {
  3695. parser->errors.clear();
  3696. Error err = resolve_inheritance(parser->head);
  3697. if (err) {
  3698. return err;
  3699. }
  3700. return resolve_program();
  3701. }
  3702. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  3703. parser = p_parser;
  3704. }