gdscript_analyzer.cpp 123 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277
  1. /*************************************************************************/
  2. /* gdscript_analyzer.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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/class_db.h"
  32. #include "core/hash_map.h"
  33. #include "core/io/resource_loader.h"
  34. #include "core/os/file_access.h"
  35. #include "core/project_settings.h"
  36. #include "core/script_language.h"
  37. #include "gdscript.h"
  38. // TODO: Move this to a central location (maybe core?).
  39. static HashMap<StringName, StringName> underscore_map;
  40. static const char *underscore_classes[] = {
  41. "ClassDB",
  42. "Directory",
  43. "Engine",
  44. "File",
  45. "Geometry",
  46. "GodotSharp",
  47. "JSON",
  48. "Marshalls",
  49. "Mutex",
  50. "OS",
  51. "ResourceLoader",
  52. "ResourceSaver",
  53. "Semaphore",
  54. "Thread",
  55. "VisualScriptEditor",
  56. nullptr,
  57. };
  58. static StringName get_real_class_name(const StringName &p_source) {
  59. if (underscore_map.empty()) {
  60. const char **class_name = underscore_classes;
  61. while (*class_name != nullptr) {
  62. underscore_map[*class_name] = String("_") + *class_name;
  63. class_name++;
  64. }
  65. }
  66. if (underscore_map.has(p_source)) {
  67. return underscore_map[p_source];
  68. }
  69. return p_source;
  70. }
  71. void GDScriptAnalyzer::cleanup() {
  72. underscore_map.clear();
  73. }
  74. static GDScriptParser::DataType make_callable_type(const MethodInfo &p_info) {
  75. GDScriptParser::DataType type;
  76. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  77. type.kind = GDScriptParser::DataType::BUILTIN;
  78. type.builtin_type = Variant::CALLABLE;
  79. type.is_constant = true;
  80. type.method_info = p_info;
  81. return type;
  82. }
  83. static GDScriptParser::DataType make_signal_type(const MethodInfo &p_info) {
  84. GDScriptParser::DataType type;
  85. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  86. type.kind = GDScriptParser::DataType::BUILTIN;
  87. type.builtin_type = Variant::SIGNAL;
  88. type.is_constant = true;
  89. type.method_info = p_info;
  90. return type;
  91. }
  92. static GDScriptParser::DataType make_native_meta_type(const StringName &p_class_name) {
  93. GDScriptParser::DataType type;
  94. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  95. type.kind = GDScriptParser::DataType::NATIVE;
  96. type.builtin_type = Variant::OBJECT;
  97. type.is_constant = true;
  98. type.native_type = p_class_name;
  99. type.is_meta_type = true;
  100. return type;
  101. }
  102. static GDScriptParser::DataType make_native_enum_type(const StringName &p_native_class, const StringName &p_enum_name) {
  103. GDScriptParser::DataType type;
  104. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  105. type.kind = GDScriptParser::DataType::ENUM;
  106. type.builtin_type = Variant::OBJECT;
  107. type.is_constant = true;
  108. type.is_meta_type = true;
  109. List<StringName> enum_values;
  110. StringName real_native_name = get_real_class_name(p_native_class);
  111. ClassDB::get_enum_constants(real_native_name, p_enum_name, &enum_values);
  112. for (const List<StringName>::Element *E = enum_values.front(); E != nullptr; E = E->next()) {
  113. type.enum_values[E->get()] = ClassDB::get_integer_constant(real_native_name, E->get());
  114. }
  115. return type;
  116. }
  117. static GDScriptParser::DataType make_builtin_meta_type(Variant::Type p_type) {
  118. GDScriptParser::DataType type;
  119. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  120. type.kind = GDScriptParser::DataType::BUILTIN;
  121. type.builtin_type = p_type;
  122. type.is_constant = true;
  123. type.is_meta_type = true;
  124. return type;
  125. }
  126. Error GDScriptAnalyzer::resolve_inheritance(GDScriptParser::ClassNode *p_class, bool p_recursive) {
  127. if (p_class->base_type.is_set()) {
  128. // Already resolved
  129. return OK;
  130. }
  131. if (p_class == parser->head) {
  132. if (p_class->identifier) {
  133. p_class->fqcn = p_class->identifier->name;
  134. } else {
  135. p_class->fqcn = parser->script_path;
  136. }
  137. } else {
  138. p_class->fqcn = p_class->outer->fqcn + "::" + String(p_class->identifier->name);
  139. }
  140. GDScriptParser::DataType result;
  141. // Set datatype for class.
  142. GDScriptParser::DataType class_type;
  143. class_type.is_constant = true;
  144. class_type.is_meta_type = true;
  145. class_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  146. class_type.kind = GDScriptParser::DataType::CLASS;
  147. class_type.class_type = p_class;
  148. class_type.script_path = parser->script_path;
  149. p_class->set_datatype(class_type);
  150. if (!p_class->extends_used) {
  151. result.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  152. result.kind = GDScriptParser::DataType::NATIVE;
  153. result.native_type = "Reference";
  154. } else {
  155. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  156. GDScriptParser::DataType base;
  157. int extends_index = 0;
  158. if (!p_class->extends_path.empty()) {
  159. Ref<GDScriptParserRef> parser = get_parser_for(p_class->extends_path);
  160. if (parser.is_null()) {
  161. push_error(vformat(R"(Could not resolve super class path "%s".)", p_class->extends_path), p_class);
  162. return ERR_PARSE_ERROR;
  163. }
  164. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  165. if (err != OK) {
  166. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", p_class->extends_path), p_class);
  167. return err;
  168. }
  169. base = parser->get_parser()->head->get_datatype();
  170. } else {
  171. if (p_class->extends.empty()) {
  172. return ERR_PARSE_ERROR;
  173. }
  174. const StringName &name = p_class->extends[extends_index++];
  175. base.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  176. if (ScriptServer::is_global_class(name)) {
  177. String base_path = ScriptServer::get_global_class_path(name);
  178. if (base_path == parser->script_path) {
  179. base = parser->head->get_datatype();
  180. } else {
  181. Ref<GDScriptParserRef> parser = get_parser_for(base_path);
  182. if (parser.is_null()) {
  183. push_error(vformat(R"(Could not resolve super class "%s".)", name), p_class);
  184. return ERR_PARSE_ERROR;
  185. }
  186. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  187. if (err != OK) {
  188. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  189. return err;
  190. }
  191. }
  192. } else if (ProjectSettings::get_singleton()->has_autoload(name) && ProjectSettings::get_singleton()->get_autoload(name).is_singleton) {
  193. const ProjectSettings::AutoloadInfo &info = ProjectSettings::get_singleton()->get_autoload(name);
  194. if (info.path.get_extension().to_lower() != ".gd") {
  195. push_error(vformat(R"(Singleton %s is not a GDScript.)", info.name), p_class);
  196. return ERR_PARSE_ERROR;
  197. }
  198. Ref<GDScriptParserRef> parser = get_parser_for(info.path);
  199. if (parser.is_null()) {
  200. push_error(vformat(R"(Could not parse singleton from "%s".)", info.path), p_class);
  201. return ERR_PARSE_ERROR;
  202. }
  203. Error err = parser->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  204. if (err != OK) {
  205. push_error(vformat(R"(Could not resolve super class inheritance from "%s".)", name), p_class);
  206. return err;
  207. }
  208. } else if (class_exists(name) && ClassDB::can_instance(get_real_class_name(name))) {
  209. base.kind = GDScriptParser::DataType::NATIVE;
  210. base.native_type = name;
  211. } else {
  212. // Look for other classes in script.
  213. GDScriptParser::ClassNode *look_class = p_class;
  214. bool found = false;
  215. while (look_class != nullptr) {
  216. if (look_class->identifier && look_class->identifier->name == name) {
  217. if (!look_class->get_datatype().is_set()) {
  218. Error err = resolve_inheritance(look_class, false);
  219. if (err) {
  220. return err;
  221. }
  222. }
  223. base = look_class->get_datatype();
  224. found = true;
  225. break;
  226. }
  227. if (look_class->members_indices.has(name) && look_class->get_member(name).type == GDScriptParser::ClassNode::Member::CLASS) {
  228. GDScriptParser::ClassNode::Member member = look_class->get_member(name);
  229. if (!member.m_class->get_datatype().is_set()) {
  230. Error err = resolve_inheritance(member.m_class, false);
  231. if (err) {
  232. return err;
  233. }
  234. }
  235. base = member.m_class->get_datatype();
  236. found = true;
  237. break;
  238. }
  239. look_class = look_class->outer;
  240. }
  241. if (!found) {
  242. push_error(vformat(R"(Could not find base class "%s".)", name), p_class);
  243. return ERR_PARSE_ERROR;
  244. }
  245. }
  246. }
  247. for (int index = extends_index; index < p_class->extends.size(); index++) {
  248. if (base.kind != GDScriptParser::DataType::CLASS) {
  249. push_error(R"(Super type "%s" is not a GDScript. Cannot get nested types.)", p_class);
  250. return ERR_PARSE_ERROR;
  251. }
  252. // TODO: Extends could use identifier nodes. That way errors can be pointed out properly and it can be used here.
  253. GDScriptParser::IdentifierNode *id = parser->alloc_node<GDScriptParser::IdentifierNode>();
  254. id->name = p_class->extends[index];
  255. reduce_identifier_from_base(id, &base);
  256. GDScriptParser::DataType id_type = id->get_datatype();
  257. if (!id_type.is_set()) {
  258. push_error(vformat(R"(Could not find type "%s" under base "%s".)", id->name, base.to_string()), p_class);
  259. }
  260. base = id_type;
  261. }
  262. result = base;
  263. }
  264. if (!result.is_set()) {
  265. // TODO: More specific error messages.
  266. push_error(vformat(R"(Could not resolve inheritance for class "%s".)", p_class->identifier == nullptr ? "<main>" : p_class->identifier->name), p_class);
  267. return ERR_PARSE_ERROR;
  268. }
  269. p_class->base_type = result;
  270. class_type.native_type = result.native_type;
  271. p_class->set_datatype(class_type);
  272. if (p_recursive) {
  273. for (int i = 0; i < p_class->members.size(); i++) {
  274. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  275. resolve_inheritance(p_class->members[i].m_class, true);
  276. }
  277. }
  278. }
  279. return OK;
  280. }
  281. GDScriptParser::DataType GDScriptAnalyzer::resolve_datatype(GDScriptParser::TypeNode *p_type) {
  282. GDScriptParser::DataType result;
  283. if (p_type == nullptr) {
  284. result.kind = GDScriptParser::DataType::VARIANT;
  285. return result;
  286. }
  287. result.type_source = result.ANNOTATED_EXPLICIT;
  288. result.builtin_type = Variant::OBJECT;
  289. if (p_type->type_chain.empty()) {
  290. // void.
  291. result.kind = GDScriptParser::DataType::BUILTIN;
  292. result.builtin_type = Variant::NIL;
  293. p_type->set_datatype(result);
  294. return result;
  295. }
  296. StringName first = p_type->type_chain[0]->name;
  297. if (first == "Variant") {
  298. result.kind = GDScriptParser::DataType::VARIANT;
  299. if (p_type->type_chain.size() > 1) {
  300. push_error(R"("Variant" type don't contain nested types.)", p_type->type_chain[1]);
  301. return GDScriptParser::DataType();
  302. }
  303. return result;
  304. }
  305. if (first == "Object") {
  306. result.kind = GDScriptParser::DataType::NATIVE;
  307. result.native_type = "Object";
  308. if (p_type->type_chain.size() > 1) {
  309. push_error(R"("Object" type don't contain nested types.)", p_type->type_chain[1]);
  310. return GDScriptParser::DataType();
  311. }
  312. return result;
  313. }
  314. if (GDScriptParser::get_builtin_type(first) < Variant::VARIANT_MAX) {
  315. // Built-in types.
  316. if (p_type->type_chain.size() > 1) {
  317. push_error(R"(Built-in types don't contain nested types.)", p_type->type_chain[1]);
  318. return GDScriptParser::DataType();
  319. }
  320. result.kind = GDScriptParser::DataType::BUILTIN;
  321. result.builtin_type = GDScriptParser::get_builtin_type(first);
  322. } else if (class_exists(first)) {
  323. // Native engine classes.
  324. result.kind = GDScriptParser::DataType::NATIVE;
  325. result.native_type = first;
  326. } else if (ScriptServer::is_global_class(first)) {
  327. if (parser->script_path == ScriptServer::get_global_class_path(first)) {
  328. result = parser->head->get_datatype();
  329. } else {
  330. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(first));
  331. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  332. push_error(vformat(R"(Could not parse global class "%s" from "%s".)", first, ScriptServer::get_global_class_path(first)), p_type);
  333. return GDScriptParser::DataType();
  334. }
  335. result = ref->get_parser()->head->get_datatype();
  336. }
  337. } else if (ProjectSettings::get_singleton()->has_autoload(first) && ProjectSettings::get_singleton()->get_autoload(first).is_singleton) {
  338. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(first);
  339. Ref<GDScriptParserRef> ref = get_parser_for(autoload.path);
  340. if (ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED) != OK) {
  341. push_error(vformat(R"(Could not parse singleton "%s" from "%s".)", first, autoload.path), p_type);
  342. return GDScriptParser::DataType();
  343. }
  344. result = ref->get_parser()->head->get_datatype();
  345. } else if (ClassDB::has_enum(get_real_class_name(parser->current_class->base_type.native_type), first)) {
  346. // Native enum in current class.
  347. result = make_native_enum_type(parser->current_class->base_type.native_type, first);
  348. } else {
  349. // Classes in current scope.
  350. GDScriptParser::ClassNode *script_class = parser->current_class;
  351. bool found = false;
  352. while (!found && script_class != nullptr) {
  353. if (script_class->identifier && script_class->identifier->name == first) {
  354. result = script_class->get_datatype();
  355. found = true;
  356. break;
  357. }
  358. if (script_class->members_indices.has(first)) {
  359. GDScriptParser::ClassNode::Member member = script_class->members[script_class->members_indices[first]];
  360. switch (member.type) {
  361. case GDScriptParser::ClassNode::Member::CLASS:
  362. result = member.m_class->get_datatype();
  363. found = true;
  364. break;
  365. case GDScriptParser::ClassNode::Member::ENUM:
  366. result = member.m_enum->get_datatype();
  367. found = true;
  368. break;
  369. case GDScriptParser::ClassNode::Member::CONSTANT:
  370. if (member.constant->get_datatype().is_meta_type) {
  371. result = member.constant->get_datatype();
  372. found = true;
  373. break;
  374. }
  375. [[fallthrough]];
  376. default:
  377. push_error(vformat(R"("%s" is a %s but does not contain a type.)", first, member.get_type_name()), p_type);
  378. return GDScriptParser::DataType();
  379. }
  380. }
  381. script_class = script_class->outer;
  382. }
  383. }
  384. if (!result.is_set()) {
  385. push_error(vformat(R"("%s" was not found in the current scope.)", first), p_type);
  386. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  387. return result;
  388. }
  389. if (p_type->type_chain.size() > 1) {
  390. if (result.kind == GDScriptParser::DataType::CLASS) {
  391. for (int i = 1; i < p_type->type_chain.size(); i++) {
  392. GDScriptParser::DataType base = result;
  393. reduce_identifier_from_base(p_type->type_chain[i], &base);
  394. result = p_type->type_chain[i]->get_datatype();
  395. if (!result.is_set()) {
  396. 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]);
  397. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  398. return result;
  399. } else if (!result.is_meta_type) {
  400. 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]);
  401. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  402. return result;
  403. }
  404. }
  405. } else if (result.kind == GDScriptParser::DataType::NATIVE) {
  406. // Only enums allowed for native.
  407. if (ClassDB::has_enum(get_real_class_name(result.native_type), p_type->type_chain[1]->name)) {
  408. if (p_type->type_chain.size() > 2) {
  409. push_error(R"(Enums cannot contain nested types.)", p_type->type_chain[2]);
  410. } else {
  411. result = make_native_enum_type(result.native_type, p_type->type_chain[1]->name);
  412. }
  413. }
  414. } else {
  415. 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]);
  416. result.kind = GDScriptParser::DataType::VARIANT; // Leave Variant anyway so future type check don't use an unresolved type.
  417. return result;
  418. }
  419. }
  420. p_type->set_datatype(result);
  421. return result;
  422. }
  423. void GDScriptAnalyzer::resolve_class_interface(GDScriptParser::ClassNode *p_class) {
  424. if (p_class->resolved_interface) {
  425. return;
  426. }
  427. p_class->resolved_interface = true;
  428. GDScriptParser::ClassNode *previous_class = parser->current_class;
  429. parser->current_class = p_class;
  430. for (int i = 0; i < p_class->members.size(); i++) {
  431. GDScriptParser::ClassNode::Member member = p_class->members[i];
  432. switch (member.type) {
  433. case GDScriptParser::ClassNode::Member::VARIABLE: {
  434. GDScriptParser::DataType datatype;
  435. datatype.kind = GDScriptParser::DataType::VARIANT;
  436. datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  437. if (member.variable->initializer != nullptr) {
  438. member.variable->set_datatype(datatype); // Allow recursive usage.
  439. reduce_expression(member.variable->initializer);
  440. datatype = member.variable->initializer->get_datatype();
  441. }
  442. if (member.variable->datatype_specifier != nullptr) {
  443. datatype = resolve_datatype(member.variable->datatype_specifier);
  444. datatype.is_meta_type = false;
  445. if (member.variable->initializer != nullptr) {
  446. if (!is_type_compatible(datatype, member.variable->initializer->get_datatype(), true)) {
  447. // Try reverse test since it can be a masked subtype.
  448. if (!is_type_compatible(member.variable->initializer->get_datatype(), datatype, true)) {
  449. 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);
  450. } else {
  451. // TODO: Add warning.
  452. mark_node_unsafe(member.variable->initializer);
  453. }
  454. } else if (datatype.builtin_type == Variant::INT && member.variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  455. #ifdef DEBUG_ENABLED
  456. parser->push_warning(member.variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  457. #endif
  458. }
  459. if (member.variable->initializer->get_datatype().is_variant()) {
  460. // TODO: Warn unsafe assign.
  461. mark_node_unsafe(member.variable->initializer);
  462. }
  463. }
  464. } else if (member.variable->infer_datatype) {
  465. if (member.variable->initializer == nullptr) {
  466. push_error(vformat(R"(Cannot infer the type of "%s" variable because there's no default value.)", member.variable->identifier->name), member.variable->identifier);
  467. } else if (!datatype.is_set() || datatype.has_no_type()) {
  468. 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);
  469. } else if (datatype.is_variant()) {
  470. 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);
  471. } else if (datatype.builtin_type == Variant::NIL) {
  472. push_error(vformat(R"(Cannot infer the type of "%s" variable because the initial value is "null".)", member.variable->identifier->name), member.variable->initializer);
  473. }
  474. }
  475. datatype.is_constant = false;
  476. member.variable->set_datatype(datatype);
  477. if (!datatype.has_no_type()) {
  478. // TODO: Move this out into a routine specific to validate annotations.
  479. if (member.variable->export_info.hint == PROPERTY_HINT_TYPE_STRING) {
  480. // @export annotation.
  481. switch (datatype.kind) {
  482. case GDScriptParser::DataType::BUILTIN:
  483. member.variable->export_info.hint_string = Variant::get_type_name(datatype.builtin_type);
  484. break;
  485. case GDScriptParser::DataType::NATIVE:
  486. if (ClassDB::is_parent_class(get_real_class_name(datatype.native_type), "Resource")) {
  487. member.variable->export_info.hint = PROPERTY_HINT_RESOURCE_TYPE;
  488. member.variable->export_info.hint_string = get_real_class_name(datatype.native_type);
  489. } else {
  490. push_error(R"(Export type can only be built-in or a resource.)", member.variable);
  491. }
  492. break;
  493. default:
  494. // TODO: Allow custom user resources.
  495. push_error(R"(Export type can only be built-in or a resource.)", member.variable);
  496. break;
  497. }
  498. }
  499. }
  500. } break;
  501. case GDScriptParser::ClassNode::Member::CONSTANT: {
  502. reduce_expression(member.constant->initializer);
  503. GDScriptParser::DataType datatype = member.constant->get_datatype();
  504. if (member.constant->initializer) {
  505. if (!member.constant->initializer->is_constant) {
  506. push_error(R"(Initializer for a constant must be a constant expression.)", member.constant->initializer);
  507. }
  508. if (member.constant->datatype_specifier != nullptr) {
  509. datatype = resolve_datatype(member.constant->datatype_specifier);
  510. datatype.is_meta_type = false;
  511. if (!is_type_compatible(datatype, member.constant->initializer->get_datatype(), true)) {
  512. 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);
  513. } else if (datatype.builtin_type == Variant::INT && member.constant->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  514. #ifdef DEBUG_ENABLED
  515. parser->push_warning(member.constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  516. #endif
  517. }
  518. }
  519. }
  520. datatype.is_constant = true;
  521. member.constant->set_datatype(datatype);
  522. } break;
  523. case GDScriptParser::ClassNode::Member::SIGNAL: {
  524. for (int j = 0; j < member.signal->parameters.size(); j++) {
  525. GDScriptParser::DataType signal_type = resolve_datatype(member.signal->parameters[j]->datatype_specifier);
  526. signal_type.is_meta_type = false;
  527. member.signal->parameters[j]->set_datatype(signal_type);
  528. }
  529. // TODO: Make MethodInfo from signal.
  530. GDScriptParser::DataType signal_type;
  531. signal_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  532. signal_type.kind = GDScriptParser::DataType::BUILTIN;
  533. signal_type.builtin_type = Variant::SIGNAL;
  534. member.signal->set_datatype(signal_type);
  535. } break;
  536. case GDScriptParser::ClassNode::Member::ENUM: {
  537. GDScriptParser::DataType enum_type;
  538. enum_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  539. enum_type.kind = GDScriptParser::DataType::ENUM;
  540. enum_type.builtin_type = Variant::DICTIONARY;
  541. enum_type.enum_type = member.m_enum->identifier->name;
  542. enum_type.native_type = p_class->fqcn + "." + member.m_enum->identifier->name;
  543. enum_type.is_meta_type = true;
  544. enum_type.is_constant = true;
  545. // Enums can't be nested, so we can safely override this.
  546. current_enum = member.m_enum;
  547. for (int j = 0; j < member.m_enum->values.size(); j++) {
  548. GDScriptParser::EnumNode::Value &element = member.m_enum->values.write[j];
  549. if (element.custom_value) {
  550. reduce_expression(element.custom_value);
  551. if (!element.custom_value->is_constant) {
  552. push_error(R"(Enum values must be constant.)", element.custom_value);
  553. } else if (element.custom_value->reduced_value.get_type() != Variant::INT) {
  554. push_error(R"(Enum values must be integers.)", element.custom_value);
  555. } else {
  556. element.value = element.custom_value->reduced_value;
  557. element.resolved = true;
  558. }
  559. } else {
  560. if (element.index > 0) {
  561. element.value = element.parent_enum->values[element.index - 1].value + 1;
  562. } else {
  563. element.value = 0;
  564. }
  565. element.resolved = true;
  566. }
  567. enum_type.enum_values[element.identifier->name] = element.value;
  568. }
  569. current_enum = nullptr;
  570. member.m_enum->set_datatype(enum_type);
  571. } break;
  572. case GDScriptParser::ClassNode::Member::FUNCTION:
  573. resolve_function_signature(member.function);
  574. break;
  575. case GDScriptParser::ClassNode::Member::ENUM_VALUE: {
  576. if (member.enum_value.custom_value) {
  577. current_enum = member.enum_value.parent_enum;
  578. reduce_expression(member.enum_value.custom_value);
  579. current_enum = nullptr;
  580. if (!member.enum_value.custom_value->is_constant) {
  581. push_error(R"(Enum values must be constant.)", member.enum_value.custom_value);
  582. } else if (member.enum_value.custom_value->reduced_value.get_type() != Variant::INT) {
  583. push_error(R"(Enum values must be integers.)", member.enum_value.custom_value);
  584. } else {
  585. member.enum_value.value = member.enum_value.custom_value->reduced_value;
  586. member.enum_value.resolved = true;
  587. }
  588. } else {
  589. if (member.enum_value.index > 0) {
  590. member.enum_value.value = member.enum_value.parent_enum->values[member.enum_value.index - 1].value + 1;
  591. } else {
  592. member.enum_value.value = 0;
  593. }
  594. member.enum_value.resolved = true;
  595. }
  596. // Also update the original references.
  597. member.enum_value.parent_enum->values.write[member.enum_value.index] = member.enum_value;
  598. p_class->members.write[i].enum_value = member.enum_value;
  599. } break;
  600. case GDScriptParser::ClassNode::Member::CLASS:
  601. break; // Done later.
  602. case GDScriptParser::ClassNode::Member::UNDEFINED:
  603. ERR_PRINT("Trying to resolve undefined member.");
  604. break;
  605. }
  606. }
  607. // Recurse nested classes.
  608. for (int i = 0; i < p_class->members.size(); i++) {
  609. GDScriptParser::ClassNode::Member member = p_class->members[i];
  610. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  611. continue;
  612. }
  613. resolve_class_interface(member.m_class);
  614. }
  615. parser->current_class = previous_class;
  616. }
  617. void GDScriptAnalyzer::resolve_class_body(GDScriptParser::ClassNode *p_class) {
  618. if (p_class->resolved_body) {
  619. return;
  620. }
  621. p_class->resolved_body = true;
  622. GDScriptParser::ClassNode *previous_class = parser->current_class;
  623. parser->current_class = p_class;
  624. // Do functions now.
  625. for (int i = 0; i < p_class->members.size(); i++) {
  626. GDScriptParser::ClassNode::Member member = p_class->members[i];
  627. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  628. continue;
  629. }
  630. resolve_function_body(member.function);
  631. }
  632. parser->current_class = previous_class;
  633. // Recurse nested classes.
  634. for (int i = 0; i < p_class->members.size(); i++) {
  635. GDScriptParser::ClassNode::Member member = p_class->members[i];
  636. if (member.type != GDScriptParser::ClassNode::Member::CLASS) {
  637. continue;
  638. }
  639. resolve_class_body(member.m_class);
  640. }
  641. // Check unused variables.
  642. for (int i = 0; i < p_class->members.size(); i++) {
  643. GDScriptParser::ClassNode::Member member = p_class->members[i];
  644. if (member.type != GDScriptParser::ClassNode::Member::VARIABLE) {
  645. continue;
  646. }
  647. #ifdef DEBUG_ENABLED
  648. if (member.variable->usages == 0 && String(member.variable->identifier->name).begins_with("_")) {
  649. parser->push_warning(member.variable->identifier, GDScriptWarning::UNUSED_PRIVATE_CLASS_VARIABLE, member.variable->identifier->name);
  650. }
  651. #endif
  652. }
  653. }
  654. void GDScriptAnalyzer::resolve_node(GDScriptParser::Node *p_node) {
  655. ERR_FAIL_COND_MSG(p_node == nullptr, "Trying to resolve type of a null node.");
  656. switch (p_node->type) {
  657. case GDScriptParser::Node::NONE:
  658. break; // Unreachable.
  659. case GDScriptParser::Node::CLASS:
  660. resolve_class_interface(static_cast<GDScriptParser::ClassNode *>(p_node));
  661. resolve_class_body(static_cast<GDScriptParser::ClassNode *>(p_node));
  662. break;
  663. case GDScriptParser::Node::CONSTANT:
  664. resolve_constant(static_cast<GDScriptParser::ConstantNode *>(p_node));
  665. break;
  666. case GDScriptParser::Node::FOR:
  667. resolve_for(static_cast<GDScriptParser::ForNode *>(p_node));
  668. break;
  669. case GDScriptParser::Node::FUNCTION:
  670. resolve_function_signature(static_cast<GDScriptParser::FunctionNode *>(p_node));
  671. resolve_function_body(static_cast<GDScriptParser::FunctionNode *>(p_node));
  672. break;
  673. case GDScriptParser::Node::IF:
  674. resolve_if(static_cast<GDScriptParser::IfNode *>(p_node));
  675. break;
  676. case GDScriptParser::Node::SUITE:
  677. resolve_suite(static_cast<GDScriptParser::SuiteNode *>(p_node));
  678. break;
  679. case GDScriptParser::Node::VARIABLE:
  680. resolve_variable(static_cast<GDScriptParser::VariableNode *>(p_node));
  681. break;
  682. case GDScriptParser::Node::WHILE:
  683. resolve_while(static_cast<GDScriptParser::WhileNode *>(p_node));
  684. break;
  685. case GDScriptParser::Node::ANNOTATION:
  686. resolve_annotation(static_cast<GDScriptParser::AnnotationNode *>(p_node));
  687. break;
  688. case GDScriptParser::Node::ASSERT:
  689. resolve_assert(static_cast<GDScriptParser::AssertNode *>(p_node));
  690. break;
  691. case GDScriptParser::Node::MATCH:
  692. resolve_match(static_cast<GDScriptParser::MatchNode *>(p_node));
  693. break;
  694. case GDScriptParser::Node::MATCH_BRANCH:
  695. resolve_match_branch(static_cast<GDScriptParser::MatchBranchNode *>(p_node), nullptr);
  696. break;
  697. case GDScriptParser::Node::PARAMETER:
  698. resolve_pararameter(static_cast<GDScriptParser::ParameterNode *>(p_node));
  699. break;
  700. case GDScriptParser::Node::PATTERN:
  701. resolve_match_pattern(static_cast<GDScriptParser::PatternNode *>(p_node), nullptr);
  702. break;
  703. case GDScriptParser::Node::RETURN:
  704. resolve_return(static_cast<GDScriptParser::ReturnNode *>(p_node));
  705. break;
  706. case GDScriptParser::Node::TYPE:
  707. resolve_datatype(static_cast<GDScriptParser::TypeNode *>(p_node));
  708. break;
  709. // Resolving expression is the same as reducing them.
  710. case GDScriptParser::Node::ARRAY:
  711. case GDScriptParser::Node::ASSIGNMENT:
  712. case GDScriptParser::Node::AWAIT:
  713. case GDScriptParser::Node::BINARY_OPERATOR:
  714. case GDScriptParser::Node::CALL:
  715. case GDScriptParser::Node::CAST:
  716. case GDScriptParser::Node::DICTIONARY:
  717. case GDScriptParser::Node::GET_NODE:
  718. case GDScriptParser::Node::IDENTIFIER:
  719. case GDScriptParser::Node::LITERAL:
  720. case GDScriptParser::Node::PRELOAD:
  721. case GDScriptParser::Node::SELF:
  722. case GDScriptParser::Node::SUBSCRIPT:
  723. case GDScriptParser::Node::TERNARY_OPERATOR:
  724. case GDScriptParser::Node::UNARY_OPERATOR:
  725. reduce_expression(static_cast<GDScriptParser::ExpressionNode *>(p_node));
  726. break;
  727. case GDScriptParser::Node::BREAK:
  728. case GDScriptParser::Node::BREAKPOINT:
  729. case GDScriptParser::Node::CONTINUE:
  730. case GDScriptParser::Node::ENUM:
  731. case GDScriptParser::Node::PASS:
  732. case GDScriptParser::Node::SIGNAL:
  733. // Nothing to do.
  734. break;
  735. }
  736. }
  737. void GDScriptAnalyzer::resolve_annotation(GDScriptParser::AnnotationNode *p_annotation) {
  738. // TODO: Add second validation function for annotations, so they can use checked types.
  739. }
  740. void GDScriptAnalyzer::resolve_function_signature(GDScriptParser::FunctionNode *p_function) {
  741. if (p_function->resolved_signature) {
  742. return;
  743. }
  744. p_function->resolved_signature = true;
  745. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  746. parser->current_function = p_function;
  747. for (int i = 0; i < p_function->parameters.size(); i++) {
  748. resolve_pararameter(p_function->parameters[i]);
  749. #ifdef DEBUG_ENABLED
  750. if (p_function->parameters[i]->usages == 0 && !String(p_function->parameters[i]->identifier->name).begins_with("_")) {
  751. parser->push_warning(p_function->parameters[i]->identifier, GDScriptWarning::UNUSED_PARAMETER, p_function->identifier->name, p_function->parameters[i]->identifier->name);
  752. }
  753. is_shadowing(p_function->parameters[i]->identifier, "function parameter");
  754. #endif
  755. }
  756. if (p_function->identifier->name == "_init") {
  757. // Constructor.
  758. GDScriptParser::DataType return_type = parser->current_class->get_datatype();
  759. return_type.is_meta_type = false;
  760. p_function->set_datatype(return_type);
  761. if (p_function->return_type) {
  762. push_error("Constructor cannot have an explicit return type.", p_function->return_type);
  763. }
  764. } else {
  765. GDScriptParser::DataType return_type = resolve_datatype(p_function->return_type);
  766. p_function->set_datatype(return_type);
  767. }
  768. parser->current_function = previous_function;
  769. }
  770. void GDScriptAnalyzer::resolve_function_body(GDScriptParser::FunctionNode *p_function) {
  771. if (p_function->resolved_body) {
  772. return;
  773. }
  774. p_function->resolved_body = true;
  775. GDScriptParser::FunctionNode *previous_function = parser->current_function;
  776. parser->current_function = p_function;
  777. resolve_suite(p_function->body);
  778. GDScriptParser::DataType return_type = p_function->body->get_datatype();
  779. if (p_function->get_datatype().has_no_type() && return_type.is_set()) {
  780. // Use the suite inferred type if return isn't explicitly set.
  781. return_type.type_source = GDScriptParser::DataType::INFERRED;
  782. p_function->set_datatype(p_function->body->get_datatype());
  783. } 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)) {
  784. if (!p_function->body->has_return && p_function->identifier->name != GDScriptLanguage::get_singleton()->strings._init) {
  785. push_error(R"(Not all code paths return a value.)", p_function);
  786. }
  787. }
  788. parser->current_function = previous_function;
  789. }
  790. void GDScriptAnalyzer::decide_suite_type(GDScriptParser::Node *p_suite, GDScriptParser::Node *p_statement) {
  791. if (p_statement == nullptr) {
  792. return;
  793. }
  794. switch (p_statement->type) {
  795. case GDScriptParser::Node::IF:
  796. case GDScriptParser::Node::FOR:
  797. case GDScriptParser::Node::MATCH:
  798. case GDScriptParser::Node::PATTERN:
  799. case GDScriptParser::Node::RETURN:
  800. case GDScriptParser::Node::WHILE:
  801. // Use return or nested suite type as this suite type.
  802. if (p_suite->get_datatype().is_set() && (p_suite->get_datatype() != p_statement->get_datatype())) {
  803. // Mixed types.
  804. // TODO: This could use the common supertype instead.
  805. p_suite->datatype.kind = GDScriptParser::DataType::VARIANT;
  806. p_suite->datatype.type_source = GDScriptParser::DataType::UNDETECTED;
  807. } else {
  808. p_suite->set_datatype(p_statement->get_datatype());
  809. }
  810. break;
  811. default:
  812. break;
  813. }
  814. }
  815. void GDScriptAnalyzer::resolve_suite(GDScriptParser::SuiteNode *p_suite) {
  816. for (int i = 0; i < p_suite->statements.size(); i++) {
  817. GDScriptParser::Node *stmt = p_suite->statements[i];
  818. resolve_node(stmt);
  819. decide_suite_type(p_suite, stmt);
  820. }
  821. }
  822. void GDScriptAnalyzer::resolve_if(GDScriptParser::IfNode *p_if) {
  823. reduce_expression(p_if->condition);
  824. resolve_suite(p_if->true_block);
  825. p_if->set_datatype(p_if->true_block->get_datatype());
  826. if (p_if->false_block != nullptr) {
  827. resolve_suite(p_if->false_block);
  828. decide_suite_type(p_if, p_if->false_block);
  829. }
  830. }
  831. void GDScriptAnalyzer::resolve_for(GDScriptParser::ForNode *p_for) {
  832. bool list_resolved = false;
  833. // Optimize constant range() call to not allocate an array.
  834. // Use int, Vector2, Vector3 instead, which also can be used as range iterators.
  835. if (p_for->list && p_for->list->type == GDScriptParser::Node::CALL) {
  836. GDScriptParser::CallNode *call = static_cast<GDScriptParser::CallNode *>(p_for->list);
  837. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  838. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  839. GDScriptParser::IdentifierNode *callee = static_cast<GDScriptParser::IdentifierNode *>(call->callee);
  840. if (callee->name == "range") {
  841. list_resolved = true;
  842. if (call->arguments.size() < 1) {
  843. push_error(R"*(Invalid call for "range()" function. Expected at least 1 argument, none given.)*", call->callee);
  844. } else if (call->arguments.size() > 3) {
  845. push_error(vformat(R"*(Invalid call for "range()" function. Expected at most 3 arguments, %d given.)*", call->arguments.size()), call->callee);
  846. } else {
  847. // Now we can optimize it.
  848. bool all_is_constant = true;
  849. Vector<Variant> args;
  850. args.resize(call->arguments.size());
  851. for (int i = 0; i < call->arguments.size(); i++) {
  852. reduce_expression(call->arguments[i]);
  853. if (!call->arguments[i]->is_constant) {
  854. all_is_constant = false;
  855. } else {
  856. args.write[i] = call->arguments[i]->reduced_value;
  857. }
  858. GDScriptParser::DataType arg_type = call->arguments[i]->get_datatype();
  859. if (arg_type.kind != GDScriptParser::DataType::BUILTIN) {
  860. all_is_constant = false;
  861. 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]);
  862. } else if (arg_type.builtin_type != Variant::INT && arg_type.builtin_type != Variant::FLOAT) {
  863. all_is_constant = false;
  864. 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]);
  865. }
  866. }
  867. Variant reduced;
  868. if (all_is_constant) {
  869. switch (args.size()) {
  870. case 1:
  871. reduced = args[0];
  872. break;
  873. case 2:
  874. reduced = Vector2i(args[0], args[1]);
  875. break;
  876. case 3:
  877. reduced = Vector3i(args[0], args[1], args[2]);
  878. break;
  879. }
  880. p_for->list->is_constant = true;
  881. p_for->list->reduced_value = reduced;
  882. }
  883. }
  884. GDScriptParser::DataType list_type;
  885. list_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  886. list_type.kind = GDScriptParser::DataType::BUILTIN;
  887. list_type.builtin_type = Variant::ARRAY;
  888. p_for->list->set_datatype(list_type);
  889. }
  890. }
  891. }
  892. if (!list_resolved) {
  893. resolve_node(p_for->list);
  894. }
  895. // TODO: If list is a typed array, the variable should be an element.
  896. // Also applicable for constant range() (so variable is int or float).
  897. resolve_suite(p_for->loop);
  898. p_for->set_datatype(p_for->loop->get_datatype());
  899. #ifdef DEBUG_ENABLED
  900. if (p_for->variable) {
  901. is_shadowing(p_for->variable, R"("for" iterator variable)");
  902. }
  903. #endif
  904. }
  905. void GDScriptAnalyzer::resolve_while(GDScriptParser::WhileNode *p_while) {
  906. resolve_node(p_while->condition);
  907. resolve_suite(p_while->loop);
  908. p_while->set_datatype(p_while->loop->get_datatype());
  909. }
  910. void GDScriptAnalyzer::resolve_variable(GDScriptParser::VariableNode *p_variable) {
  911. GDScriptParser::DataType type;
  912. type.kind = GDScriptParser::DataType::VARIANT; // By default.
  913. if (p_variable->initializer != nullptr) {
  914. reduce_expression(p_variable->initializer);
  915. type = p_variable->initializer->get_datatype();
  916. if (p_variable->infer_datatype) {
  917. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  918. if (type.has_no_type()) {
  919. 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);
  920. } else if (type.is_variant()) {
  921. 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);
  922. } else if (type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  923. 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);
  924. }
  925. } else {
  926. type.type_source = GDScriptParser::DataType::INFERRED;
  927. }
  928. #ifdef DEBUG_ENABLED
  929. if (p_variable->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  930. parser->push_warning(p_variable->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_variable->initializer)->function_name);
  931. }
  932. #endif
  933. }
  934. if (p_variable->datatype_specifier != nullptr) {
  935. type = resolve_datatype(p_variable->datatype_specifier);
  936. type.is_meta_type = false;
  937. if (p_variable->initializer != nullptr) {
  938. if (!is_type_compatible(type, p_variable->initializer->get_datatype(), true)) {
  939. // Try reverse test since it can be a masked subtype.
  940. if (!is_type_compatible(p_variable->initializer->get_datatype(), type, true)) {
  941. 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);
  942. } else {
  943. // TODO: Add warning.
  944. mark_node_unsafe(p_variable->initializer);
  945. }
  946. #ifdef DEBUG_ENABLED
  947. } else if (type.builtin_type == Variant::INT && p_variable->initializer->get_datatype().builtin_type == Variant::FLOAT) {
  948. parser->push_warning(p_variable->initializer, GDScriptWarning::NARROWING_CONVERSION);
  949. #endif
  950. }
  951. if (p_variable->initializer->get_datatype().is_variant()) {
  952. // TODO: Warn unsafe assign.
  953. mark_node_unsafe(p_variable->initializer);
  954. }
  955. }
  956. } else if (p_variable->infer_datatype) {
  957. if (type.has_no_type()) {
  958. 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);
  959. }
  960. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  961. }
  962. type.is_constant = false;
  963. p_variable->set_datatype(type);
  964. #ifdef DEBUG_ENABLED
  965. if (p_variable->usages == 0 && !String(p_variable->identifier->name).begins_with("_")) {
  966. parser->push_warning(p_variable, GDScriptWarning::UNUSED_VARIABLE, p_variable->identifier->name);
  967. } else if (p_variable->assignments == 0) {
  968. parser->push_warning(p_variable, GDScriptWarning::UNASSIGNED_VARIABLE, p_variable->identifier->name);
  969. }
  970. is_shadowing(p_variable->identifier, "variable");
  971. #endif
  972. }
  973. void GDScriptAnalyzer::resolve_constant(GDScriptParser::ConstantNode *p_constant) {
  974. GDScriptParser::DataType type;
  975. reduce_expression(p_constant->initializer);
  976. if (!p_constant->initializer->is_constant) {
  977. push_error(vformat(R"(Assigned value for constant "%s" isn't a constant expression.)", p_constant->identifier->name), p_constant->initializer);
  978. }
  979. type = p_constant->initializer->get_datatype();
  980. #ifdef DEBUG_ENABLED
  981. if (p_constant->initializer->type == GDScriptParser::Node::CALL && type.kind == GDScriptParser::DataType::BUILTIN && type.builtin_type == Variant::NIL) {
  982. parser->push_warning(p_constant->initializer, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_constant->initializer)->function_name);
  983. }
  984. #endif
  985. if (p_constant->datatype_specifier != nullptr) {
  986. GDScriptParser::DataType explicit_type = resolve_datatype(p_constant->datatype_specifier);
  987. explicit_type.is_meta_type = false;
  988. if (!is_type_compatible(explicit_type, type)) {
  989. 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);
  990. #ifdef DEBUG_ENABLED
  991. } else if (explicit_type.builtin_type == Variant::INT && type.builtin_type == Variant::FLOAT) {
  992. parser->push_warning(p_constant->initializer, GDScriptWarning::NARROWING_CONVERSION);
  993. #endif
  994. }
  995. type = explicit_type;
  996. } else if (p_constant->infer_datatype) {
  997. if (type.has_no_type()) {
  998. 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);
  999. }
  1000. type.type_source = GDScriptParser::DataType::ANNOTATED_INFERRED;
  1001. }
  1002. type.is_constant = true;
  1003. p_constant->set_datatype(type);
  1004. #ifdef DEBUG_ENABLED
  1005. if (p_constant->usages == 0) {
  1006. parser->push_warning(p_constant, GDScriptWarning::UNUSED_LOCAL_CONSTANT, p_constant->identifier->name);
  1007. }
  1008. is_shadowing(p_constant->identifier, "constant");
  1009. #endif
  1010. }
  1011. void GDScriptAnalyzer::resolve_assert(GDScriptParser::AssertNode *p_assert) {
  1012. reduce_expression(p_assert->condition);
  1013. if (p_assert->message != nullptr) {
  1014. reduce_literal(p_assert->message);
  1015. }
  1016. p_assert->set_datatype(p_assert->condition->get_datatype());
  1017. #ifdef DEBUG_ENABLED
  1018. if (p_assert->condition->is_constant) {
  1019. if (p_assert->condition->reduced_value.booleanize()) {
  1020. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_TRUE);
  1021. } else {
  1022. parser->push_warning(p_assert->condition, GDScriptWarning::ASSERT_ALWAYS_FALSE);
  1023. }
  1024. }
  1025. #endif
  1026. }
  1027. void GDScriptAnalyzer::resolve_match(GDScriptParser::MatchNode *p_match) {
  1028. reduce_expression(p_match->test);
  1029. for (int i = 0; i < p_match->branches.size(); i++) {
  1030. resolve_match_branch(p_match->branches[i], p_match->test);
  1031. decide_suite_type(p_match, p_match->branches[i]);
  1032. }
  1033. }
  1034. void GDScriptAnalyzer::resolve_match_branch(GDScriptParser::MatchBranchNode *p_match_branch, GDScriptParser::ExpressionNode *p_match_test) {
  1035. for (int i = 0; i < p_match_branch->patterns.size(); i++) {
  1036. resolve_match_pattern(p_match_branch->patterns[i], p_match_test);
  1037. }
  1038. resolve_suite(p_match_branch->block);
  1039. decide_suite_type(p_match_branch, p_match_branch->block);
  1040. }
  1041. void GDScriptAnalyzer::resolve_match_pattern(GDScriptParser::PatternNode *p_match_pattern, GDScriptParser::ExpressionNode *p_match_test) {
  1042. if (p_match_pattern == nullptr) {
  1043. return;
  1044. }
  1045. GDScriptParser::DataType result;
  1046. switch (p_match_pattern->pattern_type) {
  1047. case GDScriptParser::PatternNode::PT_LITERAL:
  1048. if (p_match_pattern->literal) {
  1049. reduce_literal(p_match_pattern->literal);
  1050. result = p_match_pattern->literal->get_datatype();
  1051. }
  1052. break;
  1053. case GDScriptParser::PatternNode::PT_EXPRESSION:
  1054. if (p_match_pattern->expression) {
  1055. reduce_expression(p_match_pattern->expression);
  1056. if (!p_match_pattern->expression->is_constant) {
  1057. push_error(R"(Expression in match pattern must be a constant.)", p_match_pattern->expression);
  1058. }
  1059. result = p_match_pattern->expression->get_datatype();
  1060. }
  1061. break;
  1062. case GDScriptParser::PatternNode::PT_BIND:
  1063. if (p_match_test != nullptr) {
  1064. result = p_match_test->get_datatype();
  1065. } else {
  1066. result.kind = GDScriptParser::DataType::VARIANT;
  1067. }
  1068. p_match_pattern->bind->set_datatype(result);
  1069. #ifdef DEBUG_ENABLED
  1070. is_shadowing(p_match_pattern->bind, "pattern bind");
  1071. if (p_match_pattern->bind->usages == 0) {
  1072. parser->push_warning(p_match_pattern->bind, GDScriptWarning::UNASSIGNED_VARIABLE, p_match_pattern->bind->name);
  1073. }
  1074. #endif
  1075. break;
  1076. case GDScriptParser::PatternNode::PT_ARRAY:
  1077. for (int i = 0; i < p_match_pattern->array.size(); i++) {
  1078. resolve_match_pattern(p_match_pattern->array[i], nullptr);
  1079. decide_suite_type(p_match_pattern, p_match_pattern->array[i]);
  1080. }
  1081. result = p_match_pattern->get_datatype();
  1082. break;
  1083. case GDScriptParser::PatternNode::PT_DICTIONARY:
  1084. for (int i = 0; i < p_match_pattern->dictionary.size(); i++) {
  1085. if (p_match_pattern->dictionary[i].key) {
  1086. reduce_expression(p_match_pattern->dictionary[i].key);
  1087. if (!p_match_pattern->dictionary[i].key->is_constant) {
  1088. push_error(R"(Expression in dictionary pattern key must be a constant.)", p_match_pattern->expression);
  1089. }
  1090. }
  1091. if (p_match_pattern->dictionary[i].value_pattern) {
  1092. resolve_match_pattern(p_match_pattern->dictionary[i].value_pattern, nullptr);
  1093. decide_suite_type(p_match_pattern, p_match_pattern->dictionary[i].value_pattern);
  1094. }
  1095. }
  1096. result = p_match_pattern->get_datatype();
  1097. break;
  1098. case GDScriptParser::PatternNode::PT_WILDCARD:
  1099. case GDScriptParser::PatternNode::PT_REST:
  1100. result.kind = GDScriptParser::DataType::VARIANT;
  1101. break;
  1102. }
  1103. p_match_pattern->set_datatype(result);
  1104. }
  1105. void GDScriptAnalyzer::resolve_pararameter(GDScriptParser::ParameterNode *p_parameter) {
  1106. GDScriptParser::DataType result;
  1107. result.kind = GDScriptParser::DataType::VARIANT;
  1108. if (p_parameter->default_value != nullptr) {
  1109. reduce_expression(p_parameter->default_value);
  1110. result = p_parameter->default_value->get_datatype();
  1111. result.type_source = GDScriptParser::DataType::INFERRED;
  1112. }
  1113. if (p_parameter->datatype_specifier != nullptr) {
  1114. resolve_datatype(p_parameter->datatype_specifier);
  1115. result = p_parameter->datatype_specifier->get_datatype();
  1116. result.is_meta_type = false;
  1117. if (p_parameter->default_value != nullptr) {
  1118. if (!is_type_compatible(result, p_parameter->default_value->get_datatype())) {
  1119. push_error(vformat(R"(Type of default value for parameter "%s" (%s) is not compatible with paremeter 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);
  1120. } else if (p_parameter->default_value->get_datatype().is_variant()) {
  1121. mark_node_unsafe(p_parameter);
  1122. }
  1123. }
  1124. }
  1125. p_parameter->set_datatype(result);
  1126. }
  1127. void GDScriptAnalyzer::resolve_return(GDScriptParser::ReturnNode *p_return) {
  1128. GDScriptParser::DataType result;
  1129. if (p_return->return_value != nullptr) {
  1130. reduce_expression(p_return->return_value);
  1131. result = p_return->return_value->get_datatype();
  1132. } else {
  1133. // Return type is null by default.
  1134. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1135. result.kind = GDScriptParser::DataType::BUILTIN;
  1136. result.builtin_type = Variant::NIL;
  1137. result.is_constant = true;
  1138. }
  1139. GDScriptParser::DataType function_type = parser->current_function->get_datatype();
  1140. function_type.is_meta_type = false;
  1141. if (function_type.is_hard_type()) {
  1142. if (!is_type_compatible(function_type, result)) {
  1143. // Try other way. Okay but not safe.
  1144. if (!is_type_compatible(result, function_type)) {
  1145. 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);
  1146. } else {
  1147. // TODO: Add warning.
  1148. mark_node_unsafe(p_return);
  1149. }
  1150. #ifdef DEBUG_ENABLED
  1151. } else if (function_type.builtin_type == Variant::INT && result.builtin_type == Variant::FLOAT) {
  1152. parser->push_warning(p_return, GDScriptWarning::NARROWING_CONVERSION);
  1153. } else if (result.is_variant()) {
  1154. mark_node_unsafe(p_return);
  1155. #endif
  1156. }
  1157. }
  1158. p_return->set_datatype(result);
  1159. }
  1160. void GDScriptAnalyzer::reduce_expression(GDScriptParser::ExpressionNode *p_expression) {
  1161. // This one makes some magic happen.
  1162. if (p_expression == nullptr) {
  1163. return;
  1164. }
  1165. if (p_expression->reduced) {
  1166. // Don't do this more than once.
  1167. return;
  1168. }
  1169. p_expression->reduced = true;
  1170. switch (p_expression->type) {
  1171. case GDScriptParser::Node::ARRAY:
  1172. reduce_array(static_cast<GDScriptParser::ArrayNode *>(p_expression));
  1173. break;
  1174. case GDScriptParser::Node::ASSIGNMENT:
  1175. reduce_assignment(static_cast<GDScriptParser::AssignmentNode *>(p_expression));
  1176. break;
  1177. case GDScriptParser::Node::AWAIT:
  1178. reduce_await(static_cast<GDScriptParser::AwaitNode *>(p_expression));
  1179. break;
  1180. case GDScriptParser::Node::BINARY_OPERATOR:
  1181. reduce_binary_op(static_cast<GDScriptParser::BinaryOpNode *>(p_expression));
  1182. break;
  1183. case GDScriptParser::Node::CALL:
  1184. reduce_call(static_cast<GDScriptParser::CallNode *>(p_expression));
  1185. break;
  1186. case GDScriptParser::Node::CAST:
  1187. reduce_cast(static_cast<GDScriptParser::CastNode *>(p_expression));
  1188. break;
  1189. case GDScriptParser::Node::DICTIONARY:
  1190. reduce_dictionary(static_cast<GDScriptParser::DictionaryNode *>(p_expression));
  1191. break;
  1192. case GDScriptParser::Node::GET_NODE:
  1193. reduce_get_node(static_cast<GDScriptParser::GetNodeNode *>(p_expression));
  1194. break;
  1195. case GDScriptParser::Node::IDENTIFIER:
  1196. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_expression));
  1197. break;
  1198. case GDScriptParser::Node::LITERAL:
  1199. reduce_literal(static_cast<GDScriptParser::LiteralNode *>(p_expression));
  1200. break;
  1201. case GDScriptParser::Node::PRELOAD:
  1202. reduce_preload(static_cast<GDScriptParser::PreloadNode *>(p_expression));
  1203. break;
  1204. case GDScriptParser::Node::SELF:
  1205. reduce_self(static_cast<GDScriptParser::SelfNode *>(p_expression));
  1206. break;
  1207. case GDScriptParser::Node::SUBSCRIPT:
  1208. reduce_subscript(static_cast<GDScriptParser::SubscriptNode *>(p_expression));
  1209. break;
  1210. case GDScriptParser::Node::TERNARY_OPERATOR:
  1211. reduce_ternary_op(static_cast<GDScriptParser::TernaryOpNode *>(p_expression));
  1212. break;
  1213. case GDScriptParser::Node::UNARY_OPERATOR:
  1214. reduce_unary_op(static_cast<GDScriptParser::UnaryOpNode *>(p_expression));
  1215. break;
  1216. // Non-expressions. Here only to make sure new nodes aren't forgotten.
  1217. case GDScriptParser::Node::NONE:
  1218. case GDScriptParser::Node::ANNOTATION:
  1219. case GDScriptParser::Node::ASSERT:
  1220. case GDScriptParser::Node::BREAK:
  1221. case GDScriptParser::Node::BREAKPOINT:
  1222. case GDScriptParser::Node::CLASS:
  1223. case GDScriptParser::Node::CONSTANT:
  1224. case GDScriptParser::Node::CONTINUE:
  1225. case GDScriptParser::Node::ENUM:
  1226. case GDScriptParser::Node::FOR:
  1227. case GDScriptParser::Node::FUNCTION:
  1228. case GDScriptParser::Node::IF:
  1229. case GDScriptParser::Node::MATCH:
  1230. case GDScriptParser::Node::MATCH_BRANCH:
  1231. case GDScriptParser::Node::PARAMETER:
  1232. case GDScriptParser::Node::PASS:
  1233. case GDScriptParser::Node::PATTERN:
  1234. case GDScriptParser::Node::RETURN:
  1235. case GDScriptParser::Node::SIGNAL:
  1236. case GDScriptParser::Node::SUITE:
  1237. case GDScriptParser::Node::TYPE:
  1238. case GDScriptParser::Node::VARIABLE:
  1239. case GDScriptParser::Node::WHILE:
  1240. ERR_FAIL_MSG("Reaching unreachable case");
  1241. }
  1242. }
  1243. void GDScriptAnalyzer::reduce_array(GDScriptParser::ArrayNode *p_array) {
  1244. bool all_is_constant = true;
  1245. for (int i = 0; i < p_array->elements.size(); i++) {
  1246. GDScriptParser::ExpressionNode *element = p_array->elements[i];
  1247. reduce_expression(element);
  1248. all_is_constant = all_is_constant && element->is_constant;
  1249. }
  1250. if (all_is_constant) {
  1251. Array array;
  1252. array.resize(p_array->elements.size());
  1253. for (int i = 0; i < p_array->elements.size(); i++) {
  1254. array[i] = p_array->elements[i]->reduced_value;
  1255. }
  1256. p_array->is_constant = true;
  1257. p_array->reduced_value = array;
  1258. }
  1259. // It's array in any case.
  1260. GDScriptParser::DataType arr_type;
  1261. arr_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1262. arr_type.kind = GDScriptParser::DataType::BUILTIN;
  1263. arr_type.builtin_type = Variant::ARRAY;
  1264. arr_type.is_constant = true;
  1265. p_array->set_datatype(arr_type);
  1266. }
  1267. void GDScriptAnalyzer::reduce_assignment(GDScriptParser::AssignmentNode *p_assignment) {
  1268. reduce_expression(p_assignment->assignee);
  1269. reduce_expression(p_assignment->assigned_value);
  1270. if (p_assignment->assigned_value == nullptr || p_assignment->assignee == nullptr) {
  1271. return;
  1272. }
  1273. if (p_assignment->assignee->get_datatype().is_constant) {
  1274. push_error("Cannot assign a new value to a constant.", p_assignment->assignee);
  1275. }
  1276. if (!p_assignment->assignee->get_datatype().is_variant() && !p_assignment->assigned_value->get_datatype().is_variant()) {
  1277. bool compatible = true;
  1278. GDScriptParser::DataType op_type = p_assignment->assigned_value->get_datatype();
  1279. if (p_assignment->operation != GDScriptParser::AssignmentNode::OP_NONE) {
  1280. op_type = get_operation_type(p_assignment->variant_op, p_assignment->assignee->get_datatype(), p_assignment->assigned_value->get_datatype(), compatible);
  1281. }
  1282. if (compatible) {
  1283. compatible = is_type_compatible(p_assignment->assignee->get_datatype(), op_type, true);
  1284. if (!compatible) {
  1285. if (p_assignment->assignee->get_datatype().is_hard_type()) {
  1286. // Try reverse test since it can be a masked subtype.
  1287. if (!is_type_compatible(op_type, p_assignment->assignee->get_datatype(), true)) {
  1288. push_error(vformat(R"(Cannot assign a value of type "%s" to a target of type "%s".)", p_assignment->assigned_value->get_datatype().to_string(), p_assignment->assignee->get_datatype().to_string()), p_assignment->assigned_value);
  1289. } else {
  1290. // TODO: Add warning.
  1291. mark_node_unsafe(p_assignment);
  1292. }
  1293. } else {
  1294. // TODO: Warning in this case.
  1295. mark_node_unsafe(p_assignment);
  1296. }
  1297. }
  1298. } else {
  1299. push_error(vformat(R"(Invalid operands "%s" and "%s" for assignment operator.)", p_assignment->assignee->get_datatype().to_string(), p_assignment->assigned_value->get_datatype().to_string()), p_assignment);
  1300. }
  1301. }
  1302. if (p_assignment->assignee->get_datatype().has_no_type() || p_assignment->assigned_value->get_datatype().is_variant()) {
  1303. mark_node_unsafe(p_assignment);
  1304. }
  1305. if (p_assignment->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1306. // Change source type so it's not wrongly detected later.
  1307. GDScriptParser::IdentifierNode *identifier = static_cast<GDScriptParser::IdentifierNode *>(p_assignment->assignee);
  1308. switch (identifier->source) {
  1309. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE: {
  1310. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1311. if (!id_type.is_hard_type()) {
  1312. id_type.kind = GDScriptParser::DataType::VARIANT;
  1313. id_type.type_source = GDScriptParser::DataType::UNDETECTED;
  1314. identifier->variable_source->set_datatype(id_type);
  1315. }
  1316. } break;
  1317. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE: {
  1318. GDScriptParser::DataType id_type = identifier->variable_source->get_datatype();
  1319. if (!id_type.is_hard_type()) {
  1320. id_type = p_assignment->assigned_value->get_datatype();
  1321. id_type.type_source = GDScriptParser::DataType::INFERRED;
  1322. id_type.is_constant = false;
  1323. identifier->variable_source->set_datatype(id_type);
  1324. }
  1325. } break;
  1326. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR: {
  1327. GDScriptParser::DataType id_type = identifier->bind_source->get_datatype();
  1328. if (!id_type.is_hard_type()) {
  1329. id_type = p_assignment->assigned_value->get_datatype();
  1330. id_type.type_source = GDScriptParser::DataType::INFERRED;
  1331. id_type.is_constant = false;
  1332. identifier->variable_source->set_datatype(id_type);
  1333. }
  1334. } break;
  1335. default:
  1336. // Nothing to do.
  1337. break;
  1338. }
  1339. }
  1340. GDScriptParser::DataType assignee_type = p_assignment->assignee->get_datatype();
  1341. GDScriptParser::DataType assigned_type = p_assignment->assigned_value->get_datatype();
  1342. #ifdef DEBUG_ENABLED
  1343. if (p_assignment->assigned_value->type == GDScriptParser::Node::CALL && assigned_type.kind == GDScriptParser::DataType::BUILTIN && assigned_type.builtin_type == Variant::NIL) {
  1344. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::VOID_ASSIGNMENT, static_cast<GDScriptParser::CallNode *>(p_assignment->assigned_value)->function_name);
  1345. } else if (assignee_type.is_hard_type() && assignee_type.builtin_type == Variant::INT && assigned_type.builtin_type == Variant::FLOAT) {
  1346. parser->push_warning(p_assignment->assigned_value, GDScriptWarning::NARROWING_CONVERSION);
  1347. }
  1348. #endif
  1349. }
  1350. void GDScriptAnalyzer::reduce_await(GDScriptParser::AwaitNode *p_await) {
  1351. if (p_await->to_await == nullptr) {
  1352. GDScriptParser::DataType await_type;
  1353. await_type.kind = GDScriptParser::DataType::VARIANT;
  1354. p_await->set_datatype(await_type);
  1355. return;
  1356. }
  1357. if (p_await->to_await->type == GDScriptParser::Node::CALL) {
  1358. reduce_call(static_cast<GDScriptParser::CallNode *>(p_await->to_await), true);
  1359. } else {
  1360. reduce_expression(p_await->to_await);
  1361. }
  1362. p_await->is_constant = p_await->to_await->is_constant;
  1363. p_await->reduced_value = p_await->to_await->reduced_value;
  1364. GDScriptParser::DataType awaiting_type = p_await->to_await->get_datatype();
  1365. p_await->set_datatype(awaiting_type);
  1366. #ifdef DEBUG_ENABLED
  1367. if (!awaiting_type.is_coroutine && awaiting_type.builtin_type != Variant::SIGNAL) {
  1368. parser->push_warning(p_await, GDScriptWarning::REDUNDANT_AWAIT);
  1369. }
  1370. #endif
  1371. }
  1372. void GDScriptAnalyzer::reduce_binary_op(GDScriptParser::BinaryOpNode *p_binary_op) {
  1373. reduce_expression(p_binary_op->left_operand);
  1374. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST && p_binary_op->right_operand && p_binary_op->right_operand->type == GDScriptParser::Node::IDENTIFIER) {
  1375. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_binary_op->right_operand), true);
  1376. } else {
  1377. reduce_expression(p_binary_op->right_operand);
  1378. }
  1379. // TODO: Right operand must be a valid type with the `is` operator. Need to check here.
  1380. GDScriptParser::DataType left_type;
  1381. if (p_binary_op->left_operand) {
  1382. left_type = p_binary_op->left_operand->get_datatype();
  1383. }
  1384. GDScriptParser::DataType right_type;
  1385. if (p_binary_op->right_operand) {
  1386. right_type = p_binary_op->right_operand->get_datatype();
  1387. }
  1388. if (!left_type.is_set() || !right_type.is_set()) {
  1389. return;
  1390. }
  1391. #ifdef DEBUG_ENABLED
  1392. if (p_binary_op->variant_op == Variant::OP_DIVIDE && left_type.builtin_type == Variant::INT && right_type.builtin_type == Variant::INT) {
  1393. parser->push_warning(p_binary_op, GDScriptWarning::INTEGER_DIVISION);
  1394. }
  1395. #endif
  1396. if (p_binary_op->left_operand->is_constant && p_binary_op->right_operand->is_constant) {
  1397. p_binary_op->is_constant = true;
  1398. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1399. bool valid = false;
  1400. 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);
  1401. if (!valid) {
  1402. if (p_binary_op->reduced_value.get_type() == Variant::STRING) {
  1403. 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);
  1404. } else {
  1405. push_error(vformat(R"(Invalid operands to operator %s, %s and %s.".)",
  1406. Variant::get_operator_name(p_binary_op->variant_op),
  1407. Variant::get_type_name(p_binary_op->left_operand->reduced_value.get_type()),
  1408. Variant::get_type_name(p_binary_op->right_operand->reduced_value.get_type())),
  1409. p_binary_op);
  1410. }
  1411. }
  1412. } else {
  1413. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1414. GDScriptParser::DataType test_type = right_type;
  1415. test_type.is_meta_type = false;
  1416. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1417. push_error(vformat(R"(Expression is of type "%s" so it can't be of type "%s".)"), p_binary_op->left_operand);
  1418. p_binary_op->reduced_value = false;
  1419. } else {
  1420. p_binary_op->reduced_value = true;
  1421. }
  1422. } else {
  1423. ERR_PRINT("Parser bug: unknown binary operation.");
  1424. }
  1425. }
  1426. p_binary_op->set_datatype(type_from_variant(p_binary_op->reduced_value));
  1427. return;
  1428. }
  1429. GDScriptParser::DataType result;
  1430. if (left_type.is_variant() || right_type.is_variant()) {
  1431. // Cannot infer type because one operand can be anything.
  1432. result.kind = GDScriptParser::DataType::VARIANT;
  1433. mark_node_unsafe(p_binary_op);
  1434. } else {
  1435. if (p_binary_op->variant_op < Variant::OP_MAX) {
  1436. bool valid = false;
  1437. result = get_operation_type(p_binary_op->variant_op, p_binary_op->left_operand->get_datatype(), right_type, valid);
  1438. if (!valid) {
  1439. push_error(vformat(R"(Invalid operands "%s" and "%s" for "%s" operator.)", p_binary_op->left_operand->get_datatype().to_string(), right_type.to_string(), Variant::get_operator_name(p_binary_op->variant_op)), p_binary_op);
  1440. }
  1441. } else {
  1442. if (p_binary_op->operation == GDScriptParser::BinaryOpNode::OP_TYPE_TEST) {
  1443. GDScriptParser::DataType test_type = right_type;
  1444. test_type.is_meta_type = false;
  1445. if (!is_type_compatible(test_type, p_binary_op->left_operand->get_datatype(), false)) {
  1446. // Test reverse as well to consider for subtypes.
  1447. if (!is_type_compatible(p_binary_op->left_operand->get_datatype(), test_type, false)) {
  1448. if (p_binary_op->left_operand->get_datatype().is_hard_type()) {
  1449. 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);
  1450. } else {
  1451. // TODO: Warning.
  1452. mark_node_unsafe(p_binary_op);
  1453. }
  1454. }
  1455. }
  1456. // "is" operator is always a boolean anyway.
  1457. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1458. result.kind = GDScriptParser::DataType::BUILTIN;
  1459. result.builtin_type = Variant::BOOL;
  1460. } else {
  1461. ERR_PRINT("Parser bug: unknown binary operation.");
  1462. }
  1463. }
  1464. }
  1465. p_binary_op->set_datatype(result);
  1466. }
  1467. void GDScriptAnalyzer::reduce_call(GDScriptParser::CallNode *p_call, bool is_await) {
  1468. bool all_is_constant = true;
  1469. for (int i = 0; i < p_call->arguments.size(); i++) {
  1470. reduce_expression(p_call->arguments[i]);
  1471. all_is_constant = all_is_constant && p_call->arguments[i]->is_constant;
  1472. }
  1473. GDScriptParser::Node::Type callee_type = p_call->get_callee_type();
  1474. GDScriptParser::DataType call_type;
  1475. if (!p_call->is_super && callee_type == GDScriptParser::Node::IDENTIFIER) {
  1476. // Call to name directly.
  1477. StringName function_name = p_call->function_name;
  1478. Variant::Type builtin_type = GDScriptParser::get_builtin_type(function_name);
  1479. GDScriptFunctions::Function builtin_function = GDScriptParser::get_builtin_function(function_name);
  1480. if (builtin_type < Variant::VARIANT_MAX) {
  1481. // Is a builtin constructor.
  1482. call_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1483. call_type.kind = GDScriptParser::DataType::BUILTIN;
  1484. call_type.builtin_type = builtin_type;
  1485. if (builtin_type == Variant::OBJECT) {
  1486. call_type.kind = GDScriptParser::DataType::NATIVE;
  1487. call_type.native_type = function_name; // "Object".
  1488. }
  1489. if (all_is_constant) {
  1490. // Construct here.
  1491. Vector<const Variant *> args;
  1492. for (int i = 0; i < p_call->arguments.size(); i++) {
  1493. args.push_back(&(p_call->arguments[i]->reduced_value));
  1494. }
  1495. Callable::CallError err;
  1496. Variant value = Variant::construct(builtin_type, (const Variant **)args.ptr(), args.size(), err);
  1497. switch (err.error) {
  1498. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT:
  1499. 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,
  1500. Variant::get_type_name(Variant::Type(err.expected)), p_call->arguments[err.argument]->get_datatype().to_string()),
  1501. p_call->arguments[err.argument]);
  1502. break;
  1503. case Callable::CallError::CALL_ERROR_INVALID_METHOD: {
  1504. String signature = Variant::get_type_name(builtin_type) + "(";
  1505. for (int i = 0; i < p_call->arguments.size(); i++) {
  1506. if (i > 0) {
  1507. signature += ", ";
  1508. }
  1509. signature += p_call->arguments[i]->get_datatype().to_string();
  1510. }
  1511. signature += ")";
  1512. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call->callee);
  1513. } break;
  1514. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1515. 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);
  1516. break;
  1517. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1518. 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);
  1519. break;
  1520. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1521. break; // Can't happen in a builtin constructor.
  1522. case Callable::CallError::CALL_OK:
  1523. p_call->is_constant = true;
  1524. p_call->reduced_value = value;
  1525. break;
  1526. }
  1527. } else {
  1528. // TODO: Check constructors without constants.
  1529. // If there's one argument, try to use copy constructor (those aren't explicitly defined).
  1530. if (p_call->arguments.size() == 1) {
  1531. GDScriptParser::DataType arg_type = p_call->arguments[0]->get_datatype();
  1532. if (arg_type.is_variant()) {
  1533. mark_node_unsafe(p_call->arguments[0]);
  1534. } else {
  1535. if (arg_type.kind == GDScriptParser::DataType::BUILTIN && arg_type.builtin_type == builtin_type) {
  1536. // Okay.
  1537. p_call->set_datatype(call_type);
  1538. return;
  1539. }
  1540. }
  1541. }
  1542. List<MethodInfo> constructors;
  1543. Variant::get_constructor_list(builtin_type, &constructors);
  1544. bool match = false;
  1545. for (const List<MethodInfo>::Element *E = constructors.front(); E != nullptr; E = E->next()) {
  1546. const MethodInfo &info = E->get();
  1547. if (p_call->arguments.size() < info.arguments.size() - info.default_arguments.size()) {
  1548. continue;
  1549. }
  1550. if (p_call->arguments.size() > info.arguments.size()) {
  1551. continue;
  1552. }
  1553. bool types_match = true;
  1554. for (int i = 0; i < p_call->arguments.size(); i++) {
  1555. GDScriptParser::DataType par_type = type_from_property(info.arguments[i]);
  1556. if (!is_type_compatible(par_type, p_call->arguments[i]->get_datatype(), true)) {
  1557. types_match = false;
  1558. break;
  1559. #ifdef DEBUG_ENABLED
  1560. } else {
  1561. if (par_type.builtin_type == Variant::INT && p_call->arguments[i]->get_datatype().builtin_type == Variant::FLOAT) {
  1562. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  1563. }
  1564. #endif
  1565. }
  1566. }
  1567. if (types_match) {
  1568. match = true;
  1569. call_type = type_from_property(info.return_val);
  1570. break;
  1571. }
  1572. }
  1573. if (!match) {
  1574. String signature = Variant::get_type_name(builtin_type) + "(";
  1575. for (int i = 0; i < p_call->arguments.size(); i++) {
  1576. if (i > 0) {
  1577. signature += ", ";
  1578. }
  1579. signature += p_call->arguments[i]->get_datatype().to_string();
  1580. }
  1581. signature += ")";
  1582. push_error(vformat(R"(No constructor of "%s" matches the signature "%s".)", Variant::get_type_name(builtin_type), signature), p_call);
  1583. }
  1584. }
  1585. p_call->set_datatype(call_type);
  1586. return;
  1587. } else if (builtin_function < GDScriptFunctions::FUNC_MAX) {
  1588. MethodInfo function_info = GDScriptFunctions::get_info(builtin_function);
  1589. if (all_is_constant && GDScriptFunctions::is_deterministic(builtin_function)) {
  1590. // Can call on compilation.
  1591. Vector<const Variant *> args;
  1592. for (int i = 0; i < p_call->arguments.size(); i++) {
  1593. args.push_back(&(p_call->arguments[i]->reduced_value));
  1594. }
  1595. Variant value;
  1596. Callable::CallError err;
  1597. GDScriptFunctions::call(builtin_function, (const Variant **)args.ptr(), args.size(), value, err);
  1598. switch (err.error) {
  1599. case Callable::CallError::CALL_ERROR_INVALID_ARGUMENT: {
  1600. PropertyInfo wrong_arg = function_info.arguments[err.argument];
  1601. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*", GDScriptFunctions::get_func_name(builtin_function), err.argument + 1,
  1602. type_from_property(wrong_arg).to_string(), p_call->arguments[err.argument]->get_datatype().to_string()),
  1603. p_call->arguments[err.argument]);
  1604. } break;
  1605. case Callable::CallError::CALL_ERROR_INVALID_METHOD:
  1606. push_error(vformat(R"(Invalid call for function "%s".)", GDScriptFunctions::get_func_name(builtin_function)), p_call);
  1607. break;
  1608. case Callable::CallError::CALL_ERROR_TOO_MANY_ARGUMENTS:
  1609. push_error(vformat(R"*(Too many arguments for "%s()" call. Expected at most %d but received %d.)*", GDScriptFunctions::get_func_name(builtin_function), err.expected, p_call->arguments.size()), p_call);
  1610. break;
  1611. case Callable::CallError::CALL_ERROR_TOO_FEW_ARGUMENTS:
  1612. push_error(vformat(R"*(Too few arguments for "%s()" call. Expected at least %d but received %d.)*", GDScriptFunctions::get_func_name(builtin_function), err.expected, p_call->arguments.size()), p_call);
  1613. break;
  1614. case Callable::CallError::CALL_ERROR_INSTANCE_IS_NULL:
  1615. break; // Can't happen in a builtin constructor.
  1616. case Callable::CallError::CALL_OK:
  1617. p_call->is_constant = true;
  1618. p_call->reduced_value = value;
  1619. break;
  1620. }
  1621. } else {
  1622. validate_call_arg(function_info, p_call);
  1623. }
  1624. p_call->set_datatype(type_from_property(function_info.return_val));
  1625. return;
  1626. }
  1627. }
  1628. GDScriptParser::DataType base_type;
  1629. call_type.kind = GDScriptParser::DataType::VARIANT;
  1630. bool is_self = false;
  1631. if (p_call->is_super) {
  1632. base_type = parser->current_class->base_type;
  1633. is_self = true;
  1634. } else if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1635. base_type = parser->current_class->get_datatype();
  1636. is_self = true;
  1637. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  1638. GDScriptParser::SubscriptNode *subscript = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee);
  1639. if (!subscript->is_attribute) {
  1640. // Invalid call. Error already sent in parser.
  1641. // TODO: Could check if Callable here.
  1642. p_call->set_datatype(call_type);
  1643. mark_node_unsafe(p_call);
  1644. return;
  1645. }
  1646. reduce_expression(subscript->base);
  1647. base_type = subscript->base->get_datatype();
  1648. } else {
  1649. // Invalid call. Error already sent in parser.
  1650. // TODO: Could check if Callable here too.
  1651. p_call->set_datatype(call_type);
  1652. mark_node_unsafe(p_call);
  1653. return;
  1654. }
  1655. bool is_static = false;
  1656. bool is_vararg = false;
  1657. int default_arg_count = 0;
  1658. GDScriptParser::DataType return_type;
  1659. List<GDScriptParser::DataType> par_types;
  1660. if (get_function_signature(p_call, base_type, p_call->function_name, return_type, par_types, default_arg_count, is_static, is_vararg)) {
  1661. validate_call_arg(par_types, default_arg_count, is_vararg, p_call);
  1662. if (is_self && parser->current_function != nullptr && parser->current_function->is_static && !is_static) {
  1663. 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);
  1664. }
  1665. call_type = return_type;
  1666. } else {
  1667. // Check if the name exists as something else.
  1668. bool found = false;
  1669. if (!p_call->is_super && callee_type != GDScriptParser::Node::NONE) {
  1670. GDScriptParser::IdentifierNode *callee_id;
  1671. if (callee_type == GDScriptParser::Node::IDENTIFIER) {
  1672. callee_id = static_cast<GDScriptParser::IdentifierNode *>(p_call->callee);
  1673. } else {
  1674. // Can only be attribute.
  1675. callee_id = static_cast<GDScriptParser::SubscriptNode *>(p_call->callee)->attribute;
  1676. }
  1677. if (callee_id) {
  1678. reduce_identifier_from_base(callee_id, &base_type);
  1679. GDScriptParser::DataType callee_datatype = callee_id->get_datatype();
  1680. if (callee_datatype.is_set() && !callee_datatype.is_variant()) {
  1681. found = true;
  1682. if (callee_datatype.builtin_type == Variant::CALLABLE) {
  1683. 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);
  1684. } else {
  1685. 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);
  1686. }
  1687. #ifdef DEBUG_ENABLED
  1688. } else if (!is_self) {
  1689. parser->push_warning(p_call, GDScriptWarning::UNSAFE_METHOD_ACCESS, p_call->function_name, base_type.to_string());
  1690. mark_node_unsafe(p_call);
  1691. #endif
  1692. }
  1693. }
  1694. }
  1695. if (!found && is_self) {
  1696. String base_name = is_self && !p_call->is_super ? "self" : base_type.to_string();
  1697. 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);
  1698. }
  1699. }
  1700. if (call_type.is_coroutine && !is_await) {
  1701. push_error(vformat(R"*(Function "%s()" is a coroutine, so it must be called with "await".)*", p_call->function_name), p_call->callee);
  1702. }
  1703. p_call->set_datatype(call_type);
  1704. }
  1705. void GDScriptAnalyzer::reduce_cast(GDScriptParser::CastNode *p_cast) {
  1706. reduce_expression(p_cast->operand);
  1707. GDScriptParser::DataType cast_type = resolve_datatype(p_cast->cast_type);
  1708. if (!cast_type.is_set()) {
  1709. return;
  1710. }
  1711. cast_type.is_meta_type = false; // The casted value won't be a type name.
  1712. p_cast->set_datatype(cast_type);
  1713. if (!cast_type.is_variant()) {
  1714. GDScriptParser::DataType op_type = p_cast->operand->get_datatype();
  1715. if (!op_type.is_variant()) {
  1716. bool valid = false;
  1717. if (op_type.kind == GDScriptParser::DataType::BUILTIN && cast_type.kind == GDScriptParser::DataType::BUILTIN) {
  1718. valid = Variant::can_convert(op_type.builtin_type, cast_type.builtin_type);
  1719. } else if (op_type.kind != GDScriptParser::DataType::BUILTIN && cast_type.kind != GDScriptParser::DataType::BUILTIN) {
  1720. valid = is_type_compatible(cast_type, op_type) || is_type_compatible(op_type, cast_type);
  1721. }
  1722. if (!valid) {
  1723. push_error(vformat(R"(Invalid cast. Cannot convert from "%s" to "%s".)", op_type.to_string(), cast_type.to_string()), p_cast->cast_type);
  1724. }
  1725. }
  1726. } else {
  1727. mark_node_unsafe(p_cast);
  1728. }
  1729. #ifdef DEBUG_ENABLED
  1730. if (p_cast->operand->get_datatype().is_variant()) {
  1731. parser->push_warning(p_cast, GDScriptWarning::UNSAFE_CAST, cast_type.to_string());
  1732. mark_node_unsafe(p_cast);
  1733. }
  1734. #endif
  1735. // TODO: Perform cast on constants.
  1736. }
  1737. void GDScriptAnalyzer::reduce_dictionary(GDScriptParser::DictionaryNode *p_dictionary) {
  1738. bool all_is_constant = true;
  1739. HashMap<Variant, GDScriptParser::ExpressionNode *, VariantHasher, VariantComparator> elements;
  1740. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  1741. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  1742. if (p_dictionary->style == GDScriptParser::DictionaryNode::PYTHON_DICT) {
  1743. reduce_expression(element.key);
  1744. }
  1745. reduce_expression(element.value);
  1746. all_is_constant = all_is_constant && element.key->is_constant && element.value->is_constant;
  1747. if (element.key->is_constant) {
  1748. if (elements.has(element.key->reduced_value)) {
  1749. 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);
  1750. } else {
  1751. elements[element.key->reduced_value] = element.value;
  1752. }
  1753. }
  1754. }
  1755. if (all_is_constant) {
  1756. Dictionary dict;
  1757. for (int i = 0; i < p_dictionary->elements.size(); i++) {
  1758. const GDScriptParser::DictionaryNode::Pair &element = p_dictionary->elements[i];
  1759. dict[element.key->reduced_value] = element.value->reduced_value;
  1760. }
  1761. p_dictionary->is_constant = true;
  1762. p_dictionary->reduced_value = dict;
  1763. }
  1764. // It's dictionary in any case.
  1765. GDScriptParser::DataType dict_type;
  1766. dict_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1767. dict_type.kind = GDScriptParser::DataType::BUILTIN;
  1768. dict_type.builtin_type = Variant::DICTIONARY;
  1769. dict_type.is_constant = true;
  1770. p_dictionary->set_datatype(dict_type);
  1771. }
  1772. void GDScriptAnalyzer::reduce_get_node(GDScriptParser::GetNodeNode *p_get_node) {
  1773. GDScriptParser::DataType result;
  1774. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1775. result.kind = GDScriptParser::DataType::NATIVE;
  1776. result.native_type = "Node";
  1777. result.builtin_type = Variant::OBJECT;
  1778. if (!ClassDB::is_parent_class(get_real_class_name(parser->current_class->base_type.native_type), result.native_type)) {
  1779. push_error(R"*(Cannot use shorthand "get_node()" notation ("$") on a class that isn't a node.)*", p_get_node);
  1780. }
  1781. p_get_node->set_datatype(result);
  1782. }
  1783. GDScriptParser::DataType GDScriptAnalyzer::make_global_class_meta_type(const StringName &p_class_name) {
  1784. Ref<GDScriptParserRef> ref = get_parser_for(ScriptServer::get_global_class_path(p_class_name));
  1785. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  1786. GDScriptParser::DataType type;
  1787. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1788. type.kind = GDScriptParser::DataType::CLASS;
  1789. type.builtin_type = Variant::OBJECT;
  1790. type.native_type = ScriptServer::get_global_class_native_base(p_class_name);
  1791. type.class_type = ref->get_parser()->head;
  1792. type.script_path = ref->get_parser()->script_path;
  1793. type.is_constant = true;
  1794. type.is_meta_type = true;
  1795. return type;
  1796. }
  1797. void GDScriptAnalyzer::reduce_identifier_from_base(GDScriptParser::IdentifierNode *p_identifier, GDScriptParser::DataType *p_base) {
  1798. GDScriptParser::DataType base;
  1799. if (p_base == nullptr) {
  1800. base = type_from_metatype(parser->current_class->get_datatype());
  1801. } else {
  1802. base = *p_base;
  1803. }
  1804. const StringName &name = p_identifier->name;
  1805. if (base.kind == GDScriptParser::DataType::BUILTIN) {
  1806. if (base.is_meta_type) {
  1807. bool valid = true;
  1808. Variant result = Variant::get_constant_value(base.builtin_type, name, &valid);
  1809. if (valid) {
  1810. p_identifier->is_constant = true;
  1811. p_identifier->reduced_value = result;
  1812. p_identifier->set_datatype(type_from_variant(result));
  1813. } else {
  1814. push_error(vformat(R"(Cannot find constant "%s" on type "%s".)", name, base.to_string()), p_identifier);
  1815. }
  1816. } else {
  1817. Callable::CallError temp;
  1818. Variant dummy = Variant::construct(base.builtin_type, nullptr, 0, temp);
  1819. List<PropertyInfo> properties;
  1820. dummy.get_property_list(&properties);
  1821. for (const List<PropertyInfo>::Element *E = properties.front(); E != nullptr; E = E->next()) {
  1822. const PropertyInfo &prop = E->get();
  1823. if (prop.name == name) {
  1824. p_identifier->set_datatype(type_from_property(prop));
  1825. return;
  1826. }
  1827. }
  1828. push_error(vformat(R"(Cannot find property "%s" on base "%s".)", name, base.to_string()), p_identifier);
  1829. }
  1830. return;
  1831. }
  1832. if (base.kind == GDScriptParser::DataType::ENUM) {
  1833. if (base.is_meta_type) {
  1834. if (base.enum_values.has(name)) {
  1835. p_identifier->is_constant = true;
  1836. p_identifier->reduced_value = base.enum_values[name];
  1837. GDScriptParser::DataType result;
  1838. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1839. result.kind = GDScriptParser::DataType::ENUM_VALUE;
  1840. result.native_type = base.native_type;
  1841. result.enum_type = name;
  1842. p_identifier->set_datatype(result);
  1843. } else {
  1844. push_error(vformat(R"(Cannot find value "%s" in "%s".)", name, base.to_string()), p_identifier);
  1845. }
  1846. } else {
  1847. push_error(R"(Cannot get property from enum value.)", p_identifier);
  1848. }
  1849. return;
  1850. }
  1851. GDScriptParser::ClassNode *base_class = base.class_type;
  1852. // TODO: Switch current class/function/suite here to avoid misrepresenting identifiers (in recursive reduce calls).
  1853. while (base_class != nullptr) {
  1854. if (base_class->identifier && base_class->identifier->name == name) {
  1855. p_identifier->set_datatype(base_class->get_datatype());
  1856. return;
  1857. }
  1858. if (base_class->has_member(name)) {
  1859. const GDScriptParser::ClassNode::Member &member = base_class->get_member(name);
  1860. p_identifier->set_datatype(member.get_datatype());
  1861. switch (member.type) {
  1862. case GDScriptParser::ClassNode::Member::CONSTANT:
  1863. // For out-of-order resolution:
  1864. reduce_expression(member.constant->initializer);
  1865. p_identifier->is_constant = true;
  1866. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  1867. p_identifier->set_datatype(member.constant->initializer->get_datatype());
  1868. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_CONSTANT;
  1869. p_identifier->constant_source = member.constant;
  1870. break;
  1871. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1872. p_identifier->is_constant = true;
  1873. p_identifier->reduced_value = member.enum_value.value;
  1874. break;
  1875. case GDScriptParser::ClassNode::Member::VARIABLE:
  1876. p_identifier->source = GDScriptParser::IdentifierNode::MEMBER_VARIABLE;
  1877. p_identifier->variable_source = member.variable;
  1878. break;
  1879. case GDScriptParser::ClassNode::Member::FUNCTION:
  1880. resolve_function_signature(member.function);
  1881. p_identifier->set_datatype(make_callable_type(member.function->info));
  1882. break;
  1883. default:
  1884. break; // Type already set.
  1885. }
  1886. return;
  1887. }
  1888. // Check outer constants.
  1889. // TODO: Allow outer static functions.
  1890. GDScriptParser::ClassNode *outer = base_class->outer;
  1891. while (outer != nullptr) {
  1892. if (outer->has_member(name)) {
  1893. const GDScriptParser::ClassNode::Member &member = outer->get_member(name);
  1894. if (member.type == GDScriptParser::ClassNode::Member::CONSTANT) {
  1895. // TODO: Make sure loops won't cause problem. And make special error message for those.
  1896. // For out-of-order resolution:
  1897. reduce_expression(member.constant->initializer);
  1898. p_identifier->set_datatype(member.get_datatype());
  1899. p_identifier->is_constant = true;
  1900. p_identifier->reduced_value = member.constant->initializer->reduced_value;
  1901. return;
  1902. }
  1903. }
  1904. outer = outer->outer;
  1905. }
  1906. base_class = base_class->base_type.class_type;
  1907. }
  1908. // Check native members.
  1909. const StringName &native = get_real_class_name(base.native_type);
  1910. if (class_exists(native)) {
  1911. PropertyInfo prop_info;
  1912. MethodInfo method_info;
  1913. if (ClassDB::get_property_info(native, name, &prop_info)) {
  1914. p_identifier->set_datatype(type_from_property(prop_info));
  1915. return;
  1916. }
  1917. if (ClassDB::get_method_info(native, name, &method_info)) {
  1918. // Method is callable.
  1919. p_identifier->set_datatype(make_callable_type(method_info));
  1920. return;
  1921. }
  1922. if (ClassDB::get_signal(native, name, &method_info)) {
  1923. // Signal is a type too.
  1924. p_identifier->set_datatype(make_signal_type(method_info));
  1925. return;
  1926. }
  1927. if (ClassDB::has_enum(native, name)) {
  1928. p_identifier->set_datatype(make_native_enum_type(native, name));
  1929. return;
  1930. }
  1931. bool valid = false;
  1932. int int_constant = ClassDB::get_integer_constant(native, name, &valid);
  1933. if (valid) {
  1934. p_identifier->is_constant = true;
  1935. p_identifier->reduced_value = int_constant;
  1936. p_identifier->set_datatype(type_from_variant(int_constant));
  1937. return;
  1938. }
  1939. }
  1940. }
  1941. void GDScriptAnalyzer::reduce_identifier(GDScriptParser::IdentifierNode *p_identifier, bool can_be_builtin) {
  1942. // TODO: This is opportunity to further infer types.
  1943. // Check if we are inside and enum. This allows enum values to access other elements of the same enum.
  1944. if (current_enum) {
  1945. for (int i = 0; i < current_enum->values.size(); i++) {
  1946. const GDScriptParser::EnumNode::Value &element = current_enum->values[i];
  1947. if (element.identifier->name == p_identifier->name) {
  1948. GDScriptParser::DataType type;
  1949. type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1950. type.kind = element.parent_enum->identifier ? GDScriptParser::DataType::ENUM_VALUE : GDScriptParser::DataType::BUILTIN;
  1951. type.builtin_type = Variant::INT;
  1952. type.is_constant = true;
  1953. if (element.parent_enum->identifier) {
  1954. type.enum_type = element.parent_enum->identifier->name;
  1955. }
  1956. p_identifier->set_datatype(type);
  1957. if (element.resolved) {
  1958. p_identifier->is_constant = true;
  1959. p_identifier->reduced_value = element.value;
  1960. } else {
  1961. push_error(R"(Cannot use another enum element before it was declared.)", p_identifier);
  1962. }
  1963. return; // Found anyway.
  1964. }
  1965. }
  1966. }
  1967. // Check if identifier is local.
  1968. // If that's the case, the declaration already was solved before.
  1969. switch (p_identifier->source) {
  1970. case GDScriptParser::IdentifierNode::FUNCTION_PARAMETER:
  1971. p_identifier->set_datatype(p_identifier->parameter_source->get_datatype());
  1972. return;
  1973. case GDScriptParser::IdentifierNode::LOCAL_CONSTANT:
  1974. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  1975. p_identifier->set_datatype(p_identifier->constant_source->get_datatype());
  1976. p_identifier->is_constant = true;
  1977. // TODO: Constant should have a value on the node itself.
  1978. p_identifier->reduced_value = p_identifier->constant_source->initializer->reduced_value;
  1979. return;
  1980. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  1981. p_identifier->variable_source->usages++;
  1982. [[fallthrough]];
  1983. case GDScriptParser::IdentifierNode::LOCAL_VARIABLE:
  1984. p_identifier->set_datatype(p_identifier->variable_source->get_datatype());
  1985. return;
  1986. case GDScriptParser::IdentifierNode::LOCAL_ITERATOR:
  1987. p_identifier->set_datatype(p_identifier->bind_source->get_datatype());
  1988. return;
  1989. case GDScriptParser::IdentifierNode::LOCAL_BIND: {
  1990. GDScriptParser::DataType result = p_identifier->bind_source->get_datatype();
  1991. result.is_constant = true;
  1992. p_identifier->set_datatype(result);
  1993. return;
  1994. }
  1995. case GDScriptParser::IdentifierNode::UNDEFINED_SOURCE:
  1996. break;
  1997. }
  1998. // Not a local, so check members.
  1999. reduce_identifier_from_base(p_identifier);
  2000. if (p_identifier->get_datatype().is_set()) {
  2001. // Found.
  2002. return;
  2003. }
  2004. StringName name = p_identifier->name;
  2005. p_identifier->source = GDScriptParser::IdentifierNode::UNDEFINED_SOURCE;
  2006. // Check globals.
  2007. if (GDScriptParser::get_builtin_type(name) < Variant::VARIANT_MAX) {
  2008. if (can_be_builtin) {
  2009. p_identifier->set_datatype(make_builtin_meta_type(GDScriptParser::get_builtin_type(name)));
  2010. return;
  2011. } else {
  2012. push_error(R"(Builtin type cannot be used as a name on its own.)", p_identifier);
  2013. }
  2014. }
  2015. if (class_exists(name)) {
  2016. p_identifier->set_datatype(make_native_meta_type(name));
  2017. return;
  2018. }
  2019. if (ScriptServer::is_global_class(name)) {
  2020. p_identifier->set_datatype(make_global_class_meta_type(name));
  2021. return;
  2022. }
  2023. if (GDScriptLanguage::get_singleton()->get_global_map().has(name)) {
  2024. int idx = GDScriptLanguage::get_singleton()->get_global_map()[name];
  2025. Variant constant = GDScriptLanguage::get_singleton()->get_global_array()[idx];
  2026. p_identifier->set_datatype(type_from_variant(constant));
  2027. p_identifier->is_constant = true;
  2028. p_identifier->reduced_value = constant;
  2029. return;
  2030. }
  2031. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(name)) {
  2032. Variant constant = GDScriptLanguage::get_singleton()->get_named_globals_map()[name];
  2033. p_identifier->set_datatype(type_from_variant(constant));
  2034. p_identifier->is_constant = true;
  2035. p_identifier->reduced_value = constant;
  2036. return;
  2037. }
  2038. // Not found.
  2039. // Check if it's a builtin function.
  2040. if (parser->get_builtin_function(name) < GDScriptFunctions::FUNC_MAX) {
  2041. push_error(vformat(R"(Built-in function "%s" cannot be used as an identifier.)", name), p_identifier);
  2042. } else {
  2043. push_error(vformat(R"(Identifier "%s" not declared in the current scope.)", name), p_identifier);
  2044. }
  2045. GDScriptParser::DataType dummy;
  2046. dummy.kind = GDScriptParser::DataType::VARIANT;
  2047. p_identifier->set_datatype(dummy); // Just so type is set to something.
  2048. }
  2049. void GDScriptAnalyzer::reduce_literal(GDScriptParser::LiteralNode *p_literal) {
  2050. p_literal->reduced_value = p_literal->value;
  2051. p_literal->is_constant = true;
  2052. p_literal->set_datatype(type_from_variant(p_literal->reduced_value));
  2053. }
  2054. void GDScriptAnalyzer::reduce_preload(GDScriptParser::PreloadNode *p_preload) {
  2055. if (!p_preload->path) {
  2056. return;
  2057. }
  2058. reduce_expression(p_preload->path);
  2059. if (!p_preload->path->is_constant) {
  2060. push_error("Preloaded path must be a constant string.", p_preload->path);
  2061. return;
  2062. }
  2063. if (p_preload->path->reduced_value.get_type() != Variant::STRING) {
  2064. push_error("Preloaded path must be a constant string.", p_preload->path);
  2065. } else {
  2066. p_preload->resolved_path = p_preload->path->reduced_value;
  2067. // TODO: Save this as script dependency.
  2068. if (p_preload->resolved_path.is_rel_path()) {
  2069. p_preload->resolved_path = parser->script_path.get_base_dir().plus_file(p_preload->resolved_path);
  2070. }
  2071. p_preload->resolved_path = p_preload->resolved_path.simplify_path();
  2072. if (!FileAccess::exists(p_preload->resolved_path)) {
  2073. push_error(vformat(R"(Preload file "%s" does not exist.)", p_preload->resolved_path), p_preload->path);
  2074. } else {
  2075. // TODO: Don't load if validating: use completion cache.
  2076. p_preload->resource = ResourceLoader::load(p_preload->resolved_path);
  2077. if (p_preload->resource.is_null()) {
  2078. push_error(vformat(R"(Could not p_preload resource file "%s".)", p_preload->resolved_path), p_preload->path);
  2079. }
  2080. }
  2081. }
  2082. p_preload->is_constant = true;
  2083. p_preload->reduced_value = p_preload->resource;
  2084. p_preload->set_datatype(type_from_variant(p_preload->reduced_value));
  2085. }
  2086. void GDScriptAnalyzer::reduce_self(GDScriptParser::SelfNode *p_self) {
  2087. p_self->is_constant = false;
  2088. p_self->set_datatype(type_from_metatype(parser->current_class->get_datatype()));
  2089. }
  2090. void GDScriptAnalyzer::reduce_subscript(GDScriptParser::SubscriptNode *p_subscript) {
  2091. if (p_subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2092. reduce_identifier(static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base), true);
  2093. } else {
  2094. reduce_expression(p_subscript->base);
  2095. }
  2096. GDScriptParser::DataType result_type;
  2097. // Reduce index first. If it's a constant StringName, use attribute instead.
  2098. if (!p_subscript->is_attribute) {
  2099. if (p_subscript->index == nullptr) {
  2100. return;
  2101. }
  2102. reduce_expression(p_subscript->index);
  2103. if (p_subscript->index->is_constant && p_subscript->index->reduced_value.get_type() == Variant::STRING_NAME) {
  2104. GDScriptParser::IdentifierNode *attribute = parser->alloc_node<GDScriptParser::IdentifierNode>();
  2105. // Copy location for better error message.
  2106. attribute->start_line = p_subscript->index->start_line;
  2107. attribute->end_line = p_subscript->index->end_line;
  2108. attribute->leftmost_column = p_subscript->index->leftmost_column;
  2109. attribute->rightmost_column = p_subscript->index->rightmost_column;
  2110. p_subscript->is_attribute = true;
  2111. p_subscript->attribute = attribute;
  2112. }
  2113. }
  2114. if (p_subscript->is_attribute) {
  2115. if (p_subscript->attribute == nullptr) {
  2116. return;
  2117. }
  2118. if (p_subscript->base->is_constant) {
  2119. // Just try to get it.
  2120. bool valid = false;
  2121. Variant value = p_subscript->base->reduced_value.get_named(p_subscript->attribute->name, &valid);
  2122. if (!valid) {
  2123. push_error(vformat(R"(Cannot get member "%s" from "%s".)", p_subscript->attribute->name, p_subscript->base->reduced_value), p_subscript->index);
  2124. } else {
  2125. p_subscript->is_constant = true;
  2126. p_subscript->reduced_value = value;
  2127. result_type = type_from_variant(value);
  2128. }
  2129. result_type.kind = GDScriptParser::DataType::VARIANT;
  2130. } else {
  2131. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2132. if (base_type.is_variant()) {
  2133. result_type.kind = GDScriptParser::DataType::VARIANT;
  2134. mark_node_unsafe(p_subscript);
  2135. } else {
  2136. reduce_identifier_from_base(p_subscript->attribute, &base_type);
  2137. GDScriptParser::DataType attr_type = p_subscript->attribute->get_datatype();
  2138. if (attr_type.is_set()) {
  2139. result_type = attr_type;
  2140. p_subscript->is_constant = p_subscript->attribute->is_constant;
  2141. p_subscript->reduced_value = p_subscript->attribute->reduced_value;
  2142. } else {
  2143. if (base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2144. push_error(vformat(R"(Cannot find member "%s" in base "%s".)", p_subscript->attribute->name, base_type.to_string()), p_subscript->attribute);
  2145. #ifdef DEBUG_ENABLED
  2146. } else {
  2147. parser->push_warning(p_subscript, GDScriptWarning::UNSAFE_PROPERTY_ACCESS, p_subscript->attribute->name, base_type.to_string());
  2148. #endif
  2149. }
  2150. result_type.kind = GDScriptParser::DataType::VARIANT;
  2151. }
  2152. }
  2153. }
  2154. } else {
  2155. // Index was already reduced before.
  2156. if (p_subscript->base->is_constant && p_subscript->index->is_constant) {
  2157. // Just try to get it.
  2158. bool valid = false;
  2159. Variant value = p_subscript->base->reduced_value.get(p_subscript->index->reduced_value, &valid);
  2160. if (!valid) {
  2161. push_error(vformat(R"(Cannot get index "%s" from "%s".)", p_subscript->index->reduced_value, p_subscript->base->reduced_value), p_subscript->index);
  2162. } else {
  2163. p_subscript->is_constant = true;
  2164. p_subscript->reduced_value = value;
  2165. result_type = type_from_variant(value);
  2166. }
  2167. result_type.kind = GDScriptParser::DataType::VARIANT;
  2168. } else {
  2169. GDScriptParser::DataType base_type = p_subscript->base->get_datatype();
  2170. GDScriptParser::DataType index_type = p_subscript->index->get_datatype();
  2171. if (base_type.is_variant()) {
  2172. result_type.kind = GDScriptParser::DataType::VARIANT;
  2173. mark_node_unsafe(p_subscript);
  2174. } else {
  2175. if (base_type.kind == GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2176. // Check if indexing is valid.
  2177. bool error = index_type.kind != GDScriptParser::DataType::BUILTIN && base_type.builtin_type != Variant::DICTIONARY;
  2178. if (!error) {
  2179. switch (base_type.builtin_type) {
  2180. // Expect int or real as index.
  2181. case Variant::PACKED_BYTE_ARRAY:
  2182. case Variant::PACKED_COLOR_ARRAY:
  2183. case Variant::PACKED_FLOAT32_ARRAY:
  2184. case Variant::PACKED_FLOAT64_ARRAY:
  2185. case Variant::PACKED_INT32_ARRAY:
  2186. case Variant::PACKED_INT64_ARRAY:
  2187. case Variant::PACKED_STRING_ARRAY:
  2188. case Variant::PACKED_VECTOR2_ARRAY:
  2189. case Variant::PACKED_VECTOR3_ARRAY:
  2190. case Variant::ARRAY:
  2191. case Variant::STRING:
  2192. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT;
  2193. break;
  2194. // Expect String only.
  2195. case Variant::RECT2:
  2196. case Variant::RECT2I:
  2197. case Variant::PLANE:
  2198. case Variant::QUAT:
  2199. case Variant::AABB:
  2200. case Variant::OBJECT:
  2201. error = index_type.builtin_type != Variant::STRING;
  2202. break;
  2203. // Expect String or number.
  2204. case Variant::BASIS:
  2205. case Variant::VECTOR2:
  2206. case Variant::VECTOR2I:
  2207. case Variant::VECTOR3:
  2208. case Variant::VECTOR3I:
  2209. case Variant::TRANSFORM:
  2210. case Variant::TRANSFORM2D:
  2211. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::FLOAT &&
  2212. index_type.builtin_type != Variant::STRING;
  2213. break;
  2214. // Expect String or int.
  2215. case Variant::COLOR:
  2216. error = index_type.builtin_type != Variant::INT && index_type.builtin_type != Variant::STRING;
  2217. break;
  2218. // Don't support indexing, but we will check it later.
  2219. case Variant::_RID:
  2220. case Variant::BOOL:
  2221. case Variant::CALLABLE:
  2222. case Variant::FLOAT:
  2223. case Variant::INT:
  2224. case Variant::NIL:
  2225. case Variant::NODE_PATH:
  2226. case Variant::SIGNAL:
  2227. case Variant::STRING_NAME:
  2228. break;
  2229. // Here for completeness.
  2230. case Variant::DICTIONARY:
  2231. case Variant::VARIANT_MAX:
  2232. break;
  2233. }
  2234. if (error) {
  2235. 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);
  2236. }
  2237. }
  2238. } else if (base_type.kind != GDScriptParser::DataType::BUILTIN && !index_type.is_variant()) {
  2239. if (index_type.builtin_type != Variant::STRING && index_type.builtin_type != Variant::STRING_NAME) {
  2240. 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);
  2241. }
  2242. }
  2243. // Check resulting type if possible.
  2244. result_type.builtin_type = Variant::NIL;
  2245. result_type.kind = GDScriptParser::DataType::BUILTIN;
  2246. result_type.type_source = base_type.is_hard_type() ? GDScriptParser::DataType::ANNOTATED_INFERRED : GDScriptParser::DataType::INFERRED;
  2247. switch (base_type.builtin_type) {
  2248. // Can't index at all.
  2249. case Variant::_RID:
  2250. case Variant::BOOL:
  2251. case Variant::CALLABLE:
  2252. case Variant::FLOAT:
  2253. case Variant::INT:
  2254. case Variant::NIL:
  2255. case Variant::NODE_PATH:
  2256. case Variant::SIGNAL:
  2257. case Variant::STRING_NAME:
  2258. result_type.kind = GDScriptParser::DataType::VARIANT;
  2259. push_error(vformat(R"(Cannot use subscript operator on a base of type "%s".)", base_type.to_string()), p_subscript->base);
  2260. break;
  2261. // Return int.
  2262. case Variant::PACKED_BYTE_ARRAY:
  2263. case Variant::PACKED_INT32_ARRAY:
  2264. case Variant::PACKED_INT64_ARRAY:
  2265. case Variant::VECTOR2I:
  2266. case Variant::VECTOR3I:
  2267. result_type.builtin_type = Variant::INT;
  2268. break;
  2269. // Return float.
  2270. case Variant::PACKED_FLOAT32_ARRAY:
  2271. case Variant::PACKED_FLOAT64_ARRAY:
  2272. case Variant::VECTOR2:
  2273. case Variant::VECTOR3:
  2274. case Variant::QUAT:
  2275. result_type.builtin_type = Variant::FLOAT;
  2276. break;
  2277. // Return Color.
  2278. case Variant::PACKED_COLOR_ARRAY:
  2279. result_type.builtin_type = Variant::COLOR;
  2280. break;
  2281. // Return String.
  2282. case Variant::PACKED_STRING_ARRAY:
  2283. case Variant::STRING:
  2284. result_type.builtin_type = Variant::STRING;
  2285. break;
  2286. // Return Vector2.
  2287. case Variant::PACKED_VECTOR2_ARRAY:
  2288. case Variant::TRANSFORM2D:
  2289. case Variant::RECT2:
  2290. result_type.builtin_type = Variant::VECTOR2;
  2291. break;
  2292. // Return Vector2I.
  2293. case Variant::RECT2I:
  2294. result_type.builtin_type = Variant::VECTOR2I;
  2295. break;
  2296. // Return Vector3.
  2297. case Variant::PACKED_VECTOR3_ARRAY:
  2298. case Variant::AABB:
  2299. case Variant::BASIS:
  2300. result_type.builtin_type = Variant::VECTOR3;
  2301. break;
  2302. // Depends on the index.
  2303. case Variant::TRANSFORM:
  2304. case Variant::PLANE:
  2305. case Variant::COLOR:
  2306. case Variant::ARRAY:
  2307. case Variant::DICTIONARY:
  2308. result_type.kind = GDScriptParser::DataType::VARIANT;
  2309. result_type.type_source = GDScriptParser::DataType::UNDETECTED;
  2310. break;
  2311. // Here for completeness.
  2312. case Variant::OBJECT:
  2313. case Variant::VARIANT_MAX:
  2314. break;
  2315. }
  2316. }
  2317. }
  2318. }
  2319. p_subscript->set_datatype(result_type);
  2320. }
  2321. void GDScriptAnalyzer::reduce_ternary_op(GDScriptParser::TernaryOpNode *p_ternary_op) {
  2322. reduce_expression(p_ternary_op->condition);
  2323. reduce_expression(p_ternary_op->true_expr);
  2324. reduce_expression(p_ternary_op->false_expr);
  2325. GDScriptParser::DataType result;
  2326. 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) {
  2327. p_ternary_op->is_constant = true;
  2328. if (p_ternary_op->condition->reduced_value.booleanize()) {
  2329. p_ternary_op->reduced_value = p_ternary_op->true_expr->reduced_value;
  2330. } else {
  2331. p_ternary_op->reduced_value = p_ternary_op->false_expr->reduced_value;
  2332. }
  2333. }
  2334. GDScriptParser::DataType true_type;
  2335. if (p_ternary_op->true_expr) {
  2336. true_type = p_ternary_op->true_expr->get_datatype();
  2337. } else {
  2338. true_type.kind = GDScriptParser::DataType::VARIANT;
  2339. }
  2340. GDScriptParser::DataType false_type;
  2341. if (p_ternary_op->false_expr) {
  2342. false_type = p_ternary_op->false_expr->get_datatype();
  2343. } else {
  2344. false_type.kind = GDScriptParser::DataType::VARIANT;
  2345. }
  2346. if (true_type.is_variant() || false_type.is_variant()) {
  2347. result.kind = GDScriptParser::DataType::VARIANT;
  2348. } else {
  2349. result = true_type;
  2350. if (!is_type_compatible(true_type, false_type)) {
  2351. result = false_type;
  2352. if (!is_type_compatible(false_type, true_type)) {
  2353. result.type_source = GDScriptParser::DataType::UNDETECTED;
  2354. result.kind = GDScriptParser::DataType::VARIANT;
  2355. #ifdef DEBUG_ENABLED
  2356. parser->push_warning(p_ternary_op, GDScriptWarning::INCOMPATIBLE_TERNARY);
  2357. #endif
  2358. }
  2359. }
  2360. }
  2361. p_ternary_op->set_datatype(result);
  2362. }
  2363. void GDScriptAnalyzer::reduce_unary_op(GDScriptParser::UnaryOpNode *p_unary_op) {
  2364. reduce_expression(p_unary_op->operand);
  2365. GDScriptParser::DataType result;
  2366. if (p_unary_op->operand == nullptr) {
  2367. result.kind = GDScriptParser::DataType::VARIANT;
  2368. p_unary_op->set_datatype(result);
  2369. return;
  2370. }
  2371. if (p_unary_op->operand->is_constant) {
  2372. p_unary_op->is_constant = true;
  2373. p_unary_op->reduced_value = Variant::evaluate(p_unary_op->variant_op, p_unary_op->operand->reduced_value, Variant());
  2374. result = type_from_variant(p_unary_op->reduced_value);
  2375. } else if (p_unary_op->operand->get_datatype().is_variant()) {
  2376. result.kind = GDScriptParser::DataType::VARIANT;
  2377. mark_node_unsafe(p_unary_op);
  2378. } else {
  2379. bool valid = false;
  2380. result = get_operation_type(p_unary_op->variant_op, p_unary_op->operand->get_datatype(), p_unary_op->operand->get_datatype(), valid);
  2381. if (!valid) {
  2382. 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);
  2383. }
  2384. }
  2385. p_unary_op->set_datatype(result);
  2386. }
  2387. GDScriptParser::DataType GDScriptAnalyzer::type_from_variant(const Variant &p_value) {
  2388. GDScriptParser::DataType result;
  2389. result.is_constant = true;
  2390. result.kind = GDScriptParser::DataType::BUILTIN;
  2391. result.builtin_type = p_value.get_type();
  2392. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT; // Constant has explicit type.
  2393. if (p_value.get_type() == Variant::OBJECT) {
  2394. Object *obj = p_value;
  2395. if (!obj) {
  2396. return GDScriptParser::DataType();
  2397. }
  2398. result.native_type = obj->get_class_name();
  2399. Ref<Script> scr = p_value; // Check if value is a script itself.
  2400. if (scr.is_valid()) {
  2401. result.is_meta_type = true;
  2402. } else {
  2403. result.is_meta_type = false;
  2404. scr = obj->get_script();
  2405. }
  2406. if (scr.is_valid()) {
  2407. result.script_type = scr;
  2408. result.script_path = scr->get_path();
  2409. Ref<GDScript> gds = scr;
  2410. if (gds.is_valid()) {
  2411. result.kind = GDScriptParser::DataType::CLASS;
  2412. // This might be an inner class, so we want to get the parser for the root.
  2413. // But still get the inner class from that tree.
  2414. GDScript *current = gds.ptr();
  2415. List<StringName> class_chain;
  2416. while (current->_owner) {
  2417. // Push to front so it's in reverse.
  2418. class_chain.push_front(current->name);
  2419. current = current->_owner;
  2420. }
  2421. Ref<GDScriptParserRef> ref = get_parser_for(current->path);
  2422. ref->raise_status(GDScriptParserRef::INTERFACE_SOLVED);
  2423. GDScriptParser::ClassNode *found = ref->get_parser()->head;
  2424. // It should be okay to assume this exists, since we have a complete script already.
  2425. for (const List<StringName>::Element *E = class_chain.front(); E; E = E->next()) {
  2426. found = found->get_member(E->get()).m_class;
  2427. }
  2428. result.class_type = found;
  2429. result.script_path = ref->get_parser()->script_path;
  2430. } else {
  2431. result.kind = GDScriptParser::DataType::SCRIPT;
  2432. }
  2433. result.native_type = scr->get_instance_base_type();
  2434. } else {
  2435. result.kind = GDScriptParser::DataType::NATIVE;
  2436. if (result.native_type == GDScriptNativeClass::get_class_static()) {
  2437. result.is_meta_type = true;
  2438. }
  2439. }
  2440. }
  2441. return result;
  2442. }
  2443. GDScriptParser::DataType GDScriptAnalyzer::type_from_metatype(const GDScriptParser::DataType &p_meta_type) const {
  2444. GDScriptParser::DataType result = p_meta_type;
  2445. result.is_meta_type = false;
  2446. result.is_constant = false;
  2447. return result;
  2448. }
  2449. GDScriptParser::DataType GDScriptAnalyzer::type_from_property(const PropertyInfo &p_property) const {
  2450. GDScriptParser::DataType result;
  2451. result.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2452. if (p_property.type == Variant::NIL && (p_property.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  2453. // Variant
  2454. result.kind = GDScriptParser::DataType::VARIANT;
  2455. return result;
  2456. }
  2457. result.builtin_type = p_property.type;
  2458. if (p_property.type == Variant::OBJECT) {
  2459. result.kind = GDScriptParser::DataType::NATIVE;
  2460. result.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  2461. } else {
  2462. result.kind = GDScriptParser::DataType::BUILTIN;
  2463. }
  2464. return result;
  2465. }
  2466. bool GDScriptAnalyzer::get_function_signature(GDScriptParser::Node *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) {
  2467. r_static = false;
  2468. r_vararg = false;
  2469. r_default_arg_count = 0;
  2470. StringName function_name = p_function;
  2471. if (p_base_type.kind == GDScriptParser::DataType::BUILTIN) {
  2472. // Construct a base type to get methods.
  2473. Callable::CallError err;
  2474. Variant dummy = Variant::construct(p_base_type.builtin_type, nullptr, 0, err);
  2475. if (err.error != Callable::CallError::CALL_OK) {
  2476. ERR_FAIL_V_MSG(false, "Could not construct base Variant type.");
  2477. }
  2478. List<MethodInfo> methods;
  2479. dummy.get_method_list(&methods);
  2480. for (const List<MethodInfo>::Element *E = methods.front(); E != nullptr; E = E->next()) {
  2481. if (E->get().name == p_function) {
  2482. return function_signature_from_info(E->get(), r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  2483. }
  2484. }
  2485. return false;
  2486. }
  2487. bool is_constructor = p_base_type.is_meta_type && p_function == "new";
  2488. if (is_constructor) {
  2489. function_name = "_init";
  2490. r_static = true;
  2491. }
  2492. GDScriptParser::ClassNode *base_class = p_base_type.class_type;
  2493. GDScriptParser::FunctionNode *found_function = nullptr;
  2494. while (found_function == nullptr && base_class != nullptr) {
  2495. if (base_class->has_member(function_name)) {
  2496. if (base_class->get_member(function_name).type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2497. // TODO: If this is Callable it can have a better error message.
  2498. push_error(vformat(R"(Member "%s" is not a function.)", function_name), p_source);
  2499. return false;
  2500. }
  2501. found_function = base_class->get_member(function_name).function;
  2502. }
  2503. base_class = base_class->base_type.class_type;
  2504. }
  2505. if (found_function != nullptr) {
  2506. r_static = is_constructor || found_function->is_static;
  2507. for (int i = 0; i < found_function->parameters.size(); i++) {
  2508. r_par_types.push_back(found_function->parameters[i]->get_datatype());
  2509. if (found_function->parameters[i]->default_value != nullptr) {
  2510. r_default_arg_count++;
  2511. }
  2512. }
  2513. r_return_type = found_function->get_datatype();
  2514. r_return_type.is_coroutine = found_function->is_coroutine;
  2515. return true;
  2516. }
  2517. Ref<Script> base_script = p_base_type.script_type;
  2518. while (base_script.is_valid() && base_script->is_valid()) {
  2519. MethodInfo info = base_script->get_method_info(function_name);
  2520. if (!(info == MethodInfo())) {
  2521. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  2522. }
  2523. base_script = base_script->get_base_script();
  2524. }
  2525. // If the base is a script, it might be trying to access members of the Script class itself.
  2526. if (p_base_type.is_meta_type && !is_constructor && (p_base_type.kind == GDScriptParser::DataType::SCRIPT || p_base_type.kind == GDScriptParser::DataType::CLASS)) {
  2527. MethodInfo info;
  2528. StringName script_class = p_base_type.kind == GDScriptParser::DataType::SCRIPT ? p_base_type.script_type->get_class_name() : StringName(GDScript::get_class_static());
  2529. if (ClassDB::get_method_info(script_class, function_name, &info)) {
  2530. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  2531. }
  2532. }
  2533. StringName base_native = p_base_type.native_type;
  2534. #ifdef DEBUG_ENABLED
  2535. if (base_native != StringName()) {
  2536. // Empty native class might happen in some Script implementations.
  2537. // Just ignore it.
  2538. if (!class_exists(base_native)) {
  2539. ERR_FAIL_V_MSG(false, vformat("Native class %s used in script doesn't exist or isn't exposed.", base_native));
  2540. }
  2541. }
  2542. #endif
  2543. if (is_constructor) {
  2544. // Native types always have a default constructor.
  2545. r_return_type = p_base_type;
  2546. r_return_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2547. r_return_type.is_meta_type = false;
  2548. return true;
  2549. }
  2550. StringName real_native = get_real_class_name(base_native);
  2551. MethodInfo info;
  2552. if (ClassDB::get_method_info(real_native, function_name, &info)) {
  2553. return function_signature_from_info(info, r_return_type, r_par_types, r_default_arg_count, r_static, r_vararg);
  2554. }
  2555. return false;
  2556. }
  2557. 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) {
  2558. r_return_type = type_from_property(p_info.return_val);
  2559. r_default_arg_count = p_info.default_arguments.size();
  2560. r_vararg = (p_info.flags & METHOD_FLAG_VARARG) != 0;
  2561. for (const List<PropertyInfo>::Element *E = p_info.arguments.front(); E != nullptr; E = E->next()) {
  2562. r_par_types.push_back(type_from_property(E->get()));
  2563. }
  2564. return true;
  2565. }
  2566. bool GDScriptAnalyzer::validate_call_arg(const MethodInfo &p_method, const GDScriptParser::CallNode *p_call) {
  2567. List<GDScriptParser::DataType> arg_types;
  2568. for (const List<PropertyInfo>::Element *E = p_method.arguments.front(); E != nullptr; E = E->next()) {
  2569. arg_types.push_back(type_from_property(E->get()));
  2570. }
  2571. return validate_call_arg(arg_types, p_method.default_arguments.size(), (p_method.flags & METHOD_FLAG_VARARG) != 0, p_call);
  2572. }
  2573. 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) {
  2574. bool valid = true;
  2575. if (p_call->arguments.size() < p_par_types.size() - p_default_args_count) {
  2576. 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);
  2577. valid = false;
  2578. }
  2579. if (!p_is_vararg && p_call->arguments.size() > p_par_types.size()) {
  2580. 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()]);
  2581. valid = false;
  2582. }
  2583. for (int i = 0; i < p_call->arguments.size(); i++) {
  2584. if (i >= p_par_types.size()) {
  2585. // Already on vararg place.
  2586. break;
  2587. }
  2588. GDScriptParser::DataType par_type = p_par_types[i];
  2589. GDScriptParser::DataType arg_type = p_call->arguments[i]->get_datatype();
  2590. if (arg_type.is_variant()) {
  2591. // Argument can be anything, so this is unsafe.
  2592. mark_node_unsafe(p_call->arguments[i]);
  2593. } else if (!is_type_compatible(par_type, arg_type, true)) {
  2594. // Supertypes are acceptable for dynamic compliance, but it's unsafe.
  2595. mark_node_unsafe(p_call);
  2596. if (!is_type_compatible(arg_type, par_type)) {
  2597. push_error(vformat(R"*(Invalid argument for "%s()" function: argument %d should be %s but is %s.)*",
  2598. p_call->function_name, i + 1, par_type.to_string(), arg_type.to_string()),
  2599. p_call->arguments[i]);
  2600. valid = false;
  2601. }
  2602. #ifdef DEBUG_ENABLED
  2603. } else {
  2604. 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) {
  2605. parser->push_warning(p_call, GDScriptWarning::NARROWING_CONVERSION, p_call->function_name);
  2606. }
  2607. #endif
  2608. }
  2609. }
  2610. return valid;
  2611. }
  2612. #ifdef DEBUG_ENABLED
  2613. bool GDScriptAnalyzer::is_shadowing(GDScriptParser::IdentifierNode *p_local, const String &p_context) {
  2614. const StringName &name = p_local->name;
  2615. GDScriptParser::DataType base = parser->current_class->get_datatype();
  2616. GDScriptParser::ClassNode *base_class = base.class_type;
  2617. while (base_class != nullptr) {
  2618. if (base_class->has_member(name)) {
  2619. 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()));
  2620. return true;
  2621. }
  2622. base_class = base_class->base_type.class_type;
  2623. }
  2624. StringName base_native = base.native_type;
  2625. ERR_FAIL_COND_V_MSG(!class_exists(base_native), false, "Non-existent native base class.");
  2626. StringName parent = base_native;
  2627. while (parent != StringName()) {
  2628. StringName real_class_name = get_real_class_name(parent);
  2629. if (ClassDB::has_method(real_class_name, name, true)) {
  2630. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "method", parent);
  2631. return true;
  2632. } else if (ClassDB::has_signal(real_class_name, name, true)) {
  2633. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "signal", parent);
  2634. return true;
  2635. } else if (ClassDB::has_property(real_class_name, name, true)) {
  2636. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "property", parent);
  2637. return true;
  2638. } else if (ClassDB::has_integer_constant(real_class_name, name, true)) {
  2639. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "constant", parent);
  2640. return true;
  2641. } else if (ClassDB::has_enum(real_class_name, name, true)) {
  2642. parser->push_warning(p_local, GDScriptWarning::SHADOWED_VARIABLE_BASE_CLASS, p_context, p_local->name, "enum", parent);
  2643. return true;
  2644. }
  2645. parent = ClassDB::get_parent_class(real_class_name);
  2646. }
  2647. return false;
  2648. }
  2649. #endif
  2650. GDScriptParser::DataType GDScriptAnalyzer::get_operation_type(Variant::Operator p_operation, const GDScriptParser::DataType &p_a, const GDScriptParser::DataType &p_b, bool &r_valid) {
  2651. // This function creates dummy variant values and apply the operation to those. Less error-prone than keeping a table of valid operations.
  2652. GDScriptParser::DataType result;
  2653. result.kind = GDScriptParser::DataType::VARIANT;
  2654. Variant::Type a_type = p_a.builtin_type;
  2655. Variant::Type b_type = p_b.builtin_type;
  2656. Variant a;
  2657. REF a_ref;
  2658. if (a_type == Variant::OBJECT) {
  2659. a_ref.instance();
  2660. a = a_ref;
  2661. } else {
  2662. Callable::CallError err;
  2663. a = Variant::construct(a_type, nullptr, 0, err);
  2664. if (err.error != Callable::CallError::CALL_OK) {
  2665. r_valid = false;
  2666. ERR_FAIL_V_MSG(result, vformat("Could not construct value of type %s", Variant::get_type_name(a_type)));
  2667. }
  2668. }
  2669. Variant b;
  2670. REF b_ref;
  2671. if (b_type == Variant::OBJECT) {
  2672. b_ref.instance();
  2673. b = b_ref;
  2674. } else {
  2675. Callable::CallError err;
  2676. b = Variant::construct(b_type, nullptr, 0, err);
  2677. if (err.error != Callable::CallError::CALL_OK) {
  2678. r_valid = false;
  2679. ERR_FAIL_V_MSG(result, vformat("Could not construct value of type %s", Variant::get_type_name(b_type)));
  2680. }
  2681. }
  2682. // Avoid division by zero.
  2683. switch (b_type) {
  2684. case Variant::INT:
  2685. b = 1;
  2686. break;
  2687. case Variant::FLOAT:
  2688. b = 1.0;
  2689. break;
  2690. case Variant::VECTOR2:
  2691. b = Vector2(1.0, 1.0);
  2692. break;
  2693. case Variant::VECTOR2I:
  2694. b = Vector2i(1, 1);
  2695. break;
  2696. case Variant::VECTOR3:
  2697. b = Vector3(1.0, 1.0, 1.0);
  2698. break;
  2699. case Variant::VECTOR3I:
  2700. b = Vector3i(1, 1, 1);
  2701. break;
  2702. case Variant::COLOR:
  2703. b = Color(1.0, 1.0, 1.0, 1.0);
  2704. break;
  2705. default:
  2706. // No change needed.
  2707. break;
  2708. }
  2709. // Avoid error in formatting operator (%) where it doesn't find a placeholder.
  2710. if (a_type == Variant::STRING && b_type != Variant::ARRAY) {
  2711. a = String("%s");
  2712. }
  2713. Variant ret;
  2714. Variant::evaluate(p_operation, a, b, ret, r_valid);
  2715. if (r_valid) {
  2716. return type_from_variant(ret);
  2717. }
  2718. return result;
  2719. }
  2720. // TODO: Add safe/unsafe return variable (for variant cases)
  2721. bool GDScriptAnalyzer::is_type_compatible(const GDScriptParser::DataType &p_target, const GDScriptParser::DataType &p_source, bool p_allow_implicit_conversion) const {
  2722. // These return "true" so it doesn't affect users negatively.
  2723. ERR_FAIL_COND_V_MSG(!p_target.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset target type");
  2724. ERR_FAIL_COND_V_MSG(!p_source.is_set(), true, "Parser bug (please report): Trying to check compatibility of unset value type");
  2725. if (p_target.kind == GDScriptParser::DataType::VARIANT) {
  2726. // Variant can receive anything.
  2727. return true;
  2728. }
  2729. if (p_source.kind == GDScriptParser::DataType::VARIANT) {
  2730. // TODO: This is acceptable but unsafe. Make sure unsafe line is set.
  2731. return true;
  2732. }
  2733. if (p_target.kind == GDScriptParser::DataType::BUILTIN) {
  2734. bool valid = p_source.kind == GDScriptParser::DataType::BUILTIN && p_target.builtin_type == p_source.builtin_type;
  2735. if (!valid && p_allow_implicit_conversion) {
  2736. valid = Variant::can_convert_strict(p_source.builtin_type, p_target.builtin_type);
  2737. }
  2738. if (!valid && p_target.builtin_type == Variant::INT && p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  2739. // Enum value is also integer.
  2740. valid = true;
  2741. }
  2742. return valid;
  2743. }
  2744. if (p_target.kind == GDScriptParser::DataType::ENUM) {
  2745. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::INT) {
  2746. return true;
  2747. }
  2748. if (p_source.kind == GDScriptParser::DataType::ENUM_VALUE) {
  2749. if (p_source.native_type == p_target.native_type && p_target.enum_values.has(p_source.enum_type)) {
  2750. return true;
  2751. }
  2752. }
  2753. return false;
  2754. }
  2755. // From here on the target type is an object, so we have to test polymorphism.
  2756. if (p_source.kind == GDScriptParser::DataType::BUILTIN && p_source.builtin_type == Variant::NIL) {
  2757. // null is acceptable in object.
  2758. return true;
  2759. }
  2760. StringName src_native;
  2761. Ref<Script> src_script;
  2762. const GDScriptParser::ClassNode *src_class = nullptr;
  2763. switch (p_source.kind) {
  2764. case GDScriptParser::DataType::NATIVE:
  2765. if (p_target.kind != GDScriptParser::DataType::NATIVE) {
  2766. // Non-native class cannot be supertype of native.
  2767. return false;
  2768. }
  2769. if (p_source.is_meta_type) {
  2770. src_native = GDScriptNativeClass::get_class_static();
  2771. } else {
  2772. src_native = p_source.native_type;
  2773. }
  2774. break;
  2775. case GDScriptParser::DataType::SCRIPT:
  2776. if (p_target.kind == GDScriptParser::DataType::CLASS) {
  2777. // A script type cannot be a subtype of a GDScript class.
  2778. return false;
  2779. }
  2780. if (p_source.is_meta_type) {
  2781. src_native = p_source.script_type->get_class_name();
  2782. } else {
  2783. src_script = p_source.script_type;
  2784. src_native = src_script->get_instance_base_type();
  2785. }
  2786. break;
  2787. case GDScriptParser::DataType::CLASS:
  2788. if (p_source.is_meta_type) {
  2789. src_native = GDScript::get_class_static();
  2790. } else {
  2791. src_class = p_source.class_type;
  2792. const GDScriptParser::ClassNode *base = src_class;
  2793. while (base->base_type.kind == GDScriptParser::DataType::CLASS) {
  2794. base = base->base_type.class_type;
  2795. }
  2796. src_native = base->base_type.native_type;
  2797. src_script = base->base_type.script_type;
  2798. }
  2799. break;
  2800. case GDScriptParser::DataType::VARIANT:
  2801. case GDScriptParser::DataType::BUILTIN:
  2802. case GDScriptParser::DataType::ENUM:
  2803. case GDScriptParser::DataType::ENUM_VALUE:
  2804. case GDScriptParser::DataType::UNRESOLVED:
  2805. break; // Already solved before.
  2806. }
  2807. // Get underscore-prefixed version for some classes.
  2808. src_native = get_real_class_name(src_native);
  2809. switch (p_target.kind) {
  2810. case GDScriptParser::DataType::NATIVE: {
  2811. if (p_target.is_meta_type) {
  2812. return ClassDB::is_parent_class(src_native, GDScriptNativeClass::get_class_static());
  2813. }
  2814. StringName tgt_native = get_real_class_name(p_target.native_type);
  2815. return ClassDB::is_parent_class(src_native, tgt_native);
  2816. }
  2817. case GDScriptParser::DataType::SCRIPT:
  2818. if (p_target.is_meta_type) {
  2819. return ClassDB::is_parent_class(src_native, p_target.script_type->get_class_name());
  2820. }
  2821. while (src_script.is_valid()) {
  2822. if (src_script == p_target.script_type) {
  2823. return true;
  2824. }
  2825. src_script = src_script->get_base_script();
  2826. }
  2827. return false;
  2828. case GDScriptParser::DataType::CLASS:
  2829. if (p_target.is_meta_type) {
  2830. return ClassDB::is_parent_class(src_native, GDScript::get_class_static());
  2831. }
  2832. while (src_class != nullptr) {
  2833. if (src_class->fqcn == p_target.class_type->fqcn) {
  2834. return true;
  2835. }
  2836. src_class = src_class->base_type.class_type;
  2837. }
  2838. return false;
  2839. case GDScriptParser::DataType::VARIANT:
  2840. case GDScriptParser::DataType::BUILTIN:
  2841. case GDScriptParser::DataType::ENUM:
  2842. case GDScriptParser::DataType::ENUM_VALUE:
  2843. case GDScriptParser::DataType::UNRESOLVED:
  2844. break; // Already solved before.
  2845. }
  2846. return false;
  2847. }
  2848. void GDScriptAnalyzer::push_error(const String &p_message, const GDScriptParser::Node *p_origin) {
  2849. mark_node_unsafe(p_origin);
  2850. parser->push_error(p_message, p_origin);
  2851. }
  2852. void GDScriptAnalyzer::mark_node_unsafe(const GDScriptParser::Node *p_node) {
  2853. #ifdef DEBUG_ENABLED
  2854. for (int i = p_node->start_line; i <= p_node->end_line; i++) {
  2855. parser->unsafe_lines.insert(i);
  2856. }
  2857. #endif
  2858. }
  2859. bool GDScriptAnalyzer::class_exists(const StringName &p_class) {
  2860. StringName real_name = get_real_class_name(p_class);
  2861. return ClassDB::class_exists(real_name) && ClassDB::is_class_exposed(real_name);
  2862. }
  2863. Ref<GDScriptParserRef> GDScriptAnalyzer::get_parser_for(const String &p_path) {
  2864. Ref<GDScriptParserRef> ref;
  2865. if (depended_parsers.has(p_path)) {
  2866. ref = depended_parsers[p_path];
  2867. } else {
  2868. Error err = OK;
  2869. ref = GDScriptCache::get_parser(p_path, GDScriptParserRef::EMPTY, err, parser->script_path);
  2870. depended_parsers[p_path] = ref;
  2871. }
  2872. return ref;
  2873. }
  2874. Error GDScriptAnalyzer::resolve_inheritance() {
  2875. return resolve_inheritance(parser->head);
  2876. }
  2877. Error GDScriptAnalyzer::resolve_interface() {
  2878. resolve_class_interface(parser->head);
  2879. return parser->errors.empty() ? OK : ERR_PARSE_ERROR;
  2880. }
  2881. Error GDScriptAnalyzer::resolve_body() {
  2882. resolve_class_body(parser->head);
  2883. return parser->errors.empty() ? OK : ERR_PARSE_ERROR;
  2884. }
  2885. Error GDScriptAnalyzer::resolve_program() {
  2886. resolve_class_interface(parser->head);
  2887. resolve_class_body(parser->head);
  2888. List<String> parser_keys;
  2889. depended_parsers.get_key_list(&parser_keys);
  2890. for (const List<String>::Element *E = parser_keys.front(); E != nullptr; E = E->next()) {
  2891. if (depended_parsers[E->get()].is_null()) {
  2892. return ERR_PARSE_ERROR;
  2893. }
  2894. depended_parsers[E->get()]->raise_status(GDScriptParserRef::FULLY_SOLVED);
  2895. }
  2896. depended_parsers.clear();
  2897. return parser->errors.empty() ? OK : ERR_PARSE_ERROR;
  2898. }
  2899. Error GDScriptAnalyzer::analyze() {
  2900. parser->errors.clear();
  2901. Error err = resolve_inheritance(parser->head);
  2902. if (err) {
  2903. return err;
  2904. }
  2905. return resolve_program();
  2906. }
  2907. GDScriptAnalyzer::GDScriptAnalyzer(GDScriptParser *p_parser) {
  2908. parser = p_parser;
  2909. }