|
| 1 | +// Protocol Buffers - Google's data interchange format |
| 2 | +// Copyright 2008 Google Inc. All rights reserved. |
| 3 | +// |
| 4 | +// Use of this source code is governed by a BSD-style |
| 5 | +// license that can be found in the LICENSE file or at |
| 6 | +// https://developers.google.com/open-source/licenses/bsd |
| 7 | + |
| 8 | +// Author: kenton@google.com (Kenton Varda) |
| 9 | +// Based on original Protocol Buffers design by |
| 10 | +// Sanjay Ghemawat, Jeff Dean, and others. |
| 11 | + |
| 12 | +#include "google/protobuf/compiler/java/internal_helpers.h" |
| 13 | + |
| 14 | +#include "absl/log/absl_log.h" |
| 15 | +#include "google/protobuf/compiler/java/helpers.h" |
| 16 | +#include "google/protobuf/compiler/java/name_resolver.h" |
| 17 | +#include "google/protobuf/descriptor.pb.h" |
| 18 | + |
| 19 | +// Must be last. |
| 20 | +#include "google/protobuf/port_def.inc" |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace protobuf { |
| 24 | +namespace compiler { |
| 25 | +namespace java { |
| 26 | +namespace { |
| 27 | + |
| 28 | +int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) { |
| 29 | + // j/c/g/protobuf/FieldType.java lists field types in a slightly different |
| 30 | + // order from FieldDescriptor::Type so we can't do a simple cast. |
| 31 | + // |
| 32 | + // TODO: Make j/c/g/protobuf/FieldType.java follow the same order. |
| 33 | + int result = field->type(); |
| 34 | + if (result == FieldDescriptor::TYPE_GROUP) { |
| 35 | + return 17; |
| 36 | + } else if (result < FieldDescriptor::TYPE_GROUP) { |
| 37 | + return result - 1; |
| 38 | + } else { |
| 39 | + return result - 2; |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +int GetExperimentalJavaFieldTypeForRepeated(const FieldDescriptor* field) { |
| 44 | + if (field->type() == FieldDescriptor::TYPE_GROUP) { |
| 45 | + return 49; |
| 46 | + } else { |
| 47 | + return GetExperimentalJavaFieldTypeForSingular(field) + 18; |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +int GetExperimentalJavaFieldTypeForPacked(const FieldDescriptor* field) { |
| 52 | + int result = field->type(); |
| 53 | + if (result < FieldDescriptor::TYPE_STRING) { |
| 54 | + return result + 34; |
| 55 | + } else if (result > FieldDescriptor::TYPE_BYTES) { |
| 56 | + return result + 30; |
| 57 | + } else { |
| 58 | + ABSL_LOG(FATAL) << field->full_name() << " can't be packed."; |
| 59 | + return 0; |
| 60 | + } |
| 61 | +} |
| 62 | +} // namespace |
| 63 | + |
| 64 | +int GetExperimentalJavaFieldType(const FieldDescriptor* field) { |
| 65 | + static const int kMapFieldType = 50; |
| 66 | + static const int kOneofFieldTypeOffset = 51; |
| 67 | + |
| 68 | + static const int kRequiredBit = 0x100; |
| 69 | + static const int kUtf8CheckBit = 0x200; |
| 70 | + static const int kCheckInitialized = 0x400; |
| 71 | + static const int kLegacyEnumIsClosedBit = 0x800; |
| 72 | + static const int kHasHasBit = 0x1000; |
| 73 | + int extra_bits = field->is_required() ? kRequiredBit : 0; |
| 74 | + if (field->type() == FieldDescriptor::TYPE_STRING && CheckUtf8(field)) { |
| 75 | + extra_bits |= kUtf8CheckBit; |
| 76 | + } |
| 77 | + if (field->is_required() || (GetJavaType(field) == JAVATYPE_MESSAGE && |
| 78 | + HasRequiredFields(field->message_type()))) { |
| 79 | + extra_bits |= kCheckInitialized; |
| 80 | + } |
| 81 | + if (HasHasbit(field)) { |
| 82 | + extra_bits |= kHasHasBit; |
| 83 | + } |
| 84 | + if (GetJavaType(field) == JAVATYPE_ENUM && !SupportUnknownEnumValue(field)) { |
| 85 | + extra_bits |= kLegacyEnumIsClosedBit; |
| 86 | + } |
| 87 | + |
| 88 | + if (field->is_map()) { |
| 89 | + if (!SupportUnknownEnumValue(MapValueField(field))) { |
| 90 | + const FieldDescriptor* value = field->message_type()->map_value(); |
| 91 | + if (GetJavaType(value) == JAVATYPE_ENUM) { |
| 92 | + extra_bits |= kLegacyEnumIsClosedBit; |
| 93 | + } |
| 94 | + } |
| 95 | + return kMapFieldType | extra_bits; |
| 96 | + } else if (field->is_packed()) { |
| 97 | + return GetExperimentalJavaFieldTypeForPacked(field) | extra_bits; |
| 98 | + } else if (field->is_repeated()) { |
| 99 | + return GetExperimentalJavaFieldTypeForRepeated(field) | extra_bits; |
| 100 | + } else if (IsRealOneof(field)) { |
| 101 | + return (GetExperimentalJavaFieldTypeForSingular(field) + |
| 102 | + kOneofFieldTypeOffset) | |
| 103 | + extra_bits; |
| 104 | + } else { |
| 105 | + return GetExperimentalJavaFieldTypeForSingular(field) | extra_bits; |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +} // namespace java |
| 110 | +} // namespace compiler |
| 111 | +} // namespace protobuf |
| 112 | +} // namespace google |
| 113 | + |
| 114 | +#include "google/protobuf/port_undef.inc" |
0 commit comments