001    /* This file was generated with JastAdd2 (http://jastadd.org) version 2.1.13-12-g880e696 */
002    package org.extendj.ast;
003    
004    import java.util.HashSet;
005    import java.io.File;
006    import java.util.Set;
007    import java.util.Collections;
008    import java.util.Collection;
009    import java.util.ArrayList;
010    import beaver.*;
011    import java.util.*;
012    import java.io.ByteArrayOutputStream;
013    import java.io.PrintStream;
014    import java.lang.reflect.InvocationTargetException;
015    import java.lang.reflect.Method;
016    import org.jastadd.util.*;
017    import java.util.zip.*;
018    import java.io.*;
019    import org.jastadd.util.PrettyPrintable;
020    import org.jastadd.util.PrettyPrinter;
021    import java.io.FileNotFoundException;
022    import java.io.BufferedInputStream;
023    import java.io.DataInputStream;
024    /**
025     * Default Java integer literal. Should only be used for numbers
026     * that can be stored in 32 bits binary.
027     * @ast node
028     * @declaredat /home/jesper/git/extendj/java7/grammar/Literals.ast:48
029     * @production IntegerLiteral : {@link NumericLiteral};
030    
031     */
032    public class IntegerLiteral extends NumericLiteral implements Cloneable {
033      /**
034       * @aspect NodeConstructors
035       * @declaredat /home/jesper/git/extendj/java4/frontend/NodeConstructors.jrag:59
036       */
037      public IntegerLiteral(int i) {
038        this(Integer.toString(i));
039      }
040      /*************************************************************
041       * Emit methods
042       * ***********************************************************
043       * @aspect CodeGeneration
044       * @declaredat /home/jesper/git/extendj/java4/backend/CodeGeneration.jrag:82
045       */
046      public static void push(CodeGeneration gen, int value) {
047        switch(value) {
048          case -1:
049            gen.emit(Bytecode.ICONST_M1);
050            break;
051          case 0:
052            gen.emit(Bytecode.ICONST_0);
053            break;
054          case 1:
055            gen.emit(Bytecode.ICONST_1);
056            break;
057          case 2:
058            gen.emit(Bytecode.ICONST_2);
059            break;
060          case 3:
061            gen.emit(Bytecode.ICONST_3);
062            break;
063          case 4:
064            gen.emit(Bytecode.ICONST_4);
065            break;
066          case 5:
067            gen.emit(Bytecode.ICONST_5);
068            break;
069          default:
070            if (value >= -128 && value <= 127) {
071              gen.emit(Bytecode.BIPUSH).add(value);
072            } else if (value >= -32768 && value <= 32767) {
073              gen.emit(Bytecode.SIPUSH).add2(value);
074            } else {
075              int index = gen.constantPool().addConstant(value);
076              if (index < 256) {
077                gen.emit(Bytecode.LDC).add(index);
078              } else {
079                gen.emit(Bytecode.LDC_W).add2(index);
080              }
081            }
082        }
083      }
084      /**
085       * @aspect CodeGeneration
086       * @declaredat /home/jesper/git/extendj/java4/backend/CodeGeneration.jrag:181
087       */
088      public void emitPushConstant(CodeGeneration gen) {
089        type().emitPushConstant(gen, constant().intValue());
090      }
091      /**
092       * @declaredat ASTNode:1
093       */
094      public IntegerLiteral() {
095        super();
096      }
097      /**
098       * Initializes the child array to the correct size.
099       * Initializes List and Opt nta children.
100       * @apilevel internal
101       * @ast method
102       * @declaredat ASTNode:10
103       */
104      public void init$Children() {
105      }
106      /**
107       * @declaredat ASTNode:12
108       */
109      public IntegerLiteral(String p0) {
110        setLITERAL(p0);
111      }
112      /**
113       * @declaredat ASTNode:15
114       */
115      public IntegerLiteral(beaver.Symbol p0) {
116        setLITERAL(p0);
117      }
118      /**
119       * @apilevel low-level
120       * @declaredat ASTNode:21
121       */
122      protected int numChildren() {
123        return 0;
124      }
125      /**
126       * @apilevel internal
127       * @declaredat ASTNode:27
128       */
129      public boolean mayHaveRewrite() {
130        return true;
131      }
132      /**
133       * @apilevel internal
134       * @declaredat ASTNode:33
135       */
136      public void flushAttrCache() {
137        super.flushAttrCache();
138        type_reset();
139        constant_reset();
140      }
141      /**
142       * @apilevel internal
143       * @declaredat ASTNode:41
144       */
145      public void flushCollectionCache() {
146        super.flushCollectionCache();
147      }
148      /**
149       * @apilevel internal
150       * @declaredat ASTNode:47
151       */
152      public void flushRewriteCache() {
153        super.flushRewriteCache();
154      }
155      /**
156       * @apilevel internal
157       * @declaredat ASTNode:53
158       */
159      public IntegerLiteral clone() throws CloneNotSupportedException {
160        IntegerLiteral node = (IntegerLiteral) super.clone();
161        return node;
162      }
163      /**
164       * @apilevel internal
165       * @declaredat ASTNode:60
166       */
167      public IntegerLiteral copy() {
168        try {
169          IntegerLiteral node = (IntegerLiteral) clone();
170          node.parent = null;
171          if (children != null) {
172            node.children = (ASTNode[]) children.clone();
173          }
174          return node;
175        } catch (CloneNotSupportedException e) {
176          throw new Error("Error: clone not supported for " + getClass().getName());
177        }
178      }
179      /**
180       * Create a deep copy of the AST subtree at this node.
181       * The copy is dangling, i.e. has no parent.
182       * @return dangling copy of the subtree at this node
183       * @apilevel low-level
184       * @deprecated Please use treeCopy or treeCopyNoTransform instead
185       * @declaredat ASTNode:79
186       */
187      @Deprecated
188      public IntegerLiteral fullCopy() {
189        return treeCopyNoTransform();
190      }
191      /**
192       * Create a deep copy of the AST subtree at this node.
193       * The copy is dangling, i.e. has no parent.
194       * @return dangling copy of the subtree at this node
195       * @apilevel low-level
196       * @declaredat ASTNode:89
197       */
198      public IntegerLiteral treeCopyNoTransform() {
199        IntegerLiteral tree = (IntegerLiteral) copy();
200        if (children != null) {
201          for (int i = 0; i < children.length; ++i) {
202            ASTNode child = (ASTNode) children[i];
203            if (child != null) {
204              child = child.treeCopyNoTransform();
205              tree.setChild(child, i);
206            }
207          }
208        }
209        return tree;
210      }
211      /**
212       * Create a deep copy of the AST subtree at this node.
213       * The subtree of this node is traversed to trigger rewrites before copy.
214       * The copy is dangling, i.e. has no parent.
215       * @return dangling copy of the subtree at this node
216       * @apilevel low-level
217       * @declaredat ASTNode:109
218       */
219      public IntegerLiteral treeCopy() {
220        doFullTraversal();
221        return treeCopyNoTransform();
222      }
223      /**
224       * @apilevel internal
225       * @declaredat ASTNode:116
226       */
227      protected boolean is$Equal(ASTNode node) {
228        return super.is$Equal(node) && (tokenString_LITERAL == ((IntegerLiteral)node).tokenString_LITERAL);    
229      }
230      /**
231       * Replaces the lexeme LITERAL.
232       * @param value The new value for the lexeme LITERAL.
233       * @apilevel high-level
234       */
235      public void setLITERAL(String value) {
236        tokenString_LITERAL = value;
237      }
238      /**
239       * JastAdd-internal setter for lexeme LITERAL using the Beaver parser.
240       * @param symbol Symbol containing the new value for the lexeme LITERAL
241       * @apilevel internal
242       */
243      public void setLITERAL(beaver.Symbol symbol) {
244        if (symbol.value != null && !(symbol.value instanceof String))
245        throw new UnsupportedOperationException("setLITERAL is only valid for String lexemes");
246        tokenString_LITERAL = (String)symbol.value;
247        LITERALstart = symbol.getStart();
248        LITERALend = symbol.getEnd();
249      }
250      /**
251       * Retrieves the value for the lexeme LITERAL.
252       * @return The value for the lexeme LITERAL.
253       * @apilevel high-level
254       */
255      @ASTNodeAnnotation.Token(name="LITERAL")
256      public String getLITERAL() {
257        return tokenString_LITERAL != null ? tokenString_LITERAL : "";
258      }
259      /**
260       * Check for and report literal-out-of-bounds error.
261       * If the constant is error-marked, there exists a literal out of bounds error.
262       * @aspect Java7Literals
263       * @declaredat /home/jesper/git/extendj/java7/frontend/Literals.jrag:146
264       */
265        public void typeCheck() {
266        if (constant().error) {
267          errorf("The integer literal \"%s\" is too large for type int.", getLITERAL());
268        }
269    
270      }
271      /**
272       * @attribute syn
273       * @aspect PositiveLiterals
274       * @declaredat /home/jesper/git/extendj/java4/frontend/PositiveLiterals.jrag:36
275       */
276      @ASTNodeAnnotation.Attribute
277      public boolean isPositive() {
278        boolean isPositive_value = !getLITERAL().startsWith("-");
279    
280        return isPositive_value;
281      }
282      /**
283       * @apilevel internal
284       */
285      protected boolean type_computed = false;
286      /**
287       * @apilevel internal
288       */
289      protected TypeDecl type_value;
290      /**
291       * @apilevel internal
292       */
293      private void type_reset() {
294        type_computed = false;
295        type_value = null;
296      }
297      /**
298       * @attribute syn
299       * @aspect TypeAnalysis
300       * @declaredat /home/jesper/git/extendj/java4/frontend/TypeAnalysis.jrag:302
301       */
302      @ASTNodeAnnotation.Attribute
303      public TypeDecl type() {
304        ASTNode$State state = state();
305        if (type_computed) {
306          return type_value;
307        }
308        boolean intermediate = state.INTERMEDIATE_VALUE;
309        state.INTERMEDIATE_VALUE = false;
310        int num = state.boundariesCrossed;
311        boolean isFinal = this.is$Final();
312        type_value = typeInt();
313        if (isFinal && num == state().boundariesCrossed) {
314          type_computed = true;
315        } else {
316        }
317        state.INTERMEDIATE_VALUE |= intermediate;
318    
319        return type_value;
320      }
321      /**
322       * @apilevel internal
323       */
324      protected boolean constant_computed = false;
325      /**
326       * @apilevel internal
327       */
328      protected Constant constant_value;
329      /**
330       * @apilevel internal
331       */
332      private void constant_reset() {
333        constant_computed = false;
334        constant_value = null;
335      }
336      /**
337       * @attribute syn
338       * @aspect ConstantExpression
339       * @declaredat /home/jesper/git/extendj/java4/frontend/ConstantExpression.jrag:37
340       */
341      @ASTNodeAnnotation.Attribute
342      public Constant constant() {
343        ASTNode$State state = state();
344        if (constant_computed) {
345          return constant_value;
346        }
347        boolean intermediate = state.INTERMEDIATE_VALUE;
348        state.INTERMEDIATE_VALUE = false;
349        int num = state.boundariesCrossed;
350        boolean isFinal = this.is$Final();
351        constant_value = constant_compute();
352        if (isFinal && num == state().boundariesCrossed) {
353          constant_computed = true;
354        } else {
355        }
356        state.INTERMEDIATE_VALUE |= intermediate;
357    
358        return constant_value;
359      }
360      /**
361       * @apilevel internal
362       */
363      private Constant constant_compute() {
364          long l = 0;
365          try {
366            l = parseLong();
367          } catch (NumberFormatException e) {
368            Constant c = Constant.create(0L);
369            c.error = true;
370            return c;
371          }
372          int clamped = (int) l;
373          Constant c = Constant.create(clamped);
374          if (l != clamped) {
375            if (l != (0xFFFFFFFFL & clamped) || getKind() == DECIMAL) {
376              c.error = true;
377            }
378          }
379          return c;
380        }
381      /**
382       * Utility attribute for literal rewriting.
383       * Any of the NumericLiteral subclasses have already
384       * been rewritten and/or parsed, and should not be
385       * rewritten again.
386       * 
387       * @return true if this literal is a "raw", not-yet-parsed NumericLiteral
388       * @attribute syn
389       * @aspect Java7Literals
390       * @declaredat /home/jesper/git/extendj/java7/frontend/Literals.jrag:373
391       */
392      @ASTNodeAnnotation.Attribute
393      public boolean needsRewrite() {
394        boolean needsRewrite_value = false;
395    
396        return needsRewrite_value;
397      }
398      /**
399       * @apilevel internal
400       */
401      public ASTNode rewriteTo() {
402        return super.rewriteTo();
403      }
404    }