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_MESSAGE_FIELD_H__
36#define GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_H__
37
38#include <map>
39#include <string>
40
41#include <google/protobuf/compiler/java/field.h>
42
43namespace google {
44namespace protobuf {
45namespace compiler {
46namespace java {
47class Context; // context.h
48class ClassNameResolver; // name_resolver.h
49} // namespace java
50} // namespace compiler
51} // namespace protobuf
52} // namespace google
53
54namespace google {
55namespace protobuf {
56namespace compiler {
57namespace java {
58
59class ImmutableMessageFieldGenerator : public ImmutableFieldGenerator {
60 public:
61 explicit ImmutableMessageFieldGenerator(const FieldDescriptor* descriptor,
62 int messageBitIndex,
63 int builderBitIndex,
64 Context* context);
65 ~ImmutableMessageFieldGenerator() override;
66
67 // implements ImmutableFieldGenerator
68 // ---------------------------------------
69 int GetNumBitsForMessage() const override;
70 int GetNumBitsForBuilder() const override;
71 void GenerateInterfaceMembers(io::Printer* printer) const override;
72 void GenerateMembers(io::Printer* printer) const override;
73 void GenerateBuilderMembers(io::Printer* printer) const override;
74 void GenerateInitializationCode(io::Printer* printer) const override;
75 void GenerateBuilderClearCode(io::Printer* printer) const override;
76 void GenerateMergingCode(io::Printer* printer) const override;
77 void GenerateBuildingCode(io::Printer* printer) const override;
78 void GenerateParsingCode(io::Printer* printer) const override;
79 void GenerateParsingDoneCode(io::Printer* printer) const override;
80 void GenerateSerializationCode(io::Printer* printer) const override;
81 void GenerateSerializedSizeCode(io::Printer* printer) const override;
82 void GenerateFieldBuilderInitializationCode(
83 io::Printer* printer) const override;
84 void GenerateEqualsCode(io::Printer* printer) const override;
85 void GenerateHashCode(io::Printer* printer) const override;
86 void GenerateKotlinDslMembers(io::Printer* printer) const override;
87
88 std::string GetBoxedType() const override;
89
90 protected:
91 const FieldDescriptor* descriptor_;
92 std::map<std::string, std::string> variables_;
93 ClassNameResolver* name_resolver_;
94
95 void PrintNestedBuilderCondition(io::Printer* printer,
96 const char* regular_case,
97 const char* nested_builder_case) const;
98 void PrintNestedBuilderFunction(io::Printer* printer,
99 const char* method_prototype,
100 const char* regular_case,
101 const char* nested_builder_case,
102 const char* trailing_code) const;
103
104 private:
105 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageFieldGenerator);
106 void GenerateKotlinOrNull(io::Printer* printer) const;
107};
108
109class ImmutableMessageOneofFieldGenerator
110 : public ImmutableMessageFieldGenerator {
111 public:
112 ImmutableMessageOneofFieldGenerator(const FieldDescriptor* descriptor,
113 int messageBitIndex, int builderBitIndex,
114 Context* context);
115 ~ImmutableMessageOneofFieldGenerator() override;
116
117 void GenerateMembers(io::Printer* printer) const override;
118 void GenerateBuilderMembers(io::Printer* printer) const override;
119 void GenerateBuildingCode(io::Printer* printer) const override;
120 void GenerateMergingCode(io::Printer* printer) const override;
121 void GenerateParsingCode(io::Printer* printer) const override;
122 void GenerateSerializationCode(io::Printer* printer) const override;
123 void GenerateSerializedSizeCode(io::Printer* printer) const override;
124
125 private:
126 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(ImmutableMessageOneofFieldGenerator);
127};
128
129class RepeatedImmutableMessageFieldGenerator : public ImmutableFieldGenerator {
130 public:
131 explicit RepeatedImmutableMessageFieldGenerator(
132 const FieldDescriptor* descriptor, int messageBitIndex,
133 int builderBitIndex, Context* context);
134 ~RepeatedImmutableMessageFieldGenerator() override;
135
136 // implements ImmutableFieldGenerator ---------------------------------------
137 int GetNumBitsForMessage() const override;
138 int GetNumBitsForBuilder() const override;
139 void GenerateInterfaceMembers(io::Printer* printer) const override;
140 void GenerateMembers(io::Printer* printer) const override;
141 void GenerateBuilderMembers(io::Printer* printer) const override;
142 void GenerateInitializationCode(io::Printer* printer) const override;
143 void GenerateBuilderClearCode(io::Printer* printer) const override;
144 void GenerateMergingCode(io::Printer* printer) const override;
145 void GenerateBuildingCode(io::Printer* printer) const override;
146 void GenerateParsingCode(io::Printer* printer) const override;
147 void GenerateParsingDoneCode(io::Printer* printer) const override;
148 void GenerateSerializationCode(io::Printer* printer) const override;
149 void GenerateSerializedSizeCode(io::Printer* printer) const override;
150 void GenerateFieldBuilderInitializationCode(
151 io::Printer* printer) const override;
152 void GenerateEqualsCode(io::Printer* printer) const override;
153 void GenerateHashCode(io::Printer* printer) const override;
154 void GenerateKotlinDslMembers(io::Printer* printer) const override;
155
156 std::string GetBoxedType() const override;
157
158 protected:
159 const FieldDescriptor* descriptor_;
160 std::map<std::string, std::string> variables_;
161 ClassNameResolver* name_resolver_;
162
163 void PrintNestedBuilderCondition(io::Printer* printer,
164 const char* regular_case,
165 const char* nested_builder_case) const;
166 void PrintNestedBuilderFunction(io::Printer* printer,
167 const char* method_prototype,
168 const char* regular_case,
169 const char* nested_builder_case,
170 const char* trailing_code) const;
171
172 private:
173 GOOGLE_DISALLOW_EVIL_CONSTRUCTORS(RepeatedImmutableMessageFieldGenerator);
174};
175
176} // namespace java
177} // namespace compiler
178} // namespace protobuf
179} // namespace google
180
181#endif // GOOGLE_PROTOBUF_COMPILER_JAVA_MESSAGE_FIELD_H__
182