cpp_primitive_field.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <google/protobuf/compiler/cpp/cpp_primitive_field.h>
  34. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/wire_format.h>
  37. #include <google/protobuf/stubs/strutil.h>
  38. namespace google {
  39. namespace protobuf {
  40. namespace compiler {
  41. namespace cpp {
  42. using internal::WireFormatLite;
  43. namespace {
  44. // For encodings with fixed sizes, returns that size in bytes. Otherwise
  45. // returns -1.
  46. int FixedSize(FieldDescriptor::Type type) {
  47. switch (type) {
  48. case FieldDescriptor::TYPE_INT32 : return -1;
  49. case FieldDescriptor::TYPE_INT64 : return -1;
  50. case FieldDescriptor::TYPE_UINT32 : return -1;
  51. case FieldDescriptor::TYPE_UINT64 : return -1;
  52. case FieldDescriptor::TYPE_SINT32 : return -1;
  53. case FieldDescriptor::TYPE_SINT64 : return -1;
  54. case FieldDescriptor::TYPE_FIXED32 : return WireFormatLite::kFixed32Size;
  55. case FieldDescriptor::TYPE_FIXED64 : return WireFormatLite::kFixed64Size;
  56. case FieldDescriptor::TYPE_SFIXED32: return WireFormatLite::kSFixed32Size;
  57. case FieldDescriptor::TYPE_SFIXED64: return WireFormatLite::kSFixed64Size;
  58. case FieldDescriptor::TYPE_FLOAT : return WireFormatLite::kFloatSize;
  59. case FieldDescriptor::TYPE_DOUBLE : return WireFormatLite::kDoubleSize;
  60. case FieldDescriptor::TYPE_BOOL : return WireFormatLite::kBoolSize;
  61. case FieldDescriptor::TYPE_ENUM : return -1;
  62. case FieldDescriptor::TYPE_STRING : return -1;
  63. case FieldDescriptor::TYPE_BYTES : return -1;
  64. case FieldDescriptor::TYPE_GROUP : return -1;
  65. case FieldDescriptor::TYPE_MESSAGE : return -1;
  66. // No default because we want the compiler to complain if any new
  67. // types are added.
  68. }
  69. GOOGLE_LOG(FATAL) << "Can't get here.";
  70. return -1;
  71. }
  72. void SetPrimitiveVariables(const FieldDescriptor* descriptor,
  73. std::map<string, string>* variables,
  74. const Options& options) {
  75. SetCommonFieldVariables(descriptor, variables, options);
  76. (*variables)["type"] = PrimitiveTypeName(descriptor->cpp_type());
  77. (*variables)["default"] = DefaultValue(descriptor);
  78. (*variables)["tag"] = SimpleItoa(internal::WireFormat::MakeTag(descriptor));
  79. int fixed_size = FixedSize(descriptor->type());
  80. if (fixed_size != -1) {
  81. (*variables)["fixed_size"] = SimpleItoa(fixed_size);
  82. }
  83. (*variables)["wire_format_field_type"] =
  84. "::google::protobuf::internal::WireFormatLite::" + FieldDescriptorProto_Type_Name(
  85. static_cast<FieldDescriptorProto_Type>(descriptor->type()));
  86. (*variables)["full_name"] = descriptor->full_name();
  87. }
  88. } // namespace
  89. // ===================================================================
  90. PrimitiveFieldGenerator::PrimitiveFieldGenerator(
  91. const FieldDescriptor* descriptor, const Options& options)
  92. : FieldGenerator(options), descriptor_(descriptor) {
  93. SetPrimitiveVariables(descriptor, &variables_, options);
  94. }
  95. PrimitiveFieldGenerator::~PrimitiveFieldGenerator() {}
  96. void PrimitiveFieldGenerator::
  97. GeneratePrivateMembers(io::Printer* printer) const {
  98. printer->Print(variables_, "$type$ $name$_;\n");
  99. }
  100. void PrimitiveFieldGenerator::
  101. GenerateAccessorDeclarations(io::Printer* printer) const {
  102. printer->Print(variables_,
  103. "$deprecated_attr$$type$ $name$() const;\n"
  104. "$deprecated_attr$void set_$name$($type$ value);\n");
  105. }
  106. void PrimitiveFieldGenerator::
  107. GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const {
  108. std::map<string, string> variables(variables_);
  109. variables["inline"] = is_inline ? "inline " : "";
  110. printer->Print(variables,
  111. "$inline$$type$ $classname$::$name$() const {\n"
  112. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  113. " return $name$_;\n"
  114. "}\n"
  115. "$inline$void $classname$::set_$name$($type$ value) {\n"
  116. " $set_hasbit$\n"
  117. " $name$_ = value;\n"
  118. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  119. "}\n");
  120. }
  121. void PrimitiveFieldGenerator::
  122. GenerateClearingCode(io::Printer* printer) const {
  123. printer->Print(variables_, "$name$_ = $default$;\n");
  124. }
  125. void PrimitiveFieldGenerator::
  126. GenerateMergingCode(io::Printer* printer) const {
  127. printer->Print(variables_, "set_$name$(from.$name$());\n");
  128. }
  129. void PrimitiveFieldGenerator::
  130. GenerateSwappingCode(io::Printer* printer) const {
  131. printer->Print(variables_, "std::swap($name$_, other->$name$_);\n");
  132. }
  133. void PrimitiveFieldGenerator::
  134. GenerateConstructorCode(io::Printer* printer) const {
  135. printer->Print(variables_, "$name$_ = $default$;\n");
  136. }
  137. void PrimitiveFieldGenerator::
  138. GenerateCopyConstructorCode(io::Printer* printer) const {
  139. printer->Print(variables_, "$name$_ = from.$name$_;\n");
  140. }
  141. void PrimitiveFieldGenerator::
  142. GenerateMergeFromCodedStream(io::Printer* printer) const {
  143. printer->Print(variables_,
  144. "$set_hasbit$\n"
  145. "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
  146. " $type$, $wire_format_field_type$>(\n"
  147. " input, &$name$_)));\n");
  148. }
  149. void PrimitiveFieldGenerator::
  150. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  151. printer->Print(variables_,
  152. "::google::protobuf::internal::WireFormatLite::Write$declared_type$("
  153. "$number$, this->$name$(), output);\n");
  154. }
  155. void PrimitiveFieldGenerator::
  156. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  157. printer->Print(variables_,
  158. "target = ::google::protobuf::internal::WireFormatLite::Write$declared_type$ToArray("
  159. "$number$, this->$name$(), target);\n");
  160. }
  161. void PrimitiveFieldGenerator::
  162. GenerateByteSize(io::Printer* printer) const {
  163. int fixed_size = FixedSize(descriptor_->type());
  164. if (fixed_size == -1) {
  165. printer->Print(variables_,
  166. "total_size += $tag_size$ +\n"
  167. " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
  168. " this->$name$());\n");
  169. } else {
  170. printer->Print(variables_,
  171. "total_size += $tag_size$ + $fixed_size$;\n");
  172. }
  173. }
  174. // ===================================================================
  175. PrimitiveOneofFieldGenerator::
  176. PrimitiveOneofFieldGenerator(const FieldDescriptor* descriptor,
  177. const Options& options)
  178. : PrimitiveFieldGenerator(descriptor, options) {
  179. SetCommonOneofFieldVariables(descriptor, &variables_);
  180. }
  181. PrimitiveOneofFieldGenerator::~PrimitiveOneofFieldGenerator() {}
  182. void PrimitiveOneofFieldGenerator::
  183. GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const {
  184. std::map<string, string> variables(variables_);
  185. variables["inline"] = is_inline ? "inline " : "";
  186. printer->Print(variables,
  187. "$inline$$type$ $classname$::$name$() const {\n"
  188. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  189. " if (has_$name$()) {\n"
  190. " return $oneof_prefix$$name$_;\n"
  191. " }\n"
  192. " return $default$;\n"
  193. "}\n"
  194. "$inline$void $classname$::set_$name$($type$ value) {\n"
  195. " if (!has_$name$()) {\n"
  196. " clear_$oneof_name$();\n"
  197. " set_has_$name$();\n"
  198. " }\n"
  199. " $oneof_prefix$$name$_ = value;\n"
  200. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  201. "}\n");
  202. }
  203. void PrimitiveOneofFieldGenerator::
  204. GenerateClearingCode(io::Printer* printer) const {
  205. printer->Print(variables_, "$oneof_prefix$$name$_ = $default$;\n");
  206. }
  207. void PrimitiveOneofFieldGenerator::
  208. GenerateSwappingCode(io::Printer* printer) const {
  209. // Don't print any swapping code. Swapping the union will swap this field.
  210. }
  211. void PrimitiveOneofFieldGenerator::
  212. GenerateConstructorCode(io::Printer* printer) const {
  213. printer->Print(
  214. variables_,
  215. " $classname$_default_oneof_instance_.$name$_ = $default$;\n");
  216. }
  217. void PrimitiveOneofFieldGenerator::
  218. GenerateMergeFromCodedStream(io::Printer* printer) const {
  219. printer->Print(variables_,
  220. "clear_$oneof_name$();\n"
  221. "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
  222. " $type$, $wire_format_field_type$>(\n"
  223. " input, &$oneof_prefix$$name$_)));\n"
  224. "set_has_$name$();\n");
  225. }
  226. // ===================================================================
  227. RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
  228. const FieldDescriptor* descriptor, const Options& options)
  229. : FieldGenerator(options), descriptor_(descriptor) {
  230. SetPrimitiveVariables(descriptor, &variables_, options);
  231. if (descriptor->is_packed()) {
  232. variables_["packed_reader"] = "ReadPackedPrimitive";
  233. variables_["repeated_reader"] = "ReadRepeatedPrimitiveNoInline";
  234. } else {
  235. variables_["packed_reader"] = "ReadPackedPrimitiveNoInline";
  236. variables_["repeated_reader"] = "ReadRepeatedPrimitive";
  237. }
  238. }
  239. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  240. void RepeatedPrimitiveFieldGenerator::
  241. GeneratePrivateMembers(io::Printer* printer) const {
  242. printer->Print(variables_,
  243. "::google::protobuf::RepeatedField< $type$ > $name$_;\n");
  244. if (descriptor_->is_packed() &&
  245. HasGeneratedMethods(descriptor_->file(), options_)) {
  246. printer->Print(variables_,
  247. "mutable int _$name$_cached_byte_size_;\n");
  248. }
  249. }
  250. void RepeatedPrimitiveFieldGenerator::
  251. GenerateAccessorDeclarations(io::Printer* printer) const {
  252. printer->Print(variables_,
  253. "$deprecated_attr$$type$ $name$(int index) const;\n"
  254. "$deprecated_attr$void set_$name$(int index, $type$ value);\n"
  255. "$deprecated_attr$void add_$name$($type$ value);\n");
  256. printer->Print(variables_,
  257. "$deprecated_attr$const ::google::protobuf::RepeatedField< $type$ >&\n"
  258. " $name$() const;\n"
  259. "$deprecated_attr$::google::protobuf::RepeatedField< $type$ >*\n"
  260. " mutable_$name$();\n");
  261. }
  262. void RepeatedPrimitiveFieldGenerator::
  263. GenerateInlineAccessorDefinitions(io::Printer* printer, bool is_inline) const {
  264. std::map<string, string> variables(variables_);
  265. variables["inline"] = is_inline ? "inline " : "";
  266. printer->Print(variables,
  267. "$inline$$type$ $classname$::$name$(int index) const {\n"
  268. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  269. " return $name$_.Get(index);\n"
  270. "}\n"
  271. "$inline$void $classname$::set_$name$(int index, $type$ value) {\n"
  272. " $name$_.Set(index, value);\n"
  273. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  274. "}\n"
  275. "$inline$void $classname$::add_$name$($type$ value) {\n"
  276. " $name$_.Add(value);\n"
  277. " // @@protoc_insertion_point(field_add:$full_name$)\n"
  278. "}\n");
  279. printer->Print(variables,
  280. "$inline$const ::google::protobuf::RepeatedField< $type$ >&\n"
  281. "$classname$::$name$() const {\n"
  282. " // @@protoc_insertion_point(field_list:$full_name$)\n"
  283. " return $name$_;\n"
  284. "}\n"
  285. "$inline$::google::protobuf::RepeatedField< $type$ >*\n"
  286. "$classname$::mutable_$name$() {\n"
  287. " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
  288. " return &$name$_;\n"
  289. "}\n");
  290. }
  291. void RepeatedPrimitiveFieldGenerator::
  292. GenerateClearingCode(io::Printer* printer) const {
  293. printer->Print(variables_, "$name$_.Clear();\n");
  294. }
  295. void RepeatedPrimitiveFieldGenerator::
  296. GenerateMergingCode(io::Printer* printer) const {
  297. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  298. }
  299. void RepeatedPrimitiveFieldGenerator::
  300. GenerateSwappingCode(io::Printer* printer) const {
  301. printer->Print(variables_, "$name$_.UnsafeArenaSwap(&other->$name$_);\n");
  302. }
  303. void RepeatedPrimitiveFieldGenerator::
  304. GenerateConstructorCode(io::Printer* printer) const {
  305. // Not needed for repeated fields.
  306. }
  307. void RepeatedPrimitiveFieldGenerator::
  308. GenerateCopyConstructorCode(io::Printer* printer) const {
  309. printer->Print(variables_, "$name$_.CopyFrom(from.$name$_);\n");
  310. }
  311. void RepeatedPrimitiveFieldGenerator::
  312. GenerateMergeFromCodedStream(io::Printer* printer) const {
  313. printer->Print(variables_,
  314. "DO_((::google::protobuf::internal::WireFormatLite::$repeated_reader$<\n"
  315. " $type$, $wire_format_field_type$>(\n"
  316. " $tag_size$, $tag$u, input, this->mutable_$name$())));\n");
  317. }
  318. void RepeatedPrimitiveFieldGenerator::
  319. GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
  320. printer->Print(variables_,
  321. "DO_((::google::protobuf::internal::WireFormatLite::$packed_reader$<\n"
  322. " $type$, $wire_format_field_type$>(\n"
  323. " input, this->mutable_$name$())));\n");
  324. }
  325. void RepeatedPrimitiveFieldGenerator::
  326. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  327. if (descriptor_->is_packed()) {
  328. // Write the tag and the size.
  329. printer->Print(variables_,
  330. "if (this->$name$_size() > 0) {\n"
  331. " ::google::protobuf::internal::WireFormatLite::WriteTag("
  332. "$number$, "
  333. "::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, "
  334. "output);\n"
  335. " output->WriteVarint32(_$name$_cached_byte_size_);\n"
  336. "}\n");
  337. }
  338. printer->Print(variables_,
  339. "for (int i = 0; i < this->$name$_size(); i++) {\n");
  340. if (descriptor_->is_packed()) {
  341. printer->Print(variables_,
  342. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$NoTag(\n"
  343. " this->$name$(i), output);\n");
  344. } else {
  345. printer->Print(variables_,
  346. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
  347. " $number$, this->$name$(i), output);\n");
  348. }
  349. printer->Print("}\n");
  350. }
  351. void RepeatedPrimitiveFieldGenerator::
  352. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  353. if (descriptor_->is_packed()) {
  354. // Write the tag and the size.
  355. printer->Print(variables_,
  356. "if (this->$name$_size() > 0) {\n"
  357. " target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n"
  358. " $number$,\n"
  359. " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
  360. " target);\n"
  361. " target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(\n"
  362. " _$name$_cached_byte_size_, target);\n"
  363. "}\n");
  364. }
  365. printer->Print(variables_,
  366. "for (int i = 0; i < this->$name$_size(); i++) {\n");
  367. if (descriptor_->is_packed()) {
  368. printer->Print(variables_,
  369. " target = ::google::protobuf::internal::WireFormatLite::\n"
  370. " Write$declared_type$NoTagToArray(this->$name$(i), target);\n");
  371. } else {
  372. printer->Print(variables_,
  373. " target = ::google::protobuf::internal::WireFormatLite::\n"
  374. " Write$declared_type$ToArray($number$, this->$name$(i), target);\n");
  375. }
  376. printer->Print("}\n");
  377. }
  378. void RepeatedPrimitiveFieldGenerator::
  379. GenerateByteSize(io::Printer* printer) const {
  380. printer->Print(variables_,
  381. "{\n"
  382. " size_t data_size = 0;\n"
  383. " unsigned int count = this->$name$_size();\n");
  384. printer->Indent();
  385. int fixed_size = FixedSize(descriptor_->type());
  386. if (fixed_size == -1) {
  387. printer->Print(variables_,
  388. "for (unsigned int i = 0; i < count; i++) {\n"
  389. " data_size += ::google::protobuf::internal::WireFormatLite::\n"
  390. " $declared_type$Size(this->$name$(i));\n"
  391. "}\n");
  392. } else {
  393. printer->Print(variables_,
  394. "data_size = $fixed_size$UL * count;\n");
  395. }
  396. if (descriptor_->is_packed()) {
  397. printer->Print(variables_,
  398. "if (data_size > 0) {\n"
  399. " total_size += $tag_size$ +\n"
  400. " ::google::protobuf::internal::WireFormatLite::Int32Size(data_size);\n"
  401. "}\n"
  402. "int cached_size = ::google::protobuf::internal::ToCachedSize(data_size);\n"
  403. "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
  404. "_$name$_cached_byte_size_ = cached_size;\n"
  405. "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
  406. "total_size += data_size;\n");
  407. } else {
  408. printer->Print(variables_,
  409. "total_size += $tag_size$ *\n"
  410. " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n"
  411. "total_size += data_size;\n");
  412. }
  413. printer->Outdent();
  414. printer->Print("}\n");
  415. }
  416. } // namespace cpp
  417. } // namespace compiler
  418. } // namespace protobuf
  419. } // namespace google