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    
024    /** The <code>make</code> method of this class has the side effect of
025    pointing the <code>parent</code> pointers of the <code>TypeDecl</code>s
026    inside a <code>CompilationUnit</code> to point to that unit. */
027    
028    public class CompilationUnit extends ASTNode
029    {
030      public Name pkgName;
031    
032      public LexicalPragmaVec lexicalPragmas;
033    
034      public /*@ non_null @*/ ImportDeclVec imports;
035    
036      public /*@ non_null @*/ TypeDeclVec elems;
037    
038      //@ invariant loc != javafe.util.Location.NULL;
039      public int loc;
040    
041      public /*@ non_null @*/ TypeDeclElemVec otherPragmas;
042    
043    
044      public boolean duplicate = false;
045    
046      private void postCheck() {
047        for(int i = 0; i < elems.size(); i++) {
048          for(int j = i+1; j < elems.size(); j++)
049            Assert.notFalse(elems.elementAt(i) != elems.elementAt(j));  //@ nowarn Pre;
050        }
051      }
052    
053        /**
054         * @return true iff this CompilationUnit was created from a .class
055         * file.
056         */
057        public boolean isBinary() {
058            return Location.toFileName(loc).endsWith(".class");
059        }
060    
061      public int getStartLoc() { return loc; }
062      public int getEndLoc() { 
063        if (elems == null || elems.size() < 1)
064          return super.getEndLoc();
065    
066        return elems.elementAt(elems.size()-1).getEndLoc();
067      }
068    
069      public javafe.genericfile.GenericFile sourceFile() {
070        return Location.toFile(loc);
071      }
072    
073    
074    // Generated boilerplate constructors:
075    
076      /**
077       * Construct a raw CompilationUnit whose class invariant(s) have not
078       * yet been established.  It is the caller's job to
079       * initialize the returned node's fields so that any
080       * class invariants hold.
081       */
082      //@ requires I_will_establish_invariants_afterwards;
083      protected CompilationUnit() {}    //@ nowarn Invariant,NonNullInit;
084    
085    
086    // Generated boilerplate methods:
087    
088      public final int childCount() {
089         int sz = 0;
090         if (this.lexicalPragmas != null) sz += this.lexicalPragmas.size();
091         if (this.imports != null) sz += this.imports.size();
092         if (this.elems != null) sz += this.elems.size();
093         if (this.otherPragmas != null) sz += this.otherPragmas.size();
094         return sz + 1;
095      }
096    
097      public final Object childAt(int index) {
098              /*throws IndexOutOfBoundsException*/
099         if (index < 0)
100            throw new IndexOutOfBoundsException("AST child index " + index);
101         int indexPre = index;
102    
103         int sz;
104    
105         if (index == 0) return this.pkgName;
106         else index--;
107    
108         sz = (this.lexicalPragmas == null ? 0 : this.lexicalPragmas.size());
109         if (0 <= index && index < sz)
110            return this.lexicalPragmas.elementAt(index);
111         else index -= sz;
112    
113         sz = (this.imports == null ? 0 : this.imports.size());
114         if (0 <= index && index < sz)
115            return this.imports.elementAt(index);
116         else index -= sz;
117    
118         sz = (this.elems == null ? 0 : this.elems.size());
119         if (0 <= index && index < sz)
120            return this.elems.elementAt(index);
121         else index -= sz;
122    
123         sz = (this.otherPragmas == null ? 0 : this.otherPragmas.size());
124         if (0 <= index && index < sz)
125            return this.otherPragmas.elementAt(index);
126         else index -= sz;
127    
128         throw new IndexOutOfBoundsException("AST child index " + indexPre);
129      }   //@ nowarn Exception;
130    
131      public final String toString() {
132         return "[CompilationUnit"
133            + " pkgName = " + this.pkgName
134            + " lexicalPragmas = " + this.lexicalPragmas
135            + " imports = " + this.imports
136            + " elems = " + this.elems
137            + " loc = " + this.loc
138            + " otherPragmas = " + this.otherPragmas
139            + "]";
140      }
141    
142      public final int getTag() {
143         return TagConstants.COMPILATIONUNIT;
144      }
145    
146      public final void accept(Visitor v) { v.visitCompilationUnit(this); }
147    
148      public final Object accept(VisitorArgResult v, Object o) {return v.visitCompilationUnit(this, o); }
149    
150      public void check() {
151         super.check();
152         if (this.pkgName != null)
153            this.pkgName.check();
154         if (this.lexicalPragmas != null)
155            for(int i = 0; i < this.lexicalPragmas.size(); i++)
156               this.lexicalPragmas.elementAt(i).check();
157         for(int i = 0; i < this.imports.size(); i++)
158            this.imports.elementAt(i).check();
159         if (this.elems == null) throw new RuntimeException();
160         if (this.otherPragmas == null) throw new RuntimeException();
161         postCheck();
162      }
163    
164      //@ requires loc != javafe.util.Location.NULL;
165      //@ ensures \result != null;
166      public static CompilationUnit make(Name pkgName, LexicalPragmaVec lexicalPragmas, /*@ non_null @*/ ImportDeclVec imports, /*@ non_null @*/ TypeDeclVec elems, int loc, /*@ non_null @*/ TypeDeclElemVec otherPragmas) {
167         //@ set I_will_establish_invariants_afterwards = true;
168         CompilationUnit result = new CompilationUnit();
169         result.pkgName = pkgName;
170         result.lexicalPragmas = lexicalPragmas;
171         result.imports = imports;
172         result.elems = elems;
173         result.loc = loc;
174         result.otherPragmas = otherPragmas;
175         return result;
176      }
177    }