KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > aspectj > compiler > base > ast > AssignExpr


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.ast;
26
27 import org.aspectj.compiler.base.AssignmentCheckerPass;
28 import org.aspectj.compiler.base.*;
29 import org.aspectj.compiler.crosscuts.AccessFixer;
30
31 import java.util.*;
32
33 import org.aspectj.compiler.base.bcg.CodeBuilder;
34 import org.aspectj.compiler.base.bcg.Label;
35
36 /**
37  *
38  * @grammar
39  * @property String op
40  * @child Expr rhs the new value
41  */

42 public abstract class AssignExpr extends BangExpr {
43
44     //INTRO from AccessFixer
45
public ASTObject fixAccessToFieldSet(FieldAccessExpr expr) {
46             return makeInPlaceSet(expr, getOp(), getRhs());
47     }
48
49     public void unparse(CodeWriter writer) {
50         writer.write(lhs);
51         writer.writeOp(op + "=");
52         writer.write(rhs);
53     }
54
55     public Expr makeReference() {
56         if (lhs instanceof VarExpr) return lhs.makeReference();
57         return super.makeReference();
58     }
59
60     // ------------------------------
61
// INTRO from FlowCheckerPass
62

63     // this is big and dumb, but careful. And overridden in BasicAssignExpr.
64
public void walkFlow(FlowCheckerPass w) {
65         w.process(getLhs());
66         w.process(getRhs());
67         if (getLhs() instanceof VarExpr) {
68             VarExpr lhs = (VarExpr)getLhs();
69             VarDec dec = lhs.getVarDec();
70             if (dec.isFinal()
71                 && ((! dec.isBlank())
72                     || (! w.getVars().isDefinitelyUnassigned(dec)))) {
73                 w.showVarError(this, dec, "Final variable " + dec.getId() +
74                                " already has a value");
75             }
76         } else if (getLhs() instanceof FieldAccessExpr) {
77             FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
78             FieldDec dec = lhs.getFieldDec();
79             if (dec.isFinal()
80                 && ((! dec.isBlank())
81                     || (! w.getVars().isDefinitelyUnassigned(dec)))) {
82                 w.showVarError(this, dec, "Final field " + dec.getId() +
83                                " already has a value");
84             }
85         }
86     }
87
88     // ------------------------------
89
// INTRO from AssignmentCheckerPass
90

91     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
92         // ???
93
Expr lhs = getLhs();
94         Type lhsType = lhs.getType();
95         Type rhsType = rhs.getType();
96
97         return this;
98     }
99
100     // ------------------------------
101
//INTRO from InnerAccessFixer
102

103     public ASTObject postInnerAccess(InnerAccessFixer w) {
104         if (! (getLhs() instanceof FieldAccessExpr)) return this;
105
106         FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
107         Expr q = lhs.getExpr();
108         FieldDec dec = lhs.getFieldDec();
109         if (w.isAccessible(dec, q)) return this;
110
111         final AST ast = getAST();
112         Type qType = q.getType();
113         Expr rhs = getRhs();
114         MethodDec newMethodDec = w.getAccessMethod(qType, dec, getOp(), this);
115         Exprs newArgs = ast.makeExprs(rhs);
116         Expr newExpr = w.makeOutsidePrimary(dec.isStatic(), newArgs, q);
117         return ast.makeCall(newMethodDec, newExpr, newArgs);
118     }
119
120     // move this down.
121
public MethodDec buildAccessMethod(InnerAccessFixer w) {
122         final AST ast = getAST();
123         FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
124         Expr q = lhs.getExpr();
125         Type qType = q.getType();
126         FieldDec dec = lhs.getFieldDec();
127         Type fieldType = dec.getType();
128
129         FormalDec valFormal = ast.makeFormal(fieldType, "val$");
130         Formals newFormals = ast.makeFormals(valFormal);
131         Expr newExpr = w.makeInsidePrimary(dec.isStatic(), newFormals, qType);
132
133         return
134             w.makeAccessMethod(fieldType,
135                                newFormals,
136                                ast.makeSet(ast.makeGet(newExpr, dec),
137                                            getOp(),
138                                            ast.makeVar(valFormal)));
139     }
140
141     // ------------------------------
142
// static factory
143

144     public static AssignExpr build(SourceLocation source,
145                                    AssignableExpr rand1, String JavaDoc op, Expr rand2) {
146         op = op.intern();
147         if (op == "") {
148             return new BasicAssignExpr(source, rand1, op, rand2);
149         } else if (op == "+") {
150             return new AddAssignExpr(source, rand1, op, rand2);
151         } else if (op == "*" || op == "/" || op == "%" || op == "-") {
152             return new NumericAssignExpr(source, rand1, op, rand2);
153         } else if (op == "&" || op == "|" || op == "^") {
154             return new BitwiseAssignExpr(source, rand1, op, rand2);
155         } else if (op == "<<" || op == ">>" || op == ">>>") {
156             return new ShiftAssignExpr(source, rand1, op, rand2);
157         } else {
158             throw new RuntimeException JavaDoc("bad op " + op);
159         }
160     }
161
162     //BEGIN: Generated from @child and @property
163
protected String JavaDoc op;
164     public String JavaDoc getOp() { return op; }
165     public void setOp(String JavaDoc _op) { op = _op; }
166
167     protected Expr rhs;
168     public Expr getRhs() { return rhs; }
169     public void setRhs(Expr _rhs) {
170         if (_rhs != null) _rhs.setParent(this);
171         rhs = _rhs;
172     }
173
174     public AssignExpr(SourceLocation location, AssignableExpr _lhs, String JavaDoc _op, Expr _rhs) {
175         super(location, _lhs);
176         setOp(_op);
177         setRhs(_rhs);
178     }
179     protected AssignExpr(SourceLocation source) {
180         super(source);
181     }
182
183     public ASTObject getChildAt(int childIndex) {
184         switch(childIndex) {
185         case 1: return rhs;
186         default: return super.getChildAt(childIndex);
187         }
188     }
189      public String JavaDoc getChildNameAt(int childIndex) {
190         switch(childIndex) {
191         case 1: return "rhs";
192         default: return super.getChildNameAt(childIndex);
193         }
194     }
195      public void setChildAt(int childIndex, ASTObject child) {
196         switch(childIndex) {
197         case 1: setRhs((Expr)child); return;
198         default: super.setChildAt(childIndex, child); return;
199         }
200     }
201      public int getChildCount() {
202         return 2;
203     }
204
205     public String JavaDoc getDefaultDisplayName() {
206         return "AssignExpr(op: "+op+")";
207     }
208
209     //END: Generated from @child and @property
210
}
211
Popular Tags