gdscript_analyzer.cpp 127 KB

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