llvm-project
173 строки · 6.5 Кб
1//===-- Implementation of APIIndexer class --------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "APIIndexer.h"
10
11#include "llvm/ADT/StringExtras.h"
12#include "llvm/ADT/StringRef.h"
13#include "llvm/TableGen/Error.h"
14#include "llvm/TableGen/Record.h"
15
16namespace llvm_libc {
17
18static const char NamedTypeClassName[] = "NamedType";
19static const char PtrTypeClassName[] = "PtrType";
20static const char RestrictedPtrTypeClassName[] = "RestrictedPtrType";
21static const char ConstTypeClassName[] = "ConstType";
22static const char StructTypeClassName[] = "Struct";
23
24static const char StandardSpecClassName[] = "StandardSpec";
25static const char PublicAPIClassName[] = "PublicAPI";
26
27static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
28llvm::RecordRecTy *RecordType = Def->getType();
29llvm::ArrayRef<llvm::Record *> Classes = RecordType->getClasses();
30// We want exact types. That is, we don't want the classes listed in
31// spec.td to be subclassed. Hence, we do not want the record |Def|
32// to be of more than one class type..
33if (Classes.size() != 1)
34return false;
35return Classes[0] == TypeClass;
36}
37
38bool APIIndexer::isaNamedType(llvm::Record *Def) {
39return isa(Def, NamedTypeClass);
40}
41
42bool APIIndexer::isaStructType(llvm::Record *Def) {
43return isa(Def, StructClass);
44}
45
46bool APIIndexer::isaPtrType(llvm::Record *Def) {
47return isa(Def, PtrTypeClass);
48}
49
50bool APIIndexer::isaConstType(llvm::Record *Def) {
51return isa(Def, ConstTypeClass);
52}
53
54bool APIIndexer::isaRestrictedPtrType(llvm::Record *Def) {
55return isa(Def, RestrictedPtrTypeClass);
56}
57
58bool APIIndexer::isaStandardSpec(llvm::Record *Def) {
59return isa(Def, StandardSpecClass);
60}
61
62bool APIIndexer::isaPublicAPI(llvm::Record *Def) {
63return isa(Def, PublicAPIClass);
64}
65
66std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
67if (isaNamedType(TypeRecord) || isaStructType(TypeRecord)) {
68return std::string(TypeRecord->getValueAsString("Name"));
69} else if (isaPtrType(TypeRecord)) {
70return getTypeAsString(TypeRecord->getValueAsDef("PointeeType")) + " *";
71} else if (isaConstType(TypeRecord)) {
72return std::string("const ") +
73getTypeAsString(TypeRecord->getValueAsDef("UnqualifiedType"));
74} else if (isaRestrictedPtrType(TypeRecord)) {
75return getTypeAsString(TypeRecord->getValueAsDef("PointeeType")) +
76" *__restrict";
77} else {
78llvm::PrintFatalError(TypeRecord->getLoc(), "Invalid type.\n");
79}
80}
81
82void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
83auto HeaderSpecList = StandardSpec->getValueAsListOfDefs("Headers");
84for (llvm::Record *HeaderSpec : HeaderSpecList) {
85llvm::StringRef Header = HeaderSpec->getValueAsString("Name");
86if (!StdHeader.has_value() || Header == StdHeader) {
87PublicHeaders.emplace(Header);
88auto MacroSpecList = HeaderSpec->getValueAsListOfDefs("Macros");
89// TODO: Trigger a fatal error on duplicate specs.
90for (llvm::Record *MacroSpec : MacroSpecList)
91MacroSpecMap[std::string(MacroSpec->getValueAsString("Name"))] =
92MacroSpec;
93
94auto TypeSpecList = HeaderSpec->getValueAsListOfDefs("Types");
95for (llvm::Record *TypeSpec : TypeSpecList)
96TypeSpecMap[std::string(TypeSpec->getValueAsString("Name"))] = TypeSpec;
97
98auto FunctionSpecList = HeaderSpec->getValueAsListOfDefs("Functions");
99for (llvm::Record *FunctionSpec : FunctionSpecList) {
100auto FunctionName = std::string(FunctionSpec->getValueAsString("Name"));
101FunctionSpecMap[FunctionName] = FunctionSpec;
102FunctionToHeaderMap[FunctionName] = std::string(Header);
103}
104
105auto EnumerationSpecList =
106HeaderSpec->getValueAsListOfDefs("Enumerations");
107for (llvm::Record *EnumerationSpec : EnumerationSpecList) {
108EnumerationSpecMap[std::string(
109EnumerationSpec->getValueAsString("Name"))] = EnumerationSpec;
110}
111
112auto ObjectSpecList = HeaderSpec->getValueAsListOfDefs("Objects");
113for (llvm::Record *ObjectSpec : ObjectSpecList) {
114auto ObjectName = std::string(ObjectSpec->getValueAsString("Name"));
115ObjectSpecMap[ObjectName] = ObjectSpec;
116ObjectToHeaderMap[ObjectName] = std::string(Header);
117}
118}
119}
120}
121
122void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
123// While indexing the public API, we do not check if any of the entities
124// requested is from an included standard. Such a check is done while
125// generating the API.
126auto MacroDefList = PublicAPI->getValueAsListOfDefs("Macros");
127for (llvm::Record *MacroDef : MacroDefList)
128MacroDefsMap[std::string(MacroDef->getValueAsString("Name"))] = MacroDef;
129
130auto TypeList = PublicAPI->getValueAsListOfStrings("Types");
131for (llvm::StringRef TypeName : TypeList)
132RequiredTypes.insert(std::string(TypeName));
133
134auto StructList = PublicAPI->getValueAsListOfStrings("Structs");
135for (llvm::StringRef StructName : StructList)
136Structs.insert(std::string(StructName));
137
138auto FunctionList = PublicAPI->getValueAsListOfStrings("Functions");
139for (llvm::StringRef FunctionName : FunctionList)
140Functions.insert(std::string(FunctionName));
141
142auto EnumerationList = PublicAPI->getValueAsListOfStrings("Enumerations");
143for (llvm::StringRef EnumerationName : EnumerationList)
144Enumerations.insert(std::string(EnumerationName));
145
146auto ObjectList = PublicAPI->getValueAsListOfStrings("Objects");
147for (llvm::StringRef ObjectName : ObjectList)
148Objects.insert(std::string(ObjectName));
149}
150
151void APIIndexer::index(llvm::RecordKeeper &Records) {
152NamedTypeClass = Records.getClass(NamedTypeClassName);
153PtrTypeClass = Records.getClass(PtrTypeClassName);
154RestrictedPtrTypeClass = Records.getClass(RestrictedPtrTypeClassName);
155StructClass = Records.getClass(StructTypeClassName);
156ConstTypeClass = Records.getClass(ConstTypeClassName);
157StandardSpecClass = Records.getClass(StandardSpecClassName);
158PublicAPIClass = Records.getClass(PublicAPIClassName);
159
160const auto &DefsMap = Records.getDefs();
161for (auto &Pair : DefsMap) {
162llvm::Record *Def = Pair.second.get();
163if (isaStandardSpec(Def))
164indexStandardSpecDef(Def);
165if (isaPublicAPI(Def)) {
166if (!StdHeader.has_value() ||
167Def->getValueAsString("HeaderName") == StdHeader)
168indexPublicAPIDef(Def);
169}
170}
171}
172
173} // namespace llvm_libc
174