python_generator.cc 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449
  1. // Protocol Buffers - Google's data interchange format
  2. // Copyright 2008 Google Inc. All rights reserved.
  3. // https://developers.google.com/protocol-buffers/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. //#PY25 compatible generated code for GAE.
  31. // Copyright 2007 Google Inc. All Rights Reserved.
  32. // Author: robinson@google.com (Will Robinson)
  33. //
  34. // This module outputs pure-Python protocol message classes that will
  35. // largely be constructed at runtime via the metaclass in reflection.py.
  36. // In other words, our job is basically to output a Python equivalent
  37. // of the C++ *Descriptor objects, and fix up all circular references
  38. // within these objects.
  39. //
  40. // Note that the runtime performance of protocol message classes created in
  41. // this way is expected to be lousy. The plan is to create an alternate
  42. // generator that outputs a Python/C extension module that lets
  43. // performance-minded Python code leverage the fast C++ implementation
  44. // directly.
  45. #include <algorithm>
  46. #include <limits>
  47. #include <map>
  48. #include <memory>
  49. #include <string>
  50. #include <utility>
  51. #include <vector>
  52. #include <google/protobuf/compiler/python/python_generator.h>
  53. #include <google/protobuf/descriptor.pb.h>
  54. #include <google/protobuf/stubs/logging.h>
  55. #include <google/protobuf/stubs/common.h>
  56. #include <google/protobuf/stubs/stringprintf.h>
  57. #include <google/protobuf/io/printer.h>
  58. #include <google/protobuf/io/zero_copy_stream.h>
  59. #include <google/protobuf/descriptor.h>
  60. #include <google/protobuf/stubs/strutil.h>
  61. #include <google/protobuf/stubs/substitute.h>
  62. namespace google {
  63. namespace protobuf {
  64. namespace compiler {
  65. namespace python {
  66. namespace {
  67. bool StrEndsWith(StringPiece sp, StringPiece x) {
  68. return sp.size() >= x.size() && sp.substr(sp.size() - x.size()) == x;
  69. }
  70. // Returns a copy of |filename| with any trailing ".protodevel" or ".proto
  71. // suffix stripped.
  72. // TODO(robinson): Unify with copy in compiler/cpp/internal/helpers.cc.
  73. string StripProto(const string& filename) {
  74. const char* suffix =
  75. StrEndsWith(filename, ".protodevel") ? ".protodevel" : ".proto";
  76. return StripSuffixString(filename, suffix);
  77. }
  78. // Returns the Python module name expected for a given .proto filename.
  79. string ModuleName(const string& filename) {
  80. string basename = StripProto(filename);
  81. ReplaceCharacters(&basename, "-", '_');
  82. ReplaceCharacters(&basename, "/", '.');
  83. return basename + "_pb2";
  84. }
  85. // Returns the alias we assign to the module of the given .proto filename
  86. // when importing. See testPackageInitializationImport in
  87. // net/proto2/python/internal/reflection_test.py
  88. // to see why we need the alias.
  89. string ModuleAlias(const string& filename) {
  90. string module_name = ModuleName(filename);
  91. // We can't have dots in the module name, so we replace each with _dot_.
  92. // But that could lead to a collision between a.b and a_dot_b, so we also
  93. // duplicate each underscore.
  94. GlobalReplaceSubstring("_", "__", &module_name);
  95. GlobalReplaceSubstring(".", "_dot_", &module_name);
  96. return module_name;
  97. }
  98. // Keywords reserved by the Python language.
  99. const char* const kKeywords[] = {
  100. "False", "None", "True", "and", "as", "assert", "break",
  101. "class", "continue", "def", "del", "elif", "else", "except",
  102. "finally", "for", "from", "global", "if", "import", "in",
  103. "is", "lambda", "nonlocal", "not", "or", "pass", "raise",
  104. "return", "try", "while", "with", "yield",
  105. };
  106. const char* const* kKeywordsEnd =
  107. kKeywords + (sizeof(kKeywords) / sizeof(kKeywords[0]));
  108. bool ContainsPythonKeyword(const string& module_name) {
  109. std::vector<string> tokens = Split(module_name, ".");
  110. for (int i = 0; i < tokens.size(); ++i) {
  111. if (std::find(kKeywords, kKeywordsEnd, tokens[i]) != kKeywordsEnd) {
  112. return true;
  113. }
  114. }
  115. return false;
  116. }
  117. // Returns the name of all containing types for descriptor,
  118. // in order from outermost to innermost, followed by descriptor's
  119. // own name. Each name is separated by |separator|.
  120. template <typename DescriptorT>
  121. string NamePrefixedWithNestedTypes(const DescriptorT& descriptor,
  122. const string& separator) {
  123. string name = descriptor.name();
  124. for (const Descriptor* current = descriptor.containing_type();
  125. current != NULL; current = current->containing_type()) {
  126. name = current->name() + separator + name;
  127. }
  128. return name;
  129. }
  130. // Name of the class attribute where we store the Python
  131. // descriptor.Descriptor instance for the generated class.
  132. // Must stay consistent with the _DESCRIPTOR_KEY constant
  133. // in proto2/public/reflection.py.
  134. const char kDescriptorKey[] = "DESCRIPTOR";
  135. // Does the file have top-level enums?
  136. inline bool HasTopLevelEnums(const FileDescriptor *file) {
  137. return file->enum_type_count() > 0;
  138. }
  139. // Should we generate generic services for this file?
  140. inline bool HasGenericServices(const FileDescriptor *file) {
  141. return file->service_count() > 0 &&
  142. file->options().py_generic_services();
  143. }
  144. // Prints the common boilerplate needed at the top of every .py
  145. // file output by this generator.
  146. void PrintTopBoilerplate(
  147. io::Printer* printer, const FileDescriptor* file, bool descriptor_proto) {
  148. // TODO(robinson): Allow parameterization of Python version?
  149. printer->Print(
  150. "# -*- coding: utf-8 -*-\n"
  151. "# Generated by the protocol buffer compiler. DO NOT EDIT!\n"
  152. "# source: $filename$\n"
  153. "\nimport sys\n_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))" //##PY25
  154. "\n",
  155. "filename", file->name());
  156. if (HasTopLevelEnums(file)) {
  157. printer->Print(
  158. "from google.protobuf.internal import enum_type_wrapper\n");
  159. }
  160. printer->Print(
  161. "from google.protobuf import descriptor as _descriptor\n"
  162. "from google.protobuf import message as _message\n"
  163. "from google.protobuf import reflection as _reflection\n"
  164. "from google.protobuf import symbol_database as "
  165. "_symbol_database\n");
  166. if (HasGenericServices(file)) {
  167. printer->Print(
  168. "from google.protobuf import service as _service\n"
  169. "from google.protobuf import service_reflection\n");
  170. }
  171. printer->Print(
  172. "# @@protoc_insertion_point(imports)\n\n"
  173. "_sym_db = _symbol_database.Default()\n");
  174. printer->Print("\n\n");
  175. }
  176. // Returns a Python literal giving the default value for a field.
  177. // If the field specifies no explicit default value, we'll return
  178. // the default default value for the field type (zero for numbers,
  179. // empty string for strings, empty list for repeated fields, and
  180. // None for non-repeated, composite fields).
  181. //
  182. // TODO(robinson): Unify with code from
  183. // //compiler/cpp/internal/primitive_field.cc
  184. // //compiler/cpp/internal/enum_field.cc
  185. // //compiler/cpp/internal/string_field.cc
  186. string StringifyDefaultValue(const FieldDescriptor& field) {
  187. if (field.is_repeated()) {
  188. return "[]";
  189. }
  190. switch (field.cpp_type()) {
  191. case FieldDescriptor::CPPTYPE_INT32:
  192. return StrCat(field.default_value_int32());
  193. case FieldDescriptor::CPPTYPE_UINT32:
  194. return StrCat(field.default_value_uint32());
  195. case FieldDescriptor::CPPTYPE_INT64:
  196. return StrCat(field.default_value_int64());
  197. case FieldDescriptor::CPPTYPE_UINT64:
  198. return StrCat(field.default_value_uint64());
  199. case FieldDescriptor::CPPTYPE_DOUBLE: {
  200. double value = field.default_value_double();
  201. if (value == std::numeric_limits<double>::infinity()) {
  202. // Python pre-2.6 on Windows does not parse "inf" correctly. However,
  203. // a numeric literal that is too big for a double will become infinity.
  204. return "1e10000";
  205. } else if (value == -std::numeric_limits<double>::infinity()) {
  206. // See above.
  207. return "-1e10000";
  208. } else if (value != value) {
  209. // infinity * 0 = nan
  210. return "(1e10000 * 0)";
  211. } else {
  212. return "float(" + SimpleDtoa(value) + ")";
  213. }
  214. }
  215. case FieldDescriptor::CPPTYPE_FLOAT: {
  216. float value = field.default_value_float();
  217. if (value == std::numeric_limits<float>::infinity()) {
  218. // Python pre-2.6 on Windows does not parse "inf" correctly. However,
  219. // a numeric literal that is too big for a double will become infinity.
  220. return "1e10000";
  221. } else if (value == -std::numeric_limits<float>::infinity()) {
  222. // See above.
  223. return "-1e10000";
  224. } else if (value != value) {
  225. // infinity - infinity = nan
  226. return "(1e10000 * 0)";
  227. } else {
  228. return "float(" + SimpleFtoa(value) + ")";
  229. }
  230. }
  231. case FieldDescriptor::CPPTYPE_BOOL:
  232. return field.default_value_bool() ? "True" : "False";
  233. case FieldDescriptor::CPPTYPE_ENUM:
  234. return StrCat(field.default_value_enum()->number());
  235. case FieldDescriptor::CPPTYPE_STRING:
  236. //##!PY25 return "b\"" + CEscape(field.default_value_string()) +
  237. //##!PY25 (field.type() != FieldDescriptor::TYPE_STRING ? "\"" :
  238. //##!PY25 "\".decode('utf-8')");
  239. return "_b(\"" + CEscape(field.default_value_string()) + //##PY25
  240. (field.type() != FieldDescriptor::TYPE_STRING ? "\")" : //##PY25
  241. "\").decode('utf-8')"); //##PY25
  242. case FieldDescriptor::CPPTYPE_MESSAGE:
  243. return "None";
  244. }
  245. // (We could add a default case above but then we wouldn't get the nice
  246. // compiler warning when a new type is added.)
  247. GOOGLE_LOG(FATAL) << "Not reached.";
  248. return "";
  249. }
  250. string StringifySyntax(FileDescriptor::Syntax syntax) {
  251. switch (syntax) {
  252. case FileDescriptor::SYNTAX_PROTO2:
  253. return "proto2";
  254. case FileDescriptor::SYNTAX_PROTO3:
  255. return "proto3";
  256. case FileDescriptor::SYNTAX_UNKNOWN:
  257. default:
  258. GOOGLE_LOG(FATAL) << "Unsupported syntax; this generator only supports proto2 "
  259. "and proto3 syntax.";
  260. return "";
  261. }
  262. }
  263. } // namespace
  264. Generator::Generator() : file_(NULL) {
  265. }
  266. Generator::~Generator() {
  267. }
  268. bool Generator::Generate(const FileDescriptor* file,
  269. const string& parameter,
  270. GeneratorContext* context,
  271. string* error) const {
  272. // Completely serialize all Generate() calls on this instance. The
  273. // thread-safety constraints of the CodeGenerator interface aren't clear so
  274. // just be as conservative as possible. It's easier to relax this later if
  275. // we need to, but I doubt it will be an issue.
  276. // TODO(kenton): The proper thing to do would be to allocate any state on
  277. // the stack and use that, so that the Generator class itself does not need
  278. // to have any mutable members. Then it is implicitly thread-safe.
  279. MutexLock lock(&mutex_);
  280. file_ = file;
  281. string module_name = ModuleName(file->name());
  282. string filename = module_name;
  283. ReplaceCharacters(&filename, ".", '/');
  284. filename += ".py";
  285. FileDescriptorProto fdp;
  286. file_->CopyTo(&fdp);
  287. fdp.SerializeToString(&file_descriptor_serialized_);
  288. std::unique_ptr<io::ZeroCopyOutputStream> output(context->Open(filename));
  289. GOOGLE_CHECK(output.get());
  290. io::Printer printer(output.get(), '$');
  291. printer_ = &printer;
  292. PrintTopBoilerplate(printer_, file_, GeneratingDescriptorProto());
  293. PrintImports();
  294. PrintFileDescriptor();
  295. PrintTopLevelEnums();
  296. PrintTopLevelExtensions();
  297. PrintAllNestedEnumsInFile();
  298. PrintMessageDescriptors();
  299. FixForeignFieldsInDescriptors();
  300. PrintMessages();
  301. // We have to fix up the extensions after the message classes themselves,
  302. // since they need to call static RegisterExtension() methods on these
  303. // classes.
  304. FixForeignFieldsInExtensions();
  305. // Descriptor options may have custom extensions. These custom options
  306. // can only be successfully parsed after we register corresponding
  307. // extensions. Therefore we parse all options again here to recognize
  308. // custom options that may be unknown when we define the descriptors.
  309. // This does not apply to services because they are not used by extensions.
  310. FixAllDescriptorOptions();
  311. PrintServiceDescriptors();
  312. if (HasGenericServices(file)) {
  313. PrintServices();
  314. }
  315. printer.Print(
  316. "# @@protoc_insertion_point(module_scope)\n");
  317. return !printer.failed();
  318. }
  319. // BEGIN GOOGLE-INTERNAL
  320. // Strip the google3.third_party.py. prefix off of a module name as we
  321. // NEVER want that invalid module import path to be generated in google3.
  322. // Our sys.path has google3/third_party/py/ in it. All modules from
  323. // that tree need to be imported using just their own name.
  324. // See http://go/ThirdPartyPython
  325. void StripThirdPartyPy(string* module_name) {
  326. const string third_party_py_prefix = "google3.third_party.py.";
  327. int len = third_party_py_prefix.length();
  328. if (module_name->compare(0, len,
  329. third_party_py_prefix, 0,
  330. len) == 0) {
  331. *module_name = module_name->erase(0, len);
  332. }
  333. }
  334. // END GOOGLE-INTERNAL
  335. // Prints Python imports for all modules imported by |file|.
  336. void Generator::PrintImports() const {
  337. for (int i = 0; i < file_->dependency_count(); ++i) {
  338. const string& filename = file_->dependency(i)->name();
  339. string module_name = ModuleName(filename);
  340. string module_alias = ModuleAlias(filename);
  341. // BEGIN GOOGLE-INTERNAL
  342. StripThirdPartyPy(&module_name);
  343. // END GOOGLE-INTERNAL
  344. if (ContainsPythonKeyword(module_name)) {
  345. // If the module path contains a Python keyword, we have to quote the
  346. // module name and import it using importlib. Otherwise the usual kind of
  347. // import statement would result in a syntax error from the presence of
  348. // the keyword.
  349. printer_->Print("import importlib\n");
  350. printer_->Print("$alias$ = importlib.import_module('$name$')\n", "alias",
  351. module_alias, "name", module_name);
  352. } else {
  353. int last_dot_pos = module_name.rfind('.');
  354. string import_statement;
  355. if (last_dot_pos == string::npos) {
  356. // NOTE(petya): this is not tested as it would require a protocol buffer
  357. // outside of any package, and I don't think that is easily achievable.
  358. import_statement = "import " + module_name;
  359. } else {
  360. import_statement = "from " + module_name.substr(0, last_dot_pos) +
  361. " import " + module_name.substr(last_dot_pos + 1);
  362. }
  363. printer_->Print("$statement$ as $alias$\n", "statement", import_statement,
  364. "alias", module_alias);
  365. }
  366. CopyPublicDependenciesAliases(module_alias, file_->dependency(i));
  367. }
  368. printer_->Print("\n");
  369. // Print public imports.
  370. for (int i = 0; i < file_->public_dependency_count(); ++i) {
  371. string module_name = ModuleName(file_->public_dependency(i)->name());
  372. // BEGIN GOOGLE-INTERNAL
  373. StripThirdPartyPy(&module_name);
  374. // END GOOGLE-INTERNAL
  375. printer_->Print("from $module$ import *\n", "module", module_name);
  376. }
  377. printer_->Print("\n");
  378. }
  379. // Prints the single file descriptor for this file.
  380. void Generator::PrintFileDescriptor() const {
  381. std::map<string, string> m;
  382. m["descriptor_name"] = kDescriptorKey;
  383. m["name"] = file_->name();
  384. m["package"] = file_->package();
  385. m["syntax"] = StringifySyntax(file_->syntax());
  386. m["options"] = OptionsValue(file_->options().SerializeAsString());
  387. const char file_descriptor_template[] =
  388. "$descriptor_name$ = _descriptor.FileDescriptor(\n"
  389. " name='$name$',\n"
  390. " package='$package$',\n"
  391. " syntax='$syntax$',\n"
  392. " serialized_options=$options$,\n";
  393. printer_->Print(m, file_descriptor_template);
  394. printer_->Indent();
  395. printer_->Print(
  396. //##!PY25 "serialized_pb=b'$value$'\n",
  397. "serialized_pb=_b('$value$')\n", //##PY25
  398. "value", strings::CHexEscape(file_descriptor_serialized_));
  399. if (file_->dependency_count() != 0) {
  400. printer_->Print(",\ndependencies=[");
  401. for (int i = 0; i < file_->dependency_count(); ++i) {
  402. string module_alias = ModuleAlias(file_->dependency(i)->name());
  403. printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias",
  404. module_alias);
  405. }
  406. printer_->Print("]");
  407. }
  408. if (file_->public_dependency_count() > 0) {
  409. printer_->Print(",\npublic_dependencies=[");
  410. for (int i = 0; i < file_->public_dependency_count(); ++i) {
  411. string module_alias = ModuleAlias(file_->public_dependency(i)->name());
  412. printer_->Print("$module_alias$.DESCRIPTOR,", "module_alias",
  413. module_alias);
  414. }
  415. printer_->Print("]");
  416. }
  417. // TODO(falk): Also print options and fix the message_type, enum_type,
  418. // service and extension later in the generation.
  419. printer_->Outdent();
  420. printer_->Print(")\n");
  421. printer_->Print("\n");
  422. }
  423. // Prints descriptors and module-level constants for all top-level
  424. // enums defined in |file|.
  425. void Generator::PrintTopLevelEnums() const {
  426. std::vector<std::pair<string, int> > top_level_enum_values;
  427. for (int i = 0; i < file_->enum_type_count(); ++i) {
  428. const EnumDescriptor& enum_descriptor = *file_->enum_type(i);
  429. PrintEnum(enum_descriptor);
  430. printer_->Print("$name$ = "
  431. "enum_type_wrapper.EnumTypeWrapper($descriptor_name$)",
  432. "name", enum_descriptor.name(),
  433. "descriptor_name",
  434. ModuleLevelDescriptorName(enum_descriptor));
  435. printer_->Print("\n");
  436. for (int j = 0; j < enum_descriptor.value_count(); ++j) {
  437. const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(j);
  438. top_level_enum_values.push_back(
  439. std::make_pair(value_descriptor.name(), value_descriptor.number()));
  440. }
  441. }
  442. for (int i = 0; i < top_level_enum_values.size(); ++i) {
  443. printer_->Print("$name$ = $value$\n", "name",
  444. top_level_enum_values[i].first, "value",
  445. StrCat(top_level_enum_values[i].second));
  446. }
  447. printer_->Print("\n");
  448. }
  449. // Prints all enums contained in all message types in |file|.
  450. void Generator::PrintAllNestedEnumsInFile() const {
  451. for (int i = 0; i < file_->message_type_count(); ++i) {
  452. PrintNestedEnums(*file_->message_type(i));
  453. }
  454. }
  455. // Prints a Python statement assigning the appropriate module-level
  456. // enum name to a Python EnumDescriptor object equivalent to
  457. // enum_descriptor.
  458. void Generator::PrintEnum(const EnumDescriptor& enum_descriptor) const {
  459. std::map<string, string> m;
  460. string module_level_descriptor_name =
  461. ModuleLevelDescriptorName(enum_descriptor);
  462. m["descriptor_name"] = module_level_descriptor_name;
  463. m["name"] = enum_descriptor.name();
  464. m["full_name"] = enum_descriptor.full_name();
  465. m["file"] = kDescriptorKey;
  466. const char enum_descriptor_template[] =
  467. "$descriptor_name$ = _descriptor.EnumDescriptor(\n"
  468. " name='$name$',\n"
  469. " full_name='$full_name$',\n"
  470. " filename=None,\n"
  471. " file=$file$,\n"
  472. " values=[\n";
  473. string options_string;
  474. enum_descriptor.options().SerializeToString(&options_string);
  475. printer_->Print(m, enum_descriptor_template);
  476. printer_->Indent();
  477. printer_->Indent();
  478. for (int i = 0; i < enum_descriptor.value_count(); ++i) {
  479. PrintEnumValueDescriptor(*enum_descriptor.value(i));
  480. printer_->Print(",\n");
  481. }
  482. printer_->Outdent();
  483. printer_->Print("],\n");
  484. printer_->Print("containing_type=None,\n");
  485. printer_->Print("serialized_options=$options_value$,\n",
  486. "options_value",
  487. OptionsValue(options_string));
  488. EnumDescriptorProto edp;
  489. PrintSerializedPbInterval(enum_descriptor, edp);
  490. printer_->Outdent();
  491. printer_->Print(")\n");
  492. printer_->Print("_sym_db.RegisterEnumDescriptor($name$)\n", "name",
  493. module_level_descriptor_name);
  494. printer_->Print("\n");
  495. }
  496. // Recursively prints enums in nested types within descriptor, then
  497. // prints enums contained at the top level in descriptor.
  498. void Generator::PrintNestedEnums(const Descriptor& descriptor) const {
  499. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  500. PrintNestedEnums(*descriptor.nested_type(i));
  501. }
  502. for (int i = 0; i < descriptor.enum_type_count(); ++i) {
  503. PrintEnum(*descriptor.enum_type(i));
  504. }
  505. }
  506. void Generator::PrintTopLevelExtensions() const {
  507. const bool is_extension = true;
  508. for (int i = 0; i < file_->extension_count(); ++i) {
  509. const FieldDescriptor& extension_field = *file_->extension(i);
  510. string constant_name = extension_field.name() + "_FIELD_NUMBER";
  511. UpperString(&constant_name);
  512. printer_->Print("$constant_name$ = $number$\n", "constant_name",
  513. constant_name, "number",
  514. StrCat(extension_field.number()));
  515. printer_->Print("$name$ = ", "name", extension_field.name());
  516. PrintFieldDescriptor(extension_field, is_extension);
  517. printer_->Print("\n");
  518. }
  519. printer_->Print("\n");
  520. }
  521. // Prints Python equivalents of all Descriptors in |file|.
  522. void Generator::PrintMessageDescriptors() const {
  523. for (int i = 0; i < file_->message_type_count(); ++i) {
  524. PrintDescriptor(*file_->message_type(i));
  525. printer_->Print("\n");
  526. }
  527. }
  528. void Generator::PrintServiceDescriptors() const {
  529. for (int i = 0; i < file_->service_count(); ++i) {
  530. PrintServiceDescriptor(*file_->service(i));
  531. AddServiceToFileDescriptor(*file_->service(i));
  532. printer_->Print("\n");
  533. }
  534. }
  535. void Generator::PrintServices() const {
  536. for (int i = 0; i < file_->service_count(); ++i) {
  537. PrintServiceClass(*file_->service(i));
  538. PrintServiceStub(*file_->service(i));
  539. printer_->Print("\n");
  540. }
  541. }
  542. void Generator::PrintServiceDescriptor(
  543. const ServiceDescriptor& descriptor) const {
  544. printer_->Print("\n");
  545. string service_name = ModuleLevelServiceDescriptorName(descriptor);
  546. string options_string;
  547. descriptor.options().SerializeToString(&options_string);
  548. printer_->Print(
  549. "$service_name$ = _descriptor.ServiceDescriptor(\n",
  550. "service_name", service_name);
  551. printer_->Indent();
  552. std::map<string, string> m;
  553. m["name"] = descriptor.name();
  554. m["full_name"] = descriptor.full_name();
  555. m["file"] = kDescriptorKey;
  556. m["index"] = StrCat(descriptor.index());
  557. m["options_value"] = OptionsValue(options_string);
  558. const char required_function_arguments[] =
  559. "name='$name$',\n"
  560. "full_name='$full_name$',\n"
  561. "file=$file$,\n"
  562. "index=$index$,\n"
  563. "serialized_options=$options_value$,\n";
  564. printer_->Print(m, required_function_arguments);
  565. ServiceDescriptorProto sdp;
  566. PrintSerializedPbInterval(descriptor, sdp);
  567. printer_->Print("methods=[\n");
  568. for (int i = 0; i < descriptor.method_count(); ++i) {
  569. const MethodDescriptor* method = descriptor.method(i);
  570. method->options().SerializeToString(&options_string);
  571. m.clear();
  572. m["name"] = method->name();
  573. m["full_name"] = method->full_name();
  574. m["index"] = StrCat(method->index());
  575. m["serialized_options"] = CEscape(options_string);
  576. m["input_type"] = ModuleLevelDescriptorName(*(method->input_type()));
  577. m["output_type"] = ModuleLevelDescriptorName(*(method->output_type()));
  578. m["options_value"] = OptionsValue(options_string);
  579. printer_->Print("_descriptor.MethodDescriptor(\n");
  580. printer_->Indent();
  581. printer_->Print(
  582. m,
  583. "name='$name$',\n"
  584. "full_name='$full_name$',\n"
  585. "index=$index$,\n"
  586. "containing_service=None,\n"
  587. "input_type=$input_type$,\n"
  588. "output_type=$output_type$,\n"
  589. "serialized_options=$options_value$,\n");
  590. printer_->Outdent();
  591. printer_->Print("),\n");
  592. }
  593. printer_->Outdent();
  594. printer_->Print("])\n");
  595. printer_->Print("_sym_db.RegisterServiceDescriptor($name$)\n", "name",
  596. service_name);
  597. printer_->Print("\n");
  598. }
  599. void Generator::PrintDescriptorKeyAndModuleName(
  600. const ServiceDescriptor& descriptor) const {
  601. printer_->Print(
  602. "$descriptor_key$ = $descriptor_name$,\n",
  603. "descriptor_key", kDescriptorKey,
  604. "descriptor_name", ModuleLevelServiceDescriptorName(descriptor));
  605. printer_->Print(
  606. "__module__ = '$module_name$'\n",
  607. "module_name", ModuleName(file_->name()));
  608. }
  609. void Generator::PrintServiceClass(const ServiceDescriptor& descriptor) const {
  610. // Print the service.
  611. printer_->Print("$class_name$ = service_reflection.GeneratedServiceType("
  612. "'$class_name$', (_service.Service,), dict(\n",
  613. "class_name", descriptor.name());
  614. printer_->Indent();
  615. Generator::PrintDescriptorKeyAndModuleName(descriptor);
  616. printer_->Print("))\n\n");
  617. printer_->Outdent();
  618. }
  619. void Generator::PrintServiceStub(const ServiceDescriptor& descriptor) const {
  620. // Print the service stub.
  621. printer_->Print("$class_name$_Stub = "
  622. "service_reflection.GeneratedServiceStubType("
  623. "'$class_name$_Stub', ($class_name$,), dict(\n",
  624. "class_name", descriptor.name());
  625. printer_->Indent();
  626. Generator::PrintDescriptorKeyAndModuleName(descriptor);
  627. printer_->Print("))\n\n");
  628. printer_->Outdent();
  629. }
  630. // Prints statement assigning ModuleLevelDescriptorName(message_descriptor)
  631. // to a Python Descriptor object for message_descriptor.
  632. //
  633. // Mutually recursive with PrintNestedDescriptors().
  634. void Generator::PrintDescriptor(const Descriptor& message_descriptor) const {
  635. PrintNestedDescriptors(message_descriptor);
  636. printer_->Print("\n");
  637. printer_->Print("$descriptor_name$ = _descriptor.Descriptor(\n",
  638. "descriptor_name",
  639. ModuleLevelDescriptorName(message_descriptor));
  640. printer_->Indent();
  641. std::map<string, string> m;
  642. m["name"] = message_descriptor.name();
  643. m["full_name"] = message_descriptor.full_name();
  644. m["file"] = kDescriptorKey;
  645. const char required_function_arguments[] =
  646. "name='$name$',\n"
  647. "full_name='$full_name$',\n"
  648. "filename=None,\n"
  649. "file=$file$,\n"
  650. "containing_type=None,\n";
  651. printer_->Print(m, required_function_arguments);
  652. PrintFieldsInDescriptor(message_descriptor);
  653. PrintExtensionsInDescriptor(message_descriptor);
  654. // Nested types
  655. printer_->Print("nested_types=[");
  656. for (int i = 0; i < message_descriptor.nested_type_count(); ++i) {
  657. const string nested_name = ModuleLevelDescriptorName(
  658. *message_descriptor.nested_type(i));
  659. printer_->Print("$name$, ", "name", nested_name);
  660. }
  661. printer_->Print("],\n");
  662. // Enum types
  663. printer_->Print("enum_types=[\n");
  664. printer_->Indent();
  665. for (int i = 0; i < message_descriptor.enum_type_count(); ++i) {
  666. const string descriptor_name = ModuleLevelDescriptorName(
  667. *message_descriptor.enum_type(i));
  668. printer_->Print(descriptor_name.c_str());
  669. printer_->Print(",\n");
  670. }
  671. printer_->Outdent();
  672. printer_->Print("],\n");
  673. string options_string;
  674. message_descriptor.options().SerializeToString(&options_string);
  675. printer_->Print(
  676. "serialized_options=$options_value$,\n"
  677. "is_extendable=$extendable$,\n"
  678. "syntax='$syntax$'",
  679. "options_value", OptionsValue(options_string),
  680. "extendable", message_descriptor.extension_range_count() > 0 ?
  681. "True" : "False",
  682. "syntax", StringifySyntax(message_descriptor.file()->syntax()));
  683. printer_->Print(",\n");
  684. // Extension ranges
  685. printer_->Print("extension_ranges=[");
  686. for (int i = 0; i < message_descriptor.extension_range_count(); ++i) {
  687. const Descriptor::ExtensionRange* range =
  688. message_descriptor.extension_range(i);
  689. printer_->Print("($start$, $end$), ", "start", StrCat(range->start),
  690. "end", StrCat(range->end));
  691. }
  692. printer_->Print("],\n");
  693. printer_->Print("oneofs=[\n");
  694. printer_->Indent();
  695. for (int i = 0; i < message_descriptor.oneof_decl_count(); ++i) {
  696. const OneofDescriptor* desc = message_descriptor.oneof_decl(i);
  697. std::map<string, string> m;
  698. m["name"] = desc->name();
  699. m["full_name"] = desc->full_name();
  700. m["index"] = StrCat(desc->index());
  701. string options_string =
  702. OptionsValue(desc->options().SerializeAsString());
  703. if (options_string == "None") {
  704. m["serialized_options"] = "";
  705. } else {
  706. m["serialized_options"] = ", serialized_options=" + options_string;
  707. }
  708. printer_->Print(
  709. m,
  710. "_descriptor.OneofDescriptor(\n"
  711. " name='$name$', full_name='$full_name$',\n"
  712. " index=$index$, containing_type=None, "
  713. "fields=[]$serialized_options$),\n");
  714. }
  715. printer_->Outdent();
  716. printer_->Print("],\n");
  717. // Serialization of proto
  718. DescriptorProto edp;
  719. PrintSerializedPbInterval(message_descriptor, edp);
  720. printer_->Outdent();
  721. printer_->Print(")\n");
  722. }
  723. // Prints Python Descriptor objects for all nested types contained in
  724. // message_descriptor.
  725. //
  726. // Mutually recursive with PrintDescriptor().
  727. void Generator::PrintNestedDescriptors(
  728. const Descriptor& containing_descriptor) const {
  729. for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) {
  730. PrintDescriptor(*containing_descriptor.nested_type(i));
  731. }
  732. }
  733. // Prints all messages in |file|.
  734. void Generator::PrintMessages() const {
  735. for (int i = 0; i < file_->message_type_count(); ++i) {
  736. std::vector<string> to_register;
  737. PrintMessage(*file_->message_type(i), "", &to_register, false);
  738. for (int j = 0; j < to_register.size(); ++j) {
  739. printer_->Print("_sym_db.RegisterMessage($name$)\n", "name",
  740. to_register[j]);
  741. }
  742. printer_->Print("\n");
  743. }
  744. }
  745. // Prints a Python class for the given message descriptor. We defer to the
  746. // metaclass to do almost all of the work of actually creating a useful class.
  747. // The purpose of this function and its many helper functions above is merely
  748. // to output a Python version of the descriptors, which the metaclass in
  749. // reflection.py will use to construct the meat of the class itself.
  750. //
  751. // Mutually recursive with PrintNestedMessages().
  752. // Collect nested message names to_register for the symbol_database.
  753. void Generator::PrintMessage(const Descriptor& message_descriptor,
  754. const string& prefix,
  755. std::vector<string>* to_register,
  756. bool is_nested) const {
  757. string qualified_name(prefix + message_descriptor.name());
  758. to_register->push_back(qualified_name);
  759. if (is_nested) {
  760. printer_->Print(
  761. "'$name$' : _reflection.GeneratedProtocolMessageType('$name$', "
  762. "(_message.Message,), {\n",
  763. "name", message_descriptor.name());
  764. } else {
  765. printer_->Print(
  766. "$name$ = _reflection.GeneratedProtocolMessageType('$name$', "
  767. "(_message.Message,), {\n",
  768. "name", message_descriptor.name());
  769. }
  770. printer_->Indent();
  771. PrintNestedMessages(message_descriptor, qualified_name + ".", to_register);
  772. std::map<string, string> m;
  773. m["descriptor_key"] = kDescriptorKey;
  774. m["descriptor_name"] = ModuleLevelDescriptorName(message_descriptor);
  775. printer_->Print(m, "'$descriptor_key$' : $descriptor_name$,\n");
  776. printer_->Print("'__module__' : '$module_name$'\n",
  777. "module_name", ModuleName(file_->name()));
  778. printer_->Print("# @@protoc_insertion_point(class_scope:$full_name$)\n",
  779. "full_name", message_descriptor.full_name());
  780. printer_->Print("})\n");
  781. printer_->Outdent();
  782. }
  783. // Prints all nested messages within |containing_descriptor|.
  784. // Mutually recursive with PrintMessage().
  785. void Generator::PrintNestedMessages(const Descriptor& containing_descriptor,
  786. const string& prefix,
  787. std::vector<string>* to_register) const {
  788. for (int i = 0; i < containing_descriptor.nested_type_count(); ++i) {
  789. printer_->Print("\n");
  790. PrintMessage(*containing_descriptor.nested_type(i), prefix, to_register,
  791. true);
  792. printer_->Print(",\n");
  793. }
  794. }
  795. // Recursively fixes foreign fields in all nested types in |descriptor|, then
  796. // sets the message_type and enum_type of all message and enum fields to point
  797. // to their respective descriptors.
  798. // Args:
  799. // descriptor: descriptor to print fields for.
  800. // containing_descriptor: if descriptor is a nested type, this is its
  801. // containing type, or NULL if this is a root/top-level type.
  802. void Generator::FixForeignFieldsInDescriptor(
  803. const Descriptor& descriptor,
  804. const Descriptor* containing_descriptor) const {
  805. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  806. FixForeignFieldsInDescriptor(*descriptor.nested_type(i), &descriptor);
  807. }
  808. for (int i = 0; i < descriptor.field_count(); ++i) {
  809. const FieldDescriptor& field_descriptor = *descriptor.field(i);
  810. FixForeignFieldsInField(&descriptor, field_descriptor, "fields_by_name");
  811. }
  812. FixContainingTypeInDescriptor(descriptor, containing_descriptor);
  813. for (int i = 0; i < descriptor.enum_type_count(); ++i) {
  814. const EnumDescriptor& enum_descriptor = *descriptor.enum_type(i);
  815. FixContainingTypeInDescriptor(enum_descriptor, &descriptor);
  816. }
  817. for (int i = 0; i < descriptor.oneof_decl_count(); ++i) {
  818. std::map<string, string> m;
  819. const OneofDescriptor* oneof = descriptor.oneof_decl(i);
  820. m["descriptor_name"] = ModuleLevelDescriptorName(descriptor);
  821. m["oneof_name"] = oneof->name();
  822. for (int j = 0; j < oneof->field_count(); ++j) {
  823. m["field_name"] = oneof->field(j)->name();
  824. printer_->Print(
  825. m,
  826. "$descriptor_name$.oneofs_by_name['$oneof_name$'].fields.append(\n"
  827. " $descriptor_name$.fields_by_name['$field_name$'])\n");
  828. printer_->Print(
  829. m,
  830. "$descriptor_name$.fields_by_name['$field_name$'].containing_oneof = "
  831. "$descriptor_name$.oneofs_by_name['$oneof_name$']\n");
  832. }
  833. }
  834. }
  835. void Generator::AddMessageToFileDescriptor(const Descriptor& descriptor) const {
  836. std::map<string, string> m;
  837. m["descriptor_name"] = kDescriptorKey;
  838. m["message_name"] = descriptor.name();
  839. m["message_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
  840. const char file_descriptor_template[] =
  841. "$descriptor_name$.message_types_by_name['$message_name$'] = "
  842. "$message_descriptor_name$\n";
  843. printer_->Print(m, file_descriptor_template);
  844. }
  845. void Generator::AddServiceToFileDescriptor(
  846. const ServiceDescriptor& descriptor) const {
  847. std::map<string, string> m;
  848. m["descriptor_name"] = kDescriptorKey;
  849. m["service_name"] = descriptor.name();
  850. m["service_descriptor_name"] = ModuleLevelServiceDescriptorName(descriptor);
  851. const char file_descriptor_template[] =
  852. "$descriptor_name$.services_by_name['$service_name$'] = "
  853. "$service_descriptor_name$\n";
  854. printer_->Print(m, file_descriptor_template);
  855. }
  856. void Generator::AddEnumToFileDescriptor(
  857. const EnumDescriptor& descriptor) const {
  858. std::map<string, string> m;
  859. m["descriptor_name"] = kDescriptorKey;
  860. m["enum_name"] = descriptor.name();
  861. m["enum_descriptor_name"] = ModuleLevelDescriptorName(descriptor);
  862. const char file_descriptor_template[] =
  863. "$descriptor_name$.enum_types_by_name['$enum_name$'] = "
  864. "$enum_descriptor_name$\n";
  865. printer_->Print(m, file_descriptor_template);
  866. }
  867. void Generator::AddExtensionToFileDescriptor(
  868. const FieldDescriptor& descriptor) const {
  869. std::map<string, string> m;
  870. m["descriptor_name"] = kDescriptorKey;
  871. m["field_name"] = descriptor.name();
  872. const char file_descriptor_template[] =
  873. "$descriptor_name$.extensions_by_name['$field_name$'] = "
  874. "$field_name$\n";
  875. printer_->Print(m, file_descriptor_template);
  876. }
  877. // Sets any necessary message_type and enum_type attributes
  878. // for the Python version of |field|.
  879. //
  880. // containing_type may be NULL, in which case this is a module-level field.
  881. //
  882. // python_dict_name is the name of the Python dict where we should
  883. // look the field up in the containing type. (e.g., fields_by_name
  884. // or extensions_by_name). We ignore python_dict_name if containing_type
  885. // is NULL.
  886. void Generator::FixForeignFieldsInField(const Descriptor* containing_type,
  887. const FieldDescriptor& field,
  888. const string& python_dict_name) const {
  889. const string field_referencing_expression = FieldReferencingExpression(
  890. containing_type, field, python_dict_name);
  891. std::map<string, string> m;
  892. m["field_ref"] = field_referencing_expression;
  893. const Descriptor* foreign_message_type = field.message_type();
  894. if (foreign_message_type) {
  895. m["foreign_type"] = ModuleLevelDescriptorName(*foreign_message_type);
  896. printer_->Print(m, "$field_ref$.message_type = $foreign_type$\n");
  897. }
  898. const EnumDescriptor* enum_type = field.enum_type();
  899. if (enum_type) {
  900. m["enum_type"] = ModuleLevelDescriptorName(*enum_type);
  901. printer_->Print(m, "$field_ref$.enum_type = $enum_type$\n");
  902. }
  903. }
  904. // Returns the module-level expression for the given FieldDescriptor.
  905. // Only works for fields in the .proto file this Generator is generating for.
  906. //
  907. // containing_type may be NULL, in which case this is a module-level field.
  908. //
  909. // python_dict_name is the name of the Python dict where we should
  910. // look the field up in the containing type. (e.g., fields_by_name
  911. // or extensions_by_name). We ignore python_dict_name if containing_type
  912. // is NULL.
  913. string Generator::FieldReferencingExpression(
  914. const Descriptor* containing_type,
  915. const FieldDescriptor& field,
  916. const string& python_dict_name) const {
  917. // We should only ever be looking up fields in the current file.
  918. // The only things we refer to from other files are message descriptors.
  919. GOOGLE_CHECK_EQ(field.file(), file_) << field.file()->name() << " vs. "
  920. << file_->name();
  921. if (!containing_type) {
  922. return field.name();
  923. }
  924. return strings::Substitute(
  925. "$0.$1['$2']",
  926. ModuleLevelDescriptorName(*containing_type),
  927. python_dict_name, field.name());
  928. }
  929. // Prints containing_type for nested descriptors or enum descriptors.
  930. template <typename DescriptorT>
  931. void Generator::FixContainingTypeInDescriptor(
  932. const DescriptorT& descriptor,
  933. const Descriptor* containing_descriptor) const {
  934. if (containing_descriptor != NULL) {
  935. const string nested_name = ModuleLevelDescriptorName(descriptor);
  936. const string parent_name = ModuleLevelDescriptorName(
  937. *containing_descriptor);
  938. printer_->Print(
  939. "$nested_name$.containing_type = $parent_name$\n",
  940. "nested_name", nested_name,
  941. "parent_name", parent_name);
  942. }
  943. }
  944. // Prints statements setting the message_type and enum_type fields in the
  945. // Python descriptor objects we've already output in ths file. We must
  946. // do this in a separate step due to circular references (otherwise, we'd
  947. // just set everything in the initial assignment statements).
  948. void Generator::FixForeignFieldsInDescriptors() const {
  949. for (int i = 0; i < file_->message_type_count(); ++i) {
  950. FixForeignFieldsInDescriptor(*file_->message_type(i), NULL);
  951. }
  952. for (int i = 0; i < file_->message_type_count(); ++i) {
  953. AddMessageToFileDescriptor(*file_->message_type(i));
  954. }
  955. for (int i = 0; i < file_->enum_type_count(); ++i) {
  956. AddEnumToFileDescriptor(*file_->enum_type(i));
  957. }
  958. for (int i = 0; i < file_->extension_count(); ++i) {
  959. AddExtensionToFileDescriptor(*file_->extension(i));
  960. }
  961. // TODO(jieluo): Move this register to PrintFileDescriptor() when
  962. // FieldDescriptor.file is added in generated file.
  963. printer_->Print("_sym_db.RegisterFileDescriptor($name$)\n", "name",
  964. kDescriptorKey);
  965. printer_->Print("\n");
  966. }
  967. // We need to not only set any necessary message_type fields, but
  968. // also need to call RegisterExtension() on each message we're
  969. // extending.
  970. void Generator::FixForeignFieldsInExtensions() const {
  971. // Top-level extensions.
  972. for (int i = 0; i < file_->extension_count(); ++i) {
  973. FixForeignFieldsInExtension(*file_->extension(i));
  974. }
  975. // Nested extensions.
  976. for (int i = 0; i < file_->message_type_count(); ++i) {
  977. FixForeignFieldsInNestedExtensions(*file_->message_type(i));
  978. }
  979. printer_->Print("\n");
  980. }
  981. void Generator::FixForeignFieldsInExtension(
  982. const FieldDescriptor& extension_field) const {
  983. GOOGLE_CHECK(extension_field.is_extension());
  984. // extension_scope() will be NULL for top-level extensions, which is
  985. // exactly what FixForeignFieldsInField() wants.
  986. FixForeignFieldsInField(extension_field.extension_scope(), extension_field,
  987. "extensions_by_name");
  988. std::map<string, string> m;
  989. // Confusingly, for FieldDescriptors that happen to be extensions,
  990. // containing_type() means "extended type."
  991. // On the other hand, extension_scope() will give us what we normally
  992. // mean by containing_type().
  993. m["extended_message_class"] = ModuleLevelMessageName(
  994. *extension_field.containing_type());
  995. m["field"] = FieldReferencingExpression(extension_field.extension_scope(),
  996. extension_field,
  997. "extensions_by_name");
  998. printer_->Print(m, "$extended_message_class$.RegisterExtension($field$)\n");
  999. }
  1000. void Generator::FixForeignFieldsInNestedExtensions(
  1001. const Descriptor& descriptor) const {
  1002. // Recursively fix up extensions in all nested types.
  1003. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  1004. FixForeignFieldsInNestedExtensions(*descriptor.nested_type(i));
  1005. }
  1006. // Fix up extensions directly contained within this type.
  1007. for (int i = 0; i < descriptor.extension_count(); ++i) {
  1008. FixForeignFieldsInExtension(*descriptor.extension(i));
  1009. }
  1010. }
  1011. // Returns a Python expression that instantiates a Python EnumValueDescriptor
  1012. // object for the given C++ descriptor.
  1013. void Generator::PrintEnumValueDescriptor(
  1014. const EnumValueDescriptor& descriptor) const {
  1015. // TODO(robinson): Fix up EnumValueDescriptor "type" fields.
  1016. // More circular references. ::sigh::
  1017. string options_string;
  1018. descriptor.options().SerializeToString(&options_string);
  1019. std::map<string, string> m;
  1020. m["name"] = descriptor.name();
  1021. m["index"] = StrCat(descriptor.index());
  1022. m["number"] = StrCat(descriptor.number());
  1023. m["options"] = OptionsValue(options_string);
  1024. printer_->Print(
  1025. m,
  1026. "_descriptor.EnumValueDescriptor(\n"
  1027. " name='$name$', index=$index$, number=$number$,\n"
  1028. " serialized_options=$options$,\n"
  1029. " type=None)");
  1030. }
  1031. // Returns a CEscaped string of serialized_options.
  1032. string Generator::OptionsValue(const string& serialized_options) const {
  1033. if (serialized_options.length() == 0 || GeneratingDescriptorProto()) {
  1034. return "None";
  1035. } else {
  1036. //##!PY25 return "b'('" + CEscape(serialized_options)+ "')";
  1037. return "_b('"+ CEscape(serialized_options) + "')"; //##PY25
  1038. }
  1039. }
  1040. // Prints an expression for a Python FieldDescriptor for |field|.
  1041. void Generator::PrintFieldDescriptor(
  1042. const FieldDescriptor& field, bool is_extension) const {
  1043. string options_string;
  1044. field.options().SerializeToString(&options_string);
  1045. std::map<string, string> m;
  1046. m["name"] = field.name();
  1047. m["full_name"] = field.full_name();
  1048. m["index"] = StrCat(field.index());
  1049. m["number"] = StrCat(field.number());
  1050. m["type"] = StrCat(field.type());
  1051. m["cpp_type"] = StrCat(field.cpp_type());
  1052. m["label"] = StrCat(field.label());
  1053. m["has_default_value"] = field.has_default_value() ? "True" : "False";
  1054. m["default_value"] = StringifyDefaultValue(field);
  1055. m["is_extension"] = is_extension ? "True" : "False";
  1056. m["serialized_options"] = OptionsValue(options_string);
  1057. m["json_name"] = field.has_json_name() ?
  1058. ", json_name='" + field.json_name() + "'": "";
  1059. // We always set message_type and enum_type to None at this point, and then
  1060. // these fields in correctly after all referenced descriptors have been
  1061. // defined and/or imported (see FixForeignFieldsInDescriptors()).
  1062. const char field_descriptor_decl[] =
  1063. "_descriptor.FieldDescriptor(\n"
  1064. " name='$name$', full_name='$full_name$', index=$index$,\n"
  1065. " number=$number$, type=$type$, cpp_type=$cpp_type$, label=$label$,\n"
  1066. " has_default_value=$has_default_value$, default_value=$default_value$,\n"
  1067. " message_type=None, enum_type=None, containing_type=None,\n"
  1068. " is_extension=$is_extension$, extension_scope=None,\n"
  1069. " serialized_options=$serialized_options$$json_name$, file=DESCRIPTOR)";
  1070. printer_->Print(m, field_descriptor_decl);
  1071. }
  1072. // Helper for Print{Fields,Extensions}InDescriptor().
  1073. void Generator::PrintFieldDescriptorsInDescriptor(
  1074. const Descriptor& message_descriptor,
  1075. bool is_extension,
  1076. const string& list_variable_name,
  1077. int (Descriptor::*CountFn)() const,
  1078. const FieldDescriptor* (Descriptor::*GetterFn)(int) const) const {
  1079. printer_->Print("$list$=[\n", "list", list_variable_name);
  1080. printer_->Indent();
  1081. for (int i = 0; i < (message_descriptor.*CountFn)(); ++i) {
  1082. PrintFieldDescriptor(*(message_descriptor.*GetterFn)(i),
  1083. is_extension);
  1084. printer_->Print(",\n");
  1085. }
  1086. printer_->Outdent();
  1087. printer_->Print("],\n");
  1088. }
  1089. // Prints a statement assigning "fields" to a list of Python FieldDescriptors,
  1090. // one for each field present in message_descriptor.
  1091. void Generator::PrintFieldsInDescriptor(
  1092. const Descriptor& message_descriptor) const {
  1093. const bool is_extension = false;
  1094. PrintFieldDescriptorsInDescriptor(
  1095. message_descriptor, is_extension, "fields",
  1096. &Descriptor::field_count, &Descriptor::field);
  1097. }
  1098. // Prints a statement assigning "extensions" to a list of Python
  1099. // FieldDescriptors, one for each extension present in message_descriptor.
  1100. void Generator::PrintExtensionsInDescriptor(
  1101. const Descriptor& message_descriptor) const {
  1102. const bool is_extension = true;
  1103. PrintFieldDescriptorsInDescriptor(
  1104. message_descriptor, is_extension, "extensions",
  1105. &Descriptor::extension_count, &Descriptor::extension);
  1106. }
  1107. bool Generator::GeneratingDescriptorProto() const {
  1108. return file_->name() == "net/proto2/proto/descriptor.proto" ||
  1109. file_->name() == "google/protobuf/descriptor.proto";
  1110. }
  1111. // Returns the unique Python module-level identifier given to a descriptor.
  1112. // This name is module-qualified iff the given descriptor describes an
  1113. // entity that doesn't come from the current file.
  1114. template <typename DescriptorT>
  1115. string Generator::ModuleLevelDescriptorName(
  1116. const DescriptorT& descriptor) const {
  1117. // FIXME(robinson):
  1118. // We currently don't worry about collisions with underscores in the type
  1119. // names, so these would collide in nasty ways if found in the same file:
  1120. // OuterProto.ProtoA.ProtoB
  1121. // OuterProto_ProtoA.ProtoB # Underscore instead of period.
  1122. // As would these:
  1123. // OuterProto.ProtoA_.ProtoB
  1124. // OuterProto.ProtoA._ProtoB # Leading vs. trailing underscore.
  1125. // (Contrived, but certainly possible).
  1126. //
  1127. // The C++ implementation doesn't guard against this either. Leaving
  1128. // it for now...
  1129. string name = NamePrefixedWithNestedTypes(descriptor, "_");
  1130. UpperString(&name);
  1131. // Module-private for now. Easy to make public later; almost impossible
  1132. // to make private later.
  1133. name = "_" + name;
  1134. // We now have the name relative to its own module. Also qualify with
  1135. // the module name iff this descriptor is from a different .proto file.
  1136. if (descriptor.file() != file_) {
  1137. name = ModuleAlias(descriptor.file()->name()) + "." + name;
  1138. }
  1139. return name;
  1140. }
  1141. // Returns the name of the message class itself, not the descriptor.
  1142. // Like ModuleLevelDescriptorName(), module-qualifies the name iff
  1143. // the given descriptor describes an entity that doesn't come from
  1144. // the current file.
  1145. string Generator::ModuleLevelMessageName(const Descriptor& descriptor) const {
  1146. string name = NamePrefixedWithNestedTypes(descriptor, ".");
  1147. if (descriptor.file() != file_) {
  1148. name = ModuleAlias(descriptor.file()->name()) + "." + name;
  1149. }
  1150. return name;
  1151. }
  1152. // Returns the unique Python module-level identifier given to a service
  1153. // descriptor.
  1154. string Generator::ModuleLevelServiceDescriptorName(
  1155. const ServiceDescriptor& descriptor) const {
  1156. string name = descriptor.name();
  1157. UpperString(&name);
  1158. name = "_" + name;
  1159. if (descriptor.file() != file_) {
  1160. name = ModuleAlias(descriptor.file()->name()) + "." + name;
  1161. }
  1162. return name;
  1163. }
  1164. // Prints standard constructor arguments serialized_start and serialized_end.
  1165. // Args:
  1166. // descriptor: The cpp descriptor to have a serialized reference.
  1167. // proto: A proto
  1168. // Example printer output:
  1169. // serialized_start=41,
  1170. // serialized_end=43,
  1171. //
  1172. template <typename DescriptorT, typename DescriptorProtoT>
  1173. void Generator::PrintSerializedPbInterval(
  1174. const DescriptorT& descriptor, DescriptorProtoT& proto) const {
  1175. descriptor.CopyTo(&proto);
  1176. string sp;
  1177. proto.SerializeToString(&sp);
  1178. int offset = file_descriptor_serialized_.find(sp);
  1179. GOOGLE_CHECK_GE(offset, 0);
  1180. printer_->Print(
  1181. "serialized_start=$serialized_start$,\n"
  1182. "serialized_end=$serialized_end$,\n",
  1183. "serialized_start", StrCat(offset), "serialized_end",
  1184. StrCat(offset + sp.size()));
  1185. }
  1186. namespace {
  1187. void PrintDescriptorOptionsFixingCode(const string& descriptor,
  1188. const string& options,
  1189. io::Printer* printer) {
  1190. // Reset the _options to None thus DescriptorBase.GetOptions() can
  1191. // parse _options again after extensions are registered.
  1192. printer->Print(
  1193. "$descriptor$._options = None\n",
  1194. "descriptor", descriptor);
  1195. }
  1196. } // namespace
  1197. // Prints expressions that set the options field of all descriptors.
  1198. void Generator::FixAllDescriptorOptions() const {
  1199. // Prints an expression that sets the file descriptor's options.
  1200. string file_options = OptionsValue(file_->options().SerializeAsString());
  1201. if (file_options != "None") {
  1202. PrintDescriptorOptionsFixingCode(kDescriptorKey, file_options, printer_);
  1203. }
  1204. // Prints expressions that set the options for all top level enums.
  1205. for (int i = 0; i < file_->enum_type_count(); ++i) {
  1206. const EnumDescriptor& enum_descriptor = *file_->enum_type(i);
  1207. FixOptionsForEnum(enum_descriptor);
  1208. }
  1209. // Prints expressions that set the options for all top level extensions.
  1210. for (int i = 0; i < file_->extension_count(); ++i) {
  1211. const FieldDescriptor& field = *file_->extension(i);
  1212. FixOptionsForField(field);
  1213. }
  1214. // Prints expressions that set the options for all messages, nested enums,
  1215. // nested extensions and message fields.
  1216. for (int i = 0; i < file_->message_type_count(); ++i) {
  1217. FixOptionsForMessage(*file_->message_type(i));
  1218. }
  1219. }
  1220. void Generator::FixOptionsForOneof(const OneofDescriptor& oneof) const {
  1221. string oneof_options = OptionsValue(oneof.options().SerializeAsString());
  1222. if (oneof_options != "None") {
  1223. string oneof_name = strings::Substitute(
  1224. "$0.$1['$2']",
  1225. ModuleLevelDescriptorName(*oneof.containing_type()),
  1226. "oneofs_by_name", oneof.name());
  1227. PrintDescriptorOptionsFixingCode(oneof_name, oneof_options, printer_);
  1228. }
  1229. }
  1230. // Prints expressions that set the options for an enum descriptor and its
  1231. // value descriptors.
  1232. void Generator::FixOptionsForEnum(const EnumDescriptor& enum_descriptor) const {
  1233. string descriptor_name = ModuleLevelDescriptorName(enum_descriptor);
  1234. string enum_options = OptionsValue(
  1235. enum_descriptor.options().SerializeAsString());
  1236. if (enum_options != "None") {
  1237. PrintDescriptorOptionsFixingCode(descriptor_name, enum_options, printer_);
  1238. }
  1239. for (int i = 0; i < enum_descriptor.value_count(); ++i) {
  1240. const EnumValueDescriptor& value_descriptor = *enum_descriptor.value(i);
  1241. string value_options = OptionsValue(
  1242. value_descriptor.options().SerializeAsString());
  1243. if (value_options != "None") {
  1244. PrintDescriptorOptionsFixingCode(
  1245. StringPrintf("%s.values_by_name[\"%s\"]", descriptor_name.c_str(),
  1246. value_descriptor.name().c_str()),
  1247. value_options, printer_);
  1248. }
  1249. }
  1250. }
  1251. // Prints expressions that set the options for field descriptors (including
  1252. // extensions).
  1253. void Generator::FixOptionsForField(
  1254. const FieldDescriptor& field) const {
  1255. string field_options = OptionsValue(field.options().SerializeAsString());
  1256. if (field_options != "None") {
  1257. string field_name;
  1258. if (field.is_extension()) {
  1259. if (field.extension_scope() == NULL) {
  1260. // Top level extensions.
  1261. field_name = field.name();
  1262. } else {
  1263. field_name = FieldReferencingExpression(
  1264. field.extension_scope(), field, "extensions_by_name");
  1265. }
  1266. } else {
  1267. field_name = FieldReferencingExpression(
  1268. field.containing_type(), field, "fields_by_name");
  1269. }
  1270. PrintDescriptorOptionsFixingCode(field_name, field_options, printer_);
  1271. }
  1272. }
  1273. // Prints expressions that set the options for a message and all its inner
  1274. // types (nested messages, nested enums, extensions, fields).
  1275. void Generator::FixOptionsForMessage(const Descriptor& descriptor) const {
  1276. // Nested messages.
  1277. for (int i = 0; i < descriptor.nested_type_count(); ++i) {
  1278. FixOptionsForMessage(*descriptor.nested_type(i));
  1279. }
  1280. // Oneofs.
  1281. for (int i = 0; i < descriptor.oneof_decl_count(); ++i) {
  1282. FixOptionsForOneof(*descriptor.oneof_decl(i));
  1283. }
  1284. // Enums.
  1285. for (int i = 0; i < descriptor.enum_type_count(); ++i) {
  1286. FixOptionsForEnum(*descriptor.enum_type(i));
  1287. }
  1288. // Fields.
  1289. for (int i = 0; i < descriptor.field_count(); ++i) {
  1290. const FieldDescriptor& field = *descriptor.field(i);
  1291. FixOptionsForField(field);
  1292. }
  1293. // Extensions.
  1294. for (int i = 0; i < descriptor.extension_count(); ++i) {
  1295. const FieldDescriptor& field = *descriptor.extension(i);
  1296. FixOptionsForField(field);
  1297. }
  1298. // Message option for this message.
  1299. string message_options = OptionsValue(
  1300. descriptor.options().SerializeAsString());
  1301. if (message_options != "None") {
  1302. string descriptor_name = ModuleLevelDescriptorName(descriptor);
  1303. PrintDescriptorOptionsFixingCode(descriptor_name,
  1304. message_options,
  1305. printer_);
  1306. }
  1307. }
  1308. // If a dependency forwards other files through public dependencies, let's
  1309. // copy over the corresponding module aliases.
  1310. void Generator::CopyPublicDependenciesAliases(
  1311. const string& copy_from, const FileDescriptor* file) const {
  1312. for (int i = 0; i < file->public_dependency_count(); ++i) {
  1313. string module_name = ModuleName(file->public_dependency(i)->name());
  1314. string module_alias = ModuleAlias(file->public_dependency(i)->name());
  1315. // There's no module alias in the dependent file if it was generated by
  1316. // an old protoc (less than 3.0.0-alpha-1). Use module name in this
  1317. // situation.
  1318. printer_->Print("try:\n"
  1319. " $alias$ = $copy_from$.$alias$\n"
  1320. "except AttributeError:\n"
  1321. " $alias$ = $copy_from$.$module$\n",
  1322. "alias", module_alias,
  1323. "module", module_name,
  1324. "copy_from", copy_from);
  1325. CopyPublicDependenciesAliases(copy_from, file->public_dependency(i));
  1326. }
  1327. }
  1328. } // namespace python
  1329. } // namespace compiler
  1330. } // namespace protobuf
  1331. } // namespace google