text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// This header file defines an internal class that encapsulates internal message
// metadata (Unknown-field set, Arena pointer, ...) and allows its
// representation to be made more space-efficient via various optimizations.
//
// Note that this is distinct from google::protobuf::Metadata, which encapsulates
// Descriptor and Reflection pointers.

#ifndef GOOGLE_PROTOBUF_METADATA_H__
#define GOOGLE_PROTOBUF_METADATA_H__

#include <google/protobuf/metadata_lite.h>
#include <google/protobuf/unknown_field_set.h>

namespace google {
namespace protobuf {
namespace internal {

class InternalMetadataWithArena
    : public InternalMetadataWithArenaBase<UnknownFieldSet,
                                           InternalMetadataWithArena> {
 public:
  InternalMetadataWithArena() {}
  explicit InternalMetadataWithArena(Arena* arena)
      : InternalMetadataWithArenaBase<UnknownFieldSet,
                                           InternalMetadataWithArena>(arena) {}

  void DoSwap(UnknownFieldSet* other) {
    mutable_unknown_fields()->Swap(other);
  }

  void DoMergeFrom(const UnknownFieldSet& other) {
    mutable_unknown_fields()->MergeFrom(other);
  }

  void DoClear() {
    mutable_unknown_fields()->Clear();
  }

  static const UnknownFieldSet& default_instance() {
    return *UnknownFieldSet::default_instance();
  }
};

}  // namespace internal
}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_METADATA_H__

mime_type: text/plain


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// A proto file we will use for unit testing.

syntax = "proto2";

// Some generic_services option(s) added automatically.
// See:  http://go/proto2-generic-services-default
option cc_generic_services = true;     // auto-added
option java_generic_services = true;   // auto-added
option py_generic_services = true;     // auto-added
option cc_enable_arenas = true;

import "google/protobuf/unittest_import.proto";

// We don't put this in a package within proto2 because we need to make sure
// that the generated code doesn't depend on being in the proto2 namespace.
// In test_util.h we do "using namespace unittest = protobuf_unittest".
package protobuf_unittest;

// Protos optimized for SPEED use a strict superset of the generated code
// of equivalent ones optimized for CODE_SIZE, so we should optimize all our
// tests for speed unless explicitly testing code size optimization.
option optimize_for = SPEED;

option java_outer_classname = "UnittestProto";

// This proto includes every type of field in both singular and repeated
// forms.
message TestAllTypes {
  message NestedMessage {
    // The field name "b" fails to compile in proto1 because it conflicts with
    // a local variable named "b" in one of the generated methods.  Doh.
    // This file needs to compile in proto1 to test backwards-compatibility.
    optional int32 bb = 1;
  }

  enum NestedEnum {
    FOO = 1;
    BAR = 2;
    BAZ = 3;
    NEG = -1;  // Intentionally negative.
  }

  // Singular
  optional    int32 optional_int32    =  1;
  optional    int64 optional_int64    =  2;
  optional   uint32 optional_uint32   =  3;
  optional   uint64 optional_uint64   =  4;
  optional   sint32 optional_sint32   =  5;
  optional   sint64 optional_sint64   =  6;
  optional  fixed32 optional_fixed32  =  7;
  optional  fixed64 optional_fixed64  =  8;
  optional sfixed32 optional_sfixed32 =  9;
  optional sfixed64 optional_sfixed64 = 10;
  optional    float optional_float    = 11;
  optional   double optional_double   = 12;
  optional     bool optional_bool     = 13;
  optional   string optional_string   = 14;
  optional    bytes optional_bytes    = 15;

  optional group OptionalGroup = 16 {
    optional int32 a = 17;
  }

  optional NestedMessage                        optional_nested_message  = 18;
  optional ForeignMessage                       optional_foreign_message = 19;
  optional protobuf_unittest_import.ImportMessage optional_import_message  = 20;

  optional NestedEnum                           optional_nested_enum     = 21;
  optional ForeignEnum                          optional_foreign_enum    = 22;
  optional protobuf_unittest_import.ImportEnum    optional_import_enum     = 23;

  optional string optional_string_piece = 24 [ctype=STRING_PIECE];
  optional string optional_cord = 25 [ctype=CORD];

  // Defined in unittest_import_public.proto
  optional protobuf_unittest_import.PublicImportMessage
      optional_public_import_message = 26;

  optional NestedMessage optional_lazy_message = 27 [lazy=true];

  // Repeated
  repeated    int32 repeated_int32    = 31;
  repeated    int64 repeated_int64    = 32;
  repeated   uint32 repeated_uint32   = 33;
  repeated   uint64 repeated_uint64   = 34;
  repeated   sint32 repeated_sint32   = 35;
  repeated   sint64 repeated_sint64   = 36;
  repeated  fixed32 repeated_fixed32  = 37;
  repeated  fixed64 repeated_fixed64  = 38;
  repeated sfixed32 repeated_sfixed32 = 39;
  repeated sfixed64 repeated_sfixed64 = 40;
  repeated    float repeated_float    = 41;
  repeated   double repeated_double   = 42;
  repeated     bool repeated_bool     = 43;
  repeated   string repeated_string   = 44;
  repeated    bytes repeated_bytes    = 45;

  repeated group RepeatedGroup = 46 {
    optional int32 a = 47;
  }

  repeated NestedMessage                        repeated_nested_message  = 48;
  repeated ForeignMessage                       repeated_foreign_message = 49;
  repeated protobuf_unittest_import.ImportMessage repeated_import_message  = 50;

  repeated NestedEnum                           repeated_nested_enum     = 51;
  repeated ForeignEnum                          repeated_foreign_enum    = 52;
  repeated protobuf_unittest_import.ImportEnum    repeated_import_enum     = 53;

  repeated string repeated_string_piece = 54 [ctype=STRING_PIECE];
  repeated string repeated_cord = 55 [ctype=CORD];

  repeated NestedMessage repeated_lazy_message = 57 [lazy=true];

  // Singular with defaults
  optional    int32 default_int32    = 61 [default =  41    ];
  optional    int64 default_int64    = 62 [default =  42    ];
  optional   uint32 default_uint32   = 63 [default =  43    ];
  optional   uint64 default_uint64   = 64 [default =  44    ];
  optional   sint32 default_sint32   = 65 [default = -45    ];
  optional   sint64 default_sint64   = 66 [default =  46    ];
  optional  fixed32 default_fixed32  = 67 [default =  47    ];
  optional  fixed64 default_fixed64  = 68 [default =  48    ];
  optional sfixed32 default_sfixed32 = 69 [default =  49    ];
  optional sfixed64 default_sfixed64 = 70 [default = -50    ];
  optional    float default_float    = 71 [default =  51.5  ];
  optional   double default_double   = 72 [default =  52e3  ];
  optional     bool default_bool     = 73 [default = true   ];
  optional   string default_string   = 74 [default = "hello"];
  optional    bytes default_bytes    = 75 [default = "world"];

  optional NestedEnum  default_nested_enum  = 81 [default = BAR        ];
  optional ForeignEnum default_foreign_enum = 82 [default = FOREIGN_BAR];
  optional protobuf_unittest_import.ImportEnum
      default_import_enum = 83 [default = IMPORT_BAR];

  optional string default_string_piece = 84 [ctype=STRING_PIECE,default="abc"];
  optional string default_cord = 85 [ctype=CORD,default="123"];

  // For oneof test
  oneof oneof_field {
    uint32 oneof_uint32 = 111;
    NestedMessage oneof_nested_message = 112;
    string oneof_string = 113;
    bytes oneof_bytes = 114;
  }
}

// This proto includes a recusively nested message.
message NestedTestAllTypes {
  optional NestedTestAllTypes child = 1;
  optional TestAllTypes payload = 2;
  repeated NestedTestAllTypes repeated_child = 3;
}

message TestDeprecatedFields {
  optional int32 deprecated_int32 = 1 [deprecated=true];
}

message TestDeprecatedMessage {
  option deprecated = true;
}

// Define these after TestAllTypes to make sure the compiler can handle
// that.
message ForeignMessage {
  optional int32 c = 1;
  optional int32 d = 2;
}

enum ForeignEnum {
  FOREIGN_FOO = 4;
  FOREIGN_BAR = 5;
  FOREIGN_BAZ = 6;
}

message TestReservedFields {
  reserved 2, 15, 9 to 11;
  reserved "bar", "baz";
}

message TestAllExtensions {
  extensions 1 to max;
}

extend TestAllExtensions {
  // Singular
  optional    int32 optional_int32_extension    =  1;
  optional    int64 optional_int64_extension    =  2;
  optional   uint32 optional_uint32_extension   =  3;
  optional   uint64 optional_uint64_extension   =  4;
  optional   sint32 optional_sint32_extension   =  5;
  optional   sint64 optional_sint64_extension   =  6;
  optional  fixed32 optional_fixed32_extension  =  7;
  optional  fixed64 optional_fixed64_extension  =  8;
  optional sfixed32 optional_sfixed32_extension =  9;
  optional sfixed64 optional_sfixed64_extension = 10;
  optional    float optional_float_extension    = 11;
  optional   double optional_double_extension   = 12;
  optional     bool optional_bool_extension     = 13;
  optional   string optional_string_extension   = 14;
  optional    bytes optional_bytes_extension    = 15;

  optional group OptionalGroup_extension = 16 {
    optional int32 a = 17;
  }

  optional TestAllTypes.NestedMessage optional_nested_message_extension = 18;
  optional ForeignMessage optional_foreign_message_extension = 19;
  optional protobuf_unittest_import.ImportMessage
    optional_import_message_extension = 20;

  optional TestAllTypes.NestedEnum optional_nested_enum_extension = 21;
  optional ForeignEnum optional_foreign_enum_extension = 22;
  optional protobuf_unittest_import.ImportEnum
    optional_import_enum_extension = 23;

  optional string optional_string_piece_extension = 24 [ctype=STRING_PIECE];
  optional string optional_cord_extension = 25 [ctype=CORD];

  optional protobuf_unittest_import.PublicImportMessage
    optional_public_import_message_extension = 26;

  optional TestAllTypes.NestedMessage
    optional_lazy_message_extension = 27 [lazy=true];

  // Repeated
  repeated    int32 repeated_int32_extension    = 31;
  repeated    int64 repeated_int64_extension    = 32;
  repeated   uint32 repeated_uint32_extension   = 33;
  repeated   uint64 repeated_uint64_extension   = 34;
  repeated   sint32 repeated_sint32_extension   = 35;
  repeated   sint64 repeated_sint64_extension   = 36;
  repeated  fixed32 repeated_fixed32_extension  = 37;
  repeated  fixed64 repeated_fixed64_extension  = 38;
  repeated sfixed32 repeated_sfixed32_extension = 39;
  repeated sfixed64 repeated_sfixed64_extension = 40;
  repeated    float repeated_float_extension    = 41;
  repeated   double repeated_double_extension   = 42;
  repeated     bool repeated_bool_extension     = 43;
  repeated   string repeated_string_extension   = 44;
  repeated    bytes repeated_bytes_extension    = 45;

  repeated group RepeatedGroup_extension = 46 {
    optional int32 a = 47;
  }

  repeated TestAllTypes.NestedMessage repeated_nested_message_extension = 48;
  repeated ForeignMessage repeated_foreign_message_extension = 49;
  repeated protobuf_unittest_import.ImportMessage
    repeated_import_message_extension = 50;

  repeated TestAllTypes.NestedEnum repeated_nested_enum_extension = 51;
  repeated ForeignEnum repeated_foreign_enum_extension = 52;
  repeated protobuf_unittest_import.ImportEnum
    repeated_import_enum_extension = 53;

  repeated string repeated_string_piece_extension = 54 [ctype=STRING_PIECE];
  repeated string repeated_cord_extension = 55 [ctype=CORD];

  repeated TestAllTypes.NestedMessage
    repeated_lazy_message_extension = 57 [lazy=true];

  // Singular with defaults
  optional    int32 default_int32_extension    = 61 [default =  41    ];
  optional    int64 default_int64_extension    = 62 [default =  42    ];
  optional   uint32 default_uint32_extension   = 63 [default =  43    ];
  optional   uint64 default_uint64_extension   = 64 [default =  44    ];
  optional   sint32 default_sint32_extension   = 65 [default = -45    ];
  optional   sint64 default_sint64_extension   = 66 [default =  46    ];
  optional  fixed32 default_fixed32_extension  = 67 [default =  47    ];
  optional  fixed64 default_fixed64_extension  = 68 [default =  48    ];
  optional sfixed32 default_sfixed32_extension = 69 [default =  49    ];
  optional sfixed64 default_sfixed64_extension = 70 [default = -50    ];
  optional    float default_float_extension    = 71 [default =  51.5  ];
  optional   double default_double_extension   = 72 [default =  52e3  ];
  optional     bool default_bool_extension     = 73 [default = true   ];
  optional   string default_string_extension   = 74 [default = "hello"];
  optional    bytes default_bytes_extension    = 75 [default = "world"];

  optional TestAllTypes.NestedEnum
    default_nested_enum_extension = 81 [default = BAR];
  optional ForeignEnum
    default_foreign_enum_extension = 82 [default = FOREIGN_BAR];
  optional protobuf_unittest_import.ImportEnum
    default_import_enum_extension = 83 [default = IMPORT_BAR];

  optional string default_string_piece_extension = 84 [ctype=STRING_PIECE,
                                                       default="abc"];
  optional string default_cord_extension = 85 [ctype=CORD, default="123"];

  // For oneof test
  optional uint32 oneof_uint32_extension = 111;
  optional TestAllTypes.NestedMessage oneof_nested_message_extension = 112;
  optional string oneof_string_extension = 113;
  optional bytes oneof_bytes_extension = 114;
}

message TestNestedExtension {
  extend TestAllExtensions {
    // Check for bug where string extensions declared in tested scope did not
    // compile.
    optional string test = 1002 [default="test"];
    // Used to test if generated extension name is correct when there are
    // underscores.
    optional string nested_string_extension = 1003;
  }
}

// We have separate messages for testing required fields because it's
// annoying to have to fill in required fields in TestProto in order to
// do anything with it.  Note that we don't need to test every type of
// required filed because the code output is basically identical to
// optional fields for all types.
message TestRequired {
  required int32 a = 1;
  optional int32 dummy2 = 2;
  required int32 b = 3;

  extend TestAllExtensions {
    optional TestRequired single = 1000;
    repeated TestRequired multi  = 1001;
  }

  // Pad the field count to 32 so that we can test that IsInitialized()
  // properly checks multiple elements of has_bits_.
  optional int32 dummy4  =  4;
  optional int32 dummy5  =  5;
  optional int32 dummy6  =  6;
  optional int32 dummy7  =  7;
  optional int32 dummy8  =  8;
  optional int32 dummy9  =  9;
  optional int32 dummy10 = 10;
  optional int32 dummy11 = 11;
  optional int32 dummy12 = 12;
  optional int32 dummy13 = 13;
  optional int32 dummy14 = 14;
  optional int32 dummy15 = 15;
  optional int32 dummy16 = 16;
  optional int32 dummy17 = 17;
  optional int32 dummy18 = 18;
  optional int32 dummy19 = 19;
  optional int32 dummy20 = 20;
  optional int32 dummy21 = 21;
  optional int32 dummy22 = 22;
  optional int32 dummy23 = 23;
  optional int32 dummy24 = 24;
  optional int32 dummy25 = 25;
  optional int32 dummy26 = 26;
  optional int32 dummy27 = 27;
  optional int32 dummy28 = 28;
  optional int32 dummy29 = 29;
  optional int32 dummy30 = 30;
  optional int32 dummy31 = 31;
  optional int32 dummy32 = 32;

  required int32 c = 33;
}

message TestRequiredForeign {
  optional TestRequired optional_message = 1;
  repeated TestRequired repeated_message = 2;
  optional int32 dummy = 3;
}

message TestRequiredMessage {
  optional TestRequired optional_message = 1;
  repeated TestRequired repeated_message = 2;
  required TestRequired required_message = 3;
}

// Test that we can use NestedMessage from outside TestAllTypes.
message TestForeignNested {
  optional TestAllTypes.NestedMessage foreign_nested = 1;
}

// TestEmptyMessage is used to test unknown field support.
message TestEmptyMessage {
}

// Like above, but declare all field numbers as potential extensions.  No
// actual extensions should ever be defined for this type.
message TestEmptyMessageWithExtensions {
  extensions 1 to max;
}

message TestMultipleExtensionRanges {
  extensions 42;
  extensions 4143 to 4243;
  extensions 65536 to max;
}

// Test that really large tag numbers don't break anything.
message TestReallyLargeTagNumber {
  // The largest possible tag number is 2^28 - 1, since the wire format uses
  // three bits to communicate wire type.
  optional int32 a = 1;
  optional int32 bb = 268435455;
}

message TestRecursiveMessage {
  optional TestRecursiveMessage a = 1;
  optional int32 i = 2;
}

// Test that mutual recursion works.
message TestMutualRecursionA {
  message SubMessage {
    optional TestMutualRecursionB b = 1;
  }
  optional TestMutualRecursionB bb = 1;
  optional group SubGroup = 2 {
    optional SubMessage sub_message = 3;  // Needed because of bug in javatest
    optional TestAllTypes not_in_this_scc = 4;
  }
}

message TestMutualRecursionB {
  optional TestMutualRecursionA a = 1;
  optional int32 optional_int32 = 2;
}

message TestIsInitialized {
  message SubMessage {
    optional group SubGroup = 1 {
      required int32 i = 2;
    }
  }
  optional SubMessage sub_message = 1;
}

// Test that groups have disjoint field numbers from their siblings and
// parents.  This is NOT possible in proto1; only google.protobuf.  When attempting
// to compile with proto1, this will emit an error; so we only include it
// in protobuf_unittest_proto.
message TestDupFieldNumber {                        // NO_PROTO1
  optional int32 a = 1;                             // NO_PROTO1
  optional group Foo = 2 { optional int32 a = 1; }  // NO_PROTO1
  optional group Bar = 3 { optional int32 a = 1; }  // NO_PROTO1
}                                                   // NO_PROTO1

// Additional messages for testing lazy fields.
message TestEagerMessage {
  optional TestAllTypes sub_message = 1 [lazy=false];
}
message TestLazyMessage {
  optional TestAllTypes sub_message = 1 [lazy=true];
}

// Needed for a Python test.
message TestNestedMessageHasBits {
  message NestedMessage {
    repeated int32 nestedmessage_repeated_int32 = 1;
    repeated ForeignMessage nestedmessage_repeated_foreignmessage = 2;
  }
  optional NestedMessage optional_nested_message = 1;
}

// Test an enum that has multiple values with the same number.
enum TestEnumWithDupValue {
  option allow_alias = true;

