clang 22.0.0git
HeuristicResolver.h
Go to the documentation of this file.
1//===--- HeuristicResolver.h - Resolution of dependent names -----*- C++-*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLVM_CLANG_SEMA_HEURISTICRESOLVER_H
10#define LLVM_CLANG_SEMA_HEURISTICRESOLVER_H
11
12#include "clang/AST/Decl.h"
13#include <vector>
14
15namespace clang {
16
17class ASTContext;
18class CallExpr;
19class CXXBasePath;
20class CXXDependentScopeMemberExpr;
21class DeclarationName;
22class DependentScopeDeclRefExpr;
23class FunctionProtoTypeLoc;
24class NamedDecl;
25class Type;
26class UnresolvedUsingValueDecl;
27
28// This class handles heuristic resolution of declarations and types in template
29// code.
30//
31// As a compiler, clang only needs to perform certain types of processing on
32// template code (such as resolving dependent names to declarations, or
33// resolving the type of a dependent expression) after instantiation. Indeed,
34// C++ language features such as template specialization mean such resolution
35// cannot be done accurately before instantiation.
36//
37// However, template code is written and read in uninstantiated form, and clangd
38// would like to provide editor features like go-to-definition in template code
39// where possible. To this end, clangd attempts to resolve declarations and
40// types in uninstantiated code by using heuristics, understanding that the
41// results may not be fully accurate but that this is better than nothing.
42//
43// At this time, the heuristic used is a simple but effective one: assume that
44// template instantiations are based on the primary template definition and not
45// not a specialization. More advanced heuristics may be added in the future.
47public:
48 HeuristicResolver(ASTContext &Ctx) : Ctx(Ctx) {}
49
50 // Try to heuristically resolve certain types of expressions, declarations, or
51 // types to one or more likely-referenced declarations.
52 std::vector<const NamedDecl *>
54 std::vector<const NamedDecl *>
56 std::vector<const NamedDecl *>
57 resolveTypeOfCallExpr(const CallExpr *CE) const;
58 std::vector<const NamedDecl *>
59 resolveCalleeOfCallExpr(const CallExpr *CE) const;
60 std::vector<const NamedDecl *>
62 std::vector<const NamedDecl *>
64 std::vector<const NamedDecl *> resolveTemplateSpecializationType(
65 const DependentTemplateSpecializationType *DTST) const;
66
67 // Try to heuristically resolve a dependent nested name specifier
68 // to the type it likely denotes. Note that *dependent* name specifiers always
69 // denote types, not namespaces.
71
72 // Perform an imprecise lookup of a dependent name in `RD`.
73 // This function does not follow strict semantic rules and should be used
74 // only when lookup rules can be relaxed, e.g. indexing.
75 std::vector<const NamedDecl *>
77 llvm::function_ref<bool(const NamedDecl *ND)> Filter);
78
79 // Given the type T of a dependent expression that appears of the LHS of a
80 // "->", heuristically find a corresponding pointee type in whose scope we
81 // could look up the name appearing on the RHS.
82 const QualType getPointeeType(QualType T) const;
83
84 // Heuristically resolve a possibly-dependent type `T` to a TagDecl
85 // in which a member's name can be looked up.
87
88 // Simplify the type `Type`.
89 // `E` is the expression whose type `Type` is, if known. This sometimes
90 // contains information relevant to the type that's not stored in `Type`
91 // itself.
92 // If `UnwrapPointer` is true, exactly only pointer type will be unwrapped
93 // during simplification, and the operation fails if no pointer type is found.
94 QualType simplifyType(QualType Type, const Expr *E, bool UnwrapPointer);
95
96 // Given an expression `Fn` representing the callee in a function call,
97 // if the call is through a function pointer, try to find the declaration of
98 // the corresponding function pointer type, so that we can recover argument
99 // names from it.
101
102private:
103 ASTContext &Ctx;
104};
105
106} // namespace clang
107
108#endif
MatchType Type
Expr * E
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition: ASTContext.h:188
Represents a C++ member access expression where the actual member referenced could not be resolved be...
Definition: ExprCXX.h:3864
Represents a C++ struct/union/class.
Definition: DeclCXX.h:258
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition: Expr.h:2879
The name of a declaration.
Represents a qualified type name for which the type name is dependent.
Definition: TypeBase.h:7414
A qualified reference to a name whose declaration cannot yet be resolved.
Definition: ExprCXX.h:3504
Represents a template specialization type whose template cannot be resolved, e.g.
Definition: TypeBase.h:7465
This represents one expression.
Definition: Expr.h:112
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
TagDecl * resolveTypeToTagDecl(QualType T) 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
HeuristicResolver(ASTContext &Ctx)
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.
Definition: Decl.h:273
Represents a C++ nested name specifier, such as "\::std::vector<int>::".
A (possibly-)qualified type.
Definition: TypeBase.h:937
Represents the declaration of a struct/union/class/enum.
Definition: Decl.h:3714
The base class of the type hierarchy.
Definition: TypeBase.h:1833
Represents a dependent using declaration which was not marked with typename.
Definition: DeclCXX.h:3934
The JSON file list parser is used to communicate input to InstallAPI.
const FunctionProtoType * T