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