[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-26 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 closed 
https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-26 Thread Erich Keane via cfe-commits

https://github.com/erichkeane approved this pull request.


https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Erich Keane via cfe-commits


@@ -0,0 +1,162 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+static std::string TemplateNameList;
+static std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS,
+   ArrayRef TemplateArgs) {
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;

erichkeane wrote:

There are distinct advantages (particularly in the case of `StringMap`) to the 
point it is nice to use them (same perhaps with `SmallVector` the line above, 
given its Small-Size-Opt and typically small number of params).

You're right that we care LESS about tablegen, though there are definitely 
folks who are particularly sensitive to the speed of tablegen.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Erich Keane via cfe-commits


@@ -0,0 +1,46 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class TemplateArg {
+  string Name = name;
+}
+
+class Template args, string name> : TemplateArg {
+  list Args = args;
+}
+
+class Class : TemplateArg {

erichkeane wrote:

This represents the `class` keyword, right?  In a few places `typename` can be 
used instead.  I was noodling whether spelling it as `typename` instead of 
`class` would be valuable.  I don't think there is 24 hrs later, but was asking.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Nikolas Klauser via cfe-commits


@@ -0,0 +1,162 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+static std::string TemplateNameList;
+static std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS,
+   ArrayRef TemplateArgs) {
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;

philnik777 wrote:

I'm really not familiar with the LLVM containers, so I don't know. Does it make 
much of a difference for a tablegen emitter?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Nikolas Klauser via cfe-commits


@@ -0,0 +1,46 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class TemplateArg {
+  string Name = name;
+}
+
+class Template args, string name> : TemplateArg {
+  list Args = args;
+}
+
+class Class : TemplateArg {

philnik777 wrote:

I'm not quite sure what you're asking. Do you mean it doesn't need to be in the 
`name`?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Nikolas Klauser via cfe-commits


@@ -0,0 +1,162 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+static std::string TemplateNameList;
+static std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS,
+   ArrayRef TemplateArgs) {
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;
+
+  std::ostringstream Code;
+  Code << std::boolalpha;
+
+  size_t Position = 0;
+  for (const Record *Arg : TemplateArgs) {
+std::string ParmName = "Parm" + std::to_string(PS.UniqueCounter++);
+if (Arg->isSubClassOf("Template")) {
+  ++PS.CurrentDepth;
+  auto [TemplateCode, TPLName] =
+  ParseTemplateParameterList(PS, Arg->getValueAsListOfDefs("Args"));
+  --PS.CurrentDepth;
+  Code << TemplateCode << " auto *" << ParmName
+   << " = TemplateTemplateParmDecl::Create(C, DC, SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, "
+   << TPLName << ");\n";
+} else if (Arg->isSubClassOf("Class")) {
+  Code << " auto *" << ParmName
+   << " = TemplateTypeParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*Id=*/nullptr, /*Typename=*/false, "
+   << Arg->getValueAsBit("IsVariadic") << ");\n";
+} else if (Arg->isSubClassOf("NTTP")) {
+  auto Type = Arg->getValueAsString("TypeName");
+
+  if (TemplateNameToParmName.find(Type.str()) ==
+  TemplateNameToParmName.end()) {
+PrintFatalError("Unkown Type Name");
+  }
+
+  auto TSIName = "TSI" + std::to_string(PS.UniqueCounter++);
+  Code << " auto *" << TSIName << " = C.getTrivialTypeSourceInfo(QualType("
+   << TemplateNameToParmName[Type.str()] << "->getTypeForDecl(), 
0));\n"
+   << " auto *" << ParmName
+   << " = NonTypeTemplateParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++ << ", /*Id=*/nullptr, "
+   << TSIName << "->getType(), " << Arg->getValueAsBit("IsVariadic")
+   << ", " << TSIName << ");\n";
+} else if (Arg->isSubClassOf("BuiltinNTTP")) {
+  if (Arg->getValueAsString("TypeName") != "size_t")
+PrintFatalError("Unkown Type Name");
+  if (!PS.EmittedSizeTInfo) {
+Code << "TypeSourceInfo *SizeTInfo = "
+"C.getTrivialTypeSourceInfo(C.getSizeType());\n";
+PS.EmittedSizeTInfo = true;
+  }
+  Code << " auto *" << ParmName
+   << " = NonTypeTemplateParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*Id=*/nullptr, SizeTInfo->getType(), "
+  "/*ParameterPack=*/false, SizeTInfo);\n";
+} else {
+  PrintFatalError("Unknown Argument Type");
+}
+
+TemplateNameToParmName[Arg->getValueAsString("Name").str()] = ParmName;
+Params.emplace_back(std::move(ParmName));
+  }
+
+  auto TPLName = "TPL" + std::to_string(PS.UniqueCounter++);
+  Code << " auto *" << TPLName
+   << " = TemplateParameterList::Create(C, SourceLocation(), "
+  "SourceLocation(), {";
+
+  if (Params.empty()) {
+PrintFatalError(
+"Expected at least one argument in template parameter list");
+  }
+
+  bool First = true;
+  for (auto e : Params) {
+if (First) {
+  First = false;
+  Code << e;
+} else {
+  Code << ", " << e;
+}
+  }
+  Code << "}, SourceLocation(), nullptr);\n";
+
+  return {std::move(Code).str(), std::move(TPLName)};
+}
+
+static void
+EmitCreateBuiltinTemplateParameterList(std::vector 
TemplateArgs,
+   StringRef Name) {
+  using namespace std::string_literals;
+  CreateBuiltinTemplateParameterList +=
+  "case BTK"s + std::string{Name} + ": {\n"s;
+
+  ParserState PS;
+  auto [Code, TPLName] = ParseTemplateParameterList(PS, TemplateArgs);
+  CreateBuiltinTemplateParameterList += Code + "\n  return " + TPLName + ";\n";
+
+  CreateBuiltinTemplateParameterList += "  }\n";
+}
+
+void E

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-20 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 updated 
https://github.com/llvm/llvm-project/pull/123736

