BfAutoComplete.cpp 121 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191
  1. #include "BfAutoComplete.h"
  2. #include "BfParser.h"
  3. #include "BfExprEvaluator.h"
  4. #include "BfSourceClassifier.h"
  5. #include "BfResolvePass.h"
  6. #include "BfFixits.h"
  7. #include "BfResolvedTypeUtils.h"
  8. #include "CeMachine.h"
  9. #include "CeDebugger.h"
  10. #define FTS_FUZZY_MATCH_IMPLEMENTATION
  11. #include "../third_party/FtsFuzzyMatch.h"
  12. #include "BeefySysLib/util/UTF8.h"
  13. #pragma warning(disable:4996)
  14. using namespace llvm;
  15. USING_NS_BF;
  16. AutoCompleteBase::AutoCompleteBase()
  17. {
  18. mIsGetDefinition = false;
  19. mIsAutoComplete = true;
  20. mDoFuzzyAutoComplete = false;
  21. mInsertStartIdx = -1;
  22. mInsertEndIdx = -1;
  23. }
  24. AutoCompleteBase::~AutoCompleteBase()
  25. {
  26. Clear();
  27. }
  28. AutoCompleteEntry* AutoCompleteBase::AddEntry(const AutoCompleteEntry& entry, const StringImpl& filter)
  29. {
  30. uint8 matches[256];
  31. if (!DoesFilterMatch(entry.mDisplay, filter.c_str(), entry.mScore, matches, 256) || (entry.mNamePrefixCount < 0))
  32. return NULL;
  33. entry.mMatchesLength = mDoFuzzyAutoComplete ? u8_strlen((char*)filter.c_str()) : 0;
  34. entry.mMatches = (entry.mMatchesLength > 0) ? matches : nullptr;
  35. auto result = AddEntry(entry);
  36. // Reset matches because the array will be invalid after return
  37. entry.mMatches = nullptr;
  38. entry.mMatchesLength = 0;
  39. return result;
  40. }
  41. AutoCompleteEntry* AutoCompleteBase::AddEntry(const AutoCompleteEntry& entry, const char* filter)
  42. {
  43. uint8 matches[256];
  44. if (!DoesFilterMatch(entry.mDisplay, filter, entry.mScore, matches, 256) || (entry.mNamePrefixCount < 0))
  45. return NULL;
  46. entry.mMatchesLength = mDoFuzzyAutoComplete ? u8_strlen((char*)filter) : 0;
  47. entry.mMatches = (entry.mMatchesLength > 0) ? matches : nullptr;
  48. auto result = AddEntry(entry);
  49. // Reset matches because the array will be invalid after return
  50. entry.mMatches = nullptr;
  51. entry.mMatchesLength = 0;
  52. return result;
  53. }
  54. AutoCompleteEntry* AutoCompleteBase::AddEntry(const AutoCompleteEntry& entry)
  55. {
  56. if (mEntriesSet.mAllocSize == 0)
  57. {
  58. mEntriesSet.Reserve(128);
  59. }
  60. AutoCompleteEntry* insertedEntry = NULL;
  61. if (mEntriesSet.TryAdd(entry, &insertedEntry))
  62. {
  63. insertedEntry->mEntryType = entry.mEntryType;
  64. const char* display = entry.mDisplay;
  65. int size = (int)strlen(display) + 1;
  66. insertedEntry->mDisplay = (char*)mAlloc.AllocBytes(size);
  67. memcpy((char*)insertedEntry->mDisplay, display, size);
  68. if (entry.mMatchesLength > 0)
  69. {
  70. insertedEntry->mMatches = (uint8*)mAlloc.AllocBytes(insertedEntry->mMatchesLength);
  71. memcpy((char*)insertedEntry->mMatches, entry.mMatches, insertedEntry->mMatchesLength);
  72. }
  73. }
  74. return insertedEntry;
  75. }
  76. bool AutoCompleteBase::DoesFilterMatch(const char* entry, const char* filter, int& score, uint8* matches, int maxMatches)
  77. {
  78. if (mIsGetDefinition)
  79. {
  80. int entryLen = (int)strlen(entry);
  81. if (entry[entryLen - 1] == '=')
  82. return (strncmp(filter, entry, entryLen - 1) == 0);
  83. return (strcmp(filter, entry) == 0);
  84. }
  85. if (!mIsAutoComplete)
  86. return false;
  87. if (filter[0] == '\0')
  88. return true;
  89. int filterLen = (int)strlen(filter);
  90. int entryLen = (int)strlen(entry);
  91. if (filterLen > entryLen)
  92. return false;
  93. if (mDoFuzzyAutoComplete)
  94. {
  95. return fts::fuzzy_match(filter, entry, score, matches, maxMatches);
  96. }
  97. else
  98. {
  99. bool hasUnderscore = false;
  100. bool checkInitials = filterLen > 1;
  101. for (int i = 0; i < (int)filterLen; i++)
  102. {
  103. char c = filter[i];
  104. if (c == '_')
  105. hasUnderscore = true;
  106. else if (islower((uint8)filter[i]))
  107. checkInitials = false;
  108. }
  109. if (hasUnderscore)
  110. return strnicmp(filter, entry, filterLen) == 0;
  111. char initialStr[256];
  112. char* initialStrP = initialStr;
  113. //String initialStr;
  114. bool prevWasUnderscore = false;
  115. for (int entryIdx = 0; entryIdx < entryLen; entryIdx++)
  116. {
  117. char entryC = entry[entryIdx];
  118. if (entryC == '_')
  119. {
  120. prevWasUnderscore = true;
  121. continue;
  122. }
  123. if ((entryIdx == 0) || (prevWasUnderscore) || (isupper((uint8)entryC) || (isdigit((uint8)entryC))))
  124. {
  125. if (strnicmp(filter, entry + entryIdx, filterLen) == 0)
  126. return true;
  127. if (checkInitials)
  128. *(initialStrP++) = entryC;
  129. }
  130. prevWasUnderscore = false;
  131. if (filterLen == 1)
  132. break; // Don't check inners for single-character case
  133. }
  134. if (!checkInitials)
  135. return false;
  136. *(initialStrP++) = 0;
  137. return strnicmp(filter, initialStr, filterLen) == 0;
  138. }
  139. }
  140. void AutoCompleteBase::Clear()
  141. {
  142. //mEntries.clear();
  143. mAlloc.Clear();
  144. mEntriesSet.Clear();
  145. }
  146. //////////////////////////////////////////////////////////////////////////
  147. BfAutoComplete::BfAutoComplete(BfResolveType resolveType, bool doFuzzyAutoComplete)
  148. {
  149. mResolveType = resolveType;
  150. mModule = NULL;
  151. mCompiler = NULL;
  152. mSystem = NULL;
  153. mIsCapturingMethodMatchInfo = false;
  154. mIgnoreFixits = false;
  155. mHasFriendSet = false;
  156. mUncertain = false;
  157. mForceAllowNonStatic = false;
  158. mMethodMatchInfo = NULL;
  159. mIsGetDefinition =
  160. (resolveType == BfResolveType_GetSymbolInfo) ||
  161. (resolveType == BfResolveType_GoToDefinition);
  162. mIsAutoComplete = (resolveType == BfResolveType_Autocomplete);
  163. mDoFuzzyAutoComplete = doFuzzyAutoComplete;
  164. mGetDefinitionNode = NULL;
  165. mShowAttributeProperties = NULL;
  166. mIdentifierUsed = NULL;
  167. //mReplaceMethodInstance = NULL;
  168. mReplaceLocalId = -1;
  169. mDefType = NULL;
  170. mDefField = NULL;
  171. mDefMethod = NULL;
  172. mDefProp = NULL;
  173. mDefMethodGenericParamIdx = -1;
  174. mDefTypeGenericParamIdx = -1;
  175. mCursorLineStart = -1;
  176. mCursorLineEnd = -1;
  177. }
  178. BfAutoComplete::~BfAutoComplete()
  179. {
  180. Clear();
  181. RemoveMethodMatchInfo();
  182. }
  183. void BfAutoComplete::SetModule(BfModule* module)
  184. {
  185. if (module != NULL)
  186. {
  187. mModule = module;
  188. mCompiler = mModule->mCompiler;
  189. mSystem = mCompiler->mSystem;
  190. }
  191. else
  192. {
  193. mModule = NULL;
  194. mCompiler = NULL;
  195. mSystem = NULL;
  196. }
  197. }
  198. void BfAutoComplete::Clear()
  199. {
  200. if (mMethodMatchInfo != NULL)
  201. {
  202. if (mMethodMatchInfo->mInstanceList.size() == 0)
  203. {
  204. delete mMethodMatchInfo;
  205. mMethodMatchInfo = NULL;
  206. }
  207. else
  208. {
  209. // Keep mBestIdx - for when we match but then backspace
  210. mMethodMatchInfo->mPrevBestIdx = mMethodMatchInfo->mBestIdx;
  211. mMethodMatchInfo->mMostParamsMatched = 0;
  212. mMethodMatchInfo->mHadExactMatch = false;
  213. mMethodMatchInfo->mInstanceList.Clear();
  214. mMethodMatchInfo->mSrcPositions.Clear();
  215. }
  216. }
  217. mInsertStartIdx = -1;
  218. mInsertEndIdx = -1;
  219. mIsCapturingMethodMatchInfo = false;
  220. AutoCompleteBase::Clear();
  221. }
  222. void BfAutoComplete::RemoveMethodMatchInfo()
  223. {
  224. mIsCapturingMethodMatchInfo = false;
  225. delete mMethodMatchInfo;
  226. mMethodMatchInfo = NULL;
  227. }
  228. void BfAutoComplete::ClearMethodMatchEntries()
  229. {
  230. mMethodMatchInfo->mInstanceList.Clear();
  231. }
  232. int BfAutoComplete::GetCursorIdx(BfAstNode* node)
  233. {
  234. if (node == NULL)
  235. return -1;
  236. // if (!node->IsFromParser(mCompiler->mResolvePassData->mParser))
  237. // return -1;
  238. if (node->IsTemporary())
  239. return false;
  240. auto bfParser = node->GetSourceData()->ToParser();
  241. if (bfParser == NULL)
  242. return -1;
  243. if ((bfParser->mParserFlags & ParserFlag_Autocomplete) == 0)
  244. return -1;
  245. return bfParser->mCursorIdx;
  246. }
  247. bool BfAutoComplete::IsAutocompleteNode(BfAstNode* node, int lengthAdd, int startAdd)
  248. {
  249. if (mModule == NULL)
  250. return false;
  251. if (node == NULL)
  252. return false;
  253. // if (!node->IsFromParser(mCompiler->mResolvePassData->mParser))
  254. // return false;
  255. if (node->IsTemporary())
  256. return false;
  257. auto bfParser = node->GetSourceData()->ToParser();
  258. if (bfParser == NULL)
  259. return false;
  260. if ((bfParser->mParserFlags & ParserFlag_Autocomplete) == 0)
  261. return false;
  262. //if (mCompiler->mResolvePassData->mResolveType != BfResolveType_Autocomplete)
  263. lengthAdd++;
  264. int cursorIdx = bfParser->mCursorCheckIdx;
  265. int nodeSrcStart = node->GetSrcStart();
  266. if ((cursorIdx < nodeSrcStart + startAdd) || (cursorIdx >= node->GetSrcEnd() + lengthAdd))
  267. return false;
  268. return true;
  269. }
  270. bool BfAutoComplete::IsAutocompleteNode(BfAstNode* startNode, BfAstNode* endNode, int lengthAdd, int startAdd)
  271. {
  272. if ((startNode == NULL) || (endNode == NULL))
  273. return false;
  274. // if (!startNode->IsFromParser(mCompiler->mResolvePassData->mParser))
  275. // return false;
  276. if (startNode->IsTemporary())
  277. return false;
  278. auto bfParser = startNode->GetSourceData()->ToParser();
  279. if (bfParser == NULL)
  280. return false;
  281. if ((bfParser->mParserFlags & ParserFlag_Autocomplete) == 0)
  282. return false;
  283. //if (mCompiler->mResolvePassData->mResolveType != BfResolveType_Autocomplete)
  284. lengthAdd++;
  285. int cursorIdx = bfParser->mCursorCheckIdx;
  286. int nodeSrcStart = startNode->GetSrcStart();
  287. if ((cursorIdx < nodeSrcStart + startAdd) || (cursorIdx >= endNode->GetSrcEnd() + lengthAdd))
  288. return false;
  289. return true;
  290. }
  291. bool BfAutoComplete::IsAutocompleteLineNode(BfAstNode* node)
  292. {
  293. if (node == NULL)
  294. return false;
  295. // if (!node->IsFromParser(mCompiler->mResolvePassData->mParser))
  296. // return false;
  297. if (node->IsTemporary())
  298. return false;
  299. auto bfParser = node->GetSourceData()->ToParser();
  300. if (bfParser == NULL)
  301. return false;
  302. if ((bfParser->mParserFlags & ParserFlag_Autocomplete) == 0)
  303. return false;
  304. int startAdd = 0;
  305. if (mCursorLineStart == -1)
  306. {
  307. auto nodeSource = node->GetSourceData();
  308. mCursorLineStart = bfParser->mCursorIdx;
  309. while (mCursorLineStart > 0)
  310. {
  311. if (nodeSource->mSrc[mCursorLineStart - 1] == '\n')
  312. break;
  313. mCursorLineStart--;
  314. }
  315. mCursorLineEnd = bfParser->mCursorIdx;
  316. while (mCursorLineEnd < nodeSource->mSrcLength)
  317. {
  318. if (nodeSource->mSrc[mCursorLineEnd] == '\n')
  319. break;
  320. mCursorLineEnd++;
  321. }
  322. }
  323. int srcStart = node->GetSrcStart();
  324. return (srcStart >= mCursorLineStart) && (srcStart <= mCursorLineEnd);
  325. }
  326. // The parser thought this was a type reference but it may not be
  327. BfTypedValue BfAutoComplete::LookupTypeRefOrIdentifier(BfAstNode* node, bool* isStatic, BfEvalExprFlags evalExprFlags, BfType* expectingType)
  328. {
  329. SetAndRestoreValue<bool> prevIgnoreClassifying(mModule->mIsInsideAutoComplete, true);
  330. auto _FixType = [](const BfTypedValue& typedValue)
  331. {
  332. if ((typedValue.mType != NULL) && (typedValue.mType->IsAllocType()))
  333. {
  334. BfTypedValue ret = typedValue;
  335. ret.mType = ret.mType->GetUnderlyingType();
  336. return ret;
  337. }
  338. return typedValue;
  339. };
  340. if (auto typeRef = BfNodeDynCast<BfTypeReference>(node))
  341. {
  342. if (auto namedTypeRef = BfNodeDynCast<BfNamedTypeReference>(typeRef))
  343. {
  344. BfExprEvaluator exprEvaluator(mModule);
  345. auto identifierResult = exprEvaluator.LookupIdentifier(namedTypeRef->mNameNode);
  346. if (identifierResult)
  347. return identifierResult;
  348. identifierResult = exprEvaluator.GetResult();// We need 'GetResult' to read property values
  349. if (identifierResult.HasType())
  350. return _FixType(identifierResult);
  351. }
  352. else if (auto qualifiedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(typeRef))
  353. {
  354. auto leftValue = LookupTypeRefOrIdentifier(qualifiedTypeRef->mLeft, isStatic);
  355. if (leftValue.mType)
  356. {
  357. if (leftValue.mType->IsPointer())
  358. {
  359. mModule->LoadValue(leftValue);
  360. leftValue.mType = leftValue.mType->GetUnderlyingType();
  361. leftValue.mKind = BfTypedValueKind_Addr;
  362. }
  363. if (auto rightNamedTypeRef = BfNodeDynCast<BfNamedTypeReference>(qualifiedTypeRef->mRight))
  364. {
  365. // This keeps the classifier from colorizing properties - this causes 'flashing' when we go back over this with a resolve pass
  366. // that wouldn't catch this
  367. SetAndRestoreValue<bool> prevClassifier(mModule->mCompiler->mResolvePassData->mIsClassifying, false);
  368. BfExprEvaluator exprEvaluator(mModule);
  369. auto fieldResult = exprEvaluator.LookupField(qualifiedTypeRef->mRight, leftValue, rightNamedTypeRef->mNameNode->ToString());
  370. if (!fieldResult) // Was property?
  371. fieldResult = exprEvaluator.GetResult();
  372. if (fieldResult.HasType())
  373. {
  374. *isStatic = false;
  375. return _FixType(fieldResult);
  376. }
  377. }
  378. }
  379. }
  380. BfResolveTypeRefFlags resolveTypeRefFlags = BfResolveTypeRefFlag_AllowUnboundGeneric;
  381. if (mResolveType == BfResolveType_VerifyTypeName)
  382. resolveTypeRefFlags = (BfResolveTypeRefFlags)(resolveTypeRefFlags | BfResolveTypeRefFlag_ForceUnboundGeneric);
  383. BfType* type = mModule->ResolveTypeRef(typeRef, BfPopulateType_Identity, resolveTypeRefFlags);
  384. if (type != NULL)
  385. {
  386. *isStatic = true;
  387. return _FixType(BfTypedValue(type));
  388. }
  389. }
  390. if (auto identifier = BfNodeDynCast<BfIdentifierNode>(node))
  391. {
  392. BfExprEvaluator exprEvaluator(mModule);
  393. auto identifierResult = exprEvaluator.LookupIdentifier(identifier, false, NULL);
  394. if (!identifierResult)
  395. identifierResult = exprEvaluator.GetResult();
  396. if (identifierResult)
  397. return _FixType(identifierResult);
  398. if (auto qualifiedIdentifier = BfNodeDynCast<BfQualifiedNameNode>(node))
  399. {
  400. bool leftIsStatic = false;
  401. auto leftValue = LookupTypeRefOrIdentifier(qualifiedIdentifier->mLeft, &leftIsStatic);
  402. if (leftValue.mType)
  403. {
  404. auto findName = qualifiedIdentifier->mRight->ToString();
  405. if (findName == "base")
  406. {
  407. return BfTypedValue(leftValue);
  408. }
  409. BfExprEvaluator exprEvaluator(mModule);
  410. auto fieldResult = exprEvaluator.LookupField(node, leftValue, findName);
  411. if (fieldResult)
  412. return fieldResult;
  413. auto result = exprEvaluator.GetResult();
  414. if (result)
  415. return _FixType(result);
  416. }
  417. }
  418. auto type = mModule->ResolveTypeRef(identifier, NULL);
  419. if (type != NULL)
  420. {
  421. *isStatic = true;
  422. return _FixType(BfTypedValue(type));
  423. }
  424. }
  425. else if (auto memberRefExpr = BfNodeDynCast<BfMemberReferenceExpression>(node))
  426. {
  427. return _FixType(mModule->CreateValueFromExpression(memberRefExpr, expectingType, evalExprFlags));
  428. }
  429. else if (auto parenExpr = BfNodeDynCast<BfParenthesizedExpression>(node))
  430. {
  431. // Don't pass BfEvalExprFlags_IgnoreNullConditional, since parenExprs end nullable chains and we actually
  432. // DO want the nullable at this point
  433. return _FixType(mModule->CreateValueFromExpression(parenExpr));
  434. }
  435. else if (auto targetExpr = BfNodeDynCast<BfExpression>(node))
  436. {
  437. return _FixType(mModule->CreateValueFromExpression(targetExpr, expectingType, evalExprFlags));
  438. }
  439. return BfTypedValue();
  440. }
  441. void BfAutoComplete::SetDefinitionLocation(BfAstNode* astNode, bool force)
  442. {
  443. if (mIsGetDefinition)
  444. {
  445. if ((mGetDefinitionNode == NULL) || (force))
  446. mGetDefinitionNode = astNode;
  447. }
  448. }
  449. bool BfAutoComplete::IsAttribute(BfTypeInstance* typeInst)
  450. {
  451. auto checkTypeInst = typeInst;
  452. while (checkTypeInst != NULL)
  453. {
  454. if (checkTypeInst->mTypeDef->GetLatest() == mModule->mCompiler->mAttributeTypeDef->GetLatest())
  455. return true;
  456. checkTypeInst = checkTypeInst->mBaseType;
  457. }
  458. return false;
  459. }
  460. void BfAutoComplete::AddMethod(BfTypeInstance* typeInstance, BfMethodDef* methodDef, BfMethodInstance* methodInstance, BfMethodDeclaration* methodDecl, const StringImpl& methodName, const StringImpl& filter)
  461. {
  462. int wantPrefixCount = 0;
  463. const char* filterStr = filter.c_str();
  464. while (filterStr[0] == '@')
  465. {
  466. filterStr++;
  467. wantPrefixCount++;
  468. }
  469. String replaceName;
  470. AutoCompleteEntry entry("method", methodName, methodDef->mNamePrefixCount - wantPrefixCount);
  471. if (methodDecl != NULL)
  472. {
  473. if (methodDecl->mMixinSpecifier != NULL)
  474. {
  475. replaceName = entry.mDisplay;
  476. replaceName += "!";
  477. entry.mDisplay = replaceName.c_str();
  478. entry.mEntryType = "mixin";
  479. }
  480. }
  481. if (methodDef->mMethodType == BfMethodType_Extension)
  482. entry.mEntryType = "extmethod";
  483. if (auto entryAdded = AddEntry(entry, filterStr))
  484. {
  485. if (methodDecl != NULL)
  486. {
  487. if ((methodInstance != NULL) && (methodInstance->mMethodDef->mIsLocalMethod) && (methodDecl->mReturnType != NULL) && (GetCursorIdx(methodDecl) == methodDecl->mReturnType->mSrcEnd))
  488. {
  489. // This isn't really a local method decl, it just looks like one
  490. return;
  491. }
  492. if (CheckDocumentation(entryAdded, NULL))
  493. {
  494. String str;
  495. if ((methodInstance == NULL) && (methodDef != NULL))
  496. methodInstance = mModule->GetRawMethodInstance(typeInstance, methodDef);
  497. if (methodInstance != NULL)
  498. {
  499. SetAndRestoreValue<BfTypeInstance*> prevTypeInstance(mModule->mCurTypeInstance, typeInstance);
  500. SetAndRestoreValue<BfMethodInstance*> prevCurMethodInstance(mModule->mCurMethodInstance, methodInstance);
  501. str = mModule->MethodToString(methodInstance, (BfMethodNameFlags)(BfMethodNameFlag_IncludeReturnType | BfMethodNameFlag_ResolveGenericParamNames));
  502. }
  503. if (entryAdded->mDocumentation != NULL)
  504. {
  505. str += "\x04";
  506. str.Append(entryAdded->mDocumentation);
  507. }
  508. else if (methodDecl->mDocumentation != NULL)
  509. {
  510. if (!str.IsEmpty())
  511. str += "\x05";
  512. methodDecl->mDocumentation->GetDocString(str);
  513. }
  514. if (!str.IsEmpty())
  515. entryAdded->mDocumentation = mAlloc.AllocString(str);
  516. }
  517. }
  518. if ((mResolveType == BfResolveType_GoToDefinition) && (mGetDefinitionNode == NULL) && (methodDecl != NULL) && (methodDecl->mNameNode != NULL))
  519. SetDefinitionLocation(methodDecl->mNameNode);
  520. }
  521. }
  522. void BfAutoComplete::AddTypeDef(BfTypeDef* typeDef, const StringImpl& filter, bool onlyAttribute)
  523. {
  524. BF_ASSERT(typeDef->mDefState != BfTypeDef::DefState_Emitted);
  525. if (typeDef->mTypeDeclaration == NULL)
  526. return;
  527. StringT<64> name(typeDef->mName->ToString());
  528. if (name == "@")
  529. return;
  530. int gravePos = (int)name.IndexOf('`');
  531. if (gravePos != -1)
  532. name = name.Substring(0, gravePos) + "<>";
  533. if (onlyAttribute)
  534. {
  535. if ((mIsGetDefinition) && (name == filter + "Attribute"))
  536. {
  537. SetDefinitionLocation(typeDef->mTypeDeclaration->mNameNode);
  538. return;
  539. }
  540. int score;
  541. uint8 matches[256];
  542. if (!DoesFilterMatch(name.c_str(), filter.c_str(), score, matches, sizeof(matches)))
  543. return;
  544. auto type = mModule->ResolveTypeDef(typeDef, BfPopulateType_Declaration);
  545. if (type != NULL)
  546. {
  547. auto typeInst = type->ToTypeInstance();
  548. if (!IsAttribute(typeInst))
  549. return;
  550. }
  551. const char* attrStr = "Attribute";
  552. const int attrStrLen = (int)strlen(attrStr);
  553. if (((int)name.length() > attrStrLen) && ((int)name.length() - (int)attrStrLen >= filter.length()) && (strcmp(name.c_str() + (int)name.length() - attrStrLen, attrStr) == 0))
  554. {
  555. // Shorter name - remove "Attribute"
  556. name = name.Substring(0, name.length() - attrStrLen);
  557. }
  558. }
  559. AutoCompleteEntry* entryAdded = NULL;
  560. if (typeDef->mTypeCode == BfTypeCode_Object)
  561. entryAdded = AddEntry(AutoCompleteEntry("class", name), filter);
  562. else if (typeDef->mTypeCode == BfTypeCode_Interface)
  563. entryAdded = AddEntry(AutoCompleteEntry("interface", name), filter);
  564. else
  565. entryAdded = AddEntry(AutoCompleteEntry("valuetype", name), filter);
  566. if (entryAdded != NULL)
  567. {
  568. if ((CheckDocumentation(entryAdded, NULL)) && (entryAdded->mDocumentation == NULL))
  569. {
  570. auto type = mModule->ResolveTypeDef(typeDef, BfPopulateType_IdentityNoRemapAlias);
  571. StringT<1024> str;
  572. if (type != NULL)
  573. {
  574. SetAndRestoreValue<BfTypeInstance*> prevTypeInst(mModule->mCurTypeInstance, type->ToTypeInstance());
  575. SetAndRestoreValue<BfMethodInstance*> prevMethodInst(mModule->mCurMethodInstance, NULL);
  576. str = mModule->TypeToString(type, (BfTypeNameFlags)(BfTypeNameFlag_ExtendedInfo | BfTypeNameFlag_ResolveGenericParamNames));
  577. }
  578. if (typeDef->mTypeDeclaration->mDocumentation != NULL)
  579. {
  580. if (!str.IsEmpty())
  581. str += "\x05";
  582. typeDef->mTypeDeclaration->mDocumentation->GetDocString(str);
  583. }
  584. entryAdded->mDocumentation = mAlloc.AllocString(str);
  585. }
  586. }
  587. }
  588. bool BfAutoComplete::CheckProtection(BfProtection protection, BfTypeDef* typeDef, bool allowProtected, bool allowPrivate)
  589. {
  590. if (mResolveType == BfResolveType_GetSymbolInfo)
  591. {
  592. // This is needed for nameof on private inner types
  593. return true;
  594. }
  595. if ((protection == BfProtection_Internal) && (typeDef != NULL))
  596. {
  597. return mModule->CheckProtection(protection, typeDef, allowProtected, allowPrivate);
  598. }
  599. return (mHasFriendSet) || (protection == BfProtection_Public) ||
  600. ((protection == BfProtection_Protected) && (allowProtected)) ||
  601. ((protection == BfProtection_Private) && (allowPrivate));
  602. }
  603. const char* BfAutoComplete::GetTypeName(BfType* type)
  604. {
  605. if (type != NULL)
  606. {
  607. if (type->IsPointer())
  608. return "pointer";
  609. if (type->IsObjectOrInterface())
  610. return "object";
  611. }
  612. return "value";
  613. }
  614. void BfAutoComplete::AddInnerTypes(BfTypeInstance* typeInst, const StringImpl& filter, BfTypeInstance* startType, bool allowProtected, bool allowPrivate)
  615. {
  616. if (typeInst->IsEnum())
  617. AddEntry(AutoCompleteEntry("valuetype", "UnderlyingType"), filter);
  618. BfShow checkShow = (typeInst == startType) ? BfShow_Hide : BfShow_HideIndirect;
  619. for (auto innerType : typeInst->mTypeDef->mNestedTypes)
  620. {
  621. if (innerType->mShow >= checkShow)
  622. continue;
  623. if (innerType->mOuterType->mTypeCode == BfTypeCode_Extension)
  624. {
  625. if (typeInst->mDefineState < BfTypeDefineState_Defined)
  626. mModule->PopulateType(typeInst);
  627. if ((typeInst->mGenericTypeInfo != NULL) && (typeInst->mGenericTypeInfo->mGenericExtensionInfo != NULL))
  628. {
  629. if (!typeInst->mGenericTypeInfo->mGenericExtensionInfo->mConstraintsPassedSet.IsSet(innerType->mOuterType->mPartialIdx))
  630. continue;
  631. }
  632. }
  633. if (CheckProtection(innerType->mProtection, innerType, allowProtected, allowPrivate))
  634. AddTypeDef(innerType, filter);
  635. }
  636. allowPrivate = false;
  637. if (typeInst->mBaseType != NULL)
  638. AddInnerTypes(typeInst->mBaseType, filter, startType, allowProtected, allowPrivate);
  639. }
  640. void BfAutoComplete::AddCurrentTypes(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate, bool onlyAttribute)
  641. {
  642. if (typeInst != mModule->mCurTypeInstance)
  643. AddTypeDef(typeInst->mTypeDef->GetDefinition(), filter, onlyAttribute);
  644. auto typeDef = typeInst->mTypeDef->GetDefinition();
  645. for (auto nestedTypeDef : typeDef->mNestedTypes)
  646. {
  647. if (nestedTypeDef->mIsPartial)
  648. {
  649. nestedTypeDef = mSystem->GetCombinedPartial(nestedTypeDef);
  650. if (nestedTypeDef == NULL)
  651. continue;
  652. }
  653. if (CheckProtection(nestedTypeDef->mProtection, nestedTypeDef, allowProtected, allowPrivate))
  654. AddTypeDef(nestedTypeDef, filter, onlyAttribute);
  655. }
  656. auto outerType = mModule->GetOuterType(typeInst);
  657. if (outerType != NULL)
  658. AddCurrentTypes(outerType, filter, allowProtected, allowPrivate, onlyAttribute);
  659. allowPrivate = false;
  660. auto baseType = mModule->GetBaseType(typeInst);
  661. if (baseType != NULL)
  662. AddCurrentTypes(baseType, filter, allowProtected, allowPrivate, onlyAttribute);
  663. }
  664. void BfAutoComplete::AddField(BfTypeInstance* typeInst, BfFieldDef* fieldDef, BfFieldInstance* fieldInstance, const StringImpl& filter)
  665. {
  666. int wantPrefixCount = 0;
  667. const char* filterStr = filter.c_str();
  668. while (filterStr[0] == '@')
  669. {
  670. filterStr++;
  671. wantPrefixCount++;
  672. }
  673. AutoCompleteEntry entry(GetTypeName(fieldInstance->mResolvedType), fieldDef->mName, fieldDef->mNamePrefixCount - wantPrefixCount);
  674. if (auto entryAdded = AddEntry(entry, filterStr))
  675. {
  676. auto fieldDecl = fieldDef->GetFieldDeclaration();
  677. auto documentation = (fieldDecl != NULL) ? fieldDecl->mDocumentation : NULL;
  678. if (CheckDocumentation(entryAdded, documentation))
  679. {
  680. mModule->PopulateType(typeInst);
  681. bool isTag = typeInst->IsEnum() && typeInst->IsOnDemand();
  682. String str;
  683. if (!isTag)
  684. {
  685. if (!fieldInstance->GetFieldDef()->IsEnumCaseEntry())
  686. {
  687. str += mModule->TypeToString(fieldInstance->mResolvedType);
  688. str += " ";
  689. }
  690. str += mModule->TypeToString(typeInst);
  691. str += ".";
  692. str += fieldDef->mName;
  693. }
  694. if (entryAdded->mDocumentation != NULL)
  695. {
  696. str += "\x04";
  697. str.Append(entryAdded->mDocumentation);
  698. }
  699. else if (documentation != NULL)
  700. {
  701. str += "\x05";
  702. documentation->GetDocString(str);
  703. }
  704. if (!str.IsEmpty())
  705. entryAdded->mDocumentation = mAlloc.AllocString(str);
  706. }
  707. if ((mIsGetDefinition) && (mDefType == NULL))
  708. {
  709. mDefType = typeInst->mTypeDef;
  710. mDefField = fieldDef;
  711. if (auto nameNode = fieldDef->GetNameNode())
  712. SetDefinitionLocation(nameNode);
  713. }
  714. }
  715. }
  716. void BfAutoComplete::AddProp(BfTypeInstance* typeInst, BfPropertyDef* propDef, const StringImpl& filter)
  717. {
  718. int wantPrefixCount = 0;
  719. const char* filterStr = filter.c_str();
  720. while (filterStr[0] == '@')
  721. {
  722. filterStr++;
  723. wantPrefixCount++;
  724. }
  725. BfCommentNode* documentation = NULL;
  726. auto fieldDecl = propDef->GetFieldDeclaration();
  727. if (fieldDecl != NULL)
  728. documentation = fieldDecl->mDocumentation;
  729. AutoCompleteEntry entry("property", propDef->mName, propDef->mNamePrefixCount - wantPrefixCount);
  730. if (auto entryAdded = AddEntry(entry, filterStr))
  731. {
  732. if (CheckDocumentation(entryAdded, documentation))
  733. {
  734. BfType* propType = NULL;
  735. bool hasGetter = false;
  736. bool hasSetter = false;
  737. for (auto methodDef : propDef->mMethods)
  738. {
  739. auto methodInstance = mModule->GetRawMethodInstance(typeInst, methodDef);
  740. if (methodInstance == NULL)
  741. continue;
  742. if (methodDef->mMethodType == BfMethodType_PropertyGetter)
  743. {
  744. hasGetter = true;
  745. propType = methodInstance->mReturnType;
  746. }
  747. if (methodDef->mMethodType == BfMethodType_PropertySetter)
  748. {
  749. hasSetter = true;
  750. if (methodInstance->GetParamCount() > 0)
  751. propType = methodInstance->GetParamType(0);
  752. }
  753. }
  754. String str;
  755. if (propType != NULL)
  756. {
  757. str += mModule->TypeToString(propType);
  758. str += " ";
  759. }
  760. str += mModule->TypeToString(typeInst);
  761. str += ".";
  762. str += propDef->mName;
  763. str += " { ";
  764. if (hasGetter)
  765. str += "get; ";
  766. if (hasSetter)
  767. str += "set; ";
  768. str += "}";
  769. if (entryAdded->mDocumentation != NULL)
  770. {
  771. str += "\x04";
  772. str.Append(entryAdded->mDocumentation);
  773. }
  774. else if (documentation != NULL)
  775. {
  776. str += "\x05";
  777. documentation->GetDocString(str);
  778. }
  779. entryAdded->mDocumentation = mAlloc.AllocString(str);
  780. }
  781. if ((mIsGetDefinition) && (fieldDecl != NULL))
  782. SetDefinitionLocation(fieldDecl->mNameNode);
  783. }
  784. }
  785. void BfAutoComplete::AddTypeMembers(BfTypeInstance* typeInst, bool addStatic, bool addNonStatic, const StringImpl& filter, BfTypeInstance* startType, bool allowInterfaces, bool allowImplicitThis, bool checkOuterType)
  786. {
  787. bool isInterface = false;
  788. if (mForceAllowNonStatic)
  789. addNonStatic = true;
  790. auto activeTypeDef = mModule->GetActiveTypeDef();
  791. if ((addStatic) && (mModule->mCurMethodInstance == NULL) && (typeInst->IsEnum()) && (allowImplicitThis))
  792. {
  793. AddEntry(AutoCompleteEntry("value", "_"), filter);
  794. }
  795. #define CHECK_STATIC(staticVal) ((staticVal && addStatic) || (!staticVal && addNonStatic))
  796. mModule->PopulateType(typeInst, BfPopulateType_Data);
  797. BfShow checkShow = (startType == typeInst) ? BfShow_Hide : BfShow_HideIndirect;
  798. BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None;
  799. for (auto& fieldInst : typeInst->mFieldInstances)
  800. {
  801. auto fieldDef = fieldInst.GetFieldDef();
  802. if (fieldDef == NULL)
  803. continue;
  804. if (fieldDef->mShow >= checkShow)
  805. continue;
  806. if ((CHECK_STATIC(fieldDef->mIsStatic)) &&
  807. ((mIsGetDefinition) || (mModule->CheckProtection(protectionCheckFlags, typeInst, fieldDef->mDeclaringType->mProject, fieldDef->mProtection, startType))))
  808. {
  809. if ((!typeInst->IsTypeMemberIncluded(fieldDef->mDeclaringType, activeTypeDef, mModule)) ||
  810. (!typeInst->IsTypeMemberAccessible(fieldDef->mDeclaringType, activeTypeDef)))
  811. continue;
  812. AddField(typeInst, fieldDef, &fieldInst, filter);
  813. }
  814. }
  815. for (auto methodDef : typeInst->mTypeDef->mMethods)
  816. {
  817. if (methodDef->mIsOverride)
  818. continue;
  819. if (methodDef->mShow >= checkShow)
  820. continue;
  821. if (methodDef->mName.IsEmpty())
  822. continue;
  823. if (methodDef->mExplicitInterface != NULL)
  824. continue;
  825. if ((!typeInst->IsTypeMemberIncluded(methodDef->mDeclaringType, activeTypeDef, mModule)) ||
  826. (!typeInst->IsTypeMemberAccessible(methodDef->mDeclaringType, activeTypeDef)))
  827. continue;
  828. bool canUseMethod;
  829. canUseMethod = (methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Mixin);
  830. if (isInterface)
  831. {
  832. // Always allow
  833. canUseMethod &= addNonStatic;
  834. }
  835. else
  836. {
  837. canUseMethod &= (CHECK_STATIC(methodDef->mIsStatic) &&
  838. (mModule->CheckProtection(protectionCheckFlags, typeInst, methodDef->mDeclaringType->mProject, methodDef->mProtection, startType)));
  839. }
  840. if (canUseMethod)
  841. {
  842. AddMethod(typeInst, methodDef, NULL, methodDef->GetMethodDeclaration(), methodDef->mName, filter);
  843. }
  844. }
  845. for (auto propDef : typeInst->mTypeDef->mProperties)
  846. {
  847. if (propDef->mShow >= checkShow)
  848. continue;
  849. if ((!typeInst->IsTypeMemberIncluded(propDef->mDeclaringType, activeTypeDef, mModule)) ||
  850. (!typeInst->IsTypeMemberAccessible(propDef->mDeclaringType, activeTypeDef)))
  851. continue;
  852. if ((CHECK_STATIC(propDef->mIsStatic)) && (mModule->CheckProtection(protectionCheckFlags, typeInst, propDef->mDeclaringType->mProject, propDef->mProtection, startType)))
  853. {
  854. if ((!allowInterfaces) && (propDef->HasExplicitInterface()))
  855. continue;
  856. if (propDef->mName == "[]")
  857. continue;
  858. AddProp(typeInst, propDef, filter);
  859. }
  860. }
  861. if (allowInterfaces)
  862. {
  863. for (auto iface : typeInst->mInterfaces)
  864. AddTypeMembers(iface.mInterfaceType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
  865. }
  866. if (typeInst->mBaseType != NULL)
  867. AddTypeMembers(typeInst->mBaseType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
  868. else
  869. {
  870. if (typeInst->IsStruct())
  871. AddTypeMembers(mModule->mContext->mBfObjectType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
  872. }
  873. if (typeInst->IsInterface())
  874. {
  875. for (auto interface : typeInst->mInterfaces)
  876. AddTypeMembers(interface.mInterfaceType, addStatic, addNonStatic, filter, startType, false, allowImplicitThis, false);
  877. }
  878. if ((addStatic) && (allowImplicitThis) && (checkOuterType))
  879. {
  880. auto outerType = mModule->GetOuterType(typeInst);
  881. if (outerType != NULL)
  882. {
  883. AddTypeMembers(outerType, true, false, filter, startType, false, allowImplicitThis, false);
  884. }
  885. }
  886. if ((typeInst->mTypeDef->mHasUsingFields) &&
  887. ((typeInst->mTypeInfoEx == NULL) || (typeInst->mTypeInfoEx->mUsingFieldData == NULL)))
  888. mModule->PopulateUsingFieldData(typeInst);
  889. if ((typeInst->mTypeInfoEx != NULL) && (typeInst->mTypeInfoEx->mUsingFieldData != NULL))
  890. {
  891. for (int pass = 0; pass < 2; pass++)
  892. {
  893. auto& dict = (pass == 0) ? typeInst->mTypeInfoEx->mUsingFieldData->mEntries : typeInst->mTypeInfoEx->mUsingFieldData->mMethods;
  894. for (auto& entryKV : dict)
  895. {
  896. for (auto& entryList : entryKV.mValue.mLookups)
  897. {
  898. auto& endEntry = entryList.back();
  899. bool isStatic = endEntry.IsStatic();
  900. if ((isStatic) && (!addStatic))
  901. continue;
  902. if ((!isStatic) && (!addNonStatic))
  903. continue;
  904. bool passesProtection = true;
  905. for (int entryIdx = 0; entryIdx < entryList.mSize; entryIdx++)
  906. {
  907. auto& entry = entryList[entryIdx];
  908. if (!mModule->CheckProtection(protectionCheckFlags, entry.mTypeInstance, entry.GetDeclaringType(mModule)->mProject,
  909. (entryIdx < entryList.mSize - 1) ? entry.GetUsingProtection() : entry.GetProtection(), typeInst))
  910. {
  911. passesProtection = false;
  912. break;
  913. }
  914. }
  915. if (!passesProtection)
  916. continue;
  917. switch (endEntry.mKind)
  918. {
  919. case BfUsingFieldData::MemberRef::Kind_Field:
  920. if (endEntry.mTypeInstance->mDefineState < BfTypeDefineState_Defined)
  921. mModule->PopulateType(endEntry.mTypeInstance);
  922. AddField(endEntry.mTypeInstance, endEntry.mTypeInstance->mTypeDef->mFields[endEntry.mIdx], &endEntry.mTypeInstance->mFieldInstances[endEntry.mIdx], filter);
  923. break;
  924. case BfUsingFieldData::MemberRef::Kind_Property:
  925. AddProp(endEntry.mTypeInstance, endEntry.mTypeInstance->mTypeDef->mProperties[endEntry.mIdx], filter);
  926. break;
  927. case BfUsingFieldData::MemberRef::Kind_Method:
  928. {
  929. auto methodDef = endEntry.mTypeInstance->mTypeDef->mMethods[endEntry.mIdx];
  930. AddMethod(endEntry.mTypeInstance, methodDef, NULL, methodDef->GetMethodDeclaration(), methodDef->mName, filter);
  931. }
  932. break;
  933. }
  934. }
  935. }
  936. }
  937. }
  938. }
  939. void BfAutoComplete::AddSelfResultTypeMembers(BfTypeInstance* typeInst, BfTypeInstance* selfType, const StringImpl& filter, bool allowPrivate)
  940. {
  941. bool isInterface = false;
  942. bool allowProtected = allowPrivate;
  943. auto activeTypeDef = mModule->GetActiveTypeDef();
  944. mModule->PopulateType(typeInst, BfPopulateType_Data);
  945. BfShow checkShow = allowPrivate ? BfShow_Hide : BfShow_HideIndirect;
  946. for (auto& fieldInst : typeInst->mFieldInstances)
  947. {
  948. auto fieldDef = fieldInst.GetFieldDef();
  949. if (fieldDef == NULL)
  950. continue;
  951. if (fieldDef->mShow > checkShow)
  952. continue;
  953. if ((fieldDef->mIsStatic) && (CheckProtection(fieldDef->mProtection, fieldDef->mDeclaringType, allowProtected, allowPrivate)))
  954. {
  955. if (!mModule->CanCast(BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), fieldInst.mResolvedType), selfType))
  956. continue;
  957. if ((!typeInst->IsTypeMemberIncluded(fieldDef->mDeclaringType, activeTypeDef, mModule)) ||
  958. (!typeInst->IsTypeMemberAccessible(fieldDef->mDeclaringType, activeTypeDef)))
  959. continue;
  960. AddField(typeInst, fieldDef, &fieldInst, filter);
  961. }
  962. }
  963. for (auto methodDef : typeInst->mTypeDef->mMethods)
  964. {
  965. if (methodDef->mIsOverride)
  966. continue;
  967. if (methodDef->mShow > checkShow)
  968. continue;
  969. if (methodDef->mName.IsEmpty())
  970. continue;
  971. if (methodDef->mExplicitInterface != NULL)
  972. continue;
  973. if ((!typeInst->IsTypeMemberIncluded(methodDef->mDeclaringType, activeTypeDef, mModule)) ||
  974. (!typeInst->IsTypeMemberAccessible(methodDef->mDeclaringType, activeTypeDef)))
  975. continue;
  976. if (!methodDef->mIsStatic)
  977. continue;
  978. bool canUseMethod;
  979. canUseMethod = (methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Mixin);
  980. canUseMethod &= CheckProtection(methodDef->mProtection, methodDef->mDeclaringType, allowProtected, allowPrivate);
  981. if (methodDef->mMethodType != BfMethodType_Normal)
  982. continue;
  983. auto methodInstance = mModule->GetRawMethodInstanceAtIdx(typeInst, methodDef->mIdx);
  984. if (methodInstance == NULL)
  985. continue;
  986. if (methodInstance->mReturnType->IsUnspecializedType())
  987. continue;
  988. if (!mModule->CanCast(BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), methodInstance->mReturnType), selfType))
  989. continue;
  990. if (canUseMethod)
  991. {
  992. if (auto methodDeclaration = methodDef->GetMethodDeclaration())
  993. {
  994. AddMethod(typeInst, methodDef, NULL, methodDeclaration, methodDef->mName, filter);
  995. }
  996. }
  997. }
  998. for (auto propDef : typeInst->mTypeDef->mProperties)
  999. {
  1000. if (propDef->mShow > checkShow)
  1001. continue;
  1002. if ((!typeInst->IsTypeMemberIncluded(propDef->mDeclaringType, activeTypeDef, mModule)) ||
  1003. (!typeInst->IsTypeMemberAccessible(propDef->mDeclaringType, activeTypeDef)))
  1004. continue;
  1005. if (!propDef->mIsStatic)
  1006. continue;
  1007. BfMethodDef* getMethod = NULL;
  1008. for (auto methodDef : propDef->mMethods)
  1009. {
  1010. if (methodDef->mMethodType == BfMethodType_PropertyGetter)
  1011. {
  1012. getMethod = methodDef;
  1013. break;
  1014. }
  1015. }
  1016. if (getMethod == NULL)
  1017. continue;
  1018. auto methodInstance = mModule->GetRawMethodInstanceAtIdx(typeInst, getMethod->mIdx);
  1019. if (methodInstance == NULL)
  1020. continue;
  1021. if (methodInstance->mReturnType != selfType)
  1022. continue;
  1023. if (CheckProtection(propDef->mProtection, propDef->mDeclaringType, allowProtected, allowPrivate))
  1024. {
  1025. if (propDef->HasExplicitInterface())
  1026. continue;
  1027. if (propDef->mName == "[]")
  1028. continue;
  1029. AddProp(typeInst, propDef, filter);
  1030. }
  1031. }
  1032. auto outerType = mModule->GetOuterType(typeInst);
  1033. if (outerType != NULL)
  1034. {
  1035. AddSelfResultTypeMembers(outerType, selfType, filter, false);
  1036. }
  1037. }
  1038. bool BfAutoComplete::InitAutocomplete(BfAstNode* dotNode, BfAstNode* nameNode, String& filter)
  1039. {
  1040. bool isDot = (dotNode != NULL) && (dotNode->mToken == BfToken_Dot);
  1041. if (IsAutocompleteNode(nameNode))
  1042. {
  1043. auto bfParser = nameNode->GetSourceData()->ToParser();
  1044. if ((mIsGetDefinition) || (!isDot))
  1045. {
  1046. mInsertStartIdx = nameNode->GetSrcStart();
  1047. mInsertEndIdx = nameNode->GetSrcEnd();
  1048. }
  1049. else
  1050. {
  1051. mInsertStartIdx = dotNode->GetSrcEnd();
  1052. mInsertEndIdx = std::min(bfParser->mCursorIdx + 1, nameNode->GetSrcEnd());
  1053. }
  1054. filter.Append(bfParser->mSrc + nameNode->GetSrcStart(), mInsertEndIdx - nameNode->GetSrcStart());
  1055. return true;
  1056. }
  1057. int lenAdd = 0;
  1058. if (!isDot)
  1059. lenAdd++;
  1060. if ((dotNode != NULL) && (IsAutocompleteNode(dotNode, lenAdd, 1)))
  1061. {
  1062. mInsertStartIdx = dotNode->GetSrcEnd();
  1063. mInsertEndIdx = dotNode->GetSrcEnd();
  1064. return true;
  1065. }
  1066. return false;
  1067. }
  1068. void BfAutoComplete::AddEnumTypeMembers(BfTypeInstance* typeInst, const StringImpl& filter, bool allowProtected, bool allowPrivate)
  1069. {
  1070. mModule->PopulateType(typeInst, BfPopulateType_Data);
  1071. auto activeTypeDef = mModule->GetActiveTypeDef();
  1072. for (auto& fieldInst : typeInst->mFieldInstances)
  1073. {
  1074. auto fieldDef = fieldInst.GetFieldDef();
  1075. if ((fieldDef != NULL) && (fieldDef->mIsConst) &&
  1076. ((fieldInst.mResolvedType == typeInst) || (fieldInst.mIsEnumPayloadCase)) &&
  1077. (CheckProtection(fieldDef->mProtection, fieldDef->mDeclaringType, allowProtected, allowPrivate)))
  1078. {
  1079. if ((!typeInst->IsTypeMemberIncluded(fieldDef->mDeclaringType, activeTypeDef, mModule)) ||
  1080. (!typeInst->IsTypeMemberAccessible(fieldDef->mDeclaringType, activeTypeDef)))
  1081. continue;
  1082. bool hasPayload = false;
  1083. if ((fieldInst.mIsEnumPayloadCase) && (fieldInst.mResolvedType->IsTuple()))
  1084. {
  1085. auto payloadType = (BfTypeInstance*)fieldInst.mResolvedType;
  1086. if (!payloadType->mFieldInstances.empty())
  1087. hasPayload = true;
  1088. }
  1089. AutoCompleteEntry entry(hasPayload ? "payloadEnum" : "value", fieldDef->mName);
  1090. if (auto entryAdded = AddEntry(entry, filter))
  1091. {
  1092. auto fieldDecl = fieldDef->GetFieldDeclaration();
  1093. if (fieldDecl == NULL)
  1094. continue;
  1095. if (CheckDocumentation(entryAdded, fieldDecl->mDocumentation))
  1096. {
  1097. }
  1098. if (mIsGetDefinition)
  1099. {
  1100. mDefType = typeInst->mTypeDef;
  1101. mDefField = fieldDef;
  1102. if (fieldDecl != NULL)
  1103. SetDefinitionLocation(fieldDecl->mNameNode);
  1104. }
  1105. }
  1106. }
  1107. }
  1108. }
  1109. void BfAutoComplete::AddExtensionMethods(BfTypeInstance* targetType, BfTypeInstance* extensionContainer, const StringImpl & filter, bool allowProtected, bool allowPrivate)
  1110. {
  1111. if (!extensionContainer->mTypeDef->mHasExtensionMethods)
  1112. return;
  1113. mModule->PopulateType(extensionContainer, BfPopulateType_Data);
  1114. BfShow checkShow = allowPrivate ? BfShow_Hide : BfShow_HideIndirect;
  1115. for (auto methodDef : extensionContainer->mTypeDef->mMethods)
  1116. {
  1117. if (methodDef->mMethodType != BfMethodType_Extension)
  1118. continue;
  1119. if (methodDef->mShow >= checkShow)
  1120. continue;
  1121. if (methodDef->mName.IsEmpty())
  1122. continue;
  1123. bool canUseMethod = true;
  1124. canUseMethod &= CheckProtection(methodDef->mProtection, methodDef->mDeclaringType, allowProtected, allowPrivate);
  1125. auto methodInstance = mModule->GetRawMethodInstanceAtIdx(extensionContainer, methodDef->mIdx);
  1126. if (methodInstance == NULL)
  1127. continue;
  1128. int score;
  1129. uint8 matches[256];
  1130. // Do filter match first- may be cheaper than generic validation
  1131. if (!DoesFilterMatch(methodDef->mName.c_str(), filter.c_str(), score, matches, sizeof(matches)))
  1132. continue;
  1133. auto thisType = methodInstance->GetParamType(0);
  1134. bool paramValidated = false;
  1135. if (methodInstance->GetNumGenericParams() > 0)
  1136. {
  1137. if ((thisType->IsGenericParam()) && (methodInstance->GetNumGenericParams() == 1))
  1138. {
  1139. auto genericParamType = (BfGenericParamType*)thisType;
  1140. if (genericParamType->mGenericParamKind == BfGenericParamKind_Method)
  1141. {
  1142. auto& genericParams = methodInstance->mMethodInfoEx->mGenericParams;
  1143. if (!mModule->CheckGenericConstraints(BfGenericParamSource(methodInstance), targetType, NULL, genericParams[genericParamType->mGenericParamIdx], NULL, NULL))
  1144. continue;
  1145. paramValidated = true;
  1146. }
  1147. }
  1148. if (((thisType->IsUnspecializedTypeVariation()) || (thisType->IsGenericParam())) &&
  1149. (!paramValidated))
  1150. {
  1151. BfTypeVector genericTypeVector;
  1152. genericTypeVector.resize(methodInstance->GetNumGenericParams());
  1153. BfGenericInferContext genericInferContext;
  1154. genericInferContext.mCheckMethodGenericArguments = &genericTypeVector;
  1155. genericInferContext.mModule = mModule;
  1156. genericInferContext.mPrevArgValues.resize(methodInstance->GetNumGenericParams());
  1157. if (!genericInferContext.InferGenericArgument(methodInstance, targetType, thisType, BfIRValue()))
  1158. continue;
  1159. genericInferContext.InferGenericArguments(methodInstance);
  1160. thisType = mModule->ResolveGenericType(thisType, NULL, &genericTypeVector, mModule->mCurTypeInstance, false);
  1161. if (thisType == NULL)
  1162. continue;
  1163. auto& genericParams = methodInstance->mMethodInfoEx->mGenericParams;
  1164. bool validateError = false;
  1165. for (int genericIdx = 0; genericIdx < (int)genericTypeVector.size(); genericIdx++)
  1166. {
  1167. auto genericArg = genericTypeVector[genericIdx];
  1168. if (genericArg == NULL)
  1169. continue;
  1170. if (!mModule->CheckGenericConstraints(BfGenericParamSource(methodInstance), genericArg, NULL, genericParams[genericIdx], &genericTypeVector, NULL))
  1171. {
  1172. validateError = true;
  1173. break;
  1174. }
  1175. }
  1176. if (validateError)
  1177. continue;
  1178. }
  1179. }
  1180. if (!paramValidated)
  1181. {
  1182. if (!mModule->CanCast(BfTypedValue(mModule->mBfIRBuilder->GetFakeVal(), targetType), thisType))
  1183. continue;
  1184. }
  1185. if (canUseMethod)
  1186. {
  1187. if (auto methodDeclaration = methodDef->GetMethodDeclaration())
  1188. {
  1189. AddMethod(extensionContainer, methodDef, NULL, methodDeclaration, methodDef->mName, filter);
  1190. }
  1191. }
  1192. }
  1193. }
  1194. BfProject* BfAutoComplete::GetActiveProject()
  1195. {
  1196. BfProject* bfProject = NULL;
  1197. auto activeTypeDef = mModule->GetActiveTypeDef();
  1198. if (activeTypeDef != NULL)
  1199. bfProject = activeTypeDef->mProject;
  1200. else if (!mCompiler->mResolvePassData->mParsers.IsEmpty())
  1201. bfProject = mCompiler->mResolvePassData->mParsers[0]->mProject;
  1202. return bfProject;
  1203. }
  1204. bool BfAutoComplete::WantsEntries()
  1205. {
  1206. if (mModule == NULL)
  1207. return false;
  1208. return (mResolveType == BfResolveType_Autocomplete) ||
  1209. (mResolveType == BfResolveType_Autocomplete_HighPri) ||
  1210. (mResolveType == BfResolveType_GetSymbolInfo) ||
  1211. (mResolveType == BfResolveType_GoToDefinition) ||
  1212. (mResolveType == BfResolveType_VerifyTypeName);
  1213. }
  1214. void BfAutoComplete::AddTopLevelNamespaces(BfAstNode* identifierNode)
  1215. {
  1216. String filter;
  1217. if (identifierNode != NULL)
  1218. {
  1219. filter = identifierNode->ToString();
  1220. mInsertStartIdx = identifierNode->GetSrcStart();
  1221. mInsertEndIdx = identifierNode->GetSrcEnd();
  1222. }
  1223. BfProject* bfProject = GetActiveProject();
  1224. auto _AddProjectNamespaces = [&](BfProject* project)
  1225. {
  1226. for (auto namespacePair : project->mNamespaces)
  1227. {
  1228. const BfAtomComposite& namespaceComposite = namespacePair.mKey;
  1229. if (namespaceComposite.GetPartsCount() == 1)
  1230. {
  1231. AddEntry(AutoCompleteEntry("namespace", namespaceComposite.ToString()), filter);
  1232. }
  1233. }
  1234. };
  1235. if (bfProject != NULL)
  1236. {
  1237. for (int depIdx = -1; depIdx < (int) bfProject->mDependencies.size(); depIdx++)
  1238. {
  1239. BfProject* depProject = (depIdx == -1) ? bfProject : bfProject->mDependencies[depIdx];
  1240. _AddProjectNamespaces(depProject);
  1241. }
  1242. }
  1243. else
  1244. {
  1245. for (auto project : mSystem->mProjects)
  1246. _AddProjectNamespaces(project);
  1247. }
  1248. }
  1249. void BfAutoComplete::AddTopLevelTypes(BfAstNode* identifierNode, bool onlyAttribute)
  1250. {
  1251. String filter;
  1252. if (identifierNode != NULL)
  1253. {
  1254. filter = identifierNode->ToString();
  1255. mInsertStartIdx = identifierNode->GetSrcStart();
  1256. mInsertEndIdx = identifierNode->GetSrcEnd();
  1257. }
  1258. AddEntry(AutoCompleteEntry("token", "function"), filter);
  1259. AddEntry(AutoCompleteEntry("token", "delegate"), filter);
  1260. AddEntry(AutoCompleteEntry("token", "decltype"), filter);
  1261. if (mModule->mCurTypeInstance != NULL)
  1262. {
  1263. if (!onlyAttribute)
  1264. {
  1265. auto activeTypeDef = mModule->GetActiveTypeDef();
  1266. for (auto genericParam : activeTypeDef->mGenericParamDefs)
  1267. AddEntry(AutoCompleteEntry("generic", genericParam->mName), filter);
  1268. }
  1269. AddCurrentTypes(mModule->mCurTypeInstance, filter, true, true, onlyAttribute);
  1270. }
  1271. if (mModule->mCurMethodInstance != NULL)
  1272. {
  1273. if (!onlyAttribute)
  1274. {
  1275. for (auto genericParam : mModule->mCurMethodInstance->mMethodDef->mGenericParams)
  1276. AddEntry(AutoCompleteEntry("generic", genericParam->mName), filter);
  1277. }
  1278. }
  1279. if (!onlyAttribute)
  1280. {
  1281. BfTypeDef* showTypeDef = NULL;
  1282. for (auto& systemTypeDefEntry : mModule->mSystem->mSystemTypeDefs)
  1283. {
  1284. auto systemTypeDef = systemTypeDefEntry.mValue;
  1285. if ((systemTypeDef->mTypeCode == BfTypeCode_IntUnknown) || (systemTypeDef->mTypeCode == BfTypeCode_UIntUnknown))
  1286. continue;
  1287. if ((AddEntry(AutoCompleteEntry("valuetype", systemTypeDef->mName->mString.mPtr), filter)) && (mIsGetDefinition))
  1288. showTypeDef = systemTypeDef;
  1289. }
  1290. AddEntry(AutoCompleteEntry("valuetype", "SelfBase"), filter);
  1291. AddEntry(AutoCompleteEntry("valuetype", "SelfOuter"), filter);
  1292. if (showTypeDef != NULL)
  1293. {
  1294. auto showType = mModule->ResolveTypeDef(showTypeDef);
  1295. BfTypeInstance* showTypeInst = NULL;
  1296. if (showType->IsPrimitiveType())
  1297. showTypeInst = mModule->GetWrappedStructType(showType);
  1298. else
  1299. showTypeInst = showType->ToTypeInstance();
  1300. if (showTypeInst != NULL)
  1301. SetDefinitionLocation(showTypeInst->mTypeDef->mTypeDeclaration->mNameNode);
  1302. }
  1303. }
  1304. auto activeTypeDef = mModule->GetActiveTypeDef();
  1305. if (activeTypeDef != NULL)
  1306. {
  1307. BfProject* curProject = activeTypeDef->mProject;
  1308. if (mModule->mCurTypeInstance != NULL)
  1309. {
  1310. for (auto innerTypeDef : mModule->mCurTypeInstance->mTypeDef->mNestedTypes)
  1311. {
  1312. if (!mModule->mCurTypeInstance->IsTypeMemberAccessible(innerTypeDef, activeTypeDef))
  1313. continue;
  1314. AddTypeDef(innerTypeDef, filter, onlyAttribute);
  1315. }
  1316. }
  1317. auto& namespaceSearch = activeTypeDef->mNamespaceSearch;
  1318. String prevName;
  1319. for (auto typeDef : mModule->mSystem->mTypeDefs)
  1320. {
  1321. if (typeDef->mIsPartial)
  1322. continue;
  1323. //TODO :Check protection
  1324. if ((curProject != NULL) && (curProject->ContainsReference(typeDef->mProject)))
  1325. {
  1326. bool matches = false;
  1327. if (typeDef->mOuterType == NULL)
  1328. {
  1329. if (((typeDef->mNamespace.IsEmpty()) ||
  1330. (namespaceSearch.Contains(typeDef->mNamespace))))
  1331. matches = true;
  1332. }
  1333. if (matches)
  1334. {
  1335. AddTypeDef(typeDef, filter, onlyAttribute);
  1336. }
  1337. }
  1338. }
  1339. }
  1340. else
  1341. {
  1342. BfProject* curProject = NULL;
  1343. if (!mModule->mCompiler->mResolvePassData->mParsers.IsEmpty())
  1344. curProject = mModule->mCompiler->mResolvePassData->mParsers[0]->mProject;
  1345. String prevName;
  1346. for (auto typeDef : mModule->mSystem->mTypeDefs)
  1347. {
  1348. if (typeDef->mIsPartial)
  1349. continue;
  1350. //TODO :Check protection
  1351. if ((curProject != NULL) && (curProject->ContainsReference(typeDef->mProject)))
  1352. {
  1353. bool matches = false;
  1354. if (typeDef->mOuterType == NULL)
  1355. {
  1356. if (typeDef->mNamespace.IsEmpty())
  1357. matches = true;
  1358. }
  1359. if (matches)
  1360. {
  1361. AddTypeDef(typeDef, filter, onlyAttribute);
  1362. }
  1363. }
  1364. }
  1365. }
  1366. }
  1367. void BfAutoComplete::CheckIdentifier(BfAstNode* identifierNode, bool isInExpression, bool isUsingDirective)
  1368. {
  1369. if ((identifierNode != NULL) && (!IsAutocompleteNode(identifierNode)))
  1370. return;
  1371. mIdentifierUsed = identifierNode;
  1372. if ((mModule->mParentNodeEntry != NULL) && (mModule->mCurMethodState != NULL))
  1373. {
  1374. if (auto binExpr = BfNodeDynCast<BfBinaryOperatorExpression>(mModule->mParentNodeEntry->mNode))
  1375. {
  1376. auto parentBlock = mModule->mCurMethodState->mCurScope->mAstBlock;
  1377. if ((identifierNode == binExpr->mRight) && (binExpr->mOp == BfBinaryOp_Multiply) && (parentBlock != NULL))
  1378. {
  1379. // If we are the last identifier in a block then we MAY be a partially-typed variable declaration
  1380. if (parentBlock->mChildArr.back() == binExpr)
  1381. {
  1382. mUncertain = true;
  1383. }
  1384. }
  1385. }
  1386. }
  1387. if (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(identifierNode))
  1388. {
  1389. CheckMemberReference(qualifiedNameNode->mLeft, qualifiedNameNode->mDot, qualifiedNameNode->mRight, false, NULL, isUsingDirective);
  1390. return;
  1391. }
  1392. AddTopLevelNamespaces(identifierNode);
  1393. if (isUsingDirective)
  1394. return; // Only do namespaces
  1395. AddTopLevelTypes(identifierNode);
  1396. String filter;
  1397. if (identifierNode != NULL)
  1398. {
  1399. filter = identifierNode->ToString();
  1400. mInsertStartIdx = identifierNode->GetSrcStart();
  1401. mInsertEndIdx = identifierNode->GetSrcEnd();
  1402. }
  1403. String addStr;
  1404. if (mShowAttributeProperties != NULL)
  1405. {
  1406. auto showAttrTypeDef = mShowAttributeProperties->mTypeDef;
  1407. for (auto prop : showAttrTypeDef->mProperties)
  1408. {
  1409. if (auto entryAdded = AddEntry(AutoCompleteEntry("property", prop->mName + "="), filter))
  1410. {
  1411. auto propertyDeclaration = prop->GetFieldDeclaration();
  1412. if (propertyDeclaration != NULL)
  1413. {
  1414. if (CheckDocumentation(entryAdded, propertyDeclaration->mDocumentation))
  1415. {
  1416. }
  1417. if (mIsGetDefinition)
  1418. SetDefinitionLocation(propertyDeclaration->mNameNode);
  1419. }
  1420. }
  1421. }
  1422. for (auto field : showAttrTypeDef->mFields)
  1423. {
  1424. if (auto entryAdded = AddEntry(AutoCompleteEntry("field", field->mName + "="), filter))
  1425. {
  1426. auto fieldDeclaration = field->GetFieldDeclaration();
  1427. if (fieldDeclaration != NULL)
  1428. {
  1429. if (CheckDocumentation(entryAdded, fieldDeclaration->mDocumentation))
  1430. {
  1431. }
  1432. if (mIsGetDefinition)
  1433. SetDefinitionLocation(fieldDeclaration->mNameNode);
  1434. }
  1435. }
  1436. }
  1437. }
  1438. if ((mModule->mContext->mCurTypeState != NULL) && (mModule->mContext->mCurTypeState->mType != NULL))
  1439. {
  1440. BF_ASSERT(mModule->mCurTypeInstance == mModule->mContext->mCurTypeState->mType);
  1441. BfGlobalLookup globalLookup;
  1442. globalLookup.mKind = BfGlobalLookup::Kind_All;
  1443. mModule->PopulateGlobalContainersList(globalLookup);
  1444. for (auto& globalContainer : mModule->mContext->mCurTypeState->mGlobalContainers)
  1445. {
  1446. if (globalContainer.mTypeInst != NULL)
  1447. AddTypeMembers(globalContainer.mTypeInst, true, false, filter, globalContainer.mTypeInst, true, true, false);
  1448. }
  1449. }
  1450. BfStaticSearch* staticSearch = mModule->GetStaticSearch();
  1451. if (staticSearch != NULL)
  1452. {
  1453. for (auto typeInst : staticSearch->mStaticTypes)
  1454. {
  1455. AddTypeMembers(typeInst, true, false, filter, typeInst, true, true, false);
  1456. AddInnerTypes(typeInst, filter, typeInst, false, false);
  1457. }
  1458. }
  1459. if (auto ceDbgState = mModule->GetCeDbgState())
  1460. {
  1461. auto ceDebugger = mModule->mCompiler->mCeMachine->mDebugger;
  1462. auto ceContext = ceDebugger->mCurDbgState->mCeContext;
  1463. auto activeFrame = ceDebugger->mCurDbgState->mActiveFrame;
  1464. if (activeFrame->mFunction->mDbgInfo != NULL)
  1465. {
  1466. int instIdx = activeFrame->GetInstIdx();
  1467. for (auto& dbgVar : activeFrame->mFunction->mDbgInfo->mVariables)
  1468. {
  1469. if ((instIdx >= dbgVar.mStartCodePos) && (instIdx < dbgVar.mEndCodePos))
  1470. AddEntry(AutoCompleteEntry(GetTypeName(dbgVar.mType), dbgVar.mName), filter);
  1471. }
  1472. }
  1473. }
  1474. //////////////////////////////////////////////////////////////////////////
  1475. BfMethodInstance* curMethodInstance = mModule->mCurMethodInstance;
  1476. if (mModule->mCurMethodState != NULL)
  1477. curMethodInstance = mModule->mCurMethodState->GetRootMethodState()->mMethodInstance;
  1478. if (curMethodInstance != NULL)
  1479. {
  1480. if (!curMethodInstance->mMethodDef->mIsStatic)
  1481. {
  1482. if (mModule->mCurTypeInstance->IsObject())
  1483. AddEntry(AutoCompleteEntry("object", "this"), filter);
  1484. else
  1485. AddEntry(AutoCompleteEntry("pointer", "this"), filter);
  1486. AddTypeMembers(mModule->mCurTypeInstance, true, true, filter, mModule->mCurTypeInstance, mModule->mCurTypeInstance->IsInterface(), true, true);
  1487. }
  1488. else
  1489. {
  1490. AddTypeMembers(mModule->mCurTypeInstance, true, false, filter, mModule->mCurTypeInstance, mModule->mCurTypeInstance->IsInterface(), true, true);
  1491. }
  1492. if (mModule->mCurMethodState != NULL)
  1493. {
  1494. int varSkipCount = 0;
  1495. StringT<128> wantName = filter;
  1496. while (wantName.StartsWith("@"))
  1497. {
  1498. varSkipCount++;
  1499. wantName.Remove(0);
  1500. }
  1501. if (varSkipCount > 0)
  1502. {
  1503. Dictionary<String, int> localCount;
  1504. auto varMethodState = mModule->mCurMethodState;
  1505. while (varMethodState != NULL)
  1506. {
  1507. for (int localIdx = (int)varMethodState->mLocals.size() - 1; localIdx >= 0; localIdx--)
  1508. {
  1509. auto local = varMethodState->mLocals[localIdx];
  1510. int* findIdx = NULL;
  1511. if (localCount.TryAdd(local->mName, NULL, &findIdx))
  1512. {
  1513. *findIdx = 0;
  1514. }
  1515. else
  1516. {
  1517. (*findIdx)++;
  1518. }
  1519. if (varSkipCount - local->mNamePrefixCount - *findIdx == 0)
  1520. {
  1521. if ((AddEntry(AutoCompleteEntry(GetTypeName(local->mResolvedType), local->mName, varSkipCount - local->mNamePrefixCount - *findIdx), wantName)) && (mIsGetDefinition))
  1522. {
  1523. }
  1524. }
  1525. }
  1526. varMethodState = varMethodState->mPrevMethodState;
  1527. if ((varMethodState == NULL) ||
  1528. (varMethodState->mMixinState != NULL) ||
  1529. ((varMethodState->mClosureState != NULL) && (!varMethodState->mClosureState->mCapturing)))
  1530. break;
  1531. }
  1532. mInsertStartIdx += varSkipCount;
  1533. }
  1534. else
  1535. {
  1536. auto varMethodState = mModule->mCurMethodState;
  1537. while (varMethodState != NULL)
  1538. {
  1539. for (auto& local : varMethodState->mLocals)
  1540. {
  1541. if ((AddEntry(AutoCompleteEntry(GetTypeName(local->mResolvedType), local->mName), wantName)) && (mIsGetDefinition))
  1542. {
  1543. }
  1544. }
  1545. varMethodState = varMethodState->mPrevMethodState;
  1546. if ((varMethodState == NULL) ||
  1547. (varMethodState->mMixinState != NULL) ||
  1548. ((varMethodState->mClosureState != NULL) && (!varMethodState->mClosureState->mCapturing)))
  1549. break;
  1550. }
  1551. }
  1552. }
  1553. }
  1554. else if (mModule->mCurTypeInstance != NULL)
  1555. {
  1556. bool staticOnly = true;
  1557. if ((mModule->mCurMethodState != NULL) && (mModule->mCurMethodState->mTempKind == BfMethodState::TempKind_NonStatic))
  1558. staticOnly = false;
  1559. //BF_ASSERT(mModule->mCurTypeInstance->mResolvingConstField);
  1560. AddTypeMembers(mModule->mCurTypeInstance, true, !staticOnly, filter, mModule->mCurTypeInstance, false, true, true);
  1561. }
  1562. auto checkMethodState = mModule->mCurMethodState;
  1563. while (checkMethodState != NULL)
  1564. {
  1565. for (auto localMethod : checkMethodState->mLocalMethods)
  1566. {
  1567. if (localMethod->mMethodInstanceGroup != NULL)
  1568. AddMethod(mModule->mCurTypeInstance, localMethod->mMethodDef, localMethod->mMethodInstanceGroup->mDefault, localMethod->mMethodDeclaration, localMethod->mMethodName, filter);
  1569. }
  1570. checkMethodState = checkMethodState->mPrevMethodState;
  1571. }
  1572. if (isInExpression)
  1573. {
  1574. const char* tokens [] =
  1575. {
  1576. "alignof", "append", "as", "asm", "base", "break", "case", "catch", "checked", "continue", "const", "default", "defer",
  1577. "delegate", "delete", "do", "else", "false", "finally",
  1578. "fixed", "for", "function", "if", "implicit", "in", "internal", "is", "isconst", "new", "mixin", "null",
  1579. "offsetof", "out", "params", "readonly", "ref", "rettype", "return",
  1580. "sealed", "sizeof", "scope", "static", "strideof", "struct", "switch", /*"this",*/ "try", "true", "typeof", "unchecked",
  1581. "using", "var", "virtual", "volatile", "where", "while",
  1582. "alloctype", "comptype", "decltype", "nullable",
  1583. };
  1584. for (int i = 0; i < sizeof(tokens) / sizeof(char*); i++)
  1585. AddEntry(AutoCompleteEntry("token", tokens[i]), filter);
  1586. if ((mModule->mCurMethodState != NULL) && (mModule->mCurMethodState->mBreakData != NULL) && (mModule->mCurMethodState->mBreakData->mIRFallthroughBlock))
  1587. {
  1588. AddEntry(AutoCompleteEntry("token", "fallthrough"), filter);
  1589. }
  1590. }
  1591. else
  1592. {
  1593. const char* tokens[] =
  1594. {
  1595. "abstract", "append", "base", "class", "const",
  1596. "delegate", "extern", "enum", "explicit", "extension", "function",
  1597. "interface", "in", "implicit", "internal", "mixin", "namespace", "new",
  1598. "operator", "out", "override", "params", "private", "protected", "public", "readonly", "ref", "rettype", "return",
  1599. "scope", "sealed", "static", "struct", "this", "typealias",
  1600. "using", "virtual", "volatile", "T", "where"
  1601. };
  1602. for (int i = 0; i < sizeof(tokens)/sizeof(char*); i++)
  1603. AddEntry(AutoCompleteEntry("token", tokens[i]), filter);
  1604. }
  1605. //if ((identifierNode != NULL) && ((mModule->mCurMethodInstance == NULL) || (BfNodeDynCast<BfExpression>(identifierNode->mParent) != NULL)))
  1606. /*if ((identifierNode != NULL) && ((mModule->mCurMethodInstance == NULL) || (isInExpression)))
  1607. {
  1608. AddEntry(AutoCompleteEntry("token", "#if"), filter);
  1609. AddEntry(AutoCompleteEntry("token", "#elif"), filter);
  1610. AddEntry(AutoCompleteEntry("token", "#endif"), filter);
  1611. }*/
  1612. //OutputDebugStrF("Autocomplete: %s\n", str.c_str());
  1613. }
  1614. String BfAutoComplete::GetFilter(BfAstNode* node)
  1615. {
  1616. String filter = node->ToString();
  1617. if (mIsGetDefinition)
  1618. {
  1619. mInsertEndIdx = node->GetSrcEnd();
  1620. }
  1621. else
  1622. {
  1623. // Only use member name up to cursor
  1624. auto bfParser = node->GetSourceData()->ToParser();
  1625. int cursorIdx = bfParser->mCursorIdx;
  1626. filter = filter.Substring(0, BF_CLAMP(cursorIdx - node->GetSrcStart(), 0, (int)filter.length()));
  1627. mInsertEndIdx = cursorIdx;
  1628. }
  1629. const char* cPtr = filter.c_str();
  1630. while (cPtr[0] == '@')
  1631. {
  1632. mInsertStartIdx++;
  1633. cPtr++;
  1634. }
  1635. return filter;
  1636. }
  1637. bool BfAutoComplete::CheckMemberReference(BfAstNode* target, BfAstNode* dotToken, BfAstNode* memberName, bool onlyShowTypes, BfType* expectingType, bool isUsingDirective, bool onlyAttribute)
  1638. {
  1639. if (!WantsEntries())
  1640. return false;
  1641. BfAttributedIdentifierNode* attrIdentifier = NULL;
  1642. bool isAutocompletingName = false;
  1643. if ((attrIdentifier = BfNodeDynCast<BfAttributedIdentifierNode>(memberName)))
  1644. {
  1645. memberName = attrIdentifier->mIdentifier;
  1646. if (IsAutocompleteNode(attrIdentifier->mAttributes))
  1647. {
  1648. auto bfParser = attrIdentifier->mAttributes->GetSourceData()->ToParser();
  1649. int cursorIdx = bfParser->mCursorIdx;
  1650. if (cursorIdx == attrIdentifier->mAttributes->GetSrcEnd())
  1651. isAutocompletingName = true;
  1652. else
  1653. return false;
  1654. }
  1655. }
  1656. if (memberName != NULL)
  1657. isAutocompletingName = IsAutocompleteNode(dotToken, memberName, 0, 1);
  1658. if ((IsAutocompleteNode(dotToken, 0, 1)) || (isAutocompletingName))
  1659. {
  1660. BfLogSys(mModule->mSystem, "Triggered autocomplete\n");
  1661. bool isFriend = false;
  1662. mInsertStartIdx = dotToken->GetSrcEnd();
  1663. if (attrIdentifier != NULL)
  1664. {
  1665. BfAttributeState attributeState;
  1666. attributeState.mTarget = (BfAttributeTargets)(BfAttributeTargets_MemberAccess | BfAttributeTargets_Invocation);
  1667. attributeState.mCustomAttributes = mModule->GetCustomAttributes(attrIdentifier->mAttributes, attributeState.mTarget);
  1668. if ((attributeState.mCustomAttributes != NULL) && (attributeState.mCustomAttributes->Contains(mModule->mCompiler->mFriendAttributeTypeDef)))
  1669. {
  1670. isFriend = true;
  1671. attributeState.mUsed = true;
  1672. }
  1673. mInsertStartIdx = attrIdentifier->mAttributes->GetSrcEnd();
  1674. }
  1675. if (memberName != NULL)
  1676. {
  1677. //Member name MAY be incorrectly identified in cases like:
  1678. // val._
  1679. // OtherCall();
  1680. int cursorIdx = GetCursorIdx(memberName);
  1681. if ((cursorIdx != -1) && (cursorIdx >= memberName->GetSrcStart()))
  1682. mInsertStartIdx = memberName->GetSrcStart();
  1683. }
  1684. SetAndRestoreValue<bool> prevFriendSet(mHasFriendSet, mHasFriendSet || isFriend);
  1685. String filter;
  1686. if ((memberName != NULL) && (IsAutocompleteNode(memberName)))
  1687. {
  1688. filter = GetFilter(memberName);
  1689. }
  1690. else if (mResolveType != BfResolveType_Autocomplete)
  1691. mInsertStartIdx = -1; // Require a full span for everything but autocomplete
  1692. SetAndRestoreValue<bool> prevIgnoreErrors(mModule->mIgnoreErrors, true);
  1693. bool isStatic = false;
  1694. BfTypedValue targetValue = LookupTypeRefOrIdentifier(target, &isStatic, (BfEvalExprFlags)(BfEvalExprFlags_IgnoreNullConditional | BfEvalExprFlags_NoCast), expectingType);
  1695. if ((targetValue) && (dotToken->mToken == BfToken_Arrow))
  1696. {
  1697. SetAndRestoreValue<bool> prevIgnoreClassifying(mModule->mIsInsideAutoComplete, true);
  1698. BfExprEvaluator exprEvaluator(mModule);
  1699. auto arrowValue = exprEvaluator.PerformUnaryOperation_TryOperator(targetValue, NULL, BfUnaryOp_Arrow, BfNodeDynCast<BfTokenNode>(dotToken), BfUnaryOpFlag_None);
  1700. if (arrowValue)
  1701. targetValue = arrowValue;
  1702. }
  1703. bool hadResults = false;
  1704. bool doAsNamespace = true;
  1705. if ((targetValue.mType) && (!isUsingDirective))
  1706. {
  1707. doAsNamespace = false;
  1708. if (auto dotTokenNode = BfNodeDynCast<BfTokenNode>(dotToken))
  1709. {
  1710. if (dotTokenNode->GetToken() == BfToken_QuestionDot)
  1711. {
  1712. if (!targetValue.mType->IsNullable())
  1713. {
  1714. // We need this for Result<T>
  1715. SetAndRestoreValue<bool> prevIgnore(mModule->mBfIRBuilder->mIgnoreWrites, true);
  1716. BfExprEvaluator exprEvaluator(mModule);
  1717. auto opResult = exprEvaluator.PerformUnaryOperation_TryOperator(targetValue, NULL, BfUnaryOp_NullConditional, dotTokenNode, BfUnaryOpFlag_None);
  1718. if (opResult)
  1719. targetValue = opResult;
  1720. }
  1721. // ?. should look inside nullable types
  1722. if (targetValue.mType->IsNullable())
  1723. {
  1724. BfTypeInstance* nullableType = (BfTypeInstance*)targetValue.mType->ToTypeInstance();
  1725. targetValue = mModule->MakeAddressable(targetValue);
  1726. BfIRValue valuePtr = mModule->mBfIRBuilder->CreateInBoundsGEP(targetValue.mValue, 0, 1); // mValue
  1727. targetValue = BfTypedValue(valuePtr, nullableType->mGenericTypeInfo->mTypeGenericArguments[0], true);
  1728. }
  1729. }
  1730. }
  1731. // Statics, inner types
  1732. auto checkType = targetValue.mType;
  1733. if (checkType->IsConcreteInterfaceType())
  1734. checkType = checkType->GetUnderlyingType();
  1735. if (checkType->IsGenericParam())
  1736. {
  1737. auto genericParamType = (BfGenericParamType*)checkType;
  1738. auto genericParamInstance = mModule->GetGenericParamInstance(genericParamType);
  1739. auto _HandleGenericParamInstance = [&](BfGenericParamInstance* genericParamInstance)
  1740. {
  1741. bool showStatics = !targetValue.mValue;
  1742. for (auto interfaceConstraint : genericParamInstance->mInterfaceConstraints)
  1743. AddTypeMembers(interfaceConstraint, showStatics, !showStatics, filter, interfaceConstraint, true, false, false);
  1744. if (genericParamInstance->mTypeConstraint != NULL)
  1745. checkType = genericParamInstance->mTypeConstraint;
  1746. else
  1747. checkType = mModule->mContext->mBfObjectType;
  1748. if ((genericParamInstance->IsEnum()))
  1749. {
  1750. if (isStatic)
  1751. AddEntry(AutoCompleteEntry("valuetype", "UnderlyingType"), filter);
  1752. else
  1753. {
  1754. AddEntry(AutoCompleteEntry("value", "Underlying"), filter);
  1755. AddEntry(AutoCompleteEntry("value", "UnderlyingRef"), filter);
  1756. }
  1757. }
  1758. };
  1759. _HandleGenericParamInstance(genericParamInstance);
  1760. // Check method generic constraints
  1761. if ((mModule->mCurMethodInstance != NULL) && (mModule->mCurMethodInstance->mIsUnspecialized) && (mModule->mCurMethodInstance->mMethodInfoEx != NULL))
  1762. {
  1763. for (int genericParamIdx = (int)mModule->mCurMethodInstance->mMethodInfoEx->mMethodGenericArguments.size();
  1764. genericParamIdx < mModule->mCurMethodInstance->mMethodInfoEx->mGenericParams.size(); genericParamIdx++)
  1765. {
  1766. auto genericParam = mModule->mCurMethodInstance->mMethodInfoEx->mGenericParams[genericParamIdx];
  1767. if (genericParam->mExternType == genericParamType)
  1768. _HandleGenericParamInstance(genericParam);
  1769. }
  1770. }
  1771. }
  1772. if (checkType->IsPointer())
  1773. {
  1774. auto underlyingType = checkType->GetUnderlyingType();
  1775. if ((underlyingType != NULL) && (underlyingType->IsValueType()))
  1776. checkType = underlyingType;
  1777. }
  1778. auto typeInst = checkType->ToTypeInstance();
  1779. if ((typeInst == NULL) &&
  1780. ((checkType->IsPrimitiveType()) || (checkType->IsSizedArray())))
  1781. typeInst = mModule->GetWrappedStructType(checkType);
  1782. if (typeInst != NULL)
  1783. {
  1784. if (typeInst->mTypeDef->IsGlobalsContainer())
  1785. doAsNamespace = true; // Also list the types in this namespace
  1786. bool allowPrivate = (mModule->mCurTypeInstance == typeInst) || (mModule->IsInnerType(mModule->mCurTypeInstance, typeInst));
  1787. bool allowProtected = allowPrivate;
  1788. if (isStatic)
  1789. AddInnerTypes(typeInst, filter, typeInst, allowProtected, allowPrivate);
  1790. if (!onlyShowTypes)
  1791. {
  1792. AddTypeMembers(typeInst, isStatic, !isStatic, filter, typeInst, false, false, false);
  1793. if (!isStatic)
  1794. {
  1795. auto checkTypeInst = mModule->mCurTypeInstance;
  1796. while (checkTypeInst != NULL)
  1797. {
  1798. AddExtensionMethods(typeInst, checkTypeInst, filter, true, true);
  1799. checkTypeInst = mModule->GetOuterType(checkTypeInst);
  1800. }
  1801. if ((mModule->mContext->mCurTypeState != NULL) && (mModule->mContext->mCurTypeState->mType != NULL))
  1802. {
  1803. BF_ASSERT(mModule->mCurTypeInstance == mModule->mContext->mCurTypeState->mType);
  1804. BfGlobalLookup globalLookup;
  1805. globalLookup.mKind = BfGlobalLookup::Kind_All;
  1806. mModule->PopulateGlobalContainersList(globalLookup);
  1807. for (auto& globalContainer : mModule->mContext->mCurTypeState->mGlobalContainers)
  1808. if (globalContainer.mTypeInst != NULL)
  1809. AddExtensionMethods(typeInst, globalContainer.mTypeInst, filter, false, false);
  1810. }
  1811. BfStaticSearch* staticSearch = mModule->GetStaticSearch();
  1812. if (staticSearch != NULL)
  1813. {
  1814. for (auto staticTypeInst : staticSearch->mStaticTypes)
  1815. AddExtensionMethods(typeInst, staticTypeInst, filter, false, false);
  1816. }
  1817. }
  1818. }
  1819. if (typeInst->IsInterface())
  1820. {
  1821. AddTypeMembers(mModule->mContext->mBfObjectType, isStatic, !isStatic, filter, mModule->mContext->mBfObjectType, true, false, false);
  1822. }
  1823. }
  1824. hadResults = true;
  1825. }
  1826. if (doAsNamespace) // Lookup namespaces
  1827. {
  1828. String targetStr = target->ToString();
  1829. BfAtomComposite targetComposite;
  1830. bool isValid = mSystem->ParseAtomComposite(targetStr, targetComposite);
  1831. BfProject* bfProject = GetActiveProject();
  1832. auto _CheckProject = [&](BfProject* project)
  1833. {
  1834. if ((isValid) && (project->mNamespaces.ContainsKey(targetComposite)))
  1835. {
  1836. for (auto namespacePair : project->mNamespaces)
  1837. {
  1838. const BfAtomComposite& namespaceComposite = namespacePair.mKey;
  1839. if ((namespaceComposite.StartsWith(targetComposite)) && (namespaceComposite.GetPartsCount() > targetComposite.GetPartsCount()))
  1840. {
  1841. BfAtom* subNamespace = namespaceComposite.mParts[targetComposite.mSize];
  1842. AutoCompleteEntry entry("namespace", subNamespace->mString.mPtr);
  1843. AddEntry(entry, filter);
  1844. }
  1845. }
  1846. if (!isUsingDirective)
  1847. {
  1848. BfProject* activeProject = GetActiveProject();
  1849. for (auto typeDef : mSystem->mTypeDefs)
  1850. {
  1851. if ((typeDef->mNamespace == targetComposite) && (typeDef->mOuterType == NULL) &&
  1852. (!typeDef->mIsPartial) &&
  1853. ((activeProject == NULL) || (activeProject->ContainsReference(typeDef->mProject))))
  1854. {
  1855. if (typeDef->IsGlobalsContainer())
  1856. {
  1857. auto type = mModule->ResolveTypeDef(typeDef, BfPopulateType_Declaration);
  1858. if (type != NULL)
  1859. {
  1860. auto typeInst = type->ToTypeInstance();
  1861. if (typeInst != NULL)
  1862. AddTypeMembers(typeInst, true, false, filter, typeInst, false, false, false);
  1863. }
  1864. }
  1865. else
  1866. AddTypeDef(typeDef, filter, onlyAttribute);
  1867. }
  1868. }
  1869. }
  1870. hadResults = true;
  1871. }
  1872. };
  1873. if (bfProject != NULL)
  1874. {
  1875. for (int depIdx = -1; depIdx < (int)bfProject->mDependencies.size(); depIdx++)
  1876. {
  1877. BfProject* depProject = (depIdx == -1) ? bfProject : bfProject->mDependencies[depIdx];
  1878. _CheckProject(depProject);
  1879. }
  1880. }
  1881. else
  1882. {
  1883. for (auto project : mSystem->mProjects)
  1884. _CheckProject(project);
  1885. }
  1886. }
  1887. return hadResults;
  1888. }
  1889. else
  1890. {
  1891. auto identifierNode = BfNodeDynCast<BfIdentifierNode>(target);
  1892. if (identifierNode != NULL)
  1893. CheckIdentifier(identifierNode);
  1894. CheckTypeRef(BfNodeDynCast<BfTypeReference>(target), true, false, onlyAttribute);
  1895. }
  1896. return false;
  1897. }
  1898. bool BfAutoComplete::CheckExplicitInterface(BfTypeInstance* interfaceType, BfAstNode* dotToken, BfAstNode* memberName)
  1899. {
  1900. bool isAutocompletingName = false;
  1901. if (memberName != NULL)
  1902. isAutocompletingName = IsAutocompleteNode(dotToken, memberName, 0, 1);
  1903. if (isAutocompletingName)
  1904. {
  1905. //
  1906. }
  1907. else if (IsAutocompleteNode(dotToken, 0, 1))
  1908. {
  1909. mInsertStartIdx = dotToken->GetSrcEnd();
  1910. mInsertEndIdx = mInsertStartIdx;
  1911. }
  1912. else
  1913. return false;
  1914. mModule->PopulateType(interfaceType, BfPopulateType_DataAndMethods);
  1915. String filter;
  1916. if (isAutocompletingName)
  1917. filter = GetFilter(memberName);
  1918. auto activeTypeDef = mModule->GetActiveTypeDef();
  1919. BfShow checkShow = BfShow_Hide;
  1920. for (auto methodDef : interfaceType->mTypeDef->mMethods)
  1921. {
  1922. if (methodDef->mIsOverride)
  1923. continue;
  1924. if (methodDef->mShow >= checkShow)
  1925. continue;
  1926. if (methodDef->mName.IsEmpty())
  1927. continue;
  1928. if (methodDef->mExplicitInterface != NULL)
  1929. continue;
  1930. if ((!interfaceType->IsTypeMemberIncluded(methodDef->mDeclaringType, activeTypeDef, mModule)) ||
  1931. (!interfaceType->IsTypeMemberAccessible(methodDef->mDeclaringType, activeTypeDef)))
  1932. continue;
  1933. if (methodDef->mIsStatic)
  1934. continue;
  1935. bool canUseMethod;
  1936. canUseMethod = (methodDef->mMethodType == BfMethodType_Normal);
  1937. if (canUseMethod)
  1938. {
  1939. AddMethod(interfaceType, methodDef, NULL, methodDef->GetMethodDeclaration(), methodDef->mName, filter);
  1940. }
  1941. }
  1942. return false;
  1943. }
  1944. void BfAutoComplete::CheckTypeRef(BfTypeReference* typeRef, bool mayBeIdentifier, bool isInExpression, bool onlyAttribute)
  1945. {
  1946. if ((typeRef == NULL) || (typeRef->IsTemporary()) || (!IsAutocompleteNode(typeRef)))
  1947. return;
  1948. if (auto genericTypeRef = BfNodeDynCast<BfGenericInstanceTypeRef>(typeRef))
  1949. {
  1950. CheckTypeRef(genericTypeRef->mElementType, mayBeIdentifier, isInExpression, onlyAttribute);
  1951. for (auto genericArg : genericTypeRef->mGenericArguments)
  1952. CheckNode(genericArg, false, isInExpression);
  1953. return;
  1954. }
  1955. if (!onlyAttribute)
  1956. {
  1957. if (auto tupleTypeRef = BfNodeDynCast<BfTupleTypeRef>(typeRef))
  1958. {
  1959. for (auto fieldTypeRef : tupleTypeRef->mFieldTypes)
  1960. CheckTypeRef(fieldTypeRef, false, isInExpression, false);
  1961. return;
  1962. }
  1963. if (auto delegateTypeRef = BfNodeDynCast<BfDelegateTypeRef>(typeRef))
  1964. {
  1965. CheckTypeRef(delegateTypeRef->mReturnType, false, isInExpression);
  1966. for (auto param : delegateTypeRef->mParams)
  1967. {
  1968. auto attributes = param->mAttributes;
  1969. while (attributes != NULL)
  1970. {
  1971. if (attributes->mAttributeTypeRef != NULL)
  1972. {
  1973. CheckAttributeTypeRef(attributes->mAttributeTypeRef);
  1974. }
  1975. attributes = attributes->mNextAttribute;
  1976. }
  1977. CheckTypeRef(param->mTypeRef, false, isInExpression);
  1978. }
  1979. return;
  1980. }
  1981. if (auto elementedTypeRef = BfNodeDynCast<BfElementedTypeRef>(typeRef))
  1982. {
  1983. // "May be identifier" where pointer types could actually end up be multiplies, etc.
  1984. CheckTypeRef(elementedTypeRef->mElementType, true, isInExpression);
  1985. return;
  1986. }
  1987. }
  1988. if ((mayBeIdentifier) && (mResolveType != BfResolveType_GoToDefinition))
  1989. {
  1990. if (auto namedTypeRef = BfNodeDynCast<BfNamedTypeReference>(typeRef))
  1991. {
  1992. CheckIdentifier(namedTypeRef->mNameNode, isInExpression);
  1993. return;
  1994. }
  1995. else if (auto varTypeRef = BfNodeDynCast<BfVarTypeReference>(typeRef))
  1996. {
  1997. CheckIdentifier(varTypeRef->mVarToken, isInExpression);
  1998. return;
  1999. }
  2000. else if (auto varTypeRef = BfNodeDynCast<BfLetTypeReference>(typeRef))
  2001. {
  2002. CheckIdentifier(varTypeRef->mLetToken, isInExpression);
  2003. return;
  2004. }
  2005. }
  2006. if (auto qualifiedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(typeRef))
  2007. {
  2008. // Only consider the left side as an identifier if there's space after the dot. Consider this:
  2009. // mVal.
  2010. // Type a = null;
  2011. // vs
  2012. // mVal.Type a = null;
  2013. // The first one is clearly a member reference being typed out even though it looks the same
  2014. // to the parser except for the spacing
  2015. if ((qualifiedTypeRef->mRight == NULL) || (qualifiedTypeRef->mDot->GetSrcEnd() < qualifiedTypeRef->mRight->GetSrcStart()))
  2016. {
  2017. BfAutoParentNodeEntry autoParentNodeEntry(mModule, qualifiedTypeRef);
  2018. //CheckMemberReference(qualifiedTypeRef->mLeft, qualifiedTypeRef->mDot, NULL, !mayBeIdentifier, NULL, false, onlyAttribute);
  2019. CheckMemberReference(qualifiedTypeRef->mLeft, qualifiedTypeRef->mDot, qualifiedTypeRef->mRight, !mayBeIdentifier, NULL, false, onlyAttribute);
  2020. }
  2021. else if (auto rightNamedTypeRef = BfNodeDynCast<BfNamedTypeReference>(qualifiedTypeRef->mRight))
  2022. {
  2023. BfAutoParentNodeEntry autoParentNodeEntry(mModule, qualifiedTypeRef);
  2024. if (CheckMemberReference(qualifiedTypeRef->mLeft, qualifiedTypeRef->mDot, rightNamedTypeRef->mNameNode, false, NULL, false, onlyAttribute))
  2025. return;
  2026. }
  2027. }
  2028. if (auto namedTypeRef = BfNodeDynCast<BfNamedTypeReference>(typeRef))
  2029. {
  2030. AddTopLevelNamespaces(namedTypeRef->mNameNode);
  2031. AddTopLevelTypes(namedTypeRef->mNameNode, onlyAttribute);
  2032. }
  2033. }
  2034. void BfAutoComplete::CheckAttributeTypeRef(BfTypeReference* typeRef)
  2035. {
  2036. if (!IsAutocompleteNode(typeRef))
  2037. return;
  2038. CheckTypeRef(typeRef, false, false, true);
  2039. }
  2040. void BfAutoComplete::CheckInvocation(BfAstNode* invocationNode, BfTokenNode* openParen, BfTokenNode* closeParen, const BfSizedArray<ASTREF(BfTokenNode*)>& commas)
  2041. {
  2042. if (!mIsAutoComplete)
  2043. return;
  2044. bool wasCapturingMethodMatchInfo = mIsCapturingMethodMatchInfo;
  2045. mIsCapturingMethodMatchInfo = false;
  2046. int lenAdd = 0;
  2047. if (closeParen == NULL)
  2048. {
  2049. // Unterminated invocation expression - allow for space after last comma in param list
  2050. lenAdd = 1;
  2051. }
  2052. else
  2053. {
  2054. // Ignore close paren
  2055. lenAdd = -1;
  2056. }
  2057. if (!IsAutocompleteNode(invocationNode, lenAdd))
  2058. return;
  2059. if (openParen == NULL)
  2060. {
  2061. mModule->AssertErrorState();
  2062. return;
  2063. }
  2064. auto bfParser = invocationNode->GetSourceData()->ToParser();
  2065. if (bfParser == NULL)
  2066. return;
  2067. int cursorIdx = bfParser->mCursorIdx;
  2068. BfAstNode* target = invocationNode;
  2069. if (auto invocationExpr = BfNodeDynCast<BfInvocationExpression>(invocationNode))
  2070. {
  2071. target = invocationExpr->mTarget;
  2072. if (auto memberTarget = BfNodeDynCast<BfMemberReferenceExpression>(target))
  2073. {
  2074. if (memberTarget->mMemberName != NULL)
  2075. target = memberTarget->mMemberName;
  2076. }
  2077. else if (auto qualifiedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(target))
  2078. {
  2079. if (qualifiedTypeRef->mRight != NULL)
  2080. target = qualifiedTypeRef->mRight;
  2081. }
  2082. else if (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(target))
  2083. {
  2084. if (qualifiedNameNode->mRight != NULL)
  2085. target = qualifiedNameNode->mRight;
  2086. }
  2087. if (auto attributedMember = BfNodeDynCast<BfAttributedIdentifierNode>(target))
  2088. if (attributedMember->mIdentifier != NULL)
  2089. target = attributedMember->mIdentifier;
  2090. }
  2091. bool doCapture = (bfParser->mCursorIdx >= openParen->GetSrcStart());
  2092. if (mIsGetDefinition)
  2093. {
  2094. doCapture |= (target != NULL) && (bfParser->mCursorIdx >= target->GetSrcStart());
  2095. }
  2096. if (doCapture)
  2097. {
  2098. mIsCapturingMethodMatchInfo = true;
  2099. delete mMethodMatchInfo;
  2100. mMethodMatchInfo = new MethodMatchInfo();
  2101. mMethodMatchInfo->mInvocationSrcIdx = target->GetSrcStart();
  2102. mMethodMatchInfo->mCurMethodInstance = mModule->mCurMethodInstance;
  2103. mMethodMatchInfo->mCurTypeInstance = mModule->mCurTypeInstance;
  2104. mMethodMatchInfo->mSrcPositions.Clear();
  2105. mMethodMatchInfo->mSrcPositions.push_back(openParen->GetSrcStart());
  2106. for (auto comma : commas)
  2107. mMethodMatchInfo->mSrcPositions.push_back(comma->GetSrcStart());
  2108. mMethodMatchInfo->mSrcPositions.push_back(invocationNode->GetSrcEnd() + lenAdd);
  2109. }
  2110. }
  2111. void BfAutoComplete::CheckNode(BfAstNode* node, bool mayBeIdentifier, bool isInExpression)
  2112. {
  2113. if (!IsAutocompleteNode(node))
  2114. return;
  2115. if (auto identifer = BfNodeDynCast<BfIdentifierNode>(node))
  2116. CheckIdentifier(identifer);
  2117. if (auto typeRef = BfNodeDynCast<BfTypeReference>(node))
  2118. CheckTypeRef(typeRef, mayBeIdentifier, isInExpression);
  2119. if (auto memberRef = BfNodeDynCast<BfMemberReferenceExpression>(node))
  2120. {
  2121. if (memberRef->mTarget != NULL)
  2122. CheckMemberReference(memberRef->mTarget, memberRef->mDotToken, memberRef->mMemberName);
  2123. }
  2124. }
  2125. bool BfAutoComplete::GetMethodInfo(BfMethodInstance* methodInst, StringImpl* showString, StringImpl* insertString, bool isImplementing, bool isExplicitInterface)
  2126. {
  2127. SetAndRestoreValue<BfMethodInstance*> prevMethodInstance(mModule->mCurMethodInstance, methodInst);
  2128. auto methodDef = methodInst->mMethodDef;
  2129. bool isInterface = methodInst->GetOwner()->IsInterface();
  2130. BfTypeNameFlags nameFlags = (BfTypeNameFlags)(BfTypeNameFlag_ReduceName | BfTypeNameFlag_ResolveGenericParamNames);
  2131. if ((methodDef->mMethodType == BfMethodType_Normal) || (methodDef->mMethodType == BfMethodType_Ctor))
  2132. {
  2133. StringT<128> methodPrefix;
  2134. StringT<128> methodName;
  2135. StringT<256> impString;
  2136. bool isAbstract = (methodDef->mIsAbstract) || (isInterface) || (!methodDef->mIsVirtual);
  2137. if (methodDef->mMethodType == BfMethodType_Ctor)
  2138. {
  2139. impString += " : base(";
  2140. isAbstract = false;
  2141. }
  2142. else if (isAbstract)
  2143. {
  2144. if (!methodInst->mReturnType->IsVoid())
  2145. impString += "return default;";
  2146. }
  2147. else if (!isAbstract)
  2148. {
  2149. if (!methodInst->mReturnType->IsVoid())
  2150. impString = "return ";
  2151. impString += "base.";
  2152. impString += methodDef->mName;
  2153. impString += "(";
  2154. }
  2155. auto methodDeclaration = methodDef->GetMethodDeclaration();
  2156. if (methodDef->mHasAppend)
  2157. methodPrefix += "[AllowAppend]\r";
  2158. if (isInterface)
  2159. {
  2160. if (!isExplicitInterface)
  2161. methodPrefix += "public ";
  2162. }
  2163. else if (methodDeclaration->mProtectionSpecifier != NULL)
  2164. methodPrefix += methodDeclaration->mProtectionSpecifier->ToString() + " ";
  2165. if ((!isInterface) && (methodDef->mMethodType != BfMethodType_Ctor))
  2166. methodPrefix += "override ";
  2167. if (methodDef->mIsStatic)
  2168. methodPrefix += "static ";
  2169. if (methodDef->mMethodType != BfMethodType_Ctor)
  2170. {
  2171. methodPrefix += mModule->TypeToString(methodInst->mReturnType, nameFlags);
  2172. methodPrefix += " ";
  2173. }
  2174. if (isExplicitInterface)
  2175. {
  2176. methodName += mModule->TypeToString(methodInst->GetOwner(), nameFlags);
  2177. methodName += ".";
  2178. }
  2179. if (methodDef->mMethodType == BfMethodType_Ctor)
  2180. methodName += "this";
  2181. else
  2182. methodName += methodDef->mName;
  2183. if (methodInst->GetNumGenericArguments() > 0)
  2184. {
  2185. methodName += "<";
  2186. for (int genericArgIdx = 0; genericArgIdx < (int)methodInst->mMethodInfoEx->mGenericParams.size(); genericArgIdx++)
  2187. {
  2188. if (genericArgIdx > 0)
  2189. methodName += ", ";
  2190. auto genericParam = methodInst->mMethodInfoEx->mGenericParams[genericArgIdx];
  2191. methodName += genericParam->GetName();
  2192. }
  2193. methodName += ">";
  2194. }
  2195. int usedParamIdx = 0;
  2196. methodName += "(";
  2197. for (int paramIdx = 0; paramIdx < (int)methodInst->GetParamCount(); paramIdx++)
  2198. {
  2199. if (methodInst->GetParamKind(paramIdx) == BfParamKind_AppendIdx)
  2200. continue;
  2201. if (usedParamIdx > 0)
  2202. {
  2203. methodName += ", ";
  2204. if (!isAbstract)
  2205. impString += ", ";
  2206. }
  2207. if (methodInst->GetParamKind(paramIdx) == BfParamKind_Params)
  2208. methodName += "params ";
  2209. methodName += mModule->TypeToString(methodInst->GetParamType(paramIdx), nameFlags);
  2210. methodName += " ";
  2211. methodName += methodDef->mParams[paramIdx]->mName;
  2212. auto paramInitializer = methodInst->GetParamInitializer(paramIdx);
  2213. if (paramInitializer != NULL)
  2214. {
  2215. methodName += " = ";
  2216. paramInitializer->ToString(methodName);
  2217. }
  2218. if (!isAbstract)
  2219. {
  2220. if (methodInst->GetParamKind(paramIdx) == BfParamKind_Params)
  2221. impString += "params ";
  2222. auto paramType = methodInst->GetParamType(paramIdx);
  2223. if (paramType->IsRef())
  2224. {
  2225. auto refType = (BfRefType*)paramType;
  2226. if (refType->mRefKind == BfRefType::RefKind_Ref)
  2227. impString += "ref ";
  2228. if (refType->mRefKind == BfRefType::RefKind_Out)
  2229. impString += "out ";
  2230. }
  2231. impString += methodDef->mParams[paramIdx]->mName;
  2232. }
  2233. usedParamIdx++;
  2234. }
  2235. methodName += ")";
  2236. if (methodInst->GetNumGenericArguments() > 0)
  2237. {
  2238. for (int genericArgIdx = 0; genericArgIdx < (int)methodInst->mMethodInfoEx->mGenericParams.size(); genericArgIdx++)
  2239. {
  2240. auto genericParam = methodInst->mMethodInfoEx->mGenericParams[genericArgIdx];
  2241. if (genericParam->mTypeConstraint != NULL)
  2242. methodName += " where " + genericParam->GetName() + " : " + mModule->TypeToString(genericParam->mTypeConstraint, nameFlags);
  2243. for (auto ifaceConstraint : genericParam->mInterfaceConstraints)
  2244. methodName += " where " + genericParam->GetName() + " : " + mModule->TypeToString(ifaceConstraint, nameFlags);
  2245. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_Class) != 0)
  2246. methodName += " where " + genericParam->GetName() + " : class";
  2247. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_Struct) != 0)
  2248. methodName += " where " + genericParam->GetName() + " : struct";
  2249. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_StructPtr) != 0)
  2250. methodName += " where " + genericParam->GetName() + " : struct*";
  2251. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_New) != 0)
  2252. methodName += " where " + genericParam->GetName() + " : new";
  2253. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_Delete) != 0)
  2254. methodName += " where " + genericParam->GetName() + " : delete";
  2255. if ((genericParam->mGenericParamFlags & BfGenericParamFlag_Var) != 0)
  2256. methodName += " where " + genericParam->GetName() + " : var";
  2257. }
  2258. }
  2259. if (methodDef->mMethodType == BfMethodType_Ctor)
  2260. impString += ")";
  2261. else if (!isAbstract)
  2262. impString += ");";
  2263. if (showString != NULL)
  2264. *showString += methodName;
  2265. if (insertString != NULL)
  2266. {
  2267. if (showString == insertString)
  2268. *insertString += "\t";
  2269. if (methodDef->mMethodType == BfMethodType_Ctor)
  2270. *insertString += methodPrefix + methodName + impString + "\t";
  2271. else
  2272. *insertString += methodPrefix + methodName + "\t" + impString;
  2273. }
  2274. return true;
  2275. }
  2276. else if ((methodDef->mMethodType == BfMethodType_PropertyGetter) || (methodDef->mMethodType == BfMethodType_PropertySetter))
  2277. {
  2278. auto propDeclaration = methodDef->GetPropertyDeclaration();
  2279. bool hasGet = propDeclaration->GetMethod("get") != NULL;
  2280. bool hasSet = propDeclaration->GetMethod("set") != NULL;
  2281. if ((methodDef->mMethodType == BfMethodType_PropertyGetter) || (!hasGet))
  2282. {
  2283. StringT<128> propName;
  2284. StringT<256> impl;
  2285. if (propDeclaration->mNameNode != NULL)
  2286. propDeclaration->mNameNode->ToString(propName);
  2287. else
  2288. {
  2289. StringT<128> args;
  2290. propName += "this[";
  2291. for (int paramIdx = 0, count = methodInst->GetParamCount(); paramIdx < count; ++paramIdx)
  2292. {
  2293. if (paramIdx > 0)
  2294. args += ", ";
  2295. args += mModule->TypeToString(methodInst->GetParamType(paramIdx), nameFlags);
  2296. args += " ";
  2297. args += methodDef->mParams[paramIdx]->mName;
  2298. }
  2299. propName += args;
  2300. propName += "]";
  2301. }
  2302. bool isAbstract = methodDef->mIsAbstract;
  2303. if (propDeclaration->mProtectionSpecifier != NULL)
  2304. impl += propDeclaration->mProtectionSpecifier->ToString() + " ";
  2305. else if (isInterface && !isExplicitInterface)
  2306. impl += "public ";
  2307. if (!isInterface)
  2308. impl += "override ";
  2309. else if (methodDef->mIsStatic)
  2310. impl += "static ";
  2311. BfType* propType = methodInst->mReturnType;
  2312. if (methodDef->mMethodType == BfMethodType_PropertySetter)
  2313. propType = methodInst->GetParamType(0);
  2314. impl += mModule->TypeToString(propType, nameFlags);
  2315. impl += " ";
  2316. if (isExplicitInterface)
  2317. {
  2318. impl += mModule->TypeToString(methodInst->GetOwner(), nameFlags);
  2319. impl += ".";
  2320. }
  2321. impl += propName;
  2322. impl += "\t";
  2323. if (hasGet)
  2324. {
  2325. impl += "get\t";
  2326. if (!isAbstract)
  2327. {
  2328. if (isInterface)
  2329. {
  2330. impl += "return default;";
  2331. }
  2332. else
  2333. {
  2334. impl += "return base.";
  2335. impl += propName;
  2336. impl += ";";
  2337. }
  2338. }
  2339. impl += "\b";
  2340. }
  2341. if (hasSet)
  2342. {
  2343. if (hasGet)
  2344. impl += "\r\r";
  2345. impl += "set\t";
  2346. if (!isAbstract)
  2347. {
  2348. if (!isInterface)
  2349. {
  2350. impl += "base.";
  2351. impl += propName;
  2352. impl += " = value;";
  2353. }
  2354. }
  2355. impl += "\b";
  2356. }
  2357. if (showString != NULL)
  2358. *showString += propName;
  2359. if (insertString != NULL)
  2360. {
  2361. if (showString == insertString)
  2362. *insertString += "\t";
  2363. *insertString += impl;
  2364. }
  2365. return true;
  2366. }
  2367. }
  2368. return false;
  2369. }
  2370. void BfAutoComplete::AddOverrides(const StringImpl& filter, bool forceAll)
  2371. {
  2372. if (!mIsAutoComplete)
  2373. return;
  2374. auto activeTypeDef = mModule->GetActiveTypeDef();
  2375. BfShow checkShow = BfShow_Hide;
  2376. BfTypeInstance* curType = mModule->mCurTypeInstance;
  2377. while (curType != NULL)
  2378. {
  2379. for (auto methodDef : curType->mTypeDef->mMethods)
  2380. {
  2381. if (methodDef->mShow >= checkShow)
  2382. continue;
  2383. bool allowInternalOverride = false;
  2384. if (curType == mModule->mCurTypeInstance)
  2385. {
  2386. // The "normal" case, and only case for types without extensions
  2387. if ((methodDef->mDeclaringType == activeTypeDef) && (!forceAll))
  2388. continue;
  2389. if ((methodDef->mDeclaringType->IsExtension()) && (methodDef->mDeclaringType->mProject == activeTypeDef->mProject))
  2390. continue;
  2391. if (!curType->IsTypeMemberAccessible(methodDef->mDeclaringType, activeTypeDef))
  2392. continue;
  2393. if (methodDef->mIsExtern)
  2394. allowInternalOverride = true;
  2395. }
  2396. auto& methodGroup = curType->mMethodInstanceGroups[methodDef->mIdx];
  2397. if (methodGroup.mDefault == NULL)
  2398. {
  2399. continue;
  2400. }
  2401. auto methodInst = methodGroup.mDefault;
  2402. if (allowInternalOverride)
  2403. {
  2404. //
  2405. }
  2406. else if ((!methodDef->mIsVirtual) || (methodDef->mIsOverride))
  2407. continue;
  2408. if ((methodDef->mMethodType != BfMethodType_Normal) &&
  2409. (methodDef->mMethodType != BfMethodType_PropertyGetter) &&
  2410. (methodDef->mMethodType != BfMethodType_PropertySetter))
  2411. continue;
  2412. if ((methodInst->mVirtualTableIdx >= 0) && (methodInst->mVirtualTableIdx < mModule->mCurTypeInstance->mVirtualMethodTable.size()) && (!forceAll))
  2413. {
  2414. auto& vEntry = mModule->mCurTypeInstance->mVirtualMethodTable[methodInst->mVirtualTableIdx];
  2415. if (vEntry.mImplementingMethod.mTypeInstance == mModule->mCurTypeInstance)
  2416. continue;
  2417. }
  2418. StringT<512> insertString;
  2419. GetMethodInfo(methodInst, &insertString, &insertString, true, false);
  2420. if (insertString.IsEmpty())
  2421. continue;
  2422. AddEntry(AutoCompleteEntry("override", insertString), filter);
  2423. }
  2424. if (curType->IsStruct())
  2425. curType = mModule->mContext->mBfObjectType;
  2426. else
  2427. curType = curType->mBaseType;
  2428. }
  2429. }
  2430. void BfAutoComplete::AddCtorPassthroughs()
  2431. {
  2432. if (!mIsAutoComplete)
  2433. return;
  2434. auto activeTypeDef = mModule->GetActiveTypeDef();
  2435. BfTypeInstance* curType = mModule->mCurTypeInstance;
  2436. auto baseType = curType->mBaseType;
  2437. String totalInsertString;
  2438. Array<BfMethodInstance*> declMethods;
  2439. for (auto methodDef : curType->mTypeDef->mMethods)
  2440. {
  2441. if (methodDef->mMethodType != BfMethodType_Ctor)
  2442. continue;
  2443. auto& methodGroup = curType->mMethodInstanceGroups[methodDef->mIdx];
  2444. auto methodInst = methodGroup.mDefault;
  2445. if (methodInst == NULL)
  2446. continue;
  2447. if (methodDef->mMethodType != BfMethodType_Ctor)
  2448. continue;
  2449. declMethods.Add(methodInst);
  2450. }
  2451. for (auto methodDef : baseType->mTypeDef->mMethods)
  2452. {
  2453. if (methodDef->mShow != BfShow_Show)
  2454. continue;
  2455. if (methodDef->mProtection < BfProtection_Protected)
  2456. continue;
  2457. if (methodDef->mIsStatic)
  2458. continue;
  2459. auto& methodGroup = baseType->mMethodInstanceGroups[methodDef->mIdx];
  2460. auto methodInst = methodGroup.mDefault;
  2461. if (methodInst == NULL)
  2462. continue;
  2463. if (methodDef->mMethodType != BfMethodType_Ctor)
  2464. continue;
  2465. if (methodInst->GetParamCount() == 0)
  2466. continue;
  2467. bool hasDecl = false;
  2468. for (auto declMethod : declMethods)
  2469. {
  2470. if (mModule->CompareMethodSignatures(methodInst, declMethod))
  2471. {
  2472. hasDecl = true;
  2473. break;
  2474. }
  2475. }
  2476. if (hasDecl)
  2477. continue;
  2478. StringT<512> insertString;
  2479. GetMethodInfo(methodInst, &insertString, &insertString, true, false);
  2480. if (insertString.IsEmpty())
  2481. continue;
  2482. AddEntry(AutoCompleteEntry("this", insertString), "");
  2483. int tabPos = (int)insertString.IndexOf('\t');
  2484. if (tabPos >= 0)
  2485. {
  2486. if (!totalInsertString.IsEmpty())
  2487. totalInsertString += "\r\r";
  2488. totalInsertString += insertString.Substring(tabPos + 1);
  2489. }
  2490. }
  2491. if ((!totalInsertString.IsEmpty()) && (mEntriesSet.GetCount() >= 2))
  2492. {
  2493. totalInsertString.Replace("\t", "\t\b");
  2494. totalInsertString.Insert(0, "this - all\t");
  2495. auto entry = AddEntry(AutoCompleteEntry("this", totalInsertString), "");
  2496. }
  2497. }
  2498. void BfAutoComplete::UpdateReplaceData()
  2499. {
  2500. }
  2501. void BfAutoComplete::CheckMethod(BfMethodDeclaration* methodDeclaration, bool isLocalMethod)
  2502. {
  2503. if (/*(propertyDeclaration->mDefinitionBlock == NULL) &&*/ (methodDeclaration->mVirtualSpecifier != NULL) &&
  2504. (methodDeclaration->mVirtualSpecifier->GetToken() == BfToken_Override))
  2505. {
  2506. auto bfParser = methodDeclaration->mVirtualSpecifier->GetSourceData()->ToParser();
  2507. if (bfParser == NULL)
  2508. return;
  2509. int cursorIdx = bfParser->mCursorIdx;
  2510. bool isInTypeRef = IsAutocompleteNode(methodDeclaration->mReturnType);
  2511. bool isInNameNode = IsAutocompleteNode(methodDeclaration->mNameNode);
  2512. if (((IsAutocompleteNode(methodDeclaration, 1)) && (cursorIdx == methodDeclaration->mVirtualSpecifier->GetSrcEnd())) ||
  2513. (isInTypeRef) || (isInNameNode))
  2514. {
  2515. if (mIsAutoComplete)
  2516. {
  2517. mInsertStartIdx = methodDeclaration->GetSrcStart();
  2518. mInsertEndIdx = methodDeclaration->GetSrcEnd();
  2519. if (methodDeclaration->mBody != NULL)
  2520. {
  2521. if (methodDeclaration->mBody->mTriviaStart != -1)
  2522. mInsertEndIdx = methodDeclaration->mBody->mTriviaStart;
  2523. else
  2524. mInsertEndIdx = methodDeclaration->mBody->GetSrcStart();
  2525. }
  2526. }
  2527. String filter;
  2528. if ((isInNameNode || isInTypeRef))
  2529. {
  2530. if (methodDeclaration->mNameNode != NULL)
  2531. filter = methodDeclaration->mNameNode->ToString();
  2532. else if (methodDeclaration->mReturnType != NULL)
  2533. filter = methodDeclaration->mReturnType->ToString();
  2534. }
  2535. else if (methodDeclaration->mBody != NULL)
  2536. {
  2537. // We're just inside 'override' - we may be inserting a new method
  2538. mInsertEndIdx = methodDeclaration->mVirtualSpecifier->GetSrcEnd();
  2539. }
  2540. AddOverrides(filter);
  2541. }
  2542. }
  2543. if (auto ctorDeclaration = BfNodeDynCast<BfConstructorDeclaration>(methodDeclaration))
  2544. {
  2545. if ((ctorDeclaration->mThisToken != NULL) && (ctorDeclaration->mOpenParen == NULL))
  2546. {
  2547. auto bfParser = ctorDeclaration->mThisToken->GetSourceData()->ToParser();
  2548. if (bfParser == NULL)
  2549. return;
  2550. int cursorIdx = bfParser->mCursorIdx;
  2551. if ((IsAutocompleteNode(methodDeclaration, 1)) && (cursorIdx == ctorDeclaration->mThisToken->GetSrcEnd()))
  2552. {
  2553. if (mIsAutoComplete)
  2554. {
  2555. mInsertStartIdx = ctorDeclaration->GetSrcStart();
  2556. mInsertEndIdx = ctorDeclaration->mThisToken->GetSrcEnd();
  2557. }
  2558. AddCtorPassthroughs();
  2559. }
  2560. }
  2561. }
  2562. if (methodDeclaration->mReturnType != NULL)
  2563. CheckTypeRef(methodDeclaration->mReturnType, true, isLocalMethod);
  2564. }
  2565. void BfAutoComplete::CheckProperty(BfPropertyDeclaration* propertyDeclaration)
  2566. {
  2567. if (IsAutocompleteNode(propertyDeclaration->mNameNode))
  2568. {
  2569. mInsertStartIdx = propertyDeclaration->mNameNode->GetSrcStart();
  2570. mInsertEndIdx = propertyDeclaration->mNameNode->GetSrcEnd();
  2571. }
  2572. if (propertyDeclaration->mExplicitInterface != NULL)
  2573. {
  2574. BfTypeInstance* typeInst = NULL;
  2575. auto type = mModule->ResolveTypeRef(propertyDeclaration->mExplicitInterface, BfPopulateType_Identity);
  2576. if (type != NULL)
  2577. typeInst = type->ToTypeInstance();
  2578. if (typeInst != NULL)
  2579. CheckExplicitInterface(typeInst, propertyDeclaration->mExplicitInterfaceDotToken, propertyDeclaration->mNameNode);
  2580. }
  2581. if ((propertyDeclaration->mVirtualSpecifier != NULL) &&
  2582. (propertyDeclaration->mVirtualSpecifier->GetToken() == BfToken_Override))
  2583. {
  2584. if (!mIsAutoComplete)
  2585. return;
  2586. auto bfParser = propertyDeclaration->mVirtualSpecifier->GetSourceData()->ToParser();
  2587. if (bfParser == NULL)
  2588. return;
  2589. int cursorIdx = bfParser->mCursorIdx;
  2590. bool isInTypeRef = IsAutocompleteNode(propertyDeclaration->mTypeRef);
  2591. bool isInNameNode = IsAutocompleteNode(propertyDeclaration->mNameNode);
  2592. if (((IsAutocompleteNode(propertyDeclaration, 1)) && (cursorIdx == propertyDeclaration->mVirtualSpecifier->GetSrcEnd())) ||
  2593. (isInTypeRef) || (isInNameNode))
  2594. {
  2595. mInsertStartIdx = propertyDeclaration->mVirtualSpecifier->GetSrcStart();
  2596. String filter;
  2597. if ((isInNameNode || isInTypeRef))
  2598. {
  2599. BfAstNode* defNode = NULL;
  2600. if (isInNameNode)
  2601. defNode = propertyDeclaration->mNameNode;
  2602. else if (isInTypeRef)
  2603. defNode = propertyDeclaration->mTypeRef;
  2604. filter = defNode->ToString();
  2605. mInsertEndIdx = defNode->GetSrcEnd();
  2606. }
  2607. else if (propertyDeclaration->mTypeRef != NULL)
  2608. {
  2609. // We're just inside 'override' - we may be inserting a new method
  2610. mInsertEndIdx = propertyDeclaration->mVirtualSpecifier->GetSrcEnd();
  2611. }
  2612. else
  2613. {
  2614. mInsertEndIdx = propertyDeclaration->mVirtualSpecifier->GetSrcEnd();
  2615. }
  2616. AddOverrides(filter);
  2617. mInsertStartIdx = propertyDeclaration->mSrcStart;
  2618. }
  2619. }
  2620. else
  2621. {
  2622. if (propertyDeclaration->mTypeRef != NULL)
  2623. CheckTypeRef(propertyDeclaration->mTypeRef, true);
  2624. }
  2625. }
  2626. void BfAutoComplete::CheckVarResolution(BfAstNode* varTypeRef, BfType* resolvedType)
  2627. {
  2628. if (IsAutocompleteNode(varTypeRef))
  2629. {
  2630. if ((resolvedType == NULL) || (resolvedType->IsVar()) || (resolvedType->IsLet()))
  2631. return;
  2632. if (mIsGetDefinition)
  2633. {
  2634. auto typeInst = resolvedType->ToTypeInstance();
  2635. if (typeInst != NULL)
  2636. {
  2637. if (typeInst->mTypeDef->mTypeDeclaration != NULL)
  2638. SetDefinitionLocation(typeInst->mTypeDef->mTypeDeclaration->mNameNode);
  2639. }
  2640. }
  2641. if (mResolveType == BfResolveType_GetResultString)
  2642. {
  2643. mResultString = ":";
  2644. mResultString += mModule->TypeToString(resolvedType, (BfTypeNameFlags)(BfTypeNameFlag_ExtendedInfo | BfTypeNameFlag_ResolveGenericParamNames));
  2645. }
  2646. }
  2647. }
  2648. class BfAutocompleteNodeChecker : public BfStructuralVisitor
  2649. {
  2650. public:
  2651. BfAutoComplete* mAutoComplete;
  2652. bool mSelected;
  2653. public:
  2654. BfAutocompleteNodeChecker(BfAutoComplete* autoComplete)
  2655. {
  2656. mAutoComplete = autoComplete;
  2657. mSelected = false;
  2658. }
  2659. virtual void Visit(BfAstNode* bfAstNode) override
  2660. {
  2661. mSelected = mAutoComplete->IsAutocompleteNode(bfAstNode);
  2662. }
  2663. virtual void Visit(BfBinaryOperatorExpression* binOpExpr) override
  2664. {
  2665. mSelected = mAutoComplete->IsAutocompleteNode(binOpExpr->mOpToken);
  2666. }
  2667. virtual void Visit(BfUnaryOperatorExpression* unaryOpExpr) override
  2668. {
  2669. VisitChild(unaryOpExpr->mExpression);
  2670. }
  2671. virtual void Visit(BfCastExpression* castExpr) override
  2672. {
  2673. VisitChild(castExpr->mExpression);
  2674. }
  2675. virtual void Visit(BfLiteralExpression* literalExpr) override
  2676. {
  2677. mSelected = mAutoComplete->IsAutocompleteNode(literalExpr);
  2678. }
  2679. virtual void Visit(BfIdentifierNode* identifierNode) override
  2680. {
  2681. mSelected = mAutoComplete->IsAutocompleteNode(identifierNode);
  2682. }
  2683. virtual void Visit(BfMemberReferenceExpression* memberRefExpr) override
  2684. {
  2685. mSelected = mAutoComplete->IsAutocompleteNode(memberRefExpr->mMemberName);
  2686. }
  2687. };
  2688. void BfAutoComplete::CheckResult(BfAstNode* node, const BfTypedValue& typedValue)
  2689. {
  2690. if (mResolveType != BfResolveType_GetResultString)
  2691. return;
  2692. if (!IsAutocompleteNode(node))
  2693. return;
  2694. if (!typedValue.mValue.IsConst())
  2695. return;
  2696. if (typedValue.mType->IsPointer())
  2697. return;
  2698. if (typedValue.mType->IsObject())
  2699. return;
  2700. BfAutocompleteNodeChecker autocompleteNodeChecker(this);
  2701. autocompleteNodeChecker.VisitChildNoRef(node);
  2702. if (!autocompleteNodeChecker.mSelected)
  2703. return;
  2704. String constStr = ConstantToString(mModule->mBfIRBuilder, typedValue);
  2705. if (!constStr.IsEmpty())
  2706. {
  2707. mResultString = ":";
  2708. mResultString += constStr;
  2709. AddResultTypeKind(typedValue.mType);
  2710. }
  2711. else
  2712. {
  2713. SetResultStringType(typedValue.mType);
  2714. }
  2715. }
  2716. void BfAutoComplete::CheckLocalDef(BfAstNode* identifierNode, BfLocalVariable* varDecl)
  2717. {
  2718. CheckLocalRef(identifierNode, varDecl);
  2719. }
  2720. void BfAutoComplete::CheckLocalRef(BfAstNode* identifierNode, BfLocalVariable* varDecl)
  2721. {
  2722. if (mReplaceLocalId != -1)
  2723. return;
  2724. if (mResolveType == BfResolveType_GoToDefinition)
  2725. {
  2726. if (IsAutocompleteNode(identifierNode))
  2727. {
  2728. if (varDecl->mNameNode != NULL)
  2729. SetDefinitionLocation(varDecl->mNameNode, true);
  2730. else if (varDecl->mIsThis)
  2731. SetDefinitionLocation(mModule->mCurTypeInstance->mTypeDef->GetRefNode(), true);
  2732. }
  2733. }
  2734. else if (mResolveType == BfResolveType_GetSymbolInfo)
  2735. {
  2736. if ((IsAutocompleteNode(identifierNode)) &&
  2737. ((!varDecl->mIsShadow) || (varDecl->mShadowedLocal != NULL)))
  2738. {
  2739. if ((mModule->mCurMethodState != NULL) && (mModule->mCurMethodState->mClosureState != NULL) &&
  2740. (!mModule->mCurMethodState->mClosureState->mCapturing))
  2741. {
  2742. // For closures, only get locals during the 'capturing' stage
  2743. return;
  2744. }
  2745. auto rootMethodInstance = mModule->mCurMethodState->GetRootMethodState()->mMethodInstance;
  2746. if (rootMethodInstance == NULL)
  2747. return;
  2748. if (varDecl->mIsThis)
  2749. return;
  2750. auto resolvePassData = mModule->mCompiler->mResolvePassData;
  2751. mDefType = mModule->mCurTypeInstance->mTypeDef;
  2752. mReplaceLocalId = varDecl->mLocalVarId;
  2753. mDefMethod = rootMethodInstance->mMethodDef;
  2754. if (mInsertStartIdx == -1)
  2755. {
  2756. mInsertStartIdx = identifierNode->GetSrcStart();
  2757. mInsertEndIdx = identifierNode->GetSrcEnd();
  2758. }
  2759. }
  2760. }
  2761. else if (mResolveType == BfResolveType_GetResultString)
  2762. {
  2763. if (IsAutocompleteNode(identifierNode))
  2764. {
  2765. String constStr;
  2766. if (varDecl->mConstValue.IsConst())
  2767. constStr = ConstantToString(mModule->mBfIRBuilder, BfTypedValue(varDecl->mConstValue, varDecl->mResolvedType));
  2768. if (!constStr.IsEmpty())
  2769. {
  2770. mResultString = constStr;
  2771. }
  2772. else
  2773. {
  2774. SetResultStringType(varDecl->mResolvedType);
  2775. }
  2776. }
  2777. }
  2778. }
  2779. void BfAutoComplete::CheckFieldRef(BfAstNode* identifierNode, BfFieldInstance* fieldInst)
  2780. {
  2781. if (mResolveType == BfResolveType_GetSymbolInfo)
  2782. {
  2783. if (mDefField != NULL)
  2784. return;
  2785. if (IsAutocompleteNode(identifierNode))
  2786. {
  2787. while (true)
  2788. {
  2789. if (auto qualifiedName = BfNodeDynCast<BfQualifiedNameNode>(identifierNode))
  2790. {
  2791. identifierNode = qualifiedName->mRight;
  2792. if (!IsAutocompleteNode(identifierNode))
  2793. return;
  2794. }
  2795. else
  2796. break;
  2797. }
  2798. //mReplaceTypeDef = fieldInst->mOwner->mTypeDef;
  2799. //mReplaceFieldDef = fieldInst->GetFieldDef();
  2800. mDefType = fieldInst->mOwner->mTypeDef;
  2801. mDefField = fieldInst->GetFieldDef();
  2802. mInsertStartIdx = identifierNode->GetSrcStart();
  2803. mInsertEndIdx = identifierNode->GetSrcEnd();
  2804. }
  2805. }
  2806. }
  2807. void BfAutoComplete::CheckLabel(BfIdentifierNode* identifierNode, BfAstNode* precedingNode, BfScopeData* scopeData)
  2808. {
  2809. String filter;
  2810. if (identifierNode != NULL)
  2811. {
  2812. if ((mModule->mCompiler->mResolvePassData != NULL) && (scopeData != NULL))
  2813. {
  2814. auto rootMethodState = mModule->mCurMethodState->GetRootMethodState();
  2815. mModule->mCompiler->mResolvePassData->HandleLocalReference(identifierNode, rootMethodState->mMethodInstance->GetOwner()->mTypeDef, rootMethodState->mMethodInstance->mMethodDef, scopeData->mScopeLocalId);
  2816. }
  2817. if (!IsAutocompleteNode(identifierNode))
  2818. return;
  2819. if (scopeData != NULL)
  2820. {
  2821. if (mResolveType == BfResolveType_GoToDefinition)
  2822. {
  2823. SetDefinitionLocation(scopeData->mLabelNode);
  2824. }
  2825. else if (mResolveType == BfResolveType_GetSymbolInfo)
  2826. {
  2827. auto rootMethodInstance = mModule->mCurMethodState->GetRootMethodState()->mMethodInstance;
  2828. if (rootMethodInstance == NULL)
  2829. return;
  2830. mDefType = mModule->mCurTypeInstance->mTypeDef;
  2831. mReplaceLocalId = scopeData->mScopeLocalId;
  2832. mDefMethod = rootMethodInstance->mMethodDef;
  2833. if (mInsertStartIdx == -1)
  2834. {
  2835. mInsertStartIdx = identifierNode->GetSrcStart();
  2836. mInsertEndIdx = identifierNode->GetSrcEnd();
  2837. }
  2838. }
  2839. if (scopeData->mLabelNode == identifierNode)
  2840. return;
  2841. }
  2842. filter = identifierNode->ToString();
  2843. mInsertStartIdx = identifierNode->GetSrcStart();
  2844. mInsertEndIdx = identifierNode->GetSrcEnd();
  2845. }
  2846. else
  2847. {
  2848. if (precedingNode == NULL)
  2849. return;
  2850. int expectSpacing = 1;
  2851. if (auto precedingToken = BfNodeDynCast<BfTokenNode>(precedingNode))
  2852. if (precedingToken->GetToken() == BfToken_Colon)
  2853. expectSpacing = 0;
  2854. if (!IsAutocompleteNode(precedingNode, expectSpacing))
  2855. return;
  2856. auto bfParser = precedingNode->GetSourceData()->ToParser();
  2857. if (bfParser->mCursorIdx != precedingNode->GetSrcEnd() + expectSpacing - 1)
  2858. return;
  2859. mInsertStartIdx = precedingNode->GetSrcEnd() + expectSpacing;
  2860. mInsertEndIdx = mInsertStartIdx;
  2861. }
  2862. auto checkScope = mModule->mCurMethodState->mCurScope;
  2863. while (checkScope != NULL)
  2864. {
  2865. if (!checkScope->mLabel.empty())
  2866. AddEntry(AutoCompleteEntry("label", checkScope->mLabel), filter);
  2867. checkScope = checkScope->mPrevScope;
  2868. }
  2869. }
  2870. void BfAutoComplete::CheckNamespace(BfAstNode* node, const BfAtomComposite& namespaceName)
  2871. {
  2872. if (mResolveType == BfResolveType_GetSymbolInfo)
  2873. {
  2874. if (IsAutocompleteNode(node))
  2875. {
  2876. int namespaceCount = namespaceName.mSize;
  2877. auto checkNode = node;
  2878. while (auto qualifiedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(checkNode))
  2879. {
  2880. if (!IsAutocompleteNode(qualifiedTypeRef->mLeft))
  2881. break;
  2882. namespaceCount--;
  2883. checkNode = qualifiedTypeRef->mLeft;
  2884. }
  2885. while (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(checkNode))
  2886. {
  2887. if (!IsAutocompleteNode(qualifiedNameNode->mLeft))
  2888. break;
  2889. namespaceCount--;
  2890. checkNode = qualifiedNameNode->mLeft;
  2891. }
  2892. while (auto qualifiedTypeRef = BfNodeDynCast<BfQualifiedTypeReference>(checkNode))
  2893. checkNode = qualifiedTypeRef->mRight;
  2894. while (auto qualifiedNameNode = BfNodeDynCast<BfQualifiedNameNode>(checkNode))
  2895. checkNode = qualifiedNameNode->mRight;
  2896. if (checkNode != NULL)
  2897. {
  2898. mInsertStartIdx = checkNode->GetSrcStart();
  2899. mInsertEndIdx = checkNode->GetSrcEnd();
  2900. mDefNamespace.Set(namespaceName.mParts, namespaceCount, NULL, 0);
  2901. }
  2902. }
  2903. }
  2904. }
  2905. void BfAutoComplete::AddTypeInstanceEntry(BfTypeInstance* typeInst)
  2906. {
  2907. String bestTypeName = mModule->TypeToString(typeInst, BfTypeNameFlag_ReduceName);
  2908. if (typeInst->IsValueType())
  2909. AddEntry(AutoCompleteEntry("valuetype", bestTypeName));
  2910. else
  2911. AddEntry(AutoCompleteEntry("class", bestTypeName));
  2912. mDefaultSelection = bestTypeName;
  2913. }
  2914. bool BfAutoComplete::CheckDocumentation(AutoCompleteEntry* entry, BfCommentNode* documentation)
  2915. {
  2916. if (mDocumentationEntryName.IsEmpty())
  2917. return false;
  2918. if (mDocumentationEntryName != entry->mDisplay)
  2919. return false;
  2920. if (documentation != NULL)
  2921. {
  2922. StringT<128> str;
  2923. documentation->GetDocString(str);
  2924. entry->mDocumentation = mAlloc.AllocString(str);
  2925. }
  2926. return true;
  2927. }
  2928. void BfAutoComplete::CheckEmptyStart(BfAstNode* prevNode, BfType* type)
  2929. {
  2930. // Temporarily (?) removed?
  2931. return;
  2932. if (IsAutocompleteNode(prevNode, 2))
  2933. {
  2934. if (!type->IsEnum())
  2935. return;
  2936. int wantCursorIdx = prevNode->GetSrcEnd() - 1;
  2937. String prevNodeString = prevNode->ToString();
  2938. if (prevNodeString != "(")
  2939. wantCursorIdx++;
  2940. if (prevNode->GetSourceData()->ToParser()->mCursorIdx != wantCursorIdx)
  2941. return;
  2942. AddTypeInstanceEntry(type->ToTypeInstance());
  2943. CheckIdentifier(NULL);
  2944. mInsertStartIdx = wantCursorIdx + 1;
  2945. mInsertEndIdx = mInsertStartIdx;
  2946. }
  2947. }
  2948. bool BfAutoComplete::CheckFixit(BfAstNode* node)
  2949. {
  2950. if (mIgnoreFixits)
  2951. return false;
  2952. if (mModule == NULL)
  2953. return false;
  2954. if (mCompiler->mResolvePassData->mResolveType != BfResolveType_GetFixits)
  2955. return false;
  2956. if (!IsAutocompleteLineNode(node))
  2957. return false;
  2958. if (mInsertStartIdx == -1)
  2959. {
  2960. mInsertStartIdx = node->GetSrcStart();
  2961. mInsertEndIdx = node->GetSrcStart();
  2962. }
  2963. return true;
  2964. }
  2965. int BfAutoComplete::FixitGetMemberInsertPos(BfTypeDef* typeDef)
  2966. {
  2967. BfTypeDeclaration* typeDecl = typeDef->mTypeDeclaration;
  2968. BfTokenNode* openNode = NULL;
  2969. BfTokenNode* closeNode = NULL;
  2970. if (auto blockNode = BfNodeDynCast<BfBlock>(typeDecl->mDefineNode))
  2971. {
  2972. openNode = blockNode->mOpenBrace;
  2973. closeNode = blockNode->mCloseBrace;
  2974. }
  2975. int insertPos = -1;
  2976. BfParserData* parser = typeDef->mTypeDeclaration->GetSourceData()->ToParserData();
  2977. if ((parser != NULL) && (closeNode != NULL))
  2978. {
  2979. int startPos = openNode->mSrcStart + 1;
  2980. insertPos = closeNode->mSrcStart;
  2981. while (insertPos > startPos)
  2982. {
  2983. char prevC = parser->mSrc[insertPos - 1];
  2984. if (prevC == '\n')
  2985. break;
  2986. insertPos--;
  2987. }
  2988. if (insertPos > startPos)
  2989. insertPos--;
  2990. }
  2991. return insertPos;
  2992. }
  2993. void BfAutoComplete::CheckInterfaceFixit(BfTypeInstance* typeInstance, BfAstNode* node)
  2994. {
  2995. if (!CheckFixit(node))
  2996. return;
  2997. if (typeInstance == NULL)
  2998. return;
  2999. if (typeInstance->IsInterface())
  3000. return;
  3001. for (auto& ifaceTypeInst : typeInstance->mInterfaces)
  3002. {
  3003. Array<BfMethodInstance*> missingMethods;
  3004. auto ifaceInst = ifaceTypeInst.mInterfaceType;
  3005. int startIdx = ifaceTypeInst.mStartInterfaceTableIdx;
  3006. int iMethodCount = (int)ifaceInst->mMethodInstanceGroups.size();
  3007. auto declTypeDef = ifaceTypeInst.mDeclaringType;
  3008. for (int iMethodIdx = 0; iMethodIdx < iMethodCount; iMethodIdx++)
  3009. {
  3010. auto matchedMethodRef = &typeInstance->mInterfaceMethodTable[iMethodIdx + startIdx].mMethodRef;
  3011. BfMethodInstance* matchedMethod = *matchedMethodRef;
  3012. auto ifaceMethodInst = ifaceInst->mMethodInstanceGroups[iMethodIdx].mDefault;
  3013. if (ifaceMethodInst == NULL)
  3014. continue;
  3015. auto iReturnType = ifaceMethodInst->mReturnType;
  3016. if (iReturnType->IsSelf())
  3017. iReturnType = typeInstance;
  3018. if (ifaceMethodInst->mMethodDef->mIsOverride)
  3019. continue; // Don't consider overrides here
  3020. // If we have "ProjA depends on LibBase", "ProjB depends on LibBase", then a type ClassC in LibBase implementing IFaceD,
  3021. // where IFaceD gets extended with MethodE in ProjA, an implementing MethodE is still required to exist on ClassC --
  3022. // the visibility is bidirectional. A type ClassF implementing IFaceD inside ProjB will not be required to implement
  3023. // MethodE, however
  3024. if ((!ifaceInst->IsTypeMemberAccessible(ifaceMethodInst->mMethodDef->mDeclaringType, ifaceTypeInst.mDeclaringType)) &&
  3025. (!ifaceInst->IsTypeMemberAccessible(ifaceTypeInst.mDeclaringType, ifaceMethodInst->mMethodDef->mDeclaringType)))
  3026. continue;
  3027. if (!ifaceInst->IsTypeMemberIncluded(ifaceMethodInst->mMethodDef->mDeclaringType, ifaceTypeInst.mDeclaringType))
  3028. continue;
  3029. bool hadMatch = matchedMethod != NULL;
  3030. bool hadPubFailure = false;
  3031. bool hadMutFailure = false;
  3032. if (!hadMatch)
  3033. missingMethods.Add(ifaceMethodInst);
  3034. }
  3035. if (!missingMethods.IsEmpty())
  3036. {
  3037. BfParserData* parser = declTypeDef->mTypeDeclaration->GetSourceData()->ToParserData();
  3038. if (parser != NULL)
  3039. {
  3040. int insertPos = FixitGetMemberInsertPos(declTypeDef);
  3041. bool wantsBreak = false;
  3042. String insertStr = "\f";
  3043. for (auto methodInst : missingMethods)
  3044. {
  3045. if (wantsBreak)
  3046. {
  3047. insertStr += "\r\r";
  3048. wantsBreak = false;
  3049. }
  3050. if (GetMethodInfo(methodInst, NULL, &insertStr, true, false))
  3051. {
  3052. insertStr += "\b";
  3053. wantsBreak = true;
  3054. }
  3055. }
  3056. wantsBreak = false;
  3057. String explicitInsertStr = "\f";
  3058. for (auto methodInst : missingMethods)
  3059. {
  3060. if (wantsBreak)
  3061. {
  3062. explicitInsertStr += "\r\r";
  3063. wantsBreak = false;
  3064. }
  3065. if (GetMethodInfo(methodInst, NULL, &explicitInsertStr, true, true))
  3066. {
  3067. explicitInsertStr += "\b";
  3068. wantsBreak = true;
  3069. }
  3070. }
  3071. if (insertPos != -1)
  3072. {
  3073. mCompiler->mResolvePassData->mAutoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Implement interface '%s'\tusing|%s|%s",
  3074. mModule->TypeToString(ifaceInst).c_str(), FixitGetLocation(parser, insertPos).c_str(), insertStr.c_str()).c_str()));
  3075. mCompiler->mResolvePassData->mAutoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Implement interface '%s' explicitly\tusing|%s|%s",
  3076. mModule->TypeToString(ifaceInst).c_str(), FixitGetLocation(parser, insertPos).c_str(), explicitInsertStr.c_str()).c_str()));
  3077. }
  3078. }
  3079. }
  3080. }
  3081. if ((!typeInstance->IsInterface()) && (!typeInstance->IsUnspecializedTypeVariation()) && (!typeInstance->IsBoxed()))
  3082. {
  3083. if (!typeInstance->mTypeDef->mIsAbstract)
  3084. {
  3085. Array<BfMethodInstance*> missingMethods;
  3086. for (int methodIdx = 0; methodIdx < (int)typeInstance->mVirtualMethodTable.size(); methodIdx++)
  3087. {
  3088. auto& methodRef = typeInstance->mVirtualMethodTable[methodIdx].mImplementingMethod;
  3089. if (methodRef.mMethodNum == -1)
  3090. {
  3091. BF_ASSERT(mCompiler->mOptions.mHasVDataExtender);
  3092. if (methodRef.mTypeInstance == typeInstance)
  3093. {
  3094. if (typeInstance->GetImplBaseType() != NULL)
  3095. BF_ASSERT(methodIdx == (int)typeInstance->GetImplBaseType()->mVirtualMethodTableSize);
  3096. }
  3097. continue;
  3098. }
  3099. auto methodInstance = (BfMethodInstance*)methodRef;
  3100. if ((methodInstance != NULL) && (methodInstance->mMethodDef->mIsAbstract))
  3101. {
  3102. if (methodInstance->mMethodDef->mIsAbstract)
  3103. {
  3104. if (!typeInstance->IsUnspecializedTypeVariation())
  3105. missingMethods.Add(methodInstance);
  3106. }
  3107. }
  3108. }
  3109. if (!missingMethods.IsEmpty())
  3110. {
  3111. auto declTypeDef = typeInstance->mTypeDef;
  3112. BfParserData* parser = declTypeDef->mTypeDeclaration->GetSourceData()->ToParserData();
  3113. if (parser != NULL)
  3114. {
  3115. int insertPos = FixitGetMemberInsertPos(declTypeDef);
  3116. bool wantsBreak = false;
  3117. String insertStr = "\f";
  3118. for (auto methodInst : missingMethods)
  3119. {
  3120. if (wantsBreak)
  3121. {
  3122. insertStr += "\r\r";
  3123. wantsBreak = false;
  3124. }
  3125. if (GetMethodInfo(methodInst, NULL, &insertStr, true, false))
  3126. {
  3127. insertStr += "\b";
  3128. wantsBreak = true;
  3129. }
  3130. }
  3131. if (insertPos != -1)
  3132. {
  3133. mCompiler->mResolvePassData->mAutoComplete->AddEntry(AutoCompleteEntry("fixit", StrFormat("Implement abstract methods\tmethod|%s|%s",
  3134. FixitGetLocation(parser, insertPos).c_str(), insertStr.c_str()).c_str()));
  3135. }
  3136. }
  3137. }
  3138. }
  3139. }
  3140. }
  3141. void BfAutoComplete::FixitAddMember(BfTypeInstance* typeInst, BfType* fieldType, const StringImpl& fieldName, bool isStatic, BfTypeInstance* referencedFrom)
  3142. {
  3143. if (typeInst == mModule->mContext->mBfObjectType)
  3144. return;
  3145. auto parser = typeInst->mTypeDef->GetDefinition()->mSource->ToParser();
  3146. if (parser == NULL)
  3147. return;
  3148. String fullName = typeInst->mTypeDef->mFullName.ToString();
  3149. String fieldStr;
  3150. if (typeInst == referencedFrom)
  3151. {
  3152. // Implicitly private
  3153. }
  3154. else if ((referencedFrom != NULL) && (mModule->TypeIsSubTypeOf(referencedFrom, typeInst)))
  3155. {
  3156. fieldStr += "protected ";
  3157. }
  3158. else
  3159. {
  3160. fieldStr += "public ";
  3161. }
  3162. if (isStatic)
  3163. fieldStr += "static ";
  3164. if (fieldType != NULL)
  3165. fieldStr += mModule->TypeToString(fieldType, BfTypeNameFlag_ReduceName);
  3166. else
  3167. fieldStr += "Object";
  3168. fieldStr += " " + fieldName + ";";
  3169. int fileLoc = typeInst->mTypeDef->mTypeDeclaration->GetSrcEnd();
  3170. if (auto defineBlock = BfNodeDynCast<BfBlock>(typeInst->mTypeDef->mTypeDeclaration->mDefineNode))
  3171. fileLoc = BfFixitFinder::FindLineStartAfter(defineBlock->mOpenBrace);
  3172. if (!typeInst->mTypeDef->mFields.empty())
  3173. {
  3174. auto fieldDecl = typeInst->mTypeDef->mFields.back()->mFieldDeclaration;
  3175. if (fieldDecl != NULL)
  3176. {
  3177. fileLoc = BfFixitFinder::FindLineStartAfter(fieldDecl);
  3178. }
  3179. }
  3180. const char* memberName = "field";
  3181. if (isStatic)
  3182. memberName = "static field";
  3183. AddEntry(AutoCompleteEntry("fixit", StrFormat("Create %s '%s' in '%s'\taddField|%s||%s", memberName, fieldName.c_str(), fullName.c_str(),
  3184. FixitGetLocation(parser->mParserData, fileLoc).c_str(), fieldStr.c_str()).c_str()));
  3185. }
  3186. void BfAutoComplete::FixitAddCase(BfTypeInstance* typeInst, const StringImpl& caseName, const BfTypeVector& fieldTypes)
  3187. {
  3188. if (typeInst == mModule->mContext->mBfObjectType)
  3189. return;
  3190. auto parser = typeInst->mTypeDef->GetDefinition()->mSource->ToParser();
  3191. if (parser == NULL)
  3192. return;
  3193. String fullName = typeInst->mTypeDef->mFullName.ToString();
  3194. String fieldStr;
  3195. int fileLoc = typeInst->mTypeDef->mTypeDeclaration->GetSrcEnd();
  3196. if (auto defineBlock = BfNodeDynCast<BfBlock>(typeInst->mTypeDef->mTypeDeclaration->mDefineNode))
  3197. fileLoc = BfFixitFinder::FindLineStartAfter(defineBlock->mOpenBrace);
  3198. if (!typeInst->mTypeDef->mFields.empty())
  3199. {
  3200. auto fieldDecl = typeInst->mTypeDef->mFields.back()->mFieldDeclaration;
  3201. if (fieldDecl != NULL)
  3202. {
  3203. fileLoc = BfFixitFinder::FindLineStartAfter(fieldDecl);
  3204. }
  3205. }
  3206. bool isSimpleCase = false;
  3207. if (!typeInst->mTypeDef->mFields.IsEmpty())
  3208. {
  3209. if (auto block = BfNodeDynCast<BfBlock>(typeInst->mTypeDef->mTypeDeclaration->mDefineNode))
  3210. {
  3211. bool endsInComma = false;
  3212. if (!block->mChildArr.IsEmpty())
  3213. {
  3214. auto lastNode = block->mChildArr.back();
  3215. if (auto tokenNode = BfNodeDynCast<BfTokenNode>(lastNode))
  3216. {
  3217. if (tokenNode->mToken == BfToken_Comma)
  3218. {
  3219. isSimpleCase = true;
  3220. endsInComma = true;
  3221. }
  3222. }
  3223. else if (auto enumEntryDecl = BfNodeDynCast<BfEnumEntryDeclaration>(lastNode))
  3224. {
  3225. isSimpleCase = true;
  3226. }
  3227. }
  3228. if (isSimpleCase)
  3229. {
  3230. if (endsInComma)
  3231. {
  3232. fieldStr += "|";
  3233. fieldStr += caseName;
  3234. }
  3235. else
  3236. {
  3237. auto fieldDef = typeInst->mTypeDef->mFields.back();
  3238. fileLoc = fieldDef->mFieldDeclaration->GetSrcEnd();
  3239. fieldStr += ",\r";
  3240. fieldStr += caseName;
  3241. }
  3242. }
  3243. }
  3244. }
  3245. if (!isSimpleCase)
  3246. {
  3247. fieldStr += "|case ";
  3248. fieldStr += caseName;
  3249. if (!fieldTypes.IsEmpty())
  3250. {
  3251. fieldStr += "(";
  3252. FixitGetParamString(fieldTypes, fieldStr);
  3253. fieldStr += ")";
  3254. }
  3255. fieldStr += ";";
  3256. }
  3257. AddEntry(AutoCompleteEntry("fixit", StrFormat("Create case '%s' in '%s'\taddField|%s|%s", caseName.c_str(), fullName.c_str(),
  3258. FixitGetLocation(parser->mParserData, fileLoc).c_str(), fieldStr.c_str()).c_str()));
  3259. }
  3260. void BfAutoComplete::FixitGetParamString(const BfTypeVector& paramTypes, StringImpl& outStr)
  3261. {
  3262. std::set<String> usedNames;
  3263. for (int argIdx = 0; argIdx < (int)paramTypes.size(); argIdx++)
  3264. {
  3265. if (argIdx > 0)
  3266. outStr += ", ";
  3267. BfType* paramType = paramTypes[argIdx];
  3268. String checkName = "param";
  3269. if (paramType != NULL)
  3270. {
  3271. bool isOut = false;
  3272. bool isArr = false;
  3273. BfType* checkType = paramType;
  3274. while (true)
  3275. {
  3276. if ((checkType->IsArray()) || (checkType->IsSizedArray()))
  3277. {
  3278. isArr = true;
  3279. checkType = checkType->GetUnderlyingType();
  3280. }
  3281. else if (checkType->IsRef())
  3282. {
  3283. BfRefType* refType = (BfRefType*)checkType;
  3284. if (refType->mRefKind == BfRefType::RefKind_Out)
  3285. isOut = true;
  3286. checkType = refType->GetUnderlyingType();
  3287. }
  3288. else if (checkType->IsTypeInstance())
  3289. {
  3290. BfTypeInstance* typeInst = (BfTypeInstance*)checkType;
  3291. checkName = typeInst->mTypeDef->mName->ToString();
  3292. if (checkName == "String")
  3293. checkName = "Str";
  3294. if (checkName == "Object")
  3295. checkName = "Obj";
  3296. if (isOut)
  3297. checkName = "out" + checkName;
  3298. else if (isupper(checkName[0]))
  3299. {
  3300. checkName[0] = tolower(checkName[0]);
  3301. for (int i = 1; i < (int)checkName.length(); i++)
  3302. {
  3303. if ((i + 1 < (int)checkName.length()) &&
  3304. (islower(checkName[i + 1])))
  3305. break;
  3306. checkName[i] = tolower(checkName[i]);
  3307. }
  3308. }
  3309. if (isArr)
  3310. checkName += "Arr";
  3311. break;
  3312. }
  3313. else
  3314. break;
  3315. }
  3316. outStr += mModule->TypeToString(paramType, BfTypeNameFlag_ReduceName);
  3317. }
  3318. else
  3319. {
  3320. checkName = "param";
  3321. outStr += "Object";
  3322. }
  3323. for (int i = 1; i < 10; i++)
  3324. {
  3325. String lookupName = checkName;
  3326. if (i > 1)
  3327. lookupName += StrFormat("%d", i);
  3328. if (usedNames.insert(lookupName).second)
  3329. {
  3330. outStr += " " + lookupName;
  3331. break;
  3332. }
  3333. }
  3334. }
  3335. }
  3336. String BfAutoComplete::FixitGetLocation(BfParserData* parser, int insertPos)
  3337. {
  3338. int line = 0;
  3339. int lineChar = 0;
  3340. parser->GetLineCharAtIdx(insertPos, line, lineChar);
  3341. return StrFormat("%s|%d:%d", parser->mFileName.c_str(), line, lineChar);
  3342. }
  3343. String BfAutoComplete::ConstantToString(BfIRConstHolder* constHolder, BfTypedValue typedValue)
  3344. {
  3345. SetAndRestoreValue<BfTypeInstance*> prevTypeInst(mModule->mCurTypeInstance, typedValue.mType->ToTypeInstance());
  3346. SetAndRestoreValue<BfMethodInstance*> prevMethodInst(mModule->mCurMethodInstance, NULL);
  3347. BF_ASSERT(typedValue.mValue.IsConst());
  3348. String result;
  3349. result = "(";
  3350. result += mModule->TypeToString(typedValue.mType);
  3351. result += ")";
  3352. char str[32];
  3353. int stringId = mModule->GetStringPoolIdx(typedValue.mValue, constHolder);
  3354. if (stringId != -1)
  3355. {
  3356. BfStringPoolEntry* entry;
  3357. if (mModule->mContext->mStringObjectIdMap.TryGetValue(stringId, &entry))
  3358. {
  3359. String result = "\"";
  3360. result += SlashString(entry->mString, true, true, true);
  3361. result += "\"";
  3362. return result;
  3363. }
  3364. }
  3365. auto constant = constHolder->GetConstant(typedValue.mValue);
  3366. switch (constant->mTypeCode)
  3367. {
  3368. case BfTypeCode_Boolean:
  3369. result += StrFormat(" %s", constant->mBool ? "true" : "false");
  3370. break;
  3371. case BfTypeCode_UInt8:
  3372. result += StrFormat(" %llu", constant->mUInt64);
  3373. break;
  3374. case BfTypeCode_UInt16:
  3375. result += StrFormat(" %llu", constant->mUInt64);
  3376. break;
  3377. case BfTypeCode_UInt32:
  3378. result += StrFormat(" %llu", constant->mUInt64);
  3379. break;
  3380. case BfTypeCode_UInt64:
  3381. result += StrFormat(" %llu", constant->mUInt64);
  3382. break;
  3383. case BfTypeCode_Int8:
  3384. result += StrFormat(" %lld", constant->mInt64);
  3385. break;
  3386. case BfTypeCode_Int16:
  3387. result += StrFormat(" %lld", constant->mInt64);
  3388. break;
  3389. case BfTypeCode_Int32:
  3390. result += StrFormat(" %lld", constant->mInt64);
  3391. break;
  3392. case BfTypeCode_Int64:
  3393. result += StrFormat(" %lld", constant->mInt64);
  3394. break;
  3395. case BfTypeCode_Float:
  3396. ExactMinimalFloatToStr((float)constant->mDouble, str);
  3397. result += " ";
  3398. result += str;
  3399. result += "f";
  3400. break;
  3401. case BfTypeCode_Double:
  3402. ExactMinimalDoubleToStr(constant->mDouble, str);
  3403. result += " ";
  3404. result += str;
  3405. break;
  3406. default:
  3407. return "";
  3408. }
  3409. return result;
  3410. }
  3411. void BfAutoComplete::FixitAddMethod(BfTypeInstance* typeInst, const StringImpl& methodName, BfType* returnType, const BfTypeVector& paramTypes, bool wantStatic)
  3412. {
  3413. if ((typeInst->IsEnum()) && (returnType == typeInst) && (wantStatic))
  3414. {
  3415. FixitAddCase(typeInst, methodName, paramTypes);
  3416. return;
  3417. }
  3418. if ((typeInst->mTypeDef->mSource != NULL) && (typeInst != mModule->mContext->mBfObjectType))
  3419. {
  3420. auto parser = typeInst->mTypeDef->GetDefinition()->mSource->ToParser();
  3421. if (parser != NULL)
  3422. {
  3423. String fullName = typeInst->mTypeDef->mFullName.ToString();
  3424. String methodStr;
  3425. methodStr += "\f\a";
  3426. if (typeInst == mModule->mCurTypeInstance)
  3427. {
  3428. // Implicitly private
  3429. }
  3430. else if (mModule->TypeIsSubTypeOf(mModule->mCurTypeInstance, typeInst))
  3431. {
  3432. methodStr += "protected ";
  3433. }
  3434. else
  3435. {
  3436. methodStr += "public ";
  3437. }
  3438. if (wantStatic)
  3439. methodStr += "static ";
  3440. if (returnType != NULL)
  3441. methodStr += mModule->TypeToString(returnType, BfTypeNameFlag_ReduceName);
  3442. else
  3443. methodStr += "void";
  3444. methodStr += " " + methodName + "(";
  3445. FixitGetParamString(paramTypes, methodStr);
  3446. int insertPos = FixitGetMemberInsertPos(typeInst->mTypeDef);
  3447. methodStr += ")";
  3448. methodStr += "\t";
  3449. AddEntry(AutoCompleteEntry("fixit", StrFormat("Create method '%s' in '%s'\taddMethod|%s|%s", methodName.c_str(), fullName.c_str(), FixitGetLocation(parser->mParserData, insertPos).c_str(), methodStr.c_str()).c_str()));
  3450. }
  3451. }
  3452. }
  3453. void BfAutoComplete::FixitAddNamespace(BfAstNode* refNode, const StringImpl& namespaceStr)
  3454. {
  3455. auto parserData = refNode->GetParserData();
  3456. BfUsingFinder usingFinder;
  3457. usingFinder.mFromIdx = refNode->mSrcStart;
  3458. usingFinder.VisitMembers(refNode->GetSourceData()->mRootNode);
  3459. AddEntry(AutoCompleteEntry("fixit", StrFormat("using %s;\t.using|%s|%d||using %s;", namespaceStr.c_str(), parserData->mFileName.c_str(),
  3460. usingFinder.mLastIdx, namespaceStr.c_str()).c_str()));
  3461. }
  3462. void BfAutoComplete::FixitCheckNamespace(BfTypeDef* activeTypeDef, BfAstNode* typeRef, BfTokenNode* nextDotToken)
  3463. {
  3464. if (nextDotToken == NULL)
  3465. return;
  3466. auto parserData = typeRef->GetParserData();
  3467. BfSizedAtomComposite namespaceComposite;
  3468. String namespaceString = typeRef->ToString();
  3469. bool isValid = mSystem->ParseAtomComposite(namespaceString, namespaceComposite);
  3470. bool hasNamespace = false;
  3471. if (activeTypeDef != NULL)
  3472. hasNamespace = activeTypeDef->mNamespaceSearch.Contains(namespaceComposite);
  3473. if (hasNamespace)
  3474. {
  3475. AddEntry(AutoCompleteEntry("fixit", StrFormat("Remove unneeded '%s'\taddMethod|%s-%d|", typeRef->ToString().c_str(),
  3476. FixitGetLocation(parserData, typeRef->GetSrcStart()).c_str(), nextDotToken->GetSrcEnd() - typeRef->GetSrcStart()).c_str()));
  3477. }
  3478. else
  3479. {
  3480. FixitAddNamespace(typeRef, namespaceString);
  3481. }
  3482. }
  3483. void BfAutoComplete::FixitAddConstructor(BfTypeInstance *typeInstance)
  3484. {
  3485. auto baseType = typeInstance->mBaseType;
  3486. auto parser = typeInstance->mTypeDef->GetDefinition()->mSource->ToParser();
  3487. if (parser != NULL)
  3488. {
  3489. for (auto methodDef : baseType->mTypeDef->mMethods)
  3490. {
  3491. if (methodDef->mMethodType != BfMethodType_Ctor)
  3492. continue;
  3493. if (methodDef->mIsStatic)
  3494. continue;
  3495. if (methodDef->mProtection <= BfProtection_Private)
  3496. continue;
  3497. auto methodInstance = mModule->GetRawMethodInstanceAtIdx(baseType, methodDef->mIdx);
  3498. String ctorShowName;
  3499. int insertPos = FixitGetMemberInsertPos(mModule->mCurTypeInstance->mTypeDef);
  3500. String methodStr = "\f\a";
  3501. if (methodInstance->mMethodDef->mHasAppend)
  3502. methodStr += "[AllowAppend]\r";
  3503. methodStr += "public this(";
  3504. int useParamIdx = 0;
  3505. for (int paramIdx = 0; paramIdx < methodInstance->GetParamCount(); paramIdx++)
  3506. {
  3507. if (useParamIdx > 0)
  3508. methodStr += ", ";
  3509. auto paramKind = methodInstance->GetParamKind(paramIdx);
  3510. if (paramKind == BfParamKind_AppendIdx)
  3511. continue;
  3512. switch (paramKind)
  3513. {
  3514. case BfParamKind_Params:
  3515. methodStr += "params ";
  3516. break;
  3517. default:
  3518. break;
  3519. }
  3520. methodStr += mModule->TypeToString(methodInstance->GetParamType(paramIdx), BfTypeNameFlag_ReduceName);
  3521. methodStr += " ";
  3522. methodStr += methodInstance->GetParamName(paramIdx);
  3523. useParamIdx++;
  3524. }
  3525. methodStr += ") : base(";
  3526. ctorShowName += "this(";
  3527. useParamIdx = 0;
  3528. for (int paramIdx = 0; paramIdx < methodInstance->GetParamCount(); paramIdx++)
  3529. {
  3530. if (useParamIdx > 0)
  3531. {
  3532. ctorShowName += ", ";
  3533. methodStr += ", ";
  3534. }
  3535. auto paramKind = methodInstance->GetParamKind(paramIdx);
  3536. if (paramKind == BfParamKind_AppendIdx)
  3537. continue;
  3538. switch (paramKind)
  3539. {
  3540. case BfParamKind_Params:
  3541. methodStr += "params ";
  3542. break;
  3543. default:
  3544. break;
  3545. }
  3546. auto paramType = methodInstance->GetParamType(paramIdx);
  3547. if (paramType->IsRef())
  3548. {
  3549. switch (((BfRefType*)paramType)->mRefKind)
  3550. {
  3551. case BfRefType::RefKind_Ref: methodStr += "ref "; break;
  3552. case BfRefType::RefKind_Out: methodStr += "out "; break;
  3553. case BfRefType::RefKind_Mut: methodStr += "mut "; break;
  3554. }
  3555. }
  3556. methodStr += methodInstance->GetParamName(paramIdx);
  3557. ctorShowName += methodInstance->GetParamName(paramIdx);
  3558. useParamIdx++;
  3559. }
  3560. ctorShowName += ")";
  3561. methodStr += ")\t";
  3562. AddEntry(AutoCompleteEntry("fixit", StrFormat("Create constructor '%s'\taddMethod|%s|%s", ctorShowName.c_str(),
  3563. FixitGetLocation(parser->mParserData, insertPos).c_str(), methodStr.c_str()).c_str()));
  3564. }
  3565. }
  3566. }
  3567. void BfAutoComplete::AddResultTypeKind(BfType* type)
  3568. {
  3569. if (type->IsObject())
  3570. mResultString += "\n:type\tclass";
  3571. else if (type->IsInterface())
  3572. mResultString += "\n:type\tinterface";
  3573. else if (type->IsPointer())
  3574. mResultString += "\n:type\tpointer";
  3575. else
  3576. mResultString += "\n:type\tvaluetype";
  3577. }
  3578. void BfAutoComplete::SetResultStringType(BfType* type)
  3579. {
  3580. SetAndRestoreValue<BfTypeInstance*> prevTypeInst(mModule->mCurTypeInstance, type->ToTypeInstance());
  3581. SetAndRestoreValue<BfMethodInstance*> prevMethodInst(mModule->mCurMethodInstance, NULL);
  3582. mResultString = ":";
  3583. mResultString += mModule->TypeToString(type);
  3584. AddResultTypeKind(type);
  3585. }
  3586. void BfAutoComplete::FixitAddFullyQualify(BfAstNode* refNode, const StringImpl& findName, const SizedArrayImpl<BfUsingFieldData::MemberRef>& foundList)
  3587. {
  3588. BfProtectionCheckFlags protectionCheckFlags = BfProtectionCheckFlag_None;
  3589. String fullName;
  3590. for (int entryIdx = 0; entryIdx < foundList.mSize - 1; entryIdx++)
  3591. {
  3592. auto& entry = foundList[entryIdx];
  3593. if (entryIdx > 0)
  3594. fullName += ".";
  3595. if (!mModule->CheckProtection(protectionCheckFlags, entry.mTypeInstance, entry.GetDeclaringType(mModule)->mProject, entry.GetProtection(), mModule->mCurTypeInstance))
  3596. fullName += "[Friend]";
  3597. fullName += entry.GetName(mModule);
  3598. }
  3599. BfParserData* parser = refNode->GetSourceData()->ToParserData();
  3600. if (parser != NULL)
  3601. {
  3602. AddEntry(AutoCompleteEntry("fixit", StrFormat("Fully qualify 'using' name as '%s.%s'\tqualify|%s|%d|%s.",
  3603. fullName.c_str(), findName.c_str(),
  3604. parser->mFileName.c_str(), refNode->mSrcStart,
  3605. fullName.c_str()).c_str()));
  3606. }
  3607. }