Compiler-Semantic.st 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722
  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. ScopeVar subclass: #InstanceVar
  250. slots: {}
  251. package: 'Compiler-Semantic'!
  252. !InstanceVar commentStamp!
  253. I am an instance variable of a method or block.!
  254. !InstanceVar methodsFor: 'testing'!
  255. alias
  256. ^ '$self.', self name
  257. !
  258. isInstanceVar
  259. ^ true
  260. ! !
  261. ScopeVar subclass: #PseudoVar
  262. slots: {}
  263. package: 'Compiler-Semantic'!
  264. !PseudoVar commentStamp!
  265. I am an pseudo variable.
  266. The five Smalltalk pseudo variables are: 'self', 'super', 'nil', 'true' and 'false'!
  267. !PseudoVar methodsFor: 'accessing'!
  268. alias
  269. ^ self name
  270. ! !
  271. !PseudoVar methodsFor: 'testing'!
  272. isImmutable
  273. ^ true
  274. !
  275. isPseudoVar
  276. ^ true
  277. ! !
  278. PseudoVar class slots: {#dictionary. #receiverNames}!
  279. !PseudoVar class methodsFor: 'accessing'!
  280. dictionary
  281. ^ dictionary ifNil: [ dictionary := Dictionary new
  282. at: #self put: PseudoVar;
  283. at: #super put: SuperVar;
  284. at: #nil put: PseudoVar;
  285. at: #false put: PseudoVar;
  286. at: #true put: PseudoVar;
  287. at: #thisContext put: ThisContextVar;
  288. yourself ]
  289. !
  290. receiverNames
  291. ^ receiverNames ifNil: [ receiverNames := Dictionary new
  292. at: #self put: '$self';
  293. at: #super put: '$self';
  294. at: #nil put: '$nil';
  295. yourself ]
  296. ! !
  297. PseudoVar subclass: #SuperVar
  298. slots: {}
  299. package: 'Compiler-Semantic'!
  300. !SuperVar commentStamp!
  301. I am a 'super' pseudo variable.!
  302. !SuperVar methodsFor: 'accessing'!
  303. lookupAsJavaScriptSource
  304. ^ '($methodClass.superclass||$boot.nilAsClass).fn.prototype'
  305. ! !
  306. !SuperVar methodsFor: 'testing'!
  307. isSuper
  308. ^ true
  309. ! !
  310. PseudoVar subclass: #ThisContextVar
  311. slots: {}
  312. package: 'Compiler-Semantic'!
  313. !ThisContextVar commentStamp!
  314. I am a 'thisContext' pseudo variable.!
  315. !ThisContextVar methodsFor: 'accessing'!
  316. alias
  317. ^ '$core.getThisContext()'
  318. ! !
  319. ScopeVar subclass: #TempVar
  320. slots: {}
  321. package: 'Compiler-Semantic'!
  322. !TempVar commentStamp!
  323. I am an temporary variable of a method or block.!
  324. !TempVar methodsFor: 'testing'!
  325. isTempVar
  326. ^ true
  327. ! !
  328. NodeVisitor subclass: #SemanticAnalyzer
  329. slots: {#currentScope. #blockIndex. #thePackage. #theClass. #classReferences. #messageSends}
  330. package: 'Compiler-Semantic'!
  331. !SemanticAnalyzer commentStamp!
  332. I semantically analyze the abstract syntax tree and annotate it with informations such as non local returns and variable scopes.!
  333. !SemanticAnalyzer methodsFor: 'accessing'!
  334. classReferences
  335. ^ classReferences ifNil: [ classReferences := Set new ]
  336. !
  337. messageSends
  338. ^ messageSends ifNil: [ messageSends := Dictionary new ]
  339. !
  340. theClass
  341. ^ theClass
  342. !
  343. theClass: aClass
  344. theClass := aClass
  345. !
  346. thePackage
  347. ^ thePackage
  348. !
  349. thePackage: aPackage
  350. thePackage := aPackage
  351. ! !
  352. !SemanticAnalyzer methodsFor: 'error handling'!
  353. errorInvalidAssignment: aString
  354. InvalidAssignmentError new
  355. variableName: aString;
  356. signal
  357. !
  358. errorShadowingVariable: aString
  359. ShadowingVariableError new
  360. variableName: aString;
  361. signal
  362. !
  363. errorUnknownVariable: aString
  364. UnknownVariableError new
  365. variableName: aString;
  366. signal
  367. ! !
  368. !SemanticAnalyzer methodsFor: 'factory'!
  369. newBlockScope
  370. ^ self newScopeOfClass: LexicalScope
  371. !
  372. newMethodScope
  373. ^ self newScopeOfClass: MethodLexicalScope
  374. !
  375. newScopeOfClass: aLexicalScopeClass
  376. ^ aLexicalScopeClass new
  377. outerScope: currentScope;
  378. yourself
  379. ! !
  380. !SemanticAnalyzer methodsFor: 'private'!
  381. bindUnscopedVariable: aString
  382. aString isCapitalized ifTrue: [ "Capital letter variables might be globals."
  383. self classReferences add: aString.
  384. ^ ClassRefVar new name: aString; yourself ].
  385. "Throw an error if the variable is undeclared in the global JS scope (i.e. window).
  386. We allow all variables listed by Smalltalk>>#globalJsVariables.
  387. This list includes: `window`, `document`, `process` and `global`
  388. for nodejs and browser environments.
  389. This is only to make sure compilation works on both browser-based and nodejs environments.
  390. The ideal solution would be to use a pragma instead"
  391. ((Smalltalk globalJsVariables includes: aString)
  392. or: [ self isVariableKnown: aString inPackage: self thePackage ]) ifTrue: [
  393. ^ ExternallyKnownVar new name: aString; yourself ].
  394. self errorUnknownVariable: aString
  395. !
  396. nextBlockIndex
  397. blockIndex ifNil: [ blockIndex := 0 ].
  398. blockIndex := blockIndex + 1.
  399. ^ blockIndex
  400. ! !
  401. !SemanticAnalyzer methodsFor: 'scope'!
  402. popScope
  403. currentScope ifNotNil: [
  404. currentScope := currentScope outerScope ]
  405. !
  406. pushScope: aScope
  407. aScope outerScope: currentScope.
  408. currentScope := aScope
  409. !
  410. validateVariableScope: aString
  411. "Validate the variable scope in by doing a recursive lookup, up to the method scope"
  412. (currentScope lookupVariable: aString) ifNotNil: [
  413. self errorShadowingVariable: aString ]
  414. ! !
  415. !SemanticAnalyzer methodsFor: 'testing'!
  416. isVariableKnown: aString inPackage: aPackage
  417. ^ Compiler new
  418. eval: 'typeof(', aString, ')!!== "undefined"||(function(){try{return(', aString, ',true)}catch(_){return false}})()'
  419. forPackage: aPackage
  420. ! !
  421. !SemanticAnalyzer methodsFor: 'visiting'!
  422. visitAssignmentNode: aNode
  423. | lhs |
  424. super visitAssignmentNode: aNode.
  425. lhs := aNode left.
  426. lhs isImmutable ifTrue: [ self errorInvalidAssignment: lhs identifier ].
  427. lhs assigned: true
  428. !
  429. visitBlockNode: aNode
  430. self pushScope: self newBlockScope.
  431. aNode scope: currentScope.
  432. currentScope node: aNode.
  433. currentScope blockIndex: self nextBlockIndex.
  434. aNode parameters do: [ :each |
  435. self validateVariableScope: each.
  436. currentScope addArg: each ].
  437. super visitBlockNode: aNode.
  438. self popScope
  439. !
  440. visitCascadeNode: aNode
  441. aNode receiver: aNode dagChildren first receiver.
  442. aNode dagChildren allButLast do: [ :each | each beSideEffect ].
  443. super visitCascadeNode: aNode
  444. !
  445. visitMethodNode: aNode
  446. self pushScope: self newMethodScope.
  447. aNode scope: currentScope.
  448. currentScope node: aNode.
  449. self theClass allInstanceVariableNames do: [ :each |
  450. currentScope addIVar: each ].
  451. aNode arguments do: [ :each |
  452. self validateVariableScope: each.
  453. currentScope addArg: each ].
  454. super visitMethodNode: aNode.
  455. aNode
  456. classReferences: self classReferences;
  457. sendIndexes: self messageSends.
  458. self popScope.
  459. ^ aNode
  460. !
  461. visitReturnNode: aNode
  462. aNode scope: currentScope.
  463. currentScope isMethodScope
  464. ifTrue: [ currentScope localReturn: true ]
  465. ifFalse: [ currentScope methodScope addNonLocalReturn: currentScope ].
  466. super visitReturnNode: aNode
  467. !
  468. visitSendNode: aNode
  469. | sends |
  470. sends := self messageSends at: aNode selector ifAbsentPut: [ OrderedCollection new ].
  471. sends add: aNode.
  472. aNode index: sends size.
  473. super visitSendNode: aNode
  474. !
  475. visitSequenceNode: aNode
  476. aNode temps do: [ :each |
  477. self validateVariableScope: each.
  478. currentScope addTemp: each ].
  479. super visitSequenceNode: aNode
  480. !
  481. visitVariableNode: aNode
  482. "Bind a ScopeVar to aNode by doing a lookup in the current scope.
  483. If no var is found in scope, represent an externally known variable or throw an error."
  484. aNode binding:
  485. ((currentScope lookupVariable: aNode identifier) ifNil: [ self bindUnscopedVariable: aNode identifier ])
  486. ! !
  487. !SemanticAnalyzer class methodsFor: 'instance creation'!
  488. on: aClass
  489. ^ self new
  490. theClass: aClass;
  491. yourself
  492. ! !
  493. CompilerError subclass: #SemanticError
  494. slots: {}
  495. package: 'Compiler-Semantic'!
  496. !SemanticError commentStamp!
  497. I represent an abstract semantic error thrown by the SemanticAnalyzer.
  498. Semantic errors can be unknown variable errors, etc.
  499. See my subclasses for concrete errors.
  500. The IDE should catch instances of Semantic error to deal with them when compiling!
  501. SemanticError subclass: #InvalidAssignmentError
  502. slots: {#variableName}
  503. package: 'Compiler-Semantic'!
  504. !InvalidAssignmentError commentStamp!
  505. I get signaled when a pseudo variable gets assigned.!
  506. !InvalidAssignmentError methodsFor: 'accessing'!
  507. messageText
  508. ^ ' Invalid assignment to variable: ', self variableName
  509. !
  510. variableName
  511. ^ variableName
  512. !
  513. variableName: aString
  514. variableName := aString
  515. ! !
  516. SemanticError subclass: #ShadowingVariableError
  517. slots: {#variableName}
  518. package: 'Compiler-Semantic'!
  519. !ShadowingVariableError commentStamp!
  520. I get signaled when a variable in a block or method scope shadows a variable of the same name in an outer scope.!
  521. !ShadowingVariableError methodsFor: 'accessing'!
  522. messageText
  523. ^ 'Variable shadowing error: ', self variableName, ' is already defined'
  524. !
  525. variableName
  526. ^ variableName
  527. !
  528. variableName: aString
  529. variableName := aString
  530. ! !
  531. SemanticError subclass: #UnknownVariableError
  532. slots: {#variableName}
  533. package: 'Compiler-Semantic'!
  534. !UnknownVariableError commentStamp!
  535. I get signaled when a variable is not defined.
  536. The default behavior is to allow it, as this is how Amber currently is able to seamlessly send messages to JavaScript objects.!
  537. !UnknownVariableError methodsFor: 'accessing'!
  538. messageText
  539. ^ 'Unknown Variable error: ', self variableName, ' is not defined'
  540. !
  541. variableName
  542. ^ variableName
  543. !
  544. variableName: aString
  545. variableName := aString
  546. ! !