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

via cfe-commits cfe-commits at lists.llvm.org
Sat Jan 25 04:07:41 PST 2025


================
@@ -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<std::string, std::string>
+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


More information about the cfe-commits mailing list