aboutsummaryrefslogtreecommitdiff
path: root/clang/utils/TableGen/ClangBuiltinTemplatesEmitter.cpp
blob: 310ae89835e00afb7e77cd32926952e881cda187 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
//=- 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/ADT/StringSet.h"
#include "llvm/TableGen/Error.h"
#include "llvm/TableGen/TableGenBackend.h"

#include <sstream>

using namespace llvm;

static std::string TemplateNameList;
static std::string CreateBuiltinTemplateParameterList;

static llvm::StringSet<> BuiltinClasses;

namespace {
struct ParserState {
  size_t UniqueCounter = 0;
  size_t CurrentDepth = 0;
  bool EmittedSizeTInfo = false;
  bool EmittedUint32TInfo = false;
};

std::pair<std::string, std::string>
ParseTemplateParameterList(ParserState &PS,
                           ArrayRef<const Record *> TemplateArgs) {
  llvm::SmallVector<std::string, 4> Params;
  llvm::StringMap<std::string> 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.contains(Type.str()))
        PrintFatalError("Unknown 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")) {
      std::string SourceInfo;
      if (Arg->getValueAsString("TypeName") == "size_t") {
        SourceInfo = "SizeTInfo";
        if (!PS.EmittedSizeTInfo) {
          Code << "TypeSourceInfo *SizeTInfo = "
                  "C.getTrivialTypeSourceInfo(C.getSizeType());\n";
          PS.EmittedSizeTInfo = true;
        }
      } else if (Arg->getValueAsString("TypeName") == "uint32_t") {
        SourceInfo = "Uint32TInfo";
        if (!PS.EmittedUint32TInfo) {
          Code << "TypeSourceInfo *Uint32TInfo = "
                  "C.getTrivialTypeSourceInfo(C.UnsignedIntTy);\n";
          PS.EmittedUint32TInfo = true;
        }
      } else {
        PrintFatalError("Unknown Type Name");
      }
      Code << " auto *" << ParmName
           << " = NonTypeTemplateParmDecl::Create(C, DC, SourceLocation(), "
              "SourceLocation(), "
           << PS.CurrentDepth << ", " << Position++ << ", /*Id=*/nullptr, "
           << SourceInfo
           << "->getType(), "
              "/*ParameterPack=*/false, "
           << SourceInfo << ");\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 (const 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<const Record *> 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 EmitBuiltinTemplate(const Record *BuiltinTemplate) {
  auto Class = BuiltinTemplate->getType()->getAsString();
  auto Name = BuiltinTemplate->getName();

  std::vector<const Record *> TemplateHead =
      BuiltinTemplate->getValueAsListOfDefs("TemplateHead");

  EmitCreateBuiltinTemplateParameterList(TemplateHead, Name);

  TemplateNameList += Class + "(";
  TemplateNameList += Name;
  TemplateNameList += ")\n";

  BuiltinClasses.insert(Class);
}

void EmitDefaultDefine(llvm::raw_ostream &OS, StringRef Name) {
  OS << "#ifndef " << Name << "\n";
  OS << "#define " << Name << "(NAME)" << " " << "BuiltinTemplate"
     << "(NAME)\n";
  OS << "#endif\n\n";
}

void EmitUndef(llvm::raw_ostream &OS, StringRef Name) {
  OS << "#undef " << Name << "\n";
}
} // namespace

void clang::EmitClangBuiltinTemplates(const llvm::RecordKeeper &Records,
                                      llvm::raw_ostream &OS) {
  emitSourceFileHeader("Tables and code for Clang's builtin templates", OS);

  for (const auto *Builtin :
       Records.getAllDerivedDefinitions("BuiltinTemplate"))
    EmitBuiltinTemplate(Builtin);

  for (const auto &ClassEntry : BuiltinClasses) {
    StringRef Class = ClassEntry.getKey();
    if (Class == "BuiltinTemplate")
      continue;
    EmitDefaultDefine(OS, Class);
  }

  OS << "#if defined(CREATE_BUILTIN_TEMPLATE_PARAMETER_LIST)\n"
     << CreateBuiltinTemplateParameterList
     << "#undef CREATE_BUILTIN_TEMPLATE_PARAMETER_LIST\n#else\n"
     << TemplateNameList << "#undef BuiltinTemplate\n#endif\n";

  for (const auto &ClassEntry : BuiltinClasses) {
    StringRef Class = ClassEntry.getKey();
    if (Class == "BuiltinTemplate")
      continue;
    EmitUndef(OS, Class);
  }
}