gdscript_analyzer.cpp 173 KB

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