PageRenderTime 60ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/src/google/protobuf/compiler/python/python_generator.cc

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