java_helpers.cc 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. #include <algorithm>
  34. #include <google/protobuf/stubs/hash.h>
  35. #include <limits>
  36. #include <vector>
  37. #include <google/protobuf/stubs/stringprintf.h>
  38. #include <google/protobuf/compiler/java/java_helpers.h>
  39. #include <google/protobuf/compiler/java/java_name_resolver.h>
  40. #include <google/protobuf/descriptor.pb.h>
  41. #include <google/protobuf/wire_format.h>
  42. #include <google/protobuf/stubs/strutil.h>
  43. #include <google/protobuf/stubs/substitute.h>
  44. #include <google/protobuf/stubs/hash.h> // for hash<T *>
  45. namespace google {
  46. namespace protobuf {
  47. namespace compiler {
  48. namespace java {
  49. using internal::WireFormat;
  50. using internal::WireFormatLite;
  51. const char kThickSeparator[] =
  52. "// ===================================================================\n";
  53. const char kThinSeparator[] =
  54. "// -------------------------------------------------------------------\n";
  55. namespace {
  56. const char* kDefaultPackage = "";
  57. // Names that should be avoided as field names.
  58. // Using them will cause the compiler to generate accessors whose names are
  59. // colliding with methods defined in base classes.
  60. const char* kForbiddenWordList[] = {
  61. // message base class:
  62. "cached_size", "serialized_size",
  63. // java.lang.Object:
  64. "class",
  65. };
  66. const int kDefaultLookUpStartFieldNumber = 40;
  67. bool IsForbidden(const string& field_name) {
  68. for (int i = 0; i < GOOGLE_ARRAYSIZE(kForbiddenWordList); ++i) {
  69. if (field_name == kForbiddenWordList[i]) {
  70. return true;
  71. }
  72. }
  73. return false;
  74. }
  75. string FieldName(const FieldDescriptor* field) {
  76. string field_name;
  77. // Groups are hacky: The name of the field is just the lower-cased name
  78. // of the group type. In Java, though, we would like to retain the original
  79. // capitalization of the type name.
  80. if (GetType(field) == FieldDescriptor::TYPE_GROUP) {
  81. field_name = field->message_type()->name();
  82. } else {
  83. field_name = field->name();
  84. }
  85. if (IsForbidden(field_name)) {
  86. // Append a trailing "#" to indicate that the name should be decorated to
  87. // avoid collision with other names.
  88. field_name += "#";
  89. }
  90. return field_name;
  91. }
  92. // Judge whether should use table or use look up.
  93. // Copied from com.google.protobuf.SchemaUtil.shouldUseTableSwitch
  94. bool ShouldUseTable(int lo, int hi, int number_of_fields) {
  95. if (hi < kDefaultLookUpStartFieldNumber) {
  96. return true;
  97. }
  98. int64 table_space_cost = (static_cast<int64>(hi) - lo + 1); // words
  99. int64 table_time_cost = 3; // comparisons
  100. int64 lookup_space_cost = 3 + 2 * static_cast<int64>(number_of_fields);
  101. int64 lookup_time_cost = 3 + number_of_fields;
  102. return table_space_cost + 3 * table_time_cost <=
  103. lookup_space_cost + 3 * lookup_time_cost;
  104. }
  105. } // namespace
  106. void PrintGeneratedAnnotation(io::Printer* printer, char delimiter,
  107. const string& annotation_file) {
  108. if (annotation_file.empty()) {
  109. return;
  110. }
  111. string ptemplate =
  112. "@javax.annotation.Generated(value=\"protoc\", comments=\"annotations:";
  113. ptemplate.push_back(delimiter);
  114. ptemplate.append("annotation_file");
  115. ptemplate.push_back(delimiter);
  116. ptemplate.append("\")\n");
  117. printer->Print(ptemplate.c_str(), "annotation_file", annotation_file);
  118. }
  119. string UnderscoresToCamelCase(const string& input, bool cap_next_letter) {
  120. string result;
  121. // Note: I distrust ctype.h due to locales.
  122. for (int i = 0; i < input.size(); i++) {
  123. if ('a' <= input[i] && input[i] <= 'z') {
  124. if (cap_next_letter) {
  125. result += input[i] + ('A' - 'a');
  126. } else {
  127. result += input[i];
  128. }
  129. cap_next_letter = false;
  130. } else if ('A' <= input[i] && input[i] <= 'Z') {
  131. if (i == 0 && !cap_next_letter) {
  132. // Force first letter to lower-case unless explicitly told to
  133. // capitalize it.
  134. result += input[i] + ('a' - 'A');
  135. } else {
  136. // Capital letters after the first are left as-is.
  137. result += input[i];
  138. }
  139. cap_next_letter = false;
  140. } else if ('0' <= input[i] && input[i] <= '9') {
  141. result += input[i];
  142. cap_next_letter = true;
  143. } else {
  144. cap_next_letter = true;
  145. }
  146. }
  147. // Add a trailing "_" if the name should be altered.
  148. if (input[input.size() - 1] == '#') {
  149. result += '_';
  150. }
  151. return result;
  152. }
  153. string UnderscoresToCamelCase(const FieldDescriptor* field) {
  154. return UnderscoresToCamelCase(FieldName(field), false);
  155. }
  156. string UnderscoresToCapitalizedCamelCase(const FieldDescriptor* field) {
  157. return UnderscoresToCamelCase(FieldName(field), true);
  158. }
  159. string UnderscoresToCamelCase(const MethodDescriptor* method) {
  160. return UnderscoresToCamelCase(method->name(), false);
  161. }
  162. string UniqueFileScopeIdentifier(const Descriptor* descriptor) {
  163. return "static_" + StringReplace(descriptor->full_name(), ".", "_", true);
  164. }
  165. string CamelCaseFieldName(const FieldDescriptor* field) {
  166. string fieldName = UnderscoresToCamelCase(field);
  167. if ('0' <= fieldName[0] && fieldName[0] <= '9') {
  168. return '_' + fieldName;
  169. }
  170. return fieldName;
  171. }
  172. string StripProto(const string& filename) {
  173. if (HasSuffixString(filename, ".protodevel")) {
  174. return StripSuffixString(filename, ".protodevel");
  175. } else {
  176. return StripSuffixString(filename, ".proto");
  177. }
  178. }
  179. string FileClassName(const FileDescriptor* file, bool immutable) {
  180. ClassNameResolver name_resolver;
  181. return name_resolver.GetFileClassName(file, immutable);
  182. }
  183. string FileJavaPackage(const FileDescriptor* file, bool immutable) {
  184. string result;
  185. if (file->options().has_java_package()) {
  186. result = file->options().java_package();
  187. } else {
  188. result = kDefaultPackage;
  189. if (!file->package().empty()) {
  190. if (!result.empty()) result += '.';
  191. result += file->package();
  192. }
  193. }
  194. return result;
  195. }
  196. string JavaPackageToDir(string package_name) {
  197. string package_dir =
  198. StringReplace(package_name, ".", "/", true);
  199. if (!package_dir.empty()) package_dir += "/";
  200. return package_dir;
  201. }
  202. // TODO(xiaofeng): This function is only kept for it's publicly referenced.
  203. // It should be removed after mutable API up-integration.
  204. string ToJavaName(const string& full_name,
  205. const FileDescriptor* file) {
  206. string result;
  207. if (file->options().java_multiple_files()) {
  208. result = FileJavaPackage(file);
  209. } else {
  210. result = ClassName(file);
  211. }
  212. if (!result.empty()) {
  213. result += '.';
  214. }
  215. if (file->package().empty()) {
  216. result += full_name;
  217. } else {
  218. // Strip the proto package from full_name since we've replaced it with
  219. // the Java package.
  220. result += full_name.substr(file->package().size() + 1);
  221. }
  222. return result;
  223. }
  224. string ClassName(const Descriptor* descriptor) {
  225. ClassNameResolver name_resolver;
  226. return name_resolver.GetClassName(descriptor, true);
  227. }
  228. string ClassName(const EnumDescriptor* descriptor) {
  229. ClassNameResolver name_resolver;
  230. return name_resolver.GetClassName(descriptor, true);
  231. }
  232. string ClassName(const ServiceDescriptor* descriptor) {
  233. ClassNameResolver name_resolver;
  234. return name_resolver.GetClassName(descriptor, true);
  235. }
  236. string ClassName(const FileDescriptor* descriptor) {
  237. ClassNameResolver name_resolver;
  238. return name_resolver.GetClassName(descriptor, true);
  239. }
  240. string ExtraMessageInterfaces(const Descriptor* descriptor) {
  241. string interfaces = "// @@protoc_insertion_point(message_implements:"
  242. + descriptor->full_name() + ")";
  243. return interfaces;
  244. }
  245. string ExtraBuilderInterfaces(const Descriptor* descriptor) {
  246. string interfaces = "// @@protoc_insertion_point(builder_implements:"
  247. + descriptor->full_name() + ")";
  248. return interfaces;
  249. }
  250. string ExtraMessageOrBuilderInterfaces(const Descriptor* descriptor) {
  251. string interfaces = "// @@protoc_insertion_point(interface_extends:"
  252. + descriptor->full_name() + ")";
  253. return interfaces;
  254. }
  255. string FieldConstantName(const FieldDescriptor *field) {
  256. string name = field->name() + "_FIELD_NUMBER";
  257. UpperString(&name);
  258. return name;
  259. }
  260. FieldDescriptor::Type GetType(const FieldDescriptor* field) {
  261. return field->type();
  262. }
  263. JavaType GetJavaType(const FieldDescriptor* field) {
  264. switch (GetType(field)) {
  265. case FieldDescriptor::TYPE_INT32:
  266. case FieldDescriptor::TYPE_UINT32:
  267. case FieldDescriptor::TYPE_SINT32:
  268. case FieldDescriptor::TYPE_FIXED32:
  269. case FieldDescriptor::TYPE_SFIXED32:
  270. return JAVATYPE_INT;
  271. case FieldDescriptor::TYPE_INT64:
  272. case FieldDescriptor::TYPE_UINT64:
  273. case FieldDescriptor::TYPE_SINT64:
  274. case FieldDescriptor::TYPE_FIXED64:
  275. case FieldDescriptor::TYPE_SFIXED64:
  276. return JAVATYPE_LONG;
  277. case FieldDescriptor::TYPE_FLOAT:
  278. return JAVATYPE_FLOAT;
  279. case FieldDescriptor::TYPE_DOUBLE:
  280. return JAVATYPE_DOUBLE;
  281. case FieldDescriptor::TYPE_BOOL:
  282. return JAVATYPE_BOOLEAN;
  283. case FieldDescriptor::TYPE_STRING:
  284. return JAVATYPE_STRING;
  285. case FieldDescriptor::TYPE_BYTES:
  286. return JAVATYPE_BYTES;
  287. case FieldDescriptor::TYPE_ENUM:
  288. return JAVATYPE_ENUM;
  289. case FieldDescriptor::TYPE_GROUP:
  290. case FieldDescriptor::TYPE_MESSAGE:
  291. return JAVATYPE_MESSAGE;
  292. // No default because we want the compiler to complain if any new
  293. // types are added.
  294. }
  295. GOOGLE_LOG(FATAL) << "Can't get here.";
  296. return JAVATYPE_INT;
  297. }
  298. const char* PrimitiveTypeName(JavaType type) {
  299. switch (type) {
  300. case JAVATYPE_INT : return "int";
  301. case JAVATYPE_LONG : return "long";
  302. case JAVATYPE_FLOAT : return "float";
  303. case JAVATYPE_DOUBLE : return "double";
  304. case JAVATYPE_BOOLEAN: return "boolean";
  305. case JAVATYPE_STRING : return "java.lang.String";
  306. case JAVATYPE_BYTES : return "com.google.protobuf.ByteString";
  307. case JAVATYPE_ENUM : return NULL;
  308. case JAVATYPE_MESSAGE: return NULL;
  309. // No default because we want the compiler to complain if any new
  310. // JavaTypes are added.
  311. }
  312. GOOGLE_LOG(FATAL) << "Can't get here.";
  313. return NULL;
  314. }
  315. const char* BoxedPrimitiveTypeName(JavaType type) {
  316. switch (type) {
  317. case JAVATYPE_INT : return "java.lang.Integer";
  318. case JAVATYPE_LONG : return "java.lang.Long";
  319. case JAVATYPE_FLOAT : return "java.lang.Float";
  320. case JAVATYPE_DOUBLE : return "java.lang.Double";
  321. case JAVATYPE_BOOLEAN: return "java.lang.Boolean";
  322. case JAVATYPE_STRING : return "java.lang.String";
  323. case JAVATYPE_BYTES : return "com.google.protobuf.ByteString";
  324. case JAVATYPE_ENUM : return NULL;
  325. case JAVATYPE_MESSAGE: return NULL;
  326. // No default because we want the compiler to complain if any new
  327. // JavaTypes are added.
  328. }
  329. GOOGLE_LOG(FATAL) << "Can't get here.";
  330. return NULL;
  331. }
  332. const char* FieldTypeName(FieldDescriptor::Type field_type) {
  333. switch (field_type) {
  334. case FieldDescriptor::TYPE_INT32 : return "INT32";
  335. case FieldDescriptor::TYPE_UINT32 : return "UINT32";
  336. case FieldDescriptor::TYPE_SINT32 : return "SINT32";
  337. case FieldDescriptor::TYPE_FIXED32 : return "FIXED32";
  338. case FieldDescriptor::TYPE_SFIXED32: return "SFIXED32";
  339. case FieldDescriptor::TYPE_INT64 : return "INT64";
  340. case FieldDescriptor::TYPE_UINT64 : return "UINT64";
  341. case FieldDescriptor::TYPE_SINT64 : return "SINT64";
  342. case FieldDescriptor::TYPE_FIXED64 : return "FIXED64";
  343. case FieldDescriptor::TYPE_SFIXED64: return "SFIXED64";
  344. case FieldDescriptor::TYPE_FLOAT : return "FLOAT";
  345. case FieldDescriptor::TYPE_DOUBLE : return "DOUBLE";
  346. case FieldDescriptor::TYPE_BOOL : return "BOOL";
  347. case FieldDescriptor::TYPE_STRING : return "STRING";
  348. case FieldDescriptor::TYPE_BYTES : return "BYTES";
  349. case FieldDescriptor::TYPE_ENUM : return "ENUM";
  350. case FieldDescriptor::TYPE_GROUP : return "GROUP";
  351. case FieldDescriptor::TYPE_MESSAGE : return "MESSAGE";
  352. // No default because we want the compiler to complain if any new
  353. // types are added.
  354. }
  355. GOOGLE_LOG(FATAL) << "Can't get here.";
  356. return NULL;
  357. }
  358. bool AllAscii(const string& text) {
  359. for (int i = 0; i < text.size(); i++) {
  360. if ((text[i] & 0x80) != 0) {
  361. return false;
  362. }
  363. }
  364. return true;
  365. }
  366. string DefaultValue(const FieldDescriptor* field, bool immutable,
  367. ClassNameResolver* name_resolver) {
  368. // Switch on CppType since we need to know which default_value_* method
  369. // of FieldDescriptor to call.
  370. switch (field->cpp_type()) {
  371. case FieldDescriptor::CPPTYPE_INT32:
  372. return SimpleItoa(field->default_value_int32());
  373. case FieldDescriptor::CPPTYPE_UINT32:
  374. // Need to print as a signed int since Java has no unsigned.
  375. return SimpleItoa(static_cast<int32>(field->default_value_uint32()));
  376. case FieldDescriptor::CPPTYPE_INT64:
  377. return SimpleItoa(field->default_value_int64()) + "L";
  378. case FieldDescriptor::CPPTYPE_UINT64:
  379. return SimpleItoa(static_cast<int64>(field->default_value_uint64())) +
  380. "L";
  381. case FieldDescriptor::CPPTYPE_DOUBLE: {
  382. double value = field->default_value_double();
  383. if (value == std::numeric_limits<double>::infinity()) {
  384. return "Double.POSITIVE_INFINITY";
  385. } else if (value == -std::numeric_limits<double>::infinity()) {
  386. return "Double.NEGATIVE_INFINITY";
  387. } else if (value != value) {
  388. return "Double.NaN";
  389. } else {
  390. return SimpleDtoa(value) + "D";
  391. }
  392. }
  393. case FieldDescriptor::CPPTYPE_FLOAT: {
  394. float value = field->default_value_float();
  395. if (value == std::numeric_limits<float>::infinity()) {
  396. return "Float.POSITIVE_INFINITY";
  397. } else if (value == -std::numeric_limits<float>::infinity()) {
  398. return "Float.NEGATIVE_INFINITY";
  399. } else if (value != value) {
  400. return "Float.NaN";
  401. } else {
  402. return SimpleFtoa(value) + "F";
  403. }
  404. }
  405. case FieldDescriptor::CPPTYPE_BOOL:
  406. return field->default_value_bool() ? "true" : "false";
  407. case FieldDescriptor::CPPTYPE_STRING:
  408. if (GetType(field) == FieldDescriptor::TYPE_BYTES) {
  409. if (field->has_default_value()) {
  410. // See comments in Internal.java for gory details.
  411. return strings::Substitute(
  412. "com.google.protobuf.Internal.bytesDefaultValue(\"$0\")",
  413. CEscape(field->default_value_string()));
  414. } else {
  415. return "com.google.protobuf.ByteString.EMPTY";
  416. }
  417. } else {
  418. if (AllAscii(field->default_value_string())) {
  419. // All chars are ASCII. In this case CEscape() works fine.
  420. return "\"" + CEscape(field->default_value_string()) + "\"";
  421. } else {
  422. // See comments in Internal.java for gory details.
  423. return strings::Substitute(
  424. "com.google.protobuf.Internal.stringDefaultValue(\"$0\")",
  425. CEscape(field->default_value_string()));
  426. }
  427. }
  428. case FieldDescriptor::CPPTYPE_ENUM:
  429. return name_resolver->GetClassName(field->enum_type(), immutable) + "." +
  430. field->default_value_enum()->name();
  431. case FieldDescriptor::CPPTYPE_MESSAGE:
  432. return name_resolver->GetClassName(field->message_type(), immutable) +
  433. ".getDefaultInstance()";
  434. // No default because we want the compiler to complain if any new
  435. // types are added.
  436. }
  437. GOOGLE_LOG(FATAL) << "Can't get here.";
  438. return "";
  439. }
  440. bool IsDefaultValueJavaDefault(const FieldDescriptor* field) {
  441. // Switch on CppType since we need to know which default_value_* method
  442. // of FieldDescriptor to call.
  443. switch (field->cpp_type()) {
  444. case FieldDescriptor::CPPTYPE_INT32:
  445. return field->default_value_int32() == 0;
  446. case FieldDescriptor::CPPTYPE_UINT32:
  447. return field->default_value_uint32() == 0;
  448. case FieldDescriptor::CPPTYPE_INT64:
  449. return field->default_value_int64() == 0L;
  450. case FieldDescriptor::CPPTYPE_UINT64:
  451. return field->default_value_uint64() == 0L;
  452. case FieldDescriptor::CPPTYPE_DOUBLE:
  453. return field->default_value_double() == 0.0;
  454. case FieldDescriptor::CPPTYPE_FLOAT:
  455. return field->default_value_float() == 0.0;
  456. case FieldDescriptor::CPPTYPE_BOOL:
  457. return field->default_value_bool() == false;
  458. case FieldDescriptor::CPPTYPE_ENUM:
  459. return field->default_value_enum()->number() == 0;
  460. case FieldDescriptor::CPPTYPE_STRING:
  461. case FieldDescriptor::CPPTYPE_MESSAGE:
  462. return false;
  463. // No default because we want the compiler to complain if any new
  464. // types are added.
  465. }
  466. GOOGLE_LOG(FATAL) << "Can't get here.";
  467. return false;
  468. }
  469. bool IsByteStringWithCustomDefaultValue(const FieldDescriptor* field) {
  470. return GetJavaType(field) == JAVATYPE_BYTES &&
  471. field->default_value_string() != "";
  472. }
  473. const char* bit_masks[] = {
  474. "0x00000001",
  475. "0x00000002",
  476. "0x00000004",
  477. "0x00000008",
  478. "0x00000010",
  479. "0x00000020",
  480. "0x00000040",
  481. "0x00000080",
  482. "0x00000100",
  483. "0x00000200",
  484. "0x00000400",
  485. "0x00000800",
  486. "0x00001000",
  487. "0x00002000",
  488. "0x00004000",
  489. "0x00008000",
  490. "0x00010000",
  491. "0x00020000",
  492. "0x00040000",
  493. "0x00080000",
  494. "0x00100000",
  495. "0x00200000",
  496. "0x00400000",
  497. "0x00800000",
  498. "0x01000000",
  499. "0x02000000",
  500. "0x04000000",
  501. "0x08000000",
  502. "0x10000000",
  503. "0x20000000",
  504. "0x40000000",
  505. "0x80000000",
  506. };
  507. string GetBitFieldName(int index) {
  508. string varName = "bitField";
  509. varName += SimpleItoa(index);
  510. varName += "_";
  511. return varName;
  512. }
  513. string GetBitFieldNameForBit(int bitIndex) {
  514. return GetBitFieldName(bitIndex / 32);
  515. }
  516. namespace {
  517. string GenerateGetBitInternal(const string& prefix, int bitIndex) {
  518. string varName = prefix + GetBitFieldNameForBit(bitIndex);
  519. int bitInVarIndex = bitIndex % 32;
  520. string mask = bit_masks[bitInVarIndex];
  521. string result = "((" + varName + " & " + mask + ") == " + mask + ")";
  522. return result;
  523. }
  524. string GenerateSetBitInternal(const string& prefix, int bitIndex) {
  525. string varName = prefix + GetBitFieldNameForBit(bitIndex);
  526. int bitInVarIndex = bitIndex % 32;
  527. string mask = bit_masks[bitInVarIndex];
  528. string result = varName + " |= " + mask;
  529. return result;
  530. }
  531. } // namespace
  532. string GenerateGetBit(int bitIndex) {
  533. return GenerateGetBitInternal("", bitIndex);
  534. }
  535. string GenerateSetBit(int bitIndex) {
  536. return GenerateSetBitInternal("", bitIndex);
  537. }
  538. string GenerateClearBit(int bitIndex) {
  539. string varName = GetBitFieldNameForBit(bitIndex);
  540. int bitInVarIndex = bitIndex % 32;
  541. string mask = bit_masks[bitInVarIndex];
  542. string result = varName + " = (" + varName + " & ~" + mask + ")";
  543. return result;
  544. }
  545. string GenerateGetBitFromLocal(int bitIndex) {
  546. return GenerateGetBitInternal("from_", bitIndex);
  547. }
  548. string GenerateSetBitToLocal(int bitIndex) {
  549. return GenerateSetBitInternal("to_", bitIndex);
  550. }
  551. string GenerateGetBitMutableLocal(int bitIndex) {
  552. return GenerateGetBitInternal("mutable_", bitIndex);
  553. }
  554. string GenerateSetBitMutableLocal(int bitIndex) {
  555. return GenerateSetBitInternal("mutable_", bitIndex);
  556. }
  557. bool IsReferenceType(JavaType type) {
  558. switch (type) {
  559. case JAVATYPE_INT : return false;
  560. case JAVATYPE_LONG : return false;
  561. case JAVATYPE_FLOAT : return false;
  562. case JAVATYPE_DOUBLE : return false;
  563. case JAVATYPE_BOOLEAN: return false;
  564. case JAVATYPE_STRING : return true;
  565. case JAVATYPE_BYTES : return true;
  566. case JAVATYPE_ENUM : return true;
  567. case JAVATYPE_MESSAGE: return true;
  568. // No default because we want the compiler to complain if any new
  569. // JavaTypes are added.
  570. }
  571. GOOGLE_LOG(FATAL) << "Can't get here.";
  572. return false;
  573. }
  574. const char* GetCapitalizedType(const FieldDescriptor* field, bool immutable) {
  575. switch (GetType(field)) {
  576. case FieldDescriptor::TYPE_INT32 : return "Int32";
  577. case FieldDescriptor::TYPE_UINT32 : return "UInt32";
  578. case FieldDescriptor::TYPE_SINT32 : return "SInt32";
  579. case FieldDescriptor::TYPE_FIXED32 : return "Fixed32";
  580. case FieldDescriptor::TYPE_SFIXED32: return "SFixed32";
  581. case FieldDescriptor::TYPE_INT64 : return "Int64";
  582. case FieldDescriptor::TYPE_UINT64 : return "UInt64";
  583. case FieldDescriptor::TYPE_SINT64 : return "SInt64";
  584. case FieldDescriptor::TYPE_FIXED64 : return "Fixed64";
  585. case FieldDescriptor::TYPE_SFIXED64: return "SFixed64";
  586. case FieldDescriptor::TYPE_FLOAT : return "Float";
  587. case FieldDescriptor::TYPE_DOUBLE : return "Double";
  588. case FieldDescriptor::TYPE_BOOL : return "Bool";
  589. case FieldDescriptor::TYPE_STRING : return "String";
  590. case FieldDescriptor::TYPE_BYTES : {
  591. return "Bytes";
  592. }
  593. case FieldDescriptor::TYPE_ENUM : return "Enum";
  594. case FieldDescriptor::TYPE_GROUP : return "Group";
  595. case FieldDescriptor::TYPE_MESSAGE : return "Message";
  596. // No default because we want the compiler to complain if any new
  597. // types are added.
  598. }
  599. GOOGLE_LOG(FATAL) << "Can't get here.";
  600. return NULL;
  601. }
  602. // For encodings with fixed sizes, returns that size in bytes. Otherwise
  603. // returns -1.
  604. int FixedSize(FieldDescriptor::Type type) {
  605. switch (type) {
  606. case FieldDescriptor::TYPE_INT32 : return -1;
  607. case FieldDescriptor::TYPE_INT64 : return -1;
  608. case FieldDescriptor::TYPE_UINT32 : return -1;
  609. case FieldDescriptor::TYPE_UINT64 : return -1;
  610. case FieldDescriptor::TYPE_SINT32 : return -1;
  611. case FieldDescriptor::TYPE_SINT64 : return -1;
  612. case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
  613. case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
  614. case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
  615. case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
  616. case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize;
  617. case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize;
  618. case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize;
  619. case FieldDescriptor::TYPE_ENUM : return -1;
  620. case FieldDescriptor::TYPE_STRING : return -1;
  621. case FieldDescriptor::TYPE_BYTES : return -1;
  622. case FieldDescriptor::TYPE_GROUP : return -1;
  623. case FieldDescriptor::TYPE_MESSAGE : return -1;
  624. // No default because we want the compiler to complain if any new
  625. // types are added.
  626. }
  627. GOOGLE_LOG(FATAL) << "Can't get here.";
  628. return -1;
  629. }
  630. // Sort the fields of the given Descriptor by number into a new[]'d array
  631. // and return it. The caller should delete the returned array.
  632. const FieldDescriptor** SortFieldsByNumber(const Descriptor* descriptor) {
  633. const FieldDescriptor** fields =
  634. new const FieldDescriptor*[descriptor->field_count()];
  635. for (int i = 0; i < descriptor->field_count(); i++) {
  636. fields[i] = descriptor->field(i);
  637. }
  638. std::sort(fields, fields + descriptor->field_count(),
  639. FieldOrderingByNumber());
  640. return fields;
  641. }
  642. // Returns true if the message type has any required fields. If it doesn't,
  643. // we can optimize out calls to its isInitialized() method.
  644. //
  645. // already_seen is used to avoid checking the same type multiple times
  646. // (and also to protect against recursion).
  647. bool HasRequiredFields(
  648. const Descriptor* type,
  649. hash_set<const Descriptor*>* already_seen) {
  650. if (already_seen->count(type) > 0) {
  651. // The type is already in cache. This means that either:
  652. // a. The type has no required fields.
  653. // b. We are in the midst of checking if the type has required fields,
  654. // somewhere up the stack. In this case, we know that if the type
  655. // has any required fields, they'll be found when we return to it,
  656. // and the whole call to HasRequiredFields() will return true.
  657. // Therefore, we don't have to check if this type has required fields
  658. // here.
  659. return false;
  660. }
  661. already_seen->insert(type);
  662. // If the type has extensions, an extension with message type could contain
  663. // required fields, so we have to be conservative and assume such an
  664. // extension exists.
  665. if (type->extension_range_count() > 0) return true;
  666. for (int i = 0; i < type->field_count(); i++) {
  667. const FieldDescriptor* field = type->field(i);
  668. if (field->is_required()) {
  669. return true;
  670. }
  671. if (GetJavaType(field) == JAVATYPE_MESSAGE) {
  672. if (HasRequiredFields(field->message_type(), already_seen)) {
  673. return true;
  674. }
  675. }
  676. }
  677. return false;
  678. }
  679. bool HasRequiredFields(const Descriptor* type) {
  680. hash_set<const Descriptor*> already_seen;
  681. return HasRequiredFields(type, &already_seen);
  682. }
  683. bool HasRepeatedFields(const Descriptor* descriptor) {
  684. for (int i = 0; i < descriptor->field_count(); ++i) {
  685. const FieldDescriptor* field = descriptor->field(i);
  686. if (field->is_repeated()) {
  687. return true;
  688. }
  689. }
  690. return false;
  691. }
  692. // Encode an unsigned 32-bit value into a sequence of UTF-16 characters.
  693. //
  694. // If the value is in [0x0000, 0xD7FF], we encode it with a single character
  695. // with the same numeric value.
  696. //
  697. // If the value is larger than 0xD7FF, we encode its lowest 13 bits into a
  698. // character in the range [0xE000, 0xFFFF] by combining these 13 bits with
  699. // 0xE000 using logic-or. Then we shift the value to the right by 13 bits, and
  700. // encode the remaining value by repeating this same process until we get to
  701. // a value in [0x0000, 0xD7FF] where we will encode it using a character with
  702. // the same numeric value.
  703. //
  704. // Note that we only use code points in [0x0000, 0xD7FF] and [0xE000, 0xFFFF].
  705. // There will be no surrogate pairs in the encoded character sequence.
  706. void WriteUInt32ToUtf16CharSequence(uint32 number,
  707. std::vector<uint16>* output) {
  708. // For values in [0x0000, 0xD7FF], only use one char to encode it.
  709. if (number < 0xD800) {
  710. output->push_back(static_cast<uint16>(number));
  711. return;
  712. }
  713. // Encode into multiple chars. All except the last char will be in the range
  714. // [0xE000, 0xFFFF], and the last char will be in the range [0x0000, 0xD7FF].
  715. // Note that we don't use any value in range [0xD800, 0xDFFF] because they
  716. // have to come in pairs and the encoding is just more space-efficient w/o
  717. // them.
  718. while (number >= 0xD800) {
  719. // [0xE000, 0xFFFF] can represent 13 bits of info.
  720. output->push_back(static_cast<uint16>(0xE000 | (number & 0x1FFF)));
  721. number >>= 13;
  722. }
  723. output->push_back(static_cast<uint16>(number));
  724. }
  725. int GetExperimentalJavaFieldTypeForSingular(const FieldDescriptor* field) {
  726. // j/c/g/protobuf/FieldType.java lists field types in a slightly different
  727. // order from FieldDescriptor::Type so we can't do a simple cast.
  728. //
  729. // TODO(xiaofeng): Make j/c/g/protobuf/FieldType.java follow the same order.
  730. int result = field->type();
  731. if (result == FieldDescriptor::TYPE_GROUP) {
  732. return 17;
  733. } else if (result < FieldDescriptor::TYPE_GROUP) {
  734. return result - 1;
  735. } else {
  736. return result - 2;
  737. }
  738. }
  739. int GetExperimentalJavaFieldTypeForRepeated(const FieldDescriptor* field) {
  740. if (field->type() == FieldDescriptor::TYPE_GROUP) {
  741. return 49;
  742. } else {
  743. return GetExperimentalJavaFieldTypeForSingular(field) + 18;
  744. }
  745. }
  746. int GetExperimentalJavaFieldTypeForPacked(const FieldDescriptor* field) {
  747. int result = field->type();
  748. if (result < FieldDescriptor::TYPE_STRING) {
  749. return result + 34;
  750. } else if (result > FieldDescriptor::TYPE_BYTES) {
  751. return result + 30;
  752. } else {
  753. GOOGLE_LOG(FATAL) << field->full_name() << " can't be packed.";
  754. return 0;
  755. }
  756. }
  757. int GetExperimentalJavaFieldType(const FieldDescriptor* field) {
  758. static const int kMapFieldType = 50;
  759. static const int kOneofFieldTypeOffset = 51;
  760. static const int kRequiredBit = 0x100;
  761. static const int kUtf8CheckBit = 0x200;
  762. static const int kCheckInitialized = 0x400;
  763. static const int kMapWithProto2EnumValue = 0x800;
  764. int extra_bits = field->is_required() ? kRequiredBit : 0;
  765. if (field->type() == FieldDescriptor::TYPE_STRING && CheckUtf8(field)) {
  766. extra_bits |= kUtf8CheckBit;
  767. }
  768. if (field->is_required() || (GetJavaType(field) == JAVATYPE_MESSAGE &&
  769. HasRequiredFields(field->message_type()))) {
  770. extra_bits |= kCheckInitialized;
  771. }
  772. if (field->is_map()) {
  773. if (SupportFieldPresence(field->file())) {
  774. const FieldDescriptor* value =
  775. field->message_type()->FindFieldByName("value");
  776. if (GetJavaType(value) == JAVATYPE_ENUM) {
  777. extra_bits |= kMapWithProto2EnumValue;
  778. }
  779. }
  780. return kMapFieldType | extra_bits;
  781. } else if (field->is_packed()) {
  782. return GetExperimentalJavaFieldTypeForPacked(field);
  783. } else if (field->is_repeated()) {
  784. return GetExperimentalJavaFieldTypeForRepeated(field) | extra_bits;
  785. } else if (field->containing_oneof() != NULL) {
  786. return (GetExperimentalJavaFieldTypeForSingular(field) +
  787. kOneofFieldTypeOffset) |
  788. extra_bits;
  789. } else {
  790. return GetExperimentalJavaFieldTypeForSingular(field) | extra_bits;
  791. }
  792. }
  793. // Escape a UTF-16 character to be embedded in a Java string.
  794. void EscapeUtf16ToString(uint16 code, string* output) {
  795. if (code == '\t') {
  796. output->append("\\t");
  797. } else if (code == '\b') {
  798. output->append("\\b");
  799. } else if (code == '\n') {
  800. output->append("\\n");
  801. } else if (code == '\r') {
  802. output->append("\\r");
  803. } else if (code == '\f') {
  804. output->append("\\f");
  805. } else if (code == '\'') {
  806. output->append("\\'");
  807. } else if (code == '\"') {
  808. output->append("\\\"");
  809. } else if (code == '\\') {
  810. output->append("\\\\");
  811. } else if (code >= 0x20 && code <= 0x7f) {
  812. output->push_back(static_cast<char>(code));
  813. } else {
  814. output->append(StringPrintf("\\u%04x", code));
  815. }
  816. }
  817. std::pair<int, int> GetTableDrivenNumberOfEntriesAndLookUpStartFieldNumber(
  818. const FieldDescriptor** fields, int count) {
  819. GOOGLE_CHECK_GT(count, 0);
  820. int table_driven_number_of_entries = count;
  821. int look_up_start_field_number = 0;
  822. for (int i = 0; i < count; i++) {
  823. const int field_number = fields[i]->number();
  824. if (ShouldUseTable(fields[0]->number(), field_number, i + 1)) {
  825. table_driven_number_of_entries =
  826. field_number - fields[0]->number() + 1 + count - i - 1;
  827. look_up_start_field_number = field_number + 1;
  828. }
  829. }
  830. return std::make_pair(
  831. table_driven_number_of_entries, look_up_start_field_number);
  832. }
  833. } // namespace java
  834. } // namespace compiler
  835. } // namespace protobuf
  836. } // namespace google