gdscript_analyzer.cpp 150 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874
  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. if (p_binary_op->left_operand->is_constant) {
  1652. left_type = type_from_variant(p_binary_op->left_operand->reduced_value, p_binary_op->left_operand);
  1653. } else {
  1654. left_type = p_binary_op->left_operand->get_datatype();
  1655. }
  1656. }
  1657. GDScriptParser::DataType right_type;
  1658. if (p_binary_op->right_operand) {
  1659. if (p_binary_op->right_operand->is_constant) {
  1660. right_type = type_from_variant(p_binary_op->right_operand->reduced_value, p_binary_op->right_operand);
  1661. } else {
  1662. right_type = p_binary_op->right_operand->get_datatype();
  1663. }
  1664. }
  1665. if (!left_type.is_set() || !right_type.is_set()) {
  1666. return;
  1667. }
  1668. #ifdef DEBUG_ENABLED
  1669. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1670. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1671. }
  1672. #endif
  1673. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1674. p_binary_op->is_constant = true;
  1675. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1676. bool valid = false;
  1677. 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);
  1678. if (!valid) {
  1679. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1680. 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);
  1681. } else {
  1682. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.)",
  1683. Variant::get_operator_name(p_binary_op->variant_op),
  1684. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1685. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1686. p_binary_op);
  1687. }
  1688. }
  1689. } else {
  1690. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1691. GDScriptParser::DataType test_type = right_type;
  1692. test_type.is_meta_type = false;
  1693. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1694. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1695. p_binary_op->reduced_value = false;
  1696. } else {
  1697. p_binary_op->reduced_value = true;
  1698. }
  1699. } else {
  1700. ERR_PRINT("Parser bug: unknown binary operation.");
  1701. }
  1702. }
  1703. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value, p_binary_op));
  1704. return;
  1705. }
  1706. GDScriptParser::DataType result;
  1707. if (left_type.is_variant() || right_type.is_variant()) {
  1708. // Cannot infer type because one operand can be anything.
  1709. result.kind = GDScriptParser::DataType::VARIANT;
  1710. mark_node_unsafe(p_binary_op);
  1711. } else {
  1712. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1713. bool valid = false;
  1714. result = get_operation_type(p_binary_op->variant_op, left_type, right_type, valid, p_binary_op);
  1715. if (!valid) {
  1716. 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);
  1717. }
  1718. } else {
  1719. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1720. GDScriptParser::DataType test_type = right_type;
  1721. test_type.is_meta_type = false;
  1722. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1723. // Test reverse as well to consider for subtypes.
  1724. if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) {
  1725. if (p_binary_op->left_operand->get_datatype().is_hard_type()) {
  1726. 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);
  1727. } else {
  1728. // TODO: Warning.
  1729. mark_node_unsafe(p_binary_op);
  1730. }
  1731. }
  1732. }
  1733. // "is" operator is always a boolean anyway.
  1734. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1735. result.kind = GDScriptParser::DataType::BUILTIN;
  1736. result.builtin_type = Variant::BOOL;
  1737. } else {
  1738. ERR_PRINT("Parser bug: unknown binary operation.");
  1739. }
  1740. }
  1741. }
  1742. p_binary_op->set_datatype(result);
  1743. }
  1744. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool is_await) {
  1745. bool all_is_constant = true;
  1746. Map<int, GDScriptParser::ArrayNode *> arrays; // For array literal to potentially type when passing.
  1747. for (int i = 0; i < p_call->arguments.size(); i++) {
  1748. reduce_expression(p_call->arguments[i]);
  1749. if (p_call->arguments[i]->type == GDScriptParser::Node::ARRAY) {
  1750. arrays[i] = static_cast<GDScriptParser::ArrayNode *>(p_call->arguments[i]);
  1751. }
  1752. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  1753. }
  1754. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  1755. GDScriptParser::DataType call_type;
  1756. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  1757. // Call to name directly.
  1758. StringName function_name = p_call->function_name;
  1759. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  1760. if (builtin_type < Variant::VARIANT_MAX) {
  1761. // Is a builtin constructor.
  1762. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1763. call_type.kind = GDScriptParser::DataType::BUILTIN;
  1764. call_type.builtin_type = builtin_type;
  1765. if (builtin_type == Variant::OBJECT) {
  1766. call_type.kind = GDScriptParser::DataType::NATIVE;
  1767. call_type.native_type = function_name; // "Object".
  1768. }
  1769. bool safe_to_fold = true;
  1770. switch (builtin_type) {
  1771. // Those are stored by reference so not suited for compile-time construction.
  1772. // Because in this case they would be the same reference in all constructed values.
  1773. case Variant::OBJECT:
  1774. case Variant::DICTIONARY:
  1775. case Variant::ARRAY:
  1776. case Variant::PACKED_BYTE_ARRAY:
  1777. case Variant::PACKED_INT32_ARRAY:
  1778. case Variant::PACKED_INT64_ARRAY:
  1779. case Variant::PACKED_FLOAT32_ARRAY:
  1780. case Variant::PACKED_FLOAT64_ARRAY:
  1781. case Variant::PACKED_STRING_ARRAY:
  1782. case Variant::PACKED_VECTOR2_ARRAY:
  1783. case Variant::PACKED_VECTOR3_ARRAY:
  1784. case Variant::PACKED_COLOR_ARRAY:
  1785. safe_to_fold = false;
  1786. break;
  1787. default:
  1788. break;
  1789. }
  1790. if (all_is_constant && safe_to_fold) {
  1791. // Construct here.
  1792. Vector<const Variant *> args;
  1793. for (int i = 0; i < p_call->arguments.size(); i++) {
  1794. args.push_back(&(p_call->arguments[i]->reduced_value));
  1795. }
  1796. Callable::CallError err;
  1797. Variant value;
  1798. Variant::construct(builtin_type, value, (const Variant **)args.ptr(), args.size(), err);
  1799. switch (err.error) {
  1800. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  1801. 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,
  1802. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  1803. p_call->arguments[err.argument]);
  1804. break;
  1805. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1806. String signature = Variant::get_type_name(builtin_type) + "(";
  1807. for (int i = 0; i < p_call->arguments.size(); i++) {
  1808. if (i > 0) {
  1809. signature += ", ";
  1810. }
  1811. signature += p_call->arguments[i]->get_datatype().to_string();
  1812. }
  1813. signature += ")";
  1814. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  1815. } break;
  1816. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1817. 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);
  1818. break;
  1819. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1820. 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);
  1821. break;
  1822. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1823. break; // Can't happen in a builtin constructor.
  1824. case Callable::CallError::CALL_OK:
  1825. p_call->is_constant = true;
  1826. p_call->reduced_value = value;
  1827. break;
  1828. }
  1829. } else {
  1830. // TODO: Check constructors without constants.
  1831. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  1832. if (p_call->arguments.size() == 1) {
  1833. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  1834. if (arg_type.is_variant()) {
  1835. mark_node_unsafe(p_call->arguments[0]);
  1836. } else {
  1837. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  1838. // Okay.
  1839. p_call->set_datatype(call_type);
  1840. return;
  1841. }
  1842. }
  1843. }
  1844. List<MethodInfo> constructors;
  1845. Variant::get_constructor_list(builtin_type, &constructors);
  1846. bool match = false;
  1847. for (const MethodInfo &info : constructors) {
  1848. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  1849. continue;
  1850. }
  1851. if (p_call->arguments.size() > info.arguments.size()) {
  1852. continue;
  1853. }
  1854. bool types_match = true;
  1855. for (int i = 0; i < p_call->arguments.size(); i++) {
  1856. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  1857. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  1858. types_match = false;
  1859. break;
  1860. #ifdef DEBUG_ENABLED
  1861. } else {
  1862. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT) {
  1863. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  1864. }
  1865. #endif
  1866. }
  1867. }
  1868. if (types_match) {
  1869. match = true;
  1870. call_type = type_from_property(info.return_val);
  1871. break;
  1872. }
  1873. }
  1874. if (!match) {
  1875. String signature = Variant::get_type_name(builtin_type) + "(";
  1876. for (int i = 0; i < p_call->arguments.size(); i++) {
  1877. if (i > 0) {
  1878. signature += ", ";
  1879. }
  1880. signature += p_call->arguments[i]->get_datatype().to_string();
  1881. }
  1882. signature += ")";
  1883. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  1884. }
  1885. }
  1886. p_call->set_datatype(call_type);
  1887. return;
  1888. } else if (GDScriptUtilityFunctions::function_exists(function_name)) {
  1889. MethodInfo function_info = GDScriptUtilityFunctions::get_function_info(function_name);
  1890. if (all_is_constant && GDScriptUtilityFunctions::is_function_constant(function_name)) {
  1891. // Can call on compilation.
  1892. Vector<const Variant *> args;
  1893. for (int i = 0; i < p_call->arguments.size(); i++) {
  1894. args.push_back(&(p_call->arguments[i]->reduced_value));
  1895. }
  1896. Variant value;
  1897. Callable::CallError err;
  1898. GDScriptUtilityFunctions::get_function(function_name)(&value, (const Variant **)args.ptr(), args.size(), err);
  1899. switch (err.error) {
  1900. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1901. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  1902. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  1903. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  1904. p_call->arguments[err.argument]);
  1905. } break;
  1906. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  1907. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  1908. break;
  1909. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1910. 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);
  1911. break;
  1912. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1913. 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);
  1914. break;
  1915. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1916. break; // Can't happen in a builtin constructor.
  1917. case Callable::CallError::CALL_OK:
  1918. p_call->is_constant = true;
  1919. p_call->reduced_value = value;
  1920. break;
  1921. }
  1922. } else {
  1923. validate_call_arg(function_info, p_call);
  1924. }
  1925. p_call->set_datatype(type_from_property(function_info.return_val));
  1926. return;
  1927. } else if (Variant::has_utility_function(function_name)) {
  1928. MethodInfo function_info = info_from_utility_func(function_name);
  1929. if (all_is_constant && Variant::get_utility_function_type(function_name) == Variant::UTILITY_FUNC_TYPE_MATH) {
  1930. // Can call on compilation.
  1931. Vector<const Variant *> args;
  1932. for (int i = 0; i < p_call->arguments.size(); i++) {
  1933. args.push_back(&(p_call->arguments[i]->reduced_value));
  1934. }
  1935. Variant value;
  1936. Callable::CallError err;
  1937. Variant::call_utility_function(function_name, &value, (const Variant **)args.ptr(), args.size(), err);
  1938. switch (err.error) {
  1939. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1940. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  1941. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", function_name, err.argument + 1,
  1942. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  1943. p_call->arguments[err.argument]);
  1944. } break;
  1945. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  1946. push_error(vformat(R"(Invalid call for function "%s".)", function_name), p_call);
  1947. break;
  1948. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1949. 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);
  1950. break;
  1951. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1952. 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);
  1953. break;
  1954. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1955. break; // Can't happen in a builtin constructor.
  1956. case Callable::CallError::CALL_OK:
  1957. p_call->is_constant = true;
  1958. p_call->reduced_value = value;
  1959. break;
  1960. }
  1961. } else {
  1962. validate_call_arg(function_info, p_call);
  1963. }
  1964. p_call->set_datatype(type_from_property(function_info.return_val));
  1965. return;
  1966. }
  1967. }
  1968. GDScriptParser::DataType base_type;
  1969. call_type.kind = GDScriptParser::DataType::VARIANT;
  1970. bool is_self = false;
  1971. if (p_call->is_super) {
  1972. base_type = parser->current_class->base_type;
  1973. base_type.is_meta_type = false;
  1974. is_self = true;
  1975. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1976. base_type = parser->current_class->get_datatype();
  1977. base_type.is_meta_type = false;
  1978. is_self = true;
  1979. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  1980. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  1981. if (subscript->base == nullptr) {
  1982. // Invalid syntax, error already set on parser.
  1983. p_call->set_datatype(call_type);
  1984. mark_node_unsafe(p_call);
  1985. return;
  1986. }
  1987. if (!subscript->is_attribute) {
  1988. // Invalid call. Error already sent in parser.
  1989. // TODO: Could check if Callable here.
  1990. p_call->set_datatype(call_type);
  1991. mark_node_unsafe(p_call);
  1992. return;
  1993. }
  1994. if (subscript->attribute == nullptr) {
  1995. // Invalid call. Error already sent in parser.
  1996. p_call->set_datatype(call_type);
  1997. mark_node_unsafe(p_call);
  1998. return;
  1999. }
  2000. GDScriptParser::IdentifierNode *base_id = nullptr;
  2001. if (subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2002. base_id = static_cast<GDScriptParser::IdentifierNode *>(subscript->base);
  2003. }
  2004. if (base_id && GDScriptParser::get_builtin_type(base_id->name) < Variant::VARIANT_MAX) {
  2005. base_type = make_builtin_meta_type(GDScriptParser::get_builtin_type(base_id->name));
  2006. } else {
  2007. reduce_expression(subscript->base);
  2008. base_type = subscript->base->get_datatype();
  2009. }
  2010. } else {
  2011. // Invalid call. Error already sent in parser.
  2012. // TODO: Could check if Callable here too.
  2013. p_call->set_datatype(call_type);
  2014. mark_node_unsafe(p_call);
  2015. return;
  2016. }
  2017. bool is_static = false;
  2018. bool is_vararg = false;
  2019. int default_arg_count = 0;
  2020. GDScriptParser::DataType return_type;
  2021. List<GDScriptParser::DataType> par_types;
  2022. if (get_function_signature(p_call, base_type, p_call->function_name, return_type, par_types, default_arg_count, is_static, is_vararg)) {
  2023. // If the function require typed arrays we must make literals be typed.
  2024. for (Map<int, GDScriptParser::ArrayNode *>::Element *E = arrays.front(); E; E = E->next()) {
  2025. int index = E->key();
  2026. if (index < par_types.size() && par_types[index].has_container_element_type()) {
  2027. update_array_literal_element_type(par_types[index], E->get());
  2028. }
  2029. }
  2030. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  2031. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  2032. 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);
  2033. } else if (!is_self && base_type.is_meta_type && !is_static) {
  2034. base_type.is_meta_type = false; // For `to_string()`.
  2035. 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);
  2036. } else if (is_self && !is_static && !lambda_stack.is_empty()) {
  2037. push_error(vformat(R"*(Cannot call non-static function "%s()" from a lambda function.)*", p_call->function_name), p_call->callee);
  2038. }
  2039. call_type = return_type;
  2040. } else {
  2041. // Check if the name exists as something else.
  2042. bool found = false;
  2043. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  2044. GDScriptParser::IdentifierNode *callee_id;
  2045. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  2046. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  2047. } else {
  2048. // Can only be attribute.
  2049. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  2050. }
  2051. if (callee_id) {
  2052. reduce_identifier_from_base(callee_id, &base_type);
  2053. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  2054. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  2055. found = true;
  2056. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  2057. 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);
  2058. } else {
  2059. 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);
  2060. }
  2061. #ifdef DEBUG_ENABLED
  2062. } else if (!is_self && !(base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN)) {
  2063. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  2064. mark_node_unsafe(p_call);
  2065. #endif
  2066. }
  2067. }
  2068. }
  2069. if (!found && (is_self || (base_type.is_hard_type() && base_type.kind == GDScriptParser::DataType::BUILTIN))) {
  2070. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  2071. 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);
  2072. }
  2073. }
  2074. if (call_type.is_coroutine && !is_await) {
  2075. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call->callee);
  2076. }
  2077. p_call->set_datatype(call_type);
  2078. }
  2079. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  2080. reduce_expression(p_cast->operand);
  2081. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  2082. if (!cast_type.is_set()) {
  2083. return;
  2084. }
  2085. cast_type.is_meta_type = false; // The casted value won't be a type name.
  2086. p_cast->set_datatype(cast_type);
  2087. if (!cast_type.is_variant()) {
  2088. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  2089. if (!op_type.is_variant()) {
  2090. bool valid = false;
  2091. if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  2092. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  2093. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  2094. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  2095. }
  2096. if (!valid) {
  2097. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  2098. }
  2099. }
  2100. } else {
  2101. mark_node_unsafe(p_cast);
  2102. }
  2103. #ifdef DEBUG_ENABLED
  2104. if (p_cast->operand->get_datatype().is_variant()) {
  2105. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  2106. mark_node_unsafe(p_cast);
  2107. }
  2108. #endif
  2109. // TODO: Perform cast on constants.
  2110. }
  2111. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2112. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
  2113. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2114. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2115. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  2116. reduce_expression(element.key);
  2117. }
  2118. reduce_expression(element.value);
  2119. if (element.key->is_constant) {
  2120. if (elements.has(element.key->reduced_value)) {
  2121. 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);
  2122. } else {
  2123. elements[element.key->reduced_value] = element.value;
  2124. }
  2125. }
  2126. }
  2127. // It's dictionary in any case.
  2128. GDScriptParser::DataType dict_type;
  2129. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2130. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  2131. dict_type.builtin_type = Variant::DICTIONARY;
  2132. dict_type.is_constant = true;
  2133. p_dictionary->set_datatype(dict_type);
  2134. }
  2135. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  2136. GDScriptParser::DataType result;
  2137. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2138. result.kind = GDScriptParser::DataType::NATIVE;
  2139. result.native_type = "Node";
  2140. result.builtin_type = Variant::OBJECT;
  2141. if (!ClassDB::is_parent_class(parser->current_class->base_type.native_type, result.native_type)) {
  2142. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  2143. } else if (!lambda_stack.is_empty()) {
  2144. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") inside a lambda. Use a captured variable instead.)*", p_get_node);
  2145. }
  2146. p_get_node->set_datatype(result);
  2147. }
  2148. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name, const GDScriptParser::Node *p_source) {
  2149. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(p_class_name));
  2150. Error err = ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2151. if (err) {
  2152. push_error(vformat(R"(Could not resolve class "%s", because of a parser error.)", p_class_name), p_source);
  2153. GDScriptParser::DataType type;
  2154. type.type_source = GDScriptParser::DataType::UNDETECTED;
  2155. type.kind = GDScriptParser::DataType::VARIANT;
  2156. return type;
  2157. }
  2158. GDScriptParser::DataType type;
  2159. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2160. type.kind = GDScriptParser::DataType::CLASS;
  2161. type.builtin_type = Variant::OBJECT;
  2162. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  2163. type.class_type = ref->get_parser()->head;
  2164. type.script_path = ref->get_parser()->script_path;
  2165. type.is_constant = true;
  2166. type.is_meta_type = true;
  2167. return type;
  2168. }
  2169. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  2170. GDScriptParser::DataType base;
  2171. if (p_base == nullptr) {
  2172. base = type_from_metatype(parser->current_class->get_datatype());
  2173. } else {
  2174. base = *p_base;
  2175. }
  2176. const StringName &name = p_identifier->name;
  2177. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  2178. if (base.is_meta_type) {
  2179. bool valid = true;
  2180. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  2181. if (valid) {
  2182. p_identifier->is_constant = true;
  2183. p_identifier->reduced_value = result;
  2184. p_identifier->set_datatype(type_from_variant(result, p_identifier));
  2185. } else if (base.is_hard_type()) {
  2186. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  2187. }
  2188. } else {
  2189. switch (base.builtin_type) {
  2190. case Variant::NIL: {
  2191. if (base.is_hard_type()) {
  2192. push_error(vformat(R"(Invalid get index "%s" on base Nil)", name), p_identifier);
  2193. }
  2194. return;
  2195. }
  2196. case Variant::DICTIONARY: {
  2197. GDScriptParser::DataType dummy;
  2198. dummy.kind = GDScriptParser::DataType::VARIANT;
  2199. p_identifier->set_datatype(dummy);
  2200. return;
  2201. }
  2202. default: {
  2203. Callable::CallError temp;
  2204. Variant dummy;
  2205. Variant::construct(base.builtin_type, dummy, nullptr, 0, temp);
  2206. List<PropertyInfo> properties;
  2207. dummy.get_property_list(&properties);
  2208. for (const PropertyInfo &prop : properties) {
  2209. if (prop.name == name) {
  2210. p_identifier->set_datatype(type_from_property(prop));
  2211. return;
  2212. }
  2213. }
  2214. if (base.is_hard_type()) {
  2215. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  2216. }
  2217. }
  2218. }
  2219. }
  2220. return;
  2221. }
  2222. if (base.kind == GDScriptParser::DataType::ENUM) {
  2223. if (base.is_meta_type) {
  2224. if (base.enum_values.has(name)) {
  2225. p_identifier->is_constant = true;
  2226. p_identifier->reduced_value = base.enum_values[name];
  2227. GDScriptParser::DataType result;
  2228. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2229. result.kind = GDScriptParser::DataType::ENUM_VALUE;
  2230. result.builtin_type = base.builtin_type;
  2231. result.native_type = base.native_type;
  2232. result.enum_type = name;
  2233. p_identifier->set_datatype(result);
  2234. } else {
  2235. // Consider as a Dictionary
  2236. GDScriptParser::DataType dummy;
  2237. dummy.kind = GDScriptParser::DataType::VARIANT;
  2238. p_identifier->set_datatype(dummy);
  2239. }
  2240. } else {
  2241. push_error(R"(Cannot get property from enum value.)", p_identifier);
  2242. }
  2243. return;
  2244. }
  2245. GDScriptParser::ClassNode *base_class = base.class_type;
  2246. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  2247. while (base_class != nullptr) {
  2248. if (base_class->identifier && base_class->identifier->name == name) {
  2249. p_identifier->set_datatype(base_class->get_datatype());
  2250. return;
  2251. }
  2252. if (base_class->has_member(name)) {
  2253. const GDScriptParser::ClassNode::Member &member = base_class->get_member(name);
  2254. p_identifier->set_datatype(member.get_datatype());
  2255. switch (member.type) {
  2256. case GDScriptParser::ClassNode::Member::CONSTANT:
  2257. // For out-of-order resolution:
  2258. reduce_expression(member.constant->initializer);
  2259. p_identifier->is_constant = true;
  2260. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2261. p_identifier->set_datatype(member.constant->initializer->get_datatype());
  2262. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2263. p_identifier->constant_source = member.constant;
  2264. break;
  2265. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  2266. p_identifier->is_constant = true;
  2267. p_identifier->reduced_value = member.enum_value.value;
  2268. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  2269. break;
  2270. case GDScriptParser::ClassNode::Member::VARIABLE:
  2271. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  2272. p_identifier->variable_source = member.variable;
  2273. member.variable->usages += 1;
  2274. break;
  2275. case GDScriptParser::ClassNode::Member::FUNCTION:
  2276. resolve_function_signature(member.function);
  2277. p_identifier->set_datatype(make_callable_type(member.function->info));
  2278. break;
  2279. case GDScriptParser::ClassNode::Member::CLASS:
  2280. // For out-of-order resolution:
  2281. resolve_class_interface(member.m_class);
  2282. p_identifier->set_datatype(member.m_class->get_datatype());
  2283. break;
  2284. default:
  2285. break; // Type already set.
  2286. }
  2287. return;
  2288. }
  2289. // Check outer constants.
  2290. // TODO: Allow outer static functions.
  2291. GDScriptParser::ClassNode *outer = base_class->outer;
  2292. while (outer != nullptr) {
  2293. if (outer->has_member(name)) {
  2294. const GDScriptParser::ClassNode::Member &member = outer->get_member(name);
  2295. switch (member.type) {
  2296. case GDScriptParser::ClassNode::Member::CONSTANT: {
  2297. // TODO: Make sure loops won't cause problem. And make special error message for those.
  2298. // For out-of-order resolution:
  2299. reduce_expression(member.constant->initializer);
  2300. p_identifier->set_datatype(member.get_datatype());
  2301. p_identifier->is_constant = true;
  2302. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  2303. return;
  2304. } break;
  2305. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  2306. p_identifier->set_datatype(member.get_datatype());
  2307. p_identifier->is_constant = true;
  2308. p_identifier->reduced_value = member.enum_value.value;
  2309. return;
  2310. } break;
  2311. case GDScriptParser::ClassNode::Member::ENUM: {
  2312. p_identifier->set_datatype(member.get_datatype());
  2313. p_identifier->is_constant = false;
  2314. return;
  2315. } break;
  2316. default:
  2317. break;
  2318. }
  2319. }
  2320. outer = outer->outer;
  2321. }
  2322. base_class = base_class->base_type.class_type;
  2323. }
  2324. // Check native members.
  2325. const StringName &native = base.native_type;
  2326. if (class_exists(native)) {
  2327. PropertyInfo prop_info;
  2328. MethodInfo method_info;
  2329. if (ClassDB::get_property_info(native, name, &prop_info)) {
  2330. p_identifier->set_datatype(type_from_property(prop_info));
  2331. return;
  2332. }
  2333. if (ClassDB::get_method_info(native, name, &method_info)) {
  2334. // Method is callable.
  2335. p_identifier->set_datatype(make_callable_type(method_info));
  2336. return;
  2337. }
  2338. if (ClassDB::get_signal(native, name, &method_info)) {
  2339. // Signal is a type too.
  2340. p_identifier->set_datatype(make_signal_type(method_info));
  2341. return;
  2342. }
  2343. if (ClassDB::has_enum(native, name)) {
  2344. p_identifier->set_datatype(make_native_enum_type(native, name));
  2345. return;
  2346. }
  2347. bool valid = false;
  2348. int int_constant = ClassDB::get_integer_constant(native, name, &valid);
  2349. if (valid) {
  2350. p_identifier->is_constant = true;
  2351. p_identifier->reduced_value = int_constant;
  2352. p_identifier->set_datatype(type_from_variant(int_constant, p_identifier));
  2353. return;
  2354. }
  2355. }
  2356. }
  2357. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  2358. // TODO: This is opportunity to further infer types.
  2359. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  2360. if (current_enum) {
  2361. for (int i = 0; i < current_enum->values.size(); i++) {
  2362. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  2363. if (element.identifier->name == p_identifier->name) {
  2364. GDScriptParser::DataType type;
  2365. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2366. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM_VALUE : GDScriptParser::DataType::BUILTIN;
  2367. type.builtin_type = Variant::INT;
  2368. type.is_constant = true;
  2369. if (element.parent_enum->identifier) {
  2370. type.enum_type = element.parent_enum->identifier->name;
  2371. }
  2372. p_identifier->set_datatype(type);
  2373. if (element.resolved) {
  2374. p_identifier->is_constant = true;
  2375. p_identifier->reduced_value = element.value;
  2376. } else {
  2377. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  2378. }
  2379. return; // Found anyway.
  2380. }
  2381. }
  2382. }
  2383. bool found_source = false;
  2384. // Check if identifier is local.
  2385. // If that's the case, the declaration already was solved before.
  2386. switch (p_identifier->source) {
  2387. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  2388. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  2389. found_source = true;
  2390. break;
  2391. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  2392. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  2393. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  2394. p_identifier->is_constant = true;
  2395. // TODO: Constant should have a value on the node itself.
  2396. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  2397. found_source = true;
  2398. break;
  2399. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  2400. p_identifier->variable_source->usages++;
  2401. [[fallthrough]];
  2402. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  2403. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  2404. found_source = true;
  2405. break;
  2406. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  2407. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  2408. found_source = true;
  2409. break;
  2410. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  2411. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  2412. result.is_constant = true;
  2413. p_identifier->set_datatype(result);
  2414. found_source = true;
  2415. } break;
  2416. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  2417. break;
  2418. }
  2419. // Not a local, so check members.
  2420. if (!found_source) {
  2421. reduce_identifier_from_base(p_identifier);
  2422. if (p_identifier->source != GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->get_datatype().is_set()) {
  2423. // Found.
  2424. found_source = true;
  2425. }
  2426. }
  2427. if (found_source) {
  2428. // If the identifier is local, check if it's any kind of capture by comparing their source function.
  2429. // Only capture locals and members and enum values. Constants are still accessible from the lambda using the script reference.
  2430. if (p_identifier->source == GDScriptParser::IdentifierNode::UNDEFINED_SOURCE || p_identifier->source == GDScriptParser::IdentifierNode::MEMBER_CONSTANT || lambda_stack.is_empty()) {
  2431. return;
  2432. }
  2433. GDScriptParser::FunctionNode *function_test = lambda_stack.back()->get()->function;
  2434. 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)) {
  2435. function_test->source_lambda->captures_indices[p_identifier->name] = function_test->source_lambda->captures.size();
  2436. function_test->source_lambda->captures.push_back(p_identifier);
  2437. function_test = function_test->source_lambda->parent_function;
  2438. }
  2439. return;
  2440. }
  2441. StringName name = p_identifier->name;
  2442. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2443. // Check globals. We make an exception for Variant::OBJECT because it's the base class for
  2444. // non-builtin types so we allow doing e.g. Object.new()
  2445. Variant::Type builtin_type = GDScriptParser::get_builtin_type(name);
  2446. if (builtin_type != Variant::OBJECT && builtin_type < Variant::VARIANT_MAX) {
  2447. if (can_be_builtin) {
  2448. p_identifier->set_datatype(make_builtin_meta_type(builtin_type));
  2449. return;
  2450. } else {
  2451. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2452. }
  2453. }
  2454. if (class_exists(name)) {
  2455. p_identifier->set_datatype(make_native_meta_type(name));
  2456. return;
  2457. }
  2458. if (ScriptServer::is_global_class(name)) {
  2459. p_identifier->set_datatype(make_global_class_meta_type(name, p_identifier));
  2460. return;
  2461. }
  2462. // Try singletons.
  2463. // Do this before globals because this might be a singleton loading another one before it's compiled.
  2464. if (ProjectSettings::get_singleton()->has_autoload(name)) {
  2465. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(name);
  2466. if (autoload.is_singleton) {
  2467. // Singleton exists, so it's at least a Node.
  2468. GDScriptParser::DataType result;
  2469. result.kind = GDScriptParser::DataType::NATIVE;
  2470. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2471. if (autoload.path.to_lower().ends_with(GDScriptLanguage::get_singleton()->get_extension())) {
  2472. Ref<GDScriptParserRef> parser = get_parser_for(autoload.path);
  2473. if (parser.is_valid()) {
  2474. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2475. if (err == OK) {
  2476. result = type_from_metatype(parser->get_parser()->head->get_datatype());
  2477. }
  2478. }
  2479. }
  2480. result.is_constant = true;
  2481. p_identifier->set_datatype(result);
  2482. return;
  2483. }
  2484. }
  2485. if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) {
  2486. int idx = GDScriptLanguage::get_singleton()->get_global_map()[name];
  2487. Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  2488. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2489. p_identifier->is_constant = true;
  2490. p_identifier->reduced_value = constant;
  2491. return;
  2492. }
  2493. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) {
  2494. Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name];
  2495. p_identifier->set_datatype(type_from_variant(constant, p_identifier));
  2496. p_identifier->is_constant = true;
  2497. p_identifier->reduced_value = constant;
  2498. return;
  2499. }
  2500. // Not found.
  2501. // Check if it's a builtin function.
  2502. if (GDScriptUtilityFunctions::function_exists(name)) {
  2503. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2504. } else {
  2505. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2506. }
  2507. GDScriptParser::DataType dummy;
  2508. dummy.kind = GDScriptParser::DataType::VARIANT;
  2509. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2510. }
  2511. void GDScriptAnalyzer::reduce_lambda(GDScriptParser::LambdaNode *p_lambda) {
  2512. // Lambda is always a Callable.
  2513. GDScriptParser::DataType lambda_type;
  2514. lambda_type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  2515. lambda_type.kind = GDScriptParser::DataType::BUILTIN;
  2516. lambda_type.builtin_type = Variant::CALLABLE;
  2517. p_lambda->set_datatype(lambda_type);
  2518. if (p_lambda->function == nullptr) {
  2519. return;
  2520. }
  2521. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  2522. parser->current_function = p_lambda->function;
  2523. lambda_stack.push_back(p_lambda);
  2524. for (int i = 0; i < p_lambda->function->parameters.size(); i++) {
  2525. resolve_parameter(p_lambda->function->parameters[i]);
  2526. }
  2527. resolve_suite(p_lambda->function->body);
  2528. int captures_amount = p_lambda->captures.size();
  2529. if (captures_amount > 0) {
  2530. // Create space for lambda parameters.
  2531. // At the beginning to not mess with optional parameters.
  2532. int param_count = p_lambda->function->parameters.size();
  2533. p_lambda->function->parameters.resize(param_count + captures_amount);
  2534. for (int i = param_count - 1; i >= 0; i--) {
  2535. p_lambda->function->parameters.write[i + captures_amount] = p_lambda->function->parameters[i];
  2536. p_lambda->function->parameters_indices[p_lambda->function->parameters[i]->identifier->name] = i + captures_amount;
  2537. }
  2538. // Add captures as extra parameters at the beginning.
  2539. for (int i = 0; i < p_lambda->captures.size(); i++) {
  2540. GDScriptParser::IdentifierNode *capture = p_lambda->captures[i];
  2541. GDScriptParser::ParameterNode *capture_param = parser->alloc_node<GDScriptParser::ParameterNode>();
  2542. capture_param->identifier = capture;
  2543. capture_param->usages = capture->usages;
  2544. capture_param->set_datatype(capture->get_datatype());
  2545. p_lambda->function->parameters.write[i] = capture_param;
  2546. p_lambda->function->parameters_indices[capture->name] = i;
  2547. }
  2548. }
  2549. lambda_stack.pop_back();
  2550. parser->current_function = previous_function;
  2551. }
  2552. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2553. p_literal->reduced_value = p_literal->value;
  2554. p_literal->is_constant = true;
  2555. p_literal->set_datatype(type_from_variant(p_literal->reduced_value, p_literal));
  2556. }
  2557. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2558. if (!p_preload->path) {
  2559. return;
  2560. }
  2561. reduce_expression(p_preload->path);
  2562. if (!p_preload->path->is_constant) {
  2563. push_error("Preloaded path must be a constant string.", p_preload->path);
  2564. return;
  2565. }
  2566. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2567. push_error("Preloaded path must be a constant string.", p_preload->path);
  2568. } else {
  2569. p_preload->resolved_path = p_preload->path->reduced_value;
  2570. // TODO: Save this as script dependency.
  2571. if (p_preload->resolved_path.is_relative_path()) {
  2572. p_preload->resolved_path = parser->script_path.get_base_dir().plus_file(p_preload->resolved_path);
  2573. }
  2574. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2575. if (!FileAccess::exists(p_preload->resolved_path)) {
  2576. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2577. } else {
  2578. // TODO: Don't load if validating: use completion cache.
  2579. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2580. if (p_preload->resource.is_null()) {
  2581. push_error(vformat(R"(Could not p_preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2582. }
  2583. }
  2584. }
  2585. p_preload->is_constant = true;
  2586. p_preload->reduced_value = p_preload->resource;
  2587. p_preload->set_datatype(type_from_variant(p_preload->reduced_value, p_preload));
  2588. }
  2589. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2590. p_self->is_constant = false;
  2591. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2592. }
  2593. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2594. if (p_subscript->base == nullptr) {
  2595. return;
  2596. }
  2597. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2598. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  2599. } else {
  2600. reduce_expression(p_subscript->base);
  2601. }
  2602. GDScriptParser::DataType result_type;
  2603. if (p_subscript->is_attribute) {
  2604. if (p_subscript->attribute == nullptr) {
  2605. return;
  2606. }
  2607. if (p_subscript->base->is_constant) {
  2608. // Just try to get it.
  2609. bool valid = false;
  2610. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, valid);
  2611. if (!valid) {
  2612. push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index);
  2613. } else {
  2614. p_subscript->is_constant = true;
  2615. p_subscript->reduced_value = value;
  2616. result_type = type_from_variant(value, p_subscript);
  2617. }
  2618. result_type.kind = GDScriptParser::DataType::VARIANT;
  2619. } else {
  2620. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2621. if (base_type.is_variant()) {
  2622. result_type.kind = GDScriptParser::DataType::VARIANT;
  2623. mark_node_unsafe(p_subscript);
  2624. } else {
  2625. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  2626. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  2627. if (attr_type.is_set()) {
  2628. result_type = attr_type;
  2629. p_subscript->is_constant = p_subscript->attribute->is_constant;
  2630. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  2631. } else {
  2632. if (base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2633. push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute);
  2634. #ifdef DEBUG_ENABLED
  2635. } else {
  2636. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  2637. #endif
  2638. }
  2639. result_type.kind = GDScriptParser::DataType::VARIANT;
  2640. }
  2641. }
  2642. }
  2643. } else {
  2644. if (p_subscript->index == nullptr) {
  2645. return;
  2646. }
  2647. reduce_expression(p_subscript->index);
  2648. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  2649. // Just try to get it.
  2650. bool valid = false;
  2651. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  2652. if (!valid) {
  2653. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  2654. } else {
  2655. p_subscript->is_constant = true;
  2656. p_subscript->reduced_value = value;
  2657. result_type = type_from_variant(value, p_subscript);
  2658. }
  2659. result_type.kind = GDScriptParser::DataType::VARIANT;
  2660. } else {
  2661. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2662. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  2663. if (base_type.is_variant()) {
  2664. result_type.kind = GDScriptParser::DataType::VARIANT;
  2665. mark_node_unsafe(p_subscript);
  2666. } else {
  2667. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2668. // Check if indexing is valid.
  2669. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  2670. if (!error) {
  2671. switch (base_type.builtin_type) {
  2672. // Expect int or real as index.
  2673. case Variant::PACKED_BYTE_ARRAY:
  2674. case Variant::PACKED_COLOR_ARRAY:
  2675. case Variant::PACKED_FLOAT32_ARRAY:
  2676. case Variant::PACKED_FLOAT64_ARRAY:
  2677. case Variant::PACKED_INT32_ARRAY:
  2678. case Variant::PACKED_INT64_ARRAY:
  2679. case Variant::PACKED_STRING_ARRAY:
  2680. case Variant::PACKED_VECTOR2_ARRAY:
  2681. case Variant::PACKED_VECTOR3_ARRAY:
  2682. case Variant::ARRAY:
  2683. case Variant::STRING:
  2684. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  2685. break;
  2686. // Expect String only.
  2687. case Variant::RECT2:
  2688. case Variant::RECT2I:
  2689. case Variant::PLANE:
  2690. case Variant::QUATERNION:
  2691. case Variant::AABB:
  2692. case Variant::OBJECT:
  2693. error = index_type.builtin_type != Variant::STRING;
  2694. break;
  2695. // Expect String or number.
  2696. case Variant::BASIS:
  2697. case Variant::VECTOR2:
  2698. case Variant::VECTOR2I:
  2699. case Variant::VECTOR3:
  2700. case Variant::VECTOR3I:
  2701. case Variant::TRANSFORM2D:
  2702. case Variant::TRANSFORM3D:
  2703. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  2704. index_type.builtin_type != Variant::STRING;
  2705. break;
  2706. // Expect String or int.
  2707. case Variant::COLOR:
  2708. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  2709. break;
  2710. // Don't support indexing, but we will check it later.
  2711. case Variant::RID:
  2712. case Variant::BOOL:
  2713. case Variant::CALLABLE:
  2714. case Variant::FLOAT:
  2715. case Variant::INT:
  2716. case Variant::NIL:
  2717. case Variant::NODE_PATH:
  2718. case Variant::SIGNAL:
  2719. case Variant::STRING_NAME:
  2720. break;
  2721. // Here for completeness.
  2722. case Variant::DICTIONARY:
  2723. case Variant::VARIANT_MAX:
  2724. break;
  2725. }
  2726. if (error) {
  2727. 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);
  2728. }
  2729. }
  2730. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2731. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  2732. 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);
  2733. }
  2734. }
  2735. // Check resulting type if possible.
  2736. result_type.builtin_type = Variant::NIL;
  2737. result_type.kind = GDScriptParser::DataType::BUILTIN;
  2738. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  2739. switch (base_type.builtin_type) {
  2740. // Can't index at all.
  2741. case Variant::RID:
  2742. case Variant::BOOL:
  2743. case Variant::CALLABLE:
  2744. case Variant::FLOAT:
  2745. case Variant::INT:
  2746. case Variant::NIL:
  2747. case Variant::NODE_PATH:
  2748. case Variant::SIGNAL:
  2749. case Variant::STRING_NAME:
  2750. result_type.kind = GDScriptParser::DataType::VARIANT;
  2751. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  2752. break;
  2753. // Return int.
  2754. case Variant::PACKED_BYTE_ARRAY:
  2755. case Variant::PACKED_INT32_ARRAY:
  2756. case Variant::PACKED_INT64_ARRAY:
  2757. case Variant::VECTOR2I:
  2758. case Variant::VECTOR3I:
  2759. result_type.builtin_type = Variant::INT;
  2760. break;
  2761. // Return float.
  2762. case Variant::PACKED_FLOAT32_ARRAY:
  2763. case Variant::PACKED_FLOAT64_ARRAY:
  2764. case Variant::VECTOR2:
  2765. case Variant::VECTOR3:
  2766. case Variant::QUATERNION:
  2767. result_type.builtin_type = Variant::FLOAT;
  2768. break;
  2769. // Return Color.
  2770. case Variant::PACKED_COLOR_ARRAY:
  2771. result_type.builtin_type = Variant::COLOR;
  2772. break;
  2773. // Return String.
  2774. case Variant::PACKED_STRING_ARRAY:
  2775. case Variant::STRING:
  2776. result_type.builtin_type = Variant::STRING;
  2777. break;
  2778. // Return Vector2.
  2779. case Variant::PACKED_VECTOR2_ARRAY:
  2780. case Variant::TRANSFORM2D:
  2781. case Variant::RECT2:
  2782. result_type.builtin_type = Variant::VECTOR2;
  2783. break;
  2784. // Return Vector2I.
  2785. case Variant::RECT2I:
  2786. result_type.builtin_type = Variant::VECTOR2I;
  2787. break;
  2788. // Return Vector3.
  2789. case Variant::PACKED_VECTOR3_ARRAY:
  2790. case Variant::AABB:
  2791. case Variant::BASIS:
  2792. result_type.builtin_type = Variant::VECTOR3;
  2793. break;
  2794. // Depends on the index.
  2795. case Variant::TRANSFORM3D:
  2796. case Variant::PLANE:
  2797. case Variant::COLOR:
  2798. case Variant::DICTIONARY:
  2799. result_type.kind = GDScriptParser::DataType::VARIANT;
  2800. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2801. break;
  2802. // Can have an element type.
  2803. case Variant::ARRAY:
  2804. if (base_type.has_container_element_type()) {
  2805. result_type = base_type.get_container_element_type();
  2806. result_type.type_source = base_type.type_source;
  2807. } else {
  2808. result_type.kind = GDScriptParser::DataType::VARIANT;
  2809. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2810. }
  2811. break;
  2812. // Here for completeness.
  2813. case Variant::OBJECT:
  2814. case Variant::VARIANT_MAX:
  2815. break;
  2816. }
  2817. }
  2818. }
  2819. }
  2820. p_subscript->set_datatype(result_type);
  2821. }
  2822. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  2823. reduce_expression(p_ternary_op->condition);
  2824. reduce_expression(p_ternary_op->true_expr);
  2825. reduce_expression(p_ternary_op->false_expr);
  2826. GDScriptParser::DataType result;
  2827. 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) {
  2828. p_ternary_op->is_constant = true;
  2829. if (p_ternary_op->condition->reduced_value.booleanize()) {
  2830. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  2831. } else {
  2832. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  2833. }
  2834. }
  2835. GDScriptParser::DataType true_type;
  2836. if (p_ternary_op->true_expr) {
  2837. true_type = p_ternary_op->true_expr->get_datatype();
  2838. } else {
  2839. true_type.kind = GDScriptParser::DataType::VARIANT;
  2840. }
  2841. GDScriptParser::DataType false_type;
  2842. if (p_ternary_op->false_expr) {
  2843. false_type = p_ternary_op->false_expr->get_datatype();
  2844. } else {
  2845. false_type.kind = GDScriptParser::DataType::VARIANT;
  2846. }
  2847. if (true_type.is_variant() || false_type.is_variant()) {
  2848. result.kind = GDScriptParser::DataType::VARIANT;
  2849. } else {
  2850. result = true_type;
  2851. if (!is_type_compatible(true_type, false_type)) {
  2852. result = false_type;
  2853. if (!is_type_compatible(false_type, true_type)) {
  2854. result.type_source = GDScriptParser::DataType::UNDETECTED;
  2855. result.kind = GDScriptParser::DataType::VARIANT;
  2856. #ifdef DEBUG_ENABLED
  2857. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  2858. #endif
  2859. }
  2860. }
  2861. }
  2862. p_ternary_op->set_datatype(result);
  2863. }
  2864. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  2865. reduce_expression(p_unary_op->operand);
  2866. GDScriptParser::DataType result;
  2867. if (p_unary_op->operand == nullptr) {
  2868. result.kind = GDScriptParser::DataType::VARIANT;
  2869. p_unary_op->set_datatype(result);
  2870. return;
  2871. }
  2872. if (p_unary_op->operand->is_constant) {
  2873. p_unary_op->is_constant = true;
  2874. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  2875. result = type_from_variant(p_unary_op->reduced_value, p_unary_op);
  2876. } else if (p_unary_op->operand->get_datatype().is_variant()) {
  2877. result.kind = GDScriptParser::DataType::VARIANT;
  2878. mark_node_unsafe(p_unary_op);
  2879. } else {
  2880. bool valid = false;
  2881. result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), valid, p_unary_op);
  2882. if (!valid) {
  2883. 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);
  2884. }
  2885. }
  2886. p_unary_op->set_datatype(result);
  2887. }
  2888. void GDScriptAnalyzer::const_fold_array(GDScriptParser::ArrayNode *p_array) {
  2889. bool all_is_constant = true;
  2890. for (int i = 0; i < p_array->elements.size(); i++) {
  2891. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  2892. all_is_constant = all_is_constant && element->is_constant;
  2893. if (!all_is_constant) {
  2894. return;
  2895. }
  2896. }
  2897. Array array;
  2898. array.resize(p_array->elements.size());
  2899. for (int i = 0; i < p_array->elements.size(); i++) {
  2900. array[i] = p_array->elements[i]->reduced_value;
  2901. }
  2902. p_array->is_constant = true;
  2903. p_array->reduced_value = array;
  2904. }
  2905. void GDScriptAnalyzer::const_fold_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  2906. bool all_is_constant = true;
  2907. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2908. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2909. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  2910. if (!all_is_constant) {
  2911. return;
  2912. }
  2913. }
  2914. Dictionary dict;
  2915. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  2916. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  2917. dict[element.key->reduced_value] = element.value->reduced_value;
  2918. }
  2919. p_dictionary->is_constant = true;
  2920. p_dictionary->reduced_value = dict;
  2921. }
  2922. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value, const GDScriptParser::Node *p_source) {
  2923. GDScriptParser::DataType result;
  2924. result.is_constant = true;
  2925. result.kind = GDScriptParser::DataType::BUILTIN;
  2926. result.builtin_type = p_value.get_type();
  2927. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  2928. if (p_value.get_type() == Variant::OBJECT) {
  2929. Object *obj = p_value;
  2930. if (!obj) {
  2931. return GDScriptParser::DataType();
  2932. }
  2933. result.native_type = obj->get_class_name();
  2934. Ref<Script> scr = p_value; // Check if value is a script itself.
  2935. if (scr.is_valid()) {
  2936. result.is_meta_type = true;
  2937. } else {
  2938. result.is_meta_type = false;
  2939. scr = obj->get_script();
  2940. }
  2941. if (scr.is_valid()) {
  2942. if (scr->is_valid()) {
  2943. result.script_type = scr;
  2944. result.script_path = scr->get_path();
  2945. Ref<GDScript> gds = scr;
  2946. if (gds.is_valid()) {
  2947. result.kind = GDScriptParser::DataType::CLASS;
  2948. // This might be an inner class, so we want to get the parser for the root.
  2949. // But still get the inner class from that tree.
  2950. GDScript *current = gds.ptr();
  2951. List<StringName> class_chain;
  2952. while (current->_owner) {
  2953. // Push to front so it's in reverse.
  2954. class_chain.push_front(current->name);
  2955. current = current->_owner;
  2956. }
  2957. Ref<GDScriptParserRef> ref = get_parser_for(current->path);
  2958. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2959. GDScriptParser::ClassNode *found = ref->get_parser()->head;
  2960. // It should be okay to assume this exists, since we have a complete script already.
  2961. for (const StringName &E : class_chain) {
  2962. found = found->get_member(E).m_class;
  2963. }
  2964. result.class_type = found;
  2965. result.script_path = ref->get_parser()->script_path;
  2966. } else {
  2967. result.kind = GDScriptParser::DataType::SCRIPT;
  2968. }
  2969. result.native_type = scr->get_instance_base_type();
  2970. } else {
  2971. push_error(vformat(R"(Constant value uses script from "%s" which is loaded but not compiled.)", scr->get_path()), p_source);
  2972. result.kind = GDScriptParser::DataType::VARIANT;
  2973. result.type_source = GDScriptParser::DataType::UNDETECTED;
  2974. result.is_meta_type = false;
  2975. }
  2976. } else {
  2977. result.kind = GDScriptParser::DataType::NATIVE;
  2978. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  2979. result.is_meta_type = true;
  2980. }
  2981. }
  2982. }
  2983. return result;
  2984. }
  2985. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  2986. GDScriptParser::DataType result = p_meta_type;
  2987. result.is_meta_type = false;
  2988. result.is_constant = false;
  2989. return result;
  2990. }
  2991. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  2992. GDScriptParser::DataType result;
  2993. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2994. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  2995. // Variant
  2996. result.kind = GDScriptParser::DataType::VARIANT;
  2997. return result;
  2998. }
  2999. result.builtin_type = p_property.type;
  3000. if (p_property.type == Variant::OBJECT) {
  3001. result.kind = GDScriptParser::DataType::NATIVE;
  3002. result.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  3003. } else {
  3004. result.kind = GDScriptParser::DataType::BUILTIN;
  3005. result.builtin_type = p_property.type;
  3006. if (p_property.type == Variant::ARRAY && p_property.hint == PROPERTY_HINT_ARRAY_TYPE) {
  3007. // Check element type.
  3008. StringName elem_type_name = p_property.hint_string;
  3009. GDScriptParser::DataType elem_type;
  3010. elem_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3011. Variant::Type elem_builtin_type = GDScriptParser::get_builtin_type(elem_type_name);
  3012. if (elem_builtin_type < Variant::VARIANT_MAX) {
  3013. // Builtin type.
  3014. elem_type.kind = GDScriptParser::DataType::BUILTIN;
  3015. elem_type.builtin_type = elem_builtin_type;
  3016. } else if (class_exists(elem_type_name)) {
  3017. elem_type.kind = GDScriptParser::DataType::NATIVE;
  3018. elem_type.builtin_type = Variant::OBJECT;
  3019. elem_type.native_type = p_property.hint_string;
  3020. } else if (ScriptServer::is_global_class(elem_type_name)) {
  3021. // Just load this as it shouldn't be a GDScript.
  3022. Ref<Script> script = ResourceLoader::load(ScriptServer::get_global_class_path(elem_type_name));
  3023. elem_type.kind = GDScriptParser::DataType::SCRIPT;
  3024. elem_type.builtin_type = Variant::OBJECT;
  3025. elem_type.native_type = script->get_instance_base_type();
  3026. elem_type.script_type = script;
  3027. } else {
  3028. ERR_FAIL_V_MSG(result, "Could not find element type from property hint of a typed array.");
  3029. }
  3030. result.set_container_element_type(elem_type);
  3031. }
  3032. }
  3033. return result;
  3034. }
  3035. 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) {
  3036. r_static = false;
  3037. r_vararg = false;
  3038. r_default_arg_count = 0;
  3039. StringName function_name = p_function;
  3040. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  3041. // Construct a base type to get methods.
  3042. Callable::CallError err;
  3043. Variant dummy;
  3044. Variant::construct(p_base_type.builtin_type, dummy, nullptr, 0, err);
  3045. if (err.error != Callable::CallError::CALL_OK) {
  3046. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  3047. }
  3048. List<MethodInfo> methods;
  3049. dummy.get_method_list(&methods);
  3050. for (const MethodInfo &E : methods) {
  3051. if (E.name == p_function) {
  3052. function_signature_from_info(E, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3053. r_static = Variant::is_builtin_method_static(p_base_type.builtin_type, function_name);
  3054. return true;
  3055. }
  3056. }
  3057. return false;
  3058. }
  3059. bool is_constructor = (p_base_type.is_meta_type || (p_source->callee && p_source->callee->type == GDScriptParser::Node::IDENTIFIER)) && p_function == StaticCString::create("new");
  3060. if (is_constructor) {
  3061. function_name = "_init";
  3062. r_static = true;
  3063. }
  3064. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  3065. GDScriptParser::FunctionNode *found_function = nullptr;
  3066. while (found_function == nullptr && base_class != nullptr) {
  3067. if (base_class->has_member(function_name)) {
  3068. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  3069. // TODO: If this is Callable it can have a better error message.
  3070. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  3071. return false;
  3072. }
  3073. found_function = base_class->get_member(function_name).function;
  3074. }
  3075. base_class = base_class->base_type.class_type;
  3076. }
  3077. if (found_function != nullptr) {
  3078. r_static = is_constructor || found_function->is_static;
  3079. for (int i = 0; i < found_function->parameters.size(); i++) {
  3080. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  3081. if (found_function->parameters[i]->default_value != nullptr) {
  3082. r_default_arg_count++;
  3083. }
  3084. }
  3085. r_return_type = found_function->get_datatype();
  3086. r_return_type.is_meta_type = false;
  3087. r_return_type.is_coroutine = found_function->is_coroutine;
  3088. return true;
  3089. }
  3090. Ref<Script> base_script = p_base_type.script_type;
  3091. while (base_script.is_valid() && base_script->is_valid()) {
  3092. MethodInfo info = base_script->get_method_info(function_name);
  3093. if (!(info == MethodInfo())) {
  3094. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3095. }
  3096. base_script = base_script->get_base_script();
  3097. }
  3098. // If the base is a script, it might be trying to access members of the Script class itself.
  3099. if (p_base_type.is_meta_type && !is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  3100. MethodInfo info;
  3101. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  3102. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  3103. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3104. }
  3105. }
  3106. StringName base_native = p_base_type.native_type;
  3107. #ifdef DEBUG_ENABLED
  3108. if (base_native != StringName()) {
  3109. // Empty native class might happen in some Script implementations.
  3110. // Just ignore it.
  3111. if (!class_exists(base_native)) {
  3112. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  3113. }
  3114. }
  3115. #endif
  3116. if (is_constructor) {
  3117. // Native types always have a default constructor.
  3118. r_return_type = p_base_type;
  3119. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  3120. r_return_type.is_meta_type = false;
  3121. return true;
  3122. }
  3123. MethodInfo info;
  3124. if (ClassDB::get_method_info(base_native, function_name, &info)) {
  3125. bool valid = function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  3126. if (valid && Engine::get_singleton()->has_singleton(base_native)) {
  3127. r_static = true;
  3128. }
  3129. return valid;
  3130. }
  3131. return false;
  3132. }
  3133. 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) {
  3134. r_return_type = type_from_property(p_info.return_val);
  3135. r_default_arg_count = p_info.default_arguments.size();
  3136. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  3137. for (const PropertyInfo &E : p_info.arguments) {
  3138. r_par_types.push_back(type_from_property(E));
  3139. }
  3140. return true;
  3141. }
  3142. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  3143. List<GDScriptParser::DataType> arg_types;
  3144. for (const PropertyInfo &E : p_method.arguments) {
  3145. arg_types.push_back(type_from_property(E));
  3146. }
  3147. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  3148. }
  3149. 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) {
  3150. bool valid = true;
  3151. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  3152. 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);
  3153. valid = false;
  3154. }
  3155. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  3156. 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()]);
  3157. valid = false;
  3158. }
  3159. for (int i = 0; i < p_call->arguments.size(); i++) {
  3160. if (i >= p_par_types.size()) {
  3161. // Already on vararg place.
  3162. break;
  3163. }
  3164. GDScriptParser::DataType par_type = p_par_types[i];
  3165. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  3166. if (arg_type.is_variant()) {
  3167. // Argument can be anything, so this is unsafe.
  3168. mark_node_unsafe(p_call->arguments[i]);
  3169. } else if (par_type.is_hard_type() && !is_type_compatible(par_type, arg_type, true)) {
  3170. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  3171. mark_node_unsafe(p_call);
  3172. if (!is_type_compatible(arg_type, par_type)) {
  3173. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  3174. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  3175. p_call->arguments[i]);
  3176. valid = false;
  3177. }
  3178. #ifdef DEBUG_ENABLED
  3179. } else {
  3180. 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) {
  3181. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  3182. }
  3183. #endif
  3184. }
  3185. }
  3186. return valid;
  3187. }
  3188. #ifdef DEBUG_ENABLED
  3189. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  3190. const StringName &name = p_local->name;
  3191. GDScriptParser::DataType base = parser->current_class->get_datatype();
  3192. GDScriptParser::ClassNode *base_class = base.class_type;
  3193. while (base_class != nullptr) {
  3194. if (base_class->has_member(name)) {
  3195. 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()));
  3196. return true;
  3197. }
  3198. base_class = base_class->base_type.class_type;
  3199. }
  3200. StringName base_native = base.native_type;
  3201. ERR_FAIL_COND_V_MSG(!class_exists(base_native), false, "Non-existent native base class.");
  3202. StringName parent = base_native;
  3203. while (parent != StringName()) {
  3204. if (ClassDB::has_method(parent, name, true)) {
  3205. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  3206. return true;
  3207. } else if (ClassDB::has_signal(parent, name, true)) {
  3208. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  3209. return true;
  3210. } else if (ClassDB::has_property(parent, name, true)) {
  3211. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  3212. return true;
  3213. } else if (ClassDB::has_integer_constant(parent, name, true)) {
  3214. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  3215. return true;
  3216. } else if (ClassDB::has_enum(parent, name, true)) {
  3217. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  3218. return true;
  3219. }
  3220. parent = ClassDB::get_parent_class(parent);
  3221. }
  3222. return false;
  3223. }
  3224. #endif
  3225. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, bool &r_valid, const GDScriptParser::Node *p_source) {
  3226. // Unary version.
  3227. GDScriptParser::DataType nil_type;
  3228. nil_type.builtin_type = Variant::NIL;
  3229. return get_operation_type(p_operation, p_a, nil_type, r_valid, p_source);
  3230. }
  3231. 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) {
  3232. GDScriptParser::DataType result;
  3233. result.kind = GDScriptParser::DataType::VARIANT;
  3234. Variant::Type a_type = p_a.builtin_type;
  3235. Variant::Type b_type = p_b.builtin_type;
  3236. Variant::ValidatedOperatorEvaluator op_eval = Variant::get_validated_operator_evaluator(p_operation, a_type, b_type);
  3237. if (op_eval == nullptr) {
  3238. r_valid = false;
  3239. return result;
  3240. }
  3241. r_valid = true;
  3242. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  3243. result.kind = GDScriptParser::DataType::BUILTIN;
  3244. result.builtin_type = Variant::get_operator_return_type(p_operation, a_type, b_type);
  3245. return result;
  3246. }
  3247. // TODO: Add safe/unsafe return variable (for variant cases)
  3248. bool GDScriptAnalyzer::is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion) const {
  3249. // These return "true" so it doesn't affect users negatively.
  3250. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  3251. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  3252. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  3253. // Variant can receive anything.
  3254. return true;
  3255. }
  3256. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  3257. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  3258. return true;
  3259. }
  3260. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  3261. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  3262. if (!valid && p_allow_implicit_conversion) {
  3263. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  3264. }
  3265. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  3266. // Enum value is also integer.
  3267. valid = true;
  3268. }
  3269. if (valid && p_target.builtin_type == Variant::ARRAY && p_source.builtin_type == Variant::ARRAY) {
  3270. // Check the element type.
  3271. if (p_target.has_container_element_type()) {
  3272. if (!p_source.has_container_element_type()) {
  3273. // TODO: Maybe this is valid but unsafe?
  3274. // Variant array can't be appended to typed array.
  3275. valid = false;
  3276. } else {
  3277. valid = is_type_compatible(p_target.get_container_element_type(), p_source.get_container_element_type(), false);
  3278. }
  3279. }
  3280. }
  3281. return valid;
  3282. }
  3283. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  3284. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  3285. return true;
  3286. }
  3287. if (p_source.kind == GDScriptParser::DataType::ENUM) {
  3288. if (p_source.native_type == p_target.native_type) {
  3289. return true;
  3290. }
  3291. }
  3292. if (p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  3293. if (p_source.native_type == p_target.native_type && p_target.enum_values.has(p_source.enum_type)) {
  3294. return true;
  3295. }
  3296. }
  3297. return false;
  3298. }
  3299. // From here on the target type is an object, so we have to test polymorphism.
  3300. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  3301. // null is acceptable in object.
  3302. return true;
  3303. }
  3304. StringName src_native;
  3305. Ref<Script> src_script;
  3306. const GDScriptParser::ClassNode *src_class = nullptr;
  3307. switch (p_source.kind) {
  3308. case GDScriptParser::DataType::NATIVE:
  3309. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  3310. // Non-native class cannot be supertype of native.
  3311. return false;
  3312. }
  3313. if (p_source.is_meta_type) {
  3314. src_native = GDScriptNativeClass::get_class_static();
  3315. } else {
  3316. src_native = p_source.native_type;
  3317. }
  3318. break;
  3319. case GDScriptParser::DataType::SCRIPT:
  3320. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  3321. // A script type cannot be a subtype of a GDScript class.
  3322. return false;
  3323. }
  3324. if (p_source.is_meta_type) {
  3325. src_native = p_source.script_type->get_class_name();
  3326. } else {
  3327. src_script = p_source.script_type;
  3328. src_native = src_script->get_instance_base_type();
  3329. }
  3330. break;
  3331. case GDScriptParser::DataType::CLASS:
  3332. if (p_source.is_meta_type) {
  3333. src_native = GDScript::get_class_static();
  3334. } else {
  3335. src_class = p_source.class_type;
  3336. const GDScriptParser::ClassNode *base = src_class;
  3337. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  3338. base = base->base_type.class_type;
  3339. }
  3340. src_native = base->base_type.native_type;
  3341. src_script = base->base_type.script_type;
  3342. }
  3343. break;
  3344. case GDScriptParser::DataType::VARIANT:
  3345. case GDScriptParser::DataType::BUILTIN:
  3346. case GDScriptParser::DataType::ENUM:
  3347. case GDScriptParser::DataType::ENUM_VALUE:
  3348. case GDScriptParser::DataType::UNRESOLVED:
  3349. break; // Already solved before.
  3350. }
  3351. switch (p_target.kind) {
  3352. case GDScriptParser::DataType::NATIVE: {
  3353. if (p_target.is_meta_type) {
  3354. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  3355. }
  3356. return ClassDB::is_parent_class(src_native, p_target.native_type);
  3357. }
  3358. case GDScriptParser::DataType::SCRIPT:
  3359. if (p_target.is_meta_type) {
  3360. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  3361. }
  3362. while (src_script.is_valid()) {
  3363. if (src_script == p_target.script_type) {
  3364. return true;
  3365. }
  3366. src_script = src_script->get_base_script();
  3367. }
  3368. return false;
  3369. case GDScriptParser::DataType::CLASS:
  3370. if (p_target.is_meta_type) {
  3371. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  3372. }
  3373. while (src_class != nullptr) {
  3374. if (src_class->fqcn == p_target.class_type->fqcn) {
  3375. return true;
  3376. }
  3377. src_class = src_class->base_type.class_type;
  3378. }
  3379. return false;
  3380. case GDScriptParser::DataType::VARIANT:
  3381. case GDScriptParser::DataType::BUILTIN:
  3382. case GDScriptParser::DataType::ENUM:
  3383. case GDScriptParser::DataType::ENUM_VALUE:
  3384. case GDScriptParser::DataType::UNRESOLVED:
  3385. break; // Already solved before.
  3386. }
  3387. return false;
  3388. }
  3389. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  3390. mark_node_unsafe(p_origin);
  3391. parser->push_error(p_message, p_origin);
  3392. }
  3393. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  3394. #ifdef DEBUG_ENABLED
  3395. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  3396. parser->unsafe_lines.insert(i);
  3397. }
  3398. #endif
  3399. }
  3400. bool GDScriptAnalyzer::class_exists(const StringName &p_class) const {
  3401. return ClassDB::class_exists(p_class) && ClassDB::is_class_exposed(p_class);
  3402. }
  3403. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  3404. Ref<GDScriptParserRef> ref;
  3405. if (depended_parsers.has(p_path)) {
  3406. ref = depended_parsers[p_path];
  3407. } else {
  3408. Error err = OK;
  3409. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  3410. depended_parsers[p_path] = ref;
  3411. }
  3412. return ref;
  3413. }
  3414. Error GDScriptAnalyzer::resolve_inheritance() {
  3415. return resolve_inheritance(parser->head);
  3416. }
  3417. Error GDScriptAnalyzer::resolve_interface() {
  3418. resolve_class_interface(parser->head);
  3419. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3420. }
  3421. Error GDScriptAnalyzer::resolve_body() {
  3422. resolve_class_body(parser->head);
  3423. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3424. }
  3425. Error GDScriptAnalyzer::resolve_program() {
  3426. resolve_class_interface(parser->head);
  3427. resolve_class_body(parser->head);
  3428. List<String> parser_keys;
  3429. depended_parsers.get_key_list(&parser_keys);
  3430. for (const String &E : parser_keys) {
  3431. if (depended_parsers[E].is_null()) {
  3432. return ERR_PARSE_ERROR;
  3433. }
  3434. depended_parsers[E]->raise_status(GDScriptParserRef::FULLY_SOLVED);
  3435. }
  3436. return parser->errors.is_empty() ? OK : ERR_PARSE_ERROR;
  3437. }
  3438. Error GDScriptAnalyzer::analyze() {
  3439. parser->errors.clear();
  3440. Error err = resolve_inheritance(parser->head);
  3441. if (err) {
  3442. return err;
  3443. }
  3444. return resolve_program();
  3445. }
  3446. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  3447. parser = p_parser;
  3448. }