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    public class NewArrayExpr extends Expr
023    {
024      /**
025       * The type of the elements being given zero-default values, or (if
026       * an array initializer is present), the type of the array
027       * initializer elements.
028       *
029       * <p> E.g., new int[4][3][] yields a type of int[] and new int[][][]{a, b}
030       * yields a type of int[][].
031       */
032      //@ invariant type.syntax;
033      public /*@ non_null @*/ Type type;
034    
035    
036      /**
037       * The array initializer, if any.  If it is present then dims will
038       * contain exactly 1 element, the inferred size of the array
039       * initializer.
040       *
041       * <p> E.g., new int[][]{7, 5} will generate a dims of {INTLIT(2)}.
042       */
043      //@ invariant init != null ==> dims.count==1;
044      public ArrayInit init;
045    
046    
047      /**
048       * If init is null, then holds Expr's between []'s in order.  If init
049       * is not null, then holds the inferred array size.  (cf. init).
050       *
051       * E.g., new int[x+y][z][] will generate a dims of {<x+y>, <z>}.
052       */
053      //@ invariant dims.count >= 1;
054      public /*@ non_null @*/ ExprVec dims;
055    
056    
057      //@ invariant loc != javafe.util.Location.NULL;
058      public int loc;
059    
060    
061      /**
062       * The locations of the open brackets for each Expr (possibly
063       * inferred if init != null) in dims.
064       *
065       * <p> The open bracket in front of dims[i] is located at
066       * locOpenBrackets[i].
067       *
068       * @note locOpenBrackets may contain junk after the first
069       * dims.size() entries.
070       */
071      //@ invariant locOpenBrackets.length >= dims.count;
072      /*@ invariant (\forall int i; (0 <= i && i<dims.count) ==> 
073                            locOpenBrackets[i] != Location.NULL); */
074      public /*@ non_null @*/ int[] locOpenBrackets;
075    
076    
077      public int getStartLoc() { return loc; }
078    
079      public int getEndLoc() { 
080        if (init == null) {
081          if (dims.size()<1)
082            Assert.fail("Invariant failure: NewArrayExpr with init & dims>0");
083          return dims.elementAt(dims.size()-1).getEndLoc();
084        } else return init.locCloseBrace;
085      }
086    
087      private void postCheck() {
088        Assert.notFalse(dims.size() >= 1);
089        if (init != null)
090            Assert.notFalse(dims.size()==1);
091      }
092    
093      //@ requires init != null ==> dims.count==1;
094      //@ requires dims.count >= 1;
095      //@ requires locOpenBrackets.length >= dims.count;
096      /*@ requires (\forall int i; (0 <= i && i<dims.count) ==> 
097                            locOpenBrackets[i] != Location.NULL); */
098      //
099      //@ requires type.syntax;
100      //@ requires loc != javafe.util.Location.NULL;
101      //@ ensures \result != null;
102      public static NewArrayExpr make(/*@ non_null @*/ Type type, 
103                                      /*@ non_null @*/ ExprVec dims, 
104                                      ArrayInit init, 
105                                      int loc, 
106                                      /*@ non_null @*/ int[] locOpenBrackets) {
107         //@ set I_will_establish_invariants_afterwards = true;
108         NewArrayExpr result = new NewArrayExpr();
109         result.type = type;
110         result.dims = dims;
111         result.init = init;
112         result.loc = loc;
113         result.locOpenBrackets = locOpenBrackets;
114         return result;
115      }
116    
117    
118    // Generated boilerplate constructors:
119    
120      /**
121       * Construct a raw NewArrayExpr whose class invariant(s) have not
122       * yet been established.  It is the caller's job to
123       * initialize the returned node's fields so that any
124       * class invariants hold.
125       */
126      //@ requires I_will_establish_invariants_afterwards;
127      protected NewArrayExpr() {}    //@ nowarn Invariant,NonNullInit;
128    
129    
130    // Generated boilerplate methods:
131    
132      public final int childCount() {
133         int sz = 0;
134         if (this.dims != null) sz += this.dims.size();
135         return sz + 3;
136      }
137    
138      public final Object childAt(int index) {
139              /*throws IndexOutOfBoundsException*/
140         if (index < 0)
141            throw new IndexOutOfBoundsException("AST child index " + index);
142         int indexPre = index;
143    
144         int sz;
145    
146         if (index == 0) return this.type;
147         else index--;
148    
149         if (index == 0) return this.init;
150         else index--;
151    
152         sz = (this.dims == null ? 0 : this.dims.size());
153         if (0 <= index && index < sz)
154            return this.dims.elementAt(index);
155         else index -= sz;
156    
157         if (index == 0) return this.locOpenBrackets;
158         else index--;
159    
160         throw new IndexOutOfBoundsException("AST child index " + indexPre);
161      }   //@ nowarn Exception;
162    
163      public final String toString() {
164         return "[NewArrayExpr"
165            + " type = " + this.type
166            + " init = " + this.init
167            + " dims = " + this.dims
168            + " loc = " + this.loc
169            + " locOpenBrackets = " + this.locOpenBrackets
170            + "]";
171      }
172    
173      public final int getTag() {
174         return TagConstants.NEWARRAYEXPR;
175      }
176    
177      public final void accept(Visitor v) { v.visitNewArrayExpr(this); }
178    
179      public final Object accept(VisitorArgResult v, Object o) {return v.visitNewArrayExpr(this, o); }
180    
181      public void check() {
182         super.check();
183         this.type.check();
184         if (this.init != null)
185            this.init.check();
186         for(int i = 0; i < this.dims.size(); i++)
187            this.dims.elementAt(i).check();
188         if (this.locOpenBrackets == null) throw new RuntimeException();
189         postCheck();
190      }
191    
192    }