From 068d8f0b7eb3fa4c7734bee96b0715c14b25a35f Mon Sep 17 00:00:00 2001
From: Johannes Mey <johannes.mey@tu-dresden.de>
Date: Tue, 24 Dec 2019 12:36:24 +0100
Subject: [PATCH] fix wrong naming of shadower/shadowed, add printing with line
 numbers.

---
 scope/src/main/jastadd/Shadowing.jadd |  6 ++-
 scope/src/main/jastadd/Shadowing.jrag | 31 ++++++++++-----
 scope4j/src/main/jastadd/Shadow.jadd  | 55 +++++++++++++++++++++++++++
 scope4j/src/main/jastadd/Shadow.jrag  |  3 --
 testprograms/simpleScope/A.java       |  4 +-
 5 files changed, 84 insertions(+), 15 deletions(-)
 create mode 100644 scope4j/src/main/jastadd/Shadow.jadd
 delete mode 100644 scope4j/src/main/jastadd/Shadow.jrag

diff --git a/scope/src/main/jastadd/Shadowing.jadd b/scope/src/main/jastadd/Shadowing.jadd
index e9c2f2c..7f05f3e 100644
--- a/scope/src/main/jastadd/Shadowing.jadd
+++ b/scope/src/main/jastadd/Shadowing.jadd
@@ -17,7 +17,11 @@ aspect Shadowing {
     }
 
     public String toString() {
-      return "declaration '" + shadowed.getName() + "' is shadowed by '" + shadower.getName() + "'!";
+      if (shadower.sourceFile().equals(shadowed.sourceFile())) {
+        return shadower.sourceFile() + ": declaration '" + shadower + "' (l." + shadower.lineNumber() + ") is shadowing '" + shadowed + "' (l." + shadowed.lineNumber() + ")!";
+      } else {
+        return shadower.sourceFile() + ": declaration '" + shadower + "' (l." + shadower.lineNumber() + ") is shadowing '" + shadowed + "(' " + shadowed.sourceLocation() + ")!";
+      }
     }
   }
 }
