001: /*
002: * Javassist, a Java-bytecode translator toolkit.
003: * Copyright (C) 1999-2006 Shigeru Chiba. All Rights Reserved.
004: *
005: * The contents of this file are subject to the Mozilla Public License Version
006: * 1.1 (the "License"); you may not use this file except in compliance with
007: * the License. Alternatively, the contents of this file may be used under
008: * the terms of the GNU Lesser General Public License Version 2.1 or later.
009: *
010: * Software distributed under the License is distributed on an "AS IS" basis,
011: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
012: * for the specific language governing rights and limitations under the
013: * License.
014: */
015:
016: package javassist.compiler.ast;
017:
018: import javassist.compiler.TokenId;
019: import javassist.compiler.CompileError;
020:
021: /**
022: * Variable declarator.
023: */
024: public class Declarator extends ASTList implements TokenId {
025: protected int varType;
026: protected int arrayDim;
027: protected int localVar;
028: protected String qualifiedClass; // JVM-internal representation
029:
030: public Declarator(int type, int dim) {
031: super (null);
032: varType = type;
033: arrayDim = dim;
034: localVar = -1;
035: qualifiedClass = null;
036: }
037:
038: public Declarator(ASTList className, int dim) {
039: super (null);
040: varType = CLASS;
041: arrayDim = dim;
042: localVar = -1;
043: qualifiedClass = astToClassName(className, '/');
044: }
045:
046: /* For declaring a pre-defined? local variable.
047: */
048: public Declarator(int type, String jvmClassName, int dim, int var,
049: Symbol sym) {
050: super (null);
051: varType = type;
052: arrayDim = dim;
053: localVar = var;
054: qualifiedClass = jvmClassName;
055: setLeft(sym);
056: append(this , null); // initializer
057: }
058:
059: public Declarator make(Symbol sym, int dim, ASTree init) {
060: Declarator d = new Declarator(this .varType, this .arrayDim + dim);
061: d.qualifiedClass = this .qualifiedClass;
062: d.setLeft(sym);
063: append(d, init);
064: return d;
065: }
066:
067: /* Returns CLASS, BOOLEAN, BYTE, CHAR, SHORT, INT, LONG, FLOAT,
068: * or DOUBLE (or VOID)
069: */
070: public int getType() {
071: return varType;
072: }
073:
074: public int getArrayDim() {
075: return arrayDim;
076: }
077:
078: public void addArrayDim(int d) {
079: arrayDim += d;
080: }
081:
082: public String getClassName() {
083: return qualifiedClass;
084: }
085:
086: public void setClassName(String s) {
087: qualifiedClass = s;
088: }
089:
090: public Symbol getVariable() {
091: return (Symbol) getLeft();
092: }
093:
094: public void setVariable(Symbol sym) {
095: setLeft(sym);
096: }
097:
098: public ASTree getInitializer() {
099: ASTList t = tail();
100: if (t != null)
101: return t.head();
102: else
103: return null;
104: }
105:
106: public void setLocalVar(int n) {
107: localVar = n;
108: }
109:
110: public int getLocalVar() {
111: return localVar;
112: }
113:
114: public String getTag() {
115: return "decl";
116: }
117:
118: public void accept(Visitor v) throws CompileError {
119: v.atDeclarator(this );
120: }
121:
122: public static String astToClassName(ASTList name, char sep) {
123: if (name == null)
124: return null;
125:
126: StringBuffer sbuf = new StringBuffer();
127: astToClassName(sbuf, name, sep);
128: return sbuf.toString();
129: }
130:
131: private static void astToClassName(StringBuffer sbuf, ASTList name,
132: char sep) {
133: for (;;) {
134: ASTree h = name.head();
135: if (h instanceof Symbol)
136: sbuf.append(((Symbol) h).get());
137: else if (h instanceof ASTList)
138: astToClassName(sbuf, (ASTList) h, sep);
139:
140: name = name.tail();
141: if (name == null)
142: break;
143:
144: sbuf.append(sep);
145: }
146: }
147: }
|