python_generator.cc 54 KB

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