@ -43,19 +43,14 @@ public class CaseBlock extends StructuredBlock {
boolean isDefault = false ;
boolean isDefault = false ;
/ * *
/ * *
* True , if this is the last case in a switch
* True , if the previous case falls through to this case
* /
* /
boolean isLastBlock ;
boolean isFallThrough = false ;
/ * *
/ * *
* All variables used somewhere inside this block .
* True , if this is the last case in a switch
* /
VariableSet allUsed ;
/ * *
* Do we want braces around the case block ( if a sub block
* declares a variable ) .
* /
* /
boolean wantBraces ;
boolean isLastBlock = false ;
public CaseBlock ( int value ) {
public CaseBlock ( int value ) {
this . value = value ;
this . value = value ;
@ -89,29 +84,40 @@ public class CaseBlock extends StructuredBlock {
return true ;
return true ;
}
}
public VariableSet propagateUsage ( ) {
/ * *
/ * We remember if this sub block uses some variables , to introduce
* Tells if we want braces around this case . We only need braces
* braces around this block in that case .
* if there is a declaration on the first level ( list of
* sequential blocks ) .
* /
* /
return ( allUsed = super . propagateUsage ( ) ) ;
protected boolean wantBraces ( ) {
StructuredBlock block = subBlock ;
if ( block = = null )
return false ;
for ( ; ; ) {
if ( ! block . declare . isEmpty ( ) ) {
/* A declaration; we need braces. */
return true ;
}
}
/ * *
if ( ! ( block instanceof SequentialBlock ) ) {
* Make the declarations , i . e . initialize the declare variable
/ * This was the last block on the first level .
* to correct values . This will declare every variable that
* If we get here , we need no braces .
* is marked as used , but not done .
* @param done The set of the already declare variables .
* /
* /
public void makeDeclaration ( VariableSet done ) {
return false ;
java . util . Enumeration enum = allUsed . elements ( ) ;
while ( enum . hasMoreElements ( ) ) {
jode . decompiler . LocalInfo li = ( jode . decompiler . LocalInfo ) enum . nextElement ( ) ;
if ( ! done . contains ( li ) ) {
wantBraces = true ;
break ;
}
}
StructuredBlock [ ] subBlocks = block . getSubBlocks ( ) ;
if ( subBlocks [ 0 ] instanceof InstructionBlock
& & ! subBlocks [ 0 ] . declare . isEmpty ( ) ) {
/ * An instruction block declares on the same level as
* the surrounding SequentialBlock .
* /
return true ;
}
/* continue with the second sub block. */
block = subBlocks [ 1 ] ;
}
}
super . makeDeclaration ( done ) ;
}
}
/ * *
/ * *
@ -127,19 +133,46 @@ public class CaseBlock extends StructuredBlock {
throws java . io . IOException
throws java . io . IOException
{
{
if ( isDefault ) {
if ( isDefault ) {
/ * If this is the default case and does nothing , we can
* skip this .
* We have to make sure , that nothing flows into the default
* block though . XXX remove if sure .
* /
if ( isLastBlock
if ( isLastBlock
& & subBlock instanceof EmptyBlock
& & subBlock instanceof EmptyBlock
& & subBlock . jump = = null )
& & subBlock . jump = = null )
return ;
return ;
if ( subBlock instanceof BreakBlock
& & ( ( BreakBlock ) subBlock ) . breaksBlock = = this ) {
/* make sure that the previous block is correctly breaked */
if ( isFallThrough ) {
writer . tab ( ) ;
subBlock . dumpSource ( writer ) ;
writer . untab ( ) ;
}
return ;
}
if ( isFallThrough ) {
writer . tab ( ) ;
writer . print ( "/* fall through */" ) ;
writer . untab ( ) ;
}
writer . print ( "default:" ) ;
writer . print ( "default:" ) ;
} else {
} else {
if ( isFallThrough ) {
writer . tab ( ) ;
writer . print ( "/* fall through */" ) ;
writer . untab ( ) ;
}
ConstOperator constOp = new ConstOperator
ConstOperator constOp = new ConstOperator
( ( ( SwitchBlock ) outer ) . getInstruction ( ) . getType ( ) ,
( ( ( SwitchBlock ) outer ) . getInstruction ( ) . getType ( ) ,
Integer . toString ( value ) ) ;
Integer . toString ( value ) ) ;
constOp . makeInitializer ( ) ;
constOp . makeInitializer ( ) ;
writer . print ( "case " + constOp . toString ( ) + ":" ) ;
writer . print ( "case " + constOp . toString ( ) + ":" ) ;
}
}
if ( wantBraces )
if ( subBlock ! = null ) {
boolean needBraces = wantBraces ( ) ;
if ( needBraces )
writer . openBrace ( ) ;
writer . openBrace ( ) ;
else
else
writer . println ( ) ;
writer . println ( ) ;
@ -148,8 +181,10 @@ public class CaseBlock extends StructuredBlock {
subBlock . dumpSource ( writer ) ;
subBlock . dumpSource ( writer ) ;
writer . untab ( ) ;
writer . untab ( ) ;
}
}
if ( want Braces )
if ( need Braces )
writer . closeBrace ( ) ;
writer . closeBrace ( ) ;
} else
writer . println ( ) ;
}
}
/ * *
/ * *