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.endsWithReturn_visited = -1;
022        node.localLookupMethod_String_List_visited = null;
023        node.localLookupMethod_String_List_values = null;
024        node.lookup_String_visited = null;
025        node.unknownDecl_visited = -1;
026        node.lookupMethod_String_List_visited = null;
027        node.in$Circle(false);
028        node.is$Final(false);
029        return node;
030      }
031      /**
032       * @apilevel internal
033       */
034      public Block copy() {
035        try {
036          Block node = (Block) clone();
037          node.parent = null;
038          if(children != null) {
039            node.children = (ASTNode[]) children.clone();
040          }
041          return node;
042        } catch (CloneNotSupportedException e) {
043          throw new Error("Error: clone not supported for " + getClass().getName());
044        }
045      }
046      /**
047       * Create a deep copy of the AST subtree at this node.
048       * The copy is dangling, i.e. has no parent.
049       * @return dangling copy of the subtree at this node
050       * @apilevel low-level
051       */
052      public Block fullCopy() {
053        Block tree = (Block) copy();
054        if (children != null) {
055          for (int i = 0; i < children.length; ++i) {
056            ASTNode child = (ASTNode) children[i];
057            if(child != null) {
058              child = child.fullCopy();
059              tree.setChild(child, i);
060            }
061          }
062        }
063        return tree;
064      }
065      /**
066       * @aspect PrettyPrint
067       * @declaredat /home/csz-naf/examples/PicoJava/spec/PrettyPrint.jadd:31
068       */
069      public void prettyPrint(StringBuilder sb, int t) {
070                sb.append("{\n");
071                for (BlockStmt bs : getBlockStmts()) {
072                        bs.prettyPrint(sb, t+1);
073                }
074                sb.append(getIndent(t)).append("}\n");
075        }
076      /**
077       */
078      public Block() {
079        super();
080      }
081      /**
082       * Initializes the child array to the correct size.
083       * Initializes List and Opt nta children.
084       * @apilevel internal
085       * @ast method
086       */
087      public void init$Children() {
088        children = new ASTNode[1];
089        setChild(new List(), 0);
090      }
091      /**
092       */
093      public Block(List<BlockStmt> p0) {
094        setChild(p0, 0);
095      }
096      /**
097       * @apilevel low-level
098       */
099      protected int numChildren() {
100        return 1;
101      }
102      /**
103       * @apilevel internal
104       */
105      public boolean mayHaveRewrite() {
106        return false;
107      }
108      /**
109       * @apilevel low-level
110       */
111      public void flushCache() {
112        super.flushCache();
113        localLookup_String_visited = null;
114        localLookup_String_values = null;
115        endsWithReturn_visited = -1;
116        localLookupMethod_String_List_visited = null;
117        localLookupMethod_String_List_values = null;
118        lookup_String_visited = null;
119        unknownDecl_visited = -1;
120        lookupMethod_String_List_visited = null;
121      }
122      /**
123       * @apilevel internal
124       */
125      public void flushCollectionCache() {
126        super.flushCollectionCache();
127      }
128      /**
129       * Replaces the BlockStmt list.
130       * @param list The new list node to be used as the BlockStmt list.
131       * @apilevel high-level
132       */
133      public void setBlockStmtList(List<BlockStmt> list) {
134        setChild(list, 0);
135      }
136      /**
137       * Retrieves the number of children in the BlockStmt list.
138       * @return Number of children in the BlockStmt list.
139       * @apilevel high-level
140       */
141      public int getNumBlockStmt() {
142        return getBlockStmtList().getNumChild();
143      }
144      /**
145       * Retrieves the number of children in the BlockStmt list.
146       * Calling this method will not trigger rewrites.
147       * @return Number of children in the BlockStmt list.
148       * @apilevel low-level
149       */
150      public int getNumBlockStmtNoTransform() {
151        return getBlockStmtListNoTransform().getNumChildNoTransform();
152      }
153      /**
154       * Retrieves the element at index {@code i} in the BlockStmt list.
155       * @param i Index of the element to return.
156       * @return The element at position {@code i} in the BlockStmt list.
157       * @apilevel high-level
158       */
159      public BlockStmt getBlockStmt(int i) {
160        return (BlockStmt) getBlockStmtList().getChild(i);
161      }
162      /**
163       * Check whether the BlockStmt list has any children.
164       * @return {@code true} if it has at least one child, {@code false} otherwise.
165       * @apilevel high-level
166       */
167      public boolean hasBlockStmt() {
168        return getBlockStmtList().getNumChild() != 0;
169      }
170      /**
171       * Append an element to the BlockStmt list.
172       * @param node The element to append to the BlockStmt list.
173       * @apilevel high-level
174       */
175      public void addBlockStmt(BlockStmt node) {
176        List<BlockStmt> list = (parent == null || state == null) ? getBlockStmtListNoTransform() : getBlockStmtList();
177        list.addChild(node);
178      }
179      /**
180       * @apilevel low-level
181       */
182      public void addBlockStmtNoTransform(BlockStmt node) {
183        List<BlockStmt> list = getBlockStmtListNoTransform();
184        list.addChild(node);
185      }
186      /**
187       * Replaces the BlockStmt list element at index {@code i} with the new node {@code node}.
188       * @param node The new node to replace the old list element.
189       * @param i The list index of the node to be replaced.
190       * @apilevel high-level
191       */
192      public void setBlockStmt(BlockStmt node, int i) {
193        List<BlockStmt> list = getBlockStmtList();
194        list.setChild(node, i);
195      }
196      /**
197       * Retrieves the BlockStmt list.
198       * @return The node representing the BlockStmt list.
199       * @apilevel high-level
200       */
201      public List<BlockStmt> getBlockStmtList() {
202        List<BlockStmt> list = (List<BlockStmt>) getChild(0);
203        list.getNumChild();
204        return list;
205      }
206      /**
207       * Retrieves the BlockStmt list.
208       * <p><em>This method does not invoke AST transformations.</em></p>
209       * @return The node representing the BlockStmt list.
210       * @apilevel low-level
211       */
212      public List<BlockStmt> getBlockStmtListNoTransform() {
213        return (List<BlockStmt>) getChildNoTransform(0);
214      }
215      /**
216       * Retrieves the BlockStmt list.
217       * @return The node representing the BlockStmt list.
218       * @apilevel high-level
219       */
220      public List<BlockStmt> getBlockStmts() {
221        return getBlockStmtList();
222      }
223      /**
224       * Retrieves the BlockStmt list.
225       * <p><em>This method does not invoke AST transformations.</em></p>
226       * @return The node representing the BlockStmt list.
227       * @apilevel low-level
228       */
229      public List<BlockStmt> getBlockStmtsNoTransform() {
230        return getBlockStmtListNoTransform();
231      }
232      /**
233       * @apilevel internal
234       */
235      protected java.util.Map localLookup_String_visited;
236      protected java.util.Map localLookup_String_values;
237      /**
238       * @attribute syn
239       * @aspect NameResolution
240       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:84
241       */
242      public Decl localLookup(String name) {
243        Object _parameters = name;
244        if(localLookup_String_visited == null) localLookup_String_visited = new java.util.HashMap(4);
245        if(localLookup_String_values == null) localLookup_String_values = new java.util.HashMap(4);
246        if(localLookup_String_values.containsKey(_parameters)) {
247          return (Decl)localLookup_String_values.get(_parameters);
248        }
249        ASTNode$State state = state();
250        if (Integer.valueOf(state().boundariesCrossed).equals(localLookup_String_visited.get(_parameters))) {
251          throw new RuntimeException("Circular definition of attr: localLookup in class: org.jastadd.ast.AST.SynDecl");
252        }
253        localLookup_String_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
254        int num = state.boundariesCrossed;
255        boolean isFinal = this.is$Final();
256        Decl localLookup_String_value = localLookup_compute(name);
257        if(isFinal && num == state().boundariesCrossed) {
258          localLookup_String_values.put(_parameters, localLookup_String_value);
259        } else {
260        }
261    
262        localLookup_String_visited.remove(_parameters);
263        return localLookup_String_value;
264      }
265      /**
266       * @apilevel internal
267       */
268      private Decl localLookup_compute(String name) {
269          for (int k = 0; k < getNumBlockStmt(); k++) {
270            Decl d = getBlockStmt(k).declarationOf(name);
271            if (d != null) return d;
272          }
273          return unknownDecl();
274        }
275      /**
276       * @apilevel internal
277       */
278      protected int endsWithReturn_visited = -1;
279      /**
280       * @attribute syn
281       * @aspect ErrorCheck
282       * @declaredat /home/csz-naf/examples/PicoJavaMethods/spec/ErrorCheck.jrag:12
283       */
284      public boolean endsWithReturn() {
285        ASTNode$State state = state();
286        if (endsWithReturn_visited == state().boundariesCrossed) {
287          throw new RuntimeException("Circular definition of attr: endsWithReturn in class: org.jastadd.ast.AST.SynDecl");
288        }
289        endsWithReturn_visited = state().boundariesCrossed;
290        try {  return getNumBlockStmt() > 0 ? getBlockStmt(getNumBlockStmt()-1).endsWithReturn() : false;  }
291        finally {
292          endsWithReturn_visited = -1;
293        }
294      }
295      /**
296       * @apilevel internal
297       */
298      protected java.util.Map localLookupMethod_String_List_visited;
299      protected java.util.Map localLookupMethod_String_List_values;
300      /**
301       * @attribute syn
302       * @aspect NameResolution
303       * @declaredat /home/csz-naf/examples/PicoJavaMethods/spec/NameResolution.jrag:34
304       */
305      public Decl localLookupMethod(String name, List argList) {
306        java.util.List _parameters = new java.util.ArrayList(2);
307        _parameters.add(name);
308        _parameters.add(argList);
309        if(localLookupMethod_String_List_visited == null) localLookupMethod_String_List_visited = new java.util.HashMap(4);
310        if(localLookupMethod_String_List_values == null) localLookupMethod_String_List_values = new java.util.HashMap(4);
311        if(localLookupMethod_String_List_values.containsKey(_parameters)) {
312          return (Decl)localLookupMethod_String_List_values.get(_parameters);
313        }
314        ASTNode$State state = state();
315        if (Integer.valueOf(state().boundariesCrossed).equals(localLookupMethod_String_List_visited.get(_parameters))) {
316          throw new RuntimeException("Circular definition of attr: localLookupMethod in class: org.jastadd.ast.AST.SynDecl");
317        }
318        localLookupMethod_String_List_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
319        int num = state.boundariesCrossed;
320        boolean isFinal = this.is$Final();
321        Decl localLookupMethod_String_List_value = localLookupMethod_compute(name, argList);
322        if(isFinal && num == state().boundariesCrossed) {
323          localLookupMethod_String_List_values.put(_parameters, localLookupMethod_String_List_value);
324        } else {
325        }
326    
327        localLookupMethod_String_List_visited.remove(_parameters);
328        return localLookupMethod_String_List_value;
329      }
330      /**
331       * @apilevel internal
332       */
333      private Decl localLookupMethod_compute(String name, List argList) {
334               for (int k = 0; k < getNumBlockStmt(); k++) {
335                       Decl d = getBlockStmt(k).declarationOf(name, argList);
336                       if (d != null) return d;
337           }
338               return unknownDecl();
339       }
340      /**
341       * @attribute inh
342       * @aspect NameResolution
343       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:79
344       */
345      public Decl lookup(String name) {
346        Object _parameters = name;
347        if(lookup_String_visited == null) lookup_String_visited = new java.util.HashMap(4);
348        ASTNode$State state = state();
349        if (Integer.valueOf(state().boundariesCrossed).equals(lookup_String_visited.get(_parameters))) {
350          throw new RuntimeException("Circular definition of attr: lookup in class: org.jastadd.ast.AST.InhDecl");
351        }
352        lookup_String_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
353        Decl lookup_String_value = getParent().Define_Decl_lookup(this, null, name);
354    
355        lookup_String_visited.remove(_parameters);
356        return lookup_String_value;
357      }
358      /**
359       * @apilevel internal
360       */
361      protected java.util.Map lookup_String_visited;
362      /**
363       * @attribute inh
364       * @aspect NullObjects
365       * @declaredat /home/csz-naf/examples/PicoJava/spec/NullObjects.jrag:21
366       */
367      public Decl unknownDecl() {
368        ASTNode$State state = state();
369        if (unknownDecl_visited == state().boundariesCrossed) {
370          throw new RuntimeException("Circular definition of attr: unknownDecl in class: org.jastadd.ast.AST.InhDecl");
371        }
372        unknownDecl_visited = state().boundariesCrossed;
373        Decl unknownDecl_value = getParent().Define_Decl_unknownDecl(this, null);
374    
375        unknownDecl_visited = -1;
376        return unknownDecl_value;
377      }
378      /**
379       * @apilevel internal
380       */
381      protected int unknownDecl_visited = -1;
382      /**
383       * @attribute inh
384       * @aspect NameResolution
385       * @declaredat /home/csz-naf/examples/PicoJavaMethods/spec/NameResolution.jrag:7
386       */
387      public Decl lookupMethod(String name, List argList) {
388        java.util.List _parameters = new java.util.ArrayList(2);
389        _parameters.add(name);
390        _parameters.add(argList);
391        if(lookupMethod_String_List_visited == null) lookupMethod_String_List_visited = new java.util.HashMap(4);
392        ASTNode$State state = state();
393        if (Integer.valueOf(state().boundariesCrossed).equals(lookupMethod_String_List_visited.get(_parameters))) {
394          throw new RuntimeException("Circular definition of attr: lookupMethod in class: org.jastadd.ast.AST.InhDecl");
395        }
396        lookupMethod_String_List_visited.put(_parameters, Integer.valueOf(state().boundariesCrossed));
397        Decl lookupMethod_String_List_value = getParent().Define_Decl_lookupMethod(this, null, name, argList);
398    
399        lookupMethod_String_List_visited.remove(_parameters);
400        return lookupMethod_String_List_value;
401      }
402      /**
403       * @apilevel internal
404       */
405      protected java.util.Map lookupMethod_String_List_visited;
406      /**
407       * @declaredat /home/csz-naf/examples/PicoJava/spec/NameResolution.jrag:23
408       * @apilevel internal
409       */
410      public Decl Define_Decl_lookup(ASTNode caller, ASTNode child, String name) {
411        if (caller == getBlockStmtListNoTransform()) {
412          int index = caller.getIndexOfChild(child);
413          {
414        // First, look in the local declarations
415        if (!localLookup(name).isUnknown())
416          return localLookup(name);
417        // Then, look in surrounding context
418        return lookup(name);
419      }
420        }
421        else {
422          return getParent().Define_Decl_lookup(this, caller, name);
423        }
424      }
425      /**
426       * @declaredat /home/csz-naf/examples/PicoJavaMethods/spec/NameResolution.jrag:10
427       * @apilevel internal
428       */
429      public Decl Define_Decl_lookupMethod(ASTNode caller, ASTNode child, String name, List argList) {
430        if (caller == getBlockStmtListNoTransform()) {
431          int index = caller.getIndexOfChild(child);
432          {
433               if (!localLookupMethod(name, argList).isUnknown())
434                       return localLookupMethod(name, argList);
435               return lookupMethod(name, argList);
436       }
437        }
438        else {
439          return getParent().Define_Decl_lookupMethod(this, caller, name, argList);
440        }
441      }
442      /**
443       * @apilevel internal
444       */
445      public ASTNode rewriteTo() {    return super.rewriteTo();
446      }}