Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
hiddenfriendtypekey.cpp
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
3
5
6#include <clang/AST/DeclTemplate.h>
7#include <clang/AST/Expr.h>
8#include <clang/AST/Type.h>
9#if __has_include(<clang/UnifiedSymbolResolution/USRGeneration.h>)
10# include <clang/UnifiedSymbolResolution/USRGeneration.h>
11#else
12# include <clang/Index/USRGeneration.h>
13#endif
14#include <llvm/ADT/SmallString.h>
15
16QT_BEGIN_NAMESPACE
17
18namespace {
19
20// A deliberately limited structural encoding, not a C++ deduction engine.
21// Constructor tags have fixed arities; variable atoms are length framed. A
22// declaration USR, a binder index and an integer value inhabit disjoint domains.
23// Atoms: q=qualifiers, n=nominal USR, b=type binder index, f=builtin kind,
24// a=value binder index, v=integer value, c=specialization argument count.
25// Constructors: p=pointer, l/r=lvalue/rvalue reference, s=specialization,
26// t=type argument, i=typed integral argument. These bytes are lookup-local,
27// not a persisted serialization format.
29{
30public:
31 explicit TypeKeyBuilder(const clang::TemplateParameterList &binders) : m_binders(binders) { }
32
33 QByteArray build(clang::QualType type)
34 {
35 return supportedBinders() && writeType(type) ? m_key : QByteArray{};
36 }
37
39 {
40 if (!supportedBinders())
41 return {};
42 atom('c', QByteArray::number(m_binders.size()));
43 for (const auto *binder : m_binders) {
44 if (const auto *value = llvm::dyn_cast<clang::NonTypeTemplateParmDecl>(binder)) {
45 m_key += 'i';
46 if (!writeType(value->getType()))
47 return {};
48 } else {
49 m_key += 't';
50 }
51 }
52 return m_key;
53 }
54
55private:
56 bool supportedBinders() const
57 {
58 // Nested binders, constraints, packs and template-template parameters
59 // need more than this positional convention. Do not guess their meaning.
60 if (m_binders.size() == 0 || m_binders.getDepth() != 0 || m_binders.getRequiresClause())
61 return false;
62 for (const auto *binder : m_binders) {
63 if (const auto *param = llvm::dyn_cast<clang::TemplateTypeParmDecl>(binder)) {
64 if (param->isParameterPack() || param->hasTypeConstraint())
65 return false;
66 } else if (const auto *param = llvm::dyn_cast<clang::NonTypeTemplateParmDecl>(binder)) {
67 if (param->isParameterPack() || param->getType()->getContainedAutoType())
68 return false;
69 } else {
70 return false;
71 }
72 }
73 return true;
74 }
75
76 void atom(char tag, const QByteArray &value)
77 {
78 m_key += tag;
79 m_key += QByteArray::number(value.size());
80 m_key += ':';
81 m_key += value;
82 }
83
84 bool nominal(const clang::Decl *decl)
85 {
86 llvm::SmallString<128> usr;
87 if (!decl || clang::index::generateUSRForDecl(decl->getCanonicalDecl(), usr))
88 return false;
89 atom('n', QByteArray(usr.data(), usr.size()));
90 return true;
91 }
92
93 bool integral(const llvm::APSInt &value, clang::QualType type)
94 {
95 m_key += 'i';
96 if (!writeType(type))
97 return false;
98 llvm::SmallString<32> digits;
99 value.toString(digits, 10);
100 atom('v', QByteArray(digits.data(), digits.size()));
101 return true;
102 }
103
104 bool argument(const clang::TemplateArgument &arg)
105 {
106 switch (arg.getKind()) {
107 case clang::TemplateArgument::Type:
108 m_key += 't';
109 return writeType(arg.getAsType());
110 case clang::TemplateArgument::Integral:
111 return integral(arg.getAsIntegral(), arg.getIntegralType());
112 case clang::TemplateArgument::Expression: {
113 const auto *expr = arg.getAsExpr()->IgnoreParens();
114 if (const auto *ref = llvm::dyn_cast<clang::DeclRefExpr>(expr)) {
115 if (const auto *param = llvm::dyn_cast<clang::NonTypeTemplateParmDecl>(ref->getDecl())) {
116 const unsigned index = param->getIndex();
117 // Clang can reuse a canonical dependent expression from a
118 // different declaration at the same binder depth/index.
119 if (param->getDepth() != m_binders.getDepth() || index >= m_binders.size()
120 || !llvm::isa<clang::NonTypeTemplateParmDecl>(m_binders.getParam(index)))
121 return false;
122 atom('a', QByteArray::number(index));
123 return writeType(param->getType());
124 }
125 }
126 // Literal spellings are irrelevant; their type and evaluated value
127 // are not. Dependent expressions other than a binder are unsupported.
128 clang::Expr::EvalResult result;
129 if (!expr->isValueDependent()
130 && expr->EvaluateAsInt(result, m_binders.getParam(0)->getASTContext()))
131 return integral(result.Val.getInt(), expr->getType());
132 return false;
133 }
134 default:
135 return false;
136 }
137 }
138
139 bool specialization(const clang::TemplateDecl *decl, llvm::ArrayRef<clang::TemplateArgument> args)
140 {
141 m_key += 's';
142 if (!nominal(decl))
143 return false;
144 atom('c', QByteArray::number(args.size()));
145 for (const auto &arg : args) {
146 if (!argument(arg))
147 return false;
148 }
149 return true;
150 }
151
152 bool writeType(clang::QualType type)
153 {
154 if (type.isNull())
155 return false;
156 type = type.getCanonicalType(); // Desugar aliases, including reference aliases.
157 atom('q', QByteArray::number(type.getLocalQualifiers().getAsOpaqueValue()));
158 const auto *raw = type.getTypePtr();
159 if (const auto *param = llvm::dyn_cast<clang::TemplateTypeParmType>(raw)) {
160 const unsigned index = param->getIndex();
161 if (param->getDepth() != m_binders.getDepth() || index >= m_binders.size()
162 || !llvm::isa<clang::TemplateTypeParmDecl>(m_binders.getParam(index)))
163 return false;
164 atom('b', QByteArray::number(index));
165 return true;
166 }
167 if (const auto *builtin = llvm::dyn_cast<clang::BuiltinType>(raw)) {
168 if (builtin->isPlaceholderType() || builtin->isDependentType())
169 return false;
170 atom('f', QByteArray::number(builtin->getKind()));
171 return true;
172 }
173 if (const auto *pointer = llvm::dyn_cast<clang::PointerType>(raw)) {
174 m_key += 'p';
175 return writeType(pointer->getPointeeType());
176 }
177 if (const auto *ref = llvm::dyn_cast<clang::ReferenceType>(raw)) {
178 m_key += llvm::isa<clang::LValueReferenceType>(ref) ? 'l' : 'r';
179 return writeType(ref->getPointeeType());
180 }
181 if (const auto *injected = llvm::dyn_cast<clang::InjectedClassNameType>(raw)) {
182#if LIBCLANG_VERSION_MAJOR >= 22
183 const auto *decl = injected->getDecl()->getDescribedClassTemplate();
184 return decl && specialization(decl, decl->getInjectedTemplateArgs(decl->getASTContext()));
185#else
186 const auto *spec = injected->getInjectedSpecializationType()->getAs<clang::TemplateSpecializationType>();
187 return spec && specialization(spec->getTemplateName().getAsTemplateDecl(), spec->template_arguments());
188#endif
189 }
190 if (const auto *spec = llvm::dyn_cast<clang::TemplateSpecializationType>(raw))
191 return specialization(spec->getTemplateName().getAsTemplateDecl(), spec->template_arguments());
192 if (const auto *record = llvm::dyn_cast<clang::RecordType>(raw)) {
193 const auto *decl = record->getDecl();
194 if (const auto *spec = llvm::dyn_cast<clang::ClassTemplateSpecializationDecl>(decl))
195 return specialization(spec->getSpecializedTemplate(), spec->getTemplateArgs().asArray());
196 // A dependent nested record needs its enclosing arguments too.
197 return !decl->isDependentContext() && nominal(decl);
198 }
199 if (const auto *enumeration = llvm::dyn_cast<clang::EnumType>(raw))
200 return !enumeration->getDecl()->isDependentContext() && nominal(enumeration->getDecl());
201 return false;
202 }
203
204 const clang::TemplateParameterList &m_binders;
205 QByteArray m_key;
206};
207
208} // namespace
209
210QByteArray hiddenFriendTypeKey(clang::QualType type, const clang::TemplateParameterList &binders)
211{
212 return TypeKeyBuilder(binders).build(type);
213}
214
215QByteArray templateParameterKey(const clang::TemplateParameterList &binders)
216{
217 return TypeKeyBuilder(binders).buildTemplateParameters();
218}
219
220QT_END_NAMESPACE
TypeKeyBuilder(const clang::TemplateParameterList &binders)
QByteArray build(clang::QualType type)
QByteArray hiddenFriendTypeKey(clang::QualType type, const clang::TemplateParameterList &binders)
QByteArray templateParameterKey(const clang::TemplateParameterList &binders)
#define __has_include(x)