openurl.library: 64-bit pointer casting cleanups
[AROS.git] / tools / genmodule / writeincdefines.c
blob890c9e6a53228b20b6994eeaa90ab5a8104c4e59
1 /*
2 Copyright © 1995-2014, The AROS Development Team. All rights reserved.
3 $Id$
5 Function to write defines/modulename.h. Part of genmodule.
6 */
7 #include "genmodule.h"
9 static void writedefineregister(FILE *, struct functionhead *, struct config *);
10 static void writedefinevararg(FILE *, struct functionhead *, struct config *);
11 static void writealiases(FILE *, struct functionhead *, struct config *);
13 void writeincdefines(struct config *cfg)
15 FILE *out;
16 char line[256], *banner;
17 struct functionhead *funclistit;
19 snprintf(line, 255, "%s/defines/%s.h", cfg->gendir, cfg->includename);
20 out = fopen(line, "w");
22 if (out == NULL)
24 perror(line);
25 exit(20);
28 banner = getBanner(cfg);
29 fprintf(out,
30 "#ifndef DEFINES_%s_H\n"
31 "#define DEFINES_%s_H\n"
32 "\n"
33 "%s"
34 "\n"
35 "/*\n"
36 " Desc: Defines for %s\n"
37 "*/\n"
38 "\n"
39 "#include <aros/libcall.h>\n"
40 "#include <exec/types.h>\n"
41 "#include <aros/symbolsets.h>\n"
42 "#include <aros/preprocessor/variadic/cast2iptr.hpp>\n"
43 "\n"
44 "__BEGIN_DECLS\n"
45 "\n",
46 cfg->includenameupper, cfg->includenameupper, banner, cfg->modulename
48 freeBanner(banner);
50 for (funclistit = cfg->funclist; funclistit!=NULL; funclistit = funclistit->next)
52 if (!funclistit->priv && (funclistit->lvo >= cfg->firstlvo) && funclistit->libcall != STACK)
54 fprintf(out,
55 "\n"
56 "#if !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__)"
57 "\n",
58 cfg->includenameupper,
59 funclistit->version,
60 cfg->includenameupper
63 writedefineregister(out, funclistit, cfg);
64 if (!funclistit->novararg)
65 writedefinevararg(out, funclistit, cfg);
67 writealiases(out, funclistit, cfg);
69 fprintf(out,
70 "\n"
71 "#endif /* !defined(__%s_LIBAPI__) || (%d <= __%s_LIBAPI__) */"
72 "\n",
73 cfg->includenameupper,
74 funclistit->version,
75 cfg->includenameupper
80 fprintf(out,
81 "\n"
82 "__END_DECLS\n"
83 "\n"
84 "#endif /* DEFINES_%s_H*/\n",
85 cfg->includenameupper
87 fclose(out);
91 void
92 writedefineregister(FILE *out, struct functionhead *funclistit, struct config *cfg)
94 struct functionarg *arglistit;
95 int count, isvoid, nquad = 0, narg = 0;
96 char *type;
98 isvoid = strcmp(funclistit->type, "void") == 0
99 || strcmp(funclistit->type, "VOID") == 0;
101 fprintf(out,
102 "\n"
103 "#define __%s_WB(__%s",
104 funclistit->name, cfg->libbase
106 for (arglistit = funclistit->arguments, count = 1;
107 arglistit!=NULL;
108 arglistit = arglistit->next, count++
111 fprintf(out, ", __arg%d", count);
112 if (strchr(arglistit->reg, '/') != NULL) {
113 nquad++;
114 } else {
115 narg++;
118 fprintf(out,
119 ") ({\\\n"
121 fprintf(out,
122 " AROS_LIBREQ(%s,%d)\\\n",
123 cfg->libbase, funclistit->version
125 if (nquad == 0)
127 fprintf(out,
128 " AROS_LC%d%s%s(%s, %s, \\\n",
129 funclistit->argcount, funclistit->unusedlibbase ? "I" : "",
130 (isvoid) ? "NR" : "",
131 funclistit->type, funclistit->name
134 for (arglistit = funclistit->arguments, count = 1;
135 arglistit!=NULL;
136 arglistit = arglistit->next, count++
139 type = getargtype(arglistit);
140 assert(type != NULL);
141 fprintf(out,
142 " AROS_LCA(%s,(__arg%d),%s), \\\n",
143 type, count, arglistit->reg
145 free(type);
148 else
150 if (narg) {
151 fprintf(out,
152 " AROS_LC%dQUAD%d%s(%s, %s,\\\n",
153 narg, nquad, (isvoid) ? "NR" : "",
154 funclistit->type, funclistit->name
156 } else {
157 fprintf(out,
158 " AROS_LCQUAD%d%s(%s, %s,\\\n",
159 nquad, (isvoid) ? "NR" : "",
160 funclistit->type, funclistit->name
164 for (arglistit = funclistit->arguments, count = 1;
165 arglistit != NULL;
166 arglistit = arglistit->next, count++
169 char *quad2 = strchr(arglistit->reg, '/');
171 arglistit->reg[2] = 0;
172 type = getargtype(arglistit);
173 assert(type != NULL);
175 if (quad2 != NULL) {
176 *quad2 = 0;
177 fprintf(out,
178 " AROS_LCAQUAD(%s, (__arg%d), %s, %s), \\\n",
179 type, count, arglistit->reg, quad2+1
181 *quad2 = '/';
182 } else {
183 fprintf(out,
184 " AROS_LCA(%s, (__arg%d), %s), \\\n",
185 type, count, arglistit->reg
188 free(type);
191 fprintf(out,
192 " %s, (__%s), %u, %s);\\\n})\n\n",
193 cfg->libbasetypeptrextern, cfg->libbase, funclistit->lvo, cfg->basename
196 fprintf(out, "#define %s(", funclistit->name);
197 for (arglistit = funclistit->arguments, count = 1;
198 arglistit != NULL;
199 arglistit = arglistit->next, count++
202 if (arglistit != funclistit->arguments)
203 fprintf(out, ", ");
204 fprintf(out, "arg%d", count);
206 fprintf(out, ") \\\n __%s_WB(__aros_getbase_%s()",
207 funclistit->name, cfg->libbase
209 for (arglistit = funclistit->arguments, count = 1;
210 arglistit != NULL;
211 arglistit = arglistit->next, count++
213 fprintf(out, ", (arg%d)", count);
214 fprintf(out, ")\n");
217 void
218 writedefinevararg(FILE *out, struct functionhead *funclistit, struct config *cfg)
220 struct functionarg *arglistit = funclistit->arguments;
221 char isvararg = 0, *varargname, *lastname;
223 /* Go to last argument */
224 if (arglistit == NULL)
225 return;
227 while (arglistit->next != NULL) arglistit = arglistit->next;
229 lastname = getargname(arglistit);
230 assert(lastname != NULL);
232 if (*(funclistit->name + strlen(funclistit->name) - 1) == 'A')
234 isvararg = 1;
235 varargname = strdup(funclistit->name);
236 varargname[strlen(funclistit->name)-1] = '\0';
238 else if (strcmp(funclistit->name + strlen(funclistit->name) - 7, "TagList") == 0)
240 isvararg = 1;
241 /* TagList has to be changed in Tags at the end of the functionname */
242 varargname = strdup(funclistit->name);
243 varargname[strlen(funclistit->name)-4] = 's';
244 varargname[strlen(funclistit->name)-3] = '\0';
246 else if (strcmp(funclistit->name + strlen(funclistit->name) - 4, "Args") == 0
247 && (strcasecmp(lastname, "args") == 0 || strcasecmp(lastname, "arglist") == 0)
250 isvararg = 1;
251 varargname = strdup(funclistit->name);
252 varargname[strlen(funclistit->name)-4] = '\0';
254 else if ((funclistit->name[0] == 'V') && (strncmp(arglistit->arg, "va_list", 7) == 0))
256 isvararg = 2;
257 varargname = malloc(strlen(funclistit->name));
258 strcpy(varargname, &funclistit->name[1]);
260 else
262 char *p;
264 if (strncmp(arglistit->arg, "const", 5) == 0) {
265 p = arglistit->arg + 5;
266 while (isspace(*p)) p++;
267 } else
268 p = arglistit->arg;
269 if (strncmp(p, "struct", 6)==0)
271 p += 6;
272 while (isspace(*p)) p++;
273 if (strncmp(p, "TagItem", 7) == 0)
275 p += 7;
276 while (isspace(*p)) p++;
278 if (*p == '*')
280 isvararg = 1;
281 varargname = malloc(strlen(funclistit->name) + 5);
282 strcpy(varargname, funclistit->name);
283 strcat(varargname, "Tags");
289 if (isvararg == 1)
291 int count;
292 char *type;
294 fprintf(out,
295 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
296 "#define %s(",
297 cfg->includenameupper, varargname
299 for (arglistit = funclistit->arguments, count = 1;
300 arglistit != NULL && arglistit->next != NULL;
301 arglistit = arglistit->next, count++
304 fprintf(out, "arg%d, ", count);
306 fprintf(out,
307 "...) \\\n"
308 "({ \\\n"
309 " %s(",
310 funclistit->name
312 for (arglistit = funclistit->arguments, count = 1;
313 arglistit != NULL;
314 arglistit = arglistit->next, count++
317 if (arglistit != funclistit->arguments)
318 fprintf(out, ", ");
320 if (arglistit->next == NULL)
322 type = getargtype(arglistit);
323 assert(type != NULL);
324 fprintf(out, "(%s)(IPTR []){ AROS_PP_VARIADIC_CAST2IPTR(__VA_ARGS__) }", type);
325 free(type);
327 else
328 fprintf(out, "(arg%d)", count);
330 fprintf(out,
331 "); \\\n"
332 "})\n"
333 "#endif /* !NO_INLINE_STDARG */\n"
336 free(varargname);
338 else if (isvararg == 2)
340 int count;
341 struct functionarg *lastarg;
343 fprintf(out,
344 "\n#if !defined(NO_INLINE_STDARG) && !defined(%s_NO_INLINE_STDARG)\n"
345 "static inline %s __%s_WB(%s __%s",
346 cfg->includenameupper,
347 funclistit->type, varargname, cfg->libbasetypeptrextern, cfg->libbase
349 for (arglistit = funclistit->arguments;
350 arglistit != NULL && arglistit->next != NULL;
351 arglistit = arglistit->next
354 fprintf(out, ", %s", arglistit->arg);
355 lastarg = arglistit;
357 fprintf(out, ", ...)\n");
359 fprintf(out,
360 "{\n"
361 " %s retval;\n"
362 " va_list args;\n"
363 "\n"
364 " va_start(args, %s);\n"
365 " retval = __%s_WB(__%s, ",
366 funclistit->type,
367 getargname(lastarg),
368 funclistit->name, cfg->libbase
370 for (arglistit = funclistit->arguments;
371 arglistit != NULL && arglistit->next != NULL;
372 arglistit = arglistit->next
375 fprintf(out, "%s, ", getargname(arglistit));
377 fprintf(out,
378 "args);\n"
379 " va_end(args);\n"
380 " return retval;\n"
381 "}\n"
382 "#define %s(",
383 varargname
385 for (arglistit = funclistit->arguments, count = 1;
386 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
387 arglistit = arglistit->next, count++
390 fprintf(out, "arg%d, ", count);
392 fprintf(out,
393 "...) __%s_WB(%s, ",
394 varargname, cfg->libbase
396 for (arglistit = funclistit->arguments, count = 1;
397 arglistit != NULL && arglistit->next != NULL && arglistit->next->next != NULL;
398 arglistit = arglistit->next, count++
401 fprintf(out, "(arg%d), ", count);
403 fprintf(out,
404 "__VA_ARGS__)\n"
405 "#endif /* !NO_INLINE_STDARG */\n"
408 free(varargname);
412 void
413 writealiases(FILE *out, struct functionhead *funclistit, struct config *cfg)
415 struct stringlist *aliasesit;
417 for (aliasesit = funclistit->aliases;
418 aliasesit != NULL;
419 aliasesit = aliasesit->next
422 fprintf(out, "#define %s %s\n", aliasesit->s, funclistit->name);