Tcl Source Code

Changes On Branch tip-457
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Changes In Branch tip-457 Excluding Merge-Ins

This is equivalent to a diff from f1c99ff14c to be60dadb16

2017-05-29
13:14
merge core-8-6-branch check-in: a9965b7335 user: jan.nijtmans tags: trunk
2017-05-27
22:17
Implement proposed 0d decimal radix prefix to compliment 0x,0b,0o. check-in: d492c01a39 user: griffin tags: bsg-0d-radix-prefix
2017-05-23
22:36
TIP#457: Update named group ending Leaf check-in: be60dadb16 user: mlafon tags: tip-457
22:27
merge trunk check-in: 340fa5f4a8 user: mlafon tags: tip-457
16:00
[19a8c9399d] Plug mem leak in TIP 459 machinery. check-in: f1c99ff14c user: dgp tags: trunk
14:48
Update internal tables to Unicode 10.0. Still in Beta, but to be released soon. check-in: 090498e9a8 user: jan.nijtmans tags: trunk

Changes to doc/info.n.

25
26
27
28
29
30
31






32
33
34
35
36
37
38
.TP
\fBinfo args \fIprocname\fR
.
Returns a list containing the names of the arguments to procedure
\fIprocname\fR, in order.  \fIProcname\fR must be the name of a
Tcl command procedure.
.TP






\fBinfo body \fIprocname\fR
.
Returns the body of procedure \fIprocname\fR.  \fIProcname\fR must be
the name of a Tcl command procedure.
.TP
\fBinfo class\fI subcommand class\fR ?\fIarg ...\fR
.VS 8.6







>
>
>
>
>
>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
.TP
\fBinfo args \fIprocname\fR
.
Returns a list containing the names of the arguments to procedure
\fIprocname\fR, in order.  \fIProcname\fR must be the name of a
Tcl command procedure.
.TP
\fBinfo argspec ?\fIarg ...\fR
.
Returns information about the arguments specification of Tcl-pure
procedures, lambda or class methods. The \fIsubcommand\fRs are
described in \fBARGSPEC INTROSPECTION\fR below.
.TP
\fBinfo body \fIprocname\fR
.
Returns the body of procedure \fIprocname\fR.  \fIProcname\fR must be
the name of a Tcl command procedure.
.TP
\fBinfo class\fI subcommand class\fR ?\fIarg ...\fR
.VS 8.6
394
395
396
397
398
399
400











































































401
402
403
404
405
406
407
the resulting list of variable names
has each matching namespace variable qualified with the name
of its namespace.
Note that a currently-visible variable may not yet
.QW exist
if it has not
been set (e.g. a variable declared but not set by \fBvariable\fR).











































































.SS "CLASS INTROSPECTION"
.VS 8.6
.PP
The following \fIsubcommand\fR values are supported by \fBinfo class\fR:
.VE 8.6
.TP
\fBinfo class call\fI class method\fR







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
the resulting list of variable names
has each matching namespace variable qualified with the name
of its namespace.
Note that a currently-visible variable may not yet
.QW exist
if it has not
been set (e.g. a variable declared but not set by \fBvariable\fR).
.SS "ARGSPEC INTROSPECTION"
.VS
.PP
The following \fIsubcommand\fR values are supported by \fBinfo argspec\fR:
.VE
.TP
\fBinfo argspec constructor \fIclassName \fR?\fIarg\fR?
.VS
If \fIarg\fR is specified, returns a list containing the argument
specifiers which describe the argument \fIarg\fR of the constructor
of the class \fIclassName\fR.
If \fIarg\fR is not specified, returns a list containing the argument
specifiers list of all arguments of the constructor of the
class \fIclassName\fR.
.TP
\fBinfo argspec lambda \fIlambdaTerm \fR?\fIarg\fR?
.VS
If \fIarg\fR is specified, returns a list containing the argument
specifiers which describe the argument \fIarg\fR of the lambda
\fIlambdaTerm\fR.
If \fIarg\fR is not specified, returns a list containing the argument
specifiers list of all arguments of the lambda \fIlambdaTerm\fR.
.TP
\fBinfo argspec method \fIclassName methodName \fR?\fIarg\fR?
.VS
If \fIarg\fR is specified, returns a list containing the argument
specifiers which describe the argument \fIarg\fR of the method
\fImethodName\fR of the class \fIclassName\fR.
If \fIarg\fR is not specified, returns a list containing the argument
specifiers list of all arguments of the method \fImethodName\fR
of the class \fIclassName\fR.
.TP
\fBinfo argspec objmethod \fIobject methodName \fR?\fIarg\fR?
.VS
If \fIarg\fR is specified, returns a list containing the argument
specifiers which describe the argument \fIarg\fR of the method
\fImethodName\fR of the object \fIobject\fR.
If \fIarg\fR is not specified, returns a list containing the argument
specifiers list of all arguments of the method \fImethodName\fR
of the object \fIobject\fR.
.TP
\fBinfo argspec proc \fIprocName \fR?\fIarg\fR?
.VS
If \fIarg\fR is specified, returns a list containing the argument
specifiers which describe the argument \fIarg\fR of the procedure
\fIprocName\fR.
If \fIarg\fR is not specified, returns a list containing the argument
specifiers list of all arguments of the procedure \fIprocName\fR.
.TP
\fBinfo argspec specifiers
.VS
Returns a list containing the argument specifiers supported by the
current interpreter.
.PP
Note that the returned argument specifiers in all subcommands, except
\fIspecifiers\fR, may not be exactly the
same as the ones used during the procedure creation, but they are
strictly equivalents.
.VE
.PP
.CS
proc p {{a -name A -name arg} {b -required 1 -default def}} {}
info argspec proc p a
                     \fI\(-> returns {a -name {A arg}}\fR

info argspec proc p
                     \fI\(-> returns {{a -name {A arg}} b}\fR
.CE
.PP
In the example above, \fI-name A -name arg\fR and \fI-name {A arg}\fR
are equivalent specifiers. Using \fI-required 1\fR and a default value
on a non-keyword argument is equivalent to using no specifiers, as the
default value is ignored when \fI-required 1\fR is used and will also
force the argument to be specified.
.VE
.SS "CLASS INTROSPECTION"
.VS 8.6
.PP
The following \fIsubcommand\fR values are supported by \fBinfo class\fR:
.VE 8.6
.TP
\fBinfo class call\fI class method\fR

Changes to doc/proc.n.

26
27
28
29
30
31
32

33
34
35




36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55




56
57
58
59
60
61
62
63
64
65
66
67
68
69
70

71
72
73
74
75
76
77
78
79
80
81
82
83
84

















































































85
86
87
88
89
90
91
and the new procedure is created in the current namespace.
If \fIname\fR includes any namespace qualifiers,
the procedure is created in the specified namespace.
\fIArgs\fR specifies the formal arguments to the
procedure.  It consists of a list, possibly empty, each of whose
elements specifies
one argument.  Each argument specifier is also a list with either

one or two fields.  If there is only a single field in the specifier
then it is the name of the argument; if there are two fields, then
the first is the argument name and the second is its default value.




Arguments with default values that are followed by non-defaulted
arguments become required arguments; enough actual arguments must be
supplied to allow all arguments up to and including the last required
formal argument.
.PP
When \fIname\fR is invoked a local variable
will be created for each of the formal arguments to the procedure; its
value will be the value of corresponding argument in the invoking command
or the argument's default value.
Actual arguments are assigned to formal arguments strictly in order.
Arguments with default values need not be
specified in a procedure invocation.  However, there must be enough
actual arguments for all the
formal arguments that do not have defaults, and there must not be any extra
actual arguments.
Arguments with default values that are followed by non-defaulted
arguments become de-facto required arguments, though this may change
in a future version of Tcl; portable code should ensure that all
optional arguments come after all required arguments.
.PP




There is one special case to permit procedures with
variable numbers of arguments.  If the last formal argument has the name
.QW \fBargs\fR ,
then a call to the procedure may contain more actual arguments
than the procedure has formal arguments.  In this case, all of the actual arguments
starting at the one that would be assigned to \fBargs\fR are combined into
a list (as if the \fBlist\fR command had been used); this combined value
is assigned to the local variable \fBargs\fR.
.PP
When \fIbody\fR is being executed, variable names normally refer to
local variables, which are created automatically when referenced and
deleted when the procedure returns.  One local variable is automatically
created for each of the procedure's arguments.
Other variables can only be accessed by invoking one of the \fBglobal\fR,
\fBvariable\fR, \fBupvar\fR or \fBnamespace upvar\fR commands.

The current namespace when \fIbody\fR is executed will be the
namespace that the procedure's name exists in, which will be the
namespace that it was created in unless it has been changed with
\fBrename\fR.
'\" We may change this! It makes [variable] unstable when renamed and is
'\" frankly pretty crazy, but doing it right is harder than it looks.
.PP
The \fBproc\fR command returns an empty string.  When a procedure is
invoked, the procedure's return value is the value specified in a
\fBreturn\fR command.  If the procedure does not execute an explicit
\fBreturn\fR, then its return value is the value of the last command
executed in the procedure's body.
If an error occurs while executing the procedure
body, then the procedure-as-a-whole will return that same error.

















































































.SH EXAMPLES
.PP
This is a procedure that takes two arguments and prints both their sum
and their product. It also returns the string
.QW OK
to the caller as an explicit result.
.PP







>
|

|
>
>
>
>









|










>
>
>
>
|













|
>














>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
and the new procedure is created in the current namespace.
If \fIname\fR includes any namespace qualifiers,
the procedure is created in the specified namespace.
\fIArgs\fR specifies the formal arguments to the
procedure.  It consists of a list, possibly empty, each of whose
elements specifies
one argument.  Each argument specifier is also a list with either
one, two fields or an odd number of fields.
If there is only a single field in the specifier
then it is the name of the argument; if there are two fields, then
the first is the argument name and the second is its default value;
if there are an odd number of fields, then the first is the argument
name and the remaining fields are extended argument specifiers.
See \fBEXTENDED ARGUMENT SPECIFIERS\fR below for details on allowed
specifiers.
Arguments with default values that are followed by non-defaulted
arguments become required arguments; enough actual arguments must be
supplied to allow all arguments up to and including the last required
formal argument.
.PP
When \fIname\fR is invoked a local variable
will be created for each of the formal arguments to the procedure; its
value will be the value of corresponding argument in the invoking command
or the argument's default value.
Actual positional arguments are assigned to formal arguments strictly in order.
Arguments with default values need not be
specified in a procedure invocation.  However, there must be enough
actual arguments for all the
formal arguments that do not have defaults, and there must not be any extra
actual arguments.
Arguments with default values that are followed by non-defaulted
arguments become de-facto required arguments, though this may change
in a future version of Tcl; portable code should ensure that all
optional arguments come after all required arguments.
.PP
Named arguments, defined with the \fB-name\fR or \fB-switch\fR extended
argument specifiers have a specific handling detailed in
\fBNAMED ARGUMENTS HANDLING\fR below.
.PP
There is an other special case to permit procedures with
variable numbers of arguments.  If the last formal argument has the name
.QW \fBargs\fR ,
then a call to the procedure may contain more actual arguments
than the procedure has formal arguments.  In this case, all of the actual arguments
starting at the one that would be assigned to \fBargs\fR are combined into
a list (as if the \fBlist\fR command had been used); this combined value
is assigned to the local variable \fBargs\fR.
.PP
When \fIbody\fR is being executed, variable names normally refer to
local variables, which are created automatically when referenced and
deleted when the procedure returns.  One local variable is automatically
created for each of the procedure's arguments.
Other variables can only be accessed by invoking one of the \fBglobal\fR,
\fBvariable\fR, \fBupvar\fR or \fBnamespace upvar\fR commands or by
using the \fB-upvar 1\fR extended argument specifier.
The current namespace when \fIbody\fR is executed will be the
namespace that the procedure's name exists in, which will be the
namespace that it was created in unless it has been changed with
\fBrename\fR.
'\" We may change this! It makes [variable] unstable when renamed and is
'\" frankly pretty crazy, but doing it right is harder than it looks.
.PP
The \fBproc\fR command returns an empty string.  When a procedure is
invoked, the procedure's return value is the value specified in a
\fBreturn\fR command.  If the procedure does not execute an explicit
\fBreturn\fR, then its return value is the value of the last command
executed in the procedure's body.
If an error occurs while executing the procedure
body, then the procedure-as-a-whole will return that same error.
.SS "EXTENDED ARGUMENT SPECIFIERS"
.VS
.PP
The following extended specifiers can be used to define an argument:
.VE
.TP
\fB-default \fIvalue\fR
.VS
Set \fIvalue\fR as the default value for the argument. It is ignored
if \fB-required 1\fR is also used.
.TP
\fB-name \fInames\fR
.VS
Defines the argument to be a named argument. \fINames\fR can be string
or a list of strings, each string being added as a potential name for
this argument. If \fB-name\fR is used several times for the same argument,
all strings are added as potential names. See \fBNAMED ARGUMENTS HANDLING\fR
below for details on named arguments.
.TP
\fB-required \fIbool\fR
.VS
Defines that the argument is required or not to be set by the caller.
If \fIbool\fR is true, it is required to be set and any default value
is ignored. It is the default handling for non-named argument
without a default value.
If \fIbool\fR is false, it is not required to be set and the related
argument will be left unset if there is no default value. It is the
default handling for named argument.
.TP
\fB-switch \fIswitches\fR
.VS
Defines the argument to be a flag/switch named argument. \fISwitches\fR
is a list of potential switches for this argument.
Each switch is defined either as a
single string or as a list of a string and a value. When the switch is
defined as a single string, the string is used as both the switch name
and the switch value.
When the switch is defined as a list of two entries, the first one is
the switch name and the second one is the switch value. If \fB-switch\fR
is used several times for the same argument, all switches are added
as potential switches. On call-site, the related switch value will be
set on the argument if one of the switch names is used.
See \fBNAMED ARGUMENTS HANDLING\fR below for details on named arguments.
.TP
\fB-upvar \fIlevel\fR
.VS
When defined, this will cause the related variable, rather than taking
the parameter value, to become an alias to the variable in the frame
at level \fIlevel\fR corresponding to the parameter value. \fILevel\fR
may have any of the forms permitted for the \fBupvar\fR command.
This specifier is incompatible with the \fB-switch\fR specifier.
.VE
.SS "NAMED ARGUMENTS HANDLING"
.VS
.PP
Named argument are arguments defined using the \fB-name\fR or \fB-switch\fR
extended argument specifiers. They have a special handling on call-site.
All contiguous named arguments, called named group, are handled together,
they are not required to be declared in order, can be omited or declared
multiple times.
.PP
The handling of a named group is started when previous formal arguments have
been assigned.
Each named parameter uses a string starting with a dash character, followed
by the name of the related argument. If it is not a switch argument, it is
followed by the value to assign to the formal argument.
.PP
The handling of
a named group is ended if the next parameter does not start with a dash or is
the special \fB--\fR keyword. In the case where the arguments after the named
group are all non-optional positional arguments and do not end with \fBargs\fR,
the handling of the named group will also be ended when the number of remaining
parameters will be equal to the number of the remaining positional arguments.
When the handling of named argument has been ended, remaining parameters,
except the \fB--\fR keyword if used, are then assigned to
following positional arguments using the default handling.
.PP
It is recommended to explicitely use the \fB--\fR keyword if the next parameter
following the named group is a variable which may start with a dash, or if it
is an object which can be expensive to stringify.
.VE
.SH EXAMPLES
.PP
This is a procedure that takes two arguments and prints both their sum
and their product. It also returns the string
.QW OK
to the caller as an explicit result.
.PP
105
106
107
108
109
110
111
112
113
114

115
116
117
118
119
120























121
122
123
124
125
126
127
128
\fBproc\fR printArguments args {
    foreach arg $args {
        puts $arg
    }
}
.CE
.PP
This procedure is a bit like the \fBincr\fR command, except it
multiplies the contents of the named variable by the value, which
defaults to \fB2\fR:

.PP
.CS
\fBproc\fR mult {varName {multiplier 2}} {
    upvar 1 $varName var
    set var [expr {$var * $multiplier}]
}























.CE
.SH "SEE ALSO"
info(n), unknown(n)
.SH KEYWORDS
argument, procedure
'\" Local Variables:
'\" mode: nroff
'\" End:







|
|
|
>






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>








196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
\fBproc\fR printArguments args {
    foreach arg $args {
        puts $arg
    }
}
.CE
.PP
These procedures are a bit like the \fBincr\fR command, except they
multiply the contents of the named variable by the value, which
defaults to \fB2\fR. The second procedure uses extended argument
specifiers and especially the \fB-upvar\fR specifier:
.PP
.CS
\fBproc\fR mult {varName {multiplier 2}} {
    upvar 1 $varName var
    set var [expr {$var * $multiplier}]
}

\fBproc\fR mult {{var -upvar 1} {multiplier -default 2}} {
  set var [expr {$var * $multiplier}]
}
.CE
.PP
This is a procedure that uses named argument for the first two
arguments. \fILevel\fR defaults to \fB1\fR and can be defined
using either \fB-level \fIlevelValue\fR or one of the switches
\fB-debug\fR (\fB0\fR) or \fB-error\fR (\fB3\fR). \fITime\fR
can be defined using either \fB-time \fItimeValue\fR or
\fB-timestamp \fItimeValue\fR. If none is used, \fItime\fR will
be left unset in the procedure.
.PP
.CS
\fBproc\fR log {
    {level -name level -switch {{debug 0} {error 3}} -default 1}
    {time -name {time timestamp}}
    {message} } {
  if {![info exists time]} {set time [clock seconds]}
  array set levels {0 DEBUG 1 INFO 2 WARN 3 ERROR}
  puts "[clock format $time] : $levels($level) : $message"
}
.CE
.SH "SEE ALSO"
info(n), unknown(n)
.SH KEYWORDS
argument, procedure
'\" Local Variables:
'\" mode: nroff
'\" End:

Changes to generic/tclCmdIL.c.

15
16
17
18
19
20
21


22
23
24
25
26
27
28
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclInt.h"
#include "tclRegexp.h"



/*
 * During execution of the "lsort" command, structures of the following type
 * are used to arrange the objects being sorted into a collection of linked
 * lists.
 */








>
>







15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * See the file "license.terms" for information on usage and redistribution of
 * this file, and for a DISCLAIMER OF ALL WARRANTIES.
 */

#include "tclInt.h"
#include "tclRegexp.h"
#include "tclCompile.h"
#include "tclOOInt.h"

/*
 * During execution of the "lsort" command, structures of the following type
 * are used to arrange the objects being sorted into a collection of linked
 * lists.
 */

104
105
106
107
108
109
110


111
112
113
114
115
116
117
 * Forward declarations for procedures defined in this file:
 */

static int		DictionaryCompare(const char *left, const char *right);
static Tcl_NRPostProc	IfConditionCallback;
static int		InfoArgsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);


static int		InfoBodyCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCmdCountCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCommandsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCompleteCmd(ClientData dummy, Tcl_Interp *interp,







>
>







106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
 * Forward declarations for procedures defined in this file:
 */

static int		DictionaryCompare(const char *left, const char *right);
static Tcl_NRPostProc	IfConditionCallback;
static int		InfoArgsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoArgSpecCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoBodyCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCmdCountCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCommandsCmd(ClientData dummy, Tcl_Interp *interp,
			    int objc, Tcl_Obj *const objv[]);
static int		InfoCompleteCmd(ClientData dummy, Tcl_Interp *interp,
157
158
159
160
161
162
163

164
165
166
167
168
169
170
/*
 * Array of values describing how to implement each standard subcommand of the
 * "info" command.
 */

static const EnsembleImplMap defaultInfoMap[] = {
    {"args",		   InfoArgsCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},

    {"body",		   InfoBodyCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"cmdcount",	   InfoCmdCountCmd,	    TclCompileBasic0ArgCmd, NULL, NULL, 0},
    {"commands",	   InfoCommandsCmd,	    TclCompileInfoCommandsCmd, NULL, NULL, 0},
    {"complete",	   InfoCompleteCmd,	    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"coroutine",	   TclInfoCoroutineCmd,     TclCompileInfoCoroutineCmd, NULL, NULL, 0},
    {"default",		   InfoDefaultCmd,	    TclCompileBasic3ArgCmd, NULL, NULL, 0},
    {"errorstack",	   InfoErrorStackCmd,	    TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},







>







161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
 * Array of values describing how to implement each standard subcommand of the
 * "info" command.
 */

static const EnsembleImplMap defaultInfoMap[] = {
    {"args",		   InfoArgsCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"argspec",		   InfoArgSpecCmd,	    TclCompileBasic2ArgCmd, NULL, NULL, 0},
    {"body",		   InfoBodyCmd,		    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"cmdcount",	   InfoCmdCountCmd,	    TclCompileBasic0ArgCmd, NULL, NULL, 0},
    {"commands",	   InfoCommandsCmd,	    TclCompileInfoCommandsCmd, NULL, NULL, 0},
    {"complete",	   InfoCompleteCmd,	    TclCompileBasic1ArgCmd, NULL, NULL, 0},
    {"coroutine",	   TclInfoCoroutineCmd,     TclCompileInfoCoroutineCmd, NULL, NULL, 0},
    {"default",		   InfoDefaultCmd,	    TclCompileBasic3ArgCmd, NULL, NULL, 0},
    {"errorstack",	   InfoErrorStackCmd,	    TclCompileBasic0Or1ArgCmd, NULL, NULL, 0},
510
511
512
513
514
515
516













































































































































































































































































517
518
519
520
521
522
523
	    Tcl_ListObjAppendElement(interp, listObjPtr,
		    Tcl_NewStringObj(localPtr->name, -1));
	}
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
}














































































































































































































































































/*
 *----------------------------------------------------------------------
 *
 * InfoBodyCmd --
 *
 *	Called to implement the "info body" command that returns the body for







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
	    Tcl_ListObjAppendElement(interp, listObjPtr,
		    Tcl_NewStringObj(localPtr->name, -1));
	}
    }
    Tcl_SetObjResult(interp, listObjPtr);
    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * InfoArgSpecCmd --
 *
 *	Called to implement the "info argspec" command that returns the
 *	extendend argument specification for a procedure.
 *	Handles the following syntaxes:
 *
 *	    info argspec proc procName ?arg?
 *	    info argspec lambda lambdaTerm ?arg?
 *	    info argspec constructor className ?arg?
 *	    info argspec method className methodName ?arg?
 *	    info argspec objmethod object methodName ?arg?
 *	    info argspec specifiers
 *
 * Results:
 *	Returns TCL_OK if successful and TCL_ERROR if there is an error.
 *
 * Side effects:
 *	Returns a result in the interpreter's result object. If there is an
 *	error, the result is an error message.
 *
 *----------------------------------------------------------------------
 */

static int
InfoArgSpecCmd(
    ClientData dummy,		/* Not used. */
    Tcl_Interp *interp,		/* Current interpreter. */
    int objc,			/* Number of arguments. */
    Tcl_Obj *const objv[])	/* Argument objects. */
{
    static const char *const types[] = {
	"proc", "lambda", "constructor", "method", "objmethod",
	"specifiers", NULL,
    };
    static const char *const specifiers[] = {
	/* supported specifiers, must be ordered */
	"-default", "-name", "-required", "-switch", "-upvar", NULL
    };
    enum Types {
	ARGSPEC_PROC, ARGSPEC_LAMBDA, ARGSPEC_CONSTRUCTOR, ARGSPEC_METHOD,
	ARGSPEC_OBJMETHOD, ARGSPEC_SPECIFIERS
    };
    register Interp *iPtr = (Interp *) interp;
    const char *procName = NULL, *argName = NULL;
    Proc *procPtr = NULL;
    CompiledLocal *localPtr;
    Tcl_Obj *listObjPtr;
    Object *oPtr;
    Method *methodPtr;
    Tcl_HashEntry *hPtr;
    int idx, result;

    if (objc < 2) {
	Tcl_WrongNumArgs(interp, 1, objv, "type ...");
	return TCL_ERROR;
    }

    if (Tcl_GetIndexFromObj(interp, objv[1], types, "type", 0, &idx)!=TCL_OK) {
	return TCL_ERROR;
    }

    switch ((enum Types) idx) {
    case ARGSPEC_PROC:
	if (objc < 3 || objc > 4) {
	    Tcl_WrongNumArgs(interp, 1, objv, "proc procname ?arg?");
	    return TCL_ERROR;
	}

	procName = TclGetString(objv[2]);
	procPtr = TclFindProc(iPtr, procName);
	if (procPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" isn't a procedure", procName));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "PROCEDURE", procName,
		NULL);
	    return TCL_ERROR;
	}

	argName = (objc == 4) ? TclGetString(objv[3]) : NULL;
	break;

    case ARGSPEC_LAMBDA:
	if (objc < 3 || objc > 4) {
	    Tcl_WrongNumArgs(interp, 1, objv, "lambda lambdaTerm ?arg?");
	    return TCL_ERROR;
	}

	if (objv[2]->typePtr == &tclLambdaType) {
	    procPtr = objv[2]->internalRep.twoPtrValue.ptr1;
	}
	if (procPtr == NULL || procPtr->iPtr != (Interp *) interp) {
	    result = tclLambdaType.setFromAnyProc(interp, objv[2]);
	    if (result != TCL_OK) {
		return result;
	    }
	    procPtr = objv[2]->internalRep.twoPtrValue.ptr1;
	}

	procName = "lambdaTerm";
	argName = (objc == 4) ? TclGetString(objv[3]) : NULL;
	break;

    case ARGSPEC_CONSTRUCTOR:
	if (objc < 3 || objc > 4) {
	    Tcl_WrongNumArgs(interp, 1, objv, "constructor className ?arg?");
	    return TCL_ERROR;
	}

	oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
	if (oPtr == NULL) {
	    return TCL_ERROR;
	}
	if (oPtr->classPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" is not a class", TclGetString(objv[2])));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
		TclGetString(objv[2]), NULL);
	    return TCL_ERROR;
	}

	methodPtr = oPtr->classPtr->constructorPtr;
	if (methodPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" has no defined constructor",
		TclGetString(objv[2])));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "ARGSPEC",
		"CONSRUCTOR", NULL);
	    return TCL_ERROR;
	}
	procPtr = TclOOGetProcFromMethod(methodPtr);
	if (procPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		"body not available for this kind of constructor", -1));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "ARGSPEC",
		"METHODTYPE", NULL);
	    return TCL_ERROR;
	}

	procName = "constructor";
	argName = (objc == 4) ? TclGetString(objv[3]) : NULL;
	break;

    case ARGSPEC_METHOD:
	if (objc < 4 || objc > 5) {
	    Tcl_WrongNumArgs(interp, 1, objv,
		"method className methodName ?arg?");
	    return TCL_ERROR;
	}

	oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
	if (oPtr == NULL) {
	    return TCL_ERROR;
	}
	if (oPtr->classPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"\"%s\" is not a class", TclGetString(objv[2])));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "CLASS",
		TclGetString(objv[2]), NULL);
	    return TCL_ERROR;
	}

	procName = TclGetString(objv[3]);
	hPtr = Tcl_FindHashEntry(&oPtr->classPtr->classMethods,
	    (char *) objv[3]);
	if (hPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"unknown method \"%s\"", procName));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD",
		TclGetString(objv[3]), NULL);
	    return TCL_ERROR;
	}
	procPtr = TclOOGetProcFromMethod(Tcl_GetHashValue(hPtr));
	if (procPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		"body not available for this kind of method", -1));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "ARGSPEC",
		"METHODTYPE", NULL);
	    return TCL_ERROR;
	}

	argName = (objc == 5) ? TclGetString(objv[4]) : NULL;
	break;

    case ARGSPEC_OBJMETHOD:
	if (objc < 4 || objc > 5) {
	    Tcl_WrongNumArgs(interp, 1, objv,
		"objmethod object methodName ?arg?");
	    return TCL_ERROR;
	}

	oPtr = (Object *) Tcl_GetObjectFromObj(interp, objv[2]);
	if (oPtr == NULL) {
	    return TCL_ERROR;
	}
	if (oPtr->methodsPtr == NULL) {
	    hPtr = NULL;
	} else {
	    hPtr = Tcl_FindHashEntry(oPtr->methodsPtr, (char *) objv[3]);
	}

	procName = TclGetString(objv[3]);
	if (hPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"unknown method \"%s\"", procName));
	    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "METHOD",
		TclGetString(objv[3]), NULL);
	    return TCL_ERROR;
	}

	procPtr = TclOOGetProcFromMethod(Tcl_GetHashValue(hPtr));
	if (procPtr == NULL) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		"body not available for this kind of method", -1));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "ARGSPEC",
		"METHODTYPE", NULL);
	    return TCL_ERROR;
	}

	argName = (objc == 5) ? TclGetString(objv[4]) : NULL;
	break;

    case ARGSPEC_SPECIFIERS:
	listObjPtr = Tcl_NewListObj(0, NULL);
	for (idx = 0; specifiers[idx] != NULL; idx++) {
	    Tcl_ListObjAppendElement(interp, listObjPtr,
		Tcl_NewStringObj(specifiers[idx], -1));
	}
	Tcl_SetObjResult(interp, listObjPtr);
	return TCL_OK;
	break;
    }

    if (argName == NULL) {
	/* info argspec ... procName */
	listObjPtr = Tcl_NewListObj(0, NULL);
	for (localPtr = procPtr->firstLocalPtr;  localPtr != NULL;
		localPtr = localPtr->nextPtr) {
	    if (TclIsVarArgument(localPtr)) {
		Tcl_ListObjAppendElement(interp, listObjPtr,
		    TclProcGetArgSpec(interp, localPtr,
		    TCL_GETARGSPEC_WITH_NAME));
	    }
	}

	Tcl_SetObjResult(interp, listObjPtr);
	return TCL_OK;
    }

    /* info argspec ... procName arg */
    for (localPtr = procPtr->firstLocalPtr;  localPtr != NULL;
	    localPtr = localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)
		&& (strcmp(argName, localPtr->name) == 0)) {
	    listObjPtr = TclProcGetArgSpec(interp, localPtr, 0);
	    Tcl_SetObjResult(interp, listObjPtr);
	    return TCL_OK;
	}
    }

    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	"%s \"%s\" doesn't have an argument \"%s\"",
	Tcl_GetString(objv[1]), procName, argName));
    Tcl_SetErrorCode(interp, "TCL", "LOOKUP", "ARGUMENT", argName, NULL);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * InfoBodyCmd --
 *
 *	Called to implement the "info body" command that returns the body for

