KickJava   Java API By Example, From Geeks To Geeks.

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


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 org.aspectj.compiler.crosscuts.AccessFixer;
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  *
37  * @grammar
38  * @property String op
39  */

40 public class PrefixExpr extends BangExpr {
41
42     //INTRO from AccessFixer
43
public ASTObject fixAccessToFieldSet(FieldAccessExpr expr) {
44         return makeInPlaceSet(expr, op.substring(1), getAST().makeLiteral(1));
45     }
46
47     public void unparse(CodeWriter writer) {
48         writer.write(op);
49         writer.write(lhs);
50     }
51     
52      public void checkSpec() {
53         Type ty = getType();
54     if (ty.isVoid() ||
55         !(ty.isNumeric() || ty.isAnyType())) {
56             showOperatorTypeError(op, ty);
57         }
58     }
59
60
61     // ------------------------------
62
//INTRO from InnerAccessFixer
63

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