diff --git a/libc/config/linux/api.td b/libc/config/linux/api.td --- a/libc/config/linux/api.td +++ b/libc/config/linux/api.td @@ -6,46 +6,6 @@ include "spec/posix.td" include "spec/stdc.td" -// TODO: Eliminate all TypeDecl specializations. Since we define all public -// types in their own self contained header files, the header generator can -// produce the boiler plate which pulls in the type definitions. - -def SizeT : TypeDecl<"size_t"> { - let Decl = [{ - #include - }]; -} - -def SSizeT : TypeDecl<"ssize_t"> { - let Decl = [{ - #include - }]; -} - -def StructTm: TypeDecl<"struct tm"> { - let Decl = [{ - #include - }]; -} - -def TimeT: TypeDecl<"time_t"> { - let Decl = [{ - #include - }]; -} - -def OffT : TypeDecl<"off_t"> { - let Decl = [{ - #include - }]; -} - -def FILE : TypeDecl<"FILE"> { - let Decl = [{ - #include - }]; -} - def AssertMacro : MacroDef<"assert"> { let Defn = [{ #undef assert @@ -102,16 +62,8 @@ def CTypeAPI : PublicAPI<"ctype.h"> { } -def IMaxDivT : TypeDecl<"imaxdiv_t"> { - let Decl = [{ - #include - }]; -} - def IntTypesAPI : PublicAPI<"inttypes.h"> { - let TypeDeclarations = [ - IMaxDivT, - ]; + let Types = ["imaxdiv_t"]; } def MathErrHandlingMacro : MacroDef<"math_errhandling"> { @@ -146,18 +98,6 @@ }]; } -def FloatT : TypeDecl<"float_t"> { - let Decl = [{ - #include - }]; -} - -def DoubleT : TypeDecl<"double_t"> { - let Decl = [{ - #include - }]; -} - def MathAPI : PublicAPI<"math.h"> { let Macros = [ SimpleMacroDef<"MATH_ERRNO", "1">, @@ -174,22 +114,7 @@ IsInfMacro, IsNanMacro, ]; - let TypeDeclarations = [ - DoubleT, - FloatT, - ]; -} - -def FEnvT : TypeDecl<"fenv_t"> { - let Decl = [{ - #include - }]; -} - -def FExceptT : TypeDecl<"fexcept_t"> { - let Decl = [{ - #include - }]; + let Types = ["double_t", "float_t"]; } def FenvAPI: PublicAPI<"fenv.h"> { @@ -208,16 +133,11 @@ SimpleMacroDef<"FE_DFL_ENV", "((fenv_t *)-1)">, ]; - let TypeDeclarations = [ - FEnvT, - FExceptT, - ]; + let Types = ["fenv_t", "fexcept_t"]; } def StringAPI : PublicAPI<"string.h"> { - let TypeDeclarations = [ - SizeT, - ]; + let Types = ["size_t"]; let Macros = [ NullMacro, @@ -225,66 +145,22 @@ } def StdIOAPI : PublicAPI<"stdio.h"> { - let TypeDeclarations = [ - SizeT, - FILE, - ]; -} - -def DivT : TypeDecl<"div_t"> { - let Decl = [{ - #include - }]; -} - -def LDivT : TypeDecl<"ldiv_t"> { - let Decl = [{ - #include - }]; -} - -def LLDivT : TypeDecl<"lldiv_t"> { - let Decl = [{ - #include - }]; -} - -def BSearchCompareTDefn : TypeDecl<"__bsearchcompare_t"> { - let Decl = [{ - #include - }]; -} - -def QSortCompareTDefn : TypeDecl<"__qsortcompare_t"> { - let Decl = [{ - #include - }]; + let Types = ["size_t", "FILE"]; } def StdlibAPI : PublicAPI<"stdlib.h"> { - let TypeDeclarations = [ - DivT, - LDivT, - LLDivT, - SizeT, - BSearchCompareTDefn, - QSortCompareTDefn, + let Types = [ + "div_t", + "ldiv_t", + "lldiv_t", + "size_t", + "__bsearchcompare_t", + "__qsortcompare_t" ]; } def TimeAPI : PublicAPI<"time.h"> { - let TypeDeclarations = [ - StructTm, - TimeT, - ]; - - let Functions = [ - "asctime", - "asctime_r", - "gmtime", - "gmtime_r", - "mktime", - ]; + let Types = ["time_t", "struct tm"]; } def ErrnoAPI : PublicAPI<"errno.h"> { @@ -307,6 +183,7 @@ } def SysMManAPI : PublicAPI<"sys/mman.h"> { + let Types = ["off_t", "size_t"]; let Macros = [ SimpleMacroDef<"PROT_NONE", "0">, SimpleMacroDef<"PROT_READ", "1">, @@ -326,65 +203,10 @@ // TODO: Add other MAP_* macros used by Linux. ]; - let TypeDeclarations = [ - SizeT, - OffT, - ]; -} - -def StructSigactionDefn : TypeDecl<"struct sigaction"> { - let Decl = [{ - #include - }]; -} - -def SighandlerTDefn : TypeDecl<"__sighandler_t"> { - let Decl = [{ - #include - }]; } def SignalAPI : PublicAPI<"signal.h"> { - let TypeDeclarations = [ - StructSigactionDefn, - SighandlerTDefn, - ]; -} - -def OnceFlag : TypeDecl<"once_flag"> { - let Decl = [{ - #include - }]; -} - -def MtxT : TypeDecl<"mtx_t"> { - let Decl = [{ - #include - }]; -} - -def CndT : TypeDecl<"cnd_t"> { - let Decl = [{ - #include - }]; -} - -def ThrdT : TypeDecl<"thrd_t"> { - let Decl = [{ - #include - }]; -} - -def ThreadStartT : TypeDecl<"thrd_start_t"> { - let Decl = [{ - #include - }]; -} - -def CallOnceFuncT : TypeDecl<"__call_once_func_t"> { - let Decl = [{ - #include - }]; + let Types = ["struct sigaction", "__sighandler_t"]; } def ThreadsAPI : PublicAPI<"threads.h"> { @@ -392,13 +214,13 @@ SimpleMacroDef<"ONCE_FLAG_INIT", "0">, ]; - let TypeDeclarations = [ - OnceFlag, - CallOnceFuncT, - MtxT, - CndT, - ThrdT, - ThreadStartT, + let Types = [ + "__call_once_func_t", + "once_flag", + "cnd_t", + "mtx_t", + "thrd_t", + "thrd_start_t", ]; let Enumerations = [ @@ -414,8 +236,5 @@ } def UniStdAPI : PublicAPI<"unistd.h"> { - let TypeDeclarations = [ - SSizeT, - SizeT, - ]; + let Types = ["size_t", "ssize_t"]; } diff --git a/libc/config/public_api.td b/libc/config/public_api.td --- a/libc/config/public_api.td +++ b/libc/config/public_api.td @@ -1,10 +1,5 @@ include "spec/spec.td" -class TypeDecl { - string Name = name; - string Decl = ""; -} - class MacroDef { string Name = name; string Defn = ""; @@ -23,7 +18,7 @@ class PublicAPI { string HeaderName = name; list Macros = []; - list TypeDeclarations = []; + list Types = []; list Enumerations = []; list Structs = []; list Functions = []; diff --git a/libc/utils/HdrGen/PublicAPICommand.cpp b/libc/utils/HdrGen/PublicAPICommand.cpp --- a/libc/utils/HdrGen/PublicAPICommand.cpp +++ b/libc/utils/HdrGen/PublicAPICommand.cpp @@ -10,6 +10,7 @@ #include "utils/LibcTableGenUtil/APIIndexer.h" +#include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringExtras.h" #include "llvm/ADT/StringRef.h" #include "llvm/Support/SourceMgr.h" @@ -38,6 +39,12 @@ } } +static std::string getTypeHdrName(const std::string &Name) { + llvm::SmallVector Parts; + llvm::SplitString(llvm::StringRef(Name), Parts); + return llvm::join(Parts.begin(), Parts.end(), "_"); +} + namespace llvm_libc { void writeAPIFromIndex(APIIndexer &G, @@ -54,16 +61,12 @@ OS << '\n'; } - for (auto &Pair : G.TypeDeclsMap) { - const std::string &Name = Pair.first; - if (G.TypeSpecMap.find(Name) == G.TypeSpecMap.end()) - llvm::PrintFatalError(Name + " not found in any standard spec.\n"); - - llvm::Record *TypeDecl = Pair.second; - dedentAndWrite(TypeDecl->getValueAsString("Decl"), OS); - - OS << '\n'; + for (auto &TypeName : G.RequiredTypes) { + if (G.TypeSpecMap.find(TypeName) == G.TypeSpecMap.end()) + llvm::PrintFatalError(TypeName + " not found in any standard spec.\n"); + OS << "#include \n"; } + OS << '\n'; if (G.Enumerations.size() != 0) OS << "enum {" << '\n'; diff --git a/libc/utils/LibcTableGenUtil/APIIndexer.h b/libc/utils/LibcTableGenUtil/APIIndexer.h --- a/libc/utils/LibcTableGenUtil/APIIndexer.h +++ b/libc/utils/LibcTableGenUtil/APIIndexer.h @@ -63,10 +63,10 @@ NameToRecordMapping EnumerationSpecMap; NameToRecordMapping FunctionSpecMap; NameToRecordMapping MacroDefsMap; - NameToRecordMapping TypeDeclsMap; std::unordered_map FunctionToHeaderMap; + NameSet RequiredTypes; NameSet Structs; NameSet Enumerations; NameSet Functions; diff --git a/libc/utils/LibcTableGenUtil/APIIndexer.cpp b/libc/utils/LibcTableGenUtil/APIIndexer.cpp --- a/libc/utils/LibcTableGenUtil/APIIndexer.cpp +++ b/libc/utils/LibcTableGenUtil/APIIndexer.cpp @@ -120,9 +120,9 @@ for (llvm::Record *MacroDef : MacroDefList) MacroDefsMap[std::string(MacroDef->getValueAsString("Name"))] = MacroDef; - auto TypeDeclList = PublicAPI->getValueAsListOfDefs("TypeDeclarations"); - for (llvm::Record *TypeDecl : TypeDeclList) - TypeDeclsMap[std::string(TypeDecl->getValueAsString("Name"))] = TypeDecl; + auto TypeList = PublicAPI->getValueAsListOfStrings("Types"); + for (llvm::StringRef TypeName : TypeList) + RequiredTypes.insert(std::string(TypeName)); auto StructList = PublicAPI->getValueAsListOfStrings("Structs"); for (llvm::StringRef StructName : StructList)