gdscript_analyzer.cpp 127 KB

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