summaryrefslogtreecommitdiff
path: root/upb/google/proto2.cc
blob: d7ad919aff95736bc6dcc19908823f18f6276b43 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
//
// upb - a minimalist implementation of protocol buffers.
//
// Copyright (c) 2011-2012 Google Inc.  See LICENSE for details.
// Author: Josh Haberman <jhaberman@gmail.com>
//
// Note that we have received an exception from c-style-artiters regarding
// dynamic_cast<> in this file:
// https://groups.google.com/a/google.com/d/msg/c-style/7Zp_XCX0e7s/I6dpzno4l-MJ
//
// IMPORTANT NOTE!  This file is compiled TWICE, once with UPB_GOOGLE3 defined
// and once without!  This allows us to provide functionality against proto2
// and protobuf opensource both in a single binary without the two conflicting.
// However we must be careful not to violate the ODR.

#include "upb/google/proto2.h"

#include "upb/def.h"
#include "upb/google/proto1.h"
#include "upb/handlers.h"
#include "upb/sink.h"

namespace upb {
namespace google_google3 { class GMR_Handlers; }
namespace google_opensource { class GMR_Handlers; }
}  // namespace upb

// BEGIN DOUBLE COMPILATION TRICKERY. //////////////////////////////////////////

#ifdef UPB_GOOGLE3

#include "net/proto2/proto/descriptor.pb.h"
#include "net/proto2/public/descriptor.h"
#include "net/proto2/public/extension_set.h"
#include "net/proto2/public/generated_message_reflection.h"
#include "net/proto2/public/lazy_field.h"
#include "net/proto2/public/message.h"
#include "net/proto2/public/repeated_field.h"
#include "net/proto2/public/string_piece_field_support.h"

namespace goog = ::proto2;
namespace me = ::upb::google_google3;

#else

// TODO(haberman): remove these once new versions of protobuf that "friend"
// upb are pervasive in the wild.
#define protected public
#include "google/protobuf/repeated_field.h"
#undef protected

#define private public
#include "google/protobuf/generated_message_reflection.h"
#undef private

#include "google/protobuf/descriptor.h"
#include "google/protobuf/descriptor.pb.h"
#include "google/protobuf/extension_set.h"
#include "google/protobuf/message.h"

namespace goog = ::google::protobuf;
namespace me = ::upb::google_opensource;

using goog::int32;
using goog::int64;
using goog::uint32;
using goog::uint64;

#endif  // ifdef UPB_GOOGLE3

// END DOUBLE COMPILATION TRICKERY. ////////////////////////////////////////////

// Have to define this manually since older versions of proto2 didn't define
// an enum value for STRING.
#define UPB_CTYPE_STRING 0

template <class T> static T* GetPointer(void* message, size_t offset) {
  return reinterpret_cast<T*>(static_cast<char*>(message) + offset);
}
template <class T>
static const T* GetConstPointer(const void* message, size_t offset) {
  return reinterpret_cast<const T*>(static_cast<const char*>(message) + offset);
}

