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