• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/helpers.h"
13 
14 #include <algorithm>
15 #include <cstdint>
16 #include <limits>
17 #include <vector>
18 
19 #include "absl/container/flat_hash_set.h"
20 #include "absl/log/absl_check.h"
21 #include "absl/log/absl_log.h"
22 #include "absl/strings/ascii.h"
23 #include "absl/strings/escaping.h"
24 #include "absl/strings/str_cat.h"
25 #include "absl/strings/str_format.h"
26 #include "absl/strings/str_replace.h"
27 #include "absl/strings/str_split.h"
28 #include "absl/strings/string_view.h"
29 #include "absl/strings/substitute.h"
30 #include "google/protobuf/compiler/java/name_resolver.h"
31 #include "google/protobuf/compiler/versions.h"
32 #include "google/protobuf/descriptor.pb.h"
33 #include "google/protobuf/io/printer.h"
34 #include "google/protobuf/io/strtod.h"
35 #include "google/protobuf/wire_format.h"
36 
37 // Must be last.
38 #include "google/protobuf/port_def.inc"
39 
40 namespace google {
41 namespace protobuf {
42 namespace compiler {
43 namespace java {
44 
45 using ::google::protobuf::internal::WireFormat;
46 using ::google::protobuf::internal::WireFormatLite;
47 
48 const char kThickSeparator[] =
49     "// ===================================================================\n";
50 const char kThinSeparator[] =
51     "// -------------------------------------------------------------------\n";
52 
PrintGeneratedAnnotation(io::Printer * printer,char delimiter,absl::string_view annotation_file,Options options)53 void PrintGeneratedAnnotation(io::Printer* printer, char delimiter,
54                               absl::string_view annotation_file,
55                               Options options) {
56   if (annotation_file.empty()) {
57     return;
58   }
59   std::string ptemplate =
60       "@javax.annotation.Generated(value=\"protoc\", comments=\"annotations:";
61   ptemplate.push_back(delimiter);
62   ptemplate.append("annotation_file");
63   ptemplate.push_back(delimiter);
64   ptemplate.append("\")\n");
65   printer->Print(ptemplate.c_str(), "annotation_file", annotation_file);
66 }
67 
PrintEnumVerifierLogic(io::Printer * printer,const FieldDescriptor * descriptor,const absl::flat_hash_map<absl::string_view,std::string> & variables,absl::string_view var_name,absl::string_view terminating_string,bool enforce_lite)68 void PrintEnumVerifierLogic(
69     io::Printer* printer, const FieldDescriptor* descriptor,
70     const absl::flat_hash_map<absl::string_view, std::string>& variables,
71     absl::string_view var_name, absl::string_view terminating_string,
72     bool enforce_lite) {
73   std::string enum_verifier_string =
74       enforce_lite ? absl::StrCat(var_name, ".internalGetVerifier()")
75                    : absl::StrCat(
76                          "new com.google.protobuf.Internal.EnumVerifier() {\n"
77                          "        @java.lang.Override\n"
78                          "        public boolean isInRange(int number) {\n"
79                          "          return ",
80                          var_name,
81                          ".forNumber(number) != null;\n"
82                          "        }\n"
83                          "      }");
84   printer->Print(variables,
85                  absl::StrCat(enum_verifier_string, terminating_string));
86 }
87 
PrintGencodeVersionValidator(io::Printer * printer,bool oss_runtime,absl::string_view java_class_name)88 void PrintGencodeVersionValidator(io::Printer* printer, bool oss_runtime,
89                                   absl::string_view java_class_name) {
90   const auto& version = GetProtobufJavaVersion(oss_runtime);
91   printer->Print(
92       "com.google.protobuf.RuntimeVersion.validateProtobufGencodeVersion(\n"
93       "  com.google.protobuf.RuntimeVersion.RuntimeDomain.$domain$,\n"
94       "  $major$,\n"
95       "  $minor$,\n"
96       "  $patch$,\n"
97       "  $suffix$,\n"
98       "  $location$);\n",
99       "domain", oss_runtime ? "PUBLIC" : "GOOGLE_INTERNAL", "major",
100       absl::StrCat("/* major= */ ", version.major()), "minor",
101       absl::StrCat("/* minor= */ ", version.minor()), "patch",
102       absl::StrCat("/* patch= */ ", version.patch()), "suffix",
103       absl::StrCat("/* suffix= */ \"", version.suffix(), "\""), "location",
104       absl::StrCat(java_class_name, ".class.getName()"));
105 }
106 
UnderscoresToCamelCase(absl::string_view input,bool cap_next_letter)107 std::string UnderscoresToCamelCase(absl::string_view input,
108                                    bool cap_next_letter) {
109   ABSL_CHECK(!input.empty());
110   std::string result;
111   // Note:  I distrust ctype.h due to locales.
112   for (int i = 0; i < input.size(); i++) {
113     if ('a' <= input[i] && input[i] <= 'z') {
114       if (cap_next_letter) {
115         result += input[i] + ('A' - 'a');
116       } else {
117         result += input[i];
118       }
119       cap_next_letter = false;
120     } else if ('A' <= input[i] && input[i] <= 'Z') {
121       if (i == 0 && !cap_next_letter) {
122         // Force first letter to lower-case unless explicitly told to
123         // capitalize it.
124         result += input[i] + ('a' - 'A');
125       } else {
126         // Capital letters after the first are left as-is.
127         result += input[i];
128       }
129       cap_next_letter = false;
130     } else if ('0' <= input[i] && input[i] <= '9') {
131       result += input[i];
132       cap_next_letter = true;
133     } else {
134       cap_next_letter = true;
135     }
136   }
137   // Add a trailing "_" if the name should be altered.
138   if (input[input.size() - 1] == '#') {
139     result += '_';
140   }
141   return result;
142 }
143 
ToCamelCase(absl::string_view input,bool lower_first)144 std::string ToCamelCase(absl::string_view input, bool lower_first) {
145   bool capitalize_next = !lower_first;
146   std::string result;
147   result.reserve(input.size());
148 
149   for (char i : input) {
150     if (i == '_') {
151       capitalize_next = true;
152     } else if (capitalize_next) {
153       result.push_back(absl::ascii_toupper(i));
154       capitalize_next = false;
155     } else {
156       result.push_back(i);
157     }
158   }
159 
160   // Lower-case the first letter.
161   if (lower_first && !result.empty()) {
162     result[0] = absl::ascii_tolower(result[0]);
163   }
164 
165   return result;
166 }
167 
168 // Names that should be avoided as field names in Kotlin.
169 // All Kotlin hard keywords are in this list.
IsForbiddenKotlin(absl::string_view field_name)170 bool IsForbiddenKotlin(absl::string_view field_name) {
171   static const auto& kKotlinForbiddenNames =
172       *new absl::flat_hash_set<absl::string_view>({
173           "as",      "as?",       "break",  "class", "continue", "do",
174           "else",    "false",     "for",    "fun",   "if",       "in",
175           "!in",     "interface", "is",     "!is",   "null",     "object",
176           "package", "return",    "super",  "this",  "throw",    "true",
177           "try",     "typealias", "typeof", "val",   "var",      "when",
178           "while",
179       });
180 
181   return kKotlinForbiddenNames.contains(field_name);
182 }
183 
EscapeKotlinKeywords(std::string name)184 std::string EscapeKotlinKeywords(std::string name) {
185   std::vector<std::string> escaped_packages;
186   std::vector<std::string> packages = absl::StrSplit(name, ".");  // NOLINT
187   for (absl::string_view package : packages) {
188     if (IsForbiddenKotlin(package)) {
189       escaped_packages.push_back(absl::StrCat("`", package, "`"));
190     } else {
191       escaped_packages.emplace_back(package);
192     }
193   }
194   return absl::StrJoin(escaped_packages, ".");
195 }
196 
UniqueFileScopeIdentifier(const Descriptor * descriptor)197 std::string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
198   return absl::StrCat(
199       "static_", absl::StrReplaceAll(descriptor->full_name(), {{".", "_"}}));
200 }
201 
CamelCaseFieldName(const FieldDescriptor * field)202 std::string CamelCaseFieldName(const FieldDescriptor* field) {
203   std::string fieldName = UnderscoresToCamelCase(field);
204   if ('0' <= fieldName[0] && fieldName[0] <= '9') {
205     return absl::StrCat("_", fieldName);
206   }
207   return fieldName;
208 }
209 
FileClassName(const FileDescriptor * file,bool immutable)210 std::string FileClassName(const FileDescriptor* file, bool immutable) {
211   return ClassNameResolver().GetFileClassName(file, immutable);
212 }
213 
JavaPackageToDir(std::string package_name)214 std::string JavaPackageToDir(std::string package_name) {
215   std::string package_dir = absl::StrReplaceAll(package_name, {{".", "/"}});
216   if (!package_dir.empty()) absl::StrAppend(&package_dir, "/");
217   return package_dir;
218 }
219 
ExtraMessageInterfaces(const Descriptor * descriptor)220 std::string ExtraMessageInterfaces(const Descriptor* descriptor) {
221   return absl::StrCat("// @@protoc_insertion_point(message_implements:",
222                       descriptor->full_name(), ")");
223 }
224 
225 
ExtraBuilderInterfaces(const Descriptor * descriptor)226 std::string ExtraBuilderInterfaces(const Descriptor* descriptor) {
227   return absl::StrCat("// @@protoc_insertion_point(builder_implements:",
228                       descriptor->full_name(), ")");
229 }
230 
ExtraMessageOrBuilderInterfaces(const Descriptor * descriptor)231 std::string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) {
232   return absl::StrCat("// @@protoc_insertion_point(interface_extends:",
233                       descriptor->full_name(), ")");
234 }
235 
FieldConstantName(const FieldDescriptor * field)236 std::string FieldConstantName(const FieldDescriptor* field) {
237   std::string name = absl::StrCat(field->name(), "_FIELD_NUMBER");
238   absl::AsciiStrToUpper(&name);
239   return name;
240 }
241 
GetType(const FieldDescriptor * field)242 FieldDescriptor::Type GetType(const FieldDescriptor* field) {
243   return field->type();
244 }
245 
GetJavaType(const FieldDescriptor * field)246 JavaType GetJavaType(const FieldDescriptor* field) {
247   switch (GetType(field)) {
248     case FieldDescriptor::TYPE_INT32:
249     case FieldDescriptor::TYPE_UINT32:
250     case FieldDescriptor::TYPE_SINT32:
251     case FieldDescriptor::TYPE_FIXED32:
252     case FieldDescriptor::TYPE_SFIXED32:
253       return JAVATYPE_INT;
254 
255     case FieldDescriptor::TYPE_INT64:
256     case FieldDescriptor::TYPE_UINT64:
257     case FieldDescriptor::TYPE_SINT64:
258     case FieldDescriptor::TYPE_FIXED64:
259     case FieldDescriptor::TYPE_SFIXED64:
260       return JAVATYPE_LONG;
261 
262     case FieldDescriptor::TYPE_FLOAT:
263       return JAVATYPE_FLOAT;
264 
265     case FieldDescriptor::TYPE_DOUBLE:
266       return JAVATYPE_DOUBLE;
267 
268     case FieldDescriptor::TYPE_BOOL:
269       return JAVATYPE_BOOLEAN;
270 
271     case FieldDescriptor::TYPE_STRING:
272       return JAVATYPE_STRING;
273 
274     case FieldDescriptor::TYPE_BYTES:
275       return JAVATYPE_BYTES;
276 
277     case FieldDescriptor::TYPE_ENUM:
278       return JAVATYPE_ENUM;
279 
280     case FieldDescriptor::TYPE_GROUP:
281     case FieldDescriptor::TYPE_MESSAGE:
282       return JAVATYPE_MESSAGE;
283 
284       // No default because we want the compiler to complain if any new
285       // types are added.
286   }
287 
288   ABSL_LOG(FATAL) << "Can't get here.";
289   return JAVATYPE_INT;
290 }
291 
PrimitiveTypeName(JavaType type)292 absl::string_view PrimitiveTypeName(JavaType type) {
293   switch (type) {
294     case JAVATYPE_INT:
295       return "int";
296     case JAVATYPE_LONG:
297       return "long";
298     case JAVATYPE_FLOAT:
299       return "float";
300     case JAVATYPE_DOUBLE:
301       return "double";
302     case JAVATYPE_BOOLEAN:
303       return "boolean";
304     case JAVATYPE_STRING:
305       return "java.lang.String";
306     case JAVATYPE_BYTES:
307       return "com.google.protobuf.ByteString";
308     case JAVATYPE_ENUM:
309       return {};
310     case JAVATYPE_MESSAGE:
311       return {};
312 
313       // No default because we want the compiler to complain if any new
314       // JavaTypes are added.
315   }
316 
317   ABSL_LOG(FATAL) << "Can't get here.";
318   return {};
319 }
320 
PrimitiveTypeName(const FieldDescriptor * descriptor)321 absl::string_view PrimitiveTypeName(const FieldDescriptor* descriptor) {
322   return PrimitiveTypeName(GetJavaType(descriptor));
323 }
324 
BoxedPrimitiveTypeName(JavaType type)325 absl::string_view BoxedPrimitiveTypeName(JavaType type) {
326   switch (type) {
327     case JAVATYPE_INT:
328       return "java.lang.Integer";
329     case JAVATYPE_LONG:
330       return "java.lang.Long";
331     case JAVATYPE_FLOAT:
332       return "java.lang.Float";
333     case JAVATYPE_DOUBLE:
334       return "java.lang.Double";
335     case JAVATYPE_BOOLEAN:
336       return "java.lang.Boolean";
337     case JAVATYPE_STRING:
338       return "java.lang.String";
339     case JAVATYPE_BYTES:
340       return "com.google.protobuf.ByteString";
341     case JAVATYPE_ENUM:
342       return {};
343     case JAVATYPE_MESSAGE:
344       return {};
345 
346       // No default because we want the compiler to complain if any new
347       // JavaTypes are added.
348   }
349 
350   ABSL_LOG(FATAL) << "Can't get here.";
351   return {};
352 }
353 
BoxedPrimitiveTypeName(const FieldDescriptor * descriptor)354 absl::string_view BoxedPrimitiveTypeName(const FieldDescriptor* descriptor) {
355   return BoxedPrimitiveTypeName(GetJavaType(descriptor));
356 }
357 
KotlinTypeName(JavaType type)358 absl::string_view KotlinTypeName(JavaType type) {
359   switch (type) {
360     case JAVATYPE_INT:
361       return "kotlin.Int";
362     case JAVATYPE_LONG:
363       return "kotlin.Long";
364     case JAVATYPE_FLOAT:
365       return "kotlin.Float";
366     case JAVATYPE_DOUBLE:
367       return "kotlin.Double";
368     case JAVATYPE_BOOLEAN:
369       return "kotlin.Boolean";
370     case JAVATYPE_STRING:
371       return "kotlin.String";
372     case JAVATYPE_BYTES:
373       return "com.google.protobuf.ByteString";
374     case JAVATYPE_ENUM:
375       return {};
376     case JAVATYPE_MESSAGE:
377       return {};
378 
379       // No default because we want the compiler to complain if any new
380       // JavaTypes are added.
381   }
382 
383   ABSL_LOG(FATAL) << "Can't get here.";
384   return {};
385 }
386 
GetOneofStoredType(const FieldDescriptor * field)387 std::string GetOneofStoredType(const FieldDescriptor* field) {
388   const JavaType javaType = GetJavaType(field);
389   switch (javaType) {
390     case JAVATYPE_ENUM:
391       return "java.lang.Integer";
392     case JAVATYPE_MESSAGE:
393       return ClassNameResolver().GetClassName(field->message_type(), true);
394     default:
395       return std::string(BoxedPrimitiveTypeName(javaType));
396   }
397 }
398 
FieldTypeName(FieldDescriptor::Type field_type)399 absl::string_view FieldTypeName(FieldDescriptor::Type field_type) {
400   switch (field_type) {
401     case FieldDescriptor::TYPE_INT32:
402       return "INT32";
403     case FieldDescriptor::TYPE_UINT32:
404       return "UINT32";
405     case FieldDescriptor::TYPE_SINT32:
406       return "SINT32";
407     case FieldDescriptor::TYPE_FIXED32:
408       return "FIXED32";
409     case FieldDescriptor::TYPE_SFIXED32:
410       return "SFIXED32";
411     case FieldDescriptor::TYPE_INT64:
412       return "INT64";
413     case FieldDescriptor::TYPE_UINT64:
414       return "UINT64";
415     case FieldDescriptor::TYPE_SINT64:
416       return "SINT64";
417     case FieldDescriptor::TYPE_FIXED64:
418       return "FIXED64";
419     case FieldDescriptor::TYPE_SFIXED64:
420       return "SFIXED64";
421     case FieldDescriptor::TYPE_FLOAT:
422       return "FLOAT";
423     case FieldDescriptor::TYPE_DOUBLE:
424       return "DOUBLE";
425     case FieldDescriptor::TYPE_BOOL:
426       return "BOOL";
427     case FieldDescriptor::TYPE_STRING:
428       return "STRING";
429     case FieldDescriptor::TYPE_BYTES:
430       return "BYTES";
431     case FieldDescriptor::TYPE_ENUM:
432       return "ENUM";
433     case FieldDescriptor::TYPE_GROUP:
434       return "GROUP";
435     case FieldDescriptor::TYPE_MESSAGE:
436       return "MESSAGE";
437 
438       // No default because we want the compiler to complain if any new
439       // types are added.
440   }
441 
442   ABSL_LOG(FATAL) << "Can't get here.";
443   return {};
444 }
445 
AllAscii(absl::string_view text)446 bool AllAscii(absl::string_view text) {
447   for (int i = 0; i < text.size(); i++) {
448     if ((text[i] & 0x80) != 0) {
449       return false;
450     }
451   }
452   return true;
453 }
454 
DefaultValue(const FieldDescriptor * field,bool immutable,ClassNameResolver * name_resolver,Options options)455 std::string DefaultValue(const FieldDescriptor* field, bool immutable,
456                          ClassNameResolver* name_resolver, Options options) {
457   // Switch on CppType since we need to know which default_value_* method
458   // of FieldDescriptor to call.
459   switch (field->cpp_type()) {
460     case FieldDescriptor::CPPTYPE_INT32:
461       return absl::StrCat(field->default_value_int32());
462     case FieldDescriptor::CPPTYPE_UINT32:
463       // Need to print as a signed int since Java has no unsigned.
464       return absl::StrCat(static_cast<int32_t>(field->default_value_uint32()));
465     case FieldDescriptor::CPPTYPE_INT64:
466       return absl::StrCat(field->default_value_int64(), "L");
467     case FieldDescriptor::CPPTYPE_UINT64:
468       return absl::StrCat(static_cast<int64_t>(field->default_value_uint64())) +
469              "L";
470     case FieldDescriptor::CPPTYPE_DOUBLE: {
471       double value = field->default_value_double();
472       if (value == std::numeric_limits<double>::infinity()) {
473         return "Double.POSITIVE_INFINITY";
474       } else if (value == -std::numeric_limits<double>::infinity()) {
475         return "Double.NEGATIVE_INFINITY";
476       } else if (value != value) {
477         return "Double.NaN";
478       } else {
479         return absl::StrCat(io::SimpleDtoa(value), "D");
480       }
481     }
482     case FieldDescriptor::CPPTYPE_FLOAT: {
483       float value = field->default_value_float();
484       if (value == std::numeric_limits<float>::infinity()) {
485         return "Float.POSITIVE_INFINITY";
486       } else if (value == -std::numeric_limits<float>::infinity()) {
487         return "Float.NEGATIVE_INFINITY";
488       } else if (value != value) {
489         return "Float.NaN";
490       } else {
491         return absl::StrCat(io::SimpleFtoa(value), "F");
492       }
493     }
494     case FieldDescriptor::CPPTYPE_BOOL:
495       return field->default_value_bool() ? "true" : "false";
496     case FieldDescriptor::CPPTYPE_STRING:
497       if (GetType(field) == FieldDescriptor::TYPE_BYTES) {
498         if (field->has_default_value()) {
499           // See comments in Internal.java for gory details.
500           return absl::Substitute(
501               "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")",
502               absl::CEscape(field->default_value_string()));
503         } else {
504           return "com.google.protobuf.ByteString.EMPTY";
505         }
506       } else {
507         if (AllAscii(field->default_value_string())) {
508           // All chars are ASCII.  In this case CEscape() works fine.
509           return absl::StrCat(
510               "\"", absl::CEscape(field->default_value_string()), "\"");
511         } else {
512           // See comments in Internal.java for gory details.
513           return absl::Substitute(
514               "com.google.protobuf.Internal.stringDefaultValue(\"$0\")",
515               absl::CEscape(field->default_value_string()));
516         }
517       }
518 
519     case FieldDescriptor::CPPTYPE_ENUM:
520       return absl::StrCat(
521           name_resolver->GetClassName(field->enum_type(), immutable), ".",
522           field->default_value_enum()->name());
523 
524     case FieldDescriptor::CPPTYPE_MESSAGE:
525       return absl::StrCat(
526           name_resolver->GetClassName(field->message_type(), immutable),
527           ".getDefaultInstance()");
528 
529       // No default because we want the compiler to complain if any new
530       // types are added.
531   }
532 
533   ABSL_LOG(FATAL) << "Can't get here.";
534   return "";
535 }
536 
IsDefaultValueJavaDefault(const FieldDescriptor * field)537 bool IsDefaultValueJavaDefault(const FieldDescriptor* field) {
538   // Switch on CppType since we need to know which default_value_* method
539   // of FieldDescriptor to call.
540   switch (field->cpp_type()) {
541     case FieldDescriptor::CPPTYPE_INT32:
542       return field->default_value_int32() == 0;
543     case FieldDescriptor::CPPTYPE_UINT32:
544       return field->default_value_uint32() == 0;
545     case FieldDescriptor::CPPTYPE_INT64:
546       return field->default_value_int64() == 0L;
547     case FieldDescriptor::CPPTYPE_UINT64:
548       return field->default_value_uint64() == 0L;
549     case FieldDescriptor::CPPTYPE_DOUBLE:
550       return field->default_value_double() == 0.0;
551     case FieldDescriptor::CPPTYPE_FLOAT:
552       return field->default_value_float() == 0.0;
553     case FieldDescriptor::CPPTYPE_BOOL:
554       return field->default_value_bool() == false;
555     case FieldDescriptor::CPPTYPE_ENUM:
556       return field->default_value_enum()->number() == 0;
557     case FieldDescriptor::CPPTYPE_STRING:
558     case FieldDescriptor::CPPTYPE_MESSAGE:
559       return false;
560 
561       // No default because we want the compiler to complain if any new
562       // types are added.
563   }
564 
565   ABSL_LOG(FATAL) << "Can't get here.";
566   return false;
567 }
568 
IsByteStringWithCustomDefaultValue(const FieldDescriptor * field)569 bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) {
570   return GetJavaType(field) == JAVATYPE_BYTES &&
571          field->default_value_string() != "";
572 }
573 
574 constexpr absl::string_view bit_masks[] = {
575     "0x00000001", "0x00000002", "0x00000004", "0x00000008",
576     "0x00000010", "0x00000020", "0x00000040", "0x00000080",
577 
578     "0x00000100", "0x00000200", "0x00000400", "0x00000800",
579     "0x00001000", "0x00002000", "0x00004000", "0x00008000",
580 
581     "0x00010000", "0x00020000", "0x00040000", "0x00080000",
582     "0x00100000", "0x00200000", "0x00400000", "0x00800000",
583 
584     "0x01000000", "0x02000000", "0x04000000", "0x08000000",
585     "0x10000000", "0x20000000", "0x40000000", "0x80000000",
586 };
587 
GetBitFieldName(int index)588 std::string GetBitFieldName(int index) {
589   return absl::StrCat("bitField", index, "_");
590 }
591 
GetBitFieldNameForBit(int bitIndex)592 std::string GetBitFieldNameForBit(int bitIndex) {
593   return GetBitFieldName(bitIndex / 32);
594 }
595 
596 namespace {
597 
GenerateGetBitInternal(absl::string_view prefix,int bitIndex)598 std::string GenerateGetBitInternal(absl::string_view prefix, int bitIndex) {
599   std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
600   int bitInVarIndex = bitIndex % 32;
601 
602   return absl::StrCat("((", varName, " & ", bit_masks[bitInVarIndex],
603                       ") != 0)");
604 }
605 
GenerateSetBitInternal(absl::string_view prefix,int bitIndex)606 std::string GenerateSetBitInternal(absl::string_view prefix, int bitIndex) {
607   std::string varName = absl::StrCat(prefix, GetBitFieldNameForBit(bitIndex));
608   int bitInVarIndex = bitIndex % 32;
609 
610   return absl::StrCat(varName, " |= ", bit_masks[bitInVarIndex]);
611 }
612 
613 }  // namespace
614 
GenerateGetBit(int bitIndex)615 std::string GenerateGetBit(int bitIndex) {
616   return GenerateGetBitInternal("", bitIndex);
617 }
618 
GenerateSetBit(int bitIndex)619 std::string GenerateSetBit(int bitIndex) {
620   return GenerateSetBitInternal("", bitIndex);
621 }
622 
GenerateClearBit(int bitIndex)623 std::string GenerateClearBit(int bitIndex) {
624   std::string varName = GetBitFieldNameForBit(bitIndex);
625   int bitInVarIndex = bitIndex % 32;
626 
627   return absl::StrCat(varName, " = (", varName, " & ~",
628                       bit_masks[bitInVarIndex], ")");
629 }
630 
GenerateGetBitFromLocal(int bitIndex)631 std::string GenerateGetBitFromLocal(int bitIndex) {
632   return GenerateGetBitInternal("from_", bitIndex);
633 }
634 
GenerateSetBitToLocal(int bitIndex)635 std::string GenerateSetBitToLocal(int bitIndex) {
636   return GenerateSetBitInternal("to_", bitIndex);
637 }
638 
GenerateGetBitMutableLocal(int bitIndex)639 std::string GenerateGetBitMutableLocal(int bitIndex) {
640   return GenerateGetBitInternal("mutable_", bitIndex);
641 }
642 
GenerateSetBitMutableLocal(int bitIndex)643 std::string GenerateSetBitMutableLocal(int bitIndex) {
644   return GenerateSetBitInternal("mutable_", bitIndex);
645 }
646 
IsReferenceType(JavaType type)647 bool IsReferenceType(JavaType type) {
648   switch (type) {
649     case JAVATYPE_INT:
650       return false;
651     case JAVATYPE_LONG:
652       return false;
653     case JAVATYPE_FLOAT:
654       return false;
655     case JAVATYPE_DOUBLE:
656       return false;
657     case JAVATYPE_BOOLEAN:
658       return false;
659     case JAVATYPE_STRING:
660       return true;
661     case JAVATYPE_BYTES:
662       return true;
663     case JAVATYPE_ENUM:
664       return true;
665     case JAVATYPE_MESSAGE:
666       return true;
667 
668       // No default because we want the compiler to complain if any new
669       // JavaTypes are added.
670   }
671 
672   ABSL_LOG(FATAL) << "Can't get here.";
673   return false;
674 }
675 
GetCapitalizedType(const FieldDescriptor * field,bool immutable,Options options)676 absl::string_view GetCapitalizedType(const FieldDescriptor* field,
677                                      bool immutable, Options options) {
678   switch (GetType(field)) {
679     case FieldDescriptor::TYPE_INT32:
680       return "Int32";
681     case FieldDescriptor::TYPE_UINT32:
682       return "UInt32";
683     case FieldDescriptor::TYPE_SINT32:
684       return "SInt32";
685     case FieldDescriptor::TYPE_FIXED32:
686       return "Fixed32";
687     case FieldDescriptor::TYPE_SFIXED32:
688       return "SFixed32";
689     case FieldDescriptor::TYPE_INT64:
690       return "Int64";
691     case FieldDescriptor::TYPE_UINT64:
692       return "UInt64";
693     case FieldDescriptor::TYPE_SINT64:
694       return "SInt64";
695     case FieldDescriptor::TYPE_FIXED64:
696       return "Fixed64";
697     case FieldDescriptor::TYPE_SFIXED64:
698       return "SFixed64";
699     case FieldDescriptor::TYPE_FLOAT:
700       return "Float";
701     case FieldDescriptor::TYPE_DOUBLE:
702       return "Double";
703     case FieldDescriptor::TYPE_BOOL:
704       return "Bool";
705     case FieldDescriptor::TYPE_STRING:
706       return "String";
707     case FieldDescriptor::TYPE_BYTES: {
708       return "Bytes";
709     }
710     case FieldDescriptor::TYPE_ENUM:
711       return "Enum";
712     case FieldDescriptor::TYPE_GROUP:
713       return "Group";
714     case FieldDescriptor::TYPE_MESSAGE:
715       return "Message";
716 
717       // No default because we want the compiler to complain if any new
718       // types are added.
719   }
720 
721   ABSL_LOG(FATAL) << "Can't get here.";
722   return {};
723 }
724 
725 // For encodings with fixed sizes, returns that size in bytes.  Otherwise
726 // returns -1.
FixedSize(FieldDescriptor::Type type)727 int FixedSize(FieldDescriptor::Type type) {
728   switch (type) {
729     case FieldDescriptor::TYPE_INT32:
730       return -1;
731     case FieldDescriptor::TYPE_INT64:
732       return -1;
733     case FieldDescriptor::TYPE_UINT32:
734       return -1;
735     case FieldDescriptor::TYPE_UINT64:
736       return -1;
737     case FieldDescriptor::TYPE_SINT32:
738       return -1;
739     case FieldDescriptor::TYPE_SINT64:
740       return -1;
741     case FieldDescriptor::TYPE_FIXED32:
742       return WireFormatLite::kFixed32Size;
743     case FieldDescriptor::TYPE_FIXED64:
744       return WireFormatLite::kFixed64Size;
745     case FieldDescriptor::TYPE_SFIXED32:
746       return WireFormatLite::kSFixed32Size;
747     case FieldDescriptor::TYPE_SFIXED64:
748       return WireFormatLite::kSFixed64Size;
749     case FieldDescriptor::TYPE_FLOAT:
750       return WireFormatLite::kFloatSize;
751     case FieldDescriptor::TYPE_DOUBLE:
752       return WireFormatLite::kDoubleSize;
753 
754     case FieldDescriptor::TYPE_BOOL:
755       return WireFormatLite::kBoolSize;
756     case FieldDescriptor::TYPE_ENUM:
757       return -1;
758 
759     case FieldDescriptor::TYPE_STRING:
760       return -1;
761     case FieldDescriptor::TYPE_BYTES:
762       return -1;
763     case FieldDescriptor::TYPE_GROUP:
764       return -1;
765     case FieldDescriptor::TYPE_MESSAGE:
766       return -1;
767 
768       // No default because we want the compiler to complain if any new
769       // types are added.
770   }
771   ABSL_LOG(FATAL) << "Can't get here.";
772   return -1;
773 }
774 
775 // Sort the fields of the given Descriptor by number into a new[]'d array
776 // and return it. The caller should delete the returned array.
SortFieldsByNumber(const Descriptor * descriptor)777 const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
778   const FieldDescriptor** fields =
779       new const FieldDescriptor*[descriptor->field_count()];
780   for (int i = 0; i < descriptor->field_count(); i++) {
781     fields[i] = descriptor->field(i);
782   }
783   std::sort(fields, fields + descriptor->field_count(),
784             FieldOrderingByNumber());
785   return fields;
786 }
787 
788 // Returns true if the message type has any required fields.  If it doesn't,
789 // we can optimize out calls to its isInitialized() method.
790 //
791 // already_seen is used to avoid checking the same type multiple times
792 // (and also to protect against recursion).
HasRequiredFields(const Descriptor * type,absl::flat_hash_set<const Descriptor * > * already_seen)793 bool HasRequiredFields(const Descriptor* type,
794                        absl::flat_hash_set<const Descriptor*>* already_seen) {
795   if (already_seen->count(type) > 0) {
796     // The type is already in cache.  This means that either:
797     // a. The type has no required fields.
798     // b. We are in the midst of checking if the type has required fields,
799     //    somewhere up the stack.  In this case, we know that if the type
800     //    has any required fields, they'll be found when we return to it,
801     //    and the whole call to HasRequiredFields() will return true.
802     //    Therefore, we don't have to check if this type has required fields
803     //    here.
804     return false;
805   }
806   already_seen->insert(type);
807 
808   // If the type has extensions, an extension with message type could contain
809   // required fields, so we have to be conservative and assume such an
810   // extension exists.
811   if (type->extension_range_count() > 0) return true;
812 
813   for (int i = 0; i < type->field_count(); i++) {
814     const FieldDescriptor* field = type->field(i);
815     if (field->is_required()) {
816       return true;
817     }
818     if (GetJavaType(field) == JAVATYPE_MESSAGE) {
819       if (HasRequiredFields(field->message_type(), already_seen)) {
820         return true;
821       }
822     }
823   }
824 
825   return false;
826 }
827 
HasRequiredFields(const Descriptor * type)828 bool HasRequiredFields(const Descriptor* type) {
829   absl::flat_hash_set<const Descriptor*> already_seen;
830   return HasRequiredFields(type, &already_seen);
831 }
832 
IsRealOneof(const FieldDescriptor * descriptor)833 bool IsRealOneof(const FieldDescriptor* descriptor) {
834   return descriptor->real_containing_oneof();
835 }
836 
HasRepeatedFields(const Descriptor * descriptor)837 bool HasRepeatedFields(const Descriptor* descriptor) {
838   for (int i = 0; i < descriptor->field_count(); ++i) {
839     const FieldDescriptor* field = descriptor->field(i);
840     if (field->is_repeated()) {
841       return true;
842     }
843   }
844   return false;
845 }
846 
847 // Encode an unsigned 32-bit value into a sequence of UTF-16 characters.
848 //
849 // If the value is in [0x0000, 0xD7FF], we encode it with a single character
850 // with the same numeric value.
851 //
852 // If the value is larger than 0xD7FF, we encode its lowest 13 bits into a
853 // character in the range [0xE000, 0xFFFF] by combining these 13 bits with
854 // 0xE000 using logic-or. Then we shift the value to the right by 13 bits, and
855 // encode the remaining value by repeating this same process until we get to
856 // a value in [0x0000, 0xD7FF] where we will encode it using a character with
857 // the same numeric value.
858 //
859 // Note that we only use code points in [0x0000, 0xD7FF] and [0xE000, 0xFFFF].
860 // There will be no surrogate pairs in the encoded character sequence.
WriteUInt32ToUtf16CharSequence(uint32_t number,std::vector<uint16_t> * output)861 void WriteUInt32ToUtf16CharSequence(uint32_t number,
862                                     std::vector<uint16_t>* output) {
863   // For values in [0x0000, 0xD7FF], only use one char to encode it.
864   if (number < 0xD800) {
865     output->push_back(static_cast<uint16_t>(number));
866     return;
867   }
868   // Encode into multiple chars. All except the last char will be in the range
869   // [0xE000, 0xFFFF], and the last char will be in the range [0x0000, 0xD7FF].
870   // Note that we don't use any value in range [0xD800, 0xDFFF] because they
871   // have to come in pairs and the encoding is just more space-efficient w/o
872   // them.
873   while (number >= 0xD800) {
874     // [0xE000, 0xFFFF] can represent 13 bits of info.
875     output->push_back(static_cast<uint16_t>(0xE000 | (number & 0x1FFF)));
876     number >>= 13;
877   }
878   output->push_back(static_cast<uint16_t>(number));
879 }
880 
881 // Escape a UTF-16 character to be embedded in a Java string.
EscapeUtf16ToString(uint16_t code,std::string * output)882 void EscapeUtf16ToString(uint16_t code, std::string* output) {
883   if (code == '\t') {
884     output->append("\\t");
885   } else if (code == '\b') {
886     output->append("\\b");
887   } else if (code == '\n') {
888     output->append("\\n");
889   } else if (code == '\r') {
890     output->append("\\r");
891   } else if (code == '\f') {
892     output->append("\\f");
893   } else if (code == '\'') {
894     output->append("\\'");
895   } else if (code == '\"') {
896     output->append("\\\"");
897   } else if (code == '\\') {
898     output->append("\\\\");
899   } else if (code >= 0x20 && code <= 0x7f) {
900     output->push_back(static_cast<char>(code));
901   } else {
902     output->append(absl::StrFormat("\\u%04x", code));
903   }
904 }
905 
MapKeyField(const FieldDescriptor * descriptor)906 const FieldDescriptor* MapKeyField(const FieldDescriptor* descriptor) {
907   ABSL_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
908   const Descriptor* message = descriptor->message_type();
909   ABSL_CHECK(message->options().map_entry());
910   return message->map_key();
911 }
912 
MapValueField(const FieldDescriptor * descriptor)913 const FieldDescriptor* MapValueField(const FieldDescriptor* descriptor) {
914   ABSL_CHECK_EQ(FieldDescriptor::TYPE_MESSAGE, descriptor->type());
915   const Descriptor* message = descriptor->message_type();
916   ABSL_CHECK(message->options().map_entry());
917   return message->map_value();
918 }
919 
920 
921 }  // namespace java
922 }  // namespace compiler
923 }  // namespace protobuf
924 }  // namespace google
925 
926 #include "google/protobuf/port_undef.inc"
927