Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Commit

Permalink
fix sgen if/else issue (#25562)
Browse files Browse the repository at this point in the history
* Port the if/else fix from SG to corefx. Fix another if/else issue when write member elements only when the total number of members is bigger than 1000

* Use goto instead of do while.

* Use do while.

* Add missing Indent++ when IsSequence=true.
  • Loading branch information
huanwu authored and shmao committed Jan 2, 2018
1 parent 72f99f6 commit dcdf47c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1057,7 +1057,7 @@ private void WriteDerivedTypes(StructMapping mapping, bool isTypedReturn, string
{
for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
{
Writer.Write("else if (");
Writer.Write("if (");
WriteQNameEqual("xsiType", derived.TypeName, derived.Namespace);
Writer.WriteLine(")");
Writer.Indent++;
Expand Down Expand Up @@ -1094,7 +1094,7 @@ private void WriteEnumAndArrayTypes()
if (m is EnumMapping)
{
EnumMapping mapping = (EnumMapping)m;
Writer.Write("else if (");
Writer.Write("if (");
WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace);
Writer.WriteLine(") {");
Writer.Indent++;
Expand All @@ -1117,7 +1117,7 @@ private void WriteEnumAndArrayTypes()
ArrayMapping mapping = (ArrayMapping)m;
if (mapping.TypeDesc.HasDefaultConstructor)
{
Writer.Write("else if (");
Writer.Write("if (");
WriteQNameEqual("xsiType", mapping.TypeName, mapping.Namespace);
Writer.WriteLine(") {");
Writer.Indent++;
Expand Down Expand Up @@ -1264,10 +1264,10 @@ private void WriteLiteralStructMethod(StructMapping structMapping)
Writer.Indent--;
}
Writer.WriteLine("}");
Writer.WriteLine("else {");
Writer.Indent++;
WriteDerivedTypes(structMapping, !useReflection && !structMapping.TypeDesc.IsRoot, typeName);
if (structMapping.TypeDesc.IsRoot) WriteEnumAndArrayTypes();
Writer.WriteLine("else");
Writer.Indent++;
if (structMapping.TypeDesc.IsRoot)
Writer.Write("return ReadTypedPrimitive((");
else
Expand All @@ -1277,6 +1277,9 @@ private void WriteLiteralStructMethod(StructMapping structMapping)
Writer.Indent--;
Writer.WriteLine("}");

if (structMapping.TypeDesc.IsRoot)
Writer.Indent--;
Writer.WriteLine("}");
if (structMapping.TypeDesc.IsNullable)
Writer.WriteLine("if (isNull) return null;");

Expand Down Expand Up @@ -2217,16 +2220,28 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e
ElementAccessor e = elements[j];
string ns = e.Form == XmlSchemaForm.Qualified ? e.Namespace : "";
if (!isSequence && e.Any && (e.Name == null || e.Name.Length == 0)) continue;
if (!firstElement || (!isSequence && count > 0))

if (!isSequence)
{
Writer.Write("else ");
if (firstElement && count == 0)
{
Writer.WriteLine("do {");
Writer.Indent++;
}
}
else if (isSequence)
else
{
Writer.Write("case ");
Writer.Write(cases.ToString(CultureInfo.InvariantCulture));
Writer.WriteLine(":");
Writer.Indent++;
if (!firstElement || (!isSequence && count > 0))
{
Writer.Write("else ");
}
else if (isSequence)
{
Writer.Write("case ");
Writer.Write(cases.ToString(CultureInfo.InvariantCulture));
Writer.WriteLine(":");
Writer.Indent++;
}
}
count++;
firstElement = false;
Expand Down Expand Up @@ -2316,6 +2331,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e
Writer.Write(member.ParamsReadSource);
Writer.WriteLine(" = true;");
}
if (!isSequence)
{
Writer.WriteLine("break;");
}
Writer.Indent--;
Writer.WriteLine("}");
}
Expand All @@ -2342,10 +2361,10 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e
if (count > 0)
{
if (isSequence)
{
Writer.WriteLine("default:");
else
Writer.WriteLine("else {");
Writer.Indent++;
Writer.Indent++;
}
}
WriteMemberElementsElse(anyElement, elementElseString);
if (count > 0)
Expand All @@ -2355,7 +2374,14 @@ private void WriteMemberElementsIf(Member[] members, Member anyElement, string e
Writer.WriteLine("break;");
}
Writer.Indent--;
Writer.WriteLine("}");
if (!isSequence)
{
Writer.WriteLine("} while (false);");
}
else
{
Writer.WriteLine("}");
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -745,7 +745,7 @@ private void WriteDerivedTypes(StructMapping mapping)
for (StructMapping derived = mapping.DerivedMappings; derived != null; derived = derived.NextDerivedMapping)
{
string fullTypeName = derived.TypeDesc.CSharpName;
Writer.Write("else if (");
Writer.Write("if (");
WriteTypeCompare("t", fullTypeName, derived.TypeDesc.UseReflection);
Writer.WriteLine(") {");
Writer.Indent++;
Expand Down Expand Up @@ -783,7 +783,7 @@ private void WriteEnumAndArrayTypes()
{
EnumMapping mapping = (EnumMapping)m;
string fullTypeName = mapping.TypeDesc.CSharpName;
Writer.Write("else if (");
Writer.Write("if (");
WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection);
Writer.WriteLine(") {");
Writer.Indent++;
Expand Down Expand Up @@ -815,7 +815,7 @@ private void WriteEnumAndArrayTypes()
ArrayMapping mapping = m as ArrayMapping;
if (mapping == null || m.IsSoap) continue;
string fullTypeName = mapping.TypeDesc.CSharpName;
Writer.Write("else if (");
Writer.Write("if (");
if (mapping.TypeDesc.IsArray)
WriteArrayTypeCompare("t", fullTypeName, mapping.TypeDesc.ArrayElementTypeDesc.CSharpName, mapping.TypeDesc.UseReflection);
else
Expand Down Expand Up @@ -885,12 +885,12 @@ private void WriteStructMethod(StructMapping mapping)
WriteTypeCompare("t", fullTypeName, mapping.TypeDesc.UseReflection);
Writer.WriteLine(") {");
Writer.WriteLine("}");
Writer.WriteLine("else {");
Writer.Indent++;
WriteDerivedTypes(mapping);
if (mapping.TypeDesc.IsRoot)
WriteEnumAndArrayTypes();
Writer.WriteLine("else {");

Writer.Indent++;
if (mapping.TypeDesc.IsRoot)
{
Writer.WriteLine("WriteTypedPrimitive(n, ns, o, true);");
Expand Down

0 comments on commit dcdf47c

Please sign in to comment.