  FOO1 = 1;
  BAR1 = 2;
  BAZ = 3;
  FOO2 = 1;
  BAR2 = 2;
}

// Test an enum with large, unordered values.
enum TestSparseEnum {
  SPARSE_A = 123;
  SPARSE_B = 62374;
  SPARSE_C = 12589234;
  SPARSE_D = -15;
  SPARSE_E = -53452;
  SPARSE_F = 0;
  SPARSE_G = 2;
}

// Test message with CamelCase field names.  This violates Protocol Buffer
// standard style.
message TestCamelCaseFieldNames {
  optional int32 PrimitiveField = 1;
  optional string StringField = 2;
  optional ForeignEnum EnumField = 3;
  optional ForeignMessage MessageField = 4;
  optional string StringPieceField = 5 [ctype=STRING_PIECE];
  optional string CordField = 6 [ctype=CORD];

  repeated int32 RepeatedPrimitiveField = 7;
  repeated string RepeatedStringField = 8;
  repeated ForeignEnum RepeatedEnumField = 9;
  repeated ForeignMessage RepeatedMessageField = 10;
  repeated string RepeatedStringPieceField = 11 [ctype=STRING_PIECE];
  repeated string RepeatedCordField = 12 [ctype=CORD];
}

// We list fields out of order, to ensure that we're using field number and not
// field index to determine serialization order.
message TestFieldOrderings {
  optional string my_string = 11;
  extensions 2 to 10;
  optional int64 my_int = 1;
  extensions 12 to 100;
  optional float my_float = 101;
  message NestedMessage {
    optional int64 oo = 2;
    // The field name "b" fails to compile in proto1 because it conflicts with
    // a local variable named "b" in one of the generated methods.  Doh.
    // This file needs to compile in proto1 to test backwards-compatibility.
    optional int32 bb = 1;
  }

  optional NestedMessage optional_nested_message  = 200;
}

extend TestFieldOrderings {
  optional string my_extension_string = 50;
  optional int32 my_extension_int = 5;
}

message TestExtremeDefaultValues {
  optional bytes escaped_bytes = 1 [default = "\0\001\a\b\f\n\r\t\v\\\'\"\xfe"];
  optional uint32 large_uint32 = 2 [default = 0xFFFFFFFF];
  optional uint64 large_uint64 = 3 [default = 0xFFFFFFFFFFFFFFFF];
  optional  int32 small_int32  = 4 [default = -0x7FFFFFFF];
  optional  int64 small_int64  = 5 [default = -0x7FFFFFFFFFFFFFFF];
  optional  int32 really_small_int32 = 21 [default = -0x80000000];
  optional  int64 really_small_int64 = 22 [default = -0x8000000000000000];

  // The default value here is UTF-8 for "\u1234".  (We could also just type
  // the UTF-8 text directly into this text file rather than escape it, but
  // lots of people use editors that would be confused by this.)
  optional string utf8_string = 6 [default = "\341\210\264"];

  // Tests for single-precision floating-point values.
  optional float zero_float = 7 [default = 0];
  optional float one_float = 8 [default = 1];
  optional float small_float = 9 [default = 1.5];
  optional float negative_one_float = 10 [default = -1];
  optional float negative_float = 11 [default = -1.5];
  // Using exponents
  optional float large_float = 12 [default = 2E8];
  optional float small_negative_float = 13 [default = -8e-28];

  // Text for nonfinite floating-point values.
  optional double inf_double = 14 [default = inf];
  optional double neg_inf_double = 15 [default = -inf];
  optional double nan_double = 16 [default = nan];
  optional float inf_float = 17 [default = inf];
  optional float neg_inf_float = 18 [default = -inf];
  optional float nan_float = 19 [default = nan];

  // Tests for C++ trigraphs.
  // Trigraphs should be escaped in C++ generated files, but they should not be
  // escaped for other languages.
  // Note that in .proto file, "\?" is a valid way to escape ? in string
  // literals.
  optional string cpp_trigraph = 20 [default = "? \? ?? \?? \??? ??/ ?\?-"];

  // String defaults containing the character '\000'
  optional string string_with_zero       = 23 [default = "hel\000lo"];
  optional  bytes bytes_with_zero        = 24 [default = "wor\000ld"];
  optional string string_piece_with_zero = 25 [ctype=STRING_PIECE,
                                               default="ab\000c"];
  optional string cord_with_zero         = 26 [ctype=CORD,
                                               default="12\0003"];
  optional string replacement_string     = 27 [default="${unknown}"];
}

message SparseEnumMessage {
  optional TestSparseEnum sparse_enum = 1;
}

// Test String and Bytes: string is for valid UTF-8 strings
message OneString {
  optional string data = 1;
}

message MoreString {
  repeated string data = 1;
}

message OneBytes {
  optional bytes data = 1;
}

message MoreBytes {
  repeated bytes data = 1;
}

// Test int32, uint32, int64, uint64, and bool are all compatible
message Int32Message {
  optional int32 data = 1;
}

message Uint32Message {
  optional uint32 data = 1;
}

message Int64Message {
  optional int64 data = 1;
}

message Uint64Message {
  optional uint64 data = 1;
}

message BoolMessage {
  optional bool data = 1;
}

// Test oneofs.
message TestOneof {
  oneof foo {
    int32 foo_int = 1;
    string foo_string = 2;
    TestAllTypes foo_message = 3;
    group FooGroup = 4 {
      optional int32 a = 5;
      optional string b = 6;
    }
  }
}

message TestOneofBackwardsCompatible {
  optional int32 foo_int = 1;
  optional string foo_string = 2;
  optional TestAllTypes foo_message = 3;
  optional group FooGroup = 4 {
    optional int32 a = 5;
    optional string b = 6;
  }
}

message TestOneof2 {
  oneof foo {
    int32 foo_int = 1;
    string foo_string = 2;
    string foo_cord = 3 [ctype=CORD];
    string foo_string_piece = 4 [ctype=STRING_PIECE];
    bytes foo_bytes = 5;
    NestedEnum foo_enum = 6;
    NestedMessage foo_message = 7;
    group FooGroup = 8 {
      optional int32 a = 9;
      optional string b = 10;
    }
    NestedMessage foo_lazy_message = 11 [lazy=true];
  }

  oneof bar {
    int32 bar_int = 12 [default = 5];
    string bar_string = 13 [default = "STRING"];
    string bar_cord = 14 [ctype=CORD, default = "CORD"];
    string bar_string_piece = 15 [ctype=STRING_PIECE, default = "SPIECE"];
    bytes bar_bytes = 16 [default = "BYTES"];
    NestedEnum bar_enum = 17 [default = BAR];
  }

  optional int32 baz_int = 18;
  optional string baz_string = 19 [default = "BAZ"];

  message NestedMessage {
    optional int64 qux_int = 1;
    repeated int32 corge_int = 2;
  }

  enum NestedEnum {
    FOO = 1;
    BAR = 2;
    BAZ = 3;
  }
}

message TestRequiredOneof {
  oneof foo {
    int32 foo_int = 1;
    string foo_string = 2;
    NestedMessage foo_message = 3;
  }
  message NestedMessage {
    required double required_double = 1;
  }
}

// Test messages for packed fields

message TestPackedTypes {
  repeated    int32 packed_int32    =  90 [packed = true];
  repeated    int64 packed_int64    =  91 [packed = true];
  repeated   uint32 packed_uint32   =  92 [packed = true];
  repeated   uint64 packed_uint64   =  93 [packed = true];
  repeated   sint32 packed_sint32   =  94 [packed = true];
  repeated   sint64 packed_sint64   =  95 [packed = true];
  repeated  fixed32 packed_fixed32  =  96 [packed = true];
  repeated  fixed64 packed_fixed64  =  97 [packed = true];
  repeated sfixed32 packed_sfixed32 =  98 [packed = true];
  repeated sfixed64 packed_sfixed64 =  99 [packed = true];
  repeated    float packed_float    = 100 [packed = true];
  repeated   double packed_double   = 101 [packed = true];
  repeated     bool packed_bool     = 102 [packed = true];
  repeated ForeignEnum packed_enum  = 103 [packed = true];
}

// A message with the same fields as TestPackedTypes, but without packing. Used
// to test packed <-> unpacked wire compatibility.
message TestUnpackedTypes {
  repeated    int32 unpacked_int32    =  90 [packed = false];
  repeated    int64 unpacked_int64    =  91 [packed = false];
  repeated   uint32 unpacked_uint32   =  92 [packed = false];
  repeated   uint64 unpacked_uint64   =  93 [packed = false];
  repeated   sint32 unpacked_sint32   =  94 [packed = false];
  repeated   sint64 unpacked_sint64   =  95 [packed = false];
  repeated  fixed32 unpacked_fixed32  =  96 [packed = false];
  repeated  fixed64 unpacked_fixed64  =  97 [packed = false];
  repeated sfixed32 unpacked_sfixed32 =  98 [packed = false];
  repeated sfixed64 unpacked_sfixed64 =  99 [packed = false];
  repeated    float unpacked_float    = 100 [packed = false];
  repeated   double unpacked_double   = 101 [packed = false];
  repeated     bool unpacked_bool     = 102 [packed = false];
  repeated ForeignEnum unpacked_enum  = 103 [packed = false];
}

message TestPackedExtensions {
  extensions 1 to max;
}

extend TestPackedExtensions {
  repeated    int32 packed_int32_extension    =  90 [packed = true];
  repeated    int64 packed_int64_extension    =  91 [packed = true];
  repeated   uint32 packed_uint32_extension   =  92 [packed = true];
  repeated   uint64 packed_uint64_extension   =  93 [packed = true];
  repeated   sint32 packed_sint32_extension   =  94 [packed = true];
  repeated   sint64 packed_sint64_extension   =  95 [packed = true];
  repeated  fixed32 packed_fixed32_extension  =  96 [packed = true];
  repeated  fixed64 packed_fixed64_extension  =  97 [packed = true];
  repeated sfixed32 packed_sfixed32_extension =  98 [packed = true];
  repeated sfixed64 packed_sfixed64_extension =  99 [packed = true];
  repeated    float packed_float_extension    = 100 [packed = true];
  repeated   double packed_double_extension   = 101 [packed = true];
  repeated     bool packed_bool_extension     = 102 [packed = true];
  repeated ForeignEnum packed_enum_extension  = 103 [packed = true];
}

message TestUnpackedExtensions {
  extensions 1 to max;
}

extend TestUnpackedExtensions {
  repeated    int32 unpacked_int32_extension    =  90 [packed = false];
  repeated    int64 unpacked_int64_extension    =  91 [packed = false];
  repeated   uint32 unpacked_uint32_extension   =  92 [packed = false];
  repeated   uint64 unpacked_uint64_extension   =  93 [packed = false];
  repeated   sint32 unpacked_sint32_extension   =  94 [packed = false];
  repeated   sint64 unpacked_sint64_extension   =  95 [packed = false];
  repeated  fixed32 unpacked_fixed32_extension  =  96 [packed = false];
  repeated  fixed64 unpacked_fixed64_extension  =  97 [packed = false];
  repeated sfixed32 unpacked_sfixed32_extension =  98 [packed = false];
  repeated sfixed64 unpacked_sfixed64_extension =  99 [packed = false];
  repeated    float unpacked_float_extension    = 100 [packed = false];
  repeated   double unpacked_double_extension   = 101 [packed = false];
  repeated     bool unpacked_bool_extension     = 102 [packed = false];
  repeated ForeignEnum unpacked_enum_extension  = 103 [packed = false];
}

// Used by ExtensionSetTest/DynamicExtensions.  The test actually builds
// a set of extensions to TestAllExtensions dynamically, based on the fields
// of this message type.
message TestDynamicExtensions {
  enum DynamicEnumType {
    DYNAMIC_FOO = 2200;
    DYNAMIC_BAR = 2201;
    DYNAMIC_BAZ = 2202;
  }
  message DynamicMessageType {
    optional int32 dynamic_field = 2100;
  }

  optional fixed32 scalar_extension = 2000;
  optional ForeignEnum enum_extension = 2001;
  optional DynamicEnumType dynamic_enum_extension = 2002;

  optional ForeignMessage message_extension = 2003;
  optional DynamicMessageType dynamic_message_extension = 2004;

  repeated string repeated_extension = 2005;
  repeated sint32 packed_extension = 2006 [packed = true];
}

message TestRepeatedScalarDifferentTagSizes {
  // Parsing repeated fixed size values used to fail. This message needs to be
  // used in order to get a tag of the right size; all of the repeated fields
  // in TestAllTypes didn't trigger the check.
  repeated fixed32 repeated_fixed32 = 12;
  // Check for a varint type, just for good measure.
  repeated int32   repeated_int32   = 13;

  // These have two-byte tags.
  repeated fixed64 repeated_fixed64 = 2046;
  repeated int64   repeated_int64   = 2047;

  // Three byte tags.
  repeated float   repeated_float   = 262142;
  repeated uint64  repeated_uint64  = 262143;
}

// Test that if an optional or required message/group field appears multiple
// times in the input, they need to be merged.
message TestParsingMerge {
  // RepeatedFieldsGenerator defines matching field types as TestParsingMerge,
  // except that all fields are repeated. In the tests, we will serialize the
  // RepeatedFieldsGenerator to bytes, and parse the bytes to TestParsingMerge.
  // Repeated fields in RepeatedFieldsGenerator are expected to be merged into
  // the corresponding required/optional fields in TestParsingMerge.
  message RepeatedFieldsGenerator {
    repeated TestAllTypes field1 = 1;
    repeated TestAllTypes field2 = 2;
    repeated TestAllTypes field3 = 3;
    repeated group Group1 = 10 {
      optional TestAllTypes field1 = 11;
    }
    repeated group Group2 = 20 {
      optional TestAllTypes field1 = 21;
    }
    repeated TestAllTypes ext1 = 1000;
    repeated TestAllTypes ext2 = 1001;
  }
  required TestAllTypes required_all_types = 1;
  optional TestAllTypes optional_all_types = 2;
  repeated TestAllTypes repeated_all_types = 3;
  optional group OptionalGroup = 10 {
    optional TestAllTypes optional_group_all_types = 11;
  }
  repeated group RepeatedGroup = 20 {
    optional TestAllTypes repeated_group_all_types = 21;
  }
  extensions 1000 to max;
  extend TestParsingMerge {
    optional TestAllTypes optional_ext = 1000;
    repeated TestAllTypes repeated_ext = 1001;
  }
}

message TestCommentInjectionMessage {
  // */ <- This should not close the generated doc comment
  optional string a = 1 [default="*/ <- Neither should this."];
}

// Test that RPC services work.
message FooRequest  {}
message FooResponse {}

message FooClientMessage {}
message FooServerMessage{}

service TestService {
  rpc Foo(FooRequest) returns (FooResponse);
  rpc Bar(BarRequest) returns (BarResponse);
}

message BarRequest  {}
message BarResponse {}

message TestJsonName {
  optional int32 field_name1 = 1;
  optional int32 fieldName2 = 2;
  optional int32 FieldName3 = 3;
  optional int32 _field_name4 = 4;
  optional int32 FIELD_NAME5 = 5;
  optional int32 field_name6 = 6 [json_name = "@type"];
}

message TestHugeFieldNumbers {
  optional int32 optional_int32 = 536870000;
  optional int32 fixed_32 = 536870001;
  repeated int32 repeated_int32 = 536870002 [packed = false];
  repeated int32 packed_int32 = 536870003 [packed = true];

  optional ForeignEnum optional_enum = 536870004;
  optional string optional_string = 536870005;
  optional bytes optional_bytes = 536870006;
  optional ForeignMessage optional_message = 536870007;

  optional group OptionalGroup = 536870008 {
    optional int32 group_a = 536870009;
  }

  map<string, string> string_string_map = 536870010;

  oneof oneof_field {
    uint32 oneof_uint32 = 536870011;
    TestAllTypes oneof_test_all_types = 536870012;
    string oneof_string = 536870013;
    bytes oneof_bytes = 536870014;
  }

  extensions  536860000 to 536869999;
}

extend TestHugeFieldNumbers {
  optional TestAllTypes test_all_types = 536860000;
}


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

syntax = "proto3";

package google.protobuf;

import "google/protobuf/any.proto";
import "google/protobuf/source_context.proto";

option csharp_namespace = "Google.Protobuf.WellKnownTypes";
option cc_enable_arenas = true;
option java_package = "com.google.protobuf";
option java_outer_classname = "TypeProto";
option java_multiple_files = true;
option objc_class_prefix = "GPB";
option go_package = "google.golang.org/genproto/protobuf/ptype;ptype";

// A protocol buffer message type.
message Type {
  // The fully qualified message name.
  string name = 1;
  // The list of fields.
  repeated Field fields = 2;
  // The list of types appearing in `oneof` definitions in this type.
  repeated string oneofs = 3;
  // The protocol buffer options.
  repeated Option options = 4;
  // The source context.
  SourceContext source_context = 5;
  // The source syntax.
  Syntax syntax = 6;
}

// A single field of a message type.
message Field {
  // Basic field types.
  enum Kind {
    // Field type unknown.
    TYPE_UNKNOWN        = 0;
    // Field type double.
    TYPE_DOUBLE         = 1;
    // Field type float.
    TYPE_FLOAT          = 2;
    // Field type int64.
    TYPE_INT64          = 3;
    // Field type uint64.
    TYPE_UINT64         = 4;
    // Field type int32.
    TYPE_INT32          = 5;
    // Field type fixed64.
    TYPE_FIXED64        = 6;
    // Field type fixed32.
    TYPE_FIXED32        = 7;
    // Field type bool.
    TYPE_BOOL           = 8;
    // Field type string.
    TYPE_STRING         = 9;
    // Field type group. Proto2 syntax only, and deprecated.
    TYPE_GROUP          = 10;
    // Field type message.
    TYPE_MESSAGE        = 11;
    // Field type bytes.
    TYPE_BYTES          = 12;
    // Field type uint32.
    TYPE_UINT32         = 13;
    // Field type enum.
    TYPE_ENUM           = 14;
    // Field type sfixed32.
    TYPE_SFIXED32       = 15;
    // Field type sfixed64.
    TYPE_SFIXED64       = 16;
    // Field type sint32.
    TYPE_SINT32         = 17;
    // Field type sint64.
    TYPE_SINT64         = 18;
  };

  // Whether a field is optional, required, or repeated.
  enum Cardinality {
    // For fields with unknown cardinality.
    CARDINALITY_UNKNOWN = 0;
    // For optional fields.
    CARDINALITY_OPTIONAL = 1;
    // For required fields. Proto2 syntax only.
    CARDINALITY_REQUIRED = 2;
    // For repeated fields.
    CARDINALITY_REPEATED = 3;
  };

  // The field type.
  Kind kind = 1;
  // The field cardinality.
  Cardinality cardinality = 2;
  // The field number.
  int32 number = 3;
  // The field name.
  string name = 4;
  // The field type URL, without the scheme, for message or enumeration
  // types. Example: `"type.googleapis.com/google.protobuf.Timestamp"`.
  string type_url = 6;
  // The index of the field type in `Type.oneofs`, for message or enumeration
  // types. The first type has index 1; zero means the type is not in the list.
  int32 oneof_index = 7;
  // Whether to use alternative packed wire representation.
  bool packed = 8;
  // The protocol buffer options.
  repeated Option options = 9;
  // The field JSON name.
  string json_name = 10;
  // The string value of the default value of this field. Proto2 syntax only.
  string default_value = 11;
}

// Enum type definition.
message Enum {
  // Enum type name.
  string name = 1;
  // Enum value definitions.
  repeated EnumValue enumvalue = 2;
  // Protocol buffer options.
  repeated Option options = 3;
  // The source context.
  SourceContext source_context = 4;
  // The source syntax.
  Syntax syntax = 5;
}

// Enum value definition.
message EnumValue {
  // Enum value name.
  string name = 1;
  // Enum value number.
  int32 number = 2;
  // Protocol buffer options.
  repeated Option options = 3;
}

// A protocol buffer option, which can be attached to a message, field,
// enumeration, etc.
message Option {
  // The option's name. For protobuf built-in options (options defined in
  // descriptor.proto), this is the short name. For example, `"map_entry"`.
  // For custom options, it should be the fully-qualified name. For example,
  // `"google.api.http"`.
  string name = 1;
  // The option's value packed in an Any message. If the value is a primitive,
  // the corresponding wrapper type defined in google/protobuf/wrappers.proto
  // should be used. If the value is an enum, it should be stored as an int32
  // value using the google.protobuf.Int32Value type.
  Any value = 2;
}

// The syntax in which a protocol buffer element is defined.
enum Syntax {
  // Syntax `proto2`.
  SYNTAX_PROTO2 = 0;
  // Syntax `proto3`.
  SYNTAX_PROTO3 = 1;
}


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// This header is logically internal, but is made public because it is used
// from protocol-compiler-generated code, which may reside in other components.

#ifndef GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__
#define GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__

#include <string>
#include <vector>
#include <google/protobuf/stubs/casts.h>
#include <google/protobuf/stubs/common.h>
// TODO(jasonh): Remove this once the compiler change to directly include this
// is released to components.
#include <google/protobuf/generated_enum_reflection.h>
#include <google/protobuf/message.h>
#include <google/protobuf/metadata.h>
#include <google/protobuf/unknown_field_set.h>

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

namespace protobuf {
class DescriptorPool;
class MapKey;
class MapValueRef;
}  // namespace protobuf

namespace protobuf {
namespace flat {
class MetadataBuilder;
}  // namespace flat
}  // namespace protobuf

namespace protobuf {
namespace internal {
class DefaultEmptyOneof;

// Defined in this file.
class GeneratedMessageReflection;

// Defined in other files.
class ExtensionSet;             // extension_set.h
class WeakFieldMap;             // weak_field_map.h

// This struct describes the internal layout of the message, hence this is
// used to act on the message reflectively.
//   default_instance:  The default instance of the message.  This is only
//                  used to obtain pointers to default instances of embedded
//                  messages, which GetMessage() will return if the particular
//                  sub-message has not been initialized yet.  (Thus, all
//                  embedded message fields *must* have non-NULL pointers
//                  in the default instance.)
//   offsets:       An array of ints giving the byte offsets.
//                  For each oneof or weak field, the offset is relative to the
//                  default_instance. These can be computed at compile time
//                  using the
//                  GOOGLE_PROTOBUF_GENERATED_DEFAULT_ONEOF_FIELD_OFFSET()
//                  macro. For each none oneof field, the offset is related to
//                  the start of the message object.  These can be computed at
//                  compile time using the
//                  GOOGLE_PROTOBUF_GENERATED_MESSAGE_FIELD_OFFSET() macro.
//                  Besides offsets for all fields, this array also contains
//                  offsets for oneof unions. The offset of the i-th oneof union
//                  is offsets[descriptor->field_count() + i].
//   has_bit_indices:  Mapping from field indexes to their index in the has
//                  bit array.
//   has_bits_offset:  Offset in the message of an array of uint32s of size
//                  descriptor->field_count()/32, rounded up.  This is a
//                  bitfield where each bit indicates whether or not the
//                  corresponding field of the message has been initialized.
//                  The bit for field index i is obtained by the expression:
//                    has_bits[i / 32] & (1 << (i % 32))
//   unknown_fields_offset:  Offset in the message of the UnknownFieldSet for
//                  the message.
//   extensions_offset:  Offset in the message of the ExtensionSet for the
//                  message, or -1 if the message type has no extension
//                  ranges.
//   oneof_case_offset:  Offset in the message of an array of uint32s of
//                  size descriptor->oneof_decl_count().  Each uint32
//                  indicates what field is set for each oneof.
//   object_size:   The size of a message object of this type, as measured
//                  by sizeof().
//   arena_offset:  If a message doesn't have a unknown_field_set that stores
//                  the arena, it must have a direct pointer to the arena.
//   weak_field_map_offset: If the message proto has weak fields, this is the
//                  offset of _weak_field_map_ in the generated proto. Otherwise
//                  -1.
struct ReflectionSchema {
 public:
  // Size of a google::protobuf::Message object of this type.
  uint32 GetObjectSize() const { return static_cast<uint32>(object_size_); }

