| 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 // http://code.google.com/p/protobuf/ |
| 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. |
| 11 // * Redistributions in binary form must reproduce the above | 11 // * Redistributions in binary form must reproduce the above |
| 12 // copyright notice, this list of conditions and the following disclaimer | 12 // copyright notice, this list of conditions and the following disclaimer |
| 13 // in the documentation and/or other materials provided with the | 13 // in the documentation and/or other materials provided with the |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 // true, prefix the type with the full namespace. For example, if you had: | 59 // true, prefix the type with the full namespace. For example, if you had: |
| 60 // package foo.bar; | 60 // package foo.bar; |
| 61 // message Baz { message Qux {} } | 61 // message Baz { message Qux {} } |
| 62 // Then the qualified ClassName for Qux would be: | 62 // Then the qualified ClassName for Qux would be: |
| 63 // ::foo::bar::Baz_Qux | 63 // ::foo::bar::Baz_Qux |
| 64 // While the non-qualified version would be: | 64 // While the non-qualified version would be: |
| 65 // Baz_Qux | 65 // Baz_Qux |
| 66 string ClassName(const Descriptor* descriptor, bool qualified); | 66 string ClassName(const Descriptor* descriptor, bool qualified); |
| 67 string ClassName(const EnumDescriptor* enum_descriptor, bool qualified); | 67 string ClassName(const EnumDescriptor* enum_descriptor, bool qualified); |
| 68 | 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 string SuperClassName(const Descriptor* descriptor); | 69 string SuperClassName(const Descriptor* descriptor); |
| 74 | 70 |
| 75 // Get the (unqualified) name that should be used for this field in C++ code. | 71 // Get the (unqualified) name that should be used for this field in C++ code. |
| 76 // The name is coerced to lower-case to emulate proto1 behavior. People | 72 // The name is coerced to lower-case to emulate proto1 behavior. People |
| 77 // should be using lowercase-with-underscores style for proto field names | 73 // should be using lowercase-with-underscores style for proto field names |
| 78 // anyway, so normally this just returns field->name(). | 74 // anyway, so normally this just returns field->name(). |
| 79 string FieldName(const FieldDescriptor* field); | 75 string FieldName(const FieldDescriptor* field); |
| 80 | 76 |
| 81 // Get the sanitized name that should be used for the given enum in C++ code. | |
| 82 string EnumValueName(const EnumValueDescriptor* enum_value); | |
| 83 | |
| 84 // Get the unqualified name that should be used for a field's field | 77 // Get the unqualified name that should be used for a field's field |
| 85 // number constant. | 78 // number constant. |
| 86 string FieldConstantName(const FieldDescriptor *field); | 79 string FieldConstantName(const FieldDescriptor *field); |
| 87 | 80 |
| 88 // Returns the scope where the field was defined (for extensions, this is | 81 // Returns the scope where the field was defined (for extensions, this is |
| 89 // different from the message type to which the field applies). | 82 // different from the message type to which the field applies). |
| 90 inline const Descriptor* FieldScope(const FieldDescriptor* field) { | 83 inline const Descriptor* FieldScope(const FieldDescriptor* field) { |
| 91 return field->is_extension() ? | 84 return field->is_extension() ? |
| 92 field->extension_scope() : field->containing_type(); | 85 field->extension_scope() : field->containing_type(); |
| 93 } | 86 } |
| 94 | 87 |
| 95 // Returns true if the given 'field_descriptor' has a message type that is | |
| 96 // a dependency of the file where the field is defined (i.e., the field | |
| 97 // type is defined in a different file than the message holding the field). | |
| 98 // | |
| 99 // This only applies to Message-typed fields. Enum-typed fields may refer | |
| 100 // to an enum in a dependency; however, enums are specified and | |
| 101 // forward-declared with an enum-base, so the definition is not required to | |
| 102 // manipulate the field value. | |
| 103 bool IsFieldDependent(const FieldDescriptor* field_descriptor); | |
| 104 | |
| 105 // Returns the name that should be used for forcing dependent lookup from a | |
| 106 // dependent base class. | |
| 107 string DependentTypeName(const FieldDescriptor* field); | |
| 108 | |
| 109 // Returns the fully-qualified type name field->message_type(). Usually this | 88 // Returns the fully-qualified type name field->message_type(). Usually this |
| 110 // is just ClassName(field->message_type(), true); | 89 // is just ClassName(field->message_type(), true); |
| 111 string FieldMessageTypeName(const FieldDescriptor* field); | 90 string FieldMessageTypeName(const FieldDescriptor* field); |
| 112 | 91 |
| 113 // Strips ".proto" or ".protodevel" from the end of a filename. | 92 // Strips ".proto" or ".protodevel" from the end of a filename. |
| 114 string StripProto(const string& filename); | 93 string StripProto(const string& filename); |
| 115 | 94 |
| 116 // Get the C++ type name for a primitive type (e.g. "double", "::google::protobu
f::int32", etc.). | 95 // Get the C++ type name for a primitive type (e.g. "double", "::google::protobu
f::int32", etc.). |
| 117 // Note: non-built-in type names will be qualified, meaning they will start | 96 // Note: non-built-in type names will be qualified, meaning they will start |
| 118 // with a ::. If you are using the type as a template parameter, you will | 97 // with a ::. If you are using the type as a template parameter, you will |
| 119 // need to insure there is a space between the < and the ::, because the | 98 // need to insure there is a space between the < and the ::, because the |
| 120 // ridiculous C++ standard defines "<:" to be a synonym for "[". | 99 // ridiculous C++ standard defines "<:" to be a synonym for "[". |
| 121 const char* PrimitiveTypeName(FieldDescriptor::CppType type); | 100 const char* PrimitiveTypeName(FieldDescriptor::CppType type); |
| 122 | 101 |
| 123 // Get the declared type name in CamelCase format, as is used e.g. for the | 102 // Get the declared type name in CamelCase format, as is used e.g. for the |
| 124 // methods of WireFormat. For example, TYPE_INT32 becomes "Int32". | 103 // methods of WireFormat. For example, TYPE_INT32 becomes "Int32". |
| 125 const char* DeclaredTypeMethodName(FieldDescriptor::Type type); | 104 const char* DeclaredTypeMethodName(FieldDescriptor::Type type); |
| 126 | 105 |
| 127 // Return the code that evaluates to the number when compiled. | |
| 128 string Int32ToString(int number); | |
| 129 | |
| 130 // Return the code that evaluates to the number when compiled. | |
| 131 string Int64ToString(int64 number); | |
| 132 | |
| 133 // Get code that evaluates to the field's default value. | 106 // Get code that evaluates to the field's default value. |
| 134 string DefaultValue(const FieldDescriptor* field); | 107 string DefaultValue(const FieldDescriptor* field); |
| 135 | 108 |
| 136 // Convert a file name into a valid identifier. | 109 // Convert a file name into a valid identifier. |
| 137 string FilenameIdentifier(const string& filename); | 110 string FilenameIdentifier(const string& filename); |
| 138 | 111 |
| 139 // Return the name of the AddDescriptors() function for a given file. | 112 // Return the name of the AddDescriptors() function for a given file. |
| 140 string GlobalAddDescriptorsName(const string& filename); | 113 string GlobalAddDescriptorsName(const string& filename); |
| 141 | 114 |
| 142 // Return the name of the AssignDescriptors() function for a given file. | 115 // Return the name of the AssignDescriptors() function for a given file. |
| 143 string GlobalAssignDescriptorsName(const string& filename); | 116 string GlobalAssignDescriptorsName(const string& filename); |
| 144 | 117 |
| 145 // Return the qualified C++ name for a file level symbol. | |
| 146 string QualifiedFileLevelSymbol(const string& package, const string& name); | |
| 147 | |
| 148 // Return the name of the ShutdownFile() function for a given file. | 118 // Return the name of the ShutdownFile() function for a given file. |
| 149 string GlobalShutdownFileName(const string& filename); | 119 string GlobalShutdownFileName(const string& filename); |
| 150 | 120 |
| 151 // Escape C++ trigraphs by escaping question marks to \? | 121 // Escape C++ trigraphs by escaping question marks to \? |
| 152 string EscapeTrigraphs(const string& to_escape); | 122 string EscapeTrigraphs(const string& to_escape); |
| 153 | 123 |
| 154 // Escaped function name to eliminate naming conflict. | 124 // Do message classes in this file keep track of unknown fields? |
| 155 string SafeFunctionName(const Descriptor* descriptor, | 125 inline bool HasUnknownFields(const FileDescriptor* file) { |
| 156 const FieldDescriptor* field, | |
| 157 const string& prefix); | |
| 158 | |
| 159 // Returns true if unknown fields are preseved after parsing. | |
| 160 inline bool PreserveUnknownFields(const Descriptor* message) { | |
| 161 return message->file()->syntax() != FileDescriptor::SYNTAX_PROTO3; | |
| 162 } | |
| 163 | |
| 164 // If PreserveUnknownFields() is true, determines whether unknown | |
| 165 // fields will be stored in an UnknownFieldSet or a string. | |
| 166 // If PreserveUnknownFields() is false, this method will not be | |
| 167 // used. | |
| 168 inline bool UseUnknownFieldSet(const FileDescriptor* file) { | |
| 169 return file->options().optimize_for() != FileOptions::LITE_RUNTIME || | 126 return file->options().optimize_for() != FileOptions::LITE_RUNTIME || |
| 170 file->options().retain_unknown_fields(); | 127 file->options().retain_unknown_fields(); |
| 171 } | 128 } |
| 172 | 129 |
| 173 | |
| 174 // Does the file have any map fields, necessitating the file to include | |
| 175 // map_field_inl.h and map.h. | |
| 176 bool HasMapFields(const FileDescriptor* file); | |
| 177 | |
| 178 // Does this file have any enum type definitions? | 130 // Does this file have any enum type definitions? |
| 179 bool HasEnumDefinitions(const FileDescriptor* file); | 131 bool HasEnumDefinitions(const FileDescriptor* file); |
| 180 | 132 |
| 181 // Does this file have generated parsing, serialization, and other | 133 // Does this file have generated parsing, serialization, and other |
| 182 // standard methods for which reflection-based fallback implementations exist? | 134 // standard methods for which reflection-based fallback implementations exist? |
| 183 inline bool HasGeneratedMethods(const FileDescriptor* file) { | 135 inline bool HasGeneratedMethods(const FileDescriptor* file) { |
| 184 return file->options().optimize_for() != FileOptions::CODE_SIZE; | 136 return file->options().optimize_for() != FileOptions::CODE_SIZE; |
| 185 } | 137 } |
| 186 | 138 |
| 187 // Do message classes in this file have descriptor and reflection methods? | 139 // Do message classes in this file have descriptor and refelction methods? |
| 188 inline bool HasDescriptorMethods(const FileDescriptor* file) { | 140 inline bool HasDescriptorMethods(const FileDescriptor* file) { |
| 189 return file->options().optimize_for() != FileOptions::LITE_RUNTIME; | 141 return file->options().optimize_for() != FileOptions::LITE_RUNTIME; |
| 190 } | 142 } |
| 191 | 143 |
| 192 // Should we generate generic services for this file? | 144 // Should we generate generic services for this file? |
| 193 inline bool HasGenericServices(const FileDescriptor* file) { | 145 inline bool HasGenericServices(const FileDescriptor* file) { |
| 194 return file->service_count() > 0 && | 146 return file->service_count() > 0 && |
| 195 file->options().optimize_for() != FileOptions::LITE_RUNTIME && | 147 file->options().optimize_for() != FileOptions::LITE_RUNTIME && |
| 196 file->options().cc_generic_services(); | 148 file->options().cc_generic_services(); |
| 197 } | 149 } |
| (...skipping 21 matching lines...) Expand all Loading... |
| 219 const char* with_static_init, const char* without_static_init, | 171 const char* with_static_init, const char* without_static_init, |
| 220 const char* var1 = NULL, const string& val1 = "", | 172 const char* var1 = NULL, const string& val1 = "", |
| 221 const char* var2 = NULL, const string& val2 = ""); | 173 const char* var2 = NULL, const string& val2 = ""); |
| 222 | 174 |
| 223 void PrintHandlingOptionalStaticInitializers( | 175 void PrintHandlingOptionalStaticInitializers( |
| 224 const map<string, string>& vars, const FileDescriptor* file, | 176 const map<string, string>& vars, const FileDescriptor* file, |
| 225 io::Printer* printer, const char* with_static_init, | 177 io::Printer* printer, const char* with_static_init, |
| 226 const char* without_static_init); | 178 const char* without_static_init); |
| 227 | 179 |
| 228 | 180 |
| 229 inline bool IsMapEntryMessage(const Descriptor* descriptor) { | |
| 230 return descriptor->options().map_entry(); | |
| 231 } | |
| 232 | |
| 233 // Returns true if the field's CPPTYPE is string or message. | |
| 234 bool IsStringOrMessage(const FieldDescriptor* field); | |
| 235 | |
| 236 // For a string field, returns the effective ctype. If the actual ctype is | |
| 237 // not supported, returns the default of STRING. | |
| 238 FieldOptions::CType EffectiveStringCType(const FieldDescriptor* field); | |
| 239 | |
| 240 string UnderscoresToCamelCase(const string& input, bool cap_next_letter); | |
| 241 | |
| 242 inline bool HasFieldPresence(const FileDescriptor* file) { | |
| 243 return file->syntax() != FileDescriptor::SYNTAX_PROTO3; | |
| 244 } | |
| 245 | |
| 246 // Returns true if 'enum' semantics are such that unknown values are preserved | |
| 247 // in the enum field itself, rather than going to the UnknownFieldSet. | |
| 248 inline bool HasPreservingUnknownEnumSemantics(const FileDescriptor* file) { | |
| 249 return file->syntax() == FileDescriptor::SYNTAX_PROTO3; | |
| 250 } | |
| 251 | |
| 252 inline bool SupportsArenas(const FileDescriptor* file) { | |
| 253 return file->options().cc_enable_arenas(); | |
| 254 } | |
| 255 | |
| 256 inline bool SupportsArenas(const Descriptor* desc) { | |
| 257 return SupportsArenas(desc->file()); | |
| 258 } | |
| 259 | |
| 260 inline bool SupportsArenas(const FieldDescriptor* field) { | |
| 261 return SupportsArenas(field->file()); | |
| 262 } | |
| 263 | |
| 264 bool IsAnyMessage(const FileDescriptor* descriptor); | |
| 265 bool IsAnyMessage(const Descriptor* descriptor); | |
| 266 | |
| 267 } // namespace cpp | 181 } // namespace cpp |
| 268 } // namespace compiler | 182 } // namespace compiler |
| 269 } // namespace protobuf | 183 } // namespace protobuf |
| 270 | 184 |
| 271 } // namespace google | 185 } // namespace google |
| 272 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__ | 186 #endif // GOOGLE_PROTOBUF_COMPILER_CPP_HELPERS_H__ |
| OLD | NEW |