27class HeuristicResolverImpl {
29 HeuristicResolverImpl(ASTContext &Ctx) : Ctx(Ctx) {}
33 std::vector<const NamedDecl *>
34 resolveMemberExpr(
const CXXDependentScopeMemberExpr *ME);
35 std::vector<const NamedDecl *>
36 resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE);
37 std::vector<const NamedDecl *> resolveTypeOfCallExpr(
const CallExpr *CE);
38 std::vector<const NamedDecl *> resolveCalleeOfCallExpr(
const CallExpr *CE);
39 std::vector<const NamedDecl *>
40 resolveUsingValueDecl(
const UnresolvedUsingValueDecl *UUVD);
41 std::vector<const NamedDecl *>
42 resolveDependentNameType(
const DependentNameType *DNT);
43 std::vector<const NamedDecl *> resolveTemplateSpecializationType(
44 const DependentTemplateSpecializationType *DTST);
45 QualType resolveNestedNameSpecifierToType(
const NestedNameSpecifier *NNS);
52 llvm::SmallSet<const DependentNameType *, 4> SeenDependentNameTypes;
63 std::vector<const NamedDecl *>
64 resolveDependentMember(QualType
T, DeclarationName Name,
65 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
69 QualType resolveExprToType(
const Expr *
E);
70 std::vector<const NamedDecl *> resolveExprToDecls(
const Expr *
E);
75 CXXRecordDecl *resolveTypeToRecordDecl(
const Type *
T);
84 std::vector<const NamedDecl *>
85 lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
86 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
89 DeclarationName Name);
94const auto NoFilter = [](
const NamedDecl *
D) {
return true; };
95const auto NonStaticFilter = [](
const NamedDecl *
D) {
96 return D->isCXXInstanceMember();
98const auto StaticFilter = [](
const NamedDecl *
D) {
99 return !
D->isCXXInstanceMember();
101const auto ValueFilter = [](
const NamedDecl *
D) {
return isa<ValueDecl>(
D); };
102const auto TypeFilter = [](
const NamedDecl *
D) {
return isa<TypeDecl>(
D); };
103const auto TemplateFilter = [](
const NamedDecl *
D) {
104 return isa<TemplateDecl>(
D);
107QualType resolveDeclsToType(
const std::vector<const NamedDecl *> &Decls,
109 if (Decls.size() != 1)
111 if (
const auto *TD = dyn_cast<TypeDecl>(Decls[0])) {
114 if (
const auto *VD = dyn_cast<ValueDecl>(Decls[0])) {
115 return VD->getType();
120TemplateName getReferencedTemplateName(
const Type *
T) {
121 if (
const auto *TST =
T->
getAs<TemplateSpecializationType>()) {
122 return TST->getTemplateName();
124 if (
const auto *DTST =
T->
getAs<DeducedTemplateSpecializationType>()) {
125 return DTST->getTemplateName();
127 return TemplateName();
133CXXRecordDecl *HeuristicResolverImpl::resolveTypeToRecordDecl(
const Type *
T) {
139 if (
const auto *DNT =
T->
getAs<DependentNameType>()) {
140 T = resolveDeclsToType(resolveDependentNameType(DNT), Ctx)
147 if (
const auto *RT =
T->
getAs<RecordType>())
148 return dyn_cast<CXXRecordDecl>(RT->getDecl());
150 if (
const auto *ICNT =
T->
getAs<InjectedClassNameType>())
151 T = ICNT->getInjectedSpecializationType().getTypePtrOrNull();
155 TemplateName TN = getReferencedTemplateName(
T);
159 const ClassTemplateDecl *TD =
160 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
164 return TD->getTemplatedDecl();
167QualType HeuristicResolverImpl::getPointeeType(QualType
T) {
178 auto ArrowOps = resolveDependentMember(
180 if (ArrowOps.empty())
189 auto *TST =
T->
getAs<TemplateSpecializationType>();
192 if (TST->template_arguments().size() == 0)
194 const TemplateArgument &FirstArg = TST->template_arguments()[0];
197 return FirstArg.getAsType();
200std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
201 const CXXDependentScopeMemberExpr *ME) {
213 if (NestedNameSpecifier *NNS = ME->getQualifier()) {
214 if (QualType QualifierType = resolveNestedNameSpecifierToType(NNS);
215 !QualifierType.isNull()) {
217 resolveDependentMember(QualifierType, ME->getMember(), NoFilter);
230 QualType BaseType = ME->getBaseType();
234 if (BaseType.isNull())
236 if (
const auto *BT = BaseType->getAs<BuiltinType>()) {
240 Expr *
Base = ME->isImplicitAccess() ? nullptr : ME->getBase();
241 if (
Base && BT->getKind() == BuiltinType::Dependent) {
242 BaseType = resolveExprToType(
Base);
245 return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
248std::vector<const NamedDecl *>
249HeuristicResolverImpl::resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE) {
250 return resolveDependentMember(QualType(RE->getQualifier()->getAsType(), 0),
251 RE->getDeclName(), StaticFilter);
254std::vector<const NamedDecl *>
255HeuristicResolverImpl::resolveTypeOfCallExpr(
const CallExpr *CE) {
256 QualType CalleeType = resolveExprToType(CE->getCallee());
257 if (CalleeType.isNull())
259 if (
const auto *FnTypePtr = CalleeType->getAs<PointerType>())
260 CalleeType = FnTypePtr->getPointeeType();
261 if (
const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
263 resolveTypeToRecordDecl(FnType->getReturnType().getTypePtr())) {
270std::vector<const NamedDecl *>
271HeuristicResolverImpl::resolveCalleeOfCallExpr(
const CallExpr *CE) {
272 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
276 return resolveExprToDecls(CE->getCallee());
279std::vector<const NamedDecl *> HeuristicResolverImpl::resolveUsingValueDecl(
280 const UnresolvedUsingValueDecl *UUVD) {
281 return resolveDependentMember(QualType(UUVD->getQualifier()->getAsType(), 0),
282 UUVD->getNameInfo().getName(), ValueFilter);
285std::vector<const NamedDecl *>
286HeuristicResolverImpl::resolveDependentNameType(
const DependentNameType *DNT) {
287 if (
auto [_, inserted] = SeenDependentNameTypes.insert(DNT); !inserted)
289 return resolveDependentMember(
290 resolveNestedNameSpecifierToType(DNT->getQualifier()),
291 DNT->getIdentifier(), TypeFilter);
294std::vector<const NamedDecl *>
295HeuristicResolverImpl::resolveTemplateSpecializationType(
296 const DependentTemplateSpecializationType *DTST) {
297 return resolveDependentMember(
298 resolveNestedNameSpecifierToType(DTST->getQualifier()),
299 DTST->getIdentifier(), TemplateFilter);
302std::vector<const NamedDecl *>
303HeuristicResolverImpl::resolveExprToDecls(
const Expr *
E) {
304 if (
const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(
E)) {
305 return resolveMemberExpr(ME);
307 if (
const auto *RE = dyn_cast<DependentScopeDeclRefExpr>(
E)) {
308 return resolveDeclRefExpr(RE);
310 if (
const auto *OE = dyn_cast<OverloadExpr>(
E)) {
311 return {OE->decls_begin(), OE->decls_end()};
313 if (
const auto *CE = dyn_cast<CallExpr>(
E)) {
314 return resolveTypeOfCallExpr(CE);
316 if (
const auto *ME = dyn_cast<MemberExpr>(
E))
317 return {ME->getMemberDecl()};
322QualType HeuristicResolverImpl::resolveExprToType(
const Expr *
E) {
323 std::vector<const NamedDecl *> Decls = resolveExprToDecls(
E);
325 return resolveDeclsToType(Decls, Ctx);
330QualType HeuristicResolverImpl::resolveNestedNameSpecifierToType(
331 const NestedNameSpecifier *NNS) {
339 switch (NNS->getKind()) {
342 return QualType(NNS->getAsType(), 0);
344 return resolveDeclsToType(
345 resolveDependentMember(
346 resolveNestedNameSpecifierToType(NNS->getPrefix()),
347 NNS->getAsIdentifier(), TypeFilter),
362 DeclarationName Name) {
363 Path.Decls = RD->lookup(Name).begin();
371bool HeuristicResolverImpl::findOrdinaryMemberInDependentClasses(
373 DeclarationName Name) {
375 resolveTypeToRecordDecl(
Specifier->getType().getTypePtr());
381std::vector<const NamedDecl *> HeuristicResolverImpl::lookupDependentName(
382 CXXRecordDecl *RD, DeclarationName Name,
383 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
384 std::vector<const NamedDecl *> Results;
387 bool AnyOrdinaryMembers =
false;
388 for (
const NamedDecl *ND : RD->lookup(Name)) {
390 AnyOrdinaryMembers =
true;
392 Results.push_back(ND);
394 if (AnyOrdinaryMembers)
400 if (!RD->lookupInBases(
402 return findOrdinaryMemberInDependentClasses(Specifier, Path, Name);
409 Results.push_back(*I);
414std::vector<const NamedDecl *> HeuristicResolverImpl::resolveDependentMember(
415 QualType QT, DeclarationName Name,
416 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
417 const Type *
T = QT.getTypePtrOrNull();
420 if (
auto *ET =
T->
getAs<EnumType>()) {
421 auto Result = ET->getDecl()->lookup(Name);
424 if (
auto *RD = resolveTypeToRecordDecl(
T)) {
425 if (!RD->hasDefinition())
427 RD = RD->getDefinition();
428 return lookupDependentName(RD, Name, [&](
const NamedDecl *ND) {
431 if (
const auto *MD = dyn_cast<CXXMethodDecl>(ND)) {
432 return MD->getMethodQualifiers().compatiblyIncludes(QT.getQualifiers(),
444 return HeuristicResolverImpl(Ctx).resolveMemberExpr(ME);
448 return HeuristicResolverImpl(Ctx).resolveDeclRefExpr(RE);
450std::vector<const NamedDecl *>
452 return HeuristicResolverImpl(Ctx).resolveTypeOfCallExpr(CE);
454std::vector<const NamedDecl *>
456 return HeuristicResolverImpl(Ctx).resolveCalleeOfCallExpr(CE);
460 return HeuristicResolverImpl(Ctx).resolveUsingValueDecl(UUVD);
464 return HeuristicResolverImpl(Ctx).resolveDependentNameType(DNT);
466std::vector<const NamedDecl *>
469 return HeuristicResolverImpl(Ctx).resolveTemplateSpecializationType(DTST);
473 return HeuristicResolverImpl(Ctx).resolveNestedNameSpecifierToType(NNS);
476 return HeuristicResolverImpl(Ctx).getPointeeType(
T);
Defines the clang::ASTContext interface.
static bool findOrdinaryMemberInDependentClasses(const CXXBaseSpecifier *Specifier, CXXBasePath &Path, DeclarationName Name)
static bool isOrdinaryMember(const NamedDecl *ND)
static bool findOrdinaryMember(const CXXRecordDecl *RD, CXXBasePath &Path, DeclarationName Name)
Defines the C++ template declaration subclasses.
Defines the clang::Expr interface and subclasses for C++ expressions.
static QualType getPointeeType(const MemRegion *R)
C Language Family Type Representation.
const NestedNameSpecifier * Specifier
DeclarationNameTable DeclarationNames
QualType getTypeDeclType(const TypeDecl *Decl, const TypeDecl *PrevDecl=nullptr) const
Return the unique reference to the type for the specified type declaration.
Represents a C++ member access expression where the actual member referenced could not be resolved be...
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
lookup_result::iterator lookup_iterator
@ IDNS_Ordinary
Ordinary names.
@ IDNS_Member
Members, declared with object declarations within tag definitions.
@ IDNS_Tag
Tags, declared with 'struct foo;' and referenced with 'struct foo'.
Represents a qualified type name for which the type name is dependent.
A qualified reference to a name whose declaration cannot yet be resolved.
Represents a template specialization type whose template cannot be resolved, e.g.
std::vector< const NamedDecl * > resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE) const
const QualType getPointeeType(QualType T) const
std::vector< const NamedDecl * > resolveMemberExpr(const CXXDependentScopeMemberExpr *ME) const
QualType resolveNestedNameSpecifierToType(const NestedNameSpecifier *NNS) const
std::vector< const NamedDecl * > resolveCalleeOfCallExpr(const CallExpr *CE) const
std::vector< const NamedDecl * > resolveTypeOfCallExpr(const CallExpr *CE) const
std::vector< const NamedDecl * > resolveUsingValueDecl(const UnresolvedUsingValueDecl *UUVD) const
std::vector< const NamedDecl * > resolveTemplateSpecializationType(const DependentTemplateSpecializationType *DTST) const
std::vector< const NamedDecl * > resolveDependentNameType(const DependentNameType *DNT) const
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
@ TypeSpec
A type, stored as a Type*.
@ TypeSpecWithTemplate
A type that was preceded by the 'template' keyword, stored as a Type*.
@ Identifier
An identifier, stored as an IdentifierInfo*.
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
@ Type
The template argument is a type.
bool isPointerType() const
const T * castAs() const
Member-template castAs<specific type>.
QualType getCanonicalTypeInternal() const
const T * getAs() const
Member-template getAs<specific type>'.
Represents a dependent using declaration which was not marked with typename.
The JSON file list parser is used to communicate input to InstallAPI.
@ Result
The result type of a method or function.
const FunctionProtoType * T