001    /* This file was generated with JastAdd2 (http://jastadd.org) version 2.1.3 */
002    package AST;
003    
004    import java.util.Collection;
005    import java.util.ArrayList;
006    import java.util.HashSet;
007    /**
008     * @ast node
009     * @declaredat /home/csz-naf/examples/PicoJava/spec/picojava.ast:3
010     * @production Block : {@link ASTNode} ::= <span class="component">{@link BlockStmt}*</span>;
011    
012     */
013    public class Block extends ASTNode<ASTNode> implements Cloneable {
014      /**
015       * @apilevel internal
016       */
017      public Block clone() throws CloneNotSupportedException {
018        Block node = (Block) super.clone();
019        node.localLookup_String_visited = null;
020        node.localLookup_String_values = null;
021        node.lookup_String_visited = null;
022        node.unknownDecl_visited = -1;
023        node.in$Circle(false);
024        node.is$Final(false);
025        return node;
026      }
027      /**
028       * @apilevel internal
029       */
030      public Block copy() {
031        try {
032          Block node = (Block) clone();
033          node.parent = null;
034          if(children != null) {
035            node.children = (ASTNode[]) children.clone();
036          }
037          return node;
038        } catch (CloneNotSupportedException e) {
039          throw new Error("Error: clone not supported for " + getClass().getName());
040        }
041      }
042      /**
043       * Create a deep copy of the AST subtree at this node.
044       * The copy is dangling, i.e. has no parent.
045       * @return dangling copy of the subtree at this node
046       * @apilevel low-level
047       */
048      public Block fullCopy() {
049        Block tree = (Block) copy();
050        if (children != null) {
051          for (int i = 0; i < children.length; ++i) {
052            ASTNode child = (ASTNode) children[i];
053            if(child != null) {
054              child = child.fullCopy();
055              tree.setChild(child, i);
056            }
057          }
058        }
059        return tree;
060      }
061      /**
062       * @aspect PrettyPrint
063       * @declaredat /home/csz-naf/examples/PicoJava/spec/PrettyPrint.jadd:31
064       */
065      public void prettyPrint(StringBuilder sb, int t) {
066                sb.append("{\n");
067                for (BlockStmt bs : getBlockStmts()) {
068                        bs.prettyPrint(sb, t+1);
069                }
070                sb.append(getIndent(t)).append("}\n");
071        }
072      /**
073       */
074      public Block() {
075        super();
076      }
077      /**
078       * Initializes the child array to the correct size.
079       * Initializes List and Opt nta children.
080       * @apilevel internal
081       * @ast method
082       */
083      public void init$Children() {
084        children = new ASTNode[1];
085        setChild(new List(), 0);
086      }
087      /**
088       */
089      public Block(List<BlockStmt> p0) {
090        setChild(p0, 0);
091      }
092      /**
093       * @apilevel low-level
094       */
095      protected int numChildren() {
096        return 1;
097      }
098      /**
099       * @apilevel internal
100       */
101      public boolean mayHaveRewrite() {
102        return false;
103      }
104      /**
105       * @apilevel low-level
106       */
107      public void flushCache() {
108        super.flushCache();
109        localLookup_String_visited = null;
110        localLookup_String_values = null;
111        lookup_String_visited = null;
112        unknownDecl_visited = -1;
113      }
114      /**
115       * @apilevel internal
116       */
117      public void flushCollectionCache() {
118        super.flushCollectionCache();
119      }
120      /**
121       * Replaces the BlockStmt list.
122       * @param list The new list node to be used as the BlockStmt list.
123       * @apilevel high-level
124       */
125      public void setBlockStmtList(List<BlockStmt> list) {
126        setChild(list, 0);
127      }
128      /**
129       * Retrieves the number of children in the BlockStmt list.
130       * @return Number of children in the BlockStmt list.
131       * @apilevel high-level
132       */
133      public int getNumBlockStmt() {
134        return getBlockStmtList().getNumChild();
135      }
136      /**
137       * Retrieves the number of children in the BlockStmt list.
138       * Calling this method will not trigger rewrites.
139       * @return Number of children in the BlockStmt list.
140       * @apilevel low-level
141       */
142      public int getNumBlockStmtNoTransform() {
143        return getBlockStmtListNoTransform().getNumChildNoTransform();
144      }
145      /**
146       * Retrieves the element at index {@code i} in the BlockStmt list.
147       * @param i Index of the element to return.
148       * @return The element at position {@code i} in the BlockStmt list.
149       * @apilevel high-level
150       */
151      public BlockStmt getBlockStmt(int i) {
152        return (BlockStmt) getBlockStmtList().getChild(i);
153      }
154      /**
155       * Check whether the BlockStmt list has any children.
156       * @return {@code true} if it has at least one child, {@code false} otherwise.
157       * @apilevel high-level
158       */
159      public boolean hasBlockStmt() {
160        return getBlockStmtList().getNumChild() != 0;
161      }
162      /**
163       * Append an element to the BlockStmt list.
164       * @param node The element to append to the BlockStmt list.
165       * @apilevel high-level
166       */
167      public void addBlockStmt(BlockStmt node) {
168        List<BlockStmt> list = (parent == null || state == null) ? getBlockStmtListNoTransform() : getBlockStmtList();
169        list.addChild(node);
170      }
171      /**
172       * @apilevel low-level
173       */
174      public void addBlockStmtNoTransform(BlockStmt node) {
175        List<BlockStmt> list = getBlockStmtListNoTransform();
176        list.addChild(node);
177      }
178      /**
179       * Replaces the BlockStmt list element at index {@code i} with the new node {@code node}.
180       * @param node The new node to replace the old list element.
181       * @param i The list index of the node to be replaced.
182       * @apilevel high-level
183       */
184      public void setBlockStmt(BlockStmt node, int i) {
185        List<BlockStmt> list = getBlockStmtList();
186        list.setChild(node, i);
187      }
188      /**
189       * Retrieves the BlockStmt list.
190       * @return The node representing the BlockStmt list.
191       * @apilevel high-level
192       */
193      public List<BlockStmt> getBlockStmtList() {
194        List<BlockStmt> list = (List<BlockStmt>) getChild(0);
195        list.getNumChild();
196        return list;
197      }
198      /**
199       * Retrieves the BlockStmt list.
200       * <p><em>This method does not invoke AST transformations.</em></p>
201       * @return The node representing the BlockStmt list.
202       * @apilevel low-level
203       */
204      public List<BlockStmt> getBlockStmtListNoTransform() {
205        return (List<BlockStmt>) getChildNoTransform(0);
206      }
207      /**
208       * Retrieves the BlockStmt list.
209       * @return The node representing the BlockStmt list.
210       * @apilevel high-level
211       */
212      public List<BlockStmt> getBlockStmts() {
213        return getBlockStmtList();
214      }
215      /**
216       * Retrieves the BlockStmt list.
217       * <p><em>This method does not invoke AST transformations.</em></p>
218       * @return The node representing the BlockStmt list.
219       * @apilevel low-level
220       */
221      public List<BlockStmt> getBlockStmtsNoTransform() {
222        return getBlockStmtListNoTransform();
223      }
224      /**
225       * @apilevel internal
226       */
227      protected java.util.Map localLookup_String_visited;
228      protected java.util.Map localLookup_String_values;
229      /**
230       * @attribute syn
231       * @aspect NameResolution
232       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:84
233       */
234      public Decl localLookup(String name) {
235        Object _parameters = name;
236        if(localLookup_String_visited == null) localLookup_String_visited = new java.util.HashMap(4);
237        if(localLookup_String_values == null) localLookup_String_values = new java.util.HashMap(4);
238        if(localLookup_String_values.containsKey(_parameters)) {
239          return (Decl)localLookup_String_values.get(_parameters);
240        }
241        ASTNode$State state = state();
242        if (Integer.valueOf(state().boundariesCrossed).equals(localLookup_String_visited.get(_parameters))) {
243          throw new RuntimeException("Circular definition of attr: localLookup in class: org.jastadd.ast.AST.SynDecl");
244        }
245        localLookup_String_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
246        int num = state.boundariesCrossed;
247        boolean isFinal = this.is$Final();
248        Decl localLookup_String_value = localLookup_compute(name);
249        if(isFinal && num == state().boundariesCrossed) {
250          localLookup_String_values.put(_parameters, localLookup_String_value);
251        } else {
252        }
253    
254        localLookup_String_visited.remove(_parameters);
255        return localLookup_String_value;
256      }
257      /**
258       * @apilevel internal
259       */
260      private Decl localLookup_compute(String name) {
261          for (int k = 0; k < getNumBlockStmt(); k++) {
262            Decl d = getBlockStmt(k).declarationOf(name);
263            if (d != null) return d;
264          }
265          return unknownDecl();
266        }
267      /**
268       * @attribute inh
269       * @aspect NameResolution
270       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:79
271       */
272      public Decl lookup(String name) {
273        Object _parameters = name;
274        if(lookup_String_visited == null) lookup_String_visited = new java.util.HashMap(4);
275        ASTNode$State state = state();
276        if (Integer.valueOf(state().boundariesCrossed).equals(lookup_String_visited.get(_parameters))) {
277          throw new RuntimeException("Circular definition of attr: lookup in class: org.jastadd.ast.AST.InhDecl");
278        }
279        lookup_String_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
280        Decl lookup_String_value = getParent().Define_Decl_lookup(this, null, name);
281    
282        lookup_String_visited.remove(_parameters);
283        return lookup_String_value;
284      }
285      /**
286       * @apilevel internal
287       */
288      protected java.util.Map lookup_String_visited;
289      /**
290       * @attribute inh
291       * @aspect NullObjects
292       * @declaredat /home/csz-naf/examples/PicoJava/spec/NullObjects.jrag:21
293       */
294      public Decl unknownDecl() {
295        ASTNode$State state = state();
296        if (unknownDecl_visited == state().boundariesCrossed) {
297          throw new RuntimeException("Circular definition of attr: unknownDecl in class: org.jastadd.ast.AST.InhDecl");
298        }
299        unknownDecl_visited = state().boundariesCrossed;
300        Decl unknownDecl_value = getParent().Define_Decl_unknownDecl(this, null);
301    
302        unknownDecl_visited = -1;
303        return unknownDecl_value;
304      }
305      /**
306       * @apilevel internal
307       */
308      protected int unknownDecl_visited = -1;
309      /**
310       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:23
311       * @apilevel internal
312       */
313      public Decl Define_Decl_lookup(ASTNode caller, ASTNode child, String name) {
314        if (caller == getBlockStmtListNoTransform()) {
315          int index = caller.getIndexOfChild(child);
316          {
317        // First, look in the local declarations
318        if (!localLookup(name).isUnknown())
319          return localLookup(name);
320        // Then, look in surrounding context
321        return lookup(name);
322      }
323        }
324        else {
325          return getParent().Define_Decl_lookup(this, caller, name);
326        }
327      }
328      /**
329       * @apilevel internal
330       */
331      public ASTNode rewriteTo() {    return super.rewriteTo();
332      }}