gdscript_analyzer.cpp 116 KB

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