29class HeuristicResolverImpl {
31 HeuristicResolverImpl(ASTContext &Ctx) : Ctx(Ctx) {}
35 std::vector<const NamedDecl *>
36 resolveMemberExpr(
const CXXDependentScopeMemberExpr *ME);
37 std::vector<const NamedDecl *>
38 resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE);
39 std::vector<const NamedDecl *> resolveCalleeOfCallExpr(
const CallExpr *CE);
40 std::vector<const NamedDecl *>
41 resolveUsingValueDecl(
const UnresolvedUsingValueDecl *UUVD);
42 std::vector<const NamedDecl *>
43 resolveDependentNameType(
const DependentNameType *DNT);
44 std::vector<const NamedDecl *>
45 resolveTemplateSpecializationType(
const TemplateSpecializationType *TST);
46 QualType resolveNestedNameSpecifierToType(NestedNameSpecifier NNS);
48 std::vector<const NamedDecl *>
49 lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
50 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
51 TagDecl *resolveTypeToTagDecl(QualType
T);
52 QualType simplifyType(QualType
Type,
const Expr *E,
bool UnwrapPointer);
53 QualType resolveExprToType(
const Expr *E);
54 FunctionProtoTypeLoc getFunctionProtoTypeLoc(
const Expr *Fn);
60 llvm::SmallPtrSet<const DependentNameType *, 4> SeenDependentNameTypes;
71 std::vector<const NamedDecl *>
72 resolveDependentMember(QualType
T, DeclarationName Name,
73 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
75 std::vector<const NamedDecl *> resolveExprToDecls(
const Expr *E);
76 QualType resolveTypeOfCallExpr(
const CallExpr *CE);
78 bool findOrdinaryMemberInDependentClasses(
const CXXBaseSpecifier *Specifier,
80 DeclarationName Name);
85const auto NoFilter = [](
const NamedDecl *D) {
return true; };
86const auto NonStaticFilter = [](
const NamedDecl *D) {
87 return D->isCXXInstanceMember();
89const auto StaticFilter = [](
const NamedDecl *D) {
90 return !D->isCXXInstanceMember();
94const auto TemplateFilter = [](
const NamedDecl *D) {
110 const auto *TTPT =
T.isNull() ?
nullptr :
T->getAs<TemplateTypeParmType>();
113 const auto *TTPD = TTPT->getDecl();
114 if (!TTPD || !TTPD->hasDefaultArgument())
116 const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
119 return DefaultArg.getAsType();
123 if (
const auto *TempD = dyn_cast<TemplateDecl>(D)) {
124 D = TempD->getTemplatedDecl();
129 if (
const auto *TND = dyn_cast<TypedefNameDecl>(D)) {
130 if (
QualType Default = getDefaultTemplateArgument(TND->getUnderlyingType());
134 if (
const auto *TD = dyn_cast<TypeDecl>(D))
136 if (
const auto *VD = dyn_cast<ValueDecl>(D)) {
137 return VD->getType();
142QualType resolveDeclsToType(
const std::vector<const NamedDecl *> &Decls,
144 if (Decls.size() != 1)
146 return resolveDeclToType(Decls[0], Ctx);
150 if (
const auto *TST =
T->getAs<TemplateSpecializationType>()) {
151 return TST->getTemplateName();
153 if (
const auto *DTST =
T->getAs<DeducedTemplateSpecializationType>()) {
154 return DTST->getTemplateName();
163 const Type *
T = QT.getTypePtrOrNull();
170 if (
const auto *DNT =
T->
getAs<DependentNameType>()) {
171 T = resolveDeclsToType(resolveDependentNameType(DNT), Ctx)
181 if (
const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) {
182 if (CTSD->getTemplateSpecializationKind() == TSK_Undeclared) {
183 return CTSD->getSpecializedTemplate()->getTemplatedDecl();
193 const ClassTemplateDecl *TD =
194 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
198 return TD->getTemplatedDecl();
201QualType HeuristicResolverImpl::getPointeeType(QualType
T) {
212 auto ArrowOps = resolveDependentMember(
214 if (ArrowOps.empty())
223 auto *TST =
T->
getAs<TemplateSpecializationType>();
226 if (TST->template_arguments().size() == 0)
228 const TemplateArgument &FirstArg = TST->template_arguments()[0];
229 if (FirstArg.getKind() != TemplateArgument::Type)
231 return FirstArg.getAsType();
234QualType HeuristicResolverImpl::simplifyType(QualType
Type,
const Expr *E,
235 bool UnwrapPointer) {
236 bool DidUnwrapPointer =
false;
240 struct TypeExprPair {
242 const Expr *E =
nullptr;
244 TypeExprPair Current{Type, E};
245 auto SimplifyOneStep = [UnwrapPointer, &DidUnwrapPointer,
246 this](TypeExprPair
T) -> TypeExprPair {
249 DidUnwrapPointer =
true;
253 if (
const auto *RT =
T.
Type->getAs<ReferenceType>()) {
255 return {RT->getPointeeType()};
257 if (
const auto *BT =
T.
Type->getAs<BuiltinType>()) {
261 if (
T.E && BT->getKind() == BuiltinType::Dependent) {
262 return {resolveExprToType(
T.E),
T.E};
265 if (
const auto *AT =
T.
Type->getContainedAutoType()) {
273 if (
const auto *DRE = dyn_cast<DeclRefExpr>(
T.E)) {
274 if (
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
275 if (
auto *Init = VD->getInit())
276 return {resolveExprToType(Init),
Init};
281 if (QualType Default = getDefaultTemplateArgument(
T.
Type);
288 if (
const auto *TST =
289 dyn_cast_if_present<TemplateSpecializationType>(
T.
Type)) {
290 if (
const auto *TTPD = dyn_cast_if_present<TemplateTemplateParmDecl>(
291 TST->getTemplateName().getAsTemplateDecl())) {
292 if (TTPD->hasDefaultArgument()) {
293 const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
294 if (DefaultArg.getKind() == TemplateArgument::Template) {
295 if (
const auto *CTD = dyn_cast_if_present<ClassTemplateDecl>(
296 DefaultArg.getAsTemplate().getAsTemplateDecl())) {
307 if (!
T.
Type.isNull() &&
308 (
T.
Type->isUndeducedAutoType() ||
T.
Type->isTemplateTypeParmType())) {
309 if (
auto *DRE = dyn_cast_if_present<DeclRefExpr>(
T.E)) {
310 auto *PrDecl = dyn_cast<ParmVarDecl>(DRE->getDecl());
311 if (PrDecl && PrDecl->isExplicitObjectParameter()) {
313 dyn_cast<TagDecl>(PrDecl->getDeclContext()->getParent());
323 size_t StepCount = 0;
324 const size_t MaxSteps = 64;
325 while (!Current.Type.isNull() && StepCount++ < MaxSteps) {
326 TypeExprPair
New = SimplifyOneStep(Current);
327 if (
New.Type == Current.Type)
331 if (UnwrapPointer && !DidUnwrapPointer)
336std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
337 const CXXDependentScopeMemberExpr *ME) {
349 if (NestedNameSpecifier NNS = ME->getQualifier()) {
350 if (QualType QualifierType = resolveNestedNameSpecifierToType(NNS);
351 !QualifierType.isNull()) {
353 resolveDependentMember(QualifierType, ME->getMember(), NoFilter);
366 Expr *
Base = ME->isImplicitAccess() ?
nullptr : ME->getBase();
367 QualType BaseType = ME->getBaseType();
368 BaseType = simplifyType(BaseType, Base, ME->isArrow());
369 return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
372std::vector<const NamedDecl *>
373HeuristicResolverImpl::resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE) {
374 QualType
Qualifier = resolveNestedNameSpecifierToType(RE->getQualifier());
375 Qualifier = simplifyType(Qualifier,
nullptr,
false);
376 return resolveDependentMember(Qualifier, RE->getDeclName(), StaticFilter);
379QualType HeuristicResolverImpl::resolveTypeOfCallExpr(
const CallExpr *CE) {
383 std::vector<const NamedDecl *> CalleeDecls;
384 for (
const NamedDecl *D : resolveExprToDecls(CE->getCallee())) {
392 if (
const auto *UUVD = dyn_cast<UnresolvedUsingValueDecl>(D)) {
393 auto Underlying = resolveUsingValueDecl(UUVD);
394 CalleeDecls.insert(CalleeDecls.end(), Underlying.begin(),
398 CalleeDecls.push_back(D);
401 QualType CommonReturnType;
402 for (
const NamedDecl *CalleeDecl : CalleeDecls) {
403 QualType CalleeType = resolveDeclToType(CalleeDecl, Ctx);
404 if (CalleeType.isNull())
406 if (
const auto *FnTypePtr = CalleeType->getAs<PointerType>())
407 CalleeType = FnTypePtr->getPointeeType();
408 if (
const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
409 QualType ReturnType =
410 simplifyType(FnType->getReturnType(),
nullptr,
false);
411 if (!CommonReturnType.isNull() && CommonReturnType != ReturnType) {
414 CommonReturnType = ReturnType;
417 return CommonReturnType;
420std::vector<const NamedDecl *>
421HeuristicResolverImpl::resolveCalleeOfCallExpr(
const CallExpr *CE) {
422 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
426 return resolveExprToDecls(CE->getCallee());
429std::vector<const NamedDecl *> HeuristicResolverImpl::resolveUsingValueDecl(
430 const UnresolvedUsingValueDecl *UUVD) {
431 NestedNameSpecifier
Qualifier = UUVD->getQualifier();
432 if (
Qualifier.getKind() != NestedNameSpecifier::Kind::Type)
434 return resolveDependentMember(QualType(
Qualifier.getAsType(), 0),
435 UUVD->getNameInfo().getName(), ValueFilter);
438std::vector<const NamedDecl *>
439HeuristicResolverImpl::resolveDependentNameType(
const DependentNameType *DNT) {
440 if (
auto [_, inserted] = SeenDependentNameTypes.insert(DNT); !inserted)
442 return resolveDependentMember(
443 resolveNestedNameSpecifierToType(DNT->getQualifier()),
444 DNT->getIdentifier(), TypeFilter);
447std::vector<const NamedDecl *>
448HeuristicResolverImpl::resolveTemplateSpecializationType(
449 const TemplateSpecializationType *TST) {
450 if (TST->getTemplateName().getKind() == TemplateName::DependentTemplate) {
451 const DependentTemplateStorage &DTN =
452 *TST->getTemplateName().getAsDependentTemplateName();
453 return resolveDependentMember(
454 resolveNestedNameSpecifierToType(DTN.getQualifier()),
455 DTN.getName().getIdentifier(), TemplateFilter);
460std::vector<const NamedDecl *>
461HeuristicResolverImpl::resolveExprToDecls(
const Expr *E) {
462 if (
const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
463 return resolveMemberExpr(ME);
465 if (
const auto *RE = dyn_cast<DependentScopeDeclRefExpr>(E)) {
466 return resolveDeclRefExpr(RE);
468 if (
const auto *OE = dyn_cast<OverloadExpr>(E)) {
469 return {OE->decls_begin(), OE->decls_end()};
471 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
472 QualType
T = resolveTypeOfCallExpr(CE);
473 if (
const auto *D = resolveTypeToTagDecl(T)) {
478 if (
const auto *ME = dyn_cast<MemberExpr>(E))
479 return {ME->getMemberDecl()};
480 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E))
481 return {DRE->getDecl()};
486QualType HeuristicResolverImpl::resolveExprToType(
const Expr *E) {
492 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
493 if (QualType Resolved = resolveTypeOfCallExpr(CE); !Resolved.isNull())
503 if (
const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) {
504 if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
505 if (
auto Pointee =
getPointeeType(resolveExprToType(UO->getSubExpr()));
512 std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
514 return resolveDeclsToType(Decls, Ctx);
519QualType HeuristicResolverImpl::resolveNestedNameSpecifierToType(
520 NestedNameSpecifier NNS) {
525 switch (NNS.getKind()) {
526 case NestedNameSpecifier::Kind::Type: {
527 const auto *
T = NNS.getAsType();
530 if (
const auto *DTN = dyn_cast<DependentNameType>(T))
531 return resolveDeclsToType(
532 resolveDependentMember(
533 resolveNestedNameSpecifierToType(DTN->getQualifier()),
534 DTN->getIdentifier(), TypeFilter),
536 return QualType(T, 0);
550 DeclarationName Name) {
551 Path.Decls = RD->lookup(Name).begin();
559bool HeuristicResolverImpl::findOrdinaryMemberInDependentClasses(
560 const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
561 DeclarationName Name) {
562 TagDecl *TD = resolveTypeToTagDecl(
Specifier->getType());
563 if (
const auto *RD = dyn_cast_if_present<CXXRecordDecl>(TD)) {
569std::vector<const NamedDecl *> HeuristicResolverImpl::lookupDependentName(
570 CXXRecordDecl *RD, DeclarationName Name,
571 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
572 std::vector<const NamedDecl *> Results;
575 bool AnyOrdinaryMembers =
false;
576 for (
const NamedDecl *ND : RD->lookup(Name)) {
578 AnyOrdinaryMembers =
true;
580 Results.push_back(ND);
582 if (AnyOrdinaryMembers)
588 if (!RD->lookupInBases(
589 [&](
const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
590 return findOrdinaryMemberInDependentClasses(Specifier, Path, Name);
594 for (DeclContext::lookup_iterator I = Paths.front().Decls, E = I.end();
597 Results.push_back(*I);
602std::vector<const NamedDecl *> HeuristicResolverImpl::resolveDependentMember(
603 QualType QT, DeclarationName Name,
604 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
605 TagDecl *TD = resolveTypeToTagDecl(QT);
608 if (
auto *ED = dyn_cast<EnumDecl>(TD)) {
609 auto Result = ED->lookup(Name);
612 if (
auto *RD = dyn_cast<CXXRecordDecl>(TD)) {
613 if (!RD->hasDefinition())
615 RD = RD->getDefinition();
616 return lookupDependentName(RD, Name, [&](
const NamedDecl *ND) {
619 if (
const auto *MD = dyn_cast<CXXMethodDecl>(ND)) {
620 return !MD->isInstance() ||
621 MD->getMethodQualifiers().compatiblyIncludes(QT.getQualifiers(),
631HeuristicResolverImpl::getFunctionProtoTypeLoc(
const Expr *Fn) {
633 const Expr *NakedFn =
Fn->IgnoreParenCasts();
634 if (
const auto *T = NakedFn->getType().getTypePtr()->getAs<TypedefType>()) {
635 Target =
T->getDecl()->getTypeSourceInfo()->getTypeLoc();
636 }
else if (
const auto *DR = dyn_cast<DeclRefExpr>(NakedFn)) {
637 const auto *D = DR->getDecl();
638 if (
const auto *
const VD = dyn_cast<VarDecl>(D)) {
639 Target = VD->getTypeSourceInfo()->getTypeLoc();
641 }
else if (
const auto *ME = dyn_cast<MemberExpr>(NakedFn)) {
642 const auto *MD = ME->getMemberDecl();
643 if (
const auto *FD = dyn_cast<FieldDecl>(MD)) {
644 Target = FD->getTypeSourceInfo()->getTypeLoc();
653 if (
auto P =
Target.getAs<PointerTypeLoc>()) {
654 Target = P.getPointeeLoc();
657 if (
auto A =
Target.getAs<AttributedTypeLoc>()) {
658 Target = A.getModifiedLoc();
661 if (
auto P =
Target.getAs<ParenTypeLoc>()) {
668 if (
auto F =
Target.getAs<FunctionProtoTypeLoc>()) {
672 if (!llvm::is_contained(F.getParams(),
nullptr))
683 return HeuristicResolverImpl(Ctx).resolveMemberExpr(ME);
687 return HeuristicResolverImpl(Ctx).resolveDeclRefExpr(RE);
689std::vector<const NamedDecl *>
691 return HeuristicResolverImpl(Ctx).resolveCalleeOfCallExpr(CE);
695 return HeuristicResolverImpl(Ctx).resolveUsingValueDecl(UUVD);
698 const DependentNameType *DNT)
const {
699 return HeuristicResolverImpl(Ctx).resolveDependentNameType(DNT);
701std::vector<const NamedDecl *>
703 const TemplateSpecializationType *TST)
const {
704 return HeuristicResolverImpl(Ctx).resolveTemplateSpecializationType(TST);
708 return HeuristicResolverImpl(Ctx).resolveNestedNameSpecifierToType(NNS);
712 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
713 return HeuristicResolverImpl(Ctx).lookupDependentName(RD, Name, Filter);
716 return HeuristicResolverImpl(Ctx).getPointeeType(
T);
719 return HeuristicResolverImpl(Ctx).resolveTypeToTagDecl(
T);
722 bool UnwrapPointer) {
723 return HeuristicResolverImpl(Ctx).simplifyType(
Type, E, UnwrapPointer);
726 return HeuristicResolverImpl(Ctx).resolveExprToType(E);
730 return HeuristicResolverImpl(Ctx).getFunctionProtoTypeLoc(Fn);
Defines the clang::ASTContext interface.
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.
Result
Implement __builtin_bit_cast and related operations.
llvm::MachO::Target Target
static QualType getPointeeType(const MemRegion *R)
C Language Family Type Representation.
DeclarationNameTable DeclarationNames
CanQualType getCanonicalTypeDeclType(const TypeDecl *TD) const
CanQualType getCanonicalTagType(const TagDecl *TD) const
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Represents a C++ member access expression where the actual member referenced could not be resolved be...
Represents a C++ struct/union/class.
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'.
The name of a declaration.
A qualified reference to a name whose declaration cannot yet be resolved.
This represents one expression.
std::vector< const NamedDecl * > resolveDeclRefExpr(const DependentScopeDeclRefExpr *RE) const
const QualType getPointeeType(QualType T) const
QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer)
std::vector< const NamedDecl * > resolveMemberExpr(const CXXDependentScopeMemberExpr *ME) const
FunctionProtoTypeLoc getFunctionProtoTypeLoc(const Expr *Fn) const
QualType resolveNestedNameSpecifierToType(NestedNameSpecifier NNS) const
std::vector< const NamedDecl * > resolveCalleeOfCallExpr(const CallExpr *CE) const
std::vector< const NamedDecl * > resolveTemplateSpecializationType(const TemplateSpecializationType *TST) const
TagDecl * resolveTypeToTagDecl(QualType T) const
QualType resolveExprToType(const Expr *E) const
std::vector< const NamedDecl * > resolveUsingValueDecl(const UnresolvedUsingValueDecl *UUVD) const
std::vector< const NamedDecl * > resolveDependentNameType(const DependentNameType *DNT) const
std::vector< const NamedDecl * > lookupDependentName(CXXRecordDecl *RD, DeclarationName Name, llvm::function_ref< bool(const NamedDecl *ND)> Filter)
This represents a decl that may have a name.
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Represents the declaration of a struct/union/class/enum.
@ Type
The template argument is a type.
Represents a C++ template name within the type system.
The base class of the type hierarchy.
bool isUndeducedAutoType() const
bool isPointerType() const
const T * castAs() const
Member-template castAs<specific type>.
Type(TypeClass tc, QualType canon, TypeDependence Dependence)
TagDecl * getAsTagDecl() const
Retrieves the TagDecl that this type refers to, either because the type is a TagType or because it is...
QualType getCanonicalTypeInternal() const
const T * getAs() const
Member-template getAs<specific type>'.
Represents a dependent using declaration which was not marked with typename.
bool Init(InterpState &S, CodePtr OpPC)
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
@ TemplateName
The identifier is a template name. FIXME: Add an annotation for that.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...
@ Default
Set to the current date and time.
const FunctionProtoType * T
@ Type
The name was classified as a type.