2
0

BfAutoComplete.cpp 122 KB

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