Compiler-AST.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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'
  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. !MethodNode methodsFor: 'visiting'!
  333. accept: aVisitor
  334. ^ aVisitor visitMethodNode: self
  335. ! !
  336. Node subclass: #QuasiSendNode
  337. instanceVariableNames: 'receiver'
  338. package: 'Compiler-AST'!
  339. !QuasiSendNode commentStamp!
  340. I am a node that has a receiver.
  341. My subclasses are `SendNode`, `CascadeNode` and `BranchSendNode`.!
  342. !QuasiSendNode methodsFor: 'accessing'!
  343. receiver
  344. ^ receiver
  345. !
  346. receiver: anObject
  347. receiver := anObject
  348. ! !
  349. Node subclass: #ReturnNode
  350. instanceVariableNames: 'scope'
  351. package: 'Compiler-AST'!
  352. !ReturnNode commentStamp!
  353. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  354. !ReturnNode methodsFor: 'accessing'!
  355. scope
  356. ^ scope
  357. !
  358. scope: aLexicalScope
  359. scope := aLexicalScope
  360. ! !
  361. !ReturnNode methodsFor: 'testing'!
  362. isReturnNode
  363. ^ true
  364. !
  365. nonLocalReturn
  366. ^ self scope isMethodScope not
  367. ! !
  368. !ReturnNode methodsFor: 'visiting'!
  369. accept: aVisitor
  370. ^ aVisitor visitReturnNode: self
  371. ! !
  372. Node subclass: #SendNode
  373. instanceVariableNames: 'selector arguments receiver index'
  374. package: 'Compiler-AST'!
  375. !SendNode commentStamp!
  376. I represent an message send node.!
  377. !SendNode methodsFor: 'accessing'!
  378. arguments
  379. ^ arguments ifNil: [ arguments := #() ]
  380. !
  381. arguments: aCollection
  382. arguments := aCollection.
  383. aCollection do: [ :each | each parent: self ]
  384. !
  385. cascadeNodeWithMessages: aCollection
  386. | first |
  387. first := SendNode new
  388. selector: self selector;
  389. arguments: self arguments;
  390. yourself.
  391. ^ CascadeNode new
  392. receiver: self receiver;
  393. nodes: (Array with: first), aCollection;
  394. yourself
  395. !
  396. index
  397. ^ index
  398. !
  399. index: anInteger
  400. index := anInteger
  401. !
  402. navigationLink
  403. ^ self selector
  404. !
  405. nodes
  406. self receiver ifNil: [ ^ self arguments copy ].
  407. ^ (Array with: self receiver)
  408. addAll: self arguments;
  409. yourself
  410. !
  411. receiver
  412. ^ receiver
  413. !
  414. receiver: aNode
  415. receiver := aNode.
  416. aNode isNode ifTrue: [
  417. aNode parent: self ]
  418. !
  419. selector
  420. ^ selector
  421. !
  422. selector: aString
  423. selector := aString
  424. !
  425. superSend
  426. ^ self receiver value = 'super'
  427. ! !
  428. !SendNode methodsFor: 'building'!
  429. valueForReceiver: anObject
  430. ^ SendNode new
  431. position: self position;
  432. source: self source;
  433. receiver: (self receiver
  434. ifNil: [ anObject ]
  435. ifNotNil: [ self receiver valueForReceiver: anObject ]);
  436. selector: self selector;
  437. arguments: self arguments;
  438. yourself
  439. ! !
  440. !SendNode methodsFor: 'testing'!
  441. isCascadeSendNode
  442. ^ self parent isCascadeNode
  443. !
  444. isNavigationNode
  445. ^ true
  446. !
  447. isSendNode
  448. ^ true
  449. !
  450. requiresSmalltalkContext
  451. ^ true
  452. !
  453. shouldBeAliased
  454. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  455. See IRJSVisitor >> visitIRSend:"
  456. | sends |
  457. sends := (self method sendIndexes at: self selector) size.
  458. ^ (super shouldBeAliased or: [
  459. self isReferenced and: [
  460. (sends > 1 and: [ self index < sends ])
  461. or: [ self superSend ] ] ])
  462. ! !
  463. !SendNode methodsFor: 'visiting'!
  464. accept: aVisitor
  465. ^ aVisitor visitSendNode: self
  466. ! !
  467. Node subclass: #SequenceNode
  468. instanceVariableNames: 'temps scope'
  469. package: 'Compiler-AST'!
  470. !SequenceNode commentStamp!
  471. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  472. !SequenceNode methodsFor: 'accessing'!
  473. scope
  474. ^ scope
  475. !
  476. scope: aLexicalScope
  477. scope := aLexicalScope
  478. !
  479. temps
  480. ^ temps ifNil: [ #() ]
  481. !
  482. temps: aCollection
  483. temps := aCollection
  484. ! !
  485. !SequenceNode methodsFor: 'building'!
  486. asBlockSequenceNode
  487. ^ BlockSequenceNode new
  488. position: self position;
  489. source: self source;
  490. nodes: self nodes;
  491. temps: self temps;
  492. yourself
  493. ! !
  494. !SequenceNode methodsFor: 'testing'!
  495. isSequenceNode
  496. ^ true
  497. ! !
  498. !SequenceNode methodsFor: 'visiting'!
  499. accept: aVisitor
  500. ^ aVisitor visitSequenceNode: self
  501. ! !
  502. SequenceNode subclass: #BlockSequenceNode
  503. instanceVariableNames: ''
  504. package: 'Compiler-AST'!
  505. !BlockSequenceNode commentStamp!
  506. I represent an special sequence node for block scopes.!
  507. !BlockSequenceNode methodsFor: 'testing'!
  508. isBlockSequenceNode
  509. ^ true
  510. ! !
  511. !BlockSequenceNode methodsFor: 'visiting'!
  512. accept: aVisitor
  513. ^ aVisitor visitBlockSequenceNode: self
  514. ! !
  515. Node subclass: #ValueNode
  516. instanceVariableNames: 'value'
  517. package: 'Compiler-AST'!
  518. !ValueNode commentStamp!
  519. I represent a value node.!
  520. !ValueNode methodsFor: 'accessing'!
  521. value
  522. ^ value
  523. !
  524. value: anObject
  525. value := anObject
  526. ! !
  527. !ValueNode methodsFor: 'testing'!
  528. isImmutable
  529. ^ self value isImmutable
  530. !
  531. isValueNode
  532. ^ true
  533. ! !
  534. !ValueNode methodsFor: 'visiting'!
  535. accept: aVisitor
  536. ^ aVisitor visitValueNode: self
  537. ! !
  538. ValueNode subclass: #VariableNode
  539. instanceVariableNames: 'assigned binding'
  540. package: 'Compiler-AST'!
  541. !VariableNode commentStamp!
  542. I represent an variable node.!
  543. !VariableNode methodsFor: 'accessing'!
  544. alias
  545. ^ self binding alias
  546. !
  547. assigned
  548. ^ assigned ifNil: [ false ]
  549. !
  550. assigned: aBoolean
  551. assigned := aBoolean
  552. !
  553. beAssigned
  554. self binding validateAssignment.
  555. assigned := true
  556. !
  557. binding
  558. ^ binding
  559. !
  560. binding: aScopeVar
  561. binding := aScopeVar
  562. !
  563. navigationLink
  564. ^ self value
  565. ! !
  566. !VariableNode methodsFor: 'testing'!
  567. isArgument
  568. ^ self binding isArgVar
  569. !
  570. isImmutable
  571. ^ self binding isImmutable
  572. !
  573. isNavigationNode
  574. ^ true
  575. !
  576. isVariableNode
  577. ^ true
  578. ! !
  579. !VariableNode methodsFor: 'visiting'!
  580. accept: aVisitor
  581. ^ aVisitor visitVariableNode: self
  582. ! !
  583. Object subclass: #NodeVisitor
  584. instanceVariableNames: ''
  585. package: 'Compiler-AST'!
  586. !NodeVisitor commentStamp!
  587. I am the abstract super class of all AST node visitors.!
  588. !NodeVisitor methodsFor: 'visiting'!
  589. visit: aNode
  590. ^ aNode accept: self
  591. !
  592. visitAll: aCollection
  593. ^ aCollection collect: [ :each | self visit: each ]
  594. !
  595. visitAssignmentNode: aNode
  596. ^ self visitNode: aNode
  597. !
  598. visitBlockNode: aNode
  599. ^ self visitNode: aNode
  600. !
  601. visitBlockSequenceNode: aNode
  602. ^ self visitSequenceNode: aNode
  603. !
  604. visitCascadeNode: aNode
  605. ^ self visitNode: aNode
  606. !
  607. visitDynamicArrayNode: aNode
  608. ^ self visitNode: aNode
  609. !
  610. visitDynamicDictionaryNode: aNode
  611. ^ self visitNode: aNode
  612. !
  613. visitJSStatementNode: aNode
  614. ^ self visitNode: aNode
  615. !
  616. visitMethodNode: aNode
  617. ^ self visitNode: aNode
  618. !
  619. visitNode: aNode
  620. ^ self visitAll: aNode nodes
  621. !
  622. visitReturnNode: aNode
  623. ^ self visitNode: aNode
  624. !
  625. visitSendNode: aNode
  626. ^ self visitNode: aNode
  627. !
  628. visitSequenceNode: aNode
  629. ^ self visitNode: aNode
  630. !
  631. visitValueNode: aNode
  632. ^ self visitNode: aNode
  633. !
  634. visitVariableNode: aNode
  635. ^ self visitNode: aNode
  636. ! !
  637. !CompiledMethod methodsFor: '*Compiler-AST'!
  638. ast
  639. self source ifEmpty: [ self error: 'Method source is empty' ].
  640. ^ Smalltalk parse: self source
  641. ! !
  642. !Object methodsFor: '*Compiler-AST'!
  643. isNode
  644. ^ false
  645. ! !