001 /* Copyright 2000, 2001, Compaq Computer Corporation */ 002 003 /* IF THIS IS A JAVA FILE, DO NOT EDIT IT! 004 005 Most Java files in this directory which are part of the Javafe AST 006 are automatically generated using the astgen comment (see 007 ESCTools/Javafe/astgen) from the input file 'hierarchy.h'. If you 008 wish to modify AST classes or introduce new ones, modify 009 'hierarchy.j.' 010 */ 011 012 package javafe.ast; 013 014 import javafe.util.Assert; 015 import javafe.util.Location; 016 import javafe.util.ErrorSet; 017 018 // Convention: unless otherwise noted, integer fields named "loc" refer 019 // to the location of the first character of the syntactic unit 020 021 022 /** 023 * Represents a Literal. 024 * 025 * <p> The tag of a LiteralExpr should be one of the *LIT (eg INTLIT) 026 * constants defined in TagConstants. The value fields is a 027 * Character/String/Long/Double/Boolean/null, as appropriate. 028 */ 029 030 public class LiteralExpr extends Expr 031 { 032 /*@ invariant ( 033 (tag==TagConstants.BOOLEANLIT) || 034 (tag==TagConstants.BYTELIT) || 035 (tag==TagConstants.SHORTLIT) || 036 (tag==TagConstants.INTLIT) || 037 (tag==TagConstants.LONGLIT) || 038 (tag==TagConstants.FLOATLIT) || 039 (tag==TagConstants.DOUBLELIT) || 040 (tag==TagConstants.STRINGLIT) || 041 (tag==TagConstants.NULLLIT) || 042 (tag==TagConstants.CHARLIT) 043 ); */ 044 public int tag; 045 046 047 /*@ invariant ( 048 ((tag==TagConstants.BOOLEANLIT) ==> (value instanceof Boolean)) && 049 ((tag==TagConstants.NULLLIT) ==> (value == null)) && 050 ((tag==TagConstants.BYTELIT) ==> (value instanceof Byte)) && 051 ((tag==TagConstants.SHORTLIT) ==> (value instanceof Short)) && 052 ((tag==TagConstants.INTLIT) ==> (value instanceof Integer)) && 053 ((tag==TagConstants.LONGLIT) ==> (value instanceof Long)) && 054 ((tag==TagConstants.FLOATLIT) ==> (value instanceof Float)) && 055 ((tag==TagConstants.DOUBLELIT) ==> (value instanceof Double)) && 056 ((tag==TagConstants.STRINGLIT) ==> (value instanceof String)) && 057 ((tag==TagConstants.CHARLIT) ==> (value instanceof Integer)) 058 ); */ 059 public Object value; 060 061 062 //@ invariant loc != javafe.util.Location.NULL; 063 public int loc; 064 065 public final int getTag() { return this.tag; } 066 067 private void postCheck() { 068 boolean goodtag = 069 (tag == TagConstants.BOOLEANLIT || tag == TagConstants.INTLIT 070 || tag == TagConstants.LONGLIT || tag == TagConstants.CHARLIT 071 || tag == TagConstants.FLOATLIT || tag == TagConstants.DOUBLELIT 072 || tag == TagConstants.BYTELIT || tag == TagConstants.SHORTLIT 073 || tag == TagConstants.STRINGLIT 074 || tag == TagConstants.NULLLIT); 075 Assert.notFalse(goodtag); 076 if (tag == TagConstants.BOOLEANLIT && value != null) 077 Assert.notFalse(value instanceof Boolean); 078 if (tag == TagConstants.BYTELIT && value != null) 079 Assert.notFalse(value instanceof Byte); 080 if (tag == TagConstants.SHORTLIT && value != null) 081 Assert.notFalse(value instanceof Short); 082 if (tag == TagConstants.INTLIT && value != null) 083 Assert.notFalse(value instanceof Integer); 084 if (tag == TagConstants.LONGLIT && value != null) 085 Assert.notFalse(value instanceof Long); 086 if (tag == TagConstants.CHARLIT && value != null) 087 Assert.notFalse(value instanceof Integer); 088 if (tag == TagConstants.FLOATLIT && value != null) 089 Assert.notFalse(value instanceof Float); 090 if (tag == TagConstants.DOUBLELIT && value != null) 091 Assert.notFalse(value instanceof Double); 092 if (tag == TagConstants.STRINGLIT && value != null) 093 Assert.notFalse(value instanceof String); 094 if (tag == TagConstants.NULLLIT) Assert.notFalse(value == null); 095 } 096 public int getStartLoc() { return loc; } 097 public int getEndLoc() { return loc; } 098 099 static public LiteralExpr cast(LiteralExpr lit, int t) { 100 if (!(lit.value instanceof Number)) return lit; 101 Number num = (Number)lit.value; 102 if (t == TagConstants.DOUBLELIT) { 103 return LiteralExpr.make(t,new Double(num.doubleValue()),lit.getStartLoc()); 104 } else if (t == TagConstants.FLOATLIT) { 105 return LiteralExpr.make(t,new Float(num.floatValue()),lit.getStartLoc()); 106 } else if (t == TagConstants.LONGLIT) { 107 return LiteralExpr.make(t,new Long(num.longValue()),lit.getStartLoc()); 108 } else if (t == TagConstants.INTLIT) { 109 return LiteralExpr.make(t,new Integer(num.intValue()),lit.getStartLoc()); 110 } else if (t == TagConstants.SHORTLIT) { 111 return LiteralExpr.make(t,new Short(num.shortValue()),lit.getStartLoc()); 112 } else if (t == TagConstants.BYTELIT) { 113 return LiteralExpr.make(t,new Integer(num.byteValue()),lit.getStartLoc()); 114 } else return lit; 115 // FIXME - what about casts to character values ??? 116 } 117 118 // Note: Java does not actually have byte and short literals. 119 // We include them here because we pre-compute literals that have been 120 // cast to other types, e.g. (short)0, as a literal of the target type 121 // FIXME - short and byte are included for completeness, but I'm not sure they actually ever get used that way 122 123 /*@ requires ( 124 (tag==TagConstants.BOOLEANLIT) || 125 (tag==TagConstants.INTLIT) || 126 (tag==TagConstants.LONGLIT) || 127 (tag==TagConstants.FLOATLIT) || 128 (tag==TagConstants.DOUBLELIT) || 129 (tag==TagConstants.BYTELIT) || 130 (tag==TagConstants.SHORTLIT) || 131 (tag==TagConstants.STRINGLIT) || 132 (tag==TagConstants.NULLLIT) || 133 (tag==TagConstants.CHARLIT) 134 ); */ 135 /*@ requires ( 136 ((tag==TagConstants.BOOLEANLIT) ==> (value instanceof Boolean)) && 137 ((tag==TagConstants.NULLLIT) ==> (value == null)) && 138 ((tag==TagConstants.BYTELIT) ==> (value instanceof Byte)) && 139 ((tag==TagConstants.SHORTLIT) ==> (value instanceof Short)) && 140 ((tag==TagConstants.INTLIT) ==> (value instanceof Integer)) && 141 ((tag==TagConstants.LONGLIT) ==> (value instanceof Long)) && 142 ((tag==TagConstants.FLOATLIT) ==> (value instanceof Float)) && 143 ((tag==TagConstants.DOUBLELIT) ==> (value instanceof Double)) && 144 ((tag==TagConstants.STRINGLIT) ==> (value instanceof String)) && 145 ((tag==TagConstants.CHARLIT) ==> (value instanceof Integer)) 146 ); */ 147 // 148 //@ requires loc != javafe.util.Location.NULL; 149 //@ ensures \result != null; 150 public static LiteralExpr make(int tag, Object value, int loc) { 151 //@ set I_will_establish_invariants_afterwards = true; 152 LiteralExpr result = new LiteralExpr(); 153 result.tag = tag; 154 result.value = value; 155 result.loc = loc; 156 return result; 157 } 158 159 160 // Generated boilerplate constructors: 161 162 /** 163 * Construct a raw LiteralExpr whose class invariant(s) have not 164 * yet been established. It is the caller's job to 165 * initialize the returned node's fields so that any 166 * class invariants hold. 167 */ 168 //@ requires I_will_establish_invariants_afterwards; 169 protected LiteralExpr() {} //@ nowarn Invariant,NonNullInit; 170 171 172 // Generated boilerplate methods: 173 174 public final int childCount() { 175 return 1; 176 } 177 178 public final Object childAt(int index) { 179 /*throws IndexOutOfBoundsException*/ 180 if (index < 0) 181 throw new IndexOutOfBoundsException("AST child index " + index); 182 int indexPre = index; 183 184 int sz; 185 186 if (index == 0) return this.value; 187 else index--; 188 189 throw new IndexOutOfBoundsException("AST child index " + indexPre); 190 } //@ nowarn Exception; 191 192 public final String toString() { 193 return "[LiteralExpr" 194 + " tag = " + this.tag 195 + " value = " + this.value 196 + " loc = " + this.loc 197 + "]"; 198 } 199 200 public final void accept(Visitor v) { v.visitLiteralExpr(this); } 201 202 public final Object accept(VisitorArgResult v, Object o) {return v.visitLiteralExpr(this, o); } 203 204 public void check() { 205 super.check(); 206 postCheck(); 207 } 208 209 }