KickJava   Java API By Example, From Geeks To Geeks.

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


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
29 import java.util.*;
30
31 import org.aspectj.compiler.base.bcg.CodeBuilder;
32 import org.aspectj.compiler.base.bcg.Label;
33
34 /**
35  *
36  * @grammar
37  * @property String op
38  */

39 public class PostfixExpr extends BangExpr {
40
41     //INTRO from AccessFixer
42
public ASTObject fixAccessToFieldSet(FieldAccessExpr expr) {
43         final AST ast = getAST();
44         //XXX this assumes that (x+1)-1 == x
45
//XXX this is certainly not true for many floats
46
Expr newExpr = makeInPlaceSet(expr, op.substring(1), ast.makeLiteral(1));
47         if (isInExprStmt()) {
48             return newExpr;
49         }
50
51         return ast.makeParen(ast.makeBinop(op.substring(1), newExpr, ast.makeLiteral(-1)));
52     }
53
54     public void unparse(CodeWriter writer) {
55         writer.write(lhs);
56         writer.write(op);
57     }
58     
59      public void checkSpec() {
60         Type ty = getType();
61     if (ty.isVoid() ||
62         !(ty.isNumeric() || ty.isAnyType())) {
63             showOperatorTypeError(op, ty);
64         }
65     }
66     // ------------------------------
67
//INTRO from InnerAccessFixer
68

69     public ASTObject postInnerAccess(InnerAccessFixer w) {
70         if (! (getLhs() instanceof FieldAccessExpr)) return this;
71
72         FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
73         Expr q = lhs.getExpr();
74         FieldDec dec = lhs.getFieldDec();
75         if (w.isAccessible(dec, q)) return this;
76
77         final AST ast = getAST();
78         Type qType = q.getType();
79         MethodDec newMethodDec = w.getAccessMethod(qType, dec, "x" + getOp(), this);
80         Exprs newArgs = ast.makeExprs();
81         Expr newExpr = w.makeOutsidePrimary(dec.isStatic(), newArgs, q);
82         return ast.makeCall(newMethodDec, newExpr, newArgs);
83     }
84
85     public MethodDec buildAccessMethod(InnerAccessFixer w) {
86         final AST ast = getAST();
87         FieldAccessExpr lhs = (FieldAccessExpr) getLhs();
88         Expr q = lhs.getExpr();
89         Type qType = q.getType();
90         FieldDec dec = lhs.getFieldDec();
91         Type fieldType = dec.getType();
92
93         Formals newFormals = ast.makeFormals();
94         Expr newExpr = w.makeInsidePrimary(dec.isStatic(), newFormals, qType);
95
96         return
97             w.makeAccessMethod(fieldType,
98                                newFormals,
99                                ast.makePostfix(newExpr, dec, getOp()));
100     }
101
102     // ------------------------------
103
// bcg
104
public void cgValue(CodeBuilder cb) {
105         cgValueEffect(cb, true);
106     }
107     public void cgEffect(CodeBuilder cb) {
108         cgValueEffect(cb, false);
109     }
110     private void cgValueEffect(CodeBuilder cb, boolean needsValue) {
111         Type ty = getType();
112         AssignableExpr lhs = getLhs();
113
114         if (ty.hasFastIncOp(lhs, 1)) {
115             if (needsValue) lhs.cgValue(cb);
116             ty.emitFastIncOp(cb, lhs, op.equals("++") ? 1 : -1);
117         } else {
118             lhs.cgLvalue(cb);
119             lhs.cgDupLvalue(cb);
120             lhs.cgLtoRvalue(cb);
121             if (needsValue) lhs.cgDupRvalue(cb);
122             ty.emitOne(cb);
123             if (op.equals("++")) ty.emitAdd(cb);
124             else ty.emitNumericOp(cb, "-");
125             if (ty instanceof IntishType) {
126                 ((IntishType)ty).emitCastFromInt(cb);
127             }
128             lhs.cgAssignment(cb);
129         }
130     }
131
132     //BEGIN: Generated from @child and @property
133
protected String JavaDoc op;
134     public String JavaDoc getOp() { return op; }
135     public void setOp(String JavaDoc _op) { op = _op; }
136
137     public PostfixExpr(SourceLocation location, AssignableExpr _lhs, String JavaDoc _op) {
138         super(location, _lhs);
139         setOp(_op);
140     }
141     protected PostfixExpr(SourceLocation source) {
142         super(source);
143     }
144
145     public ASTObject copyWalk(CopyWalker walker) {
146         PostfixExpr ret = new PostfixExpr(getSourceLocation());
147         ret.preCopy(walker, this);
148         if (lhs != null) ret.setLhs( (AssignableExpr)walker.process(lhs) );
149         ret.op = op;
150         return ret;
151     }
152
153
154     public String JavaDoc getDefaultDisplayName() {
155         return "PostfixExpr(op: "+op+")";
156     }
157
158     //END: Generated from @child and @property
159
}
160
Popular Tags