1
0

Compiler-Semantic.st 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. Smalltalk current createPackage: 'Compiler-Semantic' properties: #{}!
  2. Object subclass: #LexicalScope
  3. instanceVariableNames: 'node instruction temps args outerScope'
  4. package: 'Compiler-Semantic'!
  5. !LexicalScope commentStamp!
  6. I represent a lexical scope where variable names are associated with ScopeVars
  7. Instances are used for block scopes. Method scopes are instances of MethodLexicalScope.
  8. I am attached to a ScopeVar and method/block nodes.
  9. Each context (method/closure) get a fresh scope that inherits from its outer scope.!
  10. !LexicalScope methodsFor: 'accessing'!
  11. allVariableNames
  12. ^ self args keys, self temps keys
  13. !
  14. args
  15. ^ args ifNil: [ args := Dictionary new ]
  16. !
  17. bindingFor: aStringOrNode
  18. ^ self pseudoVars at: aStringOrNode value ifAbsent: [
  19. self args at: aStringOrNode value ifAbsent: [
  20. self temps at: aStringOrNode value ifAbsent: [ nil ]]]
  21. !
  22. instruction
  23. ^ instruction
  24. !
  25. instruction: anIRInstruction
  26. instruction := anIRInstruction
  27. !
  28. lookupVariable: aNode
  29. | lookup |
  30. lookup := (self bindingFor: aNode).
  31. lookup ifNil: [
  32. lookup := self outerScope ifNotNil: [
  33. (self outerScope lookupVariable: aNode) ]].
  34. ^ lookup
  35. !
  36. methodScope
  37. ^ self outerScope ifNotNil: [
  38. self outerScope methodScope ]
  39. !
  40. node
  41. "Answer the node in which I am defined"
  42. ^ node
  43. !
  44. node: aNode
  45. node := aNode
  46. !
  47. outerScope
  48. ^ outerScope
  49. !
  50. outerScope: aLexicalScope
  51. outerScope := aLexicalScope
  52. !
  53. pseudoVars
  54. ^ self methodScope pseudoVars
  55. !
  56. scopeLevel
  57. ^ (self outerScope
  58. ifNil: [ 0 ]
  59. ifNotNil: [ self outerScope scopeLevel ]) + 1
  60. !
  61. temps
  62. ^ temps ifNil: [ temps := Dictionary new ]
  63. ! !
  64. !LexicalScope methodsFor: 'adding'!
  65. addArg: aString
  66. self args at: aString put: (ArgVar on: aString).
  67. (self args at: aString) scope: self
  68. !
  69. addTemp: aString
  70. self temps at: aString put: (TempVar on: aString).
  71. (self temps at: aString) scope: self
  72. ! !
  73. !LexicalScope methodsFor: 'testing'!
  74. isMethodScope
  75. ^ false
  76. ! !
  77. LexicalScope subclass: #MethodLexicalScope
  78. instanceVariableNames: 'iVars unknownVariables localReturn nonLocalReturn'
  79. package: 'Compiler-Semantic'!
  80. !MethodLexicalScope commentStamp!
  81. I represent a method scope.!
  82. !MethodLexicalScope methodsFor: 'accessing'!
  83. allVariableNames
  84. ^ super allVariableNames, self iVars keys
  85. !
  86. bindingFor: aNode
  87. ^ (super bindingFor: aNode) ifNil: [
  88. self iVars at: aNode value ifAbsent: [ nil ]]
  89. !
  90. iVars
  91. ^ iVars ifNil: [ iVars := Dictionary new ]
  92. !
  93. localReturn
  94. ^ localReturn ifNil: [ false ]
  95. !
  96. localReturn: aBoolean
  97. localReturn := aBoolean
  98. !
  99. methodScope
  100. ^ self
  101. !
  102. nonLocalReturn
  103. ^ nonLocalReturn ifNil: [ false ]
  104. !
  105. nonLocalReturn: aBoolean
  106. nonLocalReturn := aBoolean
  107. !
  108. pseudoVars
  109. pseudoVars ifNil: [
  110. pseudoVars := Dictionary new.
  111. Smalltalk current pseudoVariableNames do: [ :each |
  112. pseudoVars at: each put: ((PseudoVar on: each)
  113. scope: self methodScope;
  114. yourself) ]].
  115. ^ pseudoVars
  116. !
  117. unknownVariables
  118. ^ unknownVariables ifNil: [ unknownVariables := OrderedCollection new ]
  119. ! !
  120. !MethodLexicalScope methodsFor: 'adding'!
  121. addIVar: aString
  122. self iVars at: aString put: (InstanceVar on: aString).
  123. (self iVars at: aString) scope: self
  124. ! !
  125. !MethodLexicalScope methodsFor: 'testing'!
  126. hasLocalReturn
  127. ^ self localReturn
  128. !
  129. hasNonLocalReturn
  130. ^ self nonLocalReturn
  131. !
  132. isMethodScope
  133. ^ true
  134. ! !
  135. Object subclass: #ScopeVar
  136. instanceVariableNames: 'scope name'
  137. package: 'Compiler-Semantic'!
  138. !ScopeVar commentStamp!
  139. I am an entry in a LexicalScope that gets associated with variable nodes of the same name.
  140. There are 4 different subclasses of vars: temp vars, local vars, args, and unknown/global vars.!
  141. !ScopeVar methodsFor: 'accessing'!
  142. alias
  143. ^ self name asVariableName
  144. !
  145. name
  146. ^ name
  147. !
  148. name: aString
  149. name := aString
  150. !
  151. scope
  152. ^ scope
  153. !
  154. scope: aScope
  155. scope := aScope
  156. ! !
  157. !ScopeVar methodsFor: 'testing'!
  158. isArgVar
  159. ^ false
  160. !
  161. isClassRefVar
  162. ^ false
  163. !
  164. isInstanceVar
  165. ^ false
  166. !
  167. isPseudoVar
  168. ^ false
  169. !
  170. isTempVar
  171. ^ false
  172. !
  173. isUnknownVar
  174. ^ false
  175. !
  176. validateAssignment
  177. (self isArgVar or: [ self isPseudoVar ]) ifTrue: [
  178. InvalidAssignmentError new
  179. variableName: self name;
  180. signal]
  181. ! !
  182. !ScopeVar class methodsFor: 'instance creation'!
  183. on: aString
  184. ^ self new
  185. name: aString;
  186. yourself
  187. ! !
  188. ScopeVar subclass: #AliasVar
  189. instanceVariableNames: 'node'
  190. package: 'Compiler-Semantic'!
  191. !AliasVar commentStamp!
  192. I am an internally defined variable by the compiler!
  193. !AliasVar methodsFor: 'accessing'!
  194. node
  195. ^ node
  196. !
  197. node: aNode
  198. node := aNode
  199. ! !
  200. ScopeVar subclass: #ArgVar
  201. instanceVariableNames: ''
  202. package: 'Compiler-Semantic'!
  203. !ArgVar commentStamp!
  204. I am an argument of a method or block.!
  205. !ArgVar methodsFor: 'testing'!
  206. isArgVar
  207. ^ true
  208. ! !
  209. ScopeVar subclass: #ClassRefVar
  210. instanceVariableNames: ''
  211. package: 'Compiler-Semantic'!
  212. !ClassRefVar commentStamp!
  213. I am an class reference variable!
  214. !ClassRefVar methodsFor: 'accessing'!
  215. alias
  216. ^ '(smalltalk.', self name, ' || ', self name, ')'
  217. ! !
  218. !ClassRefVar methodsFor: 'testing'!
  219. isClassRefVar
  220. ^ true
  221. ! !
  222. ScopeVar subclass: #InstanceVar
  223. instanceVariableNames: ''
  224. package: 'Compiler-Semantic'!
  225. !InstanceVar commentStamp!
  226. I am an instance variable of a method or block.!
  227. !InstanceVar methodsFor: 'testing'!
  228. alias
  229. ^ 'self["@', self name, '"]'
  230. !
  231. isInstanceVar
  232. ^ true
  233. ! !
  234. ScopeVar subclass: #PseudoVar
  235. instanceVariableNames: ''
  236. package: 'Compiler-Semantic'!
  237. !PseudoVar commentStamp!
  238. I am an pseudo variable.
  239. The five Smalltalk pseudo variables are: 'self', 'super', 'nil', 'true' and 'false'!
  240. !PseudoVar methodsFor: 'accessing'!
  241. alias
  242. ^ self name
  243. ! !
  244. !PseudoVar methodsFor: 'testing'!
  245. isPseudoVar
  246. ^ true
  247. ! !
  248. ScopeVar subclass: #TempVar
  249. instanceVariableNames: ''
  250. package: 'Compiler-Semantic'!
  251. !TempVar commentStamp!
  252. I am an temporary variable of a method or block.!
  253. !TempVar methodsFor: 'testing'!
  254. isTempVar
  255. ^ true
  256. ! !
  257. ScopeVar subclass: #UnknownVar
  258. instanceVariableNames: ''
  259. package: 'Compiler-Semantic'!
  260. !UnknownVar commentStamp!
  261. I am an unknown variable. Amber uses unknown variables as JavaScript globals!
  262. !UnknownVar methodsFor: 'testing'!
  263. isUnknownVar
  264. ^ true
  265. ! !
  266. NodeVisitor subclass: #SemanticAnalyzer
  267. instanceVariableNames: 'currentScope theClass classReferences messageSends'
  268. package: 'Compiler-Semantic'!
  269. !SemanticAnalyzer commentStamp!
  270. I semantically analyze the abstract syntax tree and annotate it with informations such as non local returns and variable scopes.!
  271. !SemanticAnalyzer methodsFor: 'accessing'!
  272. classReferences
  273. ^ classReferences ifNil: [ classReferences := Set new ]
  274. !
  275. messageSends
  276. ^ messageSends ifNil: [ messageSends := Set new ]
  277. !
  278. pseudoVariables
  279. ^#('self' 'super' 'true' 'false' 'nil' 'thisContext')
  280. !
  281. theClass
  282. ^ theClass
  283. !
  284. theClass: aClass
  285. theClass := aClass
  286. ! !
  287. !SemanticAnalyzer methodsFor: 'error handling'!
  288. errorShadowingVariable: aString
  289. ShadowingVariableError new
  290. variableName: aString;
  291. signal
  292. !
  293. errorUnknownVariable: aNode
  294. self allowUnknownVariables
  295. ifTrue: [ currentScope methodScope unknownVariables add: aNode value ]
  296. ifFalse: [
  297. UnknownVariableError new
  298. variableName: aNode value;
  299. signal ]
  300. ! !
  301. !SemanticAnalyzer methodsFor: 'factory'!
  302. newBlockScope
  303. ^ self newScopeOfClass: LexicalScope
  304. !
  305. newMethodScope
  306. ^ self newScopeOfClass: MethodLexicalScope
  307. !
  308. newScopeOfClass: aLexicalScopeClass
  309. ^ aLexicalScopeClass new
  310. outerScope: currentScope;
  311. yourself
  312. ! !
  313. !SemanticAnalyzer methodsFor: 'scope'!
  314. popScope
  315. currentScope ifNotNil: [
  316. currentScope := currentScope outerScope ]
  317. !
  318. pushScope: aScope
  319. aScope outerScope: currentScope.
  320. currentScope := aScope
  321. !
  322. validateVariableScope: aString
  323. "Validate the variable scope in by doing a recursive lookup, up to the method scope"
  324. (currentScope lookupVariable: aString) ifNotNil: [
  325. self errorShadowingVariable: aString ]
  326. ! !
  327. !SemanticAnalyzer methodsFor: 'testing'!
  328. allowUnknownVariables
  329. ^ true
  330. ! !
  331. !SemanticAnalyzer methodsFor: 'visiting'!
  332. visitAssignmentNode: aNode
  333. super visitAssignmentNode: aNode.
  334. aNode left beAssigned.
  335. aNode right beUsed
  336. !
  337. visitBlockNode: aNode
  338. self pushScope: self newBlockScope.
  339. aNode scope: currentScope.
  340. aNode parameters do: [ :each |
  341. self validateVariableScope: each.
  342. currentScope addArg: each ].
  343. super visitBlockNode: aNode.
  344. self popScope
  345. !
  346. visitCascadeNode: aNode
  347. "Populate the receiver into all children"
  348. aNode nodes do: [ :each |
  349. each receiver: aNode receiver ].
  350. super visitCascadeNode: aNode.
  351. aNode nodes first superSend ifTrue: [
  352. aNode nodes do: [ :each | each superSend: true ]]
  353. !
  354. visitClassReferenceNode: aNode
  355. self classReferences add: aNode value.
  356. aNode binding: (ClassRefVar new name: aNode value; yourself)
  357. !
  358. visitMethodNode: aNode
  359. self pushScope: self newMethodScope.
  360. aNode scope: currentScope.
  361. self theClass allInstanceVariableNames do: [:each |
  362. currentScope addIVar: each ].
  363. aNode arguments do: [ :each |
  364. self validateVariableScope: each.
  365. currentScope addArg: each ].
  366. super visitMethodNode: aNode.
  367. aNode
  368. classReferences: self classReferences;
  369. messageSends: self messageSends.
  370. self popScope
  371. !
  372. visitReturnNode: aNode
  373. currentScope isMethodScope
  374. ifTrue: [ currentScope localReturn: true ]
  375. ifFalse: [
  376. currentScope methodScope nonLocalReturn: true.
  377. aNode nonLocalReturn: true ].
  378. aNode nodes first beUsed.
  379. super visitReturnNode: aNode
  380. !
  381. visitSendNode: aNode
  382. aNode receiver value = 'super' ifTrue: [
  383. aNode superSend: true.
  384. aNode receiver value: 'self'].
  385. self messageSends add: aNode selector.
  386. aNode receiver ifNotNil: [
  387. aNode receiver beUsed ].
  388. aNode arguments do: [ :each |
  389. each beUsed ].
  390. super visitSendNode: aNode
  391. !
  392. visitSequenceNode: aNode
  393. aNode temps do: [ :each |
  394. self validateVariableScope: each.
  395. currentScope addTemp: each ].
  396. super visitSequenceNode: aNode
  397. !
  398. visitVariableNode: aNode
  399. "Bind a ScopeVar to aNode by doing a lookup in the current scope.
  400. If no ScopeVar is found, bind a UnknowVar and throw an error"
  401. aNode binding: ((currentScope lookupVariable: aNode) ifNil: [
  402. self errorUnknownVariable: aNode.
  403. UnknownVar new name: aNode value; yourself ])
  404. ! !
  405. !SemanticAnalyzer class methodsFor: 'instance creation'!
  406. on: aClass
  407. ^ self new
  408. theClass: aClass;
  409. yourself
  410. ! !