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