chiark / gitweb /
6ce038a315c9c10422d0baee7a71299c2a8ab263
[chiark-tcl.git] / base / tcmdifgen
1 #!/usr/bin/perl -w
2
3 # code generator to help with writing Tcl extensions
4 # Copyright 2006-2012 Ian Jackson
5 #
6 # This program is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU General Public License as
8 # published by the Free Software Foundation; either version 2 of the
9 # License, or (at your option) any later version.
10 #
11 # This program is distributed in the hope that it will be useful, but
12 # WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 # General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this library; if not, see <http://www.gnu.org/licenses/>.
18
19
20 # Input format is line-based, ws-significant, offside rule (some kind
21 #  of, anyway).
22 #
23 #  Type TYPE:       C-TYPE-DECLARATOR
24 #     Defines TYPE as a type (for arguments and return values)
25 #     which corresponds to the C type specified.  C-TYPE-DECLARATOR
26 #     must contain one `@' where the identifier would go.
27 #     The type may contain allocated memory, etc., in which case
28 #     `Init' and `Fini' must be used.
29 #
30 #     TYPE may be either TYPENAME or TYPENAME(ARGS) - in this case,
31 #     ARGS should be C argument declarations as for in a function
32 #     prototype, of extra arguments for the application-supplied
33 #     parser/returner functions.  Each time a TYPE is used elsewhere,
34 #     the ARGS should be the actual arguments to pass, and will be
35 #     textually copied into the calls to the parser/returner
36 #     functions.
37 #
38 #     `Type' causes declarations in the .h file of these functions:
39 #        int cht_pat_TYPENAME(Tcl_Interp*, Tcl_Obj *obj, C-TYPE *val, ARGS);
40 #        Tcl_Obj *cht_ret_TYPENAME(Tcl_Interp*, C-TYPE val, ARGS);
41 #
42 #     cht_pat_... must attempt to parse obj into the appropriate type.
43 #     val will already have been initialised with `Init' statements if
44 #     relevant.  Whether cht_pat_... fails or succeeds it may allocate
45 #     memory into the object and must leave the object valid (for
46 #     `Fini').
47 #
48 #     cht_ret_... must convert the value back to a new Tcl_Obj.  It may
49 #     not fail.
50 #
51 #  Init TYPENAME    C-STATEMENTS
52 #     Provides some statements which are used to initialise a variable
53 #     of type TYPENAME.  C-STATEMENTS should contain one or more `@',
54 #     which will be replaced by the actual variable name.  The
55 #     variable will have been declared with the C declarator specified
56 #     with `Type'.  C-STATEMENTS may not fail or longjmp, and they may
57 #     not allocate memory or other resources.  If no `Init' is
58 #     supplied then there is no invariant (so no `Fini' may be
59 #     supplied either, and the type is `flat' - no memory, external
60 #     refs, etc.)
61 #
62 #  Fini TYPENAME    C-STATEMENTS
63 #     Provides some statements (like `Init') which are used to free a
64 #     variable of type TYPENAME.  The variable will already have been
65 #     initialised with the `Init' statements, and may have been
66 #     modified since by application per-type or per-command code.  Its
67 #     invariant will be satisfied before C-STATEMENTS.  Afterwards the
68 #     invariant may or may not be satisfied, but it may not have any
69 #     memory or other resources allocated.  C-STATEMENTS may not fail
70 #     or longjmp.
71 #
72 #  H-Include    C-INCLUDE-SPECIFIER
73 #     Arranges for generated .h files to #include the specified
74 #     file.  C-INCLUDE-SPECIFIER should include the <..> or "..".
75 #
76 #  Table [*]TABLENAME C-ENTRY-TYPE
77 #     Starts a table of commands or subcommands.  The generated .h
78 #     will contain a definition of C-ENTRY-TYPE containing
79 #         const char *name;
80 #         Tcl_ObjCmdProc *func;
81 #     and the generated .c will contain
82 #         const C-ENTRY-TYPE C-ARRAY-NAME[];
83 #     where C-ARRAY-NAME is TABLENAME, with `_entries' appended
84 #     and `cht_' prepended.  The entries are indented one level (one
85 #     or more spaces) and look like this:
86 #        ENTRYNAME [ C-EXTRA-ENTRY-VALUES ]
87 #            FORMALARGNAME   TYPE
88 #            ...
89 #          [ =>  RESULT-TYPE ]
90 #     This will cause the declaration of
91 #        int cht_do_TABLENAME_ENTRYNAME(ClientData cd, Tcl_Interp *ip,
92 #                                   FORMAL-ARGUMENTS, RESULT-C-TYPE*);
93 #     which is the procedure which the application must supply to
94 #     implement the function.  If the `=> RESULT-TYPE' is omitted, so
95 #     is the result argument to the function.  Each argument to the
96 #     function is of the C type corresponding to the specified type.
97 #     TYPE may be `...', in which case the C function will be passed
98 #     two args (int objc, Tcl_Obj *const *objv) for the remaining
99 #     arguments.
100 #
101 #     The cht_do_... function should not eat any memory associated with
102 #     the arguments.  The result buffer (if any) will be initialised
103 #     using the `Init' and should on success contain the relevant
104 #     result.  On failure it should leave the result unmodified (or at
105 #     least, not in need of freeing).
106 #
107 #     As an alternative, the arguments can be replaced with just
108 #            dispatch(TYPE-ARGS-FOR-ENUM)
109 #     which is a shorthand for
110 #            subcmd   enum(TYPE-ARGS-FOR-ENUM)
111 #            args     ...
112 #     and also generates and uses a standard dispatch function.
113 #
114 #     There will be an entry in C-ARRAY-NAME for every table entry.
115 #     The name will be ENTRYNAME, and the func will be a function
116 #     suitable for use as a Tcl command procedure, which parses the
117 #     arguments, processes the command, and sets any result, as
118 #     applicable.
119 #
120 #     `*' should be used if the table name is not useful for error
121 #     messages.  It suppresses `TABLENAME ' from the front of the
122 #     autogenerated argument parsing error strings.
123 #
124 #  EntryExtra C-ENTRY-TYPE
125 #     Introduces a section of additional C code which will be inserted
126 #     into the definition of C-ENTRY-TYPE by `Table'.  The C
127 #     code, which follows on several indented lines, should be
128 #     structure member definitions.
129 #
130 #     When EntryExtra is used, in the corresponding Table, each
131 #     ENTRYNAME should be followed on the same line by whitespace and
132 #     EXTRA-VALUES; the EXTRA-VALUES are used as initialisers for the
133 #     additional structure elements.
134 #
135 #  NoEntryDefine C-ENTRY-TYPE
136 #     Prevents the definition of C-ENTRY-TYPE by Table.
137 #     The C type must be defined elsewhere.
138 #
139 #  Also expected are these functions:
140 #    void cht_setstringresult(Tcl_Interp*, const char*);
141 #        sets the Tcl result from the supplied string
142 #    int cht_pat_enum(Tcl_Interp*, Tcl_Obj*, const void **c_e_t_array,
143 #                 const void *c_e_t_return, size_t c_e_t_sz, const char *what);
144 #        scans a table of C-ENTRY-TYPEs looking for the
145 #        string matching the string supplied by the script
146 #        (as a Tcl_Obj).  On error sets the result, using
147 #        what (a noun phrase describing the type of thing).
148 #        Assumes (unportably!) that the name and func members
149 #        are in the same places no matter what the rest of
150 #        the struct contains.
151 #  and the two predefined types `int' (C `int') and `obj' (Tcl_Obj*,
152 #  unmodified.)  The corresponding definitions are in tcmdiflib.c.
153
154 use strict;
155 use IO::File;
156 use Data::Dumper;
157
158 our (%o, $oh);
159 our ($prefix, $write, $output);
160 our (%tables, %table_x, %entrytype_x);
161 our (%types, %type_init, %type_fini);
162
163 sub parse ($$);
164 sub subst_in_decl ($$;$);
165 sub subst_in ($$;$);
166 sub o ($$$);
167 sub make_decl ($$$;$);
168 sub make_decl_init ($$$$;$);
169
170 parse('builtins','DATA');
171
172 while (@ARGV) {
173     $_= shift @ARGV;
174     if (m/^\-p([-_0-9a-z]+)$/) {
175         $prefix= $1;
176         $prefix =~ y/-/_/;
177     } elsif (m/^\-w(c|h)$/) {
178         $write= $1;
179     } elsif (m/^\-o(.+)$/) {
180         $output= $1;
181     } elsif (m/^\-/) {
182         die "unknown option $_\n";
183     } else {
184         if (!defined $prefix) { $prefix= $_;  $prefix =~ s/\.[^.]+$//; }
185         my $x= new IO::File $_,'r' or die "$_: $!\n";
186         parse($_,$x);
187     }
188 }
189
190 die "must say -w<something>\n" if !defined $write;
191
192 our ($c_entry, $c_entrytype);
193 our ($c_table, $c_entryextra, $c_of);
194
195 sub zilch () {
196     undef $c_table;
197     undef $c_entryextra;
198     undef $c_of;
199 }
200
201 sub enumargs ($) {
202     my ($a) = @_;
203     $a =~ m:/(.*),: or die "invalid enum type \`$a'\n";
204     my ($a_tab, $ee_type, $estr) = ($`,$1,$');
205     if ($ee_type !~ m/^[^_]/) {
206         $ee_type= $a_tab.$ee_type;
207         $a_tab= lc($a_tab).'_entries';
208     }
209     return ($a_tab, $ee_type, $estr);
210 }
211
212 sub parse ($$) {
213     my ($wh,$f) = @_;
214     my @i;
215     while (defined($_= $f->getline)) {
216         chomp; s/\s+$//;
217         next if m/^\s*\#/;
218         next if !m/\S/;
219         while (s/\t/ ' 'x(8 - (length $`) % 8) /e) { }
220
221         s/^\s*//;
222         my $this_indent= length $&;
223         while (@i && $this_indent < $i[0]) {
224             shift @i;
225         }
226         if ($this_indent && (!@i || $this_indent > $i[0])) {
227             unshift @i, $this_indent;
228         }
229
230         if (@i==0 && m/^Table\s+(\*?)(\w+)\s+(\w+)$/) {
231             zilch();
232             $c_table= $2;
233             $table_x{$c_table}{T}= $1;
234             $table_x{$c_table}{C}= $3;
235             $entrytype_x{$3}= '' unless exists $entrytype_x{$3};
236         } elsif (@i==0 && m/^Untabled$/) {
237             zilch();
238             $c_table= '';
239         } elsif (@i==0 && m/^(C|H)\-Include\s+(\S.*)$/) {
240             o(lc $1, 30, "#include $2\n");
241         } elsif (@i==0 && m/^EntryExtra\s+(\w+)$/) {
242             zilch();
243             $c_entryextra= $1;
244         } elsif (@i==0 && m/^NoEntryDefine\s+(\w+)$/) {
245             zilch();
246             $entrytype_x{$1}= " ";
247         } elsif (@i>=1 && defined $c_entryextra) {
248             $entrytype_x{$c_entryextra} .= "  $_\n";
249         } elsif (@i==1 && m/^[a-z].*$/ && defined $c_table) {
250             if (m/^[-_0-9A-Za-z]+$/) {
251                 $c_entry= $_;
252             } elsif (m/^([-_0-9A-Za-z]+)\s+(\S.*)$/) {
253                 $c_entry= $1;
254                 $tables{$c_table}{$c_entry}{I} .= ", $2";
255             } else {
256                 badsyntax($wh,$.,"bad entry");
257             }
258             $tables{$c_table}{$c_entry}{A} = [ ];
259         } elsif (@i==2 && m/^\.\.\.\s+(\w+)$/ && defined $c_entry) {
260             $tables{$c_table}{$c_entry}{V}= $1;
261         } elsif (@i==2 && m:^dispatch\(((.*)/(.*)\,.*)\)$: && defined $c_entry) {
262             my $enumargs= $1;
263             my $subcmdtype= $2.$3;
264             $tables{$c_table}{$c_entry}{D}= $subcmdtype;
265             $tables{$c_table}{$c_entry}{V}= 'obj';
266             push @{ $tables{$c_table}{$c_entry}{A} },
267                 { N => 'subcmd', T => 'enum', A => $enumargs, O => '' };
268         } elsif (@i==2 && m/^(\??)([a-z]\w*)\s*(\S.*)/
269                  && defined $c_entry) {
270             my ($opt, $var, $typea) = ($1,$2,$3);
271             my ($type, $xtypeargs) = split_type_args($wh,$typea);
272             push @{ $tables{$c_table}{$c_entry}{A} },
273                 { N => $var, T => $type, A => $xtypeargs, O => ($opt eq '?') };
274         } elsif (@i==2 && m/^\=\>\s*(\S.*)$/ && defined $c_entry) {
275             my ($type, $xtypeargs) = split_type_args($wh,$1);
276             $tables{$c_table}{$c_entry}{R}= $type;
277             $tables{$c_table}{$c_entry}{X}= $xtypeargs;
278         } elsif (@i==0 && m/^Type\s+([^\:]+)\:\s+(\S.*)$/) {
279             my ($typenamea,$ctype)= ($1,$2);
280             $ctype .= ' @' unless $ctype =~ m/\@/;
281             my ($typename,$xtypeargs) = split_type_args($wh,$typenamea);
282             $types{$typename}= { C => $ctype, X => $xtypeargs };
283         } elsif (@i==0 && s/^Init\s+(\w+)\s+(\S.*)//) {
284             $type_init{$1}= $2;
285         } elsif (@i==0 && s/^Fini\s+(\w+)\s+(\S.*)//) {
286             $type_fini{$1}= $2;
287         } else {
288             badsyntax($wh,$., sprintf
289                       "bad directive (indent level %d)", scalar @i);
290         }
291     }
292     $f->error and die $!;
293     $f->close;
294 }
295
296 #print Dumper(\%tables),"\n";
297 #print Dumper(\%types),"\n";
298
299 foreach my $t (sort keys %types) {
300     my $type= $types{$t};
301     my $c= $type->{C};
302     my $xta= $type->{X};
303     my $decl= "int cht_pat_$t(Tcl_Interp *ip, Tcl_Obj *obj, ";
304     $decl .= subst_in_decl('*val', $c, "type $t");
305     $decl .= ", $xta",  if length $xta;
306     $decl .= ");\n";
307     o('h',160, $decl);
308
309     $decl= "Tcl_Obj *cht_ret_$t(Tcl_Interp *ip, ".subst_in_decl('val',$c);
310     $decl .= ", $xta" if length $xta;
311     $decl .= ");\n";
312     o('h',170, $decl);
313 }
314
315 foreach $c_entrytype (sort keys %entrytype_x) {
316     next if $entrytype_x{$c_entrytype} =~ m/^\s$/;
317     o('h', 20, "typedef struct $c_entrytype $c_entrytype;\n");
318     o('h', 100,
319       "struct $c_entrytype {\n".
320       "  const char *name;\n".
321       "  Tcl_ObjCmdProc *func;\n".
322       $entrytype_x{$c_entrytype}.
323       "};\n\n");
324 }
325
326 our (%dispatch_done);
327
328 foreach $c_table (sort keys %tables) {
329     my $r_table= $tables{$c_table};
330     my $x_table= $table_x{$c_table};
331     my $op_tab= '';
332
333     foreach $c_entry (sort keys %$r_table) {
334         my $c_entry_c= $c_entry; $c_entry_c =~ y/-/_/;
335         my $r_entry= $r_table->{$c_entry};
336         my $pa_decl= "int pa_${c_table}_${c_entry_c}(ClientData cd,".
337             " Tcl_Interp *ip, int objc, Tcl_Obj *const *objv)";
338         my $pa_func= "cht_do_${c_table}_${c_entry_c}";
339         if (exists $r_entry->{D}) {
340             $pa_func= "cht_dispatch_$r_entry->{D}";
341         }
342         my $do_decl= "int $pa_func(";
343         my @do_al= ('ClientData cd', 'Tcl_Interp *ip');
344         my @do_aa= qw(cd ip);
345         my $pa_init= '';
346         my $pa_argc= "  objc--; objv++;\n";
347         my $pa_vars= "  int rc;\n";
348         my $pa_body= '';
349         my $pa_rslt= '';
350         my $pa_free= '';
351         my $pa_fini= '';
352         my $any_mand= 0;
353         my $any_optl= 0;
354         my $any_eerr= 0;
355         my $any_eargc= 0;
356         my $pa_hint= '';
357         $pa_hint .= "$c_table " if length $c_table &&
358             !length $table_x{$c_table}{T};
359         $pa_hint.= $c_entry;
360         foreach my $arg (@{ $r_entry->{A} }) {
361             my $n= $arg->{N};
362             my $t= $arg->{T};
363             my $a= $arg->{A};
364             push @do_al, make_decl($n, $t, $arg->{A},
365                                    "table $c_table entry $c_entry arg $n");
366             $pa_vars .= make_decl_init("a_$n", $t, $a, \$pa_init, "pa_vars");
367             if ($arg->{O}) {
368                 $pa_hint .= " ?$n?";
369                 if ($any_mand) {
370                     $any_mand= 0;
371                     $any_eerr= 1;
372                 }
373                 $pa_body .= "  if (!objc--) goto end_optional;\n";
374                 $any_optl= 1;
375             } else {
376                 $pa_hint .= " $n";
377                 $pa_body .= "  if (!objc--) goto wrong_count_args;\n";
378                 $any_mand++;
379                 $any_eargc= 1;
380                 die if $any_optl;
381             }
382             my $paarg= "&a_$n";
383             my $pafin= '';
384             if ($t eq 'enum') {
385                 $pa_vars .= "  const void *v_$n= 0;\n";
386                 $paarg= "&v_$n";
387                 $pafin= "\n  a_$n= v_$n; ";
388                 my ($a_tab, $ee_type, $estr) = enumargs($a);
389                 $a = "cht_$a_tab, sizeof($ee_type), $estr";
390                 o('h', 210, "extern const $ee_type cht_$a_tab".'[]'.";\n");
391             }
392             if (exists $type_fini{$t}) {
393                 $pa_fini .= '  '.subst_in("a_$n", $type_fini{$t})."\n";
394             }
395             $pa_body .= "  rc= cht_pat_$t(ip, *objv++, $paarg";
396             $pa_body .= ", ".$a if length $a;
397             $pa_body .= ");$pafin if (rc) goto rc_err;\n";
398             push @do_aa, "a_$n";
399         }
400         if (exists $r_entry->{V}) {
401             $pa_hint .= " ...";
402             my $va= $r_entry->{V};
403             push @do_al, subst_in_decl("${va}c", 'int @');
404             push @do_al, subst_in_decl("${va}v", 'Tcl_Obj *const *@');
405             push @do_aa, "objc+1", "objv-1";
406         } else {
407             if (!$any_optl) {
408                 $pa_body .= "  if (objc) goto wrong_count_args;\n";
409                 $any_eargc= 1;
410             }
411         }
412         if ($any_optl) {
413             $pa_body .= "end_optional:\n";
414         }
415         if (exists $r_entry->{R}) {
416             my $t= $r_entry->{R};
417             my $xta= $r_entry->{X};
418             push @do_al, make_decl("*result", $t, "cht_do_al result");
419             $pa_vars .= make_decl_init("result", $t, $xta, \$pa_init,
420                                        "pa_vars result");
421             push @do_aa, "&result";
422             $pa_rslt .= "  Tcl_SetObjResult(ip, cht_ret_$t(ip, result";
423             $pa_rslt .= ", $xta" if length $xta;
424             $pa_rslt .= "));\n";
425         }
426         $pa_body .= "\n";
427         $pa_body .= "  rc= $pa_func(";
428         $pa_body .= join ', ', @do_aa;
429         $pa_body .= ");\n";
430         $pa_body .= "  if (rc) goto rc_err;\n";
431
432         $pa_rslt .= "  rc= TCL_OK;\n\n";
433         $pa_rslt .= "rc_err:\n";
434         
435         $pa_fini .= "  return rc;\n";
436         if ($any_eargc) {
437             $pa_fini .= "\nwrong_count_args:\n";
438             $pa_fini .= "  e=\"wrong # args: should be \\\"$pa_hint\\\"\";\n";
439             $pa_fini .= "  goto e_err;";
440             $any_eerr= 1;
441         }
442         if ($any_eerr) {
443             $pa_vars .= "  const char *e;\n";
444             $pa_fini .= "\n";
445             $pa_fini .= "e_err:\n";
446             $pa_fini .= "  cht_setstringresult(ip,e);\n";
447             $pa_fini .= "  rc= TCL_ERROR; goto rc_err;\n";
448         }
449         $pa_vars .= "\n";
450         $pa_init .= "\n" if length $pa_init;
451         $pa_fini .= "}\n\n";
452
453         my $static;
454         if (length $c_table) {
455             $static= 'static ';
456         } else {
457             $static= '';
458             o('h',90, "$pa_decl;\n");
459         }
460         o('c',100,
461           $static.$pa_decl." {\n".
462           $pa_vars.
463           $pa_init.
464           $pa_argc.
465           $pa_body.
466           $pa_rslt.
467           $pa_free.
468           $pa_fini);
469         $do_decl .= join ', ', @do_al;
470         $do_decl .= ")";
471
472         if (exists $r_entry->{D}) {
473             my $subcmdtype= $r_entry->{D};
474             if (!exists $dispatch_done{$subcmdtype}) {
475                 my $di_body='';
476                 $di_body .= "static $do_decl {\n";
477                 $di_body .= "  return subcmd->func(0,ip,objc,objv);\n";
478                 $di_body .= "}\n";
479                 o('c',50, $di_body) or die $!;
480             }
481         } else {
482             o('h',100, $do_decl.";\n") or die $!;
483         }
484         $op_tab .= sprintf("  { %-20s %-40s%s },\n",
485                            "\"$c_entry\",",
486                            "pa_${c_table}_${c_entry_c}",
487                            ($r_entry->{I}) // '');
488     }
489     if (length $c_table) {
490         my $decl= "const $x_table->{C} cht_${c_table}_entries[]";
491         o('h', 500, "extern $decl;\n");
492         o('c', 100,
493           "$decl = {\n".
494           $op_tab.
495           "  { 0 }\n".
496           "};\n\n");
497     }
498 }
499
500 o('c', 0, "#include \"$prefix.h\"\n");
501
502 o('h', 0,
503   "#ifndef INCLUDED_\U${prefix}_H\n".
504   "#define INCLUDED_\U${prefix}_H\n\n");
505
506 o('h', 999,
507   "#endif /*INCLUDED_\U${prefix}_H*/\n");
508
509 if (defined $output) {
510     $oh= new IO::File "$output.tmp", 'w' or die "$output.tmp: $!\n";
511 } else {
512     $oh= 'STDOUT';
513 }
514
515 print $oh "/* AUTOGENERATED - DO NOT EDIT */\n" or die $!;
516 foreach my $pr (sort keys %{ $o{$write} }) {
517     print $oh "\n" or die $!;
518     print $oh $o{$write}{$pr} or die $!;
519 }
520
521 die if $oh->error;
522 die $! unless $oh->close;
523
524 if (defined $output) {
525     rename "$output.tmp", $output or die $!;
526 }
527
528 sub o ($$$) {
529     my ($wh,$pr,$s) = @_;
530     $o{$wh}{sprintf "%010d", $pr} .= $s;
531 }
532
533 sub split_type_args ($$) {
534     my ($wh,$type) = @_;
535     my ($xtypeargs);
536     if ($type =~ m/^\w+$/) {
537         $xtypeargs='';
538     } elsif ($type =~ m/^(\w+)\((.+)\)$/) {
539         $type= $1;
540         $xtypeargs= $2;
541     } else {
542         badsyntax($wh,$.,"bad type name/args \`$type'\n");
543     }
544     return ($type,$xtypeargs);
545 }
546
547 sub make_decl_init ($$$$;$) {
548     my ($n, $t, $a, $initcode, $why) = @_;
549     my ($o,$init);
550     $o= make_decl($n,$t,$a,"$why _init");
551     if (exists $type_init{$t}) {
552         $init= $type_init{$t};
553         $$initcode .= "  ".subst_in("$n", $init)."\n"
554             if length $init;
555     } else {
556         $o .= ' =0';
557     }
558     return "  ".$o.";\n";
559 }
560
561 sub make_decl ($$$;$) {
562     my ($n, $t, $ta, $why) = @_;
563     my ($type, $c);
564     if ($t eq 'enum') {
565         my ($a_tab, $ee_type, $estr) = enumargs($ta);
566         $c= "const $ee_type* @";
567     } else { 
568         defined $types{$t} or die "unknown type $t ($why)\n";
569         $c= $types{$t}{C};
570     }
571     return subst_in_decl($n,$c);
572 }
573
574 sub subst_in_decl ($$;$) {
575     my ($val, $pat, $why) = @_;
576     local ($_) = subst_in($val, $pat, $why);
577     s/ *(\**) *$/$1/;
578     return $_;
579 }
580     
581 sub subst_in ($$;$) {
582     my ($val, $pat, $why) = @_;
583     $pat =~ m/\@/ or die "$pat for $val in $why ?";
584     $pat =~ s/\@/$val/g;
585     return $pat;
586 }
587
588 sub badsyntax ($$$) {
589     die "$_[0]:$_[1]: $_[2]\n";
590 }
591
592 __DATA__
593 Type int:       int
594 Type obj:       Tcl_Obj *@