aboutsummaryrefslogtreecommitdiff
path: root/offload/tools/offload-tblgen/DocGen.cpp
blob: 56526112f69ebf8755493440d7ff6b269a7e7605 (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
//===- offload-tblgen/DocGen.cpp - Tablegen backend for Offload header ----===//
//
// 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 is a Tablegen backend that produces the contents of the Offload API
// specification. The generated reStructuredText is Sphinx compatible, see
// https://www.sphinx-doc.org/en/master/usage/domains/c.html for further
// details on the C language domain.
//
//===----------------------------------------------------------------------===//

#include "llvm/ADT/StringExtras.h"
#include "llvm/Support/FormatVariadic.h"
#include "llvm/TableGen/Record.h"
#include "llvm/TableGen/TableGenBackend.h"

#include "GenCommon.hpp"
#include "RecordTypes.hpp"

using namespace llvm;
using namespace offload::tblgen;

namespace {
std::string makeFunctionSignature(StringRef RetTy, StringRef Name,
                                  ArrayRef<ParamRec> Params) {
  std::string S;
  raw_string_ostream OS{S};
  OS << RetTy << " " << Name << "(";
  for (const ParamRec &Param : Params) {
    OS << Param.getType() << " " << Param.getName();
    if (Param != Params.back()) {
      OS << ", ";
    }
  }
  OS << ")";
  return S;
}

std::string makeDoubleBackticks(StringRef R) {
  std::string S;
  for (char C : R) {
    if (C == '`') {
      S.push_back('`');
    }
    S.push_back(C);
  }
  return S;
}

void processMacro(const MacroRec &M, raw_ostream &OS) {
  OS << formatv(".. c:macro:: {0}\n\n", M.getNameWithArgs());
  OS << "  " << M.getDesc() << "\n\n";
}

void processTypedef(const TypedefRec &T, raw_ostream &OS) {
  OS << formatv(".. c:type:: {0} {1}\n\n", T.getValue(), T.getName());
  OS << "  " << T.getDesc() << "\n\n";
}

void processHandle(const HandleRec &H, raw_ostream &OS) {

  OS << formatv(".. c:type:: struct {0} *{1}\n\n", getHandleImplName(H),
                H.getName());
  OS << "  " << H.getDesc() << "\n\n";
}

void processFptrTypedef(const FptrTypedefRec &F, raw_ostream &OS) {
  OS << ".. c:type:: "
     << makeFunctionSignature(F.getReturn(),
                              StringRef{formatv("(*{0})", F.getName())},
                              F.getParams())
     << "\n\n";
  for (const ParamRec &P : F.getParams()) {
    OS << formatv("  :param {0}: {1}\n", P.getName(), P.getDesc());
  }
  OS << "\n";
}

void processEnum(const EnumRec &E, raw_ostream &OS) {
  OS << formatv(".. c:enum:: {0}\n\n", E.getName());
  OS << "  " << E.getDesc() << "\n\n";
  for (const EnumValueRec Etor : E.getValues()) {
    OS << formatv("  .. c:enumerator:: {0}_{1}\n\n", E.getEnumValNamePrefix(),
                  Etor.getName());
    OS << "    ";
    if (E.isTyped()) {
      OS << ":c:expr:`" << Etor.getTaggedType() << "` — ";
    }
    OS << Etor.getDesc() << "\n\n";
  }
}

void processStruct(const StructRec &S, raw_ostream &OS) {
  OS << formatv(".. c:struct:: {0}\n\n", S.getName());
  OS << "  " << S.getDesc() << "\n\n";
  for (const StructMemberRec &M : S.getMembers()) {
    OS << formatv("  .. c:member:: {0} {1}\n\n", M.getType(), M.getName());
    OS << "    " << M.getDesc() << "\n\n";
  }
}

void processFunction(const FunctionRec &F, raw_ostream &OS) {
  OS << ".. c:function:: "
     << makeFunctionSignature({formatv("{0}_result_t", PrefixLower)},
                              F.getName(), F.getParams())
     << "\n\n";

  OS << "  " << F.getDesc() << "\n\n";
  for (StringRef D : F.getDetails()) {
    OS << "  " << D << "\n";
  }
  if (!F.getDetails().empty()) {
    OS << "\n";
  }

  for (const ParamRec &P : F.getParams()) {
    OS << formatv("  :param {0}: {1}\n", P.getName(), P.getDesc());
  }

  for (const ReturnRec &R : F.getReturns()) {
    OS << formatv("  :retval {0}:\n", R.getValue());
    for (StringRef C : R.getConditions()) {
      OS << "    * ";
      if (C.starts_with("`") && C.ends_with("`")) {
        OS << ":c:expr:" << C;
      } else {
        OS << makeDoubleBackticks(C);
      }
      OS << "\n";
    }
  }
  OS << "\n";
}
} // namespace

void EmitOffloadDoc(const RecordKeeper &Records, raw_ostream &OS) {
  OS << "Offload API\n";
  OS << "===========\n\n";

  ArrayRef<const Record *> Macros = Records.getAllDerivedDefinitions("Macro");
  if (!Macros.empty()) {
    OS << "Macros\n";
    OS << "------\n\n";
    for (const Record *M : Macros) {
      processMacro(MacroRec{M}, OS);
    }
  }

  ArrayRef<const Record *> Handles = Records.getAllDerivedDefinitions("Handle");
  ArrayRef<const Record *> Typedefs =
      Records.getAllDerivedDefinitions("Typedef");
  ArrayRef<const Record *> FptrTypedefs =
      Records.getAllDerivedDefinitions("FptrTypedef");
  if (!Handles.empty() || !Typedefs.empty() || !FptrTypedefs.empty()) {
    OS << "Type Definitions\n";
    OS << "----------------\n\n";
    for (const Record *H : Handles) {
      processHandle(HandleRec{H}, OS);
    }
    for (const Record *T : Typedefs) {
      processTypedef(TypedefRec{T}, OS);
    }
    for (const Record *F : FptrTypedefs) {
      processFptrTypedef(FptrTypedefRec{F}, OS);
    }
  }

  ArrayRef<const Record *> Enums = Records.getAllDerivedDefinitions("Enum");
  OS << "Enums\n";
  OS << "-----\n\n";
  if (!Enums.empty()) {
    for (const Record *E : Enums) {
      processEnum(EnumRec{E}, OS);
    }
  }

  ArrayRef<const Record *> Structs = Records.getAllDerivedDefinitions("Struct");
  if (!Structs.empty()) {
    OS << "Structs\n";
    OS << "-------\n\n";
    for (const Record *S : Structs) {
      processStruct(StructRec{S}, OS);
    }
  }

  ArrayRef<const Record *> Functions =
      Records.getAllDerivedDefinitions("Function");
  if (!Functions.empty()) {
    OS << "Functions\n";
    OS << "---------\n\n";
    for (const Record *F : Functions) {
      processFunction(FunctionRec{F}, OS);
    }
  }
}