File indexing completed on 2025-01-31 10:12:00
0001
0002
0003
0004
0005
0006
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
0037
0038
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
0062
0063
0064
0065
0066
0067
0068 virtual void InMsgImpl(Context& ctx, const FieldDescriptor& field,
0069 AccessorCase accessor_case) const {}
0070
0071
0072 virtual void InExternC(Context& ctx, const FieldDescriptor& field) const {}
0073
0074
0075
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 }
0136 }
0137 }
0138 }
0139
0140 #endif