Compiler-AST.st 14 KB

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