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 xtc.Limits;
022:
023: /**
024: * An integer type.
025: *
026: * @author Robert Grimm
027: * @version $Revision: 1.36 $
028: */
029: public class IntegerT extends NumberT {
030:
031: /**
032: * Create a new integer type.
033: *
034: * @param kind The kind.
035: * @throws IllegalArgumentException Signals an invalid kind.
036: */
037: public IntegerT(Kind kind) {
038: this (null, kind);
039: }
040:
041: /**
042: * Create a new integer type.
043: *
044: * @param template The type whose annotations to copy.
045: * @param kind The kind.
046: * @throws IllegalArgumentException Signals an invalid kind.
047: */
048: public IntegerT(Type template, Kind kind) {
049: super (template, kind);
050: switch (kind) {
051: case FLOAT:
052: case DOUBLE:
053: case LONG_DOUBLE:
054: case FLOAT_COMPLEX:
055: case DOUBLE_COMPLEX:
056: case LONG_DOUBLE_COMPLEX:
057: throw new IllegalArgumentException("Not an integer kind "
058: + kind);
059: default:
060: // All is well.
061: }
062: }
063:
064: public IntegerT copy() {
065: return new IntegerT(this , kind);
066: }
067:
068: public Type.Tag tag() {
069: return Type.Tag.INTEGER;
070: }
071:
072: public boolean isInteger() {
073: return true;
074: }
075:
076: public IntegerT toInteger() {
077: return this ;
078: }
079:
080: /**
081: * Convert the specified rank to the corresponding integer kind.
082: * The rank reflects the ordering <code>char</code>,
083: * <code>short</code>, <code>int</code>, <code>long</code>, and
084: * <code>long long</code>, starting at 1 and ignoring the sign.
085: *
086: * @param rank The rank.
087: * @param signed The flag for a signed integer.
088: * @return The corresponding kind.
089: * @throws IllegalArgumentException Signals an invalid rank.
090: */
091: public static Kind fromRank(int rank, boolean signed) {
092: switch (rank) {
093: case 1:
094: if (signed) {
095: return Limits.IS_CHAR_SIGNED ? Kind.CHAR : Kind.S_CHAR;
096: } else {
097: return Limits.IS_CHAR_SIGNED ? Kind.U_CHAR : Kind.CHAR;
098: }
099: case 2:
100: return signed ? Kind.SHORT : Kind.U_SHORT;
101: case 3:
102: return signed ? Kind.INT : Kind.U_INT;
103: case 4:
104: return signed ? Kind.LONG : Kind.U_LONG;
105: case 5:
106: return signed ? Kind.LONG_LONG : Kind.U_LONG_LONG;
107: default:
108: throw new IllegalArgumentException("Invalid rank: " + rank);
109: }
110: }
111:
112: }
|