blob: 7e3a7496620d095a43d549ac058704b79520c8ed [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34//
35// This file contains classes which describe a type of protocol message.
36// You can use a message's descriptor to learn at runtime what fields
37// it contains and what the types of those fields are. The Message
38// interface also allows you to dynamically access and modify individual
39// fields by passing the FieldDescriptor of the field you are interested
40// in.
41//
42// Most users will not care about descriptors, because they will write
43// code specific to certain protocol types and will simply use the classes
44// generated by the protocol compiler directly. Advanced users who want
45// to operate on arbitrary types (not known at compile time) may want to
46// read descriptors in order to learn about the contents of a message.
47// A very small number of users will want to construct their own
48// Descriptors, either because they are implementing Message manually or
49// because they are writing something like the protocol compiler.
50//
51// For an example of how you might use descriptors, see the code example
52// at the top of message.h.
53
54#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
55#define GOOGLE_PROTOBUF_DESCRIPTOR_H__
56
Jisi Liu46e8ff62015-10-05 11:59:43 -070057#include <memory>
58#ifndef _SHARED_PTR_H
59#include <google/protobuf/stubs/shared_ptr.h>
60#endif
jieluo@google.com4de8f552014-07-18 00:47:59 +000061#include <set>
temporal40ee5512008-07-10 02:12:20 +000062#include <string>
kenton@google.comd37d46d2009-04-25 02:53:47 +000063#include <vector>
temporal40ee5512008-07-10 02:12:20 +000064#include <google/protobuf/stubs/common.h>
Feng Xiaoeee38b02015-08-22 18:25:48 -070065#include <google/protobuf/stubs/scoped_ptr.h>
temporal40ee5512008-07-10 02:12:20 +000066
Bo Yangd6c9f642015-04-24 15:34:40 -070067// TYPE_BOOL is defined in the MacOS's ConditionalMacros.h.
68#ifdef TYPE_BOOL
69#undef TYPE_BOOL
70#endif // TYPE_BOOL
temporal40ee5512008-07-10 02:12:20 +000071
72namespace google {
73namespace protobuf {
74
75// Defined in this file.
76class Descriptor;
77class FieldDescriptor;
jieluo@google.com4de8f552014-07-18 00:47:59 +000078class OneofDescriptor;
temporal40ee5512008-07-10 02:12:20 +000079class EnumDescriptor;
80class EnumValueDescriptor;
81class ServiceDescriptor;
82class MethodDescriptor;
83class FileDescriptor;
84class DescriptorDatabase;
85class DescriptorPool;
86
87// Defined in descriptor.proto
88class DescriptorProto;
89class FieldDescriptorProto;
jieluo@google.com4de8f552014-07-18 00:47:59 +000090class OneofDescriptorProto;
temporal40ee5512008-07-10 02:12:20 +000091class EnumDescriptorProto;
92class EnumValueDescriptorProto;
93class ServiceDescriptorProto;
94class MethodDescriptorProto;
95class FileDescriptorProto;
96class MessageOptions;
97class FieldOptions;
98class EnumOptions;
99class EnumValueOptions;
100class ServiceOptions;
101class MethodOptions;
102class FileOptions;
kenton@google.com24bf56f2008-09-24 20:31:01 +0000103class UninterpretedOption;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000104class SourceCodeInfo;
temporal40ee5512008-07-10 02:12:20 +0000105
106// Defined in message.h
107class Message;
108
109// Defined in descriptor.cc
110class DescriptorBuilder;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000111class FileDescriptorTables;
temporal40ee5512008-07-10 02:12:20 +0000112
kenton@google.com24bf56f2008-09-24 20:31:01 +0000113// Defined in unknown_field_set.h.
114class UnknownField;
115
Feng Xiao6ef984a2014-11-10 17:34:54 -0800116// Defined in generated_message_reflection.h.
117namespace internal {
Jisi Liu46e8ff62015-10-05 11:59:43 -0700118class GeneratedMessageReflection;
119} // namespace internal
120
121// Defined in command_line_interface.cc
122namespace compiler {
123class CommandLineInterface;
124} // namespace compiler
125
126namespace descriptor_unittest {
127class DescriptorTest;
128} // namespace descriptor_unittest
Feng Xiao6ef984a2014-11-10 17:34:54 -0800129
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000130// NB, all indices are zero-based.
131struct SourceLocation {
132 int start_line;
133 int end_line;
134 int start_column;
135 int end_column;
136
137 // Doc comments found at the source location.
Jisi Liu885b6122015-02-28 14:51:22 -0800138 // See the comments in SourceCodeInfo.Location (descriptor.proto) for details.
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000139 string leading_comments;
140 string trailing_comments;
Jisi Liu885b6122015-02-28 14:51:22 -0800141 vector<string> leading_detached_comments;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000142};
143
Feng Xiao6ef984a2014-11-10 17:34:54 -0800144// Options when generating machine-parsable output from a descriptor with
145// DebugString().
146struct DebugStringOptions {
147 // include original user comments as recorded in SourceLocation entries. N.B.
148 // that this must be |false| by default: several other pieces of code (for
149 // example, the C++ code generation for fields in the proto compiler) rely on
150 // DebugString() output being unobstructed by user comments.
151 bool include_comments;
Bo Yang5db21732015-05-21 14:28:59 -0700152 // If true, elide the braced body in the debug string.
153 bool elide_group_body;
154 bool elide_oneof_body;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800155
156 DebugStringOptions()
Bo Yang5db21732015-05-21 14:28:59 -0700157 : include_comments(false),
158 elide_group_body(false),
159 elide_oneof_body(false) {}
Feng Xiao6ef984a2014-11-10 17:34:54 -0800160};
161
temporal40ee5512008-07-10 02:12:20 +0000162// Describes a type of protocol message, or a particular group within a
163// message. To obtain the Descriptor for a given message object, call
164// Message::GetDescriptor(). Generated message classes also have a
165// static method called descriptor() which returns the type's descriptor.
166// Use DescriptorPool to construct your own descriptors.
167class LIBPROTOBUF_EXPORT Descriptor {
168 public:
169 // The name of the message type, not including its scope.
170 const string& name() const;
171
172 // The fully-qualified name of the message type, scope delimited by
173 // periods. For example, message type "Foo" which is declared in package
174 // "bar" has full name "bar.Foo". If a type "Baz" is nested within
175 // Foo, Baz's full_name is "bar.Foo.Baz". To get only the part that
176 // comes after the last '.', use name().
177 const string& full_name() const;
178
179 // Index of this descriptor within the file or containing type's message
180 // type array.
181 int index() const;
182
183 // The .proto file in which this message type was defined. Never NULL.
184 const FileDescriptor* file() const;
185
186 // If this Descriptor describes a nested type, this returns the type
187 // in which it is nested. Otherwise, returns NULL.
188 const Descriptor* containing_type() const;
189
kenton@google.com24bf56f2008-09-24 20:31:01 +0000190 // Get options for this message type. These are specified in the .proto file
191 // by placing lines like "option foo = 1234;" in the message definition.
192 // Allowed options are defined by MessageOptions in
193 // google/protobuf/descriptor.proto, and any available extensions of that
194 // message.
temporal40ee5512008-07-10 02:12:20 +0000195 const MessageOptions& options() const;
196
197 // Write the contents of this Descriptor into the given DescriptorProto.
198 // The target DescriptorProto must be clear before calling this; if it
199 // isn't, the result may be garbage.
200 void CopyTo(DescriptorProto* proto) const;
201
202 // Write the contents of this decriptor in a human-readable form. Output
203 // will be suitable for re-parsing.
204 string DebugString() const;
205
Feng Xiao6ef984a2014-11-10 17:34:54 -0800206 // Similar to DebugString(), but additionally takes options (e.g.,
207 // include original user comments in output).
208 string DebugStringWithOptions(const DebugStringOptions& options) const;
209
jieluo@google.com4de8f552014-07-18 00:47:59 +0000210 // Returns true if this is a placeholder for an unknown type. This will
211 // only be the case if this descriptor comes from a DescriptorPool
212 // with AllowUnknownDependencies() set.
213 bool is_placeholder() const;
214
temporal40ee5512008-07-10 02:12:20 +0000215 // Field stuff -----------------------------------------------------
216
217 // The number of fields in this message type.
218 int field_count() const;
219 // Gets a field by index, where 0 <= index < field_count().
temporalf2063512008-07-23 01:19:07 +0000220 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000221 const FieldDescriptor* field(int index) const;
222
223 // Looks up a field by declared tag number. Returns NULL if no such field
224 // exists.
225 const FieldDescriptor* FindFieldByNumber(int number) const;
226 // Looks up a field by name. Returns NULL if no such field exists.
227 const FieldDescriptor* FindFieldByName(const string& name) const;
228
kenton@google.com2d6daa72009-01-22 01:27:00 +0000229 // Looks up a field by lowercased name (as returned by lowercase_name()).
230 // This lookup may be ambiguous if multiple field names differ only by case,
231 // in which case the field returned is chosen arbitrarily from the matches.
232 const FieldDescriptor* FindFieldByLowercaseName(
233 const string& lowercase_name) const;
234
235 // Looks up a field by camel-case name (as returned by camelcase_name()).
236 // This lookup may be ambiguous if multiple field names differ in a way that
237 // leads them to have identical camel-case names, in which case the field
238 // returned is chosen arbitrarily from the matches.
239 const FieldDescriptor* FindFieldByCamelcaseName(
240 const string& camelcase_name) const;
241
jieluo@google.com4de8f552014-07-18 00:47:59 +0000242 // The number of oneofs in this message type.
243 int oneof_decl_count() const;
244 // Get a oneof by index, where 0 <= index < oneof_decl_count().
245 // These are returned in the order they were defined in the .proto file.
246 const OneofDescriptor* oneof_decl(int index) const;
247
248 // Looks up a oneof by name. Returns NULL if no such oneof exists.
249 const OneofDescriptor* FindOneofByName(const string& name) const;
250
temporal40ee5512008-07-10 02:12:20 +0000251 // Nested type stuff -----------------------------------------------
252
253 // The number of nested types in this message type.
254 int nested_type_count() const;
255 // Gets a nested type by index, where 0 <= index < nested_type_count().
temporalf2063512008-07-23 01:19:07 +0000256 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000257 const Descriptor* nested_type(int index) const;
258
259 // Looks up a nested type by name. Returns NULL if no such nested type
260 // exists.
261 const Descriptor* FindNestedTypeByName(const string& name) const;
262
263 // Enum stuff ------------------------------------------------------
264
265 // The number of enum types in this message type.
266 int enum_type_count() const;
267 // Gets an enum type by index, where 0 <= index < enum_type_count().
temporalf2063512008-07-23 01:19:07 +0000268 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000269 const EnumDescriptor* enum_type(int index) const;
270
271 // Looks up an enum type by name. Returns NULL if no such enum type exists.
272 const EnumDescriptor* FindEnumTypeByName(const string& name) const;
273
274 // Looks up an enum value by name, among all enum types in this message.
275 // Returns NULL if no such value exists.
276 const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
277
278 // Extensions ------------------------------------------------------
279
280 // A range of field numbers which are designated for third-party
281 // extensions.
282 struct ExtensionRange {
283 int start; // inclusive
284 int end; // exclusive
285 };
286
287 // The number of extension ranges in this message type.
288 int extension_range_count() const;
289 // Gets an extension range by index, where 0 <= index <
temporalf2063512008-07-23 01:19:07 +0000290 // extension_range_count(). These are returned in the order they were defined
291 // in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000292 const ExtensionRange* extension_range(int index) const;
293
294 // Returns true if the number is in one of the extension ranges.
295 bool IsExtensionNumber(int number) const;
296
jieluo@google.com4de8f552014-07-18 00:47:59 +0000297 // Returns NULL if no extension range contains the given number.
298 const ExtensionRange* FindExtensionRangeContainingNumber(int number) const;
299
temporal40ee5512008-07-10 02:12:20 +0000300 // The number of extensions -- extending *other* messages -- that were
301 // defined nested within this message type's scope.
302 int extension_count() const;
303 // Get an extension by index, where 0 <= index < extension_count().
temporalf2063512008-07-23 01:19:07 +0000304 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000305 const FieldDescriptor* extension(int index) const;
306
307 // Looks up a named extension (which extends some *other* message type)
308 // defined within this message type's scope.
309 const FieldDescriptor* FindExtensionByName(const string& name) const;
310
kenton@google.com2d6daa72009-01-22 01:27:00 +0000311 // Similar to FindFieldByLowercaseName(), but finds extensions defined within
312 // this message type's scope.
313 const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
314
315 // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
316 // this message type's scope.
317 const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
318
Bo Yang5db21732015-05-21 14:28:59 -0700319 // Reserved fields -------------------------------------------------
320
321 // A range of reserved field numbers.
322 struct ReservedRange {
323 int start; // inclusive
324 int end; // exclusive
325 };
326
327 // The number of reserved ranges in this message type.
328 int reserved_range_count() const;
329 // Gets an reserved range by index, where 0 <= index <
330 // reserved_range_count(). These are returned in the order they were defined
331 // in the .proto file.
332 const ReservedRange* reserved_range(int index) const;
333
334 // Returns true if the number is in one of the reserved ranges.
335 bool IsReservedNumber(int number) const;
336
337 // Returns NULL if no reserved range contains the given number.
338 const ReservedRange* FindReservedRangeContainingNumber(int number) const;
339
340 // The number of reserved field names in this message type.
341 int reserved_name_count() const;
342
343 // Gets a reserved name by index, where 0 <= index < reserved_name_count().
344 const string& reserved_name(int index) const;
345
346 // Returns true if the field name is reserved.
347 bool IsReservedName(const string& name) const;
348
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000349 // Source Location ---------------------------------------------------
350
351 // Updates |*out_location| to the source location of the complete
352 // extent of this message declaration. Returns false and leaves
353 // |*out_location| unchanged iff location information was not available.
354 bool GetSourceLocation(SourceLocation* out_location) const;
355
temporal40ee5512008-07-10 02:12:20 +0000356 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +0000357 typedef MessageOptions OptionsType;
358
Jisi Liu46e8ff62015-10-05 11:59:43 -0700359 // Allows tests to test CopyTo(proto, true).
360 friend class ::google::protobuf::descriptor_unittest::DescriptorTest;
361
362 // Fill the json_name field of FieldDescriptorProto.
363 void CopyJsonNameTo(DescriptorProto* proto) const;
364
temporal40ee5512008-07-10 02:12:20 +0000365 // Internal version of DebugString; controls the level of indenting for
Feng Xiao6ef984a2014-11-10 17:34:54 -0800366 // correct depth. Takes |options| to control debug-string options, and
367 // |include_opening_clause| to indicate whether the "message ... " part of the
368 // clause has already been generated (this varies depending on context).
369 void DebugString(int depth, string *contents,
370 const DebugStringOptions& options,
371 bool include_opening_clause) const;
temporal40ee5512008-07-10 02:12:20 +0000372
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000373 // Walks up the descriptor tree to generate the source location path
374 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -0800375 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000376
temporal40ee5512008-07-10 02:12:20 +0000377 const string* name_;
378 const string* full_name_;
379 const FileDescriptor* file_;
380 const Descriptor* containing_type_;
381 const MessageOptions* options_;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000382
383 // True if this is a placeholder for an unknown type.
384 bool is_placeholder_;
385 // True if this is a placeholder and the type name wasn't fully-qualified.
386 bool is_unqualified_placeholder_;
387
temporal40ee5512008-07-10 02:12:20 +0000388 int field_count_;
389 FieldDescriptor* fields_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000390 int oneof_decl_count_;
391 OneofDescriptor* oneof_decls_;
temporal40ee5512008-07-10 02:12:20 +0000392 int nested_type_count_;
393 Descriptor* nested_types_;
394 int enum_type_count_;
395 EnumDescriptor* enum_types_;
396 int extension_range_count_;
397 ExtensionRange* extension_ranges_;
398 int extension_count_;
399 FieldDescriptor* extensions_;
Bo Yang5db21732015-05-21 14:28:59 -0700400 int reserved_range_count_;
401 ReservedRange* reserved_ranges_;
402 int reserved_name_count_;
403 const string** reserved_names_;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000404 // IMPORTANT: If you add a new field, make sure to search for all instances
405 // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
406 // and update them to initialize the field.
temporal40ee5512008-07-10 02:12:20 +0000407
408 // Must be constructed using DescriptorPool.
409 Descriptor() {}
410 friend class DescriptorBuilder;
411 friend class EnumDescriptor;
412 friend class FieldDescriptor;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000413 friend class OneofDescriptor;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000414 friend class MethodDescriptor;
temporal40ee5512008-07-10 02:12:20 +0000415 friend class FileDescriptor;
416 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
417};
418
419// Describes a single field of a message. To get the descriptor for a given
420// field, first get the Descriptor for the message in which it is defined,
421// then call Descriptor::FindFieldByName(). To get a FieldDescriptor for
422// an extension, do one of the following:
423// - Get the Descriptor or FileDescriptor for its containing scope, then
424// call Descriptor::FindExtensionByName() or
425// FileDescriptor::FindExtensionByName().
426// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
temporal779f61c2008-08-13 03:15:00 +0000427// - Given a Reflection for a message object, call
428// Reflection::FindKnownExtensionByName() or
429// Reflection::FindKnownExtensionByNumber().
temporal40ee5512008-07-10 02:12:20 +0000430// Use DescriptorPool to construct your own descriptors.
431class LIBPROTOBUF_EXPORT FieldDescriptor {
432 public:
433 // Identifies a field type. 0 is reserved for errors. The order is weird
434 // for historical reasons. Types 12 and up are new in proto2.
435 enum Type {
436 TYPE_DOUBLE = 1, // double, exactly eight bytes on the wire.
437 TYPE_FLOAT = 2, // float, exactly four bytes on the wire.
438 TYPE_INT64 = 3, // int64, varint on the wire. Negative numbers
439 // take 10 bytes. Use TYPE_SINT64 if negative
440 // values are likely.
441 TYPE_UINT64 = 4, // uint64, varint on the wire.
442 TYPE_INT32 = 5, // int32, varint on the wire. Negative numbers
443 // take 10 bytes. Use TYPE_SINT32 if negative
444 // values are likely.
445 TYPE_FIXED64 = 6, // uint64, exactly eight bytes on the wire.
446 TYPE_FIXED32 = 7, // uint32, exactly four bytes on the wire.
447 TYPE_BOOL = 8, // bool, varint on the wire.
448 TYPE_STRING = 9, // UTF-8 text.
449 TYPE_GROUP = 10, // Tag-delimited message. Deprecated.
450 TYPE_MESSAGE = 11, // Length-delimited message.
451
452 TYPE_BYTES = 12, // Arbitrary byte array.
453 TYPE_UINT32 = 13, // uint32, varint on the wire
454 TYPE_ENUM = 14, // Enum, varint on the wire
455 TYPE_SFIXED32 = 15, // int32, exactly four bytes on the wire
456 TYPE_SFIXED64 = 16, // int64, exactly eight bytes on the wire
457 TYPE_SINT32 = 17, // int32, ZigZag-encoded varint on the wire
458 TYPE_SINT64 = 18, // int64, ZigZag-encoded varint on the wire
459
460 MAX_TYPE = 18, // Constant useful for defining lookup tables
461 // indexed by Type.
462 };
463
464 // Specifies the C++ data type used to represent the field. There is a
465 // fixed mapping from Type to CppType where each Type maps to exactly one
466 // CppType. 0 is reserved for errors.
467 enum CppType {
468 CPPTYPE_INT32 = 1, // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
469 CPPTYPE_INT64 = 2, // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
470 CPPTYPE_UINT32 = 3, // TYPE_UINT32, TYPE_FIXED32
471 CPPTYPE_UINT64 = 4, // TYPE_UINT64, TYPE_FIXED64
472 CPPTYPE_DOUBLE = 5, // TYPE_DOUBLE
473 CPPTYPE_FLOAT = 6, // TYPE_FLOAT
474 CPPTYPE_BOOL = 7, // TYPE_BOOL
475 CPPTYPE_ENUM = 8, // TYPE_ENUM
476 CPPTYPE_STRING = 9, // TYPE_STRING, TYPE_BYTES
477 CPPTYPE_MESSAGE = 10, // TYPE_MESSAGE, TYPE_GROUP
478
479 MAX_CPPTYPE = 10, // Constant useful for defining lookup tables
480 // indexed by CppType.
481 };
482
483 // Identifies whether the field is optional, required, or repeated. 0 is
484 // reserved for errors.
485 enum Label {
486 LABEL_OPTIONAL = 1, // optional
487 LABEL_REQUIRED = 2, // required
488 LABEL_REPEATED = 3, // repeated
489
490 MAX_LABEL = 3, // Constant useful for defining lookup tables
491 // indexed by Label.
492 };
493
494 // Valid field numbers are positive integers up to kMaxNumber.
495 static const int kMaxNumber = (1 << 29) - 1;
496
497 // First field number reserved for the protocol buffer library implementation.
498 // Users may not declare fields that use reserved numbers.
499 static const int kFirstReservedNumber = 19000;
500 // Last field number reserved for the protocol buffer library implementation.
501 // Users may not declare fields that use reserved numbers.
502 static const int kLastReservedNumber = 19999;
503
504 const string& name() const; // Name of this field within the message.
505 const string& full_name() const; // Fully-qualified name of the field.
Jisi Liu46e8ff62015-10-05 11:59:43 -0700506 const string& json_name() const; // JSON name of this field.
temporal40ee5512008-07-10 02:12:20 +0000507 const FileDescriptor* file() const;// File in which this field was defined.
508 bool is_extension() const; // Is this an extension field?
509 int number() const; // Declared tag number.
510
kenton@google.com2d6daa72009-01-22 01:27:00 +0000511 // Same as name() except converted to lower-case. This (and especially the
512 // FindFieldByLowercaseName() method) can be useful when parsing formats
513 // which prefer to use lowercase naming style. (Although, technically
514 // field names should be lowercased anyway according to the protobuf style
515 // guide, so this only makes a difference when dealing with old .proto files
516 // which do not follow the guide.)
517 const string& lowercase_name() const;
518
519 // Same as name() except converted to camel-case. In this conversion, any
520 // time an underscore appears in the name, it is removed and the next
521 // letter is capitalized. Furthermore, the first letter of the name is
522 // lower-cased. Examples:
523 // FooBar -> fooBar
524 // foo_bar -> fooBar
525 // fooBar -> fooBar
526 // This (and especially the FindFieldByCamelcaseName() method) can be useful
527 // when parsing formats which prefer to use camel-case naming style.
528 const string& camelcase_name() const;
529
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000530 Type type() const; // Declared type of this field.
531 const char* type_name() const; // Name of the declared type.
532 CppType cpp_type() const; // C++ type of this field.
533 const char* cpp_type_name() const; // Name of the C++ type.
534 Label label() const; // optional/required/repeated
temporal40ee5512008-07-10 02:12:20 +0000535
536 bool is_required() const; // shorthand for label() == LABEL_REQUIRED
537 bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL
538 bool is_repeated() const; // shorthand for label() == LABEL_REPEATED
kenton@google.comfccb1462009-12-18 02:11:36 +0000539 bool is_packable() const; // shorthand for is_repeated() &&
540 // IsTypePackable(type())
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000541 bool is_packed() const; // shorthand for is_packable() &&
542 // options().packed()
Feng Xiaof157a562014-11-14 11:50:31 -0800543 bool is_map() const; // shorthand for type() == TYPE_MESSAGE &&
544 // message_type()->options().map_entry()
temporal40ee5512008-07-10 02:12:20 +0000545
546 // Index of this field within the message's field array, or the file or
547 // extension scope's extensions array.
548 int index() const;
549
550 // Does this field have an explicitly-declared default value?
551 bool has_default_value() const;
552
553 // Get the field default value if cpp_type() == CPPTYPE_INT32. If no
554 // explicit default was defined, the default is 0.
555 int32 default_value_int32() const;
556 // Get the field default value if cpp_type() == CPPTYPE_INT64. If no
557 // explicit default was defined, the default is 0.
558 int64 default_value_int64() const;
559 // Get the field default value if cpp_type() == CPPTYPE_UINT32. If no
560 // explicit default was defined, the default is 0.
561 uint32 default_value_uint32() const;
562 // Get the field default value if cpp_type() == CPPTYPE_UINT64. If no
563 // explicit default was defined, the default is 0.
564 uint64 default_value_uint64() const;
565 // Get the field default value if cpp_type() == CPPTYPE_FLOAT. If no
566 // explicit default was defined, the default is 0.0.
567 float default_value_float() const;
568 // Get the field default value if cpp_type() == CPPTYPE_DOUBLE. If no
569 // explicit default was defined, the default is 0.0.
570 double default_value_double() const;
571 // Get the field default value if cpp_type() == CPPTYPE_BOOL. If no
572 // explicit default was defined, the default is false.
573 bool default_value_bool() const;
574 // Get the field default value if cpp_type() == CPPTYPE_ENUM. If no
575 // explicit default was defined, the default is the first value defined
576 // in the enum type (all enum types are required to have at least one value).
577 // This never returns NULL.
578 const EnumValueDescriptor* default_value_enum() const;
579 // Get the field default value if cpp_type() == CPPTYPE_STRING. If no
580 // explicit default was defined, the default is the empty string.
581 const string& default_value_string() const;
582
583 // The Descriptor for the message of which this is a field. For extensions,
584 // this is the extended type. Never NULL.
585 const Descriptor* containing_type() const;
586
jieluo@google.com4de8f552014-07-18 00:47:59 +0000587 // If the field is a member of a oneof, this is the one, otherwise this is
588 // NULL.
589 const OneofDescriptor* containing_oneof() const;
590
591 // If the field is a member of a oneof, returns the index in that oneof.
592 int index_in_oneof() const;
593
temporal40ee5512008-07-10 02:12:20 +0000594 // An extension may be declared within the scope of another message. If this
595 // field is an extension (is_extension() is true), then extension_scope()
596 // returns that message, or NULL if the extension was declared at global
597 // scope. If this is not an extension, extension_scope() is undefined (may
598 // assert-fail).
599 const Descriptor* extension_scope() const;
600
601 // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
jieluo@google.com4de8f552014-07-18 00:47:59 +0000602 // message or the group type. Otherwise, returns null.
temporal40ee5512008-07-10 02:12:20 +0000603 const Descriptor* message_type() const;
604 // If type is TYPE_ENUM, returns a descriptor for the enum. Otherwise,
jieluo@google.com4de8f552014-07-18 00:47:59 +0000605 // returns null.
temporal40ee5512008-07-10 02:12:20 +0000606 const EnumDescriptor* enum_type() const;
607
temporal40ee5512008-07-10 02:12:20 +0000608 // Get the FieldOptions for this field. This includes things listed in
609 // square brackets after the field definition. E.g., the field:
610 // optional string text = 1 [ctype=CORD];
kenton@google.com24bf56f2008-09-24 20:31:01 +0000611 // has the "ctype" option set. Allowed options are defined by FieldOptions
612 // in google/protobuf/descriptor.proto, and any available extensions of that
613 // message.
temporal40ee5512008-07-10 02:12:20 +0000614 const FieldOptions& options() const;
615
616 // See Descriptor::CopyTo().
617 void CopyTo(FieldDescriptorProto* proto) const;
618
619 // See Descriptor::DebugString().
620 string DebugString() const;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000621
Feng Xiao6ef984a2014-11-10 17:34:54 -0800622 // See Descriptor::DebugStringWithOptions().
623 string DebugStringWithOptions(const DebugStringOptions& options) const;
624
kenton@google.comd37d46d2009-04-25 02:53:47 +0000625 // Helper method to get the CppType for a particular Type.
626 static CppType TypeToCppType(Type type);
627
jieluo@google.com4de8f552014-07-18 00:47:59 +0000628 // Helper method to get the name of a Type.
629 static const char* TypeName(Type type);
630
631 // Helper method to get the name of a CppType.
632 static const char* CppTypeName(CppType cpp_type);
633
kenton@google.comfccb1462009-12-18 02:11:36 +0000634 // Return true iff [packed = true] is valid for fields of this type.
635 static inline bool IsTypePackable(Type field_type);
636
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000637 // Source Location ---------------------------------------------------
638
639 // Updates |*out_location| to the source location of the complete
640 // extent of this field declaration. Returns false and leaves
641 // |*out_location| unchanged iff location information was not available.
642 bool GetSourceLocation(SourceLocation* out_location) const;
643
temporal40ee5512008-07-10 02:12:20 +0000644 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +0000645 typedef FieldOptions OptionsType;
646
Jisi Liu46e8ff62015-10-05 11:59:43 -0700647 // Fill the json_name field of FieldDescriptorProto.
648 void CopyJsonNameTo(FieldDescriptorProto* proto) const;
649
temporal40ee5512008-07-10 02:12:20 +0000650 // See Descriptor::DebugString().
jieluo@google.com4de8f552014-07-18 00:47:59 +0000651 enum PrintLabelFlag { PRINT_LABEL, OMIT_LABEL };
652 void DebugString(int depth, PrintLabelFlag print_label_flag,
Feng Xiao6ef984a2014-11-10 17:34:54 -0800653 string* contents, const DebugStringOptions& options) const;
temporal40ee5512008-07-10 02:12:20 +0000654
655 // formats the default value appropriately and returns it as a string.
656 // Must have a default value to call this. If quote_string_type is true, then
657 // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
658 string DefaultValueAsString(bool quote_string_type) const;
659
Feng Xiao6ef984a2014-11-10 17:34:54 -0800660 // Helper function that returns the field type name for DebugString.
661 string FieldTypeNameDebugString() const;
662
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000663 // Walks up the descriptor tree to generate the source location path
664 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -0800665 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000666
temporal40ee5512008-07-10 02:12:20 +0000667 const string* name_;
668 const string* full_name_;
kenton@google.com2d6daa72009-01-22 01:27:00 +0000669 const string* lowercase_name_;
670 const string* camelcase_name_;
Jisi Liu46e8ff62015-10-05 11:59:43 -0700671 // Whether the user has specified the json_name field option in the .proto
672 // file.
673 bool has_json_name_;
674 // If has_json_name_ is true, it's the value specified by the user.
675 // Otherwise, it has the same value as lowercase_name_.
676 const string* json_name_;
temporal40ee5512008-07-10 02:12:20 +0000677 const FileDescriptor* file_;
678 int number_;
679 Type type_;
680 Label label_;
681 bool is_extension_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000682 int index_in_oneof_;
temporal40ee5512008-07-10 02:12:20 +0000683 const Descriptor* containing_type_;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000684 const OneofDescriptor* containing_oneof_;
temporal40ee5512008-07-10 02:12:20 +0000685 const Descriptor* extension_scope_;
686 const Descriptor* message_type_;
687 const EnumDescriptor* enum_type_;
temporal40ee5512008-07-10 02:12:20 +0000688 const FieldOptions* options_;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000689 // IMPORTANT: If you add a new field, make sure to search for all instances
690 // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
691 // descriptor.cc and update them to initialize the field.
temporal40ee5512008-07-10 02:12:20 +0000692
693 bool has_default_value_;
694 union {
695 int32 default_value_int32_;
696 int64 default_value_int64_;
697 uint32 default_value_uint32_;
698 uint64 default_value_uint64_;
699 float default_value_float_;
700 double default_value_double_;
701 bool default_value_bool_;
702
703 const EnumValueDescriptor* default_value_enum_;
704 const string* default_value_string_;
705 };
706
707 static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
708
709 static const char * const kTypeToName[MAX_TYPE + 1];
710
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000711 static const char * const kCppTypeToName[MAX_CPPTYPE + 1];
712
temporal40ee5512008-07-10 02:12:20 +0000713 static const char * const kLabelToName[MAX_LABEL + 1];
714
715 // Must be constructed using DescriptorPool.
716 FieldDescriptor() {}
717 friend class DescriptorBuilder;
718 friend class FileDescriptor;
719 friend class Descriptor;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000720 friend class OneofDescriptor;
temporal40ee5512008-07-10 02:12:20 +0000721 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
722};
723
jieluo@google.com4de8f552014-07-18 00:47:59 +0000724// Describes a oneof defined in a message type.
725class LIBPROTOBUF_EXPORT OneofDescriptor {
726 public:
727 const string& name() const; // Name of this oneof.
728 const string& full_name() const; // Fully-qualified name of the oneof.
729
730 // Index of this oneof within the message's oneof array.
731 int index() const;
732
733 // The Descriptor for the message containing this oneof.
734 const Descriptor* containing_type() const;
735
736 // The number of (non-extension) fields which are members of this oneof.
737 int field_count() const;
738 // Get a member of this oneof, in the order in which they were declared in the
739 // .proto file. Does not include extensions.
740 const FieldDescriptor* field(int index) const;
741
742 // See Descriptor::CopyTo().
743 void CopyTo(OneofDescriptorProto* proto) const;
744
745 // See Descriptor::DebugString().
746 string DebugString() const;
747
Feng Xiao6ef984a2014-11-10 17:34:54 -0800748 // See Descriptor::DebugStringWithOptions().
749 string DebugStringWithOptions(const DebugStringOptions& options) const;
750
jieluo@google.com4de8f552014-07-18 00:47:59 +0000751 // Source Location ---------------------------------------------------
752
753 // Updates |*out_location| to the source location of the complete
754 // extent of this oneof declaration. Returns false and leaves
755 // |*out_location| unchanged iff location information was not available.
756 bool GetSourceLocation(SourceLocation* out_location) const;
757
758 private:
759 // See Descriptor::DebugString().
Feng Xiao6ef984a2014-11-10 17:34:54 -0800760 void DebugString(int depth, string* contents,
761 const DebugStringOptions& options) const;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000762
763 // Walks up the descriptor tree to generate the source location path
764 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -0800765 void GetLocationPath(std::vector<int>* output) const;
jieluo@google.com4de8f552014-07-18 00:47:59 +0000766
767 const string* name_;
768 const string* full_name_;
769 const Descriptor* containing_type_;
770 bool is_extendable_;
771 int field_count_;
772 const FieldDescriptor** fields_;
773 // IMPORTANT: If you add a new field, make sure to search for all instances
774 // of Allocate<OneofDescriptor>() and AllocateArray<OneofDescriptor>()
775 // in descriptor.cc and update them to initialize the field.
776
777 // Must be constructed using DescriptorPool.
778 OneofDescriptor() {}
779 friend class DescriptorBuilder;
780 friend class Descriptor;
781 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OneofDescriptor);
782};
783
temporal40ee5512008-07-10 02:12:20 +0000784// Describes an enum type defined in a .proto file. To get the EnumDescriptor
785// for a generated enum type, call TypeName_descriptor(). Use DescriptorPool
786// to construct your own descriptors.
787class LIBPROTOBUF_EXPORT EnumDescriptor {
788 public:
789 // The name of this enum type in the containing scope.
790 const string& name() const;
791
792 // The fully-qualified name of the enum type, scope delimited by periods.
793 const string& full_name() const;
794
795 // Index of this enum within the file or containing message's enum array.
796 int index() const;
797
798 // The .proto file in which this enum type was defined. Never NULL.
799 const FileDescriptor* file() const;
800
801 // The number of values for this EnumDescriptor. Guaranteed to be greater
802 // than zero.
803 int value_count() const;
804 // Gets a value by index, where 0 <= index < value_count().
temporalf2063512008-07-23 01:19:07 +0000805 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000806 const EnumValueDescriptor* value(int index) const;
807
808 // Looks up a value by name. Returns NULL if no such value exists.
809 const EnumValueDescriptor* FindValueByName(const string& name) const;
810 // Looks up a value by number. Returns NULL if no such value exists. If
811 // multiple values have this number, the first one defined is returned.
812 const EnumValueDescriptor* FindValueByNumber(int number) const;
813
814 // If this enum type is nested in a message type, this is that message type.
815 // Otherwise, NULL.
816 const Descriptor* containing_type() const;
817
kenton@google.com24bf56f2008-09-24 20:31:01 +0000818 // Get options for this enum type. These are specified in the .proto file by
819 // placing lines like "option foo = 1234;" in the enum definition. Allowed
820 // options are defined by EnumOptions in google/protobuf/descriptor.proto,
821 // and any available extensions of that message.
temporal40ee5512008-07-10 02:12:20 +0000822 const EnumOptions& options() const;
823
824 // See Descriptor::CopyTo().
825 void CopyTo(EnumDescriptorProto* proto) const;
826
827 // See Descriptor::DebugString().
828 string DebugString() const;
829
Feng Xiao6ef984a2014-11-10 17:34:54 -0800830 // See Descriptor::DebugStringWithOptions().
831 string DebugStringWithOptions(const DebugStringOptions& options) const;
832
833
jieluo@google.com4de8f552014-07-18 00:47:59 +0000834 // Returns true if this is a placeholder for an unknown enum. This will
835 // only be the case if this descriptor comes from a DescriptorPool
836 // with AllowUnknownDependencies() set.
837 bool is_placeholder() const;
838
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000839 // Source Location ---------------------------------------------------
840
841 // Updates |*out_location| to the source location of the complete
842 // extent of this enum declaration. Returns false and leaves
843 // |*out_location| unchanged iff location information was not available.
844 bool GetSourceLocation(SourceLocation* out_location) const;
845
temporal40ee5512008-07-10 02:12:20 +0000846 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +0000847 typedef EnumOptions OptionsType;
848
Feng Xiao6ef984a2014-11-10 17:34:54 -0800849 // Looks up a value by number. If the value does not exist, dynamically
850 // creates a new EnumValueDescriptor for that value, assuming that it was
851 // unknown. If a new descriptor is created, this is done in a thread-safe way,
852 // and future calls will return the same value descriptor pointer.
853 //
854 // This is private but is used by GeneratedMessageReflection (which is
855 // friended below) to return a valid EnumValueDescriptor from GetEnum() when
856 // this feature is enabled.
857 const EnumValueDescriptor*
858 FindValueByNumberCreatingIfUnknown(int number) const;
859
860
temporal40ee5512008-07-10 02:12:20 +0000861 // See Descriptor::DebugString().
Feng Xiao6ef984a2014-11-10 17:34:54 -0800862 void DebugString(int depth, string *contents,
863 const DebugStringOptions& options) const;
temporal40ee5512008-07-10 02:12:20 +0000864
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000865 // Walks up the descriptor tree to generate the source location path
866 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -0800867 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000868
temporal40ee5512008-07-10 02:12:20 +0000869 const string* name_;
870 const string* full_name_;
871 const FileDescriptor* file_;
temporal40ee5512008-07-10 02:12:20 +0000872 const Descriptor* containing_type_;
873 const EnumOptions* options_;
874
kenton@google.comd37d46d2009-04-25 02:53:47 +0000875 // True if this is a placeholder for an unknown type.
876 bool is_placeholder_;
877 // True if this is a placeholder and the type name wasn't fully-qualified.
878 bool is_unqualified_placeholder_;
879
880 int value_count_;
881 EnumValueDescriptor* values_;
882 // IMPORTANT: If you add a new field, make sure to search for all instances
883 // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
884 // descriptor.cc and update them to initialize the field.
885
temporal40ee5512008-07-10 02:12:20 +0000886 // Must be constructed using DescriptorPool.
887 EnumDescriptor() {}
888 friend class DescriptorBuilder;
889 friend class Descriptor;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000890 friend class FieldDescriptor;
temporal40ee5512008-07-10 02:12:20 +0000891 friend class EnumValueDescriptor;
892 friend class FileDescriptor;
Feng Xiaoeee38b02015-08-22 18:25:48 -0700893 friend class internal::GeneratedMessageReflection;
temporal40ee5512008-07-10 02:12:20 +0000894 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
895};
896
897// Describes an individual enum constant of a particular type. To get the
898// EnumValueDescriptor for a given enum value, first get the EnumDescriptor
899// for its type, then use EnumDescriptor::FindValueByName() or
900// EnumDescriptor::FindValueByNumber(). Use DescriptorPool to construct
901// your own descriptors.
902class LIBPROTOBUF_EXPORT EnumValueDescriptor {
903 public:
904 const string& name() const; // Name of this enum constant.
905 int index() const; // Index within the enums's Descriptor.
906 int number() const; // Numeric value of this enum constant.
907
908 // The full_name of an enum value is a sibling symbol of the enum type.
909 // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
910 // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
911 // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32". This is to conform
912 // with C++ scoping rules for enums.
913 const string& full_name() const;
914
915 // The type of this value. Never NULL.
916 const EnumDescriptor* type() const;
917
kenton@google.com24bf56f2008-09-24 20:31:01 +0000918 // Get options for this enum value. These are specified in the .proto file
919 // by adding text like "[foo = 1234]" after an enum value definition.
920 // Allowed options are defined by EnumValueOptions in
921 // google/protobuf/descriptor.proto, and any available extensions of that
922 // message.
temporal40ee5512008-07-10 02:12:20 +0000923 const EnumValueOptions& options() const;
924
925 // See Descriptor::CopyTo().
926 void CopyTo(EnumValueDescriptorProto* proto) const;
927
928 // See Descriptor::DebugString().
929 string DebugString() const;
930
Feng Xiao6ef984a2014-11-10 17:34:54 -0800931 // See Descriptor::DebugStringWithOptions().
932 string DebugStringWithOptions(const DebugStringOptions& options) const;
933
934
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000935 // Source Location ---------------------------------------------------
936
937 // Updates |*out_location| to the source location of the complete
938 // extent of this enum value declaration. Returns false and leaves
939 // |*out_location| unchanged iff location information was not available.
940 bool GetSourceLocation(SourceLocation* out_location) const;
941
temporal40ee5512008-07-10 02:12:20 +0000942 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +0000943 typedef EnumValueOptions OptionsType;
944
temporal40ee5512008-07-10 02:12:20 +0000945 // See Descriptor::DebugString().
Feng Xiao6ef984a2014-11-10 17:34:54 -0800946 void DebugString(int depth, string *contents,
947 const DebugStringOptions& options) const;
temporal40ee5512008-07-10 02:12:20 +0000948
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000949 // Walks up the descriptor tree to generate the source location path
950 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -0800951 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +0000952
temporal40ee5512008-07-10 02:12:20 +0000953 const string* name_;
954 const string* full_name_;
955 int number_;
956 const EnumDescriptor* type_;
957 const EnumValueOptions* options_;
kenton@google.comd37d46d2009-04-25 02:53:47 +0000958 // IMPORTANT: If you add a new field, make sure to search for all instances
959 // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
960 // in descriptor.cc and update them to initialize the field.
temporal40ee5512008-07-10 02:12:20 +0000961
962 // Must be constructed using DescriptorPool.
963 EnumValueDescriptor() {}
964 friend class DescriptorBuilder;
965 friend class EnumDescriptor;
Feng Xiao6ef984a2014-11-10 17:34:54 -0800966 friend class FileDescriptorTables;
temporal40ee5512008-07-10 02:12:20 +0000967 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
968};
969
970// Describes an RPC service. To get the ServiceDescriptor for a service,
971// call Service::GetDescriptor(). Generated service classes also have a
972// static method called descriptor() which returns the type's
973// ServiceDescriptor. Use DescriptorPool to construct your own descriptors.
974class LIBPROTOBUF_EXPORT ServiceDescriptor {
975 public:
976 // The name of the service, not including its containing scope.
977 const string& name() const;
978 // The fully-qualified name of the service, scope delimited by periods.
979 const string& full_name() const;
980 // Index of this service within the file's services array.
981 int index() const;
982
983 // The .proto file in which this service was defined. Never NULL.
984 const FileDescriptor* file() const;
985
kenton@google.com24bf56f2008-09-24 20:31:01 +0000986 // Get options for this service type. These are specified in the .proto file
987 // by placing lines like "option foo = 1234;" in the service definition.
988 // Allowed options are defined by ServiceOptions in
989 // google/protobuf/descriptor.proto, and any available extensions of that
990 // message.
temporal40ee5512008-07-10 02:12:20 +0000991 const ServiceOptions& options() const;
992
993 // The number of methods this service defines.
994 int method_count() const;
995 // Gets a MethodDescriptor by index, where 0 <= index < method_count().
temporalf2063512008-07-23 01:19:07 +0000996 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +0000997 const MethodDescriptor* method(int index) const;
998
999 // Look up a MethodDescriptor by name.
1000 const MethodDescriptor* FindMethodByName(const string& name) const;
temporal40ee5512008-07-10 02:12:20 +00001001 // See Descriptor::CopyTo().
1002 void CopyTo(ServiceDescriptorProto* proto) const;
1003
1004 // See Descriptor::DebugString().
1005 string DebugString() const;
1006
Feng Xiao6ef984a2014-11-10 17:34:54 -08001007 // See Descriptor::DebugStringWithOptions().
1008 string DebugStringWithOptions(const DebugStringOptions& options) const;
1009
1010
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001011 // Source Location ---------------------------------------------------
1012
1013 // Updates |*out_location| to the source location of the complete
1014 // extent of this service declaration. Returns false and leaves
1015 // |*out_location| unchanged iff location information was not available.
1016 bool GetSourceLocation(SourceLocation* out_location) const;
1017
temporal40ee5512008-07-10 02:12:20 +00001018 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +00001019 typedef ServiceOptions OptionsType;
1020
temporal40ee5512008-07-10 02:12:20 +00001021 // See Descriptor::DebugString().
Feng Xiao6ef984a2014-11-10 17:34:54 -08001022 void DebugString(string *contents, const DebugStringOptions& options) const;
temporal40ee5512008-07-10 02:12:20 +00001023
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001024 // Walks up the descriptor tree to generate the source location path
1025 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -08001026 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001027
temporal40ee5512008-07-10 02:12:20 +00001028 const string* name_;
1029 const string* full_name_;
1030 const FileDescriptor* file_;
1031 const ServiceOptions* options_;
1032 int method_count_;
1033 MethodDescriptor* methods_;
kenton@google.comd37d46d2009-04-25 02:53:47 +00001034 // IMPORTANT: If you add a new field, make sure to search for all instances
1035 // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
1036 // descriptor.cc and update them to initialize the field.
temporal40ee5512008-07-10 02:12:20 +00001037
1038 // Must be constructed using DescriptorPool.
1039 ServiceDescriptor() {}
1040 friend class DescriptorBuilder;
1041 friend class FileDescriptor;
1042 friend class MethodDescriptor;
1043 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
1044};
1045
1046// Describes an individual service method. To obtain a MethodDescriptor given
1047// a service, first get its ServiceDescriptor, then call
1048// ServiceDescriptor::FindMethodByName(). Use DescriptorPool to construct your
1049// own descriptors.
1050class LIBPROTOBUF_EXPORT MethodDescriptor {
1051 public:
1052 // Name of this method, not including containing scope.
1053 const string& name() const;
1054 // The fully-qualified name of the method, scope delimited by periods.
1055 const string& full_name() const;
1056 // Index within the service's Descriptor.
1057 int index() const;
1058
1059 // Gets the service to which this method belongs. Never NULL.
1060 const ServiceDescriptor* service() const;
1061
1062 // Gets the type of protocol message which this method accepts as input.
1063 const Descriptor* input_type() const;
1064 // Gets the type of protocol message which this message produces as output.
1065 const Descriptor* output_type() const;
1066
Feng Xiao99aa0f92014-11-20 16:18:53 -08001067 // Gets whether the client streams multiple requests.
1068 bool client_streaming() const;
1069 // Gets whether the server streams multiple responses.
1070 bool server_streaming() const;
1071
kenton@google.com24bf56f2008-09-24 20:31:01 +00001072 // Get options for this method. These are specified in the .proto file by
1073 // placing lines like "option foo = 1234;" in curly-braces after a method
1074 // declaration. Allowed options are defined by MethodOptions in
1075 // google/protobuf/descriptor.proto, and any available extensions of that
1076 // message.
temporal40ee5512008-07-10 02:12:20 +00001077 const MethodOptions& options() const;
1078
1079 // See Descriptor::CopyTo().
1080 void CopyTo(MethodDescriptorProto* proto) const;
1081
1082 // See Descriptor::DebugString().
1083 string DebugString() const;
1084
Feng Xiao6ef984a2014-11-10 17:34:54 -08001085 // See Descriptor::DebugStringWithOptions().
1086 string DebugStringWithOptions(const DebugStringOptions& options) const;
1087
1088
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001089 // Source Location ---------------------------------------------------
1090
1091 // Updates |*out_location| to the source location of the complete
1092 // extent of this method declaration. Returns false and leaves
1093 // |*out_location| unchanged iff location information was not available.
1094 bool GetSourceLocation(SourceLocation* out_location) const;
1095
temporal40ee5512008-07-10 02:12:20 +00001096 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +00001097 typedef MethodOptions OptionsType;
1098
temporal40ee5512008-07-10 02:12:20 +00001099 // See Descriptor::DebugString().
Feng Xiao6ef984a2014-11-10 17:34:54 -08001100 void DebugString(int depth, string *contents,
1101 const DebugStringOptions& options) const;
temporal40ee5512008-07-10 02:12:20 +00001102
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001103 // Walks up the descriptor tree to generate the source location path
1104 // to this descriptor from the file root.
Jisi Liu885b6122015-02-28 14:51:22 -08001105 void GetLocationPath(std::vector<int>* output) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001106
temporal40ee5512008-07-10 02:12:20 +00001107 const string* name_;
1108 const string* full_name_;
1109 const ServiceDescriptor* service_;
1110 const Descriptor* input_type_;
1111 const Descriptor* output_type_;
1112 const MethodOptions* options_;
Feng Xiao99aa0f92014-11-20 16:18:53 -08001113 bool client_streaming_;
1114 bool server_streaming_;
kenton@google.comd37d46d2009-04-25 02:53:47 +00001115 // IMPORTANT: If you add a new field, make sure to search for all instances
1116 // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
1117 // descriptor.cc and update them to initialize the field.
temporal40ee5512008-07-10 02:12:20 +00001118
1119 // Must be constructed using DescriptorPool.
1120 MethodDescriptor() {}
1121 friend class DescriptorBuilder;
1122 friend class ServiceDescriptor;
1123 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
1124};
1125
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001126
temporal40ee5512008-07-10 02:12:20 +00001127// Describes a whole .proto file. To get the FileDescriptor for a compiled-in
1128// file, get the descriptor for something defined in that file and call
1129// descriptor->file(). Use DescriptorPool to construct your own descriptors.
1130class LIBPROTOBUF_EXPORT FileDescriptor {
1131 public:
1132 // The filename, relative to the source tree.
1133 // e.g. "google/protobuf/descriptor.proto"
1134 const string& name() const;
1135
1136 // The package, e.g. "google.protobuf.compiler".
1137 const string& package() const;
1138
1139 // The DescriptorPool in which this FileDescriptor and all its contents were
1140 // allocated. Never NULL.
1141 const DescriptorPool* pool() const;
1142
1143 // The number of files imported by this one.
1144 int dependency_count() const;
1145 // Gets an imported file by index, where 0 <= index < dependency_count().
temporalf2063512008-07-23 01:19:07 +00001146 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +00001147 const FileDescriptor* dependency(int index) const;
1148
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001149 // The number of files public imported by this one.
1150 // The public dependency list is a subset of the dependency list.
1151 int public_dependency_count() const;
1152 // Gets a public imported file by index, where 0 <= index <
1153 // public_dependency_count().
1154 // These are returned in the order they were defined in the .proto file.
1155 const FileDescriptor* public_dependency(int index) const;
1156
1157 // The number of files that are imported for weak fields.
1158 // The weak dependency list is a subset of the dependency list.
1159 int weak_dependency_count() const;
1160 // Gets a weak imported file by index, where 0 <= index <
1161 // weak_dependency_count().
1162 // These are returned in the order they were defined in the .proto file.
1163 const FileDescriptor* weak_dependency(int index) const;
1164
temporal40ee5512008-07-10 02:12:20 +00001165 // Number of top-level message types defined in this file. (This does not
1166 // include nested types.)
1167 int message_type_count() const;
1168 // Gets a top-level message type, where 0 <= index < message_type_count().
temporalf2063512008-07-23 01:19:07 +00001169 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +00001170 const Descriptor* message_type(int index) const;
1171
1172 // Number of top-level enum types defined in this file. (This does not
1173 // include nested types.)
1174 int enum_type_count() const;
1175 // Gets a top-level enum type, where 0 <= index < enum_type_count().
temporalf2063512008-07-23 01:19:07 +00001176 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +00001177 const EnumDescriptor* enum_type(int index) const;
1178
1179 // Number of services defined in this file.
1180 int service_count() const;
1181 // Gets a service, where 0 <= index < service_count().
temporalf2063512008-07-23 01:19:07 +00001182 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +00001183 const ServiceDescriptor* service(int index) const;
1184
1185 // Number of extensions defined at file scope. (This does not include
1186 // extensions nested within message types.)
1187 int extension_count() const;
1188 // Gets an extension's descriptor, where 0 <= index < extension_count().
temporalf2063512008-07-23 01:19:07 +00001189 // These are returned in the order they were defined in the .proto file.
temporal40ee5512008-07-10 02:12:20 +00001190 const FieldDescriptor* extension(int index) const;
1191
kenton@google.com24bf56f2008-09-24 20:31:01 +00001192 // Get options for this file. These are specified in the .proto file by
1193 // placing lines like "option foo = 1234;" at the top level, outside of any
1194 // other definitions. Allowed options are defined by FileOptions in
1195 // google/protobuf/descriptor.proto, and any available extensions of that
1196 // message.
temporal40ee5512008-07-10 02:12:20 +00001197 const FileOptions& options() const;
1198
Feng Xiao6ef984a2014-11-10 17:34:54 -08001199 // Syntax of this file.
1200 enum Syntax {
1201 SYNTAX_UNKNOWN = 0,
1202 SYNTAX_PROTO2 = 2,
1203 SYNTAX_PROTO3 = 3,
1204 };
1205 Syntax syntax() const;
1206 static const char* SyntaxName(Syntax syntax);
1207
temporal40ee5512008-07-10 02:12:20 +00001208 // Find a top-level message type by name. Returns NULL if not found.
1209 const Descriptor* FindMessageTypeByName(const string& name) const;
1210 // Find a top-level enum type by name. Returns NULL if not found.
1211 const EnumDescriptor* FindEnumTypeByName(const string& name) const;
1212 // Find an enum value defined in any top-level enum by name. Returns NULL if
1213 // not found.
1214 const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
1215 // Find a service definition by name. Returns NULL if not found.
1216 const ServiceDescriptor* FindServiceByName(const string& name) const;
1217 // Find a top-level extension definition by name. Returns NULL if not found.
1218 const FieldDescriptor* FindExtensionByName(const string& name) const;
kenton@google.com2d6daa72009-01-22 01:27:00 +00001219 // Similar to FindExtensionByName(), but searches by lowercased-name. See
1220 // Descriptor::FindFieldByLowercaseName().
1221 const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
1222 // Similar to FindExtensionByName(), but searches by camelcased-name. See
1223 // Descriptor::FindFieldByCamelcaseName().
1224 const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
temporal40ee5512008-07-10 02:12:20 +00001225
1226 // See Descriptor::CopyTo().
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001227 // Notes:
1228 // - This method does NOT copy source code information since it is relatively
1229 // large and rarely needed. See CopySourceCodeInfoTo() below.
temporal40ee5512008-07-10 02:12:20 +00001230 void CopyTo(FileDescriptorProto* proto) const;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001231 // Write the source code information of this FileDescriptor into the given
1232 // FileDescriptorProto. See CopyTo() above.
1233 void CopySourceCodeInfoTo(FileDescriptorProto* proto) const;
Jisi Liu46e8ff62015-10-05 11:59:43 -07001234 // Fill the json_name field of FieldDescriptorProto for all fields. Can only
1235 // be called after CopyTo().
1236 void CopyJsonNameTo(FileDescriptorProto* proto) const;
temporal40ee5512008-07-10 02:12:20 +00001237
1238 // See Descriptor::DebugString().
1239 string DebugString() const;
1240
Feng Xiao6ef984a2014-11-10 17:34:54 -08001241 // See Descriptor::DebugStringWithOptions().
1242 string DebugStringWithOptions(const DebugStringOptions& options) const;
1243
jieluo@google.com4de8f552014-07-18 00:47:59 +00001244 // Returns true if this is a placeholder for an unknown file. This will
1245 // only be the case if this descriptor comes from a DescriptorPool
1246 // with AllowUnknownDependencies() set.
1247 bool is_placeholder() const;
1248
Feng Xiao6ef984a2014-11-10 17:34:54 -08001249 // Updates |*out_location| to the source location of the complete extent of
1250 // this file declaration (namely, the empty path).
1251 bool GetSourceLocation(SourceLocation* out_location) const;
1252
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001253 // Updates |*out_location| to the source location of the complete
1254 // extent of the declaration or declaration-part denoted by |path|.
1255 // Returns false and leaves |*out_location| unchanged iff location
1256 // information was not available. (See SourceCodeInfo for
1257 // description of path encoding.)
Jisi Liu885b6122015-02-28 14:51:22 -08001258 bool GetSourceLocation(const std::vector<int>& path,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001259 SourceLocation* out_location) const;
1260
Jisi Liu885b6122015-02-28 14:51:22 -08001261 private:
kenton@google.com24bf56f2008-09-24 20:31:01 +00001262 typedef FileOptions OptionsType;
1263
temporal40ee5512008-07-10 02:12:20 +00001264 const string* name_;
1265 const string* package_;
1266 const DescriptorPool* pool_;
1267 int dependency_count_;
1268 const FileDescriptor** dependencies_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001269 int public_dependency_count_;
1270 int* public_dependencies_;
1271 int weak_dependency_count_;
1272 int* weak_dependencies_;
temporal40ee5512008-07-10 02:12:20 +00001273 int message_type_count_;
1274 Descriptor* message_types_;
1275 int enum_type_count_;
1276 EnumDescriptor* enum_types_;
1277 int service_count_;
1278 ServiceDescriptor* services_;
1279 int extension_count_;
Feng Xiao6ef984a2014-11-10 17:34:54 -08001280 Syntax syntax_;
jieluo@google.com4de8f552014-07-18 00:47:59 +00001281 bool is_placeholder_;
temporal40ee5512008-07-10 02:12:20 +00001282 FieldDescriptor* extensions_;
1283 const FileOptions* options_;
1284
kenton@google.comd37d46d2009-04-25 02:53:47 +00001285 const FileDescriptorTables* tables_;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001286 const SourceCodeInfo* source_code_info_;
kenton@google.comd37d46d2009-04-25 02:53:47 +00001287 // IMPORTANT: If you add a new field, make sure to search for all instances
1288 // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
1289 // descriptor.cc and update them to initialize the field.
1290
temporal40ee5512008-07-10 02:12:20 +00001291 FileDescriptor() {}
1292 friend class DescriptorBuilder;
1293 friend class Descriptor;
1294 friend class FieldDescriptor;
jieluo@google.com4de8f552014-07-18 00:47:59 +00001295 friend class OneofDescriptor;
temporal40ee5512008-07-10 02:12:20 +00001296 friend class EnumDescriptor;
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001297 friend class EnumValueDescriptor;
1298 friend class MethodDescriptor;
temporal40ee5512008-07-10 02:12:20 +00001299 friend class ServiceDescriptor;
1300 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
1301};
1302
1303// ===================================================================
1304
1305// Used to construct descriptors.
1306//
1307// Normally you won't want to build your own descriptors. Message classes
1308// constructed by the protocol compiler will provide them for you. However,
1309// if you are implementing Message on your own, or if you are writing a
1310// program which can operate on totally arbitrary types and needs to load
1311// them from some sort of database, you might need to.
1312//
1313// Since Descriptors are composed of a whole lot of cross-linked bits of
1314// data that would be a pain to put together manually, the
1315// DescriptorPool class is provided to make the process easier. It can
1316// take a FileDescriptorProto (defined in descriptor.proto), validate it,
1317// and convert it to a set of nicely cross-linked Descriptors.
1318//
1319// DescriptorPool also helps with memory management. Descriptors are
1320// composed of many objects containing static data and pointers to each
1321// other. In all likelihood, when it comes time to delete this data,
1322// you'll want to delete it all at once. In fact, it is not uncommon to
1323// have a whole pool of descriptors all cross-linked with each other which
1324// you wish to delete all at once. This class represents such a pool, and
1325// handles the memory management for you.
1326//
1327// You can also search for descriptors within a DescriptorPool by name, and
1328// extensions by number.
1329class LIBPROTOBUF_EXPORT DescriptorPool {
1330 public:
1331 // Create a normal, empty DescriptorPool.
1332 DescriptorPool();
1333
1334 // Constructs a DescriptorPool that, when it can't find something among the
1335 // descriptors already in the pool, looks for it in the given
1336 // DescriptorDatabase.
1337 // Notes:
1338 // - If a DescriptorPool is constructed this way, its BuildFile*() methods
1339 // must not be called (they will assert-fail). The only way to populate
1340 // the pool with descriptors is to call the Find*By*() methods.
1341 // - The Find*By*() methods may block the calling thread if the
1342 // DescriptorDatabase blocks. This in turn means that parsing messages
1343 // may block if they need to look up extensions.
1344 // - The Find*By*() methods will use mutexes for thread-safety, thus making
1345 // them slower even when they don't have to fall back to the database.
1346 // In fact, even the Find*By*() methods of descriptor objects owned by
1347 // this pool will be slower, since they will have to obtain locks too.
1348 // - An ErrorCollector may optionally be given to collect validation errors
1349 // in files loaded from the database. If not given, errors will be printed
1350 // to GOOGLE_LOG(ERROR). Remember that files are built on-demand, so this
1351 // ErrorCollector may be called from any thread that calls one of the
1352 // Find*By*() methods.
jieluo@google.com4de8f552014-07-18 00:47:59 +00001353 // - The DescriptorDatabase must not be mutated during the lifetime of
1354 // the DescriptorPool. Even if the client takes care to avoid data races,
1355 // changes to the content of the DescriptorDatabase may not be reflected
1356 // in subsequent lookups in the DescriptorPool.
temporal40ee5512008-07-10 02:12:20 +00001357 class ErrorCollector;
1358 explicit DescriptorPool(DescriptorDatabase* fallback_database,
1359 ErrorCollector* error_collector = NULL);
1360
1361 ~DescriptorPool();
1362
1363 // Get a pointer to the generated pool. Generated protocol message classes
1364 // which are compiled into the binary will allocate their descriptors in
1365 // this pool. Do not add your own descriptors to this pool.
1366 static const DescriptorPool* generated_pool();
1367
Feng Xiaoeee38b02015-08-22 18:25:48 -07001368
temporal40ee5512008-07-10 02:12:20 +00001369 // Find a FileDescriptor in the pool by file name. Returns NULL if not
1370 // found.
1371 const FileDescriptor* FindFileByName(const string& name) const;
1372
1373 // Find the FileDescriptor in the pool which defines the given symbol.
1374 // If any of the Find*ByName() methods below would succeed, then this is
1375 // equivalent to calling that method and calling the result's file() method.
1376 // Otherwise this returns NULL.
1377 const FileDescriptor* FindFileContainingSymbol(
1378 const string& symbol_name) const;
1379
1380 // Looking up descriptors ------------------------------------------
1381 // These find descriptors by fully-qualified name. These will find both
1382 // top-level descriptors and nested descriptors. They return NULL if not
1383 // found.
1384
1385 const Descriptor* FindMessageTypeByName(const string& name) const;
1386 const FieldDescriptor* FindFieldByName(const string& name) const;
1387 const FieldDescriptor* FindExtensionByName(const string& name) const;
jieluo@google.com4de8f552014-07-18 00:47:59 +00001388 const OneofDescriptor* FindOneofByName(const string& name) const;
temporal40ee5512008-07-10 02:12:20 +00001389 const EnumDescriptor* FindEnumTypeByName(const string& name) const;
1390 const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
1391 const ServiceDescriptor* FindServiceByName(const string& name) const;
1392 const MethodDescriptor* FindMethodByName(const string& name) const;
1393
1394 // Finds an extension of the given type by number. The extendee must be
1395 // a member of this DescriptorPool or one of its underlays.
1396 const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
1397 int number) const;
1398
kenton@google.comd37d46d2009-04-25 02:53:47 +00001399 // Finds extensions of extendee. The extensions will be appended to
1400 // out in an undefined order. Only extensions defined directly in
1401 // this DescriptorPool or one of its underlays are guaranteed to be
1402 // found: extensions defined in the fallback database might not be found
1403 // depending on the database implementation.
1404 void FindAllExtensions(const Descriptor* extendee,
Jisi Liu885b6122015-02-28 14:51:22 -08001405 std::vector<const FieldDescriptor*>* out) const;
kenton@google.comd37d46d2009-04-25 02:53:47 +00001406
temporal40ee5512008-07-10 02:12:20 +00001407 // Building descriptors --------------------------------------------
1408
1409 // When converting a FileDescriptorProto to a FileDescriptor, various
1410 // errors might be detected in the input. The caller may handle these
1411 // programmatically by implementing an ErrorCollector.
1412 class LIBPROTOBUF_EXPORT ErrorCollector {
1413 public:
1414 inline ErrorCollector() {}
1415 virtual ~ErrorCollector();
1416
1417 // These constants specify what exact part of the construct is broken.
1418 // This is useful e.g. for mapping the error back to an exact location
1419 // in a .proto file.
1420 enum ErrorLocation {
1421 NAME, // the symbol name, or the package name for files
1422 NUMBER, // field or extension range number
1423 TYPE, // field type
1424 EXTENDEE, // field extendee
1425 DEFAULT_VALUE, // field default value
1426 INPUT_TYPE, // method input type
1427 OUTPUT_TYPE, // method output type
kenton@google.com24bf56f2008-09-24 20:31:01 +00001428 OPTION_NAME, // name in assignment
1429 OPTION_VALUE, // value in option assignment
temporal40ee5512008-07-10 02:12:20 +00001430 OTHER // some other problem
1431 };
1432
jieluo@google.com4de8f552014-07-18 00:47:59 +00001433 // Reports an error in the FileDescriptorProto. Use this function if the
Veres Lajosc7680722014-11-08 22:59:34 +00001434 // problem occurred should interrupt building the FileDescriptorProto.
temporal40ee5512008-07-10 02:12:20 +00001435 virtual void AddError(
1436 const string& filename, // File name in which the error occurred.
1437 const string& element_name, // Full name of the erroneous element.
1438 const Message* descriptor, // Descriptor of the erroneous element.
1439 ErrorLocation location, // One of the location constants, above.
1440 const string& message // Human-readable error message.
1441 ) = 0;
1442
jieluo@google.com4de8f552014-07-18 00:47:59 +00001443 // Reports a warning in the FileDescriptorProto. Use this function if the
Veres Lajosc7680722014-11-08 22:59:34 +00001444 // problem occurred should NOT interrupt building the FileDescriptorProto.
jieluo@google.com4de8f552014-07-18 00:47:59 +00001445 virtual void AddWarning(
Austin Schuh918e3ee2014-10-31 16:27:55 -07001446 const string& /*filename*/, // File name in which the error occurred.
1447 const string& /*element_name*/, // Full name of the erroneous element.
1448 const Message* /*descriptor*/, // Descriptor of the erroneous element.
1449 ErrorLocation /*location*/, // One of the location constants, above.
1450 const string& /*message*/ // Human-readable error message.
jieluo@google.com4de8f552014-07-18 00:47:59 +00001451 ) {}
1452
temporal40ee5512008-07-10 02:12:20 +00001453 private:
1454 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
1455 };
1456
1457 // Convert the FileDescriptorProto to real descriptors and place them in
1458 // this DescriptorPool. All dependencies of the file must already be in
1459 // the pool. Returns the resulting FileDescriptor, or NULL if there were
1460 // problems with the input (e.g. the message was invalid, or dependencies
1461 // were missing). Details about the errors are written to GOOGLE_LOG(ERROR).
1462 const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
1463
1464 // Same as BuildFile() except errors are sent to the given ErrorCollector.
1465 const FileDescriptor* BuildFileCollectingErrors(
1466 const FileDescriptorProto& proto,
1467 ErrorCollector* error_collector);
1468
kenton@google.comd37d46d2009-04-25 02:53:47 +00001469 // By default, it is an error if a FileDescriptorProto contains references
1470 // to types or other files that are not found in the DescriptorPool (or its
1471 // backing DescriptorDatabase, if any). If you call
1472 // AllowUnknownDependencies(), however, then unknown types and files
jieluo@google.com4de8f552014-07-18 00:47:59 +00001473 // will be replaced by placeholder descriptors (which can be identified by
1474 // the is_placeholder() method). This can allow you to
kenton@google.comd37d46d2009-04-25 02:53:47 +00001475 // perform some useful operations with a .proto file even if you do not
1476 // have access to other .proto files on which it depends. However, some
1477 // heuristics must be used to fill in the gaps in information, and these
1478 // can lead to descriptors which are inaccurate. For example, the
1479 // DescriptorPool may be forced to guess whether an unknown type is a message
1480 // or an enum, as well as what package it resides in. Furthermore,
1481 // placeholder types will not be discoverable via FindMessageTypeByName()
1482 // and similar methods, which could confuse some descriptor-based algorithms.
jieluo@google.com4de8f552014-07-18 00:47:59 +00001483 // Generally, the results of this option should be handled with extreme care.
kenton@google.comd37d46d2009-04-25 02:53:47 +00001484 void AllowUnknownDependencies() { allow_unknown_ = true; }
1485
jieluo@google.com4de8f552014-07-18 00:47:59 +00001486 // By default, weak imports are allowed to be missing, in which case we will
1487 // use a placeholder for the dependency and convert the field to be an Empty
1488 // message field. If you call EnforceWeakDependencies(true), however, the
1489 // DescriptorPool will report a import not found error.
1490 void EnforceWeakDependencies(bool enforce) { enforce_weak_ = enforce; }
1491
temporal40ee5512008-07-10 02:12:20 +00001492 // Internal stuff --------------------------------------------------
1493 // These methods MUST NOT be called from outside the proto2 library.
1494 // These methods may contain hidden pitfalls and may be removed in a
1495 // future library version.
1496
temporal40ee5512008-07-10 02:12:20 +00001497 // Create a DescriptorPool which is overlaid on top of some other pool.
1498 // If you search for a descriptor in the overlay and it is not found, the
1499 // underlay will be searched as a backup. If the underlay has its own
1500 // underlay, that will be searched next, and so on. This also means that
1501 // files built in the overlay will be cross-linked with the underlay's
1502 // descriptors if necessary. The underlay remains property of the caller;
1503 // it must remain valid for the lifetime of the newly-constructed pool.
1504 //
1505 // Example: Say you want to parse a .proto file at runtime in order to use
1506 // its type with a DynamicMessage. Say this .proto file has dependencies,
1507 // but you know that all the dependencies will be things that are already
1508 // compiled into the binary. For ease of use, you'd like to load the types
1509 // right out of generated_pool() rather than have to parse redundant copies
1510 // of all these .protos and runtime. But, you don't want to add the parsed
1511 // types directly into generated_pool(): this is not allowed, and would be
1512 // bad design anyway. So, instead, you could use generated_pool() as an
1513 // underlay for a new DescriptorPool in which you add only the new file.
kenton@google.comfccb1462009-12-18 02:11:36 +00001514 //
1515 // WARNING: Use of underlays can lead to many subtle gotchas. Instead,
1516 // try to formulate what you want to do in terms of DescriptorDatabases.
temporal40ee5512008-07-10 02:12:20 +00001517 explicit DescriptorPool(const DescriptorPool* underlay);
1518
kenton@google.comd37d46d2009-04-25 02:53:47 +00001519 // Called by generated classes at init time to add their descriptors to
1520 // generated_pool. Do NOT call this in your own code! filename must be a
1521 // permanent string (e.g. a string literal).
1522 static void InternalAddGeneratedFile(
1523 const void* encoded_file_descriptor, int size);
1524
temporal40ee5512008-07-10 02:12:20 +00001525
1526 // For internal use only: Gets a non-const pointer to the generated pool.
1527 // This is called at static-initialization time only, so thread-safety is
1528 // not a concern. If both an underlay and a fallback database are present,
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001529 // the underlay takes precedence.
temporal40ee5512008-07-10 02:12:20 +00001530 static DescriptorPool* internal_generated_pool();
1531
1532 // For internal use only: Changes the behavior of BuildFile() such that it
1533 // allows the file to make reference to message types declared in other files
1534 // which it did not officially declare as dependencies.
1535 void InternalDontEnforceDependencies();
1536
1537 // For internal use only.
1538 void internal_set_underlay(const DescriptorPool* underlay) {
1539 underlay_ = underlay;
1540 }
1541
kenton@google.comd37d46d2009-04-25 02:53:47 +00001542 // For internal (unit test) use only: Returns true if a FileDescriptor has
1543 // been constructed for the given file, false otherwise. Useful for testing
1544 // lazy descriptor initialization behavior.
1545 bool InternalIsFileLoaded(const string& filename) const;
1546
jieluo@google.com4de8f552014-07-18 00:47:59 +00001547
1548 // Add a file to unused_import_track_files_. DescriptorBuilder will log
1549 // warnings for those files if there is any unused import.
1550 void AddUnusedImportTrackFile(const string& file_name);
1551 void ClearUnusedImportTrackFiles();
1552
temporal40ee5512008-07-10 02:12:20 +00001553 private:
1554 friend class Descriptor;
1555 friend class FieldDescriptor;
1556 friend class EnumDescriptor;
1557 friend class ServiceDescriptor;
1558 friend class FileDescriptor;
1559 friend class DescriptorBuilder;
Feng Xiao6ef984a2014-11-10 17:34:54 -08001560 friend class FileDescriptorTables;
temporal40ee5512008-07-10 02:12:20 +00001561
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001562 // Return true if the given name is a sub-symbol of any non-package
1563 // descriptor that already exists in the descriptor pool. (The full
1564 // definition of such types is already known.)
1565 bool IsSubSymbolOfBuiltType(const string& name) const;
1566
temporal40ee5512008-07-10 02:12:20 +00001567 // Tries to find something in the fallback database and link in the
1568 // corresponding proto file. Returns true if successful, in which case
1569 // the caller should search for the thing again. These are declared
1570 // const because they are called by (semantically) const methods.
1571 bool TryFindFileInFallbackDatabase(const string& name) const;
1572 bool TryFindSymbolInFallbackDatabase(const string& name) const;
1573 bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
1574 int field_number) const;
1575
1576 // Like BuildFile() but called internally when the file has been loaded from
1577 // fallback_database_. Declared const because it is called by (semantically)
1578 // const methods.
1579 const FileDescriptor* BuildFileFromDatabase(
1580 const FileDescriptorProto& proto) const;
1581
1582 // If fallback_database_ is NULL, this is NULL. Otherwise, this is a mutex
1583 // which must be locked while accessing tables_.
1584 Mutex* mutex_;
1585
1586 // See constructor.
1587 DescriptorDatabase* fallback_database_;
1588 ErrorCollector* default_error_collector_;
1589 const DescriptorPool* underlay_;
1590
1591 // This class contains a lot of hash maps with complicated types that
1592 // we'd like to keep out of the header.
1593 class Tables;
Jisi Liu46e8ff62015-10-05 11:59:43 -07001594 google::protobuf::scoped_ptr<Tables> tables_;
temporal40ee5512008-07-10 02:12:20 +00001595
1596 bool enforce_dependencies_;
kenton@google.comd37d46d2009-04-25 02:53:47 +00001597 bool allow_unknown_;
jieluo@google.com4de8f552014-07-18 00:47:59 +00001598 bool enforce_weak_;
1599 std::set<string> unused_import_track_files_;
temporal40ee5512008-07-10 02:12:20 +00001600
1601 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
1602};
1603
1604// inline methods ====================================================
1605
1606// These macros makes this repetitive code more readable.
1607#define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
1608 inline TYPE CLASS::FIELD() const { return FIELD##_; }
1609
1610// Strings fields are stored as pointers but returned as const references.
1611#define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
1612 inline const string& CLASS::FIELD() const { return *FIELD##_; }
1613
1614// Arrays take an index parameter, obviously.
1615#define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
1616 inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
1617
1618#define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
1619 inline const TYPE& CLASS::options() const { return *options_; }
1620
1621PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
1622PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
1623PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
1624PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
1625
1626PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001627PROTOBUF_DEFINE_ACCESSOR(Descriptor, oneof_decl_count, int)
temporal40ee5512008-07-10 02:12:20 +00001628PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
1629PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
1630
1631PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001632PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, oneof_decl, const OneofDescriptor*)
temporal40ee5512008-07-10 02:12:20 +00001633PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
1634PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
1635
1636PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
1637PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
1638PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
1639 const Descriptor::ExtensionRange*)
1640PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
1641 const FieldDescriptor*)
Bo Yang5db21732015-05-21 14:28:59 -07001642
1643PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_range_count, int)
1644PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, reserved_range,
1645 const Descriptor::ReservedRange*)
1646PROTOBUF_DEFINE_ACCESSOR(Descriptor, reserved_name_count, int)
1647
Swen Kooij37d6cf92016-01-28 11:40:07 +02001648PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001649PROTOBUF_DEFINE_ACCESSOR(Descriptor, is_placeholder, bool)
temporal40ee5512008-07-10 02:12:20 +00001650
1651PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
1652PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
Jisi Liu46e8ff62015-10-05 11:59:43 -07001653PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, json_name)
kenton@google.com2d6daa72009-01-22 01:27:00 +00001654PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
1655PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
temporal40ee5512008-07-10 02:12:20 +00001656PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
1657PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
1658PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
1659PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
1660PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
1661PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001662PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_oneof,
1663 const OneofDescriptor*)
1664PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, index_in_oneof, int)
temporal40ee5512008-07-10 02:12:20 +00001665PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
1666PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
1667PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
liujisi@google.com6fb956d2012-12-05 06:38:29 +00001668PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions)
temporal40ee5512008-07-10 02:12:20 +00001669PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
1670PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
1671PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
1672PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
1673PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
1674PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
1675PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
1676PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool , bool )
1677PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
1678 const EnumValueDescriptor*)
1679PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
1680
jieluo@google.com4de8f552014-07-18 00:47:59 +00001681PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, name)
1682PROTOBUF_DEFINE_STRING_ACCESSOR(OneofDescriptor, full_name)
1683PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, containing_type, const Descriptor*)
1684PROTOBUF_DEFINE_ACCESSOR(OneofDescriptor, field_count, int)
1685
temporal40ee5512008-07-10 02:12:20 +00001686PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
1687PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
1688PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
1689PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
1690PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
1691PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
1692 const EnumValueDescriptor*)
Swen Kooij37d6cf92016-01-28 11:40:07 +02001693PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001694PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, is_placeholder, bool)
temporal40ee5512008-07-10 02:12:20 +00001695
1696PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
1697PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
1698PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
1699PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
liujisi@google.com6fb956d2012-12-05 06:38:29 +00001700PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions)
temporal40ee5512008-07-10 02:12:20 +00001701
1702PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
1703PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
1704PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
1705PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
1706PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
1707 const MethodDescriptor*)
Swen Kooij37d6cf92016-01-28 11:40:07 +02001708PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions)
temporal40ee5512008-07-10 02:12:20 +00001709
1710PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
1711PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
1712PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
1713PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
1714PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
Swen Kooij37d6cf92016-01-28 11:40:07 +02001715PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions)
Feng Xiao99aa0f92014-11-20 16:18:53 -08001716PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, client_streaming, bool)
1717PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, server_streaming, bool)
1718
temporal40ee5512008-07-10 02:12:20 +00001719PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
1720PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
1721PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
1722PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001723PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int)
1724PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int)
temporal40ee5512008-07-10 02:12:20 +00001725PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
1726PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
1727PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
1728PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
Swen Kooij37d6cf92016-01-28 11:40:07 +02001729PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions)
jieluo@google.com4de8f552014-07-18 00:47:59 +00001730PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, is_placeholder, bool)
temporal40ee5512008-07-10 02:12:20 +00001731
1732PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
1733PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
1734PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
1735 const ServiceDescriptor*)
1736PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
1737 const FieldDescriptor*)
1738
1739#undef PROTOBUF_DEFINE_ACCESSOR
1740#undef PROTOBUF_DEFINE_STRING_ACCESSOR
1741#undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
1742
1743// A few accessors differ from the macros...
1744
jieluo@google.com4de8f552014-07-18 00:47:59 +00001745inline bool Descriptor::IsExtensionNumber(int number) const {
1746 return FindExtensionRangeContainingNumber(number) != NULL;
1747}
1748
Bo Yang5db21732015-05-21 14:28:59 -07001749inline bool Descriptor::IsReservedNumber(int number) const {
1750 return FindReservedRangeContainingNumber(number) != NULL;
1751}
1752
1753inline bool Descriptor::IsReservedName(const string& name) const {
1754 for (int i = 0; i < reserved_name_count(); i++) {
1755 if (name == reserved_name(i)) {
1756 return true;
1757 }
1758 }
1759 return false;
1760}
1761
1762// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because reserved_names_ is actually
1763// an array of pointers rather than the usual array of objects.
1764inline const string& Descriptor::reserved_name(int index) const {
1765 return *reserved_names_[index];
1766}
1767
temporal40ee5512008-07-10 02:12:20 +00001768inline bool FieldDescriptor::is_required() const {
1769 return label() == LABEL_REQUIRED;
1770}
1771
1772inline bool FieldDescriptor::is_optional() const {
1773 return label() == LABEL_OPTIONAL;
1774}
1775
1776inline bool FieldDescriptor::is_repeated() const {
1777 return label() == LABEL_REPEATED;
1778}
1779
kenton@google.comfccb1462009-12-18 02:11:36 +00001780inline bool FieldDescriptor::is_packable() const {
1781 return is_repeated() && IsTypePackable(type());
1782}
1783
temporal40ee5512008-07-10 02:12:20 +00001784// To save space, index() is computed by looking at the descriptor's position
1785// in the parent's array of children.
1786inline int FieldDescriptor::index() const {
1787 if (!is_extension_) {
Feng Xiao84731a12014-10-03 11:13:58 -07001788 return static_cast<int>(this - containing_type_->fields_);
temporal40ee5512008-07-10 02:12:20 +00001789 } else if (extension_scope_ != NULL) {
Feng Xiao84731a12014-10-03 11:13:58 -07001790 return static_cast<int>(this - extension_scope_->extensions_);
temporal40ee5512008-07-10 02:12:20 +00001791 } else {
Feng Xiao84731a12014-10-03 11:13:58 -07001792 return static_cast<int>(this - file_->extensions_);
temporal40ee5512008-07-10 02:12:20 +00001793 }
1794}
1795
1796inline int Descriptor::index() const {
1797 if (containing_type_ == NULL) {
Feng Xiao84731a12014-10-03 11:13:58 -07001798 return static_cast<int>(this - file_->message_types_);
temporal40ee5512008-07-10 02:12:20 +00001799 } else {
Feng Xiao84731a12014-10-03 11:13:58 -07001800 return static_cast<int>(this - containing_type_->nested_types_);
temporal40ee5512008-07-10 02:12:20 +00001801 }
1802}
1803
jieluo@google.com4de8f552014-07-18 00:47:59 +00001804inline int OneofDescriptor::index() const {
Feng Xiao84731a12014-10-03 11:13:58 -07001805 return static_cast<int>(this - containing_type_->oneof_decls_);
jieluo@google.com4de8f552014-07-18 00:47:59 +00001806}
1807
temporal40ee5512008-07-10 02:12:20 +00001808inline int EnumDescriptor::index() const {
1809 if (containing_type_ == NULL) {
Feng Xiao84731a12014-10-03 11:13:58 -07001810 return static_cast<int>(this - file_->enum_types_);
temporal40ee5512008-07-10 02:12:20 +00001811 } else {
Feng Xiao84731a12014-10-03 11:13:58 -07001812 return static_cast<int>(this - containing_type_->enum_types_);
temporal40ee5512008-07-10 02:12:20 +00001813 }
1814}
1815
1816inline int EnumValueDescriptor::index() const {
Feng Xiao84731a12014-10-03 11:13:58 -07001817 return static_cast<int>(this - type_->values_);
temporal40ee5512008-07-10 02:12:20 +00001818}
1819
1820inline int ServiceDescriptor::index() const {
Feng Xiao84731a12014-10-03 11:13:58 -07001821 return static_cast<int>(this - file_->services_);
temporal40ee5512008-07-10 02:12:20 +00001822}
1823
1824inline int MethodDescriptor::index() const {
Feng Xiao84731a12014-10-03 11:13:58 -07001825 return static_cast<int>(this - service_->methods_);
temporal40ee5512008-07-10 02:12:20 +00001826}
1827
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001828inline const char* FieldDescriptor::type_name() const {
1829 return kTypeToName[type_];
1830}
1831
temporal40ee5512008-07-10 02:12:20 +00001832inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
1833 return kTypeToCppTypeMap[type_];
1834}
1835
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001836inline const char* FieldDescriptor::cpp_type_name() const {
1837 return kCppTypeToName[kTypeToCppTypeMap[type_]];
1838}
1839
kenton@google.comd37d46d2009-04-25 02:53:47 +00001840inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
1841 return kTypeToCppTypeMap[type];
1842}
1843
jieluo@google.com4de8f552014-07-18 00:47:59 +00001844inline const char* FieldDescriptor::TypeName(Type type) {
1845 return kTypeToName[type];
1846}
1847
1848inline const char* FieldDescriptor::CppTypeName(CppType cpp_type) {
1849 return kCppTypeToName[cpp_type];
1850}
1851
kenton@google.comfccb1462009-12-18 02:11:36 +00001852inline bool FieldDescriptor::IsTypePackable(Type field_type) {
1853 return (field_type != FieldDescriptor::TYPE_STRING &&
1854 field_type != FieldDescriptor::TYPE_GROUP &&
1855 field_type != FieldDescriptor::TYPE_MESSAGE &&
1856 field_type != FieldDescriptor::TYPE_BYTES);
1857}
1858
temporal40ee5512008-07-10 02:12:20 +00001859inline const FileDescriptor* FileDescriptor::dependency(int index) const {
1860 return dependencies_[index];
1861}
1862
xiaofeng@google.comb55a20f2012-09-22 02:40:50 +00001863inline const FileDescriptor* FileDescriptor::public_dependency(
1864 int index) const {
1865 return dependencies_[public_dependencies_[index]];
1866}
1867
1868inline const FileDescriptor* FileDescriptor::weak_dependency(
1869 int index) const {
1870 return dependencies_[weak_dependencies_[index]];
1871}
1872
Feng Xiao6ef984a2014-11-10 17:34:54 -08001873inline FileDescriptor::Syntax FileDescriptor::syntax() const {
1874 return syntax_;
1875}
1876
jieluo@google.com4de8f552014-07-18 00:47:59 +00001877// Can't use PROTOBUF_DEFINE_ARRAY_ACCESSOR because fields_ is actually an array
1878// of pointers rather than the usual array of objects.
1879inline const FieldDescriptor* OneofDescriptor::field(int index) const {
1880 return fields_[index];
1881}
1882
temporal40ee5512008-07-10 02:12:20 +00001883} // namespace protobuf
1884
1885} // namespace google
1886#endif // GOOGLE_PROTOBUF_DESCRIPTOR_H__