cpp_primitive_field.cc 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  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_, "$deprecated_attr$$type$ $name$() const;\n");
  103. printer->Annotate("name", descriptor_);
  104. printer->Print(variables_,
  105. "$deprecated_attr$void ${$set_$name$$}$($type$ value);\n");
  106. printer->Annotate("{", "}", descriptor_);
  107. }
  108. void PrimitiveFieldGenerator::
  109. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  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_, "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) const {
  184. printer->Print(variables_,
  185. "inline $type$ $classname$::$name$() const {\n"
  186. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  187. " if (has_$name$()) {\n"
  188. " return $field_member$;\n"
  189. " }\n"
  190. " return $default$;\n"
  191. "}\n"
  192. "inline void $classname$::set_$name$($type$ value) {\n"
  193. " if (!has_$name$()) {\n"
  194. " clear_$oneof_name$();\n"
  195. " set_has_$name$();\n"
  196. " }\n"
  197. " $field_member$ = value;\n"
  198. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  199. "}\n");
  200. }
  201. void PrimitiveOneofFieldGenerator::
  202. GenerateClearingCode(io::Printer* printer) const {
  203. printer->Print(variables_, "$field_member$ = $default$;\n");
  204. }
  205. void PrimitiveOneofFieldGenerator::
  206. GenerateSwappingCode(io::Printer* printer) const {
  207. // Don't print any swapping code. Swapping the union will swap this field.
  208. }
  209. void PrimitiveOneofFieldGenerator::
  210. GenerateConstructorCode(io::Printer* printer) const {
  211. printer->Print(variables_,
  212. "$ns$::_$classname$_default_instance_.$name$_ = $default$;\n");
  213. }
  214. void PrimitiveOneofFieldGenerator::
  215. GenerateMergeFromCodedStream(io::Printer* printer) const {
  216. printer->Print(variables_,
  217. "clear_$oneof_name$();\n"
  218. "DO_((::google::protobuf::internal::WireFormatLite::ReadPrimitive<\n"
  219. " $type$, $wire_format_field_type$>(\n"
  220. " input, &$field_member$)));\n"
  221. "set_has_$name$();\n");
  222. }
  223. // ===================================================================
  224. RepeatedPrimitiveFieldGenerator::RepeatedPrimitiveFieldGenerator(
  225. const FieldDescriptor* descriptor, const Options& options)
  226. : FieldGenerator(options), descriptor_(descriptor) {
  227. SetPrimitiveVariables(descriptor, &variables_, options);
  228. if (descriptor->is_packed()) {
  229. variables_["packed_reader"] = "ReadPackedPrimitive";
  230. variables_["repeated_reader"] = "ReadRepeatedPrimitiveNoInline";
  231. } else {
  232. variables_["packed_reader"] = "ReadPackedPrimitiveNoInline";
  233. variables_["repeated_reader"] = "ReadRepeatedPrimitive";
  234. }
  235. }
  236. RepeatedPrimitiveFieldGenerator::~RepeatedPrimitiveFieldGenerator() {}
  237. void RepeatedPrimitiveFieldGenerator::
  238. GeneratePrivateMembers(io::Printer* printer) const {
  239. printer->Print(variables_,
  240. "::google::protobuf::RepeatedField< $type$ > $name$_;\n");
  241. if (descriptor_->is_packed() &&
  242. HasGeneratedMethods(descriptor_->file(), options_)) {
  243. printer->Print(variables_,
  244. "mutable int _$name$_cached_byte_size_;\n");
  245. }
  246. }
  247. void RepeatedPrimitiveFieldGenerator::
  248. GenerateAccessorDeclarations(io::Printer* printer) const {
  249. printer->Print(variables_,
  250. "$deprecated_attr$$type$ $name$(int index) const;\n");
  251. printer->Annotate("name", descriptor_);
  252. printer->Print(
  253. variables_,
  254. "$deprecated_attr$void ${$set_$name$$}$(int index, $type$ value);\n");
  255. printer->Annotate("{", "}", descriptor_);
  256. printer->Print(variables_,
  257. "$deprecated_attr$void ${$add_$name$$}$($type$ value);\n");
  258. printer->Annotate("{", "}", descriptor_);
  259. printer->Print(variables_,
  260. "$deprecated_attr$const ::google::protobuf::RepeatedField< $type$ >&\n"
  261. " $name$() const;\n");
  262. printer->Annotate("name", descriptor_);
  263. printer->Print(variables_,
  264. "$deprecated_attr$::google::protobuf::RepeatedField< $type$ >*\n"
  265. " ${$mutable_$name$$}$();\n");
  266. printer->Annotate("{", "}", descriptor_);
  267. }
  268. void RepeatedPrimitiveFieldGenerator::
  269. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  270. printer->Print(variables_,
  271. "inline $type$ $classname$::$name$(int index) const {\n"
  272. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  273. " return $name$_.Get(index);\n"
  274. "}\n"
  275. "inline void $classname$::set_$name$(int index, $type$ value) {\n"
  276. " $name$_.Set(index, value);\n"
  277. " // @@protoc_insertion_point(field_set:$full_name$)\n"
  278. "}\n"
  279. "inline void $classname$::add_$name$($type$ value) {\n"
  280. " $name$_.Add(value);\n"
  281. " // @@protoc_insertion_point(field_add:$full_name$)\n"
  282. "}\n"
  283. "inline const ::google::protobuf::RepeatedField< $type$ >&\n"
  284. "$classname$::$name$() const {\n"
  285. " // @@protoc_insertion_point(field_list:$full_name$)\n"
  286. " return $name$_;\n"
  287. "}\n"
  288. "inline ::google::protobuf::RepeatedField< $type$ >*\n"
  289. "$classname$::mutable_$name$() {\n"
  290. " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
  291. " return &$name$_;\n"
  292. "}\n");
  293. }
  294. void RepeatedPrimitiveFieldGenerator::
  295. GenerateClearingCode(io::Printer* printer) const {
  296. printer->Print(variables_, "$name$_.Clear();\n");
  297. }
  298. void RepeatedPrimitiveFieldGenerator::
  299. GenerateMergingCode(io::Printer* printer) const {
  300. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  301. }
  302. void RepeatedPrimitiveFieldGenerator::
  303. GenerateSwappingCode(io::Printer* printer) const {
  304. printer->Print(variables_, "$name$_.InternalSwap(&other->$name$_);\n");
  305. }
  306. void RepeatedPrimitiveFieldGenerator::
  307. GenerateConstructorCode(io::Printer* printer) const {
  308. // Not needed for repeated fields.
  309. }
  310. void RepeatedPrimitiveFieldGenerator::
  311. GenerateCopyConstructorCode(io::Printer* printer) const {
  312. printer->Print(variables_, "$name$_.CopyFrom(from.$name$_);\n");
  313. }
  314. void RepeatedPrimitiveFieldGenerator::
  315. GenerateMergeFromCodedStream(io::Printer* printer) const {
  316. printer->Print(variables_,
  317. "DO_((::google::protobuf::internal::WireFormatLite::$repeated_reader$<\n"
  318. " $type$, $wire_format_field_type$>(\n"
  319. " $tag_size$, $tag$u, input, this->mutable_$name$())));\n");
  320. }
  321. void RepeatedPrimitiveFieldGenerator::
  322. GenerateMergeFromCodedStreamWithPacking(io::Printer* printer) const {
  323. printer->Print(variables_,
  324. "DO_((::google::protobuf::internal::WireFormatLite::$packed_reader$<\n"
  325. " $type$, $wire_format_field_type$>(\n"
  326. " input, this->mutable_$name$())));\n");
  327. }
  328. void RepeatedPrimitiveFieldGenerator::
  329. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  330. bool array_written = false;
  331. if (descriptor_->is_packed()) {
  332. // Write the tag and the size.
  333. printer->Print(variables_,
  334. "if (this->$name$_size() > 0) {\n"
  335. " ::google::protobuf::internal::WireFormatLite::WriteTag("
  336. "$number$, "
  337. "::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED, "
  338. "output);\n"
  339. " output->WriteVarint32(static_cast< ::google::protobuf::uint32>(\n"
  340. " _$name$_cached_byte_size_));\n");
  341. if (FixedSize(descriptor_->type()) > 0) {
  342. // TODO(ckennelly): Use RepeatedField<T>::unsafe_data() via
  343. // WireFormatLite to access the contents of this->$name$_ to save a branch
  344. // here.
  345. printer->Print(variables_,
  346. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$Array(\n"
  347. " this->$name$().data(), this->$name$_size(), output);\n");
  348. array_written = true; // Wrote array all at once
  349. }
  350. printer->Print(variables_, "}\n");
  351. }
  352. if (!array_written) {
  353. printer->Print(variables_,
  354. "for (int i = 0, n = this->$name$_size(); i < n; i++) {\n");
  355. if (descriptor_->is_packed()) {
  356. printer->Print(variables_,
  357. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$NoTag(\n"
  358. " this->$name$(i), output);\n");
  359. } else {
  360. printer->Print(variables_,
  361. " ::google::protobuf::internal::WireFormatLite::Write$declared_type$(\n"
  362. " $number$, this->$name$(i), output);\n");
  363. }
  364. printer->Print("}\n");
  365. }
  366. }
  367. void RepeatedPrimitiveFieldGenerator::
  368. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  369. if (descriptor_->is_packed()) {
  370. // Write the tag and the size.
  371. printer->Print(variables_,
  372. "if (this->$name$_size() > 0) {\n"
  373. " target = ::google::protobuf::internal::WireFormatLite::WriteTagToArray(\n"
  374. " $number$,\n"
  375. " ::google::protobuf::internal::WireFormatLite::WIRETYPE_LENGTH_DELIMITED,\n"
  376. " target);\n"
  377. " target = ::google::protobuf::io::CodedOutputStream::WriteVarint32ToArray(\n"
  378. " static_cast< ::google::protobuf::int32>(\n"
  379. " _$name$_cached_byte_size_), target);\n"
  380. " target = ::google::protobuf::internal::WireFormatLite::\n"
  381. " Write$declared_type$NoTagToArray(this->$name$_, target);\n"
  382. "}\n");
  383. } else {
  384. printer->Print(variables_,
  385. "target = ::google::protobuf::internal::WireFormatLite::\n"
  386. " Write$declared_type$ToArray($number$, this->$name$_, target);\n");
  387. }
  388. }
  389. void RepeatedPrimitiveFieldGenerator::
  390. GenerateByteSize(io::Printer* printer) const {
  391. printer->Print(variables_, "{\n");
  392. printer->Indent();
  393. int fixed_size = FixedSize(descriptor_->type());
  394. if (fixed_size == -1) {
  395. printer->Print(variables_,
  396. "size_t data_size = ::google::protobuf::internal::WireFormatLite::\n"
  397. " $declared_type$Size(this->$name$_);\n");
  398. } else {
  399. printer->Print(variables_,
  400. "unsigned int count = static_cast<unsigned int>(this->$name$_size());\n"
  401. "size_t data_size = $fixed_size$UL * count;\n");
  402. }
  403. if (descriptor_->is_packed()) {
  404. printer->Print(variables_,
  405. "if (data_size > 0) {\n"
  406. " total_size += $tag_size$ +\n"
  407. " ::google::protobuf::internal::WireFormatLite::Int32Size(\n"
  408. " static_cast< ::google::protobuf::int32>(data_size));\n"
  409. "}\n"
  410. "int cached_size = ::google::protobuf::internal::ToCachedSize(data_size);\n"
  411. "GOOGLE_SAFE_CONCURRENT_WRITES_BEGIN();\n"
  412. "_$name$_cached_byte_size_ = cached_size;\n"
  413. "GOOGLE_SAFE_CONCURRENT_WRITES_END();\n"
  414. "total_size += data_size;\n");
  415. } else {
  416. printer->Print(variables_,
  417. "total_size += $tag_size$ *\n"
  418. " ::google::protobuf::internal::FromIntSize(this->$name$_size());\n"
  419. "total_size += data_size;\n");
  420. }
  421. printer->Outdent();
  422. printer->Print("}\n");
  423. }
  424. } // namespace cpp
  425. } // namespace compiler
  426. } // namespace protobuf
  427. } // namespace google