Compiler-Semantic.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. Smalltalk createPackage: 'Compiler-Semantic'!
  2. Object subclass: #LexicalScope
  3. slots: {#node. #instruction. #temps. #args. #outerScope. #blockIndex}
  4. package: 'Compiler-Semantic'!
  5. !LexicalScope commentStamp!
  6. I represent a lexical scope where variable names are associated with ScopeVars
  7. Instances are used for block scopes. Method scopes are instances of MethodLexicalScope.
  8. I am attached to a ScopeVar and method/block nodes.
  9. Each context (method/closure) get a fresh scope that inherits from its outer scope.!
  10. !LexicalScope methodsFor: 'accessing'!
  11. alias
  12. ^ '$ctx', self scopeLevel asString
  13. !
  14. allVariableNames
  15. ^ self args keys, self temps keys
  16. !
  17. args
  18. ^ args ifNil: [ args := Dictionary new ]
  19. !
  20. bindingFor: aNode
  21. | identifier |
  22. identifier := aNode value.
  23. ^ self pseudoVars at: identifier ifAbsent: [
  24. self args at: identifier ifAbsent: [
  25. self temps at: identifier ifAbsent: [ nil ]]]
  26. !
  27. blockIndex
  28. ^ blockIndex ifNil: [ 0 ]
  29. !
  30. blockIndex: anInteger
  31. blockIndex := anInteger
  32. !
  33. instruction
  34. ^ instruction
  35. !
  36. instruction: anIRInstruction
  37. instruction := anIRInstruction
  38. !
  39. lookupVariable: aNode
  40. | lookup |
  41. lookup := (self bindingFor: aNode).
  42. lookup ifNil: [
  43. lookup := self outerScope ifNotNil: [
  44. (self outerScope lookupVariable: aNode) ]].
  45. ^ lookup
  46. !
  47. methodScope
  48. ^ self outerScope ifNotNil: [
  49. self outerScope methodScope ]
  50. !
  51. node
  52. "Answer the node in which I am defined"
  53. ^ node
  54. !
  55. node: aNode
  56. node := aNode
  57. !
  58. outerScope
  59. ^ outerScope
  60. !
  61. outerScope: aLexicalScope
  62. outerScope := aLexicalScope
  63. !
  64. pseudoVars
  65. ^ self methodScope pseudoVars
  66. !
  67. scopeLevel
  68. self outerScope ifNil: [ ^ 1 ].
  69. self isInlined ifTrue: [ ^ self outerScope scopeLevel ].
  70. ^ self outerScope scopeLevel + 1
  71. !
  72. temps
  73. ^ temps ifNil: [ temps := Dictionary new ]
  74. ! !
  75. !LexicalScope methodsFor: 'adding'!
  76. addArg: aString
  77. self args at: aString put: (ArgVar on: aString).
  78. (self args at: aString) scope: self
  79. !
  80. addTemp: aString
  81. self temps at: aString put: (TempVar on: aString).
  82. (self temps at: aString) scope: self
  83. ! !
  84. !LexicalScope methodsFor: 'testing'!
  85. canFlattenNonLocalReturns
  86. ^ self isInlined and: [ self outerScope canFlattenNonLocalReturns ]
  87. !
  88. isBlockScope
  89. ^ self isMethodScope not
  90. !
  91. isInlined
  92. ^ self instruction notNil and: [
  93. self instruction isInlined ]
  94. !
  95. isMethodScope
  96. ^ false
  97. ! !
  98. LexicalScope subclass: #MethodLexicalScope
  99. slots: {#iVars. #pseudoVars. #localReturn. #nonLocalReturns}
  100. package: 'Compiler-Semantic'!
  101. !MethodLexicalScope commentStamp!
  102. I represent a method scope.!
  103. !MethodLexicalScope methodsFor: 'accessing'!
  104. allVariableNames
  105. ^ super allVariableNames, self iVars keys
  106. !
  107. bindingFor: aNode
  108. ^ (super bindingFor: aNode) ifNil: [
  109. self iVars at: aNode value ifAbsent: [ nil ]]
  110. !
  111. iVars
  112. ^ iVars ifNil: [ iVars := Dictionary new ]
  113. !
  114. localReturn
  115. ^ localReturn ifNil: [ false ]
  116. !
  117. localReturn: aBoolean
  118. localReturn := aBoolean
  119. !
  120. methodScope
  121. ^ self
  122. !
  123. nonLocalReturns
  124. ^ nonLocalReturns ifNil: [ nonLocalReturns := OrderedCollection new ]
  125. !
  126. pseudoVars
  127. pseudoVars ifNil: [
  128. pseudoVars := Dictionary new.
  129. PseudoVar dictionary keysAndValuesDo: [ :each :impl |
  130. pseudoVars at: each put: ((impl on: each)
  131. scope: self methodScope;
  132. yourself) ] ].
  133. ^ pseudoVars
  134. ! !
  135. !MethodLexicalScope methodsFor: 'adding'!
  136. addIVar: aString
  137. self iVars at: aString put: (InstanceVar on: aString).
  138. (self iVars at: aString) scope: self
  139. !
  140. addNonLocalReturn: aScope
  141. self nonLocalReturns add: aScope
  142. !
  143. removeNonLocalReturn: aScope
  144. self nonLocalReturns remove: aScope ifAbsent: []
  145. ! !
  146. !MethodLexicalScope methodsFor: 'testing'!
  147. canFlattenNonLocalReturns
  148. ^ true
  149. !
  150. hasLocalReturn
  151. ^ self localReturn
  152. !
  153. hasNonLocalReturn
  154. ^ self nonLocalReturns notEmpty
  155. !
  156. isMethodScope
  157. ^ true
  158. ! !
  159. Object subclass: #ScopeVar
  160. slots: {#scope. #name}
  161. package: 'Compiler-Semantic'!
  162. !ScopeVar commentStamp!
  163. I am an entry in a LexicalScope that gets associated with variable nodes of the same name.
  164. There are 4 different subclasses of vars: temp vars, local vars, args, and unknown/global vars.!
  165. !ScopeVar methodsFor: 'accessing'!
  166. alias
  167. ^ self name asVariableName
  168. !
  169. name
  170. ^ name
  171. !
  172. name: aString
  173. name := aString
  174. !
  175. scope
  176. ^ scope
  177. !
  178. scope: aScope
  179. scope := aScope
  180. ! !
  181. !ScopeVar methodsFor: 'testing'!
  182. isArgVar
  183. ^ false
  184. !
  185. isClassRefVar
  186. ^ false
  187. !
  188. isImmutable
  189. ^ false
  190. !
  191. isInstanceVar
  192. ^ false
  193. !
  194. isPseudoVar
  195. ^ false
  196. !
  197. isSelf
  198. ^ false
  199. !
  200. isSuper
  201. ^ false
  202. !
  203. isTempVar
  204. ^ false
  205. !
  206. isUnknownVar
  207. ^ false
  208. !
  209. validateAssignment
  210. (self isArgVar or: [ self isPseudoVar ]) ifTrue: [
  211. InvalidAssignmentError new
  212. variableName: self name;
  213. signal]
  214. ! !
  215. !ScopeVar class methodsFor: 'instance creation'!
  216. on: aString
  217. ^ self new
  218. name: aString;
  219. yourself
  220. ! !
  221. ScopeVar subclass: #AliasVar
  222. slots: {#node}
  223. package: 'Compiler-Semantic'!
  224. !AliasVar commentStamp!
  225. I am an internally defined variable by the compiler!
  226. !AliasVar methodsFor: 'accessing'!
  227. node
  228. ^ node
  229. !
  230. node: aNode
  231. node := aNode
  232. ! !
  233. !AliasVar methodsFor: 'testing'!
  234. isImmutable
  235. ^ true
  236. ! !
  237. ScopeVar subclass: #ArgVar
  238. slots: {}
  239. package: 'Compiler-Semantic'!
  240. !ArgVar commentStamp!
  241. I am an argument of a method or block.!
  242. !ArgVar methodsFor: 'testing'!
  243. isArgVar
  244. ^ true
  245. !
  246. isImmutable
  247. ^ true
  248. ! !
  249. ScopeVar subclass: #ClassRefVar
  250. slots: {}
  251. package: 'Compiler-Semantic'!
  252. !ClassRefVar commentStamp!
  253. I am an class reference variable!
  254. !ClassRefVar methodsFor: 'accessing'!
  255. alias
  256. ^ '$globals.', self name
  257. ! !
  258. !ClassRefVar methodsFor: 'testing'!
  259. isClassRefVar
  260. ^ true
  261. !
  262. isImmutable
  263. ^ true
  264. ! !
  265. ScopeVar subclass: #InstanceVar
  266. slots: {}
  267. package: 'Compiler-Semantic'!
  268. !InstanceVar commentStamp!
  269. I am an instance variable of a method or block.!
  270. !InstanceVar methodsFor: 'testing'!
  271. alias
  272. ^ '$self.', self name
  273. !
  274. isInstanceVar
  275. ^ true
  276. ! !
  277. ScopeVar subclass: #PseudoVar
  278. slots: {}
  279. package: 'Compiler-Semantic'!
  280. !PseudoVar commentStamp!
  281. I am an pseudo variable.
  282. The five Smalltalk pseudo variables are: 'self', 'super', 'nil', 'true' and 'false'!
  283. !PseudoVar methodsFor: 'accessing'!
  284. alias
  285. ^ self name
  286. ! !
  287. !PseudoVar methodsFor: 'testing'!
  288. isImmutable
  289. ^ true
  290. !
  291. isPseudoVar
  292. ^ true
  293. !
  294. isSelf
  295. ^ name = 'self'
  296. ! !
  297. PseudoVar class slots: {#dictionary}!
  298. !PseudoVar class methodsFor: 'accessing'!
  299. dictionary
  300. ^ dictionary ifNil: [ dictionary := Dictionary new
  301. at: #self put: PseudoVar;
  302. at: #super put: SuperVar;
  303. at: #nil put: PseudoVar;
  304. at: #false put: PseudoVar;
  305. at: #true put: PseudoVar;
  306. at: #thisContext put: ThisContextVar;
  307. yourself ]
  308. ! !
  309. PseudoVar subclass: #SuperVar
  310. slots: {}
  311. package: 'Compiler-Semantic'!
  312. !SuperVar commentStamp!
  313. I am a 'super' pseudo variable.!
  314. !SuperVar methodsFor: 'accessing'!
  315. alias
  316. ^ '$self'
  317. !
  318. lookupAsJavaScriptSource
  319. ^ '($methodClass.superclass||$boot.nilAsClass).fn.prototype'
  320. ! !
  321. !SuperVar methodsFor: 'testing'!
  322. isSuper
  323. ^ true
  324. ! !
  325. PseudoVar subclass: #ThisContextVar
  326. slots: {}
  327. package: 'Compiler-Semantic'!
  328. !ThisContextVar commentStamp!
  329. I am a 'thisContext' pseudo variable.!
  330. !ThisContextVar methodsFor: 'accessing'!
  331. alias
  332. ^ '$core.getThisContext()'
  333. ! !
  334. ScopeVar subclass: #TempVar
  335. slots: {}
  336. package: 'Compiler-Semantic'!
  337. !TempVar commentStamp!
  338. I am an temporary variable of a method or block.!
  339. !TempVar methodsFor: 'testing'!
  340. isTempVar
  341. ^ true
  342. ! !
  343. ScopeVar subclass: #UnknownVar
  344. slots: {}
  345. package: 'Compiler-Semantic'!
  346. !UnknownVar commentStamp!
  347. I am an unknown variable. Amber uses unknown variables as JavaScript globals!
  348. !UnknownVar methodsFor: 'testing'!
  349. isUnknownVar
  350. ^ true
  351. ! !
  352. NodeVisitor subclass: #SemanticAnalyzer
  353. slots: {#currentScope. #blockIndex. #thePackage. #theClass. #classReferences. #messageSends}
  354. package: 'Compiler-Semantic'!
  355. !SemanticAnalyzer commentStamp!
  356. I semantically analyze the abstract syntax tree and annotate it with informations such as non local returns and variable scopes.!
  357. !SemanticAnalyzer methodsFor: 'accessing'!
  358. classReferences
  359. ^ classReferences ifNil: [ classReferences := Set new ]
  360. !
  361. messageSends
  362. ^ messageSends ifNil: [ messageSends := Dictionary new ]
  363. !
  364. theClass
  365. ^ theClass
  366. !
  367. theClass: aClass
  368. theClass := aClass
  369. !
  370. thePackage
  371. ^ thePackage
  372. !
  373. thePackage: aPackage
  374. thePackage := aPackage
  375. ! !
  376. !SemanticAnalyzer methodsFor: 'error handling'!
  377. errorShadowingVariable: aString
  378. ShadowingVariableError new
  379. variableName: aString;
  380. signal
  381. !
  382. errorUnknownVariable: aNode
  383. "Throw an error if the variable is undeclared in the global JS scope (i.e. window).
  384. We allow all variables listed by Smalltalk>>#globalJsVariables.
  385. This list includes: `window`, `document`, `process` and `global`
  386. for nodejs and browser environments.
  387. This is only to make sure compilation works on both browser-based and nodejs environments.
  388. The ideal solution would be to use a pragma instead"
  389. | identifier |
  390. identifier := aNode value.
  391. ((Smalltalk globalJsVariables includes: identifier)
  392. or: [ self isVariableKnown: identifier inPackage: self thePackage ])
  393. ifFalse: [
  394. UnknownVariableError new
  395. variableName: aNode value;
  396. signal ]
  397. ! !
  398. !SemanticAnalyzer methodsFor: 'factory'!
  399. newBlockScope
  400. ^ self newScopeOfClass: LexicalScope
  401. !
  402. newMethodScope
  403. ^ self newScopeOfClass: MethodLexicalScope
  404. !
  405. newScopeOfClass: aLexicalScopeClass
  406. ^ aLexicalScopeClass new
  407. outerScope: currentScope;
  408. yourself
  409. ! !
  410. !SemanticAnalyzer methodsFor: 'private'!
  411. nextBlockIndex
  412. blockIndex ifNil: [ blockIndex := 0 ].
  413. blockIndex := blockIndex + 1.
  414. ^ blockIndex
  415. ! !
  416. !SemanticAnalyzer methodsFor: 'scope'!
  417. popScope
  418. currentScope ifNotNil: [
  419. currentScope := currentScope outerScope ]
  420. !
  421. pushScope: aScope
  422. aScope outerScope: currentScope.
  423. currentScope := aScope
  424. !
  425. validateVariableScope: aString
  426. "Validate the variable scope in by doing a recursive lookup, up to the method scope"
  427. (currentScope lookupVariable: aString) ifNotNil: [
  428. self errorShadowingVariable: aString ]
  429. ! !
  430. !SemanticAnalyzer methodsFor: 'testing'!
  431. isVariableKnown: aString inPackage: aPackage
  432. aPackage ifNotNil: [
  433. | packageKnownVars |
  434. packageKnownVars := (aPackage imports
  435. reject: #isString)
  436. collect: #key.
  437. (packageKnownVars includes: aString) ifTrue: [ ^ true ]].
  438. ^ Compiler new
  439. eval: 'typeof ', aString, ' !!== "undefined"'
  440. forPackage: aPackage
  441. ! !
  442. !SemanticAnalyzer methodsFor: 'visiting'!
  443. visitAssignmentNode: aNode
  444. super visitAssignmentNode: aNode.
  445. aNode left beAssigned
  446. !
  447. visitBlockNode: aNode
  448. self pushScope: self newBlockScope.
  449. aNode scope: currentScope.
  450. currentScope node: aNode.
  451. currentScope blockIndex: self nextBlockIndex.
  452. aNode parameters do: [ :each |
  453. self validateVariableScope: each.
  454. currentScope addArg: each ].
  455. super visitBlockNode: aNode.
  456. self popScope
  457. !
  458. visitCascadeNode: aNode
  459. aNode receiver: aNode dagChildren first receiver.
  460. super visitCascadeNode: aNode
  461. !
  462. visitMethodNode: aNode
  463. self pushScope: self newMethodScope.
  464. aNode scope: currentScope.
  465. currentScope node: aNode.
  466. self theClass allInstanceVariableNames do: [ :each |
  467. currentScope addIVar: each ].
  468. aNode arguments do: [ :each |
  469. self validateVariableScope: each.
  470. currentScope addArg: each ].
  471. super visitMethodNode: aNode.
  472. aNode
  473. classReferences: self classReferences;
  474. sendIndexes: self messageSends.
  475. self popScope.
  476. ^ aNode
  477. !
  478. visitReturnNode: aNode
  479. aNode scope: currentScope.
  480. currentScope isMethodScope
  481. ifTrue: [ currentScope localReturn: true ]
  482. ifFalse: [ currentScope methodScope addNonLocalReturn: currentScope ].
  483. super visitReturnNode: aNode
  484. !
  485. visitSendNode: aNode
  486. | sends |
  487. sends := self messageSends at: aNode selector ifAbsentPut: [ OrderedCollection new ].
  488. sends add: aNode.
  489. aNode index: sends size.
  490. super visitSendNode: aNode
  491. !
  492. visitSequenceNode: aNode
  493. aNode temps do: [ :each |
  494. self validateVariableScope: each.
  495. currentScope addTemp: each ].
  496. super visitSequenceNode: aNode
  497. !
  498. visitVariableNode: aNode
  499. "Bind a ScopeVar to aNode by doing a lookup in the current scope.
  500. If no ScopeVar is found, bind a UnknowVar and throw an error."
  501. | binding |
  502. binding := currentScope lookupVariable: aNode.
  503. binding ifNil: [
  504. aNode value isCapitalized
  505. ifTrue: [ "Capital letter variables might be globals."
  506. binding := ClassRefVar new name: aNode value; yourself.
  507. self classReferences add: aNode value]
  508. ifFalse: [
  509. self errorUnknownVariable: aNode.
  510. binding := UnknownVar new name: aNode value; yourself ] ].
  511. aNode binding: binding.
  512. ! !
  513. !SemanticAnalyzer class methodsFor: 'instance creation'!
  514. on: aClass
  515. ^ self new
  516. theClass: aClass;
  517. yourself
  518. ! !
  519. CompilerError subclass: #SemanticError
  520. slots: {}
  521. package: 'Compiler-Semantic'!
  522. !SemanticError commentStamp!
  523. I represent an abstract semantic error thrown by the SemanticAnalyzer.
  524. Semantic errors can be unknown variable errors, etc.
  525. See my subclasses for concrete errors.
  526. The IDE should catch instances of Semantic error to deal with them when compiling!
  527. SemanticError subclass: #InvalidAssignmentError
  528. slots: {#variableName}
  529. package: 'Compiler-Semantic'!
  530. !InvalidAssignmentError commentStamp!
  531. I get signaled when a pseudo variable gets assigned.!
  532. !InvalidAssignmentError methodsFor: 'accessing'!
  533. messageText
  534. ^ ' Invalid assignment to variable: ', self variableName
  535. !
  536. variableName
  537. ^ variableName
  538. !
  539. variableName: aString
  540. variableName := aString
  541. ! !
  542. SemanticError subclass: #ShadowingVariableError
  543. slots: {#variableName}
  544. package: 'Compiler-Semantic'!
  545. !ShadowingVariableError commentStamp!
  546. I get signaled when a variable in a block or method scope shadows a variable of the same name in an outer scope.!
  547. !ShadowingVariableError methodsFor: 'accessing'!
  548. messageText
  549. ^ 'Variable shadowing error: ', self variableName, ' is already defined'
  550. !
  551. variableName
  552. ^ variableName
  553. !
  554. variableName: aString
  555. variableName := aString
  556. ! !
  557. SemanticError subclass: #UnknownVariableError
  558. slots: {#variableName}
  559. package: 'Compiler-Semantic'!
  560. !UnknownVariableError commentStamp!
  561. I get signaled when a variable is not defined.
  562. The default behavior is to allow it, as this is how Amber currently is able to seamlessly send messages to JavaScript objects.!
  563. !UnknownVariableError methodsFor: 'accessing'!
  564. messageText
  565. ^ 'Unknown Variable error: ', self variableName, ' is not defined'
  566. !
  567. variableName
  568. ^ variableName
  569. !
  570. variableName: aString
  571. variableName := aString
  572. ! !