2
0

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. method
  22. ^ self parent ifNotNil: [ :node | node method ]
  23. !
  24. navigationNodeAt: aPoint ifAbsent: aBlock
  25. "Answer the navigation node in the receiver's tree at aPoint
  26. or nil if no navigation node was found.
  27. See `node >> isNaviationNode`"
  28. | children |
  29. children := self allNodes select: [ :each |
  30. each isNavigationNode and: [ each inPosition: aPoint ] ].
  31. children ifEmpty: [ ^ aBlock value ].
  32. ^ (children asArray sort: [ :a :b |
  33. (a positionStart dist: aPoint) <=
  34. (b positionStart dist: aPoint) ]) first
  35. !
  36. nodes
  37. ^ nodes ifNil: [ nodes := Array new ]
  38. !
  39. parent
  40. ^ parent
  41. !
  42. parent: aNode
  43. parent := aNode
  44. !
  45. position
  46. "answer the line and column of the receiver in the source code"
  47. ^ position ifNil: [
  48. self parent ifNotNil: [ :node | node position ] ]
  49. !
  50. position: aPosition
  51. position := aPosition
  52. !
  53. positionEnd
  54. ^ self positionStart + ((self source lines size - 1) @ (self source lines last size - 1))
  55. !
  56. positionStart
  57. ^ self position
  58. !
  59. shouldBeAliased
  60. ^ shouldBeAliased ifNil: [ false ]
  61. !
  62. shouldBeAliased: aBoolean
  63. shouldBeAliased := aBoolean
  64. !
  65. shouldBeInlined
  66. ^ shouldBeInlined ifNil: [ false ]
  67. !
  68. shouldBeInlined: aBoolean
  69. shouldBeInlined := aBoolean
  70. !
  71. size
  72. ^ self source size
  73. !
  74. source
  75. ^ source ifNil: [ '' ]
  76. !
  77. source: aString
  78. source := aString
  79. ! !
  80. !Node methodsFor: 'building'!
  81. nodes: aCollection
  82. nodes := aCollection.
  83. aCollection do: [ :each | each parent: self ]
  84. ! !
  85. !Node methodsFor: 'copying'!
  86. postCopy
  87. super postCopy.
  88. self nodes do: [ :each | each parent: self ]
  89. ! !
  90. !Node methodsFor: 'testing'!
  91. inPosition: aPoint
  92. ^ (self positionStart <= aPoint and: [
  93. self positionEnd >= aPoint ])
  94. !
  95. isAssignmentNode
  96. ^ false
  97. !
  98. isBlockNode
  99. ^ false
  100. !
  101. isBlockSequenceNode
  102. ^ false
  103. !
  104. isCascadeNode
  105. ^ false
  106. !
  107. isImmutable
  108. ^ false
  109. !
  110. isJSStatementNode
  111. ^ false
  112. !
  113. isLastChild
  114. ^ self parent nodes last = self
  115. !
  116. isNavigationNode
  117. "Answer true if the node can be navigated to"
  118. ^ false
  119. !
  120. isNode
  121. ^ true
  122. !
  123. isReferenced
  124. "Answer true if the receiver is referenced by other nodes.
  125. Do not take sequences or assignments into account"
  126. ^ (self parent isSequenceNode or: [
  127. self parent isAssignmentNode ]) not
  128. !
  129. isReturnNode
  130. ^ false
  131. !
  132. isSendNode
  133. ^ false
  134. !
  135. isSequenceNode
  136. ^ false
  137. !
  138. isValueNode
  139. ^ false
  140. !
  141. isVariableNode
  142. ^ false
  143. !
  144. requiresSmalltalkContext
  145. "Answer true if the receiver requires a smalltalk context.
  146. Only send nodes require a context.
  147. If no node requires a context, the method will be compiled without one.
  148. See `IRJSTranslator` and `JSStream` for context creation"
  149. ^ (self nodes
  150. detect: [ :each | each requiresSmalltalkContext ]
  151. ifNone: [ nil ]) notNil
  152. !
  153. subtreeNeedsAliasing
  154. ^ (self shouldBeAliased or: [ self shouldBeInlined ]) or: [
  155. self nodes anySatisfy: [ :each | each subtreeNeedsAliasing ] ]
  156. ! !
  157. !Node methodsFor: 'visiting'!
  158. accept: aVisitor
  159. ^ aVisitor visitNode: self
  160. ! !
  161. Node subclass: #AssignmentNode
  162. instanceVariableNames: 'left right'
  163. package: 'Compiler-AST'!
  164. !AssignmentNode commentStamp!
  165. I represent an assignment node.!
  166. !AssignmentNode methodsFor: 'accessing'!
  167. left
  168. ^ left
  169. !
  170. left: aNode
  171. left := aNode.
  172. aNode parent: self
  173. !
  174. nodes
  175. ^ Array with: self left with: self right
  176. !
  177. right
  178. ^ right
  179. !
  180. right: aNode
  181. right := aNode.
  182. aNode parent: self
  183. ! !
  184. !AssignmentNode methodsFor: 'testing'!
  185. isAssignmentNode
  186. ^ true
  187. !
  188. shouldBeAliased
  189. ^ super shouldBeAliased or: [ self isReferenced ]
  190. ! !
  191. !AssignmentNode methodsFor: 'visiting'!
  192. accept: aVisitor
  193. ^ aVisitor visitAssignmentNode: self
  194. ! !
  195. Node subclass: #BlockNode
  196. instanceVariableNames: 'parameters scope'
  197. package: 'Compiler-AST'!
  198. !BlockNode commentStamp!
  199. I represent an block closure node.!
  200. !BlockNode methodsFor: 'accessing'!
  201. parameters
  202. ^ parameters ifNil: [ parameters := Array new ]
  203. !
  204. parameters: aCollection
  205. parameters := aCollection
  206. !
  207. scope
  208. ^ scope
  209. !
  210. scope: aLexicalScope
  211. scope := aLexicalScope
  212. ! !
  213. !BlockNode methodsFor: 'testing'!
  214. isBlockNode
  215. ^ true
  216. !
  217. subtreeNeedsAliasing
  218. ^ self shouldBeAliased or: [ self shouldBeInlined ]
  219. ! !
  220. !BlockNode methodsFor: 'visiting'!
  221. accept: aVisitor
  222. ^ aVisitor visitBlockNode: self
  223. ! !
  224. Node subclass: #CascadeNode
  225. instanceVariableNames: 'receiver'
  226. package: 'Compiler-AST'!
  227. !CascadeNode commentStamp!
  228. I represent an cascade node.!
  229. !CascadeNode methodsFor: 'accessing'!
  230. receiver
  231. ^ receiver
  232. !
  233. receiver: aNode
  234. receiver := aNode
  235. ! !
  236. !CascadeNode methodsFor: 'testing'!
  237. isCascadeNode
  238. ^ true
  239. ! !
  240. !CascadeNode methodsFor: 'visiting'!
  241. accept: aVisitor
  242. ^ aVisitor visitCascadeNode: self
  243. ! !
  244. Node subclass: #DynamicArrayNode
  245. instanceVariableNames: ''
  246. package: 'Compiler-AST'!
  247. !DynamicArrayNode commentStamp!
  248. I represent an dynamic array node.!
  249. !DynamicArrayNode methodsFor: 'visiting'!
  250. accept: aVisitor
  251. ^ aVisitor visitDynamicArrayNode: self
  252. ! !
  253. Node subclass: #DynamicDictionaryNode
  254. instanceVariableNames: ''
  255. package: 'Compiler-AST'!
  256. !DynamicDictionaryNode commentStamp!
  257. I represent an dynamic dictionary node.!
  258. !DynamicDictionaryNode methodsFor: 'visiting'!
  259. accept: aVisitor
  260. ^ aVisitor visitDynamicDictionaryNode: self
  261. ! !
  262. Node subclass: #JSStatementNode
  263. instanceVariableNames: ''
  264. package: 'Compiler-AST'!
  265. !JSStatementNode commentStamp!
  266. I represent an JavaScript statement node.!
  267. !JSStatementNode methodsFor: 'testing'!
  268. isJSStatementNode
  269. ^ true
  270. !
  271. requiresSmalltalkContext
  272. ^ true
  273. ! !
  274. !JSStatementNode methodsFor: 'visiting'!
  275. accept: aVisitor
  276. ^ aVisitor visitJSStatementNode: self
  277. ! !
  278. Node subclass: #MethodNode
  279. instanceVariableNames: 'selector arguments source scope classReferences sendIndexes'
  280. package: 'Compiler-AST'!
  281. !MethodNode commentStamp!
  282. I represent an method node.
  283. A method node must be the root and only method node of a valid AST.!
  284. !MethodNode methodsFor: 'accessing'!
  285. arguments
  286. ^ arguments ifNil: [ #() ]
  287. !
  288. arguments: aCollection
  289. arguments := aCollection
  290. !
  291. classReferences
  292. ^ classReferences
  293. !
  294. classReferences: aCollection
  295. classReferences := aCollection
  296. !
  297. messageSends
  298. ^ self sendIndexes keys
  299. !
  300. method
  301. ^ self
  302. !
  303. scope
  304. ^ scope
  305. !
  306. scope: aMethodScope
  307. scope := aMethodScope
  308. !
  309. selector
  310. ^ selector
  311. !
  312. selector: aString
  313. selector := aString
  314. !
  315. sendIndexes
  316. ^ sendIndexes
  317. !
  318. sendIndexes: aDictionary
  319. sendIndexes := aDictionary
  320. !
  321. sequenceNode
  322. self nodes do: [ :each |
  323. each isSequenceNode ifTrue: [ ^ each ] ].
  324. ^ nil
  325. !
  326. source
  327. ^ source
  328. !
  329. source: aString
  330. source := aString
  331. ! !
  332. !MethodNode methodsFor: 'visiting'!
  333. accept: aVisitor
  334. ^ aVisitor visitMethodNode: self
  335. ! !
  336. Node subclass: #ReturnNode
  337. instanceVariableNames: 'scope'
  338. package: 'Compiler-AST'!
  339. !ReturnNode commentStamp!
  340. I represent an return node. At the AST level, there is not difference between a local return or non-local return.!
  341. !ReturnNode methodsFor: 'accessing'!
  342. scope
  343. ^ scope
  344. !
  345. scope: aLexicalScope
  346. scope := aLexicalScope
  347. ! !
  348. !ReturnNode methodsFor: 'testing'!
  349. isReturnNode
  350. ^ true
  351. !
  352. nonLocalReturn
  353. ^ self scope isMethodScope not
  354. ! !
  355. !ReturnNode methodsFor: 'visiting'!
  356. accept: aVisitor
  357. ^ aVisitor visitReturnNode: self
  358. ! !
  359. Node subclass: #SendNode
  360. instanceVariableNames: 'selector arguments receiver superSend index'
  361. package: 'Compiler-AST'!
  362. !SendNode commentStamp!
  363. I represent an message send node.!
  364. !SendNode methodsFor: 'accessing'!
  365. arguments
  366. ^ arguments ifNil: [ arguments := #() ]
  367. !
  368. arguments: aCollection
  369. arguments := aCollection.
  370. aCollection do: [ :each | each parent: self ]
  371. !
  372. cascadeNodeWithMessages: aCollection
  373. | first |
  374. first := SendNode new
  375. selector: self selector;
  376. arguments: self arguments;
  377. yourself.
  378. ^ CascadeNode new
  379. receiver: self receiver;
  380. nodes: (Array with: first), aCollection;
  381. yourself
  382. !
  383. index
  384. ^ index
  385. !
  386. index: anInteger
  387. index := anInteger
  388. !
  389. navigationLink
  390. ^ self selector
  391. !
  392. nodes
  393. self receiver ifNil: [ ^ self arguments copy ].
  394. ^ (Array with: self receiver)
  395. addAll: self arguments;
  396. yourself
  397. !
  398. receiver
  399. ^ receiver
  400. !
  401. receiver: aNode
  402. receiver := aNode.
  403. aNode isNode ifTrue: [
  404. aNode parent: self ]
  405. !
  406. selector
  407. ^ selector
  408. !
  409. selector: aString
  410. selector := aString
  411. !
  412. superSend
  413. ^ superSend ifNil: [ false ]
  414. !
  415. superSend: aBoolean
  416. superSend := aBoolean
  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. ! !