aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/compiler/objective_c_plugin.cc
blob: 16eba9a104b295f48a2c5addb9bfc0b16de8b542 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*
 *
 * Copyright 2015 gRPC authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

// Generates Objective C gRPC service interface out of Protobuf IDL.

#include <memory>

#include "src/compiler/config.h"
#include "src/compiler/objective_c_generator.h"
#include "src/compiler/objective_c_generator_helpers.h"

#include <google/protobuf/compiler/objectivec/objectivec_helpers.h>

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");
}

// Preprocessor condition flags.
using PreprocConditionFlag = uint32_t;
constexpr PreprocConditionFlag kInvertCondition = 0b0001;
constexpr PreprocConditionFlag kCheckIfDefined  = 0b0010;

// Convenience flag set.
constexpr PreprocConditionFlag kIfNotOrNotDefined =
    kInvertCondition | kCheckIfDefined;

inline ::grpc::string PreprocConditional(::grpc::string symbol,
                                         PreprocConditionFlag flags) {
  if (flags & kCheckIfDefined) {
    return (flags & kInvertCondition)
        ? "!defined(" + symbol + ") || !" + symbol
        : "defined(" + symbol + ") && " + symbol;
  } else {
    return (flags & kInvertCondition)
        ? "!" + symbol
        : symbol;
  }
}

inline ::grpc::string PreprocIf(const ::grpc::string& symbol,
                                const ::grpc::string& if_true,
                                PreprocConditionFlag flags = 0) {
  ::grpc::string condition = PreprocConditional(symbol, flags);
  return ::grpc::string("#if " + condition + "\n" + if_true + "#endif\n");
}

inline ::grpc::string PreprocIfElse(const ::grpc::string& symbol,
                                    const ::grpc::string& if_true,
                                    const ::grpc::string& if_false,
                                    PreprocConditionFlag flags = 0) {
  ::grpc::string condition = PreprocConditional(symbol, flags);
  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),
                       kCheckIfDefined);
}

}  // namespace

class ObjectiveCGrpcGenerator : public grpc::protobuf::compiler::CodeGenerator {
 public:
  ObjectiveCGrpcGenerator() {}
  virtual ~ObjectiveCGrpcGenerator() {}

 private:
  static const ::grpc::string kNonNullBegin = "NS_ASSUME_NONNULL_BEGIN\n";
  static const ::grpc::string kNonNullEnd = "NS_ASSUME_NONNULL_END\n";
  static const ::grpc::string kProtocolOnly = "GPB_GRPC_PROTOCOL_ONLY";
  static const ::grpc::string kForwardDeclare =
      "GPB_GRPC_FORWARD_DECLARE_MESSAGE_PROTO";

 public:
  virtual bool Generate(const grpc::protobuf::FileDescriptor* file,
                        const ::grpc::string& parameter,
                        grpc::protobuf::compiler::GeneratorContext* context,
                        ::grpc::string* error) const {
    if (file->service_count() == 0) {
      // No services.  Do nothing.
      return true;
    }

    auto OmitIf = [](const ::grpc::string& s, const ::grpc::string& v) {
      return PreprocIf(s, v, kInvertCondition | kCheckIfDefined);
    };

    ::grpc::string file_name =
        google::protobuf::compiler::objectivec::FilePath(file);

    {
      // Generate .pbrpc.h

      ::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++) {
        class_imports += ImportProtoHeaders(file->dependency(i), "  ");
      }

      ::grpc::string protocols;
      for (int i = 0; i < file->service_count(); i++) {
        const grpc::protobuf::ServiceDescriptor* service = file->service(i);
        protocols += grpc_objective_c_generator::GetProtocol(service);
      }

      ::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",
            OmitIf(kForwardDeclare, imports) + "\n" +
            OmitIf(kProtocolOnly, system_imports) + "\n" +
            PreprocIfElse(kForwardDeclare, class_declarations, class_imports,
                          kCheckIfDefined) + "\n" +
            forward_declarations + "\n" +
            kNonNullBegin + "\n" +
            protocols + "\n" +
            OmitIf(kProtocolOnly, interfaces) + "\n" +
            kNonNullEnd + "\n");
    }

    {
      // Generate .pbrpc.m

      ::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++) {
        class_imports += ImportProtoHeaders(file->dependency(i), "");
      }

      ::grpc::string definitions;
      for (int i = 0; i < file->service_count(); i++) {
        const grpc::protobuf::ServiceDescriptor* service = file->service(i);
        definitions += grpc_objective_c_generator::GetSource(service);
      }

      Write(context, file_name + ".pbrpc.m",
            OmitIf(kProtocolOnly,
                   imports + "\n" + class_imports + "\n" + definitions));
    }

    return true;
  }

 private:
  // Write the given code into the given file.
  void Write(grpc::protobuf::compiler::GeneratorContext* context,
             const ::grpc::string& filename, const ::grpc::string& code) const {
    std::unique_ptr<grpc::protobuf::io::ZeroCopyOutputStream> output(
        context->Open(filename));
    grpc::protobuf::io::CodedOutputStream coded_out(output.get());
    coded_out.WriteRaw(code.data(), code.size());
  }
};

int main(int argc, char* argv[]) {
  ObjectiveCGrpcGenerator generator;
  return grpc::protobuf::compiler::PluginMain(argc, argv, &generator);
}