From 83f59c0a2d9f8f7147c14f2dae0f9e2e7d6ca0ad Mon Sep 17 00:00:00 2001
From: rschoene <rene.schoene@tu-dresden.de>
Date: Thu, 10 Oct 2019 18:56:21 +0200
Subject: [PATCH] Clean-up, document and rename some attributes.

---
 src/main/jastadd/Analysis.jrag                | 120 +++++++++++-------
 src/main/jastadd/Backend.jadd                 |   9 --
 src/main/jastadd/Errors.jrag                  |   4 +-
 src/main/jastadd/RelAst.parser                |  12 +-
 src/test/jastadd/errors/Errors.expected       |   1 +
 src/test/jastadd/errors/Errors.relast         |   1 +
 src/test/jastadd/errors/ErrorsLeft.expected   |   1 +
 src/test/jastadd/errors/ErrorsLeft.relast     |   1 +
 src/test/jastadd/errors/Inheritance.expected  |   4 +-
 src/test/jastadd/errors/Inheritance.relast    |   1 +
 .../jastadd/errors/InheritanceLeft.expected   |   4 +-
 11 files changed, 94 insertions(+), 64 deletions(-)

diff --git a/src/main/jastadd/Analysis.jrag b/src/main/jastadd/Analysis.jrag
index 0524c61..2ad299a 100644
--- a/src/main/jastadd/Analysis.jrag
+++ b/src/main/jastadd/Analysis.jrag
@@ -4,95 +4,103 @@ import java.util.*;
 aspect TypeAnalysis {
   public abstract TypeUse Component.getTypeUse();
 
+  //--- lookupType ---
   syn TypeDecl TypeUse.decl() = lookupType(getID());
   inh TypeDecl TypeUse.lookupType(String name);
+  inh TypeDecl TypeDecl.lookupType(String name);
   eq Program.getChild().lookupType(String name) {
-    for (TypeDecl td: getTypeDecls()) {
+    for (TypeDecl td : getTypeDecls()) {
       if (td.getID().equals(name)) {
         return td;
       }
     }
     return null;
   }
-  syn boolean TypeDecl.isAlreadyDeclared()
-    = lookupType(getID()) != this;
-  inh TypeDecl TypeDecl.lookupType(String name);
 
-  syn TypeDecl TypeDecl.mostGeneralSuperType() {
-    if (!hasSuper()) {
-      return this;
-    } else {
-      return getSuper().decl().mostGeneralSuperType();
-    }
-  }
+  //--- isAlreadyDeclared ---
+  syn boolean TypeDecl.isAlreadyDeclared() = lookupType(getID()) != this;
 }
 
 aspect ComponentAnalysis {
-  syn boolean Component.isTargetOfDirectedRelation() = false;
-  eq RelationComponent.isTargetOfDirectedRelation() = isTargetOfRightDirection() | isTargetOfLeftDirection();
-  inh boolean RelationComponent.isTargetOfRightDirection();
-  eq Relation.getRight().isTargetOfRightDirection()
-    = getDirection() instanceof RightDirection;
-  eq Program.getChild().isTargetOfRightDirection() = false;
-  inh boolean RelationComponent.isTargetOfLeftDirection();
-  eq Relation.getLeft().isTargetOfLeftDirection()
-    = getDirection() instanceof LeftDirection;
-  eq Program.getChild().isTargetOfLeftDirection() = false;
+  //--- isTargetOfDirectedRelation ---
+  inh boolean Component.isTargetOfDirectedRelation();
+  eq Relation.getRight().isTargetOfDirectedRelation() = getDirection() instanceof RightDirection;
+  eq Program.getChild().isTargetOfDirectedRelation() = false;
 
+  //--- name ---
   syn String Component.name() = getID();
 
+  //--- toTypeDecl ---
+  /** @return enclosing type */
   syn TypeDecl Component.toTypeDecl() = enclosingTypeDecl();
+  /** @return type of nonterminal relation role is defined for */
   eq RelationComponent.toTypeDecl() = getTypeUse().decl();
+
+  //--- enclosingTypeDecl ---
   inh TypeDecl Component.enclosingTypeDecl();
   eq TypeDecl.getChild().enclosingTypeDecl() = this;
   eq Program.getChild().enclosingTypeDecl() = null;
 
+  //--- otherSide ---
   inh RelationComponent RelationComponent.otherSide();
   eq Relation.getLeft().otherSide() = getRight();
   eq Relation.getRight().otherSide() = getLeft();
   eq Program.getChild().otherSide() = null;
 
+  //--- ofTypeDecl ---
   syn TypeDecl RelationComponent.ofTypeDecl() = otherSide().toTypeDecl();
 
-  syn boolean Component.isAlreadyDeclared()
-    = !isTargetOfDirectedRelation()
-      && toTypeDecl() != null
-      && lookupComponent(toTypeDecl(), name()) != this;
-  inh Component Component.lookupComponent(TypeDecl td, String name);
-  eq Program.getChild().lookupComponent(TypeDecl td, String name)
-    = lookupComponentSyn(td, name);
-  syn Component Program.lookupComponentSyn(TypeDecl td, String name) {
+  //--- isAlreadyDeclared ---
+  /**
+   * Check, if role with the same name is already declared on the same nonterminal
+   */
+  syn boolean RelationComponent.isAlreadyDeclared()
+    = !isTargetOfDirectedRelation()                     /* if unnamed in relation, there is no role name, so no error */
+      && toTypeDecl() != null                           /* nonterminal type of role is defined */
+      && findComponent(toTypeDecl(), name()) != this;   /* there is another role defined previously with the same name */
+
+  //--- findComponent ---
+  /** Search for either a component on the RHS of the given type with the given name,
+   *   or a relation part for the given type and a role with the given name */
+  inh Component Component.findComponent(TypeDecl td, String name);
+  eq Program.getChild().findComponent(TypeDecl td, String name)
+    = findComponentSyn(td, name);
+  syn Component Program.findComponentSyn(TypeDecl td, String name) {
     for (Component c: td.getComponents()) {
       if (c.name().equals(name)) {
         return c;
       }
     }
 
-    for (Relation r: getRelations()) {
-      Component c = r.getLeft().lookup(td, name);
-      if (c != null) return c;
-      c = r.getRight().lookup(td, name);
-      if (c != null) return c;
+    for (Relation r : getRelations()) {
+      if (r.getLeft().matches(td, name))
+        return r.getLeft();
+      if (r.getRight().matches(td, name))
+        return r.getRight();
     }
 
     return null;
   }
 
+  //--- isInvalidRedefinition ---
+  /**
+   * Check, if a component with the same name is already declared in some supertype
+   */
   syn boolean Component.isInvalidRedefinition() {
-    if (toTypeDecl() == null) return false;
-
     TypeDecl td = toTypeDecl();
+    if (td == null) return false;
 
     while (td.hasSuper() && td.getSuper().decl() != null) {
       td = td.getSuper().decl();
-      Component c = lookupComponent(td, getID());
-      if (c != null && isTargetOfDirectedRelation()) return true;
+      // find a matching component on the RHS of the (current) super type
+      Component c = findComponent(td, getID());
 
       if (c != null && !this.isEqual(c)) return true;
     }
     return false;
   }
 
+  //--- isEqual ---
   syn boolean Component.isEqual(Component c) = this.getClass() == c.getClass() && getTypeUse().isEqual(c.getTypeUse());
 
   syn boolean TypeUse.isEqual(TypeUse u);
@@ -108,12 +116,14 @@ aspect ComponentAnalysis {
     return true;
   }
 
-  syn RelationComponent RelationComponent.lookup(TypeDecl td, String name)
-    = !isTargetOfDirectedRelation() && toTypeDecl() == td && name().equals(name)
-    ? this
-    : null;
-
+  //--- matches ---
+  /**
+   * @return true, if the component has both type and role, its type matches the given typeDecl and its name matches the given name
+   */
+  syn boolean RelationComponent.matches(TypeDecl td, String name)
+    = !isTargetOfDirectedRelation() && toTypeDecl() == td && name().equals(name);
 
+  //--- relationComponents ---
   coll Set<RelationComponent> TypeDecl.relationComponents()
     [new HashSet<RelationComponent>()]
     root Program;
@@ -122,6 +132,7 @@ aspect ComponentAnalysis {
     to TypeDecl.relationComponents()
     for toTypeDecl();
 
+  //--- relationComponentsTransitive ---
   syn Collection<RelationComponent> TypeDecl.relationComponentsTransitive() {
     ArrayList<RelationComponent> list = new ArrayList<>();
     if (hasSuper() && getSuper().decl() != null) {
@@ -131,6 +142,7 @@ aspect ComponentAnalysis {
     return list;
   }
 
+  //--- oneRelationComponents ---
   syn Set<OneRelationComponent> TypeDecl.oneRelationComponents() {
     Set<OneRelationComponent> set = new HashSet<>();
     for (RelationComponent rc: relationComponents()) {
@@ -141,27 +153,33 @@ aspect ComponentAnalysis {
     return set;
   }
 
+  //--- needUnresolvedClass ---
   syn boolean TypeDecl.needUnresolvedClass() {
     // a TypeDecl needs an unresolved class, if it can appear in a relation
     // TODO
     return true;
   }
 
+  //--- isList ---
   syn boolean Component.isList() = false;
   eq ListComponent.isList() = true;
 
+  //--- isOpt ---
   syn boolean Component.isOpt() = false;
   eq OptComponent.isOpt() = true;
 
+  //--- isNullable ---
   syn boolean Component.isNullable() = false;
   eq TokenComponent.isNullable() = !"float double int short long char byte boolean".contains(getTypeUse().getID());
 }
 
 aspect InstanceSupplier {
 
+  //--- program ---
   inh Program TypeDecl.program();
   eq Program.getTypeDecl(int i).program() = this;
 
+  //--- subTypeDecls ---
   syn Collection<TypeDecl> TypeDecl.subTypeDecls() {
     java.util.List<TypeDecl> subDecls = new ArrayList();
     for (TypeDecl decl : program().getTypeDeclList()) {
@@ -172,6 +190,7 @@ aspect InstanceSupplier {
     return subDecls;
   }
 
+  //--- instantiableSubType ---
   syn TypeDecl TypeDecl.instantiableSubType() {
     if (getAbstract() == false) {
       return this;
@@ -192,6 +211,7 @@ aspect InstanceSupplier {
 }
 
 aspect Constructors {
+  //--- componentsTransitive ---
   syn Collection<Component> TypeDecl.componentsTransitive() {
     ArrayList<Component> list = new ArrayList<>();
     if (hasSuper() && getSuper().decl() != null) {
@@ -205,6 +225,7 @@ aspect Constructors {
     return list;
   }
 
+  //--- needsConstructor ---
   syn boolean TypeDecl.needsConstructor() {
     if (componentsTransitive().isEmpty()) {
       return false;
@@ -217,30 +238,37 @@ aspect Constructors {
       && getSuper().decl().needsConstructor();
   }
 
+  //--- inConstructor ---
   /**
    * @return true, if the component should be added to the constructor (i.e., is not an NTA)
    */
   syn boolean Component.inConstructor() = true;
   eq NTAComponent.inConstructor() = false;
+  eq NTAOptComponent.inConstructor() = false;
+  eq NTAListComponent.inConstructor() = false;
 }
 
 aspect Utils {
 
+  //--- isMany ---
   syn boolean RelationComponent.isMany() = false;
   eq ManyRelationComponent.isMany() = true;
 
+  //--- toString ---
   public String SimpleTypeUse.toString() {
     return getID();
   }
   public String ParameterizedTypeUse.toString() {
     StringBuilder sb = new StringBuilder();
     sb.append(getID()).append("<");
-    int i = 0;
+    boolean first = true;
     for (TypeUse u: getTypeUses()) {
-      sb.append(u.toString());
-      if (++i < getNumTypeUse()) {
+      if (first) {
+        first = false;
+      } else {
         sb.append(", ");
       }
+      sb.append(u.toString());
     }
     sb.append(">");
     return sb.toString();
diff --git a/src/main/jastadd/Backend.jadd b/src/main/jastadd/Backend.jadd
index f5d69d3..28b7ebd 100644
--- a/src/main/jastadd/Backend.jadd
+++ b/src/main/jastadd/Backend.jadd
@@ -222,9 +222,6 @@ aspect BackendDirectedAPI {
   public void RightDirection.generateAPI(StringBuilder sb) {
     relation().getLeft().generateDirectedAPI(sb);
   }
-  public void LeftDirection.generateAPI(StringBuilder sb) {
-    relation().getRight().generateDirectedAPI(sb);
-  }
 
   public abstract void RelationComponent.generateDirectedAPI(StringBuilder sb);
   public void OneRelationComponent.generateDirectedAPI(StringBuilder sb) {
@@ -913,9 +910,6 @@ aspect NameResolutionHelper {
   public void RightDirection.generateContextDependentNameResolution(StringBuilder sb) {
     relation().getLeft().generateContextDependentNameResolution(sb);
   }
-  public void LeftDirection.generateContextDependentNameResolution(StringBuilder sb) {
-    relation().getRight().generateContextDependentNameResolution(sb);
-  }
   public void Bidirectional.generateContextDependentNameResolution(StringBuilder sb) {
     relation().getLeft().generateContextDependentNameResolution(sb);
     relation().getRight().generateContextDependentNameResolution(sb);
@@ -1479,9 +1473,6 @@ aspect PrettyPrint {
   public String RightDirection.prettyPrint() {
     return "->";
   }
-  public String LeftDirection.prettyPrint() {
-    return "<-";
-  }
   public String Bidirectional.prettyPrint() {
     return "<->";
   }
diff --git a/src/main/jastadd/Errors.jrag b/src/main/jastadd/Errors.jrag
index 9d9f755..393c09d 100644
--- a/src/main/jastadd/Errors.jrag
+++ b/src/main/jastadd/Errors.jrag
@@ -15,13 +15,13 @@ aspect Errors {
     when isAlreadyDeclared()
     to Program.errors();
 
-  Component contributes error("Component '" + name()
+  RelationComponent contributes error("Role '" + name()
       + "' is already declared for type '" + toTypeDecl() + "'")
     when isAlreadyDeclared()
     to Program.errors();
 
   Component contributes error("Component '" + name()
-    + "' is an invalid redefition for type '" + toTypeDecl() + "'")
+    + "' is an invalid redefinition for type '" + toTypeDecl() + "'")
     when isInvalidRedefinition()
     to Program.errors();
 
diff --git a/src/main/jastadd/RelAst.parser b/src/main/jastadd/RelAst.parser
index 303dcb6..6b138b9 100644
--- a/src/main/jastadd/RelAst.parser
+++ b/src/main/jastadd/RelAst.parser
@@ -86,9 +86,15 @@ Relation relation =
   RELATION relation_comp.l direction relation_comp.r SCOL
    {:
       Relation result = new Relation();
-      result.setLeft(l);
-      result.setDirection(direction);
-      result.setRight(r);
+      if (direction instanceof LeftDirection) {
+        result.setLeft(r);
+        result.setDirection(new RightDirection());
+        result.setRight(l);
+      } else {
+        result.setLeft(l);
+        result.setDirection(direction);
+        result.setRight(r);
+      }
       return result;
    :}
   ;
diff --git a/src/test/jastadd/errors/Errors.expected b/src/test/jastadd/errors/Errors.expected
index 01049a4..be149e0 100644
--- a/src/test/jastadd/errors/Errors.expected
+++ b/src/test/jastadd/errors/Errors.expected
@@ -3,3 +3,4 @@ $FILENAME Line 5, column 5: Role name missing for type 'A'
 $FILENAME Line 6, column 15: Role name missing for type 'B'
 $FILENAME Line 7, column 12: The target of a directed relation cannot have a role name
 $FILENAME Line 8, column 13: The target of a directed relation may only have multiplicity 1
+$FILENAME Line 9, column 5: Role 'b2' is already declared for type 'A'
diff --git a/src/test/jastadd/errors/Errors.relast b/src/test/jastadd/errors/Errors.relast
index c5bfd75..1d63f57 100644
--- a/src/test/jastadd/errors/Errors.relast
+++ b/src/test/jastadd/errors/Errors.relast
@@ -6,3 +6,4 @@ rel A -> B;
 rel A.bs* <-> B*;
 rel A.b -> B.b;
 rel A.b2 -> B*;
+rel A.b2 -> A;
diff --git a/src/test/jastadd/errors/ErrorsLeft.expected b/src/test/jastadd/errors/ErrorsLeft.expected
index 155d0ec..191db3e 100644
--- a/src/test/jastadd/errors/ErrorsLeft.expected
+++ b/src/test/jastadd/errors/ErrorsLeft.expected
@@ -3,3 +3,4 @@ $FILENAME Line 5, column 10: Role name missing for type 'A'
 $FILENAME Line 6, column 15: Role name missing for type 'B'
 $FILENAME Line 7, column 5: The target of a directed relation cannot have a role name
 $FILENAME Line 8, column 5: The target of a directed relation may only have multiplicity 1
+$FILENAME Line 9, column 10: Role 'b2' is already declared for type 'A'
diff --git a/src/test/jastadd/errors/ErrorsLeft.relast b/src/test/jastadd/errors/ErrorsLeft.relast
index e381449..5c4c35a 100644
--- a/src/test/jastadd/errors/ErrorsLeft.relast
+++ b/src/test/jastadd/errors/ErrorsLeft.relast
@@ -6,3 +6,4 @@ rel B <- A;
 rel A.bs* <-> B*;
 rel B.b <- A.b;
 rel B* <- A.b2;
+rel A <- A.b2;
diff --git a/src/test/jastadd/errors/Inheritance.expected b/src/test/jastadd/errors/Inheritance.expected
index 717389a..37beedd 100644
--- a/src/test/jastadd/errors/Inheritance.expected
+++ b/src/test/jastadd/errors/Inheritance.expected
@@ -1,3 +1,3 @@
 Errors:
-$FILENAME Line 4, column 12: Component 'X' is an invalid redefition for type 'B3'
-$FILENAME Line 7, column 5: Component 'X' is an invalid redefition for type 'B2'
+$FILENAME Line 4, column 12: Component 'X' is an invalid redefinition for type 'B3'
+$FILENAME Line 7, column 5: Component 'X' is an invalid redefinition for type 'B2'
diff --git a/src/test/jastadd/errors/Inheritance.relast b/src/test/jastadd/errors/Inheritance.relast
index db746eb..e79e6b3 100644
--- a/src/test/jastadd/errors/Inheritance.relast
+++ b/src/test/jastadd/errors/Inheritance.relast
@@ -5,3 +5,4 @@ B3 : A ::= <X:String>;
 X;
 
 rel B2.X -> X;
+rel X.X -> B2;
diff --git a/src/test/jastadd/errors/InheritanceLeft.expected b/src/test/jastadd/errors/InheritanceLeft.expected
index 2cb8024..7dd3b7d 100644
--- a/src/test/jastadd/errors/InheritanceLeft.expected
+++ b/src/test/jastadd/errors/InheritanceLeft.expected
@@ -1,3 +1,3 @@
 Errors:
-$FILENAME Line 4, column 12: Component 'X' is an invalid redefition for type 'B3'
-$FILENAME Line 7, column 10: Component 'X' is an invalid redefition for type 'B2'
+$FILENAME Line 4, column 12: Component 'X' is an invalid redefinition for type 'B3'
+$FILENAME Line 7, column 10: Component 'X' is an invalid redefinition for type 'B2'
-- 
GitLab