llvm-project/clang/test/SemaTemplate/current-instantiation.cpp
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

250 lines
7.2 KiB
C++

// RUN: %clang_cc1 -fsyntax-only -verify %s
// This test concerns the identity of dependent types within the
// canonical type system, specifically focusing on the difference
// between members of the current instantiation and members of an
// unknown specialization. This considers C++ [temp.type], which
// specifies type equivalence within a template, and C++0x
// [temp.dep.type], which defines what it means to be a member of the
// current instantiation.
template<typename T, typename U>
struct X0 {
typedef T T_type;
typedef U U_type;
void f0(T&); // expected-note{{previous}}
void f0(typename X0::U_type&);
void f0(typename X0::T_type&); // expected-error{{redecl}}
void f1(T&); // expected-note{{previous}}
void f1(typename X0::U_type&);
void f1(typename X0<T, U>::T_type&); // expected-error{{redecl}}
void f2(T&); // expected-note{{previous}}
void f2(typename X0::U_type&);
void f2(typename X0<T_type, U_type>::T_type&); // expected-error{{redecl}}
void f3(T&); // expected-note{{previous}}
void f3(typename X0::U_type&);
void f3(typename ::X0<T_type, U_type>::T_type&); // expected-error{{redecl}}
struct X1 {
typedef T my_T_type;
void g0(T&); // expected-note{{previous}}
void g0(typename X0::U_type&);
void g0(typename X0::T_type&); // expected-error{{redecl}}
void g1(T&); // expected-note{{previous}}
void g1(typename X0::U_type&);
void g1(typename X0<T, U>::T_type&); // expected-error{{redecl}}
void g2(T&); // expected-note{{previous}}
void g2(typename X0::U_type&);
void g2(typename X0<T_type, U_type>::T_type&); // expected-error{{redecl}}
void g3(T&); // expected-note{{previous}}
void g3(typename X0::U_type&);
void g3(typename ::X0<T_type, U_type>::T_type&); // expected-error{{redecl}}
void g4(T&); // expected-note{{previous}}
void g4(typename X0::U_type&);
void g4(typename X1::my_T_type&); // expected-error{{redecl}}
void g5(T&); // expected-note{{previous}}
void g5(typename X0::U_type&);
void g5(typename X0::X1::my_T_type&); // expected-error{{redecl}}
void g6(T&); // expected-note{{previous}}
void g6(typename X0::U_type&);
void g6(typename X0<T, U>::X1::my_T_type&); // expected-error{{redecl}}
void g7(T&); // expected-note{{previous}}
void g7(typename X0::U_type&);
void g7(typename ::X0<typename X1::my_T_type, U_type>::X1::my_T_type&); // expected-error{{redecl}}
void g8(T&); // expected-note{{previous}}
void g8(typename X0<U, T_type>::T_type&);
void g8(typename ::X0<typename X0<T_type, U>::X1::my_T_type, U_type>::X1::my_T_type&); // expected-error{{redecl}}
};
};
template<typename T, typename U>
struct X0<T*, U*> {
typedef T T_type;
typedef U U_type;
typedef T* Tptr;
typedef U* Uptr;
void f0(T&); // expected-note{{previous}}
void f0(typename X0::U_type&);
void f0(typename X0::T_type&); // expected-error{{redecl}}
void f1(T&); // expected-note{{previous}}
void f1(typename X0::U_type&);
void f1(typename X0<T*, U*>::T_type&); // expected-error{{redecl}}
void f2(T&); // expected-note{{previous}}
void f2(typename X0::U_type&);
void f2(typename X0<T_type*, U_type*>::T_type&); // expected-error{{redecl}}
void f3(T&); // expected-note{{previous}}
void f3(typename X0::U_type&);
void f3(typename ::X0<T_type*, U_type*>::T_type&); // expected-error{{redecl}}
void f4(T&); // expected-note{{previous}}
void f4(typename X0::U_type&);
void f4(typename ::X0<Tptr, Uptr>::T_type&); // expected-error{{redecl}}
void f5(X0*); // expected-note{{previous}}
void f5(::X0<T, U>*);
void f5(::X0<T*, U*>*); // expected-error{{redecl}}
struct X2 {
typedef T my_T_type;
void g0(T&); // expected-note{{previous}}
void g0(typename X0::U_type&);
void g0(typename X0::T_type&); // expected-error{{redecl}}
void g1(T&); // expected-note{{previous}}
void g1(typename X0::U_type&);
void g1(typename X0<T*, U*>::T_type&); // expected-error{{redecl}}
void g2(T&); // expected-note{{previous}}
void g2(typename X0::U_type&);
void g2(typename X0<T_type*, U_type*>::T_type&); // expected-error{{redecl}}
void g3(T&); // expected-note{{previous}}
void g3(typename X0::U_type&);
void g3(typename ::X0<T_type*, U_type*>::T_type&); // expected-error{{redecl}}
void g4(T&); // expected-note{{previous}}
void g4(typename X0::U_type&);
void g4(typename X2::my_T_type&); // expected-error{{redecl}}
void g5(T&); // expected-note{{previous}}
void g5(typename X0::U_type&);
void g5(typename X0::X2::my_T_type&); // expected-error{{redecl}}
void g6(T&); // expected-note{{previous}}
void g6(typename X0::U_type&);
void g6(typename X0<T*, U*>::X2::my_T_type&); // expected-error{{redecl}}
void g7(T&); // expected-note{{previous}}
void g7(typename X0::U_type&);
void g7(typename ::X0<typename X2::my_T_type*, U_type*>::X2::my_T_type&); // expected-error{{redecl}}
void g8(T&); // expected-note{{previous}}
void g8(typename X0<U, T_type>::T_type&);
void g8(typename ::X0<typename X0<T_type*, U*>::X2::my_T_type*, U_type*>::X2::my_T_type&); // expected-error{{redecl}}
};
};
template<typename T>
struct X1 {
static int *a;
void f(float *b) {
X1<T>::a = b; // expected-error{{incompatible}}
X1<T*>::a = b;
}
};
namespace ConstantInCurrentInstantiation {
template<typename T>
struct X {
static const int value = 2;
static int array[value];
};
template<typename T> const int X<T>::value;
template<typename T>
int X<T>::array[X<T>::value] = { 1, 2 };
}
namespace Expressions {
template <bool b>
struct Bool {
enum anonymous_enum { value = b };
};
struct True : public Bool<true> {};
struct False : public Bool<false> {};
template <typename T1, typename T2>
struct Is_Same : public False {};
template <typename T>
struct Is_Same<T, T> : public True {};
template <bool b, typename T = void>
struct Enable_If {};
template <typename T>
struct Enable_If<true, T> {
typedef T type;
};
template <typename T>
class Class {
public:
template <typename U>
typename Enable_If<Is_Same<U, Class>::value, void>::type
foo();
};
template <typename T>
template <typename U>
typename Enable_If<Is_Same<U, Class<T> >::value, void>::type
Class<T>::foo() {}
}
namespace PR9255 {
template<typename T>
class X0 {
public:
class Inner1;
class Inner2 {
public:
void f()
{
Inner1::f.g();
}
};
};
}
namespace rdar10194295 {
template<typename XT>
class X {
public:
enum Enum { Yes, No };
template<Enum> void foo();
template<Enum> class Inner;
};
template<typename XT>
template<typename X<XT>::Enum>
void X<XT>::foo()
{
}
template<typename XT>
template<typename X<XT>::Enum>
class X<XT>::Inner { };
}
namespace RebuildDependentScopeDeclRefExpr {
template<int> struct N {};
template<typename T> struct X {
static const int thing = 0;
N<thing> data();
N<thing> foo();
};
template<typename T> N<X<T>::thing> X<T>::data() {}
// FIXME: We should issue a typo-correction here.
template<typename T> N<X<T>::think> X<T>::foo() {} // expected-error {{no member named 'think' in 'RebuildDependentScopeDeclRefExpr::X<T>'}}
}