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 
16 namespace llvm_libc {
17 
18 static const char NamedTypeClassName[] = "NamedType";
19 static const char PtrTypeClassName[] = "PtrType";
20 static const char RestrictedPtrTypeClassName[] = "RestrictedPtrType";
21 static const char ConstTypeClassName[] = "ConstType";
22 static const char StructTypeClassName[] = "Struct";
23 
24 static const char StandardSpecClassName[] = "StandardSpec";
25 static const char PublicAPIClassName[] = "PublicAPI";
26 
27 static bool isa(llvm::Record *Def, llvm::Record *TypeClass) {
28   llvm::RecordRecTy *RecordType = Def->getType();
29   llvm::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..
33   if (Classes.size() != 1)
34     return false;
35   return Classes[0] == TypeClass;
36 }
37 
38 bool APIIndexer::isaNamedType(llvm::Record *Def) {
39   return isa(Def, NamedTypeClass);
40 }
41 
42 bool APIIndexer::isaStructType(llvm::Record *Def) {
43   return isa(Def, StructClass);
44 }
45 
46 bool APIIndexer::isaPtrType(llvm::Record *Def) {
47   return isa(Def, PtrTypeClass);
48 }
49 
50 bool APIIndexer::isaConstType(llvm::Record *Def) {
51   return isa(Def, ConstTypeClass);
52 }
53 
54 bool APIIndexer::isaRestrictedPtrType(llvm::Record *Def) {
55   return isa(Def, RestrictedPtrTypeClass);
56 }
57 
58 bool APIIndexer::isaStandardSpec(llvm::Record *Def) {
59   return isa(Def, StandardSpecClass);
60 }
61 
62 bool APIIndexer::isaPublicAPI(llvm::Record *Def) {
63   return isa(Def, PublicAPIClass);
64 }
65 
66 std::string APIIndexer::getTypeAsString(llvm::Record *TypeRecord) {
67   if (isaNamedType(TypeRecord) || isaStructType(TypeRecord)) {
68     return std::string(TypeRecord->getValueAsString("Name"));
69   } else if (isaPtrType(TypeRecord)) {
70     return getTypeAsString(TypeRecord->getValueAsDef("PointeeType")) + " *";
71   } else if (isaConstType(TypeRecord)) {
72     return std::string("const ") +
73            getTypeAsString(TypeRecord->getValueAsDef("UnqualifiedType"));
74   } else if (isaRestrictedPtrType(TypeRecord)) {
75     return getTypeAsString(TypeRecord->getValueAsDef("PointeeType")) +
76            " *__restrict";
77   } else {
78     llvm::PrintFatalError(TypeRecord->getLoc(), "Invalid type.\n");
79   }
80 }
81 
82 void APIIndexer::indexStandardSpecDef(llvm::Record *StandardSpec) {
83   auto HeaderSpecList = StandardSpec->getValueAsListOfDefs("Headers");
84   for (llvm::Record *HeaderSpec : HeaderSpecList) {
85     llvm::StringRef Header = HeaderSpec->getValueAsString("Name");
86     if (!StdHeader.hasValue() || Header == StdHeader) {
87       PublicHeaders.emplace(Header);
88       auto MacroSpecList = HeaderSpec->getValueAsListOfDefs("Macros");
89       // TODO: Trigger a fatal error on duplicate specs.
90       for (llvm::Record *MacroSpec : MacroSpecList)
91         MacroSpecMap[std::string(MacroSpec->getValueAsString("Name"))] =
92             MacroSpec;
93 
94       auto TypeSpecList = HeaderSpec->getValueAsListOfDefs("Types");
95       for (llvm::Record *TypeSpec : TypeSpecList)
96         TypeSpecMap[std::string(TypeSpec->getValueAsString("Name"))] = TypeSpec;
97 
98       auto FunctionSpecList = HeaderSpec->getValueAsListOfDefs("Functions");
99       for (llvm::Record *FunctionSpec : FunctionSpecList) {
100         FunctionSpecMap[std::string(FunctionSpec->getValueAsString("Name"))] =
101             FunctionSpec;
102       }
103 
104       auto EnumerationSpecList =
105           HeaderSpec->getValueAsListOfDefs("Enumerations");
106       for (llvm::Record *EnumerationSpec : EnumerationSpecList) {
107         EnumerationSpecMap[std::string(
108             EnumerationSpec->getValueAsString("Name"))] = EnumerationSpec;
109       }
110     }
111   }
112 }
113 
114 void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
115   // While indexing the public API, we do not check if any of the entities
116   // requested is from an included standard. Such a check is done while
117   // generating the API.
118   auto MacroDefList = PublicAPI->getValueAsListOfDefs("Macros");
119   for (llvm::Record *MacroDef : MacroDefList)
120     MacroDefsMap[std::string(MacroDef->getValueAsString("Name"))] = MacroDef;
121 
122   auto TypeDeclList = PublicAPI->getValueAsListOfDefs("TypeDeclarations");
123   for (llvm::Record *TypeDecl : TypeDeclList)
124     TypeDeclsMap[std::string(TypeDecl->getValueAsString("Name"))] = TypeDecl;
125 
126   auto StructList = PublicAPI->getValueAsListOfStrings("Structs");
127   for (llvm::StringRef StructName : StructList)
128     Structs.insert(std::string(StructName));
129 
130   auto FunctionList = PublicAPI->getValueAsListOfStrings("Functions");
131   for (llvm::StringRef FunctionName : FunctionList)
132     Functions.insert(std::string(FunctionName));
133 
134   auto EnumerationList = PublicAPI->getValueAsListOfStrings("Enumerations");
135   for (llvm::StringRef EnumerationName : EnumerationList)
136     Enumerations.insert(std::string(EnumerationName));
137 }
138 
139 void APIIndexer::index(llvm::RecordKeeper &Records) {
140   NamedTypeClass = Records.getClass(NamedTypeClassName);
141   PtrTypeClass = Records.getClass(PtrTypeClassName);
142   RestrictedPtrTypeClass = Records.getClass(RestrictedPtrTypeClassName);
143   StructClass = Records.getClass(StructTypeClassName);
144   ConstTypeClass = Records.getClass(ConstTypeClassName);
145   StandardSpecClass = Records.getClass(StandardSpecClassName);
146   PublicAPIClass = Records.getClass(PublicAPIClassName);
147 
148   const auto &DefsMap = Records.getDefs();
149   for (auto &Pair : DefsMap) {
150     llvm::Record *Def = Pair.second.get();
151     if (isaStandardSpec(Def))
152       indexStandardSpecDef(Def);
153     if (isaPublicAPI(Def)) {
154       if (!StdHeader.hasValue() ||
155           Def->getValueAsString("HeaderName") == StdHeader)
156         indexPublicAPIDef(Def);
157     }
158   }
159 }
160 
161 } // namespace llvm_libc
162