>From 586dd4edfc79c88cc1583b64d186c1481fbd6ce1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH 1/7] [Clang] Add BuiltinTemplates.td to generate code for
 builtin templates

---
 clang/include/clang/AST/ASTContext.h  |  35 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 234 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fb..98db5522d564e 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d..71e28ec1f9c65 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__builtin_common

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-19 Thread Erich Keane via cfe-commits


@@ -0,0 +1,162 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+static std::string TemplateNameList;
+static std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS,
+   ArrayRef TemplateArgs) {
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;

erichkeane wrote:

Does `llvm::StringMap` make sense here? 


https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-19 Thread Erich Keane via cfe-commits


@@ -0,0 +1,46 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class TemplateArg {
+  string Name = name;
+}
+
+class Template args, string name> : TemplateArg {
+  list Args = args;
+}
+
+class Class : TemplateArg {

erichkeane wrote:

We don't have a reason to care about spelling `class`/`typename`, right?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-19 Thread Erich Keane via cfe-commits


@@ -0,0 +1,162 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+#include 
+
+using namespace llvm;
+
+static std::string TemplateNameList;
+static std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS,
+   ArrayRef TemplateArgs) {
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;
+
+  std::ostringstream Code;
+  Code << std::boolalpha;
+
+  size_t Position = 0;
+  for (const Record *Arg : TemplateArgs) {
+std::string ParmName = "Parm" + std::to_string(PS.UniqueCounter++);
+if (Arg->isSubClassOf("Template")) {
+  ++PS.CurrentDepth;
+  auto [TemplateCode, TPLName] =
+  ParseTemplateParameterList(PS, Arg->getValueAsListOfDefs("Args"));
+  --PS.CurrentDepth;
+  Code << TemplateCode << " auto *" << ParmName
+   << " = TemplateTemplateParmDecl::Create(C, DC, SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*ParameterPack=*/false, /*Id=*/nullptr, /*Typename=*/false, "
+   << TPLName << ");\n";
+} else if (Arg->isSubClassOf("Class")) {
+  Code << " auto *" << ParmName
+   << " = TemplateTypeParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*Id=*/nullptr, /*Typename=*/false, "
+   << Arg->getValueAsBit("IsVariadic") << ");\n";
+} else if (Arg->isSubClassOf("NTTP")) {
+  auto Type = Arg->getValueAsString("TypeName");
+
+  if (TemplateNameToParmName.find(Type.str()) ==
+  TemplateNameToParmName.end()) {
+PrintFatalError("Unkown Type Name");
+  }
+
+  auto TSIName = "TSI" + std::to_string(PS.UniqueCounter++);
+  Code << " auto *" << TSIName << " = C.getTrivialTypeSourceInfo(QualType("
+   << TemplateNameToParmName[Type.str()] << "->getTypeForDecl(), 
0));\n"
+   << " auto *" << ParmName
+   << " = NonTypeTemplateParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++ << ", /*Id=*/nullptr, "
+   << TSIName << "->getType(), " << Arg->getValueAsBit("IsVariadic")
+   << ", " << TSIName << ");\n";
+} else if (Arg->isSubClassOf("BuiltinNTTP")) {
+  if (Arg->getValueAsString("TypeName") != "size_t")
+PrintFatalError("Unkown Type Name");
+  if (!PS.EmittedSizeTInfo) {
+Code << "TypeSourceInfo *SizeTInfo = "
+"C.getTrivialTypeSourceInfo(C.getSizeType());\n";
+PS.EmittedSizeTInfo = true;
+  }
+  Code << " auto *" << ParmName
+   << " = NonTypeTemplateParmDecl::Create(C, DC, SourceLocation(), "
+  "SourceLocation(), "
+   << PS.CurrentDepth << ", " << Position++
+   << ", /*Id=*/nullptr, SizeTInfo->getType(), "
+  "/*ParameterPack=*/false, SizeTInfo);\n";
+} else {
+  PrintFatalError("Unknown Argument Type");
+}
+
+TemplateNameToParmName[Arg->getValueAsString("Name").str()] = ParmName;
+Params.emplace_back(std::move(ParmName));
+  }
+
+  auto TPLName = "TPL" + std::to_string(PS.UniqueCounter++);
+  Code << " auto *" << TPLName
+   << " = TemplateParameterList::Create(C, SourceLocation(), "
+  "SourceLocation(), {";
+
+  if (Params.empty()) {
+PrintFatalError(
+"Expected at least one argument in template parameter list");
+  }
+
+  bool First = true;
+  for (auto e : Params) {
+if (First) {
+  First = false;
+  Code << e;
+} else {
+  Code << ", " << e;
+}
+  }
+  Code << "}, SourceLocation(), nullptr);\n";
+
+  return {std::move(Code).str(), std::move(TPLName)};
+}
+
+static void
+EmitCreateBuiltinTemplateParameterList(std::vector 
TemplateArgs,
+   StringRef Name) {
+  using namespace std::string_literals;
+  CreateBuiltinTemplateParameterList +=
+  "case BTK"s + std::string{Name} + ": {\n"s;
+
+  ParserState PS;
+  auto [Code, TPLName] = ParseTemplateParameterList(PS, TemplateArgs);
+  CreateBuiltinTemplateParameterList += Code + "\n  return " + TPLName + ";\n";
+
+  CreateBuiltinTemplateParameterList += "  }\n";
+}
+
+void E

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-19 Thread Erich Keane via cfe-commits