Changes to generic/tclCompile.c.

3028
3029
3030
3031
3032
3033
3034

3035
3036
3037
3038
3039
3040
3041
	localPtr->frameIndex = localVar;
	localPtr->flags = 0;
	if (name == NULL) {
	    localPtr->flags |= VAR_TEMPORARY;
	}
	localPtr->defValuePtr = NULL;
	localPtr->resolveInfo = NULL;


	if (name != NULL) {
	    memcpy(localPtr->name, name, (size_t) nameBytes);
	}
	localPtr->name[nameBytes] = '\0';
	procPtr->numCompiledLocals++;
    }







>







3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
	localPtr->frameIndex = localVar;
	localPtr->flags = 0;
	if (name == NULL) {
	    localPtr->flags |= VAR_TEMPORARY;
	}
	localPtr->defValuePtr = NULL;
	localPtr->resolveInfo = NULL;
	localPtr->argSpecPtr = NULL;

	if (name != NULL) {
	    memcpy(localPtr->name, name, (size_t) nameBytes);
	}
	localPtr->name[nameBytes] = '\0';
	procPtr->numCompiledLocals++;
    }

Changes to generic/tclInt.h.

694
695
696
697
698
699
700







701
702
703
704
705
706
707
 * VAR_TEMPORARY -		1 if the local variable is an anonymous
 *				temporary variable. Temporaries have a NULL
 *				name.
 * VAR_RESOLVED -		1 if name resolution has been done for this
 *				variable.
 * VAR_IS_ARGS			1 if this variable is the last argument and is
 *				named "args".







 */

/*
 * FLAGS RENUMBERED: everything breaks already, make things simpler.
 *
 * IMPORTANT: skip the values 0x10, 0x20, 0x40, 0x800 corresponding to
 * TCL_TRACE_(READS/WRITES/UNSETS/ARRAY): makes code simpler in tclTrace.c







>
>
>
>
>
>
>







694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
 * VAR_TEMPORARY -		1 if the local variable is an anonymous
 *				temporary variable. Temporaries have a NULL
 *				name.
 * VAR_RESOLVED -		1 if name resolution has been done for this
 *				variable.
 * VAR_IS_ARGS			1 if this variable is the last argument and is
 *				named "args".
 * VAR_NAMED_GROUP		1 means that this argument is part of a named
 *				group defined using an extended argument
 *				specification.
 * VAR_ARG_UPVAR		1 means that this argument has been defined
 *				using the -upvar extended arg specification.
 * VAR_ARG_OPTIONAL		1 means that this argument is not required to
 *				be specified on call-site.
 */

/*
 * FLAGS RENUMBERED: everything breaks already, make things simpler.
 *
 * IMPORTANT: skip the values 0x10, 0x20, 0x40, 0x800 corresponding to
 * TCL_TRACE_(READS/WRITES/UNSETS/ARRAY): makes code simpler in tclTrace.c
735
736
737
738
739
740
741



742
743
744
745
746
747
748
	(VAR_TRACED_READ|VAR_TRACED_WRITE|VAR_TRACED_ARRAY|VAR_TRACED_UNSET)

/* Special handling on initialisation (only CompiledLocal). */
#define VAR_ARGUMENT		0x100	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_TEMPORARY		0x200	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_IS_ARGS		0x400
#define VAR_RESOLVED		0x8000




/*
 * Macros to ensure that various flag bits are set properly for variables.
 * The ANSI C "prototypes" for these macros are:
 *
 * MODULE_SCOPE void	TclSetVarScalar(Var *varPtr);
 * MODULE_SCOPE void	TclSetVarArray(Var *varPtr);







>
>
>







742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
	(VAR_TRACED_READ|VAR_TRACED_WRITE|VAR_TRACED_ARRAY|VAR_TRACED_UNSET)

/* Special handling on initialisation (only CompiledLocal). */
#define VAR_ARGUMENT		0x100	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_TEMPORARY		0x200	/* KEEP OLD VALUE! See tclProc.c */
#define VAR_IS_ARGS		0x400
#define VAR_RESOLVED		0x8000
#define VAR_NAMED_GROUP		0x10000
#define VAR_ARG_UPVAR		0x20000
#define VAR_ARG_OPTIONAL	0x40000

/*
 * Macros to ensure that various flag bits are set properly for variables.
 * The ANSI C "prototypes" for these macros are:
 *
 * MODULE_SCOPE void	TclSetVarScalar(Var *varPtr);
 * MODULE_SCOPE void	TclSetVarArray(Var *varPtr);
841
842
843
844
845
846
847



848
849
850
851
852
853
854

#define TclIsVarInHash(varPtr) \
    ((varPtr)->flags & VAR_IN_HASHTABLE)

#define TclIsVarDeadHash(varPtr) \
    ((varPtr)->flags & VAR_DEAD_HASH)




#define TclGetVarNsPtr(varPtr) \
    (TclIsVarInHash(varPtr) \
	? ((TclVarHashTable *) ((((VarInHash *) (varPtr))->entry.tablePtr)))->nsPtr \
	: NULL)

#define VarHashRefCount(varPtr) \
    ((VarInHash *) (varPtr))->refCount







>
>
>







851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867

#define TclIsVarInHash(varPtr) \
    ((varPtr)->flags & VAR_IN_HASHTABLE)

#define TclIsVarDeadHash(varPtr) \
    ((varPtr)->flags & VAR_DEAD_HASH)

#define TclIsVarWithExtArgs(varPtr) \
    ((varPtr)->flags & (VAR_NAMED_GROUP|VAR_ARG_UPVAR|VAR_ARG_OPTIONAL))

#define TclGetVarNsPtr(varPtr) \
    (TclIsVarInHash(varPtr) \
	? ((TclVarHashTable *) ((((VarInHash *) (varPtr))->entry.tablePtr)))->nsPtr \
	: NULL)

#define VarHashRefCount(varPtr) \
    ((VarInHash *) (varPtr))->refCount
889
890
891
892
893
894
895

896
897
898



















































899
900
901
902
903
904
905
 * tclProc.c, tclCompile.c, and tclExecute.c.
 *----------------------------------------------------------------
 */

