Compiler-AST.st 11 KB

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