|
||||
File indexing completed on 2025-01-31 10:12:00
0001 // Protocol Buffers - Google's data interchange format 0002 // Copyright 2008 Google Inc. All rights reserved. 0003 // 0004 // Use of this source code is governed by a BSD-style 0005 // license that can be found in the LICENSE file or at 0006 // https://developers.google.com/open-source/licenses/bsd 0007 0008 // Author: kenton@google.com (Kenton Varda) 0009 // Based on original Protocol Buffers design by 0010 // Sanjay Ghemawat, Jeff Dean, and others. 0011 // 0012 // Defines the abstract interface implemented by each of the language-specific 0013 // code generators. 0014 0015 #ifndef GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ 0016 #define GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__ 0017 0018 #include <cstdint> 0019 #include <string> 0020 #include <utility> 0021 #include <vector> 0022 0023 #include "absl/status/statusor.h" 0024 #include "absl/strings/string_view.h" 0025 #include "google/protobuf/descriptor.h" 0026 #include "google/protobuf/descriptor.pb.h" 0027 0028 // Must be included last. 0029 #include "google/protobuf/port_def.inc" 0030 0031 namespace google { 0032 namespace protobuf { 0033 0034 namespace io { 0035 class ZeroCopyOutputStream; 0036 } 0037 class FileDescriptor; 0038 class GeneratedCodeInfo; 0039 0040 namespace compiler { 0041 class AccessInfoMap; 0042 0043 class Version; 0044 0045 // Defined in this file. 0046 class CodeGenerator; 0047 class GeneratorContext; 0048 0049 // The abstract interface to a class which generates code implementing a 0050 // particular proto file in a particular language. A number of these may 0051 // be registered with CommandLineInterface to support various languages. 0052 class PROTOC_EXPORT CodeGenerator { 0053 public: 0054 CodeGenerator() = default; 0055 CodeGenerator(const CodeGenerator&) = delete; 0056 CodeGenerator& operator=(const CodeGenerator&) = delete; 0057 virtual ~CodeGenerator(); 0058 0059 // Generates code for the given proto file, generating one or more files in 0060 // the given output directory. 0061 // 0062 // A parameter to be passed to the generator can be specified on the command 0063 // line. This is intended to be used to pass generator specific parameters. 0064 // It is empty if no parameter was given. ParseGeneratorParameter (below), 0065 // can be used to accept multiple parameters within the single parameter 0066 // command line flag. 0067 // 0068 // Returns true if successful. Otherwise, sets *error to a description of 0069 // the problem (e.g. "invalid parameter") and returns false. 0070 virtual bool Generate(const FileDescriptor* file, 0071 const std::string& parameter, 0072 GeneratorContext* generator_context, 0073 std::string* error) const = 0; 0074 0075 // Generates code for all given proto files. 0076 // 0077 // WARNING: The canonical code generator design produces one or two output 0078 // files per input .proto file, and we do not wish to encourage alternate 0079 // designs. 0080 // 0081 // A parameter is given as passed on the command line, as in |Generate()| 0082 // above. 0083 // 0084 // Returns true if successful. Otherwise, sets *error to a description of 0085 // the problem (e.g. "invalid parameter") and returns false. 0086 virtual bool GenerateAll(const std::vector<const FileDescriptor*>& files, 0087 const std::string& parameter, 0088 GeneratorContext* generator_context, 0089 std::string* error) const; 0090 0091 // This must be kept in sync with plugin.proto. See that file for 0092 // documentation on each value. 0093 // TODO Use CodeGeneratorResponse.Feature here. 0094 enum Feature { 0095 FEATURE_PROTO3_OPTIONAL = 1, 0096 FEATURE_SUPPORTS_EDITIONS = 2, 0097 }; 0098 0099 // Implement this to indicate what features this code generator supports. 0100 // 0101 // This must be a bitwise OR of values from the Feature enum above (or zero). 0102 virtual uint64_t GetSupportedFeatures() const { return 0; } 0103 0104 // This is no longer used, but this class is part of the opensource protobuf 0105 // library, so it has to remain to keep vtables the same for the current 0106 // version of the library. When protobufs does a api breaking change, the 0107 // method can be removed. 0108 virtual bool HasGenerateAll() const { return true; } 0109 0110 // Returns all the feature extensions used by this generator. This must be in 0111 // the generated pool, meaning that the extensions should be linked into this 0112 // binary. Any generator features not included here will not get properly 0113 // resolved and GetResolvedSourceFeatures will not provide useful values. 0114 virtual std::vector<const FieldDescriptor*> GetFeatureExtensions() const { 0115 return {}; 0116 } 0117 0118 // Returns the minimum edition (inclusive) supported by this generator. Any 0119 // proto files with an edition before this will result in an error. 0120 virtual Edition GetMinimumEdition() const { return Edition::EDITION_UNKNOWN; } 0121 0122 // Returns the maximum edition (inclusive) supported by this generator. Any 0123 // proto files with an edition after this will result in an error. 0124 virtual Edition GetMaximumEdition() const { return Edition::EDITION_UNKNOWN; } 0125 0126 // Builds a default feature set mapping for this generator. 0127 // 0128 // This will use the extensions specified by GetFeatureExtensions(), with the 0129 // supported edition range [GetMinimumEdition(), GetMaximumEdition]. It has 0130 // no side-effects, and code generators only need to call this if they want to 0131 // embed the defaults into the generated code. 0132 absl::StatusOr<FeatureSetDefaults> BuildFeatureSetDefaults() const; 0133 0134 protected: 0135 // Retrieves the resolved source features for a given descriptor. All the 0136 // global features and language features returned by GetFeatureExtensions will 0137 // be fully resolved. These should be used to make any feature-based decisions 0138 // during code generation. 0139 template <typename DescriptorT> 0140 static const FeatureSet& GetResolvedSourceFeatures(const DescriptorT& desc) { 0141 return ::google::protobuf::internal::InternalFeatureHelper::GetFeatures(desc); 0142 } 0143 0144 // Retrieves the unresolved source features for a given descriptor. These 0145 // should be used to validate the original .proto file. These represent the 0146 // original proto files from generated code, but should be stripped of 0147 // source-retention features before sending to a runtime. 0148 template <typename DescriptorT, typename TypeTraitsT, uint8_t field_type, 0149 bool is_packed> 0150 static typename TypeTraitsT::ConstType GetUnresolvedSourceFeatures( 0151 const DescriptorT& descriptor, 0152 const google::protobuf::internal::ExtensionIdentifier< 0153 FeatureSet, TypeTraitsT, field_type, is_packed>& extension) { 0154 return ::google::protobuf::internal::InternalFeatureHelper::GetUnresolvedFeatures( 0155 descriptor, extension); 0156 } 0157 0158 // Retrieves the edition of a built file descriptor. 0159 static Edition GetEdition(const FileDescriptor& file) { 0160 return ::google::protobuf::internal::InternalFeatureHelper::GetEdition(file); 0161 } 0162 }; 0163 0164 // CodeGenerators generate one or more files in a given directory. This 0165 // abstract interface represents the directory to which the CodeGenerator is 0166 // to write and other information about the context in which the Generator 0167 // runs. 0168 class PROTOC_EXPORT GeneratorContext { 0169 public: 0170 GeneratorContext() { 0171 } 0172 GeneratorContext(const GeneratorContext&) = delete; 0173 GeneratorContext& operator=(const GeneratorContext&) = delete; 0174 virtual ~GeneratorContext(); 0175 0176 // Opens the given file, truncating it if it exists, and returns a 0177 // ZeroCopyOutputStream that writes to the file. The caller takes ownership 0178 // of the returned object. This method never fails (a dummy stream will be 0179 // returned instead). 0180 // 0181 // The filename given should be relative to the root of the source tree. 0182 // E.g. the C++ generator, when generating code for "foo/bar.proto", will 0183 // generate the files "foo/bar.pb.h" and "foo/bar.pb.cc"; note that 0184 // "foo/" is included in these filenames. The filename is not allowed to 0185 // contain "." or ".." components. 0186 virtual io::ZeroCopyOutputStream* Open(const std::string& filename) = 0; 0187 0188 // Similar to Open() but the output will be appended to the file if exists 0189 virtual io::ZeroCopyOutputStream* OpenForAppend(const std::string& filename); 0190 0191 // Creates a ZeroCopyOutputStream which will insert code into the given file 0192 // at the given insertion point. See plugin.proto (plugin.pb.h) for more 0193 // information on insertion points. The default implementation 0194 // assert-fails -- it exists only for backwards-compatibility. 0195 // 0196 // WARNING: This feature is currently EXPERIMENTAL and is subject to change. 0197 virtual io::ZeroCopyOutputStream* OpenForInsert( 0198 const std::string& filename, const std::string& insertion_point); 0199 0200 // Similar to OpenForInsert, but if `info` is non-empty, will open (or create) 0201 // filename.pb.meta and insert info at the appropriate place with the 0202 // necessary shifts. The default implementation ignores `info`. 0203 // 0204 // WARNING: This feature will be REMOVED in the near future. 0205 virtual io::ZeroCopyOutputStream* OpenForInsertWithGeneratedCodeInfo( 0206 const std::string& filename, const std::string& insertion_point, 0207 const google::protobuf::GeneratedCodeInfo& info); 0208 0209 // Returns a vector of FileDescriptors for all the files being compiled 0210 // in this run. Useful for languages, such as Go, that treat files 0211 // differently when compiled as a set rather than individually. 0212 virtual void ListParsedFiles(std::vector<const FileDescriptor*>* output); 0213 0214 // Retrieves the version number of the protocol compiler associated with 0215 // this GeneratorContext. 0216 virtual void GetCompilerVersion(Version* version) const; 0217 0218 }; 0219 0220 // The type GeneratorContext was once called OutputDirectory. This typedef 0221 // provides backward compatibility. 0222 typedef GeneratorContext OutputDirectory; 0223 0224 // Several code generators treat the parameter argument as holding a 0225 // list of options separated by commas. This helper function parses 0226 // a set of comma-delimited name/value pairs: e.g., 0227 // "foo=bar,baz,moo=corge" 0228 // parses to the pairs: 0229 // ("foo", "bar"), ("baz", ""), ("moo", "corge") 0230 PROTOC_EXPORT void ParseGeneratorParameter( 0231 absl::string_view, std::vector<std::pair<std::string, std::string> >*); 0232 0233 // Strips ".proto" or ".protodevel" from the end of a filename. 0234 PROTOC_EXPORT std::string StripProto(absl::string_view filename); 0235 0236 // Returns true if the proto path corresponds to a known feature file. 0237 PROTOC_EXPORT bool IsKnownFeatureProto(absl::string_view filename); 0238 0239 // Returns true if the proto path can skip edition check. 0240 PROTOC_EXPORT bool CanSkipEditionCheck(absl::string_view filename); 0241 0242 } // namespace compiler 0243 } // namespace protobuf 0244 } // namespace google 0245 0246 #include "google/protobuf/port_undef.inc" 0247 0248 #endif // GOOGLE_PROTOBUF_COMPILER_CODE_GENERATOR_H__
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |