Skip to content

Commit

Permalink
Merge pull request pharo-project#6655 from dionisiydk/specialVarRefactor
Browse files Browse the repository at this point in the history
Rename special variables into reserved variables
  • Loading branch information
MarcusDenker committed Jun 23, 2020
2 parents 7544b03 + 8c7eaa3 commit f36e381
Show file tree
Hide file tree
Showing 18 changed files with 213 additions and 29 deletions.
Expand Up @@ -4,7 +4,7 @@ I model self variables
Class {
#name : #OCSelfVariable,
#superclass : #OCSpecialVariable,
#category : #'OpalCompiler-Core-Semantics'
#category : #'Deprecated90-OpalCompiler-Core'
}

{ #category : #accessing }
Expand Down
Expand Up @@ -4,7 +4,7 @@ I model self, thisContext and super
Class {
#name : #OCSpecialVariable,
#superclass : #OCAbstractLocalVariable,
#category : #'OpalCompiler-Core-Semantics'
#category : #'Deprecated90-OpalCompiler-Core'
}

{ #category : #emitting }
Expand Down
Expand Up @@ -5,5 +5,5 @@ Error when trying to assign to a special Variable.
Class {
#name : #OCStoreIntoSpecialVariableError,
#superclass : #OCSemanticError,
#category : #'OpalCompiler-Core-Exception'
#category : #'Deprecated90-OpalCompiler-Core'
}
Expand Up @@ -4,7 +4,7 @@ I model super variables
Class {
#name : #OCSuperVariable,
#superclass : #OCSpecialVariable,
#category : #'OpalCompiler-Core-Semantics'
#category : #'Deprecated90-OpalCompiler-Core'
}

{ #category : #accessing }
Expand Down
Expand Up @@ -4,7 +4,7 @@ I model thisContext variables
Class {
#name : #OCThisContextVariable,
#superclass : #OCSpecialVariable,
#category : #'OpalCompiler-Core-Semantics'
#category : #'Deprecated90-OpalCompiler-Core'
}

{ #category : #accessing }
Expand Down
10 changes: 10 additions & 0 deletions src/Deprecated90/RBVariableNode.extension.st
@@ -0,0 +1,10 @@
Extension { #name : #RBVariableNode }

{ #category : #'*Deprecated90' }
RBVariableNode >> isSpecialVariable [
self
deprecated: 'Use #isReservedVariable instead.'
transformWith: '`@receiver isSpecialVariable' -> '`@receiver isReservedVariable'.

^ self isReservedVariable
]
9 changes: 9 additions & 0 deletions src/Deprecated90/Variable.extension.st
@@ -1,5 +1,14 @@
Extension { #name : #Variable }

{ #category : #'*Deprecated90' }
Variable >> isSpecialVariable [
self
deprecated: 'Use #isReservedVariable instead.'
transformWith: '`@receiver isSpecialVariable' -> '`@receiver isReservedVariable'.

^ self isReservedVariable
]

{ #category : #'*Deprecated90' }
Variable >> variable [

Expand Down
12 changes: 6 additions & 6 deletions src/Kernel/Variable.class.st
Expand Up @@ -130,19 +130,19 @@ Variable >> isReferenced [
]

{ #category : #testing }
Variable >> isSelf [
Variable >> isReservedVariable [
^false
]

{ #category : #testing }
Variable >> isSelfOrSuper [

^ false
Variable >> isSelf [
^false
]

{ #category : #testing }
Variable >> isSpecialVariable [
^false
Variable >> isSelfOrSuper [

^ self isSelf or: [ self isSuper ]
]

{ #category : #testing }
Expand Down
6 changes: 3 additions & 3 deletions src/OpalCompiler-Core/OCASTSemanticAnalyzer.class.st
Expand Up @@ -99,7 +99,7 @@ OCASTSemanticAnalyzer >> lookupVariableForWrite: aVariableNode [
var := scope lookupVar: aVariableNode name.

var ifNil: [^var].
var isSpecialVariable ifTrue: [ self storeIntoSpecialVariable: aVariableNode ].
var isReservedVariable ifTrue: [ self storeIntoReservedVariable: aVariableNode ].
var isWritable ifFalse: [ self storeIntoReadOnlyVariable: aVariableNode ].
var isTemp ifTrue: [ self analyseEscapingWrite: var ].
^var
Expand All @@ -122,9 +122,9 @@ OCASTSemanticAnalyzer >> storeIntoReadOnlyVariable: variableNode [
]

{ #category : #'error handling' }
OCASTSemanticAnalyzer >> storeIntoSpecialVariable: variableNode [
OCASTSemanticAnalyzer >> storeIntoReservedVariable: variableNode [
compilationContext optionSkipSemanticWarnings ifTrue: [ ^ self ].
^ OCStoreIntoSpecialVariableError new
^ OCStoreIntoReservedVariableError new
node: variableNode;
compilationContext: compilationContext;
messageText: 'Cannot store into';
Expand Down
4 changes: 1 addition & 3 deletions src/OpalCompiler-Core/OCAbstractMethodScope.class.st
Expand Up @@ -120,9 +120,7 @@ OCAbstractMethodScope >> initialize [
copiedVars := Dictionary new.
id := 0.

thisContextVar := OCThisContextVariable new
scope: self; yourself.

thisContextVar := ThisContextVariable instance
]

{ #category : #'temp vars' }
Expand Down
8 changes: 2 additions & 6 deletions src/OpalCompiler-Core/OCInstanceScope.class.st
Expand Up @@ -32,12 +32,8 @@ OCInstanceScope >> hasBindingThatBeginsWith: aString [
OCInstanceScope >> initialize [

vars := Dictionary new.
selfVar := OCSelfVariable new
scope: self;
yourself.
superVar := OCSuperVariable new
scope: self;
yourself.
selfVar := SelfVariable instance.
superVar := SuperVariable instance
]

{ #category : #acessing }
Expand Down
@@ -0,0 +1,5 @@
Class {
#name : #OCStoreIntoReservedVariableError,
#superclass : #OCSemanticError,
#category : #'OpalCompiler-Core-Exception'
}
6 changes: 3 additions & 3 deletions src/OpalCompiler-Core/RBVariableNode.extension.st
Expand Up @@ -24,7 +24,7 @@ RBVariableNode >> isArgOrTemp [

{ #category : #'*opalcompiler-core' }
RBVariableNode >> isClean [
^ (self isInstance | self isSpecialVariable) not
^ (self isInstance | self isReservedVariable) not
]

{ #category : #'*opalcompiler-core' }
Expand All @@ -38,8 +38,8 @@ RBVariableNode >> isInstance [
]

{ #category : #'*opalcompiler-core' }
RBVariableNode >> isSpecialVariable [
^ self binding isSpecialVariable
RBVariableNode >> isReservedVariable [
^ self binding isReservedVariable
]

{ #category : #'*opalcompiler-core' }
Expand Down
55 changes: 55 additions & 0 deletions src/OpalCompiler-Core/ReservedVariable.class.st
@@ -0,0 +1,55 @@
"
I model self, thisContext and super
"
Class {
#name : #ReservedVariable,
#superclass : #Variable,
#classInstVars : [
'instance'
],
#category : #'OpalCompiler-Core-Semantics'
}

{ #category : #accessing }
ReservedVariable class >> instance [
^instance ifNil: [ instance := self new ]
]

{ #category : #testing }
ReservedVariable class >> isAbstract [
^self = ReservedVariable
]

{ #category : #converting }
ReservedVariable >> asString [

^ self name
]

{ #category : #'code generation' }
ReservedVariable >> emitStore: methodBuilder [

self shouldNotImplement
]

{ #category : #testing }
ReservedVariable >> isReservedVariable [
^true
]

{ #category : #testing }
ReservedVariable >> isWritable [
^ false
]

{ #category : #printing }
ReservedVariable >> printOn: stream [

stream nextPutAll: self name
]

{ #category : #debugging }
ReservedVariable >> write: aValue inContext: aContext [

self error: name, ' is reserved word and cant be modified'
]
37 changes: 37 additions & 0 deletions src/OpalCompiler-Core/SelfVariable.class.st
@@ -0,0 +1,37 @@
"
I model ""self"" keyword
"
Class {
#name : #SelfVariable,
#superclass : #ReservedVariable,
#category : #'OpalCompiler-Core-Semantics'
}

{ #category : #accessing }
SelfVariable class >> semanticNodeClass [

^RBSelfNode
]

{ #category : #emitting }
SelfVariable >> emitValue: methodBuilder [

methodBuilder pushReceiver
]

{ #category : #initialization }
SelfVariable >> initialize [
super initialize.

name := 'self'
]

{ #category : #testing }
SelfVariable >> isSelf [
^true
]

{ #category : #debugging }
SelfVariable >> readInContext: aContext [
^aContext receiver
]
37 changes: 37 additions & 0 deletions src/OpalCompiler-Core/SuperVariable.class.st
@@ -0,0 +1,37 @@
"
I model ""super"" keyword
"
Class {
#name : #SuperVariable,
#superclass : #ReservedVariable,
#category : #'OpalCompiler-Core-Semantics'
}

{ #category : #accessing }
SuperVariable class >> semanticNodeClass [

^RBSuperNode
]

{ #category : #emitting }
SuperVariable >> emitValue: methodBuilder [
"super references the receiver, send that follows is a super send (the message lookup starts in the superclass)"
methodBuilder pushReceiver
]

{ #category : #initialization }
SuperVariable >> initialize [
super initialize.

name := 'super'
]

{ #category : #testing }
SuperVariable >> isSuper [
^true
]

{ #category : #debugging }
SuperVariable >> readInContext: aContext [
^aContext receiver
]
37 changes: 37 additions & 0 deletions src/OpalCompiler-Core/ThisContextVariable.class.st
@@ -0,0 +1,37 @@
"
I model thisContext keyword
"
Class {
#name : #ThisContextVariable,
#superclass : #ReservedVariable,
#category : #'OpalCompiler-Core-Semantics'
}

{ #category : #accessing }
ThisContextVariable class >> semanticNodeClass [

^RBThisContextNode
]

{ #category : #emitting }
ThisContextVariable >> emitValue: methodBuilder [

methodBuilder pushThisContext
]

{ #category : #initialization }
ThisContextVariable >> initialize [
super initialize.

name := 'thisContext'
]

{ #category : #testing }
ThisContextVariable >> isThisContext [
^true
]

{ #category : #debugging }
ThisContextVariable >> readInContext: aContext [
^aContext
]
6 changes: 3 additions & 3 deletions src/OpalCompiler-Tests/OCASTVariableTranslatorTest.class.st
Expand Up @@ -38,14 +38,14 @@ OCASTVariableTranslatorTest >> testAssignInstanceVariable [
OCASTVariableTranslatorTest >> testAssignSelfVariable [
self
should: [self compileSource: 'foo
self := 17'] raise: OCStoreIntoSpecialVariableError
self := 17'] raise: OCStoreIntoReservedVariableError
]

{ #category : #'testing - variables' }
OCASTVariableTranslatorTest >> testAssignSuperVariable [
self
should: [self compileSource: 'foo
super := 17'] raise: OCStoreIntoSpecialVariableError
super := 17'] raise: OCStoreIntoReservedVariableError
]

{ #category : #'testing - variables' }
Expand All @@ -59,7 +59,7 @@ OCASTVariableTranslatorTest >> testAssignTemporaryVariable [
OCASTVariableTranslatorTest >> testAssignThisContextVariable [
self
should: [self compileSource: 'foo
thisContext := 17'] raise: OCStoreIntoSpecialVariableError
thisContext := 17'] raise: OCStoreIntoReservedVariableError
]

{ #category : #'testing - variables' }
Expand Down

0 comments on commit f36e381

Please sign in to comment.