2
0

gdscript_analyzer.cpp 118 KB

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