001 /*
002 $Id: Variable.java,v 1.9 2005/11/13 16:42:11 blackdrag Exp $
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 /**
054 * Represents compile time variable metadata while compiling a method.
055 *
056 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
057 * @version $Revision: 1.9 $
058 */
059 public class Variable {
060
061 private int index;
062 private ClassNode type;
063 private String name;
064 private boolean holder;
065 private boolean property;
066
067 // br for setting on the LocalVariableTable in the class file
068 // these fields should probably go to jvm Operand class
069 private Label startLabel = null;
070 private Label endLabel = null;
071 private boolean dynamicTyped;
072
073 public Variable(int index, ClassNode type, String name) {
074 this.index = index;
075 this.type = type;
076 this.name = name;
077 }
078
079 public String getName() {
080 return name;
081 }
082
083 public ClassNode getType() {
084 return type;
085 }
086
087 public String getTypeName() {
088 return type.getName();
089 }
090
091 /**
092 * @return the stack index for this variable
093 */
094 public int getIndex() {
095 return index;
096 }
097
098 /**
099 * @return is this local variable shared in other scopes (and so must use a ValueHolder)
100 */
101 public boolean isHolder() {
102 return holder;
103 }
104
105 public void setHolder(boolean holder) {
106 this.holder = holder;
107 }
108
109 public boolean isProperty() {
110 return property;
111 }
112
113 public void setProperty(boolean property) {
114 this.property = property;
115 }
116
117 public Label getStartLabel() {
118 return startLabel;
119 }
120
121 public void setStartLabel(Label startLabel) {
122 this.startLabel = startLabel;
123 }
124
125 public Label getEndLabel() {
126 return endLabel;
127 }
128
129 public void setEndLabel(Label endLabel) {
130 this.endLabel = endLabel;
131 }
132
133 public String toString() {
134 // TODO Auto-generated method stub
135 return super.toString() + "[" + type + " " + name + " (" + index + ")";
136 }
137
138 /**
139 * derive a new Variable from this if this is a primitive variable, or return this instance
140 * @return
141 */
142 public Variable deriveBoxedVersion() {
143 if (ClassHelper.isPrimitiveType(getType())) {
144 ClassNode t = ClassHelper.getWrapper(getType());
145 return new Variable(index, t, name);
146 } else {
147 return this;
148 }
149 }
150
151 public void setType(ClassNode type) {
152 this.type = type;
153 dynamicTyped |= type==ClassHelper.DYNAMIC_TYPE;
154 }
155
156 public void setDynamicTyped(boolean b) {
157 dynamicTyped = b;
158 }
159
160 public boolean isDynamicTyped() {
161 return dynamicTyped;
162 }
163 }