3 # This script generates a set of stub files for a given
7 # Copyright (c) 1998-1999 by Scriptics Corporation.
8 # See the file "license.terms" for information on usage and redistribution
9 # of this file, and for a DISCLAIMER OF ALL WARRANTIES.
11 # $Id: genStubs.tcl,v 1.1 2007/05/18 13:35:56 dkf Exp $
13 # SOURCE: tcl/tools/genStubs.tcl, revision 1.17
16 # + Don't use _ANSI_ARGS_ macro
17 # + Remove xxx_TCL_DECLARED #ifdeffery
18 # + Use application-defined storage class specifier instead of "EXTERN"
19 # + Add "epoch" and "revision" fields to stubs table record
20 # + Remove dead code related to USE_*_STUB_PROCS (emitStubs, makeStub)
21 # + Second argument to "declare" is used as a status guard
22 # instead of a platform guard.
23 # + Use void (*reserved$i)(void) = 0 instead of void *reserved$i = NULL
24 # for unused stub entries, in case pointer-to-function and
25 # pointer-to-object are different sizes.
26 # + Allow trailing semicolon in function declarations
27 # + stubs table is const-qualified
30 package require Tcl 8.5-
32 namespace eval genStubs {
35 # The name of the entire library. This value is used to compute
36 # the USE_*_STUBS macro, the name of the init file, and others.
38 variable libraryName "UNKNOWN"
42 # An array indexed by interface name that is used to maintain
43 # the set of valid interfaces. The value is empty.
45 array set interfaces {}
49 # The name of the interface currently being defined.
51 variable curName "UNKNOWN"
55 # Storage class specifier for external function declarations.
56 # Normally "extern", may be set to something like XYZAPI
58 variable scspec "extern"
62 # The epoch and revision numbers of the interface currently being defined.
63 # (@@@TODO: should be an array mapping interface names -> numbers)
71 # An array indexed by interface name that contains the set of
72 # subinterfaces that should be defined for a given interface.
78 # This three dimensional array is indexed first by interface name,
79 # second by field name, and third by a numeric offset or the
80 # constant "lastNum". The lastNum entry contains the largest
81 # numeric offset used for a given interface.
83 # Field "decl,$i" contains the C function specification that
84 # should be used for the given entry in the stub table. The spec
85 # consists of a list in the form returned by parseDecl.
86 # Other fields TBD later.
92 # The directory where the generated files should be placed.
97 # genStubs::library --
99 # This function is used in the declarations file to set the name
100 # of the library that the interfaces are associated with (e.g. "tcl").
101 # This value will be used to define the inline conditional macro.
104 # name The library name.
109 proc genStubs::library {name} {
110 variable libraryName $name
113 # genStubs::interface --
115 # This function is used in the declarations file to set the name
116 # of the interface currently being defined.
119 # name The name of the interface.
124 proc genStubs::interface {name} {
125 variable curName $name
129 set interfaces($name) {}
130 set stubs($name,lastNum) 0
134 # genStubs::scspec --
136 # Define the storage class macro used for external function declarations.
137 # Typically, this will be a macro like XYZAPI or EXTERN that
138 # expands to either DLLIMPORT or DLLEXPORT, depending on whether
139 # -DBUILD_XYZ has been set.
141 proc genStubs::scspec {value} {
142 variable scspec $value
147 # Define the epoch number for this library. The epoch
148 # should be incrememented when a release is made that
149 # contains incompatible changes to the public API.
151 proc genStubs::epoch {value} {
152 variable epoch $value
157 # This function defines the subinterface hooks for the current
161 # names The ordered list of interfaces that are reachable through the
167 proc genStubs::hooks {names} {
171 set hooks($curName) $names
175 # genStubs::declare --
177 # This function is used in the declarations file to declare a new
181 # index The index number of the interface.
182 # status Status of the interface: one of "current",
183 # "deprecated", or "obsolete".
184 # decl The C function declaration, or {} for an undefined
187 proc genStubs::declare {index status decl} {
194 # Check for duplicate declarations, then add the declaration and
195 # bump the lastNum counter if necessary.
197 if {[info exists stubs($curName,decl,$index)]} {
198 puts stderr "Duplicate entry: $index"
200 regsub -all "\[ \t\n\]+" [string trim $decl] " " decl
201 set decl [parseDecl $decl]
203 set stubs($curName,status,$index) $status
204 set stubs($curName,decl,$index) $decl
206 if {$index > $stubs($curName,lastNum)} {
207 set stubs($curName,lastNum) $index
213 # genStubs::rewriteFile --
215 # This function replaces the machine generated portion of the
216 # specified file with new contents. It looks for the !BEGIN! and
217 # !END! comments to determine where to place the new text.
220 # file The name of the file to modify.
221 # text The new text to place in the file.
226 proc genStubs::rewriteFile {file text} {
227 if {![file exists $file]} {
228 puts stderr "Cannot find file: $file"
231 set in [open ${file} r]
232 set out [open ${file}.new w]
233 fconfigure $out -translation lf
237 if {[string match "*!BEGIN!*" $line]} {
242 puts $out "/* !BEGIN!: Do not edit below this line. */"
246 if {[string match "*!END!*" $line]} {
250 puts $out "/* !END!: Do not edit above this line. */"
251 puts -nonewline $out [read $in]
254 file rename -force ${file}.new ${file}
258 # genStubs::addPlatformGuard --
260 # Wrap a string inside a platform #ifdef.
263 # plat Platform to test.
266 # Returns the original text inside an appropriate #ifdef.
268 proc genStubs::addPlatformGuard {plat text} {
271 return "#ifdef _WIN32\n${text}#endif /* _WIN32 */\n"
274 return "#if !defined(_WIN32) /* UNIX */\n${text}#endif /* UNIX */\n"
277 return "#ifdef MAC_OSX_TCL\n${text}#endif /* MAC_OSX_TCL */\n"
280 return "#ifdef MAC_OSX_TK\n${text}#endif /* MAC_OSX_TK */\n"
283 return "#if !(defined(_WIN32) || defined(MAC_OSX_TK)) /* X11 */\n${text}#endif /* X11 */\n"
289 # genStubs::emitSlots --
291 # Generate the stub table slots for the given interface.
294 # name The name of the interface being emitted.
295 # textVar The variable to use for output.
300 proc genStubs::emitSlots {name textVar} {
303 forAllStubs $name makeSlot noGuard text {" void (*reserved$i)(void);\n"}
307 # genStubs::parseDecl --
309 # Parse a C function declaration into its component parts.
312 # decl The function declaration.
315 # Returns a list of the form {returnType name args}. The args
316 # element consists of a list of type/name pairs, or a single
317 # element "void". If the function declaration is malformed
318 # then an error is displayed and the return value is {}.
320 proc genStubs::parseDecl {decl} {
321 if {![regexp {^(.*)\((.*)\);?$} $decl all prefix args]} {
322 puts stderr "Malformed declaration: $decl"
325 set prefix [string trim $prefix]
326 if {![regexp {^(.+[ ][*]*)([^ *]+)$} $prefix all rtype fname]} {
327 puts stderr "Bad return type: $decl"
330 set rtype [string trim $rtype]
331 foreach arg [split $args ,] {
332 lappend argList [string trim $arg]
334 if {![string compare [lindex $argList end] "..."]} {
335 if {[llength $argList] != 2} {
336 puts stderr "Only one argument is allowed in varargs form: $decl"
338 set arg [parseArg [lindex $argList 0]]
339 if {$arg == "" || ([llength $arg] != 2)} {
340 puts stderr "Bad argument: '[lindex $argList 0]' in '$decl'"
343 set args [list TCL_VARARGS $arg]
346 foreach arg $argList {
347 set argInfo [parseArg $arg]
348 if {![string compare $argInfo "void"]} {
351 } elseif {[llength $argInfo] == 2 || [llength $argInfo] == 3} {
352 lappend args $argInfo
354 puts stderr "Bad argument: '$arg' in '$decl'"
359 return [list $rtype $fname $args]
362 # genStubs::parseArg --
364 # This function parses a function argument into a type and name.
367 # arg The argument to parse.
370 # Returns a list of type and name with an optional third array
371 # indicator. If the argument is malformed, returns "".
373 proc genStubs::parseArg {arg} {
374 if {![regexp {^(.+[ ][*]*)([^][ *]+)(\[\])?$} $arg all type name array]} {
375 if {$arg == "void"} {
381 set result [list [string trim $type] $name]
383 lappend result $array
388 # genStubs::makeDecl --
390 # Generate the prototype for a function.
393 # name The interface name.
394 # decl The function declaration.
395 # index The slot index for this function.
398 # Returns the formatted declaration string.
400 proc genStubs::makeDecl {name decl index} {
402 lassign $decl rtype fname args
404 append text "/* $index */\n"
405 set line "$scspec $rtype"
406 set count [expr {2 - ([string length $line] / 8)}]
407 append line [string range "\t\t\t" 0 $count]
408 set pad [expr {24 - [string length $line]}]
413 append line "$fname "
415 set arg1 [lindex $args 0]
416 switch -exact $arg1 {
421 set arg [lindex $args 1]
422 append line "TCL_VARARGS([lindex $arg 0],[lindex $arg 1])"
429 append next [lindex $arg 0] " " [lindex $arg 1] \
431 if {[string length $line] + [string length $next] \
433 append text [string trimright $line] \n
449 # genStubs::makeMacro --
451 # Generate the inline macro for a function.
454 # name The interface name.
455 # decl The function declaration.
456 # index The slot index for this function.
459 # Returns the formatted macro definition.
461 proc genStubs::makeMacro {name decl index} {
462 lassign $decl rtype fname args
464 set lfname [string tolower [string index $fname 0]]
465 append lfname [string range $fname 1 end]
467 set text "#define $fname"
468 set arg1 [lindex $args 0]
470 switch -exact $arg1 {
479 append argList $sep [lindex $arg 1]
485 append text " \\\n\t(${name}StubsPtr->$lfname)"
486 append text " /* $index */\n"
490 # genStubs::makeSlot --
492 # Generate the stub table entry for a function.
495 # name The interface name.
496 # decl The function declaration.
497 # index The slot index for this function.
500 # Returns the formatted table entry.
502 proc genStubs::makeSlot {name decl index} {
503 lassign $decl rtype fname args
505 set lfname [string tolower [string index $fname 0]]
506 append lfname [string range $fname 1 end]
509 append text $rtype " (*" $lfname ") "
511 set arg1 [lindex $args 0]
512 switch -exact $arg1 {
517 set arg [lindex $args 1]
518 append text "TCL_VARARGS([lindex $arg 0],[lindex $arg 1])"
523 append text $sep [lindex $arg 0] " " [lindex $arg 1] \
531 append text "; /* $index */\n"
535 # genStubs::makeInit --
537 # Generate the prototype for a function.
540 # name The interface name.
541 # decl The function declaration.
542 # index The slot index for this function.
545 # Returns the formatted declaration string.
547 proc genStubs::makeInit {name decl index} {
548 append text " " [lindex $decl 1] ", /* " $index " */\n"
552 # genStubs::forAllStubs --
554 # This function iterates over all of the slots and invokes
555 # a callback for each slot. The result of the callback is then
556 # placed inside appropriate guards.
559 # name The interface name.
560 # slotProc The proc to invoke to handle the slot. It will
561 # have the interface name, the declaration, and
562 # the index appended.
563 # guardProc The proc to invoke to add guards. It will have
564 # the slot status and text appended.
565 # textVar The variable to use for output.
566 # skipString The string to emit if a slot is skipped. This
567 # string will be subst'ed in the loop so "$i" can
568 # be used to substitute the index value.
573 proc genStubs::forAllStubs {name slotProc guardProc textVar
574 {skipString {"/* Slot $i is reserved */\n"}}} {
578 set lastNum $stubs($name,lastNum)
580 for {set i 0} {$i <= $lastNum} {incr i} {
581 if {[info exists stubs($name,decl,$i)]} {
582 append text [$guardProc $stubs($name,status,$i) \
583 [$slotProc $name $stubs($name,decl,$i) $i]]
585 eval {append text} $skipString
590 proc genStubs::noGuard {status text} { return $text }
592 proc genStubs::addGuard {status text} {
594 set upName [string toupper $libraryName]
601 set text [ifdeffed "${upName}_DEPRECATED" $text]
607 puts stderr "Unrecognized status code $status"
613 proc genStubs::ifdeffed {macro text} {
614 join [list "#ifdef $macro" $text "#endif" ""] \n
617 # genStubs::emitDeclarations --
619 # This function emits the function declarations for this interface.
622 # name The interface name.
623 # textVar The variable to use for output.
628 proc genStubs::emitDeclarations {name textVar} {
631 append text "\n/*\n * Exported function declarations:\n */\n\n"
632 forAllStubs $name makeDecl noGuard text
636 # genStubs::emitMacros --
638 # This function emits the inline macros for an interface.
641 # name The name of the interface being emitted.
642 # textVar The variable to use for output.
647 proc genStubs::emitMacros {name textVar} {
651 set upName [string toupper $libraryName]
652 append text "\n#if defined(USE_${upName}_STUBS)\n"
653 append text "\n/*\n * Inline function declarations:\n */\n\n"
655 forAllStubs $name makeMacro addGuard text
657 append text "\n#endif /* defined(USE_${upName}_STUBS) */\n"
661 # genStubs::emitHeader --
663 # This function emits the body of the <name>Decls.h file for
664 # the specified interface.
667 # name The name of the interface being emitted.
672 proc genStubs::emitHeader {name} {
678 set capName [string toupper [string index $name 0]]
679 append capName [string range $name 1 end]
681 set CAPName [string toupper $name]
683 append text "#define ${CAPName}_STUBS_EPOCH $epoch\n"
684 append text "#define ${CAPName}_STUBS_REVISION $revision\n"
686 append text "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
688 emitDeclarations $name text
690 if {[info exists hooks($name)]} {
691 append text "\ntypedef struct {\n"
692 foreach hook $hooks($name) {
693 set capHook [string toupper [string index $hook 0]]
694 append capHook [string range $hook 1 end]
695 append text " const struct ${capHook}Stubs *${hook}Stubs;\n"
697 append text "} ${capName}StubHooks;\n"
699 append text "\ntypedef struct ${capName}Stubs {\n"
700 append text " int magic;\n"
701 append text " int epoch;\n"
702 append text " int revision;\n"
703 if {[info exists hooks($name)]} {
704 append text " const ${capName}StubHooks *hooks;\n\n"
706 append text " void *hooks;\n\n"
711 append text "} ${capName}Stubs;\n\n"
713 append text "extern const ${capName}Stubs *${name}StubsPtr;\n\n"
714 append text "#ifdef __cplusplus\n}\n#endif\n"
716 emitMacros $name text
718 rewriteFile [file join $outDir ${name}Decls.h] $text
722 # genStubs::emitInit --
724 # Generate the table initializers for an interface.
727 # name The name of the interface to initialize.
728 # textVar The variable to use for output.
731 # Returns the formatted output.
733 proc genStubs::emitInit {name textVar} {
740 set capName [string toupper [string index $name 0]]
741 append capName [string range $name 1 end]
742 set CAPName [string toupper $name]
744 if {[info exists hooks($name)]} {
745 append text "\nstatic const ${capName}StubHooks ${name}StubHooks = \{\n"
747 foreach sub $hooks($name) {
748 append text $sep "&${sub}Stubs"
751 append text "\n\};\n"
753 foreach intf [array names interfaces] {
754 if {[info exists hooks($intf)]} {
755 if {[lsearch -exact $hooks($intf) $name] >= 0} {
764 append text "static "
766 append text "const ${capName}Stubs ${name}Stubs = \{\n"
767 append text " TCL_STUB_MAGIC,\n"
768 append text " ${CAPName}_STUBS_EPOCH,\n"
769 append text " ${CAPName}_STUBS_REVISION,\n"
770 if {[info exists hooks($name)]} {
771 append text " &${name}StubHooks,\n"
776 forAllStubs $name makeInit noGuard text {" 0, /* $i */\n"}
782 # genStubs::emitInits --
784 # This function emits the body of the <name>StubInit.c file for
785 # the specified interface.
788 # name The name of the interface being emitted.
793 proc genStubs::emitInits {} {
799 # Assuming that dependencies only go one level deep, we need to emit
800 # all of the leaves first to avoid needing forward declarations.
804 foreach name [lsort [array names interfaces]] {
805 if {[info exists hooks($name)]} {
811 foreach name $leaves {
814 foreach name $roots {
818 rewriteFile [file join $outDir ${libraryName}StubInit.c] $text
823 # This is the main entry point.
831 proc genStubs::init {} {
836 if {[llength $argv] < 2} {
837 puts stderr "usage: $argv0 outDir declFile ?declFile...?"
841 set outDir [lindex $argv 0]
843 foreach file [lrange $argv 1 end] {
847 foreach name [lsort [array names interfaces]] {
848 puts "Emitting $name"
857 # This function emulates the TclX lassign command.
860 # valueList A list containing the values to be assigned.
861 # args The list of variables to be assigned.
864 # Returns any values that were not assigned to variables.
866 proc lassign {valueList args} {
867 if {[llength $args] == 0} {
868 error "wrong # args: lassign list varname ?varname..?"
871 uplevel [list foreach $args $valueList {break}]
872 return [lrange $valueList [llength $args] end]