/thirdparty/breakpad/third_party/protobuf/protobuf/src/google/protobuf/compiler/python/python_generator.cc

http://github.com/tomahawk-player/tomahawk · C++ · 1006 lines · 693 code · 95 blank · 218 comment · 72 complexity · 6d8d1ab0e129b758282bab8e81005fc0 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: robinson@google.com (Will Robinson)
  31. //
  32. // This module outputs pure-Python protocol message classes that will
  33. // largely be constructed at runtime via the metaclass in reflection.py.
  34. // In other words, our job is basically to output a Python equivalent
  35. // of the C++ *Descriptor objects, and fix up all circular references
  36. // within these objects.
  37. //
  38. // Note that the runtime performance of protocol message classes created in
  39. // this way is expected to be lousy. The plan is to create an alternate
  40. // generator that outputs a Python/C extension module that lets
  41. // performance-minded Python code leverage the fast C++ implementation
  42. // directly.
  43. #include <limits>
  44. #include <map>
  45. #include <utility>
  46. #include <string>
  47. #include <vector>
  48. #include <google/protobuf/compiler/python/python_generator.h>
  49. #include <google/protobuf/descriptor.pb.h>
  50. #include <google/protobuf/stubs/common.h>
  51. #include <google/protobuf/io/printer.h>
  52. #include <google/protobuf/descriptor.h>
  53. #include <google/protobuf/io/zero_copy_stream.h>
  54. #include <google/protobuf/stubs/strutil.h>
  55. #include <google/protobuf/stubs/substitute.h>
  56. namespace google {
  57. namespace protobuf {
  58. namespace compiler {
  59. namespace python {
  60. namespace {
  61. // Returns a copy of |filename| with any trailing ".protodevel" or ".proto
  62. // suffix stripped.
  63. // TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc.
  64. string StripProto(const string& filename) {
  65. const char* suffix = HasSuffixString(filename, ".protodevel")
  66. ? ".protodevel" : ".proto";
  67. return StripSuffixString(filename, suffix);
  68. }
  69. // Returns the Python module name expected for a given .proto filename.
  70. string ModuleName(const string& filename) {
  71. string basename = StripProto(filename);
  72. StripString(&basename, "-", '_');
  73. StripString(&basename, "/", '.');
  74. return basename + "_pb2";
  75. }
  76. // Returns the name of all containing types for descriptor,
  77. // in order from outermost to innermost, followed by descriptor's
  78. // own name. Each name is separated by |separator|.
  79. template <typename DescriptorT>
  80. string NamePrefixedWithNestedTypes(const DescriptorT& descriptor,
  81. const string& separator) {
  82. string name = descriptor.name();
  83. for (const Descriptor* current = descriptor.containing_type();
  84. current != NULL; current = current->containing_type()) {
  85. name = current->name() + separator + name;
  86. }
  87. return name;
  88. }
  89. // Name of the class attribute where we store the Python
  90. // descriptor.Descriptor instance for the generated class.
  91. // Must stay consistent with the _DESCRIPTOR_KEY constant
  92. // in proto2/public/reflection.py.
  93. const char kDescriptorKey[] = "DESCRIPTOR";
  94. // Should we generate generic services for this file?
  95. inline bool HasGenericServices(const FileDescriptor *file) {
  96. return file->service_count() > 0 &&
  97. file->options().py_generic_services();
  98. }
  99. // Prints the common boilerplate needed at the top of every .py
  100. // file output by this generator.
  101. void PrintTopBoilerplate(
  102. io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) {
  103. // TODO(robinson): Allow parameterization of Python version?
  104. printer->Print(
  105. "# Generated by the protocol buffer compiler. DO NOT EDIT!\n"
  106. "\n"
  107. "from google.protobuf import descriptor\n"
  108. "from google.protobuf import message\n"
  109. "from google.protobuf import reflection\n");
  110. if (HasGenericServices(file)) {
  111. printer->Print(
  112. "from google.protobuf import service\n"
  113. "from google.protobuf import service_reflection\n");
  114. }
  115. // Avoid circular imports if this module is descriptor_pb2.
  116. if (!descriptor_proto) {
  117. printer->Print(
  118. "from google.protobuf import descriptor_pb2\n");
  119. }
  120. printer->Print(
  121. "# @@protoc_insertion_point(imports)\n");
  122. printer->Print("\n\n");
  123. }
  124. // Returns a Python literal giving the default value for a field.
  125. // If the field specifies no explicit default value, we'll return
  126. // the default default value for the field type (zero for numbers,
  127. // empty string for strings, empty list for repeated fields, and
  128. // None for non-repeated, composite fields).
  129. //
  130. // TODO(robinson): Unify with code from
  131. // //compiler/cpp/internal/primitive_field.cc
  132. // //compiler/cpp/internal/enum_field.cc
  133. // //compiler/cpp/internal/string_field.cc
  134. string StringifyDefaultValue(const FieldDescriptor& field) {
  135. if (field.is_repeated()) {
  136. return "[]";
  137. }
  138. switch (field.cpp_type()) {
  139. case FieldDescriptor::CPPTYPE_INT32:
  140. return SimpleItoa(field.default_value_int32());
  141. case FieldDescriptor::CPPTYPE_UINT32:
  142. return SimpleItoa(field.default_value_uint32());
  143. case FieldDescriptor::CPPTYPE_INT64:
  144. return SimpleItoa(field.default_value_int64());
  145. case FieldDescriptor::CPPTYPE_UINT64:
  146. return SimpleItoa(field.default_value_uint64());
  147. case FieldDescriptor::CPPTYPE_DOUBLE: {
  148. double value = field.default_value_double();
  149. if (value == numeric_limits<double>::infinity()) {
  150. // Python pre-2.6 on Windows does not parse "inf" correctly. However,
  151. // a numeric literal that is too big for a double will become infinity.
  152. return "1e10000";
  153. } else if (value == -numeric_limits<double>::infinity()) {
  154. // See above.
  155. return "-1e10000";
  156. } else if (value != value) {
  157. // infinity * 0 = nan
  158. return "(1e10000 * 0)";
  159. } else {
  160. return SimpleDtoa(value);
  161. }
  162. }
  163. case FieldDescriptor::CPPTYPE_FLOAT: {
  164. float value = field.default_value_float();
  165. if (value == numeric_limits<float>::infinity()) {
  166. // Python pre-2.6 on Windows does not parse "inf" correctly. However,
  167. // a numeric literal that is too big for a double will become infinity.
  168. return "1e10000";
  169. } else if (value == -numeric_limits<float>::infinity()) {
  170. // See above.
  171. return "-1e10000";
  172. } else if (value != value) {
  173. // infinity - infinity = nan
  174. return "(1e10000 * 0)";
  175. } else {
  176. return SimpleFtoa(value);
  177. }
  178. }
  179. case FieldDescriptor::CPPTYPE_BOOL:
  180. return field.default_value_bool() ? "True" : "False";
  181. case FieldDescriptor::CPPTYPE_ENUM:
  182. return SimpleItoa(field.default_value_enum()->number());
  183. case FieldDescriptor::CPPTYPE_STRING:
  184. if (field.type() == FieldDescriptor::TYPE_STRING) {
  185. return "unicode(\"" + CEscape(field.default_value_string()) +
  186. "\", \"utf-8\")";
  187. } else {
  188. return "\"" + CEscape(field.default_value_string()) + "\"";
  189. }
  190. case FieldDescriptor::CPPTYPE_MESSAGE:
  191. return "None";
  192. }
  193. // (We could add a default case above but then we wouldn't get the nice
  194. // compiler warning when a new type is added.)
  195. GOOGLE_LOG(FATAL) << "Not reached.";
  196. return "";
  197. }
  198. } // namespace
  199. Generator::Generator() : file_(NULL) {
  200. }
  201. Generator::~Generator() {
  202. }
  203. bool Generator::Generate(const FileDescriptor* file,
  204. const string& parameter,
  205. GeneratorContext* context,
  206. string* error) const {
  207. // Completely serialize all Generate() calls on this instance. The
  208. // thread-safety constraints of the CodeGenerator interface aren't clear so
  209. // just be as conservative as possible. It's easier to relax this later if
  210. // we need to, but I doubt it will be an issue.
  211. // TODO(kenton): The proper thing to do would be to allocate any state on
  212. // the stack and use that, so that the Generator class itself does not need
  213. // to have any mutable members. Then it is implicitly thread-safe.
  214. MutexLock lock(&mutex_);
  215. file_ = file;
  216. string module_name = ModuleName(file->name());
  217. string filename = module_name;
  218. StripString(&filename, ".", '/');
  219. filename += ".py";
  220. FileDescriptorProto fdp;
  221. file_->CopyTo(&fdp);
  222. fdp.SerializeToString(&file_descriptor_serialized_);
  223. scoped_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
  224. GOOGLE_CHECK(output.get());
  225. io::Printer printer(output.get(), '$');
  226. printer_ = &printer;
  227. PrintTopBoilerplate(printer_, file_, GeneratingDescriptorProto());
  228. PrintImports();
  229. PrintFileDescriptor();
  230. PrintTopLevelEnums();
  231. PrintTopLevelExtensions();
  232. PrintAllNestedEnumsInFile();
  233. PrintMessageDescriptors();
  234. FixForeignFieldsInDescriptors();
  235. PrintMessages();
  236. // We have to fix up the extensions after the message classes themselves,
  237. // since they need to call static RegisterExtension() methods on these
  238. // classes.
  239. FixForeignFieldsInExtensions();
  240. if (HasGenericServices(file)) {
  241. PrintServices();
  242. }
  243. printer.Print(
  244. "# @@protoc_insertion_point(module_scope)\n");
  245. return !printer.failed();
  246. }
  247. // Prints Python imports for all modules imported by |file|.
  248. void Generator::PrintImports() const {
  249. for (int i = 0; i < file_->dependency_count(); ++i) {
  250. string module_name = ModuleName(file_->dependency(i)->name());
  251. printer_->Print("import $module$\n", "module",
  252. module_name);
  253. }
  254. printer_->Print("\n");
  255. }
  256. // Prints the single file descriptor for this file.
  257. void Generator::PrintFileDescriptor() const {
  258. map<string, string> m;
  259. m["descriptor_name"] = kDescriptorKey;
  260. m["name"] = file_->name();
  261. m["package"] = file_->package();
  262. const char file_descriptor_template[] =
  263. "$descriptor_name$ = descriptor.FileDescriptor(\n"
  264. " name='$name$',\n"
  265. " package='$package$',\n";
  266. printer_->Print(m, file_descriptor_template);
  267. printer_->Indent();
  268. printer_->Print(
  269. "serialized_pb='$value$'",
  270. "value", strings::CHexEscape(file_descriptor_serialized_));
  271. // TODO(falk): Also print options and fix the message_type, enum_type,
  272. // service and extension later in the generation.
  273. printer_->Outdent();
  274. printer_->Print(")\n");
  275. printer_->Print("\n");
  276. }
  277. // Prints descriptors and module-level constants for all top-level
  278. // enums defined in |file|.
  279. void Generator::PrintTopLevelEnums() const {
  280. vector<pair<string, int> > top_level_enum_values;
  281. for (int i = 0; i < file_->enum_type_count(); ++i) {
  282. const EnumDescriptor& enum_descriptor = *file_->enum_type(i);
  283. PrintEnum(enum_descriptor);
  284. printer_->Print("\n");
  285. for (int j = 0; j < enum_descriptor.value_count(); ++j) {
  286. const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(j);
  287. top_level_enum_values.push_back(
  288. make_pair(value_descriptor.name(), value_descriptor.number()));
  289. }
  290. }
  291. for (int i = 0; i < top_level_enum_values.size(); ++i) {
  292. printer_->Print("$name$ = $value$\n",
  293. "name", top_level_enum_values[i].first,
  294. "value", SimpleItoa(top_level_enum_values[i].second));
  295. }
  296. printer_->Print("\n");
  297. }
  298. // Prints all enums contained in all message types in |file|.
  299. void Generator::PrintAllNestedEnumsInFile() const {
  300. for (int i = 0; i < file_->message_type_count(); ++i) {
  301. PrintNestedEnums(*file_->message_type(i));
  302. }
  303. }
  304. // Prints a Python statement assigning the appropriate module-level
  305. // enum name to a Python EnumDescriptor object equivalent to
  306. // enum_descriptor.
  307. void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const {
  308. map<string, string> m;
  309. m["descriptor_name"] = ModuleLevelDescriptorName(enum_descriptor);
  310. m["name"] = enum_descriptor.name();
  311. m["full_name"] = enum_descriptor.full_name();
  312. m["file"] = kDescriptorKey;
  313. const char enum_descriptor_template[] =
  314. "$descriptor_name$ = descriptor.EnumDescriptor(\n"
  315. " name='$name$',\n"
  316. " full_name='$full_name$',\n"
  317. " filename=None,\n"
  318. " file=$file$,\n"
  319. " values=[\n";
  320. string options_string;
  321. enum_descriptor.options().SerializeToString(&options_string);
  322. printer_->Print(m, enum_descriptor_template);
  323. printer_->Indent();
  324. printer_->Indent();
  325. for (int i = 0; i < enum_descriptor.value_count(); ++i) {
  326. PrintEnumValueDescriptor(*enum_descriptor.value(i));
  327. printer_->Print(",\n");
  328. }
  329. printer_->Outdent();
  330. printer_->Print("],\n");
  331. printer_->Print("containing_type=None,\n");
  332. printer_->Print("options=$options_value$,\n",
  333. "options_value",
  334. OptionsValue("EnumOptions", options_string));
  335. EnumDescriptorProto edp;
  336. PrintSerializedPbInterval(enum_descriptor, edp);
  337. printer_->Outdent();
  338. printer_->Print(")\n");
  339. printer_->Print("\n");
  340. }
  341. // Recursively prints enums in nested types within descriptor, then
  342. // prints enums contained at the top level in descriptor.
  343. void Generator::PrintNestedEnums(const Descriptor& descriptor) const {
  344. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  345. PrintNestedEnums(*descriptor.nested_type(i));
  346. }
  347. for (int i = 0; i < descriptor.enum_type_count(); ++i) {
  348. PrintEnum(*descriptor.enum_type(i));
  349. }
  350. }
  351. void Generator::PrintTopLevelExtensions() const {
  352. const bool is_extension = true;
  353. for (int i = 0; i < file_->extension_count(); ++i) {
  354. const FieldDescriptor& extension_field = *file_->extension(i);
  355. string constant_name = extension_field.name() + "_FIELD_NUMBER";
  356. UpperString(&constant_name);
  357. printer_->Print("$constant_name$ = $number$\n",
  358. "constant_name", constant_name,
  359. "number", SimpleItoa(extension_field.number()));
  360. printer_->Print("$name$ = ", "name", extension_field.name());
  361. PrintFieldDescriptor(extension_field, is_extension);
  362. printer_->Print("\n");
  363. }
  364. printer_->Print("\n");
  365. }
  366. // Prints Python equivalents of all Descriptors in |file|.
  367. void Generator::PrintMessageDescriptors() const {
  368. for (int i = 0; i < file_->message_type_count(); ++i) {
  369. PrintDescriptor(*file_->message_type(i));
  370. printer_->Print("\n");
  371. }
  372. }
  373. void Generator::PrintServices() const {
  374. for (int i = 0; i < file_->service_count(); ++i) {
  375. PrintServiceDescriptor(*file_->service(i));
  376. PrintServiceClass(*file_->service(i));
  377. PrintServiceStub(*file_->service(i));
  378. printer_->Print("\n");
  379. }
  380. }
  381. void Generator::PrintServiceDescriptor(
  382. const ServiceDescriptor& descriptor) const {
  383. printer_->Print("\n");
  384. string service_name = ModuleLevelServiceDescriptorName(descriptor);
  385. string options_string;
  386. descriptor.options().SerializeToString(&options_string);
  387. printer_->Print(
  388. "$service_name$ = descriptor.ServiceDescriptor(\n",
  389. "service_name", service_name);
  390. printer_->Indent();
  391. map<string, string> m;
  392. m["name"] = descriptor.name();
  393. m["full_name"] = descriptor.full_name();
  394. m["file"] = kDescriptorKey;
  395. m["index"] = SimpleItoa(descriptor.index());
  396. m["options_value"] = OptionsValue("ServiceOptions", options_string);
  397. const char required_function_arguments[] =
  398. "name='$name$',\n"
  399. "full_name='$full_name$',\n"
  400. "file=$file$,\n"
  401. "index=$index$,\n"
  402. "options=$options_value$,\n";
  403. printer_->Print(m, required_function_arguments);
  404. ServiceDescriptorProto sdp;
  405. PrintSerializedPbInterval(descriptor, sdp);
  406. printer_->Print("methods=[\n");
  407. for (int i = 0; i < descriptor.method_count(); ++i) {
  408. const MethodDescriptor* method = descriptor.method(i);
  409. string options_string;
  410. method->options().SerializeToString(&options_string);
  411. m.clear();
  412. m["name"] = method->name();
  413. m["full_name"] = method->full_name();
  414. m["index"] = SimpleItoa(method->index());
  415. m["serialized_options"] = CEscape(options_string);
  416. m["input_type"] = ModuleLevelDescriptorName(*(method->input_type()));
  417. m["output_type"] = ModuleLevelDescriptorName(*(method->output_type()));
  418. m["options_value"] = OptionsValue("MethodOptions", options_string);
  419. printer_->Print("descriptor.MethodDescriptor(\n");
  420. printer_->Indent();
  421. printer_->Print(
  422. m,
  423. "name='$name$',\n"
  424. "full_name='$full_name$',\n"
  425. "index=$index$,\n"
  426. "containing_service=None,\n"
  427. "input_type=$input_type$,\n"
  428. "output_type=$output_type$,\n"
  429. "options=$options_value$,\n");
  430. printer_->Outdent();
  431. printer_->Print("),\n");
  432. }
  433. printer_->Outdent();
  434. printer_->Print("])\n\n");
  435. }
  436. void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const {
  437. // Print the service.
  438. printer_->Print("class $class_name$(service.Service):\n",
  439. "class_name", descriptor.name());
  440. printer_->Indent();
  441. printer_->Print(
  442. "__metaclass__ = service_reflection.GeneratedServiceType\n"
  443. "$descriptor_key$ = $descriptor_name$\n",
  444. "descriptor_key", kDescriptorKey,
  445. "descriptor_name", ModuleLevelServiceDescriptorName(descriptor));
  446. printer_->Outdent();
  447. }
  448. void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const {
  449. // Print the service stub.
  450. printer_->Print("class $class_name$_Stub($class_name$):\n",
  451. "class_name", descriptor.name());
  452. printer_->Indent();
  453. printer_->Print(
  454. "__metaclass__ = service_reflection.GeneratedServiceStubType\n"
  455. "$descriptor_key$ = $descriptor_name$\n",
  456. "descriptor_key", kDescriptorKey,
  457. "descriptor_name", ModuleLevelServiceDescriptorName(descriptor));
  458. printer_->Outdent();
  459. }
  460. // Prints statement assigning ModuleLevelDescriptorName(message_descriptor)
  461. // to a Python Descriptor object for message_descriptor.
  462. //
  463. // Mutually recursive with PrintNestedDescriptors().
  464. void Generator::PrintDescriptor(const Descriptor& message_descriptor) const {
  465. PrintNestedDescriptors(message_descriptor);
  466. printer_->Print("\n");
  467. printer_->Print("$descriptor_name$ = descriptor.Descriptor(\n",
  468. "descriptor_name",
  469. ModuleLevelDescriptorName(message_descriptor));
  470. printer_->Indent();
  471. map<string, string> m;
  472. m["name"] = message_descriptor.name();
  473. m["full_name"] = message_descriptor.full_name();
  474. m["file"] = kDescriptorKey;
  475. const char required_function_arguments[] =
  476. "name='$name$',\n"
  477. "full_name='$full_name$',\n"
  478. "filename=None,\n"
  479. "file=$file$,\n"
  480. "containing_type=None,\n";
  481. printer_->Print(m, required_function_arguments);
  482. PrintFieldsInDescriptor(message_descriptor);
  483. PrintExtensionsInDescriptor(message_descriptor);
  484. // Nested types
  485. printer_->Print("nested_types=[");
  486. for (int i = 0; i < message_descriptor.nested_type_count(); ++i) {
  487. const string nested_name = ModuleLevelDescriptorName(
  488. *message_descriptor.nested_type(i));
  489. printer_->Print("$name$, ", "name", nested_name);
  490. }
  491. printer_->Print("],\n");
  492. // Enum types
  493. printer_->Print("enum_types=[\n");
  494. printer_->Indent();
  495. for (int i = 0; i < message_descriptor.enum_type_count(); ++i) {
  496. const string descriptor_name = ModuleLevelDescriptorName(
  497. *message_descriptor.enum_type(i));
  498. printer_->Print(descriptor_name.c_str());
  499. printer_->Print(",\n");
  500. }
  501. printer_->Outdent();
  502. printer_->Print("],\n");
  503. string options_string;
  504. message_descriptor.options().SerializeToString(&options_string);
  505. printer_->Print(
  506. "options=$options_value$,\n"
  507. "is_extendable=$extendable$",
  508. "options_value", OptionsValue("MessageOptions", options_string),
  509. "extendable", message_descriptor.extension_range_count() > 0 ?
  510. "True" : "False");
  511. printer_->Print(",\n");
  512. // Extension ranges
  513. printer_->Print("extension_ranges=[");
  514. for (int i = 0; i < message_descriptor.extension_range_count(); ++i) {
  515. const Descriptor::ExtensionRange* range =
  516. message_descriptor.extension_range(i);
  517. printer_->Print("($start$, $end$), ",
  518. "start", SimpleItoa(range->start),
  519. "end", SimpleItoa(range->end));
  520. }
  521. printer_->Print("],\n");
  522. // Serialization of proto
  523. DescriptorProto edp;
  524. PrintSerializedPbInterval(message_descriptor, edp);
  525. printer_->Outdent();
  526. printer_->Print(")\n");
  527. }
  528. // Prints Python Descriptor objects for all nested types contained in
  529. // message_descriptor.
  530. //
  531. // Mutually recursive with PrintDescriptor().
  532. void Generator::PrintNestedDescriptors(
  533. const Descriptor& containing_descriptor) const {
  534. for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) {
  535. PrintDescriptor(*containing_descriptor.nested_type(i));
  536. }
  537. }
  538. // Prints all messages in |file|.
  539. void Generator::PrintMessages() const {
  540. for (int i = 0; i < file_->message_type_count(); ++i) {
  541. PrintMessage(*file_->message_type(i));
  542. printer_->Print("\n");
  543. }
  544. }
  545. // Prints a Python class for the given message descriptor. We defer to the
  546. // metaclass to do almost all of the work of actually creating a useful class.
  547. // The purpose of this function and its many helper functions above is merely
  548. // to output a Python version of the descriptors, which the metaclass in
  549. // reflection.py will use to construct the meat of the class itself.
  550. //
  551. // Mutually recursive with PrintNestedMessages().
  552. void Generator::PrintMessage(
  553. const Descriptor& message_descriptor) const {
  554. printer_->Print("class $name$(message.Message):\n", "name",
  555. message_descriptor.name());
  556. printer_->Indent();
  557. printer_->Print("__metaclass__ = reflection.GeneratedProtocolMessageType\n");
  558. PrintNestedMessages(message_descriptor);
  559. map<string, string> m;
  560. m["descriptor_key"] = kDescriptorKey;
  561. m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor);
  562. printer_->Print(m, "$descriptor_key$ = $descriptor_name$\n");
  563. printer_->Print(
  564. "\n"
  565. "# @@protoc_insertion_point(class_scope:$full_name$)\n",
  566. "full_name", message_descriptor.full_name());
  567. printer_->Outdent();
  568. }
  569. // Prints all nested messages within |containing_descriptor|.
  570. // Mutually recursive with PrintMessage().
  571. void Generator::PrintNestedMessages(
  572. const Descriptor& containing_descriptor) const {
  573. for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) {
  574. printer_->Print("\n");
  575. PrintMessage(*containing_descriptor.nested_type(i));
  576. }
  577. }
  578. // Recursively fixes foreign fields in all nested types in |descriptor|, then
  579. // sets the message_type and enum_type of all message and enum fields to point
  580. // to their respective descriptors.
  581. // Args:
  582. // descriptor: descriptor to print fields for.
  583. // containing_descriptor: if descriptor is a nested type, this is its
  584. // containing type, or NULL if this is a root/top-level type.
  585. void Generator::FixForeignFieldsInDescriptor(
  586. const Descriptor& descriptor,
  587. const Descriptor* containing_descriptor) const {
  588. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  589. FixForeignFieldsInDescriptor(*descriptor.nested_type(i), &descriptor);
  590. }
  591. for (int i = 0; i < descriptor.field_count(); ++i) {
  592. const FieldDescriptor& field_descriptor = *descriptor.field(i);
  593. FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name");
  594. }
  595. FixContainingTypeInDescriptor(descriptor, containing_descriptor);
  596. for (int i = 0; i < descriptor.enum_type_count(); ++i) {
  597. const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i);
  598. FixContainingTypeInDescriptor(enum_descriptor, &descriptor);
  599. }
  600. }
  601. void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const {
  602. map<string, string> m;
  603. m["descriptor_name"] = kDescriptorKey;
  604. m["message_name"] = descriptor.name();
  605. m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
  606. const char file_descriptor_template[] =
  607. "$descriptor_name$.message_types_by_name['$message_name$'] = "
  608. "$message_descriptor_name$\n";
  609. printer_->Print(m, file_descriptor_template);
  610. }
  611. // Sets any necessary message_type and enum_type attributes
  612. // for the Python version of |field|.
  613. //
  614. // containing_type may be NULL, in which case this is a module-level field.
  615. //
  616. // python_dict_name is the name of the Python dict where we should
  617. // look the field up in the containing type. (e.g., fields_by_name
  618. // or extensions_by_name). We ignore python_dict_name if containing_type
  619. // is NULL.
  620. void Generator::FixForeignFieldsInField(const Descriptor* containing_type,
  621. const FieldDescriptor& field,
  622. const string& python_dict_name) const {
  623. const string field_referencing_expression = FieldReferencingExpression(
  624. containing_type, field, python_dict_name);
  625. map<string, string> m;
  626. m["field_ref"] = field_referencing_expression;
  627. const Descriptor* foreign_message_type = field.message_type();
  628. if (foreign_message_type) {
  629. m["foreign_type"] = ModuleLevelDescriptorName(*foreign_message_type);
  630. printer_->Print(m, "$field_ref$.message_type = $foreign_type$\n");
  631. }
  632. const EnumDescriptor* enum_type = field.enum_type();
  633. if (enum_type) {
  634. m["enum_type"] = ModuleLevelDescriptorName(*enum_type);
  635. printer_->Print(m, "$field_ref$.enum_type = $enum_type$\n");
  636. }
  637. }
  638. // Returns the module-level expression for the given FieldDescriptor.
  639. // Only works for fields in the .proto file this Generator is generating for.
  640. //
  641. // containing_type may be NULL, in which case this is a module-level field.
  642. //
  643. // python_dict_name is the name of the Python dict where we should
  644. // look the field up in the containing type. (e.g., fields_by_name
  645. // or extensions_by_name). We ignore python_dict_name if containing_type
  646. // is NULL.
  647. string Generator::FieldReferencingExpression(
  648. const Descriptor* containing_type,
  649. const FieldDescriptor& field,
  650. const string& python_dict_name) const {
  651. // We should only ever be looking up fields in the current file.
  652. // The only things we refer to from other files are message descriptors.
  653. GOOGLE_CHECK_EQ(field.file(), file_) << field.file()->name() << " vs. "
  654. << file_->name();
  655. if (!containing_type) {
  656. return field.name();
  657. }
  658. return strings::Substitute(
  659. "$0.$1['$2']",
  660. ModuleLevelDescriptorName(*containing_type),
  661. python_dict_name, field.name());
  662. }
  663. // Prints containing_type for nested descriptors or enum descriptors.
  664. template <typename DescriptorT>
  665. void Generator::FixContainingTypeInDescriptor(
  666. const DescriptorT& descriptor,
  667. const Descriptor* containing_descriptor) const {
  668. if (containing_descriptor != NULL) {
  669. const string nested_name = ModuleLevelDescriptorName(descriptor);
  670. const string parent_name = ModuleLevelDescriptorName(
  671. *containing_descriptor);
  672. printer_->Print(
  673. "$nested_name$.containing_type = $parent_name$;\n",
  674. "nested_name", nested_name,
  675. "parent_name", parent_name);
  676. }
  677. }
  678. // Prints statements setting the message_type and enum_type fields in the
  679. // Python descriptor objects we've already output in ths file. We must
  680. // do this in a separate step due to circular references (otherwise, we'd
  681. // just set everything in the initial assignment statements).
  682. void Generator::FixForeignFieldsInDescriptors() const {
  683. for (int i = 0; i < file_->message_type_count(); ++i) {
  684. FixForeignFieldsInDescriptor(*file_->message_type(i), NULL);
  685. }
  686. for (int i = 0; i < file_->message_type_count(); ++i) {
  687. AddMessageToFileDescriptor(*file_->message_type(i));
  688. }
  689. printer_->Print("\n");
  690. }
  691. // We need to not only set any necessary message_type fields, but
  692. // also need to call RegisterExtension() on each message we're
  693. // extending.
  694. void Generator::FixForeignFieldsInExtensions() const {
  695. // Top-level extensions.
  696. for (int i = 0; i < file_->extension_count(); ++i) {
  697. FixForeignFieldsInExtension(*file_->extension(i));
  698. }
  699. // Nested extensions.
  700. for (int i = 0; i < file_->message_type_count(); ++i) {
  701. FixForeignFieldsInNestedExtensions(*file_->message_type(i));
  702. }
  703. }
  704. void Generator::FixForeignFieldsInExtension(
  705. const FieldDescriptor& extension_field) const {
  706. GOOGLE_CHECK(extension_field.is_extension());
  707. // extension_scope() will be NULL for top-level extensions, which is
  708. // exactly what FixForeignFieldsInField() wants.
  709. FixForeignFieldsInField(extension_field.extension_scope(), extension_field,
  710. "extensions_by_name");
  711. map<string, string> m;
  712. // Confusingly, for FieldDescriptors that happen to be extensions,
  713. // containing_type() means "extended type."
  714. // On the other hand, extension_scope() will give us what we normally
  715. // mean by containing_type().
  716. m["extended_message_class"] = ModuleLevelMessageName(
  717. *extension_field.containing_type());
  718. m["field"] = FieldReferencingExpression(extension_field.extension_scope(),
  719. extension_field,
  720. "extensions_by_name");
  721. printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n");
  722. }
  723. void Generator::FixForeignFieldsInNestedExtensions(
  724. const Descriptor& descriptor) const {
  725. // Recursively fix up extensions in all nested types.
  726. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  727. FixForeignFieldsInNestedExtensions(*descriptor.nested_type(i));
  728. }
  729. // Fix up extensions directly contained within this type.
  730. for (int i = 0; i < descriptor.extension_count(); ++i) {
  731. FixForeignFieldsInExtension(*descriptor.extension(i));
  732. }
  733. }
  734. // Returns a Python expression that instantiates a Python EnumValueDescriptor
  735. // object for the given C++ descriptor.
  736. void Generator::PrintEnumValueDescriptor(
  737. const EnumValueDescriptor& descriptor) const {
  738. // TODO(robinson): Fix up EnumValueDescriptor "type" fields.
  739. // More circular references. ::sigh::
  740. string options_string;
  741. descriptor.options().SerializeToString(&options_string);
  742. map<string, string> m;
  743. m["name"] = descriptor.name();
  744. m["index"] = SimpleItoa(descriptor.index());
  745. m["number"] = SimpleItoa(descriptor.number());
  746. m["options"] = OptionsValue("EnumValueOptions", options_string);
  747. printer_->Print(
  748. m,
  749. "descriptor.EnumValueDescriptor(\n"
  750. " name='$name$', index=$index$, number=$number$,\n"
  751. " options=$options$,\n"
  752. " type=None)");
  753. }
  754. // Returns a Python expression that calls descriptor._ParseOptions using
  755. // the given descriptor class name and serialized options protobuf string.
  756. string Generator::OptionsValue(
  757. const string& class_name, const string& serialized_options) const {
  758. if (serialized_options.length() == 0 || GeneratingDescriptorProto()) {
  759. return "None";
  760. } else {
  761. string full_class_name = "descriptor_pb2." + class_name;
  762. return "descriptor._ParseOptions(" + full_class_name + "(), '"
  763. + CEscape(serialized_options)+ "')";
  764. }
  765. }
  766. // Prints an expression for a Python FieldDescriptor for |field|.
  767. void Generator::PrintFieldDescriptor(
  768. const FieldDescriptor& field, bool is_extension) const {
  769. string options_string;
  770. field.options().SerializeToString(&options_string);
  771. map<string, string> m;
  772. m["name"] = field.name();
  773. m["full_name"] = field.full_name();
  774. m["index"] = SimpleItoa(field.index());
  775. m["number"] = SimpleItoa(field.number());
  776. m["type"] = SimpleItoa(field.type());
  777. m["cpp_type"] = SimpleItoa(field.cpp_type());
  778. m["label"] = SimpleItoa(field.label());
  779. m["has_default_value"] = field.has_default_value() ? "True" : "False";
  780. m["default_value"] = StringifyDefaultValue(field);
  781. m["is_extension"] = is_extension ? "True" : "False";
  782. m["options"] = OptionsValue("FieldOptions", options_string);
  783. // We always set message_type and enum_type to None at this point, and then
  784. // these fields in correctly after all referenced descriptors have been
  785. // defined and/or imported (see FixForeignFieldsInDescriptors()).
  786. const char field_descriptor_decl[] =
  787. "descriptor.FieldDescriptor(\n"
  788. " name='$name$', full_name='$full_name$', index=$index$,\n"
  789. " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n"
  790. " has_default_value=$has_default_value$, default_value=$default_value$,\n"
  791. " message_type=None, enum_type=None, containing_type=None,\n"
  792. " is_extension=$is_extension$, extension_scope=None,\n"
  793. " options=$options$)";
  794. printer_->Print(m, field_descriptor_decl);
  795. }
  796. // Helper for Print{Fields,Extensions}InDescriptor().
  797. void Generator::PrintFieldDescriptorsInDescriptor(
  798. const Descriptor& message_descriptor,
  799. bool is_extension,
  800. const string& list_variable_name,
  801. int (Descriptor::*CountFn)() const,
  802. const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const {
  803. printer_->Print("$list$=[\n", "list", list_variable_name);
  804. printer_->Indent();
  805. for (int i = 0; i < (message_descriptor.*CountFn)(); ++i) {
  806. PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i),
  807. is_extension);
  808. printer_->Print(",\n");
  809. }
  810. printer_->Outdent();
  811. printer_->Print("],\n");
  812. }
  813. // Prints a statement assigning "fields" to a list of Python FieldDescriptors,
  814. // one for each field present in message_descriptor.
  815. void Generator::PrintFieldsInDescriptor(
  816. const Descriptor& message_descriptor) const {
  817. const bool is_extension = false;
  818. PrintFieldDescriptorsInDescriptor(
  819. message_descriptor, is_extension, "fields",
  820. &Descriptor::field_count, &Descriptor::field);
  821. }
  822. // Prints a statement assigning "extensions" to a list of Python
  823. // FieldDescriptors, one for each extension present in message_descriptor.
  824. void Generator::PrintExtensionsInDescriptor(
  825. const Descriptor& message_descriptor) const {
  826. const bool is_extension = true;
  827. PrintFieldDescriptorsInDescriptor(
  828. message_descriptor, is_extension, "extensions",
  829. &Descriptor::extension_count, &Descriptor::extension);
  830. }
  831. bool Generator::GeneratingDescriptorProto() const {
  832. return file_->name() == "google/protobuf/descriptor.proto";
  833. }
  834. // Returns the unique Python module-level identifier given to a descriptor.
  835. // This name is module-qualified iff the given descriptor describes an
  836. // entity that doesn't come from the current file.
  837. template <typename DescriptorT>
  838. string Generator::ModuleLevelDescriptorName(
  839. const DescriptorT& descriptor) const {
  840. // FIXME(robinson):
  841. // We currently don't worry about collisions with underscores in the type
  842. // names, so these would collide in nasty ways if found in the same file:
  843. // OuterProto.ProtoA.ProtoB
  844. // OuterProto_ProtoA.ProtoB # Underscore instead of period.
  845. // As would these:
  846. // OuterProto.ProtoA_.ProtoB
  847. // OuterProto.ProtoA._ProtoB # Leading vs. trailing underscore.
  848. // (Contrived, but certainly possible).
  849. //
  850. // The C++ implementation doesn't guard against this either. Leaving
  851. // it for now...
  852. string name = NamePrefixedWithNestedTypes(descriptor, "_");
  853. UpperString(&name);
  854. // Module-private for now. Easy to make public later; almost impossible
  855. // to make private later.
  856. name = "_" + name;
  857. // We now have the name relative to its own module. Also qualify with
  858. // the module name iff this descriptor is from a different .proto file.
  859. if (descriptor.file() != file_) {
  860. name = ModuleName(descriptor.file()->name()) + "." + name;
  861. }
  862. return name;
  863. }
  864. // Returns the name of the message class itself, not the descriptor.
  865. // Like ModuleLevelDescriptorName(), module-qualifies the name iff
  866. // the given descriptor describes an entity that doesn't come from
  867. // the current file.
  868. string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const {
  869. string name = NamePrefixedWithNestedTypes(descriptor, ".");
  870. if (descriptor.file() != file_) {
  871. name = ModuleName(descriptor.file()->name()) + "." + name;
  872. }
  873. return name;
  874. }
  875. // Returns the unique Python module-level identifier given to a service
  876. // descriptor.
  877. string Generator::ModuleLevelServiceDescriptorName(
  878. const ServiceDescriptor& descriptor) const {
  879. string name = descriptor.name();
  880. UpperString(&name);
  881. name = "_" + name;
  882. if (descriptor.file() != file_) {
  883. name = ModuleName(descriptor.file()->name()) + "." + name;
  884. }
  885. return name;
  886. }
  887. // Prints standard constructor arguments serialized_start and serialized_end.
  888. // Args:
  889. // descriptor: The cpp descriptor to have a serialized reference.
  890. // proto: A proto
  891. // Example printer output:
  892. // serialized_start=41,
  893. // serialized_end=43,
  894. //
  895. template <typename DescriptorT, typename DescriptorProtoT>
  896. void Generator::PrintSerializedPbInterval(
  897. const DescriptorT& descriptor, DescriptorProtoT& proto) const {
  898. descriptor.CopyTo(&proto);
  899. string sp;
  900. proto.SerializeToString(&sp);
  901. int offset = file_descriptor_serialized_.find(sp);
  902. GOOGLE_CHECK_GE(offset, 0);
  903. printer_->Print("serialized_start=$serialized_start$,\n"
  904. "serialized_end=$serialized_end$,\n",
  905. "serialized_start", SimpleItoa(offset),
  906. "serialized_end", SimpleItoa(offset + sp.size()));
  907. }
  908. } // namespace python
  909. } // namespace compiler
  910. } // namespace protobuf
  911. } // namespace google