/*
 * Forward declaration to prevent an error when the forward reference to
 * Command is encountered in the Proc and ImportRef types declared below.

 */

struct Command;




















































/*
 * The variable-length structure below describes a local variable of a
 * procedure that was recognized by the compiler. These variables have a name,
 * an element in the array of compiler-assigned local variables in the
 * procedure's call frame, and various other items of information. If the
 * local variable is a formal argument, it may also have a default value. The







>



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
 * tclProc.c, tclCompile.c, and tclExecute.c.
 *----------------------------------------------------------------
 */

/*
 * Forward declaration to prevent an error when the forward reference to
 * Command is encountered in the Proc and ImportRef types declared below.
 * Same for CompiledLocal in the NamedGroupEntry type.
 */

struct Command;
struct CompiledLocal;

/*
 * The variable-length structure below describes an entry in a group of
 * named parameter defined together.
 */

typedef struct NamedGroupEntry {
    struct NamedGroupEntry *nextPtr;
				/* Next entry in the named-group, or NULL if
				 * this is the last entry. */
    int nameLength;		/* The number of characters in entry's name */
    int localIndex;		/* Index of the related local in the array of
				 * compiler-assigned variables in the procedure
				 * call frame. */
    Tcl_Obj *valuePtr;		/* Pointer to the default value of the named
				 * parameter, used with the -switch argument
				 * specifier. NULL if it is related to a named
				 * parameter created with the -name argument
				 * specifier. */
    struct CompiledLocal *localPtr;
				/* Pointer to the related local in the linked
				 * list of locals for the procedure. */
    char name[1];		/* Name of the entry starts here. The actual
				 * size of this field will be large enough to
				 * hold the name. MUST BE THE LAST FIELD IN
				 * THE STRUCTURE! */
} NamedGroupEntry;

/*
 * The structure below describes an extended argument specification applied
 * on a proc argument (see TIP#457).
 */

typedef struct ExtendedArgSpec {
    struct NamedGroupEntry *firstNamedEntryPtr;
				/* Pointer to the first named parameter entry
				 * defined on this proc argument. */
    Tcl_HashTable *namedHashTable;
				/* Pointer to the hash table created for a
				 * fast lookup of named entry. The pointer is
				 * only set on the first local of a named
				 * group. */
    int remainAfterNamedGroup;	/* If the number of arguments after a named
				 * group is fixed and non-optional, this will
				 * contain that number. Otherwise, will be set
				 * to -1. This is only set on the first local
				 * of a named group. */
    Tcl_Obj *upvarLevelPtr;	/* Pointer to the level value specified using
				 * the -upvar specifier. */
} ExtendedArgSpec;

/*
 * The variable-length structure below describes a local variable of a
 * procedure that was recognized by the compiler. These variables have a name,
 * an element in the array of compiler-assigned local variables in the
 * procedure's call frame, and various other items of information. If the
 * local variable is a formal argument, it may also have a default value. The
919
920
921
922
923
924
925

926
927
928
929
930
931
932
933
934
935
936



937
938
939
940
941
942
943
				 * variable's name. Used to speed up variable
				 * lookups. */
    int frameIndex;		/* Index in the array of compiler-assigned
				 * variables in the procedure call frame. */
    int flags;			/* Flag bits for the local variable. Same as
				 * the flags for the Var structure above,
				 * although only VAR_ARGUMENT, VAR_TEMPORARY,

				 * and VAR_RESOLVED make sense. */
    Tcl_Obj *defValuePtr;	/* Pointer to the default value of an
				 * argument, if any. NULL if not an argument
				 * or, if an argument, no default value. */
    Tcl_ResolvedVarInfo *resolveInfo;
				/* Customized variable resolution info
				 * supplied by the Tcl_ResolveCompiledVarProc
				 * associated with a namespace. Each variable
				 * is marked by a unique ClientData tag during
				 * compilation, and that same tag is used to
				 * find the variable at runtime. */



    char name[1];		/* Name of the local variable starts here. If
				 * the name is NULL, this will just be '\0'.
				 * The actual size of this field will be large
				 * enough to hold the name. MUST BE THE LAST
				 * FIELD IN THE STRUCTURE! */
} CompiledLocal;








>
|










>
>
>







984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
				 * variable's name. Used to speed up variable
				 * lookups. */
    int frameIndex;		/* Index in the array of compiler-assigned
				 * variables in the procedure call frame. */
    int flags;			/* Flag bits for the local variable. Same as
				 * the flags for the Var structure above,
				 * although only VAR_ARGUMENT, VAR_TEMPORARY,
				 * VAR_RESOLVED, VAR_NAMED_GROUP and VAR_ARG_*
				 * make sense. */
    Tcl_Obj *defValuePtr;	/* Pointer to the default value of an
				 * argument, if any. NULL if not an argument
				 * or, if an argument, no default value. */
    Tcl_ResolvedVarInfo *resolveInfo;
				/* Customized variable resolution info
				 * supplied by the Tcl_ResolveCompiledVarProc
				 * associated with a namespace. Each variable
				 * is marked by a unique ClientData tag during
				 * compilation, and that same tag is used to
				 * find the variable at runtime. */
    ExtendedArgSpec *argSpecPtr;
				/* Extended argument specification if this is
				 * a proc argument defined using one. */
    char name[1];		/* Name of the local variable starts here. If
				 * the name is NULL, this will just be '\0'.
				 * The actual size of this field will be large
				 * enough to hold the name. MUST BE THE LAST
				 * FIELD IN THE STRUCTURE! */
} CompiledLocal;

961
962
963
964
965
966
967

968
969
970
971
972
973
974
975
976
977
978













979
980
981
982
983
984
985
				 * procedure. */
    Tcl_Obj *bodyPtr;		/* Points to the ByteCode object for
				 * procedure's body command. */
    int numArgs;		/* Number of formal parameters. */
    int numCompiledLocals;	/* Count of local variables recognized by the
				 * compiler including arguments and
				 * temporaries. */

    CompiledLocal *firstLocalPtr;
				/* Pointer to first of the procedure's
				 * compiler-allocated local variables, or NULL
				 * if none. The first numArgs entries in this
				 * list describe the procedure's formal
				 * arguments. */
    CompiledLocal *lastLocalPtr;/* Pointer to the last allocated local
				 * variable or NULL if none. This has frame
				 * index (numCompiledLocals-1). */
} Proc;














/*
 * The type of functions called to process errors found during the execution
 * of a procedure (or lambda term or ...).
 */

typedef void (ProcErrorProc)(Tcl_Interp *interp, Tcl_Obj *procNameObj);








>











>
>
>
>
>
>
>
>
>
>
>
>
>







1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
				 * procedure. */
    Tcl_Obj *bodyPtr;		/* Points to the ByteCode object for
				 * procedure's body command. */
    int numArgs;		/* Number of formal parameters. */
    int numCompiledLocals;	/* Count of local variables recognized by the
				 * compiler including arguments and
				 * temporaries. */
    int flags;			/* Various flag bits. See below. */
    CompiledLocal *firstLocalPtr;
				/* Pointer to first of the procedure's
				 * compiler-allocated local variables, or NULL
				 * if none. The first numArgs entries in this
				 * list describe the procedure's formal
				 * arguments. */
    CompiledLocal *lastLocalPtr;/* Pointer to the last allocated local
				 * variable or NULL if none. This has frame
				 * index (numCompiledLocals-1). */
} Proc;

/*
 * Flag bits for procedures.
 *
 * PROC_HAS_EXT_ARG_SPEC	1 means that the procedure has at least one
 *				argument defined using an extended argument
 *				specification.
 * PROC_HAS_NAMED_GROUP		1 means that the procedure has at least one
 *				named group.
 */

#define PROC_HAS_EXT_ARG_SPEC   0x01
#define PROC_HAS_NAMED_GROUP    0x02

/*
 * The type of functions called to process errors found during the execution
 * of a procedure (or lambda term or ...).
 */

typedef void (ProcErrorProc)(Tcl_Interp *interp, Tcl_Obj *procNameObj);

2856
2857
2858
2859
2860
2861
2862









2863
2864
2865
2866
2867
2868
2869
#define TCL_DD_CONVERSION_TYPE_MASK	0x3
				/* Mask to isolate the conversion type */
#define TCL_DD_STEELE0 			0x1
				/* 'Steele&White' after masking */
#define TCL_DD_SHORTEST0		0x0
				/* 'Shortest possible' after masking */










/*
 *----------------------------------------------------------------
 * Procedures shared among Tcl modules but not used by the outside world:
 *----------------------------------------------------------------
 */

MODULE_SCOPE void	TclAppendBytesToByteArray(Tcl_Obj *objPtr,







>
>
>
>
>
>
>
>
>







2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
#define TCL_DD_CONVERSION_TYPE_MASK	0x3
				/* Mask to isolate the conversion type */
#define TCL_DD_STEELE0 			0x1
				/* 'Steele&White' after masking */
#define TCL_DD_SHORTEST0		0x0
				/* 'Shortest possible' after masking */

/* Flags for TclProcGetArgSpec */

#define TCL_GETARGSPEC_WITH_NAME	0x01
				/* Include arg name before specification */
#define TCL_GETARGSPEC_TRY_OLDSTYLE	0x02
				/* Return argument spec using old-style
				 * specification if there is no other options
				 * set on argument */

/*
 *----------------------------------------------------------------
 * Procedures shared among Tcl modules but not used by the outside world:
 *----------------------------------------------------------------
 */

MODULE_SCOPE void	TclAppendBytesToByteArray(Tcl_Obj *objPtr,
3063
3064
3065
3066
3067
3068
3069


3070
3071
3072
3073
3074
3075
3076
			    const char *expected, const char *bytes,
			    int numBytes, const char **endPtrPtr, int flags);
MODULE_SCOPE void	TclParseInit(Tcl_Interp *interp, const char *string,
			    int numBytes, Tcl_Parse *parsePtr);
MODULE_SCOPE int	TclParseAllWhiteSpace(const char *src, int numBytes);
MODULE_SCOPE int	TclProcessReturn(Tcl_Interp *interp,
			    int code, int level, Tcl_Obj *returnOpts);


MODULE_SCOPE int	TclpObjLstat(Tcl_Obj *pathPtr, Tcl_StatBuf *buf);
MODULE_SCOPE Tcl_Obj *	TclpTempFileName(void);
MODULE_SCOPE Tcl_Obj *  TclpTempFileNameForLibrary(Tcl_Interp *interp, Tcl_Obj* pathPtr);
MODULE_SCOPE Tcl_Obj *	TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep,
			    int len);
MODULE_SCOPE int	TclpDeleteFile(const void *path);
MODULE_SCOPE void	TclpFinalizeCondition(Tcl_Condition *condPtr);







>
>







3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
			    const char *expected, const char *bytes,
			    int numBytes, const char **endPtrPtr, int flags);
MODULE_SCOPE void	TclParseInit(Tcl_Interp *interp, const char *string,
			    int numBytes, Tcl_Parse *parsePtr);
MODULE_SCOPE int	TclParseAllWhiteSpace(const char *src, int numBytes);
MODULE_SCOPE int	TclProcessReturn(Tcl_Interp *interp,
			    int code, int level, Tcl_Obj *returnOpts);
MODULE_SCOPE Tcl_Obj *	TclProcGetArgSpec(Tcl_Interp *interp,
			    CompiledLocal *argPtr, int flags);
MODULE_SCOPE int	TclpObjLstat(Tcl_Obj *pathPtr, Tcl_StatBuf *buf);
MODULE_SCOPE Tcl_Obj *	TclpTempFileName(void);
MODULE_SCOPE Tcl_Obj *  TclpTempFileNameForLibrary(Tcl_Interp *interp, Tcl_Obj* pathPtr);
MODULE_SCOPE Tcl_Obj *	TclNewFSPathObj(Tcl_Obj *dirPtr, const char *addStrRep,
			    int len);
MODULE_SCOPE int	TclpDeleteFile(const void *path);
MODULE_SCOPE void	TclpFinalizeCondition(Tcl_Condition *condPtr);
3176
3177
3178
3179
3180
3181
3182



3183
3184
3185
3186
3187
3188
3189
MODULE_SCOPE int	TclSubstTokens(Tcl_Interp *interp, Tcl_Token *tokenPtr,
			    int count, int *tokensLeftPtr, int line,
			    int *clNextOuter, const char *outerScript);
MODULE_SCOPE int	TclTrimLeft(const char *bytes, int numBytes,
			    const char *trim, int numTrim);
MODULE_SCOPE int	TclTrimRight(const char *bytes, int numBytes,
			    const char *trim, int numTrim);



MODULE_SCOPE int	TclUtfCasecmp(const char *cs, const char *ct);
MODULE_SCOPE int	TclUtfCount(int ch);
MODULE_SCOPE Tcl_Obj *	TclpNativeToNormalized(ClientData clientData);
MODULE_SCOPE Tcl_Obj *	TclpFilesystemPathType(Tcl_Obj *pathPtr);
MODULE_SCOPE int	TclpDlopen(Tcl_Interp *interp, Tcl_Obj *pathPtr,
			    Tcl_LoadHandle *loadHandle,
			    Tcl_FSUnloadFileProc **unloadProcPtr, int flags);







>
>
>







3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
MODULE_SCOPE int	TclSubstTokens(Tcl_Interp *interp, Tcl_Token *tokenPtr,
			    int count, int *tokensLeftPtr, int line,
			    int *clNextOuter, const char *outerScript);
MODULE_SCOPE int	TclTrimLeft(const char *bytes, int numBytes,
			    const char *trim, int numTrim);
MODULE_SCOPE int	TclTrimRight(const char *bytes, int numBytes,
			    const char *trim, int numTrim);
MODULE_SCOPE int	TclUpvarForExtArg(Tcl_Interp *interp,
			    Tcl_Obj *frameNamePtr, Tcl_Obj *varNamePtr,
			    const char *localNameStr);
MODULE_SCOPE int	TclUtfCasecmp(const char *cs, const char *ct);
MODULE_SCOPE int	TclUtfCount(int ch);
MODULE_SCOPE Tcl_Obj *	TclpNativeToNormalized(ClientData clientData);
MODULE_SCOPE Tcl_Obj *	TclpFilesystemPathType(Tcl_Obj *pathPtr);
MODULE_SCOPE int	TclpDlopen(Tcl_Interp *interp, Tcl_Obj *pathPtr,
			    Tcl_LoadHandle *loadHandle,
			    Tcl_FSUnloadFileProc **unloadProcPtr, int flags);

Changes to generic/tclOO.c.

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
 */

static const char *clonedBody =
"foreach p [info procs [info object namespace $originObject]::*] {"
"    set args [info args $p];"
"    set idx -1;"
"    foreach a $args {"
"        lset args [incr idx] "
"            [if {[info default $p $a d]} {list $a $d} {list $a}]"
"    };"
"    set b [info body $p];"
"    set p [namespace tail $p];"
"    proc $p $args $b;"
"};"
"foreach v [info vars [info object namespace $originObject]::*] {"
"    upvar 0 $v vOrigin;"







|
<







182
183
184
185
186
187
188
189

190
191
192
193
194
195
196
 */

static const char *clonedBody =
"foreach p [info procs [info object namespace $originObject]::*] {"
"    set args [info args $p];"
"    set idx -1;"
"    foreach a $args {"
"        lset args [incr idx] [concat $a [info argspec proc $p $a]]"

"    };"
"    set b [info body $p];"
"    set p [namespace tail $p];"
"    proc $p $args $b;"
"};"
"foreach v [info vars [info object namespace $originObject]::*] {"
"    upvar 0 $v vOrigin;"

Changes to generic/tclOOInfo.c.

264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj;

	    argObj = Tcl_NewObj();
	    Tcl_ListObjAppendElement(NULL, argObj,
		    Tcl_NewStringObj(localPtr->name, -1));
	    if (localPtr->defValuePtr != NULL) {
		Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr);
	    }
	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(Tcl_GetHashValue(hPtr));
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}







|
|
<
<
<
<
<
|







264
265
266
267
268
269
270
271
272





273
274
275
276
277
278
279
280
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj = TclProcGetArgSpec(interp, localPtr,
		    TCL_GETARGSPEC_WITH_NAME|TCL_GETARGSPEC_TRY_OLDSTYLE);






	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(Tcl_GetHashValue(hPtr));
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj;

	    argObj = Tcl_NewObj();
	    Tcl_ListObjAppendElement(NULL, argObj,
		    Tcl_NewStringObj(localPtr->name, -1));
	    if (localPtr->defValuePtr != NULL) {
		Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr);
	    }
	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(clsPtr->constructorPtr);
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}







|
|
<
<
<
<
<
|







849
850
851
852
853
854
855
856
857





858
859
860
861
862
863
864
865
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj = TclProcGetArgSpec(interp, localPtr,
		    TCL_GETARGSPEC_WITH_NAME|TCL_GETARGSPEC_TRY_OLDSTYLE);






	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(clsPtr->constructorPtr);
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj;

	    argObj = Tcl_NewObj();
	    Tcl_ListObjAppendElement(NULL, argObj,
		    Tcl_NewStringObj(localPtr->name, -1));
	    if (localPtr->defValuePtr != NULL) {
		Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr);
	    }
	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(Tcl_GetHashValue(hPtr));
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}







|
|
<
<
<
<
<
|







912
913
914
915
916
917
918
919
920





921
922
923
924
925
926
927
928
	return TCL_ERROR;
    }

    resultObjs[0] = Tcl_NewObj();
    for (localPtr=procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj = TclProcGetArgSpec(interp, localPtr,
		    TCL_GETARGSPEC_WITH_NAME|TCL_GETARGSPEC_TRY_OLDSTYLE);






	    Tcl_ListObjAppendElement(NULL, resultObjs[0], argObj);
	}
    }
    resultObjs[1] = TclOOGetMethodBody(Tcl_GetHashValue(hPtr));
    Tcl_SetObjResult(interp, Tcl_NewListObj(2, resultObjs));
    return TCL_OK;
}

Changes to generic/tclOOMethod.c.

