KickJava   Java API By Example, From Geeks To Geeks.

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


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
35 /**
36  *
37  * @grammar lhs = rhs
38  */

39 public class BasicAssignExpr extends AssignExpr {
40
41     public Type discoverType() {
42         Type lhsType = lhs.getType();
43         Type rhsType = rhs.getType();
44         return lhsType;
45     }
46
47     // ------------------------------
48
// INTRO from AssignmentCheckerPass
49

50     public ASTObject postAssignmentCheck(AssignmentCheckerPass walker) {
51         Type lhsType = lhs.getType();
52         Type rhsType = rhs.getType();
53         if (! rhs.isAssignableTo(lhsType)) {
54             rhs.showTypeError(rhs.getType(), lhsType);
55         }
56         return this;
57     }
58
59     //INTRO from AccessFixer
60
public ASTObject fixAccessToFieldSet(FieldAccessExpr expr) {
61         return getAST().makeCall(expr.getField().getBackdoorSetterMethod(),
62                                  expr.getExpr(), getRhs());
63     }
64
65     // ------------------------------
66
// INTRO from FlowCheckerPass
67

68     // this is big and dumb, but careful.
69
public void walkFlow(FlowCheckerPass w) {
70         if (getLhs() instanceof VarExpr) {
71             w.process(getRhs());
72             VarExpr lhs = (VarExpr)getLhs();
73             VarDec dec = lhs.getVarDec();
74             if (dec.isFinal()) {
75                 if (! dec.isBlank()) {
76                     w.showVarError(this, dec, "Final variable " + dec.getId() +
77                                    " already has a value");
78                 } else if (! w.getVars().isDefinitelyUnassigned(dec)) {
79                     w.showVarError(this, dec, "Final variable " + dec.getId() +
80                                    " may already have a value");
81                 } else {
82                     w.addPossiblyAssigned(dec);
83                 }
84             }
85             w.setVars(w.getVars().addAssigned(dec));
86         } else if (getLhs() instanceof FieldAccessExpr) {
87             FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
88             FieldDec dec = lhs.getFieldDec();
89             if (lhs.isBare()) {
90                 w.process(getRhs());
91                 if (dec.isFinal()) {
92                     if (! dec.isBlank()) {
93                         w.showVarError(this, dec, "Final field " + dec.getId() +
94                                        " already has a value");
95                     } else if (! w.getVars().isDefinitelyUnassigned(dec)) {
96                         w.showVarError(this, dec, "Final field " + dec.getId() +
97                                        " may already have a value");
98                     } else {
99                         w.addPossiblyAssigned(dec);
100                         w.setVars(w.getVars().addAssigned(dec));
101                     }
102                 }
103             } else {
104                 w.process(lhs.getExpr());
105                 w.process(getRhs());
106                 if (dec.isFinal()
107                     && ((! dec.isBlank())
108                         || (! w.getVars().isDefinitelyUnassigned(dec)))) {
109                     w.showVarError(this, dec, "Final field " + dec.getId() +
110                                    " already has a value");
111                 }
112             }
113         } else {
114             w.process(getLhs());
115             w.process(getRhs());
116         }
117     }
118
119     // ------------------------------
120
// bcg
121
protected void cgValue(CodeBuilder cb) {
122         cgValueEffect(cb, true);
123     }
124     protected void cgEffect(CodeBuilder cb) {
125         cgValueEffect(cb, false);
126     }
127     private void cgValueEffect(CodeBuilder cb, boolean needsValue) {
128         AssignableExpr lhs = getLhs();
129         Expr rhs = getRhs();
130         lhs.cgLvalue(cb);
131         rhs.cgValue(cb, lhs.getType());
132         if (needsValue) lhs.cgDupRvalue(cb);
133         lhs.cgAssignment(cb);
134     }
135
136     //BEGIN: Generated from @child and @property
137

138     public BasicAssignExpr(SourceLocation location, AssignableExpr _lhs, String JavaDoc _op, Expr _rhs) {
139         super(location, _lhs, _op, _rhs);
140
141     }
142     protected BasicAssignExpr(SourceLocation source) {
143         super(source);
144     }
145
146     public ASTObject copyWalk(CopyWalker walker) {
147         BasicAssignExpr ret = new BasicAssignExpr(getSourceLocation());
148         ret.preCopy(walker, this);
149         if (lhs != null) ret.setLhs( (AssignableExpr)walker.process(lhs) );
150         ret.op = op;
151         if (rhs != null) ret.setRhs( (Expr)walker.process(rhs) );
152         return ret;
153     }
154
155
156     public String JavaDoc getDefaultDisplayName() {
157         return "BasicAssignExpr(op: "+op+")";
158     }
159
160     //END: Generated from @child and @property
161
}
162
Popular Tags