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

192 lines
5.1 KiB
C++

// RUN: %clang_cc1 -fsyntax-only -verify %s
// C++0x [class.access]p6:
// All access controls in [class.access] affect the ability to
// access a class member name from a particular scope. For purposes
// of access control, the base-specifiers of a class and the
// definitions of class members that appear outside of the class
// definition are considered to be within the scope of that
// class. In particular, access controls apply as usual to member
// names accessed as part of a function return type, even though it
// is not possible to determine the access privileges of that use
// without first parsing the rest of the function
// declarator. Similarly, access control for implicit calls to the
// constructors, the conversion functions, or the destructor called
// to create and destroy a static data member is performed as if
// these calls appeared in the scope of the member's class.
struct Public {}; struct Protected {}; struct Private {};
namespace test0 {
class A {
typedef int type; // expected-note {{declared private here}}
type foo();
};
A::type foo() { } // expected-error {{'type' is a private member}}
A::type A::foo() { }
}
// conversion decls
namespace test1 {
class A {
public:
A();
operator Public ();
A(Public);
protected:
operator Protected (); // expected-note {{declared protected here}}
A(Protected); // expected-note {{declared protected here}}
private:
operator Private (); // expected-note {{declared private here}}
A(Private); // expected-note {{declared private here}}
};
void test() {
A a;
Public pub = a;
Protected prot = a; // expected-error {{'operator Protected' is a protected member}}
Private priv = a; // expected-error {{'operator Private' is a private member}}
A apub = pub;
A aprot = prot; // expected-error {{protected constructor}}
A apriv = priv; // expected-error {{private constructor}}
}
}
// PR6967
namespace test2 {
class A {
public:
template <class T> static void set(T &t, typename T::type v) {
t.value = v;
}
template <class T> static typename T::type get(const T &t) {
return t.value;
}
};
class B {
friend class A;
private:
typedef int type;
type value;
};
int test() {
B b;
A::set(b, 0);
return A::get(b);
}
}
namespace test3 {
class Green {}; class Blue {};
// We have to wrap this in a class because a partial specialization
// isn't actually in the context of the template.
struct Outer {
template <class T, class Nat> class A {
};
};
template <class T> class Outer::A<T, typename T::nature> {
public:
static void foo(); // expected-note {{'Outer::A<test3::B, test3::Green>::foo' declared here}}
};
class B {
private: typedef Green nature;
friend class Outer;
};
void test() {
Outer::A<B, Green>::foo();
Outer::A<B, Blue>::foo(); // expected-error {{no member named 'foo' in 'test3::Outer::A<test3::B, test3::Blue>'; did you mean 'Outer::A<test3::B, test3::Green>::foo'?}}
}
}
namespace test4 {
template <class T> class A {
private: typedef int type;
template <class U> friend void foo(U &, typename U::type);
};
template <class U> void foo(U &, typename U::type) {}
void test() {
A<int> a;
foo(a, 0);
}
}
// PR7644
namespace test5 {
class A {
enum Enum { E0, E1, E2 }; // expected-note 4 {{declared private here}}
template <Enum> void foo();
template <Enum> class bar;
};
template <A::Enum en> void A::foo() {}
template <A::Enum en> class A::bar {};
template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}}
template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}}
class B {
template <A::Enum en> void foo() {} // expected-error {{'Enum' is a private member of 'test5::A'}}
template <A::Enum en> class bar {}; // expected-error {{'Enum' is a private member of 'test5::A'}}
};
}
namespace test6 {
class A {
public: class public_inner {};
protected: class protected_inner {};
private: class private_inner {}; // expected-note {{declared private here}}
};
class B : A {
public_inner a;
protected_inner b;
private_inner c; // expected-error {{'private_inner' is a private member of 'test6::A'}}
};
}
// PR9229
namespace test7 {
void foo(int arg[1]);
class A {
void check();
};
class B {
friend class A;
A ins;
};
void A::check() {
void foo(int arg[__builtin_offsetof(B, ins)]);
}
}
namespace test8 {
class A {
typedef void* (A::*UnspecifiedBoolType)() const;
operator UnspecifiedBoolType() const; // expected-note {{implicitly declared private here}}
};
void test(A &a) {
if (a) return; // expected-error-re {{'operator void *(test8::A::*)(){{( __attribute__\(\(thiscall\)\))?}} const' is a private member of 'test8::A'}}
}
}
namespace test9 {
class A {
operator char*() const; // expected-note {{implicitly declared private here}}
};
void test(A &a) {
delete a; // expected-error {{'operator char *' is a private member of 'test9::A'}}
}
}