@@ -0,0 +1,46 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class TemplateArg {
+  string Name = name;
+}
+
+class Template args, string name> : TemplateArg {
+  list Args = args;
+}
+
+class Class : TemplateArg {
+  bit IsVariadic = is_variadic;
+}
+
+class NTTP : 
TemplateArg {
+  string TypeName = type_name;
+  bit IsVariadic = is_variadic;
+}
+
+class BuiltinNTTP : TemplateArg<""> {
+  string TypeName = type_name;
+}
+
+def SizeT : BuiltinNTTP<"size_t"> {}
+
+class BuiltinTemplate template_head> {
+  list TemplateHead = template_head;
+}
+
+def __make_integer_seq : BuiltinTemplate<
+  [Template<[Class<"T">, NTTP<"T", "Ints", /*is_variadic=*/1>], "IntSeq">, 
Class<"T">, NTTP<"T", "N">]>;

erichkeane wrote:

I realize I asked you to remove the 'string' version here, but if we could 
include it as a comment (particularly as these are the FIRST and are 
effectively documenting the format), it would be helpful.


https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-19 Thread Erich Keane via cfe-commits

erichkeane wrote:

> Gentle ping @erichkeane

Thanks for the ping, I'm just getting back today from WG21, so I am still 
catching up on reviews.  

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-07 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 updated 
https://github.com/llvm/llvm-project/pull/123736