  // Offset of a non-oneof field.  Getting a field offset is slightly more
  // efficient when we know statically that it is not a oneof field.
  uint32 GetFieldOffsetNonOneof(const FieldDescriptor* field) const {
    GOOGLE_DCHECK(!field->containing_oneof());
    return offsets_[field->index()];
  }

  // Offset of any field.
  uint32 GetFieldOffset(const FieldDescriptor* field) const {
    if (field->containing_oneof()) {
      size_t offset =
          static_cast<size_t>(field->containing_type()->field_count() +
          field->containing_oneof()->index());
      return offsets_[offset];
    } else {
      return GetFieldOffsetNonOneof(field);
    }
  }

  uint32 GetOneofCaseOffset(const OneofDescriptor* oneof_descriptor) const {
    return static_cast<uint32>(oneof_case_offset_) +
           static_cast<uint32>(
               static_cast<size_t>(oneof_descriptor->index()) * sizeof(uint32));
  }

  bool HasHasbits() const { return has_bits_offset_ != -1; }

  // Bit index within the bit array of hasbits.  Bit order is low-to-high.
  uint32 HasBitIndex(const FieldDescriptor* field) const {
    GOOGLE_DCHECK(HasHasbits());
    return has_bit_indices_[field->index()];
  }

  // Byte offset of the hasbits array.
  uint32 HasBitsOffset() const {
    GOOGLE_DCHECK(HasHasbits());
    return static_cast<uint32>(has_bits_offset_);
  }