// This class contains handlers that can write into a proto2 class whose
// reflection class is GeneratedMessageReflection.  (Despite the name, even
// DynamicMessage uses GeneratedMessageReflection, so this covers all proto2
// messages generated by the compiler.)  To do this it must break the
// encapsulation of GeneratedMessageReflection and therefore depends on
// internal interfaces that are not guaranteed to be stable.  This class will
// need to be updated if any non-backward-compatible changes are made to
// GeneratedMessageReflection.
class me::GMR_Handlers {
 public:
  // Returns true if we were able to set an accessor and any other properties
  // of the FieldDef that are necessary to read/write this field to a
  // proto2::Message.
  static bool TrySet(const goog::FieldDescriptor* proto2_f,
                     const goog::Message& m, const upb::FieldDef* upb_f,
                     upb::Handlers* h) {
    const goog::Reflection* base_r = m.GetReflection();
    // See file comment re: dynamic_cast.
    const goog::internal::GeneratedMessageReflection* r =
        dynamic_cast<const goog::internal::GeneratedMessageReflection*>(base_r);
    if (!r) return false;

#define PRIMITIVE_TYPE(cpptype, cident)                                        \
case goog::FieldDescriptor::cpptype:                                           \
  SetPrimitiveHandlers<cident>(proto2_f, r, upb_f, h);                         \
  return true;

    switch (proto2_f->cpp_type()) {
      PRIMITIVE_TYPE(CPPTYPE_INT32, int32);
      PRIMITIVE_TYPE(CPPTYPE_INT64, int64);
      PRIMITIVE_TYPE(CPPTYPE_UINT32, uint32);
      PRIMITIVE_TYPE(CPPTYPE_UINT64, uint64);
      PRIMITIVE_TYPE(CPPTYPE_DOUBLE, double);
      PRIMITIVE_TYPE(CPPTYPE_FLOAT, float);
      PRIMITIVE_TYPE(CPPTYPE_BOOL, bool);
      case goog::FieldDescriptor::CPPTYPE_ENUM:
        if (proto2_f->is_extension()) {
          SetEnumExtensionHandlers(proto2_f, r, upb_f, h);
        } else {
          SetEnumHandlers(proto2_f, r, upb_f, h);
        }
        return true;
      case goog::FieldDescriptor::CPPTYPE_STRING: {
        if (proto2_f->is_extension()) {
#ifdef UPB_GOOGLE3
          SetStringExtensionHandlers<string>(proto2_f, r, upb_f, h);
#else
          SetStringExtensionHandlers<std::string>(proto2_f, r, upb_f, h);
#endif
          return true;
        }

        // Old versions of the open-source protobuf release erroneously default
        // to Cord even though that has never been supported in the open-source
        // release.
        int32_t ctype = proto2_f->options().has_ctype() ?
                        proto2_f->options().ctype()
                        : UPB_CTYPE_STRING;
        switch (ctype) {
#ifdef UPB_GOOGLE3
          case goog::FieldOptions::STRING:
            SetStringHandlers<string>(proto2_f, r, upb_f, h);
            return true;
          case goog::FieldOptions::CORD:
            SetCordHandlers(proto2_f, r, upb_f, h);
            return true;
          case goog::FieldOptions::STRING_PIECE:
            SetStringPieceHandlers(proto2_f, r, upb_f, h);
            return true;
#else
          case UPB_CTYPE_STRING:
            SetStringHandlers<std::string>(proto2_f, r, upb_f, h);
            return true;
#endif
          default:
            return false;
        }
      }
      case goog::FieldDescriptor::CPPTYPE_MESSAGE:
#ifdef UPB_GOOGLE3
        if (proto2_f->options().lazy()) {
          assert(false);
          return false;  // Not yet implemented.
        }
#endif
        if (proto2_f->is_extension()) {
          SetSubMessageExtensionHandlers(proto2_f, m, r, upb_f, h);
          return true;
        }
        SetSubMessageHandlers(proto2_f, m, r, upb_f, h);
        return true;
      default:
        return false;
    }
  }

#undef PRIMITIVE_TYPE

  static const goog::Message* GetFieldPrototype(
      const goog::Message& m, const goog::FieldDescriptor* f) {
    // We assume that all submessages (and extensions) will be constructed
    // using the same MessageFactory as this message.  This doesn't cover the
    // case of CodedInputStream::SetExtensionRegistry().
    // See file comment re: dynamic_cast.
    const goog::internal::GeneratedMessageReflection* r =
        dynamic_cast<const goog::internal::GeneratedMessageReflection*>(
            m.GetReflection());
    if (!r) return NULL;
    return r->message_factory_->GetPrototype(f->message_type());
  }

 private:
  static upb_selector_t GetSelector(const upb::FieldDef* f,
                                    upb::Handlers::Type type) {
    upb::Handlers::Selector selector;
    bool ok = upb::Handlers::GetSelector(f, type, &selector);
    UPB_ASSERT_VAR(ok, ok);
    return selector;
  }

  static int64_t GetHasbit(
      const goog::FieldDescriptor* f,
      const goog::internal::GeneratedMessageReflection* r) {
    // proto2 does not store hasbits for repeated fields.
    assert(!f->is_repeated());
    return (r->has_bits_offset_ * 8) + f->index();
  }

  static uint16_t GetOffset(
      const goog::FieldDescriptor* f,
      const goog::internal::GeneratedMessageReflection* r) {
    return r->offsets_[f->index()];
  }

