gdscript_analyzer.cpp 123 KB

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