JastAdd2 reports warnings for inherited attributes defined on ASTNode

Issue #175 resolved
Niklas Fors created an issue

JastAdd2 gives warnings for inherited attributes defined as follows:

 inh int ASTNode.a();
 eq Root.getChild().a() = 1;

Warning message:

  [jastadd] Warning at PATH missing inherited equation for attribute a

This bug was introduced in JastAdd2 version 2.1.6.

Comments (9)

  1. Jesper Öqvist

    I do not agree that this is a bug. There is no way to guarantee that ASTNode.a has an equation.

  2. Görel Hedin

    I agree that this is not a bug. However, it is very practical to write code like this. In practice, it is easy to avoid calling the attribute on the root, where the attribute is not defined. JModelica used this pattern (Jesper Mattsson mailed about this July 2014), but have now refactored the grammar to use a common superclass NonRoot (superclass of all classes except for roots), and place the attribute there instead:

    inh int NonRoot.a();
    eq Root.getChild().a() = 1;
    

    I have also used this solution in some early paper. We might document this as a pattern at some point.

  3. Görel Hedin

    The warning could be more informative. What about something like:

    [jastadd] Warning at PATH missing inherited equation for attribute a in root Root
    

    Also, I think a test case is missing for this situation. Probably add one to the inh/defCheck series.

  4. Jesper Mattsson

    There is one problem with the pattern: if you want an attribute that is available on all AST classes, including from methods on ASTNode.

    inh int ASTNode.a();
    eq Root.getChild().a() = 1;
    syn int Root.a() = 2;
    

    You would then have to do:

    syn int ASTNode.a() = 0;  // dummy value
    eq Root.a() = 2;
    inh int NonRoot.a();
    inh int List.a();
    inh int Opt.a();
    eq Root.getChild().a() = 1;
    

    but that is kind of confusing.

    An alternative is:

    syn int ASTNode.a() = 0;  // dummy value
    eq Root.a() = 2;
    eq NonRoot.a() = a_inh();
    eq List.a() = a_inh();
    eq Opt.a() = a_inh();
    
    inh int NonRoot.a_inh();
    inh int List.a_inh();
    inh int Opt.a_inh();
    eq Root.getChild().a_inh() = 1;
    

    but is significantly larger.

    Both would be slightly more logical if you could declare the attribute as abstract on ASTNode, but since ASTNode is not declared abstract, you can't.

  5. Jesper Öqvist

    Following discussions with Görel and Niklas we have decided that no warning should be given for missing inherited equations for attributes declared on root nodes until we can find a nicer solution which includes some simple way to avoid the warnings for those cases where such attributes are beneficial.

  6. Jesper Öqvist

    Relaxed missing inherited equation warning rules

    Removed reporting of missing inherited equations for attributes declared on root nodes.

    see issue #175 (bitbucket)

    → <<cset 3840681dee5a>>

  7. Log in to comment