gdscript_analyzer.cpp 115 KB

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