Line data Source code
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 : // Author: kenton@google.com (Kenton Varda)
32 : // Based on original Protocol Buffers design by
33 : // Sanjay Ghemawat, Jeff Dean, and others.
34 :
35 : #ifndef GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
36 : #define GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
37 :
38 : #include <map>
39 : #include <string>
40 : #include <google/protobuf/descriptor.h>
41 : #include <google/protobuf/descriptor.pb.h>
42 :
43 : namespace google {
44 : namespace protobuf {
45 :
46 : namespace io {
47 : class Printer;
48 : }
49 :
50 : namespace compiler {
51 : namespace cpp {
52 :
53 : // Commonly-used separator comments. Thick is a line of '=', thin is a line
54 : // of '-'.
55 : extern const char kThickSeparator[];
56 : extern const char kThinSeparator[];
57 :
58 : // Returns the non-nested type name for the given type. If "qualified" is
59 : // true, prefix the type with the full namespace. For example, if you had:
60 : // package foo.bar;
61 : // message Baz { message Qux {} }
62 : // Then the qualified ClassName for Qux would be:
63 : // ::foo::bar::Baz_Qux
64 : // While the non-qualified version would be:
65 : // Baz_Qux
66 : string ClassName(const Descriptor* descriptor, bool qualified);
67 : string ClassName(const EnumDescriptor* enum_descriptor, bool qualified);
68 :
69 : // Name of the CRTP class template (for use with proto_h).
70 : // This is a class name, like "ProtoName_InternalBase".
71 : string DependentBaseClassTemplateName(const Descriptor* descriptor);
72 :
73 : // Name of the base class: either the dependent base class (for use with
74 : // proto_h) or google::protobuf::Message.
75 : string SuperClassName(const Descriptor* descriptor);
76 :
77 : // Returns a string that down-casts from the dependent base class to the
78 : // derived class.
79 : string DependentBaseDownCast();
80 : string DependentBaseConstDownCast();
81 :
82 : // Get the (unqualified) name that should be used for this field in C++ code.
83 : // The name is coerced to lower-case to emulate proto1 behavior. People
84 : // should be using lowercase-with-underscores style for proto field names
85 : // anyway, so normally this just returns field->name().
86 : string FieldName(const FieldDescriptor* field);
87 :
88 : // Get the sanitized name that should be used for the given enum in C++ code.
89 : string EnumValueName(const EnumValueDescriptor* enum_value);
90 :
91 : // Get the unqualified name that should be used for a field's field
92 : // number constant.
93 : string FieldConstantName(const FieldDescriptor *field);
94 :
95 : // Returns the scope where the field was defined (for extensions, this is
96 : // different from the message type to which the field applies).
97 23282 : inline const Descriptor* FieldScope(const FieldDescriptor* field) {
98 11641 : return field->is_extension() ?
99 23282 : field->extension_scope() : field->containing_type();
100 : }
101 :
102 : // Returns true if the given 'field_descriptor' has a message type that is
103 : // a dependency of the file where the field is defined (i.e., the field
104 : // type is defined in a different file than the message holding the field).
105 : //
106 : // This only applies to Message-typed fields. Enum-typed fields may refer
107 : // to an enum in a dependency; however, enums are specified and
108 : // forward-declared with an enum-base, so the definition is not required to
109 : // manipulate the field value.
110 : bool IsFieldDependent(const FieldDescriptor* field_descriptor);
111 :
112 : // Returns the name that should be used for forcing dependent lookup from a
113 : // dependent base class.
114 : string DependentTypeName(const FieldDescriptor* field);
115 :
116 : // Returns the fully-qualified type name field->message_type(). Usually this
117 : // is just ClassName(field->message_type(), true);
118 : string FieldMessageTypeName(const FieldDescriptor* field);
119 :
120 : // Strips ".proto" or ".protodevel" from the end of a filename.
121 : string StripProto(const string& filename);
122 :
123 : // Get the C++ type name for a primitive type (e.g. "double", "::google::protobuf::int32", etc.).
124 : // Note: non-built-in type names will be qualified, meaning they will start
125 : // with a ::. If you are using the type as a template parameter, you will
126 : // need to insure there is a space between the < and the ::, because the
127 : // ridiculous C++ standard defines "<:" to be a synonym for "[".
128 : const char* PrimitiveTypeName(FieldDescriptor::CppType type);
129 :
130 : // Get the declared type name in CamelCase format, as is used e.g. for the
131 : // methods of WireFormat. For example, TYPE_INT32 becomes "Int32".
132 : const char* DeclaredTypeMethodName(FieldDescriptor::Type type);
133 :
134 : // Return the code that evaluates to the number when compiled.
135 : string Int32ToString(int number);
136 :
137 : // Return the code that evaluates to the number when compiled.
138 : string Int64ToString(int64 number);
139 :
140 : // Get code that evaluates to the field's default value.
141 : string DefaultValue(const FieldDescriptor* field);
142 :
143 : // Convert a file name into a valid identifier.
144 : string FilenameIdentifier(const string& filename);
145 :
146 : // Return the name of the AddDescriptors() function for a given file.
147 : string GlobalAddDescriptorsName(const string& filename);
148 :
149 : // Return the name of the AssignDescriptors() function for a given file.
150 : string GlobalAssignDescriptorsName(const string& filename);
151 :
152 : // Return the qualified C++ name for a file level symbol.
153 : string QualifiedFileLevelSymbol(const string& package, const string& name);
154 :
155 : // Return the name of the ShutdownFile() function for a given file.
156 : string GlobalShutdownFileName(const string& filename);
157 :
158 : // Escape C++ trigraphs by escaping question marks to \?
159 : string EscapeTrigraphs(const string& to_escape);
160 :
161 : // Escaped function name to eliminate naming conflict.
162 : string SafeFunctionName(const Descriptor* descriptor,
163 : const FieldDescriptor* field,
164 : const string& prefix);
165 :
166 : // Returns true if unknown fields are preseved after parsing.
167 3149 : inline bool PreserveUnknownFields(const Descriptor* message) {
168 3149 : return message->file()->syntax() != FileDescriptor::SYNTAX_PROTO3;
169 : }
170 :
171 : // If PreserveUnknownFields() is true, determines whether unknown
172 : // fields will be stored in an UnknownFieldSet or a string.
173 : // If PreserveUnknownFields() is false, this method will not be
174 : // used.
175 4710 : inline bool UseUnknownFieldSet(const FileDescriptor* file) {
176 4742 : return file->options().optimize_for() != FileOptions::LITE_RUNTIME;
177 : }
178 :
179 :
180 : // Does the file have any map fields, necessitating the file to include
181 : // map_field_inl.h and map.h.
182 : bool HasMapFields(const FileDescriptor* file);
183 :
184 : // Does this file have any enum type definitions?
185 : bool HasEnumDefinitions(const FileDescriptor* file);
186 :
187 : // Does this file have generated parsing, serialization, and other
188 : // standard methods for which reflection-based fallback implementations exist?
189 1316 : inline bool HasGeneratedMethods(const FileDescriptor* file) {
190 1316 : return file->options().optimize_for() != FileOptions::CODE_SIZE;
191 : }
192 :
193 : // Do message classes in this file have descriptor and reflection methods?
194 5355 : inline bool HasDescriptorMethods(const FileDescriptor* file) {
195 5642 : return file->options().optimize_for() != FileOptions::LITE_RUNTIME;
196 : }
197 :
198 : // Should we generate generic services for this file?
199 313 : inline bool HasGenericServices(const FileDescriptor* file) {
200 313 : return file->service_count() > 0 &&
201 393 : file->options().optimize_for() != FileOptions::LITE_RUNTIME &&
202 80 : file->options().cc_generic_services();
203 : }
204 :
205 : // Should we generate a separate, super-optimized code path for serializing to
206 : // flat arrays? We don't do this in Lite mode because we'd rather reduce code
207 : // size.
208 1078 : inline bool HasFastArraySerialization(const FileDescriptor* file) {
209 1078 : return file->options().optimize_for() == FileOptions::SPEED;
210 : }
211 :
212 : // Returns whether we have to generate code with static initializers.
213 : bool StaticInitializersForced(const FileDescriptor* file);
214 :
215 : // Prints 'with_static_init' if static initializers have to be used for the
216 : // provided file. Otherwise emits both 'with_static_init' and
217 : // 'without_static_init' using #ifdef.
218 : void PrintHandlingOptionalStaticInitializers(
219 : const FileDescriptor* file, io::Printer* printer,
220 : const char* with_static_init, const char* without_static_init,
221 : const char* var1 = NULL, const string& val1 = "",
222 : const char* var2 = NULL, const string& val2 = "");
223 :
224 : void PrintHandlingOptionalStaticInitializers(
225 : const map<string, string>& vars, const FileDescriptor* file,
226 : io::Printer* printer, const char* with_static_init,
227 : const char* without_static_init);
228 :
229 :
230 3063 : inline bool IsMapEntryMessage(const Descriptor* descriptor) {
231 3063 : return descriptor->options().map_entry();
232 : }
233 :
234 : // Returns true if the field's CPPTYPE is string or message.
235 : bool IsStringOrMessage(const FieldDescriptor* field);
236 :
237 : // For a string field, returns the effective ctype. If the actual ctype is
238 : // not supported, returns the default of STRING.
239 : FieldOptions::CType EffectiveStringCType(const FieldDescriptor* field);
240 :
241 : string UnderscoresToCamelCase(const string& input, bool cap_next_letter);
242 :
243 31766 : inline bool HasFieldPresence(const FileDescriptor* file) {
244 31955 : return file->syntax() != FileDescriptor::SYNTAX_PROTO3;
245 : }
246 :
247 : // Returns true if 'enum' semantics are such that unknown values are preserved
248 : // in the enum field itself, rather than going to the UnknownFieldSet.
249 392 : inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) {
250 392 : return file->syntax() == FileDescriptor::SYNTAX_PROTO3;
251 : }
252 :
253 12465 : inline bool SupportsArenas(const FileDescriptor* file) {
254 12529 : return file->options().cc_enable_arenas();
255 : }
256 :
257 3272 : inline bool SupportsArenas(const Descriptor* desc) {
258 3368 : return SupportsArenas(desc->file());
259 : }
260 :
261 9161 : inline bool SupportsArenas(const FieldDescriptor* field) {
262 9161 : return SupportsArenas(field->file());
263 : }
264 :
265 : bool IsAnyMessage(const FileDescriptor* descriptor);
266 : bool IsAnyMessage(const Descriptor* descriptor);
267 :
268 : void GenerateUtf8CheckCodeForString(
269 : const FieldDescriptor* field,
270 : bool for_parse,
271 : const map<string, string>& variables,
272 : const char* parameters,
273 : io::Printer* printer);
274 :
275 : void GenerateUtf8CheckCodeForCord(
276 : const FieldDescriptor* field,
277 : bool for_parse,
278 : const map<string, string>& variables,
279 : const char* parameters,
280 : io::Printer* printer);
281 :
282 : } // namespace cpp
283 : } // namespace compiler
284 : } // namespace protobuf
285 :
286 : } // namespace google
287 : #endif // GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__
|