gdscript_analyzer.cpp 122 KB

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