001: /*
002: $Id: Variable.java 3715 2006-03-27 16:53:34Z blackdrag $
003:
004: Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
005:
006: Redistribution and use of this software and associated documentation
007: ("Software"), with or without modification, are permitted provided
008: that the following conditions are met:
009:
010: 1. Redistributions of source code must retain copyright
011: statements and notices. Redistributions must also contain a
012: copy of this document.
013:
014: 2. Redistributions in binary form must reproduce the
015: above copyright notice, this list of conditions and the
016: following disclaimer in the documentation and/or other
017: materials provided with the distribution.
018:
019: 3. The name "groovy" must not be used to endorse or promote
020: products derived from this Software without prior written
021: permission of The Codehaus. For written permission,
022: please contact info@codehaus.org.
023:
024: 4. Products derived from this Software may not be called "groovy"
025: nor may "groovy" appear in their names without prior written
026: permission of The Codehaus. "groovy" is a registered
027: trademark of The Codehaus.
028:
029: 5. Due credit should be given to The Codehaus -
030: http://groovy.codehaus.org/
031:
032: THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
033: ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
034: NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
035: FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
036: THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
037: INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
038: (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
039: SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
040: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
041: STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
042: ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
043: OF THE POSSIBILITY OF SUCH DAMAGE.
044:
045: */
046: package org.codehaus.groovy.classgen;
047:
048: import org.codehaus.groovy.ast.ClassHelper;
049: import org.codehaus.groovy.ast.ClassNode;
050: import org.objectweb.asm.Label;
051:
052: /**
053: * Represents compile time variable metadata while compiling a method.
054: *
055: * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
056: * @version $Revision: 3715 $
057: */
058: public class Variable {
059:
060: public static Variable THIS_VARIABLE = new Variable();
061: public static Variable SUPER_VARIABLE = new Variable();
062:
063: private int index;
064: private ClassNode type;
065: private String name;
066: private boolean holder;
067: private boolean property;
068:
069: // br for setting on the LocalVariableTable in the class file
070: // these fields should probably go to jvm Operand class
071: private Label startLabel = null;
072: private Label endLabel = null;
073: private boolean dynamicTyped;
074:
075: private Variable() {
076: dynamicTyped = true;
077: index = 0;
078: holder = false;
079: property = false;
080: }
081:
082: public Variable(int index, ClassNode type, String name) {
083: this .index = index;
084: this .type = type;
085: this .name = name;
086: }
087:
088: public String getName() {
089: return name;
090: }
091:
092: public ClassNode getType() {
093: return type;
094: }
095:
096: public String getTypeName() {
097: return type.getName();
098: }
099:
100: /**
101: * @return the stack index for this variable
102: */
103: public int getIndex() {
104: return index;
105: }
106:
107: /**
108: * @return is this local variable shared in other scopes (and so must use a ValueHolder)
109: */
110: public boolean isHolder() {
111: return holder;
112: }
113:
114: public void setHolder(boolean holder) {
115: this .holder = holder;
116: }
117:
118: public boolean isProperty() {
119: return property;
120: }
121:
122: public void setProperty(boolean property) {
123: this .property = property;
124: }
125:
126: public Label getStartLabel() {
127: return startLabel;
128: }
129:
130: public void setStartLabel(Label startLabel) {
131: this .startLabel = startLabel;
132: }
133:
134: public Label getEndLabel() {
135: return endLabel;
136: }
137:
138: public void setEndLabel(Label endLabel) {
139: this .endLabel = endLabel;
140: }
141:
142: public String toString() {
143: // TODO Auto-generated method stub
144: return super .toString() + "[" + type + " " + name + " ("
145: + index + ")";
146: }
147:
148: public void setType(ClassNode type) {
149: this .type = type;
150: dynamicTyped |= type == ClassHelper.DYNAMIC_TYPE;
151: }
152:
153: public void setDynamicTyped(boolean b) {
154: dynamicTyped = b;
155: }
156:
157: public boolean isDynamicTyped() {
158: return dynamicTyped;
159: }
160: }
|