2
0

gdscript_analyzer.cpp 117 KB

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