>From 586dd4edfc79c88cc1583b64d186c1481fbd6ce1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH 1/6] [Clang] Add BuiltinTemplates.td to generate code for
 builtin templates

---
 clang/include/clang/AST/ASTContext.h  |  35 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 234 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba5..98db5522d564e90 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8d..71e28ec1f9c65f3 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__builti

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-02-07 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 updated 
https://github.com/llvm/llvm-project/pull/123736

>From 586dd4edfc79c88cc1583b64d186c1481fbd6ce1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH 1/5] [Clang] Add BuiltinTemplates.td to generate code for
 builtin templates

---
 clang/include/clang/AST/ASTContext.h  |  35 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 234 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba5..98db5522d564e90 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8d..71e28ec1f9c65f3 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__builti

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-27 Thread Erich Keane via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

erichkeane wrote:

> @erichkeane Do you have any better idea how to represent this? I could 
> represent as something like
> 
> ```
> def __make_integer_seq : BuiltinTemplate, NTTP<"T", 
> "Ints", /*variadic*/true>], "IntSeq">, Class<"T">, NTTP<"T", "N">>;
> ```
> 
> But that's really hard to read IMO.
> 
> @Sirraide With an attribute we'd still have to verify that the parsed 
> template actually represents the template, since users would be able to 
> declare templates with an attribute, so we'd be back at square one AFAICT. It 
> would also mean introducing code into every single translation unit, which 
> would probably increase the startup time of clang.

I REALLY don't want the whole compiler, but something like the above isn't too 
bad IMO.  Perhaps the NTTP recursively takes a type template.  

WHILE that isn't super easy to read, it isn't all that bad, it is pretty much 
the template head.



https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-27 Thread Erich Keane via cfe-commits


@@ -0,0 +1,23 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class BuiltinTemplate {
+  string Prototype = prototype;
+}
+
+def __make_integer_seq : BuiltinTemplate<
+  "template  class IntSeq, class T, T N>">;

erichkeane wrote:

I thought about this over the weekend.  I really think that we want some sort 
of structures to define the template head here.  The base needs a name, then we 
need a Type, an Non-Type, a Template-Template, and a Variadic (for now).

But having us try to parse this is really going to be error prone and difficult 
to extend.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-25 Thread via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

Sirraide wrote:

(For the record though, if my suggested approach sounds like ‘too much compiler 
magic’, then the tablegen approach is fine too imo; either way is better than 
manually building AST nodes for template parameters ;Þ)

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-25 Thread via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

Sirraide wrote:

> But that's really hard to read IMO.

Yeah that’s really verbose... which is why my line of thought was that we 
literally have an entire C++ compiler, so it’d be nice if we could just write 
actual C++.

> since users would be able to declare templates with an attribute

