Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:11:58

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: liujisi@google.com (Pherl Liu)
0009 
0010 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__
0011 #define GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__
0012 
0013 #include <memory>
0014 #include <string>
0015 
0016 #include "absl/container/btree_map.h"
0017 #include "absl/container/flat_hash_map.h"
0018 #include "absl/strings/string_view.h"
0019 #include "google/protobuf/compiler/java/context.h"
0020 #include "google/protobuf/descriptor.h"
0021 
0022 namespace google {
0023 namespace protobuf {
0024 namespace compiler {
0025 namespace java {
0026 
0027 class MessageGenerator {
0028  public:
0029   explicit MessageGenerator(const Descriptor* descriptor);
0030   MessageGenerator(const MessageGenerator&) = delete;
0031   MessageGenerator& operator=(const MessageGenerator&) = delete;
0032   virtual ~MessageGenerator();
0033 
0034   // All static variables have to be declared at the top-level of the file
0035   // so that we can control initialization order, which is important for
0036   // DescriptorProto bootstrapping to work.
0037   virtual void GenerateStaticVariables(io::Printer* printer,
0038                                        int* bytecode_estimate) = 0;
0039 
0040   // Output code which initializes the static variables generated by
0041   // GenerateStaticVariables(). Returns an estimate of bytecode size.
0042   virtual int GenerateStaticVariableInitializers(io::Printer* printer) = 0;
0043 
0044   // Generate the class itself.
0045   virtual void Generate(io::Printer* printer) = 0;
0046 
0047   // Generates the base interface that both the class and its builder
0048   // implement
0049   virtual void GenerateInterface(io::Printer* printer) = 0;
0050 
0051   // Generate code to register all contained extensions with an
0052   // ExtensionRegistry.
0053   virtual void GenerateExtensionRegistrationCode(io::Printer* printer) = 0;
0054   virtual void GenerateKotlinDsl(io::Printer* printer) const = 0;
0055   virtual void GenerateKotlinMembers(io::Printer* printer) const = 0;
0056   virtual void GenerateTopLevelKotlinMembers(io::Printer* printer) const = 0;
0057 
0058  protected:
0059   const Descriptor* descriptor_;
0060   absl::btree_map<int, const OneofDescriptor*> oneofs_;
0061 };
0062 
0063 class EnumGenerator {
0064  public:
0065   virtual ~EnumGenerator() = default;
0066   virtual void Generate(io::Printer* printer) = 0;
0067 };
0068 
0069 // Generates code for an extension, which may be within the scope of some
0070 // message or may be at file scope.  This is much simpler than FieldGenerator
0071 // since extensions are just simple identifiers with interesting types.
0072 class ExtensionGenerator {
0073  public:
0074   virtual ~ExtensionGenerator() = default;
0075 
0076   virtual void Generate(io::Printer* printer) = 0;
0077 
0078   // Returns an estimate of the number of bytes the printed code will compile
0079   // to
0080   virtual int GenerateNonNestedInitializationCode(io::Printer* printer) = 0;
0081 
0082   // Returns an estimate of the number of bytes the printed code will compile
0083   // to
0084   virtual int GenerateRegistrationCode(io::Printer* printer) = 0;
0085 
0086  protected:
0087   static void InitTemplateVars(
0088       const FieldDescriptor* descriptor, const std::string& scope,
0089       bool immutable, ClassNameResolver* name_resolver,
0090       absl::flat_hash_map<absl::string_view, std::string>* vars_pointer,
0091       Context* context);
0092 };
0093 
0094 class ServiceGenerator {
0095  public:
0096   explicit ServiceGenerator(const ServiceDescriptor* descriptor)
0097       : descriptor_(descriptor) {}
0098   virtual ~ServiceGenerator() = default;
0099 
0100   ServiceGenerator(const ServiceGenerator&) = delete;
0101   ServiceGenerator& operator=(const ServiceGenerator&) = delete;
0102 
0103   virtual void Generate(io::Printer* printer) = 0;
0104 
0105   enum RequestOrResponse { REQUEST, RESPONSE };
0106   enum IsAbstract { IS_ABSTRACT, IS_CONCRETE };
0107 
0108  protected:
0109   const ServiceDescriptor* descriptor_;
0110 };
0111 
0112 // An interface to create generators for a given descriptor.  This interface
0113 // is implemented for every variant of the Java API (Mutable, Immutable, Lite).
0114 class GeneratorFactory {
0115  public:
0116   virtual ~GeneratorFactory() = default;
0117 
0118   virtual std::unique_ptr<MessageGenerator> NewMessageGenerator(
0119       const Descriptor* descriptor) const = 0;
0120 
0121   virtual std::unique_ptr<EnumGenerator> NewEnumGenerator(
0122       const EnumDescriptor* descriptor) const = 0;
0123 
0124   virtual std::unique_ptr<ExtensionGenerator> NewExtensionGenerator(
0125       const FieldDescriptor* descriptor) const = 0;
0126 
0127   virtual std::unique_ptr<ServiceGenerator> NewServiceGenerator(
0128       const ServiceDescriptor* descriptor) const = 0;
0129 };
0130 
0131 }  // namespace java
0132 }  // namespace compiler
0133 }  // namespace protobuf
0134 }  // namespace google
0135 
0136 #endif  // GOOGLE_PROTOBUF_COMPILER_JAVA_GENERATOR_FACTORY_H__