code_generator.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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. // Author: kenton@google.com (Kenton Varda)
  31. // Based on original Protocol Buffers design by
  32. // Sanjay Ghemawat, Jeff Dean, and others.
  33. //
  34. // Defines the abstract interface implemented by each of the language-specific
  35. // code generators.
  36. #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
  37. #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
  38. #include <string>
  39. #include <utility>
  40. #include <vector>
  41. #include <google/protobuf/stubs/common.h>
  42. #include <google/protobuf/port_def.inc>
  43. namespace google {
  44. namespace protobuf {
  45. namespace io {
  46. class ZeroCopyOutputStream;
  47. }
  48. class FileDescriptor;
  49. class GeneratedCodeInfo;
  50. namespace compiler {
  51. class AccessInfoMap;
  52. class Version;
  53. // Defined in this file.
  54. class CodeGenerator;
  55. class GeneratorContext;
  56. // The abstract interface to a class which generates code implementing a
  57. // particular proto file in a particular language. A number of these may
  58. // be registered with CommandLineInterface to support various languages.
  59. class PROTOC_EXPORT CodeGenerator {
  60. public:
  61. inline CodeGenerator() {}
  62. virtual ~CodeGenerator();
  63. // Generates code for the given proto file, generating one or more files in
  64. // the given output directory.
  65. //
  66. // A parameter to be passed to the generator can be specified on the command
  67. // line. This is intended to be used to pass generator specific parameters.
  68. // It is empty if no parameter was given. ParseGeneratorParameter (below),
  69. // can be used to accept multiple parameters within the single parameter
  70. // command line flag.
  71. //
  72. // Returns true if successful. Otherwise, sets *error to a description of
  73. // the problem (e.g. "invalid parameter") and returns false.
  74. virtual bool Generate(const FileDescriptor* file,
  75. const std::string& parameter,
  76. GeneratorContext* generator_context,
  77. std::string* error) const = 0;
  78. // Generates code for all given proto files.
  79. //
  80. // WARNING: The canonical code generator design produces one or two output
  81. // files per input .proto file, and we do not wish to encourage alternate
  82. // designs.
  83. //
  84. // A parameter is given as passed on the command line, as in |Generate()|
  85. // above.
  86. //
  87. // Returns true if successful. Otherwise, sets *error to a description of
  88. // the problem (e.g. "invalid parameter") and returns false.
  89. virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files,
  90. const std::string& parameter,
  91. GeneratorContext* generator_context,
  92. std::string* error) const;
  93. // This must be kept in sync with plugin.proto. See that file for
  94. // documentation on each value.
  95. enum Feature {
  96. FEATURE_PROTO3_OPTIONAL = 1,
  97. };
  98. // Implement this to indicate what features this code generator supports.
  99. //
  100. // This must be a bitwise OR of values from the Feature enum above (or zero).
  101. virtual uint64_t GetSupportedFeatures() const { return 0; }
  102. // This is no longer used, but this class is part of the opensource protobuf
  103. // library, so it has to remain to keep vtables the same for the current
  104. // version of the library. When protobufs does a api breaking change, the
  105. // method can be removed.
  106. virtual bool HasGenerateAll() const { return true; }
  107. private:
  108. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator);
  109. };
  110. // CodeGenerators generate one or more files in a given directory. This
  111. // abstract interface represents the directory to which the CodeGenerator is
  112. // to write and other information about the context in which the Generator
  113. // runs.
  114. class PROTOC_EXPORT GeneratorContext {
  115. public:
  116. inline GeneratorContext() {
  117. }
  118. virtual ~GeneratorContext();
  119. // Opens the given file, truncating it if it exists, and returns a
  120. // ZeroCopyOutputStream that writes to the file. The caller takes ownership
  121. // of the returned object. This method never fails (a dummy stream will be
  122. // returned instead).
  123. //
  124. // The filename given should be relative to the root of the source tree.
  125. // E.g. the C++ generator, when generating code for "foo/bar.proto", will
  126. // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that
  127. // "foo/" is included in these filenames. The filename is not allowed to
  128. // contain "." or ".." components.
  129. virtual io::ZeroCopyOutputStream* Open(const std::string& filename) = 0;
  130. // Similar to Open() but the output will be appended to the file if exists
  131. virtual io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename);
  132. // Creates a ZeroCopyOutputStream which will insert code into the given file
  133. // at the given insertion point. See plugin.proto (plugin.pb.h) for more
  134. // information on insertion points. The default implementation
  135. // assert-fails -- it exists only for backwards-compatibility.
  136. //
  137. // WARNING: This feature is currently EXPERIMENTAL and is subject to change.
  138. virtual io::ZeroCopyOutputStream* OpenForInsert(
  139. const std::string& filename, const std::string& insertion_point);
  140. // Similar to OpenForInsert, but if `info` is non-empty, will open (or create)
  141. // filename.pb.meta and insert info at the appropriate place with the
  142. // necessary shifts. The default implementation ignores `info`.
  143. //
  144. // WARNING: This feature will be REMOVED in the near future.
  145. virtual io::ZeroCopyOutputStream* OpenForInsertWithGeneratedCodeInfo(
  146. const std::string& filename, const std::string& insertion_point,
  147. const google::protobuf::GeneratedCodeInfo& info);
  148. // Returns a vector of FileDescriptors for all the files being compiled
  149. // in this run. Useful for languages, such as Go, that treat files
  150. // differently when compiled as a set rather than individually.
  151. virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output);
  152. // Retrieves the version number of the protocol compiler associated with
  153. // this GeneratorContext.
  154. virtual void GetCompilerVersion(Version* version) const;
  155. private:
  156. GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext);
  157. };
  158. // The type GeneratorContext was once called OutputDirectory. This typedef
  159. // provides backward compatibility.
  160. typedef GeneratorContext OutputDirectory;
  161. // Several code generators treat the parameter argument as holding a
  162. // list of options separated by commas. This helper function parses
  163. // a set of comma-delimited name/value pairs: e.g.,
  164. // "foo=bar,baz,qux=corge"
  165. // parses to the pairs:
  166. // ("foo", "bar"), ("baz", ""), ("qux", "corge")
  167. PROTOC_EXPORT void ParseGeneratorParameter(
  168. const std::string&, std::vector<std::pair<std::string, std::string> >*);
  169. // Strips ".proto" or ".protodevel" from the end of a filename.
  170. PROTOC_EXPORT std::string StripProto(const std::string& filename);
  171. } // namespace compiler
  172. } // namespace protobuf
  173. } // namespace google
  174. #include <google/protobuf/port_undef.inc>
  175. #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__