1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
     * Copy the argument list.
     */

    argsObj = Tcl_NewObj();
    for (localPtr=pmPtr->procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj = Tcl_NewObj();

	    Tcl_ListObjAppendElement(NULL, argObj,
		    Tcl_NewStringObj(localPtr->name, -1));
	    if (localPtr->defValuePtr != NULL) {
		Tcl_ListObjAppendElement(NULL, argObj, localPtr->defValuePtr);
	    }
	    Tcl_ListObjAppendElement(NULL, argsObj, argObj);
	}
    }

    /*
     * Must strip the internal representation in order to ensure that any
     * bound references to instance variables are removed. [Bug 3609693]







|
|
<
<
<
<
|







1293
1294
1295
1296
1297
1298
1299
1300
1301




1302
1303
1304
1305
1306
1307
1308
1309
     * Copy the argument list.
     */

    argsObj = Tcl_NewObj();
    for (localPtr=pmPtr->procPtr->firstLocalPtr; localPtr!=NULL;
	    localPtr=localPtr->nextPtr) {
	if (TclIsVarArgument(localPtr)) {
	    Tcl_Obj *argObj = TclProcGetArgSpec(interp, localPtr,
		    TCL_GETARGSPEC_WITH_NAME|TCL_GETARGSPEC_TRY_OLDSTYLE);





	    Tcl_ListObjAppendElement(NULL, argsObj, argObj);
	}
    }

    /*
     * Must strip the internal representation in order to ensure that any
     * bound references to instance variables are removed. [Bug 3609693]

Changes to generic/tclProc.c.

31
32
33
34
35
36
37



38
39
40
41
42
43
44
45
46
47
48
49










50
51
52
53
54
55
56
 */

static void		DupLambdaInternalRep(Tcl_Obj *objPtr,
			    Tcl_Obj *copyPtr);
static void		FreeLambdaInternalRep(Tcl_Obj *objPtr);
static int		InitArgsAndLocals(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj, int skip);



static void		InitResolvedLocals(Tcl_Interp *interp,
			    ByteCode *codePtr, Var *defPtr,
			    Namespace *nsPtr);
static void		InitLocalCache(Proc *procPtr);
static void		ProcBodyDup(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr);
static void		ProcBodyFree(Tcl_Obj *objPtr);
static int		ProcWrongNumArgs(Tcl_Interp *interp, int skip);
static void		MakeProcError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static void		MakeLambdaError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static int		SetLambdaFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);











static Tcl_NRPostProc ApplyNR2;
static Tcl_NRPostProc InterpProcNR2;
static Tcl_NRPostProc Uplevel_Callback;

/*
 * The ProcBodyObjType type







>
>
>












>
>
>
>
>
>
>
>
>
>







31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
 */

static void		DupLambdaInternalRep(Tcl_Obj *objPtr,
			    Tcl_Obj *copyPtr);
static void		FreeLambdaInternalRep(Tcl_Obj *objPtr);
static int		InitArgsAndLocals(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj, int skip);
static int		InitArgsWithOptions(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj, Tcl_Obj *const *argObjs,
			    int numArgs);
static void		InitResolvedLocals(Tcl_Interp *interp,
			    ByteCode *codePtr, Var *defPtr,
			    Namespace *nsPtr);
static void		InitLocalCache(Proc *procPtr);
static void		ProcBodyDup(Tcl_Obj *srcPtr, Tcl_Obj *dupPtr);
static void		ProcBodyFree(Tcl_Obj *objPtr);
static int		ProcWrongNumArgs(Tcl_Interp *interp, int skip);
static void		MakeProcError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static void		MakeLambdaError(Tcl_Interp *interp,
			    Tcl_Obj *procNameObj);
static int		SetLambdaFromAny(Tcl_Interp *interp, Tcl_Obj *objPtr);
static int		ProcAddNamedGroupEntry(Tcl_Interp *interp,
			    CompiledLocal *localPtr, const char *argName,
			    Tcl_Obj *valuePtr, NamedGroupEntry **lastEntryPtr,
			    Tcl_HashTable **namedHashPtrPtr);
static int		ProcParseArgSpec(Tcl_Interp *interp,
			    const char *argSpec, int argIdx, int isLastArg,
			    CompiledLocal **localPtrPtr,
			    Tcl_HashTable **namedHashPtrPtr);
static inline int	ProcCheckScalarArg(const char *arg, const char **err);
static void		ProcCompiledLocalsFree(CompiledLocal *localPtr);

static Tcl_NRPostProc ApplyNR2;
static Tcl_NRPostProc InterpProcNR2;
static Tcl_NRPostProc Uplevel_Callback;

/*
 * The ProcBodyObjType type
393
394
395
396
397
398
399
400
401
402
403

404
405
406
407
408
409
410
    Proc **procPtrPtr)		/* Returns: pointer to proc data. */
{
    Interp *iPtr = (Interp *) interp;
    const char **argArray = NULL;

    register Proc *procPtr;
    int i, length, result, numArgs;
    const char *args, *bytes, *p;
    register CompiledLocal *localPtr = NULL;
    Tcl_Obj *defPtr;
    int precompiled = 0;


    if (bodyPtr->typePtr == &tclProcBodyType) {
	/*
	 * Because the body is a TclProProcBody, the actual body is already
	 * compiled, and it is not shared with anyone else, so it's OK not to
	 * unshare it (as a matter of fact, it is bad to unshare it, because
	 * there may be no source code).







|

|

>







406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
    Proc **procPtrPtr)		/* Returns: pointer to proc data. */
{
    Interp *iPtr = (Interp *) interp;
    const char **argArray = NULL;

    register Proc *procPtr;
    int i, length, result, numArgs;
    const char *args, *bytes;
    register CompiledLocal *localPtr = NULL;
    CompiledLocal *newLocalPtr;
    int precompiled = 0;
    Tcl_HashTable *hPtr = NULL;

    if (bodyPtr->typePtr == &tclProcBodyType) {
	/*
	 * Because the body is a TclProProcBody, the actual body is already
	 * compiled, and it is not shared with anyone else, so it's OK not to
	 * unshare it (as a matter of fact, it is bad to unshare it, because
	 * there may be no source code).
460
461
462
463
464
465
466

467
468
469
470
471
472
473

	procPtr = ckalloc(sizeof(Proc));
	procPtr->iPtr = iPtr;
	procPtr->refCount = 1;
	procPtr->bodyPtr = bodyPtr;
	procPtr->numArgs = 0;	/* Actual argument count is set below. */
	procPtr->numCompiledLocals = 0;

	procPtr->firstLocalPtr = NULL;
	procPtr->lastLocalPtr = NULL;
    }

    /*
     * Break up the argument list into argument specifiers, then process each
     * argument specifier. If the body is precompiled, processing is limited







>







474
475
476
477
478
479
480
481
482
483
484
485
486
487
488

	procPtr = ckalloc(sizeof(Proc));
	procPtr->iPtr = iPtr;
	procPtr->refCount = 1;
	procPtr->bodyPtr = bodyPtr;
	procPtr->numArgs = 0;	/* Actual argument count is set below. */
	procPtr->numCompiledLocals = 0;
	procPtr->flags = 0;
	procPtr->firstLocalPtr = NULL;
	procPtr->lastLocalPtr = NULL;
    }

    /*
     * Break up the argument list into argument specifiers, then process each
     * argument specifier. If the body is precompiled, processing is limited
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615


616
617
618
619
620
621
622
623
624
625
626
627



628
629
630
631
632
633
634
635
636
637
638
639

640
641
642

643





644

645

646





647
648


649
650
651
652

653
654

655
656

657
658
659
660

661
662
663
664



665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693







































































































































































































































































































































































































































































































694
695
696
697
698
699
700
	localPtr = procPtr->firstLocalPtr;
    } else {
	procPtr->numArgs = numArgs;
	procPtr->numCompiledLocals = numArgs;
    }

    for (i = 0; i < numArgs; i++) {
	int fieldCount, nameLength;
	size_t valueLength;
	const char **fieldValues;

	/*
	 * Now divide the specifier up into name and default.
	 */

	result = Tcl_SplitList(interp, argArray[i], &fieldCount,
		&fieldValues);
	if (result != TCL_OK) {
	    goto procError;
	}
	if (fieldCount > 2) {
	    ckfree(fieldValues);
	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "too many fields in argument specifier \"%s\"",
		    argArray[i]));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
		    "FORMALARGUMENTFORMAT", NULL);
	    goto procError;
	}
	if ((fieldCount == 0) || (*fieldValues[0] == 0)) {
	    ckfree(fieldValues);
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		    "argument with no name", -1));
	    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
		    "FORMALARGUMENTFORMAT", NULL);
	    goto procError;
	}

	nameLength = strlen(fieldValues[0]);
	if (fieldCount == 2) {
	    valueLength = strlen(fieldValues[1]);
	} else {
	    valueLength = 0;
	}

	/*
	 * Check that the formal parameter name is a scalar.
	 */

	p = fieldValues[0];
	while (*p != '\0') {
	    if (*p == '(') {
		const char *q = p;
		do {
		    q++;
		} while (*q != '\0');
		q--;
		if (*q == ')') {	/* We have an array element. */
		    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			    "formal parameter \"%s\" is an array element",
			    fieldValues[0]));
		    ckfree(fieldValues);
		    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			    "FORMALARGUMENTFORMAT", NULL);
		    goto procError;
		}
	    } else if ((*p == ':') && (*(p+1) == ':')) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"formal parameter \"%s\" is not a simple name",
			fieldValues[0]));
		ckfree(fieldValues);
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"FORMALARGUMENTFORMAT", NULL);
		goto procError;
	    }
	    p++;
	}

	if (precompiled) {
	    /*
	     * Compare the parsed argument with the stored one. Note that the
	     * only flag value that makes sense at this point is VAR_ARGUMENT
	     * (its value was kept the same as pre VarReform to simplify
	     * tbcload's processing of older byetcodes).
	     *
	     * The only other flag vlaue that is important to retrieve from
	     * precompiled procs is VAR_TEMPORARY (also unchanged). It is
	     * needed later when retrieving the variable names.
	     */

	    if ((localPtr->nameLength != nameLength)
		    || (strcmp(localPtr->name, fieldValues[0]))
		    || (localPtr->frameIndex != i)
		    || !(localPtr->flags & VAR_ARGUMENT)
		    || (localPtr->defValuePtr == NULL && fieldCount == 2)
		    || (localPtr->defValuePtr != NULL && fieldCount != 2)) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"procedure \"%s\": formal parameter %d is "
			"inconsistent with precompiled body", procName, i));
		ckfree(fieldValues);
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"BYTECODELIES", NULL);
		goto procError;
	    }

	    /*
	     * Compare the default value if any.
	     */

	    if (localPtr->defValuePtr != NULL) {
		const char *tmpPtr = TclGetString(localPtr->defValuePtr);
		size_t tmpLength = localPtr->defValuePtr->length;

		if ((valueLength != tmpLength) ||
			strncmp(fieldValues[1], tmpPtr, tmpLength)) {
		    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			    "procedure \"%s\": formal parameter \"%s\" has "
			    "default value inconsistent with precompiled body",
			    procName, fieldValues[0]));
		    ckfree(fieldValues);


		    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			    "BYTECODELIES", NULL);
		    goto procError;
		}
	    }
	    if ((i == numArgs - 1)
		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (strcmp(localPtr->name, "args") == 0)) {
		localPtr->flags |= VAR_IS_ARGS;
	    }




	    localPtr = localPtr->nextPtr;
	} else {
	    /*
	     * Allocate an entry in the runtime procedure frame's array of
	     * local variables for the argument.
	     */

	    localPtr = ckalloc(TclOffset(CompiledLocal, name) + nameLength+1);
	    if (procPtr->firstLocalPtr == NULL) {
		procPtr->firstLocalPtr = procPtr->lastLocalPtr = localPtr;
	    } else {
		procPtr->lastLocalPtr->nextPtr = localPtr;

		procPtr->lastLocalPtr = localPtr;
	    }
	    localPtr->nextPtr = NULL;

	    localPtr->nameLength = nameLength;





	    localPtr->frameIndex = i;

	    localPtr->flags = VAR_ARGUMENT;

	    localPtr->resolveInfo = NULL;






	    if (fieldCount == 2) {


		localPtr->defValuePtr =
			Tcl_NewStringObj(fieldValues[1], valueLength);
		Tcl_IncrRefCount(localPtr->defValuePtr);
	    } else {

		localPtr->defValuePtr = NULL;
	    }

	    memcpy(localPtr->name, fieldValues[0], nameLength + 1);
	    if ((i == numArgs - 1)

		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (strcmp(localPtr->name, "args") == 0)) {
		localPtr->flags |= VAR_IS_ARGS;

	    }
	}

	ckfree(fieldValues);



    }

    *procPtrPtr = procPtr;
    ckfree(argArray);
    return TCL_OK;

  procError:
    if (precompiled) {
	procPtr->refCount--;
    } else {
	Tcl_DecrRefCount(bodyPtr);
	while (procPtr->firstLocalPtr != NULL) {
	    localPtr = procPtr->firstLocalPtr;
	    procPtr->firstLocalPtr = localPtr->nextPtr;

	    defPtr = localPtr->defValuePtr;
	    if (defPtr != NULL) {
		Tcl_DecrRefCount(defPtr);
	    }

	    ckfree(localPtr);
	}
	ckfree(procPtr);
    }
    if (argArray != NULL) {
	ckfree(argArray);
    }
    return TCL_ERROR;
}








































































































































































































































































































































































































































































































/*
 *----------------------------------------------------------------------
 *
 * TclGetFrame --
 *
 *	Given a description of a procedure frame, such as the first argument







<
<
<

<
<
<
<
|
|



<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
<
<
|
<
<
<
<
<
<
|
<
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<











|
|
|
|
<
<



|






|


|
|
<
|
<
<
|
|
|
|
|
>
>
|
|
|
|
|







>
>
>


<
<
<
<
<
<

|

|
>
|
|
|
>
|
>
>
>
>
>
|
>
|
>
|
>
>
>
>
>

<
>
>
|
<
|
<
>
|

>
|
<
>
|
|
|
|
>



|
>
>
>











|
<
<
<
<
<
<
<
<
<
<







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







511
512
513
514
515
516
517



518




519
520
521
522
523








524








525






526



527






























528
529
530
531
532
533
534
535
536
537
538
539
540
541
542


543
544
545
546
547
548
549
550
551
552
553
554
555
556
557

558


559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582






583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608

609
610
611

612

613
614
615
616
617

618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642










643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
	localPtr = procPtr->firstLocalPtr;
    } else {
	procPtr->numArgs = numArgs;
	procPtr->numCompiledLocals = numArgs;
    }

    for (i = 0; i < numArgs; i++) {








	result = ProcParseArgSpec(interp, argArray[i], i, (i == numArgs - 1),
	    &newLocalPtr, &hPtr);
	if (result != TCL_OK) {
	    goto procError;
	}

















	if (precompiled) {






	    Tcl_Obj *argSpec1, *argSpec2;


































	    /*
	     * Compare the parsed argument with the stored one. Note that the
	     * only flag value that makes sense at this point is VAR_ARGUMENT
	     * (its value was kept the same as pre VarReform to simplify
	     * tbcload's processing of older byetcodes).
	     *
	     * The only other flag vlaue that is important to retrieve from
	     * precompiled procs is VAR_TEMPORARY (also unchanged). It is
	     * needed later when retrieving the variable names.
	     */

	    if ((localPtr->nameLength != newLocalPtr->nameLength)
		    || (strcmp(localPtr->name, newLocalPtr->name))
		    || (localPtr->frameIndex != newLocalPtr->frameIndex)
		    || !(localPtr->flags & VAR_ARGUMENT)) {


		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"procedure \"%s\": formal parameter %d is "
			"inconsistent with precompiled body", procName, i));
		ProcCompiledLocalsFree(newLocalPtr);
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"BYTECODELIES", NULL);
		goto procError;
	    }

	    /*
	     * Compare the argument specification.
	     */

	    argSpec1 = TclProcGetArgSpec(interp, localPtr, 0);
	    argSpec2 = TclProcGetArgSpec(interp, newLocalPtr, 0);

	    if (strcmp(Tcl_GetString(argSpec1), Tcl_GetString(argSpec2)) != 0) {


		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"procedure \"%s\": formal parameter \"%s\" has "
			"argument spec inconsistent with precompiled body",
			procName, newLocalPtr->name));
		ProcCompiledLocalsFree(newLocalPtr);
		Tcl_DecrRefCount(argSpec1);
		Tcl_DecrRefCount(argSpec2);
		Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
			"BYTECODELIES", NULL);
		goto procError;
	    }

	    if ((i == numArgs - 1)
		    && (localPtr->nameLength == 4)
		    && (localPtr->name[0] == 'a')
		    && (strcmp(localPtr->name, "args") == 0)) {
		localPtr->flags |= VAR_IS_ARGS;
	    }

	    Tcl_DecrRefCount(argSpec1);
	    Tcl_DecrRefCount(argSpec2);
	    ProcCompiledLocalsFree(newLocalPtr);
	    localPtr = localPtr->nextPtr;
	} else {






	    if (procPtr->firstLocalPtr == NULL) {
		procPtr->firstLocalPtr = newLocalPtr;
	    } else {
		procPtr->lastLocalPtr->nextPtr = newLocalPtr;
	    }
	    procPtr->lastLocalPtr = newLocalPtr;

	    if (TclIsVarWithExtArgs(newLocalPtr)) {
		procPtr->flags |= PROC_HAS_EXT_ARG_SPEC;
		if (newLocalPtr->flags & VAR_NAMED_GROUP) {
		    procPtr->flags |= PROC_HAS_NAMED_GROUP;
		}
	    }
	}
    }

    if ((procPtr->flags & PROC_HAS_NAMED_GROUP)
	    && !(procPtr->lastLocalPtr->flags & (VAR_IS_ARGS|VAR_ARG_OPTIONAL))
	    && (procPtr->lastLocalPtr->defValuePtr == NULL)) {

	/* proc with at least one named group and a non-optional and non-args
	 * last variable. Find and store the number of arguments after the
	 * last named group, this will be used to end the named group handling
	 * and ensure the last input arguments can be assigned.
	 */


	CompiledLocal *lastNamedGroupPtr = NULL;
	for (localPtr = procPtr->firstLocalPtr; localPtr != NULL;
		localPtr = localPtr->nextPtr) {

	    if ((localPtr->argSpecPtr != NULL)

		    && (localPtr->argSpecPtr->namedHashTable != NULL)) {
		lastNamedGroupPtr = localPtr;
	    }
	}


	/* find last entry of the named group */
	for (localPtr = lastNamedGroupPtr; localPtr != NULL;
		localPtr = localPtr->nextPtr) {
	    if ((localPtr->nextPtr == NULL)
		    || !(localPtr->nextPtr->flags & VAR_NAMED_GROUP)) {
		break;
	    }
	}

	if (localPtr != NULL) {
	    lastNamedGroupPtr->argSpecPtr->remainAfterNamedGroup =
		procPtr->lastLocalPtr->frameIndex - localPtr->frameIndex;
	}
    }

    *procPtrPtr = procPtr;
    ckfree(argArray);
    return TCL_OK;

  procError:
    if (precompiled) {
	procPtr->refCount--;
    } else {
	Tcl_DecrRefCount(bodyPtr);
	ProcCompiledLocalsFree(procPtr->firstLocalPtr);










	ckfree(procPtr);
    }
    if (argArray != NULL) {
	ckfree(argArray);
    }
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * ProcCheckScalarArg --
 *
 *	Check that the argument name is a scalar.
 *
 * Results:
 *	Returns TCL_OK if argument is a scalar. Returns TCL_ERROR and
 *	set |err| with an error description if it is not a scalar.
 *
 * Side effects:
 *	None.
 *
 *----------------------------------------------------------------------
 */

