1 /* Soot - a J*va Optimization Framework
2 * Copyright (C) 2003 Ondrej Lhotak
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
18 */
19
20 /*
21 * Modified by the Sable Research Group and others 1997-1999.
22 * See the 'credits' file distributed with Soot for the complete list of
23 * contributors. (Soot is distributed at http://www.sable.mcgill.ca/soot)
24 */
25
26
27
28
29
30
31 package org.kit.furia.fragment.soot.representation;
32 import soot.*;
33
34 /** Provides static helper methods to indicate if parenthesization is
35 * required.
36 *
37 * If your sub-expression has strictly higher precedence than you,
38 * then no brackets are required: 2 + (4 * 5) = 2 + 4 * 5 is
39 * unambiguous, because * has precedence 800 and + has precedence 700.
40 *
41 * If your subexpression has lower precedence than you, then
42 * brackets are required; otherwise you will bind to your
43 * grandchild instead of the subexpression. 2 * (4 + 5) without
44 * brackets would mean (2 * 4) + 5.
45 *
46 * For a binary operation, if your left sub-expression has the same
47 * precedence as you, no brackets are needed, since binary operations
48 * are all left-associative. If your right sub-expression has the
49 * same precedence than you, then brackets are needed to reproduce the
50 * parse tree (otherwise, parsing will give e.g. (2 + 4) + 5 instead
51 * of the 2 + (4 + 5) that you had to start with.) This is OK for
52 * integer addition and subtraction, but not OK for floating point
53 * multiplication. To be safe, let's put the brackets on.
54 *
55 * For the high-precedence operations, I've assigned precedences of
56 * 950 to field reads and invoke expressions (.), as well as array reads ([]).
57 * I've assigned 850 to cast, newarray and newinvoke.
58 *
59 * The Dava DCmp?Expr precedences look fishy to me; I've assigned DLengthExpr
60 * a precedence of 950, because it looks like it should parse like a field
61 * read to me.
62 *
63 * Basically, the only time I can see that brackets should be required
64 * seems to occur when a cast or a newarray occurs as a subexpression of
65 * an invoke or field read; hence 850 and 950. -PL
66 */
67 public class PrecedenceTest
68 {
69 public static boolean needsBrackets( ValueBox subExprBox, Value expr ) {
70 Value sub = subExprBox.getValue();
71 if( !(sub instanceof Precedence) ) return false;
72 Precedence subP = (Precedence) sub;
73 Precedence exprP = (Precedence) expr;
74 return subP.getPrecedence() < exprP.getPrecedence();
75 }
76 public static boolean needsBracketsRight( ValueBox subExprBox, Value expr ) {
77 Value sub = subExprBox.getValue();
78 if( !(sub instanceof Precedence) ) return false;
79 Precedence subP = (Precedence) sub;
80 Precedence exprP = (Precedence) expr;
81 return subP.getPrecedence() <= exprP.getPrecedence();
82 }
83 }