KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > javaToJimple > CastInsertionVisitor


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer 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 package soot.javaToJimple;
21
22 public class CastInsertionVisitor extends polyglot.visit.AscriptionVisitor {
23
24     public CastInsertionVisitor(polyglot.frontend.Job job, polyglot.types.TypeSystem ts, polyglot.ast.NodeFactory nf) {
25         super(job, ts, nf);
26     }
27
28     public polyglot.ast.Expr ascribe(polyglot.ast.Expr e, polyglot.types.Type toType) {
29
30         //System.out.println("expr: "+e);
31
//System.out.println("expr: "+e.getClass());
32
//System.out.println("to type: "+toType);
33
polyglot.types.Type fromType = e.type();
34         //System.out.println("from type: "+fromType);
35

36         if (toType == null){
37             return e;
38         }
39         if (toType.isVoid()) {
40             return e;
41         }
42         
43         polyglot.util.Position p = e.position();
44
45         if (toType.equals(fromType)){
46             return e;
47         }
48
49         /*
50          * double -> (int, long, float)
51          * float -> (int, long, double)
52          * long -> (int, double, float)
53          * int (byte, char, short, boolean) -> (byte, char, short, long,
54          * double, float)
55          * ie double to short goes through int etc.
56          */

57         if (toType.isPrimitive() && fromType.isPrimitive()) {
58
59             polyglot.ast.Expr newExpr;
60
61             //System.out.println("from type: "+fromType);
62
//System.out.println("to type: "+toType);
63
if (fromType.isFloat() || fromType.isLong() || fromType.isDouble()){
64                 if (toType.isFloat() || toType.isLong() || toType.isDouble() || toType.isInt()){
65                     newExpr = nf.Cast(p, nf.CanonicalTypeNode(p, toType), e).type(toType);
66                 }
67                 else {
68                     newExpr = nf.Cast(p, nf.CanonicalTypeNode(p, toType), nf.Cast(p, nf.CanonicalTypeNode(p, ts.Int()), e).type(ts.Int())).type(toType);
69                 }
70             }
71             else {
72                 newExpr = nf.Cast(p, nf.CanonicalTypeNode(p, toType), e).type(toType);
73             }
74             return newExpr;
75         }
76         
77         return e;
78         
79     }
80     
81     public polyglot.ast.Node leaveCall(polyglot.ast.Node old, polyglot.ast.Node n, polyglot.visit.NodeVisitor v) throws polyglot.types.SemanticException {
82     
83         n = super.leaveCall(old, n, v);
84
85         return n;
86     }
87 }
88
Popular Tags