  // The offset of the InternalMetadataWithArena member.
  // For Lite this will actually be an InternalMetadataWithArenaLite.
  // The schema doesn't contain enough information to distinguish between
  // these two cases.
  uint32 GetMetadataOffset() const {
    return static_cast<uint32>(metadata_offset_);
  }

  // Whether this message has an ExtensionSet.
  bool HasExtensionSet() const { return extensions_offset_ != -1; }

  // The offset of the ExtensionSet in this message.
  uint32 GetExtensionSetOffset() const {
    GOOGLE_DCHECK(HasExtensionSet());
    return static_cast<uint32>(extensions_offset_);
  }

  // The off set of WeakFieldMap when the message contains weak fields.
  // The default is 0 for now.
  int GetWeakFieldMapOffset() const { return weak_field_map_offset_; }

  bool IsDefaultInstance(const Message& message) const {
    return &message == default_instance_;
  }

  // Returns a pointer to the default value for this field.  The size and type
  // of the underlying data depends on the field's type.
  const void *GetFieldDefault(const FieldDescriptor* field) const {
    return reinterpret_cast<const uint8*>(default_instance_) +
                     offsets_[field->index()];
  }

  bool HasWeakFields() const { return weak_field_map_offset_ > 0; }

  // These members are intended to be private, but we cannot actually make them
  // private because this prevents us from using aggregate initialization of
  // them, ie.
  //
  //   ReflectionSchema schema = {a, b, c, d, e, ...};
 // private:
  const Message* default_instance_;
  const uint32* offsets_;
  const uint32* has_bit_indices_;
  int has_bits_offset_;
  int metadata_offset_;
  int extensions_offset_;
  int oneof_case_offset_;
  int object_size_;
  int weak_field_map_offset_;
};

// Structs that the code generator emits directly to describe a message.
// These should never used directly except to build a ReflectionSchema
// object.
//
// EXPERIMENTAL: these are changing rapidly, and may completely disappear
// or merge with ReflectionSchema.
struct MigrationSchema {
  int32 offsets_index;
  int32 has_bit_indices_index;
  int object_size;
};

// THIS CLASS IS NOT INTENDED FOR DIRECT USE.  It is intended for use
// by generated code.  This class is just a big hack that reduces code
// size.
//
// A GeneratedMessageReflection is an implementation of Reflection
// which expects all fields to be backed by simple variables located in
// memory.  The locations are given using a base pointer and a set of
// offsets.
//
// It is required that the user represents fields of each type in a standard
// way, so that GeneratedMessageReflection can cast the void* pointer to
// the appropriate type.  For primitive fields and string fields, each field
// should be represented using the obvious C++ primitive type.  Enums and
// Messages are different:
//  - Singular Message fields are stored as a pointer to a Message.  These
//    should start out NULL, except for in the default instance where they
//    should start out pointing to other default instances.
//  - Enum fields are stored as an int.  This int must always contain
//    a valid value, such that EnumDescriptor::FindValueByNumber() would
//    not return NULL.
//  - Repeated fields are stored as RepeatedFields or RepeatedPtrFields
//    of whatever type the individual field would be.  Strings and
//    Messages use RepeatedPtrFields while everything else uses
//    RepeatedFields.
class LIBPROTOBUF_EXPORT GeneratedMessageReflection PROTOBUF_FINAL : public Reflection {
 public:
  // Constructs a GeneratedMessageReflection.
  // Parameters:
  //   descriptor:    The descriptor for the message type being implemented.
  //   schema:        The description of the internal guts of the message.
  //   pool:          DescriptorPool to search for extension definitions.  Only
  //                  used by FindKnownExtensionByName() and
  //                  FindKnownExtensionByNumber().
  //   factory:       MessageFactory to use to construct extension messages.
  GeneratedMessageReflection(const Descriptor* descriptor,
                             const ReflectionSchema& schema,
                             const DescriptorPool* pool,
                             MessageFactory* factory);

