gdscript_analyzer.cpp 149 KB

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