1 /*
2 *
3 * Copyright 2015 gRPC authors.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18
19 #ifndef GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
20 #define GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
21
22 #include <map>
23
24 #include <google/protobuf/compiler/objectivec/names.h>
25
26 #include "src/compiler/config.h"
27 #include "src/compiler/generator_helpers.h"
28
29 namespace grpc_objective_c_generator {
30
31 using ::grpc::protobuf::FileDescriptor;
32 using ::grpc::protobuf::MethodDescriptor;
33 using ::grpc::protobuf::ServiceDescriptor;
34
MessageHeaderName(const FileDescriptor * file)35 inline std::string MessageHeaderName(const FileDescriptor* file) {
36 return google::protobuf::compiler::objectivec::FilePath(file) + ".pbobjc.h";
37 }
38
AsciiIsUpper(char c)39 inline bool AsciiIsUpper(char c) { return c >= 'A' && c <= 'Z'; }
40
ServiceClassName(const ServiceDescriptor * service)41 inline ::std::string ServiceClassName(const ServiceDescriptor* service) {
42 const FileDescriptor* file = service->file();
43 ::std::string prefix =
44 google::protobuf::compiler::objectivec::FileClassPrefix(file);
45 ::std::string class_name = service->name();
46 // We add the prefix in the cases where the string is missing a prefix.
47 // We define "missing a prefix" as where 'input':
48 // a) Doesn't start with the prefix or
49 // b) Isn't equivalent to the prefix or
50 // c) Has the prefix, but the letter after the prefix is lowercase
51 // This is the same semantics as the Objective-C protoc.
52 // https://github.com/protocolbuffers/protobuf/blob/c160ae52a91ca4c76936531d68cc846f8230dbb1/src/google/protobuf/compiler/objectivec/objectivec_helpers.cc#L389
53 if (class_name.rfind(prefix, 0) == 0) {
54 if (class_name.length() == prefix.length() ||
55 !AsciiIsUpper(class_name[prefix.length()])) {
56 return prefix + class_name;
57 } else {
58 return class_name;
59 }
60 } else {
61 return prefix + class_name;
62 }
63 }
64
LocalImport(const::std::string & import)65 inline ::std::string LocalImport(const ::std::string& import) {
66 return ::std::string("#import \"" + import + "\"\n");
67 }
68
FrameworkImport(const::std::string & import,const::std::string & framework)69 inline ::std::string FrameworkImport(const ::std::string& import,
70 const ::std::string& framework) {
71 // Flattens the directory structure: grab the file name only
72 std::size_t pos = import.rfind("/");
73 // If pos is npos, pos + 1 is 0, which gives us the entire string,
74 // so there's no need to check that
75 ::std::string filename = import.substr(pos + 1, import.size() - (pos + 1));
76 return ::std::string("#import <" + framework + "/" + filename + ">\n");
77 }
78
SystemImport(const::std::string & import)79 inline ::std::string SystemImport(const ::std::string& import) {
80 return ::std::string("#import <" + import + ">\n");
81 }
82
PreprocConditional(::std::string symbol,bool invert)83 inline ::std::string PreprocConditional(::std::string symbol, bool invert) {
84 return invert ? "!defined(" + symbol + ") || !" + symbol
85 : "defined(" + symbol + ") && " + symbol;
86 }
87
PreprocIf(const::std::string & symbol,const::std::string & if_true)88 inline ::std::string PreprocIf(const ::std::string& symbol,
89 const ::std::string& if_true) {
90 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
91 if_true + "#endif\n");
92 }
93
PreprocIfNot(const::std::string & symbol,const::std::string & if_true)94 inline ::std::string PreprocIfNot(const ::std::string& symbol,
95 const ::std::string& if_true) {
96 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
97 if_true + "#endif\n");
98 }
99
PreprocIfElse(const::std::string & symbol,const::std::string & if_true,const::std::string & if_false)100 inline ::std::string PreprocIfElse(const ::std::string& symbol,
101 const ::std::string& if_true,
102 const ::std::string& if_false) {
103 return ::std::string("#if " + PreprocConditional(symbol, false) + "\n" +
104 if_true + "#else\n" + if_false + "#endif\n");
105 }
106
PreprocIfNotElse(const::std::string & symbol,const::std::string & if_true,const::std::string & if_false)107 inline ::std::string PreprocIfNotElse(const ::std::string& symbol,
108 const ::std::string& if_true,
109 const ::std::string& if_false) {
110 return ::std::string("#if " + PreprocConditional(symbol, true) + "\n" +
111 if_true + "#else\n" + if_false + "#endif\n");
112 }
113
ShouldIncludeMethod(const MethodDescriptor * method)114 inline bool ShouldIncludeMethod(const MethodDescriptor* method) {
115 #ifdef OBJC_SKIP_METHODS_WITHOUT_MESSAGE_PREFIX
116 return (method->input_type()->file()->options().has_objc_class_prefix() &&
117 method->output_type()->file()->options().has_objc_class_prefix());
118 #else
119 (void)method; // to silence the unused warning for method.
120 return true;
121 #endif
122 }
123
124 } // namespace grpc_objective_c_generator
125 #endif // GRPC_INTERNAL_COMPILER_OBJECTIVE_C_GENERATOR_HELPERS_H
126