33 Commits

Author SHA1 Message Date
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
Younan Zhang
040e7ad828
[Clang] Consider default template arguments when synthesizing CTAD guides (#147675)
We copy arguments from different template parameter lists depending on
the deducibility of the template parameters. In particular, we may lose
the default template argument from the original alias declaration, and
this patch helps preserve that.

Fixes https://github.com/llvm/llvm-project/issues/133132
2025-07-14 14:45:23 +08:00
Younan Zhang
e04e140adb
[Clang] Reapply CWG2369 "Ordering between constraints and substitution" (#122423)
The previous approach broke code generation for the MS ABI due to an
unintended code path during constraint substitution. This time we
address the issue by inspecting the evaluation contexts and thereby
avoiding that code path.

This reapplies 96eced624 (#102857).
2025-06-02 17:10:07 +08:00
Younan Zhang
67d907a72f
[Clang] Reset ArgPackSubstIndex before rewriting CTAD template parameters (#141741)
032ad59 taught the instantiator to expand template argument packs for
rewrite. However we might already be in a pack expansion when we
synthesizing the CTAD guide, so we reset the ArgPackSubstIndex to ensure
it doesn't get confused.

No release note because this is a regression in Clang 21.

Fixes https://github.com/llvm/llvm-project/issues/141425

---------

Co-authored-by: Erich Keane <ekeane@nvidia.com>
2025-05-28 23:33:43 +08:00
Younan Zhang
de528d689f
[Clang] Handle default template arguments for alias CTAD guides (#134807)
It's possible that some deduced template arguments come from default
arguments, not just from the return type. So we need to recursively
visit the default arguments of the parameter if it's referenced, thereby
the template parameter referenced by the defualt arguments could come
along to the synthesized deduction guide.

Fixes https://github.com/llvm/llvm-project/issues/134471
2025-04-17 10:08:50 +08:00
Matheus Izvekov
cfee056b4e
[clang] NFC: introduce UnsignedOrNone as a replacement for std::optional<unsigned> (#134142)
This introduces a new class 'UnsignedOrNone', which models a lite
version of `std::optional<unsigned>`, but has the same size as
'unsigned'.

This replaces most uses of `std::optional<unsigned>`, and similar
schemes utilizing 'int' and '-1' as sentinel.

Besides the smaller size advantage, this is simpler to serialize, as its
internal representation is a single unsigned int as well.
2025-04-03 14:27:18 -03:00
Matheus Izvekov
49fd0bf35d
[clang] support pack expansions for trailing requires clauses (#133190) 2025-04-03 12:36:15 -03:00
Younan Zhang
38d71c9bdc
[Clang] Fix the assertion condition after b8d1f3d6 (#132669)
Thanks to the example provided by MagentaTreehouse, I realized the
assertion I added in b8d1f3d6 didn't cover all valid cases like, when
inheriting from a class template specialization, the source of a
synthesized template parameter might be an implicit specialization,
whose inner function template is thus living at depth 0, for which we
don’t want it to overflow too.

I've decided to remove that assertion because I don't think it's
particularly useful: we're checking whether Depth = 0 parameters come
from function templates whose parents contribute no template parameters
to the depth, which is redundant given what the template depth already
means.

This also incorporates a drive-by fix for
https://github.com/llvm/llvm-project/pull/132061#discussion_r2008756718,
which I somehow missed.
2025-03-24 16:46:48 +08:00
Younan Zhang
c5d4dfb291
[Clang][NFC] Use MarkUsedTemplateParameters in building alias CTAD guides (#132369)
It turns out that TemplateParamsReferencedInTemplateArgumentList() and
MarkUsedTemplateParameters() have the similar goal, so let's drop the
hand-written ASTVisitor.
2025-03-22 23:04:46 +08:00
Younan Zhang
032ad590d6
[Clang] Fix various bugs in alias CTAD transform (#132061) 2025-03-22 22:55:58 +08:00
Younan Zhang
b8d1f3d627
[Clang] Fix an integer overflow issue in computing CTAD's parameter depth (#128704)
There were some cases where we computed incorrect template parameter
depths for synthesized CTAD, invalid as they might be, we still
shouldn't crash anyway.

Technically the only scenario in which the inner function template's
depth is 0 is when it lives within an explicit template specialization,
where the template parameter list is empty.

Fixes https://github.com/llvm/llvm-project/issues/128691
2025-02-26 16:54:19 +08:00
Haojian Wu
b963d3768c
[clang] CTAD: Remove an incorrect assertion in BuildDeductionGuideForTypeAlias (#126532)
Fixes #125821

The assertion was too strict, as Clang can reach this code path when
recursively generating deduction guides for alias templates. See the
detailed explanation
[here](https://github.com/llvm/llvm-project/issues/125821#issuecomment-2639130893).

No release notes needed, as there is no behavior change in release
builds.
2025-02-13 12:15:24 +01:00
Haojian Wu
4d2a1bf563
[clang] CTAD alias: Respect explicit deduction guides defined after the first use of the alias template. (#125478)
Fixes #103016

This is the last missing piece for the C++20 CTAD alias feature. No
release note being added in this PR yet, I will send out a follow-up
patch to mark this feature done.

(Since the release 20 branch is cut, I think we should target on
clang21).
2025-02-10 15:05:49 +01:00
Haojian Wu
da2b415b19 [clang] Add tracking source deduction guide for the explicitly-written
deduction guide.

We miss this case in the original f94c481543bdd3b11a668ad78d46593cf974788f commit.
2025-02-02 23:06:25 +01:00
Haojian Wu
7612dcc6e8 [clang] NFC, add a "continue" bailout in the for-loop of
DeclareImplicitDeductionGuidesForTypeAlias.

This improves the code readability.
2025-02-01 10:07:40 +01:00
antangelo
f94c481543
[clang] Track source deduction guide for alias template deduction guides (#123875)
For deduction guides generated from alias template CTAD, store the
deduction guide they were originated from. The source kind is also
maintained for future expansion in CTAD from inherited constructors.

This tracking is required to determine whether an alias template already
has a deduction guide corresponding to some deduction guide on the
original template, in order to support deduction guides for the alias
from deduction guides declared after the initial usage.
2025-01-27 18:59:12 -05:00
Younan Zhang
b46fcb9fa3
[Clang] Implement CWG 2628 "Implicit deduction guides should propagate constraints" (#111143)
Closes https://github.com/llvm/llvm-project/issues/98592
2025-01-23 10:53:00 +08:00
Younan Zhang
fd4f94ddbf
[Clang] Correct the order of substituted arguments in CTAD alias guides (#123022)
We missed a case of type constraints referencing deduced template
parameters when constructing a deduction guide for the type alias. This
patch fixes the issue by swapping the order of constructing 'template
arguments not appearing in the type alias parameters' and 'template
arguments that are not yet deduced'.

Fixes https://github.com/llvm/llvm-project/issues/122134
2025-01-16 16:37:57 +08:00
Younan Zhang
3972ed5708
Revert "[Clang] Implement CWG2369 "Ordering between constraints and substitution"" (#122130)
Unfortunately that breaks some code on Windows when lambdas come into
play, as reported in
https://github.com/llvm/llvm-project/pull/102857#issuecomment-2577861178

This reverts commit 96eced624e0f120155256033fdcb8342e7e58d6e.
2025-01-08 18:27:25 +01:00
Younan Zhang
96eced624e
[Clang] Implement CWG2369 "Ordering between constraints and substitution" (#102857)
This patch partially implements CWG2369 for non-lambda-constrained
functions.

Lambdas are left intact at this point because we need extra work to
correctly instantiate captures before the function instantiation.

As a premise of CWG2369, this patch also implements CWG2770 to ensure
the function parameters are instantiated on demand.

Closes https://github.com/llvm/llvm-project/issues/54440
2025-01-05 10:50:52 +08:00
Kazu Hirata
46d750be2e
[Sema] Remove unused includes (NFC) (#116461)
Identified with misc-include-cleaner.
2024-11-16 07:37:33 -08:00
Sirraide
dde802b153
[Clang] [NFC] Refactor AST visitors in Sema and the static analyser to use DynamicRecursiveASTVisitor (#115144)
This pr refactors all recursive AST visitors in `Sema`, `Analyze`, and
`StaticAnalysis` to inherit from DRAV instead. This is over half of the
visitors that inherit from RAV directly.

See also #115132, #110040, #93462

LLVM Compile-Time Tracker link for this branch:
https://llvm-compile-time-tracker.com/compare.php?from=5adb5c05a2e9f31385fbba8b0436cbc07d91a44d&to=b58e589a86c06ba28d4d90613864d10be29aa5ba&stat=instructions%3Au
2024-11-15 08:04:08 +01:00
Krystian Stasiowski
44ab3805b5
Revert "Reapply "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585, #111173)" (#111852)" (#115159)
This reverts commit 2bb3d3a3f32ffaef3d9b6a27db7f1941f0cb1136.
2024-11-06 09:25:29 -05:00
Jay Foad
4dd55c567a
[clang] Use {} instead of std::nullopt to initialize empty ArrayRef (#109399)
Follow up to #109133.
2024-10-24 10:23:40 +01:00
Krystian Stasiowski
2bb3d3a3f3
Reapply "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585, #111173)" (#111852)
This patch reapplies #111173, fixing a bug when instantiating dependent
expressions that name a member template that is later explicitly
specialized for a class specialization that is implicitly instantiated.

The bug is addressed by adding the `hasMemberSpecialization` function,
which return `true` if _any_ redeclaration is a member specialization.
This is then used when determining the instantiation pattern for a
specialization of a template, and when collecting template arguments for
a specialization of a template.
2024-10-11 14:08:06 -04:00
Younan Zhang
0bc02b999a
[Clang] Instantiate Typedefs referenced by type alias deduction guides (#111804)
TypedefNameDecl referenced by a synthesized CTAD guide for type aliases
was not transformed previously, resulting in a substitution failure in
BuildDeductionGuideForTypeAlias() when substituting into the
right-hand-side deduction guide.

This patch fixes it in the way we have been doing since
https://reviews.llvm.org/D80743. We transform all the function
parameters, parenting referenced TypedefNameDecls with the
CXXDeductionGuideDecl. Then we instantiate these declarations in
FindInstantiatedDecl() as we build up the eventual deduction guide,
using the mechanism introduced in D80743

Fixes #111508
2024-10-11 10:31:27 +08:00
Krystian Stasiowski
1dff3309fd
Revert "Reapply "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585)" (#111173)" (#111766)
This reverts commit 4da8ac34f76e707ab94380b94f616457cfd2cb83.
2024-10-09 17:49:32 -04:00
Krystian Stasiowski
4da8ac34f7
Reapply "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585)" (#111173)
Reapplies #106585, fixing an issue where non-dependent names of member
templates appearing prior to that member template being explicitly
specialized for an implicitly instantiated class template specialization
would incorrectly use the definition of the explicitly specialized
member template.
2024-10-08 10:14:09 -04:00
Martin Storsjö
1818ca5c4a Revert "[Clang][Sema] Refactor collection of multi-level template argument lists (#106585)"
This reverts commit cdd71d61664b63ae57bdba9ee0d891f78ef79c07 (and
30adb43c897a45c18d7dd163fb4ff40c915fc488).

This change broke compiling Qt, see
https://github.com/llvm/llvm-project/pull/106585#issuecomment-2365309463
for details.
2024-09-21 23:24:49 +03:00
Krystian Stasiowski
cdd71d6166
[Clang][Sema] Refactor collection of multi-level template argument lists (#106585)
Currently, clang rejects the following explicit specialization of `f`
due to the constraints not being equivalent:
```
template<typename T>
struct A
{
    template<bool B>
    void f() requires B;
};

template<>
template<bool B>
void A<int>::f() requires B { }
```
This happens because, in most cases, we do not set the flag indicating
whether a `RedeclarableTemplate` is an explicit specialization of a
member of an implicitly instantiated class template specialization until
_after_ we compare constraints for equivalence. This patch addresses the
issue (and a number of other issues) by:
- storing the flag indicating whether a declaration is a member
specialization on a per declaration basis, and
- significantly refactoring `Sema::getTemplateInstantiationArgs` so we
collect the right set of template argument in all cases.

Many of our declaration matching & constraint evaluation woes can be
traced back to bugs in `Sema::getTemplateInstantiationArgs`. This
change/refactor should fix a lot of them. It also paves the way for
fixing #101330 and #105462 per my suggestion in #102267 (which I have
implemented on top of this patch but will merge in a subsequent PR).
2024-09-20 14:57:40 -04:00
Younan Zhang
bb064535bd
[Clang][CTAD][NFC] Unify transformTemplateParameter() (#100865)
We ended up having two transformTemplateParameter() after CTAD for type
aliases was landed. This patch cleans them up and allows them to share
one implementation.

As a bonus, this also uses getDepthAndIndex() in preference to
getTemplateParameter{Depth,Index}().
2024-07-28 19:34:23 +08:00
Younan Zhang
c7bfc41860
[Clang][NFCI] Prefer non-canonical template arguments for synthesized CTAD guides (#99840)
This seems to be low-hanging fruit: We could remove all calls to
`Context.getCanonicalTemplateArgument()` and gain a better
diagnostic/AST.

The non-canonical template arguments shouldn't make a difference when
synthesizing a CTAD guide, so this is intended to be an NFC.

Closes https://github.com/llvm/llvm-project/issues/79798
2024-07-22 16:37:05 +08:00
Haojian Wu
18c70b070b
[clang] Refactor: Move CTAD code from SemaTemplate.cpp to a dedicated file, NFC (#98524)
Split out the deduction guide related code from SemaTemplate.cpp to a
dedicated file.

These code has grown significantly, and moving it to a separate file
will improve code organization.
2024-07-15 08:43:02 +02:00