Skip to content

[swift-api-extract] BumpPtrAllocate API structs #36727

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 2, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 22 additions & 20 deletions lib/TBDGen/APIGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,27 @@ namespace apigen {
void API::addSymbol(llvm::StringRef symbol, APILoc loc, APILinkage linkage,
APIFlags flags, APIAccess access,
APIAvailability availability) {
globals.emplace_back(symbol, loc, linkage, flags, access, GVKind::Function,
availability);
auto *global = new (allocator) GlobalRecord(
symbol, loc, linkage, flags, access, GVKind::Function, availability);
globals.push_back(global);
}

ObjCInterfaceRecord *API::addObjCClass(llvm::StringRef name, APILinkage linkage,
APILoc loc, APIAccess access,
APIAvailability availability,
llvm::StringRef superClassName) {
interfaces.emplace_back(name, linkage, loc, access, availability,
superClassName);
return &interfaces.back();
auto *interface = new (allocator) ObjCInterfaceRecord(
name, linkage, loc, access, availability, superClassName);
interfaces.push_back(interface);
return interface;
}

void API::addObjCMethod(ObjCInterfaceRecord *cls, llvm::StringRef name,
APILoc loc, APIAccess access, bool isInstanceMethod,
bool isOptional, APIAvailability availability) {
cls->methods.emplace_back(name, loc, access, isInstanceMethod, isOptional,
availability);
auto method = new (allocator) ObjCMethodRecord(
name, loc, access, isInstanceMethod, isOptional, availability);
cls->methods.push_back(method);
}

static void serialize(llvm::json::OStream &OS, APIAccess access) {
Expand Down Expand Up @@ -120,6 +123,10 @@ static void serialize(llvm::json::OStream &OS, const ObjCMethodRecord &record) {
});
}

static bool sortAPIRecords(const APIRecord *base, const APIRecord *compare) {
return base->name < compare->name;
}

static void serialize(llvm::json::OStream &OS,
const ObjCInterfaceRecord &record) {
OS.object([&]() {
Expand All @@ -131,39 +138,34 @@ static void serialize(llvm::json::OStream &OS,
OS.attribute("super", record.superClassName);
OS.attributeArray("instanceMethods", [&]() {
for (auto &method : record.methods) {
if (method.isInstanceMethod)
serialize(OS, method);
if (method->isInstanceMethod)
serialize(OS, *method);
}
});
OS.attributeArray("classMethods", [&]() {
for (auto &method : record.methods) {
if (!method.isInstanceMethod)
serialize(OS, method);
if (!method->isInstanceMethod)
serialize(OS, *method);
}
});
});
}

static bool sortAPIRecords(const APIRecord &base, const APIRecord &compare) {
return base.name < compare.name;
}

void API::writeAPIJSONFile(llvm::raw_ostream &os, bool PrettyPrint) {
unsigned indentSize = PrettyPrint ? 2 : 0;
llvm::json::OStream JSON(os, indentSize);

// FIXME: only write PublicSDKContentRoot now.
JSON.object([&]() {
JSON.attribute("target", target.str());
JSON.attributeArray("globals", [&]() {
llvm::sort(globals, sortAPIRecords);
for (const auto &g : globals)
serialize(JSON, g);
for (const auto *g : globals)
serialize(JSON, *g);
});
JSON.attributeArray("interfaces", [&]() {
llvm::sort(interfaces, sortAPIRecords);
for (const auto &i : interfaces)
serialize(JSON, i);
for (const auto *i : interfaces)
serialize(JSON, *i);
});
JSON.attribute("version", "1.0");
});
Expand Down
13 changes: 8 additions & 5 deletions lib/TBDGen/APIGen.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
#include "llvm/ADT/Triple.h"
#include "llvm/Support/Allocator.h"
#include "llvm/Support/Error.h"

namespace llvm {
Expand Down Expand Up @@ -86,7 +87,7 @@ struct APIRecord {

APIRecord(llvm::StringRef name, APILoc loc, APILinkage linkage,
APIFlags flags, APIAccess access, APIAvailability availability)
: name(name.str()), loc(loc), linkage(linkage), flags(flags),
: name(name.data(), name.size()), loc(loc), linkage(linkage), flags(flags),
access(access), availability(availability) {}

bool isWeakDefined() const {
Expand Down Expand Up @@ -135,7 +136,7 @@ struct ObjCMethodRecord : APIRecord {
};

struct ObjCContainerRecord : APIRecord {
std::vector<ObjCMethodRecord> methods;
std::vector<ObjCMethodRecord*> methods;

ObjCContainerRecord(llvm::StringRef name, APILinkage linkage, APILoc loc,
APIAccess access, const APIAvailability &availability)
Expand All @@ -148,12 +149,13 @@ struct ObjCInterfaceRecord : ObjCContainerRecord {
APIAccess access, APIAvailability availability,
llvm::StringRef superClassName)
: ObjCContainerRecord(name, linkage, loc, access, availability),
superClassName(superClassName.str()) {}
superClassName(superClassName.data(), superClassName.size()) {}
};

class API {
public:
API(const llvm::Triple &triple) : target(triple) {}

const llvm::Triple &getTarget() const { return target; }

void addSymbol(llvm::StringRef symbol, APILoc loc, APILinkage linkage,
Expand All @@ -174,8 +176,9 @@ class API {
private:
const llvm::Triple target;

std::vector<GlobalRecord> globals;
std::vector<ObjCInterfaceRecord> interfaces;
llvm::BumpPtrAllocator allocator;
std::vector<GlobalRecord*> globals;
std::vector<ObjCInterfaceRecord*> interfaces;
};

} // end namespace apigen
Expand Down