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 NewInstanceExpr extends Expr
023    {
024      /**
025       * The enclosing instance is the object expression before a new
026       * call ( <enclosingInstance>.new T(...) ).  This field may be null
027       * if there is no such expression.
028       *
029       * @note If the type in question is an inner class, then the
030       * type checker will infer a [<C>.]this expression if no expression
031       * is present and place it in this slot.  (See ThisExpr for how to
032       * distinguish inferred this expressions.)<p>
033       */
034      public Expr enclosingInstance;
035    
036    
037      //@ invariant (enclosingInstance == null) == (locDot==Location.NULL);
038      public int locDot;
039    
040    
041      public /*@ non_null @*/ TypeName type;
042    
043      public /*@ non_null @*/ ExprVec args;
044    
045    
046      /**
047       * If the new expression includes a declaration of an inner class,
048       * then "anonDecl" will be non-null.  In this case, the
049       * "superclass" field of "ananDecl" will be null and
050       * "superinterfaces" list of "anonDecl" will be empty.  One of
051       * these fields needs to be modified during type checking depending
052       * on whether "type" is a class or an interface.
053       */
054      public ClassDecl anonDecl;
055    
056    
057      //@ invariant loc != javafe.util.Location.NULL;
058      public int loc;
059    
060      //@ invariant locOpenParen != javafe.util.Location.NULL;
061      public int locOpenParen;
062    
063    
064      public ConstructorDecl decl;
065    
066      public int getStartLoc() {
067        if (enclosingInstance == null) return loc;
068        else return enclosingInstance.getStartLoc();
069      }
070    
071      public int getEndLoc() {
072        if (decl == null) {
073          if (args.size()==0) return type.getEndLoc();
074          return args.elementAt(args.size()-1).getEndLoc();
075        } else return decl.getEndLoc();
076      }
077    
078      //@ requires (enclosingInstance == null) == (locDot==Location.NULL);
079      //
080      //@ requires loc != javafe.util.Location.NULL;
081      //@ requires locOpenParen != javafe.util.Location.NULL;
082      //@ ensures \result != null;
083      public static NewInstanceExpr make(Expr enclosingInstance, 
084                                         int locDot, 
085                                         /*@ non_null @*/ TypeName type, 
086                                         /*@ non_null @*/ ExprVec args, 
087                                         ClassDecl anonDecl, 
088                                         int loc, 
089                                         int locOpenParen) {
090         //@ set I_will_establish_invariants_afterwards = true;
091         NewInstanceExpr result = new NewInstanceExpr();
092         result.enclosingInstance = enclosingInstance;
093         result.locDot = locDot;
094         result.type = type;
095         result.args = args;
096         result.anonDecl = anonDecl;
097         result.loc = loc;
098         result.locOpenParen = locOpenParen;
099         return result;
100      }
101    
102    
103    // Generated boilerplate constructors:
104    
105      /**
106       * Construct a raw NewInstanceExpr whose class invariant(s) have not
107       * yet been established.  It is the caller's job to
108       * initialize the returned node's fields so that any
109       * class invariants hold.
110       */
111      //@ requires I_will_establish_invariants_afterwards;
112      protected NewInstanceExpr() {}    //@ nowarn Invariant,NonNullInit;
113    
114    
115    // Generated boilerplate methods:
116    
117      public final int childCount() {
118         int sz = 0;
119         if (this.args != null) sz += this.args.size();
120         return sz + 3;
121      }
122    
123      public final Object childAt(int index) {
124              /*throws IndexOutOfBoundsException*/
125         if (index < 0)
126            throw new IndexOutOfBoundsException("AST child index " + index);
127         int indexPre = index;
128    
129         int sz;
130    
131         if (index == 0) return this.enclosingInstance;
132         else index--;
133    
134         if (index == 0) return this.type;
135         else index--;
136    
137         sz = (this.args == null ? 0 : this.args.size());
138         if (0 <= index && index < sz)
139            return this.args.elementAt(index);
140         else index -= sz;
141    
142         if (index == 0) return this.anonDecl;
143         else index--;
144    
145         throw new IndexOutOfBoundsException("AST child index " + indexPre);
146      }   //@ nowarn Exception;
147    
148      public final String toString() {
149         return "[NewInstanceExpr"
150            + " enclosingInstance = " + this.enclosingInstance
151            + " locDot = " + this.locDot
152            + " type = " + this.type
153            + " args = " + this.args
154            + " anonDecl = " + this.anonDecl
155            + " loc = " + this.loc
156            + " locOpenParen = " + this.locOpenParen
157            + "]";
158      }
159    
160      public final int getTag() {
161         return TagConstants.NEWINSTANCEEXPR;
162      }
163    
164      public final void accept(Visitor v) { v.visitNewInstanceExpr(this); }
165    
166      public final Object accept(VisitorArgResult v, Object o) {return v.visitNewInstanceExpr(this, o); }
167    
168      public void check() {
169         super.check();
170         if (this.enclosingInstance != null)
171            this.enclosingInstance.check();
172         this.type.check();
173         for(int i = 0; i < this.args.size(); i++)
174            this.args.elementAt(i).check();
175         if (this.anonDecl != null)
176            this.anonDecl.check();
177      }
178    
179    }