KickJava   Java API By Example, From Geeks To Geeks.

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


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 x += y
39  */

40 public class AddAssignExpr extends AssignExpr {
41
42     public Type discoverType() {
43         Type ty1 = lhs.getType();
44         Type ty2 = rhs.getType();
45         if (! ((ty1.isString() && !ty2.isVoid())
46                || ((ty1 instanceof NumericType)
47                    && (ty2 instanceof NumericType)))) {
48             showOperatorTypeError(op, ty1, ty2);
49         }
50         return ty1;
51     }
52
53     // ------------------------------
54
// bcg
55
protected void cgValue(CodeBuilder cb) {
56         cgValueEffect(cb, true);
57     }
58     protected void cgEffect(CodeBuilder cb) {
59         cgValueEffect(cb, false);
60     }
61     private void cgValueEffect(CodeBuilder cb, boolean needsValue) {
62         AssignableExpr lhs = getLhs();
63         Expr rhs = getRhs();
64         Type lhsTy = lhs.getType();
65         if (lhsTy.isString()) {
66             NameType stringBufferType = getTypeManager().getStringBufferType();
67             lhs.cgLvalue(cb);
68             lhs.cgDupLvalue(cb);
69             lhs.cgLtoRvalue(cb);
70             cb.emitNEW(stringBufferType);
71             cb.emitDUP();
72             cb.emitINVOKESPECIAL(stringBufferType, "<init>", "()V", -1);
73             cb.emitSWAP();
74             cb.emitINVOKEVIRTUAL(stringBufferType, "append",
75                                  "(Ljava/lang/String;)Ljava/lang/StringBuffer;",
76                                  -1);
77             rhs.cgBuffer(cb);
78             cb.emitINVOKEVIRTUAL(stringBufferType, "toString", "()Ljava/lang/String;", 0);
79             if (needsValue) lhs.cgDupRvalue(cb);
80             lhs.cgAssignment(cb);
81         } else if ((rhs instanceof IntLiteralExpr)
82                    && lhsTy.hasFastIncOp(lhs, ((IntLiteralExpr) rhs).getIntValue())) {
83             lhsTy.emitFastIncOp(cb, lhs, ((IntLiteralExpr) rhs).getIntValue());
84             if (needsValue) lhs.cgValue(cb);
85         } else {
86             Type rhsTy = rhs.getType();
87             Type ty = getTypeManager().binaryNumericPromotion(lhsTy, rhsTy);
88             lhs.cgLvalue(cb);
89             lhs.cgDupLvalue(cb);
90             lhs.cgLtoRvalue(cb);
91             lhsTy.emitCast(cb, ty);
92             rhs.cgValue(cb, ty);
93             ty.emitAdd(cb);
94             ty.emitCast(cb, lhsTy);
95             if (needsValue) lhs.cgDupRvalue(cb);
96             lhs.cgAssignment(cb);
97         }
98     }
99
100     //BEGIN: Generated from @child and @property
101

102     public AddAssignExpr(SourceLocation location, AssignableExpr _lhs, String JavaDoc _op, Expr _rhs) {
103         super(location, _lhs, _op, _rhs);
104
105     }
106     protected AddAssignExpr(SourceLocation source) {
107         super(source);
108     }
109
110     public ASTObject copyWalk(CopyWalker walker) {
111         AddAssignExpr ret = new AddAssignExpr(getSourceLocation());
112         ret.preCopy(walker, this);
113         if (lhs != null) ret.setLhs( (AssignableExpr)walker.process(lhs) );
114         ret.op = op;
115         if (rhs != null) ret.setRhs( (Expr)walker.process(rhs) );
116         return ret;
117     }
118
119
120     public String JavaDoc getDefaultDisplayName() {
121         return "AddAssignExpr(op: "+op+")";
122     }
123
124     //END: Generated from @child and @property
125
}
126
Popular Tags