static inline int
ProcCheckScalarArg(
    const char *arg,		/* Argument name. */
    const char **err)		/* Error description (output) */
{
    const char *p = arg;

    /*
     * Check that the formal parameter name is a scalar.
     */

    while (*p != '\0') {
	if (*p == '(') {
	    const char *q = p;
	    do {
		q++;
	    } while (*q != '\0');
	    q--;
	    if (*q == ')') {	/* We have an array element. */
		*err = "is an array element";
		return TCL_ERROR;
	    }
	} else if ((*p == ':') && (*(p+1) == ':')) {
	    *err = "is not a simple name";
	    return TCL_ERROR;
	}
	p++;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ProcAddNamedGroupEntry --
 *
 *	Create a NamedGroupEntry for a specific CompiledLocal object.
 *	NamedGroupEntry are created for each name defined using -name
 *	or -switch argument specifier. In the later case, a value is
 *	also associated with the name.
 *	A Tcl_HashTable object is shared between all contiguous named
 *	parameters, it is created when parsing the first entry.
 *
 * Results:
 *	Returns TCL_OK on success and TCL_ERROR if anything goes wrong.
 *
 * Side effects:
 *	If anything goes wrong, this function returns an error message in the
 *	interpreter. On success, memory is allocated and linked into the
 *	ExtendedArgSpec structure. A Tcl_HashTable is allocated when
 *	parsing the first entry of a named group entry. The variable pointed
 *	by lastEntryPtr will be updated to always contain the pointer
 *	of the last entry.
 *
 *----------------------------------------------------------------------
 */

static int
ProcAddNamedGroupEntry(
    Tcl_Interp *interp,		/* Interpreter containing proc. */
    CompiledLocal *localPtr,	/* Related CompiledLocal */
    const char *argName,	/* Name string to add */
    Tcl_Obj *valuePtr,		/* Value to add when related to a -switch
				 * argspec (NULL for -name argspec) */
    NamedGroupEntry **lastEntryPtr,
				/* Pointer to a variable which contain the current
				 * last named entry for this local. */
    Tcl_HashTable **namedHashPtrPtr)
				/* Shared Tcl_HashTable created on the first
				 * named parameter entry to speedup lookups
				 * of named parameters. */
{
    NamedGroupEntry *entryPtr;
    ExtendedArgSpec *argSpecPtr = localPtr->argSpecPtr;
    int length = strlen(argName), isNew;
    Tcl_HashEntry *hPtr;
    Tcl_HashTable *namedHashPtr;

    /*
     * Check that the name is not empty, does not contain a space or has
     * not already been added.
     */

    if (length == 0) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj(
	    "named argument with no name", -1));
	return TCL_ERROR;
    } else if (strchr(argName, ' ') != NULL) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "named argument \"%s\" with unexpected space character",
	    argName));
	return TCL_ERROR;
    } else if ((*namedHashPtrPtr != NULL)
	    && (Tcl_FindHashEntry(*namedHashPtrPtr, argName) != NULL)) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "named argument \"%s\" has been used more than once "
	    "in the same named group", argName));
	return TCL_ERROR;
    }

    entryPtr = ckalloc(TclOffset(NamedGroupEntry, name) + length+1);
    entryPtr->nextPtr = NULL;
    entryPtr->nameLength = length;
    entryPtr->localIndex = localPtr->frameIndex;
    entryPtr->localPtr = localPtr;
    entryPtr->valuePtr = valuePtr;
    memcpy(entryPtr->name, argName, length+1);
    if (entryPtr->valuePtr != NULL) {
	Tcl_IncrRefCount(entryPtr->valuePtr);
    }

    if (argSpecPtr->firstNamedEntryPtr == NULL) {
	argSpecPtr->firstNamedEntryPtr = entryPtr;
    } else {
	(*lastEntryPtr)->nextPtr = entryPtr;
    }
    *lastEntryPtr = entryPtr;

    /* Either use existing hash table or create a new one */

    if (*namedHashPtrPtr != NULL) {
	namedHashPtr = *namedHashPtrPtr;
    }
    else {
	namedHashPtr = ckalloc(sizeof(Tcl_HashTable));
	Tcl_InitHashTable(namedHashPtr, TCL_STRING_KEYS);

	argSpecPtr->namedHashTable = namedHashPtr;
	*namedHashPtrPtr = namedHashPtr;
    }

    hPtr = Tcl_CreateHashEntry(namedHashPtr, argName, &isNew);
    Tcl_SetHashValue(hPtr, entryPtr);

    localPtr->flags |= VAR_NAMED_GROUP;

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * ProcParseArgSpec --
 *
 *	Given an argument specification defined either using old-style
 *	(arg ?default?) or extended argument specification (TIP#457),
 *	create and initialize the related CompiledLocal entry.
 *
 * Results:
 *	Returns TCL_OK on success, along with a pointer to the created
 *	CompiledLocal entry. A Tcl_HashTable object is shared between
 *	all contiguous named parameters, it is created when parsing the
 *	first entry. Returns TCL_ERROR and set an error if the argument
 *	specification is not valid.
 *
 * Side effects:
 *	CompiledLocal and related entries are allocated. A Tcl_HashTable
 *	is allocated when parsing the first entry of a named group entry.
 *
 *----------------------------------------------------------------------
 */

static int
ProcParseArgSpec(
    Tcl_Interp *interp,		/* Interpreter containing proc. */
    const char *argSpec,	/* Argument specification. */
    int argIdx,			/* Argument index in the array of variables
				 * in the procedure call frame. */
    int isLastArg,		/* != 0 means this is the last argument. */
    CompiledLocal **localPtrPtr,
				/* On success, store the created CompiledLocal
				 * here. */
    Tcl_HashTable **namedHashPtrPtr)
				/* Shared Tcl_HashTable created on the first
				 * named parameter entry to speedup lookups
				 * of named parameters. */
{
    CompiledLocal *localPtr = NULL;
    ExtendedArgSpec *argSpecPtr;
    NamedGroupEntry *lastNamedEntryPtr = NULL;
    const char **fieldValues = NULL;
    int fieldCount, length;
    const char *err;
    int required = -1, hasSwitch = 0;
    int result, i, j;

    /*
     * Divide the argument specifier into a list.
     */

    result = Tcl_SplitList(interp, argSpec, &fieldCount, &fieldValues);
    if (result != TCL_OK) {
	goto parseError;
    }

    if ((fieldCount > 2) && (fieldCount % 2 != 1)) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "unexpected fields number in argument specifier \"%s\"",
	    argSpec));
	goto parseError;
    } else if ((fieldCount == 0) || (*fieldValues[0] == 0)) {
	Tcl_SetObjResult(interp, Tcl_NewStringObj(
	    "argument with no name", -1));
	goto parseError;
    }

    /*
     * Check that the formal parameter name is a scalar.
     */

    if (ProcCheckScalarArg(fieldValues[0], &err) != TCL_OK) {
	Tcl_SetObjResult(interp, Tcl_ObjPrintf(
	    "formal parameter \"%s\" %s", fieldValues[0], err));
	goto parseError;
    }

    length = strlen(fieldValues[0]);
    localPtr = ckalloc(TclOffset(CompiledLocal, name) + length+1);
    localPtr->nextPtr = NULL;
    localPtr->nameLength = length;
    localPtr->frameIndex = argIdx;
    localPtr->flags = VAR_ARGUMENT;
    localPtr->defValuePtr = NULL;
    localPtr->resolveInfo = NULL;
    localPtr->argSpecPtr = NULL;
    memcpy(localPtr->name, fieldValues[0], length+1);

    if ((isLastArg != 0)
	    && (localPtr->nameLength == 4)
	    && (localPtr->name[0] == 'a')
	    && (strcmp(localPtr->name, "args") == 0)) {
	localPtr->flags |= VAR_IS_ARGS;
    }

    if (fieldCount <= 2) {
	/* old-style argument specification (arg ?default?) */

	if (fieldCount == 2) {
	    localPtr->defValuePtr = Tcl_NewStringObj(fieldValues[1], -1);
	    Tcl_IncrRefCount(localPtr->defValuePtr);
	}

	*localPtrPtr = localPtr;
	*namedHashPtrPtr = NULL;
	ckfree(fieldValues);
	return TCL_OK;
    }

    /*
     * Check and handle each extended argument specification.
     */

    argSpecPtr = ckalloc(sizeof(ExtendedArgSpec));
    argSpecPtr->firstNamedEntryPtr = NULL;
    argSpecPtr->namedHashTable = NULL;
    argSpecPtr->remainAfterNamedGroup = -1;
    argSpecPtr->upvarLevelPtr = NULL;
    localPtr->argSpecPtr = argSpecPtr;

    for (i = 1 ; i < fieldCount ; i += 2) {
	length = strlen(fieldValues[i]);

	if (*fieldValues[i] != '-') {

	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"unknown argument option \"%s\" or "
		"too many fields in argument specifier \"%s\"",
		fieldValues[i], argSpec));
	    goto parseError;

	} else if ((length == 8)
		&& (strcmp(fieldValues[i], "-default") == 0)) {

	    /*
	     * Handle '-default <value>', keep value in localPtr->defValuePtr.
	     */

	    if (localPtr->defValuePtr != NULL) {
		Tcl_DecrRefCount(localPtr->defValuePtr);
	    }
	    localPtr->defValuePtr = Tcl_NewStringObj(fieldValues[i+1], -1);
	    Tcl_IncrRefCount(localPtr->defValuePtr);

	} else if ((length == 5)
		&& (strcmp(fieldValues[i], "-name") == 0)) {

	    /*
	     * Handle '-name name', this create a name group if not
	     * already created.
	     */

	    int nameCount;
	    const char **nameValues;

	    result = Tcl_SplitList(interp, fieldValues[i+1], &nameCount,
		    &nameValues);
	    if (result != TCL_OK) {
		goto parseError;
	    }

	    for (j = 0; j < nameCount; j++) {
		result = ProcAddNamedGroupEntry(interp, localPtr,
		    nameValues[j], NULL, &lastNamedEntryPtr, namedHashPtrPtr);
		if (result != TCL_OK) {
		    ckfree(nameValues);
		    goto parseError;
		}
	    }

	    ckfree(nameValues);

	} else if ((length == 7)
		&& (strcmp(fieldValues[i], "-switch") == 0)) {

	    /*
	     * Handle '-switch switch', this create a name group (with a value)
	     * if not already created.
	     */

	    int swCount, swEntCount;
	    const char **swValues, **swEntValues;

	    result = Tcl_SplitList(interp, fieldValues[i+1], &swCount,
		&swValues);
	    if (result != TCL_OK) {
		goto parseError;
	    }

	    for (j = 0; j < swCount; j++) {
		result = Tcl_SplitList(interp, swValues[j], &swEntCount,
		    &swEntValues);
		if (result != TCL_OK) {
		    ckfree(swValues);
		    goto parseError;
		}

		if (swEntCount == 1) {
		    /* one field, use switch name as value */
		    result = ProcAddNamedGroupEntry(interp, localPtr,
			swEntValues[0], Tcl_NewStringObj(swEntValues[0], -1),
			&lastNamedEntryPtr, namedHashPtrPtr);
		} else if (swEntCount == 2) {
		    /* two fields, use second one as value */
		    result = ProcAddNamedGroupEntry(interp, localPtr,
			swEntValues[0], Tcl_NewStringObj(swEntValues[1], -1),
			&lastNamedEntryPtr, namedHashPtrPtr);
		} else {
		    /* invalid number of fields */
		    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
			"incorrect switch value \"%s\"", swValues[j]));
		    result = TCL_ERROR;
		}
		ckfree(swEntValues);

		if (result != TCL_OK) {
		    ckfree(swValues);
		    goto parseError;
		}
	    }

	    hasSwitch = 1;
	    ckfree(swValues);

	} else if ((length == 6)
		&& (strcmp(fieldValues[i], "-upvar") == 0)) {

	    /*
	     * Handle '-upvar level', set upvarLevelPtr and VAR_ARG_UPVAR
	     * flag.
	     */

	    const char *levelStr = fieldValues[i+1];
	    int level;

	    if (Tcl_GetInt(NULL, (*levelStr == '#') ? levelStr+1 : levelStr,
		    &level) != TCL_OK || level < 0) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "Invalid level \"%s\" for -upvar arg specifier",
		    levelStr));
		goto parseError;
	    }

	    if (argSpecPtr->upvarLevelPtr != NULL) {
		Tcl_DecrRefCount(argSpecPtr->upvarLevelPtr);
	    }
	    argSpecPtr->upvarLevelPtr = Tcl_NewStringObj(levelStr, -1);
	    Tcl_IncrRefCount(argSpecPtr->upvarLevelPtr);
	    localPtr->flags |= VAR_ARG_UPVAR;

	} else if ((length == 9)
		&& (strcmp(fieldValues[i], "-required") == 0)) {

	    /*
	     * Handle '-required bool', set required var which will be used
	     * to set VAR_ARG_OPTIONAL flag depending on the other specifiers.
	     */

	    Tcl_Obj *boolObj;
	    int boolVal;

	    boolObj = Tcl_NewStringObj(fieldValues[i+1], -1);
	    Tcl_IncrRefCount(boolObj);
	    result = Tcl_GetBooleanFromObj(NULL, boolObj, &boolVal);
	    TclDecrRefCount(boolObj);

	    if (result != TCL_OK) {
		Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		    "Invalid boolean \"%s\" for -required arg specifier",
		    fieldValues[i+1]));
		goto parseError;
	    } else if (boolVal) {
		required = 1;
	    } else {
		required = 0;
	    }

	} else {

	    Tcl_SetObjResult(interp, Tcl_ObjPrintf(
		"unknown argument option \"%s\" or "
		"too many fields in argument specifier \"%s\"",
		fieldValues[i], argSpec));
	    goto parseError;

	}
    }

    if (hasSwitch) {
	if (localPtr->flags & VAR_ARG_UPVAR) {
	    Tcl_SetObjResult(interp, Tcl_NewStringObj(
		"-upvar can't be used with -switch", -1));
	    goto parseError;
	}
    }

    if ((required == 1) && (localPtr->defValuePtr)) {
	/* '-required 1' explicitely set, remove the default value to
	 * enforce the requirement. */
	Tcl_DecrRefCount(localPtr->defValuePtr);
	localPtr->defValuePtr = NULL;
    } else if ((required == 0)
	    || ((required == -1) && (localPtr->flags & VAR_NAMED_GROUP))) {
	/* Variable is optional if '-required 0' has been explicitely set,
	 * or if is part of a named group and no '-required' specifier
	 * has been used. */
	localPtr->flags |= VAR_ARG_OPTIONAL;
    }

    if ((*namedHashPtrPtr != NULL) && !(localPtr->flags & VAR_NAMED_GROUP)) {
	/* Not part of a named group anymore, unset current named hash table */
	*namedHashPtrPtr = NULL;
    }

    *localPtrPtr = localPtr;
    ckfree(fieldValues);
    return TCL_OK;

parseError:
    if (localPtr != NULL) {
	ProcCompiledLocalsFree(localPtr);
    }
    if (fieldValues) {
	ckfree(fieldValues);
    }
    Tcl_SetErrorCode(interp, "TCL", "OPERATION", "PROC",
	"FORMALARGUMENTFORMAT", NULL);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
 * TclGetFrame --
 *
 *	Given a description of a procedure frame, such as the first argument
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
    Tcl_Interp *interp,
    int skip)
{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    register Proc *procPtr = framePtr->procPtr;
    register Var *defPtr;
    int localCt = procPtr->numCompiledLocals, numArgs, i;
    Tcl_Obj **desiredObjs;
    const char *final = NULL;

    /*
     * Build up desired argument list for Tcl_WrongNumArgs
     */

    numArgs = framePtr->procPtr->numArgs;







|







1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
    Tcl_Interp *interp,
    int skip)
{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    register Proc *procPtr = framePtr->procPtr;
    register Var *defPtr;
    int localCt = procPtr->numCompiledLocals, numArgs, i;
    Tcl_Obj **desiredObjs, *argObj, *finalObj = NULL;
    const char *final = NULL;

    /*
     * Build up desired argument list for Tcl_WrongNumArgs
     */

    numArgs = framePtr->procPtr->numArgs;
1091
1092
1093
1094
1095
1096
1097























































1098
1099
1100
1101
1102
1103
1104
1105

1106
1107
1108
1109
1110
1111
1112
1113
1114

1115
1116
1117
1118
1119
1120
1121
1122



1123
1124
1125
1126
1127
1128
1129
	desiredObjs[0] = framePtr->objv[skip-1];
#else
	desiredObjs[0] = Tcl_NewListObj(1, framePtr->objv + skip - 1);
#endif /* AVOID_HACKS_FOR_ITCL */
    }
    Tcl_IncrRefCount(desiredObjs[0]);
























































    defPtr = (Var *) (&framePtr->localCachePtr->varName0 + localCt);
    for (i=1 ; i<=numArgs ; i++, defPtr++) {
	Tcl_Obj *argObj;
	Tcl_Obj *namePtr = localName(framePtr, i-1);

	if (defPtr->value.objPtr != NULL) {
	    TclNewObj(argObj);
	    Tcl_AppendStringsToObj(argObj, "?", TclGetString(namePtr), "?", NULL);

	} else if (defPtr->flags & VAR_IS_ARGS) {
	    numArgs--;
	    final = "?arg ...?";
	    break;
	} else {
	    argObj = namePtr;
	    Tcl_IncrRefCount(namePtr);
	}
	desiredObjs[i] = argObj;

    }

    Tcl_ResetResult(interp);
    Tcl_WrongNumArgs(interp, numArgs+1, desiredObjs, final);

    for (i=0 ; i<=numArgs ; i++) {
	Tcl_DecrRefCount(desiredObjs[i]);
    }



    TclStackFree(interp, desiredObjs);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
