001: /*
002: * xtc - The eXTensible Compiler
003: * Copyright (C) 2005-2007 Robert Grimm
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * version 2 as published by the Free Software Foundation.
008: *
009: * This program is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with this program; if not, write to the Free Software
016: * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
017: * USA.
018: */
019: package xtc.type;
020:
021: import java.io.IOException;
022:
023: import java.math.BigInteger;
024:
025: /**
026: * An enumerator. An enumerator's type can be set after creation to
027: * support the deduction of the overall enum's type after all
028: * enumerators have been seen (C99 6.7.2.2).
029: *
030: * @author Robert Grimm
031: * @version $Revision: 1.38 $
032: */
033: public class EnumeratorT extends WrappedT {
034:
035: /** The name. */
036: private String name;
037:
038: /**
039: * Create a new, incomplete enumerator. The enumerator's type is
040: * the {@link ErrorT#TYPE error type}.
041: *
042: * @param name The name.
043: * @param value The value.
044: */
045: public EnumeratorT(String name, BigInteger value) {
046: this (ErrorT.TYPE, name, value);
047: }
048:
049: /**
050: * Create a new enumerator. The specified type should be an {@link
051: * IntegerT integer} or {@link ErrorT error} type.
052: *
053: * @param type The type.
054: * @param name The name.
055: * @param value The value.
056: */
057: public EnumeratorT(Type type, String name, BigInteger value) {
058: super (type);
059: this .name = name;
060: constant(value);
061: }
062:
063: /**
064: * Create a new enumerator.
065: *
066: * @param template The type whose annotations to copy.
067: * @param type The type.
068: * @param name The name.
069: * @param value The value.
070: */
071: public EnumeratorT(Type template, Type type, String name,
072: BigInteger value) {
073: super (template, type);
074: this .name = name;
075: constant(value);
076: }
077:
078: public EnumeratorT copy() {
079: return new EnumeratorT(this , getType().copy(), name, constant
080: .bigIntValue());
081: }
082:
083: public Type.Tag wtag() {
084: return Type.Tag.ENUMERATOR;
085: }
086:
087: public boolean isEnumerator() {
088: return true;
089: }
090:
091: public boolean hasEnumerator() {
092: return true;
093: }
094:
095: public EnumeratorT toEnumerator() {
096: return this ;
097: }
098:
099: /**
100: * Determine whether this enumerator has the specified name.
101: *
102: * @param name The name.
103: * @return <code>true</code> if this enumerator has the name.
104: */
105: public boolean hasName(String name) {
106: return name.equals(this .name);
107: }
108:
109: /**
110: * Get the name.
111: *
112: * @return The name.
113: */
114: public String getName() {
115: return name;
116: }
117:
118: /**
119: * Get the value.
120: *
121: * @return The value.
122: */
123: public BigInteger getValue() {
124: return constant.bigIntValue();
125: }
126:
127: public void write(Appendable out) throws IOException {
128: out.append(name);
129: }
130:
131: }
|