1
0

Compiler-Semantic.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726
  1. Smalltalk createPackage: 'Compiler-Semantic'!
  2. Object subclass: #LexicalScope
  3. instanceVariableNames: 'node instruction temps args outerScope blockIndex'
  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. alias
  12. ^ '$ctx', self scopeLevel asString
  13. !
  14. allVariableNames
  15. ^ self args keys, self temps keys
  16. !
  17. args
  18. ^ args ifNil: [ args := Dictionary new ]
  19. !
  20. bindingFor: aStringOrNode
  21. ^ self pseudoVars at: aStringOrNode value ifAbsent: [
  22. self args at: aStringOrNode value ifAbsent: [
  23. self temps at: aStringOrNode value ifAbsent: [ nil ]]]
  24. !
  25. blockIndex
  26. ^ blockIndex ifNil: [ 0 ]
  27. !
  28. blockIndex: anInteger
  29. blockIndex := anInteger
  30. !
  31. instruction
  32. ^ instruction
  33. !
  34. instruction: anIRInstruction
  35. instruction := anIRInstruction
  36. !
  37. lookupVariable: aNode
  38. | lookup |
  39. lookup := (self bindingFor: aNode).
  40. lookup ifNil: [
  41. lookup := self outerScope ifNotNil: [
  42. (self outerScope lookupVariable: aNode) ]].
  43. ^ lookup
  44. !
  45. methodScope
  46. ^ self outerScope ifNotNil: [
  47. self outerScope methodScope ]
  48. !
  49. node
  50. "Answer the node in which I am defined"
  51. ^ node
  52. !
  53. node: aNode
  54. node := aNode
  55. !
  56. outerScope
  57. ^ outerScope
  58. !
  59. outerScope: aLexicalScope
  60. outerScope := aLexicalScope
  61. !
  62. pseudoVars
  63. ^ self methodScope pseudoVars
  64. !
  65. scopeLevel
  66. self outerScope ifNil: [ ^ 1 ].
  67. self isInlined ifTrue: [ ^ self outerScope scopeLevel ].
  68. ^ self outerScope scopeLevel + 1
  69. !
  70. temps
  71. ^ temps ifNil: [ temps := Dictionary new ]
  72. ! !
  73. !LexicalScope methodsFor: 'adding'!
  74. addArg: aString
  75. self args at: aString put: (ArgVar on: aString).
  76. (self args at: aString) scope: self
  77. !
  78. addTemp: aString
  79. self temps at: aString put: (TempVar on: aString).
  80. (self temps at: aString) scope: self
  81. ! !
  82. !LexicalScope methodsFor: 'testing'!
  83. canInlineNonLocalReturns
  84. ^ self isInlined and: [ self outerScope canInlineNonLocalReturns ]
  85. !
  86. isBlockScope
  87. ^ self isMethodScope not
  88. !
  89. isInlined
  90. ^ self instruction notNil and: [
  91. self instruction isInlined ]
  92. !
  93. isMethodScope
  94. ^ false
  95. ! !
  96. LexicalScope subclass: #MethodLexicalScope
  97. instanceVariableNames: 'iVars pseudoVars unknownVariables localReturn nonLocalReturns'
  98. package: 'Compiler-Semantic'!
  99. !MethodLexicalScope commentStamp!
  100. I represent a method scope.!
  101. !MethodLexicalScope methodsFor: 'accessing'!
  102. allVariableNames
  103. ^ super allVariableNames, self iVars keys
  104. !
  105. bindingFor: aNode
  106. ^ (super bindingFor: aNode) ifNil: [
  107. self iVars at: aNode value ifAbsent: [ nil ]]
  108. !
  109. iVars
  110. ^ iVars ifNil: [ iVars := Dictionary new ]
  111. !
  112. localReturn
  113. ^ localReturn ifNil: [ false ]
  114. !
  115. localReturn: aBoolean
  116. localReturn := aBoolean
  117. !
  118. methodScope
  119. ^ self
  120. !
  121. nonLocalReturns
  122. ^ nonLocalReturns ifNil: [ nonLocalReturns := OrderedCollection new ]
  123. !
  124. pseudoVars
  125. pseudoVars ifNil: [
  126. pseudoVars := Dictionary new.
  127. Smalltalk pseudoVariableNames do: [ :each |
  128. pseudoVars at: each put: ((PseudoVar on: each)
  129. scope: self methodScope;
  130. yourself) ]].
  131. ^ pseudoVars
  132. !
  133. unknownVariables
  134. ^ unknownVariables ifNil: [ unknownVariables := OrderedCollection new ]
  135. ! !
  136. !MethodLexicalScope methodsFor: 'adding'!
  137. addIVar: aString
  138. self iVars at: aString put: (InstanceVar on: aString).
  139. (self iVars at: aString) scope: self
  140. !
  141. addNonLocalReturn: aScope
  142. self nonLocalReturns add: aScope
  143. !
  144. removeNonLocalReturn: aScope
  145. self nonLocalReturns remove: aScope ifAbsent: []
  146. ! !
  147. !MethodLexicalScope methodsFor: 'testing'!
  148. canInlineNonLocalReturns
  149. ^ true
  150. !
  151. hasLocalReturn
  152. ^ self localReturn
  153. !
  154. hasNonLocalReturn
  155. ^ self nonLocalReturns notEmpty
  156. !
  157. isMethodScope
  158. ^ true
  159. ! !
  160. Object subclass: #ScopeVar
  161. instanceVariableNames: 'scope name'
  162. package: 'Compiler-Semantic'!
  163. !ScopeVar commentStamp!
  164. I am an entry in a LexicalScope that gets associated with variable nodes of the same name.
  165. There are 4 different subclasses of vars: temp vars, local vars, args, and unknown/global vars.!
  166. !ScopeVar methodsFor: 'accessing'!
  167. alias
  168. ^ self name asVariableName
  169. !
  170. name
  171. ^ name
  172. !
  173. name: aString
  174. name := aString
  175. !
  176. scope
  177. ^ scope
  178. !
  179. scope: aScope
  180. scope := aScope
  181. ! !
  182. !ScopeVar methodsFor: 'testing'!
  183. isArgVar
  184. ^ false
  185. !
  186. isClassRefVar
  187. ^ false
  188. !
  189. isImmutable
  190. ^ false
  191. !
  192. isInstanceVar
  193. ^ false
  194. !
  195. isPseudoVar
  196. ^ false
  197. !
  198. isTempVar
  199. ^ false
  200. !
  201. isUnknownVar
  202. ^ false
  203. !
  204. validateAssignment
  205. (self isArgVar or: [ self isPseudoVar ]) ifTrue: [
  206. InvalidAssignmentError new
  207. variableName: self name;
  208. signal]
  209. ! !
  210. !ScopeVar class methodsFor: 'instance creation'!
  211. on: aString
  212. ^ self new
  213. name: aString;
  214. yourself
  215. ! !
  216. ScopeVar subclass: #AliasVar
  217. instanceVariableNames: 'node'
  218. package: 'Compiler-Semantic'!
  219. !AliasVar commentStamp!
  220. I am an internally defined variable by the compiler!
  221. !AliasVar methodsFor: 'accessing'!
  222. node
  223. ^ node
  224. !
  225. node: aNode
  226. node := aNode
  227. ! !
  228. !AliasVar methodsFor: 'testing'!
  229. isImmutable
  230. ^ true
  231. ! !
  232. ScopeVar subclass: #ArgVar
  233. instanceVariableNames: ''
  234. package: 'Compiler-Semantic'!
  235. !ArgVar commentStamp!
  236. I am an argument of a method or block.!
  237. !ArgVar methodsFor: 'testing'!
  238. isArgVar
  239. ^ true
  240. !
  241. isImmutable
  242. ^ true
  243. ! !
  244. ScopeVar subclass: #ClassRefVar
  245. instanceVariableNames: ''
  246. package: 'Compiler-Semantic'!
  247. !ClassRefVar commentStamp!
  248. I am an class reference variable!
  249. !ClassRefVar methodsFor: 'accessing'!
  250. alias
  251. "Fixes issue #190.
  252. A function is created in the method definition, answering the class or nil.
  253. See JSStream >> #nextPutClassRefFunction:"
  254. ^ '$', self name, '()'
  255. ! !
  256. !ClassRefVar methodsFor: 'testing'!
  257. isClassRefVar
  258. ^ true
  259. !
  260. isImmutable
  261. ^ true
  262. ! !
  263. ScopeVar subclass: #InstanceVar
  264. instanceVariableNames: ''
  265. package: 'Compiler-Semantic'!
  266. !InstanceVar commentStamp!
  267. I am an instance variable of a method or block.!
  268. !InstanceVar methodsFor: 'testing'!
  269. alias
  270. ^ 'self["@', self name, '"]'
  271. !
  272. isInstanceVar
  273. ^ true
  274. ! !
  275. ScopeVar subclass: #PseudoVar
  276. instanceVariableNames: ''
  277. package: 'Compiler-Semantic'!
  278. !PseudoVar commentStamp!
  279. I am an pseudo variable.
  280. The five Smalltalk pseudo variables are: 'self', 'super', 'nil', 'true' and 'false'!
  281. !PseudoVar methodsFor: 'accessing'!
  282. alias
  283. ^ self name
  284. ! !
  285. !PseudoVar methodsFor: 'testing'!
  286. isImmutable
  287. ^ true
  288. !
  289. isPseudoVar
  290. ^ true
  291. ! !
  292. ScopeVar subclass: #TempVar
  293. instanceVariableNames: ''
  294. package: 'Compiler-Semantic'!
  295. !TempVar commentStamp!
  296. I am an temporary variable of a method or block.!
  297. !TempVar methodsFor: 'testing'!
  298. isTempVar
  299. ^ true
  300. ! !
  301. ScopeVar subclass: #UnknownVar
  302. instanceVariableNames: ''
  303. package: 'Compiler-Semantic'!
  304. !UnknownVar commentStamp!
  305. I am an unknown variable. Amber uses unknown variables as JavaScript globals!
  306. !UnknownVar methodsFor: 'testing'!
  307. isUnknownVar
  308. ^ true
  309. ! !
  310. NodeVisitor subclass: #SemanticAnalyzer
  311. instanceVariableNames: 'currentScope blockIndex thePackage theClass classReferences messageSends visitedRefIds'
  312. package: 'Compiler-Semantic'!
  313. !SemanticAnalyzer commentStamp!
  314. I semantically analyze the abstract syntax tree and annotate it with informations such as non local returns and variable scopes.!
  315. !SemanticAnalyzer methodsFor: 'accessing'!
  316. classReferences
  317. ^ classReferences ifNil: [ classReferences := Set new ]
  318. !
  319. messageSends
  320. ^ messageSends ifNil: [ messageSends := Dictionary new ]
  321. !
  322. theClass
  323. ^ theClass
  324. !
  325. theClass: aClass
  326. theClass := aClass
  327. !
  328. thePackage
  329. ^ thePackage
  330. !
  331. thePackage: aPackage
  332. thePackage := aPackage
  333. ! !
  334. !SemanticAnalyzer methodsFor: 'error handling'!
  335. errorShadowingVariable: aString
  336. ShadowingVariableError new
  337. variableName: aString;
  338. signal
  339. !
  340. errorUnknownVariable: aNode
  341. "Throw an error if the variable is undeclared in the global JS scope (i.e. window).
  342. We allow all variables listed by Smalltalk>>#globalJsVariables.
  343. This list includes: `window`, `document`, `process` and `global`
  344. for nodejs and browser environments.
  345. This is only to make sure compilation works on both browser-based and nodejs environments.
  346. The ideal solution would be to use a pragma instead"
  347. | identifier |
  348. identifier := aNode value.
  349. ((Smalltalk globalJsVariables includes: identifier) not
  350. and: [ self isVariableUndefined: identifier inPackage: self thePackage ])
  351. ifTrue: [
  352. UnknownVariableError new
  353. variableName: aNode value;
  354. signal ]
  355. ifFalse: [
  356. currentScope methodScope unknownVariables add: aNode value ]
  357. ! !
  358. !SemanticAnalyzer methodsFor: 'factory'!
  359. newBlockScope
  360. ^ self newScopeOfClass: LexicalScope
  361. !
  362. newMethodScope
  363. ^ self newScopeOfClass: MethodLexicalScope
  364. !
  365. newScopeOfClass: aLexicalScopeClass
  366. ^ aLexicalScopeClass new
  367. outerScope: currentScope;
  368. yourself
  369. ! !
  370. !SemanticAnalyzer methodsFor: 'initialization'!
  371. initialize
  372. super initialize.
  373. visitedRefIds := Set new
  374. ! !
  375. !SemanticAnalyzer methodsFor: 'private'!
  376. nextBlockIndex
  377. blockIndex ifNil: [ blockIndex := 0 ].
  378. blockIndex := blockIndex + 1.
  379. ^ blockIndex
  380. ! !
  381. !SemanticAnalyzer methodsFor: 'scope'!
  382. popScope
  383. currentScope ifNotNil: [
  384. currentScope := currentScope outerScope ]
  385. !
  386. pushScope: aScope
  387. aScope outerScope: currentScope.
  388. currentScope := aScope
  389. !
  390. validateVariableScope: aString
  391. "Validate the variable scope in by doing a recursive lookup, up to the method scope"
  392. (currentScope lookupVariable: aString) ifNotNil: [
  393. self errorShadowingVariable: aString ]
  394. ! !
  395. !SemanticAnalyzer methodsFor: 'testing'!
  396. isVariableUndefined: aString inPackage: aPackage
  397. aPackage ifNotNil: [
  398. | packageKnownVars |
  399. packageKnownVars := (aPackage imports
  400. reject: #isString)
  401. collect: #key.
  402. (packageKnownVars includes: aString) ifTrue: [ ^ false ]].
  403. ^ Compiler new
  404. eval: 'typeof ', aString, ' == "undefined"'
  405. ! !
  406. !SemanticAnalyzer methodsFor: 'visiting'!
  407. visitAssignmentNode: aNode
  408. super visitAssignmentNode: aNode.
  409. aNode left beAssigned
  410. !
  411. visitBlockNode: aNode
  412. self pushScope: self newBlockScope.
  413. aNode scope: currentScope.
  414. currentScope node: aNode.
  415. currentScope blockIndex: self nextBlockIndex.
  416. aNode parameters do: [ :each |
  417. self validateVariableScope: each.
  418. currentScope addArg: each ].
  419. super visitBlockNode: aNode.
  420. self popScope
  421. !
  422. visitCascadeNode: aNode
  423. | recv |
  424. recv := aNode nodes first receiver.
  425. aNode nodes do: [ :each | each receiver: recv asRefNode ].
  426. aNode receiver: recv.
  427. super visitCascadeNode: aNode
  428. !
  429. visitMethodNode: aNode
  430. self pushScope: self newMethodScope.
  431. aNode scope: currentScope.
  432. currentScope node: aNode.
  433. self theClass allInstanceVariableNames do: [ :each |
  434. currentScope addIVar: each ].
  435. aNode arguments do: [ :each |
  436. self validateVariableScope: each.
  437. currentScope addArg: each ].
  438. super visitMethodNode: aNode.
  439. aNode
  440. classReferences: self classReferences;
  441. sendIndexes: self messageSends.
  442. self popScope
  443. !
  444. visitRefNode: aNode
  445. | ref aux |
  446. ref := aNode node.
  447. aux := visitedRefIds size.
  448. visitedRefIds add: ref nodeId.
  449. aux < visitedRefIds size "added; not visited yet" ifTrue: [
  450. ref shouldBeAliased: true.
  451. ^ super visitRefNode: aNode ]
  452. !
  453. visitReturnNode: aNode
  454. aNode scope: currentScope.
  455. currentScope isMethodScope
  456. ifTrue: [ currentScope localReturn: true ]
  457. ifFalse: [ currentScope methodScope addNonLocalReturn: currentScope ].
  458. super visitReturnNode: aNode
  459. !
  460. visitSendNode: aNode
  461. aNode receiver value = 'super'
  462. ifFalse: [ (IRSendInliner inlinedSelectors includes: aNode selector) ifTrue: [
  463. aNode shouldBeInlined: true.
  464. aNode receiver ifNotNil: [ :receiver |
  465. receiver shouldBeAliased: true ] ] ].
  466. self messageSends at: aNode selector ifAbsentPut: [ Set new ].
  467. (self messageSends at: aNode selector) add: aNode.
  468. aNode index: (self messageSends at: aNode selector) size.
  469. super visitSendNode: aNode
  470. !
  471. visitSequenceNode: aNode
  472. aNode temps do: [ :each |
  473. self validateVariableScope: each.
  474. currentScope addTemp: each ].
  475. super visitSequenceNode: aNode
  476. !
  477. visitVariableNode: aNode
  478. "Bind a ScopeVar to aNode by doing a lookup in the current scope.
  479. If no ScopeVar is found, bind a UnknowVar and throw an error."
  480. | binding |
  481. binding := currentScope lookupVariable: aNode.
  482. binding ifNil: [
  483. aNode value isCapitalized
  484. ifTrue: [ "Capital letter variables might be globals."
  485. binding := ClassRefVar new name: aNode value; yourself.
  486. self classReferences add: aNode value]
  487. ifFalse: [
  488. self errorUnknownVariable: aNode.
  489. binding := UnknownVar new name: aNode value; yourself ] ].
  490. aNode binding: binding.
  491. ! !
  492. !SemanticAnalyzer class methodsFor: 'instance creation'!
  493. on: aClass
  494. ^ self new
  495. theClass: aClass;
  496. yourself
  497. ! !
  498. CompilerError subclass: #SemanticError
  499. instanceVariableNames: ''
  500. package: 'Compiler-Semantic'!
  501. !SemanticError commentStamp!
  502. I represent an abstract semantic error thrown by the SemanticAnalyzer.
  503. Semantic errors can be unknown variable errors, etc.
  504. See my subclasses for concrete errors.
  505. The IDE should catch instances of Semantic error to deal with them when compiling!
  506. SemanticError subclass: #InvalidAssignmentError
  507. instanceVariableNames: 'variableName'
  508. package: 'Compiler-Semantic'!
  509. !InvalidAssignmentError commentStamp!
  510. I get signaled when a pseudo variable gets assigned.!
  511. !InvalidAssignmentError methodsFor: 'accessing'!
  512. messageText
  513. ^ ' Invalid assignment to variable: ', self variableName
  514. !
  515. variableName
  516. ^ variableName
  517. !
  518. variableName: aString
  519. variableName := aString
  520. ! !
  521. SemanticError subclass: #ShadowingVariableError
  522. instanceVariableNames: 'variableName'
  523. package: 'Compiler-Semantic'!
  524. !ShadowingVariableError commentStamp!
  525. I get signaled when a variable in a block or method scope shadows a variable of the same name in an outer scope.!
  526. !ShadowingVariableError methodsFor: 'accessing'!
  527. messageText
  528. ^ 'Variable shadowing error: ', self variableName, ' is already defined'
  529. !
  530. variableName
  531. ^ variableName
  532. !
  533. variableName: aString
  534. variableName := aString
  535. ! !
  536. SemanticError subclass: #UnknownVariableError
  537. instanceVariableNames: 'variableName'
  538. package: 'Compiler-Semantic'!
  539. !UnknownVariableError commentStamp!
  540. I get signaled when a variable is not defined.
  541. The default behavior is to allow it, as this is how Amber currently is able to seamlessly send messages to JavaScript objects.!
  542. !UnknownVariableError methodsFor: 'accessing'!
  543. messageText
  544. ^ 'Unknown Variable error: ', self variableName, ' is not defined'
  545. !
  546. variableName
  547. ^ variableName
  548. !
  549. variableName: aString
  550. variableName := aString
  551. ! !