Compiler-Semantic.st 14 KB

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