KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ExprMaker


1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * This file is part of the compiler and core tools for the AspectJ(tm)
4  * programming language; see http://aspectj.org
5  *
6  * The contents of this file are subject to the Mozilla Public License
7  * Version 1.1 (the "License"); you may not use this file except in
8  * compliance with the License. You may obtain a copy of the License at
9  * either http://www.mozilla.org/MPL/ or http://aspectj.org/MPL/.
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is AspectJ.
17  *
18  * The Initial Developer of the Original Code is Xerox Corporation. Portions
19  * created by Xerox Corporation are Copyright (C) 1999-2002 Xerox Corporation.
20  * All Rights Reserved.
21  *
22  * Contributor(s):
23  */

24
25 package org.aspectj.compiler.base;
26
27 import org.aspectj.compiler.base.ast.*;
28
29
30 import java.util.*;
31
32 /*
33  * XXXThis completely ignores all issues of return stmts
34 */

35 public class ExprMaker extends MovingWalker {
36     public ExprMaker(JavaCompiler compiler) {
37         super(compiler);
38     }
39     
40     boolean mayReturn;
41     boolean mustReturn;
42     
43     public Map getInParams() { return inParams; }
44     public Map getOutParams() { return outParams; }
45     public boolean getNeedsThis() { return needsThis; }
46     
47     Map inParams = new HashMap();
48     Map outParams = new HashMap();
49     boolean needsThis = false;
50     
51     public Expr moveThisExpr(ThisExpr thisExpr, Type thisType) {
52         needsThis = true;
53         return super.moveThisExpr(thisExpr, thisType);
54     }
55     
56     VarDec lookupVarDec(VarDec oldDec) {
57         VarDec ret = (VarDec)remapNodes.get(oldDec);
58         if (ret != null) {
59             return ret;
60         } else {
61             FormalDec formal = getAST().makeFormal(oldDec.getType(), oldDec.getId()); //+"XXX");
62
if (oldDec.isFinal()) formal.getModifiers().setFinal(true);
63             inParams.put(oldDec, formal);
64             remapNodes.put(oldDec, formal);
65             return formal;
66         }
67     }
68
69         
70     public Expr moveVarExpr(VarExpr var) {
71         var.setVarDec(lookupVarDec(var.getVarDec()));
72         return var;
73     }
74     
75     public Expr makeExpr(ASTObject object, boolean allowVoid) {
76         final AST ast = getAST();
77         CodeDec inCode = object.getEnclosingCodeDec();
78         
79         BlockStmt block = ast.makeBlock(this.process(object));
80         
81         Formals formals = ast.makeFormals();
82         Exprs args = ast.makeExprs();
83         
84         // loop over inParams to create call args and formals together
85
for (Iterator i = inParams.entrySet().iterator(); i.hasNext(); ) {
86             Map.Entry entry = (Map.Entry)i.next();
87             VarDec varDec = (VarDec)entry.getKey();
88             varDec.getModifiers().setFinal(true); //??? assumes too much
89
args.add(ast.makeVar(varDec));
90             
91             FormalDec formalDec = (FormalDec)entry.getValue();
92             formals.add(formalDec);
93         }
94         
95         Modifiers mods = ast.makeModifiers(Modifiers.PRIVATE);
96         mods.setStatic(inCode.isStatic());
97         
98         Type returnType = inCode.getResultType();
99         
100         String JavaDoc id = getAST().makeGeneratedName(inCode.getId() + "$ajcPostAround" +
101                       inCode.getBytecodeTypeDec().getBody().size());
102         
103         MethodDec newMethod = ast.makeMethod(mods, returnType, id, formals, block);
104         //newMethod.setSynthetic();
105
inCode.getBytecodeTypeDec().getBody().add(newMethod);
106         
107         Expr thisExpr = ast.makePrimary(newMethod, inCode.getBytecodeTypeDec());
108         //if (!newMethod.isStatic()) thisExpr = ast.makeThis();
109

110         Expr ret = ast.makeCall(newMethod, thisExpr, args);
111         
112         if (!allowVoid && newMethod.getResultType().isVoid()) {
113             MethodDec voidWrapper = (MethodDec)CopyWalker.copy(newMethod);
114             voidWrapper.setId(newMethod.getId() + "$ajcVoidWrapper");
115             //voidWrapper.setSynthetic();
116
voidWrapper.setResultTypeD( getTypeManager().getObjectType().makeTypeD() );
117             newMethod.getBytecodeTypeDec().getBody().add(voidWrapper);
118             Expr internalCall = ast.makeCall(
119                 newMethod,
120                 ast.makePrimary(newMethod, inCode.getBytecodeTypeDec()),
121                 voidWrapper.getFormals().makeExprs());
122                 
123             Stmts stmts = ast.makeStmts(
124                 ast.makeStmt(internalCall), ast.makeReturn(ast.makeNull()));
125             voidWrapper.getBody().setStmts(stmts);
126             thisExpr = ast.makePrimary(voidWrapper, newMethod.getBytecodeTypeDec());
127             ret = ast.makeCall(voidWrapper, thisExpr, args);
128         }
129         
130         return ret;
131     }
132 }
133
134
Popular Tags