Sure, but if someone thinks that using an attribute called 
`__builtin_template__` in their own code is a good idea then that’s on them 
candidly (we could just straight-up error if it’s defined in an actual file 
supplied by the user). Also, my idea was to maybe only expose the spelling w/ 
the double underscore to make it clearer that it’s for use by the 
implementation only, but that may not be necessary if we make it an error.

I’m not sure this approach would really be *simpler* or anything like that, but 
it might be worth it (if it’s feasible at all, that is), provided that the 
number of builtin templates continues to grow in the future. I feel like some 
sort of ‘preamble’ that gets prepended to every TU might be a useful thing to 
have in general.

> It would also mean introducing code into every single translation unit, which 
> would probably increase the startup time of clang.

I mean, these are all fairly small template declarations and user code 
routinely has tons of much larger templates, so I don’t think this will be too 
noticeable. ;Þ




https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-25 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 updated 
https://github.com/llvm/llvm-project/pull/123736

>From 586dd4edfc79c88cc1583b64d186c1481fbd6ce1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH 1/4] [Clang] Add BuiltinTemplates.td to generate code for
 builtin templates

---
 clang/include/clang/AST/ASTContext.h  |  35 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 234 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba..98db5522d564e9 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8..71e28ec1f9c65f 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__builtin_co

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-25 Thread Nikolas Klauser via cfe-commits


@@ -0,0 +1,23 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class BuiltinTemplate {
+  string Prototype = prototype;
+}
+
+def __make_integer_seq : BuiltinTemplate<
+  "template  class IntSeq, class T, T N>">;

philnik777 wrote:

Do you think it would be better to give the templates an internal name to be 
more consistent?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-25 Thread Nikolas Klauser via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

philnik777 wrote:

@erichkeane Do you have any better idea how to represent this? I could 
represent as something like
```
def __make_integer_seq : BuiltinTemplate, NTTP<"T", 
"Ints", /*variadic*/true>], "IntSeq">, Class<"T">, NTTP<"T", "N">>;
```
But that's really hard to read IMO.

@Sirraide With an attribute we'd still have to verify that the parsed template 
actually represents the template, since users would be able to declare 
templates with an attribute, so we'd be back at square one AFAICT. It would 
also mean introducing code into every single translation unit, which would 
probably increase the startup time of clang.


https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

Sirraide wrote:

I’m not familiar w/ every last thing that would go into that, but can we maybe 
literally just use the actual parser for this?

As in, this might be a stupid idea, but would it be feasible to just throw this 
as source text at the start of the translation unit and then parse it, maybe w/ 
something like a `[[clang::__builtin_template__]]` attribute?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;

erichkeane wrote:

Can we be consistent on the uses with the argument name?  Seems split between 
`Name` and `BTName`.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -0,0 +1,23 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class BuiltinTemplate {
+  string Prototype = prototype;
+}
+
+def __make_integer_seq : BuiltinTemplate<
+  "template  class IntSeq, class T, T N>">;

erichkeane wrote:

Its unfortunate that we have this much "just arbitrary text" here.  I don't 
have a better idea, and no real wish to try to make it 'better', but this makes 
me grumpy and I wanted to let you know.

ALSO, and something that IS actionable: Can we document the importance of the 
defined 'name'?  Since it is the actual name of the builtin that we're exposing 
to the user?  Typically these (names in .td files) are internal names that are 
generally arbitrary, so it would be nice to document that:

"An entry here will introduce a template builtin with the name X, and 
whatever-else-we-are-doing-here."

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };

erichkeane wrote:

