2
0

Compiler-AST.st 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948
  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: #CascadeNode
  231. instanceVariableNames: 'receiver'
  232. package: 'Compiler-AST'!
  233. !CascadeNode commentStamp!
  234. I represent an cascade node.!
  235. !CascadeNode methodsFor: 'accessing'!
  236. receiver
  237. ^ receiver
  238. !
  239. receiver: anObject
  240. receiver := anObject
  241. ! !
  242. !CascadeNode methodsFor: 'testing'!
  243. isCascadeNode
  244. ^ true
  245. ! !
  246. !CascadeNode methodsFor: 'visiting'!
  247. accept: aVisitor
  248. ^ aVisitor visitCascadeNode: self
  249. ! !
  250. Node subclass: #DynamicArrayNode
  251. instanceVariableNames: ''
  252. package: 'Compiler-AST'!
  253. !DynamicArrayNode commentStamp!
  254. I represent an dynamic array node.!
  255. !DynamicArrayNode methodsFor: 'visiting'!
  256. accept: aVisitor
  257. ^ aVisitor visitDynamicArrayNode: self
  258. ! !
  259. Node subclass: #DynamicDictionaryNode
  260. instanceVariableNames: ''
  261. package: 'Compiler-AST'!
  262. !DynamicDictionaryNode commentStamp!
  263. I represent an dynamic dictionary node.!
  264. !DynamicDictionaryNode methodsFor: 'visiting'!
  265. accept: aVisitor
  266. ^ aVisitor visitDynamicDictionaryNode: self
  267. ! !
  268. Node subclass: #JSStatementNode
  269. instanceVariableNames: ''
  270. package: 'Compiler-AST'!
  271. !JSStatementNode commentStamp!
  272. I represent an JavaScript statement node.!
  273. !JSStatementNode methodsFor: 'testing'!
  274. isJSStatementNode
  275. ^ true
  276. !
  277. requiresSmalltalkContext
  278. ^ true
  279. ! !
  280. !JSStatementNode methodsFor: 'visiting'!
  281. accept: aVisitor
  282. ^ aVisitor visitJSStatementNode: self
  283. ! !
  284. Node subclass: #MethodNode
  285. instanceVariableNames: 'selector arguments source scope classReferences sendIndexes'
  286. package: 'Compiler-AST'!
  287. !MethodNode commentStamp!
  288. I represent an method node.
  289. A method node must be the root and only method node of a valid AST.!
  290. !MethodNode methodsFor: 'accessing'!
  291. arguments
  292. ^ arguments ifNil: [ #() ]
  293. !
  294. arguments: aCollection
  295. arguments := aCollection
  296. !
  297. classReferences
  298. ^ classReferences
  299. !
  300. classReferences: aCollection
  301. classReferences := aCollection
  302. !
  303. messageSends
  304. ^ self sendIndexes keys
  305. !
  306. method
  307. ^ self
  308. !
  309. scope
  310. ^ scope
  311. !
  312. scope: aMethodScope
  313. scope := aMethodScope
  314. !
  315. selector
  316. ^ selector
  317. !
  318. selector: aString
  319. selector := aString
  320. !
  321. sendIndexes
  322. ^ sendIndexes
  323. !
  324. sendIndexes: aDictionary
  325. sendIndexes := aDictionary
  326. !
  327. sequenceNode
  328. self nodes do: [ :each |
  329. each isSequenceNode ifTrue: [ ^ each ] ].
  330. ^ nil
  331. !
  332. source
  333. ^ source
  334. !
  335. source: aString
  336. source := aString
  337. ! !
  338. !MethodNode methodsFor: 'visiting'!
  339. accept: aVisitor
  340. ^ aVisitor visitMethodNode: self
  341. ! !
  342. Node subclass: #QuasiSendNode
  343. instanceVariableNames: 'receiver'
  344. package: 'Compiler-AST'!
  345. !QuasiSendNode commentStamp!
  346. I am a node that has a receiver
  347. and with building API `#valueForReceiver:`.
  348. My subclasses are `SendNode` and `BranchSendNode`.!
  349. !QuasiSendNode methodsFor: 'accessing'!
  350. asBranchSendNode
  351. ^ BranchSendNode new
  352. nodes: {self};
  353. position: self position;
  354. source: self source;
  355. receiver: self receiver
  356. !
  357. receiver
  358. ^ receiver
  359. !
  360. receiver: anObject
  361. receiver := anObject
  362. ! !
  363. QuasiSendNode subclass: #BranchSendNode
  364. instanceVariableNames: ''
  365. package: 'Compiler-AST'!
  366. !BranchSendNode methodsFor: 'building'!
  367. valueForReceiver: anObject
  368. self nodes: {self nodes first valueForReceiver: anObject}.
  369. self receiver ifNil: [ self receiver: anObject ].
  370. ^ self
  371. ! !
  372. !BranchSendNode methodsFor: 'visiting'!
  373. accept: aVisitor
  374. ^ aVisitor visitBranchSendNode: self
  375. ! !
  376. QuasiSendNode subclass: #SendNode
  377. instanceVariableNames: 'selector arguments index'
  378. package: 'Compiler-AST'!
  379. !SendNode commentStamp!
  380. I represent an message send node.!
  381. !SendNode methodsFor: 'accessing'!
  382. arguments
  383. ^ arguments ifNil: [ arguments := #() ]
  384. !
  385. arguments: aCollection
  386. arguments := aCollection.
  387. aCollection do: [ :each | each parent: self ]
  388. !
  389. cascadeNodeWithMessages: aCollection
  390. | first |
  391. first := SendNode new
  392. selector: self selector;
  393. arguments: self arguments;
  394. yourself.
  395. ^ CascadeNode new
  396. receiver: self receiver;
  397. nodes: (Array with: first), aCollection;
  398. yourself
  399. !
  400. index
  401. ^ index
  402. !
  403. index: anInteger
  404. index := anInteger
  405. !
  406. navigationLink
  407. ^ self selector
  408. !
  409. nodes
  410. self receiver ifNil: [ ^ self arguments copy ].
  411. ^ (Array with: self receiver)
  412. addAll: self arguments;
  413. yourself
  414. !
  415. receiver: aNode
  416. super receiver: aNode.
  417. aNode isNode ifTrue: [
  418. aNode parent: self ]
  419. !
  420. selector
  421. ^ selector
  422. !
  423. selector: aString
  424. selector := aString
  425. !
  426. superSend
  427. ^ self receiver value = 'super'
  428. ! !
  429. !SendNode methodsFor: 'building'!
  430. valueForReceiver: anObject
  431. ^ SendNode new
  432. position: self position;
  433. source: self source;
  434. receiver: (self receiver
  435. ifNil: [ anObject ]
  436. ifNotNil: [ self receiver valueForReceiver: anObject ]);
  437. selector: self selector;
  438. arguments: self arguments;
  439. yourself
  440. ! !
  441. !SendNode methodsFor: 'testing'!
  442. isCascadeSendNode
  443. ^ self parent isCascadeNode
  444. !
  445. isNavigationNode
  446. ^ true
  447. !
  448. isSendNode
  449. ^ true
  450. !
  451. requiresSmalltalkContext
  452. ^ true
  453. !
  454. shouldBeAliased
  455. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  456. See IRJSVisitor >> visitIRSend:"
  457. | sends |
  458. sends := (self method sendIndexes at: self selector) size.
  459. ^ (super shouldBeAliased or: [
  460. self isReferenced and: [
  461. (sends > 1 and: [ self index < sends ])
  462. or: [ self superSend ] ] ])
  463. ! !
  464. !SendNode methodsFor: 'visiting'!
  465. accept: aVisitor
  466. ^ aVisitor visitSendNode: self
  467. ! !
  468. Node subclass: #RefNode
  469. instanceVariableNames: 'node'
  470. package: 'Compiler-AST'!
  471. !RefNode commentStamp!
  472. I reference other `node`, which may be used more times,
  473. each time getting separate instance of me.
  474. I have my own `parent`, but delegate visitors to `node`.
  475. During semantic analysis, analyzer does `node shouldBeAliased: true`
  476. because of the fact it may be used more times.!
  477. !RefNode methodsFor: 'accessing'!
  478. node
  479. ^ node
  480. !
  481. node: anObject
  482. node := anObject
  483. ! !
  484. !RefNode methodsFor: 'testing'!
  485. isImmutable
  486. ^ true
  487. ! !
  488. !RefNode methodsFor: 'visiting'!
  489. accept: aVisitor
  490. ^ aVisitor visitRefNode: self
  491. ! !
  492. Node subclass: #ReturnNode
  493. instanceVariableNames: 'scope'
  494. package: 'Compiler-AST'!
  495. !ReturnNode commentStamp!
  496. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  497. !ReturnNode methodsFor: 'accessing'!
  498. scope
  499. ^ scope
  500. !
  501. scope: aLexicalScope
  502. scope := aLexicalScope
  503. ! !
  504. !ReturnNode methodsFor: 'testing'!
  505. isReturnNode
  506. ^ true
  507. !
  508. nonLocalReturn
  509. ^ self scope isMethodScope not
  510. ! !
  511. !ReturnNode methodsFor: 'visiting'!
  512. accept: aVisitor
  513. ^ aVisitor visitReturnNode: self
  514. ! !
  515. Node subclass: #SequenceNode
  516. instanceVariableNames: 'temps scope'
  517. package: 'Compiler-AST'!
  518. !SequenceNode commentStamp!
  519. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  520. !SequenceNode methodsFor: 'accessing'!
  521. scope
  522. ^ scope
  523. !
  524. scope: aLexicalScope
  525. scope := aLexicalScope
  526. !
  527. temps
  528. ^ temps ifNil: [ #() ]
  529. !
  530. temps: aCollection
  531. temps := aCollection
  532. ! !
  533. !SequenceNode methodsFor: 'building'!
  534. asBlockSequenceNode
  535. ^ BlockSequenceNode new
  536. position: self position;
  537. source: self source;
  538. nodes: self nodes;
  539. temps: self temps;
  540. yourself
  541. ! !
  542. !SequenceNode methodsFor: 'testing'!
  543. isSequenceNode
  544. ^ true
  545. ! !
  546. !SequenceNode methodsFor: 'visiting'!
  547. accept: aVisitor
  548. ^ aVisitor visitSequenceNode: self
  549. ! !
  550. SequenceNode subclass: #BlockSequenceNode
  551. instanceVariableNames: ''
  552. package: 'Compiler-AST'!
  553. !BlockSequenceNode commentStamp!
  554. I represent an special sequence node for block scopes.!
  555. !BlockSequenceNode methodsFor: 'testing'!
  556. isBlockSequenceNode
  557. ^ true
  558. ! !
  559. !BlockSequenceNode methodsFor: 'visiting'!
  560. accept: aVisitor
  561. ^ aVisitor visitBlockSequenceNode: self
  562. ! !
  563. Node subclass: #ValueNode
  564. instanceVariableNames: 'value'
  565. package: 'Compiler-AST'!
  566. !ValueNode commentStamp!
  567. I represent a value node.!
  568. !ValueNode methodsFor: 'accessing'!
  569. value
  570. ^ value
  571. !
  572. value: anObject
  573. value := anObject
  574. ! !
  575. !ValueNode methodsFor: 'testing'!
  576. isImmutable
  577. ^ self value isImmutable
  578. !
  579. isValueNode
  580. ^ true
  581. ! !
  582. !ValueNode methodsFor: 'visiting'!
  583. accept: aVisitor
  584. ^ aVisitor visitValueNode: self
  585. ! !
  586. ValueNode subclass: #VariableNode
  587. instanceVariableNames: 'assigned binding'
  588. package: 'Compiler-AST'!
  589. !VariableNode commentStamp!
  590. I represent an variable node.!
  591. !VariableNode methodsFor: 'accessing'!
  592. alias
  593. ^ self binding alias
  594. !
  595. assigned
  596. ^ assigned ifNil: [ false ]
  597. !
  598. assigned: aBoolean
  599. assigned := aBoolean
  600. !
  601. beAssigned
  602. self binding validateAssignment.
  603. assigned := true
  604. !
  605. binding
  606. ^ binding
  607. !
  608. binding: aScopeVar
  609. binding := aScopeVar
  610. !
  611. navigationLink
  612. ^ self value
  613. ! !
  614. !VariableNode methodsFor: 'testing'!
  615. isArgument
  616. ^ self binding isArgVar
  617. !
  618. isImmutable
  619. ^ self binding isImmutable
  620. !
  621. isNavigationNode
  622. ^ true
  623. !
  624. isVariableNode
  625. ^ true
  626. ! !
  627. !VariableNode methodsFor: 'visiting'!
  628. accept: aVisitor
  629. ^ aVisitor visitVariableNode: self
  630. ! !
  631. Object subclass: #NodeVisitor
  632. instanceVariableNames: ''
  633. package: 'Compiler-AST'!
  634. !NodeVisitor commentStamp!
  635. I am the abstract super class of all AST node visitors.!
  636. !NodeVisitor methodsFor: 'visiting'!
  637. visit: aNode
  638. ^ aNode accept: self
  639. !
  640. visitAll: aCollection
  641. ^ aCollection collect: [ :each | self visit: each ]
  642. !
  643. visitAssignmentNode: aNode
  644. ^ self visitNode: aNode
  645. !
  646. visitBlockNode: aNode
  647. ^ self visitNode: aNode
  648. !
  649. visitBlockSequenceNode: aNode
  650. ^ self visitSequenceNode: aNode
  651. !
  652. visitBranchSendNode: aNode
  653. ^ self visitNode: aNode
  654. !
  655. visitCascadeNode: aNode
  656. ^ self visitNode: aNode
  657. !
  658. visitDynamicArrayNode: aNode
  659. ^ self visitNode: aNode
  660. !
  661. visitDynamicDictionaryNode: aNode
  662. ^ self visitNode: aNode
  663. !
  664. visitJSStatementNode: aNode
  665. ^ self visitNode: aNode
  666. !
  667. visitMethodNode: aNode
  668. ^ self visitNode: aNode
  669. !
  670. visitNode: aNode
  671. ^ self visitAll: aNode nodes
  672. !
  673. visitRefNode: aNode
  674. ^ self visit: aNode node
  675. !
  676. visitReturnNode: aNode
  677. ^ self visitNode: aNode
  678. !
  679. visitSendNode: aNode
  680. ^ self visitNode: aNode
  681. !
  682. visitSequenceNode: aNode
  683. ^ self visitNode: aNode
  684. !
  685. visitValueNode: aNode
  686. ^ self visitNode: aNode
  687. !
  688. visitVariableNode: aNode
  689. ^ self visitNode: aNode
  690. ! !
  691. !CompiledMethod methodsFor: '*Compiler-AST'!
  692. ast
  693. self source ifEmpty: [ self error: 'Method source is empty' ].
  694. ^ Smalltalk parse: self source
  695. ! !
  696. !Object methodsFor: '*Compiler-AST'!
  697. isNode
  698. ^ false
  699. ! !