1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc. All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above
12 // copyright notice, this list of conditions and the following disclaimer
13 // in the documentation and/or other materials provided with the
14 // distribution.
15 // * Neither the name of Google Inc. nor the names of its
16 // contributors may be used to endorse or promote products derived from
17 // this software without specific prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
31 // Helper functions for generating ObjectiveC code.
32
33 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
34 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
35
36 #include <string>
37 #include <vector>
38
39 #include <google/protobuf/descriptor.h>
40 #include <google/protobuf/descriptor.pb.h>
41
42 #include <google/protobuf/port_def.inc>
43
44 namespace google {
45 namespace protobuf {
46 namespace compiler {
47 namespace objectivec {
48
49 // Generator options (see objectivec_generator.cc for a description of each):
50 struct Options {
51 Options();
52 string expected_prefixes_path;
53 std::vector<string> expected_prefixes_suppressions;
54 string generate_for_named_framework;
55 string named_framework_to_proto_path_mappings_path;
56 string runtime_import_prefix;
57 };
58
59 // Escape C++ trigraphs by escaping question marks to "\?".
60 string PROTOC_EXPORT EscapeTrigraphs(const string& to_escape);
61
62 // Strips ".proto" or ".protodevel" from the end of a filename.
63 string PROTOC_EXPORT StripProto(const string& filename);
64
65 // Remove white space from either end of a StringPiece.
66 void PROTOC_EXPORT TrimWhitespace(StringPiece* input);
67
68 // Returns true if the name requires a ns_returns_not_retained attribute applied
69 // to it.
70 bool PROTOC_EXPORT IsRetainedName(const string& name);
71
72 // Returns true if the name starts with "init" and will need to have special
73 // handling under ARC.
74 bool PROTOC_EXPORT IsInitName(const string& name);
75
76 // Gets the objc_class_prefix.
77 string PROTOC_EXPORT FileClassPrefix(const FileDescriptor* file);
78
79 // Gets the path of the file we're going to generate (sans the .pb.h
80 // extension). The path will be dependent on the objectivec package
81 // declared in the proto package.
82 string PROTOC_EXPORT FilePath(const FileDescriptor* file);
83
84 // Just like FilePath(), but without the directory part.
85 string PROTOC_EXPORT FilePathBasename(const FileDescriptor* file);
86
87 // Gets the name of the root class we'll generate in the file. This class
88 // is not meant for external consumption, but instead contains helpers that
89 // the rest of the classes need
90 string PROTOC_EXPORT FileClassName(const FileDescriptor* file);
91
92 // These return the fully-qualified class name corresponding to the given
93 // descriptor.
94 string PROTOC_EXPORT ClassName(const Descriptor* descriptor);
95 string PROTOC_EXPORT ClassName(const Descriptor* descriptor,
96 string* out_suffix_added);
97 string PROTOC_EXPORT EnumName(const EnumDescriptor* descriptor);
98
99 // Returns the fully-qualified name of the enum value corresponding to the
100 // the descriptor.
101 string PROTOC_EXPORT EnumValueName(const EnumValueDescriptor* descriptor);
102
103 // Returns the name of the enum value corresponding to the descriptor.
104 string PROTOC_EXPORT EnumValueShortName(const EnumValueDescriptor* descriptor);
105
106 // Reverse what an enum does.
107 string PROTOC_EXPORT UnCamelCaseEnumShortName(const string& name);
108
109 // Returns the name to use for the extension (used as the method off the file's
110 // Root class).
111 string PROTOC_EXPORT ExtensionMethodName(const FieldDescriptor* descriptor);
112
113 // Returns the transformed field name.
114 string PROTOC_EXPORT FieldName(const FieldDescriptor* field);
115 string PROTOC_EXPORT FieldNameCapitalized(const FieldDescriptor* field);
116
117 // Returns the transformed oneof name.
118 string PROTOC_EXPORT OneofEnumName(const OneofDescriptor* descriptor);
119 string PROTOC_EXPORT OneofName(const OneofDescriptor* descriptor);
120 string PROTOC_EXPORT OneofNameCapitalized(const OneofDescriptor* descriptor);
121
122 // Returns a symbol that can be used in C code to refer to an Objective C
123 // class without initializing the class.
124 string PROTOC_EXPORT ObjCClass(const string& class_name);
125
126 // Declares an Objective C class without initializing the class so that it can
127 // be refrerred to by ObjCClass.
128 string PROTOC_EXPORT ObjCClassDeclaration(const string& class_name);
129
HasPreservingUnknownEnumSemantics(const FileDescriptor * file)130 inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
131 return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
132 }
133
IsMapEntryMessage(const Descriptor * descriptor)134 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
135 return descriptor->options().map_entry();
136 }
137
138 // Reverse of the above.
139 string PROTOC_EXPORT UnCamelCaseFieldName(const string& name,
140 const FieldDescriptor* field);
141
142 enum ObjectiveCType {
143 OBJECTIVECTYPE_INT32,
144 OBJECTIVECTYPE_UINT32,
145 OBJECTIVECTYPE_INT64,
146 OBJECTIVECTYPE_UINT64,
147 OBJECTIVECTYPE_FLOAT,
148 OBJECTIVECTYPE_DOUBLE,
149 OBJECTIVECTYPE_BOOLEAN,
150 OBJECTIVECTYPE_STRING,
151 OBJECTIVECTYPE_DATA,
152 OBJECTIVECTYPE_ENUM,
153 OBJECTIVECTYPE_MESSAGE
154 };
155
156 enum FlagType {
157 FLAGTYPE_DESCRIPTOR_INITIALIZATION,
158 FLAGTYPE_EXTENSION,
159 FLAGTYPE_FIELD
160 };
161
162 template<class TDescriptor>
163 string GetOptionalDeprecatedAttribute(
164 const TDescriptor* descriptor,
165 const FileDescriptor* file = NULL,
166 bool preSpace = true, bool postNewline = false) {
167 bool isDeprecated = descriptor->options().deprecated();
168 // The file is only passed when checking Messages & Enums, so those types
169 // get tagged. At the moment, it doesn't seem to make sense to tag every
170 // field or enum value with when the file is deprecated.
171 bool isFileLevelDeprecation = false;
172 if (!isDeprecated && file) {
173 isFileLevelDeprecation = file->options().deprecated();
174 isDeprecated = isFileLevelDeprecation;
175 }
176 if (isDeprecated) {
177 string message;
178 const FileDescriptor* sourceFile = descriptor->file();
179 if (isFileLevelDeprecation) {
180 message = sourceFile->name() + " is deprecated.";
181 } else {
182 message = descriptor->full_name() + " is deprecated (see " +
183 sourceFile->name() + ").";
184 }
185
186 string result = string("GPB_DEPRECATED_MSG(\"") + message + "\")";
187 if (preSpace) {
188 result.insert(0, " ");
189 }
190 if (postNewline) {
191 result.append("\n");
192 }
193 return result;
194 } else {
195 return "";
196 }
197 }
198
199 string PROTOC_EXPORT GetCapitalizedType(const FieldDescriptor* field);
200
201 ObjectiveCType PROTOC_EXPORT
202 GetObjectiveCType(FieldDescriptor::Type field_type);
203
GetObjectiveCType(const FieldDescriptor * field)204 inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
205 return GetObjectiveCType(field->type());
206 }
207
208 bool PROTOC_EXPORT IsPrimitiveType(const FieldDescriptor* field);
209 bool PROTOC_EXPORT IsReferenceType(const FieldDescriptor* field);
210
211 string PROTOC_EXPORT GPBGenericValueFieldName(const FieldDescriptor* field);
212 string PROTOC_EXPORT DefaultValue(const FieldDescriptor* field);
213 bool PROTOC_EXPORT HasNonZeroDefaultValue(const FieldDescriptor* field);
214
215 string PROTOC_EXPORT BuildFlagsString(const FlagType type,
216 const std::vector<string>& strings);
217
218 // Builds HeaderDoc/appledoc style comments out of the comments in the .proto
219 // file.
220 string PROTOC_EXPORT BuildCommentsString(const SourceLocation& location,
221 bool prefer_single_line);
222
223 // The name the commonly used by the library when built as a framework.
224 // This lines up to the name used in the CocoaPod.
225 extern PROTOC_EXPORT const char* const ProtobufLibraryFrameworkName;
226 // Returns the CPP symbol name to use as the gate for framework style imports
227 // for the given framework name to use.
228 string PROTOC_EXPORT
229 ProtobufFrameworkImportSymbol(const string& framework_name);
230
231 // Checks if the file is one of the proto's bundled with the library.
232 bool PROTOC_EXPORT
233 IsProtobufLibraryBundledProtoFile(const FileDescriptor* file);
234
235 // Checks the prefix for the given files and outputs any warnings as needed. If
236 // there are flat out errors, then out_error is filled in with the first error
237 // and the result is false.
238 bool PROTOC_EXPORT
239 ValidateObjCClassPrefixes(const std::vector<const FileDescriptor*>& files,
240 const Options& generation_options, string* out_error);
241
242 // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform
243 // the input into the expected output.
244 class PROTOC_EXPORT TextFormatDecodeData {
245 public:
246 TextFormatDecodeData();
247 ~TextFormatDecodeData();
248
249 TextFormatDecodeData(const TextFormatDecodeData&) = delete;
250 TextFormatDecodeData& operator=(const TextFormatDecodeData&) = delete;
251
252 void AddString(int32 key, const string& input_for_decode,
253 const string& desired_output);
num_entries()254 size_t num_entries() const { return entries_.size(); }
255 string Data() const;
256
257 static string DecodeDataForString(const string& input_for_decode,
258 const string& desired_output);
259
260 private:
261 typedef std::pair<int32, string> DataEntry;
262 std::vector<DataEntry> entries_;
263 };
264
265 // Helper for parsing simple files.
266 class PROTOC_EXPORT LineConsumer {
267 public:
268 LineConsumer();
269 virtual ~LineConsumer();
270 virtual bool ConsumeLine(const StringPiece& line, string* out_error) = 0;
271 };
272
273 bool PROTOC_EXPORT ParseSimpleFile(const string& path,
274 LineConsumer* line_consumer,
275 string* out_error);
276
277 // Helper class for parsing framework import mappings and generating
278 // import statements.
279 class PROTOC_EXPORT ImportWriter {
280 public:
281 ImportWriter(const string& generate_for_named_framework,
282 const string& named_framework_to_proto_path_mappings_path,
283 const string& runtime_import_prefix,
284 bool include_wkt_imports);
285 ~ImportWriter();
286
287 void AddFile(const FileDescriptor* file, const string& header_extension);
288 void Print(io::Printer *printer) const;
289
290 static void PrintRuntimeImports(io::Printer *printer,
291 const std::vector<string>& header_to_import,
292 const string& runtime_import_prefix,
293 bool default_cpp_symbol = false);
294
295 private:
296 class ProtoFrameworkCollector : public LineConsumer {
297 public:
ProtoFrameworkCollector(std::map<string,string> * inout_proto_file_to_framework_name)298 ProtoFrameworkCollector(std::map<string, string>* inout_proto_file_to_framework_name)
299 : map_(inout_proto_file_to_framework_name) {}
300
301 virtual bool ConsumeLine(const StringPiece& line, string* out_error);
302
303 private:
304 std::map<string, string>* map_;
305 };
306
307 void ParseFrameworkMappings();
308
309 const string generate_for_named_framework_;
310 const string named_framework_to_proto_path_mappings_path_;
311 const string runtime_import_prefix_;
312 const bool include_wkt_imports_;
313 std::map<string, string> proto_file_to_framework_name_;
314 bool need_to_parse_mapping_file_;
315
316 std::vector<string> protobuf_imports_;
317 std::vector<string> other_framework_imports_;
318 std::vector<string> other_imports_;
319 };
320
321 } // namespace objectivec
322 } // namespace compiler
323 } // namespace protobuf
324 } // namespace google
325
326 #include <google/protobuf/port_undef.inc>
327
328 #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
329