Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-31 10:12:00

0001 // Protocol Buffers - Google's data interchange format
0002 // Copyright 2023 Google LLC.  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 #ifndef GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_ACCESSOR_GENERATOR_H__
0009 #define GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_ACCESSOR_GENERATOR_H__
0010 
0011 #include <memory>
0012 #include <string>
0013 #include <utility>
0014 
0015 #include "absl/log/absl_check.h"
0016 #include "google/protobuf/compiler/rust/accessors/accessor_case.h"
0017 #include "google/protobuf/compiler/rust/context.h"
0018 #include "google/protobuf/compiler/rust/naming.h"
0019 #include "google/protobuf/descriptor.h"
0020 
0021 namespace google {
0022 namespace protobuf {
0023 namespace compiler {
0024 namespace rust {
0025 
0026 class AccessorGenerator {
0027  public:
0028   AccessorGenerator() = default;
0029   virtual ~AccessorGenerator() = default;
0030 
0031   AccessorGenerator(const AccessorGenerator&) = delete;
0032   AccessorGenerator(AccessorGenerator&&) = delete;
0033   AccessorGenerator& operator=(const AccessorGenerator&) = delete;
0034   AccessorGenerator& operator=(AccessorGenerator&&) = delete;
0035 
0036   // Constructs a generator for the given field.
0037   //
0038   // Returns `nullptr` if there is no known generator for this field.
0039   static std::unique_ptr<AccessorGenerator> For(Context& ctx,
0040                                                 const FieldDescriptor& field);
0041 
0042   void GenerateMsgImpl(Context& ctx, const FieldDescriptor& field,
0043                        AccessorCase accessor_case) const {
0044     ctx.Emit({{"comment", FieldInfoComment(ctx, field)}}, R"rs(
0045       // $comment$
0046     )rs");
0047     InMsgImpl(ctx, field, accessor_case);
0048     ctx.printer().PrintRaw("\n");
0049   }
0050   void GenerateExternC(Context& ctx, const FieldDescriptor& field) const {
0051     InExternC(ctx, field);
0052     ctx.printer().PrintRaw("\n");
0053   }
0054   void GenerateThunkCc(Context& ctx, const FieldDescriptor& field) const {
0055     ABSL_CHECK(ctx.is_cpp());
0056     InThunkCc(ctx, field);
0057     ctx.printer().PrintRaw("\n");
0058   }
0059 
0060  private:
0061   // Note: the virtual functions are duplicated as non-virtual public functions,
0062   // so that we can customize prologue and epilogue behavior for these
0063   // functions. For example, consider calling `field.printer.WithVars()` as a
0064   // prologue to inject variables automatically.
0065 
0066   // Called inside the `impl Msg {}`, `impl MsgMut {}` and `impl MsgView`
0067   // blocks.
0068   virtual void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0069                          AccessorCase accessor_case) const {}
0070 
0071   // Called inside of a message's `extern "C" {}` block.
0072   virtual void InExternC(Context& ctx, const FieldDescriptor& field) const {}
0073 
0074   // Called inside of an `extern "C" {}` block in the  `.thunk.cc` file, if such
0075   // a file is being generated.
0076   virtual void InThunkCc(Context& ctx, const FieldDescriptor& field) const {}
0077 };
0078 
0079 class SingularScalar final : public AccessorGenerator {
0080  public:
0081   ~SingularScalar() override = default;
0082   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0083                  AccessorCase accessor_case) const override;
0084   void InExternC(Context& ctx, const FieldDescriptor& field) const override;
0085   void InThunkCc(Context& ctx, const FieldDescriptor& field) const override;
0086 };
0087 
0088 class SingularString final : public AccessorGenerator {
0089  public:
0090   ~SingularString() override = default;
0091   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0092                  AccessorCase accessor_case) const override;
0093   void InExternC(Context& ctx, const FieldDescriptor& field) const override;
0094   void InThunkCc(Context& ctx, const FieldDescriptor& field) const override;
0095 };
0096 
0097 class SingularMessage final : public AccessorGenerator {
0098  public:
0099   ~SingularMessage() override = default;
0100   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0101                  AccessorCase accessor_case) const override;
0102   void InExternC(Context& ctx, const FieldDescriptor& field) const override;
0103   void InThunkCc(Context& ctx, const FieldDescriptor& field) const override;
0104 };
0105 
0106 class RepeatedField final : public AccessorGenerator {
0107  public:
0108   ~RepeatedField() override = default;
0109   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0110                  AccessorCase accessor_case) const override;
0111   void InExternC(Context& ctx, const FieldDescriptor& field) const override;
0112   void InThunkCc(Context& ctx, const FieldDescriptor& field) const override;
0113 };
0114 
0115 class UnsupportedField final : public AccessorGenerator {
0116  public:
0117   explicit UnsupportedField(std::string reason) : reason_(std::move(reason)) {}
0118   ~UnsupportedField() override = default;
0119   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0120                  AccessorCase accessor_case) const override;
0121 
0122  private:
0123   std::string reason_;
0124 };
0125 
0126 class Map final : public AccessorGenerator {
0127  public:
0128   ~Map() override = default;
0129   void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0130                  AccessorCase accessor_case) const override;
0131   void InExternC(Context& ctx, const FieldDescriptor& field) const override;
0132   void InThunkCc(Context& ctx, const FieldDescriptor& field) const override;
0133 };
0134 
0135 }  // namespace rust
0136 }  // namespace compiler
0137 }  // namespace protobuf
0138 }  // namespace google
0139 
0140 #endif  // GOOGLE_PROTOBUF_COMPILER_RUST_ACCESSORS_ACCESSOR_GENERATOR_H__