Skip to content
Tcl Source Code Artifact [35fd91b624]
Login

Artifact [35fd91b624]

Artifact 35fd91b624d21603417ee2e1103ac05d1aac21b6269faa1f5ade66c929eb9b7e:


--- a/generic/tclProc.c
+++ b/generic/tclProc.c
@@ -205,38 +205,16 @@
      */
 
     /*
-     * v16p : une liste d'arguments typée (un produit) ou un type de retour
-     * (proc {T}nom) : la procédure reçoit ses mots dans « args », la
-     * signature lie les champs à l'appel.
+     * v16p, v17k : une liste d'arguments typée (un produit) ou un type de
+     * retour (proc {T}nom) ; les types sont lus dans l'espace du nom.
      */
-    {
-	void *sig = NULL;
-	int typedArgs = 0;
-	Tcl_Obj *argsObj = objv[2];
-
-	if (TclTypedProcSig(interp, objv[1], objv[2], &sig, &typedArgs)
-		!= TCL_OK) {
-	    Tcl_AddErrorInfo(interp, "\n    (creating proc \"");
-	    Tcl_AddErrorInfo(interp, simpleName);
-	    Tcl_AddErrorInfo(interp, "\")");
-	    return TCL_ERROR;
-	}
-	if (typedArgs) {
-	    argsObj = Tcl_NewStringObj("args", 4);
-	}
-	Tcl_IncrRefCount(argsObj);
-	if (TclCreateProc(interp, /*ignored nsPtr*/ NULL, simpleName, argsObj,
-		objv[3], &procPtr) != TCL_OK) {
-	    Tcl_DecrRefCount(argsObj);
-	    TclTypedSigRelease(sig);
-	    Tcl_AddErrorInfo(interp, "\n    (creating proc \"");
-	    Tcl_AddErrorInfo(interp, simpleName);
-	    Tcl_AddErrorInfo(interp, "\")");
-	    return TCL_ERROR;
-	}
-	Tcl_DecrRefCount(argsObj);
-	TclTypedSigRelease(procPtr->typedSig);
-	procPtr->typedSig = sig;
+
+    if (TclCreateTypedProc(interp, NULL, objv[1], simpleName, objv[2],
+	    objv[3], &procPtr) != TCL_OK) {
+	Tcl_AddErrorInfo(interp, "\n    (creating proc \"");
+	Tcl_AddErrorInfo(interp, simpleName);
+	Tcl_AddErrorInfo(interp, "\")");
+	return TCL_ERROR;
     }
 
     cmd = TclNRCreateCommandInNs(interp, simpleName, (Tcl_Namespace *) nsPtr,
@@ -455,6 +433,87 @@
 /*
  *----------------------------------------------------------------------
  *
+ * TclCreateTypedProc --
+ *
+ *	TIP 764 (v17k): creates a procedure whose formal argument list may be
+ *	a typed signature (v16p: {int}n, a product, options, defaults "?=")
+ *	and whose name may carry a return type ({T}name). The one entry point
+ *	for every creator of procedures: [proc], [apply], TclOO methods. An
+ *	untyped list is handled by TclCreateProc exactly as before.
+ *
+ *	A typed signature is kept in procPtr->typedSig; the Proc itself takes
+ *	its words in "args", and the fields are bound at each call
+ *	(InitArgsAndLocals, TclTypedProcBind), the result checked on return
+ *	(TclTypedProcReturn).
+ *
+ *	nsPtr: the namespace in which the types are looked up; NULL means the
+ *	namespace of nameObj, qualified from the current one (proc, apply).
+ *	nameObj: the name as written, carrying the return type, or NULL
+ *	(procName is then used). The caller's objects may be unowned
+ *	(refCount 0, as TclOO passes them): they are never released here.
+ *
+ * Results:
+ *	As TclCreateProc. No error info is added: the caller does it.
+ *
+ *----------------------------------------------------------------------
+ */
+
+int
+TclCreateTypedProc(
+    Tcl_Interp *interp,
+    Tcl_Namespace *nsPtr,	/* Where types are resolved, or NULL. */
+    Tcl_Obj *nameObj,		/* The name as written, or NULL. */
+    const char *procName,	/* The name, as TclCreateProc wants it. */
+    Tcl_Obj *argsObj,		/* The formal arguments, typed or not. */
+    Tcl_Obj *bodyObj,		/* The body. */
+    Proc **procPtrPtr)		/* Where to put the new Proc. */
+{
+    void *sig = NULL;
+    int typedArgs = 0, code;
+    Tcl_Obj *n, *a;
+
+    /*
+     * Reading the return type pops the annotation of the name: work on
+     * copies of unowned objects, and never drop the caller's last
+     * reference.
+     */
+
+    if (nameObj == NULL) {
+	n = Tcl_NewStringObj(procName, -1);
+    } else if (nameObj->refCount == 0) {
+	n = Tcl_DuplicateObj(nameObj);
+    } else {
+	n = nameObj;
+    }
+    a = (argsObj->refCount == 0) ? Tcl_DuplicateObj(argsObj) : argsObj;
+    Tcl_IncrRefCount(n);
+    Tcl_IncrRefCount(a);
+    code = TclTypedProcSigNs(interp, nsPtr, n, a, &sig, &typedArgs);
+    Tcl_DecrRefCount(n);
+    if (code != TCL_OK) {
+	Tcl_DecrRefCount(a);
+	return TCL_ERROR;
+    }
+    if (typedArgs) {
+	Tcl_DecrRefCount(a);
+	TclNewLiteralStringObj(a, "args");
+	Tcl_IncrRefCount(a);
+    }
+    code = TclCreateProc(interp, /*ignored nsPtr*/ NULL, procName, a,
+	    bodyObj, procPtrPtr);
+    Tcl_DecrRefCount(a);
+    if (code != TCL_OK) {
+	TclTypedSigRelease(sig);
+	return TCL_ERROR;
+    }
+    TclTypedSigRelease((*procPtrPtr)->typedSig);
+    (*procPtrPtr)->typedSig = sig;
+    return TCL_OK;
+}
+
+/*
+ *----------------------------------------------------------------------
+ *
  * TclCreateProc --
  *
  *	Creates the data associated with a Tcl procedure definition. This
@@ -2611,13 +2670,11 @@
     name = TclGetString(objPtr);
 
     /*
-     * v16z : une liste d'arguments typée, comme pour proc ; les types sont
-     * lus dans l'espace de noms de la lambda. Le type de retour (apply
+     * v16z, v17k : une liste d'arguments typée, comme pour proc ; les types
+     * sont lus dans l'espace de noms de la lambda. Le type de retour (apply
      * {T}{...}) est posé par TclNRApplyObjCmd.
      */
     {
-	void *sig = NULL;
-	int typedArgs = 0;
 	Tcl_Obj *ctxName;
 
 	if (objc == 3) {
@@ -2629,26 +2686,14 @@
 	    ctxName = Tcl_NewStringObj("::lambda", -1);
 	}
 	Tcl_IncrRefCount(ctxName);
-	result = TclTypedProcSig(interp, ctxName, argsPtr, &sig, &typedArgs);
+	result = TclCreateTypedProc(interp, NULL, ctxName, name, argsPtr,
+		bodyPtr, &procPtr);
 	Tcl_DecrRefCount(ctxName);
 	if (result != TCL_OK) {
 	    Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
 		    "\n    (parsing lambda expression \"%s\")", name));
 	    return TCL_ERROR;
 	}
-	argsPtr = typedArgs ? Tcl_NewStringObj("args", 4) : objv[0];
-	Tcl_IncrRefCount(argsPtr);
-	if (TclCreateProc(interp, /*ignored nsPtr*/ NULL, name, argsPtr,
-		bodyPtr, &procPtr) != TCL_OK) {
-	    Tcl_DecrRefCount(argsPtr);
-	    TclTypedSigRelease(sig);
-	    Tcl_AppendObjToErrorInfo(interp, Tcl_ObjPrintf(
-		    "\n    (parsing lambda expression \"%s\")", name));
-	    return TCL_ERROR;
-	}
-	Tcl_DecrRefCount(argsPtr);
-	TclTypedSigRelease(procPtr->typedSig);
-	procPtr->typedSig = sig;
     }
 
     /*
--- a/generic/tclOOMethod.c
+++ b/generic/tclOOMethod.c
@@ -413,75 +413,6 @@
 /*
  * ----------------------------------------------------------------------
  *
- * MethodSig --
- *
- *	TIP 764 (v16p for methods): a method's formal argument list may be
- *	typed, as a proc's is ({int}x, or a product). Reads the signature
- *	the way [proc] does (TclTypedProcSig): *sigPtr receives it, or NULL
- *	for a classic list; when the list is a product, *argsObjPtr is
- *	replaced by "args" (the signature binds the fields at the call, in
- *	InitArgsAndLocals, as for a proc). The types are read in the current
- *	namespace (that of the definition), as a proc's are in its own. On
- *	TCL_OK with a new "args", *ownedPtr is set: the caller releases it.
- *
- * ----------------------------------------------------------------------
- */
-
-static int
-MethodSig(
-    Tcl_Interp *interp,
-    Tcl_Obj *nameObj,		/* Method name, or NULL (constructor...). */
-    const char *procName,
-    Tcl_Obj **argsObjPtr,
-    void **sigPtr,
-    int *ownedPtr)
-{
-    int typedArgs = 0, code;
-    Tcl_Obj *n, *a;
-    Tcl_Size argsLen;
-
-    /*
-     * The callers may pass unowned objects (refCount 0: the property
-     * methods, Tcl_ObjPrintf): never take and drop their last reference
-     * here, read copies of them instead.
-     */
-
-    *sigPtr = NULL;
-    *ownedPtr = 0;
-    if (nameObj == NULL) {
-	n = Tcl_NewStringObj(procName, -1);
-    } else if (nameObj->refCount == 0) {
-	n = Tcl_DuplicateObj(nameObj);
-    } else {
-	n = nameObj;
-    }
-    a = (*argsObjPtr)->refCount == 0 ? Tcl_DuplicateObj(*argsObjPtr)
-	    : *argsObjPtr;
-    Tcl_IncrRefCount(n);
-    Tcl_IncrRefCount(a);
-    code = TclTypedProcSig(interp, n, a, sigPtr, &typedArgs);
-    Tcl_DecrRefCount(n);
-    Tcl_DecrRefCount(a);
-    if (code != TCL_OK) {
-	return TCL_ERROR;
-    }
-    if (typedArgs) {
-	*argsObjPtr = Tcl_NewStringObj("args", 4);
-	Tcl_IncrRefCount(*argsObjPtr);
-	*ownedPtr = 1;
-	return TCL_OK;
-    }
-    if (TclListObjLength(interp, *argsObjPtr, &argsLen) != TCL_OK) {
-	TclTypedSigRelease(*sigPtr);
-	*sigPtr = NULL;
-	return TCL_ERROR;
-    }
-    return TCL_OK;
-}
-
-/*
- * ----------------------------------------------------------------------
- *
  * TclOONewProcInstanceMethod --
  *
  *	Create a new procedure-like method for an object.
@@ -507,29 +438,18 @@
 {
     ProcedureMethod *pmPtr;
     Tcl_Method method;
-    void *sig;
-    int owned;
 
-    if (MethodSig(interp, nameObj, TclGetString(nameObj), &argsObj, &sig,
-	    &owned) != TCL_OK) {
-	return NULL;
-    }
+    /*
+     * TIP 764 (v17k): the argument list is read by TclCreateTypedProc, as
+     * for [proc]; a typed signature ({{int}n; {int}k}) need not be a list.
+     */
+
     pmPtr = AllocProcedureMethodRecord(flags);
     method = TclOOMakeProcInstanceMethod(interp, oPtr, flags, nameObj,
 	    argsObj, bodyObj, &procMethodType, pmPtr, &pmPtr->procPtr);