  class FieldOffset {
   public:
    FieldOffset(const goog::FieldDescriptor* f,
                const goog::internal::GeneratedMessageReflection* r)
        : offset_(GetOffset(f, r)), is_repeated_(f->is_repeated()) {
      if (!is_repeated_) {
        int64_t hasbit = GetHasbit(f, r);
        hasbyte_ = hasbit / 8;
        mask_ = 1 << (hasbit % 8);
      }
    }

    template <class T> T* GetFieldPointer(void* message) const {
      return GetPointer<T>(message, offset_);
    }

    void SetHasbit(void* m) const {
      assert(!is_repeated_);
      uint8_t* byte = GetPointer<uint8_t>(m, hasbyte_);
      *byte |= mask_;
    }

   private:
    const size_t offset_;
    bool is_repeated_;

    // Only for non-repeated fields.
    int32_t hasbyte_;
    int8_t mask_;
  };

  class ExtensionFieldData {
   public:
    ExtensionFieldData(
        const goog::FieldDescriptor* proto2_f,
        const goog::internal::GeneratedMessageReflection* r)
        : offset_(r->extensions_offset_),
          number_(proto2_f->number()),
          type_(proto2_f->type()) {
    }

    int number() const { return number_; }
    goog::internal::FieldType type() const { return type_; }

    goog::internal::ExtensionSet* GetExtensionSet(goog::Message* m) const {
      return GetPointer<goog::internal::ExtensionSet>(m, offset_);
    }

   private:
    const size_t offset_;
    int number_;
    goog::internal::FieldType type_;
  };

  // StartSequence /////////////////////////////////////////////////////////////

  static void SetStartSequenceHandler(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(f->IsSequence());
    h->SetStartSequenceHandler(f, &PushOffset, new FieldOffset(proto2_f, r),
                               &upb::DeletePointer<FieldOffset>);
  }

  static void* PushOffset(const upb::SinkFrame* frame) {
    const FieldOffset* offset =
        static_cast<FieldOffset*>(frame->handler_data());
    return offset->GetFieldPointer<void>(frame->userdata());
  }

  // Primitive Value (numeric, bool) ///////////////////////////////////////////

