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 #ifndef GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
36 #define GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
37
38 #include <cstdint>
39 #include <map>
40 #include <memory>
41 #include <string>
42
43 #include <google/protobuf/stubs/logging.h>
44 #include <google/protobuf/stubs/common.h>
45 #include <google/protobuf/descriptor.h>
46
47 namespace google {
48 namespace protobuf {
49 namespace compiler {
50 namespace java {
51 class Context; // context.h
52 class ClassNameResolver; // name_resolver.h
53 } // namespace java
54 } // namespace compiler
55 namespace io {
56 class Printer; // printer.h
57 }
58 } // namespace protobuf
59 } // namespace google
60
61 namespace google {
62 namespace protobuf {
63 namespace compiler {
64 namespace java {
65
66 class ImmutableFieldGenerator {
67 public:
ImmutableFieldGenerator()68 ImmutableFieldGenerator() {}
69 virtual ~ImmutableFieldGenerator();
70
71 virtual int GetMessageBitIndex() const = 0;
72 virtual int GetBuilderBitIndex() const = 0;
73 virtual int GetNumBitsForMessage() const = 0;
74 virtual int GetNumBitsForBuilder() const = 0;
75 virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
76 virtual void GenerateMembers(io::Printer* printer) const = 0;
77 virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
78 virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
79 virtual void GenerateBuilderClearCode(io::Printer* printer) const = 0;
80 virtual void GenerateMergingCode(io::Printer* printer) const = 0;
81 virtual void GenerateBuildingCode(io::Printer* printer) const = 0;
82 virtual void GenerateBuilderParsingCode(io::Printer* printer) const = 0;
83 virtual void GenerateBuilderParsingCodeFromPacked(io::Printer* printer) const;
84 virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
85 virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;
86 virtual void GenerateFieldBuilderInitializationCode(
87 io::Printer* printer) const = 0;
88 virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
89
90 virtual void GenerateEqualsCode(io::Printer* printer) const = 0;
91 virtual void GenerateHashCode(io::Printer* printer) const = 0;
92
93 virtual std::string GetBoxedType() const = 0;
94
95 private:
96 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldGenerator);
97 };
98
99 class ImmutableFieldLiteGenerator {
100 public:
ImmutableFieldLiteGenerator()101 ImmutableFieldLiteGenerator() {}
102 virtual ~ImmutableFieldLiteGenerator();
103
104 virtual int GetNumBitsForMessage() const = 0;
105 virtual void GenerateInterfaceMembers(io::Printer* printer) const = 0;
106 virtual void GenerateMembers(io::Printer* printer) const = 0;
107 virtual void GenerateBuilderMembers(io::Printer* printer) const = 0;
108 virtual void GenerateInitializationCode(io::Printer* printer) const = 0;
109 virtual void GenerateFieldInfo(io::Printer* printer,
110 std::vector<uint16_t>* output) const = 0;
111 virtual void GenerateKotlinDslMembers(io::Printer* printer) const = 0;
112
113 virtual std::string GetBoxedType() const = 0;
114
115 private:
116 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableFieldLiteGenerator);
117 };
118
119
120 // Convenience class which constructs FieldGenerators for a Descriptor.
121 template <typename FieldGeneratorType>
122 class FieldGeneratorMap {
123 public:
124 explicit FieldGeneratorMap(const Descriptor* descriptor, Context* context);
125 ~FieldGeneratorMap();
126
127 const FieldGeneratorType& get(const FieldDescriptor* field) const;
128
129 private:
130 const Descriptor* descriptor_;
131 std::vector<std::unique_ptr<FieldGeneratorType>> field_generators_;
132
133 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(FieldGeneratorMap);
134 };
135
136 template <typename FieldGeneratorType>
get(const FieldDescriptor * field)137 inline const FieldGeneratorType& FieldGeneratorMap<FieldGeneratorType>::get(
138 const FieldDescriptor* field) const {
139 GOOGLE_CHECK_EQ(field->containing_type(), descriptor_);
140 return *field_generators_[field->index()];
141 }
142
143 // Instantiate template for mutable and immutable maps.
144 template <>
145 FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
146 const Descriptor* descriptor, Context* context);
147
148 template <>
149 FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap();
150
151
152 template <>
153 FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
154 const Descriptor* descriptor, Context* context);
155
156 template <>
157 FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap();
158
159
160 // Field information used in FieldGenerators.
161 struct FieldGeneratorInfo {
162 std::string name;
163 std::string capitalized_name;
164 std::string disambiguated_reason;
165 };
166
167 // Oneof information used in OneofFieldGenerators.
168 struct OneofGeneratorInfo {
169 std::string name;
170 std::string capitalized_name;
171 };
172
173 // Set some common variables used in variable FieldGenerators.
174 void SetCommonFieldVariables(const FieldDescriptor* descriptor,
175 const FieldGeneratorInfo* info,
176 std::map<std::string, std::string>* variables);
177
178 // Set some common oneof variables used in OneofFieldGenerators.
179 void SetCommonOneofVariables(const FieldDescriptor* descriptor,
180 const OneofGeneratorInfo* info,
181 std::map<std::string, std::string>* variables);
182
183 // Print useful comments before a field's accessors.
184 void PrintExtraFieldInfo(const std::map<std::string, std::string>& variables,
185 io::Printer* printer);
186
187 } // namespace java
188 } // namespace compiler
189 } // namespace protobuf
190 } // namespace google
191
192 #endif // GOOGLE_PROTOBUF_COMPILER_JAVA_FIELD_H__
193