Compiler-AST.st 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865
  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. nodes
  40. ^ nodes ifNil: [ nodes := Array new ]
  41. !
  42. parent
  43. ^ parent
  44. !
  45. parent: aNode
  46. parent := aNode
  47. !
  48. position
  49. "answer the line and column of the receiver in the source code"
  50. ^ position ifNil: [
  51. self parent ifNotNil: [ :node | node position ] ]
  52. !
  53. position: aPosition
  54. position := aPosition
  55. !
  56. positionEnd
  57. ^ self positionStart + ((self source lines size - 1) @ (self source lines last size - 1))
  58. !
  59. positionStart
  60. ^ self position
  61. !
  62. shouldBeAliased
  63. ^ shouldBeAliased ifNil: [ false ]
  64. !
  65. shouldBeAliased: aBoolean
  66. shouldBeAliased := aBoolean
  67. !
  68. shouldBeInlined
  69. ^ shouldBeInlined ifNil: [ false ]
  70. !
  71. shouldBeInlined: aBoolean
  72. shouldBeInlined := aBoolean
  73. !
  74. size
  75. ^ self source size
  76. !
  77. source
  78. ^ source ifNil: [ '' ]
  79. !
  80. source: aString
  81. source := aString
  82. ! !
  83. !Node methodsFor: 'building'!
  84. nodes: aCollection
  85. nodes := aCollection.
  86. aCollection do: [ :each | each parent: self ]
  87. ! !
  88. !Node methodsFor: 'copying'!
  89. postCopy
  90. super postCopy.
  91. self nodes do: [ :each | each parent: self ]
  92. ! !
  93. !Node methodsFor: 'testing'!
  94. inPosition: aPoint
  95. ^ (self positionStart <= aPoint and: [
  96. self positionEnd >= aPoint ])
  97. !
  98. isAssignmentNode
  99. ^ false
  100. !
  101. isBlockNode
  102. ^ false
  103. !
  104. isBlockSequenceNode
  105. ^ false
  106. !
  107. isCascadeNode
  108. ^ false
  109. !
  110. isImmutable
  111. ^ false
  112. !
  113. isJSStatementNode
  114. ^ false
  115. !
  116. isLastChild
  117. ^ self parent nodes last = self
  118. !
  119. isNavigationNode
  120. "Answer true if the node can be navigated to"
  121. ^ false
  122. !
  123. isNode
  124. ^ true
  125. !
  126. isReferenced
  127. "Answer true if the receiver is referenced by other nodes.
  128. Do not take sequences or assignments into account"
  129. ^ (self parent isSequenceNode or: [
  130. self parent isAssignmentNode ]) not
  131. !
  132. isReturnNode
  133. ^ false
  134. !
  135. isSendNode
  136. ^ false
  137. !
  138. isSequenceNode
  139. ^ false
  140. !
  141. isValueNode
  142. ^ false
  143. !
  144. isVariableNode
  145. ^ false
  146. !
  147. requiresSmalltalkContext
  148. "Answer true if the receiver requires a smalltalk context.
  149. Only send nodes require a context.
  150. If no node requires a context, the method will be compiled without one.
  151. See `IRJSTranslator` and `JSStream` for context creation"
  152. ^ (self nodes
  153. detect: [ :each | each requiresSmalltalkContext ]
  154. ifNone: [ nil ]) notNil
  155. !
  156. subtreeNeedsAliasing
  157. ^ (self shouldBeAliased or: [ self shouldBeInlined ]) or: [
  158. self nodes anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  159. ! !
  160. !Node methodsFor: 'visiting'!
  161. accept: aVisitor
  162. ^ aVisitor visitNode: self
  163. ! !
  164. Node subclass: #AssignmentNode
  165. instanceVariableNames: 'left right'
  166. package: 'Compiler-AST'!
  167. !AssignmentNode commentStamp!
  168. I represent an assignment node.!
  169. !AssignmentNode methodsFor: 'accessing'!
  170. left
  171. ^ left
  172. !
  173. left: aNode
  174. left := aNode.
  175. aNode parent: self
  176. !
  177. nodes
  178. ^ Array with: self left with: self right
  179. !
  180. right
  181. ^ right
  182. !
  183. right: aNode
  184. right := aNode.
  185. aNode parent: self
  186. ! !
  187. !AssignmentNode methodsFor: 'testing'!
  188. isAssignmentNode
  189. ^ true
  190. !
  191. shouldBeAliased
  192. ^ super shouldBeAliased or: [ self isReferenced ]
  193. ! !
  194. !AssignmentNode methodsFor: 'visiting'!
  195. accept: aVisitor
  196. ^ aVisitor visitAssignmentNode: self
  197. ! !
  198. Node subclass: #BlockNode
  199. instanceVariableNames: 'parameters scope'
  200. package: 'Compiler-AST'!
  201. !BlockNode commentStamp!
  202. I represent an block closure node.!
  203. !BlockNode methodsFor: 'accessing'!
  204. parameters
  205. ^ parameters ifNil: [ parameters := Array new ]
  206. !
  207. parameters: aCollection
  208. parameters := aCollection
  209. !
  210. scope
  211. ^ scope
  212. !
  213. scope: aLexicalScope
  214. scope := aLexicalScope
  215. ! !
  216. !BlockNode methodsFor: 'testing'!
  217. isBlockNode
  218. ^ true
  219. !
  220. subtreeNeedsAliasing
  221. ^ self shouldBeAliased or: [ self shouldBeInlined ]
  222. ! !
  223. !BlockNode methodsFor: 'visiting'!
  224. accept: aVisitor
  225. ^ aVisitor visitBlockNode: self
  226. ! !
  227. Node subclass: #CascadeNode
  228. instanceVariableNames: 'receiver'
  229. package: 'Compiler-AST'!
  230. !CascadeNode commentStamp!
  231. I represent an cascade node.!
  232. !CascadeNode methodsFor: 'accessing'!
  233. receiver
  234. ^ receiver
  235. !
  236. receiver: aNode
  237. receiver := aNode
  238. ! !
  239. !CascadeNode methodsFor: 'testing'!
  240. isCascadeNode
  241. ^ true
  242. !
  243. subtreeNeedsAliasing
  244. ^ self parent isSequenceNode not
  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: #ReturnNode
  343. instanceVariableNames: 'scope'
  344. package: 'Compiler-AST'!
  345. !ReturnNode commentStamp!
  346. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  347. !ReturnNode methodsFor: 'accessing'!
  348. scope
  349. ^ scope
  350. !
  351. scope: aLexicalScope
  352. scope := aLexicalScope
  353. ! !
  354. !ReturnNode methodsFor: 'testing'!
  355. isReturnNode
  356. ^ true
  357. !
  358. nonLocalReturn
  359. ^ self scope isMethodScope not
  360. ! !
  361. !ReturnNode methodsFor: 'visiting'!
  362. accept: aVisitor
  363. ^ aVisitor visitReturnNode: self
  364. ! !
  365. Node subclass: #SendNode
  366. instanceVariableNames: 'selector arguments receiver index'
  367. package: 'Compiler-AST'!
  368. !SendNode commentStamp!
  369. I represent an message send node.!
  370. !SendNode methodsFor: 'accessing'!
  371. arguments
  372. ^ arguments ifNil: [ arguments := #() ]
  373. !
  374. arguments: aCollection
  375. arguments := aCollection.
  376. aCollection do: [ :each | each parent: self ]
  377. !
  378. cascadeNodeWithMessages: aCollection
  379. | first |
  380. first := SendNode new
  381. selector: self selector;
  382. arguments: self arguments;
  383. yourself.
  384. ^ CascadeNode new
  385. receiver: self receiver;
  386. nodes: (Array with: first), aCollection;
  387. yourself
  388. !
  389. index
  390. ^ index
  391. !
  392. index: anInteger
  393. index := anInteger
  394. !
  395. navigationLink
  396. ^ self selector
  397. !
  398. nodes
  399. self receiver ifNil: [ ^ self arguments copy ].
  400. ^ (Array with: self receiver)
  401. addAll: self arguments;
  402. yourself
  403. !
  404. receiver
  405. ^ receiver
  406. !
  407. receiver: aNode
  408. receiver := aNode.
  409. aNode isNode ifTrue: [
  410. aNode parent: self ]
  411. !
  412. selector
  413. ^ selector
  414. !
  415. selector: aString
  416. selector := aString
  417. !
  418. superSend
  419. ^ self receiver value = 'super'
  420. ! !
  421. !SendNode methodsFor: 'building'!
  422. valueForReceiver: anObject
  423. ^ SendNode new
  424. position: self position;
  425. source: self source;
  426. receiver: (self receiver
  427. ifNil: [ anObject ]
  428. ifNotNil: [ self receiver valueForReceiver: anObject ]);
  429. selector: self selector;
  430. arguments: self arguments;
  431. yourself
  432. ! !
  433. !SendNode methodsFor: 'testing'!
  434. isCascadeSendNode
  435. ^ self parent isCascadeNode
  436. !
  437. isNavigationNode
  438. ^ true
  439. !
  440. isSendNode
  441. ^ true
  442. !
  443. requiresSmalltalkContext
  444. ^ true
  445. !
  446. shouldBeAliased
  447. "Because we keep track of send indexes, some send nodes need additional care for aliasing.
  448. See IRJSVisitor >> visitIRSend:"
  449. | sends |
  450. sends := (self method sendIndexes at: self selector) size.
  451. ^ (super shouldBeAliased or: [
  452. self isReferenced and: [
  453. (sends > 1 and: [ self index < sends ])
  454. or: [ self superSend ] ] ])
  455. ! !
  456. !SendNode methodsFor: 'visiting'!
  457. accept: aVisitor
  458. ^ aVisitor visitSendNode: self
  459. ! !
  460. Node subclass: #SequenceNode
  461. instanceVariableNames: 'temps scope'
  462. package: 'Compiler-AST'!
  463. !SequenceNode commentStamp!
  464. I represent an sequence node. A sequence represent a set of instructions inside the same scope (the method scope or a block scope).!
  465. !SequenceNode methodsFor: 'accessing'!
  466. scope
  467. ^ scope
  468. !
  469. scope: aLexicalScope
  470. scope := aLexicalScope
  471. !
  472. temps
  473. ^ temps ifNil: [ #() ]
  474. !
  475. temps: aCollection
  476. temps := aCollection
  477. ! !
  478. !SequenceNode methodsFor: 'building'!
  479. asBlockSequenceNode
  480. ^ BlockSequenceNode new
  481. position: self position;
  482. source: self source;
  483. nodes: self nodes;
  484. temps: self temps;
  485. yourself
  486. ! !
  487. !SequenceNode methodsFor: 'testing'!
  488. isSequenceNode
  489. ^ true
  490. ! !
  491. !SequenceNode methodsFor: 'visiting'!
  492. accept: aVisitor
  493. ^ aVisitor visitSequenceNode: self
  494. ! !
  495. SequenceNode subclass: #BlockSequenceNode
  496. instanceVariableNames: ''
  497. package: 'Compiler-AST'!
  498. !BlockSequenceNode commentStamp!
  499. I represent an special sequence node for block scopes.!
  500. !BlockSequenceNode methodsFor: 'testing'!
  501. isBlockSequenceNode
  502. ^ true
  503. ! !
  504. !BlockSequenceNode methodsFor: 'visiting'!
  505. accept: aVisitor
  506. ^ aVisitor visitBlockSequenceNode: self
  507. ! !
  508. Node subclass: #ValueNode
  509. instanceVariableNames: 'value'
  510. package: 'Compiler-AST'!
  511. !ValueNode commentStamp!
  512. I represent a value node.!
  513. !ValueNode methodsFor: 'accessing'!
  514. value
  515. ^ value
  516. !
  517. value: anObject
  518. value := anObject
  519. ! !
  520. !ValueNode methodsFor: 'testing'!
  521. isImmutable
  522. ^ self value isImmutable
  523. !
  524. isValueNode
  525. ^ true
  526. ! !
  527. !ValueNode methodsFor: 'visiting'!
  528. accept: aVisitor
  529. ^ aVisitor visitValueNode: self
  530. ! !
  531. ValueNode subclass: #VariableNode
  532. instanceVariableNames: 'assigned binding'
  533. package: 'Compiler-AST'!
  534. !VariableNode commentStamp!
  535. I represent an variable node.!
  536. !VariableNode methodsFor: 'accessing'!
  537. alias
  538. ^ self binding alias
  539. !
  540. assigned
  541. ^ assigned ifNil: [ false ]
  542. !
  543. assigned: aBoolean
  544. assigned := aBoolean
  545. !
  546. beAssigned
  547. self binding validateAssignment.
  548. assigned := true
  549. !
  550. binding
  551. ^ binding
  552. !
  553. binding: aScopeVar
  554. binding := aScopeVar
  555. !
  556. navigationLink
  557. ^ self value
  558. ! !
  559. !VariableNode methodsFor: 'testing'!
  560. isArgument
  561. ^ self binding isArgVar
  562. !
  563. isImmutable
  564. ^ self binding isImmutable
  565. !
  566. isNavigationNode
  567. ^ true
  568. !
  569. isVariableNode
  570. ^ true
  571. ! !
  572. !VariableNode methodsFor: 'visiting'!
  573. accept: aVisitor
  574. ^ aVisitor visitVariableNode: self
  575. ! !
  576. Object subclass: #NodeVisitor
  577. instanceVariableNames: ''
  578. package: 'Compiler-AST'!
  579. !NodeVisitor commentStamp!
  580. I am the abstract super class of all AST node visitors.!
  581. !NodeVisitor methodsFor: 'visiting'!
  582. visit: aNode
  583. ^ aNode accept: self
  584. !
  585. visitAll: aCollection
  586. ^ aCollection collect: [ :each | self visit: each ]
  587. !
  588. visitAssignmentNode: aNode
  589. ^ self visitNode: aNode
  590. !
  591. visitBlockNode: aNode
  592. ^ self visitNode: aNode
  593. !
  594. visitBlockSequenceNode: aNode
  595. ^ self visitSequenceNode: aNode
  596. !
  597. visitCascadeNode: aNode
  598. ^ self visitNode: aNode
  599. !
  600. visitDynamicArrayNode: aNode
  601. ^ self visitNode: aNode
  602. !
  603. visitDynamicDictionaryNode: aNode
  604. ^ self visitNode: aNode
  605. !
  606. visitJSStatementNode: aNode
  607. ^ self visitNode: aNode
  608. !
  609. visitMethodNode: aNode
  610. ^ self visitNode: aNode
  611. !
  612. visitNode: aNode
  613. ^ self visitAll: aNode nodes
  614. !
  615. visitReturnNode: aNode
  616. ^ self visitNode: aNode
  617. !
  618. visitSendNode: aNode
  619. ^ self visitNode: aNode
  620. !
  621. visitSequenceNode: aNode
  622. ^ self visitNode: aNode
  623. !
  624. visitValueNode: aNode
  625. ^ self visitNode: aNode
  626. !
  627. visitVariableNode: aNode
  628. ^ self visitNode: aNode
  629. ! !
  630. !CompiledMethod methodsFor: '*Compiler-AST'!
  631. ast
  632. self source ifEmpty: [ self error: 'Method source is empty' ].
  633. ^ Smalltalk parse: self source
  634. ! !
  635. !Object methodsFor: '*Compiler-AST'!
  636. isNode
  637. ^ false
  638. ! !