gdscript_analyzer.cpp 117 KB

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