  ~GeneratedMessageReflection();

  // implements Reflection -------------------------------------------

  const UnknownFieldSet& GetUnknownFields(const Message& message) const;
  UnknownFieldSet* MutableUnknownFields(Message* message) const;

  size_t SpaceUsedLong(const Message& message) const;

  bool HasField(const Message& message, const FieldDescriptor* field) const;
  int FieldSize(const Message& message, const FieldDescriptor* field) const;
  void ClearField(Message* message, const FieldDescriptor* field) const;
  bool HasOneof(const Message& message,
                const OneofDescriptor* oneof_descriptor) const;
  void ClearOneof(Message* message, const OneofDescriptor* field) const;
  void RemoveLast(Message* message, const FieldDescriptor* field) const;
  Message* ReleaseLast(Message* message, const FieldDescriptor* field) const;
  void Swap(Message* message1, Message* message2) const;
  void SwapFields(Message* message1, Message* message2,
                  const std::vector<const FieldDescriptor*>& fields) const;
  void SwapElements(Message* message, const FieldDescriptor* field,
                    int index1, int index2) const;
  void ListFields(const Message& message,
                  std::vector<const FieldDescriptor*>* output) const;

  int32  GetInt32 (const Message& message,
                   const FieldDescriptor* field) const;
  int64  GetInt64 (const Message& message,
                   const FieldDescriptor* field) const;
  uint32 GetUInt32(const Message& message,
                   const FieldDescriptor* field) const;
  uint64 GetUInt64(const Message& message,
                   const FieldDescriptor* field) const;
  float  GetFloat (const Message& message,
                   const FieldDescriptor* field) const;
  double GetDouble(const Message& message,
                   const FieldDescriptor* field) const;
  bool   GetBool  (const Message& message,
                   const FieldDescriptor* field) const;
  string GetString(const Message& message,
                   const FieldDescriptor* field) const;
  const string& GetStringReference(const Message& message,
                                   const FieldDescriptor* field,
                                   string* scratch) const;
  const EnumValueDescriptor* GetEnum(const Message& message,
                                     const FieldDescriptor* field) const;
  int GetEnumValue(const Message& message,
                   const FieldDescriptor* field) const;
  const Message& GetMessage(const Message& message,
                            const FieldDescriptor* field,
                            MessageFactory* factory = NULL) const;

