47#include "llvm/ADT/ArrayRef.h"
48#include "llvm/ADT/DenseSet.h"
49#include "llvm/ADT/SmallBitVector.h"
50#include "llvm/ADT/SmallPtrSet.h"
51#include "llvm/ADT/SmallString.h"
52#include "llvm/ADT/StringExtras.h"
53#include "llvm/ADT/StringSwitch.h"
54#include "llvm/ADT/Twine.h"
55#include "llvm/ADT/iterator_range.h"
56#include "llvm/Support/Casting.h"
57#include "llvm/Support/Path.h"
58#include "llvm/Support/raw_ostream.h"
77 typedef bool (ResultBuilder::*LookupFilter)(
const NamedDecl *)
const;
83 std::vector<Result> Results;
90 typedef std::pair<const NamedDecl *, unsigned> DeclIndexPair;
95 class ShadowMapEntry {
100 llvm::PointerUnion<const NamedDecl *, DeclIndexPairVector *> DeclOrVector;
104 unsigned SingleDeclIndex = 0;
107 ShadowMapEntry() =
default;
108 ShadowMapEntry(
const ShadowMapEntry &) =
delete;
109 ShadowMapEntry(ShadowMapEntry &&Move) { *
this = std::move(Move); }
110 ShadowMapEntry &operator=(
const ShadowMapEntry &) =
delete;
111 ShadowMapEntry &operator=(ShadowMapEntry &&Move) {
112 SingleDeclIndex =
Move.SingleDeclIndex;
113 DeclOrVector =
Move.DeclOrVector;
114 Move.DeclOrVector =
nullptr;
119 if (DeclOrVector.isNull()) {
122 SingleDeclIndex = Index;
127 DeclOrVector.dyn_cast<
const NamedDecl *>()) {
130 DeclIndexPairVector *Vec =
new DeclIndexPairVector;
131 Vec->push_back(DeclIndexPair(PrevND, SingleDeclIndex));
136 DeclOrVector.get<DeclIndexPairVector *>()->push_back(
137 DeclIndexPair(ND, Index));
141 if (DeclIndexPairVector *Vec =
142 DeclOrVector.dyn_cast<DeclIndexPairVector *>()) {
150 iterator begin()
const;
151 iterator end()
const;
157 typedef llvm::DenseMap<DeclarationName, ShadowMapEntry> ShadowMap;
174 bool AllowNestedNameSpecifiers;
185 std::list<ShadowMap> ShadowMaps;
199 bool HasObjectTypeQualifiers;
211 void AdjustResultPriorityForDecl(Result &R);
213 void MaybeAddConstructorResults(Result R);
219 LookupFilter Filter =
nullptr)
220 : SemaRef(SemaRef), Allocator(Allocator), CCTUInfo(CCTUInfo),
222 HasObjectTypeQualifiers(
false), CompletionContext(CompletionContext),
223 ObjCImplementation(nullptr) {
226 switch (CompletionContext.
getKind()) {
234 if (Method->isInstanceMethod())
236 ObjCImplementation =
Interface->getImplementation();
249 bool includeCodePatterns()
const {
255 void setFilter(LookupFilter Filter) { this->Filter =
Filter; }
257 Result *data() {
return Results.empty() ? nullptr : &Results.front(); }
258 unsigned size()
const {
return Results.size(); }
259 bool empty()
const {
return Results.empty(); }
274 ObjectTypeQualifiers = Quals;
276 HasObjectTypeQualifiers =
true;
284 void setPreferredSelector(
Selector Sel) { PreferredSelector = Sel; }
289 return CompletionContext;
293 void allowNestedNameSpecifiers(
bool Allow =
true) {
294 AllowNestedNameSpecifiers = Allow;
299 Sema &getSema()
const {
return SemaRef; }
313 bool isInterestingDecl(
const NamedDecl *ND,
314 bool &AsNestedNameSpecifier)
const;
339 bool CheckHiddenResult(Result &R,
DeclContext *CurContext,
349 void MaybeAddResult(Result R,
DeclContext *CurContext =
nullptr);
366 bool InBaseClass,
QualType BaseExprType);
369 void AddResult(Result R);
372 void EnterNewScope();
391 bool IsOrdinaryName(
const NamedDecl *ND)
const;
392 bool IsOrdinaryNonTypeName(
const NamedDecl *ND)
const;
393 bool IsIntegralConstantValue(
const NamedDecl *ND)
const;
394 bool IsOrdinaryNonValueName(
const NamedDecl *ND)
const;
395 bool IsNestedNameSpecifier(
const NamedDecl *ND)
const;
397 bool IsClassOrStruct(
const NamedDecl *ND)
const;
399 bool IsNamespace(
const NamedDecl *ND)
const;
400 bool IsNamespaceOrAlias(
const NamedDecl *ND)
const;
402 bool IsMember(
const NamedDecl *ND)
const;
403 bool IsObjCIvar(
const NamedDecl *ND)
const;
404 bool IsObjCMessageReceiver(
const NamedDecl *ND)
const;
405 bool IsObjCMessageReceiverOrLambdaCapture(
const NamedDecl *ND)
const;
406 bool IsObjCCollection(
const NamedDecl *ND)
const;
407 bool IsImpossibleToSatisfy(
const NamedDecl *ND)
const;
417 ComputeType =
nullptr;
418 Type = BSI->ReturnType;
422 ComputeType =
nullptr;
425 }
else if (
const auto *Method = dyn_cast<ObjCMethodDecl>(S.
CurContext)) {
426 ComputeType =
nullptr;
427 Type = Method->getReturnType();
435 auto *VD = llvm::dyn_cast_or_null<ValueDecl>(
D);
436 ComputeType =
nullptr;
448 ComputeType =
nullptr;
457 this->ComputeType = ComputeType;
467 if (ExpectedLoc == LParLoc)
478 if (Op == tok::plus || Op == tok::plusequal || Op == tok::minusequal)
481 if (Op == tok::minus)
494 case tok::minusequal:
496 case tok::percentequal:
498 case tok::slashequal:
504 case tok::equalequal:
505 case tok::exclaimequal:
509 case tok::greaterequal:
513 case tok::greatergreater:
514 case tok::greatergreaterequal:
516 case tok::lesslessequal:
523 case tok::caretcaret:
530 case tok::caretequal:
538 case tok::periodstar:
566 case tok::minusminus:
576 assert(
false &&
"unhandled unary op");
585 ComputeType =
nullptr;
592 if (!Enabled || !
Base)
595 if (ExpectedLoc !=
Base->getBeginLoc())
606 ComputeType =
nullptr;
615 ComputeType =
nullptr;
624 ComputeType =
nullptr;
632 ComputeType =
nullptr;
638 llvm::PointerUnion<const NamedDecl *, const DeclIndexPair *> DeclOrIterator;
639 unsigned SingleDeclIndex;
659 : DeclOrIterator(SingleDecl), SingleDeclIndex(Index) {}
662 : DeclOrIterator(Iterator), SingleDeclIndex(0) {}
665 if (DeclOrIterator.is<
const NamedDecl *>()) {
671 const DeclIndexPair *I = DeclOrIterator.get<
const DeclIndexPair *>();
687 return *DeclOrIterator.get<
const DeclIndexPair *>();
693 return X.DeclOrIterator.getOpaqueValue() ==
694 Y.DeclOrIterator.getOpaqueValue() &&
695 X.SingleDeclIndex == Y.SingleDeclIndex;
704ResultBuilder::ShadowMapEntry::begin()
const {
705 if (DeclOrVector.isNull())
709 return iterator(ND, SingleDeclIndex);
711 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->begin());
715ResultBuilder::ShadowMapEntry::end()
const {
716 if (DeclOrVector.is<
const NamedDecl *>() || DeclOrVector.isNull())
719 return iterator(DeclOrVector.get<DeclIndexPairVector *>()->end());
740 for (
const DeclContext *CommonAncestor = TargetContext;
741 CommonAncestor && !CommonAncestor->
Encloses(CurContext);
742 CommonAncestor = CommonAncestor->getLookupParent()) {
743 if (CommonAncestor->isTransparentContext() ||
744 CommonAncestor->isFunctionOrMethod())
747 TargetParents.push_back(CommonAncestor);
751 while (!TargetParents.empty()) {
754 if (
const auto *Namespace = dyn_cast<NamespaceDecl>(
Parent)) {
755 if (!Namespace->getIdentifier())
759 }
else if (
const auto *TD = dyn_cast<TagDecl>(
Parent))
790bool ResultBuilder::isInterestingDecl(
const NamedDecl *ND,
791 bool &AsNestedNameSpecifier)
const {
792 AsNestedNameSpecifier =
false;
807 if (isa<ClassTemplateSpecializationDecl>(ND) ||
808 isa<ClassTemplatePartialSpecializationDecl>(ND))
812 if (isa<UsingDecl>(ND))
818 if (Filter == &ResultBuilder::IsNestedNameSpecifier ||
819 (isa<NamespaceDecl>(ND) && Filter != &ResultBuilder::IsNamespace &&
820 Filter != &ResultBuilder::IsNamespaceOrAlias && Filter !=
nullptr))
821 AsNestedNameSpecifier =
true;
824 if (Filter && !(this->*Filter)(Named)) {
826 if (AllowNestedNameSpecifiers && SemaRef.
getLangOpts().CPlusPlus &&
827 IsNestedNameSpecifier(ND) &&
828 (Filter != &ResultBuilder::IsMember ||
829 (isa<CXXRecordDecl>(ND) &&
830 cast<CXXRecordDecl>(ND)->isInjectedClassName()))) {
831 AsNestedNameSpecifier =
true;
861 R.QualifierIsInformative =
false;
865 R.Declaration->getDeclContext());
874 switch (cast<BuiltinType>(
T)->
getKind()) {
875 case BuiltinType::Void:
878 case BuiltinType::NullPtr:
881 case BuiltinType::Overload:
882 case BuiltinType::Dependent:
885 case BuiltinType::ObjCId:
886 case BuiltinType::ObjCClass:
887 case BuiltinType::ObjCSel:
900 case Type::BlockPointer:
903 case Type::LValueReference:
904 case Type::RValueReference:
907 case Type::ConstantArray:
908 case Type::IncompleteArray:
909 case Type::VariableArray:
910 case Type::DependentSizedArray:
913 case Type::DependentSizedExtVector:
915 case Type::ExtVector:
918 case Type::FunctionProto:
919 case Type::FunctionNoProto:
928 case Type::ObjCObject:
929 case Type::ObjCInterface:
930 case Type::ObjCObjectPointer:
943 if (
const auto *
Type = dyn_cast<TypeDecl>(ND))
944 return C.getTypeDeclType(
Type);
945 if (
const auto *Iface = dyn_cast<ObjCInterfaceDecl>(ND))
946 return C.getObjCInterfaceType(Iface);
951 else if (
const auto *Method = dyn_cast<ObjCMethodDecl>(ND))
952 T = Method->getSendResultType();
953 else if (
const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND))
954 T =
C.getTypeDeclType(cast<EnumDecl>(Enumerator->getDeclContext()));
955 else if (
const auto *
Property = dyn_cast<ObjCPropertyDecl>(ND))
957 else if (
const auto *
Value = dyn_cast<ValueDecl>(ND))
973 if (
Pointer->getPointeeType()->isFunctionType()) {
982 T =
Block->getPointeeType();
997unsigned ResultBuilder::getBasePriority(
const NamedDecl *ND) {
1005 if (
const auto *ImplicitParam = dyn_cast<ImplicitParamDecl>(ND))
1006 if (ImplicitParam->getIdentifier() &&
1007 ImplicitParam->getIdentifier()->isStr(
"_cmd"))
1014 if (DC->
isRecord() || isa<ObjCContainerDecl>(DC)) {
1016 if (isa<CXXDestructorDecl>(ND))
1028 if (isa<EnumConstantDecl>(ND))
1034 if ((isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) &&
1036 CompletionContext.
getKind() ==
1038 CompletionContext.
getKind() ==
1045void ResultBuilder::AdjustResultPriorityForDecl(
Result &R) {
1048 if (!PreferredSelector.
isNull())
1049 if (
const auto *Method = dyn_cast<ObjCMethodDecl>(R.Declaration))
1050 if (PreferredSelector == Method->getSelector())
1055 if (!PreferredType.
isNull()) {
1065 !(PreferredType->isEnumeralType() && TC->isEnumeralType()))
1077 return Record->lookup(ConstructorName);
1080void ResultBuilder::MaybeAddConstructorResults(
Result R) {
1081 if (!SemaRef.
getLangOpts().CPlusPlus || !R.Declaration ||
1088 Record = ClassTemplate->getTemplatedDecl();
1089 else if ((
Record = dyn_cast<CXXRecordDecl>(
D))) {
1091 if (isa<ClassTemplateSpecializationDecl>(
Record))
1103 R.Declaration = Ctor;
1105 Results.push_back(R);
1110 if (
const auto *Tmpl = dyn_cast<FunctionTemplateDecl>(ND))
1111 ND = Tmpl->getTemplatedDecl();
1112 return isa<CXXConstructorDecl>(ND);
1116 assert(!ShadowMaps.empty() &&
"Must enter into a results scope");
1118 if (R.Kind != Result::RK_Declaration) {
1120 Results.push_back(R);
1125 if (
const UsingShadowDecl *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
1127 getBasePriority(
Using->getTargetDecl()),
1131 std::move(R.FixIts));
1133 MaybeAddResult(
Result, CurContext);
1140 bool AsNestedNameSpecifier =
false;
1141 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
1148 ShadowMap &
SMap = ShadowMaps.back();
1149 ShadowMapEntry::iterator I, IEnd;
1150 ShadowMap::iterator NamePos =
SMap.find(R.Declaration->getDeclName());
1151 if (NamePos !=
SMap.end()) {
1152 I = NamePos->second.begin();
1153 IEnd = NamePos->second.end();
1156 for (; I != IEnd; ++I) {
1158 unsigned Index = I->second;
1161 Results[Index].Declaration = R.Declaration;
1171 std::list<ShadowMap>::iterator
SM, SMEnd = ShadowMaps.end();
1173 for (
SM = ShadowMaps.begin();
SM != SMEnd; ++
SM) {
1174 ShadowMapEntry::iterator I, IEnd;
1175 ShadowMap::iterator NamePos =
SM->find(R.Declaration->getDeclName());
1176 if (NamePos !=
SM->end()) {
1177 I = NamePos->second.begin();
1178 IEnd = NamePos->second.end();
1180 for (; I != IEnd; ++I) {
1182 if (I->first->hasTagIdentifierNamespace() &&
1190 I->first->getIdentifierNamespace() != IDNS)
1194 if (CheckHiddenResult(R, CurContext, I->first))
1202 if (!AllDeclsFound.insert(CanonDecl).second)
1207 if (AsNestedNameSpecifier) {
1208 R.StartsNestedNameSpecifier =
true;
1211 AdjustResultPriorityForDecl(R);
1214 if (R.QualifierIsInformative && !R.Qualifier &&
1215 !R.StartsNestedNameSpecifier) {
1216 const DeclContext *Ctx = R.Declaration->getDeclContext();
1217 if (
const NamespaceDecl *Namespace = dyn_cast<NamespaceDecl>(Ctx))
1220 else if (
const TagDecl *Tag = dyn_cast<TagDecl>(Ctx))
1222 SemaRef.
Context,
nullptr,
false,
1225 R.QualifierIsInformative =
false;
1230 SMap[R.Declaration->getDeclName()].Add(R.Declaration, Results.size());
1231 Results.push_back(R);
1233 if (!AsNestedNameSpecifier)
1234 MaybeAddConstructorResults(R);
1253 return OverloadCompare::BothViable;
1258 return OverloadCompare::BothViable;
1260 if (Candidate.
parameters()[I]->getType().getCanonicalType() !=
1261 Incumbent.
parameters()[I]->getType().getCanonicalType())
1262 return OverloadCompare::BothViable;
1265 return OverloadCompare::BothViable;
1270 if (CandidateRef != IncumbentRef) {
1277 return CandidateRef ==
RQ_RValue ? OverloadCompare::Dominates
1278 : OverloadCompare::Dominated;
1286 if (CandidateSuperset == IncumbentSuperset)
1287 return OverloadCompare::BothViable;
1288 return IncumbentSuperset ? OverloadCompare::Dominates
1289 : OverloadCompare::Dominated;
1292bool ResultBuilder::canCxxMethodBeCalled(
const CXXMethodDecl *Method,
1298 const auto *CurrentClassScope = [&]() ->
const CXXRecordDecl * {
1300 const auto *CtxMethod = llvm::dyn_cast<CXXMethodDecl>(Ctx);
1301 if (CtxMethod && !CtxMethod->getParent()->isLambda()) {
1302 return CtxMethod->getParent();
1309 bool FunctionCanBeCall =
1310 CurrentClassScope &&
1311 (CurrentClassScope == Method->
getParent() ||
1315 if (FunctionCanBeCall)
1320 BaseExprType.
isNull() ?
nullptr
1324 MaybeDerived == MaybeBase || MaybeDerived->
isDerivedFrom(MaybeBase);
1327 return FunctionCanBeCall;
1330bool ResultBuilder::canFunctionBeCalled(
const NamedDecl *ND,
1341 if (
const auto *FuncTmpl = dyn_cast<FunctionTemplateDecl>(ND)) {
1342 ND = FuncTmpl->getTemplatedDecl();
1344 const auto *Method = dyn_cast<CXXMethodDecl>(ND);
1345 if (Method && !Method->
isStatic()) {
1346 return canCxxMethodBeCalled(Method, BaseExprType);
1353 NamedDecl *Hiding,
bool InBaseClass =
false,
1355 if (R.Kind != Result::RK_Declaration) {
1357 Results.push_back(R);
1362 if (
const auto *Using = dyn_cast<UsingShadowDecl>(R.Declaration)) {
1364 getBasePriority(
Using->getTargetDecl()),
1368 std::move(R.FixIts));
1370 AddResult(
Result, CurContext, Hiding,
false,
1375 bool AsNestedNameSpecifier =
false;
1376 if (!isInterestingDecl(R.Declaration, AsNestedNameSpecifier))
1383 if (Hiding && CheckHiddenResult(R, CurContext, Hiding))
1387 if (!AllDeclsFound.insert(R.Declaration->getCanonicalDecl()).second)
1392 if (AsNestedNameSpecifier) {
1393 R.StartsNestedNameSpecifier =
true;
1395 }
else if (Filter == &ResultBuilder::IsMember && !R.Qualifier &&
1398 R.Declaration->getDeclContext()->getRedeclContext()))
1399 R.QualifierIsInformative =
true;
1402 if (R.QualifierIsInformative && !R.Qualifier &&
1403 !R.StartsNestedNameSpecifier) {
1404 const DeclContext *Ctx = R.Declaration->getDeclContext();
1405 if (
const auto *Namespace = dyn_cast<NamespaceDecl>(Ctx))
1408 else if (
const auto *Tag = dyn_cast<TagDecl>(Ctx))
1410 SemaRef.
Context,
nullptr,
false,
1413 R.QualifierIsInformative =
false;
1420 AdjustResultPriorityForDecl(R);
1422 if (HasObjectTypeQualifiers)
1423 if (
const auto *Method = dyn_cast<CXXMethodDecl>(R.Declaration))
1426 if (ObjectTypeQualifiers == MethodQuals)
1428 else if (ObjectTypeQualifiers - MethodQuals) {
1450 auto &OverloadSet = OverloadMap[std::make_pair(
1452 for (
const DeclIndexPair Entry : OverloadSet) {
1453 Result &Incumbent = Results[Entry.second];
1455 *cast<CXXMethodDecl>(Incumbent.Declaration),
1456 ObjectTypeQualifiers, ObjectKind)) {
1457 case OverloadCompare::Dominates:
1461 Incumbent = std::move(R);
1463 case OverloadCompare::Dominated:
1466 case OverloadCompare::BothViable:
1470 OverloadSet.Add(Method, Results.size());
1473 R.FunctionCanBeCall = canFunctionBeCalled(R.getDeclaration(), BaseExprType);
1476 Results.push_back(R);
1478 if (!AsNestedNameSpecifier)
1479 MaybeAddConstructorResults(R);
1482void ResultBuilder::AddResult(
Result R) {
1483 assert(R.Kind != Result::RK_Declaration &&
1484 "Declaration results need more context");
1485 Results.push_back(R);
1489void ResultBuilder::EnterNewScope() { ShadowMaps.emplace_back(); }
1492void ResultBuilder::ExitScope() {
1493 ShadowMaps.pop_back();
1498bool ResultBuilder::IsOrdinaryName(
const NamedDecl *ND)
const {
1507 if (isa<ObjCIvarDecl>(ND))
1516bool ResultBuilder::IsOrdinaryNonTypeName(
const NamedDecl *ND)
const {
1518 if (isa<TypeDecl>(ND))
1523 if (
const auto *ID = dyn_cast<ObjCInterfaceDecl>(ND)) {
1524 if (!
ID->getDefinition())
1532 if (isa<ObjCIvarDecl>(ND))
1539bool ResultBuilder::IsIntegralConstantValue(
const NamedDecl *ND)
const {
1540 if (!IsOrdinaryNonTypeName(ND))
1544 if (VD->getType()->isIntegralOrEnumerationType())
1552bool ResultBuilder::IsOrdinaryNonValueName(
const NamedDecl *ND)
const {
1560 !isa<FunctionTemplateDecl>(ND) && !isa<ObjCPropertyDecl>(ND);
1565bool ResultBuilder::IsNestedNameSpecifier(
const NamedDecl *ND)
const {
1567 if (
const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1568 ND = ClassTemplate->getTemplatedDecl();
1574bool ResultBuilder::IsEnum(
const NamedDecl *ND)
const {
1575 return isa<EnumDecl>(ND);
1579bool ResultBuilder::IsClassOrStruct(
const NamedDecl *ND)
const {
1581 if (
const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1582 ND = ClassTemplate->getTemplatedDecl();
1585 if (
const auto *RD = dyn_cast<RecordDecl>(ND))
1594bool ResultBuilder::IsUnion(
const NamedDecl *ND)
const {
1596 if (
const auto *ClassTemplate = dyn_cast<ClassTemplateDecl>(ND))
1597 ND = ClassTemplate->getTemplatedDecl();
1599 if (
const auto *RD = dyn_cast<RecordDecl>(ND))
1606bool ResultBuilder::IsNamespace(
const NamedDecl *ND)
const {
1607 return isa<NamespaceDecl>(ND);
1612bool ResultBuilder::IsNamespaceOrAlias(
const NamedDecl *ND)
const {
1617bool ResultBuilder::IsType(
const NamedDecl *ND)
const {
1619 return isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND);
1625bool ResultBuilder::IsMember(
const NamedDecl *ND)
const {
1627 return isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND) ||
1628 isa<ObjCPropertyDecl>(ND);
1632 T =
C.getCanonicalType(
T);
1634 case Type::ObjCObject:
1635 case Type::ObjCInterface:
1636 case Type::ObjCObjectPointer:
1640 switch (cast<BuiltinType>(
T)->
getKind()) {
1641 case BuiltinType::ObjCId:
1642 case BuiltinType::ObjCClass:
1643 case BuiltinType::ObjCSel:
1655 if (!
C.getLangOpts().CPlusPlus)
1664bool ResultBuilder::IsObjCMessageReceiver(
const NamedDecl *ND)
const {
1673bool ResultBuilder::IsObjCMessageReceiverOrLambdaCapture(
1675 if (IsObjCMessageReceiver(ND))
1678 const auto *Var = dyn_cast<VarDecl>(ND);
1682 return Var->hasLocalStorage() && !Var->hasAttr<BlocksAttr>();
1685bool ResultBuilder::IsObjCCollection(
const NamedDecl *ND)
const {
1686 if ((SemaRef.
getLangOpts().CPlusPlus && !IsOrdinaryName(ND)) ||
1687 (!SemaRef.
getLangOpts().CPlusPlus && !IsOrdinaryNonTypeName(ND)))
1700bool ResultBuilder::IsImpossibleToSatisfy(
const NamedDecl *ND)
const {
1706bool ResultBuilder::IsObjCIvar(
const NamedDecl *ND)
const {
1707 return isa<ObjCIvarDecl>(ND);
1715 ResultBuilder &Results;
1721 std::vector<FixItHint> FixIts;
1724 CodeCompletionDeclConsumer(
1725 ResultBuilder &Results,
DeclContext *InitialLookupCtx,
1727 std::vector<FixItHint> FixIts = std::vector<FixItHint>())
1728 : Results(Results), InitialLookupCtx(InitialLookupCtx),
1729 FixIts(
std::move(FixIts)) {
1730 NamingClass = llvm::dyn_cast<CXXRecordDecl>(InitialLookupCtx);
1733 auto ThisType = Results.getSema().getCurrentThisType();
1734 if (!ThisType.isNull()) {
1735 assert(ThisType->isPointerType());
1741 this->BaseType = BaseType;
1745 bool InBaseClass)
override {
1748 Results.AddResult(Result, InitialLookupCtx, Hiding, InBaseClass, BaseType);
1752 Results.addVisitedContext(Ctx);
1761 auto *NamingClass = this->NamingClass;
1762 QualType BaseType = this->BaseType;
1763 if (
auto *Cls = llvm::dyn_cast_or_null<CXXRecordDecl>(Ctx)) {
1778 NamingClass =
nullptr;
1781 return Results.getSema().IsSimplyAccessible(ND, NamingClass, BaseType);
1788 ResultBuilder &Results) {
1815 Results.getCodeCompletionTUInfo());
1816 if (LangOpts.CPlusPlus) {
1824 Builder.AddTypedTextChunk(
"typename");
1826 Builder.AddPlaceholderChunk(
"name");
1827 Results.AddResult(
Result(Builder.TakeString()));
1829 if (LangOpts.CPlusPlus11) {
1834 Builder.AddTypedTextChunk(
"decltype");
1836 Builder.AddPlaceholderChunk(
"expression");
1838 Results.AddResult(
Result(Builder.TakeString()));
1844 if (LangOpts.GNUKeywords) {
1850 Builder.AddTypedTextChunk(
"typeof");
1852 Builder.AddPlaceholderChunk(
"expression");
1853 Results.AddResult(
Result(Builder.TakeString()));
1855 Builder.AddTypedTextChunk(
"typeof");
1857 Builder.AddPlaceholderChunk(
"type");
1859 Results.AddResult(
Result(Builder.TakeString()));
1870 const LangOptions &LangOpts, ResultBuilder &Results) {
1875 Results.AddResult(
Result(
"extern"));
1876 Results.AddResult(
Result(
"static"));
1878 if (LangOpts.CPlusPlus11) {
1883 Builder.AddTypedTextChunk(
"alignas");
1885 Builder.AddPlaceholderChunk(
"expression");
1887 Results.AddResult(
Result(Builder.TakeString()));
1889 Results.AddResult(
Result(
"constexpr"));
1890 Results.AddResult(
Result(
"thread_local"));
1896 const LangOptions &LangOpts, ResultBuilder &Results) {
1901 if (LangOpts.CPlusPlus) {
1902 Results.AddResult(
Result(
"explicit"));
1903 Results.AddResult(
Result(
"friend"));
1904 Results.AddResult(
Result(
"mutable"));
1905 Results.AddResult(
Result(
"virtual"));
1913 if (LangOpts.CPlusPlus || LangOpts.C99)
1914 Results.AddResult(
Result(
"inline"));
1934 ResultBuilder &Results,
bool NeedAt);
1936 ResultBuilder &Results,
bool NeedAt);
1938 ResultBuilder &Results,
bool NeedAt);
1943 Results.getCodeCompletionTUInfo());
1944 Builder.AddTypedTextChunk(
"typedef");
1946 Builder.AddPlaceholderChunk(
"type");
1948 Builder.AddPlaceholderChunk(
"name");
1955 ResultBuilder &Results) {
1956 Builder.AddTypedTextChunk(
"using");
1958 Builder.AddPlaceholderChunk(
"name");
1960 Builder.AddPlaceholderChunk(
"type");
1983 return LangOpts.CPlusPlus;
1990 return LangOpts.CPlusPlus || LangOpts.ObjC || LangOpts.C99;
1993 llvm_unreachable(
"Invalid ParserCompletionContext!");
2020 if (!
T.getLocalQualifiers()) {
2023 return BT->getNameAsCString(Policy);
2026 if (
const TagType *TagT = dyn_cast<TagType>(
T))
2027 if (
TagDecl *Tag = TagT->getDecl())
2028 if (!Tag->hasNameForLinkage()) {
2029 switch (Tag->getTagKind()) {
2031 return "struct <anonymous>";
2033 return "__interface <anonymous>";
2035 return "class <anonymous>";
2037 return "union <anonymous>";
2039 return "enum <anonymous>";
2046 T.getAsStringInternal(
Result, Policy);
2047 return Allocator.CopyString(
Result);
2059 Builder.AddResultTypeChunk(
2061 Builder.AddTypedTextChunk(
"this");
2066 ResultBuilder &Results,
2068 if (!LangOpts.CPlusPlus11)
2071 Builder.AddTypedTextChunk(
"static_assert");
2073 Builder.AddPlaceholderChunk(
"expression");
2075 Builder.AddPlaceholderChunk(
"message");
2084 Sema &S = Results.getSema();
2085 const auto *CR = llvm::dyn_cast<CXXRecordDecl>(S.
CurContext);
2091 llvm::StringMap<std::vector<FunctionDecl *>> Overrides;
2092 for (
auto *Method : CR->methods()) {
2095 Overrides[Method->
getName()].push_back(Method);
2098 for (
const auto &
Base : CR->bases()) {
2099 const auto *BR =
Base.getType().getTypePtr()->getAsCXXRecordDecl();
2102 for (
auto *Method : BR->methods()) {
2105 const auto it = Overrides.find(Method->
getName());
2106 bool IsOverriden =
false;
2107 if (it != Overrides.end()) {
2108 for (
auto *MD : it->second) {
2126 false, CCContext, Policy);
2136 Scope *S,
Sema &SemaRef, ResultBuilder &Results) {
2144 if (Results.includeCodePatterns()) {
2146 Builder.AddTypedTextChunk(
"namespace");
2148 Builder.AddPlaceholderChunk(
"identifier");
2152 Builder.AddPlaceholderChunk(
"declarations");
2155 Results.AddResult(
Result(Builder.TakeString()));
2159 Builder.AddTypedTextChunk(
"namespace");
2161 Builder.AddPlaceholderChunk(
"name");
2163 Builder.AddPlaceholderChunk(
"namespace");
2165 Results.AddResult(
Result(Builder.TakeString()));
2168 Builder.AddTypedTextChunk(
"using namespace");
2170 Builder.AddPlaceholderChunk(
"identifier");
2172 Results.AddResult(
Result(Builder.TakeString()));
2175 Builder.AddTypedTextChunk(
"asm");
2177 Builder.AddPlaceholderChunk(
"string-literal");
2179 Results.AddResult(
Result(Builder.TakeString()));
2181 if (Results.includeCodePatterns()) {
2183 Builder.AddTypedTextChunk(
"template");
2185 Builder.AddPlaceholderChunk(
"declaration");
2186 Results.AddResult(
Result(Builder.TakeString()));
2201 Builder.AddTypedTextChunk(
"using");
2203 Builder.AddPlaceholderChunk(
"qualifier");
2204 Builder.AddTextChunk(
"::");
2205 Builder.AddPlaceholderChunk(
"name");
2207 Results.AddResult(
Result(Builder.TakeString()));
2214 Builder.AddTypedTextChunk(
"using typename");
2216 Builder.AddPlaceholderChunk(
"qualifier");
2217 Builder.AddTextChunk(
"::");
2218 Builder.AddPlaceholderChunk(
"name");
2220 Results.AddResult(
Result(Builder.TakeString()));
2228 bool IsNotInheritanceScope = !S->isClassInheritanceScope();
2230 Builder.AddTypedTextChunk(
"public");
2231 if (IsNotInheritanceScope && Results.includeCodePatterns())
2233 Results.AddResult(
Result(Builder.TakeString()));
2236 Builder.AddTypedTextChunk(
"protected");
2237 if (IsNotInheritanceScope && Results.includeCodePatterns())
2239 Results.AddResult(
Result(Builder.TakeString()));
2242 Builder.AddTypedTextChunk(
"private");
2243 if (IsNotInheritanceScope && Results.includeCodePatterns())
2245 Results.AddResult(
Result(Builder.TakeString()));
2258 if (SemaRef.
getLangOpts().CPlusPlus && Results.includeCodePatterns()) {
2260 Builder.AddTypedTextChunk(
"template");
2262 Builder.AddPlaceholderChunk(
"parameters");
2264 Results.AddResult(
Result(Builder.TakeString()));
2297 if (SemaRef.
getLangOpts().CPlusPlus && Results.includeCodePatterns() &&
2299 Builder.AddTypedTextChunk(
"try");
2303 Builder.AddPlaceholderChunk(
"statements");
2307 Builder.AddTextChunk(
"catch");
2310 Builder.AddPlaceholderChunk(
"declaration");
2315 Builder.AddPlaceholderChunk(
"statements");
2318 Results.AddResult(
Result(Builder.TakeString()));
2323 if (Results.includeCodePatterns()) {
2325 Builder.AddTypedTextChunk(
"if");
2329 Builder.AddPlaceholderChunk(
"condition");
2331 Builder.AddPlaceholderChunk(
"expression");
2336 Builder.AddPlaceholderChunk(
"statements");
2339 Results.AddResult(
Result(Builder.TakeString()));
2342 Builder.AddTypedTextChunk(
"switch");
2346 Builder.AddPlaceholderChunk(
"condition");
2348 Builder.AddPlaceholderChunk(
"expression");
2353 Builder.AddPlaceholderChunk(
"cases");
2356 Results.AddResult(
Result(Builder.TakeString()));
2363 Builder.AddTypedTextChunk(
"case");
2365 Builder.AddPlaceholderChunk(
"expression");
2367 Results.AddResult(
Result(Builder.TakeString()));
2370 Builder.AddTypedTextChunk(
"default");
2372 Results.AddResult(
Result(Builder.TakeString()));
2375 if (Results.includeCodePatterns()) {
2377 Builder.AddTypedTextChunk(
"while");
2381 Builder.AddPlaceholderChunk(
"condition");
2383 Builder.AddPlaceholderChunk(
"expression");
2388 Builder.AddPlaceholderChunk(
"statements");
2391 Results.AddResult(
Result(Builder.TakeString()));
2394 Builder.AddTypedTextChunk(
"do");
2398 Builder.AddPlaceholderChunk(
"statements");
2401 Builder.AddTextChunk(
"while");
2404 Builder.AddPlaceholderChunk(
"expression");
2406 Results.AddResult(
Result(Builder.TakeString()));
2409 Builder.AddTypedTextChunk(
"for");
2413 Builder.AddPlaceholderChunk(
"init-statement");
2415 Builder.AddPlaceholderChunk(
"init-expression");
2418 Builder.AddPlaceholderChunk(
"condition");
2421 Builder.AddPlaceholderChunk(
"inc-expression");
2426 Builder.AddPlaceholderChunk(
"statements");
2429 Results.AddResult(
Result(Builder.TakeString()));
2433 Builder.AddTypedTextChunk(
"for");
2436 Builder.AddPlaceholderChunk(
"range-declaration");
2439 Builder.AddTextChunk(
"in");
2443 Builder.AddPlaceholderChunk(
"range-expression");
2448 Builder.AddPlaceholderChunk(
"statements");
2451 Results.AddResult(
Result(Builder.TakeString()));
2455 if (S->getContinueParent()) {
2457 Builder.AddTypedTextChunk(
"continue");
2459 Results.AddResult(
Result(Builder.TakeString()));
2462 if (S->getBreakParent()) {
2464 Builder.AddTypedTextChunk(
"break");
2466 Results.AddResult(
Result(Builder.TakeString()));
2472 ReturnType =
Function->getReturnType();
2473 else if (
const auto *Method = dyn_cast<ObjCMethodDecl>(SemaRef.
CurContext))
2479 Builder.AddTypedTextChunk(
"return");
2481 Results.AddResult(
Result(Builder.TakeString()));
2483 assert(!ReturnType.
isNull());
2485 Builder.AddTypedTextChunk(
"return");
2487 Builder.AddPlaceholderChunk(
"expression");
2489 Results.AddResult(
Result(Builder.TakeString()));
2492 Builder.AddTypedTextChunk(
"return true");
2494 Results.AddResult(
Result(Builder.TakeString()));
2496 Builder.AddTypedTextChunk(
"return false");
2498 Results.AddResult(
Result(Builder.TakeString()));
2503 Builder.AddTypedTextChunk(
"return nullptr");
2505 Results.AddResult(
Result(Builder.TakeString()));
2510 Builder.AddTypedTextChunk(
"goto");
2512 Builder.AddPlaceholderChunk(
"label");
2514 Results.AddResult(
Result(Builder.TakeString()));
2517 Builder.AddTypedTextChunk(
"using namespace");
2519 Builder.AddPlaceholderChunk(
"identifier");
2521 Results.AddResult(
Result(Builder.TakeString()));
2538 Builder.AddTypedTextChunk(
"__bridge");
2540 Builder.AddPlaceholderChunk(
"type");
2542 Builder.AddPlaceholderChunk(
"expression");
2543 Results.AddResult(
Result(Builder.TakeString()));
2546 Builder.AddTypedTextChunk(
"__bridge_transfer");
2548 Builder.AddPlaceholderChunk(
"Objective-C type");
2550 Builder.AddPlaceholderChunk(
"expression");
2551 Results.AddResult(
Result(Builder.TakeString()));
2554 Builder.AddTypedTextChunk(
"__bridge_retained");
2556 Builder.AddPlaceholderChunk(
"CF type");
2558 Builder.AddPlaceholderChunk(
"expression");
2559 Results.AddResult(
Result(Builder.TakeString()));
2570 Builder.AddResultTypeChunk(
"bool");
2571 Builder.AddTypedTextChunk(
"true");
2572 Results.AddResult(
Result(Builder.TakeString()));
2575 Builder.AddResultTypeChunk(
"bool");
2576 Builder.AddTypedTextChunk(
"false");
2577 Results.AddResult(
Result(Builder.TakeString()));
2581 Builder.AddTypedTextChunk(
"dynamic_cast");
2583 Builder.AddPlaceholderChunk(
"type");
2586 Builder.AddPlaceholderChunk(
"expression");
2588 Results.AddResult(
Result(Builder.TakeString()));
2592 Builder.AddTypedTextChunk(
"static_cast");
2594 Builder.AddPlaceholderChunk(
"type");
2597 Builder.AddPlaceholderChunk(
"expression");
2599 Results.AddResult(
Result(Builder.TakeString()));
2602 Builder.AddTypedTextChunk(
"reinterpret_cast");
2604 Builder.AddPlaceholderChunk(
"type");
2607 Builder.AddPlaceholderChunk(
"expression");
2609 Results.AddResult(
Result(Builder.TakeString()));
2612 Builder.AddTypedTextChunk(
"const_cast");
2614 Builder.AddPlaceholderChunk(
"type");
2617 Builder.AddPlaceholderChunk(
"expression");
2619 Results.AddResult(
Result(Builder.TakeString()));
2623 Builder.AddResultTypeChunk(
"std::type_info");
2624 Builder.AddTypedTextChunk(
"typeid");
2626 Builder.AddPlaceholderChunk(
"expression-or-type");
2628 Results.AddResult(
Result(Builder.TakeString()));
2632 Builder.AddTypedTextChunk(
"new");
2634 Builder.AddPlaceholderChunk(
"type");
2636 Builder.AddPlaceholderChunk(
"expressions");
2638 Results.AddResult(
Result(Builder.TakeString()));
2641 Builder.AddTypedTextChunk(
"new");
2643 Builder.AddPlaceholderChunk(
"type");
2645 Builder.AddPlaceholderChunk(
"size");
2648 Builder.AddPlaceholderChunk(
"expressions");
2650 Results.AddResult(
Result(Builder.TakeString()));
2653 Builder.AddResultTypeChunk(
"void");
2654 Builder.AddTypedTextChunk(
"delete");
2656 Builder.AddPlaceholderChunk(
"expression");
2657 Results.AddResult(
Result(Builder.TakeString()));
2660 Builder.AddResultTypeChunk(
"void");
2661 Builder.AddTypedTextChunk(
"delete");
2666 Builder.AddPlaceholderChunk(
"expression");
2667 Results.AddResult(
Result(Builder.TakeString()));
2671 Builder.AddResultTypeChunk(
"void");
2672 Builder.AddTypedTextChunk(
"throw");
2674 Builder.AddPlaceholderChunk(
"expression");
2675 Results.AddResult(
Result(Builder.TakeString()));
2682 Builder.AddResultTypeChunk(
"std::nullptr_t");
2683 Builder.AddTypedTextChunk(
"nullptr");
2684 Results.AddResult(
Result(Builder.TakeString()));
2687 Builder.AddResultTypeChunk(
"size_t");
2688 Builder.AddTypedTextChunk(
"alignof");
2690 Builder.AddPlaceholderChunk(
"type");
2692 Results.AddResult(
Result(Builder.TakeString()));
2695 Builder.AddResultTypeChunk(
"bool");
2696 Builder.AddTypedTextChunk(
"noexcept");
2698 Builder.AddPlaceholderChunk(
"expression");
2700 Results.AddResult(
Result(Builder.TakeString()));
2703 Builder.AddResultTypeChunk(
"size_t");
2704 Builder.AddTypedTextChunk(
"sizeof...");
2706 Builder.AddPlaceholderChunk(
"parameter-pack");
2708 Results.AddResult(
Result(Builder.TakeString()));
2717 if (ID->getSuperClass()) {
2718 std::string SuperType;
2719 SuperType = ID->getSuperClass()->getNameAsString();
2720 if (Method->isInstanceMethod())
2723 Builder.AddResultTypeChunk(Allocator.CopyString(SuperType));
2724 Builder.AddTypedTextChunk(
"super");
2725 Results.AddResult(
Result(Builder.TakeString()));
2734 Builder.AddResultTypeChunk(
"size_t");
2736 Builder.AddTypedTextChunk(
"alignof");
2738 Builder.AddTypedTextChunk(
"_Alignof");
2740 Builder.AddPlaceholderChunk(
"type");
2742 Results.AddResult(
Result(Builder.TakeString()));
2747 Builder.AddResultTypeChunk(
"nullptr_t");
2748 Builder.AddTypedTextChunk(
"nullptr");
2749 Results.AddResult(
Result(Builder.TakeString()));
2753 Builder.AddResultTypeChunk(
"size_t");
2754 Builder.AddTypedTextChunk(
"sizeof");
2756 Builder.AddPlaceholderChunk(
"expression-or-type");
2758 Results.AddResult(
Result(Builder.TakeString()));
2771 Results.AddResult(
Result(
"operator"));
2792 else if (
const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) {
2794 T = Method->getSendResultType(BaseType);
2797 }
else if (
const auto *Enumerator = dyn_cast<EnumConstantDecl>(ND)) {
2800 }
else if (isa<UnresolvedUsingValueDecl>(ND)) {
2802 }
else if (
const auto *Ivar = dyn_cast<ObjCIvarDecl>(ND)) {
2804 T = Ivar->getUsageType(BaseType);
2806 T = Ivar->getType();
2807 }
else if (
const auto *
Value = dyn_cast<ValueDecl>(ND)) {
2809 }
else if (
const auto *
Property = dyn_cast<ObjCPropertyDecl>(ND)) {
2819 Result.AddResultTypeChunk(
2826 if (SentinelAttr *Sentinel = FunctionOrMethod->
getAttr<SentinelAttr>())
2827 if (Sentinel->getSentinel() == 0) {
2829 Result.AddTextChunk(
", nil");
2831 Result.AddTextChunk(
", NULL");
2833 Result.AddTextChunk(
", (void*)0");
2854 switch (*nullability) {
2864 Result +=
"null_unspecified ";
2868 llvm_unreachable(
"Not supported as a context-sensitive keyword!");
2885 bool SuppressBlock =
false) {
2891 if (!SuppressBlock) {
2894 TypedefTL.getTypedefNameDecl()->getTypeSourceInfo()) {
2907 TL = AttrTL.getModifiedLoc();
2926 bool SuppressBlockName =
false,
bool SuppressBlock =
false,
2931 bool SuppressName =
false,
bool SuppressBlock =
false,
2939 if (
const auto *PVD = dyn_cast<ParmVarDecl>(Param))
2940 ObjCQual = PVD->getObjCDeclQualifier();
2941 bool ObjCMethodParam = isa<ObjCMethodDecl>(Param->
getDeclContext());
2948 if (Param->
getIdentifier() && !ObjCMethodParam && !SuppressName)
2955 if (ObjCMethodParam) {
2974 if (!
Block && ObjCMethodParam &&
2975 cast<ObjCMethodDecl>(Param->
getDeclContext())->isPropertyAccessor()) {
2976 if (
const auto *PD = cast<ObjCMethodDecl>(Param->
getDeclContext())
2977 ->findPropertyDecl(
false))
2991 if (ObjCMethodParam) {
2996 if (
Result.back() !=
')')
3010 false, SuppressBlock,
3026 bool SuppressBlockName,
bool SuppressBlock,
3034 if (!ResultType->
isVoidType() || SuppressBlock)
3039 if (!BlockProto ||
Block.getNumParams() == 0) {
3046 for (
unsigned I = 0, N =
Block.getNumParams(); I != N; ++I) {
3059 if (SuppressBlock) {
3062 if (!SuppressBlockName &&
BlockDecl->getIdentifier())
3071 if (!SuppressBlockName &&
BlockDecl->getIdentifier())
3091 if (srcText.empty() || srcText ==
"=") {
3097 std::string DefValue(srcText.str());
3100 if (DefValue.at(0) !=
'=') {
3104 return " = " + DefValue;
3106 return " " + DefValue;
3115 bool InOptional =
false) {
3116 bool FirstParameter =
true;
3118 for (
unsigned P = Start, N =
Function->getNumParams();
P != N; ++
P) {
3125 Result.getCodeCompletionTUInfo());
3126 if (!FirstParameter)
3134 FirstParameter =
false;
3146 if (
Function->isVariadic() &&
P == N - 1)
3147 PlaceholderStr +=
", ...";
3150 Result.AddPlaceholderChunk(
3151 Result.getAllocator().CopyString(PlaceholderStr));
3155 if (Proto->isVariadic()) {
3156 if (Proto->getNumParams() == 0)
3157 Result.AddPlaceholderChunk(
"...");
3167 unsigned MaxParameters = 0,
unsigned Start = 0,
bool InDefaultArg =
false) {
3168 bool FirstParameter =
true;
3177 PEnd = Params->
begin() + MaxParameters;
3180 bool HasDefaultArg =
false;
3181 std::string PlaceholderStr;
3183 if (TTP->wasDeclaredWithTypename())
3184 PlaceholderStr =
"typename";
3185 else if (
const auto *TC = TTP->getTypeConstraint()) {
3186 llvm::raw_string_ostream OS(PlaceholderStr);
3187 TC->print(OS, Policy);
3189 PlaceholderStr =
"class";
3191 if (TTP->getIdentifier()) {
3192 PlaceholderStr +=
' ';
3193 PlaceholderStr += TTP->getIdentifier()->deuglifiedName();
3196 HasDefaultArg = TTP->hasDefaultArgument();
3198 dyn_cast<NonTypeTemplateParmDecl>(*
P)) {
3199 if (NTTP->getIdentifier())
3200 PlaceholderStr = std::string(NTTP->getIdentifier()->deuglifiedName());
3201 NTTP->getType().getAsStringInternal(PlaceholderStr, Policy);
3202 HasDefaultArg = NTTP->hasDefaultArgument();
3204 assert(isa<TemplateTemplateParmDecl>(*
P));
3209 PlaceholderStr =
"template<...> class";
3211 PlaceholderStr +=
' ';
3218 if (HasDefaultArg && !InDefaultArg) {
3222 Result.getCodeCompletionTUInfo());
3223 if (!FirstParameter)
3226 P - Params->
begin(),
true);
3231 InDefaultArg =
false;
3234 FirstParameter =
false;
3239 Result.AddPlaceholderChunk(
3240 Result.getAllocator().CopyString(PlaceholderStr));
3248 bool QualifierIsInformative,
3254 std::string PrintedNNS;
3256 llvm::raw_string_ostream OS(PrintedNNS);
3257 Qualifier->print(OS, Policy);
3259 if (QualifierIsInformative)
3260 Result.AddInformativeChunk(
Result.getAllocator().CopyString(PrintedNNS));
3262 Result.AddTextChunk(
Result.getAllocator().CopyString(PrintedNNS));
3269 if (!Proto || !Proto->getMethodQuals())
3275 if (Proto->getMethodQuals().hasOnlyConst()) {
3276 Result.AddInformativeChunk(
" const");
3280 if (Proto->getMethodQuals().hasOnlyVolatile()) {
3281 Result.AddInformativeChunk(
" volatile");
3285 if (Proto->getMethodQuals().hasOnlyRestrict()) {
3286 Result.AddInformativeChunk(
" restrict");
3291 std::string QualsStr;
3292 if (Proto->isConst())
3293 QualsStr +=
" const";
3294 if (Proto->isVolatile())
3295 QualsStr +=
" volatile";
3296 if (Proto->isRestrict())
3297 QualsStr +=
" restrict";
3298 Result.AddInformativeChunk(
Result.getAllocator().CopyString(QualsStr));
3309 switch (Name.getNameKind()) {
3311 const char *OperatorName =
nullptr;
3312 switch (Name.getCXXOverloadedOperator()) {
3314 case OO_Conditional:
3316 OperatorName =
"operator";
3319#define OVERLOADED_OPERATOR(Name, Spelling, Token, Unary, Binary, MemberOnly) \
3321 OperatorName = "operator" Spelling; \
3323#define OVERLOADED_OPERATOR_MULTI(Name, Spelling, Unary, Binary, MemberOnly)
3324#include "clang/Basic/OperatorKinds.def"
3327 OperatorName =
"operator new";
3330 OperatorName =
"operator delete";
3333 OperatorName =
"operator new[]";
3335 case OO_Array_Delete:
3336 OperatorName =
"operator delete[]";
3339 OperatorName =
"operator()";
3342 OperatorName =
"operator[]";
3345 Result.AddTypedTextChunk(OperatorName);
3353 Result.AddTypedTextChunk(
3366 QualType Ty = Name.getCXXNameType();
3368 Record = cast<CXXRecordDecl>(RecordTy->getDecl());
3370 Record = InjectedTy->getDecl();
3372 Result.AddTypedTextChunk(
3377 Result.AddTypedTextChunk(
3378 Result.getAllocator().CopyString(
Record->getNameAsString()));
3392 bool IncludeBriefComments) {
3394 CCTUInfo, IncludeBriefComments);
3406 return Result.TakeString();
3417 Result.AddPlaceholderChunk(
"...");
3431 Result.AddPlaceholderChunk(
Result.getAllocator().CopyString(Arg));
3436 Result.AddPlaceholderChunk(
3437 Result.getAllocator().CopyString((*A)->getName()));
3440 return Result.TakeString();
3452 bool IncludeBriefComments) {
3468 Result.addBriefComment(RC->getBriefText(Ctx));
3478 return Result.TakeString();
3482 PP, Ctx,
Result, IncludeBriefComments, CCContext, Policy);
3486 std::string &BeforeName,
3487 std::string &NameAndSignature) {
3488 bool SeenTypedChunk =
false;
3489 for (
auto &Chunk : CCS) {
3491 assert(SeenTypedChunk &&
"optional parameter before name");
3498 NameAndSignature += Chunk.Text;
3500 BeforeName += Chunk.Text;
3512 std::string BeforeName;
3513 std::string NameAndSignature;
3516 NameAndSignature +=
" override";
3518 Result.AddTextChunk(
Result.getAllocator().CopyString(BeforeName));
3520 Result.AddTypedTextChunk(
Result.getAllocator().CopyString(NameAndSignature));
3521 return Result.TakeString();
3527 const auto *VD = dyn_cast<VarDecl>(ND);
3530 const auto *
RecordDecl = VD->getType()->getAsCXXRecordDecl();
3543 if (IncludeBriefComments) {
3546 Result.addBriefComment(RC->getBriefText(Ctx));
3551 Result.AddTypedTextChunk(
3553 Result.AddTextChunk(
"::");
3554 return Result.TakeString();
3558 Result.AddAnnotation(
Result.getAllocator().CopyString(I->getAnnotation()));
3571 if (
const auto *
Function = dyn_cast<FunctionDecl>(ND)) {
3572 AddFunctionTypeAndResult(
Function);
3573 return Result.TakeString();
3576 if (
const auto *CallOperator =
3578 AddFunctionTypeAndResult(CallOperator);
3579 return Result.TakeString();
3585 dyn_cast<FunctionTemplateDecl>(ND)) {
3596 llvm::SmallBitVector Deduced(FunTmpl->getTemplateParameters()->size());
3601 unsigned LastDeducibleArgument;
3602 for (LastDeducibleArgument = Deduced.size(); LastDeducibleArgument > 0;
3603 --LastDeducibleArgument) {
3604 if (!Deduced[LastDeducibleArgument - 1]) {
3608 bool HasDefaultArg =
false;
3609 NamedDecl *Param = FunTmpl->getTemplateParameters()->getParam(
3610 LastDeducibleArgument - 1);
3612 HasDefaultArg = TTP->hasDefaultArgument();
3614 dyn_cast<NonTypeTemplateParmDecl>(Param))
3615 HasDefaultArg = NTTP->hasDefaultArgument();
3617 assert(isa<TemplateTemplateParmDecl>(Param));
3619 cast<TemplateTemplateParmDecl>(Param)->hasDefaultArgument();
3642 LastDeducibleArgument);
3651 return Result.TakeString();
3654 if (
const auto *Template = dyn_cast<TemplateDecl>(ND)) {
3657 Result.AddTypedTextChunk(
3658 Result.getAllocator().CopyString(Template->getNameAsString()));
3662 return Result.TakeString();
3665 if (
const auto *Method = dyn_cast<ObjCMethodDecl>(ND)) {
3666 Selector Sel = Method->getSelector();
3668 Result.AddTypedTextChunk(
3670 return Result.TakeString();
3676 Result.AddTypedTextChunk(
Result.getAllocator().CopyString(SelName));
3678 Result.AddInformativeChunk(
Result.getAllocator().CopyString(SelName));
3683 Result.AddTypedTextChunk(
"");
3690 P != PEnd && Idx < Sel.
getNumArgs(); (
void)++
P, ++Idx) {
3709 QualType ParamType = (*P)->getType();
3710 std::optional<ArrayRef<QualType>> ObjCSubsts;
3726 Arg += II->getName();
3733 Result.AddTextChunk(
Result.getAllocator().CopyString(Arg));
3735 Result.AddInformativeChunk(
Result.getAllocator().CopyString(Arg));
3737 Result.AddPlaceholderChunk(
Result.getAllocator().CopyString(Arg));
3743 Result.AddTextChunk(
", ...");
3745 Result.AddInformativeChunk(
", ...");
3747 Result.AddPlaceholderChunk(
", ...");
3753 return Result.TakeString();
3760 Result.AddTypedTextChunk(
3762 return Result.TakeString();
3773 const auto *M = dyn_cast<ObjCMethodDecl>(ND);
3785 const auto *M = dyn_cast_or_null<ObjCMethodDecl>(ND);
3786 if (!M || !M->isPropertyAccessor())
3809 auto FDecl =
Result.getFunction();
3812 if (ArgIndex < FDecl->getNumParams())
3820 unsigned CurrentArg) {
3821 unsigned ChunkIndex = 0;
3822 auto AddChunk = [&](llvm::StringRef Placeholder) {
3825 const char *
Copy =
Result.getAllocator().CopyString(Placeholder);
3826 if (ChunkIndex == CurrentArg)
3834 if (
auto *CRD = llvm::dyn_cast<CXXRecordDecl>(RD)) {
3835 for (
const auto &
Base : CRD->bases())
3836 AddChunk(
Base.getType().getAsString(Policy));
3838 for (
const auto &Field : RD->
fields())
3848 unsigned CurrentArg,
unsigned Start = 0,
bool InOptional =
false) {
3854 bool FirstParameter =
true;
3855 unsigned NumParams =
3858 for (
unsigned P = Start;
P != NumParams; ++
P) {
3863 Result.getCodeCompletionTUInfo());
3864 if (!FirstParameter)
3868 PrototypeLoc, Opt, CurrentArg,
P,
3875 FirstParameter =
false;
3882 std::string Placeholder;
3883 assert(P < Prototype->getNumParams());
3892 Placeholder =
Prototype->getParamType(
P).getAsString(Policy);
3895 if (
P == CurrentArg)
3896 Result.AddCurrentParameterChunk(
3897 Result.getAllocator().CopyString(Placeholder));
3899 Result.AddPlaceholderChunk(
Result.getAllocator().CopyString(Placeholder));
3904 Result.getCodeCompletionTUInfo());
3905 if (!FirstParameter)
3908 if (CurrentArg < NumParams)
3920 if (
const auto *
Type = dyn_cast<TemplateTypeParmDecl>(Param)) {
3922 }
else if (
const auto *NonType = dyn_cast<NonTypeTemplateParmDecl>(Param)) {
3923 Optional = NonType->hasDefaultArgument();
3924 }
else if (
const auto *Template = dyn_cast<TemplateTemplateParmDecl>(Param)) {
3925 Optional = Template->hasDefaultArgument();
3928 llvm::raw_string_ostream OS(
Result);
3929 Param->
print(OS, Policy);
3935 if (
const auto *CTD = dyn_cast<ClassTemplateDecl>(TD))
3936 return CTD->getTemplatedDecl()->getKindName().str();
3937 if (
const auto *VTD = dyn_cast<VarTemplateDecl>(TD))
3938 return VTD->getTemplatedDecl()->getType().getAsString(Policy);
3939 if (
const auto *FTD = dyn_cast<FunctionTemplateDecl>(TD))
3940 return FTD->getTemplatedDecl()->getReturnType().getAsString(Policy);
3941 if (isa<TypeAliasTemplateDecl>(TD))
3943 if (isa<TemplateTemplateParmDecl>(TD))
3945 if (isa<ConceptDecl>(TD))
3955 Builder.getCodeCompletionTUInfo());
3957 if (!ResultType.empty())
3958 Builder.AddResultTypeChunk(Builder.getAllocator().CopyString(ResultType));
3959 Builder.AddTextChunk(
3965 for (
unsigned I = 0; I < Params.size(); ++I) {
3967 std::string Placeholder =
3970 Current = &OptionalBuilder;
3973 Current->AddChunk(I == CurrentArg
3976 Current->getAllocator().CopyString(Placeholder));
3979 if (Current == &OptionalBuilder)
3980 Builder.AddOptionalChunk(OptionalBuilder.
TakeString());
3984 if (isa<FunctionTemplateDecl>(TD))
3985 Builder.AddInformativeChunk(
"()");
3986 return Builder.TakeString();
3993 bool Braced)
const {
4017 if (IncludeBriefComments) {
4024 llvm::raw_string_ostream OS(Name);
4026 Result.AddTextChunk(
Result.getAllocator().CopyString(Name));
4029 Result.AddResultTypeChunk(
Result.getAllocator().CopyString(
4044 return Result.TakeString();
4049 bool PreferredTypeIsPointer) {
4053 if (MacroName ==
"nil" || MacroName ==
"NULL" || MacroName ==
"Nil") {
4055 if (PreferredTypeIsPointer)
4059 else if (MacroName ==
"YES" || MacroName ==
"NO" || MacroName ==
"true" ||
4060 MacroName ==
"false")
4063 else if (MacroName ==
"bool")
4076 case Decl::EnumConstant:
4080 case Decl::Function:
4082 case Decl::ObjCCategory:
4084 case Decl::ObjCCategoryImpl:
4086 case Decl::ObjCImplementation:
4089 case Decl::ObjCInterface:
4091 case Decl::ObjCIvar:
4093 case Decl::ObjCMethod:
4094 return cast<ObjCMethodDecl>(
D)->isInstanceMethod()
4097 case Decl::CXXMethod:
4099 case Decl::CXXConstructor:
4101 case Decl::CXXDestructor:
4103 case Decl::CXXConversion:
4105 case Decl::ObjCProperty:
4107 case Decl::ObjCProtocol:
4113 case Decl::TypeAlias:
4115 case Decl::TypeAliasTemplate:
4119 case Decl::Namespace:
4121 case Decl::NamespaceAlias:
4123 case Decl::TemplateTypeParm:
4125 case Decl::NonTypeTemplateParm:
4127 case Decl::TemplateTemplateParm:
4129 case Decl::FunctionTemplate:
4131 case Decl::ClassTemplate:
4133 case Decl::AccessSpec:
4135 case Decl::ClassTemplatePartialSpecialization:
4137 case Decl::UsingDirective:
4139 case Decl::StaticAssert:
4143 case Decl::TranslationUnit:
4147 case Decl::UnresolvedUsingValue:
4148 case Decl::UnresolvedUsingTypename:
4151 case Decl::UsingEnum:
4154 case Decl::ObjCPropertyImpl:
4155 switch (cast<ObjCPropertyImplDecl>(
D)->getPropertyImplementation()) {
4162 llvm_unreachable(
"Unexpected Kind!");
4167 case Decl::ObjCTypeParam:
4173 case Decl::LinkageSpec:
4177 if (
const auto *TD = dyn_cast<TagDecl>(
D)) {
4178 switch (TD->getTagKind()) {
4196 bool LoadExternal,
bool IncludeUndefined,
4197 bool TargetTypeIsPointer =
false) {
4200 Results.EnterNewScope();
4206 if (IncludeUndefined || MD) {
4214 TargetTypeIsPointer)));
4218 Results.ExitScope();
4222 ResultBuilder &Results) {
4225 Results.EnterNewScope();
4229 if (LangOpts.C99 || LangOpts.CPlusPlus11)
4231 Results.ExitScope();
4238 unsigned NumResults) {
4301 llvm_unreachable(
"Invalid ParserCompletionContext!");
4313 ResultBuilder &Results) {
4316 while (isa<BlockDecl>(CurContext))
4319 CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(CurContext);
4326 if (!
P->getDeclName())
4332 Results.getCodeCompletionTUInfo());
4339 S.
Context, CurContext, Overridden->getDeclContext());
4342 llvm::raw_string_ostream OS(Str);
4343 NNS->
print(OS, Policy);
4344 Builder.AddTextChunk(Results.getAllocator().CopyString(Str));
4346 }
else if (!InContext->
Equals(Overridden->getDeclContext()))
4349 Builder.AddTypedTextChunk(
4350 Results.getAllocator().CopyString(Overridden->getNameAsString()));
4352 bool FirstParam =
true;
4359 Builder.AddPlaceholderChunk(
4360 Results.getAllocator().CopyString(
P->getIdentifier()->getName()));
4366 Results.Ignore(Overridden);
4373 ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(),
4374 CodeCompleter->getCodeCompletionTUInfo(),
4376 Results.EnterNewScope();
4385 for (
unsigned I = 0, N = Modules.size(); I != N; ++I) {
4386 Builder.AddTypedTextChunk(
4387 Builder.getAllocator().CopyString(Modules[I]->Name));
4388 Results.AddResult(
Result(
4393 }
else if (getLangOpts().Modules) {
4401 Builder.AddTypedTextChunk(
4402 Builder.getAllocator().CopyString(Submodule->Name));
4403 Results.AddResult(
Result(
4410 Results.ExitScope();
4412 Results.getCompletionContext(), Results.data(),
4418 ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(),
4419 CodeCompleter->getCodeCompletionTUInfo(),
4421 Results.EnterNewScope();
4426 switch (CompletionContext) {
4429 case PCC_ObjCInterface:
4430 case PCC_ObjCImplementation:
4431 case PCC_ObjCInstanceVariableList:
4433 case PCC_MemberTemplate:
4435 case PCC_LocalDeclarationSpecifiers:
4436 Results.setFilter(&ResultBuilder::IsOrdinaryNonValueName);
4440 case PCC_TopLevelOrExpression:
4441 case PCC_ParenthesizedExpression:
4442 case PCC_Expression:
4446 Results.setFilter(&ResultBuilder::IsOrdinaryName);
4448 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
4454 case PCC_RecoveryInFunction:
4462 if (!ThisType.isNull())
4463 Results.setObjectTypeQualifiers(ThisType->getPointeeType().getQualifiers(),
4466 CodeCompletionDeclConsumer Consumer(Results, SemaRef.
CurContext);
4468 CodeCompleter->includeGlobals(),
4469 CodeCompleter->loadExternal());
4472 Results.ExitScope();
4474 switch (CompletionContext) {
4475 case PCC_ParenthesizedExpression:
4476 case PCC_Expression:
4478 case PCC_TopLevelOrExpression:
4479 case PCC_RecoveryInFunction:
4480 if (S->getFnParent())
4486 case PCC_ObjCInterface:
4487 case PCC_ObjCImplementation:
4488 case PCC_ObjCInstanceVariableList:
4490 case PCC_MemberTemplate:
4494 case PCC_LocalDeclarationSpecifiers:
4498 if (CodeCompleter->includeMacros())
4502 Results.getCompletionContext(), Results.data(),
4509 bool AtArgumentExpression,
bool IsSuper,
4510 ResultBuilder &Results);
4513 bool AllowNonIdentifiers,
4514 bool AllowNestedNameSpecifiers) {
4516 ResultBuilder Results(
4517 SemaRef, CodeCompleter->getAllocator(),
4518 CodeCompleter->getCodeCompletionTUInfo(),
4519 AllowNestedNameSpecifiers
4524 Results.EnterNewScope();
4527 Results.AddResult(
Result(
"const"));
4528 Results.AddResult(
Result(
"volatile"));
4529 if (getLangOpts().
C99)
4530 Results.AddResult(
Result(
"restrict"));
4536 Results.AddResult(
"final");
4538 if (AllowNonIdentifiers) {
4539 Results.AddResult(
Result(
"operator"));
4543 if (AllowNestedNameSpecifiers) {
4544 Results.allowNestedNameSpecifiers();
4545 Results.setFilter(&ResultBuilder::IsImpossibleToSatisfy);
4546 CodeCompletionDeclConsumer Consumer(Results, SemaRef.
CurContext);
4548 Consumer, CodeCompleter->includeGlobals(),
4549 CodeCompleter->loadExternal());
4550 Results.setFilter(
nullptr);
4553 Results.ExitScope();
4559 if (AllowNonIdentifiers && !AllowNestedNameSpecifiers &&
4579 Results.getCompletionContext(), Results.data(),
4584 if (
Scope ==
"clang")
4592 if (
Scope ==
"_Clang")
4594 if (
Scope ==
"__gnu__")
4602 if (Completion == AttributeCompletion::None)
4604 ResultBuilder Results(SemaRef, CodeCompleter->getAllocator(),
4605 CodeCompleter->getCodeCompletionTUInfo(),
4618 llvm::StringRef InScopeName;
4619 bool InScopeUnderscore =
false;
4621 InScopeName = InScope->
getName();
4623 InScopeName = NoUnderscore;
4624 InScopeUnderscore =
true;
4631 llvm::DenseSet<llvm::StringRef> FoundScopes;
4633 if (A.IsTargetSpecific &&
4634 !A.existsInTarget(getASTContext().getTargetInfo()))
4636 if (!A.acceptsLangOpts(getLangOpts()))
4638 for (
const auto &S : A.Spellings) {
4639 if (S.Syntax != Syntax)
4641 llvm::StringRef Name = S.NormalizedFullName;
4642 llvm::StringRef
Scope;
4645 std::tie(
Scope, Name) = Name.split(
"::");
4647 std::swap(Name,
Scope);
4651 if (Completion == AttributeCompletion::Scope) {
4653 if (!
Scope.empty() && FoundScopes.insert(
Scope).second) {
4664 if (!InScopeName.empty()) {
4665 if (
Scope != InScopeName)
4670 auto Add = [&](llvm::StringRef
Scope, llvm::StringRef Name,
4673 Results.getCodeCompletionTUInfo());
4675 if (!
Scope.empty()) {
4684 Builder.AddTypedTextChunk(Results.getAllocator().CopyString(
Text));
4686 if (!A.ArgNames.empty()) {
4689 for (
const char *Arg : A.ArgNames) {
4693 Builder.AddPlaceholderChunk(Arg);
4698 Results.AddResult(Builder.TakeString());
4705 if (!InScopeUnderscore)
4706 Add(
Scope, Name,
false);
4711 if (!(InScope && !InScopeUnderscore) && SyntaxSupportsGuards) {
4713 if (
Scope.empty()) {
4714 Add(
Scope, Name,
true);
4719 Add(GuardedScope, Name,
true);
4729 for (
const auto &Entry : ParsedAttrInfoRegistry::entries())
4730 AddCompletions(*Entry.instantiate());
4733 Results.getCompletionContext(), Results.data(),
4739 bool IsParenthesized =
false)
4740 : PreferredType(PreferredType), IntegralConstantExpression(
false),
4741 ObjCCollection(
false), IsParenthesized(IsParenthesized) {}
4752struct CoveredEnumerators {
4760 const CoveredEnumerators &Enumerators) {
4762 if (Context.
getLangOpts().CPlusPlus && !Qualifier && Enumerators.Seen.empty()) {
4769 Results.EnterNewScope();
4770 for (
auto *
E :
Enum->enumerators()) {
4771 if (Enumerators.Seen.count(
E))
4775 Results.AddResult(R, CurContext,
nullptr,
false);
4777 Results.ExitScope();
4783 assert(!
T.isNull());
4806 if (!Results.includeCodePatterns())
4809 Results.getCodeCompletionTUInfo());
4814 if (!Parameters.empty()) {
4823 constexpr llvm::StringLiteral NamePlaceholder =
"!#!NAME_GOES_HERE!#!";
4824 std::string
Type = std::string(NamePlaceholder);
4826 llvm::StringRef Prefix, Suffix;
4827 std::tie(Prefix, Suffix) = llvm::StringRef(
Type).split(NamePlaceholder);
4828 Prefix = Prefix.rtrim();
4829 Suffix = Suffix.ltrim();
4852 ResultBuilder Results(
4853 SemaRef, CodeCompleter->getAllocator(),
4854 CodeCompleter->getCodeCompletionTUInfo(),
4856 Data.IsParenthesized
4859 Data.PreferredType));
4861 Data.IsParenthesized ? PCC_ParenthesizedExpression : PCC_Expression;
4862 if (
Data.ObjCCollection)
4863 Results.setFilter(&ResultBuilder::IsObjCCollection);
4864 else if (
Data.IntegralConstantExpression)
4865 Results.setFilter(&ResultBuilder::IsIntegralConstantValue);
4867 Results.setFilter(&ResultBuilder::IsOrdinaryName);
4869 Results.setFilter(&ResultBuilder::IsOrdinaryNonTypeName);
4871 if (!
Data.PreferredType.isNull())
4872 Results.setPreferredType(
Data.PreferredType.getNonReferenceType());
4875 for (
unsigned I = 0, N =
Data.IgnoreDecls.size(); I != N; ++I)
4876 Results.Ignore(
Data.IgnoreDecls[I]);
4878 CodeCompletionDeclConsumer Consumer(Results, SemaRef.
CurContext);
4880 CodeCompleter->includeGlobals(),
4881 CodeCompleter->loadExternal());
4883 Results.EnterNewScope();
4885 Results.ExitScope();
4887 bool PreferredTypeIsPointer =
false;
4888 if (!
Data.PreferredType.isNull()) {