28class HeuristicResolverImpl {
30 HeuristicResolverImpl(ASTContext &Ctx) : Ctx(Ctx) {}
34 std::vector<const NamedDecl *>
35 resolveMemberExpr(
const CXXDependentScopeMemberExpr *ME);
36 std::vector<const NamedDecl *>
37 resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE);
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 *>
44 resolveTemplateSpecializationType(
const TemplateSpecializationType *TST);
45 QualType resolveNestedNameSpecifierToType(NestedNameSpecifier NNS);
47 std::vector<const NamedDecl *>
48 lookupDependentName(CXXRecordDecl *RD, DeclarationName Name,
49 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
50 TagDecl *resolveTypeToTagDecl(QualType
T);
51 QualType simplifyType(QualType
Type,
const Expr *E,
bool UnwrapPointer);
52 QualType resolveExprToType(
const Expr *E);
53 FunctionProtoTypeLoc getFunctionProtoTypeLoc(
const Expr *Fn);
59 llvm::SmallPtrSet<const DependentNameType *, 4> SeenDependentNameTypes;
70 std::vector<const NamedDecl *>
71 resolveDependentMember(QualType
T, DeclarationName Name,
72 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter);
74 std::vector<const NamedDecl *> resolveExprToDecls(
const Expr *E);
75 QualType resolveTypeOfCallExpr(
const CallExpr *CE);
77 bool findOrdinaryMemberInDependentClasses(
const CXXBaseSpecifier *Specifier,
79 DeclarationName Name);
84const auto NoFilter = [](
const NamedDecl *D) {
return true; };
85const auto NonStaticFilter = [](
const NamedDecl *D) {
86 return D->isCXXInstanceMember();
88const auto StaticFilter = [](
const NamedDecl *D) {
89 return !D->isCXXInstanceMember();
93const auto TemplateFilter = [](
const NamedDecl *D) {
98 if (
const auto *TempD = dyn_cast<TemplateDecl>(D)) {
99 D = TempD->getTemplatedDecl();
101 if (
const auto *TD = dyn_cast<TypeDecl>(D))
103 if (
const auto *VD = dyn_cast<ValueDecl>(D)) {
104 return VD->getType();
109QualType resolveDeclsToType(
const std::vector<const NamedDecl *> &Decls,
111 if (Decls.size() != 1)
113 return resolveDeclToType(Decls[0], Ctx);
117 if (
const auto *TST =
T->getAs<TemplateSpecializationType>()) {
118 return TST->getTemplateName();
120 if (
const auto *DTST =
T->getAs<DeducedTemplateSpecializationType>()) {
121 return DTST->getTemplateName();
130 const Type *
T = QT.getTypePtrOrNull();
137 if (
const auto *DNT =
T->
getAs<DependentNameType>()) {
138 T = resolveDeclsToType(resolveDependentNameType(DNT), Ctx)
148 if (
const auto *CTSD = dyn_cast<ClassTemplateSpecializationDecl>(TD)) {
149 if (CTSD->getTemplateSpecializationKind() == TSK_Undeclared) {
150 return CTSD->getSpecializedTemplate()->getTemplatedDecl();
160 const ClassTemplateDecl *TD =
161 dyn_cast_or_null<ClassTemplateDecl>(TN.getAsTemplateDecl());
165 return TD->getTemplatedDecl();
168QualType HeuristicResolverImpl::getPointeeType(QualType
T) {
179 auto ArrowOps = resolveDependentMember(
181 if (ArrowOps.empty())
190 auto *TST =
T->
getAs<TemplateSpecializationType>();
193 if (TST->template_arguments().size() == 0)
195 const TemplateArgument &FirstArg = TST->template_arguments()[0];
196 if (FirstArg.getKind() != TemplateArgument::Type)
198 return FirstArg.getAsType();
201QualType HeuristicResolverImpl::simplifyType(QualType
Type,
const Expr *E,
202 bool UnwrapPointer) {
203 bool DidUnwrapPointer =
false;
207 struct TypeExprPair {
209 const Expr *E =
nullptr;
211 TypeExprPair Current{Type, E};
212 auto SimplifyOneStep = [UnwrapPointer, &DidUnwrapPointer,
213 this](TypeExprPair
T) -> TypeExprPair {
216 DidUnwrapPointer =
true;
220 if (
const auto *RT =
T.
Type->getAs<ReferenceType>()) {
222 return {RT->getPointeeType()};
224 if (
const auto *BT =
T.
Type->getAs<BuiltinType>()) {
228 if (
T.E && BT->getKind() == BuiltinType::Dependent) {
229 return {resolveExprToType(
T.E),
T.E};
232 if (
const auto *AT =
T.
Type->getContainedAutoType()) {
240 if (
const auto *DRE = dyn_cast<DeclRefExpr>(
T.E)) {
241 if (
const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) {
242 if (
auto *Init = VD->getInit())
243 return {resolveExprToType(Init),
Init};
248 if (
const auto *TTPT = dyn_cast_if_present<TemplateTypeParmType>(
T.
Type)) {
253 if (
const auto *TTPD = TTPT->getDecl()) {
254 if (TTPD->hasDefaultArgument()) {
255 const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
256 if (DefaultArg.getKind() == TemplateArgument::Type) {
257 return {DefaultArg.getAsType()};
265 if (
const auto *TST =
266 dyn_cast_if_present<TemplateSpecializationType>(
T.
Type)) {
267 if (
const auto *TTPD = dyn_cast_if_present<TemplateTemplateParmDecl>(
268 TST->getTemplateName().getAsTemplateDecl())) {
269 if (TTPD->hasDefaultArgument()) {
270 const auto &DefaultArg = TTPD->getDefaultArgument().getArgument();
271 if (DefaultArg.getKind() == TemplateArgument::Template) {
272 if (
const auto *CTD = dyn_cast_if_present<ClassTemplateDecl>(
273 DefaultArg.getAsTemplate().getAsTemplateDecl())) {
284 if (!
T.
Type.isNull() &&
285 (
T.
Type->isUndeducedAutoType() ||
T.
Type->isTemplateTypeParmType())) {
286 if (
auto *DRE = dyn_cast_if_present<DeclRefExpr>(
T.E)) {
287 auto *PrDecl = dyn_cast<ParmVarDecl>(DRE->getDecl());
288 if (PrDecl && PrDecl->isExplicitObjectParameter()) {
290 dyn_cast<TagDecl>(PrDecl->getDeclContext()->getParent());
300 size_t StepCount = 0;
301 const size_t MaxSteps = 64;
302 while (!Current.Type.isNull() && StepCount++ < MaxSteps) {
303 TypeExprPair
New = SimplifyOneStep(Current);
304 if (
New.Type == Current.Type)
308 if (UnwrapPointer && !DidUnwrapPointer)
313std::vector<const NamedDecl *> HeuristicResolverImpl::resolveMemberExpr(
314 const CXXDependentScopeMemberExpr *ME) {
326 if (NestedNameSpecifier NNS = ME->getQualifier()) {
327 if (QualType QualifierType = resolveNestedNameSpecifierToType(NNS);
328 !QualifierType.isNull()) {
330 resolveDependentMember(QualifierType, ME->getMember(), NoFilter);
343 Expr *
Base = ME->isImplicitAccess() ?
nullptr : ME->getBase();
344 QualType BaseType = ME->getBaseType();
345 BaseType = simplifyType(BaseType, Base, ME->isArrow());
346 return resolveDependentMember(BaseType, ME->getMember(), NoFilter);
349std::vector<const NamedDecl *>
350HeuristicResolverImpl::resolveDeclRefExpr(
const DependentScopeDeclRefExpr *RE) {
351 QualType
Qualifier = resolveNestedNameSpecifierToType(RE->getQualifier());
352 Qualifier = simplifyType(Qualifier,
nullptr,
false);
353 return resolveDependentMember(Qualifier, RE->getDeclName(), StaticFilter);
356QualType HeuristicResolverImpl::resolveTypeOfCallExpr(
const CallExpr *CE) {
360 std::vector<const NamedDecl *> CalleeDecls =
361 resolveExprToDecls(CE->getCallee());
362 QualType CommonReturnType;
363 for (
const NamedDecl *CalleeDecl : CalleeDecls) {
364 QualType CalleeType = resolveDeclToType(CalleeDecl, Ctx);
365 if (CalleeType.isNull())
367 if (
const auto *FnTypePtr = CalleeType->getAs<PointerType>())
368 CalleeType = FnTypePtr->getPointeeType();
369 if (
const FunctionType *FnType = CalleeType->getAs<FunctionType>()) {
370 QualType ReturnType =
371 simplifyType(FnType->getReturnType(),
nullptr,
false);
372 if (!CommonReturnType.isNull() && CommonReturnType != ReturnType) {
375 CommonReturnType = ReturnType;
378 return CommonReturnType;
381std::vector<const NamedDecl *>
382HeuristicResolverImpl::resolveCalleeOfCallExpr(
const CallExpr *CE) {
383 if (
const auto *ND = dyn_cast_or_null<NamedDecl>(CE->getCalleeDecl())) {
387 return resolveExprToDecls(CE->getCallee());
390std::vector<const NamedDecl *> HeuristicResolverImpl::resolveUsingValueDecl(
391 const UnresolvedUsingValueDecl *UUVD) {
392 NestedNameSpecifier
Qualifier = UUVD->getQualifier();
393 if (
Qualifier.getKind() != NestedNameSpecifier::Kind::Type)
395 return resolveDependentMember(QualType(
Qualifier.getAsType(), 0),
396 UUVD->getNameInfo().getName(), ValueFilter);
399std::vector<const NamedDecl *>
400HeuristicResolverImpl::resolveDependentNameType(
const DependentNameType *DNT) {
401 if (
auto [_, inserted] = SeenDependentNameTypes.insert(DNT); !inserted)
403 return resolveDependentMember(
404 resolveNestedNameSpecifierToType(DNT->getQualifier()),
405 DNT->getIdentifier(), TypeFilter);
408std::vector<const NamedDecl *>
409HeuristicResolverImpl::resolveTemplateSpecializationType(
410 const TemplateSpecializationType *TST) {
411 const DependentTemplateStorage &DTN =
412 *TST->getTemplateName().getAsDependentTemplateName();
413 return resolveDependentMember(
414 resolveNestedNameSpecifierToType(DTN.getQualifier()),
415 DTN.getName().getIdentifier(), TemplateFilter);
418std::vector<const NamedDecl *>
419HeuristicResolverImpl::resolveExprToDecls(
const Expr *E) {
420 if (
const auto *ME = dyn_cast<CXXDependentScopeMemberExpr>(E)) {
421 return resolveMemberExpr(ME);
423 if (
const auto *RE = dyn_cast<DependentScopeDeclRefExpr>(E)) {
424 return resolveDeclRefExpr(RE);
426 if (
const auto *OE = dyn_cast<OverloadExpr>(E)) {
427 return {OE->decls_begin(), OE->decls_end()};
429 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
430 QualType
T = resolveTypeOfCallExpr(CE);
431 if (
const auto *D = resolveTypeToTagDecl(T)) {
436 if (
const auto *ME = dyn_cast<MemberExpr>(E))
437 return {ME->getMemberDecl()};
438 if (
const auto *DRE = dyn_cast<DeclRefExpr>(E))
439 return {DRE->getDecl()};
444QualType HeuristicResolverImpl::resolveExprToType(
const Expr *E) {
450 if (
const auto *CE = dyn_cast<CallExpr>(E)) {
451 if (QualType Resolved = resolveTypeOfCallExpr(CE); !Resolved.isNull())
456 if (
const auto *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) {
457 if (UO->getOpcode() == UnaryOperatorKind::UO_Deref) {
458 if (
auto Pointee =
getPointeeType(resolveExprToType(UO->getSubExpr()));
465 std::vector<const NamedDecl *> Decls = resolveExprToDecls(E);
467 return resolveDeclsToType(Decls, Ctx);
472QualType HeuristicResolverImpl::resolveNestedNameSpecifierToType(
473 NestedNameSpecifier NNS) {
478 switch (NNS.getKind()) {
479 case NestedNameSpecifier::Kind::Type: {
480 const auto *
T = NNS.getAsType();
483 if (
const auto *DTN = dyn_cast<DependentNameType>(T))
484 return resolveDeclsToType(
485 resolveDependentMember(
486 resolveNestedNameSpecifierToType(DTN->getQualifier()),
487 DTN->getIdentifier(), TypeFilter),
489 return QualType(T, 0);
503 DeclarationName Name) {
504 Path.Decls = RD->lookup(Name).begin();
512bool HeuristicResolverImpl::findOrdinaryMemberInDependentClasses(
513 const CXXBaseSpecifier *Specifier, CXXBasePath &Path,
514 DeclarationName Name) {
515 TagDecl *TD = resolveTypeToTagDecl(
Specifier->getType());
516 if (
const auto *RD = dyn_cast_if_present<CXXRecordDecl>(TD)) {
522std::vector<const NamedDecl *> HeuristicResolverImpl::lookupDependentName(
523 CXXRecordDecl *RD, DeclarationName Name,
524 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
525 std::vector<const NamedDecl *> Results;
528 bool AnyOrdinaryMembers =
false;
529 for (
const NamedDecl *ND : RD->lookup(Name)) {
531 AnyOrdinaryMembers =
true;
533 Results.push_back(ND);
535 if (AnyOrdinaryMembers)
541 if (!RD->lookupInBases(
542 [&](
const CXXBaseSpecifier *Specifier, CXXBasePath &Path) {
543 return findOrdinaryMemberInDependentClasses(Specifier, Path, Name);
547 for (DeclContext::lookup_iterator I = Paths.front().Decls, E = I.end();
550 Results.push_back(*I);
555std::vector<const NamedDecl *> HeuristicResolverImpl::resolveDependentMember(
556 QualType QT, DeclarationName Name,
557 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
558 TagDecl *TD = resolveTypeToTagDecl(QT);
561 if (
auto *ED = dyn_cast<EnumDecl>(TD)) {
562 auto Result = ED->lookup(Name);
565 if (
auto *RD = dyn_cast<CXXRecordDecl>(TD)) {
566 if (!RD->hasDefinition())
568 RD = RD->getDefinition();
569 return lookupDependentName(RD, Name, [&](
const NamedDecl *ND) {
572 if (
const auto *MD = dyn_cast<CXXMethodDecl>(ND)) {
573 return !MD->isInstance() ||
574 MD->getMethodQualifiers().compatiblyIncludes(QT.getQualifiers(),
584HeuristicResolverImpl::getFunctionProtoTypeLoc(
const Expr *Fn) {
586 const Expr *NakedFn =
Fn->IgnoreParenCasts();
587 if (
const auto *T = NakedFn->getType().getTypePtr()->getAs<TypedefType>()) {
588 Target =
T->getDecl()->getTypeSourceInfo()->getTypeLoc();
589 }
else if (
const auto *DR = dyn_cast<DeclRefExpr>(NakedFn)) {
590 const auto *D = DR->getDecl();
591 if (
const auto *
const VD = dyn_cast<VarDecl>(D)) {
592 Target = VD->getTypeSourceInfo()->getTypeLoc();
594 }
else if (
const auto *ME = dyn_cast<MemberExpr>(NakedFn)) {
595 const auto *MD = ME->getMemberDecl();
596 if (
const auto *FD = dyn_cast<FieldDecl>(MD)) {
597 Target = FD->getTypeSourceInfo()->getTypeLoc();
606 if (
auto P =
Target.getAs<PointerTypeLoc>()) {
607 Target = P.getPointeeLoc();
610 if (
auto A =
Target.getAs<AttributedTypeLoc>()) {
611 Target = A.getModifiedLoc();
614 if (
auto P =
Target.getAs<ParenTypeLoc>()) {
621 if (
auto F =
Target.getAs<FunctionProtoTypeLoc>()) {
625 if (!llvm::is_contained(F.getParams(),
nullptr))
636 return HeuristicResolverImpl(Ctx).resolveMemberExpr(ME);
640 return HeuristicResolverImpl(Ctx).resolveDeclRefExpr(RE);
642std::vector<const NamedDecl *>
644 return HeuristicResolverImpl(Ctx).resolveCalleeOfCallExpr(CE);
648 return HeuristicResolverImpl(Ctx).resolveUsingValueDecl(UUVD);
651 const DependentNameType *DNT)
const {
652 return HeuristicResolverImpl(Ctx).resolveDependentNameType(DNT);
654std::vector<const NamedDecl *>
656 const TemplateSpecializationType *TST)
const {
657 return HeuristicResolverImpl(Ctx).resolveTemplateSpecializationType(TST);
661 return HeuristicResolverImpl(Ctx).resolveNestedNameSpecifierToType(NNS);
665 llvm::function_ref<
bool(
const NamedDecl *ND)> Filter) {
666 return HeuristicResolverImpl(Ctx).lookupDependentName(RD, Name, Filter);
669 return HeuristicResolverImpl(Ctx).getPointeeType(
T);
672 return HeuristicResolverImpl(Ctx).resolveTypeToTagDecl(
T);
675 bool UnwrapPointer) {
676 return HeuristicResolverImpl(Ctx).simplifyType(
Type, E, UnwrapPointer);
679 return HeuristicResolverImpl(Ctx).resolveExprToType(E);
683 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.
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.
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)
The JSON file list parser is used to communicate input to InstallAPI.
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',...
@ Result
The result type of a method or function.
const FunctionProtoType * T
@ Type
The name was classified as a type.