KickJava   Java API By Example, From Geeks To Geeks.

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


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.FlowCheckerPass;
28 import org.aspectj.compiler.base.JavaCompiler;
29 import org.aspectj.compiler.base.CodeWriter;
30 import java.io.IOException JavaDoc;
31
32
33 import java.util.*;
34
35 import org.aspectj.compiler.base.bcg.CodeBuilder;
36 import org.aspectj.compiler.base.bcg.Label;
37
38 /**
39  * @grammar expr[index]
40  * @child Expr expr the expression
41  * @child Expr index the index
42  */

43 public class ArrayExpr extends AssignableExpr {
44
45     protected Type discoverType() {
46         Type intType = getTypeManager().intType;
47         if (! index.isAssignableTo(intType)) {
48             index.showTypeError(index.getType(), intType);
49         }
50         Type exprType = expr.getType();
51
52         if (exprType.isAnyType()) return getTypeManager().anyType;
53
54         if (exprType instanceof ArrayType) {
55             return ((ArrayType)exprType).getComponentType();
56         } else {
57             expr.showError("array required, but " + exprType.getString() + " found");
58             return getTypeManager().anyType;
59         }
60     }
61
62     public void unparse(CodeWriter writer) throws IOException JavaDoc {
63         writer.write(expr);
64         writer.write('[');
65         writer.write(getIndex());
66         writer.write(']');
67     }
68
69     // ------------------------------
70
// bcg
71
protected void cgLvalue(CodeBuilder cb) {
72         getExpr().cgValue(cb);
73         getIndex().cgValue(cb);
74     }
75     protected void cgLtoRvalue(CodeBuilder cb) {
76         getType().emitAload(cb);
77     }
78     protected void cgAssignment(CodeBuilder cb) {
79         getType().emitAstore(cb);
80     }
81     protected void cgDupLvalue(CodeBuilder cb) {
82         // array, index --> array, index, array, index
83
cb.emitDUP2();
84     }
85     protected void cgDupRvalue(CodeBuilder cb) {
86         // array, index, val --> val, array, index, val
87
getType().emitDupX2(cb);
88     }
89     // end bcg
90

91     //BEGIN: Generated from @child and @property
92
protected Expr expr;
93     public Expr getExpr() { return expr; }
94     public void setExpr(Expr _expr) {
95         if (_expr != null) _expr.setParent(this);
96         expr = _expr;
97     }
98
99     protected Expr index;
100     public Expr getIndex() { return index; }
101     public void setIndex(Expr _index) {
102         if (_index != null) _index.setParent(this);
103         index = _index;
104     }
105
106     public ArrayExpr(SourceLocation location, Expr _expr, Expr _index) {
107         super(location);
108         setExpr(_expr);
109         setIndex(_index);
110     }
111     protected ArrayExpr(SourceLocation source) {
112         super(source);
113     }
114
115     public ASTObject copyWalk(CopyWalker walker) {
116         ArrayExpr ret = new ArrayExpr(getSourceLocation());
117         ret.preCopy(walker, this);
118         if (expr != null) ret.setExpr( (Expr)walker.process(expr) );
119         if (index != null) ret.setIndex( (Expr)walker.process(index) );
120         return ret;
121     }
122
123     public ASTObject getChildAt(int childIndex) {
124         switch(childIndex) {
125         case 0: return expr;
126         case 1: return index;
127         default: return super.getChildAt(childIndex);
128         }
129     }
130      public String JavaDoc getChildNameAt(int childIndex) {
131         switch(childIndex) {
132         case 0: return "expr";
133         case 1: return "index";
134         default: return super.getChildNameAt(childIndex);
135         }
136     }
137      public void setChildAt(int childIndex, ASTObject child) {
138         switch(childIndex) {
139         case 0: setExpr((Expr)child); return;
140         case 1: setIndex((Expr)child); return;
141         default: super.setChildAt(childIndex, child); return;
142         }
143     }
144      public int getChildCount() {
145         return 2;
146     }
147
148     public String JavaDoc getDefaultDisplayName() {
149         return "ArrayExpr()";
150     }
151
152     //END: Generated from @child and @property
153
}
154
Popular Tags