Oof, a re-implemented template head parser is REALLY scary here to me.  I think 
I now AM more than just grumpy here, and wish we could do a better 'list' of 
template args/interface for them instead of this.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -0,0 +1,184 @@
+//=- ClangBuiltinsEmitter.cpp - Generate Clang builtin templates-*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+//
+// This tablegen backend emits Clang's builtin templates.
+//
+//===--===//
+
+#include "TableGenBackends.h"
+#include "llvm/TableGen/Error.h"
+#include "llvm/TableGen/TableGenBackend.h"
+
+using namespace llvm;
+
+std::string TemplateNameList;
+std::string CreateBuiltinTemplateParameterList;
+
+namespace {
+struct ParserState {
+  size_t UniqueCounter = 0;
+  size_t CurrentDepth = 0;
+  bool EmittedSizeTInfo = false;
+};
+
+std::pair
+ParseTemplateParameterList(ParserState &PS, StringRef &TemplateParmList) {
+  auto Alphabetic = [](char c) { return std::isalpha(c); };
+  auto BoolToStr = [](bool b) { return b ? "true" : "false"; };
+
+  std::string Generator;
+  std::vector Params;
+  std::unordered_map TemplateNameToParmName;
+  TemplateParmList = TemplateParmList.ltrim();
+  if (!TemplateParmList.consume_front("<"))
+PrintFatalError("Expected '<' to start the parameter list");
+
+  size_t Position = 0;
+  while (true) {
+std::string ParmName = "Parm" + std::to_string(PS.UniqueCounter++);

erichkeane wrote:

It would be lovely if we could use something like a string stream rather than 
string append through here to be more consistent with the rest of the clang 
tablegen.

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -0,0 +1,23 @@
+//===--- BuiltinTemplates.td - Clang builtin template aliases ---*- C++ 
-*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM 
Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===--===//
+
+class BuiltinTemplate {
+  string Prototype = prototype;

erichkeane wrote:

Not sure `Prototype` is what we mean here... Perhaps `TemplateHead`?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Erich Keane via cfe-commits


@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;

erichkeane wrote:

Still working through this, but elsewhere we are removing 3 items for this 
'define', yet here we are only removing 2.  Should line 417 be removed as well?

https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-23 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 updated 
https://github.com/llvm/llvm-project/pull/123736

>From 586dd4edfc79c88cc1583b64d186c1481fbd6ce1 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH] [Clang] Add BuiltinTemplates.td to generate code for builtin
 templates

---
 clang/include/clang/AST/ASTContext.h  |  35 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 234 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba..98db5522d564e9 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,8 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
-
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +621,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1149,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2051,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8..71e28ec1f9c65f 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__builtin_common

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-21 Thread via cfe-commits

github-actions[bot] wrote:




:warning: C/C++ code formatter, clang-format found issues in your code. 
:warning:



You can test this locally with the following command:


``bash
git-clang-format --diff c25bd6e35134f591ee7dfeb4494df02987106f7e 
ae765ca2a5e63ecc22068d7f0ad2aad6c3bc4da3 --extensions cpp,h -- 
clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp 
clang/include/clang/AST/ASTContext.h clang/include/clang/AST/DeclID.h 
clang/include/clang/Basic/Builtins.h clang/lib/AST/ASTContext.cpp 
clang/lib/AST/ASTImporter.cpp clang/lib/AST/DeclTemplate.cpp 
clang/lib/Lex/PPMacroExpansion.cpp clang/lib/Sema/SemaLookup.cpp 
clang/lib/Serialization/ASTReader.cpp clang/lib/Serialization/ASTWriter.cpp 
clang/utils/TableGen/TableGen.cpp clang/utils/TableGen/TableGenBackends.h
``





View the diff from clang-format here.


``diff
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index eed257e4d9..98db5522d5 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,7 +410,6 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-
 #define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
 #include "clang/Basic/BuiltinTemplates.inc"
 

``




https://github.com/llvm/llvm-project/pull/123736
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-21 Thread via cfe-commits

llvmbot wrote:



@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-modules

Author: Nikolas Klauser (philnik777)


Changes

This makes it significantly easier to add new builtin templates, since you only 
have to modify two places instead of a dozen or so.

The `BuiltinTemplates.td` could also be extended to generate documentation from 
it in the future.


---

Patch is 30.36 KiB, truncated to 20.00 KiB below, full version: 
https://github.com/llvm/llvm-project/pull/123736.diff


16 Files Affected:

- (modified) clang/include/clang/AST/ASTContext.h (+14-20) 
- (modified) clang/include/clang/AST/DeclID.h (+2-8) 
- (added) clang/include/clang/Basic/BuiltinTemplates.td (+23) 
- (modified) clang/include/clang/Basic/Builtins.h (+2-8) 
- (modified) clang/include/clang/Basic/CMakeLists.txt (+4) 
- (modified) clang/lib/AST/ASTContext.cpp (+8-22) 
- (modified) clang/lib/AST/ASTImporter.cpp (+4-8) 
- (modified) clang/lib/AST/DeclTemplate.cpp (+2-131) 
- (modified) clang/lib/Lex/PPMacroExpansion.cpp (+2-3) 
- (modified) clang/lib/Sema/SemaLookup.cpp (+6-12) 
- (modified) clang/lib/Serialization/ASTReader.cpp (+6-16) 
- (modified) clang/lib/Serialization/ASTWriter.cpp (+3-5) 
- (modified) clang/utils/TableGen/CMakeLists.txt (+1) 
- (added) clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp (+184) 
- (modified) clang/utils/TableGen/TableGen.cpp (+6) 
- (modified) clang/utils/TableGen/TableGenBackends.h (+2) 


``diff
diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba..eed257e4d92fff 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,9 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
 
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +622,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1150,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2052,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8..71e28ec1f9c65f 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_STRING_ID,
 
   /// The internal '__NSConstantString' tag type.
   PREDEF_DECL_CF_CONSTANT_STRING_TAG_ID,
 
-  /// The internal '__type_pack_element' template.
-  PREDEF_DECL_TYPE_PACK_ELEMENT_ID,
-
-  /// The internal '__buil

[clang] [Clang] Add BuiltinTemplates.td to generate code for builtin templates (PR #123736)

2025-01-21 Thread Nikolas Klauser via cfe-commits

https://github.com/philnik777 created 
https://github.com/llvm/llvm-project/pull/123736

This makes it significantly easier to add new builtin templates, since you only 
have to modify two places instead of a dozen or so.

The `BuiltinTemplates.td` could also be extended to generate documentation from 
it in the future.


>From ae765ca2a5e63ecc22068d7f0ad2aad6c3bc4da3 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser 
Date: Wed, 4 Sep 2024 13:31:39 +0200
Subject: [PATCH] [Clang] Add BuiltinTemplates.td to generate code for builtin
 templates

---
 clang/include/clang/AST/ASTContext.h  |  34 ++--
 clang/include/clang/AST/DeclID.h  |  10 +-
 clang/include/clang/Basic/BuiltinTemplates.td |  23 +++
 clang/include/clang/Basic/Builtins.h  |  10 +-
 clang/include/clang/Basic/CMakeLists.txt  |   4 +
 clang/lib/AST/ASTContext.cpp  |  30 +--
 clang/lib/AST/ASTImporter.cpp |  12 +-
 clang/lib/AST/DeclTemplate.cpp| 133 +
 clang/lib/Lex/PPMacroExpansion.cpp|   5 +-
 clang/lib/Sema/SemaLookup.cpp |  18 +-
 clang/lib/Serialization/ASTReader.cpp |  22 +--
 clang/lib/Serialization/ASTWriter.cpp |   8 +-
 clang/utils/TableGen/CMakeLists.txt   |   1 +
 .../TableGen/ClangBuiltinTemplatesEmitter.cpp | 184 ++
 clang/utils/TableGen/TableGen.cpp |   6 +
 clang/utils/TableGen/TableGenBackends.h   |   2 +
 16 files changed, 269 insertions(+), 233 deletions(-)
 create mode 100644 clang/include/clang/Basic/BuiltinTemplates.td
 create mode 100644 clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp

diff --git a/clang/include/clang/AST/ASTContext.h 
b/clang/include/clang/AST/ASTContext.h
index 0e07c5d6ce8fba..eed257e4d92fff 100644
--- a/clang/include/clang/AST/ASTContext.h
+++ b/clang/include/clang/AST/ASTContext.h
@@ -410,11 +410,9 @@ class ASTContext : public RefCountedBase {
   /// The identifier 'NSCopying'.
   IdentifierInfo *NSCopyingName = nullptr;
 
-  /// The identifier '__make_integer_seq'.
-  mutable IdentifierInfo *MakeIntegerSeqName = nullptr;
 
-  /// The identifier '__type_pack_element'.
-  mutable IdentifierInfo *TypePackElementName = nullptr;
+#define BuiltinTemplate(BTName) mutable IdentifierInfo *Name##BTName = nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The identifier '__builtin_common_type'.
   mutable IdentifierInfo *BuiltinCommonTypeName = nullptr;
@@ -624,9 +622,9 @@ class ASTContext : public RefCountedBase {
 
   TranslationUnitDecl *TUDecl = nullptr;
   mutable ExternCContextDecl *ExternCContext = nullptr;
-  mutable BuiltinTemplateDecl *MakeIntegerSeqDecl = nullptr;
-  mutable BuiltinTemplateDecl *TypePackElementDecl = nullptr;
-  mutable BuiltinTemplateDecl *BuiltinCommonTypeDecl = nullptr;
+
+#define BuiltinTemplate(Name) mutable BuiltinTemplateDecl *Decl##Name = 
nullptr;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   /// The associated SourceManager object.
   SourceManager &SourceMgr;
@@ -1152,9 +1150,9 @@ class ASTContext : public RefCountedBase {
   }
 
   ExternCContextDecl *getExternCContextDecl() const;
-  BuiltinTemplateDecl *getMakeIntegerSeqDecl() const;
-  BuiltinTemplateDecl *getTypePackElementDecl() const;
-  BuiltinTemplateDecl *getBuiltinCommonTypeDecl() const;
+
+#define BuiltinTemplate(Name) BuiltinTemplateDecl *get##Name##Decl() const;
+#include "clang/Basic/BuiltinTemplates.inc"
 
   // Builtin Types.
   CanQualType VoidTy;
@@ -2054,17 +2052,13 @@ class ASTContext : public RefCountedBase {
 return BoolName;
   }
 
-  IdentifierInfo *getMakeIntegerSeqName() const {
-if (!MakeIntegerSeqName)
-  MakeIntegerSeqName = &Idents.get("__make_integer_seq");
-return MakeIntegerSeqName;
-  }
-
-  IdentifierInfo *getTypePackElementName() const {
-if (!TypePackElementName)
-  TypePackElementName = &Idents.get("__type_pack_element");
-return TypePackElementName;
+#define BuiltinTemplate(BTName)
\
+  IdentifierInfo *get##BTName##Name() const {  
\
+if (!Name##BTName) 
\
+  Name##BTName = &Idents.get(#BTName); 
\
+return Name##BTName;   
\
   }
+#include "clang/Basic/BuiltinTemplates.inc"
 
   IdentifierInfo *getBuiltinCommonTypeName() const {
 if (!BuiltinCommonTypeName)
diff --git a/clang/include/clang/AST/DeclID.h b/clang/include/clang/AST/DeclID.h
index 49964b43c7d1d8..71e28ec1f9c65f 100644
--- a/clang/include/clang/AST/DeclID.h
+++ b/clang/include/clang/AST/DeclID.h
@@ -71,20 +71,14 @@ enum PredefinedDeclIDs {
   /// The extern "C" context.
   PREDEF_DECL_EXTERN_C_CONTEXT_ID,
 
-  /// The internal '__make_integer_seq' template.
-  PREDEF_DECL_MAKE_INTEGER_SEQ_ID,
-
   /// The internal '__NSConstantString' typedef.
   PREDEF_DECL_CF_CONSTANT_S