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