Compiler-Semantic.st 14 KB

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