• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 };
57 
58 // Escape C++ trigraphs by escaping question marks to "\?".
59 string PROTOC_EXPORT EscapeTrigraphs(const string& to_escape);
60 
61 // Strips ".proto" or ".protodevel" from the end of a filename.
62 string PROTOC_EXPORT StripProto(const string& filename);
63 
64 // Remove white space from either end of a StringPiece.
65 void PROTOC_EXPORT TrimWhitespace(StringPiece* input);
66 
67 // Returns true if the name requires a ns_returns_not_retained attribute applied
68 // to it.
69 bool PROTOC_EXPORT IsRetainedName(const string& name);
70 
71 // Returns true if the name starts with "init" and will need to have special
72 // handling under ARC.
73 bool PROTOC_EXPORT IsInitName(const string& name);
74 
75 // Gets the objc_class_prefix.
76 string PROTOC_EXPORT FileClassPrefix(const FileDescriptor* file);
77 
78 // Gets the path of the file we're going to generate (sans the .pb.h
79 // extension).  The path will be dependent on the objectivec package
80 // declared in the proto package.
81 string PROTOC_EXPORT FilePath(const FileDescriptor* file);
82 
83 // Just like FilePath(), but without the directory part.
84 string PROTOC_EXPORT FilePathBasename(const FileDescriptor* file);
85 
86 // Gets the name of the root class we'll generate in the file.  This class
87 // is not meant for external consumption, but instead contains helpers that
88 // the rest of the classes need
89 string PROTOC_EXPORT FileClassName(const FileDescriptor* file);
90 
91 // These return the fully-qualified class name corresponding to the given
92 // descriptor.
93 string PROTOC_EXPORT ClassName(const Descriptor* descriptor);
94 string PROTOC_EXPORT ClassName(const Descriptor* descriptor,
95                                string* out_suffix_added);
96 string PROTOC_EXPORT EnumName(const EnumDescriptor* descriptor);
97 
98 // Returns the fully-qualified name of the enum value corresponding to the
99 // the descriptor.
100 string PROTOC_EXPORT EnumValueName(const EnumValueDescriptor* descriptor);
101 
102 // Returns the name of the enum value corresponding to the descriptor.
103 string PROTOC_EXPORT EnumValueShortName(const EnumValueDescriptor* descriptor);
104 
105 // Reverse what an enum does.
106 string PROTOC_EXPORT UnCamelCaseEnumShortName(const string& name);
107 
108 // Returns the name to use for the extension (used as the method off the file's
109 // Root class).
110 string PROTOC_EXPORT ExtensionMethodName(const FieldDescriptor* descriptor);
111 
112 // Returns the transformed field name.
113 string PROTOC_EXPORT FieldName(const FieldDescriptor* field);
114 string PROTOC_EXPORT FieldNameCapitalized(const FieldDescriptor* field);
115 
116 // Returns the transformed oneof name.
117 string PROTOC_EXPORT OneofEnumName(const OneofDescriptor* descriptor);
118 string PROTOC_EXPORT OneofName(const OneofDescriptor* descriptor);
119 string PROTOC_EXPORT OneofNameCapitalized(const OneofDescriptor* descriptor);
120 
HasFieldPresence(const FileDescriptor * file)121 inline bool HasFieldPresence(const FileDescriptor* file) {
122   return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
123 }
124 
HasPreservingUnknownEnumSemantics(const FileDescriptor * file)125 inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
126   return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
127 }
128 
IsMapEntryMessage(const Descriptor * descriptor)129 inline bool IsMapEntryMessage(const Descriptor* descriptor) {
130   return descriptor->options().map_entry();
131 }
132 
133 // Reverse of the above.
134 string PROTOC_EXPORT UnCamelCaseFieldName(const string& name,
135                                           const FieldDescriptor* field);
136 
137 enum ObjectiveCType {
138   OBJECTIVECTYPE_INT32,
139   OBJECTIVECTYPE_UINT32,
140   OBJECTIVECTYPE_INT64,
141   OBJECTIVECTYPE_UINT64,
142   OBJECTIVECTYPE_FLOAT,
143   OBJECTIVECTYPE_DOUBLE,
144   OBJECTIVECTYPE_BOOLEAN,
145   OBJECTIVECTYPE_STRING,
146   OBJECTIVECTYPE_DATA,
147   OBJECTIVECTYPE_ENUM,
148   OBJECTIVECTYPE_MESSAGE
149 };
150 
151 enum FlagType {
152   FLAGTYPE_DESCRIPTOR_INITIALIZATION,
153   FLAGTYPE_EXTENSION,
154   FLAGTYPE_FIELD
155 };
156 
157 template<class TDescriptor>
158 string GetOptionalDeprecatedAttribute(
159     const TDescriptor* descriptor,
160     const FileDescriptor* file = NULL,
161     bool preSpace = true, bool postNewline = false) {
162   bool isDeprecated = descriptor->options().deprecated();
163   // The file is only passed when checking Messages & Enums, so those types
164   // get tagged. At the moment, it doesn't seem to make sense to tag every
165   // field or enum value with when the file is deprecated.
166   bool isFileLevelDeprecation = false;
167   if (!isDeprecated && file) {
168     isFileLevelDeprecation = file->options().deprecated();
169     isDeprecated = isFileLevelDeprecation;
170   }
171   if (isDeprecated) {
172     string message;
173     const FileDescriptor* sourceFile = descriptor->file();
174     if (isFileLevelDeprecation) {
175       message = sourceFile->name() + " is deprecated.";
176     } else {
177       message = descriptor->full_name() + " is deprecated (see " +
178                 sourceFile->name() + ").";
179     }
180 
181     string result = string("GPB_DEPRECATED_MSG(\"") + message + "\")";
182     if (preSpace) {
183       result.insert(0, " ");
184     }
185     if (postNewline) {
186       result.append("\n");
187     }
188     return result;
189   } else {
190     return "";
191   }
192 }
193 
194 string PROTOC_EXPORT GetCapitalizedType(const FieldDescriptor* field);
195 
196 ObjectiveCType PROTOC_EXPORT
197 GetObjectiveCType(FieldDescriptor::Type field_type);
198 
GetObjectiveCType(const FieldDescriptor * field)199 inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) {
200   return GetObjectiveCType(field->type());
201 }
202 
203 bool PROTOC_EXPORT IsPrimitiveType(const FieldDescriptor* field);
204 bool PROTOC_EXPORT IsReferenceType(const FieldDescriptor* field);
205 
206 string PROTOC_EXPORT GPBGenericValueFieldName(const FieldDescriptor* field);
207 string PROTOC_EXPORT DefaultValue(const FieldDescriptor* field);
208 bool PROTOC_EXPORT HasNonZeroDefaultValue(const FieldDescriptor* field);
209 
210 string PROTOC_EXPORT BuildFlagsString(const FlagType type,
211                                       const std::vector<string>& strings);
212 
213 // Builds HeaderDoc/appledoc style comments out of the comments in the .proto
214 // file.
215 string PROTOC_EXPORT BuildCommentsString(const SourceLocation& location,
216                                          bool prefer_single_line);
217 
218 // The name the commonly used by the library when built as a framework.
219 // This lines up to the name used in the CocoaPod.
220 extern PROTOC_EXPORT const char* const ProtobufLibraryFrameworkName;
221 // Returns the CPP symbol name to use as the gate for framework style imports
222 // for the given framework name to use.
223 string PROTOC_EXPORT
224 ProtobufFrameworkImportSymbol(const string& framework_name);
225 
226 // Checks if the file is one of the proto's bundled with the library.
227 bool PROTOC_EXPORT
228 IsProtobufLibraryBundledProtoFile(const FileDescriptor* file);
229 
230 // Checks the prefix for the given files and outputs any warnings as needed. If
231 // there are flat out errors, then out_error is filled in with the first error
232 // and the result is false.
233 bool PROTOC_EXPORT
234 ValidateObjCClassPrefixes(const std::vector<const FileDescriptor*>& files,
235                           const Options& generation_options, string* out_error);
236 
237 // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform
238 // the input into the expected output.
239 class PROTOC_EXPORT TextFormatDecodeData {
240  public:
241   TextFormatDecodeData();
242   ~TextFormatDecodeData();
243 
244   TextFormatDecodeData(const TextFormatDecodeData&) = delete;
245   TextFormatDecodeData& operator=(const TextFormatDecodeData&) = delete;
246 
247   void AddString(int32 key, const string& input_for_decode,
248                  const string& desired_output);
num_entries()249   size_t num_entries() const { return entries_.size(); }
250   string Data() const;
251 
252   static string DecodeDataForString(const string& input_for_decode,
253                                     const string& desired_output);
254 
255  private:
256   typedef std::pair<int32, string> DataEntry;
257   std::vector<DataEntry> entries_;
258 };
259 
260 // Helper for parsing simple files.
261 class PROTOC_EXPORT LineConsumer {
262  public:
263   LineConsumer();
264   virtual ~LineConsumer();
265   virtual bool ConsumeLine(const StringPiece& line, string* out_error) = 0;
266 };
267 
268 bool PROTOC_EXPORT ParseSimpleFile(const string& path,
269                                    LineConsumer* line_consumer,
270                                    string* out_error);
271 
272 // Helper class for parsing framework import mappings and generating
273 // import statements.
274 class PROTOC_EXPORT ImportWriter {
275  public:
276   ImportWriter(const string& generate_for_named_framework,
277                const string& named_framework_to_proto_path_mappings_path,
278                bool include_wkt_imports);
279   ~ImportWriter();
280 
281   void AddFile(const FileDescriptor* file, const string& header_extension);
282   void Print(io::Printer *printer) const;
283 
284  private:
285   class ProtoFrameworkCollector : public LineConsumer {
286    public:
ProtoFrameworkCollector(std::map<string,string> * inout_proto_file_to_framework_name)287     ProtoFrameworkCollector(std::map<string, string>* inout_proto_file_to_framework_name)
288         : map_(inout_proto_file_to_framework_name) {}
289 
290     virtual bool ConsumeLine(const StringPiece& line, string* out_error);
291 
292    private:
293     std::map<string, string>* map_;
294   };
295 
296   void ParseFrameworkMappings();
297 
298   const string generate_for_named_framework_;
299   const string named_framework_to_proto_path_mappings_path_;
300   const bool include_wkt_imports_;
301   std::map<string, string> proto_file_to_framework_name_;
302   bool need_to_parse_mapping_file_;
303 
304   std::vector<string> protobuf_framework_imports_;
305   std::vector<string> protobuf_non_framework_imports_;
306   std::vector<string> other_framework_imports_;
307   std::vector<string> other_imports_;
308 };
309 
310 }  // namespace objectivec
311 }  // namespace compiler
312 }  // namespace protobuf
313 }  // namespace google
314 
315 #include <google/protobuf/port_undef.inc>
316 
317 #endif  // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__
318