KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
28 import java.io.IOException JavaDoc;
29
30 import java.util.*;
31
32 import org.aspectj.compiler.base.bcg.CodeBuilder;
33 import org.aspectj.compiler.base.bcg.Label;
34
35 /**
36  * @grammar rand1 + rand2
37  */

38 public class AddOpExpr extends BinopExpr {
39
40     void checkAssignmentOperatorType() {
41         Type ty1 = getRand1().getType();
42         Type ty2 = getRand2().getType();
43
44         if (ty1.isVoid() || ty2.isVoid()) {
45             showOperatorTypeError("+", ty1, ty2);
46         } else if (ty1.isAnyType() || ty2.isAnyType() ||
47                    ty1.isString() || ty2.isString() ||
48                    (ty1.isNumeric() && ty2.isNumeric())) {
49         } else {
50             showOperatorTypeError("+", ty1, ty2);
51         }
52     }
53
54     protected LiteralExpr halfFold(Type type, LiteralExpr lit1, LiteralExpr lit2) {
55         return type.foldAddOp(lit1, lit2);
56     }
57
58     protected Type discoverType() {
59         Expr rand1 = getRand1();
60         Expr rand2 = getRand2();
61         Type ty1 = rand1.getType();
62         Type ty2 = rand2.getType();
63
64         if (ty1.isAnyType() || ty2.isAnyType()) {
65             return getTypeManager().anyType;
66         } else if (ty1.isVoid() || ty2.isVoid()) {
67             showOperatorTypeError("+", ty1, ty2);
68             return getTypeManager().anyType;
69         } else if (ty1.isString() || ty2.isString()) {
70             return getTypeManager().getStringType();
71         } else if (ty1.isNumeric() && ty2.isNumeric()) {
72             return getTypeManager().binaryNumericPromotion(ty1, ty2);
73         } else {
74             showOperatorTypeError("+", ty1, ty2);
75             return getTypeManager().anyType;
76         }
77     }
78
79     protected Type getLiftType() {
80         return getType();
81     }
82
83     // ------------------------------
84
// bcg
85
protected void cgValue(CodeBuilder cb) {
86         Type liftTy = getLiftType();
87         if (liftTy.isString()) {
88             NameType stringBufferType = getTypeManager().getStringBufferType();
89             cb.emitNEW(stringBufferType);
90             cb.emitDUP();
91             cb.emitINVOKESPECIAL(stringBufferType, "<init>", "()V", -1);
92             rand1.cgBuffer(cb);
93             rand2.cgBuffer(cb);
94             cb.emitINVOKEVIRTUAL(stringBufferType, "toString",
95                                  "()Ljava/lang/String;", 0);
96         } else {
97             rand1.cgValue(cb, liftTy);
98             rand2.cgValue(cb, liftTy);
99             liftTy.emitAdd(cb);
100         }
101     }
102     protected void cgBuffer(CodeBuilder cb) {
103         if (getType().isString()) {
104             rand1.cgBuffer(cb);
105             rand2.cgBuffer(cb);
106         } else {
107             super.cgBuffer(cb);
108         }
109     }
110     protected void cgOp(CodeBuilder cb, Type ty) {
111         ty.emitAdd(cb);
112     }
113
114     //BEGIN: Generated from @child and @property
115

116     public AddOpExpr(SourceLocation location, Expr _rand1, String JavaDoc _op, Expr _rand2) {
117         super(location, _rand1, _op, _rand2);
118
119     }
120     protected AddOpExpr(SourceLocation source) {
121         super(source);
122     }
123
124     public ASTObject copyWalk(CopyWalker walker) {
125         AddOpExpr ret = new AddOpExpr(getSourceLocation());
126         ret.preCopy(walker, this);
127         if (rand1 != null) ret.setRand1( (Expr)walker.process(rand1) );
128         ret.op = op;
129         if (rand2 != null) ret.setRand2( (Expr)walker.process(rand2) );
130         return ret;
131     }
132
133
134     public String JavaDoc getDefaultDisplayName() {
135         return "AddOpExpr(op: "+op+")";
136     }
137
138     //END: Generated from @child and @property
139
}
140
Popular Tags