Compiler-AST.st 13 KB

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