  const FieldDescriptor* GetOneofFieldDescriptor(
      const Message& message,
      const OneofDescriptor* oneof_descriptor) const;

 private:
  bool ContainsMapKey(const Message& message,
                      const FieldDescriptor* field,
                      const MapKey& key) const;
  bool InsertOrLookupMapValue(Message* message,
                              const FieldDescriptor* field,
                              const MapKey& key,
                              MapValueRef* val) const;
  bool DeleteMapValue(Message* message,
                      const FieldDescriptor* field,
                      const MapKey& key) const;
  MapIterator MapBegin(
      Message* message,
      const FieldDescriptor* field) const;
  MapIterator MapEnd(
      Message* message,
      const FieldDescriptor* field) const;
  int MapSize(const Message& message, const FieldDescriptor* field) const;

 public:
  void SetInt32 (Message* message,
                 const FieldDescriptor* field, int32  value) const;
  void SetInt64 (Message* message,
                 const FieldDescriptor* field, int64  value) const;
  void SetUInt32(Message* message,
                 const FieldDescriptor* field, uint32 value) const;
  void SetUInt64(Message* message,
                 const FieldDescriptor* field, uint64 value) const;
  void SetFloat (Message* message,
                 const FieldDescriptor* field, float  value) const;
  void SetDouble(Message* message,
                 const FieldDescriptor* field, double value) const;
  void SetBool  (Message* message,
                 const FieldDescriptor* field, bool   value) const;
  void SetString(Message* message,
                 const FieldDescriptor* field,
                 const string& value) const;
  void SetEnum  (Message* message, const FieldDescriptor* field,
                 const EnumValueDescriptor* value) const;
  void SetEnumValue(Message* message, const FieldDescriptor* field,
                    int value) const;
  Message* MutableMessage(Message* message, const FieldDescriptor* field,
                          MessageFactory* factory = NULL) const;
  void SetAllocatedMessage(Message* message,
                           Message* sub_message,
                           const FieldDescriptor* field) const;
  Message* ReleaseMessage(Message* message, const FieldDescriptor* field,
                          MessageFactory* factory = NULL) const;

  int32  GetRepeatedInt32 (const Message& message,
                           const FieldDescriptor* field, int index) const;
  int64  GetRepeatedInt64 (const Message& message,
                           const FieldDescriptor* field, int index) const;
  uint32 GetRepeatedUInt32(const Message& message,
                           const FieldDescriptor* field, int index) const;
  uint64 GetRepeatedUInt64(const Message& message,
                           const FieldDescriptor* field, int index) const;
  float  GetRepeatedFloat (const Message& message,
                           const FieldDescriptor* field, int index) const;
  double GetRepeatedDouble(const Message& message,
                           const FieldDescriptor* field, int index) const;
  bool   GetRepeatedBool  (const Message& message,
                           const FieldDescriptor* field, int index) const;
  string GetRepeatedString(const Message& message,
                           const FieldDescriptor* field, int index) const;
  const string& GetRepeatedStringReference(const Message& message,
                                           const FieldDescriptor* field,
                                           int index, string* scratch) const;
  const EnumValueDescriptor* GetRepeatedEnum(const Message& message,
                                             const FieldDescriptor* field,
                                             int index) const;
  int GetRepeatedEnumValue(const Message& message,
                           const FieldDescriptor* field,
                           int index) const;
  const Message& GetRepeatedMessage(const Message& message,
                                    const FieldDescriptor* field,
                                    int index) const;

  // Set the value of a field.
  void SetRepeatedInt32 (Message* message,
                         const FieldDescriptor* field, int index, int32  value) const;
  void SetRepeatedInt64 (Message* message,
                         const FieldDescriptor* field, int index, int64  value) const;
  void SetRepeatedUInt32(Message* message,
                         const FieldDescriptor* field, int index, uint32 value) const;
  void SetRepeatedUInt64(Message* message,
                         const FieldDescriptor* field, int index, uint64 value) const;
  void SetRepeatedFloat (Message* message,
                         const FieldDescriptor* field, int index, float  value) const;
  void SetRepeatedDouble(Message* message,
                         const FieldDescriptor* field, int index, double value) const;
  void SetRepeatedBool  (Message* message,
                         const FieldDescriptor* field, int index, bool   value) const;
  void SetRepeatedString(Message* message,
                         const FieldDescriptor* field, int index,
                         const string& value) const;
  void SetRepeatedEnum(Message* message, const FieldDescriptor* field,
                       int index, const EnumValueDescriptor* value) const;
  void SetRepeatedEnumValue(Message* message, const FieldDescriptor* field,
                            int index, int value) const;
  // Get a mutable pointer to a field with a message type.
  Message* MutableRepeatedMessage(Message* message,
                                  const FieldDescriptor* field,
                                  int index) const;

  void AddInt32 (Message* message,
                 const FieldDescriptor* field, int32  value) const;
  void AddInt64 (Message* message,
                 const FieldDescriptor* field, int64  value) const;
  void AddUInt32(Message* message,
                 const FieldDescriptor* field, uint32 value) const;
  void AddUInt64(Message* message,
                 const FieldDescriptor* field, uint64 value) const;
  void AddFloat (Message* message,
                 const FieldDescriptor* field, float  value) const;
  void AddDouble(Message* message,
                 const FieldDescriptor* field, double value) const;
  void AddBool  (Message* message,
                 const FieldDescriptor* field, bool   value) const;
  void AddString(Message* message,
                 const FieldDescriptor* field, const string& value) const;
  void AddEnum(Message* message,
               const FieldDescriptor* field,
               const EnumValueDescriptor* value) const;
  void AddEnumValue(Message* message,
                    const FieldDescriptor* field,
                    int value) const;
  Message* AddMessage(Message* message, const FieldDescriptor* field,
                      MessageFactory* factory = NULL) const;
  void AddAllocatedMessage(
      Message* message, const FieldDescriptor* field,
      Message* new_entry) const;

  const FieldDescriptor* FindKnownExtensionByName(const string& name) const;
  const FieldDescriptor* FindKnownExtensionByNumber(int number) const;

  bool SupportsUnknownEnumValues() const;

  // This value for arena_offset_ indicates that there is no arena pointer in
  // this message (e.g., old generated code).
  static const int kNoArenaPointer = -1;

  // This value for unknown_field_offset_ indicates that there is no
  // UnknownFieldSet in this message, and that instead, we are using the
  // Zero-Overhead Arena Pointer trick. When this is the case, arena_offset_
  // actually indexes to an InternalMetadataWithArena instance, which can return
  // either an arena pointer or an UnknownFieldSet or both. It is never the case
  // that unknown_field_offset_ == kUnknownFieldSetInMetadata && arena_offset_
  // == kNoArenaPointer.
  static const int kUnknownFieldSetInMetadata = -1;

 protected:
  void* MutableRawRepeatedField(
      Message* message, const FieldDescriptor* field, FieldDescriptor::CppType,
      int ctype, const Descriptor* desc) const;

  const void* GetRawRepeatedField(
      const Message& message, const FieldDescriptor* field,
      FieldDescriptor::CppType, int ctype,
      const Descriptor* desc) const;

  virtual MessageFactory* GetMessageFactory() const;

  virtual void* RepeatedFieldData(
      Message* message, const FieldDescriptor* field,
      FieldDescriptor::CppType cpp_type,
      const Descriptor* message_type) const;