-    if (owned) {
-	Tcl_DecrRefCount(argsObj);
-    }
     if (method == NULL) {
-	TclTypedSigRelease(sig);
 	Tcl_Free(pmPtr);
-	return NULL;
-    }
-    if (sig != NULL) {
-	TclTypedSigRelease(pmPtr->procPtr->typedSig);
-	pmPtr->procPtr->typedSig = sig;
-    }
-    if (pmPtrPtr != NULL) {
+    } else if (pmPtrPtr != NULL) {
 	*pmPtrPtr = pmPtr;
     }
     return (Method *) method;
@@ -564,12 +484,10 @@
 				 * structure's contents. NULL if caller is not
 				 * interested. */
 {
-    Tcl_Size argsLen = 0;	/* TCL_INDEX_NONE => delete argsObj before exit */
+    Tcl_Size argsLen;		/* TCL_INDEX_NONE => delete argsObj before exit */
     ProcedureMethod *pmPtr;
     const char *procName;
     Tcl_Method method;
-    void *sig = NULL;
-    int owned = 0;
 
     if (argsObj == NULL) {
 	argsLen = TCL_INDEX_NONE;
@@ -577,30 +495,21 @@
 	Tcl_IncrRefCount(argsObj);
 	procName = "<destructor>";
     } else {
+	/* TIP 764 (v17k): argsObj read by TclCreateTypedProc, not as a list */
+	argsLen = 0;
 	procName = (nameObj==NULL ? "<constructor>" : TclGetString(nameObj));
-	if (MethodSig(interp, nameObj, procName, &argsObj, &sig,
-		&owned) != TCL_OK) {
-	    return NULL;
-	}
     }
 
     pmPtr = AllocProcedureMethodRecord(flags);
     method = TclOOMakeProcMethod(interp, clsPtr, flags, nameObj, procName,
 	    argsObj, bodyObj, &procMethodType, pmPtr, &pmPtr->procPtr);
 
-    if (argsLen == TCL_INDEX_NONE || owned) {
+    if (argsLen == TCL_INDEX_NONE) {
 	Tcl_DecrRefCount(argsObj);
     }
     if (method == NULL) {
-	TclTypedSigRelease(sig);
 	Tcl_Free(pmPtr);
-	return NULL;
-    }
-    if (sig != NULL) {
-	TclTypedSigRelease(pmPtr->procPtr->typedSig);
-	pmPtr->procPtr->typedSig = sig;
-    }
-    if (pmPtrPtr != NULL) {
+    } else if (pmPtrPtr != NULL) {
 	*pmPtrPtr = pmPtr;
     }
 
@@ -694,6 +603,40 @@
 /*
  * ----------------------------------------------------------------------
  *
+ * TypeNamespace --
+ *
+ *	TIP 764 (v17k): the namespace in which the types of a method signature
+ *	are looked up, as a procedure's are in the namespace of its name: the
+ *	namespace holding the object's command (a class's, for a class
+ *	method). An anonymous object (::oo::ObjN) uses its class's; failing
+ *	that, the global namespace. Never the definition namespace
+ *	(::oo::define), which holds no types.
+ *
+ * ----------------------------------------------------------------------
+ */
+
+static Tcl_Namespace *
+TypeNamespace(
+    Tcl_Interp *interp,
+    Object *oPtr)
+{
+    int step;
+
+    for (step = 0; step < 2 && oPtr != NULL; step++) {
+	Command *cmdPtr = (Command *) oPtr->command;
+
+	if (cmdPtr != NULL && cmdPtr->nsPtr != NULL
+		&& (Tcl_Namespace *) cmdPtr->nsPtr != oPtr->fPtr->ooNs) {
+	    return (Tcl_Namespace *) cmdPtr->nsPtr;
+	}
+	oPtr = (oPtr->selfCls != NULL) ? oPtr->selfCls->thisPtr : NULL;
+    }
+    return Tcl_GetGlobalNamespace(interp);
+}
+
+/*
+ * ----------------------------------------------------------------------
+ *
  * TclOOMakeProcInstanceMethod --
  *
  *	The guts of the code to make a procedure-like method for an object.
@@ -730,8 +673,8 @@
 	Tcl_Panic("%s: Wrong version in typePtr->version, should be %s",
 		"TclOOMakeProcInstanceMethod", "TCL_OO_METHOD_VERSION_1");
     }
-    if (TclCreateProc(interp, NULL, TclGetString(nameObj), argsObj, bodyObj,
-	    procPtrPtr) != TCL_OK) {
+    if (TclCreateTypedProc(interp, TypeNamespace(interp, oPtr), nameObj,
+	    TclGetString(nameObj), argsObj, bodyObj, procPtrPtr) != TCL_OK) {
 	return NULL;
     }
     procPtr = *procPtrPtr;
@@ -769,8 +712,8 @@
 	Tcl_Panic("%s: Wrong version in typePtr->version, should be %s",
 		"TclOOMakeProcInstanceMethod2", "TCL_OO_METHOD_VERSION_2");
     }
-    if (TclCreateProc(interp, NULL, TclGetString(nameObj), argsObj, bodyObj,
-	    procPtrPtr) != TCL_OK) {
+    if (TclCreateTypedProc(interp, TypeNamespace(interp, oPtr), nameObj,
+	    TclGetString(nameObj), argsObj, bodyObj, procPtrPtr) != TCL_OK) {
 	return NULL;
     }
     procPtr = *procPtrPtr;
@@ -825,8 +768,8 @@
 	Tcl_Panic("%s: Wrong version in typePtr->version, should be %s",
 		"TclOOMakeProcMethod", "TCL_OO_METHOD_VERSION_1");
     }
-    if (TclCreateProc(interp, NULL, namePtr, argsObj, bodyObj,
-	    procPtrPtr) != TCL_OK) {
+    if (TclCreateTypedProc(interp, TypeNamespace(interp, clsPtr->thisPtr),
+	    nameObj, namePtr, argsObj, bodyObj, procPtrPtr) != TCL_OK) {
 	return NULL;
     }
     procPtr = *procPtrPtr;
@@ -868,8 +811,8 @@
 	Tcl_Panic("%s: Wrong version in typePtr->version, should be %s",
 		"TclOOMakeProcMethod2", "TCL_OO_METHOD_VERSION_2");
     }
-    if (TclCreateProc(interp, NULL, namePtr, argsObj, bodyObj,
-	    procPtrPtr) != TCL_OK) {
+    if (TclCreateTypedProc(interp, TypeNamespace(interp, clsPtr->thisPtr),
+	    nameObj, namePtr, argsObj, bodyObj, procPtrPtr) != TCL_OK) {
 	return NULL;
     }
     procPtr = *procPtrPtr;
@@ -1563,6 +1506,14 @@
     Tcl_DecrRefCount(argsObj);
     Tcl_DecrRefCount(bodyObj);
 
+    /*
+     * TIP 764 (v17k): a typed signature is not in the compiled locals (the
+     * Proc only has "args"): share it with the copy.
+     */
+
+    pm2Ptr->procPtr->typedSig = pmPtr->procPtr->typedSig;
+    TclTypedSigRetain(pm2Ptr->procPtr->typedSig);
+
     if (pmPtr->cloneClientdataProc) {
 	pm2Ptr->clientData = pmPtr->cloneClientdataProc(pmPtr->clientData);
     }