• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- A class to index libc API listed in tablegen files ------*- C++ -*-===//
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 #ifndef LLVM_LIBC_UTILS_LIBC_TABLE_GEN_UTILS_API_INDEXER_H
10 #define LLVM_LIBC_UTILS_LIBC_TABLE_GEN_UTILS_API_INDEXER_H
11 
12 #include "llvm/ADT/StringRef.h"
13 #include "llvm/TableGen/Record.h"
14 
15 #include <string>
16 #include <unordered_map>
17 #include <unordered_set>
18 
19 namespace llvm_libc {
20 
21 class APIIndexer {
22 private:
23   llvm::Optional<llvm::StringRef> StdHeader;
24 
25   // TableGen classes in spec.td.
26   llvm::Record *NamedTypeClass;
27   llvm::Record *PtrTypeClass;
28   llvm::Record *RestrictedPtrTypeClass;
29   llvm::Record *ConstTypeClass;
30   llvm::Record *StructClass;
31   llvm::Record *StandardSpecClass;
32   llvm::Record *PublicAPIClass;
33 
34   bool isaNamedType(llvm::Record *Def);
35   bool isaStructType(llvm::Record *Def);
36   bool isaPtrType(llvm::Record *Def);
37   bool isaConstType(llvm::Record *Def);
38   bool isaRestrictedPtrType(llvm::Record *Def);
39   bool isaStandardSpec(llvm::Record *Def);
40   bool isaPublicAPI(llvm::Record *Def);
41 
42   void indexStandardSpecDef(llvm::Record *StandardSpec);
43   void indexPublicAPIDef(llvm::Record *PublicAPI);
44   void index(llvm::RecordKeeper &Records);
45 
46 public:
47   using NameToRecordMapping = std::unordered_map<std::string, llvm::Record *>;
48   using NameSet = std::unordered_set<std::string>;
49 
50   // This indexes all headers, not just a specified one.
APIIndexer(llvm::RecordKeeper & Records)51   explicit APIIndexer(llvm::RecordKeeper &Records) : StdHeader(llvm::None) {
52     index(Records);
53   }
54 
APIIndexer(llvm::StringRef Header,llvm::RecordKeeper & Records)55   APIIndexer(llvm::StringRef Header, llvm::RecordKeeper &Records)
56       : StdHeader(Header) {
57     index(Records);
58   }
59 
60   // Mapping from names to records defining them.
61   NameToRecordMapping MacroSpecMap;
62   NameToRecordMapping TypeSpecMap;
63   NameToRecordMapping EnumerationSpecMap;
64   NameToRecordMapping FunctionSpecMap;
65   NameToRecordMapping MacroDefsMap;
66   NameToRecordMapping TypeDeclsMap;
67 
68   std::unordered_map<std::string, std::string> FunctionToHeaderMap;
69 
70   NameSet Structs;
71   NameSet Enumerations;
72   NameSet Functions;
73   NameSet PublicHeaders;
74 
75   std::string getTypeAsString(llvm::Record *TypeRecord);
76 };
77 
78 } // namespace llvm_libc
79 
80 #endif // LLVM_LIBC_UTILS_LIBC_TABLE_GEN_UTILS_API_INDEXER_H
81