gdscript_analyzer.cpp 138 KB

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