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