<
|

|
|
|
>
|
|
|
|
|
|
|
|
|
>








>
>
>







1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597

1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
	desiredObjs[0] = framePtr->objv[skip-1];
#else
	desiredObjs[0] = Tcl_NewListObj(1, framePtr->objv + skip - 1);
#endif /* AVOID_HACKS_FOR_ITCL */
    }
    Tcl_IncrRefCount(desiredObjs[0]);

    if (procPtr->flags & PROC_HAS_EXT_ARG_SPEC) {
	CompiledLocal *localPtr;
	int first = 1;

	TclNewObj(finalObj);
	numArgs = 0;
	for (localPtr = procPtr->firstLocalPtr;
		localPtr != NULL;
		localPtr = localPtr->nextPtr) {
	    if (!TclIsVarArgument(localPtr)) {
		continue;
	    } else  if (localPtr->flags & VAR_IS_ARGS) {
		argObj = Tcl_NewStringObj("?arg ...?", -1);
	    } else if ((localPtr->argSpecPtr != NULL)
		    && (localPtr->argSpecPtr->firstNamedEntryPtr != NULL)) {
		NamedGroupEntry *namedGroupPtr;
		TclNewObj(argObj);
		for (namedGroupPtr = localPtr->argSpecPtr->firstNamedEntryPtr;
			namedGroupPtr != NULL;
			namedGroupPtr = namedGroupPtr->nextPtr) {
		    Tcl_AppendStringsToObj(argObj, "|-", namedGroupPtr->name,
			NULL);
		    if (localPtr->flags & VAR_ARG_UPVAR) {
			Tcl_AppendStringsToObj(argObj,
			    " &", localPtr->name, "&", NULL);
		    } else if (namedGroupPtr->valuePtr == NULL) {
			Tcl_AppendStringsToObj(argObj, " ", localPtr->name,
			    NULL);
		    }
		}
		Tcl_AppendStringsToObj(argObj, "|", NULL);
	    } else {
		if (localPtr->flags & VAR_ARG_UPVAR) {
		    TclNewObj(argObj);
		    Tcl_AppendStringsToObj(argObj,
			"&", localPtr->name, "&", NULL);
		} else {
		    argObj = Tcl_NewStringObj(localPtr->name,
			localPtr->nameLength);
		}
	    }

	    if ((localPtr->defValuePtr != NULL)
		    || (localPtr->flags & VAR_ARG_OPTIONAL)) {
		Tcl_AppendStringsToObj(finalObj, first ? "?" : " ?",
		    Tcl_GetString(argObj), "?", NULL);
	    } else {
		Tcl_AppendStringsToObj(finalObj, first ? "" : " ",
		    Tcl_GetString(argObj), NULL);
	    }
	    first = 0;
	    Tcl_DecrRefCount(argObj);
	}
	final = Tcl_GetString(finalObj);
    } else {
	defPtr = (Var *) (&framePtr->localCachePtr->varName0 + localCt);
	for (i=1 ; i<=numArgs ; i++, defPtr++) {

	    Tcl_Obj *namePtr = localName(framePtr, i-1);

	    if (defPtr->value.objPtr != NULL) {
		TclNewObj(argObj);
		Tcl_AppendStringsToObj(argObj, "?", TclGetString(namePtr),
		    "?", NULL);
	    } else if (defPtr->flags & VAR_IS_ARGS) {
		numArgs--;
		final = "?arg ...?";
		break;
	    } else {
		argObj = namePtr;
		Tcl_IncrRefCount(namePtr);
	    }
	    desiredObjs[i] = argObj;
	}
    }

    Tcl_ResetResult(interp);
    Tcl_WrongNumArgs(interp, numArgs+1, desiredObjs, final);

    for (i=0 ; i<=numArgs ; i++) {
	Tcl_DecrRefCount(desiredObjs[i]);
    }
    if (finalObj != NULL) {
	Tcl_DecrRefCount(finalObj);
    }
    TclStackFree(interp, desiredObjs);
    return TCL_ERROR;
}

/*
 *----------------------------------------------------------------------
 *
1444
1445
1446
1447
1448
1449
1450














1451
1452
1453
1454
1455
1456
1457
    if (numArgs == 0) {
	if (argCt) {
	    goto incorrectArgs;
	} else {
	    goto correctArgs;
	}
    }














    imax = ((argCt < numArgs-1) ? argCt : numArgs-1);
    for (i = 0; i < imax; i++, varPtr++, defPtr ? defPtr++ : defPtr) {
	/*
	 * "Normal" arguments; last formal is special, depends on it being
	 * 'args'.
	 */








>
>
>
>
>
>
>
>
>
>
>
>
>
>







1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
    if (numArgs == 0) {
	if (argCt) {
	    goto incorrectArgs;
	} else {
	    goto correctArgs;
	}
    }

    if ((procPtr->flags & PROC_HAS_EXT_ARG_SPEC) != 0) {
	int result;

	memset(varPtr, 0, numArgs*sizeof(Var));
	result = InitArgsWithOptions(interp, procNameObj, argObjs, argCt);
	varPtr += numArgs;
	if (result != TCL_OK) {
	    goto incorrectArgs;
	} else {
	    goto correctArgs;
	}
    }

    imax = ((argCt < numArgs-1) ? argCt : numArgs-1);
    for (i = 0; i < imax; i++, varPtr++, defPtr ? defPtr++ : defPtr) {
	/*
	 * "Normal" arguments; last formal is special, depends on it being
	 * 'args'.
	 */

1529
1530
1531
1532
1533
1534
1535





































































































































































































































































































































1536
1537
1538
1539
1540
1541
1542
	    TclInitRewriteEnsemble(interp, skip-1, 0, framePtr->objv)) {
	TclNRAddCallback(interp, TclClearRootEnsemble, NULL, NULL, NULL, NULL);
    }
    memset(varPtr, 0,
	    ((framePtr->compiledLocals + localCt)-varPtr) * sizeof(Var));
    return ProcWrongNumArgs(interp, skip);
}






































































































































































































































































































































/*
 *----------------------------------------------------------------------
 *
 * TclPushProcCallFrame --
 *
 *	Compiles a proc body if necessary, then pushes a CallFrame suitable







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
	    TclInitRewriteEnsemble(interp, skip-1, 0, framePtr->objv)) {
	TclNRAddCallback(interp, TclClearRootEnsemble, NULL, NULL, NULL, NULL);
    }
    memset(varPtr, 0,
	    ((framePtr->compiledLocals + localCt)-varPtr) * sizeof(Var));
    return ProcWrongNumArgs(interp, skip);
}

/*
 *----------------------------------------------------------------------
 *
 * InitArgsWithOptions --
 *
 *	This routine is invoked in order to initialize the arguments
 *	for a new call frame for a proc with extended arguments
 *	specification. The numArgs variables in the array of local
 *	variables must be initialized prior to calling this function.
 *
 * Results:
 *	A standard Tcl result.
 *
 * Side effects:
 *	The numArgs variables inside the array of local variables
 *	can be initialized with object references, even if an error
 *	is returned.
 *
 *----------------------------------------------------------------------
 */

static int
InitArgsWithOptions(
    register Tcl_Interp *interp,/* Interpreter in which procedure was
				 * invoked. */
    Tcl_Obj *procNameObj,	/* Procedure name for error reporting. */
    Tcl_Obj *const *argObjs,	/* Array of input arguments */
    int argCt)			/* Number of input arguments */
{
    CallFrame *framePtr = ((Interp *)interp)->varFramePtr;
    register Proc *procPtr = framePtr->procPtr;
    register Var *varPtr = framePtr->compiledLocals, *localVarPtr;
    int numArgs = procPtr->numArgs, iLocal, iArg;
    Tcl_Obj *objPtr;
    CompiledLocal *localPtr;
    NamedGroupEntry *namedGroupPtr;
    int optLength, result;
    const char *optStr;

    for (iLocal = 0, iArg = 0, localPtr = procPtr->firstLocalPtr;
	    iLocal < numArgs;
	    iLocal++, localPtr = localPtr->nextPtr) {
	if (localPtr->flags & VAR_IS_ARGS) {

	    /*
	     * 'args' last argument, copy remaining arguments (can be empty)
	     */

	    objPtr = Tcl_NewListObj(argCt-iArg, argObjs+iArg);
	    varPtr[iLocal].value.objPtr = objPtr;
	    Tcl_IncrRefCount(objPtr);
	    iArg = argCt;  /* all arguments have been handled */

	} else if (localPtr->flags & VAR_NAMED_GROUP) {

	    /*
	     * Start of a named argument group. Handle all of them together.
	     */

	    for (; iArg < argCt; iArg++) {

		if ((localPtr->argSpecPtr != NULL)
			&& (localPtr->argSpecPtr->remainAfterNamedGroup > 0)
			&& ((argCt - iArg) <=
			    localPtr->argSpecPtr->remainAfterNamedGroup)) {
		    /* Remaining arguments must be assigned to remaining
		     * positional locals after named group, end handling
		     */
		    break;
		}

		optStr = TclGetString(argObjs[iArg]);

		if (*optStr != '-') {
		    /* argument without leading dash, end named group */
		    break;
		}

		optStr++;
		optLength = strlen(optStr);

		if ((optLength == 1) && (optStr[0] == '-')) {
		    /* end-of-option marker (--) */
		    iArg++;
		    break;
		}

		/*
		 * Find the referenced argument using the hash table
		 * created with the first name of the named group.
		 */

		namedGroupPtr = NULL;
		if (localPtr->argSpecPtr->namedHashTable != NULL) {
		    Tcl_HashEntry *hPtr;
		    hPtr = Tcl_FindHashEntry(
			localPtr->argSpecPtr->namedHashTable, optStr);
		    if (hPtr != NULL) {
			namedGroupPtr = Tcl_GetHashValue(hPtr);
		    }
		}

		if (namedGroupPtr == NULL) {
		    return TCL_ERROR;  /* not found */
		} else if (namedGroupPtr->valuePtr) {
		    objPtr = namedGroupPtr->valuePtr;
		} else if (iArg+1 < argCt) {
		    objPtr = argObjs[++iArg];
		} else {
		    return TCL_ERROR;  /* not enough input args */
		}

		if (namedGroupPtr->localPtr->flags & VAR_ARG_UPVAR) {
		    result = TclUpvarForExtArg(interp,
			namedGroupPtr->localPtr->argSpecPtr->upvarLevelPtr,
			objPtr, namedGroupPtr->localPtr->name);
		    if (result != TCL_OK) {
			return result;
		    }
		} else {
		    localVarPtr = &(varPtr[namedGroupPtr->localIndex]);
		    if (localVarPtr->value.objPtr != NULL) {
			Tcl_DecrRefCount(localVarPtr->value.objPtr);
		    }

		    localVarPtr->value.objPtr = objPtr;
		    Tcl_IncrRefCount(objPtr);
		}

	    }

	    /*
	     * Ensure that all variables in the named group have been
	     * set, have a default value or are optionals. localPtr is set
	     * to the last argument of the named group so that the loop
	     * will continue with the next argument. iLocal is updated in
	     * the same way.
	     */

	    for (; ; localPtr=localPtr->nextPtr, iLocal++) {
		if (!varPtr[iLocal].value.objPtr) {
		    if (localPtr->defValuePtr) {
			objPtr = localPtr->defValuePtr;
			varPtr[iLocal].value.objPtr = objPtr;
			Tcl_IncrRefCount(objPtr);
		    } else if (!(localPtr->flags & VAR_ARG_OPTIONAL)) {
			return TCL_ERROR;
		    }
		}

		/* stop on the last argument of the named group */
		if ((localPtr->nextPtr == NULL)
			|| !(localPtr->nextPtr->flags & (VAR_NAMED_GROUP))) {
		    break;
		}
	    }
	} else if (iArg < argCt) {

	    /*
	     * Set normal or 'upvar' variable using next input argument.
	     */

	    objPtr = argObjs[iArg++];
	    if (localPtr->flags & VAR_ARG_UPVAR) {
		result = TclUpvarForExtArg(interp,
		    localPtr->argSpecPtr->upvarLevelPtr, objPtr,
		    localPtr->name);
		if (result != TCL_OK) {
		    return result;
		}
	    } else {
		varPtr[iLocal].value.objPtr = objPtr;
		Tcl_IncrRefCount(objPtr);
	    }

	} else if (localPtr->defValuePtr) {

	    /*
	     * No more input arguments, set default value
	     */

	    objPtr = localPtr->defValuePtr;
	    varPtr[iLocal].value.objPtr = objPtr;
	    Tcl_IncrRefCount(objPtr);

	} else if (!(localPtr->flags & VAR_ARG_OPTIONAL)) {

	    /*
	     * No more input arguments, no default value, not optional.
	     */

	    return TCL_ERROR;
	}
    }

    if (iArg < argCt) {

	/*
	 * Remaining input arguments which have not been handled.
	 */

	return TCL_ERROR;
    }

    return TCL_OK;
}

/*
 *----------------------------------------------------------------------
 *
 * TclProcGetArgSpec --
 *
 *	This routine is invoked in order to build an argument specification
 *	for an existing argument. The TCL_GETARGSPEC_WITH_NAME or
 *	TCL_GETARGSPEC_TRY_OLDSTYLE flags can be used to change how the
 *	returned list is built.
 *
 * Results:
 *	Returns a pointer to a newly allocated Tcl_Obj list.
 *
 * Side effects:
 *	The returned list is allocated.
 *
 *----------------------------------------------------------------------
 */

Tcl_Obj *
TclProcGetArgSpec(
    Tcl_Interp *interp,
    CompiledLocal *localPtr,
    int flags)
{
    Tcl_Obj *listObjPtr, *nameListPtr = NULL, *switchListPtr = NULL;
    NamedGroupEntry *ngPtr;

    listObjPtr = Tcl_NewListObj(0, NULL);

    if (flags & TCL_GETARGSPEC_WITH_NAME) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj(localPtr->name, localPtr->nameLength));

    }

    if ((flags & TCL_GETARGSPEC_TRY_OLDSTYLE)
	    && (!TclIsVarWithExtArgs(localPtr))) {
	if (localPtr->defValuePtr != NULL) {
	    Tcl_ListObjAppendElement(interp, listObjPtr,
		localPtr->defValuePtr);
	}
	return listObjPtr;
    }

    if (localPtr->defValuePtr != NULL) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-default", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    localPtr->defValuePtr);
    }

    if ((localPtr->flags & VAR_NAMED_GROUP)
	    && !(localPtr->flags & VAR_ARG_OPTIONAL)) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-required", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewBooleanObj(1));
    } else if (!(localPtr->flags & VAR_NAMED_GROUP)
	    && (localPtr->flags & VAR_ARG_OPTIONAL)) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-required", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewBooleanObj(0));
    }

    if ((localPtr->argSpecPtr != NULL) && (localPtr->flags & VAR_ARG_UPVAR)) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-upvar", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    localPtr->argSpecPtr->upvarLevelPtr);
    }

    if ((localPtr->argSpecPtr != NULL)
	&& (localPtr->argSpecPtr->firstNamedEntryPtr != NULL)) {
	for (ngPtr = localPtr->argSpecPtr->firstNamedEntryPtr;
		ngPtr != NULL;
		ngPtr = ngPtr->nextPtr) {
	    Tcl_Obj *objArray[2] = {
		Tcl_NewStringObj(ngPtr->name, ngPtr->nameLength),
		ngPtr->valuePtr
	    };
	    if (objArray[1] == NULL) {
		if (nameListPtr == NULL) {
		    nameListPtr = Tcl_NewListObj(0, NULL);
		}
		Tcl_ListObjAppendElement(interp, nameListPtr, objArray[0]);
	    } else if ((objArray[0]->length == objArray[1]->length)
		    && (strcmp(Tcl_GetString(objArray[0]), Tcl_GetString(objArray[1])) == 0)) {
		if (switchListPtr == NULL) {
		    switchListPtr = Tcl_NewListObj(0, NULL);
		}
		Tcl_ListObjAppendElement(interp, switchListPtr, objArray[0]);
	    } else {
		if (switchListPtr == NULL) {
		    switchListPtr = Tcl_NewListObj(0, NULL);
		}
		Tcl_ListObjAppendElement(interp, switchListPtr,
		    Tcl_NewListObj(2, objArray));
	    }
	}
    }

    if (nameListPtr != NULL) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-name", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr, nameListPtr);
    }

    if (switchListPtr != NULL) {
	Tcl_ListObjAppendElement(interp, listObjPtr,
	    Tcl_NewStringObj("-switch", -1));
	Tcl_ListObjAppendElement(interp, listObjPtr, switchListPtr);
    }

    return listObjPtr;
}