 private:
  friend class google::protobuf::flat::MetadataBuilder;
  friend class upb::google_opensource::GMR_Handlers;

  const Descriptor* const descriptor_;
  const ReflectionSchema schema_;
  const DescriptorPool* const descriptor_pool_;
  MessageFactory* const message_factory_;

  // Last non weak field index. This is an optimization when most weak fields
  // are at the end of the containing message. If a message proto doesn't
  // contain weak fields, then this field equals descriptor_->field_count().
  int last_non_weak_field_index_;

  template <class T>
  const T& GetRawNonOneof(const Message& message,
                          const FieldDescriptor* field) const;
  template <class T>
  T* MutableRawNonOneof(Message* message, const FieldDescriptor* field) const;

  template <typename Type>
  const Type& GetRaw(const Message& message,
                            const FieldDescriptor* field) const;
  template <typename Type>
  inline Type* MutableRaw(Message* message,
                          const FieldDescriptor* field) const;
  template <typename Type>
  inline const Type& DefaultRaw(const FieldDescriptor* field) const;

  inline const uint32* GetHasBits(const Message& message) const;
  inline uint32* MutableHasBits(Message* message) const;
  inline uint32 GetOneofCase(
      const Message& message,
      const OneofDescriptor* oneof_descriptor) const;
  inline uint32* MutableOneofCase(
      Message* message,
      const OneofDescriptor* oneof_descriptor) const;
  inline const ExtensionSet& GetExtensionSet(const Message& message) const;
  inline ExtensionSet* MutableExtensionSet(Message* message) const;
  inline Arena* GetArena(Message* message) const;

  inline const InternalMetadataWithArena& GetInternalMetadataWithArena(
      const Message& message) const;

  inline InternalMetadataWithArena*
      MutableInternalMetadataWithArena(Message* message) const;

  inline bool HasBit(const Message& message,
                     const FieldDescriptor* field) const;
  inline void SetBit(Message* message,
                     const FieldDescriptor* field) const;
  inline void ClearBit(Message* message,
                       const FieldDescriptor* field) const;
  inline void SwapBit(Message* message1,
                      Message* message2,
                      const FieldDescriptor* field) const;

  // This function only swaps the field. Should swap corresponding has_bit
  // before or after using this function.
  void SwapField(Message* message1,
                 Message* message2,
                 const FieldDescriptor* field) const;

  void SwapOneofField(Message* message1,
                      Message* message2,
                      const OneofDescriptor* oneof_descriptor) const;

  inline bool HasOneofField(const Message& message,
                            const FieldDescriptor* field) const;
  inline void SetOneofCase(Message* message,
                           const FieldDescriptor* field) const;
  inline void ClearOneofField(Message* message,
                              const FieldDescriptor* field) const;

  template <typename Type>
  inline const Type& GetField(const Message& message,
                              const FieldDescriptor* field) const;
  template <typename Type>
  inline void SetField(Message* message,
                       const FieldDescriptor* field, const Type& value) const;
  template <typename Type>
  inline Type* MutableField(Message* message,
                            const FieldDescriptor* field) const;
  template <typename Type>
  inline const Type& GetRepeatedField(const Message& message,
                                      const FieldDescriptor* field,
                                      int index) const;
  template <typename Type>
  inline const Type& GetRepeatedPtrField(const Message& message,
                                         const FieldDescriptor* field,
                                         int index) const;
  template <typename Type>
  inline void SetRepeatedField(Message* message,
                               const FieldDescriptor* field, int index,
                               Type value) const;
  template <typename Type>
  inline Type* MutableRepeatedField(Message* message,
                                    const FieldDescriptor* field,
                                    int index) const;
  template <typename Type>
  inline void AddField(Message* message,
                       const FieldDescriptor* field, const Type& value) const;
  template <typename Type>
  inline Type* AddField(Message* message,
                        const FieldDescriptor* field) const;

  int GetExtensionNumberOrDie(const Descriptor* type) const;

  // Internal versions of EnumValue API perform no checking. Called after checks
  // by public methods.
  void SetEnumValueInternal(Message* message,
                            const FieldDescriptor* field,
                            int value) const;
  void SetRepeatedEnumValueInternal(Message* message,
                                    const FieldDescriptor* field,
                                    int index,
                                    int value) const;
  void AddEnumValueInternal(Message* message,
                            const FieldDescriptor* field,
                            int value) const;

  Message* UnsafeArenaReleaseMessage(Message* message,
                                     const FieldDescriptor* field,
                                     MessageFactory* factory = NULL) const;

  void UnsafeArenaSetAllocatedMessage(Message* message,
                                      Message* sub_message,
                                      const FieldDescriptor* field) const;

  internal::MapFieldBase* MapData(
      Message* message, const FieldDescriptor* field) const;

  friend inline  // inline so nobody can call this function.
      void
      RegisterAllTypesInternal(const Metadata* file_level_metadata, int size);
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratedMessageReflection);
};

// There are some places in proto2 where dynamic_cast would be useful as an
// optimization.  For example, take Message::MergeFrom(const Message& other).
// For a given generated message FooMessage, we generate these two methods:
//   void MergeFrom(const FooMessage& other);
//   void MergeFrom(const Message& other);
// The former method can be implemented directly in terms of FooMessage's
// inline accessors, but the latter method must work with the reflection
// interface.  However, if the parameter to the latter method is actually of
// type FooMessage, then we'd like to be able to just call the other method
// as an optimization.  So, we use dynamic_cast to check this.
//
// That said, dynamic_cast requires RTTI, which many people like to disable
// for performance and code size reasons.  When RTTI is not available, we
// still need to produce correct results.  So, in this case we have to fall
// back to using reflection, which is what we would have done anyway if the
// objects were not of the exact same class.
//
// dynamic_cast_if_available() implements this logic.  If RTTI is
// enabled, it does a dynamic_cast.  If RTTI is disabled, it just returns
// NULL.
//
// If you need to compile without RTTI, simply #define GOOGLE_PROTOBUF_NO_RTTI.
// On MSVC, this should be detected automatically.
template<typename To, typename From>
inline To dynamic_cast_if_available(From from) {
#if defined(GOOGLE_PROTOBUF_NO_RTTI) || (defined(_MSC_VER)&&!defined(_CPPRTTI))
  // Avoid the compiler warning about unused variables.
  (void)from;
  return NULL;
#else
  return dynamic_cast<To>(from);
#endif
}

// Tries to downcast this message to a generated message type.
// Returns NULL if this class is not an instance of T.
//
// This is like dynamic_cast_if_available, except it works even when
// dynamic_cast is not available by using Reflection.  However it only works
// with Message objects.
//
// TODO(haberman): can we remove dynamic_cast_if_available in favor of this?
template <typename T>
T* DynamicCastToGenerated(const Message* from) {
  // Compile-time assert that T is a generated type that has a
  // default_instance() accessor, but avoid actually calling it.
  const T&(*get_default_instance)() = &T::default_instance;
  (void)get_default_instance;

  // Compile-time assert that T is a subclass of google::protobuf::Message.
  const Message* unused = static_cast<T*>(NULL);
  (void)unused;

#if defined(GOOGLE_PROTOBUF_NO_RTTI) || \
  (defined(_MSC_VER) && !defined(_CPPRTTI))
  bool ok = &T::default_instance() ==
            from->GetReflection()->GetMessageFactory()->GetPrototype(
                from->GetDescriptor());
  return ok ? down_cast<T*>(from) : NULL;
#else
  return dynamic_cast<T*>(from);
#endif
}

template <typename T>
T* DynamicCastToGenerated(Message* from) {
  const Message* message_const = from;
  return const_cast<T*>(DynamicCastToGenerated<const T>(message_const));
}

LIBPROTOBUF_EXPORT void AssignDescriptors(
    const string& filename, const MigrationSchema* schemas,
    const Message* const* default_instances_, const uint32* offsets,
    MessageFactory* factory,
    // update the following descriptor arrays.
    Metadata* file_level_metadata,
    const EnumDescriptor** file_level_enum_descriptors,
    const ServiceDescriptor** file_level_service_descriptors);

LIBPROTOBUF_EXPORT void RegisterAllTypes(const Metadata* file_level_metadata, int size);

// These cannot be in lite so we put them in the reflection.
LIBPROTOBUF_EXPORT void UnknownFieldSetSerializer(const uint8* base, uint32 offset, uint32 tag,
                               uint32 has_offset,
                               ::google::protobuf::io::CodedOutputStream* output);

}  // namespace internal
}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_GENERATED_MESSAGE_REFLECTION_H__


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.

#ifndef GOOGLE_PROTOBUF_TEST_UTIL_LITE_H__
#define GOOGLE_PROTOBUF_TEST_UTIL_LITE_H__

#include <google/protobuf/unittest_lite.pb.h>

namespace google {
namespace protobuf {

namespace unittest = protobuf_unittest;
namespace unittest_import = protobuf_unittest_import;

class TestUtilLite {
 public:
  // Set every field in the message to a unique value.
  static void SetAllFields(unittest::TestAllTypesLite* message);
  static void SetAllExtensions(unittest::TestAllExtensionsLite* message);
  static void SetPackedFields(unittest::TestPackedTypesLite* message);
  static void SetPackedExtensions(unittest::TestPackedExtensionsLite* message);

  // Use the repeated versions of the set_*() accessors to modify all the
  // repeated fields of the message (which should already have been
  // initialized with Set*Fields()).  Set*Fields() itself only tests
  // the add_*() accessors.
  static void ModifyRepeatedFields(unittest::TestAllTypesLite* message);
  static void ModifyRepeatedExtensions(
      unittest::TestAllExtensionsLite* message);
  static void ModifyPackedFields(unittest::TestPackedTypesLite* message);
  static void ModifyPackedExtensions(
      unittest::TestPackedExtensionsLite* message);

