[Jastadd] Default constructor for AST nodes

From: Jesper Mattsson <jesper.mattsson_at_modelon.com>
Date: Wed, 3 Oct 2012 14:40:07 +0000

Hi

The generated constructors for the AST node classes are rather inefficient.
Let's take an example, we have the following ast:
A;
B ::= [BA:A];
C : B ::= [CA:A];
D : C ::= [DA:A];
E : D ::= [EA:A];
F : E ::= [FA:A];

The default constructor of F will be:
  public F() {
    super();

    setChild(new Opt(), 0);
    setChild(new Opt(), 1);
    setChild(new Opt(), 2);
    setChild(new Opt(), 3);
    setChild(new Opt(), 4);
    is$Final(true);

  }

The default constructor of E will, in turn, be:
  public E() {
    super();

    setChild(new Opt(), 0);
    setChild(new Opt(), 1);
    setChild(new Opt(), 2);
    setChild(new Opt(), 3);
    is$Final(true);

  }

The other constructor of F will be:
  public F(Opt<A> p0, Opt<A> p1, Opt<A> p2, Opt<A> p3, Opt<A> p4) {
    setChild(p0, 0);
    setChild(p1, 1);
    setChild(p2, 2);
    setChild(p3, 3);
    setChild(p4, 4);
    is$Final(true);
  }

And so on. This means several things:

* To start with, looking at setChild() one can tell that this will result in a children array of size 1 being created, then replaced with one of size 2, and so on. In a case like this it is known when the Java files are generated how many children there will be, so it would be possible to create it with the correct size to start with.

* During the super call of F(), all children of E will be set. They will then be set again in F(). This applies recursively, so for the BA child of B, 5 Opt nodes will be created.

* The constructor of F that takes the children as arguments, will implicitly call the default constructor of D, thus creating Opt nodes for the children that will then be replaced by the constructor of F.

The first point could be solved by using the fact that numChildren() is overridden in declared AST nodes.
For example by adding this to the default constructor of ASTNode:
    numChildren = 0;
    int n = numChildren();
    if (n > 0)
            children = new ASTNode[n];

The second and third points are easily solved by letting each node class set only their own children, and leaving the others to the classes they are declared for. Example:
  public F() {
    super();
    setChild(new Opt(), 4);
    is$Final(true);
  }
  public F(Opt<A> p0, Opt<A> p1, Opt<A> p2, Opt<A> p3, Opt<A> p4) {
    super(p0, p1, p2, p3);
    setChild(p4, 4);
    is$Final(true);
  }

If there is some special case where this would not work, then an alternate solution for all these problems would be to add a constructor taking an array of ASTNode to all node classes (except Opt and List):
  public ASTNode(ASTNode[] children) {
    this.children = children;
  }
  public B(ASTNode[] children) {
    super(children);
  }
  public C(ASTNode[] children) {
    super(children);
  }
  // ...
  public F(ASTNode[] children) {
    super(children);
  }
And then using it in the constructors taking the children as separate arguments:
  public F(Opt<A> p0, Opt<A> p1, Opt<A> p2, Opt<A> p3, Opt<A> p4) {
    super(new ASTNode[] {p0, p1, p2, p3, p4});
    is$Final(true);
  }

The extra work might not be that much, but it does add up.
I hope that you can either use one of my suggestions, or that if they would not work, that another one can easily be found.

Jesper

Jesper MATTSSON, MSc
Software Developer & IT Administrator

Phone direct: +46 73 324 5909
Email: jesper.mattsson_at_modelon.com<mailto:jesper.mattsson_at_modelon.com>

[Description: Description: Description: Modelon_2011_Gradient_RGB_400]
________________________________
Modelon AB
Ideon Science Park
SE-223 70 Lund, Sweden

Phone: +46 46 286 2200
Fax: +46 46 286 2201


Web: http://www.modelon.com<http://www.modelon.com/>



This email and any attachments are intended solely for the use of the individual or entity to whom it is addressed and may be confidential and/or privileged. If you are not one of the named recipients or have received this email in error, (i) you should not read, disclose, or copy it, (ii) please notify sender of your receipt by reply email and delete this email and all attachments, (iii) Modelon does not accept or assume any liability or responsibility for any use of or reliance on this email.




image001.png
(image/png attachment: image001.png)

Received on Wed Oct 03 2012 - 16:40:33 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 16 2014 - 17:19:06 CEST