BfAutoComplete.cpp 103 KB

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