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