aboutsummaryrefslogtreecommitdiff
path: root/libc/utils/HdrGen/PublicAPICommand.cpp
blob: 5c46c82a2385348bebb883d6195f51f137ec995e (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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
//===-- Implementation of PublicAPICommand --------------------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//

#include "PublicAPICommand.h"

#include "utils/LibcTableGenUtil/APIIndexer.h"

#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSwitch.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/TableGen/Record.h"
#include <algorithm>
#include <vector>

// Text blocks for macro definitions and type decls can be indented to
// suit the surrounding tablegen listing. We need to dedent such blocks
// before writing them out.
static void dedentAndWrite(llvm::StringRef Text, llvm::raw_ostream &OS) {
  llvm::SmallVector<llvm::StringRef, 10> Lines;
  llvm::SplitString(Text, Lines, "\n");
  size_t shortest_indent = 1024;
  for (llvm::StringRef L : Lines) {
    llvm::StringRef Indent = L.take_while([](char c) { return c == ' '; });
    size_t IndentSize = Indent.size();
    if (Indent.size() == L.size()) {
      // Line is all spaces so no point noting the indent.
      continue;
    }
    if (IndentSize < shortest_indent)
      shortest_indent = IndentSize;
  }
  for (llvm::StringRef L : Lines) {
    if (L.size() >= shortest_indent)
      OS << L.drop_front(shortest_indent) << '\n';
  }
}

static std::string getTypeHdrName(const std::string &Name) {
  llvm::SmallVector<llvm::StringRef> Parts;
  llvm::SplitString(llvm::StringRef(Name), Parts);
  return llvm::join(Parts.begin(), Parts.end(), "_");
}

namespace llvm_libc {

static bool isAsciiStart(char C) {
  return (C >= 'A' && C <= 'Z') || (C >= 'a' && C <= 'z') || C == '_';
}

static bool isAsciiContinue(char C) {
  return isAsciiStart(C) || (C >= '0' && C <= '9');
}

static bool isAsciiIdentifier(llvm::StringRef S) {
  if (S.empty())
    return false;
  if (!isAsciiStart(S[0]))
    return false;
  for (char C : S.drop_front())
    if (!isAsciiContinue(C))
      return false;
  return true;
}

static AttributeStyle getAttributeStyle(const llvm::Record *Instance) {
  llvm::StringRef Style = Instance->getValueAsString("Style");
  return llvm::StringSwitch<AttributeStyle>(Style)
      .Case("cxx11", AttributeStyle::Cxx11)
      .Case("gnu", AttributeStyle::Gnu)
      .Case("declspec", AttributeStyle::Declspec)
      .Default(AttributeStyle::Gnu);
}

static AttributeNamespace getAttributeNamespace(const llvm::Record *Instance) {
  llvm::StringRef Namespace = Instance->getValueAsString("Namespace");
  return llvm::StringSwitch<AttributeNamespace>(Namespace)
      .Case("clang", AttributeNamespace::Clang)
      .Case("gnu", AttributeNamespace::Gnu)
      .Default(AttributeNamespace::None);
}

using AttributeMap = llvm::DenseMap<llvm::StringRef, const llvm::Record *>;

template <class SpecMap, class FuncList>
static AttributeMap collectAttributeMacros(const SpecMap &Spec,
                                           const FuncList &Funcs) {
  llvm::DenseMap<llvm::StringRef, const llvm::Record *> MacroAttr;
  for (const auto &Name : Funcs) {
    auto Iter = Spec.find(Name);
    if (Iter == Spec.end())
      continue;

    const llvm::Record *FunctionSpec = Iter->second;
    for (const llvm::Record *Attr :
         FunctionSpec->getValueAsListOfDefs("Attributes"))
      MacroAttr[Attr->getValueAsString("Macro")] = Attr;
  }
  return MacroAttr;
}

static void emitAttributeMacroDecls(const AttributeMap &MacroAttr,
                                    llvm::raw_ostream &OS) {
  for (auto &[Macro, Attr] : MacroAttr) {
    std::vector<const llvm::Record *> Instances =
        Attr->getValueAsListOfDefs("Instances");
    llvm::SmallVector<std::pair<AttributeStyle, const llvm::Record *>> Styles;
    std::transform(Instances.begin(), Instances.end(),
                   std::back_inserter(Styles),
                   [&](const llvm::Record *Instance)
                       -> std::pair<AttributeStyle, const llvm::Record *> {
                     auto Style = getAttributeStyle(Instance);
                     return {Style, Instance};
                   });
    // 1. If __cplusplus is defined and cxx11 style is provided, define the
    // macro using cxx11 version with the following priority:
    //    1a. If there is no namespace (so the macro is supposed to be
    //        compiler-independent), use this version first. This macro will be
    //        tested via __has_cpp_attribute.
    //    1b. If the attribute is a clang attribute, check for __clang__.
    //    1c. If the attribute is a gnu attribute, check for __GNUC__.
    // 2. Otherwise, if __GNUC__ is defined and gnu style is provided,
    //    define the macro using gnu version;
    // 3. Otherwise, if _MSC_VER is defined and __declspec is provided, define
    //    the macro using __declspec version;
    // 4. Fallback to empty macro.
    std::sort(Styles.begin(), Styles.end(), [&](auto &a, auto &b) {
      if (a.first == AttributeStyle::Cxx11 && b.first == AttributeStyle::Cxx11)
        return getAttributeNamespace(a.second) <
               getAttributeNamespace(b.second);
      return a.first < b.first;
    });
    for (auto &[Style, Instance] : Styles) {
      llvm::StringRef Attr = Instance->getValueAsString("Attr");
      if (Style == AttributeStyle::Cxx11) {
        OS << "#if !defined(" << Macro << ") && defined(__cplusplus)";
        AttributeNamespace Namespace = getAttributeNamespace(Instance);
        if (Namespace == AttributeNamespace::Clang)
          OS << " && defined(__clang__)\n";
        else if (Namespace == AttributeNamespace::Gnu)
          OS << " && defined(__GNUC__)\n";
        else
          OS << '\n';
        if (isAsciiIdentifier(Attr) && Namespace != AttributeNamespace::None)
          OS << "#if __has_attribute(" << Attr << ")\n";
        else
          OS << "#if __has_cpp_attribute(" << Attr << ")\n";
        OS << "#define " << Macro << " [[";
        if (Namespace == AttributeNamespace::Clang)
          OS << "clang::";
        else if (Namespace == AttributeNamespace::Gnu)
          OS << "gnu::";
        OS << Attr << "]]\n";
        if (isAsciiIdentifier(Attr))
          OS << "#endif\n";
        OS << "#endif\n";
      }
      if (Style == AttributeStyle::Gnu) {
        OS << "#if !defined(" << Macro << ") && defined(__GNUC__)\n";
        if (isAsciiIdentifier(Attr))
          OS << "#if __has_attribute(" << Attr << ")\n";
        OS << "#define " << Macro << " __attribute__((";
        OS << Attr << "))\n";
        if (isAsciiIdentifier(Attr))
          OS << "#endif\n";
        OS << "#endif\n";
      }
      if (Style == AttributeStyle::Declspec) {
        OS << "#if !defined(" << Macro << ") && defined(_MSC_VER)\n";
        OS << "#define " << Macro << " __declspec(";
        OS << Attr << ")\n";
        OS << "#endif\n";
      }
    }
    OS << "#if !defined(" << Macro << ")\n";
    OS << "#define " << Macro << '\n';
    OS << "#endif\n";
  }

  if (!MacroAttr.empty())
    OS << '\n';
}

static void emitAttributeMacroForFunction(const llvm::Record *FunctionSpec,
                                          llvm::raw_ostream &OS) {
  std::vector<const llvm::Record *> Attributes =
      FunctionSpec->getValueAsListOfDefs("Attributes");
  llvm::interleave(
      Attributes.begin(), Attributes.end(),
      [&](const llvm::Record *Attr) { OS << Attr->getValueAsString("Macro"); },
      [&]() { OS << ' '; });
  if (!Attributes.empty())
    OS << ' ';
}

static void emitUndefsForAttributeMacros(const AttributeMap &MacroAttr,
                                         llvm::raw_ostream &OS) {
  if (!MacroAttr.empty())
    OS << '\n';
  for (auto &[Macro, Attr] : MacroAttr)
    OS << "#undef " << Macro << '\n';
}

static void writeAPIFromIndex(APIIndexer &G,
                              std::vector<std::string> EntrypointNameList,
                              llvm::raw_ostream &OS) {
  for (auto &Pair : G.MacroDefsMap) {
    const std::string &Name = Pair.first;
    if (!G.MacroSpecMap.count(Name))
      llvm::PrintFatalError(Name + " not found in any standard spec.\n");

    const llvm::Record *MacroDef = Pair.second;
    dedentAndWrite(MacroDef->getValueAsString("Defn"), OS);

    OS << '\n';
  }

  for (auto &TypeName : G.RequiredTypes) {
    if (!G.TypeSpecMap.count(TypeName))
      llvm::PrintFatalError(TypeName + " not found in any standard spec.\n");
    OS << "#include <llvm-libc-types/" << getTypeHdrName(TypeName) << ".h>\n";
  }
  OS << '\n';

  if (G.Enumerations.size() != 0)
    OS << "enum {" << '\n';
  for (const auto &Name : G.Enumerations) {
    if (!G.EnumerationSpecMap.count(Name))
      llvm::PrintFatalError(
          Name + " is not listed as an enumeration in any standard spec.\n");

    const llvm::Record *EnumerationSpec = G.EnumerationSpecMap[Name];
    OS << "  " << EnumerationSpec->getValueAsString("Name");
    auto Value = EnumerationSpec->getValueAsString("Value");
    if (Value == "__default__") {
      OS << ",\n";
    } else {
      OS << " = " << Value << ",\n";
    }
  }
  if (G.Enumerations.size() != 0)
    OS << "};\n\n";

  // Collect and declare macros for attributes
  AttributeMap MacroAttr =
      collectAttributeMacros(G.FunctionSpecMap, EntrypointNameList);
  emitAttributeMacroDecls(MacroAttr, OS);

  OS << "__BEGIN_C_DECLS\n\n";
  for (auto &Name : EntrypointNameList) {
    auto Iter = G.FunctionSpecMap.find(Name);

    // Functions that aren't in this header file are skipped as
    // opposed to erroring out because the list of functions being
    // iterated over is the complete list of functions with
    // entrypoints. Thus this is filtering out the functions that
    // don't go to this header file, whereas the other, similar
    // conditionals above are more of a sanity check.
    if (Iter == G.FunctionSpecMap.end())
      continue;

    const llvm::Record *FunctionSpec = Iter->second;
    const llvm::Record *RetValSpec = FunctionSpec->getValueAsDef("Return");
    const llvm::Record *ReturnType = RetValSpec->getValueAsDef("ReturnType");

    // TODO: https://github.com/llvm/llvm-project/issues/81208
    //   Ideally, we should group functions based on their guarding macros.
    bool Guarded =
        (FunctionSpec->getType()->getAsString() == "GuardedFunctionSpec");

    if (Guarded)
      OS << "#ifdef " << FunctionSpec->getValueAsString("Guard") << "\n";

    // Emit attribute macros for the function. Space is automatically added.
    emitAttributeMacroForFunction(FunctionSpec, OS);
    OS << G.getTypeAsString(ReturnType) << " " << Name << "(";

    auto ArgsList = FunctionSpec->getValueAsListOfDefs("Args");
    for (size_t i = 0; i < ArgsList.size(); ++i) {
      const llvm::Record *ArgType = ArgsList[i]->getValueAsDef("ArgType");
      OS << G.getTypeAsString(ArgType);
      if (i < ArgsList.size() - 1)
        OS << ", ";
    }

    OS << ") __NOEXCEPT;\n";

    if (Guarded)
      OS << "#endif // " << FunctionSpec->getValueAsString("Guard") << "\n";

    OS << "\n";
  }

  // Make another pass over entrypoints to emit object declarations.
  for (const auto &Name : EntrypointNameList) {
    auto Iter = G.ObjectSpecMap.find(Name);
    if (Iter == G.ObjectSpecMap.end())
      continue;
    const llvm::Record *ObjectSpec = Iter->second;
    auto Type = ObjectSpec->getValueAsString("Type");
    OS << "extern " << Type << " " << Name << ";\n";
  }
  OS << "__END_C_DECLS\n";

  // Undef file-level attribute macros.
  emitUndefsForAttributeMacros(MacroAttr, OS);
}

void writePublicAPI(llvm::raw_ostream &OS, const llvm::RecordKeeper &Records) {}

const char PublicAPICommand::Name[] = "public_api";

void PublicAPICommand::run(llvm::raw_ostream &OS, const ArgVector &Args,
                           llvm::StringRef StdHeader,
                           const llvm::RecordKeeper &Records,
                           const Command::ErrorReporter &Reporter) const {
  if (Args.size() != 0)
    Reporter.printFatalError("public_api command does not take any arguments.");

  APIIndexer G(StdHeader, Records);
  writeAPIFromIndex(G, EntrypointNameList, OS);
}

} // namespace llvm_libc