1
0

Compiler-AST.st 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. Smalltalk createPackage: 'Compiler-AST'!
  2. Object subclass: #Node
  3. instanceVariableNames: 'parent position source nodes shouldBeInlined shouldBeAliased'
  4. package: 'Compiler-AST'!
  5. !Node commentStamp!
  6. I am the abstract root class of the abstract syntax tree.
  7. Concrete classes should implement `#accept:` to allow visiting.
  8. `position` holds a point containing line and column number of the symbol location in the original source file.!
  9. !Node methodsFor: 'accessing'!
  10. addNode: aNode
  11. self nodes add: aNode.
  12. aNode parent: self
  13. !
  14. method
  15. ^ self parent ifNotNil: [ :node | node method ]
  16. !
  17. nextChild
  18. "Answer the next node after aNode.
  19. Recurse into the possible children of the receiver to answer the next node to be evaluated"
  20. ^ self nodes isEmpty
  21. ifTrue: [ self ]
  22. ifFalse: [ self nodes first nextChild ]
  23. !
  24. nextNode
  25. ^ self parent ifNotNil: [ :node |
  26. node nextNode: self ]
  27. !
  28. nextNode: aNode
  29. "Answer the next node after aNode.
  30. Recurse into the possible children of the next node to answer the next node to be evaluated"
  31. | next |
  32. next := self nodes
  33. at: (self nodes indexOf: aNode) + 1
  34. ifAbsent: [ ^ self ].
  35. ^ next nextChild
  36. !
  37. nodes
  38. ^ nodes ifNil: [ nodes := Array new ]
  39. !
  40. parent
  41. ^ parent
  42. !
  43. parent: aNode
  44. parent := aNode
  45. !
  46. position
  47. "answer the line and column of the receiver in the source code"
  48. ^ position ifNil: [
  49. self parent ifNotNil: [ :node | node position ] ]
  50. !
  51. position: aPosition
  52. position := aPosition
  53. !
  54. positionEnd
  55. ^ self positionStart + ((self source lines size - 1) @ (self source lines last size - 1))
  56. !
  57. positionStart
  58. ^ self position
  59. !
  60. shouldBeAliased
  61. ^ shouldBeAliased ifNil: [ false ]
  62. !
  63. shouldBeAliased: aBoolean
  64. shouldBeAliased := aBoolean
  65. !
  66. shouldBeInlined
  67. ^ shouldBeInlined ifNil: [ false ]
  68. !
  69. shouldBeInlined: aBoolean
  70. shouldBeInlined := aBoolean
  71. !
  72. size
  73. ^ self source size
  74. !
  75. source
  76. ^ source ifNil: [ '' ]
  77. !
  78. source: aString
  79. source := aString
  80. ! !
  81. !Node methodsFor: 'building'!
  82. nodes: aCollection
  83. nodes := aCollection.
  84. aCollection do: [ :each | each parent: self ]
  85. ! !
  86. !Node methodsFor: 'copying'!
  87. postCopy
  88. super postCopy.
  89. self nodes do: [ :each | each parent: self ]
  90. ! !
  91. !Node methodsFor: 'testing'!
  92. isAssignmentNode
  93. ^ false
  94. !
  95. isBlockNode
  96. ^ false
  97. !
  98. isBlockSequenceNode
  99. ^ false
  100. !
  101. isCascadeNode
  102. ^ false
  103. !
  104. isImmutable
  105. ^ false
  106. !
  107. isJSStatementNode
  108. ^ false
  109. !
  110. isLastChild
  111. ^ self parent nodes last = self
  112. !
  113. isNode
  114. ^ true
  115. !
  116. isReferenced
  117. "Answer true if the receiver is referenced by other nodes.
  118. Do not take sequences or assignments into account"
  119. ^ (self parent isSequenceNode or: [
  120. self parent isAssignmentNode ]) not
  121. !
  122. isReturnNode
  123. ^ false
  124. !
  125. isSendNode
  126. ^ false
  127. !
  128. isSequenceNode
  129. ^ false
  130. !
  131. isValueNode
  132. ^ false
  133. !
  134. isVariableNode
  135. ^ false
  136. !
  137. requiresSmalltalkContext
  138. "Answer true if the receiver requires a smalltalk context.
  139. Only send nodes require a context.
  140. If no node requires a context, the method will be compiled without one.
  141. See `IRJSTranslator` and `JSStream` for context creation"
  142. ^ (self nodes
  143. detect: [ :each | each requiresSmalltalkContext ]
  144. ifNone: [ nil ]) notNil
  145. !
  146. stopOnStepping
  147. ^ false
  148. !
  149. subtreeNeedsAliasing
  150. ^ (self shouldBeAliased or: [ self shouldBeInlined ]) or: [
  151. self nodes anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  152. ! !
  153. !Node methodsFor: 'visiting'!
  154. accept: aVisitor
  155. ^ aVisitor visitNode: self
  156. ! !
  157. Node subclass: #AssignmentNode
  158. instanceVariableNames: 'left right'
  159. package: 'Compiler-AST'!
  160. !AssignmentNode commentStamp!
  161. I represent an assignment node.!
  162. !AssignmentNode methodsFor: 'accessing'!
  163. left
  164. ^ left
  165. !
  166. left: aNode
  167. left := aNode.
  168. aNode parent: self
  169. !
  170. nodes
  171. ^ Array with: self left with: self right
  172. !
  173. right
  174. ^ right
  175. !
  176. right: aNode
  177. right := aNode.
  178. aNode parent: self
  179. ! !
  180. !AssignmentNode methodsFor: 'testing'!
  181. isAssignmentNode
  182. ^ true
  183. !
  184. shouldBeAliased
  185. ^ super shouldBeAliased or: [ self isReferenced ]
  186. ! !
  187. !AssignmentNode methodsFor: 'visiting'!
  188. accept: aVisitor
  189. ^ aVisitor visitAssignmentNode: self
  190. ! !
  191. Node subclass: #BlockNode
  192. instanceVariableNames: 'parameters scope'
  193. package: 'Compiler-AST'!
  194. !BlockNode commentStamp!
  195. I represent an block closure node.!
  196. !BlockNode methodsFor: 'accessing'!
  197. nextChild
  198. "Answer the receiver as we want to avoid eager evaluation"
  199. ^ self
  200. !
  201. nextNode: aNode
  202. "Answer the receiver as we want to avoid eager evaluation"
  203. ^ self
  204. !
  205. parameters
  206. ^ parameters ifNil: [ parameters := Array new ]
  207. !
  208. parameters: aCollection
  209. parameters := aCollection
  210. !
  211. scope
  212. ^ scope
  213. !
  214. scope: aLexicalScope
  215. scope := aLexicalScope
  216. ! !
  217. !BlockNode methodsFor: 'testing'!
  218. isBlockNode
  219. ^ true
  220. !
  221. subtreeNeedsAliasing
  222. ^ self shouldBeAliased or: [ self shouldBeInlined ]
  223. ! !
  224. !BlockNode methodsFor: 'visiting'!
  225. accept: aVisitor
  226. ^ aVisitor visitBlockNode: self
  227. ! !
  228. Node subclass: #CascadeNode
  229. instanceVariableNames: 'receiver'
  230. package: 'Compiler-AST'!
  231. !CascadeNode commentStamp!
  232. I represent an cascade node.!
  233. !CascadeNode methodsFor: 'accessing'!
  234. receiver
  235. ^ receiver
  236. !
  237. receiver: aNode
  238. receiver := aNode
  239. ! !
  240. !CascadeNode methodsFor: 'testing'!
  241. isCascadeNode
  242. ^ true
  243. ! !
  244. !CascadeNode methodsFor: 'visiting'!
  245. accept: aVisitor
  246. ^ aVisitor visitCascadeNode: self
  247. ! !
  248. Node subclass: #DynamicArrayNode
  249. instanceVariableNames: ''
  250. package: 'Compiler-AST'!
  251. !DynamicArrayNode commentStamp!
  252. I represent an dynamic array node.!
  253. !DynamicArrayNode methodsFor: 'visiting'!
  254. accept: aVisitor
  255. ^ aVisitor visitDynamicArrayNode: self
  256. ! !
  257. Node subclass: #DynamicDictionaryNode
  258. instanceVariableNames: ''
  259. package: 'Compiler-AST'!
  260. !DynamicDictionaryNode commentStamp!
  261. I represent an dynamic dictionary node.!
  262. !DynamicDictionaryNode methodsFor: 'visiting'!
  263. accept: aVisitor
  264. ^ aVisitor visitDynamicDictionaryNode: self
  265. ! !
  266. Node subclass: #JSStatementNode
  267. instanceVariableNames: ''
  268. package: 'Compiler-AST'!
  269. !JSStatementNode commentStamp!
  270. I represent an JavaScript statement node.!
  271. !JSStatementNode methodsFor: 'testing'!
  272. isJSStatementNode
  273. ^ true
  274. !
  275. requiresSmalltalkContext
  276. ^ true
  277. ! !
  278. !JSStatementNode methodsFor: 'visiting'!
  279. accept: aVisitor
  280. ^ aVisitor visitJSStatementNode: self
  281. ! !
  282. Node subclass: #MethodNode
  283. instanceVariableNames: 'selector arguments source scope classReferences sendIndexes superSends'
  284. package: 'Compiler-AST'!
  285. !MethodNode commentStamp!
  286. I represent an method node.
  287. A method node must be the root and only method node of a valid AST.!
  288. !MethodNode methodsFor: 'accessing'!
  289. arguments
  290. ^ arguments ifNil: [ #() ]
  291. !
  292. arguments: aCollection
  293. arguments := aCollection
  294. !
  295. classReferences
  296. ^ classReferences
  297. !
  298. classReferences: aCollection
  299. classReferences := aCollection
  300. !
  301. messageSends
  302. ^ self sendIndexes keys
  303. !
  304. method
  305. ^ self
  306. !
  307. scope
  308. ^ scope
  309. !
  310. scope: aMethodScope
  311. scope := aMethodScope
  312. !
  313. selector
  314. ^ selector
  315. !
  316. selector: aString
  317. selector := aString
  318. !
  319. sendIndexes
  320. ^ sendIndexes
  321. !
  322. sendIndexes: aDictionary
  323. sendIndexes := aDictionary
  324. !
  325. source
  326. ^ source
  327. !
  328. source: aString
  329. source := aString
  330. !
  331. superSends
  332. ^ superSends
  333. !
  334. superSends: aCollection
  335. superSends := aCollection
  336. ! !
  337. !MethodNode methodsFor: 'visiting'!
  338. accept: aVisitor
  339. ^ aVisitor visitMethodNode: self
  340. ! !
  341. Node subclass: #ReturnNode
  342. instanceVariableNames: 'scope'
  343. package: 'Compiler-AST'!
  344. !ReturnNode commentStamp!
  345. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  346. !ReturnNode methodsFor: 'accessing'!
  347. scope
  348. ^ scope
  349. !
  350. scope: aLexicalScope
  351. scope := aLexicalScope
  352. ! !
  353. !ReturnNode methodsFor: 'testing'!
  354. isReturnNode
  355. ^ true
  356. !
  357. nonLocalReturn
  358. ^ self scope isMethodScope not
  359. ! !
  360. !ReturnNode methodsFor: 'visiting'!
  361. accept: aVisitor
  362. ^ aVisitor visitReturnNode: self
  363. ! !
  364. Node subclass: #SendNode
  365. instanceVariableNames: 'selector arguments receiver superSend index'
  366. package: 'Compiler-AST'!
  367. !SendNode commentStamp!
  368. I represent an message send node.!
  369. !SendNode methodsFor: 'accessing'!
  370. arguments
  371. ^ arguments ifNil: [ arguments := #() ]
  372. !
  373. arguments: aCollection
  374. arguments := aCollection.
  375. aCollection do: [ :each | each parent: self ]
  376. !
  377. cascadeNodeWithMessages: aCollection
  378. | first |
  379. first := SendNode new
  380. selector: self selector;
  381. arguments: self arguments;
  382. yourself.
  383. ^ CascadeNode new
  384. receiver: self receiver;
  385. nodes: (Array with: first), aCollection;
  386. yourself
  387. !
  388. index
  389. ^ index
  390. !
  391. index: anInteger
  392. index := anInteger
  393. !
  394. nodes
  395. self receiver ifNil: [ ^ self arguments copy ].
  396. ^ (Array with: self receiver)
  397. addAll: self arguments;
  398. yourself
  399. !
  400. receiver
  401. ^ receiver
  402. !
  403. receiver: aNode
  404. receiver := aNode.
  405. aNode isNode ifTrue: [
  406. aNode parent: self ]
  407. !
  408. selector
  409. ^ selector
  410. !
  411. selector: aString
  412. selector := aString
  413. !
  414. superSend
  415. ^ superSend ifNil: [ false ]
  416. !
  417. superSend: aBoolean
  418. superSend := aBoolean
  419. !
  420. valueForReceiver: anObject
  421. ^ SendNode new
  422. position: self position;
  423. source: self source;
  424. receiver: (self receiver
  425. ifNil: [ anObject ]
  426. ifNotNil: [ self receiver valueForReceiver: anObject ]);
  427. selector: self selector;
  428. arguments: self arguments;
  429. yourself
  430. ! !
  431. !SendNode methodsFor: 'testing'!
  432. isCascadeSendNode
  433. ^ self parent isCascadeNode
  434. !
  435. isSendNode
  436. ^ true
  437. !
  438. requiresSmalltalkContext
  439. ^ true
  440. !
  441. shouldBeAliased
  442. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  443. See IRJSVisitor >> visitIRSend:"
  444. | sends |
  445. sends := (self method sendIndexes at: self selector) size.
  446. ^ super shouldBeAliased or: [
  447. (sends > 1 and: [ self index < sends ]) and: [ self isReferenced ] ]
  448. !
  449. stopOnStepping
  450. ^ true
  451. ! !
  452. !SendNode methodsFor: 'visiting'!
  453. accept: aVisitor
  454. ^ aVisitor visitSendNode: self
  455. ! !
  456. Node subclass: #SequenceNode
  457. instanceVariableNames: 'temps scope'
  458. package: 'Compiler-AST'!
  459. !SequenceNode commentStamp!
  460. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  461. !SequenceNode methodsFor: 'accessing'!
  462. scope
  463. ^ scope
  464. !
  465. scope: aLexicalScope
  466. scope := aLexicalScope
  467. !
  468. temps
  469. ^ temps ifNil: [ #() ]
  470. !
  471. temps: aCollection
  472. temps := aCollection
  473. ! !
  474. !SequenceNode methodsFor: 'converting'!
  475. asBlockSequenceNode
  476. ^ BlockSequenceNode new
  477. position: self position;
  478. source: self source;
  479. nodes: self nodes;
  480. temps: self temps;
  481. yourself
  482. ! !
  483. !SequenceNode methodsFor: 'testing'!
  484. isSequenceNode
  485. ^ true
  486. ! !
  487. !SequenceNode methodsFor: 'visiting'!
  488. accept: aVisitor
  489. ^ aVisitor visitSequenceNode: self
  490. ! !
  491. SequenceNode subclass: #BlockSequenceNode
  492. instanceVariableNames: ''
  493. package: 'Compiler-AST'!
  494. !BlockSequenceNode commentStamp!
  495. I represent an special sequence node for block scopes.!
  496. !BlockSequenceNode methodsFor: 'testing'!
  497. isBlockSequenceNode
  498. ^ true
  499. ! !
  500. !BlockSequenceNode methodsFor: 'visiting'!
  501. accept: aVisitor
  502. ^ aVisitor visitBlockSequenceNode: self
  503. ! !
  504. Node subclass: #ValueNode
  505. instanceVariableNames: 'value'
  506. package: 'Compiler-AST'!
  507. !ValueNode commentStamp!
  508. I represent a value node.!
  509. !ValueNode methodsFor: 'accessing'!
  510. value
  511. ^ value
  512. !
  513. value: anObject
  514. value := anObject
  515. ! !
  516. !ValueNode methodsFor: 'testing'!
  517. isImmutable
  518. ^ self value isImmutable
  519. !
  520. isValueNode
  521. ^ true
  522. ! !
  523. !ValueNode methodsFor: 'visiting'!
  524. accept: aVisitor
  525. ^ aVisitor visitValueNode: self
  526. ! !
  527. ValueNode subclass: #VariableNode
  528. instanceVariableNames: 'assigned binding'
  529. package: 'Compiler-AST'!
  530. !VariableNode commentStamp!
  531. I represent an variable node.!
  532. !VariableNode methodsFor: 'accessing'!
  533. alias
  534. ^ self binding alias
  535. !
  536. assigned
  537. ^ assigned ifNil: [ false ]
  538. !
  539. assigned: aBoolean
  540. assigned := aBoolean
  541. !
  542. beAssigned
  543. self binding validateAssignment.
  544. assigned := true
  545. !
  546. binding
  547. ^ binding
  548. !
  549. binding: aScopeVar
  550. binding := aScopeVar
  551. ! !
  552. !VariableNode methodsFor: 'testing'!
  553. isArgument
  554. ^ self binding isArgVar
  555. !
  556. isImmutable
  557. ^ self binding isImmutable
  558. !
  559. isVariableNode
  560. ^ true
  561. ! !
  562. !VariableNode methodsFor: 'visiting'!
  563. accept: aVisitor
  564. ^ aVisitor visitVariableNode: self
  565. ! !
  566. !CompiledMethod methodsFor: '*Compiler-AST'!
  567. ast
  568. self source ifEmpty: [ self error: 'Method source is empty' ].
  569. ^ Smalltalk parse: self source
  570. ! !
  571. !Object methodsFor: '*Compiler-AST'!
  572. isNode
  573. ^ false
  574. ! !