Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cs444
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Xun Yang
cs444
Commits
aa1b51a3
Commit
aa1b51a3
authored
5 years ago
by
Xun Yang
Browse files
Options
Downloads
Patches
Plain Diff
protected method check
parent
8c9f9e70
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
ExprPrimaryNodes.py
+10
-2
10 additions, 2 deletions
ExprPrimaryNodes.py
NameNode.py
+14
-20
14 additions, 20 deletions
NameNode.py
with
24 additions
and
22 deletions
ExprPrimaryNodes.py
+
10
−
2
View file @
aa1b51a3
...
...
@@ -2,7 +2,7 @@ from AST import ASTNode, getParseTreeNodes
from
Environment
import
Env
from
UnitNodes
import
LiteralNode
from
TheTypeNode
import
TypeNode
,
TypeStruct
from
NameNode
import
NameNode
from
NameNode
import
NameNode
,
checkProtected
# file containing smaller (lower level nodes) in the AST
# nodes in this file:
...
...
@@ -388,7 +388,7 @@ class ExprNode(ASTNode):
self
.
myType
=
TypeStruct
(
'
java.lang.String
'
,
self
.
env
.
getNode
(
'
java.lang.String
'
,
'
type
'
))
self
.
myType
.
link
(
self
.
env
)
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
))
...
...
@@ -425,6 +425,9 @@ class FieldAccessNode(ASTNode):
self
.
ID
.
checkType
()
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):
from
pprint
import
pprint
if
m
:
# check static
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
))
# check protected
if
"
protected
"
in
m
.
mods
:
checkProtected
(
m
,
self
)
self
.
method
=
m
self
.
myType
=
m
.
methodType
.
myType
return
...
...
This diff is collapsed.
Click to expand it.
NameNode.py
+
14
−
20
View file @
aa1b51a3
...
...
@@ -131,16 +131,7 @@ class NameNode(ASTNode):
# 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
))
checkProtected
(
typeFieldNode
,
self
)
return
True
...
...
@@ -220,16 +211,7 @@ class NameNode(ASTNode):
# 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
))
checkProtected
(
curType
,
self
)
self
.
prefix
=
self
.
prefix
+
"
.
"
+
self
.
IDs
[
0
]
self
.
IDs
.
pop
(
0
)
...
...
@@ -251,3 +233,15 @@ class NameNode(ASTNode):
# pprint(vars(self.prefixLink))
# pprint(vars(self))
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
))
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment