Compiler-Semantic.st 14 KB

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