Compiler-AST.st 14 KB

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