gdscript_analyzer.cpp 117 KB

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