001    // -*- mode: java -*-
002    /* Copyright 2000, 2001, Compaq Computer Corporation */
003    
004    /* IF THIS IS A JAVA FILE, DO NOT EDIT IT!  
005    
006       Most Java files in this directory which are part of the Javafe AST
007       are automatically generated using the astgen comment (see
008       ESCTools/Javafe/astgen) from the input file 'hierarchy.h'.  If you
009       wish to modify AST classes or introduce new ones, modify
010       'hierarchy.j.'
011     */
012    
013    package javafe.ast;
014    
015    import javafe.util.Assert;
016    import javafe.util.Location;
017    import javafe.util.ErrorSet;
018    
019    // Convention: unless otherwise noted, integer fields named "loc" refer
020    // to the location of the first character of the syntactic unit
021    
022    
023    public class NewArrayExpr extends Expr
024    {
025      /**
026       * The type of the elements being given zero-default values, or (if
027       * an array initializer is present), the type of the array
028       * initializer elements.
029       *
030       * <p> E.g., new int[4][3][] yields a type of int[] and new int[][][]{a, b}
031       * yields a type of int[][].
032       */
033      //@ invariant type.syntax;
034      public /*@ non_null @*/ Type type;
035    
036    
037      /**
038       * The array initializer, if any.  If it is present then dims will
039       * contain exactly 1 element, the inferred size of the array
040       * initializer.
041       *
042       * <p> E.g., new int[][]{7, 5} will generate a dims of {INTLIT(2)}.
043       */
044      //@ invariant init != null ==> dims.count==1;
045      public ArrayInit init;
046    
047    
048      /**
049       * If init is null, then holds Expr's between []'s in order.  If init
050       * is not null, then holds the inferred array size.  (cf. init).
051       *
052       * E.g., new int[x+y][z][] will generate a dims of {<x+y>, <z>}.
053       */
054      //@ invariant dims.count >= 1;
055      public /*@ non_null @*/ ExprVec dims;
056    
057    
058      //@ invariant loc != javafe.util.Location.NULL;
059      public int loc;
060    
061    
062      /**
063       * The locations of the open brackets for each Expr (possibly
064       * inferred if init != null) in dims.
065       *
066       * <p> The open bracket in front of dims[i] is located at
067       * locOpenBrackets[i].
068       *
069       * @note locOpenBrackets may contain junk after the first
070       * dims.size() entries.
071       */
072      //@ invariant locOpenBrackets.length >= dims.count;
073      /*@ invariant (\forall int i; (0 <= i && i<dims.count) ==> 
074                            locOpenBrackets[i] != Location.NULL); */
075      public /*@ non_null @*/ int[] locOpenBrackets;
076    
077    
078      //@ public represents startLoc <- loc;
079      public /*@ pure @*/ int getStartLoc() { return loc; }
080    
081      public /*@ pure @*/ int getEndLoc() { 
082        if (init == null) {
083          if (dims.size()<1)
084            Assert.fail("Invariant failure: NewArrayExpr with init & dims>0");
085          return dims.elementAt(dims.size()-1).getEndLoc();
086        } else return init.locCloseBrace;
087      }
088    
089      private void postCheck() {
090        Assert.notFalse(dims.size() >= 1);
091        if (init != null)
092            Assert.notFalse(dims.size()==1);
093      }
094    
095      //@ requires init != null ==> dims.count==1;
096      //@ requires dims.count >= 1;
097      //@ requires locOpenBrackets.length >= dims.count;
098      /*@ requires (\forall int i; (0 <= i && i<dims.count) ==> 
099                            locOpenBrackets[i] != Location.NULL); */
100      //
101      //@ requires type.syntax;
102      //@ requires loc != javafe.util.Location.NULL;
103      public static /*@non_null*/ NewArrayExpr make(/*@ non_null @*/ Type type, 
104                                      /*@ non_null @*/ ExprVec dims, 
105                                      ArrayInit init, 
106                                      int loc, 
107                                      /*@ non_null @*/ int[] locOpenBrackets) {
108         NewArrayExpr result = new NewArrayExpr(
109                                   type,
110                                   init,
111                                   dims,
112                                   loc,
113                                   locOpenBrackets);
114         return result;
115      }
116    
117    
118    // Generated boilerplate constructors:
119    
120      //@ ensures this.type == type;
121      //@ ensures this.init == init;
122      //@ ensures this.dims == dims;
123      //@ ensures this.loc == loc;
124      //@ ensures this.locOpenBrackets == locOpenBrackets;
125      protected NewArrayExpr(/*@ non_null @*/ Type type, ArrayInit init, /*@ non_null @*/ ExprVec dims, int loc, /*@ non_null @*/ int[] locOpenBrackets) {
126         super();
127         this.type = type;
128         this.init = init;
129         this.dims = dims;
130         this.loc = loc;
131         this.locOpenBrackets = locOpenBrackets;
132      }
133    
134    // Generated boilerplate methods:
135    
136      public final int childCount() {
137         int sz = 0;
138         if (this.dims != null) sz += this.dims.size();
139         return sz + 3;
140      }
141    
142      public final Object childAt(int index) {
143              /*throws IndexOutOfBoundsException*/
144         if (index < 0)
145            throw new IndexOutOfBoundsException("AST child index " + index);
146         int indexPre = index;
147    
148         int sz;
149    
150         if (index == 0) return this.type;
151         else index--;
152    
153         if (index == 0) return this.init;
154         else index--;
155    
156         sz = (this.dims == null ? 0 : this.dims.size());
157         if (0 <= index && index < sz)
158            return this.dims.elementAt(index);
159         else index -= sz;
160    
161         if (index == 0) return this.locOpenBrackets;
162         else index--;
163    
164         throw new IndexOutOfBoundsException("AST child index " + indexPre);
165      }   //@ nowarn Exception;
166    
167      public final /*@non_null*/String toString() {
168         return "[NewArrayExpr"
169            + " type = " + this.type
170            + " init = " + this.init
171            + " dims = " + this.dims
172            + " loc = " + this.loc
173            + " locOpenBrackets = " + this.locOpenBrackets
174            + "]";
175      }
176    
177      public final int getTag() {
178         return TagConstants.NEWARRAYEXPR;
179      }
180    
181      public final void accept(Visitor v) { v.visitNewArrayExpr(this); }
182    
183      public final Object accept(VisitorArgResult v, Object o) {return v.visitNewArrayExpr(this, o); }
184    
185      public void check() {
186         super.check();
187         this.type.check();
188         if (this.init != null)
189            this.init.check();
190         for(int i = 0; i < this.dims.size(); i++)
191            this.dims.elementAt(i).check();
192         if (this.locOpenBrackets == null) throw new RuntimeException();
193         postCheck();
194      }
195    
196    }