Skip to content

Commit

Permalink
rename selectors when the conflict with other selectors
Browse files Browse the repository at this point in the history
  • Loading branch information
ivojawer committed Jun 29, 2023
1 parent db42fb8 commit a830705
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 1 deletion.
28 changes: 28 additions & 0 deletions smalltalksrc/Slang-Tests/SlangBasicTranslationTest.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ SlangBasicTranslationTest >> defineAtCompileTime: aString [
^ #(#OPTION1 #OPTION3 #OPTION) includes: aString
]

{ #category : #helpers }
SlangBasicTranslationTest >> functionIdentifier: aMethodTranslation [
| prototype matchIdentifier |

prototype := aMethodTranslation lineNumber: 3.

matchIdentifier := '([a-zA-Z0-9_])+\(' asRegex.

^((matchIdentifier matchesIn: prototype) first) copyWithout: $(
]

{ #category : #accessing }
SlangBasicTranslationTest >> generator [

Expand Down Expand Up @@ -1779,6 +1790,23 @@ methodWithoutReturn(void)
}'
]
{ #category : #'tests-method' }
SlangBasicTranslationTest >> testMethodsWithConflictingName [
| translation1 translation2 tMethod1 tMethod2 |
tMethod1 := self getTMethodFrom: #methodWithRepeatedCFunctionName.
tMethod2 := generator methods at: #methodWithRepeatedCFunctionName:.
generator renameConflictingMethods.
translation1 := self translate: tMethod1.
translation2 := self translate: tMethod2.
self assert: (self functionIdentifier: translation1)
~= (self functionIdentifier: translation2)
]
{ #category : #'tests-case' }
SlangBasicTranslationTest >> testNonContiguousCaseStatementWithSameBodyAreCollapsedInSameCase [
Expand Down
12 changes: 12 additions & 0 deletions smalltalksrc/Slang-Tests/SlangBasicTranslationTestClass.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,18 @@ SlangBasicTranslationTestClass >> methodWithOptionPragma [

]

{ #category : #'as yet unclassified' }
SlangBasicTranslationTestClass >> methodWithRepeatedCFunctionName [

^ true
]

{ #category : #'as yet unclassified' }
SlangBasicTranslationTestClass >> methodWithRepeatedCFunctionName: arg1 [

^ true
]

{ #category : #inline }
SlangBasicTranslationTestClass >> methodWithReservedWordLocal: register [

Expand Down
27 changes: 26 additions & 1 deletion smalltalksrc/Slang/CCodeGenerator.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,7 @@ CCodeGenerator >> emitCCodeOn: aStream doInlining: inlineFlag doAssertions: asse
"code generation"
self renameConflictingMethods.
self renameKeywordsConflicts.
"If we're outputting the VM put the main interpreter loop first for two reasons.
Expand Down Expand Up @@ -4415,6 +4416,30 @@ CCodeGenerator >> removeVariable: aName ifAbsent: ifAbsentBlock [
^variables remove: aName ifAbsent: ifAbsentBlock
]
{ #category : #translating }
CCodeGenerator >> renameConflictingMethods [
| cFunctionNames toRename |
cFunctionNames := IdentityDictionary new.
methods do: [ :m |
| functionName conflicts |
functionName := self cFunctionNameFor: m selector.
conflicts := cFunctionNames at: functionName ifAbsent: [ #( ) ].
cFunctionNames at: functionName put: (conflicts copyWith: m) ].
toRename := cFunctionNames select: [ :conflicts | conflicts size > 1 ].
toRename keysDo: [ :key |
(toRename at: key) do: [ :method |
method isStructAccessor ifFalse: [
self
addSelectorTranslation: method selector
to: key , method args size asString ] ] ]
]
{ #category : #translating }
CCodeGenerator >> renameKeywordsConflicts [
Expand All @@ -4430,7 +4455,7 @@ CCodeGenerator >> renameKeywordsConflicts [
method renameVariablesUsing: localRewording ].
"translate selector"
cSelector := self cSelectorName: method selector.
cSelector := self cFunctionNameFor: method selector.
(self reservedWords includes: cSelector) ifTrue: [
self
Expand Down

0 comments on commit a830705

Please sign in to comment.