temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 9 | // * Redistributions of source code must retain the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer. |
| 11 | // * Redistributions in binary form must reproduce the above |
| 12 | // copyright notice, this list of conditions and the following disclaimer |
| 13 | // in the documentation and/or other materials provided with the |
| 14 | // distribution. |
| 15 | // * Neither the name of Google Inc. nor the names of its |
| 16 | // contributors may be used to endorse or promote products derived from |
| 17 | // this software without specific prior written permission. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 19 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 20 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 21 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 22 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 23 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 24 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 25 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 26 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 27 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 28 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 29 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | |
| 35 | #include <google/protobuf/stubs/hash.h> |
| 36 | #include <map> |
| 37 | #include <set> |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 38 | #include <vector> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 39 | #include <algorithm> |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 40 | #include <limits> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 41 | |
| 42 | #include <google/protobuf/descriptor.h> |
| 43 | #include <google/protobuf/descriptor_database.h> |
| 44 | #include <google/protobuf/descriptor.pb.h> |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 45 | #include <google/protobuf/dynamic_message.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 46 | #include <google/protobuf/text_format.h> |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 47 | #include <google/protobuf/unknown_field_set.h> |
| 48 | #include <google/protobuf/wire_format.h> |
| 49 | #include <google/protobuf/io/coded_stream.h> |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 50 | #include <google/protobuf/io/tokenizer.h> |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 51 | #include <google/protobuf/io/zero_copy_stream_impl.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 52 | #include <google/protobuf/stubs/common.h> |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 53 | #include <google/protobuf/stubs/once.h> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 54 | #include <google/protobuf/stubs/strutil.h> |
| 55 | #include <google/protobuf/stubs/substitute.h> |
| 56 | #include <google/protobuf/stubs/map-util.h> |
| 57 | #include <google/protobuf/stubs/stl_util-inl.h> |
| 58 | |
| 59 | #undef PACKAGE // autoheader #defines this. :( |
| 60 | |
| 61 | namespace google { |
| 62 | namespace protobuf { |
| 63 | |
| 64 | const FieldDescriptor::CppType |
| 65 | FieldDescriptor::kTypeToCppTypeMap[MAX_TYPE + 1] = { |
| 66 | static_cast<CppType>(0), // 0 is reserved for errors |
| 67 | |
| 68 | CPPTYPE_DOUBLE, // TYPE_DOUBLE |
| 69 | CPPTYPE_FLOAT, // TYPE_FLOAT |
| 70 | CPPTYPE_INT64, // TYPE_INT64 |
| 71 | CPPTYPE_UINT64, // TYPE_UINT64 |
| 72 | CPPTYPE_INT32, // TYPE_INT32 |
| 73 | CPPTYPE_UINT64, // TYPE_FIXED64 |
| 74 | CPPTYPE_UINT32, // TYPE_FIXED32 |
| 75 | CPPTYPE_BOOL, // TYPE_BOOL |
| 76 | CPPTYPE_STRING, // TYPE_STRING |
| 77 | CPPTYPE_MESSAGE, // TYPE_GROUP |
| 78 | CPPTYPE_MESSAGE, // TYPE_MESSAGE |
| 79 | CPPTYPE_STRING, // TYPE_BYTES |
| 80 | CPPTYPE_UINT32, // TYPE_UINT32 |
| 81 | CPPTYPE_ENUM, // TYPE_ENUM |
| 82 | CPPTYPE_INT32, // TYPE_SFIXED32 |
| 83 | CPPTYPE_INT64, // TYPE_SFIXED64 |
| 84 | CPPTYPE_INT32, // TYPE_SINT32 |
| 85 | CPPTYPE_INT64, // TYPE_SINT64 |
| 86 | }; |
| 87 | |
| 88 | const char * const FieldDescriptor::kTypeToName[MAX_TYPE + 1] = { |
| 89 | "ERROR", // 0 is reserved for errors |
| 90 | |
| 91 | "double", // TYPE_DOUBLE |
| 92 | "float", // TYPE_FLOAT |
| 93 | "int64", // TYPE_INT64 |
| 94 | "uint64", // TYPE_UINT64 |
| 95 | "int32", // TYPE_INT32 |
| 96 | "fixed64", // TYPE_FIXED64 |
| 97 | "fixed32", // TYPE_FIXED32 |
| 98 | "bool", // TYPE_BOOL |
| 99 | "string", // TYPE_STRING |
| 100 | "group", // TYPE_GROUP |
| 101 | "message", // TYPE_MESSAGE |
| 102 | "bytes", // TYPE_BYTES |
| 103 | "uint32", // TYPE_UINT32 |
| 104 | "enum", // TYPE_ENUM |
| 105 | "sfixed32", // TYPE_SFIXED32 |
| 106 | "sfixed64", // TYPE_SFIXED64 |
| 107 | "sint32", // TYPE_SINT32 |
| 108 | "sint64", // TYPE_SINT64 |
| 109 | }; |
| 110 | |
| 111 | const char * const FieldDescriptor::kLabelToName[MAX_LABEL + 1] = { |
| 112 | "ERROR", // 0 is reserved for errors |
| 113 | |
| 114 | "optional", // LABEL_OPTIONAL |
| 115 | "required", // LABEL_REQUIRED |
| 116 | "repeated", // LABEL_REPEATED |
| 117 | }; |
| 118 | |
| 119 | #ifndef _MSC_VER // MSVC doesn't need these and won't even accept them. |
| 120 | const int FieldDescriptor::kMaxNumber; |
| 121 | const int FieldDescriptor::kFirstReservedNumber; |
| 122 | const int FieldDescriptor::kLastReservedNumber; |
| 123 | #endif |
| 124 | |
| 125 | namespace { |
| 126 | |
| 127 | const string kEmptyString; |
| 128 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 129 | string ToCamelCase(const string& input) { |
| 130 | bool capitalize_next = false; |
| 131 | string result; |
| 132 | result.reserve(input.size()); |
| 133 | |
| 134 | for (int i = 0; i < input.size(); i++) { |
| 135 | if (input[i] == '_') { |
| 136 | capitalize_next = true; |
| 137 | } else if (capitalize_next) { |
| 138 | // Note: I distrust ctype.h due to locales. |
| 139 | if ('a' <= input[i] && input[i] <= 'z') { |
| 140 | result.push_back(input[i] - 'a' + 'A'); |
| 141 | } else { |
| 142 | result.push_back(input[i]); |
| 143 | } |
| 144 | capitalize_next = false; |
| 145 | } else { |
| 146 | result.push_back(input[i]); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Lower-case the first letter. |
| 151 | if (!result.empty() && 'A' <= result[0] && result[0] <= 'Z') { |
| 152 | result[0] = result[0] - 'A' + 'a'; |
| 153 | } |
| 154 | |
| 155 | return result; |
| 156 | } |
| 157 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 158 | // A DescriptorPool contains a bunch of hash_maps to implement the |
| 159 | // various Find*By*() methods. Since hashtable lookups are O(1), it's |
| 160 | // most efficient to construct a fixed set of large hash_maps used by |
| 161 | // all objects in the pool rather than construct one or more small |
| 162 | // hash_maps for each object. |
| 163 | // |
| 164 | // The keys to these hash_maps are (parent, name) or (parent, number) |
| 165 | // pairs. Unfortunately STL doesn't provide hash functions for pair<>, |
| 166 | // so we must invent our own. |
| 167 | // |
| 168 | // TODO(kenton): Use StringPiece rather than const char* in keys? It would |
| 169 | // be a lot cleaner but we'd just have to convert it back to const char* |
| 170 | // for the open source release. |
| 171 | |
| 172 | typedef pair<const void*, const char*> PointerStringPair; |
| 173 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 174 | struct PointerStringPairEqual { |
| 175 | inline bool operator()(const PointerStringPair& a, |
| 176 | const PointerStringPair& b) const { |
| 177 | return a.first == b.first && strcmp(a.second, b.second) == 0; |
| 178 | } |
| 179 | }; |
| 180 | |
| 181 | template<typename PairType> |
| 182 | struct PointerIntegerPairHash { |
| 183 | size_t operator()(const PairType& p) const { |
| 184 | // FIXME(kenton): What is the best way to compute this hash? I have |
| 185 | // no idea! This seems a bit better than an XOR. |
| 186 | return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + p.second; |
| 187 | } |
| 188 | |
| 189 | // Used only by MSVC and platforms where hash_map is not available. |
| 190 | static const size_t bucket_size = 4; |
| 191 | static const size_t min_buckets = 8; |
| 192 | inline bool operator()(const PairType& a, const PairType& b) const { |
| 193 | return a.first < b.first || |
| 194 | (a.first == b.first && a.second < b.second); |
| 195 | } |
| 196 | }; |
| 197 | |
| 198 | typedef pair<const Descriptor*, int> DescriptorIntPair; |
| 199 | typedef pair<const EnumDescriptor*, int> EnumIntPair; |
| 200 | |
| 201 | struct PointerStringPairHash { |
| 202 | size_t operator()(const PointerStringPair& p) const { |
| 203 | // FIXME(kenton): What is the best way to compute this hash? I have |
| 204 | // no idea! This seems a bit better than an XOR. |
| 205 | hash<const char*> cstring_hash; |
| 206 | return reinterpret_cast<intptr_t>(p.first) * ((1 << 16) - 1) + |
| 207 | cstring_hash(p.second); |
| 208 | } |
| 209 | |
| 210 | // Used only by MSVC and platforms where hash_map is not available. |
| 211 | static const size_t bucket_size = 4; |
| 212 | static const size_t min_buckets = 8; |
| 213 | inline bool operator()(const PointerStringPair& a, |
| 214 | const PointerStringPair& b) const { |
| 215 | if (a.first < b.first) return true; |
| 216 | if (a.first > b.first) return false; |
| 217 | return strcmp(a.second, b.second) < 0; |
| 218 | } |
| 219 | }; |
| 220 | |
| 221 | |
| 222 | struct Symbol { |
| 223 | enum Type { |
| 224 | NULL_SYMBOL, MESSAGE, FIELD, ENUM, ENUM_VALUE, SERVICE, METHOD, PACKAGE |
| 225 | }; |
| 226 | Type type; |
| 227 | union { |
| 228 | const Descriptor* descriptor; |
| 229 | const FieldDescriptor* field_descriptor; |
| 230 | const EnumDescriptor* enum_descriptor; |
| 231 | const EnumValueDescriptor* enum_value_descriptor; |
| 232 | const ServiceDescriptor* service_descriptor; |
| 233 | const MethodDescriptor* method_descriptor; |
| 234 | const FileDescriptor* package_file_descriptor; |
| 235 | }; |
| 236 | |
| 237 | inline Symbol() : type(NULL_SYMBOL) { descriptor = NULL; } |
| 238 | inline bool IsNull() const { return type == NULL_SYMBOL; } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 239 | inline bool IsType() const { |
| 240 | return type == MESSAGE || type == ENUM; |
| 241 | } |
| 242 | inline bool IsAggregate() const { |
| 243 | return type == MESSAGE || type == PACKAGE |
| 244 | || type == ENUM || type == SERVICE; |
| 245 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 246 | |
| 247 | #define CONSTRUCTOR(TYPE, TYPE_CONSTANT, FIELD) \ |
| 248 | inline explicit Symbol(const TYPE* value) { \ |
| 249 | type = TYPE_CONSTANT; \ |
| 250 | this->FIELD = value; \ |
| 251 | } |
| 252 | |
| 253 | CONSTRUCTOR(Descriptor , MESSAGE , descriptor ) |
| 254 | CONSTRUCTOR(FieldDescriptor , FIELD , field_descriptor ) |
| 255 | CONSTRUCTOR(EnumDescriptor , ENUM , enum_descriptor ) |
| 256 | CONSTRUCTOR(EnumValueDescriptor, ENUM_VALUE, enum_value_descriptor ) |
| 257 | CONSTRUCTOR(ServiceDescriptor , SERVICE , service_descriptor ) |
| 258 | CONSTRUCTOR(MethodDescriptor , METHOD , method_descriptor ) |
| 259 | CONSTRUCTOR(FileDescriptor , PACKAGE , package_file_descriptor) |
| 260 | #undef CONSTRUCTOR |
| 261 | |
| 262 | const FileDescriptor* GetFile() const { |
| 263 | switch (type) { |
| 264 | case NULL_SYMBOL: return NULL; |
| 265 | case MESSAGE : return descriptor ->file(); |
| 266 | case FIELD : return field_descriptor ->file(); |
| 267 | case ENUM : return enum_descriptor ->file(); |
| 268 | case ENUM_VALUE : return enum_value_descriptor->type()->file(); |
| 269 | case SERVICE : return service_descriptor ->file(); |
| 270 | case METHOD : return method_descriptor ->service()->file(); |
| 271 | case PACKAGE : return package_file_descriptor; |
| 272 | } |
| 273 | return NULL; |
| 274 | } |
| 275 | }; |
| 276 | |
| 277 | const Symbol kNullSymbol; |
| 278 | |
| 279 | typedef hash_map<const char*, Symbol, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 280 | hash<const char*>, streq> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 281 | SymbolsByNameMap; |
| 282 | typedef hash_map<PointerStringPair, Symbol, |
| 283 | PointerStringPairHash, PointerStringPairEqual> |
| 284 | SymbolsByParentMap; |
| 285 | typedef hash_map<const char*, const FileDescriptor*, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 286 | hash<const char*>, streq> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 287 | FilesByNameMap; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 288 | typedef hash_map<PointerStringPair, const FieldDescriptor*, |
| 289 | PointerStringPairHash, PointerStringPairEqual> |
| 290 | FieldsByNameMap; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 291 | typedef hash_map<DescriptorIntPair, const FieldDescriptor*, |
| 292 | PointerIntegerPairHash<DescriptorIntPair> > |
| 293 | FieldsByNumberMap; |
| 294 | typedef hash_map<EnumIntPair, const EnumValueDescriptor*, |
| 295 | PointerIntegerPairHash<EnumIntPair> > |
| 296 | EnumValuesByNumberMap; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 297 | // This is a map rather than a hash_map, since we use it to iterate |
| 298 | // through all the extensions that extend a given Descriptor, and an |
| 299 | // ordered data structure that implements lower_bound is convenient |
| 300 | // for that. |
| 301 | typedef map<DescriptorIntPair, const FieldDescriptor*> |
| 302 | ExtensionsGroupedByDescriptorMap; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 303 | |
| 304 | } // anonymous namespace |
| 305 | |
| 306 | // =================================================================== |
| 307 | // DescriptorPool::Tables |
| 308 | |
| 309 | class DescriptorPool::Tables { |
| 310 | public: |
| 311 | Tables(); |
| 312 | ~Tables(); |
| 313 | |
| 314 | // Checkpoint the state of the tables. Future calls to Rollback() will |
| 315 | // return the Tables to this state. This is used when building files, since |
| 316 | // some kinds of validation errors cannot be detected until the file's |
| 317 | // descriptors have already been added to the tables. BuildFile() calls |
| 318 | // Checkpoint() before it starts building and Rollback() if it encounters |
| 319 | // an error. |
| 320 | void Checkpoint(); |
| 321 | |
| 322 | // Roll back the Tables to the state of the last Checkpoint(), removing |
| 323 | // everything that was added after that point. |
| 324 | void Rollback(); |
| 325 | |
| 326 | // The stack of files which are currently being built. Used to detect |
| 327 | // cyclic dependencies when loading files from a DescriptorDatabase. Not |
| 328 | // used when fallback_database_ == NULL. |
| 329 | vector<string> pending_files_; |
| 330 | |
| 331 | // A set of files which we have tried to load from the fallback database |
| 332 | // and encountered errors. We will not attempt to load them again. |
| 333 | // Not used when fallback_database_ == NULL. |
| 334 | hash_set<string> known_bad_files_; |
| 335 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 336 | // The set of descriptors for which we've already loaded the full |
| 337 | // set of extensions numbers from fallback_database_. |
| 338 | hash_set<const Descriptor*> extensions_loaded_from_db_; |
| 339 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 340 | // ----------------------------------------------------------------- |
| 341 | // Finding items. |
| 342 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 343 | // Find symbols. This returns a null Symbol (symbol.IsNull() is true) |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 344 | // if not found. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 345 | inline Symbol FindSymbol(const string& key) const; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 346 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 347 | // This implements the body of DescriptorPool::Find*ByName(). It should |
| 348 | // really be a private method of DescriptorPool, but that would require |
| 349 | // declaring Symbol in descriptor.h, which would drag all kinds of other |
| 350 | // stuff into the header. Yay C++. |
| 351 | Symbol FindByNameHelper( |
| 352 | const DescriptorPool* pool, const string& name) const; |
| 353 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 354 | // These return NULL if not found. |
| 355 | inline const FileDescriptor* FindFile(const string& key) const; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 356 | inline const FieldDescriptor* FindExtension(const Descriptor* extendee, |
| 357 | int number); |
| 358 | inline void FindAllExtensions(const Descriptor* extendee, |
| 359 | vector<const FieldDescriptor*>* out) const; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 360 | |
| 361 | // ----------------------------------------------------------------- |
| 362 | // Adding items. |
| 363 | |
| 364 | // These add items to the corresponding tables. They return false if |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 365 | // the key already exists in the table. For AddSymbol(), the string passed |
| 366 | // in must be one that was constructed using AllocateString(), as it will |
| 367 | // be used as a key in the symbols_by_name_ map without copying. |
| 368 | bool AddSymbol(const string& full_name, Symbol symbol); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 369 | bool AddFile(const FileDescriptor* file); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 370 | bool AddExtension(const FieldDescriptor* field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 371 | |
| 372 | // ----------------------------------------------------------------- |
| 373 | // Allocating memory. |
| 374 | |
| 375 | // Allocate an object which will be reclaimed when the pool is |
| 376 | // destroyed. Note that the object's destructor will never be called, |
| 377 | // so its fields must be plain old data (primitive data types and |
| 378 | // pointers). All of the descriptor types are such objects. |
| 379 | template<typename Type> Type* Allocate(); |
| 380 | |
| 381 | // Allocate an array of objects which will be reclaimed when the |
| 382 | // pool in destroyed. Again, destructors are never called. |
| 383 | template<typename Type> Type* AllocateArray(int count); |
| 384 | |
| 385 | // Allocate a string which will be destroyed when the pool is destroyed. |
| 386 | // The string is initialized to the given value for convenience. |
| 387 | string* AllocateString(const string& value); |
| 388 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 389 | // Allocate a protocol message object. Some older versions of GCC have |
| 390 | // trouble understanding explicit template instantiations in some cases, so |
| 391 | // in those cases we have to pass a dummy pointer of the right type as the |
| 392 | // parameter instead of specifying the type explicitly. |
| 393 | template<typename Type> Type* AllocateMessage(Type* dummy = NULL); |
| 394 | |
| 395 | // Allocate a FileDescriptorTables object. |
| 396 | FileDescriptorTables* AllocateFileTables(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 397 | |
| 398 | private: |
| 399 | vector<string*> strings_; // All strings in the pool. |
| 400 | vector<Message*> messages_; // All messages in the pool. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 401 | vector<FileDescriptorTables*> file_tables_; // All file tables in the pool. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 402 | vector<void*> allocations_; // All other memory allocated in the pool. |
| 403 | |
| 404 | SymbolsByNameMap symbols_by_name_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 405 | FilesByNameMap files_by_name_; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 406 | ExtensionsGroupedByDescriptorMap extensions_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 407 | |
| 408 | int strings_before_checkpoint_; |
| 409 | int messages_before_checkpoint_; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 410 | int file_tables_before_checkpoint_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 411 | int allocations_before_checkpoint_; |
| 412 | vector<const char* > symbols_after_checkpoint_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 413 | vector<const char* > files_after_checkpoint_; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 414 | vector<DescriptorIntPair> extensions_after_checkpoint_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 415 | |
| 416 | // Allocate some bytes which will be reclaimed when the pool is |
| 417 | // destroyed. |
| 418 | void* AllocateBytes(int size); |
| 419 | }; |
| 420 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 421 | // Contains tables specific to a particular file. These tables are not |
| 422 | // modified once the file has been constructed, so they need not be |
| 423 | // protected by a mutex. This makes operations that depend only on the |
| 424 | // contents of a single file -- e.g. Descriptor::FindFieldByName() -- |
| 425 | // lock-free. |
| 426 | // |
| 427 | // For historical reasons, the definitions of the methods of |
| 428 | // FileDescriptorTables and DescriptorPool::Tables are interleaved below. |
| 429 | // These used to be a single class. |
| 430 | class FileDescriptorTables { |
| 431 | public: |
| 432 | FileDescriptorTables(); |
| 433 | ~FileDescriptorTables(); |
| 434 | |
| 435 | // Empty table, used with placeholder files. |
| 436 | static const FileDescriptorTables kEmpty; |
| 437 | |
| 438 | // ----------------------------------------------------------------- |
| 439 | // Finding items. |
| 440 | |
| 441 | // Find symbols. These return a null Symbol (symbol.IsNull() is true) |
| 442 | // if not found. |
| 443 | inline Symbol FindNestedSymbol(const void* parent, |
| 444 | const string& name) const; |
| 445 | inline Symbol FindNestedSymbolOfType(const void* parent, |
| 446 | const string& name, |
| 447 | const Symbol::Type type) const; |
| 448 | |
| 449 | // These return NULL if not found. |
| 450 | inline const FieldDescriptor* FindFieldByNumber( |
| 451 | const Descriptor* parent, int number) const; |
| 452 | inline const FieldDescriptor* FindFieldByLowercaseName( |
| 453 | const void* parent, const string& lowercase_name) const; |
| 454 | inline const FieldDescriptor* FindFieldByCamelcaseName( |
| 455 | const void* parent, const string& camelcase_name) const; |
| 456 | inline const EnumValueDescriptor* FindEnumValueByNumber( |
| 457 | const EnumDescriptor* parent, int number) const; |
| 458 | |
| 459 | // ----------------------------------------------------------------- |
| 460 | // Adding items. |
| 461 | |
| 462 | // These add items to the corresponding tables. They return false if |
| 463 | // the key already exists in the table. For AddAliasUnderParent(), the |
| 464 | // string passed in must be one that was constructed using AllocateString(), |
| 465 | // as it will be used as a key in the symbols_by_parent_ map without copying. |
| 466 | bool AddAliasUnderParent(const void* parent, const string& name, |
| 467 | Symbol symbol); |
| 468 | bool AddFieldByNumber(const FieldDescriptor* field); |
| 469 | bool AddEnumValueByNumber(const EnumValueDescriptor* value); |
| 470 | |
| 471 | // Adds the field to the lowercase_name and camelcase_name maps. Never |
| 472 | // fails because we allow duplicates; the first field by the name wins. |
| 473 | void AddFieldByStylizedNames(const FieldDescriptor* field); |
| 474 | |
| 475 | private: |
| 476 | SymbolsByParentMap symbols_by_parent_; |
| 477 | FieldsByNameMap fields_by_lowercase_name_; |
| 478 | FieldsByNameMap fields_by_camelcase_name_; |
| 479 | FieldsByNumberMap fields_by_number_; // Not including extensions. |
| 480 | EnumValuesByNumberMap enum_values_by_number_; |
| 481 | }; |
| 482 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 483 | DescriptorPool::Tables::Tables() |
| 484 | : strings_before_checkpoint_(0), |
| 485 | messages_before_checkpoint_(0), |
| 486 | allocations_before_checkpoint_(0) {} |
| 487 | |
| 488 | DescriptorPool::Tables::~Tables() { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 489 | // Note that the deletion order is important, since the destructors of some |
| 490 | // messages may refer to objects in allocations_. |
| 491 | STLDeleteElements(&messages_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 492 | for (int i = 0; i < allocations_.size(); i++) { |
| 493 | operator delete(allocations_[i]); |
| 494 | } |
| 495 | STLDeleteElements(&strings_); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 496 | STLDeleteElements(&file_tables_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 497 | } |
| 498 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 499 | FileDescriptorTables::FileDescriptorTables() {} |
| 500 | FileDescriptorTables::~FileDescriptorTables() {} |
| 501 | |
| 502 | const FileDescriptorTables FileDescriptorTables::kEmpty; |
| 503 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 504 | void DescriptorPool::Tables::Checkpoint() { |
| 505 | strings_before_checkpoint_ = strings_.size(); |
| 506 | messages_before_checkpoint_ = messages_.size(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 507 | file_tables_before_checkpoint_ = file_tables_.size(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 508 | allocations_before_checkpoint_ = allocations_.size(); |
| 509 | |
| 510 | symbols_after_checkpoint_.clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 511 | files_after_checkpoint_.clear(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 512 | extensions_after_checkpoint_.clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 513 | } |
| 514 | |
| 515 | void DescriptorPool::Tables::Rollback() { |
| 516 | for (int i = 0; i < symbols_after_checkpoint_.size(); i++) { |
| 517 | symbols_by_name_.erase(symbols_after_checkpoint_[i]); |
| 518 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 519 | for (int i = 0; i < files_after_checkpoint_.size(); i++) { |
| 520 | files_by_name_.erase(files_after_checkpoint_[i]); |
| 521 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 522 | for (int i = 0; i < extensions_after_checkpoint_.size(); i++) { |
| 523 | extensions_.erase(extensions_after_checkpoint_[i]); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | symbols_after_checkpoint_.clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 527 | files_after_checkpoint_.clear(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 528 | extensions_after_checkpoint_.clear(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 529 | |
| 530 | STLDeleteContainerPointers( |
| 531 | strings_.begin() + strings_before_checkpoint_, strings_.end()); |
| 532 | STLDeleteContainerPointers( |
| 533 | messages_.begin() + messages_before_checkpoint_, messages_.end()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 534 | STLDeleteContainerPointers( |
| 535 | file_tables_.begin() + file_tables_before_checkpoint_, file_tables_.end()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 536 | for (int i = allocations_before_checkpoint_; i < allocations_.size(); i++) { |
| 537 | operator delete(allocations_[i]); |
| 538 | } |
| 539 | |
| 540 | strings_.resize(strings_before_checkpoint_); |
| 541 | messages_.resize(messages_before_checkpoint_); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 542 | file_tables_.resize(file_tables_before_checkpoint_); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 543 | allocations_.resize(allocations_before_checkpoint_); |
| 544 | } |
| 545 | |
| 546 | // ------------------------------------------------------------------- |
| 547 | |
| 548 | inline Symbol DescriptorPool::Tables::FindSymbol(const string& key) const { |
| 549 | const Symbol* result = FindOrNull(symbols_by_name_, key.c_str()); |
| 550 | if (result == NULL) { |
| 551 | return kNullSymbol; |
| 552 | } else { |
| 553 | return *result; |
| 554 | } |
| 555 | } |
| 556 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 557 | inline Symbol FileDescriptorTables::FindNestedSymbol( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 558 | const void* parent, const string& name) const { |
| 559 | const Symbol* result = |
| 560 | FindOrNull(symbols_by_parent_, PointerStringPair(parent, name.c_str())); |
| 561 | if (result == NULL) { |
| 562 | return kNullSymbol; |
| 563 | } else { |
| 564 | return *result; |
| 565 | } |
| 566 | } |
| 567 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 568 | inline Symbol FileDescriptorTables::FindNestedSymbolOfType( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 569 | const void* parent, const string& name, const Symbol::Type type) const { |
| 570 | Symbol result = FindNestedSymbol(parent, name); |
| 571 | if (result.type != type) return kNullSymbol; |
| 572 | return result; |
| 573 | } |
| 574 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 575 | Symbol DescriptorPool::Tables::FindByNameHelper( |
| 576 | const DescriptorPool* pool, const string& name) const { |
| 577 | MutexLockMaybe lock(pool->mutex_); |
| 578 | Symbol result = FindSymbol(name); |
| 579 | |
| 580 | if (result.IsNull() && pool->underlay_ != NULL) { |
| 581 | // Symbol not found; check the underlay. |
| 582 | result = |
| 583 | pool->underlay_->tables_->FindByNameHelper(pool->underlay_, name); |
| 584 | } |
| 585 | |
| 586 | if (result.IsNull()) { |
| 587 | // Symbol still not found, so check fallback database. |
| 588 | if (pool->TryFindSymbolInFallbackDatabase(name)) { |
| 589 | result = FindSymbol(name); |
| 590 | } |
| 591 | } |
| 592 | |
| 593 | return result; |
| 594 | } |
| 595 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 596 | inline const FileDescriptor* DescriptorPool::Tables::FindFile( |
| 597 | const string& key) const { |
| 598 | return FindPtrOrNull(files_by_name_, key.c_str()); |
| 599 | } |
| 600 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 601 | inline const FieldDescriptor* FileDescriptorTables::FindFieldByNumber( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 602 | const Descriptor* parent, int number) const { |
| 603 | return FindPtrOrNull(fields_by_number_, make_pair(parent, number)); |
| 604 | } |
| 605 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 606 | inline const FieldDescriptor* FileDescriptorTables::FindFieldByLowercaseName( |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 607 | const void* parent, const string& lowercase_name) const { |
| 608 | return FindPtrOrNull(fields_by_lowercase_name_, |
| 609 | PointerStringPair(parent, lowercase_name.c_str())); |
| 610 | } |
| 611 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 612 | inline const FieldDescriptor* FileDescriptorTables::FindFieldByCamelcaseName( |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 613 | const void* parent, const string& camelcase_name) const { |
| 614 | return FindPtrOrNull(fields_by_camelcase_name_, |
| 615 | PointerStringPair(parent, camelcase_name.c_str())); |
| 616 | } |
| 617 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 618 | inline const EnumValueDescriptor* FileDescriptorTables::FindEnumValueByNumber( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 619 | const EnumDescriptor* parent, int number) const { |
| 620 | return FindPtrOrNull(enum_values_by_number_, make_pair(parent, number)); |
| 621 | } |
| 622 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 623 | inline const FieldDescriptor* DescriptorPool::Tables::FindExtension( |
| 624 | const Descriptor* extendee, int number) { |
| 625 | return FindPtrOrNull(extensions_, make_pair(extendee, number)); |
| 626 | } |
| 627 | |
| 628 | inline void DescriptorPool::Tables::FindAllExtensions( |
| 629 | const Descriptor* extendee, vector<const FieldDescriptor*>* out) const { |
| 630 | ExtensionsGroupedByDescriptorMap::const_iterator it = |
| 631 | extensions_.lower_bound(make_pair(extendee, 0)); |
| 632 | for (; it != extensions_.end() && it->first.first == extendee; ++it) { |
| 633 | out->push_back(it->second); |
| 634 | } |
| 635 | } |
| 636 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 637 | // ------------------------------------------------------------------- |
| 638 | |
| 639 | bool DescriptorPool::Tables::AddSymbol( |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 640 | const string& full_name, Symbol symbol) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 641 | if (InsertIfNotPresent(&symbols_by_name_, full_name.c_str(), symbol)) { |
| 642 | symbols_after_checkpoint_.push_back(full_name.c_str()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 643 | return true; |
| 644 | } else { |
| 645 | return false; |
| 646 | } |
| 647 | } |
| 648 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 649 | bool FileDescriptorTables::AddAliasUnderParent( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 650 | const void* parent, const string& name, Symbol symbol) { |
| 651 | PointerStringPair by_parent_key(parent, name.c_str()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 652 | return InsertIfNotPresent(&symbols_by_parent_, by_parent_key, symbol); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | bool DescriptorPool::Tables::AddFile(const FileDescriptor* file) { |
| 656 | if (InsertIfNotPresent(&files_by_name_, file->name().c_str(), file)) { |
| 657 | files_after_checkpoint_.push_back(file->name().c_str()); |
| 658 | return true; |
| 659 | } else { |
| 660 | return false; |
| 661 | } |
| 662 | } |
| 663 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 664 | void FileDescriptorTables::AddFieldByStylizedNames( |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 665 | const FieldDescriptor* field) { |
| 666 | const void* parent; |
| 667 | if (field->is_extension()) { |
| 668 | if (field->extension_scope() == NULL) { |
| 669 | parent = field->file(); |
| 670 | } else { |
| 671 | parent = field->extension_scope(); |
| 672 | } |
| 673 | } else { |
| 674 | parent = field->containing_type(); |
| 675 | } |
| 676 | |
| 677 | PointerStringPair lowercase_key(parent, field->lowercase_name().c_str()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 678 | InsertIfNotPresent(&fields_by_lowercase_name_, lowercase_key, field); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 679 | |
| 680 | PointerStringPair camelcase_key(parent, field->camelcase_name().c_str()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 681 | InsertIfNotPresent(&fields_by_camelcase_name_, camelcase_key, field); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 682 | } |
| 683 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 684 | bool FileDescriptorTables::AddFieldByNumber(const FieldDescriptor* field) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 685 | DescriptorIntPair key(field->containing_type(), field->number()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 686 | return InsertIfNotPresent(&fields_by_number_, key, field); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 687 | } |
| 688 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 689 | bool FileDescriptorTables::AddEnumValueByNumber( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 690 | const EnumValueDescriptor* value) { |
| 691 | EnumIntPair key(value->type(), value->number()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 692 | return InsertIfNotPresent(&enum_values_by_number_, key, value); |
| 693 | } |
| 694 | |
| 695 | bool DescriptorPool::Tables::AddExtension(const FieldDescriptor* field) { |
| 696 | DescriptorIntPair key(field->containing_type(), field->number()); |
| 697 | if (InsertIfNotPresent(&extensions_, key, field)) { |
| 698 | extensions_after_checkpoint_.push_back(key); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 699 | return true; |
| 700 | } else { |
| 701 | return false; |
| 702 | } |
| 703 | } |
| 704 | |
| 705 | // ------------------------------------------------------------------- |
| 706 | |
| 707 | template<typename Type> |
| 708 | Type* DescriptorPool::Tables::Allocate() { |
| 709 | return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type))); |
| 710 | } |
| 711 | |
| 712 | template<typename Type> |
| 713 | Type* DescriptorPool::Tables::AllocateArray(int count) { |
| 714 | return reinterpret_cast<Type*>(AllocateBytes(sizeof(Type) * count)); |
| 715 | } |
| 716 | |
| 717 | string* DescriptorPool::Tables::AllocateString(const string& value) { |
| 718 | string* result = new string(value); |
| 719 | strings_.push_back(result); |
| 720 | return result; |
| 721 | } |
| 722 | |
| 723 | template<typename Type> |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 724 | Type* DescriptorPool::Tables::AllocateMessage(Type* dummy) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 725 | Type* result = new Type; |
| 726 | messages_.push_back(result); |
| 727 | return result; |
| 728 | } |
| 729 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 730 | FileDescriptorTables* DescriptorPool::Tables::AllocateFileTables() { |
| 731 | FileDescriptorTables* result = new FileDescriptorTables; |
| 732 | file_tables_.push_back(result); |
| 733 | return result; |
| 734 | } |
| 735 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 736 | void* DescriptorPool::Tables::AllocateBytes(int size) { |
| 737 | // TODO(kenton): Would it be worthwhile to implement this in some more |
| 738 | // sophisticated way? Probably not for the open source release, but for |
| 739 | // internal use we could easily plug in one of our existing memory pool |
| 740 | // allocators... |
| 741 | if (size == 0) return NULL; |
| 742 | |
| 743 | void* result = operator new(size); |
| 744 | allocations_.push_back(result); |
| 745 | return result; |
| 746 | } |
| 747 | |
| 748 | // =================================================================== |
| 749 | // DescriptorPool |
| 750 | |
| 751 | DescriptorPool::ErrorCollector::~ErrorCollector() {} |
| 752 | |
| 753 | DescriptorPool::DescriptorPool() |
| 754 | : mutex_(NULL), |
| 755 | fallback_database_(NULL), |
| 756 | default_error_collector_(NULL), |
| 757 | underlay_(NULL), |
| 758 | tables_(new Tables), |
| 759 | enforce_dependencies_(true), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 760 | allow_unknown_(false) {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 761 | |
| 762 | DescriptorPool::DescriptorPool(DescriptorDatabase* fallback_database, |
| 763 | ErrorCollector* error_collector) |
| 764 | : mutex_(new Mutex), |
| 765 | fallback_database_(fallback_database), |
| 766 | default_error_collector_(error_collector), |
| 767 | underlay_(NULL), |
| 768 | tables_(new Tables), |
| 769 | enforce_dependencies_(true), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 770 | allow_unknown_(false) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 771 | } |
| 772 | |
| 773 | DescriptorPool::DescriptorPool(const DescriptorPool* underlay) |
| 774 | : mutex_(NULL), |
| 775 | fallback_database_(NULL), |
| 776 | default_error_collector_(NULL), |
| 777 | underlay_(underlay), |
| 778 | tables_(new Tables), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 779 | enforce_dependencies_(true), |
| 780 | allow_unknown_(false) {} |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 781 | |
| 782 | DescriptorPool::~DescriptorPool() { |
| 783 | if (mutex_ != NULL) delete mutex_; |
| 784 | } |
| 785 | |
| 786 | // DescriptorPool::BuildFile() defined later. |
| 787 | // DescriptorPool::BuildFileCollectingErrors() defined later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 788 | |
| 789 | void DescriptorPool::InternalDontEnforceDependencies() { |
| 790 | enforce_dependencies_ = false; |
| 791 | } |
| 792 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 793 | bool DescriptorPool::InternalIsFileLoaded(const string& filename) const { |
| 794 | MutexLockMaybe lock(mutex_); |
| 795 | return tables_->FindFile(filename) != NULL; |
| 796 | } |
| 797 | |
| 798 | // generated_pool ==================================================== |
| 799 | |
| 800 | namespace { |
| 801 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 802 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 803 | EncodedDescriptorDatabase* generated_database_ = NULL; |
| 804 | DescriptorPool* generated_pool_ = NULL; |
temporal | bdbb863 | 2009-12-18 08:21:00 +0000 | [diff] [blame] | 805 | GOOGLE_PROTOBUF_DECLARE_ONCE(generated_pool_init_); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 806 | |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 807 | void DeleteGeneratedPool() { |
| 808 | delete generated_database_; |
| 809 | generated_database_ = NULL; |
| 810 | delete generated_pool_; |
| 811 | generated_pool_ = NULL; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 812 | } |
| 813 | |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 814 | void InitGeneratedPool() { |
| 815 | generated_database_ = new EncodedDescriptorDatabase; |
| 816 | generated_pool_ = new DescriptorPool(generated_database_); |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 817 | |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 818 | internal::OnShutdown(&DeleteGeneratedPool); |
| 819 | } |
| 820 | |
| 821 | inline void InitGeneratedPoolOnce() { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 822 | ::google::protobuf::GoogleOnceInit(&generated_pool_init_, &InitGeneratedPool); |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 823 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 824 | |
| 825 | } // anonymous namespace |
| 826 | |
| 827 | const DescriptorPool* DescriptorPool::generated_pool() { |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 828 | InitGeneratedPoolOnce(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 829 | return generated_pool_; |
| 830 | } |
| 831 | |
| 832 | DescriptorPool* DescriptorPool::internal_generated_pool() { |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 833 | InitGeneratedPoolOnce(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 834 | return generated_pool_; |
| 835 | } |
| 836 | |
| 837 | void DescriptorPool::InternalAddGeneratedFile( |
| 838 | const void* encoded_file_descriptor, int size) { |
| 839 | // So, this function is called in the process of initializing the |
| 840 | // descriptors for generated proto classes. Each generated .pb.cc file |
| 841 | // has an internal procedure called AddDescriptors() which is called at |
| 842 | // process startup, and that function calls this one in order to register |
| 843 | // the raw bytes of the FileDescriptorProto representing the file. |
| 844 | // |
| 845 | // We do not actually construct the descriptor objects right away. We just |
| 846 | // hang on to the bytes until they are actually needed. We actually construct |
| 847 | // the descriptor the first time one of the following things happens: |
| 848 | // * Someone calls a method like descriptor(), GetDescriptor(), or |
| 849 | // GetReflection() on the generated types, which requires returning the |
| 850 | // descriptor or an object based on it. |
| 851 | // * Someone looks up the descriptor in DescriptorPool::generated_pool(). |
| 852 | // |
| 853 | // Once one of these happens, the DescriptorPool actually parses the |
| 854 | // FileDescriptorProto and generates a FileDescriptor (and all its children) |
| 855 | // based on it. |
| 856 | // |
| 857 | // Note that FileDescriptorProto is itself a generated protocol message. |
| 858 | // Therefore, when we parse one, we have to be very careful to avoid using |
| 859 | // any descriptor-based operations, since this might cause infinite recursion |
| 860 | // or deadlock. |
kenton@google.com | 63e646b | 2009-05-06 19:27:03 +0000 | [diff] [blame] | 861 | InitGeneratedPoolOnce(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 862 | GOOGLE_CHECK(generated_database_->Add(encoded_file_descriptor, size)); |
| 863 | } |
| 864 | |
| 865 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 866 | // Find*By* methods ================================================== |
| 867 | |
| 868 | // TODO(kenton): There's a lot of repeated code here, but I'm not sure if |
| 869 | // there's any good way to factor it out. Think about this some time when |
| 870 | // there's nothing more important to do (read: never). |
| 871 | |
| 872 | const FileDescriptor* DescriptorPool::FindFileByName(const string& name) const { |
| 873 | MutexLockMaybe lock(mutex_); |
| 874 | const FileDescriptor* result = tables_->FindFile(name); |
| 875 | if (result != NULL) return result; |
| 876 | if (underlay_ != NULL) { |
| 877 | const FileDescriptor* result = underlay_->FindFileByName(name); |
| 878 | if (result != NULL) return result; |
| 879 | } |
| 880 | if (TryFindFileInFallbackDatabase(name)) { |
| 881 | const FileDescriptor* result = tables_->FindFile(name); |
| 882 | if (result != NULL) return result; |
| 883 | } |
| 884 | return NULL; |
| 885 | } |
| 886 | |
| 887 | const FileDescriptor* DescriptorPool::FindFileContainingSymbol( |
| 888 | const string& symbol_name) const { |
| 889 | MutexLockMaybe lock(mutex_); |
| 890 | Symbol result = tables_->FindSymbol(symbol_name); |
| 891 | if (!result.IsNull()) return result.GetFile(); |
| 892 | if (underlay_ != NULL) { |
| 893 | const FileDescriptor* result = |
| 894 | underlay_->FindFileContainingSymbol(symbol_name); |
| 895 | if (result != NULL) return result; |
| 896 | } |
| 897 | if (TryFindSymbolInFallbackDatabase(symbol_name)) { |
| 898 | Symbol result = tables_->FindSymbol(symbol_name); |
| 899 | if (!result.IsNull()) return result.GetFile(); |
| 900 | } |
| 901 | return NULL; |
| 902 | } |
| 903 | |
| 904 | const Descriptor* DescriptorPool::FindMessageTypeByName( |
| 905 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 906 | Symbol result = tables_->FindByNameHelper(this, name); |
| 907 | return (result.type == Symbol::MESSAGE) ? result.descriptor : NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 908 | } |
| 909 | |
| 910 | const FieldDescriptor* DescriptorPool::FindFieldByName( |
| 911 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 912 | Symbol result = tables_->FindByNameHelper(this, name); |
| 913 | if (result.type == Symbol::FIELD && |
| 914 | !result.field_descriptor->is_extension()) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 915 | return result.field_descriptor; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 916 | } else { |
| 917 | return NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 918 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 919 | } |
| 920 | |
| 921 | const FieldDescriptor* DescriptorPool::FindExtensionByName( |
| 922 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 923 | Symbol result = tables_->FindByNameHelper(this, name); |
| 924 | if (result.type == Symbol::FIELD && |
| 925 | result.field_descriptor->is_extension()) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 926 | return result.field_descriptor; |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 927 | } else { |
| 928 | return NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 929 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | const EnumDescriptor* DescriptorPool::FindEnumTypeByName( |
| 933 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 934 | Symbol result = tables_->FindByNameHelper(this, name); |
| 935 | return (result.type == Symbol::ENUM) ? result.enum_descriptor : NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | const EnumValueDescriptor* DescriptorPool::FindEnumValueByName( |
| 939 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 940 | Symbol result = tables_->FindByNameHelper(this, name); |
| 941 | return (result.type == Symbol::ENUM_VALUE) ? |
| 942 | result.enum_value_descriptor : NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 943 | } |
| 944 | |
| 945 | const ServiceDescriptor* DescriptorPool::FindServiceByName( |
| 946 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 947 | Symbol result = tables_->FindByNameHelper(this, name); |
| 948 | return (result.type == Symbol::SERVICE) ? result.service_descriptor : NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 949 | } |
| 950 | |
| 951 | const MethodDescriptor* DescriptorPool::FindMethodByName( |
| 952 | const string& name) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 953 | Symbol result = tables_->FindByNameHelper(this, name); |
| 954 | return (result.type == Symbol::METHOD) ? result.method_descriptor : NULL; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | const FieldDescriptor* DescriptorPool::FindExtensionByNumber( |
| 958 | const Descriptor* extendee, int number) const { |
| 959 | MutexLockMaybe lock(mutex_); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 960 | const FieldDescriptor* result = tables_->FindExtension(extendee, number); |
| 961 | if (result != NULL) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 962 | return result; |
| 963 | } |
| 964 | if (underlay_ != NULL) { |
| 965 | const FieldDescriptor* result = |
| 966 | underlay_->FindExtensionByNumber(extendee, number); |
| 967 | if (result != NULL) return result; |
| 968 | } |
| 969 | if (TryFindExtensionInFallbackDatabase(extendee, number)) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 970 | const FieldDescriptor* result = tables_->FindExtension(extendee, number); |
| 971 | if (result != NULL) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 972 | return result; |
| 973 | } |
| 974 | } |
| 975 | return NULL; |
| 976 | } |
| 977 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 978 | void DescriptorPool::FindAllExtensions( |
| 979 | const Descriptor* extendee, vector<const FieldDescriptor*>* out) const { |
| 980 | MutexLockMaybe lock(mutex_); |
| 981 | |
| 982 | // Initialize tables_->extensions_ from the fallback database first |
| 983 | // (but do this only once per descriptor). |
| 984 | if (fallback_database_ != NULL && |
| 985 | tables_->extensions_loaded_from_db_.count(extendee) == 0) { |
| 986 | vector<int> numbers; |
| 987 | if (fallback_database_->FindAllExtensionNumbers(extendee->full_name(), |
| 988 | &numbers)) { |
| 989 | for (int i = 0; i < numbers.size(); ++i) { |
| 990 | int number = numbers[i]; |
| 991 | if (tables_->FindExtension(extendee, number) == NULL) { |
| 992 | TryFindExtensionInFallbackDatabase(extendee, number); |
| 993 | } |
| 994 | } |
| 995 | tables_->extensions_loaded_from_db_.insert(extendee); |
| 996 | } |
| 997 | } |
| 998 | |
| 999 | tables_->FindAllExtensions(extendee, out); |
| 1000 | if (underlay_ != NULL) { |
| 1001 | underlay_->FindAllExtensions(extendee, out); |
| 1002 | } |
| 1003 | } |
| 1004 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1005 | // ------------------------------------------------------------------- |
| 1006 | |
| 1007 | const FieldDescriptor* |
| 1008 | Descriptor::FindFieldByNumber(int key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1009 | const FieldDescriptor* result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1010 | file()->tables_->FindFieldByNumber(this, key); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1011 | if (result == NULL || result->is_extension()) { |
| 1012 | return NULL; |
| 1013 | } else { |
| 1014 | return result; |
| 1015 | } |
| 1016 | } |
| 1017 | |
| 1018 | const FieldDescriptor* |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1019 | Descriptor::FindFieldByLowercaseName(const string& key) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1020 | const FieldDescriptor* result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1021 | file()->tables_->FindFieldByLowercaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1022 | if (result == NULL || result->is_extension()) { |
| 1023 | return NULL; |
| 1024 | } else { |
| 1025 | return result; |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | const FieldDescriptor* |
| 1030 | Descriptor::FindFieldByCamelcaseName(const string& key) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1031 | const FieldDescriptor* result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1032 | file()->tables_->FindFieldByCamelcaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1033 | if (result == NULL || result->is_extension()) { |
| 1034 | return NULL; |
| 1035 | } else { |
| 1036 | return result; |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | const FieldDescriptor* |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1041 | Descriptor::FindFieldByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1042 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1043 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1044 | if (!result.IsNull() && !result.field_descriptor->is_extension()) { |
| 1045 | return result.field_descriptor; |
| 1046 | } else { |
| 1047 | return NULL; |
| 1048 | } |
| 1049 | } |
| 1050 | |
| 1051 | const FieldDescriptor* |
| 1052 | Descriptor::FindExtensionByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1053 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1054 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1055 | if (!result.IsNull() && result.field_descriptor->is_extension()) { |
| 1056 | return result.field_descriptor; |
| 1057 | } else { |
| 1058 | return NULL; |
| 1059 | } |
| 1060 | } |
| 1061 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1062 | const FieldDescriptor* |
| 1063 | Descriptor::FindExtensionByLowercaseName(const string& key) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1064 | const FieldDescriptor* result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1065 | file()->tables_->FindFieldByLowercaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1066 | if (result == NULL || !result->is_extension()) { |
| 1067 | return NULL; |
| 1068 | } else { |
| 1069 | return result; |
| 1070 | } |
| 1071 | } |
| 1072 | |
| 1073 | const FieldDescriptor* |
| 1074 | Descriptor::FindExtensionByCamelcaseName(const string& key) const { |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1075 | const FieldDescriptor* result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1076 | file()->tables_->FindFieldByCamelcaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1077 | if (result == NULL || !result->is_extension()) { |
| 1078 | return NULL; |
| 1079 | } else { |
| 1080 | return result; |
| 1081 | } |
| 1082 | } |
| 1083 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1084 | const Descriptor* |
| 1085 | Descriptor::FindNestedTypeByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1086 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1087 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1088 | if (!result.IsNull()) { |
| 1089 | return result.descriptor; |
| 1090 | } else { |
| 1091 | return NULL; |
| 1092 | } |
| 1093 | } |
| 1094 | |
| 1095 | const EnumDescriptor* |
| 1096 | Descriptor::FindEnumTypeByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1097 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1098 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1099 | if (!result.IsNull()) { |
| 1100 | return result.enum_descriptor; |
| 1101 | } else { |
| 1102 | return NULL; |
| 1103 | } |
| 1104 | } |
| 1105 | |
| 1106 | const EnumValueDescriptor* |
| 1107 | Descriptor::FindEnumValueByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1108 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1109 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1110 | if (!result.IsNull()) { |
| 1111 | return result.enum_value_descriptor; |
| 1112 | } else { |
| 1113 | return NULL; |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | const EnumValueDescriptor* |
| 1118 | EnumDescriptor::FindValueByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1119 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1120 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1121 | if (!result.IsNull()) { |
| 1122 | return result.enum_value_descriptor; |
| 1123 | } else { |
| 1124 | return NULL; |
| 1125 | } |
| 1126 | } |
| 1127 | |
| 1128 | const EnumValueDescriptor* |
| 1129 | EnumDescriptor::FindValueByNumber(int key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1130 | return file()->tables_->FindEnumValueByNumber(this, key); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | const MethodDescriptor* |
| 1134 | ServiceDescriptor::FindMethodByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1135 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1136 | file()->tables_->FindNestedSymbolOfType(this, key, Symbol::METHOD); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1137 | if (!result.IsNull()) { |
| 1138 | return result.method_descriptor; |
| 1139 | } else { |
| 1140 | return NULL; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | const Descriptor* |
| 1145 | FileDescriptor::FindMessageTypeByName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1146 | Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::MESSAGE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1147 | if (!result.IsNull()) { |
| 1148 | return result.descriptor; |
| 1149 | } else { |
| 1150 | return NULL; |
| 1151 | } |
| 1152 | } |
| 1153 | |
| 1154 | const EnumDescriptor* |
| 1155 | FileDescriptor::FindEnumTypeByName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1156 | Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1157 | if (!result.IsNull()) { |
| 1158 | return result.enum_descriptor; |
| 1159 | } else { |
| 1160 | return NULL; |
| 1161 | } |
| 1162 | } |
| 1163 | |
| 1164 | const EnumValueDescriptor* |
| 1165 | FileDescriptor::FindEnumValueByName(const string& key) const { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1166 | Symbol result = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1167 | tables_->FindNestedSymbolOfType(this, key, Symbol::ENUM_VALUE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1168 | if (!result.IsNull()) { |
| 1169 | return result.enum_value_descriptor; |
| 1170 | } else { |
| 1171 | return NULL; |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | const ServiceDescriptor* |
| 1176 | FileDescriptor::FindServiceByName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1177 | Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::SERVICE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1178 | if (!result.IsNull()) { |
| 1179 | return result.service_descriptor; |
| 1180 | } else { |
| 1181 | return NULL; |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | const FieldDescriptor* |
| 1186 | FileDescriptor::FindExtensionByName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1187 | Symbol result = tables_->FindNestedSymbolOfType(this, key, Symbol::FIELD); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1188 | if (!result.IsNull() && result.field_descriptor->is_extension()) { |
| 1189 | return result.field_descriptor; |
| 1190 | } else { |
| 1191 | return NULL; |
| 1192 | } |
| 1193 | } |
| 1194 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1195 | const FieldDescriptor* |
| 1196 | FileDescriptor::FindExtensionByLowercaseName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1197 | const FieldDescriptor* result = tables_->FindFieldByLowercaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1198 | if (result == NULL || !result->is_extension()) { |
| 1199 | return NULL; |
| 1200 | } else { |
| 1201 | return result; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | const FieldDescriptor* |
| 1206 | FileDescriptor::FindExtensionByCamelcaseName(const string& key) const { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1207 | const FieldDescriptor* result = tables_->FindFieldByCamelcaseName(this, key); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 1208 | if (result == NULL || !result->is_extension()) { |
| 1209 | return NULL; |
| 1210 | } else { |
| 1211 | return result; |
| 1212 | } |
| 1213 | } |
| 1214 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1215 | bool Descriptor::IsExtensionNumber(int number) const { |
| 1216 | // Linear search should be fine because we don't expect a message to have |
| 1217 | // more than a couple extension ranges. |
| 1218 | for (int i = 0; i < extension_range_count(); i++) { |
| 1219 | if (number >= extension_range(i)->start && |
| 1220 | number < extension_range(i)->end) { |
| 1221 | return true; |
| 1222 | } |
| 1223 | } |
| 1224 | return false; |
| 1225 | } |
| 1226 | |
| 1227 | // ------------------------------------------------------------------- |
| 1228 | |
| 1229 | bool DescriptorPool::TryFindFileInFallbackDatabase(const string& name) const { |
| 1230 | if (fallback_database_ == NULL) return false; |
| 1231 | |
| 1232 | if (tables_->known_bad_files_.count(name) > 0) return false; |
| 1233 | |
| 1234 | FileDescriptorProto file_proto; |
| 1235 | if (!fallback_database_->FindFileByName(name, &file_proto) || |
| 1236 | BuildFileFromDatabase(file_proto) == NULL) { |
| 1237 | tables_->known_bad_files_.insert(name); |
| 1238 | return false; |
| 1239 | } |
| 1240 | |
| 1241 | return true; |
| 1242 | } |
| 1243 | |
| 1244 | bool DescriptorPool::TryFindSymbolInFallbackDatabase(const string& name) const { |
| 1245 | if (fallback_database_ == NULL) return false; |
| 1246 | |
| 1247 | FileDescriptorProto file_proto; |
| 1248 | if (!fallback_database_->FindFileContainingSymbol(name, &file_proto)) { |
| 1249 | return false; |
| 1250 | } |
| 1251 | |
| 1252 | if (tables_->FindFile(file_proto.name()) != NULL) { |
| 1253 | // We've already loaded this file, and it apparently doesn't contain the |
| 1254 | // symbol we're looking for. Some DescriptorDatabases return false |
| 1255 | // positives. |
| 1256 | return false; |
| 1257 | } |
| 1258 | |
| 1259 | if (BuildFileFromDatabase(file_proto) == NULL) { |
| 1260 | return false; |
| 1261 | } |
| 1262 | |
| 1263 | return true; |
| 1264 | } |
| 1265 | |
| 1266 | bool DescriptorPool::TryFindExtensionInFallbackDatabase( |
| 1267 | const Descriptor* containing_type, int field_number) const { |
| 1268 | if (fallback_database_ == NULL) return false; |
| 1269 | |
| 1270 | FileDescriptorProto file_proto; |
| 1271 | if (!fallback_database_->FindFileContainingExtension( |
| 1272 | containing_type->full_name(), field_number, &file_proto)) { |
| 1273 | return false; |
| 1274 | } |
| 1275 | |
| 1276 | if (tables_->FindFile(file_proto.name()) != NULL) { |
| 1277 | // We've already loaded this file, and it apparently doesn't contain the |
| 1278 | // extension we're looking for. Some DescriptorDatabases return false |
| 1279 | // positives. |
| 1280 | return false; |
| 1281 | } |
| 1282 | |
| 1283 | if (BuildFileFromDatabase(file_proto) == NULL) { |
| 1284 | return false; |
| 1285 | } |
| 1286 | |
| 1287 | return true; |
| 1288 | } |
| 1289 | |
| 1290 | // =================================================================== |
| 1291 | |
| 1292 | string FieldDescriptor::DefaultValueAsString(bool quote_string_type) const { |
| 1293 | GOOGLE_CHECK(has_default_value()) << "No default value"; |
| 1294 | switch (cpp_type()) { |
| 1295 | case CPPTYPE_INT32: |
| 1296 | return SimpleItoa(default_value_int32()); |
| 1297 | break; |
| 1298 | case CPPTYPE_INT64: |
| 1299 | return SimpleItoa(default_value_int64()); |
| 1300 | break; |
| 1301 | case CPPTYPE_UINT32: |
| 1302 | return SimpleItoa(default_value_uint32()); |
| 1303 | break; |
| 1304 | case CPPTYPE_UINT64: |
| 1305 | return SimpleItoa(default_value_uint64()); |
| 1306 | break; |
| 1307 | case CPPTYPE_FLOAT: |
| 1308 | return SimpleFtoa(default_value_float()); |
| 1309 | break; |
| 1310 | case CPPTYPE_DOUBLE: |
| 1311 | return SimpleDtoa(default_value_double()); |
| 1312 | break; |
| 1313 | case CPPTYPE_BOOL: |
| 1314 | return default_value_bool() ? "true" : "false"; |
| 1315 | break; |
| 1316 | case CPPTYPE_STRING: |
| 1317 | if (quote_string_type) { |
| 1318 | return "\"" + CEscape(default_value_string()) + "\""; |
| 1319 | } else { |
| 1320 | if (type() == TYPE_BYTES) { |
| 1321 | return CEscape(default_value_string()); |
| 1322 | } else { |
| 1323 | return default_value_string(); |
| 1324 | } |
| 1325 | } |
| 1326 | break; |
| 1327 | case CPPTYPE_ENUM: |
| 1328 | return default_value_enum()->name(); |
| 1329 | break; |
| 1330 | case CPPTYPE_MESSAGE: |
| 1331 | GOOGLE_LOG(DFATAL) << "Messages can't have default values!"; |
| 1332 | break; |
| 1333 | } |
| 1334 | GOOGLE_LOG(FATAL) << "Can't get here: failed to get default value as string"; |
| 1335 | return ""; |
| 1336 | } |
| 1337 | |
| 1338 | // CopyTo methods ==================================================== |
| 1339 | |
| 1340 | void FileDescriptor::CopyTo(FileDescriptorProto* proto) const { |
| 1341 | proto->set_name(name()); |
| 1342 | if (!package().empty()) proto->set_package(package()); |
| 1343 | |
| 1344 | for (int i = 0; i < dependency_count(); i++) { |
| 1345 | proto->add_dependency(dependency(i)->name()); |
| 1346 | } |
| 1347 | |
| 1348 | for (int i = 0; i < message_type_count(); i++) { |
| 1349 | message_type(i)->CopyTo(proto->add_message_type()); |
| 1350 | } |
| 1351 | for (int i = 0; i < enum_type_count(); i++) { |
| 1352 | enum_type(i)->CopyTo(proto->add_enum_type()); |
| 1353 | } |
| 1354 | for (int i = 0; i < service_count(); i++) { |
| 1355 | service(i)->CopyTo(proto->add_service()); |
| 1356 | } |
| 1357 | for (int i = 0; i < extension_count(); i++) { |
| 1358 | extension(i)->CopyTo(proto->add_extension()); |
| 1359 | } |
| 1360 | |
| 1361 | if (&options() != &FileOptions::default_instance()) { |
| 1362 | proto->mutable_options()->CopyFrom(options()); |
| 1363 | } |
| 1364 | } |
| 1365 | |
| 1366 | void Descriptor::CopyTo(DescriptorProto* proto) const { |
| 1367 | proto->set_name(name()); |
| 1368 | |
| 1369 | for (int i = 0; i < field_count(); i++) { |
| 1370 | field(i)->CopyTo(proto->add_field()); |
| 1371 | } |
| 1372 | for (int i = 0; i < nested_type_count(); i++) { |
| 1373 | nested_type(i)->CopyTo(proto->add_nested_type()); |
| 1374 | } |
| 1375 | for (int i = 0; i < enum_type_count(); i++) { |
| 1376 | enum_type(i)->CopyTo(proto->add_enum_type()); |
| 1377 | } |
| 1378 | for (int i = 0; i < extension_range_count(); i++) { |
| 1379 | DescriptorProto::ExtensionRange* range = proto->add_extension_range(); |
| 1380 | range->set_start(extension_range(i)->start); |
| 1381 | range->set_end(extension_range(i)->end); |
| 1382 | } |
| 1383 | for (int i = 0; i < extension_count(); i++) { |
| 1384 | extension(i)->CopyTo(proto->add_extension()); |
| 1385 | } |
| 1386 | |
| 1387 | if (&options() != &MessageOptions::default_instance()) { |
| 1388 | proto->mutable_options()->CopyFrom(options()); |
| 1389 | } |
| 1390 | } |
| 1391 | |
| 1392 | void FieldDescriptor::CopyTo(FieldDescriptorProto* proto) const { |
| 1393 | proto->set_name(name()); |
| 1394 | proto->set_number(number()); |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 1395 | |
| 1396 | // Some compilers do not allow static_cast directly between two enum types, |
| 1397 | // so we must cast to int first. |
| 1398 | proto->set_label(static_cast<FieldDescriptorProto::Label>( |
| 1399 | implicit_cast<int>(label()))); |
| 1400 | proto->set_type(static_cast<FieldDescriptorProto::Type>( |
| 1401 | implicit_cast<int>(type()))); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1402 | |
| 1403 | if (is_extension()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1404 | if (!containing_type()->is_unqualified_placeholder_) { |
| 1405 | proto->set_extendee("."); |
| 1406 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1407 | proto->mutable_extendee()->append(containing_type()->full_name()); |
| 1408 | } |
| 1409 | |
| 1410 | if (cpp_type() == CPPTYPE_MESSAGE) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1411 | if (message_type()->is_placeholder_) { |
| 1412 | // We don't actually know if the type is a message type. It could be |
| 1413 | // an enum. |
| 1414 | proto->clear_type(); |
| 1415 | } |
| 1416 | |
| 1417 | if (!message_type()->is_unqualified_placeholder_) { |
| 1418 | proto->set_type_name("."); |
| 1419 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1420 | proto->mutable_type_name()->append(message_type()->full_name()); |
| 1421 | } else if (cpp_type() == CPPTYPE_ENUM) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1422 | if (!enum_type()->is_unqualified_placeholder_) { |
| 1423 | proto->set_type_name("."); |
| 1424 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1425 | proto->mutable_type_name()->append(enum_type()->full_name()); |
| 1426 | } |
| 1427 | |
| 1428 | if (has_default_value()) { |
| 1429 | proto->set_default_value(DefaultValueAsString(false)); |
| 1430 | } |
| 1431 | |
| 1432 | if (&options() != &FieldOptions::default_instance()) { |
| 1433 | proto->mutable_options()->CopyFrom(options()); |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | void EnumDescriptor::CopyTo(EnumDescriptorProto* proto) const { |
| 1438 | proto->set_name(name()); |
| 1439 | |
| 1440 | for (int i = 0; i < value_count(); i++) { |
| 1441 | value(i)->CopyTo(proto->add_value()); |
| 1442 | } |
| 1443 | |
| 1444 | if (&options() != &EnumOptions::default_instance()) { |
| 1445 | proto->mutable_options()->CopyFrom(options()); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | void EnumValueDescriptor::CopyTo(EnumValueDescriptorProto* proto) const { |
| 1450 | proto->set_name(name()); |
| 1451 | proto->set_number(number()); |
| 1452 | |
| 1453 | if (&options() != &EnumValueOptions::default_instance()) { |
| 1454 | proto->mutable_options()->CopyFrom(options()); |
| 1455 | } |
| 1456 | } |
| 1457 | |
| 1458 | void ServiceDescriptor::CopyTo(ServiceDescriptorProto* proto) const { |
| 1459 | proto->set_name(name()); |
| 1460 | |
| 1461 | for (int i = 0; i < method_count(); i++) { |
| 1462 | method(i)->CopyTo(proto->add_method()); |
| 1463 | } |
| 1464 | |
| 1465 | if (&options() != &ServiceOptions::default_instance()) { |
| 1466 | proto->mutable_options()->CopyFrom(options()); |
| 1467 | } |
| 1468 | } |
| 1469 | |
| 1470 | void MethodDescriptor::CopyTo(MethodDescriptorProto* proto) const { |
| 1471 | proto->set_name(name()); |
| 1472 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1473 | if (!input_type()->is_unqualified_placeholder_) { |
| 1474 | proto->set_input_type("."); |
| 1475 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1476 | proto->mutable_input_type()->append(input_type()->full_name()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1477 | |
| 1478 | if (!output_type()->is_unqualified_placeholder_) { |
| 1479 | proto->set_output_type("."); |
| 1480 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1481 | proto->mutable_output_type()->append(output_type()->full_name()); |
| 1482 | |
| 1483 | if (&options() != &MethodOptions::default_instance()) { |
| 1484 | proto->mutable_options()->CopyFrom(options()); |
| 1485 | } |
| 1486 | } |
| 1487 | |
| 1488 | // DebugString methods =============================================== |
| 1489 | |
| 1490 | namespace { |
| 1491 | |
| 1492 | // Used by each of the option formatters. |
| 1493 | bool RetrieveOptions(const Message &options, vector<string> *option_entries) { |
| 1494 | option_entries->clear(); |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1495 | const Reflection* reflection = options.GetReflection(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1496 | vector<const FieldDescriptor*> fields; |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1497 | reflection->ListFields(options, &fields); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1498 | for (int i = 0; i < fields.size(); i++) { |
| 1499 | // Doesn't make sense to have message type fields here |
| 1500 | if (fields[i]->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 1501 | continue; |
| 1502 | } |
| 1503 | int count = 1; |
| 1504 | bool repeated = false; |
| 1505 | if (fields[i]->is_repeated()) { |
temporal | 779f61c | 2008-08-13 03:15:00 +0000 | [diff] [blame] | 1506 | count = reflection->FieldSize(options, fields[i]); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1507 | repeated = true; |
| 1508 | } |
| 1509 | for (int j = 0; j < count; j++) { |
| 1510 | string fieldval; |
| 1511 | TextFormat::PrintFieldValueToString(options, fields[i], |
| 1512 | repeated ? count : -1, &fieldval); |
| 1513 | option_entries->push_back(fields[i]->name() + " = " + fieldval); |
| 1514 | } |
| 1515 | } |
| 1516 | return !option_entries->empty(); |
| 1517 | } |
| 1518 | |
| 1519 | // Formats options that all appear together in brackets. Does not include |
| 1520 | // brackets. |
| 1521 | bool FormatBracketedOptions(const Message &options, string *output) { |
| 1522 | vector<string> all_options; |
| 1523 | if (RetrieveOptions(options, &all_options)) { |
| 1524 | output->append(JoinStrings(all_options, ", ")); |
| 1525 | } |
| 1526 | return !all_options.empty(); |
| 1527 | } |
| 1528 | |
| 1529 | // Formats options one per line |
| 1530 | bool FormatLineOptions(int depth, const Message &options, string *output) { |
| 1531 | string prefix(depth * 2, ' '); |
| 1532 | vector<string> all_options; |
| 1533 | if (RetrieveOptions(options, &all_options)) { |
| 1534 | for (int i = 0; i < all_options.size(); i++) { |
| 1535 | strings::SubstituteAndAppend(output, "$0option $1;\n", |
| 1536 | prefix, all_options[i]); |
| 1537 | } |
| 1538 | } |
| 1539 | return !all_options.empty(); |
| 1540 | } |
| 1541 | |
| 1542 | } // anonymous namespace |
| 1543 | |
| 1544 | string FileDescriptor::DebugString() const { |
| 1545 | string contents = "syntax = \"proto2\";\n\n"; |
| 1546 | |
| 1547 | for (int i = 0; i < dependency_count(); i++) { |
| 1548 | strings::SubstituteAndAppend(&contents, "import \"$0\";\n", |
| 1549 | dependency(i)->name()); |
| 1550 | } |
| 1551 | |
| 1552 | if (!package().empty()) { |
| 1553 | strings::SubstituteAndAppend(&contents, "package $0;\n\n", package()); |
| 1554 | } |
| 1555 | |
| 1556 | if (FormatLineOptions(0, options(), &contents)) { |
| 1557 | contents.append("\n"); // add some space if we had options |
| 1558 | } |
| 1559 | |
| 1560 | for (int i = 0; i < enum_type_count(); i++) { |
| 1561 | enum_type(i)->DebugString(0, &contents); |
| 1562 | contents.append("\n"); |
| 1563 | } |
| 1564 | |
| 1565 | // Find all the 'group' type extensions; we will not output their nested |
| 1566 | // definitions (those will be done with their group field descriptor). |
| 1567 | set<const Descriptor*> groups; |
| 1568 | for (int i = 0; i < extension_count(); i++) { |
| 1569 | if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) { |
| 1570 | groups.insert(extension(i)->message_type()); |
| 1571 | } |
| 1572 | } |
| 1573 | |
| 1574 | for (int i = 0; i < message_type_count(); i++) { |
| 1575 | if (groups.count(message_type(i)) == 0) { |
| 1576 | strings::SubstituteAndAppend(&contents, "message $0", |
| 1577 | message_type(i)->name()); |
| 1578 | message_type(i)->DebugString(0, &contents); |
| 1579 | contents.append("\n"); |
| 1580 | } |
| 1581 | } |
| 1582 | |
| 1583 | for (int i = 0; i < service_count(); i++) { |
| 1584 | service(i)->DebugString(&contents); |
| 1585 | contents.append("\n"); |
| 1586 | } |
| 1587 | |
| 1588 | const Descriptor* containing_type = NULL; |
| 1589 | for (int i = 0; i < extension_count(); i++) { |
| 1590 | if (extension(i)->containing_type() != containing_type) { |
| 1591 | if (i > 0) contents.append("}\n\n"); |
| 1592 | containing_type = extension(i)->containing_type(); |
| 1593 | strings::SubstituteAndAppend(&contents, "extend .$0 {\n", |
| 1594 | containing_type->full_name()); |
| 1595 | } |
| 1596 | extension(i)->DebugString(1, &contents); |
| 1597 | } |
| 1598 | if (extension_count() > 0) contents.append("}\n\n"); |
| 1599 | |
| 1600 | return contents; |
| 1601 | } |
| 1602 | |
| 1603 | string Descriptor::DebugString() const { |
| 1604 | string contents; |
| 1605 | strings::SubstituteAndAppend(&contents, "message $0", name()); |
| 1606 | DebugString(0, &contents); |
| 1607 | return contents; |
| 1608 | } |
| 1609 | |
| 1610 | void Descriptor::DebugString(int depth, string *contents) const { |
| 1611 | string prefix(depth * 2, ' '); |
| 1612 | ++depth; |
| 1613 | contents->append(" {\n"); |
| 1614 | |
| 1615 | FormatLineOptions(depth, options(), contents); |
| 1616 | |
| 1617 | // Find all the 'group' types for fields and extensions; we will not output |
| 1618 | // their nested definitions (those will be done with their group field |
| 1619 | // descriptor). |
| 1620 | set<const Descriptor*> groups; |
| 1621 | for (int i = 0; i < field_count(); i++) { |
| 1622 | if (field(i)->type() == FieldDescriptor::TYPE_GROUP) { |
| 1623 | groups.insert(field(i)->message_type()); |
| 1624 | } |
| 1625 | } |
| 1626 | for (int i = 0; i < extension_count(); i++) { |
| 1627 | if (extension(i)->type() == FieldDescriptor::TYPE_GROUP) { |
| 1628 | groups.insert(extension(i)->message_type()); |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | for (int i = 0; i < nested_type_count(); i++) { |
| 1633 | if (groups.count(nested_type(i)) == 0) { |
| 1634 | strings::SubstituteAndAppend(contents, "$0 message $1", |
| 1635 | prefix, nested_type(i)->name()); |
| 1636 | nested_type(i)->DebugString(depth, contents); |
| 1637 | } |
| 1638 | } |
| 1639 | for (int i = 0; i < enum_type_count(); i++) { |
| 1640 | enum_type(i)->DebugString(depth, contents); |
| 1641 | } |
| 1642 | for (int i = 0; i < field_count(); i++) { |
| 1643 | field(i)->DebugString(depth, contents); |
| 1644 | } |
| 1645 | |
| 1646 | for (int i = 0; i < extension_range_count(); i++) { |
| 1647 | strings::SubstituteAndAppend(contents, "$0 extensions $1 to $2;\n", |
| 1648 | prefix, |
| 1649 | extension_range(i)->start, |
| 1650 | extension_range(i)->end - 1); |
| 1651 | } |
| 1652 | |
| 1653 | // Group extensions by what they extend, so they can be printed out together. |
| 1654 | const Descriptor* containing_type = NULL; |
| 1655 | for (int i = 0; i < extension_count(); i++) { |
| 1656 | if (extension(i)->containing_type() != containing_type) { |
| 1657 | if (i > 0) strings::SubstituteAndAppend(contents, "$0 }\n", prefix); |
| 1658 | containing_type = extension(i)->containing_type(); |
| 1659 | strings::SubstituteAndAppend(contents, "$0 extend .$1 {\n", |
| 1660 | prefix, containing_type->full_name()); |
| 1661 | } |
| 1662 | extension(i)->DebugString(depth + 1, contents); |
| 1663 | } |
| 1664 | if (extension_count() > 0) |
| 1665 | strings::SubstituteAndAppend(contents, "$0 }\n", prefix); |
| 1666 | |
| 1667 | strings::SubstituteAndAppend(contents, "$0}\n", prefix); |
| 1668 | } |
| 1669 | |
| 1670 | string FieldDescriptor::DebugString() const { |
| 1671 | string contents; |
| 1672 | int depth = 0; |
| 1673 | if (is_extension()) { |
| 1674 | strings::SubstituteAndAppend(&contents, "extend .$0 {\n", |
| 1675 | containing_type()->full_name()); |
| 1676 | depth = 1; |
| 1677 | } |
| 1678 | DebugString(depth, &contents); |
| 1679 | if (is_extension()) { |
| 1680 | contents.append("}\n"); |
| 1681 | } |
| 1682 | return contents; |
| 1683 | } |
| 1684 | |
| 1685 | void FieldDescriptor::DebugString(int depth, string *contents) const { |
| 1686 | string prefix(depth * 2, ' '); |
| 1687 | string field_type; |
| 1688 | switch (type()) { |
| 1689 | case TYPE_MESSAGE: |
| 1690 | field_type = "." + message_type()->full_name(); |
| 1691 | break; |
| 1692 | case TYPE_ENUM: |
| 1693 | field_type = "." + enum_type()->full_name(); |
| 1694 | break; |
| 1695 | default: |
| 1696 | field_type = kTypeToName[type()]; |
| 1697 | } |
| 1698 | |
| 1699 | strings::SubstituteAndAppend(contents, "$0$1 $2 $3 = $4", |
| 1700 | prefix, |
| 1701 | kLabelToName[label()], |
| 1702 | field_type, |
| 1703 | type() == TYPE_GROUP ? message_type()->name() : |
| 1704 | name(), |
| 1705 | number()); |
| 1706 | |
| 1707 | bool bracketed = false; |
| 1708 | if (has_default_value()) { |
| 1709 | bracketed = true; |
| 1710 | strings::SubstituteAndAppend(contents, " [default = $0", |
| 1711 | DefaultValueAsString(true)); |
| 1712 | } |
| 1713 | |
| 1714 | string formatted_options; |
| 1715 | if (FormatBracketedOptions(options(), &formatted_options)) { |
| 1716 | contents->append(bracketed ? ", " : " ["); |
| 1717 | bracketed = true; |
| 1718 | contents->append(formatted_options); |
| 1719 | } |
| 1720 | |
| 1721 | if (bracketed) { |
| 1722 | contents->append("]"); |
| 1723 | } |
| 1724 | |
| 1725 | if (type() == TYPE_GROUP) { |
| 1726 | message_type()->DebugString(depth, contents); |
| 1727 | } else { |
| 1728 | contents->append(";\n"); |
| 1729 | } |
| 1730 | } |
| 1731 | |
| 1732 | string EnumDescriptor::DebugString() const { |
| 1733 | string contents; |
| 1734 | DebugString(0, &contents); |
| 1735 | return contents; |
| 1736 | } |
| 1737 | |
| 1738 | void EnumDescriptor::DebugString(int depth, string *contents) const { |
| 1739 | string prefix(depth * 2, ' '); |
| 1740 | ++depth; |
| 1741 | strings::SubstituteAndAppend(contents, "$0enum $1 {\n", |
| 1742 | prefix, name()); |
| 1743 | |
| 1744 | FormatLineOptions(depth, options(), contents); |
| 1745 | |
| 1746 | for (int i = 0; i < value_count(); i++) { |
| 1747 | value(i)->DebugString(depth, contents); |
| 1748 | } |
| 1749 | strings::SubstituteAndAppend(contents, "$0}\n", prefix); |
| 1750 | } |
| 1751 | |
| 1752 | string EnumValueDescriptor::DebugString() const { |
| 1753 | string contents; |
| 1754 | DebugString(0, &contents); |
| 1755 | return contents; |
| 1756 | } |
| 1757 | |
| 1758 | void EnumValueDescriptor::DebugString(int depth, string *contents) const { |
| 1759 | string prefix(depth * 2, ' '); |
| 1760 | strings::SubstituteAndAppend(contents, "$0$1 = $2", |
| 1761 | prefix, name(), number()); |
| 1762 | |
| 1763 | string formatted_options; |
| 1764 | if (FormatBracketedOptions(options(), &formatted_options)) { |
| 1765 | strings::SubstituteAndAppend(contents, " [$0]", formatted_options); |
| 1766 | } |
| 1767 | contents->append(";\n"); |
| 1768 | } |
| 1769 | |
| 1770 | string ServiceDescriptor::DebugString() const { |
| 1771 | string contents; |
| 1772 | DebugString(&contents); |
| 1773 | return contents; |
| 1774 | } |
| 1775 | |
| 1776 | void ServiceDescriptor::DebugString(string *contents) const { |
| 1777 | strings::SubstituteAndAppend(contents, "service $0 {\n", name()); |
| 1778 | |
| 1779 | FormatLineOptions(1, options(), contents); |
| 1780 | |
| 1781 | for (int i = 0; i < method_count(); i++) { |
| 1782 | method(i)->DebugString(1, contents); |
| 1783 | } |
| 1784 | |
| 1785 | contents->append("}\n"); |
| 1786 | } |
| 1787 | |
| 1788 | string MethodDescriptor::DebugString() const { |
| 1789 | string contents; |
| 1790 | DebugString(0, &contents); |
| 1791 | return contents; |
| 1792 | } |
| 1793 | |
| 1794 | void MethodDescriptor::DebugString(int depth, string *contents) const { |
| 1795 | string prefix(depth * 2, ' '); |
| 1796 | ++depth; |
| 1797 | strings::SubstituteAndAppend(contents, "$0rpc $1(.$2) returns (.$3)", |
| 1798 | prefix, name(), |
| 1799 | input_type()->full_name(), |
| 1800 | output_type()->full_name()); |
| 1801 | |
| 1802 | string formatted_options; |
| 1803 | if (FormatLineOptions(depth, options(), &formatted_options)) { |
| 1804 | strings::SubstituteAndAppend(contents, " {\n$0$1}\n", |
| 1805 | formatted_options, prefix); |
| 1806 | } else { |
| 1807 | contents->append(";\n"); |
| 1808 | } |
| 1809 | } |
| 1810 | // =================================================================== |
| 1811 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1812 | namespace { |
| 1813 | |
| 1814 | // Represents an options message to interpret. Extension names in the option |
| 1815 | // name are respolved relative to name_scope. element_name and orig_opt are |
| 1816 | // used only for error reporting (since the parser records locations against |
| 1817 | // pointers in the original options, not the mutable copy). The Message must be |
| 1818 | // one of the Options messages in descriptor.proto. |
| 1819 | struct OptionsToInterpret { |
| 1820 | OptionsToInterpret(const string& ns, |
| 1821 | const string& el, |
| 1822 | const Message* orig_opt, |
| 1823 | Message* opt) |
| 1824 | : name_scope(ns), |
| 1825 | element_name(el), |
| 1826 | original_options(orig_opt), |
| 1827 | options(opt) { |
| 1828 | } |
| 1829 | string name_scope; |
| 1830 | string element_name; |
| 1831 | const Message* original_options; |
| 1832 | Message* options; |
| 1833 | }; |
| 1834 | |
| 1835 | } // namespace |
| 1836 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1837 | class DescriptorBuilder { |
| 1838 | public: |
| 1839 | DescriptorBuilder(const DescriptorPool* pool, |
| 1840 | DescriptorPool::Tables* tables, |
| 1841 | DescriptorPool::ErrorCollector* error_collector); |
| 1842 | ~DescriptorBuilder(); |
| 1843 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1844 | const FileDescriptor* BuildFile(const FileDescriptorProto& proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1845 | |
| 1846 | private: |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1847 | friend class OptionInterpreter; |
| 1848 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1849 | const DescriptorPool* pool_; |
| 1850 | DescriptorPool::Tables* tables_; // for convenience |
| 1851 | DescriptorPool::ErrorCollector* error_collector_; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1852 | |
| 1853 | // As we build descriptors we store copies of the options messages in |
| 1854 | // them. We put pointers to those copies in this vector, as we build, so we |
| 1855 | // can later (after cross-linking) interpret those options. |
| 1856 | vector<OptionsToInterpret> options_to_interpret_; |
| 1857 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1858 | bool had_errors_; |
| 1859 | string filename_; |
| 1860 | FileDescriptor* file_; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1861 | FileDescriptorTables* file_tables_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1862 | |
| 1863 | // If LookupSymbol() finds a symbol that is in a file which is not a declared |
| 1864 | // dependency of this file, it will fail, but will set |
| 1865 | // possible_undeclared_dependency_ to point at that file. This is only used |
| 1866 | // by AddNotDefinedError() to report a more useful error message. |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1867 | // possible_undeclared_dependency_name_ is the name of the symbol that was |
| 1868 | // actually found in possible_undeclared_dependency_, which may be a parent |
| 1869 | // of the symbol actually looked for. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1870 | const FileDescriptor* possible_undeclared_dependency_; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 1871 | string possible_undeclared_dependency_name_; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1872 | |
| 1873 | void AddError(const string& element_name, |
| 1874 | const Message& descriptor, |
| 1875 | DescriptorPool::ErrorCollector::ErrorLocation location, |
| 1876 | const string& error); |
| 1877 | |
| 1878 | // Adds an error indicating that undefined_symbol was not defined. Must |
| 1879 | // only be called after LookupSymbol() fails. |
| 1880 | void AddNotDefinedError( |
| 1881 | const string& element_name, |
| 1882 | const Message& descriptor, |
| 1883 | DescriptorPool::ErrorCollector::ErrorLocation location, |
| 1884 | const string& undefined_symbol); |
| 1885 | |
| 1886 | // Silly helper which determines if the given file is in the given package. |
| 1887 | // I.e., either file->package() == package_name or file->package() is a |
| 1888 | // nested package within package_name. |
| 1889 | bool IsInPackage(const FileDescriptor* file, const string& package_name); |
| 1890 | |
| 1891 | // Like tables_->FindSymbol(), but additionally: |
| 1892 | // - Search the pool's underlay if not found in tables_. |
| 1893 | // - Insure that the resulting Symbol is from one of the file's declared |
| 1894 | // dependencies. |
| 1895 | Symbol FindSymbol(const string& name); |
| 1896 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 1897 | // Like FindSymbol() but does not require that the symbol is in one of the |
| 1898 | // file's declared dependencies. |
| 1899 | Symbol FindSymbolNotEnforcingDeps(const string& name); |
| 1900 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1901 | // Like FindSymbol(), but looks up the name relative to some other symbol |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1902 | // name. This first searches siblings of relative_to, then siblings of its |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1903 | // parents, etc. For example, LookupSymbol("foo.bar", "baz.qux.corge") makes |
| 1904 | // the following calls, returning the first non-null result: |
| 1905 | // FindSymbol("baz.qux.foo.bar"), FindSymbol("baz.foo.bar"), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1906 | // FindSymbol("foo.bar"). If AllowUnknownDependencies() has been called |
| 1907 | // on the DescriptorPool, this will generate a placeholder type if |
| 1908 | // the name is not found (unless the name itself is malformed). The |
| 1909 | // placeholder_type parameter indicates what kind of placeholder should be |
| 1910 | // constructed in this case. The resolve_mode parameter determines whether |
| 1911 | // any symbol is returned, or only symbols that are types. Note, however, |
| 1912 | // that LookupSymbol may still return a non-type symbol in LOOKUP_TYPES mode, |
| 1913 | // if it believes that's all it could refer to. The caller should always |
| 1914 | // check that it receives the type of symbol it was expecting. |
| 1915 | enum PlaceholderType { |
| 1916 | PLACEHOLDER_MESSAGE, |
| 1917 | PLACEHOLDER_ENUM, |
| 1918 | PLACEHOLDER_EXTENDABLE_MESSAGE |
| 1919 | }; |
| 1920 | enum ResolveMode { |
| 1921 | LOOKUP_ALL, LOOKUP_TYPES |
| 1922 | }; |
| 1923 | Symbol LookupSymbol(const string& name, const string& relative_to, |
| 1924 | PlaceholderType placeholder_type = PLACEHOLDER_MESSAGE, |
| 1925 | ResolveMode resolve_mode = LOOKUP_ALL); |
| 1926 | |
| 1927 | // Like LookupSymbol() but will not return a placeholder even if |
| 1928 | // AllowUnknownDependencies() has been used. |
| 1929 | Symbol LookupSymbolNoPlaceholder(const string& name, |
| 1930 | const string& relative_to, |
| 1931 | ResolveMode resolve_mode = LOOKUP_ALL); |
| 1932 | |
| 1933 | // Creates a placeholder type suitable for return from LookupSymbol(). May |
| 1934 | // return kNullSymbol if the name is not a valid type name. |
| 1935 | Symbol NewPlaceholder(const string& name, PlaceholderType placeholder_type); |
| 1936 | |
| 1937 | // Creates a placeholder file. Never returns NULL. This is used when an |
| 1938 | // import is not found and AllowUnknownDependencies() is enabled. |
| 1939 | const FileDescriptor* NewPlaceholderFile(const string& name); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1940 | |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 1941 | // Calls tables_->AddSymbol() and records an error if it fails. Returns |
| 1942 | // true if successful or false if failed, though most callers can ignore |
| 1943 | // the return value since an error has already been recorded. |
| 1944 | bool AddSymbol(const string& full_name, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1945 | const void* parent, const string& name, |
| 1946 | const Message& proto, Symbol symbol); |
| 1947 | |
| 1948 | // Like AddSymbol(), but succeeds if the symbol is already defined as long |
| 1949 | // as the existing definition is also a package (because it's OK to define |
| 1950 | // the same package in two different files). Also adds all parents of the |
| 1951 | // packgae to the symbol table (e.g. AddPackage("foo.bar", ...) will add |
| 1952 | // "foo.bar" and "foo" to the table). |
| 1953 | void AddPackage(const string& name, const Message& proto, |
| 1954 | const FileDescriptor* file); |
| 1955 | |
| 1956 | // Checks that the symbol name contains only alphanumeric characters and |
| 1957 | // underscores. Records an error otherwise. |
| 1958 | void ValidateSymbolName(const string& name, const string& full_name, |
| 1959 | const Message& proto); |
| 1960 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 1961 | // Like ValidateSymbolName(), but the name is allowed to contain periods and |
| 1962 | // an error is indicated by returning false (not recording the error). |
| 1963 | bool ValidateQualifiedName(const string& name); |
| 1964 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1965 | // Used by BUILD_ARRAY macro (below) to avoid having to have the type |
| 1966 | // specified as a macro parameter. |
| 1967 | template <typename Type> |
| 1968 | inline void AllocateArray(int size, Type** output) { |
| 1969 | *output = tables_->AllocateArray<Type>(size); |
| 1970 | } |
| 1971 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1972 | // Allocates a copy of orig_options in tables_ and stores it in the |
| 1973 | // descriptor. Remembers its uninterpreted options, to be interpreted |
| 1974 | // later. DescriptorT must be one of the Descriptor messages from |
| 1975 | // descriptor.proto. |
| 1976 | template<class DescriptorT> void AllocateOptions( |
| 1977 | const typename DescriptorT::OptionsType& orig_options, |
| 1978 | DescriptorT* descriptor); |
kenton@google.com | de75437 | 2008-11-06 21:36:28 +0000 | [diff] [blame] | 1979 | // Specialization for FileOptions. |
| 1980 | void AllocateOptions(const FileOptions& orig_options, |
| 1981 | FileDescriptor* descriptor); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 1982 | |
| 1983 | // Implementation for AllocateOptions(). Don't call this directly. |
| 1984 | template<class DescriptorT> void AllocateOptionsImpl( |
| 1985 | const string& name_scope, |
| 1986 | const string& element_name, |
| 1987 | const typename DescriptorT::OptionsType& orig_options, |
| 1988 | DescriptorT* descriptor); |
| 1989 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1990 | // These methods all have the same signature for the sake of the BUILD_ARRAY |
| 1991 | // macro, below. |
| 1992 | void BuildMessage(const DescriptorProto& proto, |
| 1993 | const Descriptor* parent, |
| 1994 | Descriptor* result); |
| 1995 | void BuildFieldOrExtension(const FieldDescriptorProto& proto, |
| 1996 | const Descriptor* parent, |
| 1997 | FieldDescriptor* result, |
| 1998 | bool is_extension); |
| 1999 | void BuildField(const FieldDescriptorProto& proto, |
| 2000 | const Descriptor* parent, |
| 2001 | FieldDescriptor* result) { |
| 2002 | BuildFieldOrExtension(proto, parent, result, false); |
| 2003 | } |
| 2004 | void BuildExtension(const FieldDescriptorProto& proto, |
| 2005 | const Descriptor* parent, |
| 2006 | FieldDescriptor* result) { |
| 2007 | BuildFieldOrExtension(proto, parent, result, true); |
| 2008 | } |
| 2009 | void BuildExtensionRange(const DescriptorProto::ExtensionRange& proto, |
| 2010 | const Descriptor* parent, |
| 2011 | Descriptor::ExtensionRange* result); |
| 2012 | void BuildEnum(const EnumDescriptorProto& proto, |
| 2013 | const Descriptor* parent, |
| 2014 | EnumDescriptor* result); |
| 2015 | void BuildEnumValue(const EnumValueDescriptorProto& proto, |
| 2016 | const EnumDescriptor* parent, |
| 2017 | EnumValueDescriptor* result); |
| 2018 | void BuildService(const ServiceDescriptorProto& proto, |
| 2019 | const void* dummy, |
| 2020 | ServiceDescriptor* result); |
| 2021 | void BuildMethod(const MethodDescriptorProto& proto, |
| 2022 | const ServiceDescriptor* parent, |
| 2023 | MethodDescriptor* result); |
| 2024 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2025 | // Must be run only after building. |
| 2026 | // |
| 2027 | // NOTE: Options will not be available during cross-linking, as they |
| 2028 | // have not yet been interpreted. Defer any handling of options to the |
| 2029 | // Validate*Options methods. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2030 | void CrossLinkFile(FileDescriptor* file, const FileDescriptorProto& proto); |
| 2031 | void CrossLinkMessage(Descriptor* message, const DescriptorProto& proto); |
| 2032 | void CrossLinkField(FieldDescriptor* field, |
| 2033 | const FieldDescriptorProto& proto); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2034 | void CrossLinkEnum(EnumDescriptor* enum_type, |
| 2035 | const EnumDescriptorProto& proto); |
| 2036 | void CrossLinkEnumValue(EnumValueDescriptor* enum_value, |
| 2037 | const EnumValueDescriptorProto& proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2038 | void CrossLinkService(ServiceDescriptor* service, |
| 2039 | const ServiceDescriptorProto& proto); |
| 2040 | void CrossLinkMethod(MethodDescriptor* method, |
| 2041 | const MethodDescriptorProto& proto); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2042 | |
| 2043 | // Must be run only after cross-linking. |
| 2044 | void InterpretOptions(); |
| 2045 | |
| 2046 | // A helper class for interpreting options. |
| 2047 | class OptionInterpreter { |
| 2048 | public: |
| 2049 | // Creates an interpreter that operates in the context of the pool of the |
| 2050 | // specified builder, which must not be NULL. We don't take ownership of the |
| 2051 | // builder. |
| 2052 | explicit OptionInterpreter(DescriptorBuilder* builder); |
| 2053 | |
| 2054 | ~OptionInterpreter(); |
| 2055 | |
| 2056 | // Interprets the uninterpreted options in the specified Options message. |
| 2057 | // On error, calls AddError() on the underlying builder and returns false. |
| 2058 | // Otherwise returns true. |
| 2059 | bool InterpretOptions(OptionsToInterpret* options_to_interpret); |
| 2060 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 2061 | class AggregateOptionFinder; |
| 2062 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2063 | private: |
| 2064 | // Interprets uninterpreted_option_ on the specified message, which |
| 2065 | // must be the mutable copy of the original options message to which |
| 2066 | // uninterpreted_option_ belongs. |
| 2067 | bool InterpretSingleOption(Message* options); |
| 2068 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2069 | // Adds the uninterpreted_option to the given options message verbatim. |
| 2070 | // Used when AllowUnknownDependencies() is in effect and we can't find |
| 2071 | // the option's definition. |
| 2072 | void AddWithoutInterpreting(const UninterpretedOption& uninterpreted_option, |
| 2073 | Message* options); |
| 2074 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2075 | // A recursive helper function that drills into the intermediate fields |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 2076 | // in unknown_fields to check if field innermost_field is set on the |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2077 | // innermost message. Returns false and sets an error if so. |
| 2078 | bool ExamineIfOptionIsSet( |
| 2079 | vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter, |
| 2080 | vector<const FieldDescriptor*>::const_iterator intermediate_fields_end, |
| 2081 | const FieldDescriptor* innermost_field, const string& debug_msg_name, |
| 2082 | const UnknownFieldSet& unknown_fields); |
| 2083 | |
| 2084 | // Validates the value for the option field of the currently interpreted |
| 2085 | // option and then sets it on the unknown_field. |
| 2086 | bool SetOptionValue(const FieldDescriptor* option_field, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2087 | UnknownFieldSet* unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2088 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 2089 | // Parses an aggregate value for a CPPTYPE_MESSAGE option and |
| 2090 | // saves it into *unknown_fields. |
| 2091 | bool SetAggregateOption(const FieldDescriptor* option_field, |
| 2092 | UnknownFieldSet* unknown_fields); |
| 2093 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2094 | // Convenience functions to set an int field the right way, depending on |
| 2095 | // its wire type (a single int CppType can represent multiple wire types). |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2096 | void SetInt32(int number, int32 value, FieldDescriptor::Type type, |
| 2097 | UnknownFieldSet* unknown_fields); |
| 2098 | void SetInt64(int number, int64 value, FieldDescriptor::Type type, |
| 2099 | UnknownFieldSet* unknown_fields); |
| 2100 | void SetUInt32(int number, uint32 value, FieldDescriptor::Type type, |
| 2101 | UnknownFieldSet* unknown_fields); |
| 2102 | void SetUInt64(int number, uint64 value, FieldDescriptor::Type type, |
| 2103 | UnknownFieldSet* unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2104 | |
| 2105 | // A helper function that adds an error at the specified location of the |
| 2106 | // option we're currently interpreting, and returns false. |
| 2107 | bool AddOptionError(DescriptorPool::ErrorCollector::ErrorLocation location, |
| 2108 | const string& msg) { |
| 2109 | builder_->AddError(options_to_interpret_->element_name, |
| 2110 | *uninterpreted_option_, location, msg); |
| 2111 | return false; |
| 2112 | } |
| 2113 | |
| 2114 | // A helper function that adds an error at the location of the option name |
| 2115 | // and returns false. |
| 2116 | bool AddNameError(const string& msg) { |
| 2117 | return AddOptionError(DescriptorPool::ErrorCollector::OPTION_NAME, msg); |
| 2118 | } |
| 2119 | |
| 2120 | // A helper function that adds an error at the location of the option name |
| 2121 | // and returns false. |
| 2122 | bool AddValueError(const string& msg) { |
| 2123 | return AddOptionError(DescriptorPool::ErrorCollector::OPTION_VALUE, msg); |
| 2124 | } |
| 2125 | |
| 2126 | // We interpret against this builder's pool. Is never NULL. We don't own |
| 2127 | // this pointer. |
| 2128 | DescriptorBuilder* builder_; |
| 2129 | |
| 2130 | // The options we're currently interpreting, or NULL if we're not in a call |
| 2131 | // to InterpretOptions. |
| 2132 | const OptionsToInterpret* options_to_interpret_; |
| 2133 | |
| 2134 | // The option we're currently interpreting within options_to_interpret_, or |
| 2135 | // NULL if we're not in a call to InterpretOptions(). This points to a |
| 2136 | // submessage of the original option, not the mutable copy. Therefore we |
| 2137 | // can use it to find locations recorded by the parser. |
| 2138 | const UninterpretedOption* uninterpreted_option_; |
| 2139 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 2140 | // Factory used to create the dynamic messages we need to parse |
| 2141 | // any aggregate option values we encounter. |
| 2142 | DynamicMessageFactory dynamic_factory_; |
| 2143 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2144 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(OptionInterpreter); |
| 2145 | }; |
| 2146 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2147 | // Work-around for broken compilers: According to the C++ standard, |
| 2148 | // OptionInterpreter should have access to the private members of any class |
| 2149 | // which has declared DescriptorBuilder as a friend. Unfortunately some old |
| 2150 | // versions of GCC and other compilers do not implement this correctly. So, |
| 2151 | // we have to have these intermediate methods to provide access. We also |
| 2152 | // redundantly declare OptionInterpreter a friend just to make things extra |
| 2153 | // clear for these bad compilers. |
| 2154 | friend class OptionInterpreter; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 2155 | friend class OptionInterpreter::AggregateOptionFinder; |
| 2156 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2157 | static inline bool get_allow_unknown(const DescriptorPool* pool) { |
| 2158 | return pool->allow_unknown_; |
| 2159 | } |
| 2160 | static inline bool get_is_placeholder(const Descriptor* descriptor) { |
| 2161 | return descriptor->is_placeholder_; |
| 2162 | } |
| 2163 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2164 | // Must be run only after options have been interpreted. |
| 2165 | // |
| 2166 | // NOTE: Validation code must only reference the options in the mutable |
| 2167 | // descriptors, which are the ones that have been interpreted. The const |
| 2168 | // proto references are passed in only so they can be provided to calls to |
| 2169 | // AddError(). Do not look at their options, which have not been interpreted. |
| 2170 | void ValidateFileOptions(FileDescriptor* file, |
| 2171 | const FileDescriptorProto& proto); |
| 2172 | void ValidateMessageOptions(Descriptor* message, |
| 2173 | const DescriptorProto& proto); |
| 2174 | void ValidateFieldOptions(FieldDescriptor* field, |
| 2175 | const FieldDescriptorProto& proto); |
| 2176 | void ValidateEnumOptions(EnumDescriptor* enm, |
| 2177 | const EnumDescriptorProto& proto); |
| 2178 | void ValidateEnumValueOptions(EnumValueDescriptor* enum_value, |
| 2179 | const EnumValueDescriptorProto& proto); |
| 2180 | void ValidateServiceOptions(ServiceDescriptor* service, |
| 2181 | const ServiceDescriptorProto& proto); |
| 2182 | void ValidateMethodOptions(MethodDescriptor* method, |
| 2183 | const MethodDescriptorProto& proto); |
| 2184 | |
| 2185 | void ValidateMapKey(FieldDescriptor* field, |
| 2186 | const FieldDescriptorProto& proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2187 | }; |
| 2188 | |
| 2189 | const FileDescriptor* DescriptorPool::BuildFile( |
| 2190 | const FileDescriptorProto& proto) { |
| 2191 | GOOGLE_CHECK(fallback_database_ == NULL) |
| 2192 | << "Cannot call BuildFile on a DescriptorPool that uses a " |
| 2193 | "DescriptorDatabase. You must instead find a way to get your file " |
| 2194 | "into the underlying database."; |
| 2195 | GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2196 | return DescriptorBuilder(this, tables_.get(), NULL).BuildFile(proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2197 | } |
| 2198 | |
| 2199 | const FileDescriptor* DescriptorPool::BuildFileCollectingErrors( |
| 2200 | const FileDescriptorProto& proto, |
| 2201 | ErrorCollector* error_collector) { |
| 2202 | GOOGLE_CHECK(fallback_database_ == NULL) |
| 2203 | << "Cannot call BuildFile on a DescriptorPool that uses a " |
| 2204 | "DescriptorDatabase. You must instead find a way to get your file " |
| 2205 | "into the underlying database."; |
| 2206 | GOOGLE_CHECK(mutex_ == NULL); // Implied by the above GOOGLE_CHECK. |
| 2207 | return DescriptorBuilder(this, tables_.get(), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2208 | error_collector).BuildFile(proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
| 2211 | const FileDescriptor* DescriptorPool::BuildFileFromDatabase( |
| 2212 | const FileDescriptorProto& proto) const { |
| 2213 | mutex_->AssertHeld(); |
| 2214 | return DescriptorBuilder(this, tables_.get(), |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2215 | default_error_collector_).BuildFile(proto); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2216 | } |
| 2217 | |
| 2218 | DescriptorBuilder::DescriptorBuilder( |
| 2219 | const DescriptorPool* pool, |
| 2220 | DescriptorPool::Tables* tables, |
| 2221 | DescriptorPool::ErrorCollector* error_collector) |
| 2222 | : pool_(pool), |
| 2223 | tables_(tables), |
| 2224 | error_collector_(error_collector), |
| 2225 | had_errors_(false), |
| 2226 | possible_undeclared_dependency_(NULL) {} |
| 2227 | |
| 2228 | DescriptorBuilder::~DescriptorBuilder() {} |
| 2229 | |
| 2230 | void DescriptorBuilder::AddError( |
| 2231 | const string& element_name, |
| 2232 | const Message& descriptor, |
| 2233 | DescriptorPool::ErrorCollector::ErrorLocation location, |
| 2234 | const string& error) { |
| 2235 | if (error_collector_ == NULL) { |
| 2236 | if (!had_errors_) { |
| 2237 | GOOGLE_LOG(ERROR) << "Invalid proto descriptor for file \"" << filename_ |
| 2238 | << "\":"; |
| 2239 | } |
| 2240 | GOOGLE_LOG(ERROR) << " " << element_name << ": " << error; |
| 2241 | } else { |
| 2242 | error_collector_->AddError(filename_, element_name, |
| 2243 | &descriptor, location, error); |
| 2244 | } |
| 2245 | had_errors_ = true; |
| 2246 | } |
| 2247 | |
| 2248 | void DescriptorBuilder::AddNotDefinedError( |
| 2249 | const string& element_name, |
| 2250 | const Message& descriptor, |
| 2251 | DescriptorPool::ErrorCollector::ErrorLocation location, |
| 2252 | const string& undefined_symbol) { |
| 2253 | if (possible_undeclared_dependency_ == NULL) { |
| 2254 | AddError(element_name, descriptor, location, |
| 2255 | "\"" + undefined_symbol + "\" is not defined."); |
| 2256 | } else { |
| 2257 | AddError(element_name, descriptor, location, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 2258 | "\"" + possible_undeclared_dependency_name_ + |
| 2259 | "\" seems to be defined in \"" + |
| 2260 | possible_undeclared_dependency_->name() + "\", which is not " |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2261 | "imported by \"" + filename_ + "\". To use it here, please " |
| 2262 | "add the necessary import."); |
| 2263 | } |
| 2264 | } |
| 2265 | |
| 2266 | bool DescriptorBuilder::IsInPackage(const FileDescriptor* file, |
| 2267 | const string& package_name) { |
| 2268 | return HasPrefixString(file->package(), package_name) && |
| 2269 | (file->package().size() == package_name.size() || |
| 2270 | file->package()[package_name.size()] == '.'); |
| 2271 | } |
| 2272 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 2273 | Symbol DescriptorBuilder::FindSymbolNotEnforcingDeps(const string& name) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2274 | Symbol result; |
| 2275 | |
| 2276 | // We need to search our pool and all its underlays. |
| 2277 | const DescriptorPool* pool = pool_; |
| 2278 | while (true) { |
| 2279 | // If we are looking at an underlay, we must lock its mutex_, since we are |
| 2280 | // accessing the underlay's tables_ dircetly. |
| 2281 | MutexLockMaybe lock((pool == pool_) ? NULL : pool->mutex_); |
| 2282 | |
| 2283 | // Note that we don't have to check fallback_database_ here because the |
| 2284 | // symbol has to be in one of its file's direct dependencies, and we have |
| 2285 | // already loaded those by the time we get here. |
| 2286 | result = pool->tables_->FindSymbol(name); |
| 2287 | if (!result.IsNull()) break; |
| 2288 | if (pool->underlay_ == NULL) return kNullSymbol; |
| 2289 | pool = pool->underlay_; |
| 2290 | } |
| 2291 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 2292 | return result; |
| 2293 | } |
| 2294 | |
| 2295 | Symbol DescriptorBuilder::FindSymbol(const string& name) { |
| 2296 | Symbol result = FindSymbolNotEnforcingDeps(name); |
| 2297 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2298 | if (!pool_->enforce_dependencies_) { |
| 2299 | // Hack for CompilerUpgrader. |
| 2300 | return result; |
| 2301 | } |
| 2302 | |
| 2303 | // Only find symbols which were defined in this file or one of its |
| 2304 | // dependencies. |
| 2305 | const FileDescriptor* file = result.GetFile(); |
| 2306 | if (file == file_) return result; |
| 2307 | for (int i = 0; i < file_->dependency_count(); i++) { |
| 2308 | if (file == file_->dependency(i)) return result; |
| 2309 | } |
| 2310 | |
| 2311 | if (result.type == Symbol::PACKAGE) { |
| 2312 | // Arg, this is overcomplicated. The symbol is a package name. It could |
| 2313 | // be that the package was defined in multiple files. result.GetFile() |
| 2314 | // returns the first file we saw that used this package. We've determined |
| 2315 | // that that file is not a direct dependency of the file we are currently |
| 2316 | // building, but it could be that some other file which *is* a direct |
| 2317 | // dependency also defines the same package. We can't really rule out this |
| 2318 | // symbol unless none of the dependencies define it. |
| 2319 | if (IsInPackage(file_, name)) return result; |
| 2320 | for (int i = 0; i < file_->dependency_count(); i++) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 2321 | // Note: A dependency may be NULL if it was not found or had errors. |
| 2322 | if (file_->dependency(i) != NULL && |
| 2323 | IsInPackage(file_->dependency(i), name)) { |
| 2324 | return result; |
| 2325 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2326 | } |
| 2327 | } |
| 2328 | |
| 2329 | possible_undeclared_dependency_ = file; |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 2330 | possible_undeclared_dependency_name_ = name; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2331 | return kNullSymbol; |
| 2332 | } |
| 2333 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2334 | Symbol DescriptorBuilder::LookupSymbolNoPlaceholder( |
| 2335 | const string& name, const string& relative_to, ResolveMode resolve_mode) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2336 | possible_undeclared_dependency_ = NULL; |
| 2337 | |
| 2338 | if (name.size() > 0 && name[0] == '.') { |
| 2339 | // Fully-qualified name. |
| 2340 | return FindSymbol(name.substr(1)); |
| 2341 | } |
| 2342 | |
| 2343 | // If name is something like "Foo.Bar.baz", and symbols named "Foo" are |
| 2344 | // defined in multiple parent scopes, we only want to find "Bar.baz" in the |
| 2345 | // innermost one. E.g., the following should produce an error: |
| 2346 | // message Bar { message Baz {} } |
| 2347 | // message Foo { |
| 2348 | // message Bar { |
| 2349 | // } |
| 2350 | // optional Bar.Baz baz = 1; |
| 2351 | // } |
| 2352 | // So, we look for just "Foo" first, then look for "Bar.baz" within it if |
| 2353 | // found. |
| 2354 | int name_dot_pos = name.find_first_of('.'); |
| 2355 | string first_part_of_name; |
| 2356 | if (name_dot_pos == string::npos) { |
| 2357 | first_part_of_name = name; |
| 2358 | } else { |
| 2359 | first_part_of_name = name.substr(0, name_dot_pos); |
| 2360 | } |
| 2361 | |
| 2362 | string scope_to_try(relative_to); |
| 2363 | |
| 2364 | while (true) { |
| 2365 | // Chop off the last component of the scope. |
| 2366 | string::size_type dot_pos = scope_to_try.find_last_of('.'); |
| 2367 | if (dot_pos == string::npos) { |
| 2368 | return FindSymbol(name); |
| 2369 | } else { |
| 2370 | scope_to_try.erase(dot_pos); |
| 2371 | } |
| 2372 | |
| 2373 | // Append ".first_part_of_name" and try to find. |
| 2374 | string::size_type old_size = scope_to_try.size(); |
| 2375 | scope_to_try.append(1, '.'); |
| 2376 | scope_to_try.append(first_part_of_name); |
| 2377 | Symbol result = FindSymbol(scope_to_try); |
| 2378 | if (!result.IsNull()) { |
| 2379 | if (first_part_of_name.size() < name.size()) { |
| 2380 | // name is a compound symbol, of which we only found the first part. |
| 2381 | // Now try to look up the rest of it. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2382 | if (result.IsAggregate()) { |
| 2383 | scope_to_try.append(name, first_part_of_name.size(), |
| 2384 | name.size() - first_part_of_name.size()); |
| 2385 | return FindSymbol(scope_to_try); |
| 2386 | } else { |
| 2387 | // We found a symbol but it's not an aggregate. Continue the loop. |
| 2388 | } |
| 2389 | } else { |
| 2390 | if (resolve_mode == LOOKUP_TYPES && !result.IsType()) { |
| 2391 | // We found a symbol but it's not a type. Continue the loop. |
| 2392 | } else { |
| 2393 | return result; |
| 2394 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2395 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2396 | } |
| 2397 | |
| 2398 | // Not found. Remove the name so we can try again. |
| 2399 | scope_to_try.erase(old_size); |
| 2400 | } |
| 2401 | } |
| 2402 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2403 | Symbol DescriptorBuilder::LookupSymbol( |
| 2404 | const string& name, const string& relative_to, |
| 2405 | PlaceholderType placeholder_type, ResolveMode resolve_mode) { |
| 2406 | Symbol result = LookupSymbolNoPlaceholder( |
| 2407 | name, relative_to, resolve_mode); |
| 2408 | if (result.IsNull() && pool_->allow_unknown_) { |
| 2409 | // Not found, but AllowUnknownDependencies() is enabled. Return a |
| 2410 | // placeholder instead. |
| 2411 | result = NewPlaceholder(name, placeholder_type); |
| 2412 | } |
| 2413 | return result; |
| 2414 | } |
| 2415 | |
| 2416 | Symbol DescriptorBuilder::NewPlaceholder(const string& name, |
| 2417 | PlaceholderType placeholder_type) { |
| 2418 | // Compute names. |
| 2419 | const string* placeholder_full_name; |
| 2420 | const string* placeholder_name; |
| 2421 | const string* placeholder_package; |
| 2422 | |
| 2423 | if (!ValidateQualifiedName(name)) return kNullSymbol; |
| 2424 | if (name[0] == '.') { |
| 2425 | // Fully-qualified. |
| 2426 | placeholder_full_name = tables_->AllocateString(name.substr(1)); |
| 2427 | } else { |
| 2428 | placeholder_full_name = tables_->AllocateString(name); |
| 2429 | } |
| 2430 | |
| 2431 | string::size_type dotpos = placeholder_full_name->find_last_of('.'); |
| 2432 | if (dotpos != string::npos) { |
| 2433 | placeholder_package = tables_->AllocateString( |
| 2434 | placeholder_full_name->substr(0, dotpos)); |
| 2435 | placeholder_name = tables_->AllocateString( |
| 2436 | placeholder_full_name->substr(dotpos + 1)); |
| 2437 | } else { |
| 2438 | placeholder_package = &kEmptyString; |
| 2439 | placeholder_name = placeholder_full_name; |
| 2440 | } |
| 2441 | |
| 2442 | // Create the placeholders. |
| 2443 | FileDescriptor* placeholder_file = tables_->Allocate<FileDescriptor>(); |
| 2444 | memset(placeholder_file, 0, sizeof(*placeholder_file)); |
| 2445 | |
| 2446 | placeholder_file->name_ = |
| 2447 | tables_->AllocateString(*placeholder_full_name + ".placeholder.proto"); |
| 2448 | placeholder_file->package_ = placeholder_package; |
| 2449 | placeholder_file->pool_ = pool_; |
| 2450 | placeholder_file->options_ = &FileOptions::default_instance(); |
| 2451 | placeholder_file->tables_ = &FileDescriptorTables::kEmpty; |
| 2452 | // All other fields are zero or NULL. |
| 2453 | |
| 2454 | if (placeholder_type == PLACEHOLDER_ENUM) { |
| 2455 | placeholder_file->enum_type_count_ = 1; |
| 2456 | placeholder_file->enum_types_ = |
| 2457 | tables_->AllocateArray<EnumDescriptor>(1); |
| 2458 | |
| 2459 | EnumDescriptor* placeholder_enum = &placeholder_file->enum_types_[0]; |
| 2460 | memset(placeholder_enum, 0, sizeof(*placeholder_enum)); |
| 2461 | |
| 2462 | placeholder_enum->full_name_ = placeholder_full_name; |
| 2463 | placeholder_enum->name_ = placeholder_name; |
| 2464 | placeholder_enum->file_ = placeholder_file; |
| 2465 | placeholder_enum->options_ = &EnumOptions::default_instance(); |
| 2466 | placeholder_enum->is_placeholder_ = true; |
| 2467 | placeholder_enum->is_unqualified_placeholder_ = (name[0] != '.'); |
| 2468 | |
| 2469 | // Enums must have at least one value. |
| 2470 | placeholder_enum->value_count_ = 1; |
| 2471 | placeholder_enum->values_ = tables_->AllocateArray<EnumValueDescriptor>(1); |
| 2472 | |
| 2473 | EnumValueDescriptor* placeholder_value = &placeholder_enum->values_[0]; |
| 2474 | memset(placeholder_value, 0, sizeof(*placeholder_value)); |
| 2475 | |
| 2476 | placeholder_value->name_ = tables_->AllocateString("PLACEHOLDER_VALUE"); |
| 2477 | // Note that enum value names are siblings of their type, not children. |
| 2478 | placeholder_value->full_name_ = |
| 2479 | placeholder_package->empty() ? placeholder_value->name_ : |
| 2480 | tables_->AllocateString(*placeholder_package + ".PLACEHOLDER_VALUE"); |
| 2481 | |
| 2482 | placeholder_value->number_ = 0; |
| 2483 | placeholder_value->type_ = placeholder_enum; |
| 2484 | placeholder_value->options_ = &EnumValueOptions::default_instance(); |
| 2485 | |
| 2486 | return Symbol(placeholder_enum); |
| 2487 | } else { |
| 2488 | placeholder_file->message_type_count_ = 1; |
| 2489 | placeholder_file->message_types_ = |
| 2490 | tables_->AllocateArray<Descriptor>(1); |
| 2491 | |
| 2492 | Descriptor* placeholder_message = &placeholder_file->message_types_[0]; |
| 2493 | memset(placeholder_message, 0, sizeof(*placeholder_message)); |
| 2494 | |
| 2495 | placeholder_message->full_name_ = placeholder_full_name; |
| 2496 | placeholder_message->name_ = placeholder_name; |
| 2497 | placeholder_message->file_ = placeholder_file; |
| 2498 | placeholder_message->options_ = &MessageOptions::default_instance(); |
| 2499 | placeholder_message->is_placeholder_ = true; |
| 2500 | placeholder_message->is_unqualified_placeholder_ = (name[0] != '.'); |
| 2501 | |
| 2502 | if (placeholder_type == PLACEHOLDER_EXTENDABLE_MESSAGE) { |
| 2503 | placeholder_message->extension_range_count_ = 1; |
| 2504 | placeholder_message->extension_ranges_ = |
| 2505 | tables_->AllocateArray<Descriptor::ExtensionRange>(1); |
| 2506 | placeholder_message->extension_ranges_->start = 1; |
| 2507 | // kMaxNumber + 1 because ExtensionRange::end is exclusive. |
| 2508 | placeholder_message->extension_ranges_->end = |
| 2509 | FieldDescriptor::kMaxNumber + 1; |
| 2510 | } |
| 2511 | |
| 2512 | return Symbol(placeholder_message); |
| 2513 | } |
| 2514 | } |
| 2515 | |
| 2516 | const FileDescriptor* DescriptorBuilder::NewPlaceholderFile( |
| 2517 | const string& name) { |
| 2518 | FileDescriptor* placeholder = tables_->Allocate<FileDescriptor>(); |
| 2519 | memset(placeholder, 0, sizeof(*placeholder)); |
| 2520 | |
| 2521 | placeholder->name_ = tables_->AllocateString(name); |
| 2522 | placeholder->package_ = &kEmptyString; |
| 2523 | placeholder->pool_ = pool_; |
| 2524 | placeholder->options_ = &FileOptions::default_instance(); |
| 2525 | placeholder->tables_ = &FileDescriptorTables::kEmpty; |
| 2526 | // All other fields are zero or NULL. |
| 2527 | |
| 2528 | return placeholder; |
| 2529 | } |
| 2530 | |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 2531 | bool DescriptorBuilder::AddSymbol( |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2532 | const string& full_name, const void* parent, const string& name, |
| 2533 | const Message& proto, Symbol symbol) { |
| 2534 | // If the caller passed NULL for the parent, the symbol is at file scope. |
| 2535 | // Use its file as the parent instead. |
| 2536 | if (parent == NULL) parent = file_; |
| 2537 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2538 | if (tables_->AddSymbol(full_name, symbol)) { |
| 2539 | if (!file_tables_->AddAliasUnderParent(parent, name, symbol)) { |
| 2540 | GOOGLE_LOG(DFATAL) << "\"" << full_name << "\" not previously defined in " |
| 2541 | "symbols_by_name_, but was defined in symbols_by_parent_; " |
| 2542 | "this shouldn't be possible."; |
| 2543 | return false; |
| 2544 | } |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 2545 | return true; |
| 2546 | } else { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2547 | const FileDescriptor* other_file = tables_->FindSymbol(full_name).GetFile(); |
| 2548 | if (other_file == file_) { |
| 2549 | string::size_type dot_pos = full_name.find_last_of('.'); |
| 2550 | if (dot_pos == string::npos) { |
| 2551 | AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2552 | "\"" + full_name + "\" is already defined."); |
| 2553 | } else { |
| 2554 | AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2555 | "\"" + full_name.substr(dot_pos + 1) + |
| 2556 | "\" is already defined in \"" + |
| 2557 | full_name.substr(0, dot_pos) + "\"."); |
| 2558 | } |
| 2559 | } else { |
| 2560 | // Symbol seems to have been defined in a different file. |
| 2561 | AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2562 | "\"" + full_name + "\" is already defined in file \"" + |
| 2563 | other_file->name() + "\"."); |
| 2564 | } |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 2565 | return false; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2566 | } |
| 2567 | } |
| 2568 | |
| 2569 | void DescriptorBuilder::AddPackage( |
| 2570 | const string& name, const Message& proto, const FileDescriptor* file) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2571 | if (tables_->AddSymbol(name, Symbol(file))) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2572 | // Success. Also add parent package, if any. |
| 2573 | string::size_type dot_pos = name.find_last_of('.'); |
| 2574 | if (dot_pos == string::npos) { |
| 2575 | // No parents. |
| 2576 | ValidateSymbolName(name, name, proto); |
| 2577 | } else { |
| 2578 | // Has parent. |
| 2579 | string* parent_name = tables_->AllocateString(name.substr(0, dot_pos)); |
| 2580 | AddPackage(*parent_name, proto, file); |
| 2581 | ValidateSymbolName(name.substr(dot_pos + 1), name, proto); |
| 2582 | } |
| 2583 | } else { |
| 2584 | Symbol existing_symbol = tables_->FindSymbol(name); |
| 2585 | // It's OK to redefine a package. |
| 2586 | if (existing_symbol.type != Symbol::PACKAGE) { |
| 2587 | // Symbol seems to have been defined in a different file. |
| 2588 | AddError(name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2589 | "\"" + name + "\" is already defined (as something other than " |
| 2590 | "a package) in file \"" + existing_symbol.GetFile()->name() + |
| 2591 | "\"."); |
| 2592 | } |
| 2593 | } |
| 2594 | } |
| 2595 | |
| 2596 | void DescriptorBuilder::ValidateSymbolName( |
| 2597 | const string& name, const string& full_name, const Message& proto) { |
| 2598 | if (name.empty()) { |
| 2599 | AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2600 | "Missing name."); |
| 2601 | } else { |
| 2602 | for (int i = 0; i < name.size(); i++) { |
| 2603 | // I don't trust isalnum() due to locales. :( |
| 2604 | if ((name[i] < 'a' || 'z' < name[i]) && |
| 2605 | (name[i] < 'A' || 'Z' < name[i]) && |
| 2606 | (name[i] < '0' || '9' < name[i]) && |
| 2607 | (name[i] != '_')) { |
| 2608 | AddError(full_name, proto, DescriptorPool::ErrorCollector::NAME, |
| 2609 | "\"" + name + "\" is not a valid identifier."); |
| 2610 | } |
| 2611 | } |
| 2612 | } |
| 2613 | } |
| 2614 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2615 | bool DescriptorBuilder::ValidateQualifiedName(const string& name) { |
| 2616 | bool last_was_period = false; |
| 2617 | |
| 2618 | for (int i = 0; i < name.size(); i++) { |
| 2619 | // I don't trust isalnum() due to locales. :( |
| 2620 | if (('a' <= name[i] && name[i] <= 'z') || |
| 2621 | ('A' <= name[i] && name[i] <= 'Z') || |
| 2622 | ('0' <= name[i] && name[i] <= '9') || |
| 2623 | (name[i] == '_')) { |
| 2624 | last_was_period = false; |
| 2625 | } else if (name[i] == '.') { |
| 2626 | if (last_was_period) return false; |
| 2627 | last_was_period = true; |
| 2628 | } else { |
| 2629 | return false; |
| 2630 | } |
| 2631 | } |
| 2632 | |
| 2633 | return !name.empty() && !last_was_period; |
| 2634 | } |
| 2635 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2636 | // ------------------------------------------------------------------- |
| 2637 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2638 | // This generic implementation is good for all descriptors except |
| 2639 | // FileDescriptor. |
| 2640 | template<class DescriptorT> void DescriptorBuilder::AllocateOptions( |
| 2641 | const typename DescriptorT::OptionsType& orig_options, |
| 2642 | DescriptorT* descriptor) { |
| 2643 | AllocateOptionsImpl(descriptor->full_name(), descriptor->full_name(), |
| 2644 | orig_options, descriptor); |
| 2645 | } |
| 2646 | |
| 2647 | // We specialize for FileDescriptor. |
kenton@google.com | de75437 | 2008-11-06 21:36:28 +0000 | [diff] [blame] | 2648 | void DescriptorBuilder::AllocateOptions(const FileOptions& orig_options, |
| 2649 | FileDescriptor* descriptor) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2650 | // We add the dummy token so that LookupSymbol does the right thing. |
| 2651 | AllocateOptionsImpl(descriptor->package() + ".dummy", descriptor->name(), |
| 2652 | orig_options, descriptor); |
| 2653 | } |
| 2654 | |
| 2655 | template<class DescriptorT> void DescriptorBuilder::AllocateOptionsImpl( |
| 2656 | const string& name_scope, |
| 2657 | const string& element_name, |
| 2658 | const typename DescriptorT::OptionsType& orig_options, |
| 2659 | DescriptorT* descriptor) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2660 | // We need to use a dummy pointer to work around a bug in older versions of |
| 2661 | // GCC. Otherwise, the following two lines could be replaced with: |
| 2662 | // typename DescriptorT::OptionsType* options = |
| 2663 | // tables_->AllocateMessage<typename DescriptorT::OptionsType>(); |
| 2664 | typename DescriptorT::OptionsType* const dummy = NULL; |
| 2665 | typename DescriptorT::OptionsType* options = tables_->AllocateMessage(dummy); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2666 | options->CopyFrom(orig_options); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2667 | descriptor->options_ = options; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2668 | |
| 2669 | // Don't add to options_to_interpret_ unless there were uninterpreted |
| 2670 | // options. This not only avoids unnecessary work, but prevents a |
| 2671 | // bootstrapping problem when building descriptors for descriptor.proto. |
| 2672 | // descriptor.proto does not contain any uninterpreted options, but |
| 2673 | // attempting to interpret options anyway will cause |
| 2674 | // OptionsType::GetDescriptor() to be called which may then deadlock since |
| 2675 | // we're still trying to build it. |
| 2676 | if (options->uninterpreted_option_size() > 0) { |
| 2677 | options_to_interpret_.push_back( |
| 2678 | OptionsToInterpret(name_scope, element_name, &orig_options, options)); |
| 2679 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2680 | } |
| 2681 | |
| 2682 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2683 | // A common pattern: We want to convert a repeated field in the descriptor |
| 2684 | // to an array of values, calling some method to build each value. |
| 2685 | #define BUILD_ARRAY(INPUT, OUTPUT, NAME, METHOD, PARENT) \ |
| 2686 | OUTPUT->NAME##_count_ = INPUT.NAME##_size(); \ |
| 2687 | AllocateArray(INPUT.NAME##_size(), &OUTPUT->NAME##s_); \ |
| 2688 | for (int i = 0; i < INPUT.NAME##_size(); i++) { \ |
| 2689 | METHOD(INPUT.NAME(i), PARENT, OUTPUT->NAME##s_ + i); \ |
| 2690 | } |
| 2691 | |
| 2692 | const FileDescriptor* DescriptorBuilder::BuildFile( |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2693 | const FileDescriptorProto& proto) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2694 | filename_ = proto.name(); |
| 2695 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2696 | // Check if the file already exists and is identical to the one being built. |
| 2697 | // Note: This only works if the input is canonical -- that is, it |
| 2698 | // fully-qualifies all type names, has no UninterpretedOptions, etc. |
| 2699 | // This is fine, because this idempotency "feature" really only exists to |
| 2700 | // accomodate one hack in the proto1->proto2 migration layer. |
| 2701 | const FileDescriptor* existing_file = tables_->FindFile(filename_); |
| 2702 | if (existing_file != NULL) { |
| 2703 | // File already in pool. Compare the existing one to the input. |
| 2704 | FileDescriptorProto existing_proto; |
| 2705 | existing_file->CopyTo(&existing_proto); |
| 2706 | if (existing_proto.SerializeAsString() == proto.SerializeAsString()) { |
| 2707 | // They're identical. Return the existing descriptor. |
| 2708 | return existing_file; |
| 2709 | } |
| 2710 | |
| 2711 | // Not a match. The error will be detected and handled later. |
| 2712 | } |
| 2713 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2714 | // Check to see if this file is already on the pending files list. |
| 2715 | // TODO(kenton): Allow recursive imports? It may not work with some |
| 2716 | // (most?) programming languages. E.g., in C++, a forward declaration |
| 2717 | // of a type is not sufficient to allow it to be used even in a |
| 2718 | // generated header file due to inlining. This could perhaps be |
| 2719 | // worked around using tricks involving inserting #include statements |
| 2720 | // mid-file, but that's pretty ugly, and I'm pretty sure there are |
| 2721 | // some languages out there that do not allow recursive dependencies |
| 2722 | // at all. |
| 2723 | for (int i = 0; i < tables_->pending_files_.size(); i++) { |
| 2724 | if (tables_->pending_files_[i] == proto.name()) { |
| 2725 | string error_message("File recursively imports itself: "); |
| 2726 | for (; i < tables_->pending_files_.size(); i++) { |
| 2727 | error_message.append(tables_->pending_files_[i]); |
| 2728 | error_message.append(" -> "); |
| 2729 | } |
| 2730 | error_message.append(proto.name()); |
| 2731 | |
| 2732 | AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, |
| 2733 | error_message); |
| 2734 | return NULL; |
| 2735 | } |
| 2736 | } |
| 2737 | |
| 2738 | // If we have a fallback_database_, attempt to load all dependencies now, |
| 2739 | // before checkpointing tables_. This avoids confusion with recursive |
| 2740 | // checkpoints. |
| 2741 | if (pool_->fallback_database_ != NULL) { |
| 2742 | tables_->pending_files_.push_back(proto.name()); |
| 2743 | for (int i = 0; i < proto.dependency_size(); i++) { |
| 2744 | if (tables_->FindFile(proto.dependency(i)) == NULL && |
| 2745 | (pool_->underlay_ == NULL || |
| 2746 | pool_->underlay_->FindFileByName(proto.dependency(i)) == NULL)) { |
| 2747 | // We don't care what this returns since we'll find out below anyway. |
| 2748 | pool_->TryFindFileInFallbackDatabase(proto.dependency(i)); |
| 2749 | } |
| 2750 | } |
| 2751 | tables_->pending_files_.pop_back(); |
| 2752 | } |
| 2753 | |
| 2754 | // Checkpoint the tables so that we can roll back if something goes wrong. |
| 2755 | tables_->Checkpoint(); |
| 2756 | |
| 2757 | FileDescriptor* result = tables_->Allocate<FileDescriptor>(); |
| 2758 | file_ = result; |
| 2759 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2760 | file_tables_ = tables_->AllocateFileTables(); |
| 2761 | file_->tables_ = file_tables_; |
| 2762 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2763 | if (!proto.has_name()) { |
| 2764 | AddError("", proto, DescriptorPool::ErrorCollector::OTHER, |
| 2765 | "Missing field: FileDescriptorProto.name."); |
| 2766 | } |
| 2767 | |
| 2768 | result->name_ = tables_->AllocateString(proto.name()); |
temporal | 6fdb096 | 2008-07-25 04:38:05 +0000 | [diff] [blame] | 2769 | if (proto.has_package()) { |
| 2770 | result->package_ = tables_->AllocateString(proto.package()); |
| 2771 | } else { |
| 2772 | // We cannot rely on proto.package() returning a valid string if |
| 2773 | // proto.has_package() is false, because we might be running at static |
| 2774 | // initialization time, in which case default values have not yet been |
| 2775 | // initialized. |
| 2776 | result->package_ = tables_->AllocateString(""); |
| 2777 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2778 | result->pool_ = pool_; |
| 2779 | |
| 2780 | // Add to tables. |
| 2781 | if (!tables_->AddFile(result)) { |
| 2782 | AddError(proto.name(), proto, DescriptorPool::ErrorCollector::OTHER, |
| 2783 | "A file with this name is already in the pool."); |
| 2784 | // Bail out early so that if this is actually the exact same file, we |
| 2785 | // don't end up reporting that every single symbol is already defined. |
| 2786 | tables_->Rollback(); |
| 2787 | return NULL; |
| 2788 | } |
| 2789 | if (!result->package().empty()) { |
| 2790 | AddPackage(result->package(), proto, result); |
| 2791 | } |
| 2792 | |
| 2793 | // Make sure all dependencies are loaded. |
| 2794 | set<string> seen_dependencies; |
| 2795 | result->dependency_count_ = proto.dependency_size(); |
| 2796 | result->dependencies_ = |
| 2797 | tables_->AllocateArray<const FileDescriptor*>(proto.dependency_size()); |
| 2798 | for (int i = 0; i < proto.dependency_size(); i++) { |
| 2799 | if (!seen_dependencies.insert(proto.dependency(i)).second) { |
| 2800 | AddError(proto.name(), proto, |
| 2801 | DescriptorPool::ErrorCollector::OTHER, |
| 2802 | "Import \"" + proto.dependency(i) + "\" was listed twice."); |
| 2803 | } |
| 2804 | |
| 2805 | const FileDescriptor* dependency = tables_->FindFile(proto.dependency(i)); |
| 2806 | if (dependency == NULL && pool_->underlay_ != NULL) { |
| 2807 | dependency = pool_->underlay_->FindFileByName(proto.dependency(i)); |
| 2808 | } |
| 2809 | |
| 2810 | if (dependency == NULL) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2811 | if (pool_->allow_unknown_) { |
| 2812 | dependency = NewPlaceholderFile(proto.dependency(i)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2813 | } else { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2814 | string message; |
| 2815 | if (pool_->fallback_database_ == NULL) { |
| 2816 | message = "Import \"" + proto.dependency(i) + |
| 2817 | "\" has not been loaded."; |
| 2818 | } else { |
| 2819 | message = "Import \"" + proto.dependency(i) + |
| 2820 | "\" was not found or had errors."; |
| 2821 | } |
| 2822 | AddError(proto.name(), proto, |
| 2823 | DescriptorPool::ErrorCollector::OTHER, |
| 2824 | message); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2825 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2826 | } |
| 2827 | |
| 2828 | result->dependencies_[i] = dependency; |
| 2829 | } |
| 2830 | |
| 2831 | // Convert children. |
| 2832 | BUILD_ARRAY(proto, result, message_type, BuildMessage , NULL); |
| 2833 | BUILD_ARRAY(proto, result, enum_type , BuildEnum , NULL); |
| 2834 | BUILD_ARRAY(proto, result, service , BuildService , NULL); |
| 2835 | BUILD_ARRAY(proto, result, extension , BuildExtension, NULL); |
| 2836 | |
| 2837 | // Copy options. |
| 2838 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2839 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2840 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2841 | AllocateOptions(proto.options(), result); |
| 2842 | } |
| 2843 | |
| 2844 | // Note that the following steps must occur in exactly the specified order. |
| 2845 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2846 | // Cross-link. |
| 2847 | CrossLinkFile(result, proto); |
| 2848 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2849 | // Interpret any remaining uninterpreted options gathered into |
| 2850 | // options_to_interpret_ during descriptor building. Cross-linking has made |
| 2851 | // extension options known, so all interpretations should now succeed. |
| 2852 | if (!had_errors_) { |
| 2853 | OptionInterpreter option_interpreter(this); |
| 2854 | for (vector<OptionsToInterpret>::iterator iter = |
| 2855 | options_to_interpret_.begin(); |
| 2856 | iter != options_to_interpret_.end(); ++iter) { |
| 2857 | option_interpreter.InterpretOptions(&(*iter)); |
| 2858 | } |
| 2859 | options_to_interpret_.clear(); |
| 2860 | } |
| 2861 | |
| 2862 | // Validate options. |
| 2863 | if (!had_errors_) { |
| 2864 | ValidateFileOptions(result, proto); |
| 2865 | } |
| 2866 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2867 | if (had_errors_) { |
| 2868 | tables_->Rollback(); |
| 2869 | return NULL; |
| 2870 | } else { |
| 2871 | tables_->Checkpoint(); |
| 2872 | return result; |
| 2873 | } |
| 2874 | } |
| 2875 | |
| 2876 | void DescriptorBuilder::BuildMessage(const DescriptorProto& proto, |
| 2877 | const Descriptor* parent, |
| 2878 | Descriptor* result) { |
| 2879 | const string& scope = (parent == NULL) ? |
| 2880 | file_->package() : parent->full_name(); |
| 2881 | string* full_name = tables_->AllocateString(scope); |
| 2882 | if (!full_name->empty()) full_name->append(1, '.'); |
| 2883 | full_name->append(proto.name()); |
| 2884 | |
| 2885 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 2886 | |
| 2887 | result->name_ = tables_->AllocateString(proto.name()); |
| 2888 | result->full_name_ = full_name; |
| 2889 | result->file_ = file_; |
| 2890 | result->containing_type_ = parent; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2891 | result->is_placeholder_ = false; |
| 2892 | result->is_unqualified_placeholder_ = false; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2893 | |
| 2894 | BUILD_ARRAY(proto, result, field , BuildField , result); |
| 2895 | BUILD_ARRAY(proto, result, nested_type , BuildMessage , result); |
| 2896 | BUILD_ARRAY(proto, result, enum_type , BuildEnum , result); |
| 2897 | BUILD_ARRAY(proto, result, extension_range, BuildExtensionRange, result); |
| 2898 | BUILD_ARRAY(proto, result, extension , BuildExtension , result); |
| 2899 | |
| 2900 | // Copy options. |
| 2901 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2902 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2903 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2904 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
| 2907 | AddSymbol(result->full_name(), parent, result->name(), |
| 2908 | proto, Symbol(result)); |
| 2909 | |
| 2910 | // Check that no fields have numbers in extension ranges. |
| 2911 | for (int i = 0; i < result->field_count(); i++) { |
| 2912 | const FieldDescriptor* field = result->field(i); |
| 2913 | for (int j = 0; j < result->extension_range_count(); j++) { |
| 2914 | const Descriptor::ExtensionRange* range = result->extension_range(j); |
| 2915 | if (range->start <= field->number() && field->number() < range->end) { |
| 2916 | AddError(field->full_name(), proto.extension_range(j), |
| 2917 | DescriptorPool::ErrorCollector::NUMBER, |
| 2918 | strings::Substitute( |
| 2919 | "Extension range $0 to $1 includes field \"$2\" ($3).", |
| 2920 | range->start, range->end - 1, |
| 2921 | field->name(), field->number())); |
| 2922 | } |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | // Check that extension ranges don't overlap. |
| 2927 | for (int i = 0; i < result->extension_range_count(); i++) { |
| 2928 | const Descriptor::ExtensionRange* range1 = result->extension_range(i); |
| 2929 | for (int j = i + 1; j < result->extension_range_count(); j++) { |
| 2930 | const Descriptor::ExtensionRange* range2 = result->extension_range(j); |
| 2931 | if (range1->end > range2->start && range2->end > range1->start) { |
| 2932 | AddError(result->full_name(), proto.extension_range(j), |
| 2933 | DescriptorPool::ErrorCollector::NUMBER, |
| 2934 | strings::Substitute("Extension range $0 to $1 overlaps with " |
| 2935 | "already-defined range $2 to $3.", |
| 2936 | range2->start, range2->end - 1, |
| 2937 | range1->start, range1->end - 1)); |
| 2938 | } |
| 2939 | } |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | void DescriptorBuilder::BuildFieldOrExtension(const FieldDescriptorProto& proto, |
| 2944 | const Descriptor* parent, |
| 2945 | FieldDescriptor* result, |
| 2946 | bool is_extension) { |
| 2947 | const string& scope = (parent == NULL) ? |
| 2948 | file_->package() : parent->full_name(); |
| 2949 | string* full_name = tables_->AllocateString(scope); |
| 2950 | if (!full_name->empty()) full_name->append(1, '.'); |
| 2951 | full_name->append(proto.name()); |
| 2952 | |
| 2953 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 2954 | |
| 2955 | result->name_ = tables_->AllocateString(proto.name()); |
| 2956 | result->full_name_ = full_name; |
| 2957 | result->file_ = file_; |
| 2958 | result->number_ = proto.number(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2959 | result->is_extension_ = is_extension; |
| 2960 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 2961 | // If .proto files follow the style guide then the name should already be |
| 2962 | // lower-cased. If that's the case we can just reuse the string we already |
| 2963 | // allocated rather than allocate a new one. |
| 2964 | string lowercase_name(proto.name()); |
| 2965 | LowerString(&lowercase_name); |
| 2966 | if (lowercase_name == proto.name()) { |
| 2967 | result->lowercase_name_ = result->name_; |
| 2968 | } else { |
| 2969 | result->lowercase_name_ = tables_->AllocateString(lowercase_name); |
| 2970 | } |
| 2971 | |
| 2972 | // Don't bother with the above optimization for camel-case names since |
| 2973 | // .proto files that follow the guide shouldn't be using names in this |
| 2974 | // format, so the optimization wouldn't help much. |
| 2975 | result->camelcase_name_ = tables_->AllocateString(ToCamelCase(proto.name())); |
| 2976 | |
kenton@google.com | a2a32c2 | 2008-11-14 17:29:32 +0000 | [diff] [blame] | 2977 | // Some compilers do not allow static_cast directly between two enum types, |
| 2978 | // so we must cast to int first. |
| 2979 | result->type_ = static_cast<FieldDescriptor::Type>( |
| 2980 | implicit_cast<int>(proto.type())); |
| 2981 | result->label_ = static_cast<FieldDescriptor::Label>( |
| 2982 | implicit_cast<int>(proto.label())); |
| 2983 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2984 | // Some of these may be filled in when cross-linking. |
| 2985 | result->containing_type_ = NULL; |
| 2986 | result->extension_scope_ = NULL; |
| 2987 | result->experimental_map_key_ = NULL; |
| 2988 | result->message_type_ = NULL; |
| 2989 | result->enum_type_ = NULL; |
| 2990 | |
| 2991 | result->has_default_value_ = proto.has_default_value(); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 2992 | if (proto.has_default_value() && result->is_repeated()) { |
| 2993 | AddError(result->full_name(), proto, |
| 2994 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 2995 | "Repeated fields can't have default values."); |
| 2996 | } |
| 2997 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 2998 | if (proto.has_type()) { |
| 2999 | if (proto.has_default_value()) { |
| 3000 | char* end_pos = NULL; |
| 3001 | switch (result->cpp_type()) { |
| 3002 | case FieldDescriptor::CPPTYPE_INT32: |
| 3003 | result->default_value_int32_ = |
| 3004 | strtol(proto.default_value().c_str(), &end_pos, 0); |
| 3005 | break; |
| 3006 | case FieldDescriptor::CPPTYPE_INT64: |
| 3007 | result->default_value_int64_ = |
| 3008 | strto64(proto.default_value().c_str(), &end_pos, 0); |
| 3009 | break; |
| 3010 | case FieldDescriptor::CPPTYPE_UINT32: |
| 3011 | result->default_value_uint32_ = |
| 3012 | strtoul(proto.default_value().c_str(), &end_pos, 0); |
| 3013 | break; |
| 3014 | case FieldDescriptor::CPPTYPE_UINT64: |
| 3015 | result->default_value_uint64_ = |
| 3016 | strtou64(proto.default_value().c_str(), &end_pos, 0); |
| 3017 | break; |
| 3018 | case FieldDescriptor::CPPTYPE_FLOAT: |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 3019 | if (proto.default_value() == "inf") { |
| 3020 | result->default_value_float_ = numeric_limits<float>::infinity(); |
| 3021 | } else if (proto.default_value() == "-inf") { |
| 3022 | result->default_value_float_ = -numeric_limits<float>::infinity(); |
| 3023 | } else if (proto.default_value() == "nan") { |
| 3024 | result->default_value_float_ = numeric_limits<float>::quiet_NaN(); |
| 3025 | } else { |
| 3026 | result->default_value_float_ = |
| 3027 | NoLocaleStrtod(proto.default_value().c_str(), &end_pos); |
| 3028 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3029 | break; |
| 3030 | case FieldDescriptor::CPPTYPE_DOUBLE: |
kenton@google.com | 684d45b | 2009-12-19 04:50:00 +0000 | [diff] [blame] | 3031 | if (proto.default_value() == "inf") { |
| 3032 | result->default_value_double_ = numeric_limits<double>::infinity(); |
| 3033 | } else if (proto.default_value() == "-inf") { |
| 3034 | result->default_value_double_ = -numeric_limits<double>::infinity(); |
| 3035 | } else if (proto.default_value() == "nan") { |
| 3036 | result->default_value_double_ = numeric_limits<double>::quiet_NaN(); |
| 3037 | } else { |
| 3038 | result->default_value_double_ = |
| 3039 | NoLocaleStrtod(proto.default_value().c_str(), &end_pos); |
| 3040 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3041 | break; |
| 3042 | case FieldDescriptor::CPPTYPE_BOOL: |
| 3043 | if (proto.default_value() == "true") { |
| 3044 | result->default_value_bool_ = true; |
| 3045 | } else if (proto.default_value() == "false") { |
| 3046 | result->default_value_bool_ = false; |
| 3047 | } else { |
| 3048 | AddError(result->full_name(), proto, |
| 3049 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 3050 | "Boolean default must be true or false."); |
| 3051 | } |
| 3052 | break; |
| 3053 | case FieldDescriptor::CPPTYPE_ENUM: |
| 3054 | // This will be filled in when cross-linking. |
| 3055 | result->default_value_enum_ = NULL; |
| 3056 | break; |
| 3057 | case FieldDescriptor::CPPTYPE_STRING: |
| 3058 | if (result->type() == FieldDescriptor::TYPE_BYTES) { |
| 3059 | result->default_value_string_ = tables_->AllocateString( |
| 3060 | UnescapeCEscapeString(proto.default_value())); |
| 3061 | } else { |
| 3062 | result->default_value_string_ = |
| 3063 | tables_->AllocateString(proto.default_value()); |
| 3064 | } |
| 3065 | break; |
| 3066 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 3067 | AddError(result->full_name(), proto, |
| 3068 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 3069 | "Messages can't have default values."); |
| 3070 | result->has_default_value_ = false; |
| 3071 | break; |
| 3072 | } |
| 3073 | |
| 3074 | if (end_pos != NULL) { |
| 3075 | // end_pos is only set non-NULL by the parsers for numeric types, above. |
| 3076 | // This checks that the default was non-empty and had no extra junk |
| 3077 | // after the end of the number. |
| 3078 | if (proto.default_value().empty() || *end_pos != '\0') { |
| 3079 | AddError(result->full_name(), proto, |
| 3080 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 3081 | "Couldn't parse default value."); |
| 3082 | } |
| 3083 | } |
| 3084 | } else { |
| 3085 | // No explicit default value |
| 3086 | switch (result->cpp_type()) { |
| 3087 | case FieldDescriptor::CPPTYPE_INT32: |
| 3088 | result->default_value_int32_ = 0; |
| 3089 | break; |
| 3090 | case FieldDescriptor::CPPTYPE_INT64: |
| 3091 | result->default_value_int64_ = 0; |
| 3092 | break; |
| 3093 | case FieldDescriptor::CPPTYPE_UINT32: |
| 3094 | result->default_value_uint32_ = 0; |
| 3095 | break; |
| 3096 | case FieldDescriptor::CPPTYPE_UINT64: |
| 3097 | result->default_value_uint64_ = 0; |
| 3098 | break; |
| 3099 | case FieldDescriptor::CPPTYPE_FLOAT: |
| 3100 | result->default_value_float_ = 0.0f; |
| 3101 | break; |
| 3102 | case FieldDescriptor::CPPTYPE_DOUBLE: |
| 3103 | result->default_value_double_ = 0.0; |
| 3104 | break; |
| 3105 | case FieldDescriptor::CPPTYPE_BOOL: |
| 3106 | result->default_value_bool_ = false; |
| 3107 | break; |
| 3108 | case FieldDescriptor::CPPTYPE_ENUM: |
| 3109 | // This will be filled in when cross-linking. |
| 3110 | result->default_value_enum_ = NULL; |
| 3111 | break; |
| 3112 | case FieldDescriptor::CPPTYPE_STRING: |
| 3113 | result->default_value_string_ = &kEmptyString; |
| 3114 | break; |
| 3115 | case FieldDescriptor::CPPTYPE_MESSAGE: |
| 3116 | break; |
| 3117 | } |
| 3118 | } |
| 3119 | } |
| 3120 | |
| 3121 | if (result->number() <= 0) { |
| 3122 | AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, |
| 3123 | "Field numbers must be positive integers."); |
| 3124 | } else if (result->number() > FieldDescriptor::kMaxNumber) { |
| 3125 | AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, |
| 3126 | strings::Substitute("Field numbers cannot be greater than $0.", |
| 3127 | FieldDescriptor::kMaxNumber)); |
| 3128 | } else if (result->number() >= FieldDescriptor::kFirstReservedNumber && |
| 3129 | result->number() <= FieldDescriptor::kLastReservedNumber) { |
| 3130 | AddError(result->full_name(), proto, DescriptorPool::ErrorCollector::NUMBER, |
| 3131 | strings::Substitute( |
| 3132 | "Field numbers $0 through $1 are reserved for the protocol " |
| 3133 | "buffer library implementation.", |
| 3134 | FieldDescriptor::kFirstReservedNumber, |
| 3135 | FieldDescriptor::kLastReservedNumber)); |
| 3136 | } |
| 3137 | |
| 3138 | if (is_extension) { |
| 3139 | if (!proto.has_extendee()) { |
| 3140 | AddError(result->full_name(), proto, |
| 3141 | DescriptorPool::ErrorCollector::EXTENDEE, |
| 3142 | "FieldDescriptorProto.extendee not set for extension field."); |
| 3143 | } |
| 3144 | |
| 3145 | result->extension_scope_ = parent; |
| 3146 | } else { |
| 3147 | if (proto.has_extendee()) { |
| 3148 | AddError(result->full_name(), proto, |
| 3149 | DescriptorPool::ErrorCollector::EXTENDEE, |
| 3150 | "FieldDescriptorProto.extendee set for non-extension field."); |
| 3151 | } |
| 3152 | |
| 3153 | result->containing_type_ = parent; |
| 3154 | } |
| 3155 | |
| 3156 | // Copy options. |
| 3157 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3158 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3159 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3160 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3161 | } |
| 3162 | |
| 3163 | AddSymbol(result->full_name(), parent, result->name(), |
| 3164 | proto, Symbol(result)); |
| 3165 | } |
| 3166 | |
| 3167 | void DescriptorBuilder::BuildExtensionRange( |
| 3168 | const DescriptorProto::ExtensionRange& proto, |
| 3169 | const Descriptor* parent, |
| 3170 | Descriptor::ExtensionRange* result) { |
| 3171 | result->start = proto.start(); |
| 3172 | result->end = proto.end(); |
| 3173 | if (result->start <= 0) { |
| 3174 | AddError(parent->full_name(), proto, |
| 3175 | DescriptorPool::ErrorCollector::NUMBER, |
| 3176 | "Extension numbers must be positive integers."); |
| 3177 | } |
| 3178 | |
| 3179 | if (result->end > FieldDescriptor::kMaxNumber + 1) { |
| 3180 | AddError(parent->full_name(), proto, |
| 3181 | DescriptorPool::ErrorCollector::NUMBER, |
| 3182 | strings::Substitute("Extension numbers cannot be greater than $0.", |
| 3183 | FieldDescriptor::kMaxNumber)); |
| 3184 | } |
| 3185 | |
| 3186 | if (result->start >= result->end) { |
| 3187 | AddError(parent->full_name(), proto, |
| 3188 | DescriptorPool::ErrorCollector::NUMBER, |
| 3189 | "Extension range end number must be greater than start number."); |
| 3190 | } |
| 3191 | } |
| 3192 | |
| 3193 | void DescriptorBuilder::BuildEnum(const EnumDescriptorProto& proto, |
| 3194 | const Descriptor* parent, |
| 3195 | EnumDescriptor* result) { |
| 3196 | const string& scope = (parent == NULL) ? |
| 3197 | file_->package() : parent->full_name(); |
| 3198 | string* full_name = tables_->AllocateString(scope); |
| 3199 | if (!full_name->empty()) full_name->append(1, '.'); |
| 3200 | full_name->append(proto.name()); |
| 3201 | |
| 3202 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 3203 | |
| 3204 | result->name_ = tables_->AllocateString(proto.name()); |
| 3205 | result->full_name_ = full_name; |
| 3206 | result->file_ = file_; |
| 3207 | result->containing_type_ = parent; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3208 | result->is_placeholder_ = false; |
| 3209 | result->is_unqualified_placeholder_ = false; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3210 | |
| 3211 | if (proto.value_size() == 0) { |
| 3212 | // We cannot allow enums with no values because this would mean there |
| 3213 | // would be no valid default value for fields of this type. |
| 3214 | AddError(result->full_name(), proto, |
| 3215 | DescriptorPool::ErrorCollector::NAME, |
| 3216 | "Enums must contain at least one value."); |
| 3217 | } |
| 3218 | |
| 3219 | BUILD_ARRAY(proto, result, value, BuildEnumValue, result); |
| 3220 | |
| 3221 | // Copy options. |
| 3222 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3223 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3224 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3225 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3226 | } |
| 3227 | |
| 3228 | AddSymbol(result->full_name(), parent, result->name(), |
| 3229 | proto, Symbol(result)); |
| 3230 | } |
| 3231 | |
| 3232 | void DescriptorBuilder::BuildEnumValue(const EnumValueDescriptorProto& proto, |
| 3233 | const EnumDescriptor* parent, |
| 3234 | EnumValueDescriptor* result) { |
| 3235 | result->name_ = tables_->AllocateString(proto.name()); |
| 3236 | result->number_ = proto.number(); |
| 3237 | result->type_ = parent; |
| 3238 | |
| 3239 | // Note: full_name for enum values is a sibling to the parent's name, not a |
| 3240 | // child of it. |
| 3241 | string* full_name = tables_->AllocateString(*parent->full_name_); |
| 3242 | full_name->resize(full_name->size() - parent->name_->size()); |
| 3243 | full_name->append(*result->name_); |
| 3244 | result->full_name_ = full_name; |
| 3245 | |
| 3246 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 3247 | |
| 3248 | // Copy options. |
| 3249 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3250 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3251 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3252 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| 3255 | // Again, enum values are weird because we makes them appear as siblings |
| 3256 | // of the enum type instead of children of it. So, we use |
| 3257 | // parent->containing_type() as the value's parent. |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 3258 | bool added_to_outer_scope = |
| 3259 | AddSymbol(result->full_name(), parent->containing_type(), result->name(), |
| 3260 | proto, Symbol(result)); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3261 | |
| 3262 | // However, we also want to be able to search for values within a single |
| 3263 | // enum type, so we add it as a child of the enum type itself, too. |
| 3264 | // Note: This could fail, but if it does, the error has already been |
| 3265 | // reported by the above AddSymbol() call, so we ignore the return code. |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 3266 | bool added_to_inner_scope = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3267 | file_tables_->AddAliasUnderParent(parent, result->name(), Symbol(result)); |
temporal | f206351 | 2008-07-23 01:19:07 +0000 | [diff] [blame] | 3268 | |
| 3269 | if (added_to_inner_scope && !added_to_outer_scope) { |
| 3270 | // This value did not conflict with any values defined in the same enum, |
| 3271 | // but it did conflict with some other symbol defined in the enum type's |
| 3272 | // scope. Let's print an additional error to explain this. |
| 3273 | string outer_scope; |
| 3274 | if (parent->containing_type() == NULL) { |
| 3275 | outer_scope = file_->package(); |
| 3276 | } else { |
| 3277 | outer_scope = parent->containing_type()->full_name(); |
| 3278 | } |
| 3279 | |
| 3280 | if (outer_scope.empty()) { |
| 3281 | outer_scope = "the global scope"; |
| 3282 | } else { |
| 3283 | outer_scope = "\"" + outer_scope + "\""; |
| 3284 | } |
| 3285 | |
| 3286 | AddError(result->full_name(), proto, |
| 3287 | DescriptorPool::ErrorCollector::NAME, |
| 3288 | "Note that enum values use C++ scoping rules, meaning that " |
| 3289 | "enum values are siblings of their type, not children of it. " |
| 3290 | "Therefore, \"" + result->name() + "\" must be unique within " |
| 3291 | + outer_scope + ", not just within \"" + parent->name() + "\"."); |
| 3292 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3293 | |
| 3294 | // An enum is allowed to define two numbers that refer to the same value. |
| 3295 | // FindValueByNumber() should return the first such value, so we simply |
| 3296 | // ignore AddEnumValueByNumber()'s return code. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3297 | file_tables_->AddEnumValueByNumber(result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | void DescriptorBuilder::BuildService(const ServiceDescriptorProto& proto, |
| 3301 | const void* dummy, |
| 3302 | ServiceDescriptor* result) { |
| 3303 | string* full_name = tables_->AllocateString(file_->package()); |
| 3304 | if (!full_name->empty()) full_name->append(1, '.'); |
| 3305 | full_name->append(proto.name()); |
| 3306 | |
| 3307 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 3308 | |
| 3309 | result->name_ = tables_->AllocateString(proto.name()); |
| 3310 | result->full_name_ = full_name; |
| 3311 | result->file_ = file_; |
| 3312 | |
| 3313 | BUILD_ARRAY(proto, result, method, BuildMethod, result); |
| 3314 | |
| 3315 | // Copy options. |
| 3316 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3317 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3318 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3319 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3320 | } |
| 3321 | |
| 3322 | AddSymbol(result->full_name(), NULL, result->name(), |
| 3323 | proto, Symbol(result)); |
| 3324 | } |
| 3325 | |
| 3326 | void DescriptorBuilder::BuildMethod(const MethodDescriptorProto& proto, |
| 3327 | const ServiceDescriptor* parent, |
| 3328 | MethodDescriptor* result) { |
| 3329 | result->name_ = tables_->AllocateString(proto.name()); |
| 3330 | result->service_ = parent; |
| 3331 | |
| 3332 | string* full_name = tables_->AllocateString(parent->full_name()); |
| 3333 | full_name->append(1, '.'); |
| 3334 | full_name->append(*result->name_); |
| 3335 | result->full_name_ = full_name; |
| 3336 | |
| 3337 | ValidateSymbolName(proto.name(), *full_name, proto); |
| 3338 | |
| 3339 | // These will be filled in when cross-linking. |
| 3340 | result->input_type_ = NULL; |
| 3341 | result->output_type_ = NULL; |
| 3342 | |
| 3343 | // Copy options. |
| 3344 | if (!proto.has_options()) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3345 | result->options_ = NULL; // Will set to default_instance later. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3346 | } else { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3347 | AllocateOptions(proto.options(), result); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
| 3350 | AddSymbol(result->full_name(), parent, result->name(), |
| 3351 | proto, Symbol(result)); |
| 3352 | } |
| 3353 | |
| 3354 | #undef BUILD_ARRAY |
| 3355 | |
| 3356 | // ------------------------------------------------------------------- |
| 3357 | |
| 3358 | void DescriptorBuilder::CrossLinkFile( |
| 3359 | FileDescriptor* file, const FileDescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3360 | if (file->options_ == NULL) { |
| 3361 | file->options_ = &FileOptions::default_instance(); |
| 3362 | } |
| 3363 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3364 | for (int i = 0; i < file->message_type_count(); i++) { |
| 3365 | CrossLinkMessage(&file->message_types_[i], proto.message_type(i)); |
| 3366 | } |
| 3367 | |
| 3368 | for (int i = 0; i < file->extension_count(); i++) { |
| 3369 | CrossLinkField(&file->extensions_[i], proto.extension(i)); |
| 3370 | } |
| 3371 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3372 | for (int i = 0; i < file->enum_type_count(); i++) { |
| 3373 | CrossLinkEnum(&file->enum_types_[i], proto.enum_type(i)); |
| 3374 | } |
| 3375 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3376 | for (int i = 0; i < file->service_count(); i++) { |
| 3377 | CrossLinkService(&file->services_[i], proto.service(i)); |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | void DescriptorBuilder::CrossLinkMessage( |
| 3382 | Descriptor* message, const DescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3383 | if (message->options_ == NULL) { |
| 3384 | message->options_ = &MessageOptions::default_instance(); |
| 3385 | } |
| 3386 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3387 | for (int i = 0; i < message->nested_type_count(); i++) { |
| 3388 | CrossLinkMessage(&message->nested_types_[i], proto.nested_type(i)); |
| 3389 | } |
| 3390 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3391 | for (int i = 0; i < message->enum_type_count(); i++) { |
| 3392 | CrossLinkEnum(&message->enum_types_[i], proto.enum_type(i)); |
| 3393 | } |
| 3394 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3395 | for (int i = 0; i < message->field_count(); i++) { |
| 3396 | CrossLinkField(&message->fields_[i], proto.field(i)); |
| 3397 | } |
| 3398 | |
| 3399 | for (int i = 0; i < message->extension_count(); i++) { |
| 3400 | CrossLinkField(&message->extensions_[i], proto.extension(i)); |
| 3401 | } |
| 3402 | } |
| 3403 | |
| 3404 | void DescriptorBuilder::CrossLinkField( |
| 3405 | FieldDescriptor* field, const FieldDescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3406 | if (field->options_ == NULL) { |
| 3407 | field->options_ = &FieldOptions::default_instance(); |
| 3408 | } |
| 3409 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3410 | if (proto.has_extendee()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3411 | Symbol extendee = LookupSymbol(proto.extendee(), field->full_name(), |
| 3412 | PLACEHOLDER_EXTENDABLE_MESSAGE); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3413 | if (extendee.IsNull()) { |
| 3414 | AddNotDefinedError(field->full_name(), proto, |
| 3415 | DescriptorPool::ErrorCollector::EXTENDEE, |
| 3416 | proto.extendee()); |
| 3417 | return; |
| 3418 | } else if (extendee.type != Symbol::MESSAGE) { |
| 3419 | AddError(field->full_name(), proto, |
| 3420 | DescriptorPool::ErrorCollector::EXTENDEE, |
| 3421 | "\"" + proto.extendee() + "\" is not a message type."); |
| 3422 | return; |
| 3423 | } |
| 3424 | field->containing_type_ = extendee.descriptor; |
| 3425 | |
| 3426 | if (!field->containing_type()->IsExtensionNumber(field->number())) { |
| 3427 | AddError(field->full_name(), proto, |
| 3428 | DescriptorPool::ErrorCollector::NUMBER, |
| 3429 | strings::Substitute("\"$0\" does not declare $1 as an " |
| 3430 | "extension number.", |
| 3431 | field->containing_type()->full_name(), |
| 3432 | field->number())); |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | if (proto.has_type_name()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3437 | // Assume we are expecting a message type unless the proto contains some |
| 3438 | // evidence that it expects an enum type. This only makes a difference if |
| 3439 | // we end up creating a placeholder. |
| 3440 | bool expecting_enum = (proto.type() == FieldDescriptorProto::TYPE_ENUM) || |
| 3441 | proto.has_default_value(); |
| 3442 | |
| 3443 | Symbol type = |
| 3444 | LookupSymbol(proto.type_name(), field->full_name(), |
| 3445 | expecting_enum ? PLACEHOLDER_ENUM : PLACEHOLDER_MESSAGE, |
| 3446 | LOOKUP_TYPES); |
| 3447 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3448 | if (type.IsNull()) { |
| 3449 | AddNotDefinedError(field->full_name(), proto, |
| 3450 | DescriptorPool::ErrorCollector::TYPE, |
| 3451 | proto.type_name()); |
| 3452 | return; |
| 3453 | } |
| 3454 | |
| 3455 | if (!proto.has_type()) { |
| 3456 | // Choose field type based on symbol. |
| 3457 | if (type.type == Symbol::MESSAGE) { |
| 3458 | field->type_ = FieldDescriptor::TYPE_MESSAGE; |
| 3459 | } else if (type.type == Symbol::ENUM) { |
| 3460 | field->type_ = FieldDescriptor::TYPE_ENUM; |
| 3461 | } else { |
| 3462 | AddError(field->full_name(), proto, |
| 3463 | DescriptorPool::ErrorCollector::TYPE, |
| 3464 | "\"" + proto.type_name() + "\" is not a type."); |
| 3465 | return; |
| 3466 | } |
| 3467 | } |
| 3468 | |
| 3469 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 3470 | if (type.type != Symbol::MESSAGE) { |
| 3471 | AddError(field->full_name(), proto, |
| 3472 | DescriptorPool::ErrorCollector::TYPE, |
| 3473 | "\"" + proto.type_name() + "\" is not a message type."); |
| 3474 | return; |
| 3475 | } |
| 3476 | field->message_type_ = type.descriptor; |
| 3477 | |
| 3478 | if (field->has_default_value()) { |
| 3479 | AddError(field->full_name(), proto, |
| 3480 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 3481 | "Messages can't have default values."); |
| 3482 | } |
| 3483 | } else if (field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { |
| 3484 | if (type.type != Symbol::ENUM) { |
| 3485 | AddError(field->full_name(), proto, |
| 3486 | DescriptorPool::ErrorCollector::TYPE, |
| 3487 | "\"" + proto.type_name() + "\" is not an enum type."); |
| 3488 | return; |
| 3489 | } |
| 3490 | field->enum_type_ = type.enum_descriptor; |
| 3491 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3492 | if (field->enum_type()->is_placeholder_) { |
| 3493 | // We can't look up default values for placeholder types. We'll have |
| 3494 | // to just drop them. |
| 3495 | field->has_default_value_ = false; |
| 3496 | } |
| 3497 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3498 | if (field->has_default_value()) { |
| 3499 | // We can't just use field->enum_type()->FindValueByName() here |
| 3500 | // because that locks the pool's mutex, which we have already locked |
| 3501 | // at this point. |
| 3502 | Symbol default_value = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3503 | LookupSymbolNoPlaceholder(proto.default_value(), |
| 3504 | field->enum_type()->full_name()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3505 | |
| 3506 | if (default_value.type == Symbol::ENUM_VALUE && |
| 3507 | default_value.enum_value_descriptor->type() == field->enum_type()) { |
| 3508 | field->default_value_enum_ = default_value.enum_value_descriptor; |
| 3509 | } else { |
| 3510 | AddError(field->full_name(), proto, |
| 3511 | DescriptorPool::ErrorCollector::DEFAULT_VALUE, |
| 3512 | "Enum type \"" + field->enum_type()->full_name() + |
| 3513 | "\" has no value named \"" + proto.default_value() + "\"."); |
| 3514 | } |
| 3515 | } else if (field->enum_type()->value_count() > 0) { |
| 3516 | // All enums must have at least one value, or we would have reported |
| 3517 | // an error elsewhere. We use the first defined value as the default |
| 3518 | // if a default is not explicitly defined. |
| 3519 | field->default_value_enum_ = field->enum_type()->value(0); |
| 3520 | } |
| 3521 | } else { |
| 3522 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3523 | "Field with primitive type has type_name."); |
| 3524 | } |
| 3525 | } else { |
| 3526 | if (field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE || |
| 3527 | field->cpp_type() == FieldDescriptor::CPPTYPE_ENUM) { |
| 3528 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3529 | "Field with message or enum type missing type_name."); |
| 3530 | } |
| 3531 | } |
| 3532 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3533 | // Add the field to the fields-by-number table. |
| 3534 | // Note: We have to do this *after* cross-linking because extensions do not |
| 3535 | // know their containing type until now. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3536 | if (!file_tables_->AddFieldByNumber(field)) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3537 | const FieldDescriptor* conflicting_field = |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3538 | file_tables_->FindFieldByNumber(field->containing_type(), |
| 3539 | field->number()); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3540 | if (field->is_extension()) { |
| 3541 | AddError(field->full_name(), proto, |
| 3542 | DescriptorPool::ErrorCollector::NUMBER, |
| 3543 | strings::Substitute("Extension number $0 has already been used " |
| 3544 | "in \"$1\" by extension \"$2\".", |
| 3545 | field->number(), |
| 3546 | field->containing_type()->full_name(), |
| 3547 | conflicting_field->full_name())); |
| 3548 | } else { |
| 3549 | AddError(field->full_name(), proto, |
| 3550 | DescriptorPool::ErrorCollector::NUMBER, |
| 3551 | strings::Substitute("Field number $0 has already been used in " |
| 3552 | "\"$1\" by field \"$2\".", |
| 3553 | field->number(), |
| 3554 | field->containing_type()->full_name(), |
| 3555 | conflicting_field->name())); |
| 3556 | } |
| 3557 | } |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 3558 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3559 | if (field->is_extension()) { |
| 3560 | // No need for error checking: if the extension number collided, |
| 3561 | // we've already been informed of it by the if() above. |
| 3562 | tables_->AddExtension(field); |
| 3563 | } |
| 3564 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 3565 | // Add the field to the lowercase-name and camelcase-name tables. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3566 | file_tables_->AddFieldByStylizedNames(field); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3567 | } |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3568 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3569 | void DescriptorBuilder::CrossLinkEnum( |
| 3570 | EnumDescriptor* enum_type, const EnumDescriptorProto& proto) { |
| 3571 | if (enum_type->options_ == NULL) { |
| 3572 | enum_type->options_ = &EnumOptions::default_instance(); |
| 3573 | } |
| 3574 | |
| 3575 | for (int i = 0; i < enum_type->value_count(); i++) { |
| 3576 | CrossLinkEnumValue(&enum_type->values_[i], proto.value(i)); |
| 3577 | } |
| 3578 | } |
| 3579 | |
| 3580 | void DescriptorBuilder::CrossLinkEnumValue( |
| 3581 | EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) { |
| 3582 | if (enum_value->options_ == NULL) { |
| 3583 | enum_value->options_ = &EnumValueOptions::default_instance(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3584 | } |
| 3585 | } |
| 3586 | |
| 3587 | void DescriptorBuilder::CrossLinkService( |
| 3588 | ServiceDescriptor* service, const ServiceDescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3589 | if (service->options_ == NULL) { |
| 3590 | service->options_ = &ServiceOptions::default_instance(); |
| 3591 | } |
| 3592 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3593 | for (int i = 0; i < service->method_count(); i++) { |
| 3594 | CrossLinkMethod(&service->methods_[i], proto.method(i)); |
| 3595 | } |
| 3596 | } |
| 3597 | |
| 3598 | void DescriptorBuilder::CrossLinkMethod( |
| 3599 | MethodDescriptor* method, const MethodDescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3600 | if (method->options_ == NULL) { |
| 3601 | method->options_ = &MethodOptions::default_instance(); |
| 3602 | } |
| 3603 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3604 | Symbol input_type = LookupSymbol(proto.input_type(), method->full_name()); |
| 3605 | if (input_type.IsNull()) { |
| 3606 | AddNotDefinedError(method->full_name(), proto, |
| 3607 | DescriptorPool::ErrorCollector::INPUT_TYPE, |
| 3608 | proto.input_type()); |
| 3609 | } else if (input_type.type != Symbol::MESSAGE) { |
| 3610 | AddError(method->full_name(), proto, |
| 3611 | DescriptorPool::ErrorCollector::INPUT_TYPE, |
| 3612 | "\"" + proto.input_type() + "\" is not a message type."); |
| 3613 | } else { |
| 3614 | method->input_type_ = input_type.descriptor; |
| 3615 | } |
| 3616 | |
| 3617 | Symbol output_type = LookupSymbol(proto.output_type(), method->full_name()); |
| 3618 | if (output_type.IsNull()) { |
| 3619 | AddNotDefinedError(method->full_name(), proto, |
| 3620 | DescriptorPool::ErrorCollector::OUTPUT_TYPE, |
| 3621 | proto.output_type()); |
| 3622 | } else if (output_type.type != Symbol::MESSAGE) { |
| 3623 | AddError(method->full_name(), proto, |
| 3624 | DescriptorPool::ErrorCollector::OUTPUT_TYPE, |
| 3625 | "\"" + proto.output_type() + "\" is not a message type."); |
| 3626 | } else { |
| 3627 | method->output_type_ = output_type.descriptor; |
| 3628 | } |
| 3629 | } |
| 3630 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3631 | // ------------------------------------------------------------------- |
| 3632 | |
| 3633 | #define VALIDATE_OPTIONS_FROM_ARRAY(descriptor, array_name, type) \ |
| 3634 | for (int i = 0; i < descriptor->array_name##_count(); ++i) { \ |
| 3635 | Validate##type##Options(descriptor->array_name##s_ + i, \ |
| 3636 | proto.array_name(i)); \ |
| 3637 | } |
| 3638 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3639 | // Determine if the file uses optimize_for = LITE_RUNTIME, being careful to |
| 3640 | // avoid problems that exist at init time. |
| 3641 | static bool IsLite(const FileDescriptor* file) { |
| 3642 | // TODO(kenton): I don't even remember how many of these conditions are |
| 3643 | // actually possible. I'm just being super-safe. |
| 3644 | return file != NULL && |
| 3645 | &file->options() != NULL && |
| 3646 | &file->options() != &FileOptions::default_instance() && |
| 3647 | file->options().optimize_for() == FileOptions::LITE_RUNTIME; |
| 3648 | } |
| 3649 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3650 | void DescriptorBuilder::ValidateFileOptions(FileDescriptor* file, |
| 3651 | const FileDescriptorProto& proto) { |
| 3652 | VALIDATE_OPTIONS_FROM_ARRAY(file, message_type, Message); |
| 3653 | VALIDATE_OPTIONS_FROM_ARRAY(file, enum_type, Enum); |
| 3654 | VALIDATE_OPTIONS_FROM_ARRAY(file, service, Service); |
| 3655 | VALIDATE_OPTIONS_FROM_ARRAY(file, extension, Field); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3656 | |
| 3657 | // Lite files can only be imported by other Lite files. |
| 3658 | if (!IsLite(file)) { |
| 3659 | for (int i = 0; i < file->dependency_count(); i++) { |
| 3660 | if (IsLite(file->dependency(i))) { |
| 3661 | AddError( |
| 3662 | file->name(), proto, |
| 3663 | DescriptorPool::ErrorCollector::OTHER, |
| 3664 | "Files that do not use optimize_for = LITE_RUNTIME cannot import " |
| 3665 | "files which do use this option. This file is not lite, but it " |
| 3666 | "imports \"" + file->dependency(i)->name() + "\" which is."); |
| 3667 | break; |
| 3668 | } |
| 3669 | } |
| 3670 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3671 | } |
| 3672 | |
| 3673 | void DescriptorBuilder::ValidateMessageOptions(Descriptor* message, |
| 3674 | const DescriptorProto& proto) { |
| 3675 | VALIDATE_OPTIONS_FROM_ARRAY(message, field, Field); |
| 3676 | VALIDATE_OPTIONS_FROM_ARRAY(message, nested_type, Message); |
| 3677 | VALIDATE_OPTIONS_FROM_ARRAY(message, enum_type, Enum); |
| 3678 | VALIDATE_OPTIONS_FROM_ARRAY(message, extension, Field); |
| 3679 | } |
| 3680 | |
| 3681 | void DescriptorBuilder::ValidateFieldOptions(FieldDescriptor* field, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3682 | const FieldDescriptorProto& proto) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3683 | if (field->options().has_experimental_map_key()) { |
| 3684 | ValidateMapKey(field, proto); |
| 3685 | } |
| 3686 | |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 3687 | // Only repeated primitive fields may be packed. |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 3688 | if (field->options().packed() && !field->is_packable()) { |
| 3689 | AddError( |
| 3690 | field->full_name(), proto, |
| 3691 | DescriptorPool::ErrorCollector::TYPE, |
| 3692 | "[packed = true] can only be specified for repeated primitive fields."); |
kenton@google.com | 2d6daa7 | 2009-01-22 01:27:00 +0000 | [diff] [blame] | 3693 | } |
| 3694 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3695 | // Note: Default instance may not yet be initialized here, so we have to |
| 3696 | // avoid reading from it. |
| 3697 | if (field->containing_type_ != NULL && |
| 3698 | &field->containing_type()->options() != |
| 3699 | &MessageOptions::default_instance() && |
| 3700 | field->containing_type()->options().message_set_wire_format()) { |
| 3701 | if (field->is_extension()) { |
| 3702 | if (!field->is_optional() || |
| 3703 | field->type() != FieldDescriptor::TYPE_MESSAGE) { |
| 3704 | AddError(field->full_name(), proto, |
| 3705 | DescriptorPool::ErrorCollector::TYPE, |
| 3706 | "Extensions of MessageSets must be optional messages."); |
| 3707 | } |
| 3708 | } else { |
| 3709 | AddError(field->full_name(), proto, |
| 3710 | DescriptorPool::ErrorCollector::NAME, |
| 3711 | "MessageSets cannot have fields, only extensions."); |
| 3712 | } |
| 3713 | } |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3714 | |
| 3715 | // Lite extensions can only be of Lite types. |
| 3716 | if (IsLite(field->file()) && |
| 3717 | field->containing_type_ != NULL && |
| 3718 | !IsLite(field->containing_type()->file())) { |
| 3719 | AddError(field->full_name(), proto, |
| 3720 | DescriptorPool::ErrorCollector::EXTENDEE, |
| 3721 | "Extensions to non-lite types can only be declared in non-lite " |
| 3722 | "files. Note that you cannot extend a non-lite type to contain " |
| 3723 | "a lite type, but the reverse is allowed."); |
| 3724 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3725 | } |
| 3726 | |
| 3727 | void DescriptorBuilder::ValidateEnumOptions(EnumDescriptor* enm, |
| 3728 | const EnumDescriptorProto& proto) { |
| 3729 | VALIDATE_OPTIONS_FROM_ARRAY(enm, value, EnumValue); |
| 3730 | } |
| 3731 | |
| 3732 | void DescriptorBuilder::ValidateEnumValueOptions( |
| 3733 | EnumValueDescriptor* enum_value, const EnumValueDescriptorProto& proto) { |
| 3734 | // Nothing to do so far. |
| 3735 | } |
| 3736 | void DescriptorBuilder::ValidateServiceOptions(ServiceDescriptor* service, |
| 3737 | const ServiceDescriptorProto& proto) { |
kenton@google.com | d09ab85 | 2010-04-19 19:15:12 +0000 | [diff] [blame] | 3738 | if (IsLite(service->file()) && |
| 3739 | (service->file()->options().cc_generic_services() || |
| 3740 | service->file()->options().java_generic_services())) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3741 | AddError(service->full_name(), proto, |
| 3742 | DescriptorPool::ErrorCollector::NAME, |
kenton@google.com | d09ab85 | 2010-04-19 19:15:12 +0000 | [diff] [blame] | 3743 | "Files with optimize_for = LITE_RUNTIME cannot define services " |
| 3744 | "unless you set both options cc_generic_services and " |
| 3745 | "java_generic_sevices to false."); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3746 | } |
| 3747 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3748 | VALIDATE_OPTIONS_FROM_ARRAY(service, method, Method); |
| 3749 | } |
| 3750 | |
| 3751 | void DescriptorBuilder::ValidateMethodOptions(MethodDescriptor* method, |
| 3752 | const MethodDescriptorProto& proto) { |
| 3753 | // Nothing to do so far. |
| 3754 | } |
| 3755 | |
| 3756 | void DescriptorBuilder::ValidateMapKey(FieldDescriptor* field, |
| 3757 | const FieldDescriptorProto& proto) { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3758 | if (!field->is_repeated()) { |
| 3759 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3760 | "map type is only allowed for repeated fields."); |
| 3761 | return; |
| 3762 | } |
| 3763 | |
| 3764 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { |
| 3765 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3766 | "map type is only allowed for fields with a message type."); |
| 3767 | return; |
| 3768 | } |
| 3769 | |
| 3770 | const Descriptor* item_type = field->message_type(); |
| 3771 | if (item_type == NULL) { |
| 3772 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3773 | "Could not find field type."); |
| 3774 | return; |
| 3775 | } |
| 3776 | |
| 3777 | // Find the field in item_type named by "experimental_map_key" |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3778 | const string& key_name = field->options().experimental_map_key(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3779 | const Symbol key_symbol = LookupSymbol( |
| 3780 | key_name, |
| 3781 | // We append ".key_name" to the containing type's name since |
| 3782 | // LookupSymbol() searches for peers of the supplied name, not |
| 3783 | // children of the supplied name. |
| 3784 | item_type->full_name() + "." + key_name); |
| 3785 | |
| 3786 | if (key_symbol.IsNull() || key_symbol.field_descriptor->is_extension()) { |
| 3787 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3788 | "Could not find field named \"" + key_name + "\" in type \"" + |
| 3789 | item_type->full_name() + "\"."); |
| 3790 | return; |
| 3791 | } |
| 3792 | const FieldDescriptor* key_field = key_symbol.field_descriptor; |
| 3793 | |
| 3794 | if (key_field->is_repeated()) { |
| 3795 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3796 | "map_key must not name a repeated field."); |
| 3797 | return; |
| 3798 | } |
| 3799 | |
| 3800 | if (key_field->cpp_type() == FieldDescriptor::CPPTYPE_MESSAGE) { |
| 3801 | AddError(field->full_name(), proto, DescriptorPool::ErrorCollector::TYPE, |
| 3802 | "map key must name a scalar or string field."); |
| 3803 | return; |
| 3804 | } |
| 3805 | |
| 3806 | field->experimental_map_key_ = key_field; |
| 3807 | } |
| 3808 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3809 | #undef VALIDATE_OPTIONS_FROM_ARRAY |
| 3810 | |
| 3811 | // ------------------------------------------------------------------- |
| 3812 | |
| 3813 | DescriptorBuilder::OptionInterpreter::OptionInterpreter( |
| 3814 | DescriptorBuilder* builder) : builder_(builder) { |
| 3815 | GOOGLE_CHECK(builder_); |
| 3816 | } |
| 3817 | |
| 3818 | DescriptorBuilder::OptionInterpreter::~OptionInterpreter() { |
| 3819 | } |
| 3820 | |
| 3821 | bool DescriptorBuilder::OptionInterpreter::InterpretOptions( |
| 3822 | OptionsToInterpret* options_to_interpret) { |
| 3823 | // Note that these may be in different pools, so we can't use the same |
| 3824 | // descriptor and reflection objects on both. |
| 3825 | Message* options = options_to_interpret->options; |
| 3826 | const Message* original_options = options_to_interpret->original_options; |
| 3827 | |
| 3828 | bool failed = false; |
| 3829 | options_to_interpret_ = options_to_interpret; |
| 3830 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3831 | // Find the uninterpreted_option field in the mutable copy of the options |
| 3832 | // and clear them, since we're about to interpret them. |
| 3833 | const FieldDescriptor* uninterpreted_options_field = |
| 3834 | options->GetDescriptor()->FindFieldByName("uninterpreted_option"); |
| 3835 | GOOGLE_CHECK(uninterpreted_options_field != NULL) |
| 3836 | << "No field named \"uninterpreted_option\" in the Options proto."; |
| 3837 | options->GetReflection()->ClearField(options, uninterpreted_options_field); |
| 3838 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3839 | // Find the uninterpreted_option field in the original options. |
| 3840 | const FieldDescriptor* original_uninterpreted_options_field = |
| 3841 | original_options->GetDescriptor()-> |
| 3842 | FindFieldByName("uninterpreted_option"); |
| 3843 | GOOGLE_CHECK(original_uninterpreted_options_field != NULL) |
| 3844 | << "No field named \"uninterpreted_option\" in the Options proto."; |
| 3845 | |
| 3846 | const int num_uninterpreted_options = original_options->GetReflection()-> |
| 3847 | FieldSize(*original_options, original_uninterpreted_options_field); |
| 3848 | for (int i = 0; i < num_uninterpreted_options; ++i) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 3849 | uninterpreted_option_ = down_cast<const UninterpretedOption*>( |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3850 | &original_options->GetReflection()->GetRepeatedMessage( |
| 3851 | *original_options, original_uninterpreted_options_field, i)); |
| 3852 | if (!InterpretSingleOption(options)) { |
| 3853 | // Error already added by InterpretSingleOption(). |
| 3854 | failed = true; |
| 3855 | break; |
| 3856 | } |
| 3857 | } |
| 3858 | // Reset these, so we don't have any dangling pointers. |
| 3859 | uninterpreted_option_ = NULL; |
| 3860 | options_to_interpret_ = NULL; |
| 3861 | |
| 3862 | if (!failed) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3863 | // InterpretSingleOption() added the interpreted options in the |
| 3864 | // UnknownFieldSet, in case the option isn't yet known to us. Now we |
| 3865 | // serialize the options message and deserialize it back. That way, any |
| 3866 | // option fields that we do happen to know about will get moved from the |
| 3867 | // UnknownFieldSet into the real fields, and thus be available right away. |
| 3868 | // If they are not known, that's OK too. They will get reparsed into the |
| 3869 | // UnknownFieldSet and wait there until the message is parsed by something |
| 3870 | // that does know about the options. |
| 3871 | string buf; |
| 3872 | options->AppendToString(&buf); |
| 3873 | GOOGLE_CHECK(options->ParseFromString(buf)) |
| 3874 | << "Protocol message serialized itself in invalid fashion."; |
| 3875 | } |
| 3876 | |
| 3877 | return !failed; |
| 3878 | } |
| 3879 | |
| 3880 | bool DescriptorBuilder::OptionInterpreter::InterpretSingleOption( |
| 3881 | Message* options) { |
| 3882 | // First do some basic validation. |
| 3883 | if (uninterpreted_option_->name_size() == 0) { |
| 3884 | // This should never happen unless the parser has gone seriously awry or |
| 3885 | // someone has manually created the uninterpreted option badly. |
| 3886 | return AddNameError("Option must have a name."); |
| 3887 | } |
| 3888 | if (uninterpreted_option_->name(0).name_part() == "uninterpreted_option") { |
| 3889 | return AddNameError("Option must not use reserved name " |
| 3890 | "\"uninterpreted_option\"."); |
| 3891 | } |
| 3892 | |
| 3893 | const Descriptor* options_descriptor = NULL; |
| 3894 | // Get the options message's descriptor from the builder's pool, so that we |
| 3895 | // get the version that knows about any extension options declared in the |
| 3896 | // file we're currently building. The descriptor should be there as long as |
| 3897 | // the file we're building imported "google/protobuf/descriptors.proto". |
| 3898 | |
| 3899 | // Note that we use DescriptorBuilder::FindSymbol(), not |
| 3900 | // DescriptorPool::FindMessageTypeByName() because we're already holding the |
| 3901 | // pool's mutex, and the latter method locks it again. |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 3902 | Symbol symbol = builder_->FindSymbolNotEnforcingDeps( |
| 3903 | options->GetDescriptor()->full_name()); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3904 | if (!symbol.IsNull() && symbol.type == Symbol::MESSAGE) { |
| 3905 | options_descriptor = symbol.descriptor; |
| 3906 | } else { |
| 3907 | // The options message's descriptor was not in the builder's pool, so use |
| 3908 | // the standard version from the generated pool. We're not holding the |
| 3909 | // generated pool's mutex, so we can search it the straightforward way. |
| 3910 | options_descriptor = options->GetDescriptor(); |
| 3911 | } |
| 3912 | GOOGLE_CHECK(options_descriptor); |
| 3913 | |
| 3914 | // We iterate over the name parts to drill into the submessages until we find |
| 3915 | // the leaf field for the option. As we drill down we remember the current |
| 3916 | // submessage's descriptor in |descriptor| and the next field in that |
| 3917 | // submessage in |field|. We also track the fields we're drilling down |
| 3918 | // through in |intermediate_fields|. As we go, we reconstruct the full option |
| 3919 | // name in |debug_msg_name|, for use in error messages. |
| 3920 | const Descriptor* descriptor = options_descriptor; |
| 3921 | const FieldDescriptor* field = NULL; |
| 3922 | vector<const FieldDescriptor*> intermediate_fields; |
| 3923 | string debug_msg_name = ""; |
| 3924 | |
| 3925 | for (int i = 0; i < uninterpreted_option_->name_size(); ++i) { |
| 3926 | const string& name_part = uninterpreted_option_->name(i).name_part(); |
| 3927 | if (debug_msg_name.size() > 0) { |
| 3928 | debug_msg_name += "."; |
| 3929 | } |
| 3930 | if (uninterpreted_option_->name(i).is_extension()) { |
| 3931 | debug_msg_name += "(" + name_part + ")"; |
| 3932 | // Search for the extension's descriptor as an extension in the builder's |
| 3933 | // pool. Note that we use DescriptorBuilder::LookupSymbol(), not |
| 3934 | // DescriptorPool::FindExtensionByName(), for two reasons: 1) It allows |
| 3935 | // relative lookups, and 2) because we're already holding the pool's |
| 3936 | // mutex, and the latter method locks it again. |
| 3937 | Symbol symbol = builder_->LookupSymbol(name_part, |
| 3938 | options_to_interpret_->name_scope); |
| 3939 | if (!symbol.IsNull() && symbol.type == Symbol::FIELD) { |
| 3940 | field = symbol.field_descriptor; |
| 3941 | } |
| 3942 | // If we don't find the field then the field's descriptor was not in the |
| 3943 | // builder's pool, but there's no point in looking in the generated |
| 3944 | // pool. We require that you import the file that defines any extensions |
| 3945 | // you use, so they must be present in the builder's pool. |
| 3946 | } else { |
| 3947 | debug_msg_name += name_part; |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3948 | // Search for the field's descriptor as a regular field. |
| 3949 | field = descriptor->FindFieldByName(name_part); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 3952 | if (field == NULL) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3953 | if (get_allow_unknown(builder_->pool_)) { |
| 3954 | // We can't find the option, but AllowUnknownDependencies() is enabled, |
| 3955 | // so we will just leave it as uninterpreted. |
| 3956 | AddWithoutInterpreting(*uninterpreted_option_, options); |
| 3957 | return true; |
| 3958 | } else { |
| 3959 | return AddNameError("Option \"" + debug_msg_name + "\" unknown."); |
| 3960 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3961 | } else if (field->containing_type() != descriptor) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 3962 | if (get_is_placeholder(field->containing_type())) { |
| 3963 | // The field is an extension of a placeholder type, so we can't |
| 3964 | // reliably verify whether it is a valid extension to use here (e.g. |
| 3965 | // we don't know if it is an extension of the correct *Options message, |
| 3966 | // or if it has a valid field number, etc.). Just leave it as |
| 3967 | // uninterpreted instead. |
| 3968 | AddWithoutInterpreting(*uninterpreted_option_, options); |
| 3969 | return true; |
| 3970 | } else { |
| 3971 | // This can only happen if, due to some insane misconfiguration of the |
| 3972 | // pools, we find the options message in one pool but the field in |
| 3973 | // another. This would probably imply a hefty bug somewhere. |
| 3974 | return AddNameError("Option field \"" + debug_msg_name + |
| 3975 | "\" is not a field or extension of message \"" + |
| 3976 | descriptor->name() + "\"."); |
| 3977 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3978 | } else if (field->is_repeated()) { |
| 3979 | return AddNameError("Option field \"" + debug_msg_name + |
| 3980 | "\" is repeated. Repeated options are not " |
| 3981 | "supported."); |
| 3982 | } else if (i < uninterpreted_option_->name_size() - 1) { |
| 3983 | if (field->cpp_type() != FieldDescriptor::CPPTYPE_MESSAGE) { |
| 3984 | return AddNameError("Option \"" + debug_msg_name + |
| 3985 | "\" is an atomic type, not a message."); |
| 3986 | } else { |
| 3987 | // Drill down into the submessage. |
| 3988 | intermediate_fields.push_back(field); |
| 3989 | descriptor = field->message_type(); |
| 3990 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 3991 | } |
| 3992 | } |
| 3993 | |
| 3994 | // We've found the leaf field. Now we use UnknownFieldSets to set its value |
| 3995 | // on the options message. We do so because the message may not yet know |
| 3996 | // about its extension fields, so we may not be able to set the fields |
| 3997 | // directly. But the UnknownFieldSets will serialize to the same wire-format |
| 3998 | // message, so reading that message back in once the extension fields are |
| 3999 | // known will populate them correctly. |
| 4000 | |
| 4001 | // First see if the option is already set. |
| 4002 | if (!ExamineIfOptionIsSet( |
| 4003 | intermediate_fields.begin(), |
| 4004 | intermediate_fields.end(), |
| 4005 | field, debug_msg_name, |
| 4006 | options->GetReflection()->GetUnknownFields(*options))) { |
| 4007 | return false; // ExamineIfOptionIsSet() already added the error. |
| 4008 | } |
| 4009 | |
| 4010 | |
| 4011 | // First set the value on the UnknownFieldSet corresponding to the |
| 4012 | // innermost message. |
| 4013 | scoped_ptr<UnknownFieldSet> unknown_fields(new UnknownFieldSet()); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4014 | if (!SetOptionValue(field, unknown_fields.get())) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4015 | return false; // SetOptionValue() already added the error. |
| 4016 | } |
| 4017 | |
| 4018 | // Now wrap the UnknownFieldSet with UnknownFieldSets corresponding to all |
| 4019 | // the intermediate messages. |
| 4020 | for (vector<const FieldDescriptor*>::reverse_iterator iter = |
| 4021 | intermediate_fields.rbegin(); |
| 4022 | iter != intermediate_fields.rend(); ++iter) { |
| 4023 | scoped_ptr<UnknownFieldSet> parent_unknown_fields(new UnknownFieldSet()); |
| 4024 | switch ((*iter)->type()) { |
| 4025 | case FieldDescriptor::TYPE_MESSAGE: { |
| 4026 | io::StringOutputStream outstr( |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4027 | parent_unknown_fields->AddLengthDelimited((*iter)->number())); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4028 | io::CodedOutputStream out(&outstr); |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4029 | internal::WireFormat::SerializeUnknownFields(*unknown_fields, &out); |
| 4030 | GOOGLE_CHECK(!out.HadError()) |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4031 | << "Unexpected failure while serializing option submessage " |
| 4032 | << debug_msg_name << "\"."; |
| 4033 | break; |
| 4034 | } |
| 4035 | |
| 4036 | case FieldDescriptor::TYPE_GROUP: { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4037 | parent_unknown_fields->AddGroup((*iter)->number()) |
| 4038 | ->MergeFrom(*unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4039 | break; |
| 4040 | } |
| 4041 | |
| 4042 | default: |
| 4043 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " |
| 4044 | << (*iter)->type(); |
| 4045 | return false; |
| 4046 | } |
| 4047 | unknown_fields.reset(parent_unknown_fields.release()); |
| 4048 | } |
| 4049 | |
| 4050 | // Now merge the UnknownFieldSet corresponding to the top-level message into |
| 4051 | // the options message. |
| 4052 | options->GetReflection()->MutableUnknownFields(options)->MergeFrom( |
| 4053 | *unknown_fields); |
| 4054 | |
| 4055 | return true; |
| 4056 | } |
| 4057 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4058 | void DescriptorBuilder::OptionInterpreter::AddWithoutInterpreting( |
| 4059 | const UninterpretedOption& uninterpreted_option, Message* options) { |
| 4060 | const FieldDescriptor* field = |
| 4061 | options->GetDescriptor()->FindFieldByName("uninterpreted_option"); |
| 4062 | GOOGLE_CHECK(field != NULL); |
| 4063 | |
| 4064 | options->GetReflection()->AddMessage(options, field) |
| 4065 | ->CopyFrom(uninterpreted_option); |
| 4066 | } |
| 4067 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4068 | bool DescriptorBuilder::OptionInterpreter::ExamineIfOptionIsSet( |
| 4069 | vector<const FieldDescriptor*>::const_iterator intermediate_fields_iter, |
| 4070 | vector<const FieldDescriptor*>::const_iterator intermediate_fields_end, |
| 4071 | const FieldDescriptor* innermost_field, const string& debug_msg_name, |
| 4072 | const UnknownFieldSet& unknown_fields) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4073 | // We do linear searches of the UnknownFieldSet and its sub-groups. This |
| 4074 | // should be fine since it's unlikely that any one options structure will |
| 4075 | // contain more than a handful of options. |
| 4076 | |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4077 | if (intermediate_fields_iter == intermediate_fields_end) { |
| 4078 | // We're at the innermost submessage. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4079 | for (int i = 0; i < unknown_fields.field_count(); i++) { |
| 4080 | if (unknown_fields.field(i).number() == innermost_field->number()) { |
| 4081 | return AddNameError("Option \"" + debug_msg_name + |
| 4082 | "\" was already set."); |
| 4083 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4084 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4085 | return true; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4088 | for (int i = 0; i < unknown_fields.field_count(); i++) { |
| 4089 | if (unknown_fields.field(i).number() == |
| 4090 | (*intermediate_fields_iter)->number()) { |
| 4091 | const UnknownField* unknown_field = &unknown_fields.field(i); |
| 4092 | FieldDescriptor::Type type = (*intermediate_fields_iter)->type(); |
| 4093 | // Recurse into the next submessage. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4094 | switch (type) { |
| 4095 | case FieldDescriptor::TYPE_MESSAGE: |
| 4096 | if (unknown_field->type() == UnknownField::TYPE_LENGTH_DELIMITED) { |
| 4097 | UnknownFieldSet intermediate_unknown_fields; |
| 4098 | if (intermediate_unknown_fields.ParseFromString( |
| 4099 | unknown_field->length_delimited()) && |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4100 | !ExamineIfOptionIsSet(intermediate_fields_iter + 1, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4101 | intermediate_fields_end, |
| 4102 | innermost_field, debug_msg_name, |
| 4103 | intermediate_unknown_fields)) { |
| 4104 | return false; // Error already added. |
| 4105 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4106 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4107 | break; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4108 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4109 | case FieldDescriptor::TYPE_GROUP: |
| 4110 | if (unknown_field->type() == UnknownField::TYPE_GROUP) { |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4111 | if (!ExamineIfOptionIsSet(intermediate_fields_iter + 1, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4112 | intermediate_fields_end, |
| 4113 | innermost_field, debug_msg_name, |
| 4114 | unknown_field->group())) { |
| 4115 | return false; // Error already added. |
| 4116 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4117 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4118 | break; |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4119 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4120 | default: |
| 4121 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_MESSAGE: " << type; |
| 4122 | return false; |
| 4123 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4124 | } |
| 4125 | } |
| 4126 | return true; |
| 4127 | } |
| 4128 | |
| 4129 | bool DescriptorBuilder::OptionInterpreter::SetOptionValue( |
| 4130 | const FieldDescriptor* option_field, |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4131 | UnknownFieldSet* unknown_fields) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4132 | // We switch on the CppType to validate. |
| 4133 | switch (option_field->cpp_type()) { |
| 4134 | |
| 4135 | case FieldDescriptor::CPPTYPE_INT32: |
| 4136 | if (uninterpreted_option_->has_positive_int_value()) { |
| 4137 | if (uninterpreted_option_->positive_int_value() > |
| 4138 | static_cast<uint64>(kint32max)) { |
| 4139 | return AddValueError("Value out of range for int32 option \"" + |
| 4140 | option_field->full_name() + "\"."); |
| 4141 | } else { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4142 | SetInt32(option_field->number(), |
| 4143 | uninterpreted_option_->positive_int_value(), |
| 4144 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4145 | } |
| 4146 | } else if (uninterpreted_option_->has_negative_int_value()) { |
| 4147 | if (uninterpreted_option_->negative_int_value() < |
| 4148 | static_cast<int64>(kint32min)) { |
| 4149 | return AddValueError("Value out of range for int32 option \"" + |
| 4150 | option_field->full_name() + "\"."); |
| 4151 | } else { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4152 | SetInt32(option_field->number(), |
| 4153 | uninterpreted_option_->negative_int_value(), |
| 4154 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4155 | } |
| 4156 | } else { |
| 4157 | return AddValueError("Value must be integer for int32 option \"" + |
| 4158 | option_field->full_name() + "\"."); |
| 4159 | } |
| 4160 | break; |
| 4161 | |
| 4162 | case FieldDescriptor::CPPTYPE_INT64: |
| 4163 | if (uninterpreted_option_->has_positive_int_value()) { |
| 4164 | if (uninterpreted_option_->positive_int_value() > |
| 4165 | static_cast<uint64>(kint64max)) { |
| 4166 | return AddValueError("Value out of range for int64 option \"" + |
| 4167 | option_field->full_name() + "\"."); |
| 4168 | } else { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4169 | SetInt64(option_field->number(), |
| 4170 | uninterpreted_option_->positive_int_value(), |
| 4171 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4172 | } |
| 4173 | } else if (uninterpreted_option_->has_negative_int_value()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4174 | SetInt64(option_field->number(), |
| 4175 | uninterpreted_option_->negative_int_value(), |
| 4176 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4177 | } else { |
| 4178 | return AddValueError("Value must be integer for int64 option \"" + |
| 4179 | option_field->full_name() + "\"."); |
| 4180 | } |
| 4181 | break; |
| 4182 | |
| 4183 | case FieldDescriptor::CPPTYPE_UINT32: |
| 4184 | if (uninterpreted_option_->has_positive_int_value()) { |
| 4185 | if (uninterpreted_option_->positive_int_value() > kuint32max) { |
| 4186 | return AddValueError("Value out of range for uint32 option \"" + |
| 4187 | option_field->name() + "\"."); |
| 4188 | } else { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4189 | SetUInt32(option_field->number(), |
| 4190 | uninterpreted_option_->positive_int_value(), |
| 4191 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4192 | } |
| 4193 | } else { |
| 4194 | return AddValueError("Value must be non-negative integer for uint32 " |
| 4195 | "option \"" + option_field->full_name() + "\"."); |
| 4196 | } |
| 4197 | break; |
| 4198 | |
| 4199 | case FieldDescriptor::CPPTYPE_UINT64: |
| 4200 | if (uninterpreted_option_->has_positive_int_value()) { |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4201 | SetUInt64(option_field->number(), |
| 4202 | uninterpreted_option_->positive_int_value(), |
| 4203 | option_field->type(), unknown_fields); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4204 | } else { |
| 4205 | return AddValueError("Value must be non-negative integer for uint64 " |
| 4206 | "option \"" + option_field->full_name() + "\"."); |
| 4207 | } |
| 4208 | break; |
| 4209 | |
| 4210 | case FieldDescriptor::CPPTYPE_FLOAT: { |
| 4211 | float value; |
| 4212 | if (uninterpreted_option_->has_double_value()) { |
| 4213 | value = uninterpreted_option_->double_value(); |
| 4214 | } else if (uninterpreted_option_->has_positive_int_value()) { |
| 4215 | value = uninterpreted_option_->positive_int_value(); |
| 4216 | } else if (uninterpreted_option_->has_negative_int_value()) { |
| 4217 | value = uninterpreted_option_->negative_int_value(); |
| 4218 | } else { |
| 4219 | return AddValueError("Value must be number for float option \"" + |
| 4220 | option_field->full_name() + "\"."); |
| 4221 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4222 | unknown_fields->AddFixed32(option_field->number(), |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4223 | google::protobuf::internal::WireFormatLite::EncodeFloat(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4224 | break; |
| 4225 | } |
| 4226 | |
| 4227 | case FieldDescriptor::CPPTYPE_DOUBLE: { |
| 4228 | double value; |
| 4229 | if (uninterpreted_option_->has_double_value()) { |
| 4230 | value = uninterpreted_option_->double_value(); |
| 4231 | } else if (uninterpreted_option_->has_positive_int_value()) { |
| 4232 | value = uninterpreted_option_->positive_int_value(); |
| 4233 | } else if (uninterpreted_option_->has_negative_int_value()) { |
| 4234 | value = uninterpreted_option_->negative_int_value(); |
| 4235 | } else { |
| 4236 | return AddValueError("Value must be number for double option \"" + |
| 4237 | option_field->full_name() + "\"."); |
| 4238 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4239 | unknown_fields->AddFixed64(option_field->number(), |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4240 | google::protobuf::internal::WireFormatLite::EncodeDouble(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4241 | break; |
| 4242 | } |
| 4243 | |
| 4244 | case FieldDescriptor::CPPTYPE_BOOL: |
| 4245 | uint64 value; |
| 4246 | if (!uninterpreted_option_->has_identifier_value()) { |
| 4247 | return AddValueError("Value must be identifier for boolean option " |
| 4248 | "\"" + option_field->full_name() + "\"."); |
| 4249 | } |
| 4250 | if (uninterpreted_option_->identifier_value() == "true") { |
| 4251 | value = 1; |
| 4252 | } else if (uninterpreted_option_->identifier_value() == "false") { |
| 4253 | value = 0; |
| 4254 | } else { |
| 4255 | return AddValueError("Value must be \"true\" or \"false\" for boolean " |
| 4256 | "option \"" + option_field->full_name() + "\"."); |
| 4257 | } |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4258 | unknown_fields->AddVarint(option_field->number(), value); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4259 | break; |
| 4260 | |
| 4261 | case FieldDescriptor::CPPTYPE_ENUM: { |
| 4262 | if (!uninterpreted_option_->has_identifier_value()) { |
| 4263 | return AddValueError("Value must be identifier for enum-valued option " |
| 4264 | "\"" + option_field->full_name() + "\"."); |
| 4265 | } |
| 4266 | const EnumDescriptor* enum_type = option_field->enum_type(); |
| 4267 | const string& value_name = uninterpreted_option_->identifier_value(); |
| 4268 | const EnumValueDescriptor* enum_value = NULL; |
| 4269 | |
| 4270 | if (enum_type->file()->pool() != DescriptorPool::generated_pool()) { |
| 4271 | // Note that the enum value's fully-qualified name is a sibling of the |
| 4272 | // enum's name, not a child of it. |
| 4273 | string fully_qualified_name = enum_type->full_name(); |
| 4274 | fully_qualified_name.resize(fully_qualified_name.size() - |
| 4275 | enum_type->name().size()); |
| 4276 | fully_qualified_name += value_name; |
| 4277 | |
| 4278 | // Search for the enum value's descriptor in the builder's pool. Note |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 4279 | // that we use DescriptorBuilder::FindSymbolNotEnforcingDeps(), not |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4280 | // DescriptorPool::FindEnumValueByName() because we're already holding |
| 4281 | // the pool's mutex, and the latter method locks it again. |
kenton@google.com | 26bd9ee | 2008-11-21 00:06:27 +0000 | [diff] [blame] | 4282 | Symbol symbol = |
| 4283 | builder_->FindSymbolNotEnforcingDeps(fully_qualified_name); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4284 | if (!symbol.IsNull() && symbol.type == Symbol::ENUM_VALUE) { |
| 4285 | if (symbol.enum_value_descriptor->type() != enum_type) { |
| 4286 | return AddValueError("Enum type \"" + enum_type->full_name() + |
| 4287 | "\" has no value named \"" + value_name + "\" for option \"" + |
| 4288 | option_field->full_name() + |
| 4289 | "\". This appears to be a value from a sibling type."); |
| 4290 | } else { |
| 4291 | enum_value = symbol.enum_value_descriptor; |
| 4292 | } |
| 4293 | } |
| 4294 | } else { |
| 4295 | // The enum type is in the generated pool, so we can search for the |
| 4296 | // value there. |
| 4297 | enum_value = enum_type->FindValueByName(value_name); |
| 4298 | } |
| 4299 | |
| 4300 | if (enum_value == NULL) { |
| 4301 | return AddValueError("Enum type \"" + |
| 4302 | option_field->enum_type()->full_name() + |
| 4303 | "\" has no value named \"" + value_name + "\" for " |
| 4304 | "option \"" + option_field->full_name() + "\"."); |
| 4305 | } else { |
| 4306 | // Sign-extension is not a problem, since we cast directly from int32 to |
| 4307 | // uint64, without first going through uint32. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4308 | unknown_fields->AddVarint(option_field->number(), |
| 4309 | static_cast<uint64>(static_cast<int64>(enum_value->number()))); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4310 | } |
| 4311 | break; |
| 4312 | } |
| 4313 | |
| 4314 | case FieldDescriptor::CPPTYPE_STRING: |
| 4315 | if (!uninterpreted_option_->has_string_value()) { |
| 4316 | return AddValueError("Value must be quoted string for string option " |
| 4317 | "\"" + option_field->full_name() + "\"."); |
| 4318 | } |
| 4319 | // The string has already been unquoted and unescaped by the parser. |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4320 | unknown_fields->AddLengthDelimited(option_field->number(), |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4321 | uninterpreted_option_->string_value()); |
| 4322 | break; |
| 4323 | |
| 4324 | case FieldDescriptor::CPPTYPE_MESSAGE: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 4325 | if (!SetAggregateOption(option_field, unknown_fields)) { |
| 4326 | return false; |
| 4327 | } |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4328 | break; |
| 4329 | } |
| 4330 | |
| 4331 | return true; |
| 4332 | } |
| 4333 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 4334 | class DescriptorBuilder::OptionInterpreter::AggregateOptionFinder |
| 4335 | : public TextFormat::Finder { |
| 4336 | public: |
| 4337 | DescriptorBuilder* builder_; |
| 4338 | |
| 4339 | virtual const FieldDescriptor* FindExtension( |
| 4340 | Message* message, const string& name) const { |
| 4341 | if (builder_->pool_->mutex_ != NULL) { |
| 4342 | builder_->pool_->mutex_->AssertHeld(); |
| 4343 | } |
| 4344 | Symbol result = builder_->LookupSymbolNoPlaceholder( |
| 4345 | name, message->GetDescriptor()->full_name()); |
| 4346 | if (result.type == Symbol::FIELD && |
| 4347 | result.field_descriptor->is_extension()) { |
| 4348 | return result.field_descriptor; |
| 4349 | } else { |
| 4350 | return NULL; |
| 4351 | } |
| 4352 | } |
| 4353 | }; |
| 4354 | |
| 4355 | // A custom error collector to record any text-format parsing errors |
| 4356 | namespace { |
| 4357 | class AggregateErrorCollector : public io::ErrorCollector { |
| 4358 | public: |
| 4359 | string error_; |
| 4360 | |
| 4361 | virtual void AddError(int line, int column, const string& message) { |
| 4362 | if (!error_.empty()) { |
| 4363 | error_ += "; "; |
| 4364 | } |
| 4365 | error_ += message; |
| 4366 | } |
| 4367 | |
| 4368 | virtual void AddWarning(int line, int column, const string& message) { |
| 4369 | // Ignore warnings |
| 4370 | } |
| 4371 | }; |
| 4372 | } |
| 4373 | |
| 4374 | // We construct a dynamic message of the type corresponding to |
| 4375 | // option_field, parse the supplied text-format string into this |
| 4376 | // message, and serialize the resulting message to produce the value. |
| 4377 | bool DescriptorBuilder::OptionInterpreter::SetAggregateOption( |
| 4378 | const FieldDescriptor* option_field, |
| 4379 | UnknownFieldSet* unknown_fields) { |
| 4380 | if (!uninterpreted_option_->has_aggregate_value()) { |
| 4381 | return AddValueError("Option \"" + option_field->full_name() + |
| 4382 | "\" is a message. To set the entire message, use " |
| 4383 | "syntax like \"" + option_field->name() + |
| 4384 | " = { <proto text format> }\". " |
| 4385 | "To set fields within it, use " |
| 4386 | "syntax like \"" + option_field->name() + |
| 4387 | ".foo = value\"."); |
| 4388 | } |
| 4389 | |
| 4390 | const Descriptor* type = option_field->message_type(); |
| 4391 | scoped_ptr<Message> dynamic(dynamic_factory_.GetPrototype(type)->New()); |
| 4392 | GOOGLE_CHECK(dynamic.get() != NULL) |
| 4393 | << "Could not create an instance of " << option_field->DebugString(); |
| 4394 | |
| 4395 | AggregateErrorCollector collector; |
| 4396 | AggregateOptionFinder finder; |
| 4397 | finder.builder_ = builder_; |
| 4398 | TextFormat::Parser parser; |
| 4399 | parser.RecordErrorsTo(&collector); |
| 4400 | parser.SetFinder(&finder); |
| 4401 | if (!parser.ParseFromString(uninterpreted_option_->aggregate_value(), |
| 4402 | dynamic.get())) { |
| 4403 | AddValueError("Error while parsing option value for \"" + |
| 4404 | option_field->name() + "\": " + collector.error_); |
| 4405 | return false; |
| 4406 | } else { |
| 4407 | string serial; |
| 4408 | dynamic->SerializeToString(&serial); // Never fails |
| 4409 | unknown_fields->AddLengthDelimited(option_field->number(), serial); |
| 4410 | return true; |
| 4411 | } |
| 4412 | } |
| 4413 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4414 | void DescriptorBuilder::OptionInterpreter::SetInt32(int number, int32 value, |
| 4415 | FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4416 | switch (type) { |
| 4417 | case FieldDescriptor::TYPE_INT32: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4418 | unknown_fields->AddVarint(number, |
| 4419 | static_cast<uint64>(static_cast<int64>(value))); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4420 | break; |
| 4421 | |
| 4422 | case FieldDescriptor::TYPE_SFIXED32: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4423 | unknown_fields->AddFixed32(number, static_cast<uint32>(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4424 | break; |
| 4425 | |
| 4426 | case FieldDescriptor::TYPE_SINT32: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4427 | unknown_fields->AddVarint(number, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4428 | google::protobuf::internal::WireFormatLite::ZigZagEncode32(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4429 | break; |
| 4430 | |
| 4431 | default: |
| 4432 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT32: " << type; |
| 4433 | break; |
| 4434 | } |
| 4435 | } |
| 4436 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4437 | void DescriptorBuilder::OptionInterpreter::SetInt64(int number, int64 value, |
| 4438 | FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4439 | switch (type) { |
| 4440 | case FieldDescriptor::TYPE_INT64: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4441 | unknown_fields->AddVarint(number, static_cast<uint64>(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4442 | break; |
| 4443 | |
| 4444 | case FieldDescriptor::TYPE_SFIXED64: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4445 | unknown_fields->AddFixed64(number, static_cast<uint64>(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4446 | break; |
| 4447 | |
| 4448 | case FieldDescriptor::TYPE_SINT64: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4449 | unknown_fields->AddVarint(number, |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 4450 | google::protobuf::internal::WireFormatLite::ZigZagEncode64(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4451 | break; |
| 4452 | |
| 4453 | default: |
| 4454 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_INT64: " << type; |
| 4455 | break; |
| 4456 | } |
| 4457 | } |
| 4458 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4459 | void DescriptorBuilder::OptionInterpreter::SetUInt32(int number, uint32 value, |
| 4460 | FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4461 | switch (type) { |
| 4462 | case FieldDescriptor::TYPE_UINT32: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4463 | unknown_fields->AddVarint(number, static_cast<uint64>(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4464 | break; |
| 4465 | |
| 4466 | case FieldDescriptor::TYPE_FIXED32: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4467 | unknown_fields->AddFixed32(number, static_cast<uint32>(value)); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4468 | break; |
| 4469 | |
| 4470 | default: |
| 4471 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT32: " << type; |
| 4472 | break; |
| 4473 | } |
| 4474 | } |
| 4475 | |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4476 | void DescriptorBuilder::OptionInterpreter::SetUInt64(int number, uint64 value, |
| 4477 | FieldDescriptor::Type type, UnknownFieldSet* unknown_fields) { |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4478 | switch (type) { |
| 4479 | case FieldDescriptor::TYPE_UINT64: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4480 | unknown_fields->AddVarint(number, value); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4481 | break; |
| 4482 | |
| 4483 | case FieldDescriptor::TYPE_FIXED64: |
kenton@google.com | d37d46d | 2009-04-25 02:53:47 +0000 | [diff] [blame] | 4484 | unknown_fields->AddFixed64(number, value); |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 4485 | break; |
| 4486 | |
| 4487 | default: |
| 4488 | GOOGLE_LOG(FATAL) << "Invalid wire type for CPPTYPE_UINT64: " << type; |
| 4489 | break; |
| 4490 | } |
| 4491 | } |
| 4492 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 4493 | } // namespace protobuf |
| 4494 | } // namespace google |