Skip to content
Snippets Groups Projects
Commit aa1b51a3 authored by Xun Yang's avatar Xun Yang
Browse files

protected method check

parent 8c9f9e70
No related branches found
No related tags found
No related merge requests found
...@@ -2,7 +2,7 @@ from AST import ASTNode, getParseTreeNodes ...@@ -2,7 +2,7 @@ from AST import ASTNode, getParseTreeNodes
from Environment import Env from Environment import Env
from UnitNodes import LiteralNode from UnitNodes import LiteralNode
from TheTypeNode import TypeNode, TypeStruct from TheTypeNode import TypeNode, TypeStruct
from NameNode import NameNode from NameNode import NameNode, checkProtected
# file containing smaller (lower level nodes) in the AST # file containing smaller (lower level nodes) in the AST
# nodes in this file: # nodes in this file:
...@@ -388,7 +388,7 @@ class ExprNode(ASTNode): ...@@ -388,7 +388,7 @@ class ExprNode(ASTNode):
self.myType = TypeStruct('java.lang.String', self.env.getNode('java.lang.String', 'type')) self.myType = TypeStruct('java.lang.String', self.env.getNode('java.lang.String', 'type'))
self.myType.link(self.env) self.myType.link(self.env)
return return
raise Exception("ERROR: Incompatible types. Left of {} type can't be used with right of {} type on operation {}".format(self.left.myType.name, self.right.myType.name, self.op)) raise Exception("ERROR: Incompatible types. Left of {} type can't be used with right of {} type on operation {}".format(self.left.myType.name, self.right.myType.name, self.op))
...@@ -425,6 +425,9 @@ class FieldAccessNode(ASTNode): ...@@ -425,6 +425,9 @@ class FieldAccessNode(ASTNode):
self.ID.checkType() self.ID.checkType()
self.myType = self.ID.myType self.myType = self.ID.myType
# check protected
if "protected" in self.ID.prefixLink.mods:
checkProtected(self.ID.prefixLink, self)
################################################################################### ###################################################################################
...@@ -484,11 +487,16 @@ class MethodInvNode(ASTNode): ...@@ -484,11 +487,16 @@ class MethodInvNode(ASTNode):
from pprint import pprint from pprint import pprint
if m: if m:
# check static
if self.ID.shouldBeStatic and (not 'static' in m.mods): if self.ID.shouldBeStatic and (not 'static' in m.mods):
raise Exception("ERROR: Static access of non-static method {}.".format(m.name)) raise Exception("ERROR: Static access of non-static method {}.".format(m.name))
if (not self.ID.shouldBeStatic) and 'static' in m.mods: if (not self.ID.shouldBeStatic) and 'static' in m.mods:
raise Exception("ERROR: Non-static access of static method {}.".format(m.name)) raise Exception("ERROR: Non-static access of static method {}.".format(m.name))
# check protected
if "protected" in m.mods:
checkProtected(m, self)
self.method = m self.method = m
self.myType = m.methodType.myType self.myType = m.methodType.myType
return return
......
...@@ -131,16 +131,7 @@ class NameNode(ASTNode): ...@@ -131,16 +131,7 @@ class NameNode(ASTNode):
# if protected, check if we have access to it # if protected, check if we have access to it
if "protected" in typeFieldNode.mods: if "protected" in typeFieldNode.mods:
checkProtected(typeFieldNode, self)
# get typeFieldNode's class it was declared in
typeFieldNodeClass = typeFieldNode.env.getNode(typeFieldNode.typeName, "type").canonName
# get current class we're in
curClass = self.env.getNode(self.typeName, "type")
# check to see if typeFieldNode's class is in the current class' super list
if typeFieldNodeClass != curClass.canonName and typeFieldNodeClass not in getSupers(curClass):
raise Exception("ERROR: Class {} is attempting to access a protected field from class {}".format(curClass.canonName, typeFieldNodeClass))
return True return True
...@@ -220,16 +211,7 @@ class NameNode(ASTNode): ...@@ -220,16 +211,7 @@ class NameNode(ASTNode):
# if protected, check if we have access to it # if protected, check if we have access to it
if "protected" in curType.mods: if "protected" in curType.mods:
checkProtected(curType, self)
# get curType's class it was declared in
typeFieldNodeClass = curType.env.getNode(curType.typeName, "type").canonName
# get current class we're in
curClass = self.env.getNode(self.typeName, "type")
# check to see if curType's class is in the current class' super list
if typeFieldNodeClass != curClass.canonName and typeFieldNodeClass not in getSupers(curClass):
raise Exception("ERROR: Class {} is attempting to access a protected field from class {}".format(curClass.canonName, typeFieldNodeClass))
self.prefix = self.prefix + "." + self.IDs[0] self.prefix = self.prefix + "." + self.IDs[0]
self.IDs.pop(0) self.IDs.pop(0)
...@@ -251,3 +233,15 @@ class NameNode(ASTNode): ...@@ -251,3 +233,15 @@ class NameNode(ASTNode):
# pprint(vars(self.prefixLink)) # pprint(vars(self.prefixLink))
# pprint(vars(self)) # pprint(vars(self))
raise Exception("ERROR: Cannot check type of name {}".format(self.name)) raise Exception("ERROR: Cannot check type of name {}".format(self.name))
# helper
def checkProtected(dcl, usage):
# get curType's class it was declared in
typeFieldNodeClass = dcl.env.getNode(dcl.typeName, "type")
# get current class we're in
curClass = usage.env.getNode(usage.typeName, "type")
# check to see if curType's class is in the current class' super list
if typeFieldNodeClass.canonName != curClass.canonName and typeFieldNodeClass.canonName not in getSupers(curClass) and typeFieldNodeClass.packageName != curClass.packageName:
raise Exception("ERROR: Class {} is attempting to access a protected field from class {}".format(curClass.canonName, typeFieldNodeClass.canonName))
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment