Merge "Prevent conflicts between local and proto variable names."

This commit is contained in:
Wink Saville 2013-04-04 12:55:12 +00:00 committed by Gerrit Code Review
commit d4bb580f1b
8 changed files with 68 additions and 118 deletions

View File

@ -84,26 +84,21 @@ GenerateMembers(io::Printer* printer) const {
"public int $name$ = $default$;\n");
}
void EnumFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, "$name$ = other.$name$;\n");
}
void EnumFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
printer->Print(variables_,
" $name$ = input.readInt32();\n");
" this.$name$ = input.readInt32();\n");
}
void EnumFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
if (descriptor_->is_required()) {
printer->Print(variables_,
"output.writeInt32($number$, $name$);\n");
"output.writeInt32($number$, this.$name$);\n");
} else {
printer->Print(variables_,
"if ($name$ != $default$) {\n"
" output.writeInt32($number$, $name$);\n"
"if (this.$name$ != $default$) {\n"
" output.writeInt32($number$, this.$name$);\n"
"}\n");
}
}
@ -113,12 +108,12 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
if (descriptor_->is_required()) {
printer->Print(variables_,
"size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeInt32Size($number$, $name$);\n");
" .computeInt32Size($number$, this.$name$);\n");
} else {
printer->Print(variables_,
"if ($name$ != $default$) {\n"
"if (this.$name$ != $default$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeInt32Size($number$, $name$);\n"
" .computeInt32Size($number$, this.$name$);\n"
"}\n");
}
}
@ -147,16 +142,6 @@ GenerateMembers(io::Printer* printer) const {
}
}
void RepeatedEnumFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
"if (other.$name$.length > 0) {\n"
" int[] merged = java.util.Arrays.copyOf(result.$name$, result.$name$.length + other.$name$.length);\n"
" java.lang.System.arraycopy(other.$name$, 0, merged, results.$name$.length, other.$name$.length);\n"
" result.$name$ = merged;\n"
"}\n");
}
void RepeatedEnumFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
// First, figure out the length of the array, then parse.
@ -172,41 +157,41 @@ GenerateParsingCode(io::Printer* printer) const {
" arrayLength++;\n"
"}\n"
"input.rewindToPosition(startPos);\n"
"$name$ = new $type$[arrayLength];\n"
"this.$name$ = new $type$[arrayLength];\n"
"for (int i = 0; i < arrayLength; i++) {\n"
" $name$[i] = input.readInt32();\n"
" this.$name$[i] = input.readInt32();\n"
"}\n"
"input.popLimit(limit);\n");
} else {
printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = $name$.length;\n"
"$name$ = java.util.Arrays.copyOf($name$, $name$.length + arrayLength);\n"
"for (; i < $name$.length - 1; i++) {\n"
" $name$[i] = input.readInt32();\n"
"int i = this.$name$.length;\n"
"this.$name$ = java.util.Arrays.copyOf(this.$name$, this.$name$.length + arrayLength);\n"
"for (; i < this.$name$.length - 1; i++) {\n"
" this.$name$[i] = input.readInt32();\n"
" input.readTag();\n"
"}\n"
"// Last one without readTag.\n"
"$name$[i] = input.readInt32();\n");
"this.$name$[i] = input.readInt32();\n");
}
}
void RepeatedEnumFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($name$.length > 0) {\n");
"if (this.$name$.length > 0) {\n");
printer->Indent();
if (descriptor_->options().packed()) {
printer->Print(variables_,
"output.writeRawVarint32($tag$);\n"
"output.writeRawVarint32($name$MemoizedSerializedSize);\n"
"for (int element : $name$) {\n"
"for (int element : this.$name$) {\n"
" output.writeRawVarint32(element);\n"
"}\n");
} else {
printer->Print(variables_,
"for (int element : $name$) {\n"
"for (int element : this.$name$) {\n"
" output.writeInt32($number$, element);\n"
"}\n");
}
@ -218,12 +203,12 @@ GenerateSerializationCode(io::Printer* printer) const {
void RepeatedEnumFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($name$.length > 0) {\n");
"if (this.$name$.length > 0) {\n");
printer->Indent();
printer->Print(variables_,
"int dataSize = 0;\n"
"for (int element : $name$) {\n"
"for (int element : this.$name$) {\n"
" dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .computeInt32SizeNoTag(element);\n"
"}\n");
@ -239,7 +224,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
"$name$MemoizedSerializedSize = dataSize;\n");
} else {
printer->Print(variables_,
"size += $tag_size$ * $name$.length;\n");
"size += $tag_size$ * this.$name$.length;\n");
}
printer->Outdent();