/*
 *----------------------------------------------------------------------
 *
 * TclPushProcCallFrame --
 *
 *	Compiles a proc body if necessary, then pushes a CallFrame suitable
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026

2027
2028
2029
2030
2031
2032
2033

	    if (lastPtr) {
		lastPtr->nextPtr = NULL;
	    } else {
		procPtr->firstLocalPtr = NULL;
	    }
	    procPtr->lastLocalPtr = lastPtr;
	    while (clPtr) {
		CompiledLocal *toFree = clPtr;

		clPtr = clPtr->nextPtr;
		if (toFree->resolveInfo) {
		    if (toFree->resolveInfo->deleteProc) {
			toFree->resolveInfo->deleteProc(toFree->resolveInfo);
		    } else {
			ckfree(toFree->resolveInfo);
		    }
		}
		ckfree(toFree);
	    }
	    procPtr->numCompiledLocals = procPtr->numArgs;

	}

	(void) TclPushStackFrame(interp, &framePtr, (Tcl_Namespace *) nsPtr,
		/* isProcCallFrame */ 0);

	/*
	 * TIP #280: We get the invoking context from the cmdFrame which







<
<
<
<
<
<
<
<
<
<
<
<
<

>







2847
2848
2849
2850
2851
2852
2853













2854
2855
2856
2857
2858
2859
2860
2861
2862

	    if (lastPtr) {
		lastPtr->nextPtr = NULL;
	    } else {
		procPtr->firstLocalPtr = NULL;
	    }
	    procPtr->lastLocalPtr = lastPtr;













	    procPtr->numCompiledLocals = procPtr->numArgs;
	    ProcCompiledLocalsFree(clPtr);
	}

	(void) TclPushStackFrame(interp, &framePtr, (Tcl_Namespace *) nsPtr,
		/* isProcCallFrame */ 0);

	/*
	 * TIP #280: We get the invoking context from the cmdFrame which
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
 *----------------------------------------------------------------------
 */

void
TclProcCleanupProc(
    register Proc *procPtr)	/* Procedure to be deleted. */
{
    register CompiledLocal *localPtr;
    Tcl_Obj *bodyPtr = procPtr->bodyPtr;
    Tcl_Obj *defPtr;
    Tcl_ResolvedVarInfo *resVarInfo;
    Tcl_HashEntry *hePtr = NULL;
    CmdFrame *cfPtr = NULL;
    Interp *iPtr = procPtr->iPtr;

    if (bodyPtr != NULL) {
	Tcl_DecrRefCount(bodyPtr);
    }
    for (localPtr = procPtr->firstLocalPtr; localPtr != NULL; ) {
	CompiledLocal *nextPtr = localPtr->nextPtr;

	resVarInfo = localPtr->resolveInfo;
	if (resVarInfo) {
	    if (resVarInfo->deleteProc) {
		resVarInfo->deleteProc(resVarInfo);
	    } else {
		ckfree(resVarInfo);
	    }
	}

	if (localPtr->defValuePtr != NULL) {
	    defPtr = localPtr->defValuePtr;
	    Tcl_DecrRefCount(defPtr);
	}
	ckfree(localPtr);
	localPtr = nextPtr;
    }
    ckfree(procPtr);

    /*
     * TIP #280: Release the location data associated with this Proc
     * structure, if any. The interpreter may not exist (For example for
     * procbody structures created by tbcload.
     */







<

<
<







<
<
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







2969
2970
2971
2972
2973
2974
2975

2976


2977
2978
2979
2980
2981
2982
2983


2984
















2985
2986
2987
2988
2989
2990
2991
 *----------------------------------------------------------------------
 */

void
TclProcCleanupProc(
    register Proc *procPtr)	/* Procedure to be deleted. */
{

    Tcl_Obj *bodyPtr = procPtr->bodyPtr;


    Tcl_HashEntry *hePtr = NULL;
    CmdFrame *cfPtr = NULL;
    Interp *iPtr = procPtr->iPtr;

    if (bodyPtr != NULL) {
	Tcl_DecrRefCount(bodyPtr);
    }


    ProcCompiledLocalsFree(procPtr->firstLocalPtr);
















    ckfree(procPtr);

    /*
     * TIP #280: Release the location data associated with this Proc
     * structure, if any. The interpreter may not exist (For example for
     * procbody structures created by tbcload.
     */
2200
2201
2202
2203
2204
2205
2206











































































2207
2208
2209
2210
2211
2212
2213
	}
	ckfree(cfPtr->line);
	cfPtr->line = NULL;
	ckfree(cfPtr);
    }
    Tcl_DeleteHashEntry(hePtr);
}












































































/*
 *----------------------------------------------------------------------
 *
 * TclUpdateReturnInfo --
 *
 *	This function is called when procedures return, and at other points







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
	}
	ckfree(cfPtr->line);
	cfPtr->line = NULL;
	ckfree(cfPtr);
    }
    Tcl_DeleteHashEntry(hePtr);
}


/*
 *----------------------------------------------------------------------
 *
 * ProcCompiledLocalsFree --
 *
 *	This function does all the real work of freeing up a CompiledLocal
 *	linked list.
 *
 * Results:
 *	None.
 *
 * Side effects:
 *	Memory gets freed.
 *
 *----------------------------------------------------------------------
 */

static void
ProcCompiledLocalsFree(
    CompiledLocal *localPtr)
{
    CompiledLocal *nextLocalPtr = localPtr;
    Tcl_ResolvedVarInfo *resVarInfo;
    NamedGroupEntry *entryPtr, *nextEntryPtr;

    while (nextLocalPtr != NULL) {
	localPtr = nextLocalPtr;
	nextLocalPtr = nextLocalPtr->nextPtr;

	resVarInfo = localPtr->resolveInfo;
	if (resVarInfo) {
	    if (resVarInfo->deleteProc) {
		resVarInfo->deleteProc(resVarInfo);
	    } else {
		ckfree(resVarInfo);
	    }
	}

	if (localPtr->defValuePtr != NULL) {
	    Tcl_DecrRefCount(localPtr->defValuePtr);
	}

	if (localPtr->argSpecPtr != NULL) {
	    entryPtr = localPtr->argSpecPtr->firstNamedEntryPtr;

	    while (entryPtr != NULL) {
		nextEntryPtr = entryPtr->nextPtr;

		if (entryPtr->valuePtr) {
		    Tcl_DecrRefCount(entryPtr->valuePtr);
		}

		ckfree(entryPtr);
		entryPtr = nextEntryPtr;
	    }

	    if (localPtr->argSpecPtr->namedHashTable != NULL) {
		Tcl_HashTable *hTablePtr;
		hTablePtr = localPtr->argSpecPtr->namedHashTable;
		Tcl_DeleteHashTable(hTablePtr);
		ckfree(hTablePtr);
	    }

	    if (localPtr->argSpecPtr->upvarLevelPtr != NULL) {
		Tcl_DecrRefCount(localPtr->argSpecPtr->upvarLevelPtr);
	    }

	    ckfree(localPtr->argSpecPtr);
	}

	ckfree(localPtr);
    }
}

/*
 *----------------------------------------------------------------------
 *
 * TclUpdateReturnInfo --
 *
 *	This function is called when procedures return, and at other points

Changes to generic/tclVar.c.

4407
4408
4409
4410
4411
4412
4413















































4414
4415
4416
4417
4418
4419
4420

    result = ObjMakeUpvar(interp, framePtr, part1Ptr, part2, 0,
	    localNamePtr, flags, -1);
    Tcl_DecrRefCount(part1Ptr);
    Tcl_DecrRefCount(localNamePtr);
    return result;
}
















































/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetVariableFullName --
 *
 *	Given a Tcl_Var token returned by Tcl_FindNamespaceVar, this function







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467

    result = ObjMakeUpvar(interp, framePtr, part1Ptr, part2, 0,
	    localNamePtr, flags, -1);
    Tcl_DecrRefCount(part1Ptr);
    Tcl_DecrRefCount(localNamePtr);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * TclUpvarForExtArg --
 *
 *	This function links one variable to another. It is used during
 *	initialization of proc variables which use the upvar extended
 *	argument specification.
 *
 * Results:
 *	A standard Tcl completion code. If an error occurs then an error
 *	message is left in the interp's result.
 *
 * Side effects:
 *	The variable in upper frame whose name is given by varName becomes
 *	accessible under the name localNameStr, so that references to
 *	localNameStr are redirected to the other variable like a symbolic
 *	link.
 *
 *----------------------------------------------------------------------
 */
int
TclUpvarForExtArg(
    Tcl_Interp *interp,		/* Command interpreter in which varName is to
				 * be looked up. */
    Tcl_Obj *frameNamePtr,	/* Name of the frame containing the source
				 * variable. */
    Tcl_Obj *varNamePtr,	/* Name of variable in interp to link to. */
    const char *localNameStr)	/* Name of link variable. */
{
    int result;
    CallFrame *framePtr;
    Tcl_Obj *localNamePtr;

    if (TclObjGetFrame(interp, frameNamePtr, &framePtr) == -1) {
	return TCL_ERROR;
    }

    localNamePtr = Tcl_NewStringObj(localNameStr, -1);
    Tcl_IncrRefCount(localNamePtr);

    result = ObjMakeUpvar(interp, framePtr, varNamePtr, NULL, 0,
	    localNamePtr, 0, -1);
    Tcl_DecrRefCount(localNamePtr);
    return result;
}

/*
 *----------------------------------------------------------------------
 *
 * Tcl_GetVariableFullName --
 *
 *	Given a Tcl_Var token returned by Tcl_FindNamespaceVar, this function

Changes to tests/apply.test.

45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
} {1 {argument with no name} {argument with no name
    (parsing lambda expression "{{}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.3 {malformed lambda} {
    set lambda [list {{a b c}} boo]
    list [catch {apply $lambda} msg] $msg $::errorInfo
} {1 {too many fields in argument specifier "a b c"} {too many fields in argument specifier "a b c"
    (parsing lambda expression "{{a b c}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.4 {malformed lambda} {
    set lambda [list a(1) boo]
    list [catch {apply $lambda} msg] $msg $::errorInfo
} {1 {formal parameter "a(1)" is an array element} {formal parameter "a(1)" is an array element







|







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
} {1 {argument with no name} {argument with no name
    (parsing lambda expression "{{}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.3 {malformed lambda} {
    set lambda [list {{a b c}} boo]
    list [catch {apply $lambda} msg] $msg $::errorInfo
} {1 {unknown argument option "b" or too many fields in argument specifier "a b c"} {unknown argument option "b" or too many fields in argument specifier "a b c"
    (parsing lambda expression "{{a b c}} boo")
    invoked from within
"apply $lambda"}}
test apply-2.4 {malformed lambda} {
    set lambda [list a(1) boo]
    list [catch {apply $lambda} msg] $msg $::errorInfo
} {1 {formal parameter "a(1)" is an array element} {formal parameter "a(1)" is an array element

Changes to tests/info.test.

36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

test info-1.1 {info args option} {
    proc t1 {a bbb c} {return foo}
    info args t1
} {a bbb c}
test info-1.2 {info args option} {
    proc t1 {{a default1} {bbb default2} {c default3} args} {return foo}
    info a t1
} {a bbb c args}
test info-1.3 {info args option} {
    proc t1 "" {return foo}
    info args t1
} {}
test info-1.4 {info args option} -body {
    catch {rename t1 {}}







|







36
37
38
39
40
41
42
43
44
45
46
47
48
49
50

test info-1.1 {info args option} {
    proc t1 {a bbb c} {return foo}
    info args t1
} {a bbb c}
test info-1.2 {info args option} {
    proc t1 {{a default1} {bbb default2} {c default3} args} {return foo}
    info args t1
} {a bbb c args}
test info-1.3 {info args option} {
    proc t1 "" {return foo}
    info args t1
} {}
test info-1.4 {info args option} -body {
    catch {rename t1 {}}
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
unset functions msg

test info-21.1 {miscellaneous error conditions} -returnCodes error -body {
    info
} -result {wrong # args: should be "info subcommand ?arg ...?"}
test info-21.2 {miscellaneous error conditions} -returnCodes error -body {
    info gorp
} -result {unknown or ambiguous subcommand "gorp": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.3 {miscellaneous error conditions} -returnCodes error -body {
    info c
} -result {unknown or ambiguous subcommand "c": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.4 {miscellaneous error conditions} -returnCodes error -body {
    info l
} -result {unknown or ambiguous subcommand "l": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.5 {miscellaneous error conditions} -returnCodes error -body {
    info s
} -result {unknown or ambiguous subcommand "s": must be args, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}

##
# ### ### ### ######### ######### #########
## info frame

## Helper
# For the more complex results we cut the file name down to remove path







|


|


|


|







674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
unset functions msg

test info-21.1 {miscellaneous error conditions} -returnCodes error -body {
    info
} -result {wrong # args: should be "info subcommand ?arg ...?"}
test info-21.2 {miscellaneous error conditions} -returnCodes error -body {
    info gorp
} -result {unknown or ambiguous subcommand "gorp": must be args, argspec, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.3 {miscellaneous error conditions} -returnCodes error -body {
    info c
} -result {unknown or ambiguous subcommand "c": must be args, argspec, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.4 {miscellaneous error conditions} -returnCodes error -body {
    info l
} -result {unknown or ambiguous subcommand "l": must be args, argspec, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}
test info-21.5 {miscellaneous error conditions} -returnCodes error -body {
    info s
} -result {unknown or ambiguous subcommand "s": must be args, argspec, body, class, cmdcount, commands, complete, coroutine, default, errorstack, exists, frame, functions, globals, hostname, level, library, loaded, locals, nameofexecutable, object, patchlevel, procs, script, sharedlibextension, tclversion, or vars}

##
# ### ### ### ######### ######### #########
## info frame

## Helper
# For the more complex results we cut the file name down to remove path
2410
2411
2412
2413
2414
2415
2416
2417

























































































2418
2419
2420
2421
} -body {
    demo
} -cleanup {
    unset -nocomplain body
    rename demo {}
    rename probe {}
} -result 3


























































































# cleanup
catch {namespace delete test_ns_info1 test_ns_info2}
::tcltest::cleanupTests
return








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
} -body {
    demo
} -cleanup {
    unset -nocomplain body
    rename demo {}
    rename probe {}
} -result 3

# -------------------------------------------------------------------------
# info argspec
test info-40-0 {info argspec missing type} -body {
    info argspec
} -returnCodes error -result {wrong # args: should be "info argspec type ..."}
test info-40-1 {info argspec proc with old-style proc} {
    proc p { a { b 1 } { c {} } } {}
    list [info argspec proc p a] [info argspec proc p b] [info argspec proc p c]
} {{} {-default 1} {-default {}}}
test info-40-2 {info argspec proc missing procname} -body {
    info argspec proc
} -returnCodes error -result {wrong # args: should be "info argspec proc procname ?arg?"}
test info-40-3 {info argspec proc too many args} -body {
    info argspec proc proc arg foo
} -returnCodes error -result {wrong # args: should be "info argspec proc procname ?arg?"}
test info-40-4 {info argspec proc unknown proc} -body {
    info argspec proc fooproc x
} -returnCodes error -result {"fooproc" isn't a procedure}
test info-40-5 {info argspec proc unknown arg} -body {
    proc p {a} {}
    info argspec proc p x
} -returnCodes error -result {proc "p" doesn't have an argument "x"}
test info-40-6 {info argspec proc on local variable} -body {
    proc p {a} {set x 0}
    info argspec proc p x
} -returnCodes error -result {proc "p" doesn't have an argument "x"}
test info-40-7 {info argspec proc with new-style proc} {
    proc p {
	{ a -default 2 }
	{ b -upvar #1 }
	{ c -required 0 }
    } {}
    list [info argspec proc p a] [info argspec proc p b] [info argspec proc p c]
} {{-default 2} {-upvar #1} {-required 0}}
test info-40-8 {info argspec proc with multiple named args} {
    proc p {
	{ a -default 0 -name val -switch {flag1 { flag2 22 }} -name {val2}}
    } {}
    info argspec proc p a
} {-default 0 -name {val val2} -switch {flag1 {flag2 22}}}
test info-40-9 {info argspec proc without arg} {
    proc p {
	{ a -default 2 }
	{ b -name {B B1} -required 1 }
	args
    } {}
    info argspec proc p
} {{a -default 2} {b -required 1 -name {B B1}} args}
test info-40-10 {info argspec unknown type} -body {
    info argspec foo
} -returnCodes error -result {bad type "foo": must be proc, lambda, constructor, method, objmethod, or specifiers}
test info-40-11 {info argspec lambda} {
    set l [list {{a -name A} {b -default 5}} {list $a $b}]
    list [info argspec lambda $l] [info argspec lambda $l a]
} {{{a -name A} {b -default 5}} {-name A}}
test info-40-12 {info argspec lambda not a lambda} -body {
    proc p {} {}
    info argspec lambda p
} -returnCodes error -result {can't interpret "p" as a lambda expression}
test info-40-13 {info argspec specifiers} {
    info argspec specifiers
} {-default -name -required -switch -upvar}
test info-40-14 {info argspec objmethod} -setup {
    oo::object create foo
} -body {
    oo::objdefine foo method bar {a {b B} {c -switch {{C 1}} -default 0}} {}
    list [info argspec objmethod foo bar] [info argspec objmethod foo bar c]
} -cleanup {
    foo destroy
} -result {{a {b -default B} {c -default 0 -switch {{C 1}}}} {-default 0 -switch {{C 1}}}}
test info-40-15 {info argspec constructor} -setup {
    oo::class create testClass {
	constructor {{a -default 0 -name A}} { }
    }
} -body {
    list [info argspec constructor testClass] [info argspec constructor testClass a]
} -cleanup {
    testClass destroy
} -result {{{a -default 0 -name A}} {-default 0 -name A}}
test info-40-16 {info argspec method} -setup {
    oo::class create testClass {
	method m {a {b -required 0}} { }
    }
} -body {
    list [info argspec method testClass m] [info argspec method testClass m b]
} -cleanup {
    testClass destroy
} -result {{a {b -required 0}} {-required 0}}

# cleanup
catch {namespace delete test_ns_info1 test_ns_info2}
::tcltest::cleanupTests
return

Changes to tests/oo.test.

3761
3762
3763
3764
3765
3766
3767


































































3768
3769
3770
3771
3772
3773
3774
} {}
test oo-35.4 {Bug 593baa032c: mixins list teardown} {
    # Bug makes this crash, especially with mem-debugging on
    oo::class create B {}
    oo::class create D {mixin B}
    namespace eval [info object namespace D] [list [namespace which B] destroy]
} {}




































































cleanupTests
return

# Local Variables:
# mode: tcl







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
} {}
test oo-35.4 {Bug 593baa032c: mixins list teardown} {
    # Bug makes this crash, especially with mem-debugging on
    oo::class create B {}
    oo::class create D {mixin B}
    namespace eval [info object namespace D] [list [namespace which B] destroy]
} {}

# TIP457-related tests
test oo-36.1 {OO/CloneProcedureMethod with extended argspec} {
    oo::object create foo
    oo::objdefine foo {
	method p { {x -default off -switch {{ON on}}} } {
	    lappend ::result [self object] >$x<
	}
    }
    set result {}
    foo p; foo p -ON
    oo::copy foo bar
    bar p; bar p -ON
    foo destroy; bar destroy
    set result
} {::foo >off< ::foo >on< ::bar >off< ::bar >on<}
catch {foo destroy}
catch {bar destroy}

test oo-36.2 {OO/clonedBody with extended argspec} -setup {
    oo::class create ArbitraryClass {export eval}
} -body {
    ArbitraryClass create a
    a eval {proc foo {a {n -name num -default 3}} {
	return [string repeat $a $n]
    }}
    set result [a eval {foo A -num 2}]
    oo::copy a b
    lappend result [b eval {foo B -num 4}] [b eval {foo C}]
} -cleanup {
    ArbitraryClass destroy
} -result {AA BBBB CCC}

test oo-36.3 {"info object definition" with extended argspec} -setup {
    oo::object create foo
} -body {
    oo::objdefine foo method bar {a {b B} {c -switch {{C 1}} -default 0}
	{d -upvar 1} args} {body}
    set result [info object definition foo bar]
} -cleanup {
    foo destroy
} -result {{a {b B} {c -default 0 -switch {{C 1}}} {d -upvar 1} args} body}

test oo-36.4 {"info class definition" with extended argspec} -setup {
    oo::class create foo
} -body {
    oo::define foo method bar {a {b 8} {c -name C} {d -name D -upvar 1}
	args} {body}
    set result [info class definition foo bar]
} -cleanup {
    foo destroy
} -result {{a {b 8} {c -name C} {d -upvar 1 -name D} args} body}

test oo-36.5 {OO constructor with extended argspec} {
    oo::class create testClass {
	constructor {{a -default 0 -name A}} {
	    global result
	    lappend result [self object] $a
	}
    }
    set result {}
    testClass create foo1
    testClass create foo2 -A 5
    testClass destroy
    return $result
} {::foo1 0 ::foo2 5}


cleanupTests
return

# Local Variables:
# mode: tcl

Added tests/proc-enh.test.

























































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# Commands covered:  proc
#
# This file contains a collection of tests for one or more of the Tcl built-in
# commands.  Sourcing this file into Tcl runs the tests and generates output
# for errors.  No output means no errors were found.
#
# See the file "license.terms" for information on usage and redistribution of
# this file, and for a DISCLAIMER OF ALL WARRANTIES.

if {[lsearch [namespace children] ::tcltest] == -1} {
    package require tcltest
    namespace import -force ::tcltest::*
}

testConstraint procbodytest [expr {![catch {package require procbodytest}]}]

# proc-enh-1.x: error while parsing argspec
test proc-enh-1.1 {argspec parsing error: unexpected fields number} {
    list [catch {
	proc p {{a -default 1 -name}} {}
    } msg] $msg $errorCode
} {1 {unexpected fields number in argument specifier "a -default 1 -name"} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.2 {argspec parsing error: argument with no name} {
    list [catch {
	proc p {{}} {}
    } msg] $msg $errorCode
} {1 {argument with no name} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.3 {argspec parsing error: formal parameter is an array elt} {
    list [catch {
	proc p {a(1)} {}
    } msg] $msg $errorCode
} {1 {formal parameter "a(1)" is an array element} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.4 {argspec parsing error: formal parameter is not a simple name} {
    list [catch {
	proc p {a::b} {}
    } msg] $msg $errorCode
} {1 {formal parameter "a::b" is not a simple name} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.5 {argspec parsing error: unknown option} {
    list [catch {
	proc p {{a -foo 1}} {}
    } msg] $msg $errorCode
} {1 {unknown argument option "-foo" or too many fields in argument specifier "a -foo 1"} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.5b {argspec parsing error: unknown option} {
    list [catch {
	proc p {{a foo 1}} {}
    } msg] $msg $errorCode
} {1 {unknown argument option "foo" or too many fields in argument specifier "a foo 1"} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.6 {argspec parsing error: empty named argument} {
    list [catch {
	proc p {{a -name {""}}} {}
    } msg] $msg $errorCode
} {1 {named argument with no name} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.7 {argspec parsing error: named argument with space} {
    list [catch {
	proc p {{a -name {"a b"}}} {}
    } msg] $msg $errorCode
} {1 {named argument "a b" with unexpected space character} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.8 {argspec parsing error: empty named argument (switch)} {
    list [catch {
	proc p {{a -switch {a {}}}} {}
    } msg] $msg $errorCode
} {1 {incorrect switch value ""} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.9 {argspec parsing error: named argument with space (switch)} {
    list [catch {
	proc p {{a -switch {a {"a b" 3}}}} {}
    } msg] $msg $errorCode
} {1 {named argument "a b" with unexpected space character} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.10 {argspec parsing error: two many fields in switch} {
    list [catch {
	proc p {{a -switch {{a b c}}}} {}
    } msg] $msg $errorCode
} {1 {incorrect switch value "a b c"} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.11 {argspec parsing error: upvar with bad level} {
    list [catch {
	proc p {{a -upvar foo}} {}
    } msg] $msg $errorCode
} {1 {Invalid level "foo" for -upvar arg specifier} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.12 {argspec parsing error: upvar with bad level} {
    list [catch {
	proc p {{a -upvar -1}} {}
    } msg] $msg $errorCode
} {1 {Invalid level "-1" for -upvar arg specifier} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.13 {argspec parsing error: required with no boolean} {
    list [catch {
	proc p {{a -required foo}} {}
    } msg] $msg $errorCode
} {1 {Invalid boolean "foo" for -required arg specifier} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.14 {argspec parsing error: same arg name used twice} {
    list [catch {
	proc p {{a -name A} {b -name B} {a2 -name A}} {}
    } msg] $msg $errorCode
} {1 {named argument "A" has been used more than once in the same named group} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}
test proc-enh-1.15 {argspec parsing error: -upvar with -switch} {
    list [catch {
	proc p {{a -switch A -upvar 1}} {}
    } msg] $msg $errorCode
} {1 {-upvar can't be used with -switch} {TCL OPERATION PROC FORMALARGUMENTFORMAT}}

# proc-enh-2.x: correct usage
test proc-enh-2.1 {correct usage: -default} {
    proc p { { a -default 1 } } { list $a }
    list [p] [p 2]
} {1 2}
test proc-enh-2.2 {correct usage: -default set twice} {
    proc p { { a -default 1 -default 2 } } { return $a }; p
} {2}
test proc-enh-2.3 {correct usage: -name} {
    proc p {{a -name A} {b -name {B0 B1}} {c -name C0 -name C1}} {
	list $a $b $c
    };
    list [p -B0 5 -A 1 -C1 6] [p -C0 0 -B1 8 -A 2 -C0 3]
} {{1 5 6} {2 8 3}}
test proc-enh-2.4 {correct usage: -switch} {
    proc p {{a -switch {A}} {b -switch {B0 {B1 b1}}} {c -switch {{C0 c0}} -switch C1}} {
	list $a $b $c
    };
    list [p -B1 -C1 -A] [p -A -C0 -B0]
} {{A b1 C1} {A B0 c0}}
test proc-enh-2.5 {correct usage: -switch + -name} {
    proc p {{v -name val -switch {{low 0} {high 9}}}} { return $v }
    list [p -low] [p -val 5] [p -high]
} {0 5 9}
test proc-enh-2.6 {correct usage: -required} {
    proc p {{a -required 0}} {
	if {[info exists a]} { return $a } else { return unset }
    };
    list [p] [p 2]
} {unset 2}
test proc-enh-2.7 {correct usage: -required + -switch} {
    proc p {{a -required 0 -switch dbg}} {
	if {[info exists a]} { return $a } else { return unset }
    };
    list [p] [p -dbg]
} {unset dbg}
test proc-enh-2.8 {correct usage: -upvar} {
    proc p {{a -upvar #0}} { incr a; return $a }
    proc p2 {{a -upvar 2}} { incr a; return $a }
    proc p3 {name} { p2 $name }
    set i 5;
    if [info exists j] { unset j }
    list [p i] [p j] [p3 i] [list $i $j]
} {6 1 7 {7 1}}
test proc-enh-2.9 {correct usage: -upvar + -name} {
    proc p {{a -upvar 1 -name A} {b -name B -upvar 1}} {
	incr a; incr b; list $a $b
    }
    set i 5;
    if [info exists j] { unset j }
    list [p -B j -A i] [p -A j -B i] [list $i $j]
} {{6 1} {2 7} {7 2}}
test proc-enh-2.10 {correct usage: end of named group} {
    proc p {{a -name A -default 0} {b -default 1 -name B} args} {
	list $a $b $args
    }
    list [p foo] [p -B 3 -- -A 5]
} {{0 1 foo} {0 3 {-A 5}}}
test proc-enh-2.11 {correct usage: -upvar inside a named group (not last one)} {
    proc p {{v -name var -upvar 1} {i -default 1 -name incr}} {
	incr v $i
    }
    if [info exists i] {unset i}
    list [p -var i -incr 2] [p -incr 3 -var i] [p -var i]
} {2 5 6}
test proc-enh-2.12 {correct usage: -upvar inside a named group + end-of-option marker} {
    proc p {{v -name var -upvar 1} {i -default 1 -name incr} args} {
	incr v $i; list $v $args
    }
    set i 0
    list [p -var i] [p -var i -- -incr 3] [p -var i -- a b c] [p -var i -- -- abc]
} {{1 {}} {2 {-incr 3}} {3 {a b c}} {4 {-- abc}}}
test proc-enh-2.13 {correct usage: two distinct named group} {
    proc p {{a -switch A -default 0} {b -switch B -default 0} c {d -switch D -default 0} {e -switch E -default 0}} {
	list $a $b $c $d $e
    }
    list [p -B 5 -E -D] [p -- foo -E]
} {{0 B 5 D E} {0 0 foo 0 E}}
test proc-enh-2.14 {correct usage: named arg without required is optionnal} {
    proc p {{a -name A}} {
	if {[info exists a]} { return $a } else { return unset }
    };
    list [p] [p -A 2]
} {unset 2}
test proc-enh-2.15 {correct usage: fixed number of arguments after named group, automatically ended} {
    proc p {{a -name A -default 0} {b -name B -default 0} c} {
	list $a $b $c
    };
    set l [list -Z 1]
    list [p -2] [p -A 1 -5] [p -- -3] [p --]
} {{0 0 -2} {1 0 -5} {0 0 -3} {0 0 --}}

# proc-enh-3.x: wrong # args
test proc-enh-3.1 {wrong # args: -name arg without value} {
    proc p {{a -name A} {b -name B -default 1}} { }
    list [catch { p -A } msg] $msg $errorCode
} {1 {wrong # args: should be "p ?|-A a|? ?|-B b|?"} {TCL WRONGARGS}}
test proc-enh-3.2 {wrong # args: named group ended by an arg with leading dash} {
    proc p {{a -name A} {b -name B} args} { }
    list [catch { p -b Z -5 } msg] $msg $errorCode
} {1 {wrong # args: should be "p ?|-A a|? ?|-B b|? ?arg ...?"} {TCL WRONGARGS}}
test proc-enh-3.3 {wrong # args: named group followed by too many options} {
    proc p {{a -name A -default 1} b} { }
    list [catch { p -- 5 6 } msg] $msg $errorCode
} {1 {wrong # args: should be "p ?|-A a|? b"} {TCL WRONGARGS}}
test proc-enh-3.4 {wrong # args: required named group with name+switch} {
    proc p {{a -name A -switch {A0 A1} -required 1}} { }
    list [catch { p } msg] $msg $errorCode
} {1 {wrong # args: should be "p |-A a|-A0|-A1|"} {TCL WRONGARGS}}
test proc-enh-3.5 {wrong # args: required named group with upvar} {
    proc p {{a -name A -upvar 1 -required 1} {b -upvar 1}} { }
    list [catch { p } msg] $msg $errorCode
} {1 {wrong # args: should be "p |-A &a&| &b&"} {TCL WRONGARGS}}
test proc-enh-3.6 {wrong # args: two distinct named group, specified in wrong order} {
    proc p {{a -switch A} {b -switch B} c {d -switch D} {e -switch E}} { }
    list [catch { p -E 1 } msg] $msg $errorCode
} {1 {wrong # args: should be "p ?|-A|? ?|-B|? c ?|-D|? ?|-E|?"} {TCL WRONGARGS}}

# proc-enh-4.x: errors during call
test proc-enh-4.1 {errors during call: -upvar read access with non-existing arg} {
    proc p {{a -upvar 1}} { return $a }
    if [info exists v] { unset v}
    list [catch { p v } msg] $msg $errorCode
} {1 {can't read "a": no such variable} {TCL READ VARNAME}}
test proc-enh-4.2 {errors during call: -upvar read access with non-existing arg + -name} {
    proc p {{a -upvar 1 -name A}} { return $a }
    if [info exists v] { unset v}
    list [catch { p -A v } msg] $msg $errorCode
} {1 {can't read "a": no such variable} {TCL READ VARNAME}}

# proc-enh-5.x: precompiled
test proc-enh-5.1 {precompiled: inconsistent arg default value} -body {
    proc p {x y {z -default 2}} { }
    procbodytest::proc t {x y {z ZZ}} p
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has argument spec inconsistent with precompiled body}
test proc-enh-5.2 {precompiled: inconsistent arg spec} -body {
    proc p {x y {z -name z}} { }
    procbodytest::proc t {x y {z -name ZZ}} p
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has argument spec inconsistent with precompiled body}
test proc-enh-5.3 {precompiled: with upvar arg} -body {
    proc p {x {y -upvar 1} z} { }
    procbodytest::proc t {x {y -upvar 1} z} p
} -constraints procbodytest -result {}

# proc-enh-6.x: apply/lambda
test proc-enh-6.1 {apply/lambda: wrong args} {
    set lambda [list {{a -name A -switch A1 -required 1} {b -default B0 -name B}} {list $a $b}]
    list [catch {
	apply $lambda
    } msg] $msg $errorCode
} {1 {wrong # args: should be "apply lambdaExpr |-A a|-A1| ?|-B b|?"} {TCL WRONGARGS}}
test proc-enh-6.2 {apply/lambda: correct usage} {
    set lambda [list {{a -name A -switch A1} {b -default B0 -name B}} {list $a $b}]
    list [apply $lambda -A1] [apply $lambda -B 4 -A 8]
} {{A1 B0} {8 4}}

# cleanup
::tcltest::cleanupTests
return

# Local Variables:
# mode: tcl
# fill-column: 78
# End:

Changes to tests/proc-old.test.

274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    list [catch {proc tproc {{} y} {return foo}} msg] $msg
} {1 {argument with no name}}
test proc-old-5.6 {error conditions} {
    list [catch {proc tproc {{} y} {return foo}} msg] $msg
} {1 {argument with no name}}
test proc-old-5.7 {error conditions} {
    list [catch {proc tproc {{x 1 2} y} {return foo}} msg] $msg
} {1 {too many fields in argument specifier "x 1 2"}}
test proc-old-5.8 {error conditions} {
    catch {return}
} 2
proc tproc {} {
    set a 22
    global a
}







|







274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
    list [catch {proc tproc {{} y} {return foo}} msg] $msg
} {1 {argument with no name}}
test proc-old-5.6 {error conditions} {
    list [catch {proc tproc {{} y} {return foo}} msg] $msg
} {1 {argument with no name}}
test proc-old-5.7 {error conditions} {
    list [catch {proc tproc {{x 1 2} y} {return foo}} msg] $msg
} {1 {unknown argument option "1" or too many fields in argument specifier "x 1 2"}}
test proc-old-5.8 {error conditions} {
    catch {return}
} 2
proc tproc {} {
    set a 22
    global a
}

Changes to tests/proc.test.

260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    }
    set rv [p P Q R]
    procbodytest::proc t {x y z} p
    lappend rv [t S T U]
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter 2 is inconsistent with precompiled body}
test proc-4.6 {TclCreateProc, procbody obj, inconsistent arg default type} -body {
    proc p {x y z} {
	set v [join [list $x $y $z]]
	set w [string tolower $v]
	return "$v:$w"
    }
    set rv [p P Q R]
    procbodytest::proc t {x y {z Z}} p
    lappend rv [t S T U]
} -returnCodes error -constraints procbodytest -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter 2 is inconsistent with precompiled body}
test proc-4.7 {TclCreateProc, procbody obj, inconsistent arg default value} -body {
    proc p {x y {z Z}} {
	set v [join [list $x $y $z]]
	set w [string tolower $v]
	return "$v:$w"
    }
    set rv [p P Q R]
    procbodytest::proc t {x y {z ZZ}} p
    lappend rv [t S T U]
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has default value inconsistent with precompiled body}
test proc-4.8 {TclCreateProc, procbody obj, no leak on multiple iterations} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
    proc px x {
	set y [string tolower $x]







|












|












|







260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
    }
    set rv [p P Q R]
    procbodytest::proc t {x y z} p
    lappend rv [t S T U]
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has argument spec inconsistent with precompiled body}
test proc-4.6 {TclCreateProc, procbody obj, inconsistent arg default type} -body {
    proc p {x y z} {
	set v [join [list $x $y $z]]
	set w [string tolower $v]
	return "$v:$w"
    }
    set rv [p P Q R]
    procbodytest::proc t {x y {z Z}} p
    lappend rv [t S T U]
} -returnCodes error -constraints procbodytest -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has argument spec inconsistent with precompiled body}
test proc-4.7 {TclCreateProc, procbody obj, inconsistent arg default value} -body {
    proc p {x y {z Z}} {
	set v [join [list $x $y $z]]
	set w [string tolower $v]
	return "$v:$w"
    }
    set rv [p P Q R]
    procbodytest::proc t {x y {z ZZ}} p
    lappend rv [t S T U]
} -constraints procbodytest -returnCodes error -cleanup {
    catch {rename p ""}
    catch {rename t ""}
} -result {procedure "t": formal parameter "z" has argument spec inconsistent with precompiled body}
test proc-4.8 {TclCreateProc, procbody obj, no leak on multiple iterations} -setup {
    proc getbytes {} {
	set lines [split [memory info] "\n"]
	lindex $lines 3 3
    }
    proc px x {
	set y [string tolower $x]