|
1 "{ Package: 'stx:goodies/petitparser/compiler' }" |
|
2 |
|
3 "{ NameSpace: Smalltalk }" |
|
4 |
|
5 Object subclass:#PPCASTUtilities |
|
6 instanceVariableNames:'' |
|
7 classVariableNames:'' |
|
8 poolDictionaries:'' |
|
9 category:'PetitCompiler-Support' |
|
10 ! |
|
11 |
|
12 !PPCASTUtilities methodsFor:'checks'! |
|
13 |
|
14 checkNodeIsFunctional: anRBBlockNode inClass: aClaas |
|
15 "Check whether the given node is purely functional or not. |
|
16 If no, raise an erorr. If not, this method is noop. |
|
17 |
|
18 A block is purely functional if and only if: |
|
19 (i) it does not refer to any instance or class variable or non-local variable |
|
20 (ii) all self-sends within the block are to 'purely-functional' methods |
|
21 (transitively) |
|
22 (iiI) contains no super-sends. |
|
23 " |
|
24 | allDefinedVarNames allInstVarNames allClassVarNames cls | |
|
25 |
|
26 allDefinedVarNames := anRBBlockNode allDefinedVariables. |
|
27 allInstVarNames := Set new. |
|
28 allClassVarNames := Set new. |
|
29 cls := aClaas. |
|
30 [ cls notNil ] whileTrue:[ |
|
31 allInstVarNames addAll: cls instanceVariables. |
|
32 allClassVarNames addAll: cls classVariables. |
|
33 cls := cls superclass. |
|
34 ]. |
|
35 |
|
36 self withAllVariableNodesOf: anRBBlockNode do: [ :node | |
|
37 (allDefinedVarNames includes: node name) ifFalse:[ |
|
38 (allInstVarNames includes: node name) ifTrue:[ |
|
39 PPCCompilationError new signalWith: 'code refers to an instance variable named `',node name,'`'. |
|
40 ^ self. |
|
41 ]. |
|
42 (allClassVarNames includes: node name) ifTrue:[ |
|
43 PPCCompilationError new signalWith: 'code refers to a class variable named `',node name,'`'. |
|
44 ^ self. |
|
45 ]. |
|
46 (Smalltalk includesKey: node name asSymbol) ifFalse:[ |
|
47 PPCCompilationError new signalWith: 'code refers to an unknown variable named `',node name,'`'. |
|
48 ^ self. |
|
49 ]. |
|
50 ] |
|
51 ]. |
|
52 self withAllMessageNodesOf: anRBBlockNode sentToSelfDo:[:node | |
|
53 | method | |
|
54 |
|
55 method := aClaas lookupSelector: node selector. |
|
56 method isNil ifTrue:[ |
|
57 PPCCompilationError new signalWith: 'code contains self-send to non-existent method'. |
|
58 ^ self |
|
59 ]. |
|
60 self checkNodeIsFunctional: method parseTree inClass: method methodClass. |
|
61 ]. |
|
62 self withAllSuperNodesOf: anRBBlockNode do: [ :node | |
|
63 PPCCompilationError new signalWith: 'code contains a super-send'. |
|
64 ^ self |
|
65 ]. |
|
66 |
|
67 "Created: / 27-07-2015 / 12:15:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
68 "Modified: / 27-07-2015 / 14:43:07 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
69 ! ! |
|
70 |
|
71 !PPCASTUtilities methodsFor:'enumerating'! |
|
72 |
|
73 withAllMessageNodesOf: anRBProgramNode do: aBlock |
|
74 "Enumerate all chilren of `anRBProgramNode` (including itself) |
|
75 and evaluate `aBlock` for each message node." |
|
76 |
|
77 self withAllNodesOf: anRBProgramNode suchThat: [ :node | node isMessage ] do: aBlock. |
|
78 |
|
79 "Created: / 18-06-2015 / 22:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
80 "Modified (comment): / 27-07-2015 / 11:26:29 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
81 ! |
|
82 |
|
83 withAllMessageNodesOf: anRBProgramNode sentToSelfDo: aBlock |
|
84 "Enumerate all chilren of `anRBProgramNode` (including itself) |
|
85 and evaluate `aBlock` for each message node which sends a message |
|
86 to self (i.e., for self-sends)." |
|
87 |
|
88 self withAllNodesOf: anRBProgramNode suchThat: [ :node | node isMessage and:[node receiver isSelf ] ] do: aBlock. |
|
89 |
|
90 "Created: / 27-07-2015 / 14:41:30 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
91 ! |
|
92 |
|
93 withAllNodesOf: node suchThat: predicate do: action |
|
94 "Enumerate all chilren of `node` (including itself) |
|
95 and evaluate `aBlock` for each node for which `predicate` returns true." |
|
96 |
|
97 (predicate value: node) ifTrue:[ |
|
98 action value: node. |
|
99 ]. |
|
100 node children do:[:each | |
|
101 self withAllNodesOf: each suchThat: predicate do: action |
|
102 ]. |
|
103 |
|
104 "Created: / 18-06-2015 / 22:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
105 "Modified (comment): / 27-07-2015 / 11:26:46 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
106 ! |
|
107 |
|
108 withAllSelfNodesOf: anRBProgramNode do: aBlock |
|
109 "Enumerate all chilren of `anRBProgramNode` (including itself) |
|
110 and evaluate `aBlock` for each `self` node." |
|
111 |
|
112 self withAllNodesOf: anRBProgramNode suchThat: [ :node | node isSelf ] do: aBlock. |
|
113 |
|
114 "Created: / 18-06-2015 / 22:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
115 "Modified (comment): / 27-07-2015 / 11:26:52 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
116 ! |
|
117 |
|
118 withAllSuperNodesOf: anRBProgramNode do: aBlock |
|
119 "Enumerate all chilren of `anRBProgramNode` (including itself) |
|
120 and evaluate `aBlock` for each `super` node." |
|
121 |
|
122 self withAllNodesOf: anRBProgramNode suchThat: [ :node | node isSuper ] do: aBlock. |
|
123 |
|
124 "Created: / 27-07-2015 / 14:42:28 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
125 ! |
|
126 |
|
127 withAllVariableNodesOf: anRBProgramNode do: aBlock |
|
128 "Enumerate all chilren of `anRBProgramNode` (including itself) |
|
129 and evaluate `aBlock` for each variable node. |
|
130 This is a replacement for Smalltalk/X's RBProgramNode>>variableNodesDo: |
|
131 which is not present in Pharo" |
|
132 |
|
133 self withAllNodesOf: anRBProgramNode suchThat: [ :node | node isVariable and:[node isSelf not and:[node isSuper not]]] do: aBlock. |
|
134 |
|
135 "Created: / 18-06-2015 / 22:02:43 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
136 "Modified (comment): / 27-07-2015 / 11:27:00 / Jan Vrany <jan.vrany@fit.cvut.cz>" |
|
137 ! ! |
|
138 |