/src/google/protobuf/descriptor.cc
C++ | 4947 lines | 3637 code | 621 blank | 689 comment | 843 complexity | c496ead239e188edac527033feeb9e3c MD5 | raw file
Possible License(s): BSD-3-Clause
Large files files are truncated, but you can click here to view the full file
- // Protocol Buffers - Google's data interchange format
- // Copyright 2008 Google Inc. All rights reserved.
- // http://code.google.com/p/protobuf/
- //
- // Redistribution and use in source and binary forms, with or without
- // modification, are permitted provided that the following conditions are
- // met:
- //
- // * Redistributions of source code must retain the above copyright
- // notice, this list of conditions and the following disclaimer.
- // * Redistributions in binary form must reproduce the above
- // copyright notice, this list of conditions and the following disclaimer
- // in the documentation and/or other materials provided with the
- // distribution.
- // * Neither the name of Google Inc. nor the names of its
- // contributors may be used to endorse or promote products derived from
- // this software without specific prior written permission.
- //
- // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
- // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
- // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
- // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
- // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
- // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
- // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
- // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
- // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
- // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- // Author: kenton@google.com (Kenton Varda)
- // Based on original Protocol Buffers design by
- // Sanjay Ghemawat, Jeff Dean, and others.
- #include <google/protobuf/stubs/hash.h>
- #include <map>
- #include <set>
- #include <vector>
- #include <algorithm>
- #include <limits>
- #include <google/protobuf/descriptor.h>
- #include <google/protobuf/descriptor_database.h>
- #include <google/protobuf/descriptor.pb.h>
- #include <google/protobuf/dynamic_message.h>
- #include <google/protobuf/text_format.h>
- #include <google/protobuf/unknown_field_set.h>
- #include <google/protobuf/wire_format.h>
- #include <google/protobuf/io/coded_stream.h>
- #include <google/protobuf/io/tokenizer.h>
- #include <google/protobuf/io/zero_copy_stream_impl.h>
- #include <google/protobuf/stubs/common.h>
- #include <google/protobuf/stubs/once.h>
- #include <google/protobuf/stubs/strutil.h>
- #include <google/protobuf/stubs/substitute.h>
- #include <google/protobuf/stubs/map-util.h>
- #include <google/protobuf/stubs/stl_util.h>
- #undef PACKAGE // autoheader #defines this. :(
- namespace google {
- namespace protobuf {
- const FieldDescriptor::CppType
- FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = {
- static_cast<CppType>(0), // 0 is reserved for errors
- CPPTYPE_DOUBLE, // TYPE_DOUBLE
- CPPTYPE_FLOAT, // TYPE_FLOAT
- CPPTYPE_INT64, // TYPE_INT64
- CPPTYPE_UINT64, // TYPE_UINT64
- CPPTYPE_INT32, // TYPE_INT32
- CPPTYPE_UINT64, // TYPE_FIXED64
- CPPTYPE_UINT32, // TYPE_FIXED32
- CPPTYPE_BOOL, // TYPE_BOOL
- CPPTYPE_STRING, // TYPE_STRING
- CPPTYPE_MESSAGE, // TYPE_GROUP
- CPPTYPE_MESSAGE, // TYPE_MESSAGE
- CPPTYPE_STRING, // TYPE_BYTES
- CPPTYPE_UINT32, // TYPE_UINT32
- CPPTYPE_ENUM, // TYPE_ENUM
- CPPTYPE_INT32, // TYPE_SFIXED32
- CPPTYPE_INT64, // TYPE_SFIXED64
- CPPTYPE_INT32, // TYPE_SINT32
- CPPTYPE_INT64, // TYPE_SINT64
- };
- const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = {
- "ERROR", // 0 is reserved for errors
- "double", // TYPE_DOUBLE
- "float", // TYPE_FLOAT
- "int64", // TYPE_INT64
- "uint64", // TYPE_UINT64
- "int32", // TYPE_INT32
- "fixed64", // TYPE_FIXED64
- "fixed32", // TYPE_FIXED32
- "bool", // TYPE_BOOL
- "string", // TYPE_STRING
- "group", // TYPE_GROUP
- "message", // TYPE_MESSAGE
- "bytes", // TYPE_BYTES
- "uint32", // TYPE_UINT32
- "enum", // TYPE_ENUM
- "sfixed32", // TYPE_SFIXED32
- "sfixed64", // TYPE_SFIXED64
- "sint32", // TYPE_SINT32
- "sint64", // TYPE_SINT64
- };
- const char * const FieldDescriptor::kCppTypeToName[MAX_CPPTYPE + 1] = {
- "ERROR", // 0 is reserved for errors
- "int32", // CPPTYPE_INT32
- "int64", // CPPTYPE_INT64
- "uint32", // CPPTYPE_UINT32
- "uint64", // CPPTYPE_UINT64
- "double", // CPPTYPE_DOUBLE
- "float", // CPPTYPE_FLOAT
- "bool", // CPPTYPE_BOOL
- "enum", // CPPTYPE_ENUM
- "string", // CPPTYPE_STRING
- "message", // CPPTYPE_MESSAGE
- };
- const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = {
- "ERROR", // 0 is reserved for errors
- "optional", // LABEL_OPTIONAL
- "required", // LABEL_REQUIRED
- "repeated", // LABEL_REPEATED
- };
- #ifndef _MSC_VER // MSVC doesn't need these and won't even accept them.
- const int FieldDescriptor::kMaxNumber;
- const int FieldDescriptor::kFirstReservedNumber;
- const int FieldDescriptor::kLastReservedNumber;
- #endif
- namespace {
- string ToCamelCase(const string& input) {
- bool capitalize_next = false;
- string result;
- result.reserve(input.size());
- for (int i = 0; i < input.size(); i++) {
- if (input[i] == '_') {
- capitalize_next = true;
- } else if (capitalize_next) {
- // Note: I distrust ctype.h due to locales.
- if ('a' <= input[i] && input[i] <= 'z') {
- result.push_back(input[i] - 'a' + 'A');
- } else {
- result.push_back(input[i]);
- }
- capitalize_next = false;
- } else {
- result.push_back(input[i]);
- }
- }
- // Lower-case the first letter.
- if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') {
- result[0] = result[0] - 'A' + 'a';
- }
- return result;
- }
- // A DescriptorPool contains a bunch of hash_maps to implement the
- // various Find*By*() methods. Since hashtable lookups are O(1), it's
- // most efficient to construct a fixed set of large hash_maps used by
- // all objects in the pool rather than construct one or more small
- // hash_maps for each object.
- //
- // The keys to these hash_maps are (parent, name) or (parent, number)
- // pairs. Unfortunately STL doesn't provide hash functions for pair<>,
- // so we must invent our own.
- //
- // TODO(kenton): Use StringPiece rather than const char* in keys? It would
- // be a lot cleaner but we'd just have to convert it back to const char*
- // for the open source release.
- typedef pair<const void*, const char*> PointerStringPair;
- struct PointerStringPairEqual {
- inline bool operator()(const PointerStringPair& a,
- const PointerStringPair& b) const {
- return a.first == b.first && strcmp(a.second, b.second) == 0;
- }
- };
- template<typename PairType>
- struct PointerIntegerPairHash {
- size_t operator()(const PairType& p) const {
- // FIXME(kenton): What is the best way to compute this hash? I have
- // no idea! This seems a bit better than an XOR.
- return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second;
- }
- // Used only by MSVC and platforms where hash_map is not available.
- static const size_t bucket_size = 4;
- static const size_t min_buckets = 8;
- inline bool operator()(const PairType& a, const PairType& b) const {
- return a.first < b.first ||
- (a.first == b.first && a.second < b.second);
- }
- };
- typedef pair<const Descriptor*, int> DescriptorIntPair;
- typedef pair<const EnumDescriptor*, int> EnumIntPair;
- struct PointerStringPairHash {
- size_t operator()(const PointerStringPair& p) const {
- // FIXME(kenton): What is the best way to compute this hash? I have
- // no idea! This seems a bit better than an XOR.
- hash<const char*> cstring_hash;
- return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) +
- cstring_hash(p.second);
- }
- // Used only by MSVC and platforms where hash_map is not available.
- static const size_t bucket_size = 4;
- static const size_t min_buckets = 8;
- inline bool operator()(const PointerStringPair& a,
- const PointerStringPair& b) const {
- if (a.first < b.first) return true;
- if (a.first > b.first) return false;
- return strcmp(a.second, b.second) < 0;
- }
- };
- struct Symbol {
- enum Type {
- NULL_SYMBOL, MESSAGE, FIELD, ENUM, ENUM_VALUE, SERVICE, METHOD,
- PACKAGE
- };
- Type type;
- union {
- const Descriptor* descriptor;
- const FieldDescriptor* field_descriptor;
- const EnumDescriptor* enum_descriptor;
- const EnumValueDescriptor* enum_value_descriptor;
- const ServiceDescriptor* service_descriptor;
- const MethodDescriptor* method_descriptor;
- const FileDescriptor* package_file_descriptor;
- };
- inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; }
- inline bool IsNull() const { return type == NULL_SYMBOL; }
- inline bool IsType() const {
- return type == MESSAGE || type == ENUM;
- }
- inline bool IsAggregate() const {
- return type == MESSAGE || type == PACKAGE
- || type == ENUM || type == SERVICE;
- }
- #define CONSTRUCTOR(TYPE, TYPE_CONSTANT, FIELD) \
- inline explicit Symbol(const TYPE* value) { \
- type = TYPE_CONSTANT; \
- this->FIELD = value; \
- }
- CONSTRUCTOR(Descriptor , MESSAGE , descriptor )
- CONSTRUCTOR(FieldDescriptor , FIELD , field_descriptor )
- CONSTRUCTOR(EnumDescriptor , ENUM , enum_descriptor )
- CONSTRUCTOR(EnumValueDescriptor, ENUM_VALUE, enum_value_descriptor )
- CONSTRUCTOR(ServiceDescriptor , SERVICE , service_descriptor )
- CONSTRUCTOR(MethodDescriptor , METHOD , method_descriptor )
- CONSTRUCTOR(FileDescriptor , PACKAGE , package_file_descriptor)
- #undef CONSTRUCTOR
- const FileDescriptor* GetFile() const {
- switch (type) {
- case NULL_SYMBOL: return NULL;
- case MESSAGE : return descriptor ->file();
- case FIELD : return field_descriptor ->file();
- case ENUM : return enum_descriptor ->file();
- case ENUM_VALUE : return enum_value_descriptor->type()->file();
- case SERVICE : return service_descriptor ->file();
- case METHOD : return method_descriptor ->service()->file();
- case PACKAGE : return package_file_descriptor;
- }
- return NULL;
- }
- };
- const Symbol kNullSymbol;
- typedef hash_map<const char*, Symbol,
- hash<const char*>, streq>
- SymbolsByNameMap;
- typedef hash_map<PointerStringPair, Symbol,
- PointerStringPairHash, PointerStringPairEqual>
- SymbolsByParentMap;
- typedef hash_map<const char*, const FileDescriptor*,
- hash<const char*>, streq>
- FilesByNameMap;
- typedef hash_map<PointerStringPair, const FieldDescriptor*,
- PointerStringPairHash, PointerStringPairEqual>
- FieldsByNameMap;
- typedef hash_map<DescriptorIntPair, const FieldDescriptor*,
- PointerIntegerPairHash<DescriptorIntPair> >
- FieldsByNumberMap;
- typedef hash_map<EnumIntPair, const EnumValueDescriptor*,
- PointerIntegerPairHash<EnumIntPair> >
- EnumValuesByNumberMap;
- // This is a map rather than a hash_map, since we use it to iterate
- // through all the extensions that extend a given Descriptor, and an
- // ordered data structure that implements lower_bound is convenient
- // for that.
- typedef map<DescriptorIntPair, const FieldDescriptor*>
- ExtensionsGroupedByDescriptorMap;
- } // anonymous namespace
- // ===================================================================
- // DescriptorPool::Tables
- class DescriptorPool::Tables {
- public:
- Tables();
- ~Tables();
- // Record the current state of the tables to the stack of checkpoints.
- // Each call to AddCheckpoint() must be paired with exactly one call to either
- // ClearLastCheckpoint() or RollbackToLastCheckpoint().
- //
- // This is used when building files, since some kinds of validation errors
- // cannot be detected until the file's descriptors have already been added to
- // the tables.
- //
- // This supports recursive checkpoints, since building a file may trigger
- // recursive building of other files. Note that recursive checkpoints are not
- // normally necessary; explicit dependencies are built prior to checkpointing.
- // So although we recursively build transitive imports, there is at most one
- // checkpoint in the stack during dependency building.
- //
- // Recursive checkpoints only arise during cross-linking of the descriptors.
- // Symbol references must be resolved, via DescriptorBuilder::FindSymbol and
- // friends. If the pending file references an unknown symbol
- // (e.g., it is not defined in the pending file's explicit dependencies), and
- // the pool is using a fallback database, and that database contains a file
- // defining that symbol, and that file has not yet been built by the pool,
- // the pool builds the file during cross-linking, leading to another
- // checkpoint.
- void AddCheckpoint();
- // Mark the last checkpoint as having cleared successfully, removing it from
- // the stack. If the stack is empty, all pending symbols will be committed.
- //
- // Note that this does not guarantee that the symbols added since the last
- // checkpoint won't be rolled back: if a checkpoint gets rolled back,
- // everything past that point gets rolled back, including symbols added after
- // checkpoints that were pushed onto the stack after it and marked as cleared.
- void ClearLastCheckpoint();
- // Roll back the Tables to the state of the checkpoint at the top of the
- // stack, removing everything that was added after that point.
- void RollbackToLastCheckpoint();
- // The stack of files which are currently being built. Used to detect
- // cyclic dependencies when loading files from a DescriptorDatabase. Not
- // used when fallback_database_ == NULL.
- vector<string> pending_files_;
- // A set of files which we have tried to load from the fallback database
- // and encountered errors. We will not attempt to load them again.
- // Not used when fallback_database_ == NULL.
- hash_set<string> known_bad_files_;
- // The set of descriptors for which we've already loaded the full
- // set of extensions numbers from fallback_database_.
- hash_set<const Descriptor*> extensions_loaded_from_db_;
- // -----------------------------------------------------------------
- // Finding items.
- // Find symbols. This returns a null Symbol (symbol.IsNull() is true)
- // if not found.
- inline Symbol FindSymbol(const string& key) const;
- // This implements the body of DescriptorPool::Find*ByName(). It should
- // really be a private method of DescriptorPool, but that would require
- // declaring Symbol in descriptor.h, which would drag all kinds of other
- // stuff into the header. Yay C++.
- Symbol FindByNameHelper(
- const DescriptorPool* pool, const string& name) const;
- // These return NULL if not found.
- inline const FileDescriptor* FindFile(const string& key) const;
- inline const FieldDescriptor* FindExtension(const Descriptor* extendee,
- int number);
- inline void FindAllExtensions(const Descriptor* extendee,
- vector<const FieldDescriptor*>* out) const;
- // -----------------------------------------------------------------
- // Adding items.
- // These add items to the corresponding tables. They return false if
- // the key already exists in the table. For AddSymbol(), the string passed
- // in must be one that was constructed using AllocateString(), as it will
- // be used as a key in the symbols_by_name_ map without copying.
- bool AddSymbol(const string& full_name, Symbol symbol);
- bool AddFile(const FileDescriptor* file);
- bool AddExtension(const FieldDescriptor* field);
- // -----------------------------------------------------------------
- // Allocating memory.
- // Allocate an object which will be reclaimed when the pool is
- // destroyed. Note that the object's destructor will never be called,
- // so its fields must be plain old data (primitive data types and
- // pointers). All of the descriptor types are such objects.
- template<typename Type> Type* Allocate();
- // Allocate an array of objects which will be reclaimed when the
- // pool in destroyed. Again, destructors are never called.
- template<typename Type> Type* AllocateArray(int count);
- // Allocate a string which will be destroyed when the pool is destroyed.
- // The string is initialized to the given value for convenience.
- string* AllocateString(const string& value);
- // Allocate a protocol message object. Some older versions of GCC have
- // trouble understanding explicit template instantiations in some cases, so
- // in those cases we have to pass a dummy pointer of the right type as the
- // parameter instead of specifying the type explicitly.
- template<typename Type> Type* AllocateMessage(Type* dummy = NULL);
- // Allocate a FileDescriptorTables object.
- FileDescriptorTables* AllocateFileTables();
- private:
- vector<string*> strings_; // All strings in the pool.
- vector<Message*> messages_; // All messages in the pool.
- vector<FileDescriptorTables*> file_tables_; // All file tables in the pool.
- vector<void*> allocations_; // All other memory allocated in the pool.
- SymbolsByNameMap symbols_by_name_;
- FilesByNameMap files_by_name_;
- ExtensionsGroupedByDescriptorMap extensions_;
- struct CheckPoint {
- explicit CheckPoint(const Tables* tables)
- : strings_before_checkpoint(tables->strings_.size()),
- messages_before_checkpoint(tables->messages_.size()),
- file_tables_before_checkpoint(tables->file_tables_.size()),
- allocations_before_checkpoint(tables->allocations_.size()),
- pending_symbols_before_checkpoint(
- tables->symbols_after_checkpoint_.size()),
- pending_files_before_checkpoint(
- tables->files_after_checkpoint_.size()),
- pending_extensions_before_checkpoint(
- tables->extensions_after_checkpoint_.size()) {
- }
- int strings_before_checkpoint;
- int messages_before_checkpoint;
- int file_tables_before_checkpoint;
- int allocations_before_checkpoint;
- int pending_symbols_before_checkpoint;
- int pending_files_before_checkpoint;
- int pending_extensions_before_checkpoint;
- };
- vector<CheckPoint> checkpoints_;
- vector<const char* > symbols_after_checkpoint_;
- vector<const char* > files_after_checkpoint_;
- vector<DescriptorIntPair> extensions_after_checkpoint_;
- // Allocate some bytes which will be reclaimed when the pool is
- // destroyed.
- void* AllocateBytes(int size);
- };
- // Contains tables specific to a particular file. These tables are not
- // modified once the file has been constructed, so they need not be
- // protected by a mutex. This makes operations that depend only on the
- // contents of a single file -- e.g. Descriptor::FindFieldByName() --
- // lock-free.
- //
- // For historical reasons, the definitions of the methods of
- // FileDescriptorTables and DescriptorPool::Tables are interleaved below.
- // These used to be a single class.
- class FileDescriptorTables {
- public:
- FileDescriptorTables();
- ~FileDescriptorTables();
- // Empty table, used with placeholder files.
- static const FileDescriptorTables kEmpty;
- // -----------------------------------------------------------------
- // Finding items.
- // Find symbols. These return a null Symbol (symbol.IsNull() is true)
- // if not found.
- inline Symbol FindNestedSymbol(const void* parent,
- const string& name) const;
- inline Symbol FindNestedSymbolOfType(const void* parent,
- const string& name,
- const Symbol::Type type) const;
- // These return NULL if not found.
- inline const FieldDescriptor* FindFieldByNumber(
- const Descriptor* parent, int number) const;
- inline const FieldDescriptor* FindFieldByLowercaseName(
- const void* parent, const string& lowercase_name) const;
- inline const FieldDescriptor* FindFieldByCamelcaseName(
- const void* parent, const string& camelcase_name) const;
- inline const EnumValueDescriptor* FindEnumValueByNumber(
- const EnumDescriptor* parent, int number) const;
- // -----------------------------------------------------------------
- // Adding items.
- // These add items to the corresponding tables. They return false if
- // the key already exists in the table. For AddAliasUnderParent(), the
- // string passed in must be one that was constructed using AllocateString(),
- // as it will be used as a key in the symbols_by_parent_ map without copying.
- bool AddAliasUnderParent(const void* parent, const string& name,
- Symbol symbol);
- bool AddFieldByNumber(const FieldDescriptor* field);
- bool AddEnumValueByNumber(const EnumValueDescriptor* value);
- // Adds the field to the lowercase_name and camelcase_name maps. Never
- // fails because we allow duplicates; the first field by the name wins.
- void AddFieldByStylizedNames(const FieldDescriptor* field);
- private:
- SymbolsByParentMap symbols_by_parent_;
- FieldsByNameMap fields_by_lowercase_name_;
- FieldsByNameMap fields_by_camelcase_name_;
- FieldsByNumberMap fields_by_number_; // Not including extensions.
- EnumValuesByNumberMap enum_values_by_number_;
- };
- DescriptorPool::Tables::Tables()
- // Start some hash_map and hash_set objects with a small # of buckets
- : known_bad_files_(3),
- extensions_loaded_from_db_(3),
- symbols_by_name_(3),
- files_by_name_(3) {}
- DescriptorPool::Tables::~Tables() {
- GOOGLE_DCHECK(checkpoints_.empty());
- // Note that the deletion order is important, since the destructors of some
- // messages may refer to objects in allocations_.
- STLDeleteElements(&messages_);
- for (int i = 0; i < allocations_.size(); i++) {
- operator delete(allocations_[i]);
- }
- STLDeleteElements(&strings_);
- STLDeleteElements(&file_tables_);
- }
- FileDescriptorTables::FileDescriptorTables()
- // Initialize all the hash tables to start out with a small # of buckets
- : symbols_by_parent_(3),
- fields_by_lowercase_name_(3),
- fields_by_camelcase_name_(3),
- fields_by_number_(3),
- enum_values_by_number_(3) {
- }
- FileDescriptorTables::~FileDescriptorTables() {}
- const FileDescriptorTables FileDescriptorTables::kEmpty;
- void DescriptorPool::Tables::AddCheckpoint() {
- checkpoints_.push_back(CheckPoint(this));
- }
- void DescriptorPool::Tables::ClearLastCheckpoint() {
- GOOGLE_DCHECK(!checkpoints_.empty());
- checkpoints_.pop_back();
- if (checkpoints_.empty()) {
- // All checkpoints have been cleared: we can now commit all of the pending
- // data.
- symbols_after_checkpoint_.clear();
- files_after_checkpoint_.clear();
- extensions_after_checkpoint_.clear();
- }
- }
- void DescriptorPool::Tables::RollbackToLastCheckpoint() {
- GOOGLE_DCHECK(!checkpoints_.empty());
- const CheckPoint& checkpoint = checkpoints_.back();
- for (int i = checkpoint.pending_symbols_before_checkpoint;
- i < symbols_after_checkpoint_.size();
- i++) {
- symbols_by_name_.erase(symbols_after_checkpoint_[i]);
- }
- for (int i = checkpoint.pending_files_before_checkpoint;
- i < files_after_checkpoint_.size();
- i++) {
- files_by_name_.erase(files_after_checkpoint_[i]);
- }
- for (int i = checkpoint.pending_extensions_before_checkpoint;
- i < extensions_after_checkpoint_.size();
- i++) {
- extensions_.erase(extensions_after_checkpoint_[i]);
- }
- symbols_after_checkpoint_.resize(
- checkpoint.pending_symbols_before_checkpoint);
- files_after_checkpoint_.resize(checkpoint.pending_files_before_checkpoint);
- extensions_after_checkpoint_.resize(
- checkpoint.pending_extensions_before_checkpoint);
- STLDeleteContainerPointers(
- strings_.begin() + checkpoint.strings_before_checkpoint, strings_.end());
- STLDeleteContainerPointers(
- messages_.begin() + checkpoint.messages_before_checkpoint,
- messages_.end());
- STLDeleteContainerPointers(
- file_tables_.begin() + checkpoint.file_tables_before_checkpoint,
- file_tables_.end());
- for (int i = checkpoint.allocations_before_checkpoint;
- i < allocations_.size();
- i++) {
- operator delete(allocations_[i]);
- }
- strings_.resize(checkpoint.strings_before_checkpoint);
- messages_.resize(checkpoint.messages_before_checkpoint);
- file_tables_.resize(checkpoint.file_tables_before_checkpoint);
- allocations_.resize(checkpoint.allocations_before_checkpoint);
- checkpoints_.pop_back();
- }
- // -------------------------------------------------------------------
- inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const {
- const Symbol* result = FindOrNull(symbols_by_name_, key.c_str());
- if (result == NULL) {
- return kNullSymbol;
- } else {
- return *result;
- }
- }
- inline Symbol FileDescriptorTables::FindNestedSymbol(
- const void* parent, const string& name) const {
- const Symbol* result =
- FindOrNull(symbols_by_parent_, PointerStringPair(parent, name.c_str()));
- if (result == NULL) {
- return kNullSymbol;
- } else {
- return *result;
- }
- }
- inline Symbol FileDescriptorTables::FindNestedSymbolOfType(
- const void* parent, const string& name, const Symbol::Type type) const {
- Symbol result = FindNestedSymbol(parent, name);
- if (result.type != type) return kNullSymbol;
- return result;
- }
- Symbol DescriptorPool::Tables::FindByNameHelper(
- const DescriptorPool* pool, const string& name) const {
- MutexLockMaybe lock(pool->mutex_);
- Symbol result = FindSymbol(name);
- if (result.IsNull() && pool->underlay_ != NULL) {
- // Symbol not found; check the underlay.
- result =
- pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name);
- }
- if (result.IsNull()) {
- // Symbol still not found, so check fallback database.
- if (pool->TryFindSymbolInFallbackDatabase(name)) {
- result = FindSymbol(name);
- }
- }
- return result;
- }
- inline const FileDescriptor* DescriptorPool::Tables::FindFile(
- const string& key) const {
- return FindPtrOrNull(files_by_name_, key.c_str());
- }
- inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber(
- const Descriptor* parent, int number) const {
- return FindPtrOrNull(fields_by_number_, make_pair(parent, number));
- }
- inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName(
- const void* parent, const string& lowercase_name) const {
- return FindPtrOrNull(fields_by_lowercase_name_,
- PointerStringPair(parent, lowercase_name.c_str()));
- }
- inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName(
- const void* parent, const string& camelcase_name) const {
- return FindPtrOrNull(fields_by_camelcase_name_,
- PointerStringPair(parent, camelcase_name.c_str()));
- }
- inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber(
- const EnumDescriptor* parent, int number) const {
- return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number));
- }
- inline const FieldDescriptor* DescriptorPool::Tables::FindExtension(
- const Descriptor* extendee, int number) {
- return FindPtrOrNull(extensions_, make_pair(extendee, number));
- }
- inline void DescriptorPool::Tables::FindAllExtensions(
- const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
- ExtensionsGroupedByDescriptorMap::const_iterator it =
- extensions_.lower_bound(make_pair(extendee, 0));
- for (; it != extensions_.end() && it->first.first == extendee; ++it) {
- out->push_back(it->second);
- }
- }
- // -------------------------------------------------------------------
- bool DescriptorPool::Tables::AddSymbol(
- const string& full_name, Symbol symbol) {
- if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) {
- symbols_after_checkpoint_.push_back(full_name.c_str());
- return true;
- } else {
- return false;
- }
- }
- bool FileDescriptorTables::AddAliasUnderParent(
- const void* parent, const string& name, Symbol symbol) {
- PointerStringPair by_parent_key(parent, name.c_str());
- return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol);
- }
- bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) {
- if (InsertIfNotPresent(&files_by_name_, file->name().c_str(), file)) {
- files_after_checkpoint_.push_back(file->name().c_str());
- return true;
- } else {
- return false;
- }
- }
- void FileDescriptorTables::AddFieldByStylizedNames(
- const FieldDescriptor* field) {
- const void* parent;
- if (field->is_extension()) {
- if (field->extension_scope() == NULL) {
- parent = field->file();
- } else {
- parent = field->extension_scope();
- }
- } else {
- parent = field->containing_type();
- }
- PointerStringPair lowercase_key(parent, field->lowercase_name().c_str());
- InsertIfNotPresent(&fields_by_lowercase_name_, lowercase_key, field);
- PointerStringPair camelcase_key(parent, field->camelcase_name().c_str());
- InsertIfNotPresent(&fields_by_camelcase_name_, camelcase_key, field);
- }
- bool FileDescriptorTables::AddFieldByNumber(const FieldDescriptor* field) {
- DescriptorIntPair key(field->containing_type(), field->number());
- return InsertIfNotPresent(&fields_by_number_, key, field);
- }
- bool FileDescriptorTables::AddEnumValueByNumber(
- const EnumValueDescriptor* value) {
- EnumIntPair key(value->type(), value->number());
- return InsertIfNotPresent(&enum_values_by_number_, key, value);
- }
- bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) {
- DescriptorIntPair key(field->containing_type(), field->number());
- if (InsertIfNotPresent(&extensions_, key, field)) {
- extensions_after_checkpoint_.push_back(key);
- return true;
- } else {
- return false;
- }
- }
- // -------------------------------------------------------------------
- template<typename Type>
- Type* DescriptorPool::Tables::Allocate() {
- return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type)));
- }
- template<typename Type>
- Type* DescriptorPool::Tables::AllocateArray(int count) {
- return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count));
- }
- string* DescriptorPool::Tables::AllocateString(const string& value) {
- string* result = new string(value);
- strings_.push_back(result);
- return result;
- }
- template<typename Type>
- Type* DescriptorPool::Tables::AllocateMessage(Type* dummy) {
- Type* result = new Type;
- messages_.push_back(result);
- return result;
- }
- FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() {
- FileDescriptorTables* result = new FileDescriptorTables;
- file_tables_.push_back(result);
- return result;
- }
- void* DescriptorPool::Tables::AllocateBytes(int size) {
- // TODO(kenton): Would it be worthwhile to implement this in some more
- // sophisticated way? Probably not for the open source release, but for
- // internal use we could easily plug in one of our existing memory pool
- // allocators...
- if (size == 0) return NULL;
- void* result = operator new(size);
- allocations_.push_back(result);
- return result;
- }
- // ===================================================================
- // DescriptorPool
- DescriptorPool::ErrorCollector::~ErrorCollector() {}
- DescriptorPool::DescriptorPool()
- : mutex_(NULL),
- fallback_database_(NULL),
- default_error_collector_(NULL),
- underlay_(NULL),
- tables_(new Tables),
- enforce_dependencies_(true),
- allow_unknown_(false) {}
- DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database,
- ErrorCollector* error_collector)
- : mutex_(new Mutex),
- fallback_database_(fallback_database),
- default_error_collector_(error_collector),
- underlay_(NULL),
- tables_(new Tables),
- enforce_dependencies_(true),
- allow_unknown_(false) {
- }
- DescriptorPool::DescriptorPool(const DescriptorPool* underlay)
- : mutex_(NULL),
- fallback_database_(NULL),
- default_error_collector_(NULL),
- underlay_(underlay),
- tables_(new Tables),
- enforce_dependencies_(true),
- allow_unknown_(false) {}
- DescriptorPool::~DescriptorPool() {
- if (mutex_ != NULL) delete mutex_;
- }
- // DescriptorPool::BuildFile() defined later.
- // DescriptorPool::BuildFileCollectingErrors() defined later.
- void DescriptorPool::InternalDontEnforceDependencies() {
- enforce_dependencies_ = false;
- }
- bool DescriptorPool::InternalIsFileLoaded(const string& filename) const {
- MutexLockMaybe lock(mutex_);
- return tables_->FindFile(filename) != NULL;
- }
- // generated_pool ====================================================
- namespace {
- EncodedDescriptorDatabase* generated_database_ = NULL;
- DescriptorPool* generated_pool_ = NULL;
- GOOGLE_PROTOBUF_DECLARE_ONCE(generated_pool_init_);
- void DeleteGeneratedPool() {
- delete generated_database_;
- generated_database_ = NULL;
- delete generated_pool_;
- generated_pool_ = NULL;
- }
- static void InitGeneratedPool() {
- generated_database_ = new EncodedDescriptorDatabase;
- generated_pool_ = new DescriptorPool(generated_database_);
- internal::OnShutdown(&DeleteGeneratedPool);
- }
- inline void InitGeneratedPoolOnce() {
- ::google::protobuf::GoogleOnceInit(&generated_pool_init_, &InitGeneratedPool);
- }
- } // anonymous namespace
- const DescriptorPool* DescriptorPool::generated_pool() {
- InitGeneratedPoolOnce();
- return generated_pool_;
- }
- DescriptorPool* DescriptorPool::internal_generated_pool() {
- InitGeneratedPoolOnce();
- return generated_pool_;
- }
- void DescriptorPool::InternalAddGeneratedFile(
- const void* encoded_file_descriptor, int size) {
- // So, this function is called in the process of initializing the
- // descriptors for generated proto classes. Each generated .pb.cc file
- // has an internal procedure called AddDescriptors() which is called at
- // process startup, and that function calls this one in order to register
- // the raw bytes of the FileDescriptorProto representing the file.
- //
- // We do not actually construct the descriptor objects right away. We just
- // hang on to the bytes until they are actually needed. We actually construct
- // the descriptor the first time one of the following things happens:
- // * Someone calls a method like descriptor(), GetDescriptor(), or
- // GetReflection() on the generated types, which requires returning the
- // descriptor or an object based on it.
- // * Someone looks up the descriptor in DescriptorPool::generated_pool().
- //
- // Once one of these happens, the DescriptorPool actually parses the
- // FileDescriptorProto and generates a FileDescriptor (and all its children)
- // based on it.
- //
- // Note that FileDescriptorProto is itself a generated protocol message.
- // Therefore, when we parse one, we have to be very careful to avoid using
- // any descriptor-based operations, since this might cause infinite recursion
- // or deadlock.
- InitGeneratedPoolOnce();
- GOOGLE_CHECK(generated_database_->Add(encoded_file_descriptor, size));
- }
- // Find*By* methods ==================================================
- // TODO(kenton): There's a lot of repeated code here, but I'm not sure if
- // there's any good way to factor it out. Think about this some time when
- // there's nothing more important to do (read: never).
- const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const {
- MutexLockMaybe lock(mutex_);
- const FileDescriptor* result = tables_->FindFile(name);
- if (result != NULL) return result;
- if (underlay_ != NULL) {
- result = underlay_->FindFileByName(name);
- if (result != NULL) return result;
- }
- if (TryFindFileInFallbackDatabase(name)) {
- result = tables_->FindFile(name);
- if (result != NULL) return result;
- }
- return NULL;
- }
- const FileDescriptor* DescriptorPool::FindFileContainingSymbol(
- const string& symbol_name) const {
- MutexLockMaybe lock(mutex_);
- Symbol result = tables_->FindSymbol(symbol_name);
- if (!result.IsNull()) return result.GetFile();
- if (underlay_ != NULL) {
- const FileDescriptor* file_result =
- underlay_->FindFileContainingSymbol(symbol_name);
- if (file_result != NULL) return file_result;
- }
- if (TryFindSymbolInFallbackDatabase(symbol_name)) {
- result = tables_->FindSymbol(symbol_name);
- if (!result.IsNull()) return result.GetFile();
- }
- return NULL;
- }
- const Descriptor* DescriptorPool::FindMessageTypeByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL;
- }
- const FieldDescriptor* DescriptorPool::FindFieldByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- if (result.type == Symbol::FIELD &&
- !result.field_descriptor->is_extension()) {
- return result.field_descriptor;
- } else {
- return NULL;
- }
- }
- const FieldDescriptor* DescriptorPool::FindExtensionByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- if (result.type == Symbol::FIELD &&
- result.field_descriptor->is_extension()) {
- return result.field_descriptor;
- } else {
- return NULL;
- }
- }
- const EnumDescriptor* DescriptorPool::FindEnumTypeByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL;
- }
- const EnumValueDescriptor* DescriptorPool::FindEnumValueByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- return (result.type == Symbol::ENUM_VALUE) ?
- result.enum_value_descriptor : NULL;
- }
- const ServiceDescriptor* DescriptorPool::FindServiceByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL;
- }
- const MethodDescriptor* DescriptorPool::FindMethodByName(
- const string& name) const {
- Symbol result = tables_->FindByNameHelper(this, name);
- return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL;
- }
- const FieldDescriptor* DescriptorPool::FindExtensionByNumber(
- const Descriptor* extendee, int number) const {
- MutexLockMaybe lock(mutex_);
- const FieldDescriptor* result = tables_->FindExtension(extendee, number);
- if (result != NULL) {
- return result;
- }
- if (underlay_ != NULL) {
- result = underlay_->FindExtensionByNumber(extendee, number);
- if (result != NULL) return result;
- }
- if (TryFindExtensionInFallbackDatabase(extendee, number)) {
- result = tables_->FindExtension(extendee, number);
- if (result != NULL) {
- return result;
- }
- }
- return NULL;
- }
- void DescriptorPool::FindAllExtensions(
- const Descriptor* extendee, vector<const FieldDescriptor*>* out) const {
- MutexLockMaybe lock(mutex_);
- // Initialize tables_->extensions_ from the fallback database first
- // (but do this only once per descriptor).
- if (fallback_database_ != NULL &&
- tables_->extensions_loaded_from_db_.count(extendee) == 0) {
- vector<int> numbers;
- if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(),
- &numbers)) {
- for (int i = 0; i < numbers.size(); ++i) {
- int number = numbers[i];
- if (tables_->FindExtension(extendee, number) == NULL) {
- TryFindExtensionInFallbackDatabase(extendee, number);
- }
- }
- tables_->extensions_loaded_from_db_.insert(extendee);
- }
- }
- tables_->FindAllExtensions(extendee, out);
- if (underlay_ != NULL) {
- underlay_->FindAllExtensions(extendee, out);
- }
- }
- // -------------------------------------------------------------------
- const FieldDescriptor*
- Descriptor::FindFieldByNumber(int key) const {
- const FieldDescriptor* result =
- file()->tables_->FindFieldByNumber(this, key);
- if (result == NULL || result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const FieldDescriptor*
- Descriptor::FindFieldByLowercaseName(const string& key) const {
- const FieldDescriptor* result =
- file()->tables_->FindFieldByLowercaseName(this, key);
- if (result == NULL || result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const FieldDescriptor*
- Descriptor::FindFieldByCamelcaseName(const string& key) const {
- const FieldDescriptor* result =
- file()->tables_->FindFieldByCamelcaseName(this, key);
- if (result == NULL || result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const FieldDescriptor*
- Descriptor::FindFieldByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
- if (!result.IsNull() && !result.field_descriptor->is_extension()) {
- return result.field_descriptor;
- } else {
- return NULL;
- }
- }
- const FieldDescriptor*
- Descriptor::FindExtensionByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
- if (!result.IsNull() && result.field_descriptor->is_extension()) {
- return result.field_descriptor;
- } else {
- return NULL;
- }
- }
- const FieldDescriptor*
- Descriptor::FindExtensionByLowercaseName(const string& key) const {
- const FieldDescriptor* result =
- file()->tables_->FindFieldByLowercaseName(this, key);
- if (result == NULL || !result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const FieldDescriptor*
- Descriptor::FindExtensionByCamelcaseName(const string& key) const {
- const FieldDescriptor* result =
- file()->tables_->FindFieldByCamelcaseName(this, key);
- if (result == NULL || !result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const Descriptor*
- Descriptor::FindNestedTypeByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
- if (!result.IsNull()) {
- return result.descriptor;
- } else {
- return NULL;
- }
- }
- const EnumDescriptor*
- Descriptor::FindEnumTypeByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
- if (!result.IsNull()) {
- return result.enum_descriptor;
- } else {
- return NULL;
- }
- }
- const EnumValueDescriptor*
- Descriptor::FindEnumValueByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
- if (!result.IsNull()) {
- return result.enum_value_descriptor;
- } else {
- return NULL;
- }
- }
- const EnumValueDescriptor*
- EnumDescriptor::FindValueByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
- if (!result.IsNull()) {
- return result.enum_value_descriptor;
- } else {
- return NULL;
- }
- }
- const EnumValueDescriptor*
- EnumDescriptor::FindValueByNumber(int key) const {
- return file()->tables_->FindEnumValueByNumber(this, key);
- }
- const MethodDescriptor*
- ServiceDescriptor::FindMethodByName(const string& key) const {
- Symbol result =
- file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD);
- if (!result.IsNull()) {
- return result.method_descriptor;
- } else {
- return NULL;
- }
- }
- const Descriptor*
- FileDescriptor::FindMessageTypeByName(const string& key) const {
- Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE);
- if (!result.IsNull()) {
- return result.descriptor;
- } else {
- return NULL;
- }
- }
- const EnumDescriptor*
- FileDescriptor::FindEnumTypeByName(const string& key) const {
- Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM);
- if (!result.IsNull()) {
- return result.enum_descriptor;
- } else {
- return NULL;
- }
- }
- const EnumValueDescriptor*
- FileDescriptor::FindEnumValueByName(const string& key) const {
- Symbol result =
- tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE);
- if (!result.IsNull()) {
- return result.enum_value_descriptor;
- } else {
- return NULL;
- }
- }
- const ServiceDescriptor*
- FileDescriptor::FindServiceByName(const string& key) const {
- Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE);
- if (!result.IsNull()) {
- return result.service_descriptor;
- } else {
- return NULL;
- }
- }
- const FieldDescriptor*
- FileDescriptor::FindExtensionByName(const string& key) const {
- Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD);
- if (!result.IsNull() && result.field_descriptor->is_extension()) {
- return result.field_descriptor;
- } else {
- return NULL;
- }
- }
- const FieldDescriptor*
- FileDescriptor::FindExtensionByLowercaseName(const string& key) const {
- const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key);
- if (result == NULL || !result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- const FieldDescriptor*
- FileDescriptor::FindExtensionByCamelcaseName(const string& key) const {
- const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key);
- if (result == NULL || !result->is_extension()) {
- return NULL;
- } else {
- return result;
- }
- }
- bool Descriptor::IsExtensionNumber(int number) const {
- // Linear search should be fine because we don't expect a message to have
- // more than a couple extension ranges.
- for (int i = 0; i < extension_range_count(); i++) {
- if (number >= extension_range(i)->start &&
- number < extension_range(i)->end) {
- return true;
- }
- }
- return false;
- }
- // -------------------------------------------------------------------
- bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const {
- if (fallback_database_ == NULL) return false;
- if (tables_->known_bad_files_.count(name) > 0) return false;
- FileDescriptorProto file_proto;
- if (!fallback_database_->FindFileByName(name, &file_proto) ||
- BuildFileFromDatabase(file_proto) == NULL) {
- tables_->known_bad_files_.insert(name);
- return false;
- }
- return true;
- }
- bool DescriptorPool::IsSubSymbolOfBuiltType(const string& name) const {
- string prefix = name;
- for (;;) {
- string::size_type dot_pos = prefix.find_last_of('.');
- if (dot_pos == string::npos) {
- break;
- }
- prefix = prefix.substr(0, dot_pos);
- Symbol symbol = tables_->FindSymbol(prefix);
- // If the symbol type is anything other than PACKAGE, then its complete
- // definition is already known.
- if (!symbol.IsNull() && symbol.type != Symbol::PACKAGE) {
- return true;
- }
- }
- if (underlay_ != NULL) {
- // Check to see if any prefix of this symbol exists in the underlay.
- return underlay_->IsSubSymbolOfBuiltType(name);
- }
- return false;
- }
- bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const {
- if (fallback_database_ == NULL) return false;
- // We skip looking in the fallback database if the name is a sub-symbol of
- // any descriptor that already exists in the descriptor pool (except for
- // package descriptors). This is valid because all symbols except for
- // packages are defined in a single file, so if the symbol exists then we
- // should already have its definition.
- //
- // The other reason to do this is to support "overriding" type definitions
- // by merging two databases that define the same type. (Yes, people do
- // this.) The main difficulty with making this work is that
- // FindFileContainingSymbol() is allowed to return both false positives
- // (e.g., SimpleDescriptorDatabase, UpgradedDescriptorDatabase) and false
- // negatives (e.g. ProtoFileParser, SourceTreeDescriptorDatabase). When two
- // such databases are merged, looking up a non-existent sub-symbol of a type
- // that already exists in the descriptor pool can result in an attempt to
- // load multiple definitions of the same type. The check below avoids this.
- if (IsSubSymbolOfBuiltType(name)) return false;
- FileDescriptorProto file_proto;
- if (!fallback_database_->FindFileContainingSymbol(name, &file_proto)) {
- return false;
- }
- if (tables_->FindFile(file_proto.name()) != NULL) {
- // We've already loaded this file, and it apparently doesn't contain the
- // symbol we're looking for. Some DescriptorDatabases return false
- // positives.
- return false;
- }
- if (BuildFileFromDatabase(file_proto) == NULL) {
- return false;
- }
- return true;
- }
- bool DescriptorPool::TryFindExtensionInFallbackDatabase(
- const Descriptor* containing_type, int field_number) const {
- if (fallback_database_ == NULL) return false;
- FileDescriptorProto file_proto;
- if (!fallback_database_->FindFileContainingExtension(
- containing_type->full_name(), field_number, &file_proto)) {
- return false;
- }
- if (tables_->FindFile(file_proto.name()) != NULL) {
- // We've already loaded this file, and it apparently doesn't contain the
- // extension we're looking for. Some DescriptorDatabases return false
- // positives.
- return false;
- }
- if (BuildFileFromDatabase(file_proto) == NULL) {
- return false;
- }
- return true;
- }
- // ===================================================================
- string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const {
- GOOGLE_CHECK(has_default_value()) << "No default value";
- switch (cpp_type()) {
- case CPPTYPE_INT32:
- return SimpleItoa(default_value_int32());
- break;
- case CPPTYPE_INT64:
- return SimpleItoa(default_value_int64());
- break;
- case CPPTYPE_UINT32:
- return SimpleItoa(default_value_uint32());
- break;
- case CPPTYPE_UINT64:
- return SimpleItoa(default_value_uint64());
- break;
- case CPPTYPE_FLOAT:
- return SimpleFtoa(default_value_float());
- break;
- case CPPTYPE_DOUBLE:
- return SimpleDtoa(default_value_double());
- break;
- case CPPTYPE_BOOL:
- return default_value_bool() ? "true" : "false";
- break;
- case CPPTYPE_STRING:
- if (quote_string_type) {
- return "\"" + CEscape(default_val…
Large files files are truncated, but you can click here to view the full file