descriptor.h revision d0332953cda33fb4f8e24ebff9c49159b69c43d6
1f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Protocol Buffers - Google's data interchange format
2f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Copyright 2008 Google Inc.  All rights reserved.
3f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// http://code.google.com/p/protobuf/
4f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
5f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Redistribution and use in source and binary forms, with or without
6f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// modification, are permitted provided that the following conditions are
7f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// met:
8f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
9f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//     * Redistributions of source code must retain the above copyright
10f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// notice, this list of conditions and the following disclaimer.
11f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//     * Redistributions in binary form must reproduce the above
12f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// copyright notice, this list of conditions and the following disclaimer
13f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// in the documentation and/or other materials provided with the
14d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// distribution.
15d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks//     * Neither the name of Google Inc. nor the names of its
16e6348c336fecc8da9288ea367375a1b1cd2358d2Argyrios Kyrtzidis// contributors may be used to endorse or promote products derived from
17f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// this software without specific prior written permission.
18f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek//
19802be99a6817aba6edb166b93c133da4358aa783Zhongxing Xu// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
2527af04bcca46f8a3374586be1301477f9123f5e1Argyrios Kyrtzidis// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2643dee220252ef0b42c5f8a3bb1eca97f84f2565fArgyrios Kyrtzidis// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2721142581d55918beed544a757e4af3bb865b1812Ted Kremenek// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
289b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
299b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
309b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek
319b663716449b618ba0390b1dbebc54fa8e971124Ted Kremenek// Author: kenton@google.com (Kenton Varda)
32f39d962cf84f46d2c0512157259ae1d41a1a5173David Blaikie//  Based on original Protocol Buffers design by
33a7af5ea88a6c5bdf87497cca6c20831e8c546751Argyrios Kyrtzidis//  Sanjay Ghemawat, Jeff Dean, and others.
34efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar//
35efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar// This file contains classes which describe a type of protocol message.
369b414d3e2d0cb84512b55a3275a98490b090162aDaniel Dunbar// You can use a message's descriptor to learn at runtime what fields
37efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar// it contains and what the types of those fields are.  The Message
38f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek// interface also allows you to dynamically access and modify individual
3903013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer// fields by passing the FieldDescriptor of the field you are interested
4003013fa9a0bf1ef4b907f5fec006c8f4000fdd21Michael J. Spencer// in.
41d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks//
42d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// Most users will not care about descriptors, because they will write
436cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer// code specific to certain protocol types and will simply use the classes
4481fb169f42769e02c7425b23885a261c025fd5e6Anna Zaks// generated by the protocol compiler directly.  Advanced users who want
45db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek// to operate on arbitrary types (not known at compile time) may want to
467fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks// read descriptors in order to learn about the contents of a message.
477fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks// A very small number of users will want to construct their own
48f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Descriptors, either because they are implementing Message manually or
499ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenek// because they are writing something like the protocol compiler.
50d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks//
51f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// For an example of how you might use descriptors, see the code example
52c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu// at the top of message.h.
53ff944a8c481d6c0f1ad2633e4be9bf8b1dd2a09fZhongxing Xu
543fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks#ifndef GOOGLE_PROTOBUF_DESCRIPTOR_H__
55d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#define GOOGLE_PROTOBUF_DESCRIPTOR_H__
56e62f048960645b79363408fdead53fec2a063c52Anna Zaks
57e62f048960645b79363408fdead53fec2a063c52Anna Zaks#include <string>
58e62f048960645b79363408fdead53fec2a063c52Anna Zaks#include <vector>
59d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks#include <google/protobuf/stubs/common.h>
60be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6cTed Kremenek
61f39d962cf84f46d2c0512157259ae1d41a1a5173David Blaikie
62f75560670bcdd59b051149bdece3eac14e313853Ted Kremeneknamespace google {
63f75560670bcdd59b051149bdece3eac14e313853Ted Kremeneknamespace protobuf {
64ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie
65ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie// Defined in this file.
66efceabd2380f49306bc0229583458e93b062094bDaniel Dunbarclass Descriptor;
67ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikieclass FieldDescriptor;
68ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikieclass EnumDescriptor;
69ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikieclass EnumValueDescriptor;
70f75560670bcdd59b051149bdece3eac14e313853Ted Kremenekclass ServiceDescriptor;
71f75560670bcdd59b051149bdece3eac14e313853Ted Kremenekclass MethodDescriptor;
72f75560670bcdd59b051149bdece3eac14e313853Ted Kremenekclass FileDescriptor;
73f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenekclass DescriptorDatabase;
74f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenekclass DescriptorPool;
75f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
76f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Defined in descriptor.proto
77f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenekclass DescriptorProto;
78aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass FieldDescriptorProto;
79aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass EnumDescriptorProto;
80aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass EnumValueDescriptorProto;
81aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass ServiceDescriptorProto;
82aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass MethodDescriptorProto;
83aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass FileDescriptorProto;
84aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass MessageOptions;
85aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass FieldOptions;
86aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass EnumOptions;
87aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass EnumValueOptions;
88aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass ServiceOptions;
89aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass MethodOptions;
90aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaksclass FileOptions;
91ed8afacb8118b71bcfa8017059e51da325e7691bZhongxing Xuclass UninterpretedOption;
929c378f705405d37f49795d5e915989de774fe11fTed Kremenek
931d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek// Defined in message.h
941d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenekclass Message;
951d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
9608b86531ade68727c56918f162816075b87c864aJordy Rose// Defined in descriptor.cc
97d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xuclass DescriptorBuilder;
981d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenekclass FileDescriptorTables;
99ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie
100d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu// Defined in unknown_field_set.h.
1011d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenekclass UnknownField;
1021d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
103f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek// Describes a type of protocol message, or a particular group within a
1046f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith// message.  To obtain the Descriptor for a given message object, call
1056f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith// Message::GetDescriptor().  Generated message classes also have a
106c471e7b44e63ff1b46b480e723c4130aeaef5a8aZhongxing Xu// static method called descriptor() which returns the type's descriptor.
107d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks// Use DescriptorPool to construct your own descriptors.
108d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaksclass LIBPROTOBUF_EXPORT Descriptor {
109d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks public:
1103bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  // The name of the message type, not including its scope.
1113bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  const string& name() const;
1123bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks
1133bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  // The fully-qualified name of the message type, scope delimited by
1141d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // periods.  For example, message type "Foo" which is declared in package
1151d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // "bar" has full name "bar.Foo".  If a type "Baz" is nested within
11608b86531ade68727c56918f162816075b87c864aJordy Rose  // Foo, Baz's full_name is "bar.Foo.Baz".  To get only the part that
11708b86531ade68727c56918f162816075b87c864aJordy Rose  // comes after the last '.', use name().
118aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const string& full_name() const;
119aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
1201d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // Index of this descriptor within the file or containing type's message
121d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  // type array.
122d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  int index() const;
123d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
124d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  // The .proto file in which this message type was defined.  Never NULL.
125d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  const FileDescriptor* file() const;
126d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks
127d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  // If this Descriptor describes a nested type, this returns the type
128e62f048960645b79363408fdead53fec2a063c52Anna Zaks  // in which it is nested.  Otherwise, returns NULL.
129e62f048960645b79363408fdead53fec2a063c52Anna Zaks  const Descriptor* containing_type() const;
130e62f048960645b79363408fdead53fec2a063c52Anna Zaks
131e62f048960645b79363408fdead53fec2a063c52Anna Zaks  // Get options for this message type.  These are specified in the .proto file
132e62f048960645b79363408fdead53fec2a063c52Anna Zaks  // by placing lines like "option foo = 1234;" in the message definition.
133e62f048960645b79363408fdead53fec2a063c52Anna Zaks  // Allowed options are defined by MessageOptions in
134e62f048960645b79363408fdead53fec2a063c52Anna Zaks  // google/protobuf/descriptor.proto, and any available extensions of that
135d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  // message.
136d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  const MessageOptions& options() const;
1371d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
138fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // Write the contents of this Descriptor into the given DescriptorProto.
1391d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // The target DescriptorProto must be clear before calling this; if it
140ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // isn't, the result may be garbage.
1411d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  void CopyTo(DescriptorProto* proto) const;
1421d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
1431d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // Write the contents of this decriptor in a human-readable form. Output
144fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // will be suitable for re-parsing.
1451d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  string DebugString() const;
146fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
147fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // Field stuff -----------------------------------------------------
148a599ae8826b01c0160a519b0fc5a4871f599bf04Argyrios Kyrtzidis
149a599ae8826b01c0160a519b0fc5a4871f599bf04Argyrios Kyrtzidis  // The number of fields in this message type.
150a599ae8826b01c0160a519b0fc5a4871f599bf04Argyrios Kyrtzidis  int field_count() const;
151ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // Gets a field by index, where 0 <= index < field_count().
1521d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // These are returned in the order they were defined in the .proto file.
153fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  const FieldDescriptor* field(int index) const;
1541d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
1555f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  // Looks up a field by declared tag number.  Returns NULL if no such field
1565f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  // exists.
157b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie  const FieldDescriptor* FindFieldByNumber(int number) const;
158fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // Looks up a field by name.  Returns NULL if no such field exists.
1595f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  const FieldDescriptor* FindFieldByName(const string& name) const;
160fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
1611d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // Looks up a field by lowercased name (as returned by lowercase_name()).
1621eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // This lookup may be ambiguous if multiple field names differ only by case,
1635f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  // in which case the field returned is chosen arbitrarily from the matches.
1645f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  const FieldDescriptor* FindFieldByLowercaseName(
165b219cfc4d75f0a03630b7c4509ef791b7e97b2c8David Blaikie      const string& lowercase_name) const;
166fda7832b000ff8927386f093b52c067641679469Zhongxing Xu
1675f83d6f36a7308eef21d87104fd70c421e854448Argyrios Kyrtzidis  // Looks up a field by camel-case name (as returned by camelcase_name()).
168fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // This lookup may be ambiguous if multiple field names differ in a way that
169fda7832b000ff8927386f093b52c067641679469Zhongxing Xu  // leads them to have identical camel-case names, in which case the field
1701d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // returned is chosen arbitrarily from the matches.
171f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  const FieldDescriptor* FindFieldByCamelcaseName(
172aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks      const string& camelcase_name) const;
173c4a1437c15da43eb8d2601cdce13161ef41a4389Ted Kremenek
1741d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // Nested type stuff -----------------------------------------------
175f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
176fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  // The number of nested types in this message type.
177fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  int nested_type_count() const;
178cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // Gets a nested type by index, where 0 <= index < nested_type_count().
179aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // These are returned in the order they were defined in the .proto file.
180aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const Descriptor* nested_type(int index) const;
181aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
182aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Looks up a nested type by name.  Returns NULL if no such nested type
183aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // exists.
184aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const Descriptor* FindNestedTypeByName(const string& name) const;
185aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
186cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // Enum stuff ------------------------------------------------------
187cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor
188b8989f27f116ff2400e92a52c067a69846119eb5Benjamin Kramer  // The number of enum types in this message type.
189cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  int enum_type_count() const;
190cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // Gets an enum type by index, where 0 <= index < enum_type_count().
191cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // These are returned in the order they were defined in the .proto file.
192cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  const EnumDescriptor* enum_type(int index) const;
193cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor
194cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // Looks up an enum type by name.  Returns NULL if no such enum type exists.
195cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
196cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor
197cb7b1e17b63967317ab5cc55682168cf0380519aDouglas Gregor  // Looks up an enum value by name, among all enum types in this message.
19835fa76d0bb6fb8c86159a7506efd094a4fe376d2Ted Kremenek  // Returns NULL if no such value exists.
1991d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
2001eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
2011d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // Extensions ------------------------------------------------------
2021d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek
2034e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  // A range of field numbers which are designated for third-party
20408b86531ade68727c56918f162816075b87c864aJordy Rose  // extensions.
2051d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  struct ExtensionRange {
2064e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie    int start;  // inclusive
2071d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek    int end;    // exclusive
20843dee220252ef0b42c5f8a3bb1eca97f84f2565fArgyrios Kyrtzidis  };
209c6238d2786cfd961b94580b3d3675a1b3ff0721cZhongxing Xu
2106362b893731ccf4480a96527db9e55e04b801503Zhongxing Xu  // The number of extension ranges in this message type.
2111d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  int extension_range_count() const;
212d30952838421ddfb9f7e346b2ba8213889a5f789Anna Zaks  // Gets an extension range by index, where 0 <= index <
21366253352131e3e7a22b3bfd0e180607aa2bfb988Anna Zaks  // extension_range_count(). These are returned in the order they were defined
2149121ba232903ebe61e7bbe14ca294cf0f07dfa96Marcin Swiderski  // in the .proto file.
215d767d81290288c030f3be0be1d3e62b9c8df51dcTed Kremenek  const ExtensionRange* extension_range(int index) const;
2168235f9c9c8b3d1737d1c6bd57f7ba3f616b92392Anna Zaks
21766253352131e3e7a22b3bfd0e180607aa2bfb988Anna Zaks  // Returns true if the number is in one of the extension ranges.
2188235f9c9c8b3d1737d1c6bd57f7ba3f616b92392Anna Zaks  bool IsExtensionNumber(int number) const;
21966253352131e3e7a22b3bfd0e180607aa2bfb988Anna Zaks
2205903a373db3d27794c90b25687e0dd6adb0e497dAnna Zaks  // The number of extensions -- extending *other* messages -- that were
221b47dbcbc12430fdf3e5a5b9f59cdec5480e89e75Anna Zaks  // defined nested within this message type's scope.
2221d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  int extension_count() const;
2231eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get an extension by index, where 0 <= index < extension_count().
2241d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  // These are returned in the order they were defined in the .proto file.
22514cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenek  const FieldDescriptor* extension(int index) const;
226a2e589e60d147f4f04cee5682b8389b55c410244Anna Zaks
227aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Looks up a named extension (which extends some *other* message type)
228a2e589e60d147f4f04cee5682b8389b55c410244Anna Zaks  // defined within this message type's scope.
229aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const FieldDescriptor* FindExtensionByName(const string& name) const;
230aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
231aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Similar to FindFieldByLowercaseName(), but finds extensions defined within
232aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // this message type's scope.
233aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
234aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
235aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Similar to FindFieldByCamelcaseName(), but finds extensions defined within
236aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // this message type's scope.
237aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
238aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
23998520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks private:
240aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  typedef MessageOptions OptionsType;
2413fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks
2423fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // Internal version of DebugString; controls the level of indenting for
243aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // correct depth
244aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  void DebugString(int depth, string *contents) const;
245aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
246aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const string* name_;
247aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const string* full_name_;
248aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const FileDescriptor* file_;
249aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const Descriptor* containing_type_;
250aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const MessageOptions* options_;
251aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
252aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // True if this is a placeholder for an unknown type.
253aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool is_placeholder_;
254aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // True if this is a placeholder and the type name wasn't fully-qualified.
255aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool is_unqualified_placeholder_;
256aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
257aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int field_count_;
258aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  FieldDescriptor* fields_;
259aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int nested_type_count_;
260aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  Descriptor* nested_types_;
261aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int enum_type_count_;
262aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  EnumDescriptor* enum_types_;
263aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int extension_range_count_;
264aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  ExtensionRange* extension_ranges_;
265aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int extension_count_;
266aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  FieldDescriptor* extensions_;
267aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // IMPORTANT:  If you add a new field, make sure to search for all instances
268aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // of Allocate<Descriptor>() and AllocateArray<Descriptor>() in descriptor.cc
269aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // and update them to initialize the field.
270aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
271aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Must be constructed using DescriptorPool.
2721d9cbeb76cf4c36acf5545028e2b2ac207086442Ted Kremenek  Descriptor() {}
273f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  friend class DescriptorBuilder;
274f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  friend class EnumDescriptor;
275aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  friend class FieldDescriptor;
276f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  friend class MethodDescriptor;
277f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  friend class FileDescriptor;
278f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Descriptor);
279d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks};
280f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
281a2e589e60d147f4f04cee5682b8389b55c410244Anna Zaks// Describes a single field of a message.  To get the descriptor for a given
282d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// field, first get the Descriptor for the message in which it is defined,
283d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// then call Descriptor::FindFieldByName().  To get a FieldDescriptor for
284d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// an extension, do one of the following:
285a2e589e60d147f4f04cee5682b8389b55c410244Anna Zaks// - Get the Descriptor or FileDescriptor for its containing scope, then
286d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks//   call Descriptor::FindExtensionByName() or
287d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks//   FileDescriptor::FindExtensionByName().
288d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks// - Given a DescriptorPool, call DescriptorPool::FindExtensionByNumber().
2893fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks// - Given a Reflection for a message object, call
290b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks//   Reflection::FindKnownExtensionByName() or
291b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks//   Reflection::FindKnownExtensionByNumber().
292b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks// Use DescriptorPool to construct your own descriptors.
293b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaksclass LIBPROTOBUF_EXPORT FieldDescriptor {
294b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks public:
295d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Identifies a field type.  0 is reserved for errors.  The order is weird
296d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // for historical reasons.  Types 12 and up are new in proto2.
2973fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  enum Type {
298d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    TYPE_DOUBLE         = 1,   // double, exactly eight bytes on the wire.
2993fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks    TYPE_FLOAT          = 2,   // float, exactly four bytes on the wire.
3003fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks    TYPE_INT64          = 3,   // int64, varint on the wire.  Negative numbers
301d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks                               // take 10 bytes.  Use TYPE_SINT64 if negative
302b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks                               // values are likely.
303b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    TYPE_UINT64         = 4,   // uint64, varint on the wire.
304b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    TYPE_INT32          = 5,   // int32, varint on the wire.  Negative numbers
305b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks                               // take 10 bytes.  Use TYPE_SINT32 if negative
3067fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks                               // values are likely.
307b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    TYPE_FIXED64        = 6,   // uint64, exactly eight bytes on the wire.
308b990d039c7e01ad0055dcbd1e13a691813397b96Anna Zaks    TYPE_FIXED32        = 7,   // uint32, exactly four bytes on the wire.
3097fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_BOOL           = 8,   // bool, varint on the wire.
3107fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_STRING         = 9,   // UTF-8 text.
311d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    TYPE_GROUP          = 10,  // Tag-delimited message.  Deprecated.
3127fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_MESSAGE        = 11,  // Length-delimited message.
3137fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
314d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    TYPE_BYTES          = 12,  // Arbitrary byte array.
315d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks    TYPE_UINT32         = 13,  // uint32, varint on the wire
3167fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_ENUM           = 14,  // Enum, varint on the wire
3177fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_SFIXED32       = 15,  // int32, exactly four bytes on the wire
3187fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_SFIXED64       = 16,  // int64, exactly eight bytes on the wire
3197fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_SINT32         = 17,  // int32, ZigZag-encoded varint on the wire
3207fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    TYPE_SINT64         = 18,  // int64, ZigZag-encoded varint on the wire
3217fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
3227fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    MAX_TYPE            = 18,  // Constant useful for defining lookup tables
3237fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks                               // indexed by Type.
3247fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  };
3257fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
3267fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  // Specifies the C++ data type used to represent the field.  There is a
3277fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  // fixed mapping from Type to CppType where each Type maps to exactly one
3287fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  // CppType.  0 is reserved for errors.
3297fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  enum CppType {
3307fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_INT32       = 1,     // TYPE_INT32, TYPE_SINT32, TYPE_SFIXED32
3317fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_INT64       = 2,     // TYPE_INT64, TYPE_SINT64, TYPE_SFIXED64
3327fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_UINT32      = 3,     // TYPE_UINT32, TYPE_FIXED32
3337fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_UINT64      = 4,     // TYPE_UINT64, TYPE_FIXED64
3347fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_DOUBLE      = 5,     // TYPE_DOUBLE
3357fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_FLOAT       = 6,     // TYPE_FLOAT
3367fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_BOOL        = 7,     // TYPE_BOOL
3377fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_ENUM        = 8,     // TYPE_ENUM
3387fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_STRING      = 9,     // TYPE_STRING, TYPE_BYTES
3397fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    CPPTYPE_MESSAGE     = 10,    // TYPE_MESSAGE, TYPE_GROUP
3407fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
3417fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks    MAX_CPPTYPE         = 10,    // Constant useful for defining lookup tables
3427fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks                                 // indexed by CppType.
3437fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks  };
3447fe8dcef71ae56e43fd7df345db2895f84f2d0caAnna Zaks
345d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Identifies whether the field is optional, required, or repeated.  0 is
346d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // reserved for errors.
347fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek  enum Label {
348fee618af5dd7dee2caaa7347b372eb3dc5fdeffcTed Kremenek    LABEL_OPTIONAL      = 1,    // optional
34914cc9451de4a9539bf79e4e5d63248c2377426dbTed Kremenek    LABEL_REQUIRED      = 2,    // required
350aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    LABEL_REPEATED      = 3,    // repeated
351aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
352aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks    MAX_LABEL           = 3,    // Constant useful for defining lookup tables
353aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks                                // indexed by Label.
354aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  };
355c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks
356d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  // Valid field numbers are positive integers up to kMaxNumber.
357d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  static const int kMaxNumber = (1 << 29) - 1;
358c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks
359c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  // First field number reserved for the protocol buffer library implementation.
360c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  // Users may not declare fields that use reserved numbers.
361c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  static const int kFirstReservedNumber = 19000;
362aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Last field number reserved for the protocol buffer library implementation.
363aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Users may not declare fields that use reserved numbers.
364aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  static const int kLastReservedNumber  = 19999;
365aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks
366aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const string& name() const;        // Name of this field within the message.
367aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const string& full_name() const;   // Fully-qualified name of the field.
368aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const FileDescriptor* file() const;// File in which this field was defined.
369aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  bool is_extension() const;         // Is this an extension field?
370aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  int number() const;                // Declared tag number.
371a2e589e60d147f4f04cee5682b8389b55c410244Anna Zaks
372b317f8f5ca8737a5bbad97a3f7566a2dbd2ed61bZhongxing Xu  // Same as name() except converted to lower-case.  This (and especially the
373c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  // FindFieldByLowercaseName() method) can be useful when parsing formats
374c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  // which prefer to use lowercase naming style.  (Although, technically
375aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // field names should be lowercased anyway according to the protobuf style
376aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // guide, so this only makes a difference when dealing with old .proto files
377c5bdc556f6a65c677e0ed73f918c3000ecad33afAnna Zaks  // which do not follow the guide.)
3789be6e7ce5788e50c62d40c59b0bbc2ea423683f7Ted Kremenek  const string& lowercase_name() const;
379ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie
380690a7f431d6863a101711e67636d51ddd13f35c5Ted Kremenek  // Same as name() except converted to camel-case.  In this conversion, any
381ef3643fbbbf66247c5e205497fae0f46e240c143David Blaikie  // time an underscore appears in the name, it is removed and the next
382da17fd50ad485fd2a1fc5c2f055caacf532992daZhongxing Xu  // letter is capitalized.  Furthermore, the first letter of the name is
383d07a0d0279c09d1017f8450fce575a94dc9703c0Zhongxing Xu  // lower-cased.  Examples:
384d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  //   FooBar -> fooBar
385d38f79543136ba68cd14b1dab0856474df1fbfd5Anna Zaks  //   foo_bar -> fooBar
386db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek  //   fooBar -> fooBar
387db09a4dee28a4515438af60f2d2b4a83e4965c31Ted Kremenek  // This (and especially the FindFieldByCamelcaseName() method) can be useful
3885f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  // when parsing formats which prefer to use camel-case naming style.
389fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  const string& camelcase_name() const;
390fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek
391f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  Type type() const;                 // Declared type of this field.
392fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  CppType cpp_type() const;          // C++ type of this field.
393fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  Label label() const;               // optional/required/repeated
394fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek
395fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  bool is_required() const;      // shorthand for label() == LABEL_REQUIRED
396fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  bool is_optional() const;      // shorthand for label() == LABEL_OPTIONAL
397fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  bool is_repeated() const;      // shorthand for label() == LABEL_REPEATED
398d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  bool is_packable() const;      // shorthand for is_repeated() &&
399d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks                                 //               IsTypePackable(type())
400d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
401d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Index of this field within the message's field array, or the file or
402d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // extension scope's extensions array.
403d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  int index() const;
404d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
405d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Does this field have an explicitly-declared default value?
406d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  bool has_default_value() const;
407d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks
408d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Get the field default value if cpp_type() == CPPTYPE_INT32.  If no
409d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // explicit default was defined, the default is 0.
41098520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  int32 default_value_int32() const;
411d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // Get the field default value if cpp_type() == CPPTYPE_INT64.  If no
412d95e0b830ed031f2ea0e15e3679cd51b9bf23a9cAnna Zaks  // explicit default was defined, the default is 0.
41398520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  int64 default_value_int64() const;
4141eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get the field default value if cpp_type() == CPPTYPE_UINT32.  If no
415f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // explicit default was defined, the default is 0.
416f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  uint32 default_value_uint32() const;
417fcd783d583d270b7ec1ec3e0fcf83cd93d30e381Ted Kremenek  // Get the field default value if cpp_type() == CPPTYPE_UINT64.  If no
418402785357ab053dd53f4fdd858b9630a5e0f8badChandler Carruth  // explicit default was defined, the default is 0.
419fcd783d583d270b7ec1ec3e0fcf83cd93d30e381Ted Kremenek  uint64 default_value_uint64() const;
42098520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // Get the field default value if cpp_type() == CPPTYPE_FLOAT.  If no
42198520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // explicit default was defined, the default is 0.0.
42298520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  float default_value_float() const;
42398520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // Get the field default value if cpp_type() == CPPTYPE_DOUBLE.  If no
42498520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // explicit default was defined, the default is 0.0.
425aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  double default_value_double() const;
426aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // Get the field default value if cpp_type() == CPPTYPE_BOOL.  If no
42798520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // explicit default was defined, the default is false.
4281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  bool default_value_bool() const;
429f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // Get the field default value if cpp_type() == CPPTYPE_ENUM.  If no
430aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // explicit default was defined, the default is the first value defined
43198520835eb1aa091429afa06e9f4f7ebe3864d34Anna Zaks  // in the enum type (all enum types are required to have at least one value).
4321d26f48dc2eea1c07431ca1519d7034a21b9bcffTed Kremenek  // This never returns NULL.
43358f5ec7d56b1ebf5f90ee11226ebe7663f2821eaTed Kremenek  const EnumValueDescriptor* default_value_enum() const;
434f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  // Get the field default value if cpp_type() == CPPTYPE_STRING.  If no
4351eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // explicit default was defined, the default is the empty string.
4365f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  const string& default_value_string() const;
437fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek
438f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  // The Descriptor for the message of which this is a field.  For extensions,
43906a54a38be5054c910ffc92db60edab23f9ea105Argyrios Kyrtzidis  // this is the extended type.  Never NULL.
440fc576514d06c46a7cac49500169411d82f38d04bTed Kremenek  const Descriptor* containing_type() const;
441f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek
4429fb9474c5b267400d4abfbff63c8b39f378235d4Argyrios Kyrtzidis  // An extension may be declared within the scope of another message.  If this
4435f9e272e632e951b1efe824cd16acb4d96077930Chris Lattner  // field is an extension (is_extension() is true), then extension_scope()
4449fb9474c5b267400d4abfbff63c8b39f378235d4Argyrios Kyrtzidis  // returns that message, or NULL if the extension was declared at global
445d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis  // scope.  If this is not an extension, extension_scope() is undefined (may
446aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  // assert-fail).
447aa5609891df937291bf962dd2fc7deb2ceae292fAnna Zaks  const Descriptor* extension_scope() const;
4481f5171edc9fb9b0698a75fa981c29a750b0a6082Anna Zaks
4493fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // If type is TYPE_MESSAGE or TYPE_GROUP, returns a descriptor for the
4501f5171edc9fb9b0698a75fa981c29a750b0a6082Anna Zaks  // message or the group type.  Otherwise, undefined.
4511f5171edc9fb9b0698a75fa981c29a750b0a6082Anna Zaks  const Descriptor* message_type() const;
452d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis  // If type is TYPE_ENUM, returns a descriptor for the enum.  Otherwise,
453f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // undefined.
454f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  const EnumDescriptor* enum_type() const;
455f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
456d655ab28fdf7c940d3f79f8f287954d7f76e0977Argyrios Kyrtzidis  // EXPERIMENTAL; DO NOT USE.
457f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // If this field is a map field, experimental_map_key() is the field
458f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // that is the key for this map.
4593fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // experimental_map_key()->containing_type() is the same as message_type().
4603fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  const FieldDescriptor* experimental_map_key() const;
461a5937bbfd19e61d651a58b0f0ffeef68457902a5Ted Kremenek
46275d03cffe20b5c945ef04eba208efb0437339997Ted Kremenek  // Get the FieldOptions for this field.  This includes things listed in
4633fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // square brackets after the field definition.  E.g., the field:
464f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  //   optional string text = 1 [ctype=CORD];
4653fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // has the "ctype" option set.  Allowed options are defined by FieldOptions
4663bbd8cd831788c506f2980293eb3c7e1b3ca2501Anna Zaks  // in google/protobuf/descriptor.proto, and any available extensions of that
467f6eafcca7734274d277afa121f2c4fb025a54218Ted Kremenek  // message.
468f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const FieldOptions& options() const;
4696f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith
4703fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // See Descriptor::CopyTo().
471f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  void CopyTo(FieldDescriptorProto* proto) const;
472c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu
473f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // See Descriptor::DebugString().
4741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  string DebugString() const;
475b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek
4763fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // Helper method to get the CppType for a particular Type.
4773fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  static CppType TypeToCppType(Type type);
4781eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
479f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Return true iff [packed = true] is valid for fields of this type.
480f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  static inline bool IsTypePackable(Type field_type);
481c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu
4823df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek private:
48334d7734b6ed1d9c0f647405e065251eb67f42badTed Kremenek  typedef FieldOptions OptionsType;
4843fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks
4853fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  // See Descriptor::DebugString().
4861eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  void DebugString(int depth, string *contents) const;
4873df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek
4883df6421150271266b5a90fd4c6bfa6566c38c036Ted Kremenek  // formats the default value appropriately and returns it as a string.
489bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek  // Must have a default value to call this. If quote_string_type is true, then
490bc46f345838b1c0d420dbd3655c94f5f360fb5b8Ted Kremenek  // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped.
4913fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  string DefaultValueAsString(bool quote_string_type) const;
49217a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose
4934e4d08403ca5cfd4d558fa2936215d3a4e5a528dDavid Blaikie  const string* name_;
49417a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const string* full_name_;
4953fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  const string* lowercase_name_;
49617a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const string* camelcase_name_;
49717a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const FileDescriptor* file_;
49817a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  int number_;
4993fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  Type type_;
50017a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  Label label_;
50117a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  bool is_extension_;
50217a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const Descriptor* containing_type_;
5033fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  const Descriptor* extension_scope_;
5043fd5f370a28552976c52e76c3035d79012d78ddaAnna Zaks  const Descriptor* message_type_;
50517a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const EnumDescriptor* enum_type_;
50617a38e2636a8b1ce473fc6504c4b16cb09db29f4Jordy Rose  const FieldDescriptor* experimental_map_key_;
507b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek  const FieldOptions* options_;
508b35a74a40c1d2656a25e560e773ed48bdf49f9c0Ted Kremenek  // IMPORTANT:  If you add a new field, make sure to search for all instances
509f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // of Allocate<FieldDescriptor>() and AllocateArray<FieldDescriptor>() in
510f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  // descriptor.cc and update them to initialize the field.
511f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
512f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek  bool has_default_value_;
5139ef6537a894c33003359b1f9b9676e9178e028b7Ted Kremenek  union {
51408b86531ade68727c56918f162816075b87c864aJordy Rose    int32  default_value_int32_;
51508b86531ade68727c56918f162816075b87c864aJordy Rose    int64  default_value_int64_;
51608b86531ade68727c56918f162816075b87c864aJordy Rose    uint32 default_value_uint32_;
51708b86531ade68727c56918f162816075b87c864aJordy Rose    uint64 default_value_uint64_;
518efceabd2380f49306bc0229583458e93b062094bDaniel Dunbar    float  default_value_float_;
519be1fe1eb12a1cb91c8e3a9fcc2db4dfe989def6cTed Kremenek    double default_value_double_;
52008b86531ade68727c56918f162816075b87c864aJordy Rose    bool   default_value_bool_;
521f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek
522f4381fddf152a63e1ac97185293c47ec0ac2f1a6Ted Kremenek    const EnumValueDescriptor* default_value_enum_;
523f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek    const string* default_value_string_;
524f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  };
525f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
526f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  static const CppType kTypeToCppTypeMap[MAX_TYPE + 1];
527f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
5281eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  static const char * const kTypeToName[MAX_TYPE + 1];
529c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu
5306f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  static const char * const kLabelToName[MAX_LABEL + 1];
531710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek
532f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Must be constructed using DescriptorPool.
533f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  FieldDescriptor() {}
534f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  friend class DescriptorBuilder;
535f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  friend class FileDescriptor;
5361eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  friend class Descriptor;
537f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldDescriptor);
5389c378f705405d37f49795d5e915989de774fe11fTed Kremenek};
53956b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek
5401eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// Describes an enum type defined in a .proto file.  To get the EnumDescriptor
541710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// for a generated enum type, call TypeName_descriptor().  Use DescriptorPool
5421eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump// to construct your own descriptors.
5439c378f705405d37f49795d5e915989de774fe11fTed Kremenekclass LIBPROTOBUF_EXPORT EnumDescriptor {
544f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek public:
5451eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // The name of this enum type in the containing scope.
546f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const string& name() const;
547f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
548c5619d901a68dc27a9e310a6a831f03efebcd950Zhongxing Xu  // The fully-qualified name of the enum type, scope delimited by periods.
549f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const string& full_name() const;
5501eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
551710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  // Index of this enum within the file or containing message's enum array.
552f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  int index() const;
553f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
554f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // The .proto file in which this enum type was defined.  Never NULL.
555710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  const FileDescriptor* file() const;
556f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
557f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // The number of values for this EnumDescriptor.  Guaranteed to be greater
558f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // than zero.
559f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  int value_count() const;
560f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Gets a value by index, where 0 <= index < value_count().
561f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // These are returned in the order they were defined in the .proto file.
562d57a7ef9252964bc6c8471451d7bd395b0520cb8Chris Lattner  const EnumValueDescriptor* value(int index) const;
5631eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
5646f42b62b6194f53bcbc349f5d17388e1936535d7Dylan Noblesmith  // Looks up a value by name.  Returns NULL if no such value exists.
565b044c473a155b7e827680635c1699cb2b5b6c0eeDan Gohman  const EnumValueDescriptor* FindValueByName(const string& name) const;
566f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Looks up a value by number.  Returns NULL if no such value exists.  If
567f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // multiple values have this number, the first one defined is returned.
568f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const EnumValueDescriptor* FindValueByNumber(int number) const;
5691eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
570710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  // If this enum type is nested in a message type, this is that message type.
571f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // Otherwise, NULL.
572f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const Descriptor* containing_type() const;
5739c378f705405d37f49795d5e915989de774fe11fTed Kremenek
5741eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // Get options for this enum type.  These are specified in the .proto file by
57545479c88c8a44c55d27c3bd855273bb318082f37Ted Kremenek  // placing lines like "option foo = 1234;" in the enum definition.  Allowed
5761eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // options are defined by EnumOptions in google/protobuf/descriptor.proto,
577f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // and any available extensions of that message.
578f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const EnumOptions& options() const;
579f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
5801eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // See Descriptor::CopyTo().
581f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  void CopyTo(EnumDescriptorProto* proto) const;
582f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
583f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // See Descriptor::DebugString().
584f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  string DebugString() const;
585f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
586f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek private:
5871eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  typedef EnumOptions OptionsType;
588f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
589f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // See Descriptor::DebugString().
590f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  void DebugString(int depth, string *contents) const;
591f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
592f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const string* name_;
593f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const string* full_name_;
594f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const FileDescriptor* file_;
595f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  const Descriptor* containing_type_;
59656b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  const EnumOptions* options_;
59756b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek
598f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // True if this is a placeholder for an unknown type.
59956b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  bool is_placeholder_;
60056b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  // True if this is a placeholder and the type name wasn't fully-qualified.
601f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  bool is_unqualified_placeholder_;
602f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek
6031eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  int value_count_;
604d1289327f7d2126d732e23cc189ecd0aa5d07cbbTed Kremenek  EnumValueDescriptor* values_;
605f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // IMPORTANT:  If you add a new field, make sure to search for all instances
606f8ce6991f41d55b6e8526b7a7919771428e2b181Ted Kremenek  // of Allocate<EnumDescriptor>() and AllocateArray<EnumDescriptor>() in
6079c378f705405d37f49795d5e915989de774fe11fTed Kremenek  // descriptor.cc and update them to initialize the field.
60856b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek
60956b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  // Must be constructed using DescriptorPool.
61056b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  EnumDescriptor() {}
61156b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  friend class DescriptorBuilder;
61256b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  friend class Descriptor;
61356b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  friend class FieldDescriptor;
61456b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  friend class EnumValueDescriptor;
61556b98719b0dbebb33cb228afa888c47156be2381Ted Kremenek  friend class FileDescriptor;
616710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumDescriptor);
617710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek};
6186cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer
619710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// Describes an individual enum constant of a particular type.  To get the
620710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// EnumValueDescriptor for a given enum value, first get the EnumDescriptor
621710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// for its type, then use EnumDescriptor::FindValueByName() or
622710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// EnumDescriptor::FindValueByNumber().  Use DescriptorPool to construct
623710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek// your own descriptors.
624710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenekclass LIBPROTOBUF_EXPORT EnumValueDescriptor {
6251eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump public:
626710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  const string& name() const;  // Name of this enum constant.
6276cb7c1a43b0c8f739d1f54b7fdae5ede86033496Benjamin Kramer  int index() const;           // Index within the enums's Descriptor.
628710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  int number() const;          // Numeric value of this enum constant.
6291eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump
630710ad9343f32b33b336369b20edad1a21a0b3299Ted Kremenek  // The full_name of an enum value is a sibling symbol of the enum type.
6311eb4433ac451dc16f4133a88af2d002ac26c58efMike Stump  // e.g. the full name of FieldDescriptorProto::TYPE_INT32 is actually
632932680ecdeab4c216ce7c0736093d3dcd2f60f6dDaniel Dunbar  // "google.protobuf.FieldDescriptorProto.TYPE_INT32", NOT
633  // "google.protobuf.FieldDescriptorProto.Type.TYPE_INT32".  This is to conform
634  // with C++ scoping rules for enums.
635  const string& full_name() const;
636
637  // The type of this value.  Never NULL.
638  const EnumDescriptor* type() const;
639
640  // Get options for this enum value.  These are specified in the .proto file
641  // by adding text like "[foo = 1234]" after an enum value definition.
642  // Allowed options are defined by EnumValueOptions in
643  // google/protobuf/descriptor.proto, and any available extensions of that
644  // message.
645  const EnumValueOptions& options() const;
646
647  // See Descriptor::CopyTo().
648  void CopyTo(EnumValueDescriptorProto* proto) const;
649
650  // See Descriptor::DebugString().
651  string DebugString() const;
652
653 private:
654  typedef EnumValueOptions OptionsType;
655
656  // See Descriptor::DebugString().
657  void DebugString(int depth, string *contents) const;
658
659  const string* name_;
660  const string* full_name_;
661  int number_;
662  const EnumDescriptor* type_;
663  const EnumValueOptions* options_;
664  // IMPORTANT:  If you add a new field, make sure to search for all instances
665  // of Allocate<EnumValueDescriptor>() and AllocateArray<EnumValueDescriptor>()
666  // in descriptor.cc and update them to initialize the field.
667
668  // Must be constructed using DescriptorPool.
669  EnumValueDescriptor() {}
670  friend class DescriptorBuilder;
671  friend class EnumDescriptor;
672  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(EnumValueDescriptor);
673};
674
675// Describes an RPC service.  To get the ServiceDescriptor for a service,
676// call Service::GetDescriptor().  Generated service classes also have a
677// static method called descriptor() which returns the type's
678// ServiceDescriptor.  Use DescriptorPool to construct your own descriptors.
679class LIBPROTOBUF_EXPORT ServiceDescriptor {
680 public:
681  // The name of the service, not including its containing scope.
682  const string& name() const;
683  // The fully-qualified name of the service, scope delimited by periods.
684  const string& full_name() const;
685  // Index of this service within the file's services array.
686  int index() const;
687
688  // The .proto file in which this service was defined.  Never NULL.
689  const FileDescriptor* file() const;
690
691  // Get options for this service type.  These are specified in the .proto file
692  // by placing lines like "option foo = 1234;" in the service definition.
693  // Allowed options are defined by ServiceOptions in
694  // google/protobuf/descriptor.proto, and any available extensions of that
695  // message.
696  const ServiceOptions& options() const;
697
698  // The number of methods this service defines.
699  int method_count() const;
700  // Gets a MethodDescriptor by index, where 0 <= index < method_count().
701  // These are returned in the order they were defined in the .proto file.
702  const MethodDescriptor* method(int index) const;
703
704  // Look up a MethodDescriptor by name.
705  const MethodDescriptor* FindMethodByName(const string& name) const;
706
707  // See Descriptor::CopyTo().
708  void CopyTo(ServiceDescriptorProto* proto) const;
709
710  // See Descriptor::DebugString().
711  string DebugString() const;
712
713 private:
714  typedef ServiceOptions OptionsType;
715
716  // See Descriptor::DebugString().
717  void DebugString(string *contents) const;
718
719  const string* name_;
720  const string* full_name_;
721  const FileDescriptor* file_;
722  const ServiceOptions* options_;
723  int method_count_;
724  MethodDescriptor* methods_;
725  // IMPORTANT:  If you add a new field, make sure to search for all instances
726  // of Allocate<ServiceDescriptor>() and AllocateArray<ServiceDescriptor>() in
727  // descriptor.cc and update them to initialize the field.
728
729  // Must be constructed using DescriptorPool.
730  ServiceDescriptor() {}
731  friend class DescriptorBuilder;
732  friend class FileDescriptor;
733  friend class MethodDescriptor;
734  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ServiceDescriptor);
735};
736
737// Describes an individual service method.  To obtain a MethodDescriptor given
738// a service, first get its ServiceDescriptor, then call
739// ServiceDescriptor::FindMethodByName().  Use DescriptorPool to construct your
740// own descriptors.
741class LIBPROTOBUF_EXPORT MethodDescriptor {
742 public:
743  // Name of this method, not including containing scope.
744  const string& name() const;
745  // The fully-qualified name of the method, scope delimited by periods.
746  const string& full_name() const;
747  // Index within the service's Descriptor.
748  int index() const;
749
750  // Gets the service to which this method belongs.  Never NULL.
751  const ServiceDescriptor* service() const;
752
753  // Gets the type of protocol message which this method accepts as input.
754  const Descriptor* input_type() const;
755  // Gets the type of protocol message which this message produces as output.
756  const Descriptor* output_type() const;
757
758  // Get options for this method.  These are specified in the .proto file by
759  // placing lines like "option foo = 1234;" in curly-braces after a method
760  // declaration.  Allowed options are defined by MethodOptions in
761  // google/protobuf/descriptor.proto, and any available extensions of that
762  // message.
763  const MethodOptions& options() const;
764
765  // See Descriptor::CopyTo().
766  void CopyTo(MethodDescriptorProto* proto) const;
767
768  // See Descriptor::DebugString().
769  string DebugString() const;
770
771 private:
772  typedef MethodOptions OptionsType;
773
774  // See Descriptor::DebugString().
775  void DebugString(int depth, string *contents) const;
776
777  const string* name_;
778  const string* full_name_;
779  const ServiceDescriptor* service_;
780  const Descriptor* input_type_;
781  const Descriptor* output_type_;
782  const MethodOptions* options_;
783  // IMPORTANT:  If you add a new field, make sure to search for all instances
784  // of Allocate<MethodDescriptor>() and AllocateArray<MethodDescriptor>() in
785  // descriptor.cc and update them to initialize the field.
786
787  // Must be constructed using DescriptorPool.
788  MethodDescriptor() {}
789  friend class DescriptorBuilder;
790  friend class ServiceDescriptor;
791  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor);
792};
793
794// Describes a whole .proto file.  To get the FileDescriptor for a compiled-in
795// file, get the descriptor for something defined in that file and call
796// descriptor->file().  Use DescriptorPool to construct your own descriptors.
797class LIBPROTOBUF_EXPORT FileDescriptor {
798 public:
799  // The filename, relative to the source tree.
800  // e.g. "google/protobuf/descriptor.proto"
801  const string& name() const;
802
803  // The package, e.g. "google.protobuf.compiler".
804  const string& package() const;
805
806  // The DescriptorPool in which this FileDescriptor and all its contents were
807  // allocated.  Never NULL.
808  const DescriptorPool* pool() const;
809
810  // The number of files imported by this one.
811  int dependency_count() const;
812  // Gets an imported file by index, where 0 <= index < dependency_count().
813  // These are returned in the order they were defined in the .proto file.
814  const FileDescriptor* dependency(int index) const;
815
816  // Number of top-level message types defined in this file.  (This does not
817  // include nested types.)
818  int message_type_count() const;
819  // Gets a top-level message type, where 0 <= index < message_type_count().
820  // These are returned in the order they were defined in the .proto file.
821  const Descriptor* message_type(int index) const;
822
823  // Number of top-level enum types defined in this file.  (This does not
824  // include nested types.)
825  int enum_type_count() const;
826  // Gets a top-level enum type, where 0 <= index < enum_type_count().
827  // These are returned in the order they were defined in the .proto file.
828  const EnumDescriptor* enum_type(int index) const;
829
830  // Number of services defined in this file.
831  int service_count() const;
832  // Gets a service, where 0 <= index < service_count().
833  // These are returned in the order they were defined in the .proto file.
834  const ServiceDescriptor* service(int index) const;
835
836  // Number of extensions defined at file scope.  (This does not include
837  // extensions nested within message types.)
838  int extension_count() const;
839  // Gets an extension's descriptor, where 0 <= index < extension_count().
840  // These are returned in the order they were defined in the .proto file.
841  const FieldDescriptor* extension(int index) const;
842
843  // Get options for this file.  These are specified in the .proto file by
844  // placing lines like "option foo = 1234;" at the top level, outside of any
845  // other definitions.  Allowed options are defined by FileOptions in
846  // google/protobuf/descriptor.proto, and any available extensions of that
847  // message.
848  const FileOptions& options() const;
849
850  // Find a top-level message type by name.  Returns NULL if not found.
851  const Descriptor* FindMessageTypeByName(const string& name) const;
852  // Find a top-level enum type by name.  Returns NULL if not found.
853  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
854  // Find an enum value defined in any top-level enum by name.  Returns NULL if
855  // not found.
856  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
857  // Find a service definition by name.  Returns NULL if not found.
858  const ServiceDescriptor* FindServiceByName(const string& name) const;
859  // Find a top-level extension definition by name.  Returns NULL if not found.
860  const FieldDescriptor* FindExtensionByName(const string& name) const;
861  // Similar to FindExtensionByName(), but searches by lowercased-name.  See
862  // Descriptor::FindFieldByLowercaseName().
863  const FieldDescriptor* FindExtensionByLowercaseName(const string& name) const;
864  // Similar to FindExtensionByName(), but searches by camelcased-name.  See
865  // Descriptor::FindFieldByCamelcaseName().
866  const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const;
867
868  // See Descriptor::CopyTo().
869  void CopyTo(FileDescriptorProto* proto) const;
870
871  // See Descriptor::DebugString().
872  string DebugString() const;
873
874 private:
875  typedef FileOptions OptionsType;
876
877  const string* name_;
878  const string* package_;
879  const DescriptorPool* pool_;
880  int dependency_count_;
881  const FileDescriptor** dependencies_;
882  int message_type_count_;
883  Descriptor* message_types_;
884  int enum_type_count_;
885  EnumDescriptor* enum_types_;
886  int service_count_;
887  ServiceDescriptor* services_;
888  int extension_count_;
889  FieldDescriptor* extensions_;
890  const FileOptions* options_;
891
892  const FileDescriptorTables* tables_;
893  // IMPORTANT:  If you add a new field, make sure to search for all instances
894  // of Allocate<FileDescriptor>() and AllocateArray<FileDescriptor>() in
895  // descriptor.cc and update them to initialize the field.
896
897  FileDescriptor() {}
898  friend class DescriptorBuilder;
899  friend class Descriptor;
900  friend class FieldDescriptor;
901  friend class EnumDescriptor;
902  friend class ServiceDescriptor;
903  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor);
904};
905
906// ===================================================================
907
908// Used to construct descriptors.
909//
910// Normally you won't want to build your own descriptors.  Message classes
911// constructed by the protocol compiler will provide them for you.  However,
912// if you are implementing Message on your own, or if you are writing a
913// program which can operate on totally arbitrary types and needs to load
914// them from some sort of database, you might need to.
915//
916// Since Descriptors are composed of a whole lot of cross-linked bits of
917// data that would be a pain to put together manually, the
918// DescriptorPool class is provided to make the process easier.  It can
919// take a FileDescriptorProto (defined in descriptor.proto), validate it,
920// and convert it to a set of nicely cross-linked Descriptors.
921//
922// DescriptorPool also helps with memory management.  Descriptors are
923// composed of many objects containing static data and pointers to each
924// other.  In all likelihood, when it comes time to delete this data,
925// you'll want to delete it all at once.  In fact, it is not uncommon to
926// have a whole pool of descriptors all cross-linked with each other which
927// you wish to delete all at once.  This class represents such a pool, and
928// handles the memory management for you.
929//
930// You can also search for descriptors within a DescriptorPool by name, and
931// extensions by number.
932class LIBPROTOBUF_EXPORT DescriptorPool {
933 public:
934  // Create a normal, empty DescriptorPool.
935  DescriptorPool();
936
937  // Constructs a DescriptorPool that, when it can't find something among the
938  // descriptors already in the pool, looks for it in the given
939  // DescriptorDatabase.
940  // Notes:
941  // - If a DescriptorPool is constructed this way, its BuildFile*() methods
942  //   must not be called (they will assert-fail).  The only way to populate
943  //   the pool with descriptors is to call the Find*By*() methods.
944  // - The Find*By*() methods may block the calling thread if the
945  //   DescriptorDatabase blocks.  This in turn means that parsing messages
946  //   may block if they need to look up extensions.
947  // - The Find*By*() methods will use mutexes for thread-safety, thus making
948  //   them slower even when they don't have to fall back to the database.
949  //   In fact, even the Find*By*() methods of descriptor objects owned by
950  //   this pool will be slower, since they will have to obtain locks too.
951  // - An ErrorCollector may optionally be given to collect validation errors
952  //   in files loaded from the database.  If not given, errors will be printed
953  //   to GOOGLE_LOG(ERROR).  Remember that files are built on-demand, so this
954  //   ErrorCollector may be called from any thread that calls one of the
955  //   Find*By*() methods.
956  class ErrorCollector;
957  explicit DescriptorPool(DescriptorDatabase* fallback_database,
958                          ErrorCollector* error_collector = NULL);
959
960  ~DescriptorPool();
961
962  // Get a pointer to the generated pool.  Generated protocol message classes
963  // which are compiled into the binary will allocate their descriptors in
964  // this pool.  Do not add your own descriptors to this pool.
965  static const DescriptorPool* generated_pool();
966
967  // Find a FileDescriptor in the pool by file name.  Returns NULL if not
968  // found.
969  const FileDescriptor* FindFileByName(const string& name) const;
970
971  // Find the FileDescriptor in the pool which defines the given symbol.
972  // If any of the Find*ByName() methods below would succeed, then this is
973  // equivalent to calling that method and calling the result's file() method.
974  // Otherwise this returns NULL.
975  const FileDescriptor* FindFileContainingSymbol(
976      const string& symbol_name) const;
977
978  // Looking up descriptors ------------------------------------------
979  // These find descriptors by fully-qualified name.  These will find both
980  // top-level descriptors and nested descriptors.  They return NULL if not
981  // found.
982
983  const Descriptor* FindMessageTypeByName(const string& name) const;
984  const FieldDescriptor* FindFieldByName(const string& name) const;
985  const FieldDescriptor* FindExtensionByName(const string& name) const;
986  const EnumDescriptor* FindEnumTypeByName(const string& name) const;
987  const EnumValueDescriptor* FindEnumValueByName(const string& name) const;
988  const ServiceDescriptor* FindServiceByName(const string& name) const;
989  const MethodDescriptor* FindMethodByName(const string& name) const;
990
991  // Finds an extension of the given type by number.  The extendee must be
992  // a member of this DescriptorPool or one of its underlays.
993  const FieldDescriptor* FindExtensionByNumber(const Descriptor* extendee,
994                                               int number) const;
995
996  // Finds extensions of extendee. The extensions will be appended to
997  // out in an undefined order. Only extensions defined directly in
998  // this DescriptorPool or one of its underlays are guaranteed to be
999  // found: extensions defined in the fallback database might not be found
1000  // depending on the database implementation.
1001  void FindAllExtensions(const Descriptor* extendee,
1002                         vector<const FieldDescriptor*>* out) const;
1003
1004  // Building descriptors --------------------------------------------
1005
1006  // When converting a FileDescriptorProto to a FileDescriptor, various
1007  // errors might be detected in the input.  The caller may handle these
1008  // programmatically by implementing an ErrorCollector.
1009  class LIBPROTOBUF_EXPORT ErrorCollector {
1010   public:
1011    inline ErrorCollector() {}
1012    virtual ~ErrorCollector();
1013
1014    // These constants specify what exact part of the construct is broken.
1015    // This is useful e.g. for mapping the error back to an exact location
1016    // in a .proto file.
1017    enum ErrorLocation {
1018      NAME,              // the symbol name, or the package name for files
1019      NUMBER,            // field or extension range number
1020      TYPE,              // field type
1021      EXTENDEE,          // field extendee
1022      DEFAULT_VALUE,     // field default value
1023      INPUT_TYPE,        // method input type
1024      OUTPUT_TYPE,       // method output type
1025      OPTION_NAME,       // name in assignment
1026      OPTION_VALUE,      // value in option assignment
1027      OTHER              // some other problem
1028    };
1029
1030    // Reports an error in the FileDescriptorProto.
1031    virtual void AddError(
1032      const string& filename,      // File name in which the error occurred.
1033      const string& element_name,  // Full name of the erroneous element.
1034      const Message* descriptor,   // Descriptor of the erroneous element.
1035      ErrorLocation location,      // One of the location constants, above.
1036      const string& message        // Human-readable error message.
1037      ) = 0;
1038
1039   private:
1040    GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ErrorCollector);
1041  };
1042
1043  // Convert the FileDescriptorProto to real descriptors and place them in
1044  // this DescriptorPool.  All dependencies of the file must already be in
1045  // the pool.  Returns the resulting FileDescriptor, or NULL if there were
1046  // problems with the input (e.g. the message was invalid, or dependencies
1047  // were missing).  Details about the errors are written to GOOGLE_LOG(ERROR).
1048  const FileDescriptor* BuildFile(const FileDescriptorProto& proto);
1049
1050  // Same as BuildFile() except errors are sent to the given ErrorCollector.
1051  const FileDescriptor* BuildFileCollectingErrors(
1052    const FileDescriptorProto& proto,
1053    ErrorCollector* error_collector);
1054
1055  // By default, it is an error if a FileDescriptorProto contains references
1056  // to types or other files that are not found in the DescriptorPool (or its
1057  // backing DescriptorDatabase, if any).  If you call
1058  // AllowUnknownDependencies(), however, then unknown types and files
1059  // will be replaced by placeholder descriptors.  This can allow you to
1060  // perform some useful operations with a .proto file even if you do not
1061  // have access to other .proto files on which it depends.  However, some
1062  // heuristics must be used to fill in the gaps in information, and these
1063  // can lead to descriptors which are inaccurate.  For example, the
1064  // DescriptorPool may be forced to guess whether an unknown type is a message
1065  // or an enum, as well as what package it resides in.  Furthermore,
1066  // placeholder types will not be discoverable via FindMessageTypeByName()
1067  // and similar methods, which could confuse some descriptor-based algorithms.
1068  // Generally, the results of this option should only be relied upon for
1069  // debugging purposes.
1070  void AllowUnknownDependencies() { allow_unknown_ = true; }
1071
1072  // Internal stuff --------------------------------------------------
1073  // These methods MUST NOT be called from outside the proto2 library.
1074  // These methods may contain hidden pitfalls and may be removed in a
1075  // future library version.
1076
1077  // Create a DescriptorPool which is overlaid on top of some other pool.
1078  // If you search for a descriptor in the overlay and it is not found, the
1079  // underlay will be searched as a backup.  If the underlay has its own
1080  // underlay, that will be searched next, and so on.  This also means that
1081  // files built in the overlay will be cross-linked with the underlay's
1082  // descriptors if necessary.  The underlay remains property of the caller;
1083  // it must remain valid for the lifetime of the newly-constructed pool.
1084  //
1085  // Example:  Say you want to parse a .proto file at runtime in order to use
1086  // its type with a DynamicMessage.  Say this .proto file has dependencies,
1087  // but you know that all the dependencies will be things that are already
1088  // compiled into the binary.  For ease of use, you'd like to load the types
1089  // right out of generated_pool() rather than have to parse redundant copies
1090  // of all these .protos and runtime.  But, you don't want to add the parsed
1091  // types directly into generated_pool(): this is not allowed, and would be
1092  // bad design anyway.  So, instead, you could use generated_pool() as an
1093  // underlay for a new DescriptorPool in which you add only the new file.
1094  //
1095  // WARNING:  Use of underlays can lead to many subtle gotchas.  Instead,
1096  //   try to formulate what you want to do in terms of DescriptorDatabases.
1097  explicit DescriptorPool(const DescriptorPool* underlay);
1098
1099  // Called by generated classes at init time to add their descriptors to
1100  // generated_pool.  Do NOT call this in your own code!  filename must be a
1101  // permanent string (e.g. a string literal).
1102  static void InternalAddGeneratedFile(
1103      const void* encoded_file_descriptor, int size);
1104
1105
1106  // For internal use only:  Gets a non-const pointer to the generated pool.
1107  // This is called at static-initialization time only, so thread-safety is
1108  // not a concern.  If both an underlay and a fallback database are present,
1109  // the fallback database takes precedence.
1110  static DescriptorPool* internal_generated_pool();
1111
1112  // For internal use only:  Changes the behavior of BuildFile() such that it
1113  // allows the file to make reference to message types declared in other files
1114  // which it did not officially declare as dependencies.
1115  void InternalDontEnforceDependencies();
1116
1117  // For internal use only.
1118  void internal_set_underlay(const DescriptorPool* underlay) {
1119    underlay_ = underlay;
1120  }
1121
1122  // For internal (unit test) use only:  Returns true if a FileDescriptor has
1123  // been constructed for the given file, false otherwise.  Useful for testing
1124  // lazy descriptor initialization behavior.
1125  bool InternalIsFileLoaded(const string& filename) const;
1126
1127 private:
1128  friend class Descriptor;
1129  friend class FieldDescriptor;
1130  friend class EnumDescriptor;
1131  friend class ServiceDescriptor;
1132  friend class FileDescriptor;
1133  friend class DescriptorBuilder;
1134
1135  // Tries to find something in the fallback database and link in the
1136  // corresponding proto file.  Returns true if successful, in which case
1137  // the caller should search for the thing again.  These are declared
1138  // const because they are called by (semantically) const methods.
1139  bool TryFindFileInFallbackDatabase(const string& name) const;
1140  bool TryFindSymbolInFallbackDatabase(const string& name) const;
1141  bool TryFindExtensionInFallbackDatabase(const Descriptor* containing_type,
1142                                          int field_number) const;
1143
1144  // Like BuildFile() but called internally when the file has been loaded from
1145  // fallback_database_.  Declared const because it is called by (semantically)
1146  // const methods.
1147  const FileDescriptor* BuildFileFromDatabase(
1148    const FileDescriptorProto& proto) const;
1149
1150  // If fallback_database_ is NULL, this is NULL.  Otherwise, this is a mutex
1151  // which must be locked while accessing tables_.
1152  Mutex* mutex_;
1153
1154  // See constructor.
1155  DescriptorDatabase* fallback_database_;
1156  ErrorCollector* default_error_collector_;
1157  const DescriptorPool* underlay_;
1158
1159  // This class contains a lot of hash maps with complicated types that
1160  // we'd like to keep out of the header.
1161  class Tables;
1162  scoped_ptr<Tables> tables_;
1163
1164  bool enforce_dependencies_;
1165  bool allow_unknown_;
1166
1167  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(DescriptorPool);
1168};
1169
1170// inline methods ====================================================
1171
1172// These macros makes this repetitive code more readable.
1173#define PROTOBUF_DEFINE_ACCESSOR(CLASS, FIELD, TYPE) \
1174  inline TYPE CLASS::FIELD() const { return FIELD##_; }
1175
1176// Strings fields are stored as pointers but returned as const references.
1177#define PROTOBUF_DEFINE_STRING_ACCESSOR(CLASS, FIELD) \
1178  inline const string& CLASS::FIELD() const { return *FIELD##_; }
1179
1180// Arrays take an index parameter, obviously.
1181#define PROTOBUF_DEFINE_ARRAY_ACCESSOR(CLASS, FIELD, TYPE) \
1182  inline TYPE CLASS::FIELD(int index) const { return FIELD##s_ + index; }
1183
1184#define PROTOBUF_DEFINE_OPTIONS_ACCESSOR(CLASS, TYPE) \
1185  inline const TYPE& CLASS::options() const { return *options_; }
1186
1187PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, name)
1188PROTOBUF_DEFINE_STRING_ACCESSOR(Descriptor, full_name)
1189PROTOBUF_DEFINE_ACCESSOR(Descriptor, file, const FileDescriptor*)
1190PROTOBUF_DEFINE_ACCESSOR(Descriptor, containing_type, const Descriptor*)
1191
1192PROTOBUF_DEFINE_ACCESSOR(Descriptor, field_count, int)
1193PROTOBUF_DEFINE_ACCESSOR(Descriptor, nested_type_count, int)
1194PROTOBUF_DEFINE_ACCESSOR(Descriptor, enum_type_count, int)
1195
1196PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, field, const FieldDescriptor*)
1197PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, nested_type, const Descriptor*)
1198PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, enum_type, const EnumDescriptor*)
1199
1200PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_range_count, int)
1201PROTOBUF_DEFINE_ACCESSOR(Descriptor, extension_count, int)
1202PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension_range,
1203                               const Descriptor::ExtensionRange*)
1204PROTOBUF_DEFINE_ARRAY_ACCESSOR(Descriptor, extension,
1205                               const FieldDescriptor*)
1206PROTOBUF_DEFINE_OPTIONS_ACCESSOR(Descriptor, MessageOptions);
1207
1208PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, name)
1209PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, full_name)
1210PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, lowercase_name)
1211PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, camelcase_name)
1212PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, file, const FileDescriptor*)
1213PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, number, int)
1214PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, is_extension, bool)
1215PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, type, FieldDescriptor::Type)
1216PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, label, FieldDescriptor::Label)
1217PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, containing_type, const Descriptor*)
1218PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, extension_scope, const Descriptor*)
1219PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, message_type, const Descriptor*)
1220PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, enum_type, const EnumDescriptor*)
1221PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, experimental_map_key,
1222                         const FieldDescriptor*)
1223PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FieldDescriptor, FieldOptions);
1224PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, has_default_value, bool)
1225PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int32 , int32 )
1226PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_int64 , int64 )
1227PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint32, uint32)
1228PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_uint64, uint64)
1229PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_float , float )
1230PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_double, double)
1231PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_bool  , bool  )
1232PROTOBUF_DEFINE_ACCESSOR(FieldDescriptor, default_value_enum,
1233                         const EnumValueDescriptor*)
1234PROTOBUF_DEFINE_STRING_ACCESSOR(FieldDescriptor, default_value_string)
1235
1236PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, name)
1237PROTOBUF_DEFINE_STRING_ACCESSOR(EnumDescriptor, full_name)
1238PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, file, const FileDescriptor*)
1239PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, containing_type, const Descriptor*)
1240PROTOBUF_DEFINE_ACCESSOR(EnumDescriptor, value_count, int)
1241PROTOBUF_DEFINE_ARRAY_ACCESSOR(EnumDescriptor, value,
1242                               const EnumValueDescriptor*)
1243PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumDescriptor, EnumOptions);
1244
1245PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, name)
1246PROTOBUF_DEFINE_STRING_ACCESSOR(EnumValueDescriptor, full_name)
1247PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, number, int)
1248PROTOBUF_DEFINE_ACCESSOR(EnumValueDescriptor, type, const EnumDescriptor*)
1249PROTOBUF_DEFINE_OPTIONS_ACCESSOR(EnumValueDescriptor, EnumValueOptions);
1250
1251PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, name)
1252PROTOBUF_DEFINE_STRING_ACCESSOR(ServiceDescriptor, full_name)
1253PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, file, const FileDescriptor*)
1254PROTOBUF_DEFINE_ACCESSOR(ServiceDescriptor, method_count, int)
1255PROTOBUF_DEFINE_ARRAY_ACCESSOR(ServiceDescriptor, method,
1256                               const MethodDescriptor*)
1257PROTOBUF_DEFINE_OPTIONS_ACCESSOR(ServiceDescriptor, ServiceOptions);
1258
1259PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, name)
1260PROTOBUF_DEFINE_STRING_ACCESSOR(MethodDescriptor, full_name)
1261PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*)
1262PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*)
1263PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*)
1264PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions);
1265
1266PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name)
1267PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package)
1268PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*)
1269PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int)
1270PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int)
1271PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int)
1272PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int)
1273PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, extension_count, int)
1274PROTOBUF_DEFINE_OPTIONS_ACCESSOR(FileDescriptor, FileOptions);
1275
1276PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, message_type, const Descriptor*)
1277PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, enum_type, const EnumDescriptor*)
1278PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, service,
1279                               const ServiceDescriptor*)
1280PROTOBUF_DEFINE_ARRAY_ACCESSOR(FileDescriptor, extension,
1281                               const FieldDescriptor*)
1282
1283#undef PROTOBUF_DEFINE_ACCESSOR
1284#undef PROTOBUF_DEFINE_STRING_ACCESSOR
1285#undef PROTOBUF_DEFINE_ARRAY_ACCESSOR
1286
1287// A few accessors differ from the macros...
1288
1289inline bool FieldDescriptor::is_required() const {
1290  return label() == LABEL_REQUIRED;
1291}
1292
1293inline bool FieldDescriptor::is_optional() const {
1294  return label() == LABEL_OPTIONAL;
1295}
1296
1297inline bool FieldDescriptor::is_repeated() const {
1298  return label() == LABEL_REPEATED;
1299}
1300
1301inline bool FieldDescriptor::is_packable() const {
1302  return is_repeated() && IsTypePackable(type());
1303}
1304
1305// To save space, index() is computed by looking at the descriptor's position
1306// in the parent's array of children.
1307inline int FieldDescriptor::index() const {
1308  if (!is_extension_) {
1309    return this - containing_type_->fields_;
1310  } else if (extension_scope_ != NULL) {
1311    return this - extension_scope_->extensions_;
1312  } else {
1313    return this - file_->extensions_;
1314  }
1315}
1316
1317inline int Descriptor::index() const {
1318  if (containing_type_ == NULL) {
1319    return this - file_->message_types_;
1320  } else {
1321    return this - containing_type_->nested_types_;
1322  }
1323}
1324
1325inline int EnumDescriptor::index() const {
1326  if (containing_type_ == NULL) {
1327    return this - file_->enum_types_;
1328  } else {
1329    return this - containing_type_->enum_types_;
1330  }
1331}
1332
1333inline int EnumValueDescriptor::index() const {
1334  return this - type_->values_;
1335}
1336
1337inline int ServiceDescriptor::index() const {
1338  return this - file_->services_;
1339}
1340
1341inline int MethodDescriptor::index() const {
1342  return this - service_->methods_;
1343}
1344
1345inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const {
1346  return kTypeToCppTypeMap[type_];
1347}
1348
1349inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) {
1350  return kTypeToCppTypeMap[type];
1351}
1352
1353inline bool FieldDescriptor::IsTypePackable(Type field_type) {
1354  return (field_type != FieldDescriptor::TYPE_STRING &&
1355          field_type != FieldDescriptor::TYPE_GROUP &&
1356          field_type != FieldDescriptor::TYPE_MESSAGE &&
1357          field_type != FieldDescriptor::TYPE_BYTES);
1358}
1359
1360inline const FileDescriptor* FileDescriptor::dependency(int index) const {
1361  return dependencies_[index];
1362}
1363
1364}  // namespace protobuf
1365
1366}  // namespace google
1367#endif  // GOOGLE_PROTOBUF_DESCRIPTOR_H__
1368