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

staticThis

parent bf32e63a
No related branches found
No related tags found
No related merge requests found
...@@ -139,6 +139,13 @@ class MethodNode(ASTNode): ...@@ -139,6 +139,13 @@ class MethodNode(ASTNode):
if not self.body: if not self.body:
return return
# check no use of this in static method
if 'static' in self.mods:
names = getNameNodes(self.body)
for n in names:
if n.pointToThis and n.prefixLink.__class__.__name__ in ['MethodNode', 'FieldNode'] and 'static' not in n.prefixLink.mods:
raise Exception("ERROR: Cannot use non-static member {} in static method {} in class {}".format(n.name, self.name, self.typeName))
# With method body # With method body
returnNodes = getASTNode(["ReturnNode"], self.body) returnNodes = getASTNode(["ReturnNode"], self.body)
...@@ -177,3 +184,18 @@ def getForwardRefNames(node): ...@@ -177,3 +184,18 @@ def getForwardRefNames(node):
result.extend(getForwardRefNames(c)) result.extend(getForwardRefNames(c))
return result return result
# Input: AST Node
# Output: A list of names to be check
def getNameNodes(node):
if not node:
return []
if node.__class__.__name__ == 'NameNode':
return [node]
result = []
for c in node.children:
result.extend(getNameNodes(c))
return result
...@@ -34,6 +34,7 @@ class NameNode(ASTNode): ...@@ -34,6 +34,7 @@ class NameNode(ASTNode):
self.children = [] self.children = []
self.myType = None # will become TypeStruct to tell us what the whole is/returns self.myType = None # will become TypeStruct to tell us what the whole is/returns
self.shouldBeStatic = False self.shouldBeStatic = False
self.pointToThis = False
self.name = getParseTreeNodes(["ID", "COMPID"], parseTree)[0].lex self.name = getParseTreeNodes(["ID", "COMPID"], parseTree)[0].lex
...@@ -148,6 +149,7 @@ class NameNode(ASTNode): ...@@ -148,6 +149,7 @@ class NameNode(ASTNode):
def disambigName(self): def disambigName(self):
# Checking if a1 is "this" # Checking if a1 is "this"
if self.checkThis(): if self.checkThis():
self.pointToThis = True
return return
# Checking if a1 is a local variable # Checking if a1 is a local variable
...@@ -156,6 +158,7 @@ class NameNode(ASTNode): ...@@ -156,6 +158,7 @@ class NameNode(ASTNode):
# Checking if a1 is in contains set # Checking if a1 is in contains set
if self.checkContains(): if self.checkContains():
self.pointToThis = True
return return
......
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