• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
isa(llvm::Record * Def,llvm::Record * TypeClass)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 
isaNamedType(llvm::Record * Def)38 bool APIIndexer::isaNamedType(llvm::Record *Def) {
39   return isa(Def, NamedTypeClass);
40 }
41 
isaStructType(llvm::Record * Def)42 bool APIIndexer::isaStructType(llvm::Record *Def) {
43   return isa(Def, StructClass);
44 }
45 
isaPtrType(llvm::Record * Def)46 bool APIIndexer::isaPtrType(llvm::Record *Def) {
47   return isa(Def, PtrTypeClass);
48 }
49 
isaConstType(llvm::Record * Def)50 bool APIIndexer::isaConstType(llvm::Record *Def) {
51   return isa(Def, ConstTypeClass);
52 }
53 
isaRestrictedPtrType(llvm::Record * Def)54 bool APIIndexer::isaRestrictedPtrType(llvm::Record *Def) {
55   return isa(Def, RestrictedPtrTypeClass);
56 }
57 
isaStandardSpec(llvm::Record * Def)58 bool APIIndexer::isaStandardSpec(llvm::Record *Def) {
59   return isa(Def, StandardSpecClass);
60 }
61 
isaPublicAPI(llvm::Record * Def)62 bool APIIndexer::isaPublicAPI(llvm::Record *Def) {
63   return isa(Def, PublicAPIClass);
64 }
65 
getTypeAsString(llvm::Record * TypeRecord)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 
indexStandardSpecDef(llvm::Record * StandardSpec)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         auto FunctionName = std::string(FunctionSpec->getValueAsString("Name"));
101         FunctionSpecMap[FunctionName] = FunctionSpec;
102         FunctionToHeaderMap[FunctionName] = std::string(Header);
103       }
104 
105       auto EnumerationSpecList =
106           HeaderSpec->getValueAsListOfDefs("Enumerations");
107       for (llvm::Record *EnumerationSpec : EnumerationSpecList) {
108         EnumerationSpecMap[std::string(
109             EnumerationSpec->getValueAsString("Name"))] = EnumerationSpec;
110       }
111     }
112   }
113 }
114 
indexPublicAPIDef(llvm::Record * PublicAPI)115 void APIIndexer::indexPublicAPIDef(llvm::Record *PublicAPI) {
116   // While indexing the public API, we do not check if any of the entities
117   // requested is from an included standard. Such a check is done while
118   // generating the API.
119   auto MacroDefList = PublicAPI->getValueAsListOfDefs("Macros");
120   for (llvm::Record *MacroDef : MacroDefList)
121     MacroDefsMap[std::string(MacroDef->getValueAsString("Name"))] = MacroDef;
122 
123   auto TypeDeclList = PublicAPI->getValueAsListOfDefs("TypeDeclarations");
124   for (llvm::Record *TypeDecl : TypeDeclList)
125     TypeDeclsMap[std::string(TypeDecl->getValueAsString("Name"))] = TypeDecl;
126 
127   auto StructList = PublicAPI->getValueAsListOfStrings("Structs");
128   for (llvm::StringRef StructName : StructList)
129     Structs.insert(std::string(StructName));
130 
131   auto FunctionList = PublicAPI->getValueAsListOfStrings("Functions");
132   for (llvm::StringRef FunctionName : FunctionList)
133     Functions.insert(std::string(FunctionName));
134 
135   auto EnumerationList = PublicAPI->getValueAsListOfStrings("Enumerations");
136   for (llvm::StringRef EnumerationName : EnumerationList)
137     Enumerations.insert(std::string(EnumerationName));
138 }
139 
index(llvm::RecordKeeper & Records)140 void APIIndexer::index(llvm::RecordKeeper &Records) {
141   NamedTypeClass = Records.getClass(NamedTypeClassName);
142   PtrTypeClass = Records.getClass(PtrTypeClassName);
143   RestrictedPtrTypeClass = Records.getClass(RestrictedPtrTypeClassName);
144   StructClass = Records.getClass(StructTypeClassName);
145   ConstTypeClass = Records.getClass(ConstTypeClassName);
146   StandardSpecClass = Records.getClass(StandardSpecClassName);
147   PublicAPIClass = Records.getClass(PublicAPIClassName);
148 
149   const auto &DefsMap = Records.getDefs();
150   for (auto &Pair : DefsMap) {
151     llvm::Record *Def = Pair.second.get();
152     if (isaStandardSpec(Def))
153       indexStandardSpecDef(Def);
154     if (isaPublicAPI(Def)) {
155       if (!StdHeader.hasValue() ||
156           Def->getValueAsString("HeaderName") == StdHeader)
157         indexPublicAPIDef(Def);
158     }
159   }
160 }
161 
162 } // namespace llvm_libc
163