Skip to content
Snippets Groups Projects
Commit 7ea527ab authored by Nicholas Robinson's avatar Nicholas Robinson
Browse files

check protected

parent bf32e63a
No related branches found
No related tags found
No related merge requests found
from AST import ASTNode, getParseTreeNodes
from TheTypeNode import TypeStruct
from TheTypeNode import TypeStruct, getSupers
import MemberNodes
import TypeNodes
from Environment import Env
......@@ -127,7 +127,22 @@ class NameNode(ASTNode):
self.prefix = currPrefix + "." + staticFieldName
self.IDs = self.IDs[index+2:]
# if protected, check if we have access to it
if "protected" in typeFieldNode.mods:
# 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 False
elif self.methodInvoke and index+1 == len(self.IDs) - 1:
# TODO set the most recent? is this the correct implementation we want?
......@@ -196,8 +211,22 @@ class NameNode(ASTNode):
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))
if curType.__class__.__name__ == 'FieldNode':
if 'static' in curType.mods:
raise Exception("ERROR: Non-static access of static field {}".format(curType.name))
# if protected, check if we have access to it
if "protected" in curType.mods:
# 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.IDs.pop(0)
......
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