View File

@ -51,7 +51,6 @@ class EnumFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;
@ -72,7 +71,6 @@ class RepeatedEnumFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;

View File

@ -58,7 +58,6 @@ class FieldGenerator {
virtual ~FieldGenerator();
virtual void GenerateMembers(io::Printer* printer) const = 0;
virtual void GenerateMergingCode(io::Printer* printer) const = 0;
virtual void GenerateParsingCode(io::Printer* printer) const = 0;
virtual void GenerateSerializationCode(io::Printer* printer) const = 0;
virtual void GenerateSerializedSizeCode(io::Printer* printer) const = 0;

View File

@ -87,42 +87,34 @@ GenerateMembers(io::Printer* printer) const {
"public $type$ $name$ = null;\n");
}
void MessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
"if (other.$name$ != null) {\n"
" merge$capitalized_name$(other.$name$);\n"
"}\n");
}
void MessageFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
printer->Print(variables_,
"$name$ = new $type$();\n");
"this.$name$ = new $type$();\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
printer->Print(variables_,
"input.readGroup($name$, $number$);\n");
"input.readGroup(this.$name$, $number$);\n");
} else {
printer->Print(variables_,
"input.readMessage($name$);\n");
"input.readMessage(this.$name$);\n");
}
}
void MessageFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($name$ != null) {\n"
" output.write$group_or_message$($number$, $name$);\n"
"if (this.$name$ != null) {\n"
" output.write$group_or_message$($number$, this.$name$);\n"
"}\n");
}
void MessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($name$ != null) {\n"
"if (this.$name$ != null) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$group_or_message$Size($number$, $name$);\n"
" .compute$group_or_message$Size($number$, this.$name$);\n"
"}\n");
}
@ -146,53 +138,43 @@ GenerateMembers(io::Printer* printer) const {
"public $type$[] $name$ = $type$.EMPTY_ARRAY;\n");
}
void RepeatedMessageFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
"if (other.$name$.length > 0) {\n"
" $type$[] merged = java.util.Arrays.copyOf(result.$name$, result.$name$.length + other.$name$.length);\n"
" java.lang.System.arraycopy(other.$name$, 0, merged, results.$name$.length, other.$name$.length);\n"
" result.$name$ = merged;\n"
"}\n");
}
void RepeatedMessageFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
// First, figure out the length of the array, then parse.
printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = $name$.length;\n"
"$name$ = java.util.Arrays.copyOf($name$, i + arrayLength);\n"
"for (; i < $name$.length - 1; i++) {\n"
" $name$[i] = new $type$();\n");
"int i = this.$name$.length;\n"
"this.$name$ = java.util.Arrays.copyOf(this.$name$, i + arrayLength);\n"
"for (; i < this.$name$.length - 1; i++) {\n"
" this.$name$[i] = new $type$();\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
printer->Print(variables_,
" input.readGroup($name$[i], $number$);\n");
" input.readGroup(this.$name$[i], $number$);\n");
} else {
printer->Print(variables_,
" input.readMessage($name$[i]);\n");
" input.readMessage(this.$name$[i]);\n");
}
printer->Print(variables_,
" input.readTag();\n"
"}\n"
"// Last one without readTag.\n"
"$name$[i] = new $type$();\n");
"this.$name$[i] = new $type$();\n");
if (descriptor_->type() == FieldDescriptor::TYPE_GROUP) {
printer->Print(variables_,
"input.readGroup($name$[i], $number$);\n");
"input.readGroup(this.$name$[i], $number$);\n");
} else {
printer->Print(variables_,
"input.readMessage($name$[i]);\n");
"input.readMessage(this.$name$[i]);\n");
}
}
void RepeatedMessageFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
printer->Print(variables_,
"for ($type$ element : $name$) {\n"
"for ($type$ element : this.$name$) {\n"
" output.write$group_or_message$($number$, element);\n"
"}\n");
}
@ -200,7 +182,7 @@ GenerateSerializationCode(io::Printer* printer) const {
void RepeatedMessageFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"for ($type$ element : $name$) {\n"
"for ($type$ element : this.$name$) {\n"
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$group_or_message$Size($number$, element);\n"
"}\n");

