os/persistentdata/persistentstorage/sqlite3api/TEST/TCL/tcldistribution/tools/genStubs.tcl
First public contribution.
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 # RCS: @(#) $Id: genStubs.tcl,v 1.13 2002/10/04 08:25:14 dkf Exp $
15 namespace eval genStubs {
18 # The name of the entire library. This value is used to compute
19 # the USE_*_STUB_PROCS macro and the name of the init file.
21 variable libraryName "UNKNOWN"
25 # An array indexed by interface name that is used to maintain
26 # the set of valid interfaces. The value is empty.
28 array set interfaces {}
32 # The name of the interface currently being defined.
34 variable curName "UNKNOWN"
38 # An array indexed by interface name that contains the set of
39 # subinterfaces that should be defined for a given interface.
45 # This three dimensional array is indexed first by interface name,
46 # second by platform name, and third by a numeric offset or the
47 # constant "lastNum". The lastNum entry contains the largest
48 # numeric offset used for a given interface/platform combo. Each
49 # numeric offset contains the C function specification that
50 # should be used for the given entry in the stub table. The spec
51 # consists of a list in the form returned by parseDecl.
57 # The directory where the generated files should be placed.
62 # genStubs::library --
64 # This function is used in the declarations file to set the name
65 # of the library that the interfaces are associated with (e.g. "tcl").
66 # This value will be used to define the inline conditional macro.
69 # name The library name.
74 proc genStubs::library {name} {
75 variable libraryName $name
78 # genStubs::interface --
80 # This function is used in the declarations file to set the name
81 # of the interface currently being defined.
84 # name The name of the interface.
89 proc genStubs::interface {name} {
90 variable curName $name
93 set interfaces($name) {}
99 # This function defines the subinterface hooks for the current
103 # names The ordered list of interfaces that are reachable through the
109 proc genStubs::hooks {names} {
113 set hooks($curName) $names
117 # genStubs::declare --
119 # This function is used in the declarations file to declare a new
123 # index The index number of the interface.
124 # platform The platform the interface belongs to. Should be one
125 # of generic, win, unix, or mac, or macosx or aqua or x11.
126 # decl The C function declaration, or {} for an undefined
132 proc genStubs::declare {args} {
136 if {[llength $args] != 3} {
137 puts stderr "wrong # args: declare $args"
139 lassign $args index platformList decl
141 # Check for duplicate declarations, then add the declaration and
142 # bump the lastNum counter if necessary.
144 foreach platform $platformList {
145 if {[info exists stubs($curName,$platform,$index)]} {
146 puts stderr "Duplicate entry: declare $args"
149 regsub -all "\[ \t\n\]+" [string trim $decl] " " decl
150 set decl [parseDecl $decl]
152 foreach platform $platformList {
154 set stubs($curName,$platform,$index) $decl
155 if {![info exists stubs($curName,$platform,lastNum)] \
156 || ($index > $stubs($curName,$platform,lastNum))} {
157 set stubs($curName,$platform,lastNum) $index
164 # genStubs::rewriteFile --
166 # This function replaces the machine generated portion of the
167 # specified file with new contents. It looks for the !BEGIN! and
168 # !END! comments to determine where to place the new text.
171 # file The name of the file to modify.
172 # text The new text to place in the file.
177 proc genStubs::rewriteFile {file text} {
178 if {![file exists $file]} {
179 puts stderr "Cannot find file: $file"
182 set in [open ${file} r]
183 set out [open ${file}.new w]
187 if {[regexp {!BEGIN!} $line]} {
192 puts $out "/* !BEGIN!: Do not edit below this line. */"
196 if {[regexp {!END!} $line]} {
200 puts $out "/* !END!: Do not edit above this line. */"
201 puts -nonewline $out [read $in]
204 file rename -force ${file}.new ${file}
208 # genStubs::addPlatformGuard --
210 # Wrap a string inside a platform #ifdef.
213 # plat Platform to test.
216 # Returns the original text inside an appropriate #ifdef.
218 proc genStubs::addPlatformGuard {plat text} {
221 return "#ifdef __WIN32__\n${text}#endif /* __WIN32__ */\n"
224 return "#if !defined(__WIN32__) && !defined(MAC_TCL) /* UNIX */\n${text}#endif /* UNIX */\n"
227 return "#ifdef MAC_TCL\n${text}#endif /* MAC_TCL */\n"
230 return "#ifdef MAC_OSX_TCL\n${text}#endif /* MAC_OSX_TCL */\n"
233 return "#ifdef MAC_OSX_TK\n${text}#endif /* MAC_OSX_TK */\n"
236 return "#if !(defined(__WIN32__) || defined(MAC_TCL) || defined(MAC_OSX_TK)) /* X11 */\n${text}#endif /* X11 */\n"
242 # genStubs::emitSlots --
244 # Generate the stub table slots for the given interface. If there
245 # are no generic slots, then one table is generated for each
246 # platform, otherwise one table is generated for all platforms.
249 # name The name of the interface being emitted.
250 # textVar The variable to use for output.
255 proc genStubs::emitSlots {name textVar} {
259 forAllStubs $name makeSlot 1 text {" void *reserved$i;\n"}
263 # genStubs::parseDecl --
265 # Parse a C function declaration into its component parts.
268 # decl The function declaration.
271 # Returns a list of the form {returnType name args}. The args
272 # element consists of a list of type/name pairs, or a single
273 # element "void". If the function declaration is malformed
274 # then an error is displayed and the return value is {}.
276 proc genStubs::parseDecl {decl} {
277 if {![regexp {^(.*)\((.*)\)$} $decl all prefix args]} {
278 puts stderr "Malformed declaration: $decl"
281 set prefix [string trim $prefix]
282 if {![regexp {^(.+[ ][*]*)([^ *]+)$} $prefix all rtype fname]} {
283 puts stderr "Bad return type: $decl"
286 set rtype [string trim $rtype]
287 foreach arg [split $args ,] {
288 lappend argList [string trim $arg]
290 if {![string compare [lindex $argList end] "..."]} {
291 if {[llength $argList] != 2} {
292 puts stderr "Only one argument is allowed in varargs form: $decl"
294 set arg [parseArg [lindex $argList 0]]
295 if {$arg == "" || ([llength $arg] != 2)} {
296 puts stderr "Bad argument: '[lindex $argList 0]' in '$decl'"
299 set args [list TCL_VARARGS $arg]
302 foreach arg $argList {
303 set argInfo [parseArg $arg]
304 if {![string compare $argInfo "void"]} {
307 } elseif {[llength $argInfo] == 2 || [llength $argInfo] == 3} {
308 lappend args $argInfo
310 puts stderr "Bad argument: '$arg' in '$decl'"
315 return [list $rtype $fname $args]
318 # genStubs::parseArg --
320 # This function parses a function argument into a type and name.
323 # arg The argument to parse.
326 # Returns a list of type and name with an optional third array
327 # indicator. If the argument is malformed, returns "".
329 proc genStubs::parseArg {arg} {
330 if {![regexp {^(.+[ ][*]*)([^][ *]+)(\[\])?$} $arg all type name array]} {
331 if {$arg == "void"} {
337 set result [list [string trim $type] $name]
339 lappend result $array
344 # genStubs::makeDecl --
346 # Generate the prototype for a function.
349 # name The interface name.
350 # decl The function declaration.
351 # index The slot index for this function.
354 # Returns the formatted declaration string.
356 proc genStubs::makeDecl {name decl index} {
357 lassign $decl rtype fname args
359 append text "/* $index */\n"
360 set line "EXTERN $rtype"
361 set count [expr {2 - ([string length $line] / 8)}]
362 append line [string range "\t\t\t" 0 $count]
363 set pad [expr {24 - [string length $line]}]
368 append line "$fname _ANSI_ARGS_("
370 set arg1 [lindex $args 0]
371 switch -exact $arg1 {
376 set arg [lindex $args 1]
377 append line "TCL_VARARGS([lindex $arg 0],[lindex $arg 1])"
384 append next [lindex $arg 0] " " [lindex $arg 1] \
386 if {[string length $line] + [string length $next] \
404 # genStubs::makeMacro --
406 # Generate the inline macro for a function.
409 # name The interface name.
410 # decl The function declaration.
411 # index The slot index for this function.
414 # Returns the formatted macro definition.
416 proc genStubs::makeMacro {name decl index} {
417 lassign $decl rtype fname args
419 set lfname [string tolower [string index $fname 0]]
420 append lfname [string range $fname 1 end]
422 set text "#ifndef $fname\n#define $fname"
423 set arg1 [lindex $args 0]
425 switch -exact $arg1 {
434 append argList $sep [lindex $arg 1]
440 append text " \\\n\t(${name}StubsPtr->$lfname)"
441 append text " /* $index */\n#endif\n"
445 # genStubs::makeStub --
447 # Emits a stub function definition.
450 # name The interface name.
451 # decl The function declaration.
452 # index The slot index for this function.
455 # Returns the formatted stub function definition.
457 proc genStubs::makeStub {name decl index} {
458 lassign $decl rtype fname args
460 set lfname [string tolower [string index $fname 0]]
461 append lfname [string range $fname 1 end]
463 append text "/* Slot $index */\n" $rtype "\n" $fname
465 set arg1 [lindex $args 0]
467 if {![string compare $arg1 "TCL_VARARGS"]} {
468 lassign [lindex $args 1] type argName
469 append text " TCL_VARARGS_DEF($type,$argName)\n\{\n"
470 append text " " $type " var;\n va_list argList;\n"
471 if {[string compare $rtype "void"]} {
472 append text " " $rtype " resultValue;\n"
474 append text "\n var = (" $type ") TCL_VARARGS_START(" \
475 $type "," $argName ",argList);\n\n "
476 if {[string compare $rtype "void"]} {
477 append text "resultValue = "
479 append text "(" $name "StubsPtr->" $lfname "VA)(var, argList);\n"
480 append text " va_end(argList);\n"
481 if {[string compare $rtype "void"]} {
482 append text "return resultValue;\n"
488 if {![string compare $arg1 "void"]} {
495 append argList $sep [lindex $arg 1]
496 append argDecls " " [lindex $arg 0] " " \
497 [lindex $arg 1] [lindex $arg 2] ";\n"
502 append text $argList "\n" $argDecls "{\n "
503 if {[string compare $rtype "void"]} {
504 append text "return "
506 append text "(" $name "StubsPtr->" $lfname ")" $argList ";\n}\n\n"
510 # genStubs::makeSlot --
512 # Generate the stub table entry for a function.
515 # name The interface name.
516 # decl The function declaration.
517 # index The slot index for this function.
520 # Returns the formatted table entry.
522 proc genStubs::makeSlot {name decl index} {
523 lassign $decl rtype fname args
525 set lfname [string tolower [string index $fname 0]]
526 append lfname [string range $fname 1 end]
529 append text $rtype " (*" $lfname ") _ANSI_ARGS_("
531 set arg1 [lindex $args 0]
532 switch -exact $arg1 {
537 set arg [lindex $args 1]
538 append text "TCL_VARARGS([lindex $arg 0],[lindex $arg 1])"
543 append text $sep [lindex $arg 0] " " [lindex $arg 1] \
551 append text "); /* $index */\n"
555 # genStubs::makeInit --
557 # Generate the prototype for a function.
560 # name The interface name.
561 # decl The function declaration.
562 # index The slot index for this function.
565 # Returns the formatted declaration string.
567 proc genStubs::makeInit {name decl index} {
568 append text " " [lindex $decl 1] ", /* " $index " */\n"
572 # genStubs::forAllStubs --
574 # This function iterates over all of the platforms and invokes
575 # a callback for each slot. The result of the callback is then
576 # placed inside appropriate platform guards.
579 # name The interface name.
580 # slotProc The proc to invoke to handle the slot. It will
581 # have the interface name, the declaration, and
582 # the index appended.
583 # onAll If 1, emit the skip string even if there are
584 # definitions for one or more platforms.
585 # textVar The variable to use for output.
586 # skipString The string to emit if a slot is skipped. This
587 # string will be subst'ed in the loop so "$i" can
588 # be used to substitute the index value.
593 proc genStubs::forAllStubs {name slotProc onAll textVar \
594 {skipString {"/* Slot $i is reserved */\n"}}} {
598 set plats [array names stubs $name,*,lastNum]
599 if {[info exists stubs($name,generic,lastNum)]} {
600 # Emit integrated stubs block
602 foreach plat [array names stubs $name,*,lastNum] {
603 if {$stubs($plat) > $lastNum} {
604 set lastNum $stubs($plat)
607 for {set i 0} {$i <= $lastNum} {incr i} {
608 set slots [array names stubs $name,*,$i]
610 if {[info exists stubs($name,generic,$i)]} {
611 if {[llength $slots] > 1} {
612 puts stderr "platform entry duplicates generic entry: $i"
614 append text [$slotProc $name $stubs($name,generic,$i) $i]
616 } elseif {[llength $slots] > 0} {
617 foreach plat {unix win mac} {
618 if {[info exists stubs($name,$plat,$i)]} {
619 append text [addPlatformGuard $plat \
620 [$slotProc $name $stubs($name,$plat,$i) $i]]
623 append text [eval {addPlatformGuard $plat} $skipString]
628 # "aqua" and "macosx" and "x11" are special cases,
629 # since "macosx" always implies "unix" and "aqua",
630 # "macosx", so we need to be careful not to
631 # emit duplicate stubs entries for the two.
633 if {[info exists stubs($name,aqua,$i)]
634 && ![info exists stubs($name,macosx,$i)]} {
635 append text [addPlatformGuard aqua \
636 [$slotProc $name $stubs($name,aqua,$i) $i]]
639 if {[info exists stubs($name,macosx,$i)]
640 && ![info exists stubs($name,unix,$i)]} {
641 append text [addPlatformGuard macosx \
642 [$slotProc $name $stubs($name,macosx,$i) $i]]
645 if {[info exists stubs($name,x11,$i)]
646 && ![info exists stubs($name,unix,$i)]} {
647 append text [addPlatformGuard x11 \
648 [$slotProc $name $stubs($name,x11,$i) $i]]
653 eval {append text} $skipString
658 # Emit separate stubs blocks per platform
659 foreach plat {unix win mac} {
660 if {[info exists stubs($name,$plat,lastNum)]} {
661 set lastNum $stubs($name,$plat,lastNum)
663 for {set i 0} {$i <= $lastNum} {incr i} {
664 if {![info exists stubs($name,$plat,$i)]} {
665 eval {append temp} $skipString
667 append temp [$slotProc $name $stubs($name,$plat,$i) $i]
670 append text [addPlatformGuard $plat $temp]
673 # Again, make sure you don't duplicate entries for macosx & aqua.
674 if {[info exists stubs($name,aqua,lastNum)]
675 && ![info exists stubs($name,macosx,lastNum)]} {
676 set lastNum $stubs($name,aqua,lastNum)
678 for {set i 0} {$i <= $lastNum} {incr i} {
679 if {![info exists stubs($name,aqua,$i)]} {
680 eval {append temp} $skipString
682 append temp [$slotProc $name $stubs($name,aqua,$i) $i]
685 append text [addPlatformGuard aqua $temp]
687 # Again, make sure you don't duplicate entries for macosx & unix.
688 if {[info exists stubs($name,macosx,lastNum)]
689 && ![info exists stubs($name,unix,lastNum)]} {
690 set lastNum $stubs($name,macosx,lastNum)
692 for {set i 0} {$i <= $lastNum} {incr i} {
693 if {![info exists stubs($name,macosx,$i)]} {
694 eval {append temp} $skipString
696 append temp [$slotProc $name $stubs($name,macosx,$i) $i]
699 append text [addPlatformGuard macosx $temp]
701 # Again, make sure you don't duplicate entries for x11 & unix.
702 if {[info exists stubs($name,x11,lastNum)]
703 && ![info exists stubs($name,unix,lastNum)]} {
704 set lastNum $stubs($name,x11,lastNum)
706 for {set i 0} {$i <= $lastNum} {incr i} {
707 if {![info exists stubs($name,x11,$i)]} {
708 eval {append temp} $skipString
710 append temp [$slotProc $name $stubs($name,x11,$i) $i]
713 append text [addPlatformGuard x11 $temp]
718 # genStubs::emitDeclarations --
720 # This function emits the function declarations for this interface.
723 # name The interface name.
724 # textVar The variable to use for output.
729 proc genStubs::emitDeclarations {name textVar} {
733 append text "\n/*\n * Exported function declarations:\n */\n\n"
734 forAllStubs $name makeDecl 0 text
738 # genStubs::emitMacros --
740 # This function emits the inline macros for an interface.
743 # name The name of the interface being emitted.
744 # textVar The variable to use for output.
749 proc genStubs::emitMacros {name textVar} {
754 set upName [string toupper $libraryName]
755 append text "\n#if defined(USE_${upName}_STUBS) && !defined(USE_${upName}_STUB_PROCS)\n"
756 append text "\n/*\n * Inline function declarations:\n */\n\n"
758 forAllStubs $name makeMacro 0 text
760 append text "\n#endif /* defined(USE_${upName}_STUBS) && !defined(USE_${upName}_STUB_PROCS) */\n"
764 # genStubs::emitHeader --
766 # This function emits the body of the <name>Decls.h file for
767 # the specified interface.
770 # name The name of the interface being emitted.
775 proc genStubs::emitHeader {name} {
779 set capName [string toupper [string index $name 0]]
780 append capName [string range $name 1 end]
782 emitDeclarations $name text
784 if {[info exists hooks($name)]} {
785 append text "\ntypedef struct ${capName}StubHooks {\n"
786 foreach hook $hooks($name) {
787 set capHook [string toupper [string index $hook 0]]
788 append capHook [string range $hook 1 end]
789 append text " struct ${capHook}Stubs *${hook}Stubs;\n"
791 append text "} ${capName}StubHooks;\n"
793 append text "\ntypedef struct ${capName}Stubs {\n"
794 append text " int magic;\n"
795 append text " struct ${capName}StubHooks *hooks;\n\n"
799 append text "} ${capName}Stubs;\n"
801 append text "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"
802 append text "extern ${capName}Stubs *${name}StubsPtr;\n"
803 append text "#ifdef __cplusplus\n}\n#endif\n"
805 emitMacros $name text
807 rewriteFile [file join $outDir ${name}Decls.h] $text
811 # genStubs::emitStubs --
813 # This function emits the body of the <name>Stubs.c file for
814 # the specified interface.
817 # name The name of the interface being emitted.
822 proc genStubs::emitStubs {name} {
825 append text "\n/*\n * Exported stub functions:\n */\n\n"
826 forAllStubs $name makeStub 0 text
828 rewriteFile [file join $outDir ${name}Stubs.c] $text
832 # genStubs::emitInit --
834 # Generate the table initializers for an interface.
837 # name The name of the interface to initialize.
838 # textVar The variable to use for output.
841 # Returns the formatted output.
843 proc genStubs::emitInit {name textVar} {
848 set capName [string toupper [string index $name 0]]
849 append capName [string range $name 1 end]
851 if {[info exists hooks($name)]} {
852 append text "\nstatic ${capName}StubHooks ${name}StubHooks = \{\n"
854 foreach sub $hooks($name) {
855 append text $sep "&${sub}Stubs"
858 append text "\n\};\n"
860 append text "\n${capName}Stubs ${name}Stubs = \{\n"
861 append text " TCL_STUB_MAGIC,\n"
862 if {[info exists hooks($name)]} {
863 append text " &${name}StubHooks,\n"
865 append text " NULL,\n"
868 forAllStubs $name makeInit 1 text {" NULL, /* $i */\n"}
874 # genStubs::emitInits --
876 # This function emits the body of the <name>StubInit.c file for
877 # the specified interface.
880 # name The name of the interface being emitted.
885 proc genStubs::emitInits {} {
891 # Assuming that dependencies only go one level deep, we need to emit
892 # all of the leaves first to avoid needing forward declarations.
896 foreach name [lsort [array names interfaces]] {
897 if {[info exists hooks($name)]} {
903 foreach name $leaves {
906 foreach name $roots {
910 rewriteFile [file join $outDir ${libraryName}StubInit.c] $text
915 # This is the main entry point.
923 proc genStubs::init {} {
928 if {[llength $argv] < 2} {
929 puts stderr "usage: $argv0 outDir declFile ?declFile...?"
933 set outDir [lindex $argv 0]
935 foreach file [lrange $argv 1 end] {
939 foreach name [lsort [array names interfaces]] {
940 puts "Emitting $name"
949 # This function emulates the TclX lassign command.
952 # valueList A list containing the values to be assigned.
953 # args The list of variables to be assigned.
956 # Returns any values that were not assigned to variables.
958 proc lassign {valueList args} {
959 if {[llength $args] == 0} {
960 error "wrong # args: lassign list varname ?varname..?"
963 uplevel [list foreach $args $valueList {break}]
964 return [lrange $valueList [llength $args] end]