• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 // https://developers.google.com/protocol-buffers/
4 //
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are
7 // met:
8 //
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.
18 //
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.
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 <iostream>
36 #include <stack>
37 #include <unordered_map>
38 
39 #include <google/protobuf/generated_message_reflection.h>
40 #include <google/protobuf/message.h>
41 
42 #include <google/protobuf/stubs/casts.h>
43 #include <google/protobuf/stubs/logging.h>
44 #include <google/protobuf/stubs/common.h>
45 #include <google/protobuf/descriptor.pb.h>
46 #include <google/protobuf/parse_context.h>
47 #include <google/protobuf/reflection_internal.h>
48 #include <google/protobuf/io/coded_stream.h>
49 #include <google/protobuf/io/zero_copy_stream_impl.h>
50 #include <google/protobuf/descriptor.h>
51 #include <google/protobuf/generated_message_util.h>
52 #include <google/protobuf/map_field.h>
53 #include <google/protobuf/map_field_inl.h>
54 #include <google/protobuf/reflection_ops.h>
55 #include <google/protobuf/unknown_field_set.h>
56 #include <google/protobuf/wire_format.h>
57 #include <google/protobuf/wire_format_lite.h>
58 #include <google/protobuf/stubs/strutil.h>
59 
60 #include <google/protobuf/stubs/map_util.h>
61 #include <google/protobuf/stubs/stl_util.h>
62 #include <google/protobuf/stubs/hash.h>
63 
64 #include <google/protobuf/port_def.inc>
65 
66 namespace google {
67 namespace protobuf {
68 
69 namespace internal {
70 
71 // TODO(gerbens) make this factorized better. This should not have to hop
72 // to reflection. Currently uses GeneratedMessageReflection and thus is
73 // defined in generated_message_reflection.cc
74 void RegisterFileLevelMetadata(const DescriptorTable* descriptor_table);
75 
76 }  // namespace internal
77 
78 using internal::ReflectionOps;
79 using internal::WireFormat;
80 using internal::WireFormatLite;
81 
MergeFrom(const Message & from)82 void Message::MergeFrom(const Message& from) {
83   const Descriptor* descriptor = GetDescriptor();
84   GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
85       << ": Tried to merge from a message with a different type.  "
86          "to: "
87       << descriptor->full_name()
88       << ", "
89          "from: "
90       << from.GetDescriptor()->full_name();
91   ReflectionOps::Merge(from, this);
92 }
93 
CheckTypeAndMergeFrom(const MessageLite & other)94 void Message::CheckTypeAndMergeFrom(const MessageLite& other) {
95   MergeFrom(*down_cast<const Message*>(&other));
96 }
97 
CopyFrom(const Message & from)98 void Message::CopyFrom(const Message& from) {
99   const Descriptor* descriptor = GetDescriptor();
100   GOOGLE_CHECK_EQ(from.GetDescriptor(), descriptor)
101       << ": Tried to copy from a message with a different type. "
102          "to: "
103       << descriptor->full_name()
104       << ", "
105          "from: "
106       << from.GetDescriptor()->full_name();
107   ReflectionOps::Copy(from, this);
108 }
109 
GetTypeName() const110 std::string Message::GetTypeName() const {
111   return GetDescriptor()->full_name();
112 }
113 
Clear()114 void Message::Clear() { ReflectionOps::Clear(this); }
115 
IsInitialized() const116 bool Message::IsInitialized() const {
117   return ReflectionOps::IsInitialized(*this);
118 }
119 
FindInitializationErrors(std::vector<std::string> * errors) const120 void Message::FindInitializationErrors(std::vector<std::string>* errors) const {
121   return ReflectionOps::FindInitializationErrors(*this, "", errors);
122 }
123 
InitializationErrorString() const124 std::string Message::InitializationErrorString() const {
125   std::vector<std::string> errors;
126   FindInitializationErrors(&errors);
127   return Join(errors, ", ");
128 }
129 
CheckInitialized() const130 void Message::CheckInitialized() const {
131   GOOGLE_CHECK(IsInitialized()) << "Message of type \"" << GetDescriptor()->full_name()
132                          << "\" is missing required fields: "
133                          << InitializationErrorString();
134 }
135 
DiscardUnknownFields()136 void Message::DiscardUnknownFields() {
137   return ReflectionOps::DiscardUnknownFields(this);
138 }
139 
140 #if !GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
MergePartialFromCodedStream(io::CodedInputStream * input)141 bool Message::MergePartialFromCodedStream(io::CodedInputStream* input) {
142   return WireFormat::ParseAndMergePartial(input, this);
143 }
144 #endif
145 
146 #if GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
147 namespace internal {
148 
149 class ReflectionAccessor {
150  public:
GetOffset(void * msg,const google::protobuf::FieldDescriptor * f,const google::protobuf::Reflection * r)151   static void* GetOffset(void* msg, const google::protobuf::FieldDescriptor* f,
152                          const google::protobuf::Reflection* r) {
153     return static_cast<char*>(msg) + r->schema_.GetFieldOffset(f);
154   }
155 
GetRepeatedEnum(const Reflection * reflection,const FieldDescriptor * field,Message * msg)156   static void* GetRepeatedEnum(const Reflection* reflection,
157                                const FieldDescriptor* field, Message* msg) {
158     return reflection->MutableRawRepeatedField(
159         msg, field, FieldDescriptor::CPPTYPE_ENUM, 0, nullptr);
160   }
161 
MutableInternalMetadataWithArena(const Reflection * reflection,Message * msg)162   static InternalMetadataWithArena* MutableInternalMetadataWithArena(
163       const Reflection* reflection, Message* msg) {
164     return reflection->MutableInternalMetadataWithArena(msg);
165   }
166 };
167 
168 }  // namespace internal
169 
SetField(uint64 val,const FieldDescriptor * field,Message * msg,const Reflection * reflection)170 void SetField(uint64 val, const FieldDescriptor* field, Message* msg,
171               const Reflection* reflection) {
172 #define STORE_TYPE(CPPTYPE_METHOD)                        \
173   do                                                      \
174     if (field->is_repeated()) {                           \
175       reflection->Add##CPPTYPE_METHOD(msg, field, value); \
176     } else {                                              \
177       reflection->Set##CPPTYPE_METHOD(msg, field, value); \
178     }                                                     \
179   while (0)
180 
181   switch (field->type()) {
182 #define HANDLE_TYPE(TYPE, CPPTYPE, CPPTYPE_METHOD) \
183   case FieldDescriptor::TYPE_##TYPE: {             \
184     CPPTYPE value = val;                           \
185     STORE_TYPE(CPPTYPE_METHOD);                    \
186     break;                                         \
187   }
188 
189     // Varints
190     HANDLE_TYPE(INT32, int32, Int32)
191     HANDLE_TYPE(INT64, int64, Int64)
192     HANDLE_TYPE(UINT32, uint32, UInt32)
193     HANDLE_TYPE(UINT64, uint64, UInt64)
194     case FieldDescriptor::TYPE_SINT32: {
195       int32 value = WireFormatLite::ZigZagDecode32(val);
196       STORE_TYPE(Int32);
197       break;
198     }
199     case FieldDescriptor::TYPE_SINT64: {
200       int64 value = WireFormatLite::ZigZagDecode64(val);
201       STORE_TYPE(Int64);
202       break;
203     }
204       HANDLE_TYPE(BOOL, bool, Bool)
205 
206       // Fixed
207       HANDLE_TYPE(FIXED32, uint32, UInt32)
208       HANDLE_TYPE(FIXED64, uint64, UInt64)
209       HANDLE_TYPE(SFIXED32, int32, Int32)
210       HANDLE_TYPE(SFIXED64, int64, Int64)
211 
212     case FieldDescriptor::TYPE_FLOAT: {
213       float value;
214       uint32 bit_rep = val;
215       std::memcpy(&value, &bit_rep, sizeof(value));
216       STORE_TYPE(Float);
217       break;
218     }
219     case FieldDescriptor::TYPE_DOUBLE: {
220       double value;
221       uint64 bit_rep = val;
222       std::memcpy(&value, &bit_rep, sizeof(value));
223       STORE_TYPE(Double);
224       break;
225     }
226     case FieldDescriptor::TYPE_ENUM: {
227       int value = val;
228       if (field->is_repeated()) {
229         reflection->AddEnumValue(msg, field, value);
230       } else {
231         reflection->SetEnumValue(msg, field, value);
232       }
233       break;
234     }
235     default:
236       GOOGLE_LOG(FATAL) << "Error in descriptors, primitve field with field type "
237                  << field->type();
238   }
239 #undef STORE_TYPE
240 #undef HANDLE_TYPE
241 }
242 
ReflectiveValidator(const void * arg,int val)243 bool ReflectiveValidator(const void* arg, int val) {
244   auto d = static_cast<const EnumDescriptor*>(arg);
245   return d->FindValueByNumber(val) != nullptr;
246 }
247 
ParsePackedField(const FieldDescriptor * field,Message * msg,const Reflection * reflection,const char * ptr,internal::ParseContext * ctx)248 const char* ParsePackedField(const FieldDescriptor* field, Message* msg,
249                              const Reflection* reflection, const char* ptr,
250                              internal::ParseContext* ctx) {
251   switch (field->type()) {
252 #define HANDLE_PACKED_TYPE(TYPE, CPPTYPE, METHOD_NAME) \
253   case FieldDescriptor::TYPE_##TYPE:                   \
254     return internal::Packed##METHOD_NAME##Parser(      \
255         reflection->MutableRepeatedField<CPPTYPE>(msg, field), ptr, ctx)
256     HANDLE_PACKED_TYPE(INT32, int32, Int32);
257     HANDLE_PACKED_TYPE(INT64, int64, Int64);
258     HANDLE_PACKED_TYPE(SINT32, int32, SInt32);
259     HANDLE_PACKED_TYPE(SINT64, int64, SInt64);
260     HANDLE_PACKED_TYPE(UINT32, uint32, UInt32);
261     HANDLE_PACKED_TYPE(UINT64, uint64, UInt64);
262     HANDLE_PACKED_TYPE(BOOL, bool, Bool);
263     case FieldDescriptor::TYPE_ENUM: {
264       auto object =
265           internal::ReflectionAccessor::GetRepeatedEnum(reflection, field, msg);
266       if (field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
267         return internal::PackedEnumParser(object, ptr, ctx);
268       } else {
269         return internal::PackedEnumParserArg(
270             object, ptr, ctx, ReflectiveValidator, field->enum_type(),
271             internal::ReflectionAccessor::MutableInternalMetadataWithArena(
272                 reflection, msg),
273             field->number());
274       }
275     }
276       HANDLE_PACKED_TYPE(FIXED32, uint32, Fixed32);
277       HANDLE_PACKED_TYPE(FIXED64, uint64, Fixed64);
278       HANDLE_PACKED_TYPE(SFIXED32, int32, SFixed32);
279       HANDLE_PACKED_TYPE(SFIXED64, int64, SFixed64);
280       HANDLE_PACKED_TYPE(FLOAT, float, Float);
281       HANDLE_PACKED_TYPE(DOUBLE, double, Double);
282 #undef HANDLE_PACKED_TYPE
283 
284     default:
285       GOOGLE_LOG(FATAL) << "Type is not packable " << field->type();
286       return nullptr;  // Make compiler happy
287   }
288 }
289 
ParseLenDelim(int field_number,const FieldDescriptor * field,Message * msg,const Reflection * reflection,const char * ptr,internal::ParseContext * ctx)290 const char* ParseLenDelim(int field_number, const FieldDescriptor* field,
291                           Message* msg, const Reflection* reflection,
292                           const char* ptr, internal::ParseContext* ctx) {
293   if (WireFormat::WireTypeForFieldType(field->type()) !=
294       WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
295     GOOGLE_DCHECK(field->is_packable());
296     return ParsePackedField(field, msg, reflection, ptr, ctx);
297   }
298   enum { kNone = 0, kVerify, kStrict } utf8_level = kNone;
299   const char* field_name = nullptr;
300   auto parse_string = [ptr, ctx, &utf8_level, &field_name](std::string* s) {
301     switch (utf8_level) {
302       case kNone:
303         return internal::InlineGreedyStringParser(s, ptr, ctx);
304       case kVerify:
305         return internal::InlineGreedyStringParserUTF8Verify(s, ptr, ctx,
306                                                             field_name);
307       case kStrict:
308         return internal::InlineGreedyStringParserUTF8(s, ptr, ctx, field_name);
309     }
310   };
311   switch (field->type()) {
312     case FieldDescriptor::TYPE_STRING: {
313       bool enforce_utf8 = true;
314       bool utf8_verification = true;
315       if (enforce_utf8 &&
316           field->file()->syntax() == FileDescriptor::SYNTAX_PROTO3) {
317         utf8_level = kStrict;
318       } else if (utf8_verification) {
319         utf8_level = kVerify;
320       }
321       field_name = field->full_name().c_str();
322       PROTOBUF_FALLTHROUGH_INTENDED;
323     }
324     case FieldDescriptor::TYPE_BYTES: {
325       if (field->is_repeated()) {
326         int index = reflection->FieldSize(*msg, field);
327         // Add new empty value.
328         reflection->AddString(msg, field, "");
329         if (field->options().ctype() == FieldOptions::STRING ||
330             field->is_extension()) {
331           auto object =
332               reflection->MutableRepeatedPtrField<std::string>(msg, field)
333                   ->Mutable(index);
334           return parse_string(object);
335         } else {
336           auto object =
337               reflection->MutableRepeatedPtrField<std::string>(msg, field)
338                   ->Mutable(index);
339           return parse_string(object);
340         }
341       } else {
342         // Clear value and make sure it's set.
343         reflection->SetString(msg, field, "");
344         if (field->options().ctype() == FieldOptions::STRING ||
345             field->is_extension()) {
346           // HACK around inability to get mutable_string in reflection
347           std::string* object = &const_cast<std::string&>(
348               reflection->GetStringReference(*msg, field, nullptr));
349           return parse_string(object);
350         } else {
351           // HACK around inability to get mutable_string in reflection
352           std::string* object = &const_cast<std::string&>(
353               reflection->GetStringReference(*msg, field, nullptr));
354           return parse_string(object);
355         }
356       }
357       GOOGLE_LOG(FATAL) << "No other type than string supported";
358     }
359     case FieldDescriptor::TYPE_MESSAGE: {
360       Message* object;
361       if (field->is_repeated()) {
362         object = reflection->AddMessage(msg, field, ctx->data().factory);
363       } else {
364         object = reflection->MutableMessage(msg, field, ctx->data().factory);
365       }
366       return ctx->ParseMessage(object, ptr);
367     }
368     default:
369       GOOGLE_LOG(FATAL) << "Wrong type for length delim " << field->type();
370   }
371   return nullptr;  // Make compiler happy.
372 }
373 
GetGroup(int field_number,const FieldDescriptor * field,Message * msg,const Reflection * reflection)374 Message* GetGroup(int field_number, const FieldDescriptor* field, Message* msg,
375                   const Reflection* reflection) {
376   if (field->is_repeated()) {
377     return reflection->AddMessage(msg, field, nullptr);
378   } else {
379     return reflection->MutableMessage(msg, field, nullptr);
380   }
381 }
382 
_InternalParse(const char * ptr,internal::ParseContext * ctx)383 const char* Message::_InternalParse(const char* ptr,
384                                     internal::ParseContext* ctx) {
385   class ReflectiveFieldParser {
386    public:
387     ReflectiveFieldParser(Message* msg, internal::ParseContext* ctx)
388         : ReflectiveFieldParser(msg, ctx, false) {}
389 
390     void AddVarint(uint32 num, uint64 value) {
391       if (is_item_ && num == 2) {
392         if (!payload_.empty()) {
393           auto field = Field(value, 2);
394           if (field && field->message_type()) {
395             auto child = reflection_->MutableMessage(msg_, field);
396             // TODO(gerbens) signal error
397             child->ParsePartialFromString(payload_);
398           } else {
399             MutableUnknown()->AddLengthDelimited(value)->swap(payload_);
400           }
401           return;
402         }
403         type_id_ = value;
404         return;
405       }
406       auto field = Field(num, 0);
407       if (field) {
408         SetField(value, field, msg_, reflection_);
409       } else {
410         MutableUnknown()->AddVarint(num, value);
411       }
412     }
413     void AddFixed64(uint32 num, uint64 value) {
414       auto field = Field(num, 1);
415       if (field) {
416         SetField(value, field, msg_, reflection_);
417       } else {
418         MutableUnknown()->AddFixed64(num, value);
419       }
420     }
421     const char* ParseLengthDelimited(uint32 num, const char* ptr,
422                                      internal::ParseContext* ctx) {
423       if (is_item_ && num == 3) {
424         if (type_id_ == 0) {
425           return InlineGreedyStringParser(&payload_, ptr, ctx);
426         }
427         num = type_id_;
428         type_id_ = 0;
429       }
430       auto field = Field(num, 2);
431       if (field) {
432         return ParseLenDelim(num, field, msg_, reflection_, ptr, ctx);
433       } else {
434         return InlineGreedyStringParser(
435             MutableUnknown()->AddLengthDelimited(num), ptr, ctx);
436       }
437     }
438     const char* ParseGroup(uint32 num, const char* ptr,
439                            internal::ParseContext* ctx) {
440       if (!is_item_ && descriptor_->options().message_set_wire_format() &&
441           num == 1) {
442         is_item_ = true;
443         ptr = ctx->ParseGroup(this, ptr, num * 8 + 3);
444         is_item_ = false;
445         type_id_ = 0;
446         return ptr;
447       }
448       auto field = Field(num, 3);
449       if (field) {
450         auto msg = GetGroup(num, field, msg_, reflection_);
451         return ctx->ParseGroup(msg, ptr, num * 8 + 3);
452       } else {
453         return UnknownFieldParse(num * 8 + 3, MutableUnknown(), ptr, ctx);
454       }
455     }
456     void AddFixed32(uint32 num, uint32 value) {
457       auto field = Field(num, 5);
458       if (field) {
459         SetField(value, field, msg_, reflection_);
460       } else {
461         MutableUnknown()->AddFixed32(num, value);
462       }
463     }
464 
465     const char* _InternalParse(const char* ptr, internal::ParseContext* ctx) {
466       // We're parsing the a MessageSetItem
467       GOOGLE_DCHECK(is_item_);
468       return internal::WireFormatParser(*this, ptr, ctx);
469     }
470 
471    private:
472     Message* msg_;
473     const Descriptor* descriptor_;
474     const Reflection* reflection_;
475     internal::ParseContext* ctx_;
476     UnknownFieldSet* unknown_ = nullptr;
477     bool is_item_ = false;
478     uint32 type_id_ = 0;
479     std::string payload_;
480 
481     ReflectiveFieldParser(Message* msg, internal::ParseContext* ctx,
482                           bool is_item)
483         : msg_(msg),
484           descriptor_(msg->GetDescriptor()),
485           reflection_(msg->GetReflection()),
486           ctx_(ctx),
487           is_item_(is_item) {
488       GOOGLE_CHECK(descriptor_) << msg->GetTypeName();
489       GOOGLE_CHECK(reflection_) << msg->GetTypeName();
490     }
491 
492     const FieldDescriptor* Field(int num, int wire_type) {
493       auto field = descriptor_->FindFieldByNumber(num);
494 
495       // If that failed, check if the field is an extension.
496       if (field == nullptr && descriptor_->IsExtensionNumber(num)) {
497         const DescriptorPool* pool = ctx_->data().pool;
498         if (pool == NULL) {
499           field = reflection_->FindKnownExtensionByNumber(num);
500         } else {
501           field = pool->FindExtensionByNumber(descriptor_, num);
502         }
503       }
504       if (field == nullptr) return nullptr;
505 
506       if (internal::WireFormat::WireTypeForFieldType(field->type()) !=
507           wire_type) {
508         if (field->is_packable()) {
509           if (wire_type ==
510               internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED) {
511             return field;
512           }
513         }
514         return nullptr;
515       }
516       return field;
517     }
518 
519     UnknownFieldSet* MutableUnknown() {
520       if (unknown_) return unknown_;
521       return unknown_ = reflection_->MutableUnknownFields(msg_);
522     }
523   };
524 
525   ReflectiveFieldParser field_parser(this, ctx);
526   return internal::WireFormatParser(field_parser, ptr, ctx);
527 }
528 #endif  // GOOGLE_PROTOBUF_ENABLE_EXPERIMENTAL_PARSER
529 
SerializeWithCachedSizes(io::CodedOutputStream * output) const530 void Message::SerializeWithCachedSizes(io::CodedOutputStream* output) const {
531   const internal::SerializationTable* table =
532       static_cast<const internal::SerializationTable*>(InternalGetTable());
533   if (table == 0) {
534     WireFormat::SerializeWithCachedSizes(*this, GetCachedSize(), output);
535   } else {
536     internal::TableSerialize(*this, table, output);
537   }
538 }
539 
ByteSizeLong() const540 size_t Message::ByteSizeLong() const {
541   size_t size = WireFormat::ByteSize(*this);
542   SetCachedSize(internal::ToCachedSize(size));
543   return size;
544 }
545 
SetCachedSize(int) const546 void Message::SetCachedSize(int /* size */) const {
547   GOOGLE_LOG(FATAL) << "Message class \"" << GetDescriptor()->full_name()
548              << "\" implements neither SetCachedSize() nor ByteSize().  "
549                 "Must implement one or the other.";
550 }
551 
SpaceUsedLong() const552 size_t Message::SpaceUsedLong() const {
553   return GetReflection()->SpaceUsedLong(*this);
554 }
555 
556 // =============================================================================
557 // MessageFactory
558 
~MessageFactory()559 MessageFactory::~MessageFactory() {}
560 
561 namespace {
562 
563 class GeneratedMessageFactory : public MessageFactory {
564  public:
565   static GeneratedMessageFactory* singleton();
566 
567   void RegisterFile(const google::protobuf::internal::DescriptorTable* table);
568   void RegisterType(const Descriptor* descriptor, const Message* prototype);
569 
570   // implements MessageFactory ---------------------------------------
571   const Message* GetPrototype(const Descriptor* type) override;
572 
573  private:
574   // Only written at static init time, so does not require locking.
575   std::unordered_map<const char*, const google::protobuf::internal::DescriptorTable*,
576                      hash<const char*>, streq>
577       file_map_;
578 
579   internal::WrappedMutex mutex_;
580   // Initialized lazily, so requires locking.
581   std::unordered_map<const Descriptor*, const Message*> type_map_;
582 };
583 
singleton()584 GeneratedMessageFactory* GeneratedMessageFactory::singleton() {
585   static auto instance =
586       internal::OnShutdownDelete(new GeneratedMessageFactory);
587   return instance;
588 }
589 
RegisterFile(const google::protobuf::internal::DescriptorTable * table)590 void GeneratedMessageFactory::RegisterFile(
591     const google::protobuf::internal::DescriptorTable* table) {
592   if (!InsertIfNotPresent(&file_map_, table->filename, table)) {
593     GOOGLE_LOG(FATAL) << "File is already registered: " << table->filename;
594   }
595 }
596 
RegisterType(const Descriptor * descriptor,const Message * prototype)597 void GeneratedMessageFactory::RegisterType(const Descriptor* descriptor,
598                                            const Message* prototype) {
599   GOOGLE_DCHECK_EQ(descriptor->file()->pool(), DescriptorPool::generated_pool())
600       << "Tried to register a non-generated type with the generated "
601          "type registry.";
602 
603   // This should only be called as a result of calling a file registration
604   // function during GetPrototype(), in which case we already have locked
605   // the mutex.
606   mutex_.AssertHeld();
607   if (!InsertIfNotPresent(&type_map_, descriptor, prototype)) {
608     GOOGLE_LOG(DFATAL) << "Type is already registered: " << descriptor->full_name();
609   }
610 }
611 
612 
GetPrototype(const Descriptor * type)613 const Message* GeneratedMessageFactory::GetPrototype(const Descriptor* type) {
614   {
615     ReaderMutexLock lock(&mutex_);
616     const Message* result = FindPtrOrNull(type_map_, type);
617     if (result != NULL) return result;
618   }
619 
620   // If the type is not in the generated pool, then we can't possibly handle
621   // it.
622   if (type->file()->pool() != DescriptorPool::generated_pool()) return NULL;
623 
624   // Apparently the file hasn't been registered yet.  Let's do that now.
625   const internal::DescriptorTable* registration_data =
626       FindPtrOrNull(file_map_, type->file()->name().c_str());
627   if (registration_data == NULL) {
628     GOOGLE_LOG(DFATAL) << "File appears to be in generated pool but wasn't "
629                    "registered: "
630                 << type->file()->name();
631     return NULL;
632   }
633 
634   WriterMutexLock lock(&mutex_);
635 
636   // Check if another thread preempted us.
637   const Message* result = FindPtrOrNull(type_map_, type);
638   if (result == NULL) {
639     // Nope.  OK, register everything.
640     internal::RegisterFileLevelMetadata(registration_data);
641     // Should be here now.
642     result = FindPtrOrNull(type_map_, type);
643   }
644 
645   if (result == NULL) {
646     GOOGLE_LOG(DFATAL) << "Type appears to be in generated pool but wasn't "
647                 << "registered: " << type->full_name();
648   }
649 
650   return result;
651 }
652 
653 }  // namespace
654 
generated_factory()655 MessageFactory* MessageFactory::generated_factory() {
656   return GeneratedMessageFactory::singleton();
657 }
658 
InternalRegisterGeneratedFile(const google::protobuf::internal::DescriptorTable * table)659 void MessageFactory::InternalRegisterGeneratedFile(
660     const google::protobuf::internal::DescriptorTable* table) {
661   GeneratedMessageFactory::singleton()->RegisterFile(table);
662 }
663 
InternalRegisterGeneratedMessage(const Descriptor * descriptor,const Message * prototype)664 void MessageFactory::InternalRegisterGeneratedMessage(
665     const Descriptor* descriptor, const Message* prototype) {
666   GeneratedMessageFactory::singleton()->RegisterType(descriptor, prototype);
667 }
668 
669 
670 namespace {
671 template <typename T>
GetSingleton()672 T* GetSingleton() {
673   static T singleton;
674   return &singleton;
675 }
676 }  // namespace
677 
RepeatedFieldAccessor(const FieldDescriptor * field) const678 const internal::RepeatedFieldAccessor* Reflection::RepeatedFieldAccessor(
679     const FieldDescriptor* field) const {
680   GOOGLE_CHECK(field->is_repeated());
681   switch (field->cpp_type()) {
682 #define HANDLE_PRIMITIVE_TYPE(TYPE, type) \
683   case FieldDescriptor::CPPTYPE_##TYPE:   \
684     return GetSingleton<internal::RepeatedFieldPrimitiveAccessor<type> >();
685     HANDLE_PRIMITIVE_TYPE(INT32, int32)
686     HANDLE_PRIMITIVE_TYPE(UINT32, uint32)
687     HANDLE_PRIMITIVE_TYPE(INT64, int64)
688     HANDLE_PRIMITIVE_TYPE(UINT64, uint64)
689     HANDLE_PRIMITIVE_TYPE(FLOAT, float)
690     HANDLE_PRIMITIVE_TYPE(DOUBLE, double)
691     HANDLE_PRIMITIVE_TYPE(BOOL, bool)
692     HANDLE_PRIMITIVE_TYPE(ENUM, int32)
693 #undef HANDLE_PRIMITIVE_TYPE
694     case FieldDescriptor::CPPTYPE_STRING:
695       switch (field->options().ctype()) {
696         default:
697         case FieldOptions::STRING:
698           return GetSingleton<internal::RepeatedPtrFieldStringAccessor>();
699       }
700       break;
701     case FieldDescriptor::CPPTYPE_MESSAGE:
702       if (field->is_map()) {
703         return GetSingleton<internal::MapFieldAccessor>();
704       } else {
705         return GetSingleton<internal::RepeatedPtrFieldMessageAccessor>();
706       }
707   }
708   GOOGLE_LOG(FATAL) << "Should not reach here.";
709   return NULL;
710 }
711 
712 namespace internal {
713 template <>
714 #if defined(_MSC_VER) && (_MSC_VER >= 1800)
715 // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
716 // #240
717 PROTOBUF_NOINLINE
718 #endif
719     Message*
NewFromPrototype(const Message * prototype,Arena * arena)720     GenericTypeHandler<Message>::NewFromPrototype(const Message* prototype,
721                                                   Arena* arena) {
722   return prototype->New(arena);
723 }
724 template <>
725 #if defined(_MSC_VER) && (_MSC_VER >= 1800)
726 // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
727 // #240
728 PROTOBUF_NOINLINE
729 #endif
730     Arena*
GetArena(Message * value)731     GenericTypeHandler<Message>::GetArena(Message* value) {
732   return value->GetArena();
733 }
734 template <>
735 #if defined(_MSC_VER) && (_MSC_VER >= 1800)
736 // Note: force noinline to workaround MSVC compiler bug with /Zc:inline, issue
737 // #240
738 PROTOBUF_NOINLINE
739 #endif
740     void*
GetMaybeArenaPointer(Message * value)741     GenericTypeHandler<Message>::GetMaybeArenaPointer(Message* value) {
742   return value->GetMaybeArenaPointer();
743 }
744 }  // namespace internal
745 
746 }  // namespace protobuf
747 }  // namespace google
748