View File

@ -51,7 +51,6 @@ class MessageFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;
@ -73,7 +72,6 @@ class RepeatedMessageFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;

View File

@ -290,36 +290,31 @@ GenerateMembers(io::Printer* printer) const {
}
}
void PrimitiveFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_, "$name$ = other.$name$;\n");
}
void PrimitiveFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
printer->Print(variables_,
"$name$ = input.read$capitalized_type$();\n");
"this.$name$ = input.read$capitalized_type$();\n");
}
void PrimitiveFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
if (descriptor_->is_required()) {
printer->Print(variables_,
"output.write$capitalized_type$($number$, $name$);\n");
"output.write$capitalized_type$($number$, this.$name$);\n");
} else {
if (IsArrayType(GetJavaType(descriptor_))) {
printer->Print(variables_,
"if (!java.util.Arrays.equals($name$, $default$)) {\n");
"if (!java.util.Arrays.equals(this.$name$, $default$)) {\n");
} else if (IsReferenceType(GetJavaType(descriptor_))) {
printer->Print(variables_,
"if (!$name$.equals($default$)) {\n");
"if (!this.$name$.equals($default$)) {\n");
} else {
printer->Print(variables_,
"if ($name$ != $default$) {\n");
"if (this.$name$ != $default$) {\n");
}
printer->Print(variables_,
" output.write$capitalized_type$($number$, $name$);\n"
" output.write$capitalized_type$($number$, this.$name$);\n"
"}\n");
}
}
@ -329,22 +324,22 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
if (descriptor_->is_required()) {
printer->Print(variables_,
"size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$Size($number$, $name$);\n");
" .compute$capitalized_type$Size($number$, this.$name$);\n");
} else {
if (IsArrayType(GetJavaType(descriptor_))) {
printer->Print(variables_,
"if (!java.util.Arrays.equals($name$, $default$)) {\n");
"if (!java.util.Arrays.equals(this.$name$, $default$)) {\n");
} else if (IsReferenceType(GetJavaType(descriptor_))) {
printer->Print(variables_,
"if (!$name$.equals($default$)) {\n");
"if (!this.$name$.equals($default$)) {\n");
} else {
printer->Print(variables_,
"if ($name$ != $default$) {\n");
"if (this.$name$ != $default$) {\n");
}
printer->Print(variables_,
" size += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$Size($number$, $name$);\n"
" .compute$capitalized_type$Size($number$, this.$name$);\n"
"}\n");
}
}
@ -373,16 +368,6 @@ GenerateMembers(io::Printer* printer) const {
}
}
void RepeatedPrimitiveFieldGenerator::
GenerateMergingCode(io::Printer* printer) const {
printer->Print(variables_,
"if (other.$name$.length > 0) {\n"
" $type$[] merged = java.util.Arrays.copyOf(result.$name$, result.$name$.length + other.$name$.length);\n"
" java.lang.System.arraycopy(other.$name$, 0, merged, results.$name$.length, other.$name$.length);\n"
" result.$name$ = merged;\n"
"}\n");
}
void RepeatedPrimitiveFieldGenerator::
GenerateParsingCode(io::Printer* printer) const {
// First, figure out the length of the array, then parse.
@ -398,22 +383,22 @@ GenerateParsingCode(io::Printer* printer) const {
" arrayLength++;\n"
"}\n"
"input.rewindToPosition(startPos);\n"
"$name$ = new $type$[arrayLength];\n"
"this.$name$ = new $type$[arrayLength];\n"
"for (int i = 0; i < arrayLength; i++) {\n"
" $name$[i] = input.read$capitalized_type$();\n"
" this.$name$[i] = input.read$capitalized_type$();\n"
"}\n"
"input.popLimit(limit);\n");
} else {
printer->Print(variables_,
"int arrayLength = com.google.protobuf.nano.WireFormatNano.getRepeatedFieldArrayLength(input, $tag$);\n"
"int i = $name$.length;\n"
"$name$ = java.util.Arrays.copyOf($name$, $name$.length + arrayLength);\n"
"for (; i < $name$.length - 1; i++) {\n"
" $name$[i] = input.read$capitalized_type$();\n"
"int i = this.$name$.length;\n"
"this.$name$ = java.util.Arrays.copyOf(this.$name$, this.$name$.length + arrayLength);\n"
"for (; i < this.$name$.length - 1; i++) {\n"
" this.$name$[i] = input.read$capitalized_type$();\n"
" input.readTag();\n"
"}\n"
"// Last one without readTag.\n"
"$name$[i] = input.read$capitalized_type$();\n");
"this.$name$[i] = input.read$capitalized_type$();\n");
}
}
@ -421,17 +406,17 @@ void RepeatedPrimitiveFieldGenerator::
GenerateSerializationCode(io::Printer* printer) const {
if (descriptor_->options().packed()) {
printer->Print(variables_,
"if ($name$.length > 0) {\n"
"if (this.$name$.length > 0) {\n"
" output.writeRawVarint32($tag$);\n"
" output.writeRawVarint32($name$MemoizedSerializedSize);\n"
"}\n");
printer->Print(variables_,
"for ($type$ element : $name$) {\n"
"for ($type$ element : this.$name$) {\n"
" output.write$capitalized_type$NoTag(element);\n"
"}\n");
} else {
printer->Print(variables_,
"for ($type$ element : $name$) {\n"
"for ($type$ element : this.$name$) {\n"
" output.write$capitalized_type$($number$, element);\n"
"}\n");
}
@ -440,19 +425,19 @@ GenerateSerializationCode(io::Printer* printer) const {
void RepeatedPrimitiveFieldGenerator::
GenerateSerializedSizeCode(io::Printer* printer) const {
printer->Print(variables_,
"if ($name$.length > 0) {\n");
"if (this.$name$.length > 0) {\n");
printer->Indent();
if (FixedSize(descriptor_->type()) == -1) {
printer->Print(variables_,
"int dataSize = 0;\n"
"for ($type$ element : $name$) {\n"
"for ($type$ element : this.$name$) {\n"
" dataSize += com.google.protobuf.nano.CodedOutputByteBufferNano\n"
" .compute$capitalized_type$SizeNoTag(element);\n"
"}\n");
} else {
printer->Print(variables_,
"int dataSize = $fixed_size$ * $name$.length;\n");
"int dataSize = $fixed_size$ * this.$name$.length;\n");
}
printer->Print(
@ -466,7 +451,7 @@ GenerateSerializedSizeCode(io::Printer* printer) const {
"$name$MemoizedSerializedSize = dataSize;\n");
} else {
printer->Print(variables_,
"size += $tag_size$ * $name$.length;\n");
"size += $tag_size$ * this.$name$.length;\n");
}
printer->Outdent();

View File

@ -51,7 +51,6 @@ class PrimitiveFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;
@ -72,7 +71,6 @@ class RepeatedPrimitiveFieldGenerator : public FieldGenerator {
// implements FieldGenerator ---------------------------------------
void GenerateMembers(io::Printer* printer) const;
void GenerateMergingCode(io::Printer* printer) const;
void GenerateParsingCode(io::Printer* printer) const;
void GenerateSerializationCode(io::Printer* printer) const;
void GenerateSerializedSizeCode(io::Printer* printer) const;

View File

@ -152,6 +152,11 @@ message TestAllTypesNano {
optional string default_cord = 85 [ctype=CORD,default="123"];
required int32 id = 86;
// Try to cause conflicts.
optional int32 tag = 93;
optional int32 get_serialized_size = 94;
optional int32 write_to = 95;
}
message ForeignMessageNano {