Skip to content

Commit

Permalink
add a query method to LocalVariable #declaringNode. It returns the AS…
Browse files Browse the repository at this point in the history
…T node that declared the variables (that is, the argument in the method signature, the block arg or the RBVariable that is in the | | temp definition in the AST.

- remove #definingScope as it is the same as scope (and just used in tests)
- add Tests
  • Loading branch information
MarcusDenker committed Jul 14, 2020
1 parent 1621ee2 commit 845bf7b
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 22 deletions.
5 changes: 0 additions & 5 deletions src/Deprecated90/OCAbstractLocalVariable.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,6 @@ OCAbstractLocalVariable >> asString [
^ self name
]

{ #category : #accessing }
OCAbstractLocalVariable >> definingScope [
^ scope
]

{ #category : #'read/write usage' }
OCAbstractLocalVariable >> isRead [
^usage = #read
Expand Down
5 changes: 0 additions & 5 deletions src/Deprecated90/OCAbstractVariable.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ OCAbstractVariable >> asString [
^ self name
]

{ #category : #accessing }
OCAbstractVariable >> definingScope [
^ scope
]

{ #category : #'read/write usage' }
OCAbstractVariable >> isRead [
^usage = #read
Expand Down
5 changes: 5 additions & 0 deletions src/OpalCompiler-Core/ArgumentVariable.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ ArgumentVariable class >> semanticNodeClass [
^RBArgumentNode
]

{ #category : #queries }
ArgumentVariable >> declaringNode [
^ scope node arguments detect: [ :each | each name = name ]
]

{ #category : #testing }
ArgumentVariable >> isArgumentVariable [
^ true
Expand Down
8 changes: 4 additions & 4 deletions src/OpalCompiler-Core/LocalVariable.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ LocalVariable >> astNodes [
^scope node methodNode variableNodes select: [ :each | each binding == self]
]

{ #category : #accessing }
LocalVariable >> definingScope [
^ scope
{ #category : #queries }
LocalVariable >> declaringNode [
^ self subclassResponsibility
]

{ #category : #emitting }
Expand Down Expand Up @@ -245,7 +245,7 @@ LocalVariable >> readInContext: aContext [

{ #category : #accessing }
LocalVariable >> scope [

"this is the scope that the variable is declared in"
^ scope
]

Expand Down
5 changes: 5 additions & 0 deletions src/OpalCompiler-Core/TemporaryVariable.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ TemporaryVariable class >> semanticNodeClass [
^RBTemporaryNode
]

{ #category : #queries }
TemporaryVariable >> declaringNode [
^ scope node temporaries detect: [ :each | each name = name ]
]

{ #category : #testing }
TemporaryVariable >> isTemp [

Expand Down
10 changes: 5 additions & 5 deletions src/OpalCompiler-Tests/OCASTCheckerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ OCASTCheckerTest >> testOptimizedBlockWrittenAfterClosedOverCase1 [

"index is not escaping as it is (due to the optimized block) defined in the same block"
self deny: (ast scope lookupVar: 'index') isEscaping.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.

scopes := (OCScopesCollector new visitNode: ast) scopes.

Expand All @@ -216,7 +216,7 @@ OCASTCheckerTest >> testOptimizedBlockWrittenAfterClosedOverCase2 [
self assert: ast scope tempVars size equals: 1.

self deny: (ast scope lookupVar: 'index') isEscaping.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.

scopes := (OCScopesCollector new visitNode: ast) scopes.

Expand Down Expand Up @@ -302,7 +302,7 @@ OCASTCheckerTest >> testSingleRemoteTempVar [
self assert: ast scope tempVars size equals: 3.
self assert: (ast scope lookupVar: 'index') isEscaping.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isEscaping.
self assert: (ast scope lookupVar: 'theCollection') isEscaping.
self assert: (ast scope lookupVar: 'block') isTemp.
Expand All @@ -321,7 +321,7 @@ OCASTCheckerTest >> testsingleRemoteTempVarWhileWithTempNotInlined [
self assert: ast scope tempVars size equals: 2.
self assert: (ast scope lookupVar: 'index') isEscaping.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isEscaping.
self assert: (ast scope lookupVar: 'block') isTemp
]
Expand All @@ -335,7 +335,7 @@ OCASTCheckerTest >> testsingleRemoteTempVarWrittenAfterClosedOver [
self assert: ast scope tempVars size equals: 2.
self assert: (ast scope lookupVar: 'index') isWrite.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isEscaping.
self assert: (ast scope lookupVar: 'block') isTemp
]
6 changes: 3 additions & 3 deletions src/OpalCompiler-Tests/OCASTClosureAnalyzerTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ OCASTClosureAnalyzerTest >> testSingleRemoteTempVar [
self assert: ast scope tempVector size equals: 1.

self assert: (ast scope lookupVar: 'index') isRemote.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isRemote.
self deny: (ast scope lookupVar: 'theCollection') isRemote
]
Expand Down Expand Up @@ -502,7 +502,7 @@ OCASTClosureAnalyzerTest >> testsingleRemoteTempVarWhileWithTempNotInlined [
self assert: ast scope tempVector size equals: 1.

self assert: (ast scope lookupVar: 'index') isRemote.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isRemote
]

Expand All @@ -516,6 +516,6 @@ OCASTClosureAnalyzerTest >> testsingleRemoteTempVarWrittenAfterClosedOver [
self assert: ast scope tempVector size equals: 1.

self assert: (ast scope lookupVar: 'index') isRemote.
self assert: (ast scope lookupVar: 'index') definingScope equals: ast scope.
self assert: (ast scope lookupVar: 'index') scope equals: ast scope.
self deny: (ast scope lookupVar: 'block') isRemote
]
20 changes: 20 additions & 0 deletions src/Slot-Tests/ArgumentVariableTest.class.st
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Class {
#name : #ArgumentVariableTest,
#superclass : #TestCase,
#category : #'Slot-Tests-VariablesAndSlots'
}

{ #category : #tests }
ArgumentVariableTest >> testDeclaringNode [
| method declaringNode declaringNodeViaVariable|

method := OrderedCollection >> #do:.
declaringNode := method ast arguments first.
declaringNodeViaVariable := method ast variableReadNodes third variable variable declaringNode.
self assert: declaringNodeViaVariable equals: declaringNode.

"check block argument"
declaringNode := method blockNodes first arguments first.
declaringNodeViaVariable := method ast variableReadNodes fifth variable variable declaringNode.
self assert: declaringNodeViaVariable equals: declaringNode.
]
14 changes: 14 additions & 0 deletions src/Slot-Tests/TemporaryVariableTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,20 @@ Class {
#category : #'Slot-Tests-VariablesAndSlots'
}

{ #category : #tests }
TemporaryVariableTest >> testDeclaringNode [
| method declaringNode declaringNodeViaVariable|
method := self class >> #testTemporaryVariablesMethod.
declaringNode := method ast body temporaries first.
declaringNodeViaVariable := method assignmentNodes first variable variable declaringNode.
self assert: declaringNodeViaVariable equals: declaringNode.

method := OrderedCollection >> #do:.
declaringNode := method ast arguments first.
declaringNodeViaVariable := method ast variableReadNodes third variable variable declaringNode.
self assert: declaringNodeViaVariable equals: declaringNode.
]

{ #category : #tests }
TemporaryVariableTest >> testHasTemporaryVariablesBlock [
| block |
Expand Down

0 comments on commit 845bf7b

Please sign in to comment.