js_generator.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. // Generates JavaScript code for a given .proto file.
  31. //
  32. #ifndef GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__
  33. #define GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__
  34. #include <set>
  35. #include <string>
  36. #include <google/protobuf/stubs/logging.h>
  37. #include <google/protobuf/stubs/common.h>
  38. #include <google/protobuf/compiler/scc.h>
  39. #include <google/protobuf/compiler/code_generator.h>
  40. #include <google/protobuf/port_def.inc>
  41. namespace google {
  42. namespace protobuf {
  43. class Descriptor;
  44. class EnumDescriptor;
  45. class FieldDescriptor;
  46. class OneofDescriptor;
  47. class FileDescriptor;
  48. namespace io {
  49. class Printer;
  50. }
  51. namespace compiler {
  52. namespace js {
  53. struct GeneratorOptions {
  54. // Output path.
  55. std::string output_dir;
  56. // Namespace prefix.
  57. std::string namespace_prefix;
  58. // Enable binary-format support?
  59. bool binary;
  60. // What style of imports should be used.
  61. enum ImportStyle {
  62. kImportClosure, // goog.require()
  63. kImportCommonJs, // require()
  64. kImportCommonJsStrict, // require() with no global export
  65. kImportBrowser, // no import statements
  66. kImportEs6, // import { member } from ''
  67. } import_style;
  68. GeneratorOptions()
  69. : output_dir("."),
  70. namespace_prefix(""),
  71. binary(false),
  72. import_style(kImportClosure),
  73. add_require_for_enums(false),
  74. testonly(false),
  75. library(""),
  76. error_on_name_conflict(false),
  77. extension(".js"),
  78. one_output_file_per_input_file(false),
  79. annotate_code(false) {}
  80. bool ParseFromOptions(
  81. const std::vector<std::pair<std::string, std::string> >& options,
  82. std::string* error);
  83. // Returns the file name extension to use for generated code.
  84. std::string GetFileNameExtension() const {
  85. return import_style == kImportClosure ? extension : "_pb.js";
  86. }
  87. enum OutputMode {
  88. // Create an output file for each input .proto file.
  89. kOneOutputFilePerInputFile,
  90. // Create an output file for each type.
  91. kOneOutputFilePerSCC,
  92. // Put everything in a single file named by the library option.
  93. kEverythingInOneFile,
  94. };
  95. // Indicates how to output the generated code based on the provided options.
  96. OutputMode output_mode() const;
  97. // The remaining options are only relevant when we are using kImportClosure.
  98. // Add a `goog.requires()` call for each enum type used. If not set, a
  99. // forward declaration with `goog.forwardDeclare` is produced instead.
  100. bool add_require_for_enums;
  101. // Set this as a test-only module via `goog.setTestOnly();`.
  102. bool testonly;
  103. // Create a library with name <name>_lib.js rather than a separate .js file
  104. // per type?
  105. std::string library;
  106. // Error if there are two types that would generate the same output file?
  107. bool error_on_name_conflict;
  108. // The extension to use for output file names.
  109. std::string extension;
  110. // Create a separate output file for each input file?
  111. bool one_output_file_per_input_file;
  112. // If true, we should append annotations as commen on the last line for
  113. // generated .js file. Annotations used by tools like https://kythe.io
  114. // to provide cross-references between .js and .proto files. Annotations
  115. // are enced as base64 proto of GeneratedCodeInfo message (see
  116. // descriptor.proto).
  117. bool annotate_code;
  118. };
  119. // CodeGenerator implementation which generates a JavaScript source file and
  120. // header. If you create your own protocol compiler binary and you want it to
  121. // support JavaScript output, you can do so by registering an instance of this
  122. // CodeGenerator with the CommandLineInterface in your main() function.
  123. class PROTOC_EXPORT Generator : public CodeGenerator {
  124. public:
  125. Generator() {}
  126. virtual ~Generator() {}
  127. virtual bool Generate(const FileDescriptor* file,
  128. const std::string& parameter, GeneratorContext* context,
  129. std::string* error) const {
  130. *error = "Unimplemented Generate() method. Call GenerateAll() instead.";
  131. return false;
  132. }
  133. virtual bool HasGenerateAll() const { return true; }
  134. virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
  135. const std::string& parameter,
  136. GeneratorContext* context, std::string* error) const;
  137. private:
  138. void GenerateHeader(const GeneratorOptions& options,
  139. io::Printer* printer) const;
  140. // Generate goog.provides() calls.
  141. void FindProvides(const GeneratorOptions& options, io::Printer* printer,
  142. const std::vector<const FileDescriptor*>& file,
  143. std::set<std::string>* provided) const;
  144. void FindProvidesForFile(const GeneratorOptions& options,
  145. io::Printer* printer, const FileDescriptor* file,
  146. std::set<std::string>* provided) const;
  147. void FindProvidesForMessage(const GeneratorOptions& options,
  148. io::Printer* printer, const Descriptor* desc,
  149. std::set<std::string>* provided) const;
  150. void FindProvidesForEnum(const GeneratorOptions& options,
  151. io::Printer* printer, const EnumDescriptor* enumdesc,
  152. std::set<std::string>* provided) const;
  153. // For extension fields at file scope.
  154. void FindProvidesForFields(const GeneratorOptions& options,
  155. io::Printer* printer,
  156. const std::vector<const FieldDescriptor*>& fields,
  157. std::set<std::string>* provided) const;
  158. // Print the goog.provides() found by the methods above.
  159. void GenerateProvides(const GeneratorOptions& options, io::Printer* printer,
  160. std::set<std::string>* provided) const;
  161. // Generate goog.setTestOnly() if indicated.
  162. void GenerateTestOnly(const GeneratorOptions& options,
  163. io::Printer* printer) const;
  164. // Generate goog.requires() calls.
  165. void GenerateRequiresForLibrary(
  166. const GeneratorOptions& options, io::Printer* printer,
  167. const std::vector<const FileDescriptor*>& files,
  168. std::set<std::string>* provided) const;
  169. void GenerateRequiresForSCC(const GeneratorOptions& options,
  170. io::Printer* printer, const SCC* scc,
  171. std::set<std::string>* provided) const;
  172. // For extension fields at file scope.
  173. void GenerateRequiresForExtensions(
  174. const GeneratorOptions& options, io::Printer* printer,
  175. const std::vector<const FieldDescriptor*>& fields,
  176. std::set<std::string>* provided) const;
  177. void GenerateRequiresImpl(const GeneratorOptions& options,
  178. io::Printer* printer,
  179. std::set<std::string>* required,
  180. std::set<std::string>* forwards,
  181. std::set<std::string>* provided, bool require_jspb,
  182. bool require_extension, bool require_map) const;
  183. void FindRequiresForMessage(const GeneratorOptions& options,
  184. const Descriptor* desc,
  185. std::set<std::string>* required,
  186. std::set<std::string>* forwards,
  187. bool* have_message) const;
  188. void FindRequiresForField(const GeneratorOptions& options,
  189. const FieldDescriptor* field,
  190. std::set<std::string>* required,
  191. std::set<std::string>* forwards) const;
  192. void FindRequiresForExtension(const GeneratorOptions& options,
  193. const FieldDescriptor* field,
  194. std::set<std::string>* required,
  195. std::set<std::string>* forwards) const;
  196. // Generate all things in a proto file into one file.
  197. // If use_short_name is true, the generated file's name will only be short
  198. // name that without directory, otherwise filename equals file->name()
  199. bool GenerateFile(const FileDescriptor* file, const GeneratorOptions& options,
  200. GeneratorContext* context, bool use_short_name) const;
  201. void GenerateFile(const GeneratorOptions& options, io::Printer* printer,
  202. const FileDescriptor* file) const;
  203. // Generate definitions for all message classes and enums in all files,
  204. // processing the files in dependence order.
  205. void GenerateFilesInDepOrder(
  206. const GeneratorOptions& options, io::Printer* printer,
  207. const std::vector<const FileDescriptor*>& file) const;
  208. // Helper for above.
  209. void GenerateFileAndDeps(const GeneratorOptions& options,
  210. io::Printer* printer, const FileDescriptor* root,
  211. std::set<const FileDescriptor*>* all_files,
  212. std::set<const FileDescriptor*>* generated) const;
  213. // Generate definitions for all message classes and enums.
  214. void GenerateClassesAndEnums(const GeneratorOptions& options,
  215. io::Printer* printer,
  216. const FileDescriptor* file) const;
  217. void GenerateFieldValueExpression(io::Printer* printer,
  218. const char* obj_reference,
  219. const FieldDescriptor* field,
  220. bool use_default) const;
  221. // Generate definition for one class.
  222. void GenerateClass(const GeneratorOptions& options, io::Printer* printer,
  223. const Descriptor* desc) const;
  224. void GenerateClassConstructor(const GeneratorOptions& options,
  225. io::Printer* printer,
  226. const Descriptor* desc) const;
  227. void GenerateClassFieldInfo(const GeneratorOptions& options,
  228. io::Printer* printer,
  229. const Descriptor* desc) const;
  230. void GenerateClassConstructorAndDeclareExtensionFieldInfo(
  231. const GeneratorOptions& options, io::Printer* printer,
  232. const Descriptor* desc) const;
  233. void GenerateClassXid(const GeneratorOptions& options, io::Printer* printer,
  234. const Descriptor* desc) const;
  235. void GenerateOneofCaseDefinition(const GeneratorOptions& options,
  236. io::Printer* printer,
  237. const OneofDescriptor* oneof) const;
  238. void GenerateObjectTypedef(const GeneratorOptions& options,
  239. io::Printer* printer,
  240. const Descriptor* desc) const;
  241. void GenerateClassToObject(const GeneratorOptions& options,
  242. io::Printer* printer,
  243. const Descriptor* desc) const;
  244. void GenerateClassFieldToObject(const GeneratorOptions& options,
  245. io::Printer* printer,
  246. const FieldDescriptor* field) const;
  247. void GenerateClassFromObject(const GeneratorOptions& options,
  248. io::Printer* printer,
  249. const Descriptor* desc) const;
  250. void GenerateClassFieldFromObject(const GeneratorOptions& options,
  251. io::Printer* printer,
  252. const FieldDescriptor* field) const;
  253. void GenerateClassRegistration(const GeneratorOptions& options,
  254. io::Printer* printer,
  255. const Descriptor* desc) const;
  256. void GenerateClassFields(const GeneratorOptions& options,
  257. io::Printer* printer, const Descriptor* desc) const;
  258. void GenerateClassField(const GeneratorOptions& options, io::Printer* printer,
  259. const FieldDescriptor* desc) const;
  260. void GenerateClassExtensionFieldInfo(const GeneratorOptions& options,
  261. io::Printer* printer,
  262. const Descriptor* desc) const;
  263. void GenerateClassDeserialize(const GeneratorOptions& options,
  264. io::Printer* printer,
  265. const Descriptor* desc) const;
  266. void GenerateClassDeserializeBinary(const GeneratorOptions& options,
  267. io::Printer* printer,
  268. const Descriptor* desc) const;
  269. void GenerateClassDeserializeBinaryField(const GeneratorOptions& options,
  270. io::Printer* printer,
  271. const FieldDescriptor* field) const;
  272. void GenerateClassSerializeBinary(const GeneratorOptions& options,
  273. io::Printer* printer,
  274. const Descriptor* desc) const;
  275. void GenerateClassSerializeBinaryField(const GeneratorOptions& options,
  276. io::Printer* printer,
  277. const FieldDescriptor* field) const;
  278. // Generate definition for one enum.
  279. void GenerateEnum(const GeneratorOptions& options, io::Printer* printer,
  280. const EnumDescriptor* enumdesc) const;
  281. // Generate an extension definition.
  282. void GenerateExtension(const GeneratorOptions& options, io::Printer* printer,
  283. const FieldDescriptor* field) const;
  284. // Generate addFoo() method for repeated primitive fields.
  285. void GenerateRepeatedPrimitiveHelperMethods(const GeneratorOptions& options,
  286. io::Printer* printer,
  287. const FieldDescriptor* field,
  288. bool untyped) const;
  289. // Generate addFoo() method for repeated message fields.
  290. void GenerateRepeatedMessageHelperMethods(const GeneratorOptions& options,
  291. io::Printer* printer,
  292. const FieldDescriptor* field) const;
  293. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(Generator);
  294. };
  295. } // namespace js
  296. } // namespace compiler
  297. } // namespace protobuf
  298. } // namespace google
  299. #include <google/protobuf/port_undef.inc>
  300. #endif // GOOGLE_PROTOBUF_COMPILER_JS_GENERATOR_H__