/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/java/java_enum_field.cc

http://github.com/tomahawk-player/tomahawk · C++ · 537 lines · 412 code · 60 blank · 65 comment · 10 complexity · d6bdcbfccfa38cdc9c76565db82f22f3 MD5 · raw file

  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // http://code.google.com/p/protobuf/
  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 <map>
  34. #include <string>
  35. #include <google/protobuf/compiler/java/java_enum_field.h>
  36. #include <google/protobuf/stubs/common.h>
  37. #include <google/protobuf/compiler/java/java_helpers.h>
  38. #include <google/protobuf/io/printer.h>
  39. #include <google/protobuf/wire_format.h>
  40. #include <google/protobuf/stubs/strutil.h>
  41. namespace google {
  42. namespace protobuf {
  43. namespace compiler {
  44. namespace java {
  45. namespace {
  46. // TODO(kenton): Factor out a "SetCommonFieldVariables()" to get rid of
  47. // repeat code between this and the other field types.
  48. void SetEnumVariables(const FieldDescriptor* descriptor,
  49. int messageBitIndex,
  50. int builderBitIndex,
  51. map<string, string>* variables) {
  52. (*variables)["name"] =
  53. UnderscoresToCamelCase(descriptor);
  54. (*variables)["capitalized_name"] =
  55. UnderscoresToCapitalizedCamelCase(descriptor);
  56. (*variables)["constant_name"] = FieldConstantName(descriptor);
  57. (*variables)["number"] = SimpleItoa(descriptor->number());
  58. (*variables)["type"] = ClassName(descriptor->enum_type());
  59. (*variables)["default"] = DefaultValue(descriptor);
  60. (*variables)["tag"] = SimpleItoa(internal::WireFormat::MakeTag(descriptor));
  61. (*variables)["tag_size"] = SimpleItoa(
  62. internal::WireFormat::TagSize(descriptor->number(), GetType(descriptor)));
  63. // TODO(birdo): Add @deprecated javadoc when generating javadoc is supported
  64. // by the proto compiler
  65. (*variables)["deprecation"] = descriptor->options().deprecated()
  66. ? "@java.lang.Deprecated " : "";
  67. (*variables)["on_changed"] =
  68. HasDescriptorMethods(descriptor->containing_type()) ? "onChanged();" : "";
  69. // For singular messages and builders, one bit is used for the hasField bit.
  70. (*variables)["get_has_field_bit_message"] = GenerateGetBit(messageBitIndex);
  71. (*variables)["get_has_field_bit_builder"] = GenerateGetBit(builderBitIndex);
  72. (*variables)["set_has_field_bit_builder"] = GenerateSetBit(builderBitIndex);
  73. (*variables)["clear_has_field_bit_builder"] =
  74. GenerateClearBit(builderBitIndex);
  75. // For repated builders, one bit is used for whether the array is immutable.
  76. (*variables)["get_mutable_bit_builder"] = GenerateGetBit(builderBitIndex);
  77. (*variables)["set_mutable_bit_builder"] = GenerateSetBit(builderBitIndex);
  78. (*variables)["clear_mutable_bit_builder"] = GenerateClearBit(builderBitIndex);
  79. (*variables)["get_has_field_bit_from_local"] =
  80. GenerateGetBitFromLocal(builderBitIndex);
  81. (*variables)["set_has_field_bit_to_local"] =
  82. GenerateSetBitToLocal(messageBitIndex);
  83. }
  84. } // namespace
  85. // ===================================================================
  86. EnumFieldGenerator::
  87. EnumFieldGenerator(const FieldDescriptor* descriptor,
  88. int messageBitIndex,
  89. int builderBitIndex)
  90. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  91. builderBitIndex_(builderBitIndex) {
  92. SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, &variables_);
  93. }
  94. EnumFieldGenerator::~EnumFieldGenerator() {}
  95. int EnumFieldGenerator::GetNumBitsForMessage() const {
  96. return 1;
  97. }
  98. int EnumFieldGenerator::GetNumBitsForBuilder() const {
  99. return 1;
  100. }
  101. void EnumFieldGenerator::
  102. GenerateInterfaceMembers(io::Printer* printer) const {
  103. printer->Print(variables_,
  104. "$deprecation$boolean has$capitalized_name$();\n"
  105. "$deprecation$$type$ get$capitalized_name$();\n");
  106. }
  107. void EnumFieldGenerator::
  108. GenerateMembers(io::Printer* printer) const {
  109. printer->Print(variables_,
  110. "private $type$ $name$_;\n"
  111. "$deprecation$public boolean has$capitalized_name$() {\n"
  112. " return $get_has_field_bit_message$;\n"
  113. "}\n"
  114. "$deprecation$public $type$ get$capitalized_name$() {\n"
  115. " return $name$_;\n"
  116. "}\n");
  117. }
  118. void EnumFieldGenerator::
  119. GenerateBuilderMembers(io::Printer* printer) const {
  120. printer->Print(variables_,
  121. "private $type$ $name$_ = $default$;\n"
  122. "$deprecation$public boolean has$capitalized_name$() {\n"
  123. " return $get_has_field_bit_builder$;\n"
  124. "}\n"
  125. "$deprecation$public $type$ get$capitalized_name$() {\n"
  126. " return $name$_;\n"
  127. "}\n"
  128. "$deprecation$public Builder set$capitalized_name$($type$ value) {\n"
  129. " if (value == null) {\n"
  130. " throw new NullPointerException();\n"
  131. " }\n"
  132. " $set_has_field_bit_builder$;\n"
  133. " $name$_ = value;\n"
  134. " $on_changed$\n"
  135. " return this;\n"
  136. "}\n"
  137. "$deprecation$public Builder clear$capitalized_name$() {\n"
  138. " $clear_has_field_bit_builder$;\n"
  139. " $name$_ = $default$;\n"
  140. " $on_changed$\n"
  141. " return this;\n"
  142. "}\n");
  143. }
  144. void EnumFieldGenerator::
  145. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  146. // noop for enums
  147. }
  148. void EnumFieldGenerator::
  149. GenerateInitializationCode(io::Printer* printer) const {
  150. printer->Print(variables_, "$name$_ = $default$;\n");
  151. }
  152. void EnumFieldGenerator::
  153. GenerateBuilderClearCode(io::Printer* printer) const {
  154. printer->Print(variables_,
  155. "$name$_ = $default$;\n"
  156. "$clear_has_field_bit_builder$;\n");
  157. }
  158. void EnumFieldGenerator::
  159. GenerateMergingCode(io::Printer* printer) const {
  160. printer->Print(variables_,
  161. "if (other.has$capitalized_name$()) {\n"
  162. " set$capitalized_name$(other.get$capitalized_name$());\n"
  163. "}\n");
  164. }
  165. void EnumFieldGenerator::
  166. GenerateBuildingCode(io::Printer* printer) const {
  167. printer->Print(variables_,
  168. "if ($get_has_field_bit_from_local$) {\n"
  169. " $set_has_field_bit_to_local$;\n"
  170. "}\n"
  171. "result.$name$_ = $name$_;\n");
  172. }
  173. void EnumFieldGenerator::
  174. GenerateParsingCode(io::Printer* printer) const {
  175. printer->Print(variables_,
  176. "int rawValue = input.readEnum();\n"
  177. "$type$ value = $type$.valueOf(rawValue);\n");
  178. if (HasUnknownFields(descriptor_->containing_type())) {
  179. printer->Print(variables_,
  180. "if (value == null) {\n"
  181. " unknownFields.mergeVarintField($number$, rawValue);\n"
  182. "} else {\n");
  183. } else {
  184. printer->Print(variables_,
  185. "if (value != null) {\n");
  186. }
  187. printer->Print(variables_,
  188. " $set_has_field_bit_builder$;\n"
  189. " $name$_ = value;\n"
  190. "}\n");
  191. }
  192. void EnumFieldGenerator::
  193. GenerateSerializationCode(io::Printer* printer) const {
  194. printer->Print(variables_,
  195. "if ($get_has_field_bit_message$) {\n"
  196. " output.writeEnum($number$, $name$_.getNumber());\n"
  197. "}\n");
  198. }
  199. void EnumFieldGenerator::
  200. GenerateSerializedSizeCode(io::Printer* printer) const {
  201. printer->Print(variables_,
  202. "if ($get_has_field_bit_message$) {\n"
  203. " size += com.google.protobuf.CodedOutputStream\n"
  204. " .computeEnumSize($number$, $name$_.getNumber());\n"
  205. "}\n");
  206. }
  207. void EnumFieldGenerator::
  208. GenerateEqualsCode(io::Printer* printer) const {
  209. printer->Print(variables_,
  210. "result = result &&\n"
  211. " (get$capitalized_name$() == other.get$capitalized_name$());\n");
  212. }
  213. void EnumFieldGenerator::
  214. GenerateHashCode(io::Printer* printer) const {
  215. printer->Print(variables_,
  216. "hash = (37 * hash) + $constant_name$;\n"
  217. "hash = (53 * hash) + hashEnum(get$capitalized_name$());\n");
  218. }
  219. string EnumFieldGenerator::GetBoxedType() const {
  220. return ClassName(descriptor_->enum_type());
  221. }
  222. // ===================================================================
  223. RepeatedEnumFieldGenerator::
  224. RepeatedEnumFieldGenerator(const FieldDescriptor* descriptor,
  225. int messageBitIndex,
  226. int builderBitIndex)
  227. : descriptor_(descriptor), messageBitIndex_(messageBitIndex),
  228. builderBitIndex_(builderBitIndex) {
  229. SetEnumVariables(descriptor, messageBitIndex, builderBitIndex, &variables_);
  230. }
  231. RepeatedEnumFieldGenerator::~RepeatedEnumFieldGenerator() {}
  232. int RepeatedEnumFieldGenerator::GetNumBitsForMessage() const {
  233. return 0;
  234. }
  235. int RepeatedEnumFieldGenerator::GetNumBitsForBuilder() const {
  236. return 1;
  237. }
  238. void RepeatedEnumFieldGenerator::
  239. GenerateInterfaceMembers(io::Printer* printer) const {
  240. printer->Print(variables_,
  241. "$deprecation$java.util.List<$type$> get$capitalized_name$List();\n"
  242. "$deprecation$int get$capitalized_name$Count();\n"
  243. "$deprecation$$type$ get$capitalized_name$(int index);\n");
  244. }
  245. void RepeatedEnumFieldGenerator::
  246. GenerateMembers(io::Printer* printer) const {
  247. printer->Print(variables_,
  248. "private java.util.List<$type$> $name$_;\n"
  249. "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
  250. " return $name$_;\n" // note: unmodifiable list
  251. "}\n"
  252. "$deprecation$public int get$capitalized_name$Count() {\n"
  253. " return $name$_.size();\n"
  254. "}\n"
  255. "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
  256. " return $name$_.get(index);\n"
  257. "}\n");
  258. if (descriptor_->options().packed() &&
  259. HasGeneratedMethods(descriptor_->containing_type())) {
  260. printer->Print(variables_,
  261. "private int $name$MemoizedSerializedSize;\n");
  262. }
  263. }
  264. void RepeatedEnumFieldGenerator::
  265. GenerateBuilderMembers(io::Printer* printer) const {
  266. printer->Print(variables_,
  267. // One field is the list and the other field keeps track of whether the
  268. // list is immutable. If it's immutable, the invariant is that it must
  269. // either an instance of Collections.emptyList() or it's an ArrayList
  270. // wrapped in a Collections.unmodifiableList() wrapper and nobody else has
  271. // a refererence to the underlying ArrayList. This invariant allows us to
  272. // share instances of lists between protocol buffers avoiding expensive
  273. // memory allocations. Note, immutable is a strong guarantee here -- not
  274. // just that the list cannot be modified via the reference but that the
  275. // list can never be modified.
  276. "private java.util.List<$type$> $name$_ =\n"
  277. " java.util.Collections.emptyList();\n"
  278. "private void ensure$capitalized_name$IsMutable() {\n"
  279. " if (!$get_mutable_bit_builder$) {\n"
  280. " $name$_ = new java.util.ArrayList<$type$>($name$_);\n"
  281. " $set_mutable_bit_builder$;\n"
  282. " }\n"
  283. "}\n"
  284. // Note: We return an unmodifiable list because otherwise the caller
  285. // could hold on to the returned list and modify it after the message
  286. // has been built, thus mutating the message which is supposed to be
  287. // immutable.
  288. "$deprecation$public java.util.List<$type$> get$capitalized_name$List() {\n"
  289. " return java.util.Collections.unmodifiableList($name$_);\n"
  290. "}\n"
  291. "$deprecation$public int get$capitalized_name$Count() {\n"
  292. " return $name$_.size();\n"
  293. "}\n"
  294. "$deprecation$public $type$ get$capitalized_name$(int index) {\n"
  295. " return $name$_.get(index);\n"
  296. "}\n"
  297. "$deprecation$public Builder set$capitalized_name$(\n"
  298. " int index, $type$ value) {\n"
  299. " if (value == null) {\n"
  300. " throw new NullPointerException();\n"
  301. " }\n"
  302. " ensure$capitalized_name$IsMutable();\n"
  303. " $name$_.set(index, value);\n"
  304. " $on_changed$\n"
  305. " return this;\n"
  306. "}\n"
  307. "$deprecation$public Builder add$capitalized_name$($type$ value) {\n"
  308. " if (value == null) {\n"
  309. " throw new NullPointerException();\n"
  310. " }\n"
  311. " ensure$capitalized_name$IsMutable();\n"
  312. " $name$_.add(value);\n"
  313. " $on_changed$\n"
  314. " return this;\n"
  315. "}\n"
  316. "$deprecation$public Builder addAll$capitalized_name$(\n"
  317. " java.lang.Iterable<? extends $type$> values) {\n"
  318. " ensure$capitalized_name$IsMutable();\n"
  319. " super.addAll(values, $name$_);\n"
  320. " $on_changed$\n"
  321. " return this;\n"
  322. "}\n"
  323. "$deprecation$public Builder clear$capitalized_name$() {\n"
  324. " $name$_ = java.util.Collections.emptyList();\n"
  325. " $clear_mutable_bit_builder$;\n"
  326. " $on_changed$\n"
  327. " return this;\n"
  328. "}\n");
  329. }
  330. void RepeatedEnumFieldGenerator::
  331. GenerateFieldBuilderInitializationCode(io::Printer* printer) const {
  332. // noop for enums
  333. }
  334. void RepeatedEnumFieldGenerator::
  335. GenerateInitializationCode(io::Printer* printer) const {
  336. printer->Print(variables_, "$name$_ = java.util.Collections.emptyList();\n");
  337. }
  338. void RepeatedEnumFieldGenerator::
  339. GenerateBuilderClearCode(io::Printer* printer) const {
  340. printer->Print(variables_,
  341. "$name$_ = java.util.Collections.emptyList();\n"
  342. "$clear_mutable_bit_builder$;\n");
  343. }
  344. void RepeatedEnumFieldGenerator::
  345. GenerateMergingCode(io::Printer* printer) const {
  346. // The code below does two optimizations:
  347. // 1. If the other list is empty, there's nothing to do. This ensures we
  348. // don't allocate a new array if we already have an immutable one.
  349. // 2. If the other list is non-empty and our current list is empty, we can
  350. // reuse the other list which is guaranteed to be immutable.
  351. printer->Print(variables_,
  352. "if (!other.$name$_.isEmpty()) {\n"
  353. " if ($name$_.isEmpty()) {\n"
  354. " $name$_ = other.$name$_;\n"
  355. " $clear_mutable_bit_builder$;\n"
  356. " } else {\n"
  357. " ensure$capitalized_name$IsMutable();\n"
  358. " $name$_.addAll(other.$name$_);\n"
  359. " }\n"
  360. " $on_changed$\n"
  361. "}\n");
  362. }
  363. void RepeatedEnumFieldGenerator::
  364. GenerateBuildingCode(io::Printer* printer) const {
  365. // The code below ensures that the result has an immutable list. If our
  366. // list is immutable, we can just reuse it. If not, we make it immutable.
  367. printer->Print(variables_,
  368. "if ($get_mutable_bit_builder$) {\n"
  369. " $name$_ = java.util.Collections.unmodifiableList($name$_);\n"
  370. " $clear_mutable_bit_builder$;\n"
  371. "}\n"
  372. "result.$name$_ = $name$_;\n");
  373. }
  374. void RepeatedEnumFieldGenerator::
  375. GenerateParsingCode(io::Printer* printer) const {
  376. // Read and store the enum
  377. printer->Print(variables_,
  378. "int rawValue = input.readEnum();\n"
  379. "$type$ value = $type$.valueOf(rawValue);\n");
  380. if (HasUnknownFields(descriptor_->containing_type())) {
  381. printer->Print(variables_,
  382. "if (value == null) {\n"
  383. " unknownFields.mergeVarintField($number$, rawValue);\n"
  384. "} else {\n");
  385. } else {
  386. printer->Print(variables_,
  387. "if (value != null) {\n");
  388. }
  389. printer->Print(variables_,
  390. " add$capitalized_name$(value);\n"
  391. "}\n");
  392. }
  393. void RepeatedEnumFieldGenerator::
  394. GenerateParsingCodeFromPacked(io::Printer* printer) const {
  395. // Wrap GenerateParsingCode's contents with a while loop.
  396. printer->Print(variables_,
  397. "int length = input.readRawVarint32();\n"
  398. "int oldLimit = input.pushLimit(length);\n"
  399. "while(input.getBytesUntilLimit() > 0) {\n");
  400. printer->Indent();
  401. GenerateParsingCode(printer);
  402. printer->Outdent();
  403. printer->Print(variables_,
  404. "}\n"
  405. "input.popLimit(oldLimit);\n");
  406. }
  407. void RepeatedEnumFieldGenerator::
  408. GenerateSerializationCode(io::Printer* printer) const {
  409. if (descriptor_->options().packed()) {
  410. printer->Print(variables_,
  411. "if (get$capitalized_name$List().size() > 0) {\n"
  412. " output.writeRawVarint32($tag$);\n"
  413. " output.writeRawVarint32($name$MemoizedSerializedSize);\n"
  414. "}\n"
  415. "for (int i = 0; i < $name$_.size(); i++) {\n"
  416. " output.writeEnumNoTag($name$_.get(i).getNumber());\n"
  417. "}\n");
  418. } else {
  419. printer->Print(variables_,
  420. "for (int i = 0; i < $name$_.size(); i++) {\n"
  421. " output.writeEnum($number$, $name$_.get(i).getNumber());\n"
  422. "}\n");
  423. }
  424. }
  425. void RepeatedEnumFieldGenerator::
  426. GenerateSerializedSizeCode(io::Printer* printer) const {
  427. printer->Print(variables_,
  428. "{\n"
  429. " int dataSize = 0;\n");
  430. printer->Indent();
  431. printer->Print(variables_,
  432. "for (int i = 0; i < $name$_.size(); i++) {\n"
  433. " dataSize += com.google.protobuf.CodedOutputStream\n"
  434. " .computeEnumSizeNoTag($name$_.get(i).getNumber());\n"
  435. "}\n");
  436. printer->Print(
  437. "size += dataSize;\n");
  438. if (descriptor_->options().packed()) {
  439. printer->Print(variables_,
  440. "if (!get$capitalized_name$List().isEmpty()) {"
  441. " size += $tag_size$;\n"
  442. " size += com.google.protobuf.CodedOutputStream\n"
  443. " .computeRawVarint32Size(dataSize);\n"
  444. "}");
  445. } else {
  446. printer->Print(variables_,
  447. "size += $tag_size$ * $name$_.size();\n");
  448. }
  449. // cache the data size for packed fields.
  450. if (descriptor_->options().packed()) {
  451. printer->Print(variables_,
  452. "$name$MemoizedSerializedSize = dataSize;\n");
  453. }
  454. printer->Outdent();
  455. printer->Print("}\n");
  456. }
  457. void RepeatedEnumFieldGenerator::
  458. GenerateEqualsCode(io::Printer* printer) const {
  459. printer->Print(variables_,
  460. "result = result && get$capitalized_name$List()\n"
  461. " .equals(other.get$capitalized_name$List());\n");
  462. }
  463. void RepeatedEnumFieldGenerator::
  464. GenerateHashCode(io::Printer* printer) const {
  465. printer->Print(variables_,
  466. "if (get$capitalized_name$Count() > 0) {\n"
  467. " hash = (37 * hash) + $constant_name$;\n"
  468. " hash = (53 * hash) + hashEnumList(get$capitalized_name$List());\n"
  469. "}\n");
  470. }
  471. string RepeatedEnumFieldGenerator::GetBoxedType() const {
  472. return ClassName(descriptor_->enum_type());
  473. }
  474. } // namespace java
  475. } // namespace compiler
  476. } // namespace protobuf
  477. } // namespace google