]>
Commit | Line | Data |
---|---|---|
c90f71dd RD |
1 | /******************************************************************************* |
2 | * Simplified Wrapper and Interface Generator (SWIG) | |
3 | * | |
4 | * Author : David Beazley | |
5 | * | |
6 | * Department of Computer Science | |
7 | * University of Chicago | |
8 | * 1100 E 58th Street | |
9 | * Chicago, IL 60637 | |
10 | * beazley@cs.uchicago.edu | |
11 | * | |
12 | * Please read the file LICENSE for the copyright and terms by which SWIG | |
13 | * can be used and distributed. | |
14 | *******************************************************************************/ | |
15 | ||
16 | #include "internal.h" | |
17 | #include <limits.h> | |
18 | #include <ctype.h> | |
19 | ||
20 | // -------------------------------------------------------------------------------- | |
21 | // $Header$ | |
22 | // | |
23 | // naming.cxx | |
24 | // | |
25 | // SWIG naming service. | |
26 | // | |
27 | // This module provides universal naming services for manufacturing function names. | |
28 | // All language modules use this so it provides a convenient centralized | |
29 | // mechanism for producing names. | |
30 | // -------------------------------------------------------------------------------- | |
31 | ||
32 | // Structure for holding names | |
33 | ||
34 | struct NamingScheme { | |
35 | char *format; | |
36 | int first; // Scoping information | |
37 | int last; // Scoping information | |
38 | NamingScheme *next; | |
39 | NamingScheme(char *n) { | |
40 | format = copy_string(n); | |
41 | first = type_id; | |
42 | last = INT_MAX; | |
43 | next = 0; | |
44 | }; | |
45 | }; | |
46 | ||
47 | // Hash table containing naming data | |
48 | ||
49 | static Hash naming_hash; | |
50 | ||
51 | // Variable indicating naming scope | |
52 | ||
53 | static int naming_scope = -1; | |
54 | ||
55 | //----------------------------------------------------------------- | |
56 | // make_wrap_name(char *s) | |
57 | // | |
58 | // Takes the name at src, and converts it into a syntactically | |
59 | // valid identifier name. This is a hack to get the wrapper | |
60 | // generator to support class member functions and other things. | |
61 | // | |
62 | // ie. We can define a function name as obj->foo(), | |
63 | // but we'll need to call the wrapper function something like | |
64 | // _wrap_obj__foo() | |
65 | //----------------------------------------------------------------- | |
66 | ||
67 | void make_wrap_name(char *s) { | |
68 | ||
69 | char *c1 = s; | |
70 | int i; | |
71 | ||
72 | for (i = 0; i < (int) strlen(s); i++, c1++) { | |
73 | if(!isalnum(*c1)) *c1 = '_'; | |
74 | } | |
75 | } | |
76 | ||
77 | // -------------------------------------------------------------------------------- | |
78 | // int name_scope(int scope) | |
79 | // | |
80 | // Set the scope variable. This is used to determine what naming scheme to | |
81 | // use. Returns the current value of the scope. | |
82 | // -------------------------------------------------------------------------------- | |
83 | ||
84 | int name_scope(int scope) { | |
85 | int s = naming_scope; | |
86 | naming_scope = scope; | |
87 | return s; | |
88 | } | |
89 | ||
90 | // -------------------------------------------------------------------------------- | |
91 | // void name_register(char *method, char *format) | |
92 | // | |
93 | // Registers a new naming scheme. | |
94 | // -------------------------------------------------------------------------------- | |
95 | ||
96 | void name_register(char *method, char *format) { | |
97 | NamingScheme *ns, *nns; | |
98 | ||
99 | ns = (NamingScheme *) naming_hash.lookup(method); | |
100 | if (ns) { | |
101 | naming_hash.remove(method); | |
102 | } | |
103 | ||
104 | nns = new NamingScheme(format); // Create a new naming scheme | |
105 | if (ns) ns->last = type_id; | |
106 | nns->next = ns; | |
107 | ||
108 | naming_hash.add(method,nns); | |
109 | }; | |
110 | ||
111 | // -------------------------------------------------------------------------------- | |
112 | // char *name_getformat(char *method) | |
113 | // | |
114 | // Looks up a naming scheme in the hash table. The scope of the name should have | |
115 | // been set prior to calling this. If not set, we just use the last name entered. | |
116 | // Returns the format string or NULL if no name has been set. | |
117 | // -------------------------------------------------------------------------------- | |
118 | ||
119 | static char *name_getformat(char *method) { | |
120 | ||
121 | NamingScheme *ns; | |
122 | int scope; | |
123 | if (naming_scope == -1) scope = type_id; | |
124 | else scope = naming_scope; | |
125 | ||
126 | ns = (NamingScheme *) naming_hash.lookup(method); | |
127 | while (ns) { | |
128 | if ((ns->first <= scope) && (scope < ns->last)) | |
129 | return ns->format; | |
130 | ns = ns->next; | |
131 | } | |
132 | return 0; | |
133 | } | |
134 | ||
135 | // -------------------------------------------------------------------------------- | |
136 | // char *name_wrapper(char *fname, char *prefix, int suppress) | |
137 | // | |
138 | // Returns the name of a wrapper function. The following variables are | |
139 | // available : | |
140 | // | |
141 | // %f -> fname | |
142 | // %p -> prefix | |
143 | // %l -> language | |
144 | // | |
145 | // By default a wrapper function gets the name _wrap_prefixfname. | |
146 | // | |
147 | // -------------------------------------------------------------------------------- | |
148 | ||
149 | char *name_wrapper(char *fname, char *prefix, int suppress) { | |
150 | static String fmt; | |
151 | char *f; | |
152 | ||
153 | f = name_getformat("wrapper"); | |
154 | if (!f) { | |
155 | f = "_wrap_%p%f"; // Default wrapper name | |
156 | } | |
157 | fmt = f; | |
158 | fmt.replace("%f",fname); | |
159 | fmt.replace("%l",typemap_lang); | |
160 | fmt.replace("%p",prefix); | |
161 | if (!suppress) | |
162 | make_wrap_name(fmt); | |
163 | return fmt; | |
164 | } | |
165 | ||
166 | ||
167 | // -------------------------------------------------------------------------------- | |
168 | // char *name_member(char *fname, char *classname, int suppress) | |
169 | // | |
170 | // Returns the name of a method function. The following variables are | |
171 | // available : | |
172 | // | |
173 | // %f -> fname | |
174 | // %c -> classname | |
175 | // %l -> language | |
176 | // | |
177 | // By default, the name of a method is given as Classname_method. | |
178 | // -------------------------------------------------------------------------------- | |
179 | ||
180 | char *name_member(char *fname, char *classname, int suppress) { | |
181 | static String fmt; | |
182 | char *f; | |
183 | ||
184 | f = name_getformat("member"); | |
185 | if (!f) { | |
186 | f = "%c_%f"; | |
187 | } | |
188 | fmt = f; | |
189 | fmt.replace("%f",fname); | |
190 | fmt.replace("%l",typemap_lang); | |
191 | fmt.replace("%c",classname); | |
192 | if (!suppress) | |
193 | make_wrap_name(fmt); | |
194 | return fmt; | |
195 | } | |
196 | ||
197 | ||
198 | // -------------------------------------------------------------------------------- | |
199 | // char *name_get(char *vname, int suppress) | |
200 | // | |
201 | // Returns the name of the accessor function used to get a variable. | |
202 | // | |
203 | // %v -> variable name | |
204 | // | |
205 | // -------------------------------------------------------------------------------- | |
206 | ||
207 | char *name_get(char *vname, int suppress) { | |
208 | static String fmt; | |
209 | char *f; | |
210 | ||
211 | f = name_getformat("get"); | |
212 | if (!f) { | |
213 | f = "%v_get"; | |
214 | } | |
215 | fmt = f; | |
216 | fmt.replace("%v",vname); | |
217 | if (!suppress) | |
218 | make_wrap_name(fmt); | |
219 | return fmt; | |
220 | } | |
221 | ||
222 | // -------------------------------------------------------------------------------- | |
223 | // char *name_set(char *vname, int suppress) | |
224 | // | |
225 | // Returns the name of the accessor function used to set a variable. | |
226 | // | |
227 | // %v -> variable name | |
228 | // -------------------------------------------------------------------------------- | |
229 | ||
230 | char *name_set(char *vname, int suppress) { | |
231 | static String fmt; | |
232 | char *f; | |
233 | ||
234 | f = name_getformat("set"); | |
235 | if (!f) { | |
236 | f = "%v_set"; | |
237 | } | |
238 | fmt = f; | |
239 | fmt.replace("%v",vname); | |
240 | if (!suppress) | |
241 | make_wrap_name(fmt); | |
242 | return fmt; | |
243 | } | |
244 | ||
245 | ||
246 | // -------------------------------------------------------------------------------- | |
247 | // char *name_construct(char *classname, int suppress) | |
248 | // | |
249 | // Returns the name of the accessor function used to create an object. | |
250 | // By default this is "new_classname" | |
251 | // | |
252 | // %c -> classname | |
253 | // %l -> language | |
254 | // | |
255 | // -------------------------------------------------------------------------------- | |
256 | ||
257 | char *name_construct(char *classname, int suppress) { | |
258 | static String fmt; | |
259 | char *f; | |
260 | ||
261 | f = name_getformat("construct"); | |
262 | if (!f) { | |
263 | f = "new_%c"; | |
264 | } | |
265 | fmt = f; | |
266 | fmt.replace("%l",typemap_lang); | |
267 | fmt.replace("%c",classname); | |
268 | if (!suppress) | |
269 | make_wrap_name(fmt); | |
270 | return fmt; | |
271 | } | |
272 | ||
273 | ||
274 | // -------------------------------------------------------------------------------- | |
275 | // char *name_destroy(char *classname, int suppress) | |
276 | // | |
277 | // Returns the name of the accessor function used to destroy an object. | |
278 | // By default this is "delete_classname" | |
279 | // | |
280 | // %c -> classname | |
281 | // %l -> language | |
282 | // | |
283 | // -------------------------------------------------------------------------------- | |
284 | ||
285 | char *name_destroy(char *classname, int suppress) { | |
286 | static String fmt; | |
287 | char *f; | |
288 | ||
289 | f = name_getformat("destroy"); | |
290 | if (!f) { | |
291 | f = "delete_%c"; | |
292 | } | |
293 | fmt = f; | |
294 | fmt.replace("%l",typemap_lang); | |
295 | fmt.replace("%c",classname); | |
296 | if (!suppress) | |
297 | make_wrap_name(fmt); | |
298 | return fmt; | |
299 | } |