Compiler-AST.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939
  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: #RefNode
  462. instanceVariableNames: 'node'
  463. package: 'Compiler-AST'!
  464. !RefNode commentStamp!
  465. I reference other `node`, which may be used more times,
  466. each time getting separate instance of me.
  467. I have my own `parent`, but delegate visitors to `node`.
  468. During semantic analysis, analyzer does `node shouldBeAliased: true`
  469. because of the fact it may be used more times.!
  470. !RefNode methodsFor: 'accessing'!
  471. node
  472. ^ node
  473. !
  474. node: anObject
  475. node := anObject
  476. ! !
  477. !RefNode methodsFor: 'testing'!
  478. isImmutable
  479. ^ true
  480. ! !
  481. !RefNode methodsFor: 'visiting'!
  482. accept: aVisitor
  483. ^ aVisitor visitRefNode: self
  484. ! !
  485. Node subclass: #ReturnNode
  486. instanceVariableNames: 'scope'
  487. package: 'Compiler-AST'!
  488. !ReturnNode commentStamp!
  489. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  490. !ReturnNode methodsFor: 'accessing'!
  491. scope
  492. ^ scope
  493. !
  494. scope: aLexicalScope
  495. scope := aLexicalScope
  496. ! !
  497. !ReturnNode methodsFor: 'testing'!
  498. isReturnNode
  499. ^ true
  500. !
  501. nonLocalReturn
  502. ^ self scope isMethodScope not
  503. ! !
  504. !ReturnNode methodsFor: 'visiting'!
  505. accept: aVisitor
  506. ^ aVisitor visitReturnNode: self
  507. ! !
  508. Node subclass: #SequenceNode
  509. instanceVariableNames: 'temps scope'
  510. package: 'Compiler-AST'!
  511. !SequenceNode commentStamp!
  512. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  513. !SequenceNode methodsFor: 'accessing'!
  514. scope
  515. ^ scope
  516. !
  517. scope: aLexicalScope
  518. scope := aLexicalScope
  519. !
  520. temps
  521. ^ temps ifNil: [ #() ]
  522. !
  523. temps: aCollection
  524. temps := aCollection
  525. ! !
  526. !SequenceNode methodsFor: 'building'!
  527. asBlockSequenceNode
  528. ^ BlockSequenceNode new
  529. position: self position;
  530. source: self source;
  531. nodes: self nodes;
  532. temps: self temps;
  533. yourself
  534. ! !
  535. !SequenceNode methodsFor: 'testing'!
  536. isSequenceNode
  537. ^ true
  538. ! !
  539. !SequenceNode methodsFor: 'visiting'!
  540. accept: aVisitor
  541. ^ aVisitor visitSequenceNode: self
  542. ! !
  543. SequenceNode subclass: #BlockSequenceNode
  544. instanceVariableNames: ''
  545. package: 'Compiler-AST'!
  546. !BlockSequenceNode commentStamp!
  547. I represent an special sequence node for block scopes.!
  548. !BlockSequenceNode methodsFor: 'testing'!
  549. isBlockSequenceNode
  550. ^ true
  551. ! !
  552. !BlockSequenceNode methodsFor: 'visiting'!
  553. accept: aVisitor
  554. ^ aVisitor visitBlockSequenceNode: self
  555. ! !
  556. Node subclass: #ValueNode
  557. instanceVariableNames: 'value'
  558. package: 'Compiler-AST'!
  559. !ValueNode commentStamp!
  560. I represent a value node.!
  561. !ValueNode methodsFor: 'accessing'!
  562. value
  563. ^ value
  564. !
  565. value: anObject
  566. value := anObject
  567. ! !
  568. !ValueNode methodsFor: 'testing'!
  569. isImmutable
  570. ^ self value isImmutable
  571. !
  572. isValueNode
  573. ^ true
  574. ! !
  575. !ValueNode methodsFor: 'visiting'!
  576. accept: aVisitor
  577. ^ aVisitor visitValueNode: self
  578. ! !
  579. ValueNode subclass: #VariableNode
  580. instanceVariableNames: 'assigned binding'
  581. package: 'Compiler-AST'!
  582. !VariableNode commentStamp!
  583. I represent an variable node.!
  584. !VariableNode methodsFor: 'accessing'!
  585. alias
  586. ^ self binding alias
  587. !
  588. assigned
  589. ^ assigned ifNil: [ false ]
  590. !
  591. assigned: aBoolean
  592. assigned := aBoolean
  593. !
  594. beAssigned
  595. self binding validateAssignment.
  596. assigned := true
  597. !
  598. binding
  599. ^ binding
  600. !
  601. binding: aScopeVar
  602. binding := aScopeVar
  603. !
  604. navigationLink
  605. ^ self value
  606. ! !
  607. !VariableNode methodsFor: 'testing'!
  608. isArgument
  609. ^ self binding isArgVar
  610. !
  611. isImmutable
  612. ^ self binding isImmutable
  613. !
  614. isNavigationNode
  615. ^ true
  616. !
  617. isVariableNode
  618. ^ true
  619. ! !
  620. !VariableNode methodsFor: 'visiting'!
  621. accept: aVisitor
  622. ^ aVisitor visitVariableNode: self
  623. ! !
  624. Object subclass: #NodeVisitor
  625. instanceVariableNames: ''
  626. package: 'Compiler-AST'!
  627. !NodeVisitor commentStamp!
  628. I am the abstract super class of all AST node visitors.!
  629. !NodeVisitor methodsFor: 'visiting'!
  630. visit: aNode
  631. ^ aNode accept: self
  632. !
  633. visitAll: aCollection
  634. ^ aCollection collect: [ :each | self visit: each ]
  635. !
  636. visitAssignmentNode: aNode
  637. ^ self visitNode: aNode
  638. !
  639. visitBlockNode: aNode
  640. ^ self visitNode: aNode
  641. !
  642. visitBlockSequenceNode: aNode
  643. ^ self visitSequenceNode: aNode
  644. !
  645. visitBranchSendNode: aNode
  646. ^ self visitNode: aNode
  647. !
  648. visitCascadeNode: aNode
  649. ^ self visitNode: aNode
  650. !
  651. visitDynamicArrayNode: aNode
  652. ^ self visitNode: aNode
  653. !
  654. visitDynamicDictionaryNode: aNode
  655. ^ self visitNode: aNode
  656. !
  657. visitJSStatementNode: aNode
  658. ^ self visitNode: aNode
  659. !
  660. visitMethodNode: aNode
  661. ^ self visitNode: aNode
  662. !
  663. visitNode: aNode
  664. ^ self visitAll: aNode nodes
  665. !
  666. visitRefNode: aNode
  667. ^ self visit: aNode node
  668. !
  669. visitReturnNode: aNode
  670. ^ self visitNode: aNode
  671. !
  672. visitSendNode: aNode
  673. ^ self visitNode: aNode
  674. !
  675. visitSequenceNode: aNode
  676. ^ self visitNode: aNode
  677. !
  678. visitValueNode: aNode
  679. ^ self visitNode: aNode
  680. !
  681. visitVariableNode: aNode
  682. ^ self visitNode: aNode
  683. ! !
  684. !CompiledMethod methodsFor: '*Compiler-AST'!
  685. ast
  686. self source ifEmpty: [ self error: 'Method source is empty' ].
  687. ^ Smalltalk parse: self source
  688. ! !
  689. !Object methodsFor: '*Compiler-AST'!
  690. isNode
  691. ^ false
  692. ! !