Prevent conflicts between local and proto variable names.

Prefix access to proto variable names with "this.". Also remove unused
GenerateMergingCode.

Change-Id: I5f07d3252fc385c4174d9165b64785b40f676e17
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
index 68020db..2c6fd5d 100644
--- a/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.cc
@@ -85,25 +85,20 @@
 }
 
 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 @@
   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");
   }
 }
@@ -148,16 +143,6 @@
 }
 
 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.
   if (descriptor_->options().packed()) {
@@ -172,41 +157,41 @@
       "  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 @@
 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 @@
       "$name$MemoizedSerializedSize = dataSize;\n");
   } else {
     printer->Print(variables_,
-        "size += $tag_size$ * $name$.length;\n");
+        "size += $tag_size$ * this.$name$.length;\n");
   }
 
   printer->Outdent();
diff --git a/src/google/protobuf/compiler/javanano/javanano_enum_field.h b/src/google/protobuf/compiler/javanano/javanano_enum_field.h
index 8985b0f..6e1a443 100644
--- a/src/google/protobuf/compiler/javanano/javanano_enum_field.h
+++ b/src/google/protobuf/compiler/javanano/javanano_enum_field.h
@@ -51,7 +51,6 @@
 
   // 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 @@
 
   // 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;
diff --git a/src/google/protobuf/compiler/javanano/javanano_field.h b/src/google/protobuf/compiler/javanano/javanano_field.h
index a0bf909..7db48a5 100644
--- a/src/google/protobuf/compiler/javanano/javanano_field.h
+++ b/src/google/protobuf/compiler/javanano/javanano_field.h
@@ -58,7 +58,6 @@
   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;
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.cc b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
index 6d89bd5..01529c8 100644
--- a/src/google/protobuf/compiler/javanano/javanano_message_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_message_field.cc
@@ -88,41 +88,33 @@
 }
 
 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");
 }
 
@@ -147,52 +139,42 @@
 }
 
 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 @@
 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");
diff --git a/src/google/protobuf/compiler/javanano/javanano_message_field.h b/src/google/protobuf/compiler/javanano/javanano_message_field.h
index 5fb0061..943a832 100644
--- a/src/google/protobuf/compiler/javanano/javanano_message_field.h
+++ b/src/google/protobuf/compiler/javanano/javanano_message_field.h
@@ -51,7 +51,6 @@
 
   // 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 @@
 
   // 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;
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
index 041d22f..f5b25b6 100644
--- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.cc
@@ -291,35 +291,30 @@
 }
 
 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 @@
   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");
   }
 }
@@ -374,16 +369,6 @@
 }
 
 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.
   if (descriptor_->options().packed()) {
@@ -398,22 +383,22 @@
       "  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 @@
 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 @@
 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 @@
       "$name$MemoizedSerializedSize = dataSize;\n");
   } else {
     printer->Print(variables_,
-        "size += $tag_size$ * $name$.length;\n");
+        "size += $tag_size$ * this.$name$.length;\n");
   }
 
   printer->Outdent();
diff --git a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h
index 7c14e4c..9b1bb46 100644
--- a/src/google/protobuf/compiler/javanano/javanano_primitive_field.h
+++ b/src/google/protobuf/compiler/javanano/javanano_primitive_field.h
@@ -51,7 +51,6 @@
 
   // 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 @@
 
   // 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;
diff --git a/src/google/protobuf/unittest_nano.proto b/src/google/protobuf/unittest_nano.proto
index c689ac0..62af6bc 100644
--- a/src/google/protobuf/unittest_nano.proto
+++ b/src/google/protobuf/unittest_nano.proto
@@ -152,6 +152,11 @@
   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 {