Debian Codegraph @

lib/stubs_gen_slot/gen_slot.tcl

raw
1 # -*- tcl -*-
2 # STUBS handling -- Code generation: Writing SLOT code.
3 #
4 # (c) 2011,2022-2023 Andreas Kupries http://wiki.tcl.tk/andreas%20kupries
5
6 # # ## ### ##### ######## #############
7 ## Requisites
8
9 package require Tcl 8.6 9
10 package require stubs::gen
11
12 namespace eval ::stubs::gen::slot::g {
13 namespace import ::stubs::gen::*
14 }
15
16 # # ## ### ##### ######## #############
17 ## Implementation.
18
19 proc ::stubs::gen::slot::gen {table name} {
20 return [g::forall $table $name [namespace current]::Make 1 \
21 " void (*reserved@@)(void);\n"]
22 }
23
24 # # ## ### #####
25 ## Internal helpers.
26
27 proc ::stubs::gen::slot::Make {name decl index} {
28 #puts "SLOT($name $index) = |$decl|"
29
30 lassign $decl rtype fname args
31
32 set capName [g::uncap $fname]
33
34 set text " "
35 if {![llength $args]} {
36 append text $rtype " *" $capName "; /* $index */\n"
37 return $text
38 }
39
40 if {[string range $rtype end-7 end] eq "CALLBACK"} {
41 append text \
42 [string trim [string range $rtype 0 end-8]] \
43 " (CALLBACK *" $capName ") "
44 } else {
45 append text $rtype " (*" $capName ") "
46 }
47
48 set arg1 [lindex $args 0]
49 switch -exact -- $arg1 {
50 void {
51 append text "(void)"
52 }
53 TCL_VARARGS {
54 append text [MakeArgs [lrange $args 1 end] ", ..."]
55 }
56 default {
57 append text [MakeArgs $args]
58 }
59 }
60
61 append text "; /* $index */\n"
62 return $text
63 }
64
65 proc ::stubs::gen::slot::MakeArgs {arguments {suffix {}}} {
66 set text ""
67 set sep "("
68 foreach arg $arguments {
69 lassign $arg atype aname aind
70 append text $sep $atype
71 if {[string index $text end] ne "*"} {
72 append text " "
73 }
74 append text $aname $aind
75 set sep ", "
76 }
77 append text "$suffix)"
78
79 if {[lindex $arguments end] eq "\{const char *\} format"} {
80 # TCL_VARARGS case... arguments list already shrunken.
81 set n [llength $arguments]
82 append text " TCL_FORMAT_PRINTF(" $n ", " [expr {$n + 1}] ")"
83 }
84
85 return $text
86 }
87
88 # # ## ### #####
89 namespace eval ::stubs::gen::slot {
90 namespace export gen
91 }
92
93 # # ## ### #####
94 package provide stubs::gen::slot 1.1.1
95 return