gdscript_analyzer.cpp 121 KB

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