Skip to content

Commit cbba960

Browse files
authored
Revert "[clang] Improved canonicalization for template specialization types" (#135354)
Reverts #135119 because of the assert in ASTContext.cpp, line 5619. See #135352 for details.
1 parent 1c05c61 commit cbba960

28 files changed

+422
-448
lines changed

clang-tools-extra/clangd/AST.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,7 @@ QualType declaredType(const TypeDecl *D) {
439439
if (const auto *CTSD = llvm::dyn_cast<ClassTemplateSpecializationDecl>(D))
440440
if (const auto *Args = CTSD->getTemplateArgsAsWritten())
441441
return Context.getTemplateSpecializationType(
442-
TemplateName(CTSD->getSpecializedTemplate()), Args->arguments(),
443-
/*CanonicalArgs=*/std::nullopt);
442+
TemplateName(CTSD->getSpecializedTemplate()), Args->arguments());
444443
return Context.getTypeDeclType(D);
445444
}
446445

clang/docs/ReleaseNotes.rst

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,6 @@ Improvements to Clang's diagnostics
301301
- Clang now better preserves the sugared types of pointers to member.
302302
- Clang now better preserves the presence of the template keyword with dependent
303303
prefixes.
304-
- Clang now in more cases avoids printing 'type-parameter-X-X' instead of the name of
305-
the template parameter.
306304
- Clang now respects the current language mode when printing expressions in
307305
diagnostics. This fixes a bunch of `bool` being printed as `_Bool`, and also
308306
a bunch of HLSL types being printed as their C++ equivalents.

clang/include/clang/AST/ASTContext.h

