From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 13103 invoked by alias); 10 Dec 2007 22:00:56 -0000 Received: (qmail 13063 invoked by uid 22791); 10 Dec 2007 22:00:39 -0000 X-Spam-Status: No, hits=-2.4 required=5.0 tests=AWL,BAYES_00,DK_POLICY_SIGNSOME,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: sourceware.org Received: from mx1.redhat.com (HELO mx1.redhat.com) (66.187.233.31) by sourceware.org (qpsmtpd/0.31) with ESMTP; Mon, 10 Dec 2007 22:00:30 +0000 Received: from int-mx1.corp.redhat.com (int-mx1.corp.redhat.com [172.16.52.254]) by mx1.redhat.com (8.13.8/8.13.1) with ESMTP id lBAM0SFp023746 for ; Mon, 10 Dec 2007 17:00:28 -0500 Received: from pobox.corp.redhat.com (pobox.corp.redhat.com [10.11.255.20]) by int-mx1.corp.redhat.com (8.13.1/8.13.1) with ESMTP id lBAM0SlK009494 for ; Mon, 10 Dec 2007 17:00:28 -0500 Received: from [172.16.57.153] (multics.rdu.redhat.com [172.16.57.153]) by pobox.corp.redhat.com (8.13.1/8.13.1) with ESMTP id lBAM0Se2003467 for ; Mon, 10 Dec 2007 17:00:28 -0500 Subject: Re: Use StringBuilder instead of PrintWriter for type display From: Stan Cox To: Frysk List In-Reply-To: <4759EB1B.7040007@redhat.com> References: <1197063360.10805.165.camel@multics.rdu.redhat.com> <4759D33D.5050206@redhat.com> <4759EB1B.7040007@redhat.com> Content-Type: text/plain Date: Mon, 10 Dec 2007 22:00:00 -0000 Message-Id: <1197323542.10805.196.camel@multics.rdu.redhat.com> Mime-Version: 1.0 X-Mailer: Evolution 2.10.3 (2.10.3-4.fc7) Content-Transfer-Encoding: 7bit X-IsSubscribed: yes Mailing-List: contact frysk-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Post: List-Help: , Sender: frysk-owner@sourceware.org X-SW-Source: 2007-q4/txt/msg00215.txt.bz2 > wouldn't passing in the middle part Here is a variation that works top down, middle out instead of bottom up left to right. With the middle out approach, in some situations it is necessary to go from the middle to the beginning and end of the token, instead of the beginning and end of the entire declaration. For example: int (*ptr_to_func) (const int), where the const applies to a parameter and not to the return value. So in those cases a temporary string buffer is used. (This does not currently use a specialized class.) The diffs are comparing to the bottom up approach. (Both patch sets pass TestTypeEntry) ##### Similar to bottom up approach --- /home/scox/accu/save/ArrayType.java 2007-12-07 18:15:43.000000000 -0500 +++ frysk/value/ArrayType.java 2007-12-10 11:18:30.000000000 -0500 @@ -276,9 +276,11 @@ public class ArrayType } public void toPrint(StringBuilder stringBuilder, int indent) { - type.toPrint(stringBuilder, indent); - stringBuilder.append(" "); - stringBuilder.append("%s"); + StringBuilder tmpStringBuilder = new StringBuilder(); + type.toPrint(tmpStringBuilder, indent); + if (indent == 0) + tmpStringBuilder.append(" "); + stringBuilder.insert(0, tmpStringBuilder); for(int i = 0; i < this.dimension.length; i++) { stringBuilder.append("["); stringBuilder.append(dimension[i]); ##### Similar to bottom up approach but uses temporary memberStringBuilder for struct members --- /home/scox/accu/save/CompositeType.java 2007-12-09 20:37:17.000000000 -0500 +++ frysk/value/CompositeType.java 2007-12-10 16:06:37.000000000 -0500 @@ -275,7 +275,7 @@ public abstract class CompositeType writer.print("}"); } - public void toPrint(StringBuilder stringBuilder, int indentation) { + public void toPrint(StringBuilder stringBuilderParm, int indentation) { if (indentation == 0) indentation = 2; String indentPrefix = ""; @@ -283,6 +283,7 @@ public abstract class CompositeType indentPrefix = indentPrefix + " "; // {class,union,struct} NAME + StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append(getPrefix()); if (getName() != null && getName().length() > 0) { stringBuilder.append(" "); @@ -314,6 +315,7 @@ public abstract class CompositeType Access previousAccess = null; stringBuilder.append(" {\n"); + StringBuilder memberStringBuilder = new StringBuilder(); while (member != null) { if (member.access != previousAccess) { previousAccess = member.access; @@ -323,13 +325,11 @@ public abstract class CompositeType stringBuilder.append(":\n"); } } - stringBuilder.append(indentPrefix); - member.type.toPrint (stringBuilder, indentation + 2); - if (! toPrintFormat(stringBuilder, "%s", member.name)) { - if (stringBuilder.charAt(stringBuilder.length() - 1) != '*') - stringBuilder.append(" "); - stringBuilder.append(member.name); - } + memberStringBuilder.delete(0, memberStringBuilder.length()); + memberStringBuilder.append(" " + member.name); + member.type.toPrint(memberStringBuilder, indentation + 2); + memberStringBuilder.insert(0, indentPrefix); + stringBuilder.append(memberStringBuilder); if (member.bitSize > 0) { stringBuilder.append(":"); stringBuilder.append(member.bitSize); @@ -344,6 +344,7 @@ public abstract class CompositeType for (int indent = 1; indent <= indentation - 2; indent++) stringBuilder.append(" "); stringBuilder.append("}"); + stringBuilderParm.insert(0, stringBuilder); } public Value member(Value var1, String member) ##### Similar to bottom up approach but uses tmpStringBuilder for parameters + StringBuilder tmpStringBuilder = new StringBuilder(); + if (stringBuilder.charAt(0) == ' ') + stringBuilder.deleteCharAt(0); if (returnType == null) - stringBuilder.append("void"); + tmpStringBuilder.insert(0, "void"); else - returnType.toPrint(stringBuilder, 0); - stringBuilder.append(" "); - stringBuilder.append("%s"); + returnType.toPrint(tmpStringBuilder, 0); + tmpStringBuilder.append(" "); + stringBuilder.insert(0, tmpStringBuilder); stringBuilder.append(" ("); for (int i = 0; i < this.parmTypes.size(); i++) { - ((Type)this.parmTypes.get(i)).toPrint(stringBuilder, 0); - stringBuilder.append((String)this.parmNames.get(i)); + tmpStringBuilder.delete(0, tmpStringBuilder.length()); + ((Type)this.parmTypes.get(i)).toPrint(tmpStringBuilder, 0); + tmpStringBuilder.append((String)this.parmNames.get(i)); + stringBuilder.append(tmpStringBuilder); if (i < this.parmTypes.size() - 1) // Not last arg stringBuilder.append(","); } stringBuilder.append(")"); } ##### Similar to bottom up but needs to specialize the (*x) cases a bit more --- /home/scox/accu/save/PointerType.java 2007-12-09 20:36:51.000000000 -0500 +++ frysk/value/PointerType.java 2007-12-10 15:03:34.000000000 -0500 @@ -118,21 +118,25 @@ public class PointerType extends Integer public void toPrint(StringBuilder stringBuilder, int indent) { // For handling int (*x)[2] - if (type instanceof ArrayType) { - ((ArrayType)type).toPrint(stringBuilder, indent); - toPrintFormat(stringBuilder, "%s", "(*%s)"); - } - else if (type instanceof FunctionType) { - ((FunctionType)type).toPrint(stringBuilder, indent); - toPrintFormat(stringBuilder, "%s", "(*%s)"); + if (type instanceof ArrayType || type instanceof FunctionType) { + if (indent > 0 && stringBuilder.length() > 0 + && stringBuilder.charAt(0) == ' ') { + stringBuilder.deleteCharAt(0); + stringBuilder.insert(0, " (*"); + } + else + stringBuilder.insert(0, "(*"); + stringBuilder.append(")"); + type.toPrint(stringBuilder, indent); } else { + if (indent > 0 && stringBuilder.length() > 0 + && stringBuilder.charAt(0) == ' ') + stringBuilder.deleteCharAt(0); + stringBuilder.insert(0, "*"); + if (! (type instanceof PointerType)) + stringBuilder.insert(0, " "); type.toPrint(stringBuilder, indent); - if (! toPrintFormat(stringBuilder, "*%s", "**%s")) { - if (stringBuilder.charAt(stringBuilder.length() - 1) != '*') - stringBuilder.append(" "); - stringBuilder.append("*"); - } } ##### The other files are similar to bottom up approach but use insert instead of append, e.g. --- /home/scox/accu/save/ArithmeticType.java 2007-12-06 14:20:13.000000000 -0500 +++ frysk/value/ArithmeticType.java 2007-12-10 09:42:16.000000000 -0500 @@ -70,7 +70,7 @@ public abstract class ArithmeticType } public void toPrint(StringBuilder stringBuilder, int indent) { - stringBuilder.append(getName()); + stringBuilder.insert(0, getName()); }