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 |
31 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ | 33 #ifndef GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ |
32 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ | 34 #define GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ |
33 | 35 |
34 #include <string> | 36 #include <string> |
35 #include <vector> | 37 #include <vector> |
36 | 38 |
37 #include <google/protobuf/descriptor.h> | 39 #include <google/protobuf/descriptor.h> |
38 #include <google/protobuf/descriptor.pb.h> | 40 #include <google/protobuf/descriptor.pb.h> |
39 | 41 |
40 namespace google { | 42 namespace google { |
41 namespace protobuf { | 43 namespace protobuf { |
42 namespace compiler { | 44 namespace compiler { |
43 namespace objectivec { | 45 namespace objectivec { |
44 | 46 |
45 // Generator options (see objectivec_generator.cc for a description of each): | 47 // Generator options (see objectivec_generator.cc for a description of each): |
46 struct Options { | 48 struct Options { |
47 Options(); | 49 Options(); |
48 string expected_prefixes_path; | 50 string expected_prefixes_path; |
| 51 string generate_for_named_framework; |
| 52 string named_framework_to_proto_path_mappings_path; |
49 }; | 53 }; |
50 | 54 |
51 // Escape C++ trigraphs by escaping question marks to "\?". | 55 // Escape C++ trigraphs by escaping question marks to "\?". |
52 string EscapeTrigraphs(const string& to_escape); | 56 string EscapeTrigraphs(const string& to_escape); |
53 | 57 |
54 // Strips ".proto" or ".protodevel" from the end of a filename. | 58 // Strips ".proto" or ".protodevel" from the end of a filename. |
55 string StripProto(const string& filename); | 59 string StripProto(const string& filename); |
56 | 60 |
| 61 // Remove white space from either end of a StringPiece. |
| 62 void StringPieceTrimWhitespace(StringPiece* input); |
| 63 |
57 // Returns true if the name requires a ns_returns_not_retained attribute applied | 64 // Returns true if the name requires a ns_returns_not_retained attribute applied |
58 // to it. | 65 // to it. |
59 bool IsRetainedName(const string& name); | 66 bool IsRetainedName(const string& name); |
60 | 67 |
61 // Returns true if the name starts with "init" and will need to have special | 68 // Returns true if the name starts with "init" and will need to have special |
62 // handling under ARC. | 69 // handling under ARC. |
63 bool IsInitName(const string& name); | 70 bool IsInitName(const string& name); |
64 | 71 |
65 // Gets the name of the file we're going to generate (sans the .pb.h | 72 // Gets the objc_class_prefix. |
66 // extension). This does not include the path to that file. | 73 string FileClassPrefix(const FileDescriptor* file); |
67 string FileName(const FileDescriptor* file); | |
68 | 74 |
69 // Gets the path of the file we're going to generate (sans the .pb.h | 75 // Gets the path of the file we're going to generate (sans the .pb.h |
70 // extension). The path will be dependent on the objectivec package | 76 // extension). The path will be dependent on the objectivec package |
71 // declared in the proto package. | 77 // declared in the proto package. |
72 string FilePath(const FileDescriptor* file); | 78 string FilePath(const FileDescriptor* file); |
73 | 79 |
| 80 // Just like FilePath(), but without the directory part. |
| 81 string FilePathBasename(const FileDescriptor* file); |
| 82 |
74 // Gets the name of the root class we'll generate in the file. This class | 83 // Gets the name of the root class we'll generate in the file. This class |
75 // is not meant for external consumption, but instead contains helpers that | 84 // is not meant for external consumption, but instead contains helpers that |
76 // the rest of the classes need | 85 // the rest of the classes need |
77 string FileClassName(const FileDescriptor* file); | 86 string FileClassName(const FileDescriptor* file); |
78 | 87 |
79 // These return the fully-qualified class name corresponding to the given | 88 // These return the fully-qualified class name corresponding to the given |
80 // descriptor. | 89 // descriptor. |
81 string ClassName(const Descriptor* descriptor); | 90 string ClassName(const Descriptor* descriptor); |
| 91 string ClassName(const Descriptor* descriptor, string* out_suffix_added); |
82 string EnumName(const EnumDescriptor* descriptor); | 92 string EnumName(const EnumDescriptor* descriptor); |
83 | 93 |
84 // Returns the fully-qualified name of the enum value corresponding to the | 94 // Returns the fully-qualified name of the enum value corresponding to the |
85 // the descriptor. | 95 // the descriptor. |
86 string EnumValueName(const EnumValueDescriptor* descriptor); | 96 string EnumValueName(const EnumValueDescriptor* descriptor); |
87 | 97 |
88 // Returns the name of the enum value corresponding to the descriptor. | 98 // Returns the name of the enum value corresponding to the descriptor. |
89 string EnumValueShortName(const EnumValueDescriptor* descriptor); | 99 string EnumValueShortName(const EnumValueDescriptor* descriptor); |
90 | 100 |
91 // Reverse what an enum does. | 101 // Reverse what an enum does. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
126 OBJECTIVECTYPE_UINT64, | 136 OBJECTIVECTYPE_UINT64, |
127 OBJECTIVECTYPE_FLOAT, | 137 OBJECTIVECTYPE_FLOAT, |
128 OBJECTIVECTYPE_DOUBLE, | 138 OBJECTIVECTYPE_DOUBLE, |
129 OBJECTIVECTYPE_BOOLEAN, | 139 OBJECTIVECTYPE_BOOLEAN, |
130 OBJECTIVECTYPE_STRING, | 140 OBJECTIVECTYPE_STRING, |
131 OBJECTIVECTYPE_DATA, | 141 OBJECTIVECTYPE_DATA, |
132 OBJECTIVECTYPE_ENUM, | 142 OBJECTIVECTYPE_ENUM, |
133 OBJECTIVECTYPE_MESSAGE | 143 OBJECTIVECTYPE_MESSAGE |
134 }; | 144 }; |
135 | 145 |
| 146 enum FlagType { |
| 147 FLAGTYPE_DESCRIPTOR_INITIALIZATION, |
| 148 FLAGTYPE_EXTENSION, |
| 149 FLAGTYPE_FIELD |
| 150 }; |
| 151 |
136 template<class TDescriptor> | 152 template<class TDescriptor> |
137 string GetOptionalDeprecatedAttribute(const TDescriptor* descriptor, bool preSpa
ce = true, bool postNewline = false) { | 153 string GetOptionalDeprecatedAttribute( |
138 if (descriptor->options().deprecated()) { | 154 const TDescriptor* descriptor, |
| 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) { |
139 string result = "DEPRECATED_ATTRIBUTE"; | 165 string result = "DEPRECATED_ATTRIBUTE"; |
140 if (preSpace) { | 166 if (preSpace) { |
141 result.insert(0, " "); | 167 result.insert(0, " "); |
142 } | 168 } |
143 if (postNewline) { | 169 if (postNewline) { |
144 result.append("\n"); | 170 result.append("\n"); |
145 } | 171 } |
146 return result; | 172 return result; |
147 } else { | 173 } else { |
148 return ""; | 174 return ""; |
149 } | 175 } |
150 } | 176 } |
151 | 177 |
152 string GetCapitalizedType(const FieldDescriptor* field); | 178 string GetCapitalizedType(const FieldDescriptor* field); |
153 | 179 |
154 ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type); | 180 ObjectiveCType GetObjectiveCType(FieldDescriptor::Type field_type); |
155 | 181 |
156 inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) { | 182 inline ObjectiveCType GetObjectiveCType(const FieldDescriptor* field) { |
157 return GetObjectiveCType(field->type()); | 183 return GetObjectiveCType(field->type()); |
158 } | 184 } |
159 | 185 |
160 bool IsPrimitiveType(const FieldDescriptor* field); | 186 bool IsPrimitiveType(const FieldDescriptor* field); |
161 bool IsReferenceType(const FieldDescriptor* field); | 187 bool IsReferenceType(const FieldDescriptor* field); |
162 | 188 |
163 string GPBGenericValueFieldName(const FieldDescriptor* field); | 189 string GPBGenericValueFieldName(const FieldDescriptor* field); |
164 string DefaultValue(const FieldDescriptor* field); | 190 string DefaultValue(const FieldDescriptor* field); |
165 bool HasNonZeroDefaultValue(const FieldDescriptor* field); | 191 bool HasNonZeroDefaultValue(const FieldDescriptor* field); |
166 | 192 |
167 string BuildFlagsString(const vector<string>& strings); | 193 string BuildFlagsString(const FlagType type, const vector<string>& strings); |
168 | 194 |
169 // Builds a HeaderDoc style comment out of the comments in the .proto file. | 195 // Builds HeaderDoc/appledoc style comments out of the comments in the .proto |
170 string BuildCommentsString(const SourceLocation& location); | 196 // file. |
| 197 string BuildCommentsString(const SourceLocation& location, |
| 198 bool prefer_single_line); |
171 | 199 |
172 // Checks the prefix for a given file and outputs any warnings needed, if | 200 // The name the commonly used by the library when built as a framework. |
173 // there are flat out errors, then out_error is filled in and the result is | 201 // This lines up to the name used in the CocoaPod. |
174 // false. | 202 extern const char* const ProtobufLibraryFrameworkName; |
175 bool ValidateObjCClassPrefix(const FileDescriptor* file, | 203 // Returns the CPP symbol name to use as the gate for framework style imports |
176 const Options& generation_options, | 204 // for the given framework name to use. |
177 string* out_error); | 205 string ProtobufFrameworkImportSymbol(const string& framework_name); |
| 206 |
| 207 // Checks if the file is one of the proto's bundled with the library. |
| 208 bool IsProtobufLibraryBundledProtoFile(const FileDescriptor* file); |
| 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 ValidateObjCClassPrefixes(const vector<const FileDescriptor*>& files, |
| 214 const Options& generation_options, |
| 215 string* out_error); |
178 | 216 |
179 // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform | 217 // Generate decode data needed for ObjC's GPBDecodeTextFormatName() to transform |
180 // the input into the expected output. | 218 // the input into the expected output. |
181 class LIBPROTOC_EXPORT TextFormatDecodeData { | 219 class LIBPROTOC_EXPORT TextFormatDecodeData { |
182 public: | 220 public: |
183 TextFormatDecodeData() {} | 221 TextFormatDecodeData(); |
| 222 ~TextFormatDecodeData(); |
184 | 223 |
185 void AddString(int32 key, const string& input_for_decode, | 224 void AddString(int32 key, const string& input_for_decode, |
186 const string& desired_output); | 225 const string& desired_output); |
187 size_t num_entries() const { return entries_.size(); } | 226 size_t num_entries() const { return entries_.size(); } |
188 string Data() const; | 227 string Data() const; |
189 | 228 |
190 static string DecodeDataForString(const string& input_for_decode, | 229 static string DecodeDataForString(const string& input_for_decode, |
191 const string& desired_output); | 230 const string& desired_output); |
192 | 231 |
193 private: | 232 private: |
194 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormatDecodeData); | 233 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TextFormatDecodeData); |
195 | 234 |
196 typedef std::pair<int32, string> DataEntry; | 235 typedef std::pair<int32, string> DataEntry; |
197 vector<DataEntry> entries_; | 236 vector<DataEntry> entries_; |
198 }; | 237 }; |
199 | 238 |
| 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 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 |
200 } // namespace objectivec | 287 } // namespace objectivec |
201 } // namespace compiler | 288 } // namespace compiler |
202 } // namespace protobuf | 289 } // namespace protobuf |
203 } // namespace google | 290 } // namespace google |
204 #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ | 291 #endif // GOOGLE_PROTOBUF_COMPILER_OBJECTIVEC_HELPERS_H__ |
OLD | NEW |