  // Check that all fields have the values that they should have after
  // Set*Fields() is called.
  static void ExpectAllFieldsSet(const unittest::TestAllTypesLite& message);
  static void ExpectAllExtensionsSet(
      const unittest::TestAllExtensionsLite& message);
  static void ExpectPackedFieldsSet(
      const unittest::TestPackedTypesLite& message);
  static void ExpectPackedExtensionsSet(
      const unittest::TestPackedExtensionsLite& message);

  // Expect that the message is modified as would be expected from
  // Modify*Fields().
  static void ExpectRepeatedFieldsModified(
      const unittest::TestAllTypesLite& message);
  static void ExpectRepeatedExtensionsModified(
      const unittest::TestAllExtensionsLite& message);
  static void ExpectPackedFieldsModified(
      const unittest::TestPackedTypesLite& message);
  static void ExpectPackedExtensionsModified(
      const unittest::TestPackedExtensionsLite& message);

  // Check that all fields have their default values.
  static void ExpectClear(const unittest::TestAllTypesLite& message);
  static void ExpectExtensionsClear(
      const unittest::TestAllExtensionsLite& message);
  static void ExpectPackedClear(const unittest::TestPackedTypesLite& message);
  static void ExpectPackedExtensionsClear(
      const unittest::TestPackedExtensionsLite& message);

 private:
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(TestUtilLite);
};

}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_TEST_UTIL_LITE_H__


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#include <google/protobuf/arena_test_util.h>
#include <google/protobuf/map_lite_test_util.h>
#include <google/protobuf/testing/googletest.h>
#include <gtest/gtest.h>

namespace google {
namespace protobuf {
namespace {

class LiteArenaTest : public testing::Test {
 protected:
  LiteArenaTest() {
    ArenaOptions options;
    options.start_block_size = 128 * 1024;
    options.max_block_size = 128 * 1024;
    arena_.reset(new Arena(options));
    // Trigger the allocation of the first arena block, so that further use of
    // the arena will not require any heap allocations.
    google::protobuf::Arena::CreateArray<char>(arena_.get(), 1);
  }

  google::protobuf::scoped_ptr<Arena> arena_;
};

TEST_F(LiteArenaTest, MapNoHeapAllocation) {
  string data;
  data.reserve(128 * 1024);

  {
    // TODO(teboring): Enable no heap check when ArenaStringPtr is used in
    // Map.
    // google::protobuf::internal::NoHeapChecker no_heap;

    protobuf_unittest::TestArenaMapLite* from =
        Arena::CreateMessage<protobuf_unittest::TestArenaMapLite>(arena_.get());
    google::protobuf::MapLiteTestUtil::SetArenaMapFields(from);
    from->SerializeToString(&data);

    protobuf_unittest::TestArenaMapLite* to =
        Arena::CreateMessage<protobuf_unittest::TestArenaMapLite>(arena_.get());
    to->ParseFromString(data);
    google::protobuf::MapLiteTestUtil::ExpectArenaMapFieldsSet(*to);
  }
}

TEST_F(LiteArenaTest, UnknownFieldMemLeak) {
  protobuf_unittest::ForeignMessageArenaLite* message =
      google::protobuf::Arena::CreateMessage<protobuf_unittest::ForeignMessageArenaLite>(
          arena_.get());
  string data = "\012\000";
  int original_capacity = data.capacity();
  while (data.capacity() <= original_capacity) {
    data.append("a");
  }
  data[1] = data.size() - 2;
  message->ParseFromString(data);
}

}  // namespace
}  // namespace protobuf
}  // namespace google


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

#ifndef GOOGLE_PROTOBUF_ANY_H__
#define GOOGLE_PROTOBUF_ANY_H__

#include <string>

#include <google/protobuf/stubs/common.h>
#include <google/protobuf/descriptor.h>
#include <google/protobuf/message.h>
#include <google/protobuf/arenastring.h>

namespace google {
namespace protobuf {
namespace internal {

// Helper class used to implement google::protobuf::Any.
class LIBPROTOBUF_EXPORT AnyMetadata {
  typedef ArenaStringPtr UrlType;
  typedef ArenaStringPtr ValueType;
 public:
  // AnyMetadata does not take ownership of "type_url" and "value".
  AnyMetadata(UrlType* type_url, ValueType* value);

  // Packs a message using the default type URL prefix: "type.googleapis.com".
  // The resulted type URL will be "type.googleapis.com/<message_full_name>".
  void PackFrom(const Message& message);
  // Packs a message using the given type URL prefix. The type URL will be
  // constructed by concatenating the message type's full name to the prefix
  // with an optional "/" separator if the prefix doesn't already end up "/".
  // For example, both PackFrom(message, "type.googleapis.com") and
  // PackFrom(message, "type.googleapis.com/") yield the same result type
  // URL: "type.googleapis.com/<message_full_name>".
  void PackFrom(const Message& message, const string& type_url_prefix);

  // Unpacks the payload into the given message. Returns false if the message's
  // type doesn't match the type specified in the type URL (i.e., the full
  // name after the last "/" of the type URL doesn't match the message's actual
  // full name) or parsing the payload has failed.
  bool UnpackTo(Message* message) const;

  // Checks whether the type specified in the type URL matches the given type.
  // A type is consdiered matching if its full name matches the full name after
  // the last "/" in the type URL.
  template<typename T>
  bool Is() const {
    return InternalIs(T::default_instance().GetDescriptor());
  }

 private:
  bool InternalIs(const Descriptor* message) const;

  UrlType* type_url_;
  ValueType* value_;

  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(AnyMetadata);
};

extern const char kAnyFullTypeName[];          // "google.protobuf.Any".
extern const char kTypeGoogleApisComPrefix[];  // "type.googleapis.com/".
extern const char kTypeGoogleProdComPrefix[];  // "type.googleprod.com/".

// Get the proto type name from Any::type_url value. For example, passing
// "type.googleapis.com/rpc.QueryOrigin" will return "rpc.QueryOrigin" in
// *full_type_name. Returns false if the type_url does not have a "/"
// in the type url separating the full type name.
bool ParseAnyTypeUrl(const string& type_url, string* full_type_name);

// See if message is of type google.protobuf.Any, if so, return the descriptors
// for "type_url" and "value" fields.
bool GetAnyFieldDescriptors(const Message& message,
                            const FieldDescriptor** type_url_field,
                            const FieldDescriptor** value_field);

}  // namespace internal
}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_ANY_H__


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// This file is similar to unittest_mset_wire_format.proto, but does not
// have a TestMessageSet, so it can be downgraded to proto1.

syntax = "proto2";

import "google/protobuf/unittest_mset_wire_format.proto";

package protobuf_unittest;

option cc_enable_arenas = true;
option optimize_for = SPEED;

message TestMessageSetContainer {
  optional proto2_wireformat_unittest.TestMessageSet message_set = 1;
}

message TestMessageSetExtension1 {
  extend proto2_wireformat_unittest.TestMessageSet {
    optional TestMessageSetExtension1 message_set_extension = 1545008;
  }
  optional int32 i = 15;
}

message TestMessageSetExtension2 {
  extend proto2_wireformat_unittest.TestMessageSet {
    optional TestMessageSetExtension2 message_set_extension = 1547769;
  }
  optional string str = 25;
}

// This message was used to generate
// //net/proto2/python/internal/testdata/message_set_message, but is commented
// out since it must not actually exist in code, to simulate an "unknown"
// extension.
// message TestMessageSetUnknownExtension {
//   extend TestMessageSet {
//     optional TestMessageSetUnknownExtension message_set_extension = 56141421;
//   }
//   optional int64 a = 1;
// }

// MessageSet wire format is equivalent to this.
message RawMessageSet {
  repeated group Item = 1 {
    required int32 type_id = 2;
    required bytes message = 3;
  }
}


text:
// Protocol Buffers - Google's data interchange format
// Copyright 2008 Google Inc.  All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
//     * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

// Author: kenton@google.com (Kenton Varda)
//  Based on original Protocol Buffers design by
//  Sanjay Ghemawat, Jeff Dean, and others.
//
// This header is logically internal, but is made public because it is used
// from protocol-compiler-generated code, which may reside in other components.

#ifndef GOOGLE_PROTOBUF_REFLECTION_OPS_H__
#define GOOGLE_PROTOBUF_REFLECTION_OPS_H__

#include <google/protobuf/stubs/common.h>
#include <google/protobuf/message.h>

namespace google {
namespace protobuf {
namespace internal {

// Basic operations that can be performed using reflection.
// These can be used as a cheap way to implement the corresponding
// methods of the Message interface, though they are likely to be
// slower than implementations tailored for the specific message type.
//
// This class should stay limited to operations needed to implement
// the Message interface.
//
// This class is really a namespace that contains only static methods.
class LIBPROTOBUF_EXPORT ReflectionOps {
 public:
  static void Copy(const Message& from, Message* to);
  static void Merge(const Message& from, Message* to);
  static void Clear(Message* message);
  static bool IsInitialized(const Message& message);
  static void DiscardUnknownFields(Message* message);

  // Finds all unset required fields in the message and adds their full
  // paths (e.g. "foo.bar[5].baz") to *names.  "prefix" will be attached to
  // the front of each name.
  static void FindInitializationErrors(const Message& message,
                                       const string& prefix,
                                       std::vector<string>* errors);

 private:
  // All methods are static.  No need to construct.
  GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ReflectionOps);
};

}  // namespace internal
}  // namespace protobuf

}  // namespace google
#endif  // GOOGLE_PROTOBUF_REFLECTION_OPS_H__