Matheus Izvekov 91cdd35008
[clang] Improve nested name specifier AST representation (#147835)
This is a major change on how we represent nested name qualifications in
the AST.

* The nested name specifier itself and how it's stored is changed. The
prefixes for types are handled within the type hierarchy, which makes
canonicalization for them super cheap, no memory allocation required.
Also translating a type into nested name specifier form becomes a no-op.
An identifier is stored as a DependentNameType. The nested name
specifier gains a lightweight handle class, to be used instead of
passing around pointers, which is similar to what is implemented for
TemplateName. There is still one free bit available, and this handle can
be used within a PointerUnion and PointerIntPair, which should keep
bit-packing aficionados happy.
* The ElaboratedType node is removed, all type nodes in which it could
previously apply to can now store the elaborated keyword and name
qualifier, tail allocating when present.
* TagTypes can now point to the exact declaration found when producing
these, as opposed to the previous situation of there only existing one
TagType per entity. This increases the amount of type sugar retained,
and can have several applications, for example in tracking module
ownership, and other tools which care about source file origins, such as
IWYU. These TagTypes are lazily allocated, in order to limit the
increase in AST size.

This patch offers a great performance benefit.

It greatly improves compilation time for
[stdexec](https://github.com/NVIDIA/stdexec). For one datapoint, for
`test_on2.cpp` in that project, which is the slowest compiling test,
this patch improves `-c` compilation time by about 7.2%, with the
`-fsyntax-only` improvement being at ~12%.

This has great results on compile-time-tracker as well:

![image](https://github.com/user-attachments/assets/700dce98-2cab-4aa8-97d1-b038c0bee831)

This patch also further enables other optimziations in the future, and
will reduce the performance impact of template specialization resugaring
when that lands.

It has some other miscelaneous drive-by fixes.

About the review: Yes the patch is huge, sorry about that. Part of the
reason is that I started by the nested name specifier part, before the
ElaboratedType part, but that had a huge performance downside, as
ElaboratedType is a big performance hog. I didn't have the steam to go
back and change the patch after the fact.

There is also a lot of internal API changes, and it made sense to remove
ElaboratedType in one go, versus removing it from one type at a time, as
that would present much more churn to the users. Also, the nested name
specifier having a different API avoids missing changes related to how
prefixes work now, which could make existing code compile but not work.

How to review: The important changes are all in
`clang/include/clang/AST` and `clang/lib/AST`, with also important
changes in `clang/lib/Sema/TreeTransform.h`.

The rest and bulk of the changes are mostly consequences of the changes
in API.

PS: TagType::getDecl is renamed to `getOriginalDecl` in this patch, just
for easier to rebasing. I plan to rename it back after this lands.

Fixes #136624
Fixes https://github.com/llvm/llvm-project/issues/43179
Fixes https://github.com/llvm/llvm-project/issues/68670
Fixes https://github.com/llvm/llvm-project/issues/92757
2025-08-09 05:06:53 -03:00

181 lines
6.1 KiB
C++

//===-- NameSearchContext.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "NameSearchContext.h"
#include "ClangUtil.h"
#include "lldb/Utility/LLDBLog.h"
using namespace clang;
using namespace lldb_private;
clang::NamedDecl *NameSearchContext::AddVarDecl(const CompilerType &type) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return nullptr;
auto lldb_ast = type.GetTypeSystem<TypeSystemClang>();
if (!lldb_ast)
return nullptr;
IdentifierInfo *ii = m_decl_name.getAsIdentifierInfo();
clang::ASTContext &ast = lldb_ast->getASTContext();
clang::NamedDecl *Decl = VarDecl::Create(
ast, const_cast<DeclContext *>(m_decl_context), SourceLocation(),
SourceLocation(), ii, ClangUtil::GetQualType(type), nullptr, SC_Static);
m_decls.push_back(Decl);
return Decl;
}
clang::NamedDecl *NameSearchContext::AddFunDecl(const CompilerType &type,
bool extern_c) {
assert(type && "Type for variable must be valid!");
if (!type.IsValid())
return nullptr;
if (m_function_types.count(type))
return nullptr;
auto lldb_ast = type.GetTypeSystem<TypeSystemClang>();
if (!lldb_ast)
return nullptr;
m_function_types.insert(type);
QualType qual_type(ClangUtil::GetQualType(type));
clang::ASTContext &ast = lldb_ast->getASTContext();
const bool isInlineSpecified = false;
const bool hasWrittenPrototype = true;
const bool isConstexprSpecified = false;
clang::DeclContext *context = const_cast<DeclContext *>(m_decl_context);
if (extern_c) {
context = LinkageSpecDecl::Create(ast, context, SourceLocation(),
SourceLocation(),
clang::LinkageSpecLanguageIDs::C, false);
// FIXME: The LinkageSpecDecl here should be added to m_decl_context.
}
// Pass the identifier info for functions the decl_name is needed for
// operators
clang::DeclarationName decl_name =
m_decl_name.getNameKind() == DeclarationName::Identifier
? m_decl_name.getAsIdentifierInfo()
: m_decl_name;
clang::FunctionDecl *func_decl = FunctionDecl::Create(
ast, context, SourceLocation(), SourceLocation(), decl_name, qual_type,
nullptr, SC_Extern, /*UsesFPIntrin=*/false, isInlineSpecified, hasWrittenPrototype,
isConstexprSpecified ? ConstexprSpecKind::Constexpr
: ConstexprSpecKind::Unspecified);
// We have to do more than just synthesize the FunctionDecl. We have to
// synthesize ParmVarDecls for all of the FunctionDecl's arguments. To do
// this, we raid the function's FunctionProtoType for types.
const FunctionProtoType *func_proto_type =
qual_type.getTypePtr()->getAs<FunctionProtoType>();
if (func_proto_type) {
unsigned NumArgs = func_proto_type->getNumParams();
unsigned ArgIndex;
SmallVector<ParmVarDecl *, 5> parm_var_decls;
for (ArgIndex = 0; ArgIndex < NumArgs; ++ArgIndex) {
QualType arg_qual_type(func_proto_type->getParamType(ArgIndex));
parm_var_decls.push_back(
ParmVarDecl::Create(ast, const_cast<DeclContext *>(context),
SourceLocation(), SourceLocation(), nullptr,
arg_qual_type, nullptr, SC_Static, nullptr));
}
func_decl->setParams(ArrayRef<ParmVarDecl *>(parm_var_decls));
} else {
Log *log = GetLog(LLDBLog::Expressions);
LLDB_LOG(log, "Function type wasn't a FunctionProtoType");
}
// If this is an operator (e.g. operator new or operator==), only insert the
// declaration we inferred from the symbol if we can provide the correct
// number of arguments. We shouldn't really inject random decl(s) for
// functions that are analyzed semantically in a special way, otherwise we
// will crash in clang.
clang::OverloadedOperatorKind op_kind = clang::NUM_OVERLOADED_OPERATORS;
if (func_proto_type &&
TypeSystemClang::IsOperator(decl_name.getAsString().c_str(), op_kind)) {
if (!TypeSystemClang::CheckOverloadedOperatorKindParameterCount(
false, op_kind, func_proto_type->getNumParams()))
return nullptr;
}
m_decls.push_back(func_decl);
return func_decl;
}
clang::NamedDecl *NameSearchContext::AddGenericFunDecl() {
FunctionProtoType::ExtProtoInfo proto_info;
proto_info.Variadic = true;
QualType generic_function_type(
GetASTContext().getFunctionType(GetASTContext().UnknownAnyTy, // result
ArrayRef<QualType>(), // argument types
proto_info));
return AddFunDecl(m_clang_ts.GetType(generic_function_type), true);
}
clang::NamedDecl *
NameSearchContext::AddTypeDecl(const CompilerType &clang_type) {
if (ClangUtil::IsClangType(clang_type)) {
QualType qual_type = ClangUtil::GetQualType(clang_type);
if (const TypedefType *typedef_type =
llvm::dyn_cast<TypedefType>(qual_type)) {
TypedefNameDecl *typedef_name_decl = typedef_type->getDecl();
m_decls.push_back(typedef_name_decl);
return (NamedDecl *)typedef_name_decl;
} else if (const TagType *tag_type = qual_type->getAs<TagType>()) {
TagDecl *tag_decl = tag_type->getOriginalDecl()->getDefinitionOrSelf();
m_decls.push_back(tag_decl);
return tag_decl;
} else if (const ObjCObjectType *objc_object_type =
qual_type->getAs<ObjCObjectType>()) {
ObjCInterfaceDecl *interface_decl = objc_object_type->getInterface();
m_decls.push_back((NamedDecl *)interface_decl);
return (NamedDecl *)interface_decl;
}
}
return nullptr;
}
void NameSearchContext::AddLookupResult(clang::DeclContextLookupResult result) {
for (clang::NamedDecl *decl : result)
m_decls.push_back(decl);
}
void NameSearchContext::AddNamedDecl(clang::NamedDecl *decl) {
m_decls.push_back(decl);
}