Lines changed: 15 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,9 @@ class ASTContext : public RefCountedBase<ASTContext> {
367367
const ASTContext&>
368368
CanonTemplateTemplateParms;
369369

370+
TemplateTemplateParmDecl *
371+
getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const;
372+
370373
/// The typedef for the __int128_t type.
371374
mutable TypedefDecl *Int128Decl = nullptr;
372375

@@ -1808,26 +1811,22 @@ class ASTContext : public RefCountedBase<ASTContext> {
18081811
bool ParameterPack,
18091812
TemplateTypeParmDecl *ParmDecl = nullptr) const;
18101813

1811-
QualType getCanonicalTemplateSpecializationType(
1812-
TemplateName T, ArrayRef<TemplateArgument> CanonicalArgs) const;
1814+
QualType getTemplateSpecializationType(TemplateName T,
1815+
ArrayRef<TemplateArgument> Args,
1816+
QualType Canon = QualType()) const;
18131817

18141818
QualType
1815-
getTemplateSpecializationType(TemplateName T,
1816-
ArrayRef<TemplateArgument> SpecifiedArgs,
1817-
ArrayRef<TemplateArgument> CanonicalArgs,
1818-
QualType Underlying = QualType()) const;
1819+
getCanonicalTemplateSpecializationType(TemplateName T,
1820+
ArrayRef<TemplateArgument> Args) const;
18191821

1820-
QualType
1821-
getTemplateSpecializationType(TemplateName T,
1822-
ArrayRef<TemplateArgumentLoc> SpecifiedArgs,
1823-
ArrayRef<TemplateArgument> CanonicalArgs,
1824-
QualType Canon = QualType()) const;
1822+
QualType getTemplateSpecializationType(TemplateName T,
1823+
ArrayRef<TemplateArgumentLoc> Args,
1824+
QualType Canon = QualType()) const;
18251825

1826-
TypeSourceInfo *getTemplateSpecializationTypeInfo(
1827-
TemplateName T, SourceLocation TLoc,
1828-
const TemplateArgumentListInfo &SpecifiedArgs,
1829-
ArrayRef<TemplateArgument> CanonicalArgs,
1830-
QualType Canon = QualType()) const;
1826+
TypeSourceInfo *
1827+
getTemplateSpecializationTypeInfo(TemplateName T, SourceLocation TLoc,
1828+
const TemplateArgumentListInfo &Args,
1829+
QualType Canon = QualType()) const;
18311830

18321831
QualType getParenType(QualType NamedType) const;
18331832

@@ -2943,21 +2942,6 @@ class ASTContext : public RefCountedBase<ASTContext> {
29432942
TemplateArgument getCanonicalTemplateArgument(const TemplateArgument &Arg)
29442943
const;
29452944

2946-
/// Canonicalize the given template argument list.
2947-
///
2948-
/// Returns true if any arguments were non-canonical, false otherwise.
2949-
bool
2950-
canonicalizeTemplateArguments(MutableArrayRef<TemplateArgument> Args) const;
2951-
2952-
/// Canonicalize the given TemplateTemplateParmDecl.
2953-
TemplateTemplateParmDecl *
2954-
getCanonicalTemplateTemplateParmDecl(TemplateTemplateParmDecl *TTP) const;
2955-
2956-
TemplateTemplateParmDecl *findCanonicalTemplateTemplateParmDeclInternal(
2957-
TemplateTemplateParmDecl *TTP) const;
2958-
TemplateTemplateParmDecl *insertCanonicalTemplateTemplateParmDeclInternal(
2959-
TemplateTemplateParmDecl *CanonTTP) const;
2960-
29612945
/// Type Query functions. If the type is an instance of the specified class,
29622946
/// return the Type pointer for the underlying maximally pretty type. This
29632947
/// is a member of ASTContext because this may need to do some amount of

clang/include/clang/AST/PropertiesBase.td

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -877,14 +877,11 @@ let Class = PropertyTypeCase<TemplateArgument, "Expression"> in {
877877
def : Property<"expression", ExprRef> {
878878
let Read = [{ node.getAsExpr() }];
879879
}
880-
def : Property<"IsCanonical", Bool> {
881-
let Read = [{ node.isCanonicalExpr() }];
882-
}
883880
def : Property<"isDefaulted", Bool> {
884881
let Read = [{ node.getIsDefaulted() }];
885882
}
886883
def : Creator<[{
887-
return TemplateArgument(expression, IsCanonical, isDefaulted);
884+
return TemplateArgument(expression, isDefaulted);
888885
}]>;
889886
}
890887
let Class = PropertyTypeCase<TemplateArgument, "Pack"> in {

clang/include/clang/AST/TemplateBase.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -167,8 +167,6 @@ class TemplateArgument {
167167
unsigned Kind : 31;
168168
LLVM_PREFERRED_TYPE(bool)
169169
unsigned IsDefaulted : 1;
170-
LLVM_PREFERRED_TYPE(bool)
171-
unsigned IsCanonicalExpr : 1;
172170
uintptr_t V;
173171
};
174172
union {
@@ -189,8 +187,7 @@ class TemplateArgument {
189187

190188
public:
191189
/// Construct an empty, invalid template argument.
192-
constexpr TemplateArgument()
193-
: TypeOrValue{Null, /*IsDefaulted=*/0, /*IsCanonicalExpr=*/0, /*V=*/0} {}
190+
constexpr TemplateArgument() : TypeOrValue({Null, 0, /* IsDefaulted */ 0}) {}
194191

195192
/// Construct a template type argument.
196193
TemplateArgument(QualType T, bool isNullPtr = false,
@@ -265,10 +262,9 @@ class TemplateArgument {
265262
/// This form of template argument only occurs in template argument
266263
/// lists used for dependent types and for expression; it will not
267264
/// occur in a non-dependent, canonical template argument list.
268-
TemplateArgument(Expr *E, bool IsCanonical, bool IsDefaulted = false) {
265+
explicit TemplateArgument(Expr *E, bool IsDefaulted = false) {
269266
TypeOrValue.Kind = Expression;
270267
TypeOrValue.IsDefaulted = IsDefaulted;
271-
TypeOrValue.IsCanonicalExpr = IsCanonical;
272268
TypeOrValue.V = reinterpret_cast<uintptr_t>(E);
273269
}
274270

@@ -411,11 +407,6 @@ class TemplateArgument {
411407
return reinterpret_cast<Expr *>(TypeOrValue.V);
412408
}
413409

414-
bool isCanonicalExpr() const {
415-
assert(getKind() == Expression && "Unexpected kind");
416-
return TypeOrValue.IsCanonicalExpr;
417-
}
418-
419410
/// Iterator that traverses the elements of a template argument pack.
420411
using pack_iterator = const TemplateArgument *;
421412

clang/include/clang/AST/Type.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6676,9 +6676,10 @@ class TemplateSpecializationType : public Type, public llvm::FoldingSetNode {
66766676
/// replacement must, recursively, be one of these).
66776677
TemplateName Template;
66786678

6679-
TemplateSpecializationType(TemplateName T, bool IsAlias,
6679+
TemplateSpecializationType(TemplateName T,
66806680
ArrayRef<TemplateArgument> Args,
6681-
QualType Underlying);
6681+
QualType Canon,
6682+
QualType Aliased);
66826683

66836684
public:
66846685
/// Determine whether any of the given template arguments are dependent.
@@ -6746,7 +6747,7 @@ class TemplateSpecializationType : public Type, public llvm::FoldingSetNode {
67466747

67476748
void Profile(llvm::FoldingSetNodeID &ID, const ASTContext &Ctx);
67486749
static void Profile(llvm::FoldingSetNodeID &ID, TemplateName T,
6749-
ArrayRef<TemplateArgument> Args, QualType Underlying,
6750+
ArrayRef<TemplateArgument> Args,
67506751
const ASTContext &Context);
67516752

67526753
static bool classof(const Type *T) {

clang/include/clang/AST/TypeProperties.td

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -737,19 +737,39 @@ let Class = DependentAddressSpaceType in {
737737
}
738738

739739
let Class = TemplateSpecializationType in {
740+
def : Property<"dependent", Bool> {
741+
let Read = [{ node->isDependentType() }];
742+
}
740743
def : Property<"templateName", TemplateName> {
741744
let Read = [{ node->getTemplateName() }];
742745
}
743-
def : Property<"args", Array<TemplateArgument>> {
746+
def : Property<"templateArguments", Array<TemplateArgument>> {
744747
let Read = [{ node->template_arguments() }];
745748
}
746-
def : Property<"UnderlyingType", QualType> {
747-
let Read = [{ node->isCanonicalUnqualified() ? QualType() :
748-
node->desugar() }];
749+
def : Property<"underlyingType", Optional<QualType>> {
750+
let Read = [{
751+
node->isTypeAlias()
752+
? std::optional<QualType>(node->getAliasedType())
753+
: node->isCanonicalUnqualified()
754+
? std::nullopt
755+
: std::optional<QualType>(node->getCanonicalTypeInternal())
756+
}];
749757
}
750758

751759
def : Creator<[{
752-
return ctx.getTemplateSpecializationType(templateName, args, std::nullopt, UnderlyingType);
760+
QualType result;
761+
if (!underlyingType) {
762+
result = ctx.getCanonicalTemplateSpecializationType(templateName,
763+
templateArguments);
764+
} else {
765+
result = ctx.getTemplateSpecializationType(templateName,
766+
templateArguments,
767+
*underlyingType);
768+
}
769+
if (dependent)
770+
const_cast<Type *>(result.getTypePtr())
771+
->addDependence(TypeDependence::DependentInstantiation);
772+
return result;
753773
}]>;
754774
}
755775

0 commit comments

Comments
 (0)