Compiler-AST.st 13 KB

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