blob: 3c7bc5c4674fbfc6dfb3a66594886cb8b3b8b797 [file] [log] [blame]
temporal40ee5512008-07-10 02:12:20 +00001// Protocol Buffers - Google's data interchange format
kenton@google.com24bf56f2008-09-24 20:31:01 +00002// Copyright 2008 Google Inc. All rights reserved.
Feng Xiaoe4288622014-10-01 16:26:23 -07003// https://developers.google.com/protocol-buffers/
temporal40ee5512008-07-10 02:12:20 +00004//
kenton@google.com24bf56f2008-09-24 20:31:01 +00005// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are
7// met:
temporal40ee5512008-07-10 02:12:20 +00008//
kenton@google.com24bf56f2008-09-24 20:31:01 +00009// * 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.
temporal40ee5512008-07-10 02:12:20 +000018//
kenton@google.com24bf56f2008-09-24 20:31:01 +000019// 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.
temporal40ee5512008-07-10 02:12:20 +000030
31// Author: kenton@google.com (Kenton Varda)
32// Based on original Protocol Buffers design by
33// Sanjay Ghemawat, Jeff Dean, and others.
34
35#include <google/protobuf/compiler/java/java_field.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000036
37#include <memory>
Feng Xiao6ef984a2014-11-10 17:34:54 -080038#ifndef _SHARED_PTR_H
39#include <google/protobuf/stubs/shared_ptr.h>
40#endif
jieluo@google.com4de8f552014-07-18 00:47:59 +000041
Feng Xiaoeee38b02015-08-22 18:25:48 -070042#include <google/protobuf/stubs/logging.h>
temporal40ee5512008-07-10 02:12:20 +000043#include <google/protobuf/stubs/common.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000044#include <google/protobuf/compiler/java/java_context.h>
45#include <google/protobuf/compiler/java/java_enum_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070046#include <google/protobuf/compiler/java/java_enum_field_lite.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000047#include <google/protobuf/compiler/java/java_helpers.h>
48#include <google/protobuf/compiler/java/java_lazy_message_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070049#include <google/protobuf/compiler/java/java_lazy_message_field_lite.h>
Feng Xiao6ef984a2014-11-10 17:34:54 -080050#include <google/protobuf/compiler/java/java_map_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070051#include <google/protobuf/compiler/java/java_map_field_lite.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000052#include <google/protobuf/compiler/java/java_message_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070053#include <google/protobuf/compiler/java/java_message_field_lite.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000054#include <google/protobuf/compiler/java/java_primitive_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070055#include <google/protobuf/compiler/java/java_primitive_field_lite.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000056#include <google/protobuf/compiler/java/java_string_field.h>
Bo Yang5db21732015-05-21 14:28:59 -070057#include <google/protobuf/compiler/java/java_string_field_lite.h>
jieluo@google.com4de8f552014-07-18 00:47:59 +000058#include <google/protobuf/io/printer.h>
59#include <google/protobuf/stubs/strutil.h>
60#include <google/protobuf/stubs/substitute.h>
temporal40ee5512008-07-10 02:12:20 +000061
Feng Xiao6ef984a2014-11-10 17:34:54 -080062
temporal40ee5512008-07-10 02:12:20 +000063namespace google {
64namespace protobuf {
65namespace compiler {
66namespace java {
67
jieluo@google.com4de8f552014-07-18 00:47:59 +000068namespace {
temporal40ee5512008-07-10 02:12:20 +000069
jieluo@google.com4de8f552014-07-18 00:47:59 +000070ImmutableFieldGenerator* MakeImmutableGenerator(
71 const FieldDescriptor* field, int messageBitIndex, int builderBitIndex,
72 Context* context) {
73 if (field->is_repeated()) {
74 switch (GetJavaType(field)) {
75 case JAVATYPE_MESSAGE:
Feng Xiao6ef984a2014-11-10 17:34:54 -080076 if (IsMapEntry(field->message_type())) {
77 return new ImmutableMapFieldGenerator(
jieluo@google.com4de8f552014-07-18 00:47:59 +000078 field, messageBitIndex, builderBitIndex, context);
79 } else {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -070080 if (IsLazy(field, context->EnforceLite())) {
Feng Xiao6ef984a2014-11-10 17:34:54 -080081 return new RepeatedImmutableLazyMessageFieldGenerator(
82 field, messageBitIndex, builderBitIndex, context);
83 } else {
84 return new RepeatedImmutableMessageFieldGenerator(
85 field, messageBitIndex, builderBitIndex, context);
86 }
jieluo@google.com4de8f552014-07-18 00:47:59 +000087 }
88 case JAVATYPE_ENUM:
89 return new RepeatedImmutableEnumFieldGenerator(
90 field, messageBitIndex, builderBitIndex, context);
91 case JAVATYPE_STRING:
92 return new RepeatedImmutableStringFieldGenerator(
93 field, messageBitIndex, builderBitIndex, context);
94 default:
95 return new RepeatedImmutablePrimitiveFieldGenerator(
96 field, messageBitIndex, builderBitIndex, context);
97 }
98 } else {
99 if (field->containing_oneof()) {
100 switch (GetJavaType(field)) {
101 case JAVATYPE_MESSAGE:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700102 if (IsLazy(field, context->EnforceLite())) {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000103 return new ImmutableLazyMessageOneofFieldGenerator(
104 field, messageBitIndex, builderBitIndex, context);
105 } else {
106 return new ImmutableMessageOneofFieldGenerator(
107 field, messageBitIndex, builderBitIndex, context);
108 }
109 case JAVATYPE_ENUM:
110 return new ImmutableEnumOneofFieldGenerator(
111 field, messageBitIndex, builderBitIndex, context);
112 case JAVATYPE_STRING:
113 return new ImmutableStringOneofFieldGenerator(
114 field, messageBitIndex, builderBitIndex, context);
115 default:
116 return new ImmutablePrimitiveOneofFieldGenerator(
117 field, messageBitIndex, builderBitIndex, context);
118 }
119 } else {
120 switch (GetJavaType(field)) {
121 case JAVATYPE_MESSAGE:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700122 if (IsLazy(field, context->EnforceLite())) {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000123 return new ImmutableLazyMessageFieldGenerator(
124 field, messageBitIndex, builderBitIndex, context);
125 } else {
126 return new ImmutableMessageFieldGenerator(
127 field, messageBitIndex, builderBitIndex, context);
128 }
129 case JAVATYPE_ENUM:
130 return new ImmutableEnumFieldGenerator(
131 field, messageBitIndex, builderBitIndex, context);
132 case JAVATYPE_STRING:
133 return new ImmutableStringFieldGenerator(
134 field, messageBitIndex, builderBitIndex, context);
135 default:
136 return new ImmutablePrimitiveFieldGenerator(
137 field, messageBitIndex, builderBitIndex, context);
138 }
139 }
140 }
141}
142
Bo Yang5db21732015-05-21 14:28:59 -0700143ImmutableFieldLiteGenerator* MakeImmutableLiteGenerator(
144 const FieldDescriptor* field, int messageBitIndex, int builderBitIndex,
145 Context* context) {
146 if (field->is_repeated()) {
147 switch (GetJavaType(field)) {
148 case JAVATYPE_MESSAGE:
149 if (IsMapEntry(field->message_type())) {
150 return new ImmutableMapFieldLiteGenerator(
151 field, messageBitIndex, builderBitIndex, context);
152 } else {
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700153 if (IsLazy(field, context->EnforceLite())) {
Bo Yang5db21732015-05-21 14:28:59 -0700154 return new RepeatedImmutableLazyMessageFieldLiteGenerator(
155 field, messageBitIndex, builderBitIndex, context);
156 } else {
157 return new RepeatedImmutableMessageFieldLiteGenerator(
158 field, messageBitIndex, builderBitIndex, context);
159 }
160 }
161 case JAVATYPE_ENUM:
162 return new RepeatedImmutableEnumFieldLiteGenerator(
163 field, messageBitIndex, builderBitIndex, context);
164 case JAVATYPE_STRING:
165 return new RepeatedImmutableStringFieldLiteGenerator(
166 field, messageBitIndex, builderBitIndex, context);
167 default:
168 return new RepeatedImmutablePrimitiveFieldLiteGenerator(
169 field, messageBitIndex, builderBitIndex, context);
170 }
171 } else {
172 if (field->containing_oneof()) {
173 switch (GetJavaType(field)) {
174 case JAVATYPE_MESSAGE:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700175 if (IsLazy(field, context->EnforceLite())) {
Bo Yang5db21732015-05-21 14:28:59 -0700176 return new ImmutableLazyMessageOneofFieldLiteGenerator(
177 field, messageBitIndex, builderBitIndex, context);
178 } else {
179 return new ImmutableMessageOneofFieldLiteGenerator(
180 field, messageBitIndex, builderBitIndex, context);
181 }
182 case JAVATYPE_ENUM:
183 return new ImmutableEnumOneofFieldLiteGenerator(
184 field, messageBitIndex, builderBitIndex, context);
185 case JAVATYPE_STRING:
186 return new ImmutableStringOneofFieldLiteGenerator(
187 field, messageBitIndex, builderBitIndex, context);
188 default:
189 return new ImmutablePrimitiveOneofFieldLiteGenerator(
190 field, messageBitIndex, builderBitIndex, context);
191 }
192 } else {
193 switch (GetJavaType(field)) {
194 case JAVATYPE_MESSAGE:
Jisi Liu3b3c8ab2016-03-30 11:39:59 -0700195 if (IsLazy(field, context->EnforceLite())) {
Bo Yang5db21732015-05-21 14:28:59 -0700196 return new ImmutableLazyMessageFieldLiteGenerator(
197 field, messageBitIndex, builderBitIndex, context);
198 } else {
199 return new ImmutableMessageFieldLiteGenerator(
200 field, messageBitIndex, builderBitIndex, context);
201 }
202 case JAVATYPE_ENUM:
203 return new ImmutableEnumFieldLiteGenerator(
204 field, messageBitIndex, builderBitIndex, context);
205 case JAVATYPE_STRING:
206 return new ImmutableStringFieldLiteGenerator(
207 field, messageBitIndex, builderBitIndex, context);
208 default:
209 return new ImmutablePrimitiveFieldLiteGenerator(
210 field, messageBitIndex, builderBitIndex, context);
211 }
212 }
213 }
214}
215
jieluo@google.com4de8f552014-07-18 00:47:59 +0000216
217static inline void ReportUnexpectedPackedFieldsCall(io::Printer* printer) {
kenton@google.comfccb1462009-12-18 02:11:36 +0000218 // Reaching here indicates a bug. Cases are:
jieluo@google.com4de8f552014-07-18 00:47:59 +0000219 // - This FieldGenerator should support packing,
220 // but this method should be overridden.
221 // - This FieldGenerator doesn't support packing, and this method
222 // should never have been called.
kenton@google.comfccb1462009-12-18 02:11:36 +0000223 GOOGLE_LOG(FATAL) << "GenerateParsingCodeFromPacked() "
224 << "called on field generator that does not support packing.";
225}
226
jieluo@google.com4de8f552014-07-18 00:47:59 +0000227} // namespace
228
229ImmutableFieldGenerator::~ImmutableFieldGenerator() {}
230
231void ImmutableFieldGenerator::
232GenerateParsingCodeFromPacked(io::Printer* printer) const {
233 ReportUnexpectedPackedFieldsCall(printer);
234}
235
Bo Yang5db21732015-05-21 14:28:59 -0700236ImmutableFieldLiteGenerator::~ImmutableFieldLiteGenerator() {}
237
238void ImmutableFieldLiteGenerator::
239GenerateParsingCodeFromPacked(io::Printer* printer) const {
240 ReportUnexpectedPackedFieldsCall(printer);
241}
242
jieluo@google.com4de8f552014-07-18 00:47:59 +0000243// ===================================================================
244
245template <>
246FieldGeneratorMap<ImmutableFieldGenerator>::FieldGeneratorMap(
247 const Descriptor* descriptor, Context* context)
248 : descriptor_(descriptor),
Feng Xiaof157a562014-11-14 11:50:31 -0800249 field_generators_(new google::protobuf::scoped_ptr<
jieluo@google.com4de8f552014-07-18 00:47:59 +0000250 ImmutableFieldGenerator>[descriptor->field_count()]) {
temporal40ee5512008-07-10 02:12:20 +0000251
liujisi@google.com33165fe2010-11-02 13:14:58 +0000252 // Construct all the FieldGenerators and assign them bit indices for their
253 // bit fields.
254 int messageBitIndex = 0;
255 int builderBitIndex = 0;
temporal40ee5512008-07-10 02:12:20 +0000256 for (int i = 0; i < descriptor->field_count(); i++) {
jieluo@google.com4de8f552014-07-18 00:47:59 +0000257 ImmutableFieldGenerator* generator = MakeImmutableGenerator(
258 descriptor->field(i), messageBitIndex, builderBitIndex, context);
liujisi@google.com33165fe2010-11-02 13:14:58 +0000259 field_generators_[i].reset(generator);
260 messageBitIndex += generator->GetNumBitsForMessage();
261 builderBitIndex += generator->GetNumBitsForBuilder();
temporal40ee5512008-07-10 02:12:20 +0000262 }
jieluo@google.com4de8f552014-07-18 00:47:59 +0000263}
264
265template<>
266FieldGeneratorMap<ImmutableFieldGenerator>::~FieldGeneratorMap() {}
267
Bo Yang5db21732015-05-21 14:28:59 -0700268template <>
269FieldGeneratorMap<ImmutableFieldLiteGenerator>::FieldGeneratorMap(
270 const Descriptor* descriptor, Context* context)
271 : descriptor_(descriptor),
272 field_generators_(new google::protobuf::scoped_ptr<
273 ImmutableFieldLiteGenerator>[descriptor->field_count()]) {
274 // Construct all the FieldGenerators and assign them bit indices for their
275 // bit fields.
276 int messageBitIndex = 0;
277 int builderBitIndex = 0;
278 for (int i = 0; i < descriptor->field_count(); i++) {
279 ImmutableFieldLiteGenerator* generator = MakeImmutableLiteGenerator(
280 descriptor->field(i), messageBitIndex, builderBitIndex, context);
281 field_generators_[i].reset(generator);
282 messageBitIndex += generator->GetNumBitsForMessage();
283 builderBitIndex += generator->GetNumBitsForBuilder();
284 }
285}
286
287template<>
288FieldGeneratorMap<ImmutableFieldLiteGenerator>::~FieldGeneratorMap() {}
289
jieluo@google.com4de8f552014-07-18 00:47:59 +0000290
291void SetCommonFieldVariables(const FieldDescriptor* descriptor,
292 const FieldGeneratorInfo* info,
293 map<string, string>* variables) {
294 (*variables)["field_name"] = descriptor->name();
295 (*variables)["name"] = info->name;
296 (*variables)["capitalized_name"] = info->capitalized_name;
297 (*variables)["disambiguated_reason"] = info->disambiguated_reason;
298 (*variables)["constant_name"] = FieldConstantName(descriptor);
299 (*variables)["number"] = SimpleItoa(descriptor->number());
300}
301
302void SetCommonOneofVariables(const FieldDescriptor* descriptor,
303 const OneofGeneratorInfo* info,
304 map<string, string>* variables) {
305 (*variables)["oneof_name"] = info->name;
306 (*variables)["oneof_capitalized_name"] = info->capitalized_name;
307 (*variables)["oneof_index"] =
308 SimpleItoa(descriptor->containing_oneof()->index());
309 (*variables)["set_oneof_case_message"] = info->name +
310 "Case_ = " + SimpleItoa(descriptor->number());
311 (*variables)["clear_oneof_case_message"] = info->name +
312 "Case_ = 0";
313 (*variables)["has_oneof_case_message"] = info->name +
314 "Case_ == " + SimpleItoa(descriptor->number());
315}
316
317void PrintExtraFieldInfo(const map<string, string>& variables,
318 io::Printer* printer) {
319 const map<string, string>::const_iterator it =
320 variables.find("disambiguated_reason");
321 if (it != variables.end() && !it->second.empty()) {
322 printer->Print(
323 variables,
324 "// An alternative name is used for field \"$field_name$\" because:\n"
325 "// $disambiguated_reason$\n");
temporal40ee5512008-07-10 02:12:20 +0000326 }
327}
328
temporal40ee5512008-07-10 02:12:20 +0000329} // namespace java
330} // namespace compiler
331} // namespace protobuf
332} // namespace google