temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 1 | // Protocol Buffers - Google's data interchange format |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 2 | // Copyright 2008 Google Inc. All rights reserved. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 3 | // http://code.google.com/p/protobuf/ |
| 4 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are |
| 7 | // met: |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 8 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 18 | // |
kenton@google.com | 24bf56f | 2008-09-24 20:31:01 +0000 | [diff] [blame] | 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. |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 30 | |
| 31 | // Author: kenton@google.com (Kenton Varda) |
| 32 | // Based on original Protocol Buffers design by |
| 33 | // Sanjay Ghemawat, Jeff Dean, and others. |
| 34 | // |
| 35 | // Defines the abstract interface implemented by each of the language-specific |
| 36 | // code generators. |
| 37 | |
| 38 | #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ |
| 39 | #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ |
| 40 | |
| 41 | #include <google/protobuf/stubs/common.h> |
| 42 | #include <string> |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 43 | #include <vector> |
| 44 | #include <utility> |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 45 | |
| 46 | namespace google { |
| 47 | namespace protobuf { |
| 48 | |
| 49 | namespace io { class ZeroCopyOutputStream; } |
| 50 | class FileDescriptor; |
| 51 | |
| 52 | namespace compiler { |
| 53 | |
| 54 | // Defined in this file. |
| 55 | class CodeGenerator; |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 56 | class GeneratorContext; |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 57 | |
| 58 | // The abstract interface to a class which generates code implementing a |
| 59 | // particular proto file in a particular language. A number of these may |
| 60 | // be registered with CommandLineInterface to support various languages. |
| 61 | class LIBPROTOC_EXPORT CodeGenerator { |
| 62 | public: |
| 63 | inline CodeGenerator() {} |
| 64 | virtual ~CodeGenerator(); |
| 65 | |
| 66 | // Generates code for the given proto file, generating one or more files in |
| 67 | // the given output directory. |
| 68 | // |
| 69 | // A parameter to be passed to the generator can be specified on the |
| 70 | // command line. This is intended to be used by Java and similar languages |
| 71 | // to specify which specific class from the proto file is to be generated, |
| 72 | // though it could have other uses as well. It is empty if no parameter was |
| 73 | // given. |
| 74 | // |
| 75 | // Returns true if successful. Otherwise, sets *error to a description of |
| 76 | // the problem (e.g. "invalid parameter") and returns false. |
| 77 | virtual bool Generate(const FileDescriptor* file, |
| 78 | const string& parameter, |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 79 | GeneratorContext* generator_context, |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 80 | string* error) const = 0; |
| 81 | |
| 82 | private: |
| 83 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(CodeGenerator); |
| 84 | }; |
| 85 | |
| 86 | // CodeGenerators generate one or more files in a given directory. This |
| 87 | // abstract interface represents the directory to which the CodeGenerator is |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 88 | // to write and other information about the context in which the Generator |
| 89 | // runs. |
| 90 | class LIBPROTOC_EXPORT GeneratorContext { |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 91 | public: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 92 | inline GeneratorContext() {} |
| 93 | virtual ~GeneratorContext(); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 94 | |
| 95 | // Opens the given file, truncating it if it exists, and returns a |
| 96 | // ZeroCopyOutputStream that writes to the file. The caller takes ownership |
| 97 | // of the returned object. This method never fails (a dummy stream will be |
| 98 | // returned instead). |
| 99 | // |
| 100 | // The filename given should be relative to the root of the source tree. |
| 101 | // E.g. the C++ generator, when generating code for "foo/bar.proto", will |
kenton@google.com | 7b7a80e | 2010-01-08 03:56:03 +0000 | [diff] [blame] | 102 | // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 103 | // "foo/" is included in these filenames. The filename is not allowed to |
| 104 | // contain "." or ".." components. |
| 105 | virtual io::ZeroCopyOutputStream* Open(const string& filename) = 0; |
| 106 | |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 107 | // Creates a ZeroCopyOutputStream which will insert code into the given file |
kenton@google.com | 7b7a80e | 2010-01-08 03:56:03 +0000 | [diff] [blame] | 108 | // at the given insertion point. See plugin.proto (plugin.pb.h) for more |
| 109 | // information on insertion points. The default implementation |
| 110 | // assert-fails -- it exists only for backwards-compatibility. |
kenton@google.com | 7f4938b | 2009-12-22 22:57:39 +0000 | [diff] [blame] | 111 | // |
| 112 | // WARNING: This feature is currently EXPERIMENTAL and is subject to change. |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 113 | virtual io::ZeroCopyOutputStream* OpenForInsert( |
| 114 | const string& filename, const string& insertion_point); |
| 115 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 116 | // Returns a vector of FileDescriptors for all the files being compiled |
| 117 | // in this run. Useful for languages, such as Go, that treat files |
| 118 | // differently when compiled as a set rather than individually. |
| 119 | virtual void ListParsedFiles(vector<const FileDescriptor*>* output); |
| 120 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 121 | private: |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 122 | GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(GeneratorContext); |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 123 | }; |
| 124 | |
liujisi@google.com | 33165fe | 2010-11-02 13:14:58 +0000 | [diff] [blame^] | 125 | // The type GeneratorContext was once called OutputDirectory. This typedef |
| 126 | // provides backward compatibility. |
| 127 | typedef GeneratorContext OutputDirectory; |
| 128 | |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 129 | // Several code generators treat the parameter argument as holding a |
| 130 | // list of options separated by commas. This helper function parses |
| 131 | // a set of comma-delimited name/value pairs: e.g., |
| 132 | // "foo=bar,baz,qux=corge" |
| 133 | // parses to the pairs: |
| 134 | // ("foo", "bar"), ("baz", ""), ("qux", "corge") |
| 135 | extern void ParseGeneratorParameter(const string&, |
kenton@google.com | fccb146 | 2009-12-18 02:11:36 +0000 | [diff] [blame] | 136 | vector<pair<string, string> >*); |
kenton@google.com | 80b1d62 | 2009-07-29 01:13:20 +0000 | [diff] [blame] | 137 | |
temporal | 40ee551 | 2008-07-10 02:12:20 +0000 | [diff] [blame] | 138 | } // namespace compiler |
| 139 | } // namespace protobuf |
| 140 | |
| 141 | } // namespace google |
| 142 | #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ |