From b55a20fa2c669b181f47ea9219b8e74d1263da19 Mon Sep 17 00:00:00 2001 From: "xiaofeng@google.com" Date: Sat, 22 Sep 2012 02:40:50 +0000 Subject: Down-integrate from internal branch --- src/google/protobuf/descriptor.h | 162 +++++++++++++++++++++++++++++++++++++-- 1 file changed, 156 insertions(+), 6 deletions(-) (limited to 'src/google/protobuf/descriptor.h') diff --git a/src/google/protobuf/descriptor.h b/src/google/protobuf/descriptor.h index 7f87dd80..410d3ed4 100644 --- a/src/google/protobuf/descriptor.h +++ b/src/google/protobuf/descriptor.h @@ -89,6 +89,7 @@ class ServiceOptions; class MethodOptions; class FileOptions; class UninterpretedOption; +class SourceCodeInfo; // Defined in message.h class Message; @@ -100,6 +101,20 @@ class FileDescriptorTables; // Defined in unknown_field_set.h. class UnknownField; +// NB, all indices are zero-based. +struct SourceLocation { + int start_line; + int end_line; + int start_column; + int end_column; + + // Doc comments found at the source location. + // TODO(kenton): Maybe this struct should have been named SourceInfo or + // something instead. Oh well. + string leading_comments; + string trailing_comments; +}; + // Describes a type of protocol message, or a particular group within a // message. To obtain the Descriptor for a given message object, call // Message::GetDescriptor(). Generated message classes also have a @@ -236,6 +251,13 @@ class LIBPROTOBUF_EXPORT Descriptor { // this message type's scope. const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const; + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this message declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef MessageOptions OptionsType; @@ -243,6 +265,10 @@ class LIBPROTOBUF_EXPORT Descriptor { // correct depth void DebugString(int depth, string *contents) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; const FileDescriptor* file_; @@ -388,15 +414,19 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // when parsing formats which prefer to use camel-case naming style. const string& camelcase_name() const; - Type type() const; // Declared type of this field. - CppType cpp_type() const; // C++ type of this field. - Label label() const; // optional/required/repeated + Type type() const; // Declared type of this field. + const char* type_name() const; // Name of the declared type. + CppType cpp_type() const; // C++ type of this field. + const char* cpp_type_name() const; // Name of the C++ type. + Label label() const; // optional/required/repeated bool is_required() const; // shorthand for label() == LABEL_REQUIRED bool is_optional() const; // shorthand for label() == LABEL_OPTIONAL bool is_repeated() const; // shorthand for label() == LABEL_REPEATED bool is_packable() const; // shorthand for is_repeated() && // IsTypePackable(type()) + bool is_packed() const; // shorthand for is_packable() && + // options().packed() // Index of this field within the message's field array, or the file or // extension scope's extensions array. @@ -479,6 +509,13 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // Return true iff [packed = true] is valid for fields of this type. static inline bool IsTypePackable(Type field_type); + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this field declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef FieldOptions OptionsType; @@ -490,6 +527,10 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { // types of CPPTYPE_STRING whill be surrounded by quotes and CEscaped. string DefaultValueAsString(bool quote_string_type) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; const string* lowercase_name_; @@ -527,6 +568,8 @@ class LIBPROTOBUF_EXPORT FieldDescriptor { static const char * const kTypeToName[MAX_TYPE + 1]; + static const char * const kCppTypeToName[MAX_CPPTYPE + 1]; + static const char * const kLabelToName[MAX_LABEL + 1]; // Must be constructed using DescriptorPool. @@ -583,12 +626,23 @@ class LIBPROTOBUF_EXPORT EnumDescriptor { // See Descriptor::DebugString(). string DebugString() const; + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this enum declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef EnumOptions OptionsType; // See Descriptor::DebugString(). void DebugString(int depth, string *contents) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; const FileDescriptor* file_; @@ -650,12 +704,23 @@ class LIBPROTOBUF_EXPORT EnumValueDescriptor { // See Descriptor::DebugString(). string DebugString() const; + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this enum value declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef EnumValueOptions OptionsType; // See Descriptor::DebugString(). void DebugString(int depth, string *contents) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; int number_; @@ -703,19 +768,29 @@ class LIBPROTOBUF_EXPORT ServiceDescriptor { // Look up a MethodDescriptor by name. const MethodDescriptor* FindMethodByName(const string& name) const; - // See Descriptor::CopyTo(). void CopyTo(ServiceDescriptorProto* proto) const; // See Descriptor::DebugString(). string DebugString() const; + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this service declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef ServiceOptions OptionsType; // See Descriptor::DebugString(). void DebugString(string *contents) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; const FileDescriptor* file_; @@ -768,12 +843,23 @@ class LIBPROTOBUF_EXPORT MethodDescriptor { // See Descriptor::DebugString(). string DebugString() const; + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of this method declaration. Returns false and leaves + // |*out_location| unchanged iff location information was not available. + bool GetSourceLocation(SourceLocation* out_location) const; + private: typedef MethodOptions OptionsType; // See Descriptor::DebugString(). void DebugString(int depth, string *contents) const; + // Walks up the descriptor tree to generate the source location path + // to this descriptor from the file root. + void GetLocationPath(vector* output) const; + const string* name_; const string* full_name_; const ServiceDescriptor* service_; @@ -791,6 +877,7 @@ class LIBPROTOBUF_EXPORT MethodDescriptor { GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(MethodDescriptor); }; + // Describes a whole .proto file. To get the FileDescriptor for a compiled-in // file, get the descriptor for something defined in that file and call // descriptor->file(). Use DescriptorPool to construct your own descriptors. @@ -813,6 +900,22 @@ class LIBPROTOBUF_EXPORT FileDescriptor { // These are returned in the order they were defined in the .proto file. const FileDescriptor* dependency(int index) const; + // The number of files public imported by this one. + // The public dependency list is a subset of the dependency list. + int public_dependency_count() const; + // Gets a public imported file by index, where 0 <= index < + // public_dependency_count(). + // These are returned in the order they were defined in the .proto file. + const FileDescriptor* public_dependency(int index) const; + + // The number of files that are imported for weak fields. + // The weak dependency list is a subset of the dependency list. + int weak_dependency_count() const; + // Gets a weak imported file by index, where 0 <= index < + // weak_dependency_count(). + // These are returned in the order they were defined in the .proto file. + const FileDescriptor* weak_dependency(int index) const; + // Number of top-level message types defined in this file. (This does not // include nested types.) int message_type_count() const; @@ -866,12 +969,28 @@ class LIBPROTOBUF_EXPORT FileDescriptor { const FieldDescriptor* FindExtensionByCamelcaseName(const string& name) const; // See Descriptor::CopyTo(). + // Notes: + // - This method does NOT copy source code information since it is relatively + // large and rarely needed. See CopySourceCodeInfoTo() below. void CopyTo(FileDescriptorProto* proto) const; + // Write the source code information of this FileDescriptor into the given + // FileDescriptorProto. See CopyTo() above. + void CopySourceCodeInfoTo(FileDescriptorProto* proto) const; // See Descriptor::DebugString(). string DebugString() const; private: + // Source Location --------------------------------------------------- + + // Updates |*out_location| to the source location of the complete + // extent of the declaration or declaration-part denoted by |path|. + // Returns false and leaves |*out_location| unchanged iff location + // information was not available. (See SourceCodeInfo for + // description of path encoding.) + bool GetSourceLocation(const vector& path, + SourceLocation* out_location) const; + typedef FileOptions OptionsType; const string* name_; @@ -879,6 +998,10 @@ class LIBPROTOBUF_EXPORT FileDescriptor { const DescriptorPool* pool_; int dependency_count_; const FileDescriptor** dependencies_; + int public_dependency_count_; + int* public_dependencies_; + int weak_dependency_count_; + int* weak_dependencies_; int message_type_count_; Descriptor* message_types_; int enum_type_count_; @@ -890,6 +1013,7 @@ class LIBPROTOBUF_EXPORT FileDescriptor { const FileOptions* options_; const FileDescriptorTables* tables_; + const SourceCodeInfo* source_code_info_; // IMPORTANT: If you add a new field, make sure to search for all instances // of Allocate() and AllocateArray() in // descriptor.cc and update them to initialize the field. @@ -899,6 +1023,8 @@ class LIBPROTOBUF_EXPORT FileDescriptor { friend class Descriptor; friend class FieldDescriptor; friend class EnumDescriptor; + friend class EnumValueDescriptor; + friend class MethodDescriptor; friend class ServiceDescriptor; GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FileDescriptor); }; @@ -1106,7 +1232,7 @@ class LIBPROTOBUF_EXPORT DescriptorPool { // For internal use only: Gets a non-const pointer to the generated pool. // This is called at static-initialization time only, so thread-safety is // not a concern. If both an underlay and a fallback database are present, - // the fallback database takes precedence. + // the underlay takes precedence. static DescriptorPool* internal_generated_pool(); // For internal use only: Changes the behavior of BuildFile() such that it @@ -1132,6 +1258,11 @@ class LIBPROTOBUF_EXPORT DescriptorPool { friend class FileDescriptor; friend class DescriptorBuilder; + // Return true if the given name is a sub-symbol of any non-package + // descriptor that already exists in the descriptor pool. (The full + // definition of such types is already known.) + bool IsSubSymbolOfBuiltType(const string& name) const; + // Tries to find something in the fallback database and link in the // corresponding proto file. Returns true if successful, in which case // the caller should search for the thing again. These are declared @@ -1262,11 +1393,12 @@ PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, service, const ServiceDescriptor*) PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, input_type, const Descriptor*) PROTOBUF_DEFINE_ACCESSOR(MethodDescriptor, output_type, const Descriptor*) PROTOBUF_DEFINE_OPTIONS_ACCESSOR(MethodDescriptor, MethodOptions); - PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, name) PROTOBUF_DEFINE_STRING_ACCESSOR(FileDescriptor, package) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, pool, const DescriptorPool*) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, dependency_count, int) +PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, public_dependency_count, int) +PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, weak_dependency_count, int) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, message_type_count, int) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, enum_type_count, int) PROTOBUF_DEFINE_ACCESSOR(FileDescriptor, service_count, int) @@ -1342,10 +1474,18 @@ inline int MethodDescriptor::index() const { return this - service_->methods_; } +inline const char* FieldDescriptor::type_name() const { + return kTypeToName[type_]; +} + inline FieldDescriptor::CppType FieldDescriptor::cpp_type() const { return kTypeToCppTypeMap[type_]; } +inline const char* FieldDescriptor::cpp_type_name() const { + return kCppTypeToName[kTypeToCppTypeMap[type_]]; +} + inline FieldDescriptor::CppType FieldDescriptor::TypeToCppType(Type type) { return kTypeToCppTypeMap[type]; } @@ -1361,6 +1501,16 @@ inline const FileDescriptor* FileDescriptor::dependency(int index) const { return dependencies_[index]; } +inline const FileDescriptor* FileDescriptor::public_dependency( + int index) const { + return dependencies_[public_dependencies_[index]]; +} + +inline const FileDescriptor* FileDescriptor::weak_dependency( + int index) const { + return dependencies_[weak_dependencies_[index]]; +} + } // namespace protobuf } // namespace google -- cgit v1.2.3