BfAutoComplete.cpp 110 KB

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