cpp_message_field.cc 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836
  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_message_field.h>
  34. #include <google/protobuf/compiler/cpp/cpp_helpers.h>
  35. #include <google/protobuf/io/printer.h>
  36. #include <google/protobuf/stubs/strutil.h>
  37. namespace google {
  38. namespace protobuf {
  39. namespace compiler {
  40. namespace cpp {
  41. namespace {
  42. // When we are generating code for implicit weak fields, we need to insert some
  43. // additional casts. These functions return the casted expression if
  44. // implicit_weak_field is true but otherwise return the original expression.
  45. // Ordinarily a static_cast is enough to cast google::protobuf::MessageLite* to a class
  46. // deriving from it, but we need a reinterpret_cast in cases where the generated
  47. // message is forward-declared but its full definition is not visible.
  48. string StaticCast(const string& type, const string& expression,
  49. bool implicit_weak_field) {
  50. if (implicit_weak_field) {
  51. return "static_cast< " + type + " >(" + expression + ")";
  52. } else {
  53. return expression;
  54. }
  55. }
  56. string ReinterpretCast(const string& type, const string& expression,
  57. bool implicit_weak_field) {
  58. if (implicit_weak_field) {
  59. return "reinterpret_cast< " + type + " >(" + expression + ")";
  60. } else {
  61. return expression;
  62. }
  63. }
  64. void SetMessageVariables(const FieldDescriptor* descriptor,
  65. const Options& options, bool implicit_weak,
  66. std::map<string, string>* variables) {
  67. SetCommonFieldVariables(descriptor, variables, options);
  68. (*variables)["type"] = FieldMessageTypeName(descriptor);
  69. (*variables)["casted_member"] = ReinterpretCast(
  70. (*variables)["type"] + "*", (*variables)["name"] + "_", implicit_weak);
  71. (*variables)["type_default_instance"] =
  72. DefaultInstanceName(descriptor->message_type());
  73. (*variables)["type_reference_function"] =
  74. implicit_weak
  75. ? (" " + ReferenceFunctionName(descriptor->message_type()) + "();\n")
  76. : "";
  77. (*variables)["stream_writer"] =
  78. (*variables)["declared_type"] +
  79. (HasFastArraySerialization(descriptor->message_type()->file(), options)
  80. ? "MaybeToArray"
  81. : "");
  82. // NOTE: Escaped here to unblock proto1->proto2 migration.
  83. // TODO(liujisi): Extend this to apply for other conflicting methods.
  84. (*variables)["release_name"] =
  85. SafeFunctionName(descriptor->containing_type(),
  86. descriptor, "release_");
  87. (*variables)["full_name"] = descriptor->full_name();
  88. }
  89. } // namespace
  90. // ===================================================================
  91. MessageFieldGenerator::MessageFieldGenerator(const FieldDescriptor* descriptor,
  92. const Options& options,
  93. SCCAnalyzer* scc_analyzer)
  94. : FieldGenerator(options),
  95. descriptor_(descriptor),
  96. implicit_weak_field_(
  97. IsImplicitWeakField(descriptor, options, scc_analyzer)) {
  98. SetMessageVariables(descriptor, options, implicit_weak_field_, &variables_);
  99. }
  100. MessageFieldGenerator::~MessageFieldGenerator() {}
  101. void MessageFieldGenerator::
  102. GeneratePrivateMembers(io::Printer* printer) const {
  103. if (implicit_weak_field_) {
  104. printer->Print(variables_, "::google::protobuf::MessageLite* $name$_;\n");
  105. } else {
  106. printer->Print(variables_, "$type$* $name$_;\n");
  107. }
  108. }
  109. void MessageFieldGenerator::
  110. GenerateAccessorDeclarations(io::Printer* printer) const {
  111. if (implicit_weak_field_) {
  112. // These private accessors are used by MergeFrom and
  113. // MergePartialFromCodedStream, and their purpose is to provide access to
  114. // the field without creating a strong dependency on the message type.
  115. printer->Print(variables_,
  116. "private:\n"
  117. "const ::google::protobuf::MessageLite& _internal_$name$() const;\n"
  118. "::google::protobuf::MessageLite* _internal_mutable_$name$();\n"
  119. "public:\n");
  120. } else {
  121. // This inline accessor directly returns member field and is used in
  122. // Serialize such that AFDO profile correctly captures access information to
  123. // message fields under serialize.
  124. printer->Print(variables_,
  125. "private:\n"
  126. "const $type$& _internal_$name$() const;\n"
  127. "public:\n");
  128. }
  129. printer->Print(variables_,
  130. "$deprecated_attr$const $type$& $name$() const;\n");
  131. printer->Annotate("name", descriptor_);
  132. printer->Print(variables_, "$deprecated_attr$$type$* $release_name$();\n");
  133. printer->Annotate("release_name", descriptor_);
  134. printer->Print(variables_,
  135. "$deprecated_attr$$type$* ${$mutable_$name$$}$();\n");
  136. printer->Annotate("{", "}", descriptor_);
  137. printer->Print(variables_,
  138. "$deprecated_attr$void ${$set_allocated_$name$$}$"
  139. "($type$* $name$);\n");
  140. printer->Annotate("{", "}", descriptor_);
  141. if (SupportsArenas(descriptor_)) {
  142. printer->Print(variables_,
  143. "$deprecated_attr$void "
  144. "${$unsafe_arena_set_allocated_$name$$}$(\n"
  145. " $type$* $name$);\n");
  146. printer->Annotate("{", "}", descriptor_);
  147. printer->Print(
  148. variables_,
  149. "$deprecated_attr$$type$* ${$unsafe_arena_release_$name$$}$();\n");
  150. printer->Annotate("{", "}", descriptor_);
  151. }
  152. }
  153. void MessageFieldGenerator::GenerateNonInlineAccessorDefinitions(
  154. io::Printer* printer) const {
  155. if (implicit_weak_field_) {
  156. printer->Print(variables_,
  157. "const ::google::protobuf::MessageLite& $classname$::_internal_$name$() const {\n"
  158. " if ($name$_ != NULL) {\n"
  159. " return *$name$_;\n"
  160. " } else if (&$type_default_instance$ != NULL) {\n"
  161. " return *reinterpret_cast<const ::google::protobuf::MessageLite*>(\n"
  162. " &$type_default_instance$);\n"
  163. " } else {\n"
  164. " return "
  165. "*::google::protobuf::internal::ImplicitWeakMessage::default_instance();\n"
  166. " }\n"
  167. "}\n");
  168. }
  169. if (SupportsArenas(descriptor_)) {
  170. if (implicit_weak_field_) {
  171. printer->Print(variables_,
  172. "::google::protobuf::MessageLite* $classname$::_internal_mutable_$name$() {\n"
  173. " $set_hasbit$\n"
  174. " if ($name$_ == NULL) {\n"
  175. " if (&$type_default_instance$ == NULL) {\n"
  176. " $name$_ = ::google::protobuf::Arena::CreateMessage<\n"
  177. " ::google::protobuf::internal::ImplicitWeakMessage>(\n"
  178. " GetArenaNoVirtual());\n"
  179. " } else {\n"
  180. " $name$_ = reinterpret_cast<const ::google::protobuf::MessageLite*>(\n"
  181. " &$type_default_instance$)->New(GetArenaNoVirtual());\n"
  182. " }\n"
  183. " }\n"
  184. " return $name$_;\n"
  185. "}\n");
  186. }
  187. printer->Print(variables_,
  188. "void $classname$::unsafe_arena_set_allocated_$name$(\n"
  189. " $type$* $name$) {\n"
  190. // If we're not on an arena, free whatever we were holding before.
  191. // (If we are on arena, we can just forget the earlier pointer.)
  192. " if (GetArenaNoVirtual() == NULL) {\n"
  193. " delete $name$_;\n"
  194. " }\n"
  195. " $name$_ = $name$;\n"
  196. " if ($name$) {\n"
  197. " $set_hasbit$\n"
  198. " } else {\n"
  199. " $clear_hasbit$\n"
  200. " }\n"
  201. " // @@protoc_insertion_point(field_unsafe_arena_set_allocated"
  202. ":$full_name$)\n"
  203. "}\n");
  204. } else if (implicit_weak_field_) {
  205. printer->Print(variables_,
  206. "::google::protobuf::MessageLite* $classname$::_internal_mutable_$name$() {\n"
  207. " $set_hasbit$\n"
  208. " if ($name$_ == NULL) {\n"
  209. " if (&$type_default_instance$ == NULL) {\n"
  210. " $name$_ = new ::google::protobuf::internal::ImplicitWeakMessage;\n"
  211. " } else {\n"
  212. " $name$_ = reinterpret_cast<const ::google::protobuf::MessageLite*>(\n"
  213. " &$type_default_instance$)->New();\n"
  214. " }\n"
  215. " }\n"
  216. " return $name$_;\n"
  217. "}\n");
  218. }
  219. }
  220. void MessageFieldGenerator::
  221. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  222. if (!implicit_weak_field_) {
  223. printer->Print(variables_,
  224. "inline const $type$& $classname$::_internal_$name$() const {\n"
  225. " return *$field_member$;\n"
  226. "}\n");
  227. }
  228. printer->Print(variables_,
  229. "inline const $type$& $classname$::$name$() const {\n"
  230. " const $type$* p = $casted_member$;\n"
  231. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  232. " return p != NULL ? *p : *reinterpret_cast<const $type$*>(\n"
  233. " &$type_default_instance$);\n"
  234. "}\n");
  235. printer->Print(variables_,
  236. "inline $type$* $classname$::$release_name$() {\n"
  237. " // @@protoc_insertion_point(field_release:$full_name$)\n"
  238. "$type_reference_function$"
  239. " $clear_hasbit$\n"
  240. " $type$* temp = $casted_member$;\n");
  241. if (SupportsArenas(descriptor_)) {
  242. printer->Print(variables_,
  243. " if (GetArenaNoVirtual() != NULL) {\n"
  244. " temp = ::google::protobuf::internal::DuplicateIfNonNull(temp);\n"
  245. " }\n");
  246. }
  247. printer->Print(variables_,
  248. " $name$_ = NULL;\n"
  249. " return temp;\n"
  250. "}\n");
  251. if (SupportsArenas(descriptor_)) {
  252. printer->Print(variables_,
  253. "inline $type$* $classname$::unsafe_arena_release_$name$() {\n"
  254. " // @@protoc_insertion_point(field_unsafe_arena_release:$full_name$)\n"
  255. "$type_reference_function$"
  256. " $clear_hasbit$\n"
  257. " $type$* temp = $casted_member$;\n"
  258. " $name$_ = NULL;\n"
  259. " return temp;\n"
  260. "}\n");
  261. }
  262. printer->Print(variables_,
  263. "inline $type$* $classname$::mutable_$name$() {\n"
  264. " $set_hasbit$\n"
  265. " if ($name$_ == NULL) {\n"
  266. " auto* p = CreateMaybeMessage<$type$>(GetArenaNoVirtual());\n");
  267. if (implicit_weak_field_) {
  268. printer->Print(variables_,
  269. " $name$_ = reinterpret_cast<::google::protobuf::MessageLite*>(p);\n");
  270. } else {
  271. printer->Print(variables_,
  272. " $name$_ = p;\n");
  273. }
  274. printer->Print(variables_,
  275. " }\n"
  276. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  277. " return $casted_member$;\n"
  278. "}\n");
  279. // We handle the most common case inline, and delegate less common cases to
  280. // the slow fallback function.
  281. printer->Print(variables_,
  282. "inline void $classname$::set_allocated_$name$($type$* $name$) {\n"
  283. " ::google::protobuf::Arena* message_arena = GetArenaNoVirtual();\n");
  284. printer->Print(variables_,
  285. " if (message_arena == NULL) {\n");
  286. if (IsCrossFileMessage(descriptor_)) {
  287. printer->Print(variables_,
  288. " delete reinterpret_cast< ::google::protobuf::MessageLite*>($name$_);\n");
  289. } else {
  290. printer->Print(variables_,
  291. " delete $name$_;\n");
  292. }
  293. printer->Print(variables_,
  294. " }\n"
  295. " if ($name$) {\n");
  296. if (SupportsArenas(descriptor_->message_type()) &&
  297. IsCrossFileMessage(descriptor_)) {
  298. // We have to read the arena through the virtual method, because the type
  299. // isn't defined in this file.
  300. printer->Print(variables_,
  301. " ::google::protobuf::Arena* submessage_arena =\n"
  302. " reinterpret_cast<::google::protobuf::MessageLite*>($name$)->GetArena();\n");
  303. } else if (!SupportsArenas(descriptor_->message_type())) {
  304. printer->Print(variables_,
  305. " ::google::protobuf::Arena* submessage_arena = NULL;\n");
  306. } else {
  307. printer->Print(variables_,
  308. " ::google::protobuf::Arena* submessage_arena =\n"
  309. " ::google::protobuf::Arena::GetArena($name$);\n");
  310. }
  311. printer->Print(variables_,
  312. " if (message_arena != submessage_arena) {\n"
  313. " $name$ = ::google::protobuf::internal::GetOwnedMessage(\n"
  314. " message_arena, $name$, submessage_arena);\n"
  315. " }\n"
  316. " $set_hasbit$\n"
  317. " } else {\n"
  318. " $clear_hasbit$\n"
  319. " }\n");
  320. if (implicit_weak_field_) {
  321. printer->Print(variables_,
  322. " $name$_ = reinterpret_cast<MessageLite*>($name$);\n");
  323. } else {
  324. printer->Print(variables_,
  325. " $name$_ = $name$;\n");
  326. }
  327. printer->Print(variables_,
  328. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  329. "}\n");
  330. }
  331. void MessageFieldGenerator::
  332. GenerateClearingCode(io::Printer* printer) const {
  333. if (!HasFieldPresence(descriptor_->file())) {
  334. // If we don't have has-bits, message presence is indicated only by ptr !=
  335. // NULL. Thus on clear, we need to delete the object.
  336. printer->Print(variables_,
  337. "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n"
  338. " delete $name$_;\n"
  339. "}\n"
  340. "$name$_ = NULL;\n");
  341. } else {
  342. printer->Print(variables_,
  343. "if ($name$_ != NULL) $name$_->Clear();\n");
  344. }
  345. }
  346. void MessageFieldGenerator::
  347. GenerateMessageClearingCode(io::Printer* printer) const {
  348. if (!HasFieldPresence(descriptor_->file())) {
  349. // If we don't have has-bits, message presence is indicated only by ptr !=
  350. // NULL. Thus on clear, we need to delete the object.
  351. printer->Print(variables_,
  352. "if (GetArenaNoVirtual() == NULL && $name$_ != NULL) {\n"
  353. " delete $name$_;\n"
  354. "}\n"
  355. "$name$_ = NULL;\n");
  356. } else {
  357. printer->Print(variables_,
  358. "GOOGLE_DCHECK($name$_ != NULL);\n"
  359. "$name$_->Clear();\n");
  360. }
  361. }
  362. void MessageFieldGenerator::
  363. GenerateMergingCode(io::Printer* printer) const {
  364. if (implicit_weak_field_) {
  365. printer->Print(variables_,
  366. "_internal_mutable_$name$()->CheckTypeAndMergeFrom(\n"
  367. " from._internal_$name$());\n");
  368. } else {
  369. printer->Print(variables_,
  370. "mutable_$name$()->$type$::MergeFrom(from.$name$());\n");
  371. }
  372. }
  373. void MessageFieldGenerator::
  374. GenerateSwappingCode(io::Printer* printer) const {
  375. printer->Print(variables_, "swap($name$_, other->$name$_);\n");
  376. }
  377. void MessageFieldGenerator::
  378. GenerateDestructorCode(io::Printer* printer) const {
  379. // TODO(gerbens) Remove this when we don't need to destruct default instances.
  380. // In google3 a default instance will never get deleted so we don't need to
  381. // worry about that but in opensource protobuf default instances are deleted
  382. // in shutdown process and we need to take special care when handling them.
  383. printer->Print(variables_,
  384. "if (this != internal_default_instance()) ");
  385. printer->Print(variables_, "delete $name$_;\n");
  386. }
  387. void MessageFieldGenerator::
  388. GenerateConstructorCode(io::Printer* printer) const {
  389. printer->Print(variables_, "$name$_ = NULL;\n");
  390. }
  391. void MessageFieldGenerator::
  392. GenerateCopyConstructorCode(io::Printer* printer) const {
  393. printer->Print(variables_,
  394. "if (from.has_$name$()) {\n"
  395. " $name$_ = new $type$(*from.$name$_);\n"
  396. "} else {\n"
  397. " $name$_ = NULL;\n"
  398. "}\n");
  399. }
  400. void MessageFieldGenerator::
  401. GenerateMergeFromCodedStream(io::Printer* printer) const {
  402. if (implicit_weak_field_) {
  403. printer->Print(variables_,
  404. "DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(\n"
  405. " input, _internal_mutable_$name$()));\n");
  406. } else if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
  407. printer->Print(variables_,
  408. "DO_(::google::protobuf::internal::WireFormatLite::ReadMessage(\n"
  409. " input, mutable_$name$()));\n");
  410. } else {
  411. printer->Print(variables_,
  412. "DO_(::google::protobuf::internal::WireFormatLite::ReadGroup(\n"
  413. " $number$, input, mutable_$name$()));\n");
  414. }
  415. }
  416. void MessageFieldGenerator::
  417. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  418. printer->Print(variables_,
  419. "::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
  420. " $number$, this->_internal_$name$(), output);\n");
  421. }
  422. void MessageFieldGenerator::
  423. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  424. printer->Print(variables_,
  425. "target = ::google::protobuf::internal::WireFormatLite::\n"
  426. " InternalWrite$declared_type$ToArray(\n"
  427. " $number$, this->_internal_$name$(), deterministic, target);\n");
  428. }
  429. void MessageFieldGenerator::
  430. GenerateByteSize(io::Printer* printer) const {
  431. printer->Print(variables_,
  432. "total_size += $tag_size$ +\n"
  433. " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n"
  434. " *$field_member$);\n");
  435. }
  436. // ===================================================================
  437. MessageOneofFieldGenerator::MessageOneofFieldGenerator(
  438. const FieldDescriptor* descriptor, const Options& options,
  439. SCCAnalyzer* scc_analyzer)
  440. : MessageFieldGenerator(descriptor, options, scc_analyzer) {
  441. SetCommonOneofFieldVariables(descriptor, &variables_);
  442. }
  443. MessageOneofFieldGenerator::~MessageOneofFieldGenerator() {}
  444. void MessageOneofFieldGenerator::GenerateNonInlineAccessorDefinitions(
  445. io::Printer* printer) const {
  446. printer->Print(variables_,
  447. "void $classname$::set_allocated_$name$($type$* $name$) {\n"
  448. " ::google::protobuf::Arena* message_arena = GetArenaNoVirtual();\n"
  449. " clear_$oneof_name$();\n"
  450. " if ($name$) {\n");
  451. if (SupportsArenas(descriptor_->message_type()) &&
  452. descriptor_->file() != descriptor_->message_type()->file()) {
  453. // We have to read the arena through the virtual method, because the type
  454. // isn't defined in this file.
  455. printer->Print(variables_,
  456. " ::google::protobuf::Arena* submessage_arena =\n"
  457. " reinterpret_cast<::google::protobuf::MessageLite*>($name$)->GetArena();\n");
  458. } else if (!SupportsArenas(descriptor_->message_type())) {
  459. printer->Print(variables_,
  460. " ::google::protobuf::Arena* submessage_arena = NULL;\n");
  461. } else {
  462. printer->Print(variables_,
  463. " ::google::protobuf::Arena* submessage_arena =\n"
  464. " ::google::protobuf::Arena::GetArena($name$);\n");
  465. }
  466. printer->Print(variables_,
  467. " if (message_arena != submessage_arena) {\n"
  468. " $name$ = ::google::protobuf::internal::GetOwnedMessage(\n"
  469. " message_arena, $name$, submessage_arena);\n"
  470. " }\n"
  471. " set_has_$name$();\n"
  472. " $field_member$ = $name$;\n"
  473. " }\n"
  474. " // @@protoc_insertion_point(field_set_allocated:$full_name$)\n"
  475. "}\n");
  476. }
  477. void MessageOneofFieldGenerator::
  478. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  479. if (!implicit_weak_field_) {
  480. printer->Print(variables_,
  481. "inline const $type$& $classname$::_internal_$name$() const {\n"
  482. " return *$field_member$;\n"
  483. "}\n");
  484. }
  485. printer->Print(variables_,
  486. "inline $type$* $classname$::$release_name$() {\n"
  487. " // @@protoc_insertion_point(field_release:$full_name$)\n"
  488. " if (has_$name$()) {\n"
  489. " clear_has_$oneof_name$();\n"
  490. " $type$* temp = $field_member$;\n");
  491. if (SupportsArenas(descriptor_)) {
  492. printer->Print(variables_,
  493. " if (GetArenaNoVirtual() != NULL) {\n"
  494. " temp = ::google::protobuf::internal::DuplicateIfNonNull(temp);\n"
  495. " }\n");
  496. }
  497. printer->Print(variables_,
  498. " $field_member$ = NULL;\n"
  499. " return temp;\n"
  500. " } else {\n"
  501. " return NULL;\n"
  502. " }\n"
  503. "}\n");
  504. printer->Print(variables_,
  505. "inline const $type$& $classname$::$name$() const {\n"
  506. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  507. " return has_$name$()\n"
  508. " ? *$field_member$\n"
  509. " : *reinterpret_cast< $type$*>(&$type_default_instance$);\n"
  510. "}\n");
  511. if (SupportsArenas(descriptor_)) {
  512. printer->Print(variables_,
  513. "inline $type$* $classname$::unsafe_arena_release_$name$() {\n"
  514. " // @@protoc_insertion_point(field_unsafe_arena_release"
  515. ":$full_name$)\n"
  516. " if (has_$name$()) {\n"
  517. " clear_has_$oneof_name$();\n"
  518. " $type$* temp = $field_member$;\n"
  519. " $field_member$ = NULL;\n"
  520. " return temp;\n"
  521. " } else {\n"
  522. " return NULL;\n"
  523. " }\n"
  524. "}\n"
  525. "inline void $classname$::unsafe_arena_set_allocated_$name$"
  526. "($type$* $name$) {\n"
  527. // We rely on the oneof clear method to free the earlier contents of this
  528. // oneof. We can directly use the pointer we're given to set the new
  529. // value.
  530. " clear_$oneof_name$();\n"
  531. " if ($name$) {\n"
  532. " set_has_$name$();\n"
  533. " $field_member$ = $name$;\n"
  534. " }\n"
  535. " // @@protoc_insertion_point(field_unsafe_arena_set_allocated:"
  536. "$full_name$)\n"
  537. "}\n");
  538. }
  539. printer->Print(variables_,
  540. "inline $type$* $classname$::mutable_$name$() {\n"
  541. " if (!has_$name$()) {\n"
  542. " clear_$oneof_name$();\n"
  543. " set_has_$name$();\n"
  544. " $field_member$ = CreateMaybeMessage< $type$ >(\n"
  545. " GetArenaNoVirtual());\n"
  546. " }\n"
  547. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  548. " return $field_member$;\n"
  549. "}\n");
  550. }
  551. void MessageOneofFieldGenerator::
  552. GenerateClearingCode(io::Printer* printer) const {
  553. if (SupportsArenas(descriptor_)) {
  554. printer->Print(variables_,
  555. "if (GetArenaNoVirtual() == NULL) {\n"
  556. " delete $field_member$;\n"
  557. "}\n");
  558. } else {
  559. printer->Print(variables_,
  560. "delete $field_member$;\n");
  561. }
  562. }
  563. void MessageOneofFieldGenerator::
  564. GenerateMessageClearingCode(io::Printer* printer) const {
  565. GenerateClearingCode(printer);
  566. }
  567. void MessageOneofFieldGenerator::
  568. GenerateSwappingCode(io::Printer* printer) const {
  569. // Don't print any swapping code. Swapping the union will swap this field.
  570. }
  571. void MessageOneofFieldGenerator::
  572. GenerateDestructorCode(io::Printer* printer) const {
  573. // We inherit from MessageFieldGenerator, so we need to override the default
  574. // behavior.
  575. }
  576. void MessageOneofFieldGenerator::
  577. GenerateConstructorCode(io::Printer* printer) const {
  578. // Don't print any constructor code. The field is in a union. We allocate
  579. // space only when this field is used.
  580. }
  581. // ===================================================================
  582. RepeatedMessageFieldGenerator::RepeatedMessageFieldGenerator(
  583. const FieldDescriptor* descriptor, const Options& options,
  584. SCCAnalyzer* scc_analyzer)
  585. : FieldGenerator(options),
  586. descriptor_(descriptor),
  587. implicit_weak_field_(
  588. IsImplicitWeakField(descriptor, options, scc_analyzer)) {
  589. SetMessageVariables(descriptor, options, implicit_weak_field_, &variables_);
  590. }
  591. RepeatedMessageFieldGenerator::~RepeatedMessageFieldGenerator() {}
  592. void RepeatedMessageFieldGenerator::
  593. GeneratePrivateMembers(io::Printer* printer) const {
  594. printer->Print(variables_,
  595. "::google::protobuf::RepeatedPtrField< $type$ > $name$_;\n");
  596. }
  597. void RepeatedMessageFieldGenerator::
  598. GenerateAccessorDeclarations(io::Printer* printer) const {
  599. printer->Print(variables_,
  600. "$deprecated_attr$$type$* ${$mutable_$name$$}$(int index);\n");
  601. printer->Annotate("{", "}", descriptor_);
  602. printer->Print(variables_,
  603. "$deprecated_attr$::google::protobuf::RepeatedPtrField< $type$ >*\n"
  604. " ${$mutable_$name$$}$();\n");
  605. printer->Annotate("{", "}", descriptor_);
  606. printer->Print(variables_,
  607. "$deprecated_attr$const $type$& $name$(int index) const;\n");
  608. printer->Annotate("name", descriptor_);
  609. printer->Print(variables_, "$deprecated_attr$$type$* ${$add_$name$$}$();\n");
  610. printer->Annotate("{", "}", descriptor_);
  611. printer->Print(variables_,
  612. "$deprecated_attr$const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  613. " $name$() const;\n");
  614. printer->Annotate("name", descriptor_);
  615. }
  616. void RepeatedMessageFieldGenerator::
  617. GenerateInlineAccessorDefinitions(io::Printer* printer) const {
  618. printer->Print(variables_,
  619. "inline $type$* $classname$::mutable_$name$(int index) {\n"
  620. // TODO(dlj): move insertion points
  621. " // @@protoc_insertion_point(field_mutable:$full_name$)\n"
  622. "$type_reference_function$"
  623. " return $name$_.Mutable(index);\n"
  624. "}\n"
  625. "inline ::google::protobuf::RepeatedPtrField< $type$ >*\n"
  626. "$classname$::mutable_$name$() {\n"
  627. " // @@protoc_insertion_point(field_mutable_list:$full_name$)\n"
  628. "$type_reference_function$"
  629. " return &$name$_;\n"
  630. "}\n");
  631. if (options_.safe_boundary_check) {
  632. printer->Print(variables_,
  633. "inline const $type$& $classname$::$name$(int index) const {\n"
  634. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  635. " return $name$_.InternalCheckedGet(index,\n"
  636. " *reinterpret_cast<const $type$*>(&$type_default_instance$));\n"
  637. "}\n");
  638. } else {
  639. printer->Print(variables_,
  640. "inline const $type$& $classname$::$name$(int index) const {\n"
  641. " // @@protoc_insertion_point(field_get:$full_name$)\n"
  642. "$type_reference_function$"
  643. " return $name$_.Get(index);\n"
  644. "}\n");
  645. }
  646. printer->Print(variables_,
  647. "inline $type$* $classname$::add_$name$() {\n"
  648. " // @@protoc_insertion_point(field_add:$full_name$)\n"
  649. " return $name$_.Add();\n"
  650. "}\n");
  651. printer->Print(variables_,
  652. "inline const ::google::protobuf::RepeatedPtrField< $type$ >&\n"
  653. "$classname$::$name$() const {\n"
  654. " // @@protoc_insertion_point(field_list:$full_name$)\n"
  655. "$type_reference_function$"
  656. " return $name$_;\n"
  657. "}\n");
  658. }
  659. void RepeatedMessageFieldGenerator::
  660. GenerateClearingCode(io::Printer* printer) const {
  661. if (implicit_weak_field_) {
  662. printer->Print(
  663. variables_,
  664. "CastToBase(&$name$_)->Clear<"
  665. "::google::protobuf::internal::ImplicitWeakTypeHandler<$type$>>();\n");
  666. } else {
  667. printer->Print(variables_, "$name$_.Clear();\n");
  668. }
  669. }
  670. void RepeatedMessageFieldGenerator::
  671. GenerateMergingCode(io::Printer* printer) const {
  672. if (implicit_weak_field_) {
  673. printer->Print(
  674. variables_,
  675. "CastToBase(&$name$_)->MergeFrom<"
  676. "::google::protobuf::internal::ImplicitWeakTypeHandler<$type$>>(CastToBase("
  677. "from.$name$_));\n");
  678. } else {
  679. printer->Print(variables_, "$name$_.MergeFrom(from.$name$_);\n");
  680. }
  681. }
  682. void RepeatedMessageFieldGenerator::
  683. GenerateSwappingCode(io::Printer* printer) const {
  684. printer->Print(
  685. variables_,
  686. "CastToBase(&$name$_)->InternalSwap(CastToBase(&other->$name$_));\n");
  687. }
  688. void RepeatedMessageFieldGenerator::
  689. GenerateConstructorCode(io::Printer* printer) const {
  690. // Not needed for repeated fields.
  691. }
  692. void RepeatedMessageFieldGenerator::
  693. GenerateMergeFromCodedStream(io::Printer* printer) const {
  694. if (descriptor_->type() == FieldDescriptor::TYPE_MESSAGE) {
  695. if (implicit_weak_field_) {
  696. printer->Print(variables_,
  697. "DO_(::google::protobuf::internal::WireFormatLite::"
  698. "ReadMessage(input, CastToBase(&$name$_)->AddWeak(\n"
  699. " reinterpret_cast<const ::google::protobuf::MessageLite*>(\n"
  700. " &$type_default_instance$))));\n");
  701. } else {
  702. printer->Print(variables_,
  703. "DO_(::google::protobuf::internal::WireFormatLite::"
  704. "ReadMessage(\n"
  705. " input, add_$name$()));\n");
  706. }
  707. } else {
  708. printer->Print(variables_,
  709. "DO_(::google::protobuf::internal::WireFormatLite::"
  710. "ReadGroup($number$, input, add_$name$()));\n");
  711. }
  712. }
  713. void RepeatedMessageFieldGenerator::
  714. GenerateSerializeWithCachedSizes(io::Printer* printer) const {
  715. printer->Print(variables_,
  716. "for (unsigned int i = 0,\n"
  717. " n = static_cast<unsigned int>(this->$name$_size()); i < n; i++) {\n"
  718. " ::google::protobuf::internal::WireFormatLite::Write$stream_writer$(\n"
  719. " $number$,\n");
  720. if (implicit_weak_field_) {
  721. printer->Print(
  722. variables_,
  723. " CastToBase($name$_).Get<"
  724. "::google::protobuf::internal::ImplicitWeakTypeHandler<$type$>>("
  725. "static_cast<int>(i)),\n");
  726. } else {
  727. printer->Print(variables_,
  728. " this->$name$(static_cast<int>(i)),\n");
  729. }
  730. printer->Print(variables_,
  731. " output);\n"
  732. "}\n");
  733. }
  734. void RepeatedMessageFieldGenerator::
  735. GenerateSerializeWithCachedSizesToArray(io::Printer* printer) const {
  736. printer->Print(variables_,
  737. "for (unsigned int i = 0,\n"
  738. " n = static_cast<unsigned int>(this->$name$_size()); i < n; i++) {\n"
  739. " target = ::google::protobuf::internal::WireFormatLite::\n"
  740. " InternalWrite$declared_type$ToArray(\n"
  741. " $number$, this->$name$(static_cast<int>(i)), deterministic, target);\n"
  742. "}\n");
  743. }
  744. void RepeatedMessageFieldGenerator::
  745. GenerateByteSize(io::Printer* printer) const {
  746. printer->Print(variables_,
  747. "{\n"
  748. " unsigned int count = static_cast<unsigned int>(this->$name$_size());\n");
  749. printer->Indent();
  750. printer->Print(variables_,
  751. "total_size += $tag_size$UL * count;\n"
  752. "for (unsigned int i = 0; i < count; i++) {\n"
  753. " total_size +=\n"
  754. " ::google::protobuf::internal::WireFormatLite::$declared_type$Size(\n");
  755. if (implicit_weak_field_) {
  756. printer->Print(
  757. variables_,
  758. " CastToBase($name$_).Get<"
  759. "::google::protobuf::internal::ImplicitWeakTypeHandler<$type$>>("
  760. "static_cast<int>(i)));\n");
  761. } else {
  762. printer->Print(variables_,
  763. " this->$name$(static_cast<int>(i)));\n");
  764. }
  765. printer->Print(variables_, "}\n");
  766. printer->Outdent();
  767. printer->Print("}\n");
  768. }
  769. } // namespace cpp
  770. } // namespace compiler
  771. } // namespace protobuf
  772. } // namespace google