diff options
author | Tyson Roberts <nall@google.com> | 2018-02-22 11:34:53 +0900 |
---|---|---|
committer | Tyson Roberts <nall@google.com> | 2018-02-22 13:46:10 +0900 |
commit | 87c4001006a9b5d49b06a31fb73389684126dd59 (patch) | |
tree | 6caf432a1a194f1c249e70e875dab64460a9a4dc /src/compiler/objective_c_plugin.cc | |
parent | d555bd85435ea79f74a58c519dd71a5bb65c7f7a (diff) |
Updates Objective-C Generation to allow for ommission of the generated @interface and @implementation
Diffstat (limited to 'src/compiler/objective_c_plugin.cc')
-rw-r--r-- | src/compiler/objective_c_plugin.cc | 168 |
1 files changed, 97 insertions, 71 deletions
diff --git a/src/compiler/objective_c_plugin.cc b/src/compiler/objective_c_plugin.cc index d5d488e84d..398bbc25a7 100644 --- a/src/compiler/objective_c_plugin.cc +++ b/src/compiler/objective_c_plugin.cc @@ -30,11 +30,58 @@ using ::google::protobuf::compiler::objectivec:: IsProtobufLibraryBundledProtoFile; using ::google::protobuf::compiler::objectivec::ProtobufLibraryFrameworkName; +namespace { + +inline ::grpc::string LocalImport(const ::grpc::string &import) { + return ::grpc::string("#import \"" + import + "\"\n"); +} + +inline ::grpc::string SystemImport(const ::grpc::string &import) { + return ::grpc::string("#import <" + import + ">\n"); +} + +inline ::grpc::string PreprocIf(const ::grpc::string& condition, + const ::grpc::string& if_true) { + return ::grpc::string("#if " + condition + "\n" + if_true + "#endif\n"); +} + +inline ::grpc::string PreprocIfElse(const ::grpc::string& condition, + const ::grpc::string& if_true, + const ::grpc::string& if_false) { + return ::grpc::string("#if " + condition + "\n" + + if_true + "#else\n" + if_false + "#endif\n"); +} + +inline ::grpc::string ImportProtoHeaders( + const grpc::protobuf::FileDescriptor* dep, + const char *indent) { + ::grpc::string header = grpc_objective_c_generator::MessageHeaderName(dep); + + if (!IsProtobufLibraryBundledProtoFile(dep)) { + return indent + LocalImport(header); + } + + ::grpc::string base_name = header; + grpc_generator::StripPrefix(&base_name, "google/protobuf/"); + // create the import code snippet + ::grpc::string framework_header = + ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name; + + static const ::grpc::string kFrameworkImportsCondition = + "GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS"; + return PreprocIfElse(kFrameworkImportsCondition, + indent + SystemImport(framework_header), + indent + LocalImport(header)); +} + +} // namespace + class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { public: ObjectiveCGrpcGenerator() {} virtual ~ObjectiveCGrpcGenerator() {} + public: virtual bool Generate(const grpc::protobuf::FileDescriptor* file, const ::grpc::string& parameter, grpc::protobuf::compiler::GeneratorContext* context, @@ -46,95 +93,70 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { ::grpc::string file_name = google::protobuf::compiler::objectivec::FilePath(file); - ::grpc::string prefix = file->options().objc_class_prefix(); + static const ::grpc::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n"; + static const ::grpc::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n"; + static const ::grpc::string kForwardDeclareCondition = + "GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO"; + static const ::grpc::string kOmitInterfaceCondition = + "GPB_GRPC_OMIT_INTERFACE"; { // Generate .pbrpc.h - ::grpc::string imports = - ::grpc::string("#if !GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO\n") + - "#import \"" + file_name + - ".pbobjc.h\"\n" - "#endif\n\n" - "#import <ProtoRPC/ProtoService.h>\n" - "#import <ProtoRPC/ProtoRPC.h>\n" - "#import <RxLibrary/GRXWriteable.h>\n" - "#import <RxLibrary/GRXWriter.h>\n"; - - ::grpc::string proto_imports; - proto_imports += "#if GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO\n" + - grpc_objective_c_generator::GetAllMessageClasses(file) + - "#else\n"; + ::grpc::string imports = LocalImport(file_name + ".pbobjc.h"); + + ::grpc::string system_imports = + SystemImport("ProtoRPC/ProtoService.h") + + SystemImport("ProtoRPC/ProtoRPC.h") + + SystemImport("RxLibrary/GRXWriteable.h") + + SystemImport("RxLibrary/GRXWriter.h"); + + ::grpc::string forward_declarations = "@class GRPCProtoCall;\n\n"; + + ::grpc::string class_declarations = + grpc_objective_c_generator::GetAllMessageClasses(file); + + ::grpc::string class_imports; for (int i = 0; i < file->dependency_count(); i++) { - ::grpc::string header = - grpc_objective_c_generator::MessageHeaderName(file->dependency(i)); - const grpc::protobuf::FileDescriptor* dependency = file->dependency(i); - if (IsProtobufLibraryBundledProtoFile(dependency)) { - ::grpc::string base_name = header; - grpc_generator::StripPrefix(&base_name, "google/protobuf/"); - // create the import code snippet - proto_imports += - " #if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n" - " #import <" + - ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name + - ">\n" - " #else\n" - " #import \"" + - header + - "\"\n" - " #endif\n"; - } else { - proto_imports += ::grpc::string(" #import \"") + header + "\"\n"; - } + class_imports += ImportProtoHeaders(file->dependency(i), " "); } - proto_imports += "#endif\n"; - ::grpc::string declarations; + ::grpc::string protocols; for (int i = 0; i < file->service_count(); i++) { const grpc::protobuf::ServiceDescriptor* service = file->service(i); - declarations += grpc_objective_c_generator::GetHeader(service); + protocols += grpc_objective_c_generator::GetProtocol(service); } - static const ::grpc::string kNonNullBegin = - "\nNS_ASSUME_NONNULL_BEGIN\n\n"; - static const ::grpc::string kNonNullEnd = "\nNS_ASSUME_NONNULL_END\n"; + ::grpc::string interfaces; + for (int i = 0; i < file->service_count(); i++) { + const grpc::protobuf::ServiceDescriptor* service = file->service(i); + interfaces += grpc_objective_c_generator::GetInterface(service); + } Write(context, file_name + ".pbrpc.h", - imports + '\n' + proto_imports + '\n' + kNonNullBegin + - declarations + kNonNullEnd); + PreprocIf("!" + kForwardDeclareCondition, imports) + "\n" + + PreprocIf("!" + kOmitInterfaceCondition, system_imports) + "\n" + + PreprocIfElse(kForwardDeclareCondition, + class_declarations, class_imports) + "\n" + + forward_declarations + "\n" + + kNonNullBegin + "\n" + + protocols + "\n" + + PreprocIf("!" + kOmitInterfaceCondition, interfaces) + "\n" + + kNonNullEnd + "\n"); } { // Generate .pbrpc.m - ::grpc::string imports = ::grpc::string("#import \"") + file_name + - ".pbrpc.h\"\n" - "#import \"" + - file_name + - ".pbobjc.h\"\n\n" - "#import <ProtoRPC/ProtoRPC.h>\n" - "#import <RxLibrary/GRXWriter+Immediate.h>\n"; + ::grpc::string imports = + LocalImport(file_name + ".pbrpc.h") + + LocalImport(file_name + ".pbobjc.h") + + SystemImport("ProtoRPC/ProtoRPC.h") + + SystemImport("RxLibrary/GRXWriter+Immediate.h"); + + ::grpc::string class_imports; for (int i = 0; i < file->dependency_count(); i++) { - ::grpc::string header = - grpc_objective_c_generator::MessageHeaderName(file->dependency(i)); - const grpc::protobuf::FileDescriptor* dependency = file->dependency(i); - if (IsProtobufLibraryBundledProtoFile(dependency)) { - ::grpc::string base_name = header; - grpc_generator::StripPrefix(&base_name, "google/protobuf/"); - // create the import code snippet - imports += - "#if GPB_USE_PROTOBUF_FRAMEWORK_IMPORTS\n" - " #import <" + - ::grpc::string(ProtobufLibraryFrameworkName) + "/" + base_name + - ">\n" - "#else\n" - " #import \"" + - header + - "\"\n" - "#endif\n"; - } else { - imports += ::grpc::string("#import \"") + header + "\"\n"; - } + class_imports += ImportProtoHeaders(file->dependency(i), ""); } ::grpc::string definitions; @@ -143,7 +165,11 @@ class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator { definitions += grpc_objective_c_generator::GetSource(service); } - Write(context, file_name + ".pbrpc.m", imports + '\n' + definitions); + Write(context, file_name + ".pbrpc.m", + PreprocIf("!" + kOmitInterfaceCondition, + imports + "\n" + + class_imports + "\n" + + definitions)); } return true; |