BfAutoComplete.cpp 121 KB

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