gdscript_analyzer.cpp 122 KB

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