From 5241f0bcffbb53161c934a48fb8040bfd53172fe Mon Sep 17 00:00:00 2001
From: Xun Yang <x299yang@uwaterloo.ca>
Date: Fri, 6 Mar 2020 21:22:00 -0500
Subject: [PATCH] check static

---
 ExprPrimaryNodes.py |  5 +++++
 NameNode.py         | 10 ++++++++--
 2 files changed, 13 insertions(+), 2 deletions(-)

diff --git a/ExprPrimaryNodes.py b/ExprPrimaryNodes.py
index d1dba9a..812bb17 100644
--- a/ExprPrimaryNodes.py
+++ b/ExprPrimaryNodes.py
@@ -484,6 +484,11 @@ class MethodInvNode(ASTNode):
         from pprint import pprint
 
         if m:
+            if self.ID.shouldBeStatic and (not 'static' in m.mods):
+                raise Exception("ERROR: Static access of non-static method {}.".format(m.name))
+            if (not self.ID.shouldBeStatic) and 'static' in m.mods:
+                raise Exception("ERROR: Non-static access of static method {}.".format(m.name))
+
             self.method = m
             self.myType = m.methodType.myType
             return
diff --git a/NameNode.py b/NameNode.py
index 141b789..3401352 100644
--- a/NameNode.py
+++ b/NameNode.py
@@ -33,6 +33,7 @@ class NameNode(ASTNode):
         self.typeName = typeName # the name of the class or interface that this node belongs under
         self.children = []
         self.myType = None # will become TypeStruct to tell us what the whole is/returns
+        self.shouldBeStatic = False
 
 
         self.name = getParseTreeNodes(["ID", "COMPID"], parseTree)[0].lex
@@ -119,11 +120,11 @@ class NameNode(ASTNode):
                 typeFieldNode = typeNode.env.getNode(staticFieldName, "fieldDcl")
                 if "static" in typeFieldNode.mods:
                     self.prefixLink = typeFieldNode.variableDcl
-                    
+
                     # if it is primitive, then we leave it as a VarDclNode
                     if not self.prefixLink.dclType.myType.isPrimitive:
                         self.prefixLink = self.prefixLink.dclType.myType.typePointer
-                    
+
                     self.prefix = currPrefix + "." + staticFieldName
                     self.IDs = self.IDs[index+2:]
                     return True
@@ -160,6 +161,7 @@ class NameNode(ASTNode):
 
         # Checking if the shortest prefix is a static field
         if self.checkStatic():
+            self.shouldBeStatic = True
             return
 
         raise Exception("ERROR at disambiguating namespace: no prefix of name {} is found in environment.".format(self.name))
@@ -193,6 +195,10 @@ class NameNode(ASTNode):
                 else:
                     curType = curType.env.getNode(self.IDs[0], 'fieldDcl')
 
+                # at this stage, all newly resolved field should be non static:
+                if curType.__class__.__name__ == 'FieldNode' and 'static' in curType.mods:
+                    raise Exception("ERROR: Non-static access of static field {}".format(curType.name))
+
                 self.prefix = self.prefix + "." + self.IDs[0]
                 self.IDs.pop(0)
 
-- 
GitLab