gdscript_editor.cpp 130 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669
  1. /**************************************************************************/
  2. /* gdscript_editor.cpp */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  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.h"
  31. #include "gdscript_analyzer.h"
  32. #include "gdscript_compiler.h"
  33. #include "gdscript_parser.h"
  34. #include "gdscript_tokenizer.h"
  35. #include "gdscript_utility_functions.h"
  36. #ifdef TOOLS_ENABLED
  37. #include "editor/script_templates/templates.gen.h"
  38. #endif
  39. #include "core/config/engine.h"
  40. #include "core/core_constants.h"
  41. #include "core/io/file_access.h"
  42. #ifdef TOOLS_ENABLED
  43. #include "core/config/project_settings.h"
  44. #include "editor/editor_file_system.h"
  45. #include "editor/editor_settings.h"
  46. #endif
  47. void GDScriptLanguage::get_comment_delimiters(List<String> *p_delimiters) const {
  48. p_delimiters->push_back("#");
  49. }
  50. void GDScriptLanguage::get_doc_comment_delimiters(List<String> *p_delimiters) const {
  51. p_delimiters->push_back("##");
  52. }
  53. void GDScriptLanguage::get_string_delimiters(List<String> *p_delimiters) const {
  54. p_delimiters->push_back("\" \"");
  55. p_delimiters->push_back("' '");
  56. p_delimiters->push_back("\"\"\" \"\"\"");
  57. p_delimiters->push_back("''' '''");
  58. // NOTE: StringName, NodePath and r-strings are not listed here.
  59. }
  60. bool GDScriptLanguage::is_using_templates() {
  61. return true;
  62. }
  63. Ref<Script> GDScriptLanguage::make_template(const String &p_template, const String &p_class_name, const String &p_base_class_name) const {
  64. Ref<GDScript> scr;
  65. scr.instantiate();
  66. String processed_template = p_template;
  67. bool type_hints = false;
  68. #ifdef TOOLS_ENABLED
  69. type_hints = EDITOR_GET("text_editor/completion/add_type_hints");
  70. #endif
  71. if (!type_hints) {
  72. processed_template = processed_template.replace(": int", "")
  73. .replace(": Shader.Mode", "")
  74. .replace(": VisualShader.Type", "")
  75. .replace(": float", "")
  76. .replace(": String", "")
  77. .replace(": Array[String]", "")
  78. .replace(": Node", "")
  79. .replace(": CharFXTransform", "")
  80. .replace(":=", "=")
  81. .replace(" -> void", "")
  82. .replace(" -> bool", "")
  83. .replace(" -> int", "")
  84. .replace(" -> PortType", "")
  85. .replace(" -> String", "")
  86. .replace(" -> Object", "");
  87. }
  88. processed_template = processed_template.replace("_BASE_", p_base_class_name)
  89. .replace("_CLASS_SNAKE_CASE_", p_class_name.to_snake_case().validate_identifier())
  90. .replace("_CLASS_", p_class_name.to_pascal_case().validate_identifier())
  91. .replace("_TS_", _get_indentation());
  92. scr->set_source_code(processed_template);
  93. return scr;
  94. }
  95. Vector<ScriptLanguage::ScriptTemplate> GDScriptLanguage::get_built_in_templates(StringName p_object) {
  96. Vector<ScriptLanguage::ScriptTemplate> templates;
  97. #ifdef TOOLS_ENABLED
  98. for (int i = 0; i < TEMPLATES_ARRAY_SIZE; i++) {
  99. if (TEMPLATES[i].inherit == p_object) {
  100. templates.append(TEMPLATES[i]);
  101. }
  102. }
  103. #endif
  104. return templates;
  105. }
  106. static void get_function_names_recursively(const GDScriptParser::ClassNode *p_class, const String &p_prefix, HashMap<int, String> &r_funcs) {
  107. for (int i = 0; i < p_class->members.size(); i++) {
  108. if (p_class->members[i].type == GDScriptParser::ClassNode::Member::FUNCTION) {
  109. const GDScriptParser::FunctionNode *function = p_class->members[i].function;
  110. r_funcs[function->start_line] = p_prefix.is_empty() ? String(function->identifier->name) : p_prefix + "." + String(function->identifier->name);
  111. } else if (p_class->members[i].type == GDScriptParser::ClassNode::Member::CLASS) {
  112. String new_prefix = p_class->members[i].m_class->identifier->name;
  113. get_function_names_recursively(p_class->members[i].m_class, p_prefix.is_empty() ? new_prefix : p_prefix + "." + new_prefix, r_funcs);
  114. }
  115. }
  116. }
  117. bool GDScriptLanguage::validate(const String &p_script, const String &p_path, List<String> *r_functions, List<ScriptLanguage::ScriptError> *r_errors, List<ScriptLanguage::Warning> *r_warnings, HashSet<int> *r_safe_lines) const {
  118. GDScriptParser parser;
  119. GDScriptAnalyzer analyzer(&parser);
  120. Error err = parser.parse(p_script, p_path, false);
  121. if (err == OK) {
  122. err = analyzer.analyze();
  123. }
  124. #ifdef DEBUG_ENABLED
  125. if (r_warnings) {
  126. for (const GDScriptWarning &E : parser.get_warnings()) {
  127. const GDScriptWarning &warn = E;
  128. ScriptLanguage::Warning w;
  129. w.start_line = warn.start_line;
  130. w.end_line = warn.end_line;
  131. w.leftmost_column = warn.leftmost_column;
  132. w.rightmost_column = warn.rightmost_column;
  133. w.code = (int)warn.code;
  134. w.string_code = GDScriptWarning::get_name_from_code(warn.code);
  135. w.message = warn.get_message();
  136. r_warnings->push_back(w);
  137. }
  138. }
  139. #endif
  140. if (err) {
  141. if (r_errors) {
  142. for (const GDScriptParser::ParserError &pe : parser.get_errors()) {
  143. ScriptLanguage::ScriptError e;
  144. e.path = p_path;
  145. e.line = pe.line;
  146. e.column = pe.column;
  147. e.message = pe.message;
  148. r_errors->push_back(e);
  149. }
  150. for (KeyValue<String, Ref<GDScriptParserRef>> E : analyzer.get_depended_parsers()) {
  151. GDScriptParser *depended_parser = E.value->get_parser();
  152. for (const GDScriptParser::ParserError &pe : depended_parser->get_errors()) {
  153. ScriptLanguage::ScriptError e;
  154. e.path = E.key;
  155. e.line = pe.line;
  156. e.column = pe.column;
  157. e.message = pe.message;
  158. r_errors->push_back(e);
  159. }
  160. }
  161. }
  162. return false;
  163. } else {
  164. const GDScriptParser::ClassNode *cl = parser.get_tree();
  165. HashMap<int, String> funcs;
  166. get_function_names_recursively(cl, "", funcs);
  167. for (const KeyValue<int, String> &E : funcs) {
  168. r_functions->push_back(E.value + ":" + itos(E.key));
  169. }
  170. }
  171. #ifdef DEBUG_ENABLED
  172. if (r_safe_lines) {
  173. const HashSet<int> &unsafe_lines = parser.get_unsafe_lines();
  174. for (int i = 1; i <= parser.get_last_line_number(); i++) {
  175. if (!unsafe_lines.has(i)) {
  176. r_safe_lines->insert(i);
  177. }
  178. }
  179. }
  180. #endif
  181. return true;
  182. }
  183. bool GDScriptLanguage::supports_builtin_mode() const {
  184. return true;
  185. }
  186. bool GDScriptLanguage::supports_documentation() const {
  187. return true;
  188. }
  189. int GDScriptLanguage::find_function(const String &p_function, const String &p_code) const {
  190. GDScriptTokenizer tokenizer;
  191. tokenizer.set_source_code(p_code);
  192. int indent = 0;
  193. GDScriptTokenizer::Token current = tokenizer.scan();
  194. while (current.type != GDScriptTokenizer::Token::TK_EOF && current.type != GDScriptTokenizer::Token::ERROR) {
  195. if (current.type == GDScriptTokenizer::Token::INDENT) {
  196. indent++;
  197. } else if (current.type == GDScriptTokenizer::Token::DEDENT) {
  198. indent--;
  199. }
  200. if (indent == 0 && current.type == GDScriptTokenizer::Token::FUNC) {
  201. current = tokenizer.scan();
  202. if (current.is_identifier()) {
  203. String identifier = current.get_identifier();
  204. if (identifier == p_function) {
  205. return current.start_line;
  206. }
  207. }
  208. }
  209. current = tokenizer.scan();
  210. }
  211. return -1;
  212. }
  213. Script *GDScriptLanguage::create_script() const {
  214. return memnew(GDScript);
  215. }
  216. /* DEBUGGER FUNCTIONS */
  217. thread_local int GDScriptLanguage::_debug_parse_err_line = -1;
  218. thread_local String GDScriptLanguage::_debug_parse_err_file;
  219. thread_local String GDScriptLanguage::_debug_error;
  220. bool GDScriptLanguage::debug_break_parse(const String &p_file, int p_line, const String &p_error) {
  221. // break because of parse error
  222. if (EngineDebugger::is_active() && Thread::get_caller_id() == Thread::get_main_id()) {
  223. _debug_parse_err_line = p_line;
  224. _debug_parse_err_file = p_file;
  225. _debug_error = p_error;
  226. EngineDebugger::get_script_debugger()->debug(this, false, true);
  227. // Because this is thread local, clear the memory afterwards.
  228. _debug_parse_err_file = String();
  229. _debug_error = String();
  230. return true;
  231. } else {
  232. return false;
  233. }
  234. }
  235. bool GDScriptLanguage::debug_break(const String &p_error, bool p_allow_continue) {
  236. if (EngineDebugger::is_active()) {
  237. _debug_parse_err_line = -1;
  238. _debug_parse_err_file = "";
  239. _debug_error = p_error;
  240. bool is_error_breakpoint = p_error != "Breakpoint";
  241. EngineDebugger::get_script_debugger()->debug(this, p_allow_continue, is_error_breakpoint);
  242. // Because this is thread local, clear the memory afterwards.
  243. _debug_parse_err_file = String();
  244. _debug_error = String();
  245. return true;
  246. } else {
  247. return false;
  248. }
  249. }
  250. String GDScriptLanguage::debug_get_error() const {
  251. return _debug_error;
  252. }
  253. int GDScriptLanguage::debug_get_stack_level_count() const {
  254. if (_debug_parse_err_line >= 0) {
  255. return 1;
  256. }
  257. return _call_stack.stack_pos;
  258. }
  259. int GDScriptLanguage::debug_get_stack_level_line(int p_level) const {
  260. if (_debug_parse_err_line >= 0) {
  261. return _debug_parse_err_line;
  262. }
  263. ERR_FAIL_INDEX_V(p_level, _call_stack.stack_pos, -1);
  264. int l = _call_stack.stack_pos - p_level - 1;
  265. return *(_call_stack.levels[l].line);
  266. }
  267. String GDScriptLanguage::debug_get_stack_level_function(int p_level) const {
  268. if (_debug_parse_err_line >= 0) {
  269. return "";
  270. }
  271. ERR_FAIL_INDEX_V(p_level, _call_stack.stack_pos, "");
  272. int l = _call_stack.stack_pos - p_level - 1;
  273. return _call_stack.levels[l].function->get_name();
  274. }
  275. String GDScriptLanguage::debug_get_stack_level_source(int p_level) const {
  276. if (_debug_parse_err_line >= 0) {
  277. return _debug_parse_err_file;
  278. }
  279. ERR_FAIL_INDEX_V(p_level, _call_stack.stack_pos, "");
  280. int l = _call_stack.stack_pos - p_level - 1;
  281. return _call_stack.levels[l].function->get_source();
  282. }
  283. void GDScriptLanguage::debug_get_stack_level_locals(int p_level, List<String> *p_locals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  284. if (_debug_parse_err_line >= 0) {
  285. return;
  286. }
  287. ERR_FAIL_INDEX(p_level, _call_stack.stack_pos);
  288. int l = _call_stack.stack_pos - p_level - 1;
  289. GDScriptFunction *f = _call_stack.levels[l].function;
  290. List<Pair<StringName, int>> locals;
  291. f->debug_get_stack_member_state(*_call_stack.levels[l].line, &locals);
  292. for (const Pair<StringName, int> &E : locals) {
  293. p_locals->push_back(E.first);
  294. p_values->push_back(_call_stack.levels[l].stack[E.second]);
  295. }
  296. }
  297. void GDScriptLanguage::debug_get_stack_level_members(int p_level, List<String> *p_members, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  298. if (_debug_parse_err_line >= 0) {
  299. return;
  300. }
  301. ERR_FAIL_INDEX(p_level, _call_stack.stack_pos);
  302. int l = _call_stack.stack_pos - p_level - 1;
  303. GDScriptInstance *instance = _call_stack.levels[l].instance;
  304. if (!instance) {
  305. return;
  306. }
  307. Ref<GDScript> scr = instance->get_script();
  308. ERR_FAIL_COND(scr.is_null());
  309. const HashMap<StringName, GDScript::MemberInfo> &mi = scr->debug_get_member_indices();
  310. for (const KeyValue<StringName, GDScript::MemberInfo> &E : mi) {
  311. p_members->push_back(E.key);
  312. p_values->push_back(instance->debug_get_member_by_index(E.value.index));
  313. }
  314. }
  315. ScriptInstance *GDScriptLanguage::debug_get_stack_level_instance(int p_level) {
  316. if (_debug_parse_err_line >= 0) {
  317. return nullptr;
  318. }
  319. ERR_FAIL_INDEX_V(p_level, _call_stack.stack_pos, nullptr);
  320. int l = _call_stack.stack_pos - p_level - 1;
  321. ScriptInstance *instance = _call_stack.levels[l].instance;
  322. return instance;
  323. }
  324. void GDScriptLanguage::debug_get_globals(List<String> *p_globals, List<Variant> *p_values, int p_max_subitems, int p_max_depth) {
  325. const HashMap<StringName, int> &name_idx = GDScriptLanguage::get_singleton()->get_global_map();
  326. const Variant *gl_array = GDScriptLanguage::get_singleton()->get_global_array();
  327. List<Pair<String, Variant>> cinfo;
  328. get_public_constants(&cinfo);
  329. for (const KeyValue<StringName, int> &E : name_idx) {
  330. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  331. continue;
  332. }
  333. bool is_script_constant = false;
  334. for (List<Pair<String, Variant>>::Element *CE = cinfo.front(); CE; CE = CE->next()) {
  335. if (CE->get().first == E.key) {
  336. is_script_constant = true;
  337. break;
  338. }
  339. }
  340. if (is_script_constant) {
  341. continue;
  342. }
  343. const Variant &var = gl_array[E.value];
  344. if (Object *obj = var) {
  345. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  346. continue;
  347. }
  348. }
  349. bool skip = false;
  350. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  351. if (E.key == CoreConstants::get_global_constant_name(i)) {
  352. skip = true;
  353. break;
  354. }
  355. }
  356. if (skip) {
  357. continue;
  358. }
  359. p_globals->push_back(E.key);
  360. p_values->push_back(var);
  361. }
  362. }
  363. String GDScriptLanguage::debug_parse_stack_level_expression(int p_level, const String &p_expression, int p_max_subitems, int p_max_depth) {
  364. return "";
  365. }
  366. void GDScriptLanguage::get_recognized_extensions(List<String> *p_extensions) const {
  367. p_extensions->push_back("gd");
  368. }
  369. void GDScriptLanguage::get_public_functions(List<MethodInfo> *p_functions) const {
  370. List<StringName> functions;
  371. GDScriptUtilityFunctions::get_function_list(&functions);
  372. for (const StringName &E : functions) {
  373. p_functions->push_back(GDScriptUtilityFunctions::get_function_info(E));
  374. }
  375. // Not really "functions", but show in documentation.
  376. {
  377. MethodInfo mi;
  378. mi.name = "preload";
  379. mi.arguments.push_back(PropertyInfo(Variant::STRING, "path"));
  380. mi.return_val = PropertyInfo(Variant::OBJECT, "", PROPERTY_HINT_RESOURCE_TYPE, "Resource");
  381. p_functions->push_back(mi);
  382. }
  383. {
  384. MethodInfo mi;
  385. mi.name = "assert";
  386. mi.return_val.type = Variant::NIL;
  387. mi.arguments.push_back(PropertyInfo(Variant::BOOL, "condition"));
  388. mi.arguments.push_back(PropertyInfo(Variant::STRING, "message"));
  389. mi.default_arguments.push_back(String());
  390. p_functions->push_back(mi);
  391. }
  392. }
  393. void GDScriptLanguage::get_public_constants(List<Pair<String, Variant>> *p_constants) const {
  394. Pair<String, Variant> pi;
  395. pi.first = "PI";
  396. pi.second = Math_PI;
  397. p_constants->push_back(pi);
  398. Pair<String, Variant> tau;
  399. tau.first = "TAU";
  400. tau.second = Math_TAU;
  401. p_constants->push_back(tau);
  402. Pair<String, Variant> infinity;
  403. infinity.first = "INF";
  404. infinity.second = INFINITY;
  405. p_constants->push_back(infinity);
  406. Pair<String, Variant> nan;
  407. nan.first = "NAN";
  408. nan.second = NAN;
  409. p_constants->push_back(nan);
  410. }
  411. void GDScriptLanguage::get_public_annotations(List<MethodInfo> *p_annotations) const {
  412. GDScriptParser parser;
  413. List<MethodInfo> annotations;
  414. parser.get_annotation_list(&annotations);
  415. for (const MethodInfo &E : annotations) {
  416. p_annotations->push_back(E);
  417. }
  418. }
  419. String GDScriptLanguage::make_function(const String &p_class, const String &p_name, const PackedStringArray &p_args) const {
  420. #ifdef TOOLS_ENABLED
  421. bool th = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints");
  422. #else
  423. bool th = false;
  424. #endif
  425. String s = "func " + p_name + "(";
  426. if (p_args.size()) {
  427. for (int i = 0; i < p_args.size(); i++) {
  428. if (i > 0) {
  429. s += ", ";
  430. }
  431. s += p_args[i].get_slice(":", 0);
  432. if (th) {
  433. String type = p_args[i].get_slice(":", 1);
  434. if (!type.is_empty()) {
  435. s += ": " + type;
  436. }
  437. }
  438. }
  439. }
  440. s += String(")") + (th ? " -> void" : "") + ":\n" + _get_indentation() + "pass # Replace with function body.\n";
  441. return s;
  442. }
  443. //////// COMPLETION //////////
  444. #ifdef TOOLS_ENABLED
  445. #define COMPLETION_RECURSION_LIMIT 200
  446. struct GDScriptCompletionIdentifier {
  447. GDScriptParser::DataType type;
  448. String enumeration;
  449. Variant value;
  450. const GDScriptParser::ExpressionNode *assigned_expression = nullptr;
  451. };
  452. // LOCATION METHODS
  453. // These methods are used to populate the `CodeCompletionOption::location` integer.
  454. // For these methods, the location is based on the depth in the inheritance chain that the property
  455. // appears. For example, if you are completing code in a class that inherits Node2D, a property found on Node2D
  456. // will have a "better" (lower) location "score" than a property that is found on CanvasItem.
  457. static int _get_property_location(StringName p_class, StringName p_property) {
  458. if (!ClassDB::has_property(p_class, p_property)) {
  459. return ScriptLanguage::LOCATION_OTHER;
  460. }
  461. int depth = 0;
  462. StringName class_test = p_class;
  463. while (class_test && !ClassDB::has_property(class_test, p_property, true)) {
  464. class_test = ClassDB::get_parent_class(class_test);
  465. depth++;
  466. }
  467. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  468. }
  469. static int _get_constant_location(StringName p_class, StringName p_constant) {
  470. if (!ClassDB::has_integer_constant(p_class, p_constant)) {
  471. return ScriptLanguage::LOCATION_OTHER;
  472. }
  473. int depth = 0;
  474. StringName class_test = p_class;
  475. while (class_test && !ClassDB::has_integer_constant(class_test, p_constant, true)) {
  476. class_test = ClassDB::get_parent_class(class_test);
  477. depth++;
  478. }
  479. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  480. }
  481. static int _get_signal_location(StringName p_class, StringName p_signal) {
  482. if (!ClassDB::has_signal(p_class, p_signal)) {
  483. return ScriptLanguage::LOCATION_OTHER;
  484. }
  485. int depth = 0;
  486. StringName class_test = p_class;
  487. while (class_test && !ClassDB::has_signal(class_test, p_signal, true)) {
  488. class_test = ClassDB::get_parent_class(class_test);
  489. depth++;
  490. }
  491. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  492. }
  493. static int _get_method_location(StringName p_class, StringName p_method) {
  494. if (!ClassDB::has_method(p_class, p_method)) {
  495. return ScriptLanguage::LOCATION_OTHER;
  496. }
  497. int depth = 0;
  498. StringName class_test = p_class;
  499. while (class_test && !ClassDB::has_method(class_test, p_method, true)) {
  500. class_test = ClassDB::get_parent_class(class_test);
  501. depth++;
  502. }
  503. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  504. }
  505. static int _get_enum_constant_location(StringName p_class, StringName p_enum_constant) {
  506. if (!ClassDB::get_integer_constant_enum(p_class, p_enum_constant)) {
  507. return ScriptLanguage::LOCATION_OTHER;
  508. }
  509. int depth = 0;
  510. StringName class_test = p_class;
  511. while (class_test && !ClassDB::get_integer_constant_enum(class_test, p_enum_constant, true)) {
  512. class_test = ClassDB::get_parent_class(class_test);
  513. depth++;
  514. }
  515. return depth | ScriptLanguage::LOCATION_PARENT_MASK;
  516. }
  517. // END LOCATION METHODS
  518. static String _trim_parent_class(const String &p_class, const String &p_base_class) {
  519. if (p_base_class.is_empty()) {
  520. return p_class;
  521. }
  522. Vector<String> names = p_class.split(".", false, 1);
  523. if (names.size() == 2) {
  524. String first = names[0];
  525. String rest = names[1];
  526. if (ClassDB::class_exists(p_base_class) && ClassDB::class_exists(first) && ClassDB::is_parent_class(p_base_class, first)) {
  527. return rest;
  528. }
  529. }
  530. return p_class;
  531. }
  532. static String _get_visual_datatype(const PropertyInfo &p_info, bool p_is_arg, const String &p_base_class = "") {
  533. String class_name = p_info.class_name;
  534. bool is_enum = p_info.type == Variant::INT && p_info.usage & PROPERTY_USAGE_CLASS_IS_ENUM;
  535. // PROPERTY_USAGE_CLASS_IS_BITFIELD: BitField[T] isn't supported (yet?), use plain int.
  536. if ((p_info.type == Variant::OBJECT || is_enum) && !class_name.is_empty()) {
  537. if (is_enum && CoreConstants::is_global_enum(p_info.class_name)) {
  538. return class_name;
  539. }
  540. return _trim_parent_class(class_name, p_base_class);
  541. } else if (p_info.type == Variant::ARRAY && p_info.hint == PROPERTY_HINT_ARRAY_TYPE && !p_info.hint_string.is_empty()) {
  542. return "Array[" + _trim_parent_class(p_info.hint_string, p_base_class) + "]";
  543. } else if (p_info.type == Variant::NIL) {
  544. if (p_is_arg || (p_info.usage & PROPERTY_USAGE_NIL_IS_VARIANT)) {
  545. return "Variant";
  546. } else {
  547. return "void";
  548. }
  549. }
  550. return Variant::get_type_name(p_info.type);
  551. }
  552. static String _make_arguments_hint(const MethodInfo &p_info, int p_arg_idx, bool p_is_annotation = false) {
  553. String arghint;
  554. if (!p_is_annotation) {
  555. arghint += _get_visual_datatype(p_info.return_val, false) + " ";
  556. }
  557. arghint += p_info.name + "(";
  558. int def_args = p_info.arguments.size() - p_info.default_arguments.size();
  559. int i = 0;
  560. for (const PropertyInfo &E : p_info.arguments) {
  561. if (i > 0) {
  562. arghint += ", ";
  563. }
  564. if (i == p_arg_idx) {
  565. arghint += String::chr(0xFFFF);
  566. }
  567. arghint += E.name + ": " + _get_visual_datatype(E, true);
  568. if (i - def_args >= 0) {
  569. arghint += String(" = ") + p_info.default_arguments[i - def_args].get_construct_string();
  570. }
  571. if (i == p_arg_idx) {
  572. arghint += String::chr(0xFFFF);
  573. }
  574. i++;
  575. }
  576. if (p_info.flags & METHOD_FLAG_VARARG) {
  577. if (p_info.arguments.size() > 0) {
  578. arghint += ", ";
  579. }
  580. if (p_arg_idx >= p_info.arguments.size()) {
  581. arghint += String::chr(0xFFFF);
  582. }
  583. arghint += "...";
  584. if (p_arg_idx >= p_info.arguments.size()) {
  585. arghint += String::chr(0xFFFF);
  586. }
  587. }
  588. arghint += ")";
  589. return arghint;
  590. }
  591. static String _make_arguments_hint(const GDScriptParser::FunctionNode *p_function, int p_arg_idx) {
  592. String arghint;
  593. if (p_function->get_datatype().builtin_type == Variant::NIL) {
  594. arghint = "void " + p_function->identifier->name.operator String() + "(";
  595. } else {
  596. arghint = p_function->get_datatype().to_string() + " " + p_function->identifier->name.operator String() + "(";
  597. }
  598. for (int i = 0; i < p_function->parameters.size(); i++) {
  599. if (i > 0) {
  600. arghint += ", ";
  601. }
  602. if (i == p_arg_idx) {
  603. arghint += String::chr(0xFFFF);
  604. }
  605. const GDScriptParser::ParameterNode *par = p_function->parameters[i];
  606. if (!par->get_datatype().is_hard_type()) {
  607. arghint += par->identifier->name.operator String() + ": Variant";
  608. } else {
  609. arghint += par->identifier->name.operator String() + ": " + par->get_datatype().to_string();
  610. }
  611. if (par->initializer) {
  612. String def_val = "<unknown>";
  613. switch (par->initializer->type) {
  614. case GDScriptParser::Node::LITERAL: {
  615. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(par->initializer);
  616. def_val = literal->value.get_construct_string();
  617. } break;
  618. case GDScriptParser::Node::IDENTIFIER: {
  619. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(par->initializer);
  620. def_val = id->name.operator String();
  621. } break;
  622. case GDScriptParser::Node::CALL: {
  623. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(par->initializer);
  624. if (call->is_constant && call->reduced) {
  625. def_val = call->function_name.operator String() + call->reduced_value.operator String();
  626. }
  627. } break;
  628. case GDScriptParser::Node::ARRAY: {
  629. const GDScriptParser::ArrayNode *arr = static_cast<const GDScriptParser::ArrayNode *>(par->initializer);
  630. if (arr->is_constant && arr->reduced) {
  631. def_val = arr->reduced_value.operator String();
  632. }
  633. } break;
  634. case GDScriptParser::Node::DICTIONARY: {
  635. const GDScriptParser::DictionaryNode *dict = static_cast<const GDScriptParser::DictionaryNode *>(par->initializer);
  636. if (dict->is_constant && dict->reduced) {
  637. def_val = dict->reduced_value.operator String();
  638. }
  639. } break;
  640. case GDScriptParser::Node::SUBSCRIPT: {
  641. const GDScriptParser::SubscriptNode *sub = static_cast<const GDScriptParser::SubscriptNode *>(par->initializer);
  642. if (sub->is_constant) {
  643. if (sub->datatype.kind == GDScriptParser::DataType::ENUM) {
  644. def_val = sub->get_datatype().to_string();
  645. } else if (sub->reduced) {
  646. const Variant::Type vt = sub->reduced_value.get_type();
  647. if (vt == Variant::Type::NIL || vt == Variant::Type::FLOAT || vt == Variant::Type::INT || vt == Variant::Type::STRING || vt == Variant::Type::STRING_NAME || vt == Variant::Type::BOOL || vt == Variant::Type::NODE_PATH) {
  648. def_val = sub->reduced_value.operator String();
  649. } else {
  650. def_val = sub->get_datatype().to_string() + sub->reduced_value.operator String();
  651. }
  652. }
  653. }
  654. } break;
  655. default:
  656. break;
  657. }
  658. arghint += " = " + def_val;
  659. }
  660. if (i == p_arg_idx) {
  661. arghint += String::chr(0xFFFF);
  662. }
  663. }
  664. arghint += ")";
  665. return arghint;
  666. }
  667. static void _get_directory_contents(EditorFileSystemDirectory *p_dir, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_list) {
  668. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  669. for (int i = 0; i < p_dir->get_file_count(); i++) {
  670. ScriptLanguage::CodeCompletionOption option(p_dir->get_file_path(i), ScriptLanguage::CODE_COMPLETION_KIND_FILE_PATH);
  671. option.insert_text = option.display.quote(quote_style);
  672. r_list.insert(option.display, option);
  673. }
  674. for (int i = 0; i < p_dir->get_subdir_count(); i++) {
  675. _get_directory_contents(p_dir->get_subdir(i), r_list);
  676. }
  677. }
  678. static void _find_annotation_arguments(const GDScriptParser::AnnotationNode *p_annotation, int p_argument, const String p_quote_style, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  679. if (p_annotation->name == SNAME("@export_range")) {
  680. if (p_argument == 3 || p_argument == 4 || p_argument == 5) {
  681. // Slider hint.
  682. ScriptLanguage::CodeCompletionOption slider1("or_greater", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  683. slider1.insert_text = slider1.display.quote(p_quote_style);
  684. r_result.insert(slider1.display, slider1);
  685. ScriptLanguage::CodeCompletionOption slider2("or_less", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  686. slider2.insert_text = slider2.display.quote(p_quote_style);
  687. r_result.insert(slider2.display, slider2);
  688. ScriptLanguage::CodeCompletionOption slider3("hide_slider", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  689. slider3.insert_text = slider3.display.quote(p_quote_style);
  690. r_result.insert(slider3.display, slider3);
  691. }
  692. } else if (p_annotation->name == SNAME("@export_exp_easing")) {
  693. if (p_argument == 0 || p_argument == 1) {
  694. // Easing hint.
  695. ScriptLanguage::CodeCompletionOption hint1("attenuation", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  696. hint1.insert_text = hint1.display.quote(p_quote_style);
  697. r_result.insert(hint1.display, hint1);
  698. ScriptLanguage::CodeCompletionOption hint2("inout", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  699. hint2.insert_text = hint2.display.quote(p_quote_style);
  700. r_result.insert(hint2.display, hint2);
  701. }
  702. } else if (p_annotation->name == SNAME("@export_node_path")) {
  703. ScriptLanguage::CodeCompletionOption node("Node", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  704. node.insert_text = node.display.quote(p_quote_style);
  705. r_result.insert(node.display, node);
  706. List<StringName> native_classes;
  707. ClassDB::get_inheriters_from_class("Node", &native_classes);
  708. for (const StringName &E : native_classes) {
  709. if (!ClassDB::is_class_exposed(E)) {
  710. continue;
  711. }
  712. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  713. option.insert_text = option.display.quote(p_quote_style);
  714. r_result.insert(option.display, option);
  715. }
  716. List<StringName> global_script_classes;
  717. ScriptServer::get_global_class_list(&global_script_classes);
  718. for (const StringName &E : global_script_classes) {
  719. if (!ClassDB::is_parent_class(ScriptServer::get_global_class_native_base(E), "Node")) {
  720. continue;
  721. }
  722. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  723. option.insert_text = option.display.quote(p_quote_style);
  724. r_result.insert(option.display, option);
  725. }
  726. } else if (p_annotation->name == SNAME("@warning_ignore")) {
  727. for (int warning_code = 0; warning_code < GDScriptWarning::WARNING_MAX; warning_code++) {
  728. ScriptLanguage::CodeCompletionOption warning(GDScriptWarning::get_name_from_code((GDScriptWarning::Code)warning_code).to_lower(), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  729. warning.insert_text = warning.display.quote(p_quote_style);
  730. r_result.insert(warning.display, warning);
  731. }
  732. } else if (p_annotation->name == SNAME("@rpc")) {
  733. if (p_argument == 0 || p_argument == 1 || p_argument == 2) {
  734. static const char *options[7] = { "call_local", "call_remote", "any_peer", "authority", "reliable", "unreliable", "unreliable_ordered" };
  735. for (int i = 0; i < 7; i++) {
  736. ScriptLanguage::CodeCompletionOption option(options[i], ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  737. option.insert_text = option.display.quote(p_quote_style);
  738. r_result.insert(option.display, option);
  739. }
  740. }
  741. }
  742. }
  743. static void _find_built_in_variants(HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool exclude_nil = false) {
  744. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  745. if (!exclude_nil && Variant::Type(i) == Variant::Type::NIL) {
  746. ScriptLanguage::CodeCompletionOption option("null", ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  747. r_result.insert(option.display, option);
  748. } else {
  749. ScriptLanguage::CodeCompletionOption option(Variant::get_type_name(Variant::Type(i)), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  750. r_result.insert(option.display, option);
  751. }
  752. }
  753. }
  754. static void _list_available_types(bool p_inherit_only, GDScriptParser::CompletionContext &p_context, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  755. // Built-in Variant Types
  756. _find_built_in_variants(r_result, true);
  757. List<StringName> native_types;
  758. ClassDB::get_class_list(&native_types);
  759. for (const StringName &E : native_types) {
  760. if (ClassDB::is_class_exposed(E) && !Engine::get_singleton()->has_singleton(E)) {
  761. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  762. r_result.insert(option.display, option);
  763. }
  764. }
  765. if (p_context.current_class) {
  766. if (!p_inherit_only && p_context.current_class->base_type.is_set()) {
  767. // Native enums from base class
  768. List<StringName> enums;
  769. ClassDB::get_enum_list(p_context.current_class->base_type.native_type, &enums);
  770. for (const StringName &E : enums) {
  771. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  772. r_result.insert(option.display, option);
  773. }
  774. }
  775. // Check current class for potential types
  776. const GDScriptParser::ClassNode *current = p_context.current_class;
  777. while (current) {
  778. for (int i = 0; i < current->members.size(); i++) {
  779. const GDScriptParser::ClassNode::Member &member = current->members[i];
  780. switch (member.type) {
  781. case GDScriptParser::ClassNode::Member::CLASS: {
  782. ScriptLanguage::CodeCompletionOption option(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  783. r_result.insert(option.display, option);
  784. } break;
  785. case GDScriptParser::ClassNode::Member::ENUM: {
  786. if (!p_inherit_only) {
  787. ScriptLanguage::CodeCompletionOption option(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, ScriptLanguage::LOCATION_LOCAL);
  788. r_result.insert(option.display, option);
  789. }
  790. } break;
  791. case GDScriptParser::ClassNode::Member::CONSTANT: {
  792. if (member.constant->get_datatype().is_meta_type && p_context.current_class->outer != nullptr) {
  793. ScriptLanguage::CodeCompletionOption option(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_LOCAL);
  794. r_result.insert(option.display, option);
  795. }
  796. } break;
  797. default:
  798. break;
  799. }
  800. }
  801. current = current->outer;
  802. }
  803. }
  804. // Global scripts
  805. List<StringName> global_classes;
  806. ScriptServer::get_global_class_list(&global_classes);
  807. for (const StringName &E : global_classes) {
  808. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  809. r_result.insert(option.display, option);
  810. }
  811. // Autoload singletons
  812. HashMap<StringName, ProjectSettings::AutoloadInfo> autoloads = ProjectSettings::get_singleton()->get_autoload_list();
  813. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : autoloads) {
  814. const ProjectSettings::AutoloadInfo &info = E.value;
  815. if (!info.is_singleton || info.path.get_extension().to_lower() != "gd") {
  816. continue;
  817. }
  818. ScriptLanguage::CodeCompletionOption option(info.name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  819. r_result.insert(option.display, option);
  820. }
  821. }
  822. static void _find_identifiers_in_suite(const GDScriptParser::SuiteNode *p_suite, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth = 0) {
  823. for (int i = 0; i < p_suite->locals.size(); i++) {
  824. ScriptLanguage::CodeCompletionOption option;
  825. int location = p_recursion_depth == 0 ? ScriptLanguage::LOCATION_LOCAL : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
  826. if (p_suite->locals[i].type == GDScriptParser::SuiteNode::Local::CONSTANT) {
  827. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  828. option.default_value = p_suite->locals[i].constant->initializer->reduced_value;
  829. } else {
  830. option = ScriptLanguage::CodeCompletionOption(p_suite->locals[i].name, ScriptLanguage::CODE_COMPLETION_KIND_VARIABLE, location);
  831. }
  832. r_result.insert(option.display, option);
  833. }
  834. if (p_suite->parent_block) {
  835. _find_identifiers_in_suite(p_suite->parent_block, r_result, p_recursion_depth + 1);
  836. }
  837. }
  838. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth);
  839. static void _find_identifiers_in_class(const GDScriptParser::ClassNode *p_class, bool p_only_functions, bool p_static, bool p_parent_only, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  840. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  841. if (!p_parent_only) {
  842. bool outer = false;
  843. const GDScriptParser::ClassNode *clss = p_class;
  844. int classes_processed = 0;
  845. while (clss) {
  846. for (int i = 0; i < clss->members.size(); i++) {
  847. const int location = p_recursion_depth == 0 ? classes_processed : (p_recursion_depth | ScriptLanguage::LOCATION_PARENT_MASK);
  848. const GDScriptParser::ClassNode::Member &member = clss->members[i];
  849. ScriptLanguage::CodeCompletionOption option;
  850. switch (member.type) {
  851. case GDScriptParser::ClassNode::Member::VARIABLE:
  852. if (p_only_functions || outer || (p_static)) {
  853. continue;
  854. }
  855. option = ScriptLanguage::CodeCompletionOption(member.variable->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  856. break;
  857. case GDScriptParser::ClassNode::Member::CONSTANT:
  858. if (p_only_functions) {
  859. continue;
  860. }
  861. if (r_result.has(member.constant->identifier->name)) {
  862. continue;
  863. }
  864. option = ScriptLanguage::CodeCompletionOption(member.constant->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  865. if (member.constant->initializer) {
  866. option.default_value = member.constant->initializer->reduced_value;
  867. }
  868. break;
  869. case GDScriptParser::ClassNode::Member::CLASS:
  870. if (p_only_functions) {
  871. continue;
  872. }
  873. option = ScriptLanguage::CodeCompletionOption(member.m_class->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, location);
  874. break;
  875. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  876. if (p_only_functions) {
  877. continue;
  878. }
  879. option = ScriptLanguage::CodeCompletionOption(member.enum_value.identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  880. break;
  881. case GDScriptParser::ClassNode::Member::ENUM:
  882. if (p_only_functions) {
  883. continue;
  884. }
  885. option = ScriptLanguage::CodeCompletionOption(member.m_enum->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  886. break;
  887. case GDScriptParser::ClassNode::Member::FUNCTION:
  888. if (outer || (p_static && !member.function->is_static) || member.function->identifier->name.operator String().begins_with("@")) {
  889. continue;
  890. }
  891. option = ScriptLanguage::CodeCompletionOption(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  892. if (member.function->parameters.size() > 0) {
  893. option.insert_text += "(";
  894. } else {
  895. option.insert_text += "()";
  896. }
  897. break;
  898. case GDScriptParser::ClassNode::Member::SIGNAL:
  899. if (p_only_functions || outer || p_static) {
  900. continue;
  901. }
  902. option = ScriptLanguage::CodeCompletionOption(member.signal->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  903. break;
  904. case GDScriptParser::ClassNode::Member::GROUP:
  905. break; // No-op, but silences warnings.
  906. case GDScriptParser::ClassNode::Member::UNDEFINED:
  907. break;
  908. }
  909. r_result.insert(option.display, option);
  910. }
  911. outer = true;
  912. clss = clss->outer;
  913. classes_processed++;
  914. }
  915. }
  916. // Parents.
  917. GDScriptCompletionIdentifier base_type;
  918. base_type.type = p_class->base_type;
  919. base_type.type.is_meta_type = p_static;
  920. _find_identifiers_in_base(base_type, p_only_functions, r_result, p_recursion_depth + 1);
  921. }
  922. static void _find_identifiers_in_base(const GDScriptCompletionIdentifier &p_base, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  923. ERR_FAIL_COND(p_recursion_depth > COMPLETION_RECURSION_LIMIT);
  924. GDScriptParser::DataType base_type = p_base.type;
  925. if (base_type.is_meta_type && base_type.kind != GDScriptParser::DataType::BUILTIN && base_type.kind != GDScriptParser::DataType::ENUM) {
  926. ScriptLanguage::CodeCompletionOption option("new", ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, ScriptLanguage::LOCATION_LOCAL);
  927. option.insert_text += "(";
  928. r_result.insert(option.display, option);
  929. }
  930. while (!base_type.has_no_type()) {
  931. switch (base_type.kind) {
  932. case GDScriptParser::DataType::CLASS: {
  933. _find_identifiers_in_class(base_type.class_type, p_only_functions, base_type.is_meta_type, false, r_result, p_recursion_depth);
  934. // This already finds all parent identifiers, so we are done.
  935. base_type = GDScriptParser::DataType();
  936. } break;
  937. case GDScriptParser::DataType::SCRIPT: {
  938. Ref<Script> scr = base_type.script_type;
  939. if (scr.is_valid()) {
  940. if (!p_only_functions) {
  941. if (!base_type.is_meta_type) {
  942. List<PropertyInfo> members;
  943. scr->get_script_property_list(&members);
  944. for (const PropertyInfo &E : members) {
  945. int location = p_recursion_depth + _get_property_location(scr->get_class_name(), E.name);
  946. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  947. r_result.insert(option.display, option);
  948. }
  949. List<MethodInfo> signals;
  950. scr->get_script_signal_list(&signals);
  951. for (const MethodInfo &E : signals) {
  952. int location = p_recursion_depth + _get_signal_location(scr->get_class_name(), E.name);
  953. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  954. r_result.insert(option.display, option);
  955. }
  956. }
  957. HashMap<StringName, Variant> constants;
  958. scr->get_constants(&constants);
  959. for (const KeyValue<StringName, Variant> &E : constants) {
  960. int location = p_recursion_depth + _get_constant_location(scr->get_class_name(), E.key);
  961. ScriptLanguage::CodeCompletionOption option(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  962. r_result.insert(option.display, option);
  963. }
  964. }
  965. List<MethodInfo> methods;
  966. scr->get_script_method_list(&methods);
  967. for (const MethodInfo &E : methods) {
  968. if (E.name.begins_with("@")) {
  969. continue;
  970. }
  971. int location = p_recursion_depth + _get_method_location(scr->get_class_name(), E.name);
  972. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  973. if (E.arguments.size()) {
  974. option.insert_text += "(";
  975. } else {
  976. option.insert_text += "()";
  977. }
  978. r_result.insert(option.display, option);
  979. }
  980. Ref<Script> base_script = scr->get_base_script();
  981. if (base_script.is_valid()) {
  982. base_type.script_type = base_script;
  983. } else {
  984. base_type.kind = GDScriptParser::DataType::NATIVE;
  985. base_type.builtin_type = Variant::OBJECT;
  986. base_type.native_type = scr->get_instance_base_type();
  987. }
  988. } else {
  989. return;
  990. }
  991. } break;
  992. case GDScriptParser::DataType::NATIVE: {
  993. StringName type = base_type.native_type;
  994. if (!ClassDB::class_exists(type)) {
  995. return;
  996. }
  997. if (!p_only_functions) {
  998. List<String> constants;
  999. ClassDB::get_integer_constant_list(type, &constants);
  1000. for (const String &E : constants) {
  1001. int location = p_recursion_depth + _get_constant_location(type, StringName(E));
  1002. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT, location);
  1003. r_result.insert(option.display, option);
  1004. }
  1005. if (!base_type.is_meta_type || Engine::get_singleton()->has_singleton(type)) {
  1006. List<PropertyInfo> pinfo;
  1007. ClassDB::get_property_list(type, &pinfo);
  1008. for (const PropertyInfo &E : pinfo) {
  1009. if (E.usage & (PROPERTY_USAGE_GROUP | PROPERTY_USAGE_CATEGORY)) {
  1010. continue;
  1011. }
  1012. if (E.name.contains("/")) {
  1013. continue;
  1014. }
  1015. int location = p_recursion_depth + _get_property_location(type, E.name);
  1016. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER, location);
  1017. r_result.insert(option.display, option);
  1018. }
  1019. List<MethodInfo> signals;
  1020. ClassDB::get_signal_list(type, &signals);
  1021. for (const MethodInfo &E : signals) {
  1022. int location = p_recursion_depth + _get_signal_location(type, StringName(E.name));
  1023. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_SIGNAL, location);
  1024. r_result.insert(option.display, option);
  1025. }
  1026. }
  1027. }
  1028. bool only_static = base_type.is_meta_type && !Engine::get_singleton()->has_singleton(type);
  1029. List<MethodInfo> methods;
  1030. ClassDB::get_method_list(type, &methods, false, true);
  1031. for (const MethodInfo &E : methods) {
  1032. if (only_static && (E.flags & METHOD_FLAG_STATIC) == 0) {
  1033. continue;
  1034. }
  1035. if (E.name.begins_with("_")) {
  1036. continue;
  1037. }
  1038. int location = p_recursion_depth + _get_method_location(type, E.name);
  1039. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION, location);
  1040. if (E.arguments.size()) {
  1041. option.insert_text += "(";
  1042. } else {
  1043. option.insert_text += "()";
  1044. }
  1045. r_result.insert(option.display, option);
  1046. }
  1047. return;
  1048. } break;
  1049. case GDScriptParser::DataType::ENUM:
  1050. case GDScriptParser::DataType::BUILTIN: {
  1051. Callable::CallError err;
  1052. Variant tmp;
  1053. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  1054. if (err.error != Callable::CallError::CALL_OK) {
  1055. return;
  1056. }
  1057. if (!p_only_functions) {
  1058. List<PropertyInfo> members;
  1059. if (p_base.value.get_type() != Variant::NIL) {
  1060. p_base.value.get_property_list(&members);
  1061. } else {
  1062. tmp.get_property_list(&members);
  1063. }
  1064. for (const PropertyInfo &E : members) {
  1065. if (!String(E.name).contains("/")) {
  1066. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_MEMBER);
  1067. if (GDScriptParser::theme_color_names.has(E.name)) {
  1068. option.theme_color_name = GDScriptParser::theme_color_names[E.name];
  1069. }
  1070. r_result.insert(option.display, option);
  1071. }
  1072. }
  1073. }
  1074. List<MethodInfo> methods;
  1075. tmp.get_method_list(&methods);
  1076. for (const MethodInfo &E : methods) {
  1077. if (base_type.kind == GDScriptParser::DataType::ENUM && base_type.is_meta_type && !(E.flags & METHOD_FLAG_CONST)) {
  1078. // Enum types are static and cannot change, therefore we skip non-const dictionary methods.
  1079. continue;
  1080. }
  1081. ScriptLanguage::CodeCompletionOption option(E.name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1082. if (E.arguments.size()) {
  1083. option.insert_text += "(";
  1084. } else {
  1085. option.insert_text += "()";
  1086. }
  1087. r_result.insert(option.display, option);
  1088. }
  1089. return;
  1090. } break;
  1091. default: {
  1092. return;
  1093. } break;
  1094. }
  1095. }
  1096. }
  1097. static void _find_identifiers(const GDScriptParser::CompletionContext &p_context, bool p_only_functions, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, int p_recursion_depth) {
  1098. if (!p_only_functions && p_context.current_suite) {
  1099. // This includes function parameters, since they are also locals.
  1100. _find_identifiers_in_suite(p_context.current_suite, r_result);
  1101. }
  1102. if (p_context.current_class) {
  1103. _find_identifiers_in_class(p_context.current_class, p_only_functions, (!p_context.current_function || p_context.current_function->is_static), false, r_result, p_recursion_depth);
  1104. }
  1105. List<StringName> functions;
  1106. GDScriptUtilityFunctions::get_function_list(&functions);
  1107. for (const StringName &E : functions) {
  1108. MethodInfo function = GDScriptUtilityFunctions::get_function_info(E);
  1109. ScriptLanguage::CodeCompletionOption option(String(E), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1110. if (function.arguments.size() || (function.flags & METHOD_FLAG_VARARG)) {
  1111. option.insert_text += "(";
  1112. } else {
  1113. option.insert_text += "()";
  1114. }
  1115. r_result.insert(option.display, option);
  1116. }
  1117. if (p_only_functions) {
  1118. return;
  1119. }
  1120. _find_built_in_variants(r_result);
  1121. static const char *_keywords[] = {
  1122. "true", "false", "PI", "TAU", "INF", "NAN", "null", "self", "super",
  1123. "break", "breakpoint", "continue", "pass", "return",
  1124. nullptr
  1125. };
  1126. const char **kw = _keywords;
  1127. while (*kw) {
  1128. ScriptLanguage::CodeCompletionOption option(*kw, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1129. r_result.insert(option.display, option);
  1130. kw++;
  1131. }
  1132. static const char *_keywords_with_space[] = {
  1133. "and", "not", "or", "in", "as", "class", "class_name", "extends", "is", "func", "signal", "await",
  1134. "const", "enum", "static", "var", "if", "elif", "else", "for", "match", "while",
  1135. nullptr
  1136. };
  1137. const char **kws = _keywords_with_space;
  1138. while (*kws) {
  1139. ScriptLanguage::CodeCompletionOption option(*kws, ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  1140. option.insert_text += " ";
  1141. r_result.insert(option.display, option);
  1142. kws++;
  1143. }
  1144. static const char *_keywords_with_args[] = {
  1145. "assert", "preload",
  1146. nullptr
  1147. };
  1148. const char **kwa = _keywords_with_args;
  1149. while (*kwa) {
  1150. ScriptLanguage::CodeCompletionOption option(*kwa, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1151. option.insert_text += "(";
  1152. r_result.insert(option.display, option);
  1153. kwa++;
  1154. }
  1155. List<StringName> utility_func_names;
  1156. Variant::get_utility_function_list(&utility_func_names);
  1157. for (List<StringName>::Element *E = utility_func_names.front(); E; E = E->next()) {
  1158. ScriptLanguage::CodeCompletionOption option(E->get(), ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  1159. option.insert_text += "(";
  1160. r_result.insert(option.display, option);
  1161. }
  1162. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1163. if (!E.value.is_singleton) {
  1164. continue;
  1165. }
  1166. ScriptLanguage::CodeCompletionOption option(E.key, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1167. r_result.insert(option.display, option);
  1168. }
  1169. // Native classes and global constants.
  1170. for (const KeyValue<StringName, int> &E : GDScriptLanguage::get_singleton()->get_global_map()) {
  1171. ScriptLanguage::CodeCompletionOption option;
  1172. if (ClassDB::class_exists(E.key) || Engine::get_singleton()->has_singleton(E.key)) {
  1173. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CLASS);
  1174. } else {
  1175. option = ScriptLanguage::CodeCompletionOption(E.key.operator String(), ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  1176. }
  1177. r_result.insert(option.display, option);
  1178. }
  1179. // Global classes
  1180. List<StringName> global_classes;
  1181. ScriptServer::get_global_class_list(&global_classes);
  1182. for (const StringName &E : global_classes) {
  1183. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CLASS, ScriptLanguage::LOCATION_OTHER_USER_CODE);
  1184. r_result.insert(option.display, option);
  1185. }
  1186. }
  1187. static GDScriptCompletionIdentifier _type_from_variant(const Variant &p_value) {
  1188. GDScriptCompletionIdentifier ci;
  1189. ci.value = p_value;
  1190. ci.type.is_constant = true;
  1191. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1192. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1193. ci.type.builtin_type = p_value.get_type();
  1194. if (ci.type.builtin_type == Variant::OBJECT) {
  1195. Object *obj = p_value.operator Object *();
  1196. if (!obj) {
  1197. return ci;
  1198. }
  1199. ci.type.native_type = obj->get_class_name();
  1200. Ref<Script> scr = p_value;
  1201. if (scr.is_valid()) {
  1202. ci.type.is_meta_type = true;
  1203. } else {
  1204. ci.type.is_meta_type = false;
  1205. scr = obj->get_script();
  1206. }
  1207. if (scr.is_valid()) {
  1208. ci.type.script_type = scr;
  1209. ci.type.kind = GDScriptParser::DataType::SCRIPT;
  1210. ci.type.native_type = scr->get_instance_base_type();
  1211. } else {
  1212. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1213. }
  1214. }
  1215. return ci;
  1216. }
  1217. static GDScriptCompletionIdentifier _type_from_property(const PropertyInfo &p_property) {
  1218. GDScriptCompletionIdentifier ci;
  1219. if (p_property.type == Variant::NIL) {
  1220. // Variant
  1221. return ci;
  1222. }
  1223. if (p_property.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  1224. ci.enumeration = p_property.class_name;
  1225. }
  1226. ci.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1227. ci.type.builtin_type = p_property.type;
  1228. if (p_property.type == Variant::OBJECT) {
  1229. ci.type.kind = GDScriptParser::DataType::NATIVE;
  1230. ci.type.native_type = p_property.class_name == StringName() ? "Object" : p_property.class_name;
  1231. } else {
  1232. ci.type.kind = GDScriptParser::DataType::BUILTIN;
  1233. }
  1234. return ci;
  1235. }
  1236. #define MAX_COMPLETION_RECURSION 100
  1237. struct RecursionCheck {
  1238. int *counter;
  1239. _FORCE_INLINE_ bool check() {
  1240. return (*counter) > MAX_COMPLETION_RECURSION;
  1241. }
  1242. RecursionCheck(int *p_counter) :
  1243. counter(p_counter) {
  1244. (*counter)++;
  1245. }
  1246. ~RecursionCheck() {
  1247. (*counter)--;
  1248. }
  1249. };
  1250. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type);
  1251. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type);
  1252. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type);
  1253. static bool _is_expression_named_identifier(const GDScriptParser::ExpressionNode *p_expression, const StringName &p_name) {
  1254. if (p_expression) {
  1255. switch (p_expression->type) {
  1256. case GDScriptParser::Node::IDENTIFIER: {
  1257. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1258. if (id->name == p_name) {
  1259. return true;
  1260. }
  1261. } break;
  1262. case GDScriptParser::Node::CAST: {
  1263. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1264. return _is_expression_named_identifier(cn->operand, p_name);
  1265. } break;
  1266. default:
  1267. break;
  1268. }
  1269. }
  1270. return false;
  1271. }
  1272. static bool _guess_expression_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::ExpressionNode *p_expression, GDScriptCompletionIdentifier &r_type) {
  1273. bool found = false;
  1274. if (p_expression == nullptr) {
  1275. return false;
  1276. }
  1277. static int recursion_depth = 0;
  1278. RecursionCheck recursion(&recursion_depth);
  1279. if (unlikely(recursion.check())) {
  1280. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  1281. }
  1282. if (p_expression->is_constant) {
  1283. // Already has a value, so just use that.
  1284. r_type = _type_from_variant(p_expression->reduced_value);
  1285. switch (p_expression->get_datatype().kind) {
  1286. case GDScriptParser::DataType::ENUM:
  1287. case GDScriptParser::DataType::CLASS:
  1288. r_type.type = p_expression->get_datatype();
  1289. break;
  1290. default:
  1291. break;
  1292. }
  1293. found = true;
  1294. } else {
  1295. switch (p_expression->type) {
  1296. case GDScriptParser::Node::LITERAL: {
  1297. const GDScriptParser::LiteralNode *literal = static_cast<const GDScriptParser::LiteralNode *>(p_expression);
  1298. r_type = _type_from_variant(literal->value);
  1299. found = true;
  1300. } break;
  1301. case GDScriptParser::Node::SELF: {
  1302. if (p_context.current_class) {
  1303. if (p_context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  1304. r_type.type = p_context.current_class->get_datatype();
  1305. } else {
  1306. r_type.type = p_context.current_class->base_type;
  1307. }
  1308. found = true;
  1309. }
  1310. } break;
  1311. case GDScriptParser::Node::IDENTIFIER: {
  1312. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(p_expression);
  1313. found = _guess_identifier_type(p_context, id, r_type);
  1314. } break;
  1315. case GDScriptParser::Node::DICTIONARY: {
  1316. // Try to recreate the dictionary.
  1317. const GDScriptParser::DictionaryNode *dn = static_cast<const GDScriptParser::DictionaryNode *>(p_expression);
  1318. Dictionary d;
  1319. bool full = true;
  1320. for (int i = 0; i < dn->elements.size(); i++) {
  1321. GDScriptCompletionIdentifier key;
  1322. if (_guess_expression_type(p_context, dn->elements[i].key, key)) {
  1323. if (!key.type.is_constant) {
  1324. full = false;
  1325. break;
  1326. }
  1327. GDScriptCompletionIdentifier value;
  1328. if (_guess_expression_type(p_context, dn->elements[i].value, value)) {
  1329. if (!value.type.is_constant) {
  1330. full = false;
  1331. break;
  1332. }
  1333. d[key.value] = value.value;
  1334. } else {
  1335. full = false;
  1336. break;
  1337. }
  1338. } else {
  1339. full = false;
  1340. break;
  1341. }
  1342. }
  1343. if (full) {
  1344. r_type.value = d;
  1345. r_type.type.is_constant = true;
  1346. }
  1347. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1348. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1349. r_type.type.builtin_type = Variant::DICTIONARY;
  1350. found = true;
  1351. } break;
  1352. case GDScriptParser::Node::ARRAY: {
  1353. // Try to recreate the array
  1354. const GDScriptParser::ArrayNode *an = static_cast<const GDScriptParser::ArrayNode *>(p_expression);
  1355. Array a;
  1356. bool full = true;
  1357. a.resize(an->elements.size());
  1358. for (int i = 0; i < an->elements.size(); i++) {
  1359. GDScriptCompletionIdentifier value;
  1360. if (_guess_expression_type(p_context, an->elements[i], value)) {
  1361. if (value.type.is_constant) {
  1362. a[i] = value.value;
  1363. } else {
  1364. full = false;
  1365. break;
  1366. }
  1367. } else {
  1368. full = false;
  1369. break;
  1370. }
  1371. }
  1372. if (full) {
  1373. // If not fully constant, setting this value is detrimental to the inference.
  1374. r_type.value = a;
  1375. }
  1376. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1377. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1378. r_type.type.builtin_type = Variant::ARRAY;
  1379. found = true;
  1380. } break;
  1381. case GDScriptParser::Node::CAST: {
  1382. const GDScriptParser::CastNode *cn = static_cast<const GDScriptParser::CastNode *>(p_expression);
  1383. GDScriptCompletionIdentifier value;
  1384. if (_guess_expression_type(p_context, cn->operand, r_type)) {
  1385. r_type.type = cn->get_datatype();
  1386. found = true;
  1387. }
  1388. } break;
  1389. case GDScriptParser::Node::CALL: {
  1390. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_expression);
  1391. if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  1392. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1393. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1394. r_type.type.builtin_type = GDScriptParser::get_builtin_type(call->function_name);
  1395. found = true;
  1396. break;
  1397. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  1398. MethodInfo mi = GDScriptUtilityFunctions::get_function_info(call->function_name);
  1399. r_type = _type_from_property(mi.return_val);
  1400. found = true;
  1401. break;
  1402. } else {
  1403. GDScriptParser::CompletionContext c = p_context;
  1404. c.current_line = call->start_line;
  1405. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  1406. GDScriptCompletionIdentifier base;
  1407. if (callee_type == GDScriptParser::Node::IDENTIFIER || call->is_super) {
  1408. // Simple call, so base is 'self'.
  1409. if (p_context.current_class) {
  1410. if (call->is_super) {
  1411. base.type = p_context.current_class->base_type;
  1412. base.value = p_context.base;
  1413. } else {
  1414. base.type.kind = GDScriptParser::DataType::CLASS;
  1415. base.type.type_source = GDScriptParser::DataType::INFERRED;
  1416. base.type.is_constant = true;
  1417. base.type.class_type = p_context.current_class;
  1418. base.value = p_context.base;
  1419. }
  1420. } else {
  1421. break;
  1422. }
  1423. } else if (callee_type == GDScriptParser::Node::SUBSCRIPT && static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->is_attribute) {
  1424. if (!_guess_expression_type(c, static_cast<const GDScriptParser::SubscriptNode *>(call->callee)->base, base)) {
  1425. found = false;
  1426. break;
  1427. }
  1428. } else {
  1429. break;
  1430. }
  1431. // Try call if constant methods with constant arguments
  1432. if (base.type.is_constant && base.value.get_type() == Variant::OBJECT) {
  1433. GDScriptParser::DataType native_type = base.type;
  1434. while (native_type.kind == GDScriptParser::DataType::CLASS) {
  1435. native_type = native_type.class_type->base_type;
  1436. }
  1437. while (native_type.kind == GDScriptParser::DataType::SCRIPT) {
  1438. if (native_type.script_type.is_valid()) {
  1439. Ref<Script> parent = native_type.script_type->get_base_script();
  1440. if (parent.is_valid()) {
  1441. native_type.script_type = parent;
  1442. } else {
  1443. native_type.kind = GDScriptParser::DataType::NATIVE;
  1444. native_type.builtin_type = Variant::OBJECT;
  1445. native_type.native_type = native_type.script_type->get_instance_base_type();
  1446. if (!ClassDB::class_exists(native_type.native_type)) {
  1447. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  1448. }
  1449. }
  1450. }
  1451. }
  1452. if (native_type.kind == GDScriptParser::DataType::NATIVE) {
  1453. MethodBind *mb = ClassDB::get_method(native_type.native_type, call->function_name);
  1454. if (mb && mb->is_const()) {
  1455. bool all_is_const = true;
  1456. Vector<Variant> args;
  1457. GDScriptParser::CompletionContext c2 = p_context;
  1458. c2.current_line = call->start_line;
  1459. for (int i = 0; all_is_const && i < call->arguments.size(); i++) {
  1460. GDScriptCompletionIdentifier arg;
  1461. if (!call->arguments[i]->is_constant) {
  1462. all_is_const = false;
  1463. }
  1464. }
  1465. Object *baseptr = base.value;
  1466. if (all_is_const && call->function_name == SNAME("get_node") && ClassDB::is_parent_class(native_type.native_type, SNAME("Node")) && args.size()) {
  1467. String arg1 = args[0];
  1468. if (arg1.begins_with("/root/")) {
  1469. String which = arg1.get_slice("/", 2);
  1470. if (!which.is_empty()) {
  1471. // Try singletons first
  1472. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(which)) {
  1473. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[which]);
  1474. found = true;
  1475. } else {
  1476. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  1477. String name = E.key;
  1478. if (name == which) {
  1479. String script = E.value.path;
  1480. if (!script.begins_with("res://")) {
  1481. script = "res://" + script;
  1482. }
  1483. if (!script.ends_with(".gd")) {
  1484. // not a script, try find the script anyway,
  1485. // may have some success
  1486. script = script.get_basename() + ".gd";
  1487. }
  1488. if (FileAccess::exists(script)) {
  1489. Error err = OK;
  1490. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1491. if (err == OK) {
  1492. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1493. r_type.type.script_path = script;
  1494. r_type.type.class_type = parser->get_parser()->get_tree();
  1495. r_type.type.is_constant = false;
  1496. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1497. r_type.value = Variant();
  1498. p_context.dependent_parsers.push_back(parser);
  1499. found = true;
  1500. }
  1501. }
  1502. break;
  1503. }
  1504. }
  1505. }
  1506. }
  1507. }
  1508. }
  1509. if (!found && all_is_const && baseptr) {
  1510. Vector<const Variant *> argptr;
  1511. for (int i = 0; i < args.size(); i++) {
  1512. argptr.push_back(&args[i]);
  1513. }
  1514. Callable::CallError ce;
  1515. Variant ret = mb->call(baseptr, (const Variant **)argptr.ptr(), argptr.size(), ce);
  1516. if (ce.error == Callable::CallError::CALL_OK && ret.get_type() != Variant::NIL) {
  1517. if (ret.get_type() != Variant::OBJECT || ret.operator Object *() != nullptr) {
  1518. r_type = _type_from_variant(ret);
  1519. found = true;
  1520. }
  1521. }
  1522. }
  1523. }
  1524. }
  1525. }
  1526. if (!found && base.value.get_type() != Variant::NIL) {
  1527. found = _guess_method_return_type_from_base(c, base, call->function_name, r_type);
  1528. }
  1529. }
  1530. } break;
  1531. case GDScriptParser::Node::SUBSCRIPT: {
  1532. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(p_expression);
  1533. if (subscript->is_attribute) {
  1534. GDScriptParser::CompletionContext c = p_context;
  1535. c.current_line = subscript->start_line;
  1536. GDScriptCompletionIdentifier base;
  1537. if (!_guess_expression_type(c, subscript->base, base)) {
  1538. found = false;
  1539. break;
  1540. }
  1541. if (base.value.get_type() == Variant::DICTIONARY && base.value.operator Dictionary().has(String(subscript->attribute->name))) {
  1542. Variant value = base.value.operator Dictionary()[String(subscript->attribute->name)];
  1543. r_type = _type_from_variant(value);
  1544. found = true;
  1545. break;
  1546. }
  1547. const GDScriptParser::DictionaryNode *dn = nullptr;
  1548. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1549. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1550. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1551. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1552. }
  1553. if (dn) {
  1554. for (int i = 0; i < dn->elements.size(); i++) {
  1555. GDScriptCompletionIdentifier key;
  1556. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1557. continue;
  1558. }
  1559. if (key.value == String(subscript->attribute->name)) {
  1560. r_type.assigned_expression = dn->elements[i].value;
  1561. found = _guess_expression_type(c, dn->elements[i].value, r_type);
  1562. break;
  1563. }
  1564. }
  1565. }
  1566. if (!found) {
  1567. found = _guess_identifier_type_from_base(c, base, subscript->attribute->name, r_type);
  1568. }
  1569. } else {
  1570. if (subscript->index == nullptr) {
  1571. found = false;
  1572. break;
  1573. }
  1574. GDScriptParser::CompletionContext c = p_context;
  1575. c.current_line = subscript->start_line;
  1576. GDScriptCompletionIdentifier base;
  1577. if (!_guess_expression_type(c, subscript->base, base)) {
  1578. found = false;
  1579. break;
  1580. }
  1581. GDScriptCompletionIdentifier index;
  1582. if (!_guess_expression_type(c, subscript->index, index)) {
  1583. found = false;
  1584. break;
  1585. }
  1586. if (base.value.in(index.value)) {
  1587. Variant value = base.value.get(index.value);
  1588. r_type = _type_from_variant(value);
  1589. found = true;
  1590. break;
  1591. }
  1592. // Look if it is a dictionary node.
  1593. const GDScriptParser::DictionaryNode *dn = nullptr;
  1594. if (subscript->base->type == GDScriptParser::Node::DICTIONARY) {
  1595. dn = static_cast<const GDScriptParser::DictionaryNode *>(subscript->base);
  1596. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::DICTIONARY) {
  1597. dn = static_cast<const GDScriptParser::DictionaryNode *>(base.assigned_expression);
  1598. }
  1599. if (dn) {
  1600. for (int i = 0; i < dn->elements.size(); i++) {
  1601. GDScriptCompletionIdentifier key;
  1602. if (!_guess_expression_type(c, dn->elements[i].key, key)) {
  1603. continue;
  1604. }
  1605. if (key.value == index.value) {
  1606. r_type.assigned_expression = dn->elements[i].value;
  1607. found = _guess_expression_type(p_context, dn->elements[i].value, r_type);
  1608. break;
  1609. }
  1610. }
  1611. }
  1612. // Look if it is an array node.
  1613. if (!found && index.value.is_num()) {
  1614. int idx = index.value;
  1615. const GDScriptParser::ArrayNode *an = nullptr;
  1616. if (subscript->base->type == GDScriptParser::Node::ARRAY) {
  1617. an = static_cast<const GDScriptParser::ArrayNode *>(subscript->base);
  1618. } else if (base.assigned_expression && base.assigned_expression->type == GDScriptParser::Node::ARRAY) {
  1619. an = static_cast<const GDScriptParser::ArrayNode *>(base.assigned_expression);
  1620. }
  1621. if (an && idx >= 0 && an->elements.size() > idx) {
  1622. r_type.assigned_expression = an->elements[idx];
  1623. found = _guess_expression_type(c, an->elements[idx], r_type);
  1624. break;
  1625. }
  1626. }
  1627. // Look for valid indexing in other types
  1628. if (!found && (index.value.get_type() == Variant::STRING || index.value.get_type() == Variant::NODE_PATH)) {
  1629. StringName id = index.value;
  1630. found = _guess_identifier_type_from_base(c, base, id, r_type);
  1631. } else if (!found && index.type.kind == GDScriptParser::DataType::BUILTIN) {
  1632. Callable::CallError err;
  1633. Variant base_val;
  1634. Variant::construct(base.type.builtin_type, base_val, nullptr, 0, err);
  1635. bool valid = false;
  1636. Variant res = base_val.get(index.value, &valid);
  1637. if (valid) {
  1638. r_type = _type_from_variant(res);
  1639. r_type.value = Variant();
  1640. r_type.type.is_constant = false;
  1641. found = true;
  1642. }
  1643. }
  1644. }
  1645. } break;
  1646. case GDScriptParser::Node::BINARY_OPERATOR: {
  1647. const GDScriptParser::BinaryOpNode *op = static_cast<const GDScriptParser::BinaryOpNode *>(p_expression);
  1648. if (op->variant_op == Variant::OP_MAX) {
  1649. break;
  1650. }
  1651. GDScriptParser::CompletionContext context = p_context;
  1652. context.current_line = op->start_line;
  1653. GDScriptCompletionIdentifier p1;
  1654. GDScriptCompletionIdentifier p2;
  1655. if (!_guess_expression_type(context, op->left_operand, p1)) {
  1656. found = false;
  1657. break;
  1658. }
  1659. if (!_guess_expression_type(context, op->right_operand, p2)) {
  1660. found = false;
  1661. break;
  1662. }
  1663. Callable::CallError ce;
  1664. bool v1_use_value = p1.value.get_type() != Variant::NIL && p1.value.get_type() != Variant::OBJECT;
  1665. Variant d1;
  1666. Variant::construct(p1.type.builtin_type, d1, nullptr, 0, ce);
  1667. Variant d2;
  1668. Variant::construct(p2.type.builtin_type, d2, nullptr, 0, ce);
  1669. Variant v1 = (v1_use_value) ? p1.value : d1;
  1670. bool v2_use_value = p2.value.get_type() != Variant::NIL && p2.value.get_type() != Variant::OBJECT;
  1671. Variant v2 = (v2_use_value) ? p2.value : d2;
  1672. // avoid potential invalid ops
  1673. if ((op->variant_op == Variant::OP_DIVIDE || op->variant_op == Variant::OP_MODULE) && v2.get_type() == Variant::INT) {
  1674. v2 = 1;
  1675. v2_use_value = false;
  1676. }
  1677. if (op->variant_op == Variant::OP_DIVIDE && v2.get_type() == Variant::FLOAT) {
  1678. v2 = 1.0;
  1679. v2_use_value = false;
  1680. }
  1681. Variant res;
  1682. bool valid;
  1683. Variant::evaluate(op->variant_op, v1, v2, res, valid);
  1684. if (!valid) {
  1685. found = false;
  1686. break;
  1687. }
  1688. r_type = _type_from_variant(res);
  1689. if (!v1_use_value || !v2_use_value) {
  1690. r_type.value = Variant();
  1691. r_type.type.is_constant = false;
  1692. }
  1693. found = true;
  1694. } break;
  1695. default:
  1696. break;
  1697. }
  1698. }
  1699. // It may have found a null, but that's never useful
  1700. if (found && r_type.type.kind == GDScriptParser::DataType::BUILTIN && r_type.type.builtin_type == Variant::NIL) {
  1701. found = false;
  1702. }
  1703. // Check type hint last. For collections we want chance to get the actual value first
  1704. // This way we can detect types from the content of dictionaries and arrays
  1705. if (!found && p_expression->get_datatype().is_hard_type()) {
  1706. r_type.type = p_expression->get_datatype();
  1707. if (!r_type.assigned_expression) {
  1708. r_type.assigned_expression = p_expression;
  1709. }
  1710. found = true;
  1711. }
  1712. return found;
  1713. }
  1714. static bool _guess_identifier_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::IdentifierNode *p_identifier, GDScriptCompletionIdentifier &r_type) {
  1715. static int recursion_depth = 0;
  1716. RecursionCheck recursion(&recursion_depth);
  1717. if (unlikely(recursion.check())) {
  1718. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  1719. }
  1720. // Look in blocks first.
  1721. int last_assign_line = -1;
  1722. const GDScriptParser::ExpressionNode *last_assigned_expression = nullptr;
  1723. GDScriptParser::DataType id_type;
  1724. GDScriptParser::SuiteNode *suite = p_context.current_suite;
  1725. bool is_function_parameter = false;
  1726. bool can_be_local = true;
  1727. switch (p_identifier->source) {
  1728. case GDScriptParser::IdentifierNode::MEMBER_VARIABLE:
  1729. case GDScriptParser::IdentifierNode::MEMBER_CONSTANT:
  1730. case GDScriptParser::IdentifierNode::MEMBER_FUNCTION:
  1731. case GDScriptParser::IdentifierNode::MEMBER_SIGNAL:
  1732. case GDScriptParser::IdentifierNode::MEMBER_CLASS:
  1733. case GDScriptParser::IdentifierNode::INHERITED_VARIABLE:
  1734. case GDScriptParser::IdentifierNode::STATIC_VARIABLE:
  1735. can_be_local = false;
  1736. break;
  1737. default:
  1738. break;
  1739. }
  1740. if (can_be_local && suite && suite->has_local(p_identifier->name)) {
  1741. const GDScriptParser::SuiteNode::Local &local = suite->get_local(p_identifier->name);
  1742. id_type = local.get_datatype();
  1743. // Check initializer as the first assignment.
  1744. switch (local.type) {
  1745. case GDScriptParser::SuiteNode::Local::VARIABLE:
  1746. if (local.variable->initializer) {
  1747. last_assign_line = local.variable->initializer->end_line;
  1748. last_assigned_expression = local.variable->initializer;
  1749. }
  1750. break;
  1751. case GDScriptParser::SuiteNode::Local::CONSTANT:
  1752. if (local.constant->initializer) {
  1753. last_assign_line = local.constant->initializer->end_line;
  1754. last_assigned_expression = local.constant->initializer;
  1755. }
  1756. break;
  1757. case GDScriptParser::SuiteNode::Local::PARAMETER:
  1758. if (local.parameter->initializer) {
  1759. last_assign_line = local.parameter->initializer->end_line;
  1760. last_assigned_expression = local.parameter->initializer;
  1761. }
  1762. is_function_parameter = true;
  1763. break;
  1764. default:
  1765. break;
  1766. }
  1767. }
  1768. while (suite) {
  1769. for (int i = 0; i < suite->statements.size(); i++) {
  1770. if (suite->statements[i]->end_line >= p_context.current_line) {
  1771. break;
  1772. }
  1773. switch (suite->statements[i]->type) {
  1774. case GDScriptParser::Node::ASSIGNMENT: {
  1775. const GDScriptParser::AssignmentNode *assign = static_cast<const GDScriptParser::AssignmentNode *>(suite->statements[i]);
  1776. if (assign->end_line > last_assign_line && assign->assignee && assign->assigned_value && assign->assignee->type == GDScriptParser::Node::IDENTIFIER) {
  1777. const GDScriptParser::IdentifierNode *id = static_cast<const GDScriptParser::IdentifierNode *>(assign->assignee);
  1778. if (id->name == p_identifier->name && id->source == p_identifier->source) {
  1779. last_assign_line = assign->assigned_value->end_line;
  1780. last_assigned_expression = assign->assigned_value;
  1781. }
  1782. }
  1783. } break;
  1784. default:
  1785. // TODO: Check sub blocks (control flow statements) as they might also reassign stuff.
  1786. break;
  1787. }
  1788. }
  1789. if (suite->parent_if && suite->parent_if->condition && suite->parent_if->condition->type == GDScriptParser::Node::TYPE_TEST) {
  1790. // Operator `is` used, check if identifier is in there! this helps resolve in blocks that are (if (identifier is value)): which are very common..
  1791. // Super dirty hack, but very useful.
  1792. // Credit: Zylann.
  1793. // TODO: this could be hacked to detect ANDed conditions too...
  1794. const GDScriptParser::TypeTestNode *type_test = static_cast<const GDScriptParser::TypeTestNode *>(suite->parent_if->condition);
  1795. if (type_test->operand && type_test->test_type && type_test->operand->type == GDScriptParser::Node::IDENTIFIER && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->name == p_identifier->name && static_cast<const GDScriptParser::IdentifierNode *>(type_test->operand)->source == p_identifier->source) {
  1796. // Bingo.
  1797. GDScriptParser::CompletionContext c = p_context;
  1798. c.current_line = type_test->operand->start_line;
  1799. c.current_suite = suite;
  1800. if (type_test->test_datatype.is_hard_type()) {
  1801. id_type = type_test->test_datatype;
  1802. if (last_assign_line < c.current_line) {
  1803. // Override last assignment.
  1804. last_assign_line = c.current_line;
  1805. last_assigned_expression = nullptr;
  1806. }
  1807. }
  1808. }
  1809. }
  1810. suite = suite->parent_block;
  1811. }
  1812. if (last_assigned_expression && last_assign_line < p_context.current_line) {
  1813. GDScriptParser::CompletionContext c = p_context;
  1814. c.current_line = last_assign_line;
  1815. r_type.assigned_expression = last_assigned_expression;
  1816. if (_guess_expression_type(c, last_assigned_expression, r_type)) {
  1817. return true;
  1818. }
  1819. }
  1820. if (is_function_parameter && p_context.current_function && p_context.current_function->source_lambda == nullptr && p_context.current_class) {
  1821. // Check if it's override of native function, then we can assume the type from the signature.
  1822. GDScriptParser::DataType base_type = p_context.current_class->base_type;
  1823. while (base_type.is_set()) {
  1824. switch (base_type.kind) {
  1825. case GDScriptParser::DataType::CLASS:
  1826. if (base_type.class_type->has_function(p_context.current_function->identifier->name)) {
  1827. GDScriptParser::FunctionNode *parent_function = base_type.class_type->get_member(p_context.current_function->identifier->name).function;
  1828. if (parent_function->parameters_indices.has(p_identifier->name)) {
  1829. const GDScriptParser::ParameterNode *parameter = parent_function->parameters[parent_function->parameters_indices[p_identifier->name]];
  1830. if ((!id_type.is_set() || id_type.is_variant()) && parameter->get_datatype().is_hard_type()) {
  1831. id_type = parameter->get_datatype();
  1832. }
  1833. if (parameter->initializer) {
  1834. GDScriptParser::CompletionContext c = p_context;
  1835. c.current_function = parent_function;
  1836. c.current_class = base_type.class_type;
  1837. c.base = nullptr;
  1838. if (_guess_expression_type(c, parameter->initializer, r_type)) {
  1839. return true;
  1840. }
  1841. }
  1842. }
  1843. }
  1844. base_type = base_type.class_type->base_type;
  1845. break;
  1846. case GDScriptParser::DataType::NATIVE: {
  1847. if (id_type.is_set() && !id_type.is_variant()) {
  1848. base_type = GDScriptParser::DataType();
  1849. break;
  1850. }
  1851. MethodInfo info;
  1852. if (ClassDB::get_method_info(base_type.native_type, p_context.current_function->identifier->name, &info)) {
  1853. for (const PropertyInfo &E : info.arguments) {
  1854. if (E.name == p_identifier->name) {
  1855. r_type = _type_from_property(E);
  1856. return true;
  1857. }
  1858. }
  1859. }
  1860. base_type = GDScriptParser::DataType();
  1861. } break;
  1862. default:
  1863. break;
  1864. }
  1865. }
  1866. }
  1867. if (id_type.is_set() && !id_type.is_variant()) {
  1868. r_type.type = id_type;
  1869. return true;
  1870. }
  1871. // Check current class (including inheritance).
  1872. if (p_context.current_class) {
  1873. GDScriptCompletionIdentifier base;
  1874. base.value = p_context.base;
  1875. base.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1876. base.type.kind = GDScriptParser::DataType::CLASS;
  1877. base.type.class_type = p_context.current_class;
  1878. base.type.is_meta_type = p_context.current_function && p_context.current_function->is_static;
  1879. if (_guess_identifier_type_from_base(p_context, base, p_identifier->name, r_type)) {
  1880. return true;
  1881. }
  1882. }
  1883. // Check global scripts.
  1884. if (ScriptServer::is_global_class(p_identifier->name)) {
  1885. String script = ScriptServer::get_global_class_path(p_identifier->name);
  1886. if (script.to_lower().ends_with(".gd")) {
  1887. Error err = OK;
  1888. Ref<GDScriptParserRef> parser = GDScriptCache::get_parser(script, GDScriptParserRef::INTERFACE_SOLVED, err);
  1889. if (err == OK) {
  1890. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1891. r_type.type.script_path = script;
  1892. r_type.type.class_type = parser->get_parser()->get_tree();
  1893. r_type.type.is_meta_type = true;
  1894. r_type.type.is_constant = false;
  1895. r_type.type.kind = GDScriptParser::DataType::CLASS;
  1896. r_type.value = Variant();
  1897. p_context.dependent_parsers.push_back(parser);
  1898. return true;
  1899. }
  1900. } else {
  1901. Ref<Script> scr = ResourceLoader::load(ScriptServer::get_global_class_path(p_identifier->name));
  1902. if (scr.is_valid()) {
  1903. r_type = _type_from_variant(scr);
  1904. r_type.type.is_meta_type = true;
  1905. return true;
  1906. }
  1907. }
  1908. return false;
  1909. }
  1910. // Check global variables (including autoloads).
  1911. if (GDScriptLanguage::get_singleton()->get_named_globals_map().has(p_identifier->name)) {
  1912. r_type = _type_from_variant(GDScriptLanguage::get_singleton()->get_named_globals_map()[p_identifier->name]);
  1913. return true;
  1914. }
  1915. // Check ClassDB.
  1916. if (ClassDB::class_exists(p_identifier->name) && ClassDB::is_class_exposed(p_identifier->name)) {
  1917. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1918. r_type.type.kind = GDScriptParser::DataType::NATIVE;
  1919. r_type.type.builtin_type = Variant::OBJECT;
  1920. r_type.type.native_type = p_identifier->name;
  1921. r_type.type.is_constant = true;
  1922. if (Engine::get_singleton()->has_singleton(p_identifier->name)) {
  1923. r_type.type.is_meta_type = false;
  1924. r_type.value = Engine::get_singleton()->get_singleton_object(p_identifier->name);
  1925. } else {
  1926. r_type.type.is_meta_type = true;
  1927. r_type.value = Variant();
  1928. }
  1929. }
  1930. return false;
  1931. }
  1932. static bool _guess_identifier_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_identifier, GDScriptCompletionIdentifier &r_type) {
  1933. static int recursion_depth = 0;
  1934. RecursionCheck recursion(&recursion_depth);
  1935. if (unlikely(recursion.check())) {
  1936. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  1937. }
  1938. GDScriptParser::DataType base_type = p_base.type;
  1939. bool is_static = base_type.is_meta_type;
  1940. while (base_type.is_set()) {
  1941. switch (base_type.kind) {
  1942. case GDScriptParser::DataType::CLASS:
  1943. if (base_type.class_type->has_member(p_identifier)) {
  1944. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_identifier);
  1945. switch (member.type) {
  1946. case GDScriptParser::ClassNode::Member::CONSTANT:
  1947. r_type.type = member.constant->get_datatype();
  1948. if (member.constant->initializer && member.constant->initializer->is_constant) {
  1949. r_type.value = member.constant->initializer->reduced_value;
  1950. }
  1951. return true;
  1952. case GDScriptParser::ClassNode::Member::VARIABLE:
  1953. if (!is_static) {
  1954. if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1955. r_type.type = member.variable->get_datatype();
  1956. return true;
  1957. } else if (member.variable->initializer) {
  1958. const GDScriptParser::ExpressionNode *init = member.variable->initializer;
  1959. if (init->is_constant) {
  1960. r_type.value = init->reduced_value;
  1961. r_type = _type_from_variant(init->reduced_value);
  1962. return true;
  1963. } else if (init->start_line == p_context.current_line) {
  1964. return false;
  1965. // Detects if variable is assigned to itself
  1966. } else if (_is_expression_named_identifier(init, member.variable->identifier->name)) {
  1967. if (member.variable->initializer->get_datatype().is_set()) {
  1968. r_type.type = member.variable->initializer->get_datatype();
  1969. } else if (member.variable->get_datatype().is_set() && !member.variable->get_datatype().is_variant()) {
  1970. r_type.type = member.variable->get_datatype();
  1971. }
  1972. return true;
  1973. } else if (_guess_expression_type(p_context, init, r_type)) {
  1974. return true;
  1975. } else if (init->get_datatype().is_set() && !init->get_datatype().is_variant()) {
  1976. r_type.type = init->get_datatype();
  1977. return true;
  1978. }
  1979. }
  1980. }
  1981. // TODO: Check assignments in constructor.
  1982. return false;
  1983. case GDScriptParser::ClassNode::Member::ENUM:
  1984. r_type.type = member.m_enum->get_datatype();
  1985. r_type.enumeration = member.m_enum->identifier->name;
  1986. return true;
  1987. case GDScriptParser::ClassNode::Member::ENUM_VALUE:
  1988. r_type = _type_from_variant(member.enum_value.value);
  1989. return true;
  1990. case GDScriptParser::ClassNode::Member::SIGNAL:
  1991. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  1992. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  1993. r_type.type.builtin_type = Variant::SIGNAL;
  1994. return true;
  1995. case GDScriptParser::ClassNode::Member::FUNCTION:
  1996. if (is_static && !member.function->is_static) {
  1997. return false;
  1998. }
  1999. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2000. r_type.type.kind = GDScriptParser::DataType::BUILTIN;
  2001. r_type.type.builtin_type = Variant::CALLABLE;
  2002. return true;
  2003. case GDScriptParser::ClassNode::Member::CLASS:
  2004. r_type.type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2005. r_type.type.kind = GDScriptParser::DataType::CLASS;
  2006. r_type.type.class_type = member.m_class;
  2007. r_type.type.is_meta_type = true;
  2008. return true;
  2009. case GDScriptParser::ClassNode::Member::GROUP:
  2010. return false; // No-op, but silences warnings.
  2011. case GDScriptParser::ClassNode::Member::UNDEFINED:
  2012. return false; // Unreachable.
  2013. }
  2014. return false;
  2015. }
  2016. base_type = base_type.class_type->base_type;
  2017. break;
  2018. case GDScriptParser::DataType::SCRIPT: {
  2019. Ref<Script> scr = base_type.script_type;
  2020. if (scr.is_valid()) {
  2021. HashMap<StringName, Variant> constants;
  2022. scr->get_constants(&constants);
  2023. if (constants.has(p_identifier)) {
  2024. r_type = _type_from_variant(constants[p_identifier]);
  2025. return true;
  2026. }
  2027. if (!is_static) {
  2028. List<PropertyInfo> members;
  2029. scr->get_script_property_list(&members);
  2030. for (const PropertyInfo &prop : members) {
  2031. if (prop.name == p_identifier) {
  2032. r_type = _type_from_property(prop);
  2033. return true;
  2034. }
  2035. }
  2036. }
  2037. Ref<Script> parent = scr->get_base_script();
  2038. if (parent.is_valid()) {
  2039. base_type.script_type = parent;
  2040. } else {
  2041. base_type.kind = GDScriptParser::DataType::NATIVE;
  2042. base_type.builtin_type = Variant::OBJECT;
  2043. base_type.native_type = scr->get_instance_base_type();
  2044. }
  2045. } else {
  2046. return false;
  2047. }
  2048. } break;
  2049. case GDScriptParser::DataType::NATIVE: {
  2050. StringName class_name = base_type.native_type;
  2051. if (!ClassDB::class_exists(class_name)) {
  2052. return false;
  2053. }
  2054. // Skip constants since they're all integers. Type does not matter because int has no members.
  2055. PropertyInfo prop;
  2056. if (ClassDB::get_property_info(class_name, p_identifier, &prop)) {
  2057. StringName getter = ClassDB::get_property_getter(class_name, p_identifier);
  2058. if (getter != StringName()) {
  2059. MethodBind *g = ClassDB::get_method(class_name, getter);
  2060. if (g) {
  2061. r_type = _type_from_property(g->get_return_info());
  2062. return true;
  2063. }
  2064. } else {
  2065. r_type = _type_from_property(prop);
  2066. return true;
  2067. }
  2068. }
  2069. return false;
  2070. } break;
  2071. case GDScriptParser::DataType::BUILTIN: {
  2072. Callable::CallError err;
  2073. Variant tmp;
  2074. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2075. if (err.error != Callable::CallError::CALL_OK) {
  2076. return false;
  2077. }
  2078. bool valid = false;
  2079. Variant res = tmp.get(p_identifier, &valid);
  2080. if (valid) {
  2081. r_type = _type_from_variant(res);
  2082. r_type.value = Variant();
  2083. r_type.type.is_constant = false;
  2084. return true;
  2085. }
  2086. return false;
  2087. } break;
  2088. default: {
  2089. return false;
  2090. } break;
  2091. }
  2092. }
  2093. return false;
  2094. }
  2095. static void _find_last_return_in_block(GDScriptParser::CompletionContext &p_context, int &r_last_return_line, const GDScriptParser::ExpressionNode **r_last_returned_value) {
  2096. if (!p_context.current_suite) {
  2097. return;
  2098. }
  2099. for (int i = 0; i < p_context.current_suite->statements.size(); i++) {
  2100. if (p_context.current_suite->statements[i]->start_line < r_last_return_line) {
  2101. break;
  2102. }
  2103. GDScriptParser::CompletionContext c = p_context;
  2104. switch (p_context.current_suite->statements[i]->type) {
  2105. case GDScriptParser::Node::FOR:
  2106. c.current_suite = static_cast<const GDScriptParser::ForNode *>(p_context.current_suite->statements[i])->loop;
  2107. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2108. break;
  2109. case GDScriptParser::Node::WHILE:
  2110. c.current_suite = static_cast<const GDScriptParser::WhileNode *>(p_context.current_suite->statements[i])->loop;
  2111. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2112. break;
  2113. case GDScriptParser::Node::IF: {
  2114. const GDScriptParser::IfNode *_if = static_cast<const GDScriptParser::IfNode *>(p_context.current_suite->statements[i]);
  2115. c.current_suite = _if->true_block;
  2116. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2117. if (_if->false_block) {
  2118. c.current_suite = _if->false_block;
  2119. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2120. }
  2121. } break;
  2122. case GDScriptParser::Node::MATCH: {
  2123. const GDScriptParser::MatchNode *match = static_cast<const GDScriptParser::MatchNode *>(p_context.current_suite->statements[i]);
  2124. for (int j = 0; j < match->branches.size(); j++) {
  2125. c.current_suite = match->branches[j]->block;
  2126. _find_last_return_in_block(c, r_last_return_line, r_last_returned_value);
  2127. }
  2128. } break;
  2129. case GDScriptParser::Node::RETURN: {
  2130. const GDScriptParser::ReturnNode *ret = static_cast<const GDScriptParser::ReturnNode *>(p_context.current_suite->statements[i]);
  2131. if (ret->return_value) {
  2132. if (ret->start_line > r_last_return_line) {
  2133. r_last_return_line = ret->start_line;
  2134. *r_last_returned_value = ret->return_value;
  2135. }
  2136. }
  2137. } break;
  2138. default:
  2139. break;
  2140. }
  2141. }
  2142. }
  2143. static bool _guess_method_return_type_from_base(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, GDScriptCompletionIdentifier &r_type) {
  2144. static int recursion_depth = 0;
  2145. RecursionCheck recursion(&recursion_depth);
  2146. if (unlikely(recursion.check())) {
  2147. ERR_FAIL_V_MSG(false, "Reached recursion limit while trying to guess type.");
  2148. }
  2149. GDScriptParser::DataType base_type = p_base.type;
  2150. bool is_static = base_type.is_meta_type;
  2151. if (is_static && p_method == SNAME("new")) {
  2152. r_type.type = base_type;
  2153. r_type.type.is_meta_type = false;
  2154. r_type.type.is_constant = false;
  2155. return true;
  2156. }
  2157. while (base_type.is_set() && !base_type.is_variant()) {
  2158. switch (base_type.kind) {
  2159. case GDScriptParser::DataType::CLASS:
  2160. if (base_type.class_type->has_function(p_method)) {
  2161. const GDScriptParser::FunctionNode *method = base_type.class_type->get_member(p_method).function;
  2162. if (!is_static || method->is_static) {
  2163. if (method->get_datatype().is_set() && !method->get_datatype().is_variant()) {
  2164. r_type.type = method->get_datatype();
  2165. return true;
  2166. }
  2167. int last_return_line = -1;
  2168. const GDScriptParser::ExpressionNode *last_returned_value = nullptr;
  2169. GDScriptParser::CompletionContext c = p_context;
  2170. c.current_class = base_type.class_type;
  2171. c.current_function = const_cast<GDScriptParser::FunctionNode *>(method);
  2172. c.current_suite = method->body;
  2173. _find_last_return_in_block(c, last_return_line, &last_returned_value);
  2174. if (last_returned_value) {
  2175. c.current_line = c.current_suite->end_line;
  2176. if (_guess_expression_type(c, last_returned_value, r_type)) {
  2177. return true;
  2178. }
  2179. }
  2180. }
  2181. }
  2182. base_type = base_type.class_type->base_type;
  2183. break;
  2184. case GDScriptParser::DataType::SCRIPT: {
  2185. Ref<Script> scr = base_type.script_type;
  2186. if (scr.is_valid()) {
  2187. List<MethodInfo> methods;
  2188. scr->get_script_method_list(&methods);
  2189. for (const MethodInfo &mi : methods) {
  2190. if (mi.name == p_method) {
  2191. r_type = _type_from_property(mi.return_val);
  2192. return true;
  2193. }
  2194. }
  2195. Ref<Script> base_script = scr->get_base_script();
  2196. if (base_script.is_valid()) {
  2197. base_type.script_type = base_script;
  2198. } else {
  2199. base_type.kind = GDScriptParser::DataType::NATIVE;
  2200. base_type.builtin_type = Variant::OBJECT;
  2201. base_type.native_type = scr->get_instance_base_type();
  2202. }
  2203. } else {
  2204. return false;
  2205. }
  2206. } break;
  2207. case GDScriptParser::DataType::NATIVE: {
  2208. if (!ClassDB::class_exists(base_type.native_type)) {
  2209. return false;
  2210. }
  2211. MethodBind *mb = ClassDB::get_method(base_type.native_type, p_method);
  2212. if (mb) {
  2213. r_type = _type_from_property(mb->get_return_info());
  2214. return true;
  2215. }
  2216. return false;
  2217. } break;
  2218. case GDScriptParser::DataType::BUILTIN: {
  2219. Callable::CallError err;
  2220. Variant tmp;
  2221. Variant::construct(base_type.builtin_type, tmp, nullptr, 0, err);
  2222. if (err.error != Callable::CallError::CALL_OK) {
  2223. return false;
  2224. }
  2225. List<MethodInfo> methods;
  2226. tmp.get_method_list(&methods);
  2227. for (const MethodInfo &mi : methods) {
  2228. if (mi.name == p_method) {
  2229. r_type = _type_from_property(mi.return_val);
  2230. return true;
  2231. }
  2232. }
  2233. return false;
  2234. } break;
  2235. default: {
  2236. return false;
  2237. }
  2238. }
  2239. }
  2240. return false;
  2241. }
  2242. static void _find_enumeration_candidates(GDScriptParser::CompletionContext &p_context, const String &p_enum_hint, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result) {
  2243. if (!p_enum_hint.contains(".")) {
  2244. // Global constant or in the current class.
  2245. StringName current_enum = p_enum_hint;
  2246. if (p_context.current_class && p_context.current_class->has_member(current_enum) && p_context.current_class->get_member(current_enum).type == GDScriptParser::ClassNode::Member::ENUM) {
  2247. const GDScriptParser::EnumNode *_enum = p_context.current_class->get_member(current_enum).m_enum;
  2248. for (int i = 0; i < _enum->values.size(); i++) {
  2249. ScriptLanguage::CodeCompletionOption option(_enum->values[i].identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2250. r_result.insert(option.display, option);
  2251. }
  2252. } else {
  2253. for (int i = 0; i < CoreConstants::get_global_constant_count(); i++) {
  2254. if (CoreConstants::get_global_constant_enum(i) == current_enum) {
  2255. ScriptLanguage::CodeCompletionOption option(CoreConstants::get_global_constant_name(i), ScriptLanguage::CODE_COMPLETION_KIND_ENUM);
  2256. r_result.insert(option.display, option);
  2257. }
  2258. }
  2259. }
  2260. } else {
  2261. String class_name = p_enum_hint.get_slice(".", 0);
  2262. String enum_name = p_enum_hint.get_slice(".", 1);
  2263. if (!ClassDB::class_exists(class_name)) {
  2264. return;
  2265. }
  2266. List<StringName> enum_constants;
  2267. ClassDB::get_enum_constants(class_name, enum_name, &enum_constants);
  2268. for (const StringName &E : enum_constants) {
  2269. String candidate = class_name + "." + E;
  2270. int location = _get_enum_constant_location(class_name, E);
  2271. ScriptLanguage::CodeCompletionOption option(candidate, ScriptLanguage::CODE_COMPLETION_KIND_ENUM, location);
  2272. r_result.insert(option.display, option);
  2273. }
  2274. }
  2275. }
  2276. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptCompletionIdentifier &p_base, const StringName &p_method, int p_argidx, bool p_static, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, String &r_arghint) {
  2277. Variant base = p_base.value;
  2278. GDScriptParser::DataType base_type = p_base.type;
  2279. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2280. while (base_type.is_set() && !base_type.is_variant()) {
  2281. switch (base_type.kind) {
  2282. case GDScriptParser::DataType::CLASS: {
  2283. if (base_type.class_type->has_member(p_method)) {
  2284. const GDScriptParser::ClassNode::Member &member = base_type.class_type->get_member(p_method);
  2285. if (member.type == GDScriptParser::ClassNode::Member::FUNCTION) {
  2286. r_arghint = _make_arguments_hint(member.function, p_argidx);
  2287. return;
  2288. }
  2289. }
  2290. base_type = base_type.class_type->base_type;
  2291. } break;
  2292. case GDScriptParser::DataType::NATIVE: {
  2293. StringName class_name = base_type.native_type;
  2294. if (!ClassDB::class_exists(class_name)) {
  2295. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2296. break;
  2297. }
  2298. MethodInfo info;
  2299. int method_args = 0;
  2300. if (ClassDB::get_method_info(class_name, p_method, &info)) {
  2301. method_args = info.arguments.size();
  2302. if (base.get_type() == Variant::OBJECT) {
  2303. Object *obj = base.operator Object *();
  2304. if (obj) {
  2305. List<String> options;
  2306. obj->get_argument_options(p_method, p_argidx, &options);
  2307. for (String &opt : options) {
  2308. if (opt.is_quoted()) {
  2309. opt = opt.unquote().quote(quote_style); // Handle user preference.
  2310. }
  2311. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2312. r_result.insert(option.display, option);
  2313. }
  2314. }
  2315. }
  2316. if (p_argidx < method_args) {
  2317. PropertyInfo arg_info = info.arguments[p_argidx];
  2318. if (arg_info.usage & (PROPERTY_USAGE_CLASS_IS_ENUM | PROPERTY_USAGE_CLASS_IS_BITFIELD)) {
  2319. _find_enumeration_candidates(p_context, arg_info.class_name, r_result);
  2320. }
  2321. }
  2322. r_arghint = _make_arguments_hint(info, p_argidx);
  2323. }
  2324. if (p_argidx == 0 && ClassDB::is_parent_class(class_name, SNAME("Node")) && (p_method == SNAME("get_node") || p_method == SNAME("has_node"))) {
  2325. // Get autoloads
  2326. List<PropertyInfo> props;
  2327. ProjectSettings::get_singleton()->get_property_list(&props);
  2328. for (const PropertyInfo &E : props) {
  2329. String s = E.name;
  2330. if (!s.begins_with("autoload/")) {
  2331. continue;
  2332. }
  2333. String name = s.get_slice("/", 1);
  2334. ScriptLanguage::CodeCompletionOption option("/root/" + name, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2335. option.insert_text = option.display.quote(quote_style);
  2336. r_result.insert(option.display, option);
  2337. }
  2338. }
  2339. if (p_argidx == 0 && method_args > 0 && ClassDB::is_parent_class(class_name, SNAME("InputEvent")) && p_method.operator String().contains("action")) {
  2340. // Get input actions
  2341. List<PropertyInfo> props;
  2342. ProjectSettings::get_singleton()->get_property_list(&props);
  2343. for (const PropertyInfo &E : props) {
  2344. String s = E.name;
  2345. if (!s.begins_with("input/")) {
  2346. continue;
  2347. }
  2348. String name = s.get_slice("/", 1);
  2349. ScriptLanguage::CodeCompletionOption option(name, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2350. option.insert_text = option.display.quote(quote_style);
  2351. r_result.insert(option.display, option);
  2352. }
  2353. }
  2354. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2355. } break;
  2356. case GDScriptParser::DataType::BUILTIN: {
  2357. if (base.get_type() == Variant::NIL) {
  2358. Callable::CallError err;
  2359. Variant::construct(base_type.builtin_type, base, nullptr, 0, err);
  2360. if (err.error != Callable::CallError::CALL_OK) {
  2361. return;
  2362. }
  2363. }
  2364. List<MethodInfo> methods;
  2365. base.get_method_list(&methods);
  2366. for (const MethodInfo &E : methods) {
  2367. if (E.name == p_method) {
  2368. r_arghint = _make_arguments_hint(E, p_argidx);
  2369. return;
  2370. }
  2371. }
  2372. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2373. } break;
  2374. default: {
  2375. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2376. } break;
  2377. }
  2378. }
  2379. }
  2380. static bool _get_subscript_type(GDScriptParser::CompletionContext &p_context, const GDScriptParser::SubscriptNode *p_subscript, GDScriptParser::DataType &r_base_type, Variant *r_base = nullptr) {
  2381. if (p_context.base == nullptr) {
  2382. return false;
  2383. }
  2384. const GDScriptParser::GetNodeNode *get_node = nullptr;
  2385. switch (p_subscript->base->type) {
  2386. case GDScriptParser::Node::GET_NODE: {
  2387. get_node = static_cast<GDScriptParser::GetNodeNode *>(p_subscript->base);
  2388. } break;
  2389. case GDScriptParser::Node::IDENTIFIER: {
  2390. const GDScriptParser::IdentifierNode *identifier_node = static_cast<GDScriptParser::IdentifierNode *>(p_subscript->base);
  2391. switch (identifier_node->source) {
  2392. case GDScriptParser::IdentifierNode::Source::MEMBER_VARIABLE: {
  2393. if (p_context.current_class != nullptr) {
  2394. const StringName &member_name = identifier_node->name;
  2395. const GDScriptParser::ClassNode *current_class = p_context.current_class;
  2396. if (current_class->has_member(member_name)) {
  2397. const GDScriptParser::ClassNode::Member &member = current_class->get_member(member_name);
  2398. if (member.type == GDScriptParser::ClassNode::Member::VARIABLE) {
  2399. const GDScriptParser::VariableNode *variable = static_cast<GDScriptParser::VariableNode *>(member.variable);
  2400. if (variable->initializer && variable->initializer->type == GDScriptParser::Node::GET_NODE) {
  2401. get_node = static_cast<GDScriptParser::GetNodeNode *>(variable->initializer);
  2402. }
  2403. }
  2404. }
  2405. }
  2406. } break;
  2407. case GDScriptParser::IdentifierNode::Source::LOCAL_VARIABLE: {
  2408. if (identifier_node->next != nullptr && identifier_node->next->type == GDScriptParser::ClassNode::Node::GET_NODE) {
  2409. get_node = static_cast<GDScriptParser::GetNodeNode *>(identifier_node->next);
  2410. }
  2411. } break;
  2412. default: {
  2413. } break;
  2414. }
  2415. } break;
  2416. default: {
  2417. } break;
  2418. }
  2419. if (get_node != nullptr) {
  2420. const Object *node = p_context.base->call("get_node_or_null", NodePath(get_node->full_path));
  2421. if (node != nullptr) {
  2422. if (r_base != nullptr) {
  2423. *r_base = node;
  2424. }
  2425. r_base_type.type_source = GDScriptParser::DataType::ANNOTATED_EXPLICIT;
  2426. r_base_type.kind = GDScriptParser::DataType::NATIVE;
  2427. r_base_type.builtin_type = Variant::OBJECT;
  2428. r_base_type.native_type = node->get_class_name();
  2429. return true;
  2430. }
  2431. }
  2432. return false;
  2433. }
  2434. static void _find_call_arguments(GDScriptParser::CompletionContext &p_context, const GDScriptParser::Node *p_call, int p_argidx, HashMap<String, ScriptLanguage::CodeCompletionOption> &r_result, bool &r_forced, String &r_arghint) {
  2435. if (p_call->type == GDScriptParser::Node::PRELOAD) {
  2436. if (p_argidx == 0 && bool(EDITOR_GET("text_editor/completion/complete_file_paths"))) {
  2437. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), r_result);
  2438. }
  2439. MethodInfo mi(PropertyInfo(Variant::OBJECT, "resource", PROPERTY_HINT_RESOURCE_TYPE, "Resource"), "preload", PropertyInfo(Variant::STRING, "path"));
  2440. r_arghint = _make_arguments_hint(mi, p_argidx);
  2441. return;
  2442. } else if (p_call->type != GDScriptParser::Node::CALL) {
  2443. return;
  2444. }
  2445. Variant base;
  2446. GDScriptParser::DataType base_type;
  2447. bool _static = false;
  2448. const GDScriptParser::CallNode *call = static_cast<const GDScriptParser::CallNode *>(p_call);
  2449. GDScriptParser::Node::Type callee_type = call->get_callee_type();
  2450. GDScriptCompletionIdentifier connect_base;
  2451. if (callee_type == GDScriptParser::Node::SUBSCRIPT) {
  2452. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(call->callee);
  2453. if (subscript->base != nullptr && subscript->base->type == GDScriptParser::Node::IDENTIFIER) {
  2454. const GDScriptParser::IdentifierNode *base_identifier = static_cast<const GDScriptParser::IdentifierNode *>(subscript->base);
  2455. Variant::Type method_type = GDScriptParser::get_builtin_type(base_identifier->name);
  2456. if (method_type < Variant::VARIANT_MAX) {
  2457. Variant v;
  2458. Callable::CallError err;
  2459. Variant::construct(method_type, v, nullptr, 0, err);
  2460. if (err.error != Callable::CallError::CALL_OK) {
  2461. return;
  2462. }
  2463. List<MethodInfo> methods;
  2464. v.get_method_list(&methods);
  2465. for (MethodInfo &E : methods) {
  2466. if (p_argidx >= E.arguments.size()) {
  2467. continue;
  2468. }
  2469. if (E.name == call->function_name) {
  2470. r_arghint += _make_arguments_hint(E, p_argidx);
  2471. return;
  2472. }
  2473. }
  2474. }
  2475. }
  2476. if (subscript->is_attribute) {
  2477. bool found_type = _get_subscript_type(p_context, subscript, base_type, &base);
  2478. if (!found_type) {
  2479. GDScriptCompletionIdentifier ci;
  2480. if (_guess_expression_type(p_context, subscript->base, ci)) {
  2481. base_type = ci.type;
  2482. base = ci.value;
  2483. } else {
  2484. return;
  2485. }
  2486. }
  2487. _static = base_type.is_meta_type;
  2488. }
  2489. } else if (Variant::has_utility_function(call->function_name)) {
  2490. MethodInfo info = Variant::get_utility_function_info(call->function_name);
  2491. r_arghint = _make_arguments_hint(info, p_argidx);
  2492. return;
  2493. } else if (GDScriptUtilityFunctions::function_exists(call->function_name)) {
  2494. MethodInfo info = GDScriptUtilityFunctions::get_function_info(call->function_name);
  2495. r_arghint = _make_arguments_hint(info, p_argidx);
  2496. return;
  2497. } else if (GDScriptParser::get_builtin_type(call->function_name) < Variant::VARIANT_MAX) {
  2498. // Complete constructor.
  2499. List<MethodInfo> constructors;
  2500. Variant::get_constructor_list(GDScriptParser::get_builtin_type(call->function_name), &constructors);
  2501. int i = 0;
  2502. for (const MethodInfo &E : constructors) {
  2503. if (p_argidx >= E.arguments.size()) {
  2504. continue;
  2505. }
  2506. if (i > 0) {
  2507. r_arghint += "\n";
  2508. }
  2509. r_arghint += _make_arguments_hint(E, p_argidx);
  2510. i++;
  2511. }
  2512. return;
  2513. } else if (call->is_super || callee_type == GDScriptParser::Node::IDENTIFIER) {
  2514. base = p_context.base;
  2515. if (p_context.current_class) {
  2516. base_type = p_context.current_class->get_datatype();
  2517. _static = !p_context.current_function || p_context.current_function->is_static;
  2518. }
  2519. } else {
  2520. return;
  2521. }
  2522. GDScriptCompletionIdentifier ci;
  2523. ci.type = base_type;
  2524. ci.value = base;
  2525. _find_call_arguments(p_context, ci, call->function_name, p_argidx, _static, r_result, r_arghint);
  2526. r_forced = r_result.size() > 0;
  2527. }
  2528. ::Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2529. const String quote_style = EDITOR_GET("text_editor/completion/use_single_quotes") ? "'" : "\"";
  2530. GDScriptParser parser;
  2531. GDScriptAnalyzer analyzer(&parser);
  2532. parser.parse(p_code, p_path, true);
  2533. analyzer.analyze();
  2534. r_forced = false;
  2535. HashMap<String, ScriptLanguage::CodeCompletionOption> options;
  2536. GDScriptParser::CompletionContext completion_context = parser.get_completion_context();
  2537. completion_context.base = p_owner;
  2538. bool is_function = false;
  2539. switch (completion_context.type) {
  2540. case GDScriptParser::COMPLETION_NONE:
  2541. break;
  2542. case GDScriptParser::COMPLETION_ANNOTATION: {
  2543. List<MethodInfo> annotations;
  2544. parser.get_annotation_list(&annotations);
  2545. for (const MethodInfo &E : annotations) {
  2546. ScriptLanguage::CodeCompletionOption option(E.name.substr(1), ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2547. if (E.arguments.size() > 0) {
  2548. option.insert_text += "(";
  2549. }
  2550. options.insert(option.display, option);
  2551. }
  2552. r_forced = true;
  2553. } break;
  2554. case GDScriptParser::COMPLETION_ANNOTATION_ARGUMENTS: {
  2555. if (completion_context.node == nullptr || completion_context.node->type != GDScriptParser::Node::ANNOTATION) {
  2556. break;
  2557. }
  2558. const GDScriptParser::AnnotationNode *annotation = static_cast<const GDScriptParser::AnnotationNode *>(completion_context.node);
  2559. _find_annotation_arguments(annotation, completion_context.current_argument, quote_style, options);
  2560. r_forced = true;
  2561. } break;
  2562. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  2563. // Constants.
  2564. {
  2565. List<StringName> constants;
  2566. Variant::get_constants_for_type(completion_context.builtin_type, &constants);
  2567. for (const StringName &E : constants) {
  2568. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_CONSTANT);
  2569. bool valid = false;
  2570. Variant default_value = Variant::get_constant_value(completion_context.builtin_type, E, &valid);
  2571. if (valid) {
  2572. option.default_value = default_value;
  2573. }
  2574. options.insert(option.display, option);
  2575. }
  2576. }
  2577. // Methods.
  2578. {
  2579. List<StringName> methods;
  2580. Variant::get_builtin_method_list(completion_context.builtin_type, &methods);
  2581. for (const StringName &E : methods) {
  2582. if (Variant::is_builtin_method_static(completion_context.builtin_type, E)) {
  2583. ScriptLanguage::CodeCompletionOption option(E, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2584. if (Variant::get_builtin_method_argument_count(completion_context.builtin_type, E) > 0 || Variant::is_builtin_method_vararg(completion_context.builtin_type, E)) {
  2585. option.insert_text += "(";
  2586. } else {
  2587. option.insert_text += "()";
  2588. }
  2589. options.insert(option.display, option);
  2590. }
  2591. }
  2592. }
  2593. } break;
  2594. case GDScriptParser::COMPLETION_INHERIT_TYPE: {
  2595. _list_available_types(true, completion_context, options);
  2596. r_forced = true;
  2597. } break;
  2598. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID: {
  2599. ScriptLanguage::CodeCompletionOption option("void", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2600. options.insert(option.display, option);
  2601. }
  2602. [[fallthrough]];
  2603. case GDScriptParser::COMPLETION_TYPE_NAME: {
  2604. _list_available_types(false, completion_context, options);
  2605. r_forced = true;
  2606. } break;
  2607. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE: {
  2608. _list_available_types(false, completion_context, options);
  2609. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2610. options.insert(get.display, get);
  2611. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2612. options.insert(set.display, set);
  2613. r_forced = true;
  2614. } break;
  2615. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION: {
  2616. ScriptLanguage::CodeCompletionOption get("get", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2617. options.insert(get.display, get);
  2618. ScriptLanguage::CodeCompletionOption set("set", ScriptLanguage::CODE_COMPLETION_KIND_PLAIN_TEXT);
  2619. options.insert(set.display, set);
  2620. r_forced = true;
  2621. } break;
  2622. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  2623. if (!completion_context.current_class) {
  2624. break;
  2625. }
  2626. for (int i = 0; i < completion_context.current_class->members.size(); i++) {
  2627. const GDScriptParser::ClassNode::Member &member = completion_context.current_class->members[i];
  2628. if (member.type != GDScriptParser::ClassNode::Member::FUNCTION) {
  2629. continue;
  2630. }
  2631. if (member.function->is_static) {
  2632. continue;
  2633. }
  2634. ScriptLanguage::CodeCompletionOption option(member.function->identifier->name, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2635. options.insert(option.display, option);
  2636. }
  2637. r_forced = true;
  2638. } break;
  2639. case GDScriptParser::COMPLETION_ASSIGN: {
  2640. GDScriptCompletionIdentifier type;
  2641. if (!completion_context.node || completion_context.node->type != GDScriptParser::Node::ASSIGNMENT) {
  2642. break;
  2643. }
  2644. if (!_guess_expression_type(completion_context, static_cast<const GDScriptParser::AssignmentNode *>(completion_context.node)->assignee, type)) {
  2645. _find_identifiers(completion_context, false, options, 0);
  2646. r_forced = true;
  2647. break;
  2648. }
  2649. if (!type.enumeration.is_empty()) {
  2650. _find_enumeration_candidates(completion_context, type.enumeration, options);
  2651. r_forced = options.size() > 0;
  2652. } else {
  2653. _find_identifiers(completion_context, false, options, 0);
  2654. r_forced = true;
  2655. }
  2656. } break;
  2657. case GDScriptParser::COMPLETION_METHOD:
  2658. is_function = true;
  2659. [[fallthrough]];
  2660. case GDScriptParser::COMPLETION_IDENTIFIER: {
  2661. _find_identifiers(completion_context, is_function, options, 0);
  2662. } break;
  2663. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD:
  2664. is_function = true;
  2665. [[fallthrough]];
  2666. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  2667. r_forced = true;
  2668. const GDScriptParser::SubscriptNode *attr = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2669. if (attr->base) {
  2670. GDScriptCompletionIdentifier base;
  2671. bool found_type = _get_subscript_type(completion_context, attr, base.type);
  2672. if (!found_type && !_guess_expression_type(completion_context, attr->base, base)) {
  2673. break;
  2674. }
  2675. _find_identifiers_in_base(base, is_function, options, 0);
  2676. }
  2677. } break;
  2678. case GDScriptParser::COMPLETION_SUBSCRIPT: {
  2679. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(completion_context.node);
  2680. GDScriptCompletionIdentifier base;
  2681. if (!_guess_expression_type(completion_context, subscript->base, base)) {
  2682. break;
  2683. }
  2684. _find_identifiers_in_base(base, false, options, 0);
  2685. } break;
  2686. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  2687. if (!completion_context.current_class) {
  2688. break;
  2689. }
  2690. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(completion_context.node);
  2691. bool found = true;
  2692. GDScriptCompletionIdentifier base;
  2693. base.type.kind = GDScriptParser::DataType::CLASS;
  2694. base.type.type_source = GDScriptParser::DataType::INFERRED;
  2695. base.type.is_constant = true;
  2696. base.type.class_type = completion_context.current_class;
  2697. base.value = completion_context.base;
  2698. for (int i = 0; i < completion_context.current_argument; i++) {
  2699. GDScriptCompletionIdentifier ci;
  2700. if (!_guess_identifier_type_from_base(completion_context, base, type->type_chain[i]->name, ci)) {
  2701. found = false;
  2702. break;
  2703. }
  2704. base = ci;
  2705. }
  2706. // TODO: Improve this to only list types.
  2707. if (found) {
  2708. _find_identifiers_in_base(base, false, options, 0);
  2709. }
  2710. r_forced = true;
  2711. } break;
  2712. case GDScriptParser::COMPLETION_RESOURCE_PATH: {
  2713. if (EDITOR_GET("text_editor/completion/complete_file_paths")) {
  2714. _get_directory_contents(EditorFileSystem::get_singleton()->get_filesystem(), options);
  2715. r_forced = true;
  2716. }
  2717. } break;
  2718. case GDScriptParser::COMPLETION_CALL_ARGUMENTS: {
  2719. if (!completion_context.node) {
  2720. break;
  2721. }
  2722. _find_call_arguments(completion_context, completion_context.node, completion_context.current_argument, options, r_forced, r_call_hint);
  2723. } break;
  2724. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  2725. GDScriptParser::DataType native_type = completion_context.current_class->base_type;
  2726. while (native_type.is_set() && native_type.kind != GDScriptParser::DataType::NATIVE) {
  2727. switch (native_type.kind) {
  2728. case GDScriptParser::DataType::CLASS: {
  2729. native_type = native_type.class_type->base_type;
  2730. } break;
  2731. default: {
  2732. native_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2733. } break;
  2734. }
  2735. }
  2736. if (!native_type.is_set()) {
  2737. break;
  2738. }
  2739. StringName class_name = native_type.native_type;
  2740. if (!ClassDB::class_exists(class_name)) {
  2741. break;
  2742. }
  2743. bool use_type_hint = EditorSettings::get_singleton()->get_setting("text_editor/completion/add_type_hints").operator bool();
  2744. List<MethodInfo> virtual_methods;
  2745. ClassDB::get_virtual_methods(class_name, &virtual_methods);
  2746. {
  2747. // Not truly a virtual method, but can also be "overridden".
  2748. MethodInfo static_init("_static_init");
  2749. static_init.return_val.type = Variant::NIL;
  2750. static_init.flags |= METHOD_FLAG_STATIC | METHOD_FLAG_VIRTUAL;
  2751. virtual_methods.push_back(static_init);
  2752. }
  2753. for (const MethodInfo &mi : virtual_methods) {
  2754. String method_hint = mi.name;
  2755. if (method_hint.contains(":")) {
  2756. method_hint = method_hint.get_slice(":", 0);
  2757. }
  2758. method_hint += "(";
  2759. if (mi.arguments.size()) {
  2760. for (int i = 0; i < mi.arguments.size(); i++) {
  2761. if (i > 0) {
  2762. method_hint += ", ";
  2763. }
  2764. String arg = mi.arguments[i].name;
  2765. if (arg.contains(":")) {
  2766. arg = arg.substr(0, arg.find(":"));
  2767. }
  2768. method_hint += arg;
  2769. if (use_type_hint) {
  2770. method_hint += ": " + _get_visual_datatype(mi.arguments[i], true, class_name);
  2771. }
  2772. }
  2773. }
  2774. method_hint += ")";
  2775. if (use_type_hint) {
  2776. method_hint += " -> " + _get_visual_datatype(mi.return_val, false, class_name);
  2777. }
  2778. method_hint += ":";
  2779. ScriptLanguage::CodeCompletionOption option(method_hint, ScriptLanguage::CODE_COMPLETION_KIND_FUNCTION);
  2780. options.insert(option.display, option);
  2781. }
  2782. } break;
  2783. case GDScriptParser::COMPLETION_GET_NODE: {
  2784. // Handles the `$Node/Path` or `$"Some NodePath"` syntax specifically.
  2785. if (p_owner) {
  2786. List<String> opts;
  2787. p_owner->get_argument_options("get_node", 0, &opts);
  2788. for (const String &E : opts) {
  2789. r_forced = true;
  2790. String opt = E.strip_edges();
  2791. if (opt.is_quoted()) {
  2792. // Remove quotes so that we can handle user preferred quote style,
  2793. // or handle NodePaths which are valid identifiers and don't need quotes.
  2794. opt = opt.unquote();
  2795. }
  2796. // The path needs quotes if it's not a valid identifier (with an exception
  2797. // for "/" as path separator, which also doesn't require quotes).
  2798. if (!opt.replace("/", "_").is_valid_identifier()) {
  2799. // Ignore quote_style and just use double quotes for paths with apostrophes.
  2800. // Double quotes don't need to be checked because they're not valid in node and property names.
  2801. opt = opt.quote(opt.contains("'") ? "\"" : quote_style); // Handle user preference.
  2802. }
  2803. ScriptLanguage::CodeCompletionOption option(opt, ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2804. options.insert(option.display, option);
  2805. }
  2806. // Get autoloads.
  2807. for (const KeyValue<StringName, ProjectSettings::AutoloadInfo> &E : ProjectSettings::get_singleton()->get_autoload_list()) {
  2808. String path = "/root/" + E.key;
  2809. ScriptLanguage::CodeCompletionOption option(path.quote(quote_style), ScriptLanguage::CODE_COMPLETION_KIND_NODE_PATH);
  2810. options.insert(option.display, option);
  2811. }
  2812. }
  2813. } break;
  2814. case GDScriptParser::COMPLETION_SUPER_METHOD: {
  2815. if (!completion_context.current_class) {
  2816. break;
  2817. }
  2818. _find_identifiers_in_class(completion_context.current_class, true, false, true, options, 0);
  2819. } break;
  2820. }
  2821. for (const KeyValue<String, ScriptLanguage::CodeCompletionOption> &E : options) {
  2822. r_options->push_back(E.value);
  2823. }
  2824. return OK;
  2825. }
  2826. #else
  2827. Error GDScriptLanguage::complete_code(const String &p_code, const String &p_path, Object *p_owner, List<ScriptLanguage::CodeCompletionOption> *r_options, bool &r_forced, String &r_call_hint) {
  2828. return OK;
  2829. }
  2830. #endif
  2831. //////// END COMPLETION //////////
  2832. String GDScriptLanguage::_get_indentation() const {
  2833. #ifdef TOOLS_ENABLED
  2834. if (Engine::get_singleton()->is_editor_hint()) {
  2835. bool use_space_indentation = EDITOR_GET("text_editor/behavior/indent/type");
  2836. if (use_space_indentation) {
  2837. int indent_size = EDITOR_GET("text_editor/behavior/indent/size");
  2838. return String(" ").repeat(indent_size);
  2839. }
  2840. }
  2841. #endif
  2842. return "\t";
  2843. }
  2844. void GDScriptLanguage::auto_indent_code(String &p_code, int p_from_line, int p_to_line) const {
  2845. String indent = _get_indentation();
  2846. Vector<String> lines = p_code.split("\n");
  2847. List<int> indent_stack;
  2848. for (int i = 0; i < lines.size(); i++) {
  2849. String l = lines[i];
  2850. int tc = 0;
  2851. for (int j = 0; j < l.length(); j++) {
  2852. if (l[j] == ' ' || l[j] == '\t') {
  2853. tc++;
  2854. } else {
  2855. break;
  2856. }
  2857. }
  2858. String st = l.substr(tc, l.length()).strip_edges();
  2859. if (st.is_empty() || st.begins_with("#")) {
  2860. continue; //ignore!
  2861. }
  2862. int ilevel = 0;
  2863. if (indent_stack.size()) {
  2864. ilevel = indent_stack.back()->get();
  2865. }
  2866. if (tc > ilevel) {
  2867. indent_stack.push_back(tc);
  2868. } else if (tc < ilevel) {
  2869. while (indent_stack.size() && indent_stack.back()->get() > tc) {
  2870. indent_stack.pop_back();
  2871. }
  2872. if (indent_stack.size() && indent_stack.back()->get() != tc) {
  2873. indent_stack.push_back(tc); // this is not right but gets the job done
  2874. }
  2875. }
  2876. if (i >= p_from_line) {
  2877. l = indent.repeat(indent_stack.size()) + st;
  2878. } else if (i > p_to_line) {
  2879. break;
  2880. }
  2881. lines.write[i] = l;
  2882. }
  2883. p_code = "";
  2884. for (int i = 0; i < lines.size(); i++) {
  2885. if (i > 0) {
  2886. p_code += "\n";
  2887. }
  2888. p_code += lines[i];
  2889. }
  2890. }
  2891. #ifdef TOOLS_ENABLED
  2892. static Error _lookup_symbol_from_base(const GDScriptParser::DataType &p_base, const String &p_symbol, bool p_is_function, GDScriptLanguage::LookupResult &r_result) {
  2893. GDScriptParser::DataType base_type = p_base;
  2894. while (base_type.is_set()) {
  2895. switch (base_type.kind) {
  2896. case GDScriptParser::DataType::CLASS: {
  2897. if (base_type.class_type) {
  2898. if (base_type.class_type->has_member(p_symbol)) {
  2899. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2900. r_result.location = base_type.class_type->get_member(p_symbol).get_line();
  2901. r_result.class_path = base_type.script_path;
  2902. Error err = OK;
  2903. r_result.script = GDScriptCache::get_shallow_script(r_result.class_path, err);
  2904. return err;
  2905. }
  2906. base_type = base_type.class_type->base_type;
  2907. }
  2908. } break;
  2909. case GDScriptParser::DataType::SCRIPT: {
  2910. Ref<Script> scr = base_type.script_type;
  2911. if (scr.is_valid()) {
  2912. int line = scr->get_member_line(p_symbol);
  2913. if (line >= 0) {
  2914. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  2915. r_result.location = line;
  2916. r_result.script = scr;
  2917. return OK;
  2918. }
  2919. Ref<Script> base_script = scr->get_base_script();
  2920. if (base_script.is_valid()) {
  2921. base_type.script_type = base_script;
  2922. } else {
  2923. base_type.kind = GDScriptParser::DataType::NATIVE;
  2924. base_type.builtin_type = Variant::OBJECT;
  2925. base_type.native_type = scr->get_instance_base_type();
  2926. }
  2927. } else {
  2928. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2929. }
  2930. } break;
  2931. case GDScriptParser::DataType::NATIVE: {
  2932. StringName class_name = base_type.native_type;
  2933. if (!ClassDB::class_exists(class_name)) {
  2934. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2935. break;
  2936. }
  2937. if (ClassDB::has_method(class_name, p_symbol, true)) {
  2938. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2939. r_result.class_name = base_type.native_type;
  2940. r_result.class_member = p_symbol;
  2941. return OK;
  2942. }
  2943. List<MethodInfo> virtual_methods;
  2944. ClassDB::get_virtual_methods(class_name, &virtual_methods, true);
  2945. for (const MethodInfo &E : virtual_methods) {
  2946. if (E.name == p_symbol) {
  2947. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  2948. r_result.class_name = base_type.native_type;
  2949. r_result.class_member = p_symbol;
  2950. return OK;
  2951. }
  2952. }
  2953. if (ClassDB::has_signal(class_name, p_symbol, true)) {
  2954. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_SIGNAL;
  2955. r_result.class_name = base_type.native_type;
  2956. r_result.class_member = p_symbol;
  2957. return OK;
  2958. }
  2959. StringName enum_name = ClassDB::get_integer_constant_enum(class_name, p_symbol, true);
  2960. if (enum_name != StringName()) {
  2961. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  2962. r_result.class_name = base_type.native_type;
  2963. r_result.class_member = enum_name;
  2964. return OK;
  2965. }
  2966. List<String> constants;
  2967. ClassDB::get_integer_constant_list(class_name, &constants, true);
  2968. for (const String &E : constants) {
  2969. if (E == p_symbol) {
  2970. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2971. r_result.class_name = base_type.native_type;
  2972. r_result.class_member = p_symbol;
  2973. return OK;
  2974. }
  2975. }
  2976. if (ClassDB::has_property(class_name, p_symbol, true)) {
  2977. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  2978. r_result.class_name = base_type.native_type;
  2979. r_result.class_member = p_symbol;
  2980. return OK;
  2981. }
  2982. StringName parent = ClassDB::get_parent_class(class_name);
  2983. if (parent != StringName()) {
  2984. base_type.native_type = parent;
  2985. } else {
  2986. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2987. }
  2988. } break;
  2989. case GDScriptParser::DataType::BUILTIN: {
  2990. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  2991. if (Variant::has_constant(base_type.builtin_type, p_symbol)) {
  2992. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  2993. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  2994. r_result.class_member = p_symbol;
  2995. return OK;
  2996. }
  2997. Variant v;
  2998. Ref<RefCounted> v_ref;
  2999. if (base_type.builtin_type == Variant::OBJECT) {
  3000. v_ref.instantiate();
  3001. v = v_ref;
  3002. } else {
  3003. Callable::CallError err;
  3004. Variant::construct(base_type.builtin_type, v, nullptr, 0, err);
  3005. if (err.error != Callable::CallError::CALL_OK) {
  3006. break;
  3007. }
  3008. }
  3009. if (v.has_method(p_symbol)) {
  3010. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3011. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3012. r_result.class_member = p_symbol;
  3013. return OK;
  3014. }
  3015. bool valid = false;
  3016. v.get(p_symbol, &valid);
  3017. if (valid) {
  3018. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_PROPERTY;
  3019. r_result.class_name = Variant::get_type_name(base_type.builtin_type);
  3020. r_result.class_member = p_symbol;
  3021. return OK;
  3022. }
  3023. } break;
  3024. default: {
  3025. base_type.kind = GDScriptParser::DataType::UNRESOLVED;
  3026. } break;
  3027. }
  3028. }
  3029. return ERR_CANT_RESOLVE;
  3030. }
  3031. ::Error GDScriptLanguage::lookup_code(const String &p_code, const String &p_symbol, const String &p_path, Object *p_owner, LookupResult &r_result) {
  3032. // Before parsing, try the usual stuff.
  3033. if (ClassDB::class_exists(p_symbol)) {
  3034. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3035. r_result.class_name = p_symbol;
  3036. return OK;
  3037. }
  3038. for (int i = 0; i < Variant::VARIANT_MAX; i++) {
  3039. Variant::Type t = Variant::Type(i);
  3040. if (Variant::get_type_name(t) == p_symbol) {
  3041. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3042. r_result.class_name = Variant::get_type_name(t);
  3043. return OK;
  3044. }
  3045. }
  3046. if ("Variant" == p_symbol) {
  3047. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3048. r_result.class_name = "Variant";
  3049. return OK;
  3050. }
  3051. if ("PI" == p_symbol || "TAU" == p_symbol || "INF" == p_symbol || "NAN" == p_symbol) {
  3052. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3053. r_result.class_name = "@GDScript";
  3054. r_result.class_member = p_symbol;
  3055. return OK;
  3056. }
  3057. GDScriptParser parser;
  3058. parser.parse(p_code, p_path, true);
  3059. GDScriptParser::CompletionContext context = parser.get_completion_context();
  3060. context.base = p_owner;
  3061. // Allows class functions with the names like built-ins to be handled properly.
  3062. if (context.type != GDScriptParser::COMPLETION_ATTRIBUTE) {
  3063. // Need special checks for assert and preload as they are technically
  3064. // keywords, so are not registered in GDScriptUtilityFunctions.
  3065. if (GDScriptUtilityFunctions::function_exists(p_symbol) || "assert" == p_symbol || "preload" == p_symbol) {
  3066. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_METHOD;
  3067. r_result.class_name = "@GDScript";
  3068. r_result.class_member = p_symbol;
  3069. return OK;
  3070. }
  3071. }
  3072. GDScriptAnalyzer analyzer(&parser);
  3073. analyzer.analyze();
  3074. if (context.current_class && context.current_class->extends.size() > 0) {
  3075. bool success = false;
  3076. ClassDB::get_integer_constant(context.current_class->extends[0]->name, p_symbol, &success);
  3077. if (success) {
  3078. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3079. r_result.class_name = context.current_class->extends[0]->name;
  3080. r_result.class_member = p_symbol;
  3081. return OK;
  3082. }
  3083. }
  3084. bool is_function = false;
  3085. switch (context.type) {
  3086. case GDScriptParser::COMPLETION_BUILT_IN_TYPE_CONSTANT_OR_STATIC_METHOD: {
  3087. if (!Variant::has_builtin_method(context.builtin_type, StringName(p_symbol))) {
  3088. // A constant.
  3089. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3090. r_result.class_name = Variant::get_type_name(context.builtin_type);
  3091. r_result.class_member = p_symbol;
  3092. return OK;
  3093. }
  3094. // A method.
  3095. GDScriptParser::DataType base_type;
  3096. base_type.kind = GDScriptParser::DataType::BUILTIN;
  3097. base_type.builtin_type = context.builtin_type;
  3098. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3099. return OK;
  3100. }
  3101. } break;
  3102. case GDScriptParser::COMPLETION_SUPER_METHOD:
  3103. case GDScriptParser::COMPLETION_METHOD: {
  3104. is_function = true;
  3105. [[fallthrough]];
  3106. }
  3107. case GDScriptParser::COMPLETION_ASSIGN:
  3108. case GDScriptParser::COMPLETION_CALL_ARGUMENTS:
  3109. case GDScriptParser::COMPLETION_IDENTIFIER:
  3110. case GDScriptParser::COMPLETION_PROPERTY_METHOD: {
  3111. GDScriptParser::DataType base_type;
  3112. if (context.current_class) {
  3113. if (context.type != GDScriptParser::COMPLETION_SUPER_METHOD) {
  3114. base_type = context.current_class->get_datatype();
  3115. } else {
  3116. base_type = context.current_class->base_type;
  3117. }
  3118. } else {
  3119. break;
  3120. }
  3121. if (!is_function && context.current_suite) {
  3122. // Lookup local variables.
  3123. const GDScriptParser::SuiteNode *suite = context.current_suite;
  3124. while (suite) {
  3125. if (suite->has_local(p_symbol)) {
  3126. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  3127. r_result.location = suite->get_local(p_symbol).start_line;
  3128. return OK;
  3129. }
  3130. suite = suite->parent_block;
  3131. }
  3132. }
  3133. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  3134. return OK;
  3135. }
  3136. if (!is_function) {
  3137. // Guess in autoloads as singletons.
  3138. if (ProjectSettings::get_singleton()->has_autoload(p_symbol)) {
  3139. const ProjectSettings::AutoloadInfo &autoload = ProjectSettings::get_singleton()->get_autoload(p_symbol);
  3140. if (autoload.is_singleton) {
  3141. String scr_path = autoload.path;
  3142. if (!scr_path.ends_with(".gd")) {
  3143. // Not a script, try find the script anyway,
  3144. // may have some success.
  3145. scr_path = scr_path.get_basename() + ".gd";
  3146. }
  3147. if (FileAccess::exists(scr_path)) {
  3148. r_result.type = ScriptLanguage::LOOKUP_RESULT_SCRIPT_LOCATION;
  3149. r_result.location = 0;
  3150. r_result.script = ResourceLoader::load(scr_path);
  3151. return OK;
  3152. }
  3153. }
  3154. }
  3155. // Global.
  3156. HashMap<StringName, int> classes = GDScriptLanguage::get_singleton()->get_global_map();
  3157. if (classes.has(p_symbol)) {
  3158. Variant value = GDScriptLanguage::get_singleton()->get_global_array()[classes[p_symbol]];
  3159. if (value.get_type() == Variant::OBJECT) {
  3160. Object *obj = value;
  3161. if (obj) {
  3162. if (Object::cast_to<GDScriptNativeClass>(obj)) {
  3163. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3164. r_result.class_name = Object::cast_to<GDScriptNativeClass>(obj)->get_name();
  3165. } else {
  3166. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS;
  3167. r_result.class_name = obj->get_class();
  3168. }
  3169. // proxy class remove the underscore.
  3170. if (r_result.class_name.begins_with("_")) {
  3171. r_result.class_name = r_result.class_name.substr(1);
  3172. }
  3173. return OK;
  3174. }
  3175. } else {
  3176. /*
  3177. // Because get_integer_constant_enum and get_integer_constant don't work on @GlobalScope
  3178. // We cannot determine the exact nature of the identifier here
  3179. // Otherwise these codes would work
  3180. StringName enumName = ClassDB::get_integer_constant_enum("@GlobalScope", p_symbol, true);
  3181. if (enumName != nullptr) {
  3182. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ENUM;
  3183. r_result.class_name = "@GlobalScope";
  3184. r_result.class_member = enumName;
  3185. return OK;
  3186. }
  3187. else {
  3188. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_CONSTANT;
  3189. r_result.class_name = "@GlobalScope";
  3190. r_result.class_member = p_symbol;
  3191. return OK;
  3192. }*/
  3193. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  3194. r_result.class_name = "@GlobalScope";
  3195. r_result.class_member = p_symbol;
  3196. return OK;
  3197. }
  3198. } else {
  3199. List<StringName> utility_functions;
  3200. Variant::get_utility_function_list(&utility_functions);
  3201. if (utility_functions.find(p_symbol) != nullptr) {
  3202. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_TBD_GLOBALSCOPE;
  3203. r_result.class_name = "@GlobalScope";
  3204. r_result.class_member = p_symbol;
  3205. return OK;
  3206. }
  3207. }
  3208. }
  3209. } break;
  3210. case GDScriptParser::COMPLETION_ATTRIBUTE_METHOD: {
  3211. is_function = true;
  3212. [[fallthrough]];
  3213. }
  3214. case GDScriptParser::COMPLETION_ATTRIBUTE: {
  3215. if (context.node->type != GDScriptParser::Node::SUBSCRIPT) {
  3216. break;
  3217. }
  3218. const GDScriptParser::SubscriptNode *subscript = static_cast<const GDScriptParser::SubscriptNode *>(context.node);
  3219. if (!subscript->is_attribute) {
  3220. break;
  3221. }
  3222. GDScriptCompletionIdentifier base;
  3223. bool found_type = _get_subscript_type(context, subscript, base.type);
  3224. if (!found_type && !_guess_expression_type(context, subscript->base, base)) {
  3225. break;
  3226. }
  3227. if (_lookup_symbol_from_base(base.type, p_symbol, is_function, r_result) == OK) {
  3228. return OK;
  3229. }
  3230. } break;
  3231. case GDScriptParser::COMPLETION_TYPE_ATTRIBUTE: {
  3232. if (context.node == nullptr || context.node->type != GDScriptParser::Node::TYPE) {
  3233. break;
  3234. }
  3235. const GDScriptParser::TypeNode *type = static_cast<const GDScriptParser::TypeNode *>(context.node);
  3236. GDScriptParser::DataType base_type;
  3237. const GDScriptParser::IdentifierNode *prev = nullptr;
  3238. for (const GDScriptParser::IdentifierNode *E : type->type_chain) {
  3239. if (E->name == p_symbol && prev != nullptr) {
  3240. base_type = prev->get_datatype();
  3241. break;
  3242. }
  3243. prev = E;
  3244. }
  3245. if (base_type.kind != GDScriptParser::DataType::CLASS) {
  3246. GDScriptCompletionIdentifier base;
  3247. if (!_guess_expression_type(context, prev, base)) {
  3248. break;
  3249. }
  3250. base_type = base.type;
  3251. }
  3252. if (_lookup_symbol_from_base(base_type, p_symbol, is_function, r_result) == OK) {
  3253. return OK;
  3254. }
  3255. } break;
  3256. case GDScriptParser::COMPLETION_OVERRIDE_METHOD: {
  3257. GDScriptParser::DataType base_type = context.current_class->base_type;
  3258. if (_lookup_symbol_from_base(base_type, p_symbol, true, r_result) == OK) {
  3259. return OK;
  3260. }
  3261. } break;
  3262. case GDScriptParser::COMPLETION_PROPERTY_DECLARATION_OR_TYPE:
  3263. case GDScriptParser::COMPLETION_TYPE_NAME_OR_VOID:
  3264. case GDScriptParser::COMPLETION_TYPE_NAME: {
  3265. GDScriptParser::DataType base_type = context.current_class->get_datatype();
  3266. if (_lookup_symbol_from_base(base_type, p_symbol, false, r_result) == OK) {
  3267. return OK;
  3268. }
  3269. } break;
  3270. case GDScriptParser::COMPLETION_ANNOTATION: {
  3271. const String annotation_symbol = "@" + p_symbol;
  3272. if (parser.annotation_exists(annotation_symbol)) {
  3273. r_result.type = ScriptLanguage::LOOKUP_RESULT_CLASS_ANNOTATION;
  3274. r_result.class_name = "@GDScript";
  3275. r_result.class_member = annotation_symbol;
  3276. return OK;
  3277. }
  3278. } break;
  3279. default: {
  3280. }
  3281. }
  3282. return ERR_CANT_RESOLVE;
  3283. }
  3284. #endif