gdscript_analyzer.cpp 156 KB

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