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

source code of libc/utils/LibcTableGenUtil/APIIndexer.h