gdscript_analyzer.cpp 154 KB

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