  template <typename T> static void SetPrimitiveHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    if (proto2_f->is_extension()) {
      ExtensionFieldData* data = new ExtensionFieldData(proto2_f, r);
      upb::Handlers::Free* free = &upb::DeletePointer<ExtensionFieldData>;
      if (f->IsSequence()) {
        h->SetValueHandler<T>(f, &AppendPrimitiveExtension<T>, data, free);
      } else {
        h->SetValueHandler<T>(f, &SetPrimitiveExtension<T>, data, free);
      }
    } else {
      if (f->IsSequence()) {
        SetStartSequenceHandler(proto2_f, r, f, h);
        h->SetValueHandler<T>(f, &AppendPrimitive<T>, NULL, NULL);
      } else {
        upb::SetStoreValueHandler<T>(f, GetOffset(proto2_f, r),
                                     GetHasbit(proto2_f, r), h);
      }
    }
  }

  template <typename T>
  static bool AppendPrimitive(const upb::SinkFrame* frame, T val) {
    goog::RepeatedField<T>* r =
        static_cast<goog::RepeatedField<T>*>(frame->userdata());
    r->Add(val);
    return true;
  }

  template <typename T>
  static bool AppendPrimitiveExtension(const upb::SinkFrame* frame, T val) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    ExtensionFieldData* data =
        static_cast<ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    // TODO(haberman): give an accurate value for "packed"
    goog::internal::RepeatedPrimitiveTypeTraits<T>::Add(
        data->number(), data->type(), true, val, set);
    return true;
  }

  template <typename T>
  static bool SetPrimitiveExtension(const upb::SinkFrame* frame, T val) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    ExtensionFieldData* data =
        static_cast<ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    goog::internal::PrimitiveTypeTraits<T>::Set(data->number(), data->type(),
                                                val, set);
    return true;
  }

  // Enum //////////////////////////////////////////////////////////////////////

  class EnumHandlerData : public FieldOffset {
   public:
    EnumHandlerData(const goog::FieldDescriptor* proto2_f,
                    const goog::internal::GeneratedMessageReflection* r,
                    const upb::FieldDef* f)
        : FieldOffset(proto2_f, r),
          field_number_(f->number()),
          unknown_fields_offset_(r->unknown_fields_offset_),
          enum_(upb_downcast_enumdef(f->subdef())) {}

    bool IsValidValue(int32_t val) const {
      return enum_->FindValueByNumber(val) != NULL;
    }

    int32_t field_number() const { return field_number_; }

    goog::UnknownFieldSet* mutable_unknown_fields(goog::Message* m) const {
      return GetPointer<goog::UnknownFieldSet>(m, unknown_fields_offset_);
    }

   private:
    int32_t field_number_;
    size_t unknown_fields_offset_;
    const upb::EnumDef* enum_;
  };

  static void SetEnumHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(!proto2_f->is_extension());
    EnumHandlerData* data = new EnumHandlerData(proto2_f, r, f);
    if (f->IsSequence()) {
      h->SetInt32Handler(f, &AppendEnum, data,
                         &upb::DeletePointer<EnumHandlerData>);
    } else {
      h->SetInt32Handler(f, &SetEnum, data,
                         &upb::DeletePointer<EnumHandlerData>);
    }
  }

  static bool SetEnum(const upb::SinkFrame* frame, int32_t val) {
    goog::Message* m = static_cast<goog::Message*>(frame->userdata());
    const EnumHandlerData* data =
        static_cast<const EnumHandlerData*>(frame->handler_data());
    if (data->IsValidValue(val)) {
      int32_t* message_val = data->GetFieldPointer<int32_t>(m);
      *message_val = val;
      data->SetHasbit(m);
    } else {
      data->mutable_unknown_fields(m)->AddVarint(data->field_number(), val);
    }
    return true;
  }

  static bool AppendEnum(const upb::SinkFrame* frame, int32_t val) {
    // Closure is the enclosing message.  We can't use the RepeatedField<> as
    // the closure because we need to go back to the message for unrecognized
    // enum values, which go into the unknown field set.
    goog::Message* m = static_cast<goog::Message*>(frame->userdata());
    const EnumHandlerData* data =
        static_cast<const EnumHandlerData*>(frame->handler_data());
    if (data->IsValidValue(val)) {
      goog::RepeatedField<int32_t>* r =
          data->GetFieldPointer<goog::RepeatedField<int32_t>>(m);
      r->Add(val);
    } else {
      data->mutable_unknown_fields(m)->AddVarint(data->field_number(), val);
    }
    return true;
  }

  // EnumExtension /////////////////////////////////////////////////////////////

  static void SetEnumExtensionHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(proto2_f->is_extension());
    ExtensionFieldData* data = new ExtensionFieldData(proto2_f, r);
    if (f->IsSequence()) {
      h->SetInt32Handler(f, &AppendEnumExtension, data,
                         upb::DeletePointer<ExtensionFieldData>);
    } else {
      h->SetInt32Handler(f, &SetEnumExtension, data,
                         upb::DeletePointer<ExtensionFieldData>);
    }
  }

  static bool SetEnumExtension(const upb::SinkFrame* frame, int32_t val) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const ExtensionFieldData* data =
        static_cast<const ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    set->SetEnum(data->number(), data->type(), val, NULL);
    return true;
  }

  static bool AppendEnumExtension(const upb::SinkFrame* frame, int32_t val) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const ExtensionFieldData* data =
        static_cast<const ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    // TODO(haberman): give an accurate value for "packed"
    set->AddEnum(data->number(), data->type(), true, val, NULL);
    return true;
  }

  // String ////////////////////////////////////////////////////////////////////

  // For scalar (non-repeated) string fields.
  template <class T> class StringHandlerData : public FieldOffset {
   public:
    StringHandlerData(const goog::FieldDescriptor* proto2_f,
                      const goog::internal::GeneratedMessageReflection* r)
        : FieldOffset(proto2_f, r),
          prototype_(*GetConstPointer<T*>(r->default_instance_,
                                          GetOffset(proto2_f, r))) {}

    const T* prototype() const { return prototype_; }

    T** GetStringPointer(void* message) const {
      return GetFieldPointer<T*>(message);
    }

   private:
    const T* prototype_;
  };

  template <typename T> static void SetStringHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f,
      upb::Handlers* h) {
    assert(!proto2_f->is_extension());
    h->SetStringHandler(f, &OnStringBuf<T>, NULL, NULL);
    if (f->IsSequence()) {
      SetStartSequenceHandler(proto2_f, r, f, h);
      h->SetStartStringHandler(f, &StartRepeatedString<T>, NULL, NULL);
    } else {
      StringHandlerData<T>* data = new StringHandlerData<T>(proto2_f, r);
      h->SetStartStringHandler(f, &StartString<T>, data,
                               &upb::DeletePointer<StringHandlerData<T>>);
    }
  }

  // This needs to be templated because google3 string is not std::string.
  template <typename T>
  static void* StartString(const upb::SinkFrame* frame, size_t size_hint) {
    UPB_UNUSED(size_hint);
    goog::Message* m = static_cast<goog::Message*>(frame->userdata());
    const StringHandlerData<T>* data =
        static_cast<const StringHandlerData<T>*>(frame->handler_data());
    T** str = data->GetStringPointer(m);
    data->SetHasbit(m);
    // If it points to the default instance, we must create a new instance.
    if (*str == data->prototype()) *str = new T();
    (*str)->clear();
    // reserve() here appears to hurt performance rather than help.
    return *str;
  }

  template <typename T>
  static size_t OnStringBuf(const upb::SinkFrame* frame,
                            const char* buf, size_t n) {
    T* str = static_cast<T*>(frame->userdata());
    str->append(buf, n);
    return n;
  }

  template <typename T>
  static void* StartRepeatedString(const upb::SinkFrame* frame,
                                   size_t size_hint) {
    UPB_UNUSED(size_hint);
    goog::RepeatedPtrField<T>* r =
        static_cast<goog::RepeatedPtrField<T>*>(frame->userdata());
    T* str = r->Add();
    str->clear();
    // reserve() here appears to hurt performance rather than help.
    return str;
  }

  // StringExtension ///////////////////////////////////////////////////////////

  template <typename T>
  static void SetStringExtensionHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(proto2_f->is_extension());
    h->SetStringHandler(f, &OnStringBuf<T>, NULL, NULL);
    ExtensionFieldData* data = new ExtensionFieldData(proto2_f, r);
    if (f->IsSequence()) {
      h->SetStartStringHandler(f, &StartRepeatedStringExtension, data,
                               upb::DeletePointer<ExtensionFieldData>);
    } else {
      h->SetStartStringHandler(f, &StartStringExtension, data,
                               upb::DeletePointer<ExtensionFieldData>);
    }
  }

  // google3 string is not std::string, but we avoid needing to template
  // because we do not actually have to declare the string type.
  static void* StartStringExtension(const upb::SinkFrame* frame,
                                    size_t size_hint) {
    UPB_UNUSED(size_hint);
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const ExtensionFieldData* data =
        static_cast<const ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    return set->MutableString(data->number(), data->type(), NULL);
  }

  static void* StartRepeatedStringExtension(const upb::SinkFrame* frame,
                                            size_t size_hint) {
    UPB_UNUSED(size_hint);
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const ExtensionFieldData* data =
        static_cast<const ExtensionFieldData*>(frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    return set->AddString(data->number(), data->type(), NULL);
  }

  // SubMessage ////////////////////////////////////////////////////////////////

  class SubMessageHandlerData : public FieldOffset {
   public:
    SubMessageHandlerData(const goog::FieldDescriptor* f,
                          const goog::internal::GeneratedMessageReflection* r,
                          const goog::Message* prototype)
        : FieldOffset(f, r), prototype_(prototype) {}

    const goog::Message* prototype() const { return prototype_; }

   private:
    const goog::Message* const prototype_;
  };

  static void SetSubMessageHandlers(
      const goog::FieldDescriptor* proto2_f, const goog::Message& m,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    const goog::Message* field_prototype = GetFieldPrototype(m, proto2_f);
    SubMessageHandlerData* data =
        new SubMessageHandlerData(proto2_f, r, field_prototype);
    upb::Handlers::Free* free = &upb::DeletePointer<SubMessageHandlerData>;
    if (f->IsSequence()) {
      SetStartSequenceHandler(proto2_f, r, f, h);
      h->SetStartSubMessageHandler(f, &StartRepeatedSubMessage, data, free);
    } else {
      h->SetStartSubMessageHandler(f, &StartSubMessage, data, free);
    }
  }

  static void* StartSubMessage(const upb::SinkFrame* frame) {
    void* m = frame->userdata();
    const SubMessageHandlerData* data =
        static_cast<const SubMessageHandlerData*>(frame->handler_data());
    data->SetHasbit(m);
    goog::Message** subm =
        data->GetFieldPointer<goog::Message*>(frame->userdata());
    if (*subm == NULL || *subm == data->prototype()) {
      *subm = data->prototype()->New();
    }
    return *subm;
  }

  class RepeatedMessageTypeHandler {
   public:
    typedef void Type;
    // AddAllocated() calls this, but only if other objects are sitting
    // around waiting for reuse, which we will not do.
    static void Delete(Type* t) {
      UPB_UNUSED(t);
      assert(false);
    }
  };

  // Closure is a RepeatedPtrField<SubMessageType>*, but we access it through
  // its base class RepeatedPtrFieldBase*.
  static void* StartRepeatedSubMessage(const upb::SinkFrame* frame) {
    const SubMessageHandlerData* data =
        static_cast<const SubMessageHandlerData*>(frame->handler_data());
    goog::internal::RepeatedPtrFieldBase* r =
        static_cast<goog::internal::RepeatedPtrFieldBase*>(frame->userdata());
    void* submsg = r->AddFromCleared<RepeatedMessageTypeHandler>();
    if (!submsg) {
      submsg = data->prototype()->New();
      r->AddAllocated<RepeatedMessageTypeHandler>(submsg);
    }
    return submsg;
  }

  // SubMessageExtension ///////////////////////////////////////////////////////

  class SubMessageExtensionHandlerData : public ExtensionFieldData {
   public:
    SubMessageExtensionHandlerData(
        const goog::FieldDescriptor* proto2_f,
        const goog::internal::GeneratedMessageReflection* r,
        const goog::Message* prototype)
        : ExtensionFieldData(proto2_f, r),
          prototype_(prototype) {
    }

    const goog::Message* prototype() const { return prototype_; }

   private:
    const goog::Message* const prototype_;
  };

  static void SetSubMessageExtensionHandlers(
      const goog::FieldDescriptor* proto2_f,
      const goog::Message& m,
      const goog::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f,
      upb::Handlers* h) {
    const goog::Message* field_prototype = GetFieldPrototype(m, proto2_f);
    SubMessageExtensionHandlerData* data =
        new SubMessageExtensionHandlerData(proto2_f, r, field_prototype);
    upb::Handlers::Free* free = &upb::DeletePointer<SubMessageHandlerData>;
    if (f->IsSequence()) {
      h->SetStartSubMessageHandler(f, &StartRepeatedSubMessageExtension, data,
                                   free);
    } else {
      h->SetStartSubMessageHandler(f, &StartSubMessageExtension, data, free);
    }
  }

  static void* StartRepeatedSubMessageExtension(const upb::SinkFrame* frame) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const SubMessageExtensionHandlerData* data =
        static_cast<const SubMessageExtensionHandlerData*>(
            frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    return set->AddMessage(data->number(), data->type(), *data->prototype(),
                           NULL);
  }

  static void* StartSubMessageExtension(const upb::SinkFrame* frame) {
    goog::Message* m = frame->GetUserdata<goog::Message>();
    const SubMessageExtensionHandlerData* data =
        static_cast<const SubMessageExtensionHandlerData*>(
            frame->handler_data());
    goog::internal::ExtensionSet* set = data->GetExtensionSet(m);
    return set->MutableMessage(data->number(), data->type(), *data->prototype(),
                               NULL);
  }

  // TODO(haberman): handle Unknown Fields.

#ifdef UPB_GOOGLE3
  // Handlers for types/features only included in internal proto2 release:
  // Cord, StringPiece, LazyField, and MessageSet.
  // TODO(haberman): LazyField, MessageSet.

  // Cord //////////////////////////////////////////////////////////////////////

  static void SetCordHandlers(
      const proto2::FieldDescriptor* proto2_f,
      const proto2::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(!proto2_f->is_extension());
    h->SetStringHandler(f, &OnCordBuf, NULL, NULL);
    if (f->IsSequence()) {
      SetStartSequenceHandler(proto2_f, r, f, h);
      h->SetStartStringHandler(f, &StartRepeatedCord, NULL, NULL);
    } else {
      h->SetStartStringHandler(f, &StartCord, new FieldOffset(proto2_f, r),
                               &upb::DeletePointer<FieldOffset*>);
    }
  }

  static void* StartCord(const upb::SinkFrame* frame, size_t size_hint) {
    UPB_UNUSED(size_hint);
    void* m = frame->userdata();
    const FieldOffset* offset =
        static_cast<const FieldOffset*>(frame->handler_data());
    offset->SetHasbit(m);
    Cord* field = offset->GetFieldPointer<Cord>(m);
    field->Clear();
    return field;
  }

  static size_t OnCordBuf(const upb::SinkFrame* frame,
                          const char* buf, size_t n) {
    Cord* c = static_cast<Cord*>(frame->userdata());
    c->Append(StringPiece(buf, n));
    return n;
  }

  static void* StartRepeatedCord(const upb::SinkFrame* frame,
                                 size_t size_hint) {
    UPB_UNUSED(size_hint);
    proto2::RepeatedField<Cord>* r =
        static_cast<proto2::RepeatedField<Cord>*>(frame->userdata());
    return r->Add();
  }

  // StringPiece ///////////////////////////////////////////////////////////////

  static void SetStringPieceHandlers(
      const proto2::FieldDescriptor* proto2_f,
      const proto2::internal::GeneratedMessageReflection* r,
      const upb::FieldDef* f, upb::Handlers* h) {
    assert(!proto2_f->is_extension());
    h->SetStringHandler(f, &OnStringPieceBuf, NULL, NULL);
    if (f->IsSequence()) {
      SetStartSequenceHandler(proto2_f, r, f, h);
      h->SetStartStringHandler(f, &StartRepeatedStringPiece, NULL, NULL);
    } else {
      h->SetStartStringHandler(f, &StartStringPiece,
                               new FieldOffset(proto2_f, r),
                               &upb::DeletePointer<FieldOffset*>);
    }
  }

  static size_t OnStringPieceBuf(const upb::SinkFrame* frame,
                                 const char* buf, size_t len) {
    // TODO(haberman): alias if possible and enabled on the input stream.
    // TODO(haberman): add a method to StringPieceField that lets us avoid
    // this copy/malloc/free.
    proto2::internal::StringPieceField* field =
        static_cast<proto2::internal::StringPieceField*>(frame->userdata());
    size_t new_len = field->size() + len;
    char* data = new char[new_len];
    memcpy(data, field->data(), field->size());
    memcpy(data + field->size(), buf, len);
    field->CopyFrom(StringPiece(data, new_len));
    delete[] data;
    return len;
  }

  static void* StartStringPiece(const upb::SinkFrame* frame,
                                size_t size_hint) {
    UPB_UNUSED(size_hint);
    void* m = frame->userdata();
    const FieldOffset* offset =
        static_cast<const FieldOffset*>(frame->handler_data());
    offset->SetHasbit(m);
    proto2::internal::StringPieceField* field =
        offset->GetFieldPointer<proto2::internal::StringPieceField>(m);
    field->Clear();
    return field;
  }

  static void* StartRepeatedStringPiece(const upb::SinkFrame* frame,
                                        size_t size_hint) {
    UPB_UNUSED(size_hint);
    typedef proto2::RepeatedPtrField<
        proto2::internal::StringPieceField> RepeatedStringPiece;
    RepeatedStringPiece* r =
        static_cast<RepeatedStringPiece*>(frame->userdata());
    proto2::internal::StringPieceField* field = r->Add();
    field->Clear();
    return field;
  }

#endif  // UPB_GOOGLE3
};

namespace upb {
namespace google {

bool TrySetWriteHandlers(const goog::FieldDescriptor* proto2_f,
                         const goog::Message& prototype,
                         const upb::FieldDef* upb_f, upb::Handlers* h) {
  return me::GMR_Handlers::TrySet(proto2_f, prototype, upb_f, h);
}

const goog::Message* GetFieldPrototype(const goog::Message& m,
                                       const goog::FieldDescriptor* f) {
  return me::GMR_Handlers::GetFieldPrototype(m, f);
}

}  // namespace google
}  // namespace upb
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback