author | Jan Vrany <jan.vrany@labware.com> |
Wed, 23 Dec 2020 14:26:52 +0000 | |
changeset 21 | 3945989c6e00 |
parent 12 | ae376bb422f5 |
permissions | -rw-r--r-- |
9 | 1 |
" |
2 |
COPYRIGHT (c) 2020 LabWare |
|
3 |
" |
|
4 |
"{ Package: 'stx:goodies/ring/core' }" |
|
5 |
||
6 |
"{ NameSpace: Smalltalk }" |
|
7 |
||
8 |
RGElement subclass:#RGMethod |
|
9 |
instanceVariableNames:'sourceCode package author time tags' |
|
10 |
classVariableNames:'' |
|
11 |
poolDictionaries:'' |
|
12 |
category:'Ring-Core-Kernel' |
|
13 |
! |
|
14 |
||
15 |
RGMethod comment:'RG2Method is a concrete representation of methods. It can be used to build browser for methods that are not in the image. It is polymorphic with CompiledMethod. * We can ask a RG2Method for its selector using the selector message. Example: (Point>>#dist:) asRing2Definition selector -> #dist We can also ask the ring object representation of its class or the Smalltalk class actually implementing the corresponding compiledMethod. * To access the ring class definition name, use parentName aRG2MethodDefinition parentName Example: (Point>>#dist:) asRing2Definition parentName -> #Point * If you have a complete model where classes and methods are ring definition, to access the ring class definition , use parent aRG2MethodDefinition parent Example: aRG2MethodDefinition(Point>>#dist:) parent -> aRG2ClassDefinition(Point) * If you want to access the smalltalk class that contains the compiledMethod that is represented by a ringMethodDefinition, use realParent aRG2MethodDefinition realParent Example: (Point>>#dist:) asRing2Definition realParent -> Point Now a RingEntityDefinition offers two APIs: one that is generic and works for all the source code entities and this is the one we just presented: parent, parentName and realParent. Having such interface is important to build generic tools that could manipulate any entities in a polymorphic way (yes no isKindOf: everywhere). In addition, a ring method definition offers a specific interface that should only be used when you know that you are solely manipulate specific entity such as class element: method definition, class comment, and variables. Here is the equivalence table realParent realClass parent ringClass parentName className * The message class returns the class of the object :). Example: (Point>>#dist:) asRing2Definition class -> RingMethodDefinition * The message className returns the name of the ring class defining the reingMethodDefinition. Example: (Point>>#dist:) asRing2Definition className -> #Point * If you have a complete model where classes and methods are ring definition, to access the ring class definition , use parent aRG2MethodDefinition ringClass Example: aRG2MethodDefinition(Point>>#dist:) ringClass -> aRG2ClassDefinition(Point) * If you want to access the smalltalk class that contains the compiledMethod that is represented by a ringMethodDefinition, use realClass aRG2MethodDefinition realClass Example: (Point>>#dist:) asRing2Definition realClass -> Point ' |
|
16 |
! |
|
17 |
||
18 |
!RGMethod class methodsFor:'documentation'! |
|
19 |
||
20 |
copyright |
|
21 |
" |
|
22 |
COPYRIGHT (c) 2020 LabWare |
|
23 |
||
24 |
" |
|
25 |
! ! |
|
26 |
||
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
27 |
|
9 | 28 |
!RGMethod methodsFor:'accessing'! |
29 |
||
30 |
argumentNames |
|
31 |
||
32 |
^ self ast argumentNames |
|
33 |
! |
|
34 |
||
35 |
ast |
|
36 |
"Answer my AST with semantic analysis. See #parseTree." |
|
37 |
||
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
38 |
^ self propertyNamed: #ast ifAbsentPut: [ self parseTree ] |
9 | 39 |
|
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
40 |
"Modified: / 23-12-2020 / 08:30:34 / Jan Vrany <jan.vrany@labware.com>" |
9 | 41 |
! |
42 |
||
43 |
compiledMethod |
|
44 |
^ self |
|
45 |
! |
|
46 |
||
47 |
fullName |
|
48 |
"Keeps a unique description for the receiver. As annotation to avoid converting each time is invoked" |
|
49 |
||
50 |
^ (self parent name, '>>#', self selector) asSymbol |
|
51 |
! |
|
52 |
||
53 |
hasSourceCode |
|
54 |
||
55 |
^ self backend forMethod hasSourceCodeFor: self |
|
56 |
! |
|
57 |
||
58 |
isExtension |
|
59 |
"The receiver is an extension when is defined in a different package to the one of its parent" |
|
60 |
||
61 |
^ self parent package ~= self package |
|
62 |
! |
|
63 |
||
64 |
numArgs |
|
65 |
^ self selector asString numArgs |
|
66 |
! |
|
67 |
||
68 |
parseTree |
|
69 |
||
70 |
^(RBParser |
|
71 |
parseMethod: self sourceCode |
|
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
72 |
onError: [ :msg :pos | ^ nil ]) |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
73 |
|
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
74 |
"Modified: / 22-12-2020 / 22:34:32 / Jan Vrany <jan.vrany@labware.com>" |
9 | 75 |
! |
76 |
||
77 |
selector |
|
78 |
"Retrieves the name of the method" |
|
79 |
||
80 |
^ self name asSymbol |
|
81 |
! |
|
82 |
||
83 |
sourceCode |
|
84 |
||
85 |
^ self backend forMethod sourceCodeFor: self |
|
86 |
! |
|
87 |
||
88 |
sourceCode: anObject |
|
89 |
||
90 |
"ATTENTION: There is no check here if the selector is changed!!" |
|
91 |
||
92 |
self backend forMethod setSourceCodeFor: self to: anObject asString |
|
93 |
"TODO: announcements" |
|
94 |
! |
|
95 |
||
96 |
sourceCodeForNoSelector |
|
97 |
||
98 |
^ 'unresolvedMessage', String cr, String tab, '"source code for the method model not set"' |
|
99 |
! |
|
100 |
||
101 |
sourceCodeForNoSource |
|
102 |
||
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
103 |
^ (Parser methodSpecificationForSelector: self selector), String cr, ' "source code for the method model not set"' |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
104 |
|
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
105 |
"Modified: / 22-12-2020 / 22:11:23 / Jan Vrany <jan.vrany@labware.com>" |
9 | 106 |
! ! |
107 |
||
108 |
!RGMethod methodsFor:'accessing - backend'! |
|
109 |
||
110 |
author |
|
111 |
||
112 |
^ self backend forBehavior authorFor: self |
|
113 |
! |
|
114 |
||
115 |
author: aString |
|
116 |
||
117 |
self backend forBehavior setAuthorFor: self to: aString |
|
118 |
! |
|
119 |
||
120 |
cleanTags |
|
121 |
||
122 |
self changeProtocolDuring: [ |
|
123 |
self cleanTagsWithoutAnnouncemnt ] |
|
124 |
! |
|
125 |
||
126 |
cleanTagsWithoutAnnouncemnt |
|
127 |
||
128 |
self backend forMethod cleanMethodTagsFor: self. |
|
129 |
! |
|
130 |
||
131 |
package |
|
132 |
||
133 |
^ self backend forMethod methodPackageFor: self |
|
134 |
! |
|
135 |
||
136 |
package: anRGPackage |
|
137 |
||
138 |
self backend forMethod setMethodPackageFor: self to: anRGPackage. |
|
139 |
self environment addPackage: anRGPackage. |
|
140 |
(self parent package = anRGPackage) |
|
141 |
ifFalse: [ self package addExtensionMethod: self ]. |
|
142 |
! |
|
143 |
||
144 |
protocol |
|
145 |
||
146 |
| methodTags | |
|
147 |
||
148 |
methodTags := self tags. |
|
149 |
^ methodTags |
|
150 |
ifEmpty: [ self class asYetUnclassifiedProtocolName] |
|
151 |
ifNotEmpty: [ methodTags sorted first ] |
|
152 |
! |
|
153 |
||
154 |
protocol: aSymbol |
|
155 |
||
156 |
self cleanTagsWithoutAnnouncemnt. |
|
157 |
self tagWith: aSymbol. |
|
158 |
||
159 |
||
160 |
! |
|
161 |
||
162 |
tagWith: aSymbol |
|
163 |
||
164 |
self changeProtocolDuring: [ |
|
165 |
self backend forMethod tagMethod: self with: aSymbol. |
|
166 |
self parent addMethodTag: aSymbol. |
|
167 |
] |
|
168 |
! |
|
169 |
||
170 |
tagsDo: aBlock |
|
171 |
||
172 |
self backend forMethod tagsForMethod: self do: aBlock |
|
173 |
! |
|
174 |
||
175 |
time |
|
176 |
||
177 |
^ self backend forBehavior timeFor: self |
|
178 |
! |
|
179 |
||
180 |
time: aDateAndTime |
|
181 |
||
182 |
self backend forBehavior setTimeFor: self to: aDateAndTime |
|
183 |
! |
|
184 |
||
185 |
untagFrom: aSymbol |
|
186 |
||
187 |
self backend forMethod untagMethod: self from: aSymbol |
|
188 |
! ! |
|
189 |
||
190 |
!RGMethod methodsFor:'accessing - model'! |
|
191 |
||
192 |
tags |
|
193 |
||
194 |
| allTags | |
|
195 |
allTags := IdentitySet new. |
|
196 |
self tagsDo: [ :each | allTags add: each]. |
|
197 |
^ allTags asArray |
|
198 |
! ! |
|
199 |
||
200 |
!RGMethod methodsFor:'backward compatibility'! |
|
201 |
||
202 |
category |
|
203 |
||
204 |
^ self protocol |
|
205 |
! ! |
|
206 |
||
207 |
!RGMethod methodsFor:'default model values'! |
|
208 |
||
209 |
defaultAuthor |
|
210 |
||
211 |
^ '' |
|
212 |
! |
|
213 |
||
214 |
defaultSourceCode |
|
215 |
||
216 |
^ self sourceCodeForNoSelector |
|
217 |
! |
|
218 |
||
219 |
defaultTags |
|
220 |
||
221 |
^ Set new |
|
222 |
! |
|
223 |
||
224 |
defaultTime |
|
225 |
||
10
1fa4cd506c87
core: use `Timestamp` instead of (Pharo) `DateAndTime`
Jan Vrany <jan.vrany@labware.com>
parents:
9
diff
changeset
|
226 |
^ Timestamp epoch |
9 | 227 |
! ! |
228 |
||
229 |
!RGMethod methodsFor:'error handling'! |
|
230 |
||
231 |
changeProtocolDuring: aBlock |
|
232 |
||
233 |
| oldProtocol | |
|
234 |
||
235 |
oldProtocol := self protocol. |
|
236 |
aBlock value. |
|
12
ae376bb422f5
core: consolidated environment announcements
Jan Vrany <jan.vrany@labware.com>
parents:
10
diff
changeset
|
237 |
self announcer methodRecategorized: self from: oldProtocol |
ae376bb422f5
core: consolidated environment announcements
Jan Vrany <jan.vrany@labware.com>
parents:
10
diff
changeset
|
238 |
|
ae376bb422f5
core: consolidated environment announcements
Jan Vrany <jan.vrany@labware.com>
parents:
10
diff
changeset
|
239 |
"Modified: / 22-12-2020 / 10:57:48 / Jan Vrany <jan.vrany@labware.com>" |
9 | 240 |
! ! |
241 |
||
242 |
!RGMethod methodsFor:'initialization'! |
|
243 |
||
244 |
initialize |
|
245 |
||
246 |
super initialize. |
|
247 |
||
248 |
sourceCode := self unresolvedValue: self defaultSourceCode. |
|
249 |
tags := self unresolvedValue: self defaultTags. "tags must be set before package" |
|
250 |
package := self unresolvedValue: self parent package. |
|
251 |
author := self unresolvedValue: self defaultAuthor. |
|
252 |
time := self unresolvedValue: self defaultTime. |
|
253 |
||
254 |
! |
|
255 |
||
256 |
initializeUnresolved |
|
257 |
||
258 |
super initializeUnresolved. |
|
259 |
||
260 |
sourceCode := self unresolvedValue: self defaultSourceCode. |
|
261 |
tags := self unresolvedValue: self defaultTags. |
|
262 |
package := self unresolvedValue: self parent package. |
|
263 |
author := self unresolvedValue: self defaultAuthor. |
|
264 |
time := self unresolvedValue: self defaultTime. |
|
265 |
||
266 |
! ! |
|
267 |
||
268 |
!RGMethod methodsFor:'managing container'! |
|
269 |
||
270 |
addoptToParentStub |
|
271 |
||
272 |
super addoptToParentStub. |
|
273 |
self environment backend createUnresolvedClassGroupFor: self parent. |
|
274 |
self parent pvtAddLocalMethod: self. |
|
275 |
||
276 |
||
277 |
! |
|
278 |
||
279 |
defaultParentStub |
|
280 |
||
281 |
^ self defaultClassStub |
|
282 |
! ! |
|
283 |
||
284 |
!RGMethod methodsFor:'printing'! |
|
285 |
||
286 |
printOn: aStream |
|
287 |
||
288 |
self parent name ifNotNil: [ |
|
289 |
aStream nextPutAll: self parent name; |
|
290 |
nextPutAll: '>>' ]. |
|
291 |
aStream print: self selector |
|
292 |
! ! |
|
293 |
||
294 |
!RGMethod methodsFor:'private'! |
|
295 |
||
296 |
astFromSource |
|
297 |
||
298 |
^ RBParser parseMethod: self sourceCode |
|
299 |
! |
|
300 |
||
301 |
pvtSafeSourceCode |
|
302 |
||
303 |
| aStringOrUnresolved | |
|
304 |
aStringOrUnresolved := self pvtSourceCode. |
|
305 |
^ aStringOrUnresolved isRingResolved |
|
306 |
ifFalse: [ |
|
307 |
self pvtName isRingResolved |
|
308 |
ifTrue: [ self sourceCodeForNoSource ] |
|
309 |
ifFalse: [ self sourceCodeForNoSelector ] ] |
|
310 |
ifTrue: [ aStringOrUnresolved value ] |
|
311 |
! |
|
312 |
||
313 |
pvtSourceCode |
|
314 |
||
315 |
^ sourceCode |
|
316 |
! |
|
317 |
||
318 |
pvtSourceCode: anObject |
|
319 |
||
320 |
sourceCode := anObject |
|
321 |
! |
|
322 |
||
323 |
pvtTagsDo: aBlock |
|
324 |
||
325 |
^ tags value do: aBlock |
|
326 |
! ! |
|
327 |
||
328 |
!RGMethod methodsFor:'private - backend interface'! |
|
329 |
||
330 |
pvtAuthor |
|
331 |
||
332 |
^ author value |
|
333 |
! |
|
334 |
||
335 |
pvtAuthor: aString |
|
336 |
||
337 |
^ author := aString |
|
338 |
! |
|
339 |
||
340 |
pvtCleanTags |
|
341 |
||
342 |
tags := self defaultTags. |
|
343 |
||
344 |
"TODO:Announce if not empty" |
|
345 |
||
346 |
||
347 |
! |
|
348 |
||
349 |
pvtPackage |
|
350 |
||
351 |
^ package value |
|
352 |
! |
|
353 |
||
354 |
pvtPackage: anRGPackageDefinition |
|
355 |
||
356 |
self environment verifyOwnership: anRGPackageDefinition. |
|
357 |
||
358 |
^ package := anRGPackageDefinition |
|
359 |
! |
|
360 |
||
361 |
pvtResolvableProperties |
|
362 |
||
363 |
^ super pvtResolvableProperties, { |
|
364 |
#sourceCode -> sourceCode. |
|
365 |
#package -> package. |
|
366 |
#author -> author. |
|
367 |
#time -> time. |
|
368 |
#tags -> tags. |
|
369 |
} |
|
370 |
||
371 |
||
372 |
! |
|
373 |
||
374 |
pvtTagWith: aSymbol |
|
375 |
||
376 |
tags isRingResolved ifFalse: [ |
|
377 |
self pvtCleanTags ]. |
|
378 |
||
379 |
tags add: aSymbol. |
|
380 |
||
381 |
! |
|
382 |
||
383 |
pvtTime |
|
384 |
||
385 |
^ time value |
|
386 |
! |
|
387 |
||
388 |
pvtTime: aDateAndTime |
|
389 |
||
390 |
^ time := aDateAndTime |
|
391 |
! |
|
392 |
||
393 |
pvtUntagFrom: aSymbol |
|
394 |
||
395 |
(tags value includes: aSymbol) |
|
396 |
ifTrue: [ tags remove: aSymbol ]. |
|
397 |
||
398 |
"TODO:Announce" |
|
399 |
||
400 |
||
401 |
! ! |
|
402 |
||
403 |
!RGMethod methodsFor:'queries - class'! |
|
404 |
||
405 |
methodClass |
|
406 |
||
407 |
^ self parent |
|
408 |
! ! |
|
409 |
||
410 |
!RGMethod methodsFor:'queries - tags'! |
|
411 |
||
412 |
isTaggedWith: aSymbol |
|
413 |
||
414 |
^self tags includes: aSymbol |
|
415 |
! ! |
|
416 |
||
417 |
!RGMethod methodsFor:'removing'! |
|
418 |
||
419 |
removeFromSystem |
|
420 |
||
421 |
^ self parent removeLocalMethod: self |
|
422 |
! ! |
|
423 |
||
424 |
!RGMethod methodsFor:'resolving'! |
|
425 |
||
426 |
makeResolved |
|
427 |
||
428 |
super makeResolved. |
|
429 |
||
430 |
sourceCode := self sourceCode markAsRingResolved. |
|
431 |
package := self package markAsRingResolved. |
|
432 |
author := self author markAsRingResolved. |
|
433 |
time := self time markAsRingResolved. |
|
434 |
tags := self tags markAsRingResolved. |
|
435 |
! ! |
|
436 |
||
437 |
!RGMethod methodsFor:'testing'! |
|
438 |
||
439 |
isFromTrait |
|
440 |
^ self parent isTrait |
|
441 |
! |
|
442 |
||
443 |
isLiteralMethod |
|
444 |
"Ring methods does not know how to detect if they are literal" |
|
445 |
^ false |
|
446 |
! |
|
447 |
||
448 |
isMetaSide |
|
449 |
||
450 |
^ self parent isMeta |
|
451 |
! |
|
452 |
||
453 |
isMethod |
|
454 |
||
455 |
^true |
|
456 |
! ! |
|
457 |
||
21
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
458 |
!RGMethod class methodsFor:'documentation'! |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
459 |
|
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
460 |
version_HG |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
461 |
|
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
462 |
^ '$Changeset: <not expanded> $' |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
463 |
! ! |
3945989c6e00
core: fix (port) AST access in `RGMethod`
Jan Vrany <jan.vrany@labware.com>
parents:
12
diff
changeset
|
464 |