Debian Codegraph @

generic/tclOOCall.c

raw
1 /*
2 * tclOOCall.c --
3 *
4 * This file contains the method call chain management code for the
5 * object-system core.
6 *
7 * Copyright (c) 2005-2012 Donal K. Fellows
8 *
9 * See the file "license.terms" for information on usage and redistribution of
10 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 */
12
13 #ifdef HAVE_CONFIG_H
14 #include "config.h"
15 #endif
16 #include "tclInt.h"
17 #include "tclOOInt.h"
18
19 /*
20 * Structure containing a CallContext and any other values needed only during
21 * the construction of the CallContext.
22 */
23
24 struct ChainBuilder {
25 CallChain *callChainPtr; /* The call chain being built. */
26 int filterLength; /* Number of entries in the call chain that
27 * are due to processing filters and not the
28 * main call chain. */
29 Object *oPtr; /* The object that we are building the chain
30 * for. */
31 };
32
33 /*
34 * Extra flags used for call chain management.
35 */
36
37 #define DEFINITE_PROTECTED 0x100000
38 #define DEFINITE_PUBLIC 0x200000
39 #define KNOWN_STATE (DEFINITE_PROTECTED | DEFINITE_PUBLIC)
40 #define SPECIAL (CONSTRUCTOR | DESTRUCTOR | FORCE_UNKNOWN)
41 #define BUILDING_MIXINS 0x400000
42 #define TRAVERSED_MIXIN 0x800000
43 #define OBJECT_MIXIN 0x1000000
44 #define MIXIN_CONSISTENT(flags) \
45 (((flags) & OBJECT_MIXIN) || \
46 !((flags) & BUILDING_MIXINS) == !((flags) & TRAVERSED_MIXIN))
47
48 /*
49 * Function declarations for things defined in this file.
50 */
51
52 static void AddClassFiltersToCallContext(Object *const oPtr,
53 Class *clsPtr, struct ChainBuilder *const cbPtr,
54 Tcl_HashTable *const doneFilters, int flags);
55 static void AddClassMethodNames(Class *clsPtr, int flags,
56 Tcl_HashTable *const namesPtr,
57 Tcl_HashTable *const examinedClassesPtr);
58 static inline void AddMethodToCallChain(Method *const mPtr,
59 struct ChainBuilder *const cbPtr,
60 Tcl_HashTable *const doneFilters,
61 Class *const filterDecl, int flags);
62 static inline void AddSimpleChainToCallContext(Object *const oPtr,
63 Tcl_Obj *const methodNameObj,
64 struct ChainBuilder *const cbPtr,
65 Tcl_HashTable *const doneFilters, int flags,
66 Class *const filterDecl);
67 static void AddSimpleClassChainToCallContext(Class *classPtr,
68 Tcl_Obj *const methodNameObj,
69 struct ChainBuilder *const cbPtr,
70 Tcl_HashTable *const doneFilters, int flags,
71 Class *const filterDecl);
72 static int CmpStr(const void *ptr1, const void *ptr2);
73 static void DupMethodNameRep(Tcl_Obj *srcPtr, Tcl_Obj *dstPtr);
74 static Tcl_NRPostProc FinalizeMethodRefs;
75 static void FreeMethodNameRep(Tcl_Obj *objPtr);
76 static inline int IsStillValid(CallChain *callPtr, Object *oPtr,
77 int flags, int reuseMask);
78 static Tcl_NRPostProc ResetFilterFlags;
79 static Tcl_NRPostProc SetFilterFlags;
80 static inline void StashCallChain(Tcl_Obj *objPtr, CallChain *callPtr);
81
82 /*
83 * Object type used to manage type caches attached to method names.
84 */
85
86 static const Tcl_ObjType methodNameType = {
87 "TclOO method name",
88 FreeMethodNameRep,
89 DupMethodNameRep,
90 NULL,
91 NULL
92 };
93
94
95 /*
96 * ----------------------------------------------------------------------
97 *
98 * TclOODeleteContext --
99 *
100 * Destroys a method call-chain context, which should not be in use.
101 *
102 * ----------------------------------------------------------------------
103 */
104
105 void
106 TclOODeleteContext(
107 CallContext *contextPtr)
108 {
109 Object *oPtr = contextPtr->oPtr;
110
111 TclOODeleteChain(contextPtr->callPtr);
112 if (oPtr != NULL) {
113 TclStackFree(oPtr->fPtr->interp, contextPtr);
114
115 /*
116 * Corresponding AddRef() in TclOO.c/TclOOObjectCmdCore
117 */
118
119 TclOODecrRefCount(oPtr);
120 }
121 }
122
123 /*
124 * ----------------------------------------------------------------------
125 *
126 * TclOODeleteChainCache --
127 *
128 * Destroy the cache of method call-chains.
129 *
130 * ----------------------------------------------------------------------
131 */
132
133 void
134 TclOODeleteChainCache(
135 Tcl_HashTable *tablePtr)
136 {
137 FOREACH_HASH_DECLS;
138 CallChain *callPtr;
139
140 FOREACH_HASH_VALUE(callPtr, tablePtr) {
141 if (callPtr) {
142 TclOODeleteChain(callPtr);
143 }
144 }
145 Tcl_DeleteHashTable(tablePtr);
146 ckfree(tablePtr);
147 }
148
149 /*
150 * ----------------------------------------------------------------------
151 *
152 * TclOODeleteChain --
153 *
154 * Destroys a method call-chain.
155 *
156 * ----------------------------------------------------------------------
157 */
158
159 void
160 TclOODeleteChain(
161 CallChain *callPtr)
162 {
163 if (callPtr == NULL || callPtr->refCount-- > 1) {
164 return;
165 }
166 if (callPtr->chain != callPtr->staticChain) {
167 ckfree(callPtr->chain);
168 }
169 ckfree(callPtr);
170 }
171
172 /*
173 * ----------------------------------------------------------------------
174 *
175 * TclOOStashContext --
176 *
177 * Saves a reference to a method call context in a Tcl_Obj's internal
178 * representation.
179 *
180 * ----------------------------------------------------------------------
181 */
182
183 static inline void
184 StashCallChain(
185 Tcl_Obj *objPtr,
186 CallChain *callPtr)
187 {
188 callPtr->refCount++;
189 TclGetString(objPtr);
190 TclFreeIntRep(objPtr);
191 objPtr->typePtr = &methodNameType;
192 objPtr->internalRep.twoPtrValue.ptr1 = callPtr;
193 }
194
195 void
196 TclOOStashContext(
197 Tcl_Obj *objPtr,
198 CallContext *contextPtr)
199 {
200 StashCallChain(objPtr, contextPtr->callPtr);
201 }
202
203 /*
204 * ----------------------------------------------------------------------
205 *
206 * DupMethodNameRep, FreeMethodNameRep --
207 *
208 * Functions to implement the required parts of the Tcl_Obj guts needed
209 * for caching of method contexts in Tcl_Objs.
210 *
211 * ----------------------------------------------------------------------
212 */
213
214 static void
215 DupMethodNameRep(
216 Tcl_Obj *srcPtr,
217 Tcl_Obj *dstPtr)
218 {
219 CallChain *callPtr = srcPtr->internalRep.twoPtrValue.ptr1;
220
221 dstPtr->typePtr = &methodNameType;
222 dstPtr->internalRep.twoPtrValue.ptr1 = callPtr;
223 callPtr->refCount++;
224 }
225
226 static void
227 FreeMethodNameRep(
228 Tcl_Obj *objPtr)
229 {
230 CallChain *callPtr = objPtr->internalRep.twoPtrValue.ptr1;
231
232 TclOODeleteChain(callPtr);
233 objPtr->typePtr = NULL;
234 }
235
236 /*
237 * ----------------------------------------------------------------------
238 *
239 * TclOOInvokeContext --
240 *
241 * Invokes a single step along a method call-chain context. Note that the
242 * invocation of a step along the chain can cause further steps along the
243 * chain to be invoked. Note that this function is written to be as light
244 * in stack usage as possible.
245 *
246 * ----------------------------------------------------------------------
247 */
248
249 int
250 TclOOInvokeContext(
251 void *clientData, /* The method call context. */
252 Tcl_Interp *interp, /* Interpreter for error reporting, and many
253 * other sorts of context handling (e.g.,
254 * commands, variables) depending on method
255 * implementation. */
256 int objc, /* The number of arguments. */
257 Tcl_Obj *const objv[]) /* The arguments as actually seen. */
258 {
259 CallContext *const contextPtr = (CallContext *)clientData;
260 Method *const mPtr = contextPtr->callPtr->chain[contextPtr->index].mPtr;
261 const int isFilter =
262 contextPtr->callPtr->chain[contextPtr->index].isFilter;
263
264 /*
265 * If this is the first step along the chain, we preserve the method
266 * entries in the chain so that they do not get deleted out from under our
267 * feet.
268 */
269
270 if (contextPtr->index == 0) {
271 int i;
272
273 for (i = 0 ; i < contextPtr->callPtr->numChain ; i++) {
274 AddRef(contextPtr->callPtr->chain[i].mPtr);
275 }
276
277 /*
278 * Ensure that the method name itself is part of the arguments when
279 * we're doing unknown processing.
280 */
281
282 if (contextPtr->callPtr->flags & OO_UNKNOWN_METHOD) {
283 contextPtr->skip--;
284 }
285
286 /*
287 * Add a callback to ensure that method references are dropped once
288 * this call is finished.
289 */
290
291 TclNRAddCallback(interp, FinalizeMethodRefs, contextPtr, NULL, NULL,
292 NULL);
293 }
294
295 /*
296 * Save whether we were in a filter and set up whether we are now.
297 */
298
299 if (contextPtr->oPtr->flags & FILTER_HANDLING) {
300 TclNRAddCallback(interp, SetFilterFlags, contextPtr, NULL,NULL,NULL);
301 } else {
302 TclNRAddCallback(interp, ResetFilterFlags,contextPtr,NULL,NULL,NULL);
303 }
304 if (isFilter || contextPtr->callPtr->flags & FILTER_HANDLING) {
305 contextPtr->oPtr->flags |= FILTER_HANDLING;
306 } else {
307 contextPtr->oPtr->flags &= ~FILTER_HANDLING;
308 }
309
310 /*
311 * Run the method implementation.
312 */
313
314 return mPtr->typePtr->callProc(mPtr->clientData, interp,
315 (Tcl_ObjectContext) contextPtr, objc, objv);
316 }
317
318 static int
319 SetFilterFlags(
320 void *data[],
321 Tcl_Interp *interp,
322 int result)
323 {
324 CallContext *contextPtr = (CallContext *)data[0];
325
326 contextPtr->oPtr->flags |= FILTER_HANDLING;
327 return result;
328 }
329
330 static int
331 ResetFilterFlags(
332 void *data[],
333 Tcl_Interp *interp,
334 int result)
335 {
336 CallContext *contextPtr = (CallContext *)data[0];
337
338 contextPtr->oPtr->flags &= ~FILTER_HANDLING;
339 return result;
340 }
341
342 static int
343 FinalizeMethodRefs(
344 void *data[],
345 Tcl_Interp *interp,
346 int result)
347 {
348 CallContext *contextPtr = (CallContext *)data[0];
349 int i;
350
351 for (i = 0 ; i < contextPtr->callPtr->numChain ; i++) {
352 TclOODelMethodRef(contextPtr->callPtr->chain[i].mPtr);
353 }
354 return result;
355 }
356
357 /*
358 * ----------------------------------------------------------------------
359 *
360 * TclOOGetSortedMethodList, TclOOGetSortedClassMethodList --
361 *
362 * Discovers the list of method names supported by an object or class.
363 *
364 * ----------------------------------------------------------------------
365 */
366
367 int
368 TclOOGetSortedMethodList(
369 Object *oPtr, /* The object to get the method names for. */
370 int flags, /* Whether we just want the public method
371 * names. */
372 const char ***stringsPtr) /* Where to write a pointer to the array of
373 * strings to. */
374 {
375 Tcl_HashTable names; /* Tcl_Obj* method name to "wanted in list"
376 * mapping. */
377 Tcl_HashTable examinedClasses;
378 /* Used to track what classes have been looked
379 * at. Is set-like in nature and keyed by
380 * pointer to class. */
381 FOREACH_HASH_DECLS;
382 int i;
383 Class *mixinPtr;
384 Tcl_Obj *namePtr;
385 Method *mPtr;
386 int isWantedIn;
387 void *isWanted;
388
389 Tcl_InitObjHashTable(&names);
390 Tcl_InitHashTable(&examinedClasses, TCL_ONE_WORD_KEYS);
391
392 /*
393 * Name the bits used in the names table values.
394 */
395 #define IN_LIST 1
396 #define NO_IMPLEMENTATION 2
397
398 /*
399 * Process method names due to the object.
400 */
401
402 if (oPtr->methodsPtr) {
403 FOREACH_HASH(namePtr, mPtr, oPtr->methodsPtr) {
404 int isNew;
405
406 if ((mPtr->flags & PRIVATE_METHOD) && !(flags & PRIVATE_METHOD)) {
407 continue;
408 }
409 hPtr = Tcl_CreateHashEntry(&names, (char *) namePtr, &isNew);
410 if (isNew) {
411 isWantedIn = ((!(flags & PUBLIC_METHOD)
412 || mPtr->flags & PUBLIC_METHOD) ? IN_LIST : 0);
413 isWantedIn |= (mPtr->typePtr == NULL ? NO_IMPLEMENTATION : 0);
414 Tcl_SetHashValue(hPtr, INT2PTR(isWantedIn));
415 }
416 }
417 }
418
419 /*
420 * Process method names due to private methods on the object's class.
421 */
422
423 if (flags & PRIVATE_METHOD) {
424 FOREACH_HASH(namePtr, mPtr, &oPtr->selfCls->classMethods) {
425 if (mPtr->flags & PRIVATE_METHOD) {
426 int isNew;
427
428 hPtr = Tcl_CreateHashEntry(&names, (char *) namePtr, &isNew);
429 if (isNew) {
430 isWantedIn = IN_LIST;
431 if (mPtr->typePtr == NULL) {
432 isWantedIn |= NO_IMPLEMENTATION;
433 }
434 Tcl_SetHashValue(hPtr, INT2PTR(isWantedIn));
435 } else if (mPtr->typePtr != NULL) {
436 isWantedIn = PTR2INT(Tcl_GetHashValue(hPtr));
437 if (isWantedIn & NO_IMPLEMENTATION) {
438 isWantedIn &= ~NO_IMPLEMENTATION;
439 Tcl_SetHashValue(hPtr, INT2PTR(isWantedIn));
440 }
441 }
442 }
443 }
444 }
445
446 /*
447 * Process (normal) method names from the class hierarchy and the mixin
448 * hierarchy.
449 */
450
451 AddClassMethodNames(oPtr->selfCls, flags, &names, &examinedClasses);
452 FOREACH(mixinPtr, oPtr->mixins) {
453 AddClassMethodNames(mixinPtr, flags|TRAVERSED_MIXIN, &names,
454 &examinedClasses);
455 }
456
457 Tcl_DeleteHashTable(&examinedClasses);
458
459 /*
460 * See how many (visible) method names there are. If none, we do not (and
461 * should not) try to sort the list of them.
462 */
463
464 i = 0;
465 if (names.numEntries != 0) {
466 const char **strings;
467
468 /*
469 * We need to build the list of methods to sort. We will be using
470 * qsort() for this, because it is very unlikely that the list will be
471 * heavily sorted when it is long enough to matter.
472 */
473
474 strings = ckalloc(sizeof(char *) * names.numEntries);
475 FOREACH_HASH(namePtr, isWanted, &names) {
476 if (!(flags & PUBLIC_METHOD) || (PTR2INT(isWanted) & IN_LIST)) {
477 if (PTR2INT(isWanted) & NO_IMPLEMENTATION) {
478 continue;
479 }
480 strings[i++] = TclGetString(namePtr);
481 }
482 }
483
484 /*
485 * Note that 'i' may well be less than names.numEntries when we are
486 * dealing with public method names.
487 */
488
489 if (i > 0) {
490 if (i > 1) {
491 qsort((void *) strings, i, sizeof(char *), CmpStr);
492 }
493 *stringsPtr = strings;
494 } else {
495 ckfree(strings);
496 }
497 }
498
499 Tcl_DeleteHashTable(&names);
500 return i;
501 }
502
503 int
504 TclOOGetSortedClassMethodList(
505 Class *clsPtr, /* The class to get the method names for. */
506 int flags, /* Whether we just want the public method
507 * names. */
508 const char ***stringsPtr) /* Where to write a pointer to the array of
509 * strings to. */
510 {
511 Tcl_HashTable names; /* Tcl_Obj* method name to "wanted in list"
512 * mapping. */
513 Tcl_HashTable examinedClasses;
514 /* Used to track what classes have been looked
515 * at. Is set-like in nature and keyed by
516 * pointer to class. */
517 FOREACH_HASH_DECLS;
518 int i;
519 Tcl_Obj *namePtr;
520 void *isWanted;
521
522 Tcl_InitObjHashTable(&names);
523 Tcl_InitHashTable(&examinedClasses, TCL_ONE_WORD_KEYS);
524
525 /*
526 * Process method names from the class hierarchy and the mixin hierarchy.
527 */
528
529 AddClassMethodNames(clsPtr, flags, &names, &examinedClasses);
530 Tcl_DeleteHashTable(&examinedClasses);
531
532 /*
533 * See how many (visible) method names there are. If none, we do not (and
534 * should not) try to sort the list of them.
535 */
536
537 i = 0;
538 if (names.numEntries != 0) {
539 const char **strings;
540
541 /*
542 * We need to build the list of methods to sort. We will be using
543 * qsort() for this, because it is very unlikely that the list will be
544 * heavily sorted when it is long enough to matter.
545 */
546
547 strings = (const char **)ckalloc(sizeof(char *) * names.numEntries);
548 FOREACH_HASH(namePtr, isWanted, &names) {
549 if (!(flags & PUBLIC_METHOD) || (PTR2INT(isWanted) & IN_LIST)) {
550 if (PTR2INT(isWanted) & NO_IMPLEMENTATION) {
551 continue;
552 }
553 strings[i++] = TclGetString(namePtr);
554 }
555 }
556
557 /*
558 * Note that 'i' may well be less than names.numEntries when we are
559 * dealing with public method names.
560 */
561
562 if (i > 0) {
563 if (i > 1) {
564 qsort((void *) strings, i, sizeof(char *), CmpStr);
565 }
566 *stringsPtr = strings;
567 } else {
568 ckfree(strings);
569 }
570 }
571
572 Tcl_DeleteHashTable(&names);
573 return i;
574 }
575
576 /*
577 * Comparator for GetSortedMethodList
578 */
579
580 static int
581 CmpStr(
582 const void *ptr1,
583 const void *ptr2)
584 {
585 const char **strPtr1 = (const char **) ptr1;
586 const char **strPtr2 = (const char **) ptr2;
587
588 return TclUtfNcmp2(*strPtr1, *strPtr2, strlen(*strPtr1) + 1);
589 }
590
591 /*
592 * ----------------------------------------------------------------------
593 *
594 * AddClassMethodNames --
595 *
596 * Adds the method names defined by a class (or its superclasses) to the
597 * collection being built. The collection is built in a hash table to
598 * ensure that duplicates are excluded. Helper for GetSortedMethodList().
599 *
600 * ----------------------------------------------------------------------
601 */
602
603 static void
604 AddClassMethodNames(
605 Class *clsPtr, /* Class to get method names from. */
606 int flags, /* Whether we are interested in just the
607 * public method names. */
608 Tcl_HashTable *const namesPtr,
609 /* Reference to the hash table to put the
610 * information in. The hash table maps the
611 * Tcl_Obj * method name to an integral value
612 * describing whether the method is wanted.
613 * This ensures that public/private override
614 * semantics are handled correctly. */
615 Tcl_HashTable *const examinedClassesPtr)
616 /* Hash table that tracks what classes have
617 * already been looked at. The keys are the
618 * pointers to the classes, and the values are
619 * immaterial. */
620 {
621 /*
622 * If we've already started looking at this class, stop working on it now
623 * to prevent repeated work.
624 */
625
626 if (Tcl_FindHashEntry(examinedClassesPtr, (char *) clsPtr)) {
627 return;
628 }
629
630 /*
631 * Scope all declarations so that the compiler can stand a good chance of
632 * making the recursive step highly efficient. We also hand-implement the
633 * tail-recursive case using a while loop; C compilers typically cannot do
634 * tail-recursion optimization usefully.
635 */
636
637 while (1) {
638 FOREACH_HASH_DECLS;
639 Tcl_Obj *namePtr;
640 Method *mPtr;
641 int isNew;
642
643 (void) Tcl_CreateHashEntry(examinedClassesPtr, (char *) clsPtr,
644 &isNew);
645 if (!isNew) {
646 break;
647 }
648
649 if (clsPtr->mixins.num != 0) {
650 Class *mixinPtr;
651 int i;
652
653 FOREACH(mixinPtr, clsPtr->mixins) {
654 if (mixinPtr != clsPtr) {
655 AddClassMethodNames(mixinPtr, flags|TRAVERSED_MIXIN,
656 namesPtr, examinedClassesPtr);
657 }
658 }
659 }
660
661 FOREACH_HASH(namePtr, mPtr, &clsPtr->classMethods) {
662 hPtr = Tcl_CreateHashEntry(namesPtr, (char *) namePtr, &isNew);
663 if (isNew) {
664 int isWanted = (!(flags & PUBLIC_METHOD)
665 || (mPtr->flags & PUBLIC_METHOD)) ? IN_LIST : 0;
666
667 isWanted |= (mPtr->typePtr == NULL ? NO_IMPLEMENTATION : 0);
668 Tcl_SetHashValue(hPtr, INT2PTR(isWanted));
669 } else if ((PTR2INT(Tcl_GetHashValue(hPtr)) & NO_IMPLEMENTATION)
670 && mPtr->typePtr != NULL) {
671 int isWanted = PTR2INT(Tcl_GetHashValue(hPtr));
672
673 isWanted &= ~NO_IMPLEMENTATION;
674 Tcl_SetHashValue(hPtr, INT2PTR(isWanted));
675 }
676 }
677
678 if (clsPtr->superclasses.num != 1) {
679 break;
680 }
681 clsPtr = clsPtr->superclasses.list[0];
682 }
683 if (clsPtr->superclasses.num != 0) {
684 Class *superPtr;
685 int i;
686
687 FOREACH(superPtr, clsPtr->superclasses) {
688 AddClassMethodNames(superPtr, flags, namesPtr,
689 examinedClassesPtr);
690 }
691 }
692 }
693
694 /*
695 * ----------------------------------------------------------------------
696 *
697 * AddSimpleChainToCallContext --
698 *
699 * The core of the call-chain construction engine, this handles calling a
700 * particular method on a particular object. Note that filters and
701 * unknown handling are already handled by the logic that uses this
702 * function.
703 *
704 * ----------------------------------------------------------------------
705 */
706
707 static inline void
708 AddSimpleChainToCallContext(
709 Object *const oPtr, /* Object to add call chain entries for. */
710 Tcl_Obj *const methodNameObj,
711 /* Name of method to add the call chain
712 * entries for. */
713 struct ChainBuilder *const cbPtr,
714 /* Where to add the call chain entries. */
715 Tcl_HashTable *const doneFilters,
716 /* Where to record what call chain entries
717 * have been processed. */
718 int flags, /* What sort of call chain are we building. */
719 Class *const filterDecl) /* The class that declared the filter. If
720 * NULL, either the filter was declared by the
721 * object or this isn't a filter. */
722 {
723 int i;
724
725 if (!(flags & (KNOWN_STATE | SPECIAL)) && oPtr->methodsPtr) {
726 Tcl_HashEntry *hPtr = Tcl_FindHashEntry(oPtr->methodsPtr,
727 (char *) methodNameObj);
728
729 if (hPtr != NULL) {
730 Method *mPtr = (Method *)Tcl_GetHashValue(hPtr);
731
732 if (flags & PUBLIC_METHOD) {
733 if (!(mPtr->flags & PUBLIC_METHOD)) {
734 return;
735 } else {
736 flags |= DEFINITE_PUBLIC;
737 }
738 } else {
739 flags |= DEFINITE_PROTECTED;
740 }
741 }
742 }
743 if (!(flags & SPECIAL)) {
744 Tcl_HashEntry *hPtr;
745 Class *mixinPtr;
746
747 FOREACH(mixinPtr, oPtr->mixins) {
748 AddSimpleClassChainToCallContext(mixinPtr, methodNameObj, cbPtr,
749 doneFilters, flags|TRAVERSED_MIXIN, filterDecl);
750 }
751 if (oPtr->methodsPtr) {
752 hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, (char*) methodNameObj);
753 if (hPtr != NULL) {
754 AddMethodToCallChain((Method *)Tcl_GetHashValue(hPtr), cbPtr,
755 doneFilters, filterDecl, flags);
756 }
757 }
758 }
759 if (!oPtr->selfCls) {
760 return;
761 }
762 AddSimpleClassChainToCallContext(oPtr->selfCls, methodNameObj, cbPtr,
763 doneFilters, flags, filterDecl);
764 }
765
766 /*
767 * ----------------------------------------------------------------------
768 *
769 * AddMethodToCallChain --
770 *
771 * Utility method that manages the adding of a particular method
772 * implementation to a call-chain.
773 *
774 * ----------------------------------------------------------------------
775 */
776
777 static inline void
778 AddMethodToCallChain(
779 Method *const mPtr, /* Actual method implementation to add to call
780 * chain (or NULL, a no-op). */
781 struct ChainBuilder *const cbPtr,
782 /* The call chain to add the method
783 * implementation to. */
784 Tcl_HashTable *const doneFilters,
785 /* Where to record what filters have been
786 * processed. If NULL, not processing filters.
787 * Note that this function does not update
788 * this hashtable. */
789 Class *const filterDecl, /* The class that declared the filter. If
790 * NULL, either the filter was declared by the
791 * object or this isn't a filter. */
792 int flags) /* Used to check if we're mixin-consistent
793 * only. Mixin-consistent means that either
794 * we're looking to add things from a mixin
795 * and we have passed a mixin, or we're not
796 * looking to add things from a mixin and have
797 * not passed a mixin. */
798 {
799 CallChain *callPtr = cbPtr->callChainPtr;
800 int i;
801
802 /*
803 * Return if this is just an entry used to record whether this is a public
804 * method. If so, there's nothing real to call and so nothing to add to
805 * the call chain.
806 *
807 * This is also where we enforce mixin-consistency.
808 */
809
810 if (mPtr == NULL || mPtr->typePtr == NULL || !MIXIN_CONSISTENT(flags)) {
811 return;
812 }
813
814 /*
815 * Enforce real private method handling here. We will skip adding this
816 * method IF
817 * 1) we are not allowing private methods, AND
818 * 2) this is a private method, AND
819 * 3) this is a class method, AND
820 * 4) this method was not declared by the class of the current object.
821 *
822 * This does mean that only classes really handle private methods. This
823 * should be sufficient for [incr Tcl] support though.
824 */
825
826 if (!(callPtr->flags & PRIVATE_METHOD)
827 && (mPtr->flags & PRIVATE_METHOD)
828 && (mPtr->declaringClassPtr != NULL)
829 && (mPtr->declaringClassPtr != cbPtr->oPtr->selfCls)) {
830 return;
831 }
832
833 /*
834 * First test whether the method is already in the call chain. Skip over
835 * any leading filters.
836 */
837
838 for (i = cbPtr->filterLength ; i < callPtr->numChain ; i++) {
839 if (callPtr->chain[i].mPtr == mPtr &&
840 callPtr->chain[i].isFilter == (doneFilters != NULL)) {
841 /*
842 * Call chain semantics states that methods come as *late* in the
843 * call chain as possible. This is done by copying down the
844 * following methods. Note that this does not change the number of
845 * method invocations in the call chain; it just rearranges them.
846 */
847
848 Class *declCls = callPtr->chain[i].filterDeclarer;
849
850 for (; i + 1 < callPtr->numChain ; i++) {
851 callPtr->chain[i] = callPtr->chain[i + 1];
852 }
853 callPtr->chain[i].mPtr = mPtr;
854 callPtr->chain[i].isFilter = (doneFilters != NULL);
855 callPtr->chain[i].filterDeclarer = declCls;
856 return;
857 }
858 }
859
860 /*
861 * Need to really add the method. This is made a bit more complex by the
862 * fact that we are using some "static" space initially, and only start
863 * realloc-ing if the chain gets long.
864 */
865
866 if (callPtr->numChain == CALL_CHAIN_STATIC_SIZE) {
867 callPtr->chain =
868 (struct MInvoke *)ckalloc(sizeof(struct MInvoke) * (callPtr->numChain + 1));
869 memcpy(callPtr->chain, callPtr->staticChain,
870 sizeof(struct MInvoke) * callPtr->numChain);
871 } else if (callPtr->numChain > CALL_CHAIN_STATIC_SIZE) {
872 callPtr->chain = (struct MInvoke *)ckrealloc(callPtr->chain,
873 sizeof(struct MInvoke) * (callPtr->numChain + 1));
874 }
875 callPtr->chain[i].mPtr = mPtr;
876 callPtr->chain[i].isFilter = (doneFilters != NULL);
877 callPtr->chain[i].filterDeclarer = filterDecl;
878 callPtr->numChain++;
879 }
880
881 /*
882 * ----------------------------------------------------------------------
883 *
884 * InitCallChain --
885 * Encoding of the policy of how to set up a call chain. Doesn't populate
886 * the chain with the method implementation data.
887 *
888 * ----------------------------------------------------------------------
889 */
890
891 static inline void
892 InitCallChain(
893 CallChain *callPtr,
894 Object *oPtr,
895 int flags)
896 {
897 /*
898 * Note that it's possible to end up with a NULL oPtr->selfCls here if
899 * there is a call into stereotypical object after it has finished running
900 * its destructor phase. Such things can't be cached for a long time so the
901 * epoch can be bogus. [Bug 7842f33a5c]
902 */
903
904 callPtr->flags = flags &
905 (PUBLIC_METHOD | PRIVATE_METHOD | SPECIAL | FILTER_HANDLING);
906 if (oPtr->flags & USE_CLASS_CACHE) {
907 oPtr = (oPtr->selfCls ? oPtr->selfCls->thisPtr : NULL);
908 callPtr->flags |= USE_CLASS_CACHE;
909 }
910 if (oPtr) {
911 callPtr->epoch = oPtr->fPtr->epoch;
912 callPtr->objectCreationEpoch = oPtr->creationEpoch;
913 callPtr->objectEpoch = oPtr->epoch;
914 } else {
915 callPtr->epoch = 0;
916 callPtr->objectCreationEpoch = 0;
917 callPtr->objectEpoch = 0;
918 }
919 callPtr->refCount = 1;
920 callPtr->numChain = 0;
921 callPtr->chain = callPtr->staticChain;
922 }
923
924 /*
925 * ----------------------------------------------------------------------
926 *
927 * IsStillValid --
928 *
929 * Calculates whether the given call chain can be used for executing a
930 * method for the given object. The condition on a chain from a cached
931 * location being reusable is:
932 * - Refers to the same object (same creation epoch), and
933 * - Still across the same class structure (same global epoch), and
934 * - Still across the same object structure (same local epoch), and
935 * - No public/private/filter magic leakage (same flags, modulo the fact
936 * that a public chain will satisfy a non-public call).
937 *
938 * ----------------------------------------------------------------------
939 */
940
941 static inline int
942 IsStillValid(
943 CallChain *callPtr,
944 Object *oPtr,
945 int flags,
946 int mask)
947 {
948 if ((oPtr->flags & USE_CLASS_CACHE)) {
949 /*
950 * If the object is in a weird state (due to stereotype tricks) then
951 * just declare the cache invalid. [Bug 7842f33a5c]
952 */
953 if (!oPtr->selfCls) {
954 return 0;
955 }
956 oPtr = oPtr->selfCls->thisPtr;
957 flags |= USE_CLASS_CACHE;
958 }
959 return ((callPtr->objectCreationEpoch == oPtr->creationEpoch)
960 && (callPtr->epoch == oPtr->fPtr->epoch)
961 && (callPtr->objectEpoch == oPtr->epoch)
962 && ((callPtr->flags & mask) == (flags & mask)));
963 }
964
965 /*
966 * ----------------------------------------------------------------------
967 *
968 * TclOOGetCallContext --
969 *
970 * Responsible for constructing the call context, an ordered list of all
971 * method implementations to be called as part of a method invocation.
972 * This method is central to the whole operation of the OO system.
973 *
974 * ----------------------------------------------------------------------
975 */
976
977 CallContext *
978 TclOOGetCallContext(
979 Object *oPtr, /* The object to get the context for. */
980 Tcl_Obj *methodNameObj, /* The name of the method to get the context
981 * for. NULL when getting a constructor or
982 * destructor chain. */
983 int flags, /* What sort of context are we looking for.
984 * Only the bits PUBLIC_METHOD, CONSTRUCTOR,
985 * PRIVATE_METHOD, DESTRUCTOR and
986 * FILTER_HANDLING are useful. */
987 Tcl_Obj *cacheInThisObj) /* What object to cache in, or NULL if it is
988 * to be in the same object as the
989 * methodNameObj. */
990 {
991 CallContext *contextPtr;
992 CallChain *callPtr;
993 struct ChainBuilder cb;
994 int i, count;
995 int doFilters;
996 Tcl_HashEntry *hPtr;
997 Tcl_HashTable doneFilters;
998
999 if (cacheInThisObj == NULL) {
1000 cacheInThisObj = methodNameObj;
1001 }
1002 if (flags&(SPECIAL|FILTER_HANDLING) || (oPtr->flags&FILTER_HANDLING)) {
1003 hPtr = NULL;
1004 doFilters = 0;
1005
1006 /*
1007 * Check if we have a cached valid constructor or destructor.
1008 */
1009
1010 if (flags & CONSTRUCTOR) {
1011 callPtr = oPtr->selfCls->constructorChainPtr;
1012 if ((callPtr != NULL)
1013 && (callPtr->objectEpoch == oPtr->selfCls->thisPtr->epoch)
1014 && (callPtr->epoch == oPtr->fPtr->epoch)) {
1015 callPtr->refCount++;
1016 goto returnContext;
1017 }
1018 } else if (flags & DESTRUCTOR) {
1019 callPtr = oPtr->selfCls->destructorChainPtr;
1020 if ((oPtr->mixins.num == 0) && (callPtr != NULL)
1021 && (callPtr->objectEpoch == oPtr->selfCls->thisPtr->epoch)
1022 && (callPtr->epoch == oPtr->fPtr->epoch)) {
1023 callPtr->refCount++;
1024 goto returnContext;
1025 }
1026 }
1027 } else {
1028 /*
1029 * Check if we can get the chain out of the Tcl_Obj method name or out
1030 * of the cache. This is made a bit more complex by the fact that
1031 * there are multiple different layers of cache (in the Tcl_Obj, in
1032 * the object, and in the class).
1033 */
1034
1035 const int reuseMask = ((flags & PUBLIC_METHOD) ? ~0 : ~PUBLIC_METHOD);
1036
1037 if (cacheInThisObj->typePtr == &methodNameType) {
1038 callPtr = (CallChain *)cacheInThisObj->internalRep.twoPtrValue.ptr1;
1039 if (IsStillValid(callPtr, oPtr, flags, reuseMask)) {
1040 callPtr->refCount++;
1041 goto returnContext;
1042 }
1043 FreeMethodNameRep(cacheInThisObj);
1044 }
1045
1046 /*
1047 * Note that it's possible to end up with a NULL oPtr->selfCls here if
1048 * there is a call into stereotypical object after it has finished
1049 * running its destructor phase. It's quite a tangle, but at that
1050 * point, we simply can't get stereotypes from the cache.
1051 * [Bug 7842f33a5c]
1052 */
1053
1054 if (oPtr->flags & USE_CLASS_CACHE && oPtr->selfCls) {
1055 if (oPtr->selfCls->classChainCache) {
1056 hPtr = Tcl_FindHashEntry(oPtr->selfCls->classChainCache,
1057 (char *) methodNameObj);
1058 } else {
1059 hPtr = NULL;
1060 }
1061 } else {
1062 if (oPtr->chainCache != NULL) {
1063 hPtr = Tcl_FindHashEntry(oPtr->chainCache,
1064 (char *) methodNameObj);
1065 } else {
1066 hPtr = NULL;
1067 }
1068 }
1069
1070 if (hPtr != NULL && Tcl_GetHashValue(hPtr) != NULL) {
1071 callPtr = (CallChain *)Tcl_GetHashValue(hPtr);
1072 if (IsStillValid(callPtr, oPtr, flags, reuseMask)) {
1073 callPtr->refCount++;
1074 goto returnContext;
1075 }
1076 Tcl_SetHashValue(hPtr, NULL);
1077 TclOODeleteChain(callPtr);
1078 }
1079
1080 doFilters = 1;
1081 }
1082
1083 callPtr = (CallChain *)ckalloc(sizeof(CallChain));
1084 InitCallChain(callPtr, oPtr, flags);
1085
1086 cb.callChainPtr = callPtr;
1087 cb.filterLength = 0;
1088 cb.oPtr = oPtr;
1089
1090 /*
1091 * If we're working with a forced use of unknown, do that now.
1092 */
1093
1094 if (flags & FORCE_UNKNOWN) {
1095 AddSimpleChainToCallContext(oPtr, oPtr->fPtr->unknownMethodNameObj,
1096 &cb, NULL, BUILDING_MIXINS, NULL);
1097 AddSimpleChainToCallContext(oPtr, oPtr->fPtr->unknownMethodNameObj,
1098 &cb, NULL, 0, NULL);
1099 callPtr->flags |= OO_UNKNOWN_METHOD;
1100 callPtr->epoch = -1;
1101 if (callPtr->numChain == 0) {
1102 TclOODeleteChain(callPtr);
1103 return NULL;
1104 }
1105 goto returnContext;
1106 }
1107
1108 /*
1109 * Add all defined filters (if any, and if we're going to be processing
1110 * them; they're not processed for constructors, destructors or when we're
1111 * in the middle of processing a filter).
1112 */
1113
1114 if (doFilters) {
1115 Tcl_Obj *filterObj;
1116 Class *mixinPtr;
1117
1118 doFilters = 1;
1119 Tcl_InitObjHashTable(&doneFilters);
1120 FOREACH(mixinPtr, oPtr->mixins) {
1121 AddClassFiltersToCallContext(oPtr, mixinPtr, &cb, &doneFilters,
1122 TRAVERSED_MIXIN|BUILDING_MIXINS|OBJECT_MIXIN);
1123 AddClassFiltersToCallContext(oPtr, mixinPtr, &cb, &doneFilters,
1124 OBJECT_MIXIN);
1125 }
1126 FOREACH(filterObj, oPtr->filters) {
1127 AddSimpleChainToCallContext(oPtr, filterObj, &cb, &doneFilters,
1128 BUILDING_MIXINS, NULL);
1129 AddSimpleChainToCallContext(oPtr, filterObj, &cb, &doneFilters, 0,
1130 NULL);
1131 }
1132 AddClassFiltersToCallContext(oPtr, oPtr->selfCls, &cb, &doneFilters,
1133 BUILDING_MIXINS);
1134 AddClassFiltersToCallContext(oPtr, oPtr->selfCls, &cb, &doneFilters,
1135 0);
1136 Tcl_DeleteHashTable(&doneFilters);
1137 }
1138 count = cb.filterLength = callPtr->numChain;
1139
1140 /*
1141 * Add the actual method implementations. We have to do this twice to
1142 * handle class mixins right.
1143 */
1144
1145 AddSimpleChainToCallContext(oPtr, methodNameObj, &cb, NULL,
1146 flags|BUILDING_MIXINS, NULL);
1147 AddSimpleChainToCallContext(oPtr, methodNameObj, &cb, NULL, flags, NULL);
1148
1149 /*
1150 * Check to see if the method has no implementation. If so, we probably
1151 * need to add in a call to the unknown method. Otherwise, set up the
1152 * cacheing of the method implementation (if relevant).
1153 */
1154
1155 if (count == callPtr->numChain) {
1156 /*
1157 * Method does not actually exist. If we're dealing with constructors
1158 * or destructors, this isn't a problem.
1159 */
1160
1161 if (flags & SPECIAL) {
1162 TclOODeleteChain(callPtr);
1163 return NULL;
1164 }
1165 AddSimpleChainToCallContext(oPtr, oPtr->fPtr->unknownMethodNameObj,
1166 &cb, NULL, BUILDING_MIXINS, NULL);
1167 AddSimpleChainToCallContext(oPtr, oPtr->fPtr->unknownMethodNameObj,
1168 &cb, NULL, 0, NULL);
1169 callPtr->flags |= OO_UNKNOWN_METHOD;
1170 callPtr->epoch = -1;
1171 if (count == callPtr->numChain) {
1172 TclOODeleteChain(callPtr);
1173 return NULL;
1174 }
1175 } else if (doFilters) {
1176 if (hPtr == NULL) {
1177 if (oPtr->flags & USE_CLASS_CACHE) {
1178 if (oPtr->selfCls->classChainCache == NULL) {
1179 oPtr->selfCls->classChainCache =
1180 (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
1181
1182 Tcl_InitObjHashTable(oPtr->selfCls->classChainCache);
1183 }
1184 hPtr = Tcl_CreateHashEntry(oPtr->selfCls->classChainCache,
1185 (char *) methodNameObj, &i);
1186 } else {
1187 if (oPtr->chainCache == NULL) {
1188 oPtr->chainCache = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
1189
1190 Tcl_InitObjHashTable(oPtr->chainCache);
1191 }
1192 hPtr = Tcl_CreateHashEntry(oPtr->chainCache,
1193 (char *) methodNameObj, &i);
1194 }
1195 }
1196 callPtr->refCount++;
1197 Tcl_SetHashValue(hPtr, callPtr);
1198 StashCallChain(cacheInThisObj, callPtr);
1199 } else if (flags & CONSTRUCTOR) {
1200 if (oPtr->selfCls->constructorChainPtr) {
1201 TclOODeleteChain(oPtr->selfCls->constructorChainPtr);
1202 }
1203 oPtr->selfCls->constructorChainPtr = callPtr;
1204 callPtr->refCount++;
1205 } else if ((flags & DESTRUCTOR) && oPtr->mixins.num == 0) {
1206 if (oPtr->selfCls->destructorChainPtr) {
1207 TclOODeleteChain(oPtr->selfCls->destructorChainPtr);
1208 }
1209 oPtr->selfCls->destructorChainPtr = callPtr;
1210 callPtr->refCount++;
1211 }
1212
1213 returnContext:
1214 contextPtr = (CallContext *)TclStackAlloc(oPtr->fPtr->interp, sizeof(CallContext));
1215 contextPtr->oPtr = oPtr;
1216
1217 /*
1218 * Corresponding TclOODecrRefCount() in TclOODeleteContext
1219 */
1220
1221 AddRef(oPtr);
1222 contextPtr->callPtr = callPtr;
1223 contextPtr->skip = 2;
1224 contextPtr->index = 0;
1225 return contextPtr;
1226 }
1227
1228 /*
1229 * ----------------------------------------------------------------------
1230 *
1231 * TclOOGetStereotypeCallChain --
1232 *
1233 * Construct a call-chain for a method that would be used by a
1234 * stereotypical instance of the given class (i.e., where the object has
1235 * no definitions special to itself).
1236 *
1237 * ----------------------------------------------------------------------
1238 */
1239
1240 CallChain *
1241 TclOOGetStereotypeCallChain(
1242 Class *clsPtr, /* The object to get the context for. */
1243 Tcl_Obj *methodNameObj, /* The name of the method to get the context
1244 * for. NULL when getting a constructor or
1245 * destructor chain. */
1246 int flags) /* What sort of context are we looking for.
1247 * Only the bits PUBLIC_METHOD, CONSTRUCTOR,
1248 * PRIVATE_METHOD, DESTRUCTOR and
1249 * FILTER_HANDLING are useful. */
1250 {
1251 CallChain *callPtr;
1252 struct ChainBuilder cb;
1253 int i, count;
1254 Foundation *fPtr = clsPtr->thisPtr->fPtr;
1255 Tcl_HashEntry *hPtr;
1256 Tcl_HashTable doneFilters;
1257 Object obj;
1258
1259 /*
1260 * Note that it's possible to end up with a NULL clsPtr here if there is
1261 * a call into stereotypical object after it has finished running its
1262 * destructor phase. It's quite a tangle, but at that point, we simply
1263 * can't get stereotypes. [Bug 7842f33a5c]
1264 */
1265
1266 if (clsPtr == NULL) {
1267 return NULL;
1268 }
1269
1270 /*
1271 * Synthesize a temporary stereotypical object so that we can use existing
1272 * machinery to produce the stereotypical call chain.
1273 */
1274
1275 memset(&obj, 0, sizeof(Object));
1276 obj.fPtr = fPtr;
1277 obj.selfCls = clsPtr;
1278 obj.refCount = 1;
1279 obj.flags = USE_CLASS_CACHE;
1280
1281 /*
1282 * Check if we can get the chain out of the Tcl_Obj method name or out of
1283 * the cache. This is made a bit more complex by the fact that there are
1284 * multiple different layers of cache (in the Tcl_Obj, in the object, and
1285 * in the class).
1286 */
1287
1288 if (clsPtr->classChainCache != NULL) {
1289 hPtr = Tcl_FindHashEntry(clsPtr->classChainCache,
1290 (char *) methodNameObj);
1291 if (hPtr != NULL && Tcl_GetHashValue(hPtr) != NULL) {
1292 const int reuseMask =
1293 ((flags & PUBLIC_METHOD) ? ~0 : ~PUBLIC_METHOD);
1294
1295 callPtr = (CallChain *)Tcl_GetHashValue(hPtr);
1296 if (IsStillValid(callPtr, &obj, flags, reuseMask)) {
1297 callPtr->refCount++;
1298 return callPtr;
1299 }
1300 Tcl_SetHashValue(hPtr, NULL);
1301 TclOODeleteChain(callPtr);
1302 }
1303 } else {
1304 hPtr = NULL;
1305 }
1306
1307 callPtr = (CallChain *)ckalloc(sizeof(CallChain));
1308 memset(callPtr, 0, sizeof(CallChain));
1309 callPtr->flags = flags & (PUBLIC_METHOD|PRIVATE_METHOD|FILTER_HANDLING);
1310 callPtr->epoch = fPtr->epoch;
1311 callPtr->objectCreationEpoch = fPtr->tsdPtr->nsCount;
1312 callPtr->objectEpoch = clsPtr->thisPtr->epoch;
1313 callPtr->refCount = 1;
1314 callPtr->chain = callPtr->staticChain;
1315
1316 cb.callChainPtr = callPtr;
1317 cb.filterLength = 0;
1318 cb.oPtr = &obj;
1319
1320 /*
1321 * Add all defined filters (if any, and if we're going to be processing
1322 * them; they're not processed for constructors, destructors or when we're
1323 * in the middle of processing a filter).
1324 */
1325
1326 Tcl_InitObjHashTable(&doneFilters);
1327 AddClassFiltersToCallContext(&obj, clsPtr, &cb, &doneFilters,
1328 BUILDING_MIXINS);
1329 AddClassFiltersToCallContext(&obj, clsPtr, &cb, &doneFilters, 0);
1330 Tcl_DeleteHashTable(&doneFilters);
1331 count = cb.filterLength = callPtr->numChain;
1332
1333 /*
1334 * Add the actual method implementations.
1335 */
1336
1337 AddSimpleChainToCallContext(&obj, methodNameObj, &cb, NULL,
1338 flags|BUILDING_MIXINS, NULL);
1339 AddSimpleChainToCallContext(&obj, methodNameObj, &cb, NULL, flags, NULL);
1340
1341 /*
1342 * Check to see if the method has no implementation. If so, we probably
1343 * need to add in a call to the unknown method. Otherwise, set up the
1344 * caching of the method implementation (if relevant).
1345 */
1346
1347 if (count == callPtr->numChain) {
1348 AddSimpleChainToCallContext(&obj, fPtr->unknownMethodNameObj, &cb,
1349 NULL, BUILDING_MIXINS, NULL);
1350 AddSimpleChainToCallContext(&obj, fPtr->unknownMethodNameObj, &cb,
1351 NULL, 0, NULL);
1352 callPtr->flags |= OO_UNKNOWN_METHOD;
1353 callPtr->epoch = -1;
1354 if (count == callPtr->numChain) {
1355 TclOODeleteChain(callPtr);
1356 return NULL;
1357 }
1358 } else {
1359 if (hPtr == NULL) {
1360 if (clsPtr->classChainCache == NULL) {
1361 clsPtr->classChainCache = (Tcl_HashTable *)ckalloc(sizeof(Tcl_HashTable));
1362 Tcl_InitObjHashTable(clsPtr->classChainCache);
1363 }
1364 hPtr = Tcl_CreateHashEntry(clsPtr->classChainCache,
1365 (char *) methodNameObj, &i);
1366 }
1367 callPtr->refCount++;
1368 Tcl_SetHashValue(hPtr, callPtr);
1369 StashCallChain(methodNameObj, callPtr);
1370 }
1371 return callPtr;
1372 }
1373
1374 /*
1375 * ----------------------------------------------------------------------
1376 *
1377 * AddClassFiltersToCallContext --
1378 *
1379 * Logic to make extracting all the filters from the class context much
1380 * easier.
1381 *
1382 * ----------------------------------------------------------------------
1383 */
1384
1385 static void
1386 AddClassFiltersToCallContext(
1387 Object *const oPtr, /* Object that the filters operate on. */
1388 Class *clsPtr, /* Class to get the filters from. */
1389 struct ChainBuilder *const cbPtr,
1390 /* Context to fill with call chain entries. */
1391 Tcl_HashTable *const doneFilters,
1392 /* Where to record what filters have been
1393 * processed. Keys are objects, values are
1394 * ignored. */
1395 int flags) /* Whether we've gone along a mixin link
1396 * yet. */
1397 {
1398 int i;
1399 int clearedFlags =
1400 flags & ~(TRAVERSED_MIXIN|OBJECT_MIXIN|BUILDING_MIXINS);
1401 Class *superPtr, *mixinPtr;
1402 Tcl_Obj *filterObj;
1403
1404 tailRecurse:
1405 if (clsPtr == NULL) {
1406 return;
1407 }
1408
1409 /*
1410 * Add all the filters defined by classes mixed into the main class
1411 * hierarchy.
1412 */
1413
1414 FOREACH(mixinPtr, clsPtr->mixins) {
1415 AddClassFiltersToCallContext(oPtr, mixinPtr, cbPtr, doneFilters,
1416 flags|TRAVERSED_MIXIN);
1417 }
1418
1419 /*
1420 * Add all the class filters from the current class. Note that the filters
1421 * are added starting at the object root, as this allows the object to
1422 * override how filters work to extend their behaviour.
1423 */
1424
1425 if (MIXIN_CONSISTENT(flags)) {
1426 FOREACH(filterObj, clsPtr->filters) {
1427 int isNew;
1428
1429 (void) Tcl_CreateHashEntry(doneFilters, (char *) filterObj,
1430 &isNew);
1431 if (isNew) {
1432 AddSimpleChainToCallContext(oPtr, filterObj, cbPtr,
1433 doneFilters, clearedFlags|BUILDING_MIXINS, clsPtr);
1434 AddSimpleChainToCallContext(oPtr, filterObj, cbPtr,
1435 doneFilters, clearedFlags, clsPtr);
1436 }
1437 }
1438 }
1439
1440 /*
1441 * Now process the recursive case. Notice the tail-call optimization.
1442 */
1443
1444 switch (clsPtr->superclasses.num) {
1445 case 1:
1446 clsPtr = clsPtr->superclasses.list[0];
1447 goto tailRecurse;
1448 default:
1449 FOREACH(superPtr, clsPtr->superclasses) {
1450 AddClassFiltersToCallContext(oPtr, superPtr, cbPtr, doneFilters,
1451 flags);
1452 }
1453 case 0:
1454 return;
1455 }
1456 }
1457
1458 /*
1459 * ----------------------------------------------------------------------
1460 *
1461 * AddSimpleClassChainToCallContext --
1462 *
1463 * Construct a call-chain from a class hierarchy.
1464 *
1465 * ----------------------------------------------------------------------
1466 */
1467
1468 static void
1469 AddSimpleClassChainToCallContext(
1470 Class *classPtr, /* Class to add the call chain entries for. */
1471 Tcl_Obj *const methodNameObj,
1472 /* Name of method to add the call chain
1473 * entries for. */
1474 struct ChainBuilder *const cbPtr,
1475 /* Where to add the call chain entries. */
1476 Tcl_HashTable *const doneFilters,
1477 /* Where to record what call chain entries
1478 * have been processed. */
1479 int flags, /* What sort of call chain are we building. */
1480 Class *const filterDecl) /* The class that declared the filter. If
1481 * NULL, either the filter was declared by the
1482 * object or this isn't a filter. */
1483 {
1484 int i;
1485 Class *superPtr;
1486
1487 /*
1488 * We hard-code the tail-recursive form. It's by far the most common case
1489 * *and* it is much more gentle on the stack.
1490 *
1491 * Note that mixins must be processed before the main class hierarchy.
1492 * [Bug 1998221]
1493 *
1494 * Note also that it's possible to end up with a null classPtr here if
1495 * there is a call into stereotypical object after it has finished running
1496 * its destructor phase. [Bug 7842f33a5c]
1497 */
1498
1499 tailRecurse:
1500 if (classPtr == NULL) {
1501 return;
1502 }
1503 FOREACH(superPtr, classPtr->mixins) {
1504 AddSimpleClassChainToCallContext(superPtr, methodNameObj, cbPtr,
1505 doneFilters, flags|TRAVERSED_MIXIN, filterDecl);
1506 }
1507
1508 if (flags & CONSTRUCTOR) {
1509 AddMethodToCallChain(classPtr->constructorPtr, cbPtr, doneFilters,
1510 filterDecl, flags);
1511 } else if (flags & DESTRUCTOR) {
1512 AddMethodToCallChain(classPtr->destructorPtr, cbPtr, doneFilters,
1513 filterDecl, flags);
1514 } else {
1515 Tcl_HashEntry *hPtr = Tcl_FindHashEntry(&classPtr->classMethods,
1516 (char *) methodNameObj);
1517
1518 if (hPtr != NULL) {
1519 Method *mPtr = (Method *)Tcl_GetHashValue(hPtr);
1520
1521 if (!(flags & KNOWN_STATE)) {
1522 if (flags & PUBLIC_METHOD) {
1523 if (mPtr->flags & PUBLIC_METHOD) {
1524 flags |= DEFINITE_PUBLIC;
1525 } else {
1526 return;
1527 }
1528 } else {
1529 flags |= DEFINITE_PROTECTED;
1530 }
1531 }
1532 AddMethodToCallChain(mPtr, cbPtr, doneFilters, filterDecl, flags);
1533 }
1534 }
1535
1536 switch (classPtr->superclasses.num) {
1537 case 1:
1538 classPtr = classPtr->superclasses.list[0];
1539 goto tailRecurse;
1540 default:
1541 FOREACH(superPtr, classPtr->superclasses) {
1542 AddSimpleClassChainToCallContext(superPtr, methodNameObj, cbPtr,
1543 doneFilters, flags, filterDecl);
1544 }
1545 /* FALLTHRU */
1546 case 0:
1547 return;
1548 }
1549 }
1550
1551 /*
1552 * ----------------------------------------------------------------------
1553 *
1554 * TclOORenderCallChain --
1555 *
1556 * Create a description of a call chain. Used in [info object call],
1557 * [info class call], and [self call].
1558 *
1559 * ----------------------------------------------------------------------
1560 */
1561
1562 Tcl_Obj *
1563 TclOORenderCallChain(
1564 Tcl_Interp *interp,
1565 CallChain *callPtr)
1566 {
1567 Tcl_Obj *filterLiteral, *methodLiteral, *objectLiteral;
1568 Tcl_Obj *resultObj, *descObjs[4], **objv;
1569 Foundation *fPtr = TclOOGetFoundation(interp);
1570 int i;
1571
1572 /*
1573 * Allocate the literals (potentially) used in our description.
1574 */
1575
1576 filterLiteral = Tcl_NewStringObj("filter", -1);
1577 Tcl_IncrRefCount(filterLiteral);
1578 methodLiteral = Tcl_NewStringObj("method", -1);
1579 Tcl_IncrRefCount(methodLiteral);
1580 objectLiteral = Tcl_NewStringObj("object", -1);
1581 Tcl_IncrRefCount(objectLiteral);
1582
1583 /*
1584 * Do the actual construction of the descriptions. They consist of a list
1585 * of triples that describe the details of how a method is understood. For
1586 * each triple, the first word is the type of invocation ("method" is
1587 * normal, "unknown" is special because it adds the method name as an
1588 * extra argument when handled by some method types, and "filter" is
1589 * special because it's a filter method). The second word is the name of
1590 * the method in question (which differs for "unknown" and "filter" types)
1591 * and the third word is the full name of the class that declares the
1592 * method (or "object" if it is declared on the instance).
1593 */
1594
1595 objv = (Tcl_Obj **)TclStackAlloc(interp, callPtr->numChain * sizeof(Tcl_Obj *));
1596 for (i = 0 ; i < callPtr->numChain ; i++) {
1597 struct MInvoke *miPtr = &callPtr->chain[i];
1598
1599 descObjs[0] =
1600 miPtr->isFilter ? filterLiteral :
1601 callPtr->flags & OO_UNKNOWN_METHOD ? fPtr->unknownMethodNameObj :
1602 methodLiteral;
1603 descObjs[1] =
1604 callPtr->flags & CONSTRUCTOR ? fPtr->constructorName :
1605 callPtr->flags & DESTRUCTOR ? fPtr->destructorName :
1606 miPtr->mPtr->namePtr;
1607 descObjs[2] = miPtr->mPtr->declaringClassPtr
1608 ? Tcl_GetObjectName(interp,
1609 (Tcl_Object) miPtr->mPtr->declaringClassPtr->thisPtr)
1610 : objectLiteral;
1611 descObjs[3] = Tcl_NewStringObj(miPtr->mPtr->typePtr->name, -1);
1612
1613 objv[i] = Tcl_NewListObj(4, descObjs);
1614 }
1615
1616 /*
1617 * Drop the local references to the literals; if they're actually used,
1618 * they'll live on the description itself.
1619 */
1620
1621 Tcl_DecrRefCount(filterLiteral);
1622 Tcl_DecrRefCount(methodLiteral);
1623 Tcl_DecrRefCount(objectLiteral);
1624
1625 /*
1626 * Finish building the description and return it.
1627 */
1628
1629 resultObj = Tcl_NewListObj(callPtr->numChain, objv);
1630 TclStackFree(interp, objv);
1631 return resultObj;
1632 }
1633
1634 /*
1635 * Local Variables:
1636 * mode: c
1637 * c-basic-offset: 4
1638 * fill-column: 78
1639 * End:
1640 */