diff --git a/scope/src/main/jastadd/Shadowing.jrag b/scope/src/main/jastadd/Shadowing.jrag
index dcf092f..4d20158 100644
--- a/scope/src/main/jastadd/Shadowing.jrag
+++ b/scope/src/main/jastadd/Shadowing.jrag
@@ -1,30 +1,37 @@
 aspect Shadowing {
   coll Set<VariableShadowFinding> ScopeTree.variableShadowings() [new HashSet<>()] with add root ScopeTree;
 
-  Declaration contributes new VariableShadowFinding(this, shadower()) when isShadowed() to ScopeTree.variableShadowings();
+  Declaration contributes new VariableShadowFinding(shadowed(), this) when isShadowing() to ScopeTree.variableShadowings();
 
-  syn Declaration Declaration.shadower() = shadower(this);
+  syn Declaration Declaration.shadowed() = shadowed(this);
 
-  inh Declaration Element.shadower(Declaration shadowed);
-  eq ScopeTree.getElement().shadower(Declaration shadowed) {
+  inh Declaration Element.shadowed(Declaration shadower);
+  eq ScopeTree.getElement().shadowed(Declaration shadower) {
     for (Declaration declaration : declarations()) {
-      if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) {
+      if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
         return declaration;
       }
     }
     return null;
   }
-  eq Scope.getElement().shadower(Declaration shadowed) {
+  eq Scope.getElement().shadowed(Declaration shadower) {
     for (Declaration declaration : declarations()) {
-      if (declaration != shadowed && declaration.getName().equals(shadowed.getName())) {
+      if (declaration != shadower && declaration.getName().equals(shadower.getName())) {
         return declaration;
       }
     }
-    return shadower(shadowed);
+    return shadowed(shadower);
   }
 
-  syn boolean Declaration.isShadowed() = shadower() != null;
+  syn boolean Declaration.isShadowing() = shadowed() != null;
 
+
+
+}
+
+aspect Statictics {
+
+  /** count all scopes in this subtree */
   syn int Scope.numScopes() {
     int result = 1;
     for (Element element : getElementList()) {
@@ -35,6 +42,7 @@ aspect Shadowing {
     return result;
   }
 
+  /** count all declarations in this subtree */
   syn int Scope.numDeclarations() {
     int result = 0;
     for (Element element : getElementList()) {
@@ -46,5 +54,10 @@ aspect Shadowing {
     }
     return result;
   }
+}
 
+aspect Printing {
+  syn String Element.toString();
+  eq Scope.toString() = "Scope";
+  eq Declaration.toString() = getName();
 }
diff --git a/scope4j/src/main/jastadd/Shadow.jadd b/scope4j/src/main/jastadd/Shadow.jadd
new file mode 100644
index 0000000..644a178
--- /dev/null
+++ b/scope4j/src/main/jastadd/Shadow.jadd
@@ -0,0 +1,55 @@
+aspect Shadow{
+    eq TypeDeclScope.toString() = getTypeDecl().getClass().getSimpleName() + "-Scope";
+    eq BlockScope.toString() = getBlock().getClass().getSimpleName() + "-Scope";
+    eq ForStmtScope.toString() = getForStmt().getClass().getSimpleName() + "-Scope";
+    eq EnhancedForStmtScope.toString() = getEnhancedForStmt().getClass().getSimpleName() + "-Scope";
+
+    eq JavaDeclaration.toString() = getDeclarator().getClass().getSimpleName() + ":" + super.toString();
+    eq JavaParameterDeclaration.toString() = getParameterDeclaration().getClass().getSimpleName() + ":" + super.toString();
+    eq JavaInferredLambdaParameterDeclaration.toString() = getInferredParameterDeclaration().getClass().getSimpleName() + ":" + super.toString();
+
+    public int TypeDeclScope.lineNumber(){
+      return getTypeDecl().lineNumber();
+    }
+    public int BlockScope.lineNumber(){
+      return getBlock().lineNumber();
+    }
+    public int ForStmtScope.lineNumber(){
+      return getForStmt().lineNumber();
+    }
+    public int EnhancedForStmtScope.lineNumber(){
+      return getEnhancedForStmt().lineNumber();
+    }
+    public int JavaDeclaration.lineNumber() {
+      return getDeclarator().lineNumber();
+    }
+    public int JavaParameterDeclaration.lineNumber() {
+      return getParameterDeclaration().lineNumber();
+    }
+    public int JavaInferredLambdaParameterDeclaration.lineNumber() {
+      return getInferredParameterDeclaration().lineNumber();
+    }
+
+    public String TypeDeclScope.sourceFile() {
+      return getTypeDecl().sourceFile();
+    }
+    public String BlockScope.sourceFile() {
+      return getBlock().sourceFile();
+    }
+    public String ForStmtScope.sourceFile() {
+      return getForStmt().sourceFile();
+    }
+    public String EnhancedForStmtScope.sourceFile() {
+      return getEnhancedForStmt().sourceFile();
+    }
+    public String JavaDeclaration.sourceFile() {
+      return getDeclarator().sourceFile();
+    }
+    public String JavaParameterDeclaration.sourceFile() {
+      return getParameterDeclaration().sourceFile();
+    }
+    public String JavaInferredLambdaParameterDeclaration.sourceFile() {
+      return getInferredParameterDeclaration().sourceFile();
+    }
+
+}
diff --git a/scope4j/src/main/jastadd/Shadow.jrag b/scope4j/src/main/jastadd/Shadow.jrag
deleted file mode 100644
index 4052de8..0000000
--- a/scope4j/src/main/jastadd/Shadow.jrag
+++ /dev/null
@@ -1,3 +0,0 @@
-aspect Shadow {
-
-}
diff --git a/testprograms/simpleScope/A.java b/testprograms/simpleScope/A.java
index bed8e30..1647111 100644
--- a/testprograms/simpleScope/A.java
+++ b/testprograms/simpleScope/A.java
@@ -17,7 +17,7 @@ public abstract class ClassA {
     {
       int localVarInBlockA = 2;
 
-      // this is shadowed (and forbidden)
+      // this is shadowing (and forbidden)
       int localVarA = 3;
     }
 
@@ -27,7 +27,7 @@ public abstract class ClassA {
       }
     }
 
-    // this is shadowed (over two levels, not forbidden)
+    // this is shadowing (over two levels, not forbidden)
     int fieldA;
 
     try (
-- 
GitLab