gdscript_analyzer.cpp 115 KB

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