[clang] [clang-tools-extra] [llvm] [Clang] Add __builtin_type_pack_dedup template to deduplicate types in template arguments (PR #106730)

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Tue Sep 17 11:52:22 PDT 2024


================
@@ -3158,6 +3161,33 @@ checkBuiltinTemplateIdType(Sema &SemaRef, BuiltinTemplateDecl *BTD,
     int64_t N = Index.getExtValue();
     return Ts.getPackAsArray()[N].getAsType();
   }
+  case BTK__type_pack_dedup: {
+    assert(Converted.size() == 2 && "__builtin_type_pack_dedup should be given "
+                                    "a template and a parameter pack");
+    TemplateArgument Template = Converted[0];
+    TemplateArgument Ts = Converted[1];
+    if (Template.isDependent() || Ts.isDependent())
+      return Context.getCanonicalTemplateSpecializationType(TemplateName(BTD),
+                                                            Converted);
+
+    assert(Template.getKind() == clang::TemplateArgument::Template);
+    assert(Ts.getKind() == clang::TemplateArgument::Pack);
+    TemplateArgumentListInfo SyntheticTemplateArgs;
+    llvm::SmallDenseSet<QualType> Seen;
+    // Synthesize a new template argument list, removing duplicates.
+    for (auto T : Ts.getPackAsArray()) {
+      assert(T.getKind() == clang::TemplateArgument::Type);
+      if (!Seen.insert(T.getAsType().getCanonicalType()).second)
+        continue;
+      SyntheticTemplateArgs.addArgument(TemplateArgumentLoc(
+          TemplateArgument(T), SemaRef.Context.getTrivialTypeSourceInfo(
+                                   T.getAsType(),
+                                   /*FIXME: add location*/ SourceLocation())));
----------------
zygoloid wrote:

I think the only thing we would use this source location for is to diagnose in `CheckTemplateIdType` if the template arguments aren't suitable for use as arguments of `Template`, which we already checked before getting here. So I don't think it's worth going to any effort to get a good location. Let's just pass in `TemplateLoc` as the location.

(It's notable that `__make_integer_seq` uses (eg) `TemplateArguments[1].getLocation()`. We could do the same, but that will break once we add universal template parameters, which [seem to be making good progress through WG21](https://github.com/cplusplus/papers/issues/1662) -- where there might be only a single template argument that pack-expands to all the arguments of `__make_integer_seq`, and likewise here. So maybe it'd be worth changing `__make_integer_seq` to pass in `TemplateLoc` instead while we're thinking about this.)

https://github.com/llvm/llvm-project/pull/106730


More information about the cfe-commits mailing list