Compiler-AST.st 14 KB

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