python_generator.cc 31 KB

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