Fix create_patch macro.
[AROS.git] / tools / genmodule / writeincinterfaces.c
blob02d18556e954c578c5b61c1496d25eb77b598489
1 /*
2 * Copyright (C) 2012, The AROS Development Team
3 * All right reserved.
4 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
6 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
8 * Generate include/interface/XXXX.h files
9 */
10 #include "genmodule.h"
12 static void writeincinterface(struct config *cfg, struct interfaceinfo *in)
14 FILE *out;
15 char line[256], *banner;
16 struct functionhead *funclistit;
17 int maxlvo = -1;
19 snprintf(line, 255, "%s/interface/%s.h", cfg->gendir, in->interfacename);
20 out = fopen(line, "w");
22 if (out == NULL)
24 perror(line);
25 exit(20);
28 banner = getBanner(cfg);
29 fprintf(out,
30 "#ifndef INTERFACE_%s_H\n"
31 "#define INTERFACE_%s_H\n"
32 "\n"
33 "%s"
34 "\n"
35 "/*\n"
36 " Desc: interface inlines for %s\n"
37 "*/\n"
38 "\n"
39 "#include <exec/types.h>\n"
40 "#include <proto/oop.h>\n"
41 "\n"
42 "#define IID_%-32s \"%s\"\n"
43 "\n"
44 , in->interfacename
45 , in->interfacename
46 , banner
47 , in->interfacename
48 , in->interfacename
49 , in->interfaceid
51 freeBanner(banner);
53 /* Emit the get-the-methodbase stub */
54 fprintf(out,
55 "#if !defined(%s) && !defined(__OOP_NOMETHODBASES__) && !defined(__%s_NOMETHODBASE__)\n"
56 "#define %s %s_GetMethodBase(__obj)\n"
57 "\n"
58 "static inline OOP_MethodID %s_GetMethodBase(OOP_Object *obj)\n"
59 "{\n"
60 " static OOP_MethodID %s_mid;\n"
61 " if (!%s_mid) {\n"
62 " struct Library *OOPBase = (struct Library *)OOP_OCLASS(obj)->OOPBasePtr;\n"
63 " %s_mid = OOP_GetMethodID(IID_%s, 0);\n"
64 " }\n"
65 " return %s_mid;\n"
66 "}\n"
67 "#endif\n"
68 "\n"
69 ,in->methodbase
70 ,in->interfacename
71 ,in->methodbase
72 ,in->interfacename
73 ,in->interfacename
74 ,in->interfacename
75 ,in->interfacename
76 ,in->interfacename
77 ,in->interfacename
78 ,in->interfacename
81 if (in->attributelist) {
82 fprintf(out,
83 "#define %-32s __I%s\n"
84 "\n"
85 "#if !defined(__OOP_NOATTRBASES__) && !defined(__%s_NOATTRBASE__)\n"
86 "extern OOP_AttrBase %s;\n"
87 "#endif\n"
88 "\n"
89 "enum\n"
90 "{\n"
91 , in->attributebase
92 , in->interfacename
93 , in->interfacename
94 , in->attributebase
98 /* Define all the attribute ids */
99 for (funclistit = in->attributelist; funclistit!=NULL; funclistit = funclistit->next)
101 fprintf(out,
102 " ao%s_%s = %d,"
103 , in->interfacename
104 , funclistit->name
105 , funclistit->lvo
107 if (funclistit->comment)
109 fprintf(out,
110 " /* %s */"
111 , funclistit->comment
114 fprintf(out, "\n");
115 if ((int)funclistit->lvo > maxlvo)
116 maxlvo = funclistit->lvo;
119 if (maxlvo >= 0) {
120 fprintf(out,
121 " num_%s_Attrs = %d,\n"
122 "};\n"
123 "\n"
124 , in->interfacename
125 , maxlvo+1
129 for (funclistit = in->attributelist; funclistit!=NULL; funclistit = funclistit->next)
131 fprintf(out,
132 "#define a%s_%-32s (%s + ao%s_%s)\n"
133 , in->interfacename
134 , funclistit->name
135 , in->attributebase
136 , in->interfacename
137 , funclistit->name
141 if (!in->methodlist)
142 goto done;
144 /* Define method ids */
145 fprintf(out,
146 "\n"
147 "enum {\n"
149 maxlvo = -1;
150 for (funclistit = in->methodlist; funclistit!=NULL; funclistit = funclistit->next)
152 fprintf(out,
153 " mo%s_%s = %d,\n"
154 , in->interfacename
155 , funclistit->name
156 , funclistit->lvo
158 if ((int)funclistit->lvo > maxlvo)
159 maxlvo = funclistit->lvo;
162 fprintf(out,
163 " num_%s_Methods = %d\n"
164 "};\n"
165 "\n"
166 , in->interfacename
167 , maxlvo+1
170 /* Define method stubs */
171 for (funclistit = in->methodlist; funclistit!=NULL; funclistit = funclistit->next)
173 struct functionarg *arg;
175 /* Define message types */
176 fprintf(out,
177 "struct p%s_%s\n"
178 "{\n"
179 " OOP_MethodID mID;\n"
180 , in->interfacename
181 , funclistit->name
184 for (arg = funclistit->arguments; arg; arg = arg->next)
186 fprintf(out,
187 " %s;\n"
188 , arg->arg
192 fprintf(out,
193 "};\n"
194 "\n"
197 if (funclistit->comment)
199 fprintf(out,
200 "/* %s */\n"
201 , funclistit->comment
205 fprintf(out,
206 "#define %s_%s(obj, args...) \\\n"
207 " ({OOP_Object *__obj = obj;\\\n"
208 " %s_%s_(%s, __obj ,##args); })\n"
209 "\n"
210 ,in->methodstub
211 ,funclistit->name
212 ,in->methodstub
213 ,funclistit->name
214 ,in->methodbase
217 fprintf(out,
218 "static inline %s %s_%s_(OOP_MethodID __%s, OOP_Object *__obj"
219 , funclistit->type
220 , in->methodstub
221 , funclistit->name
222 , in->methodbase
225 for (arg = funclistit->arguments; arg; arg = arg->next)
227 fprintf(out,
228 ", %s"
229 , arg->arg
233 fprintf(out,
234 ")\n"
235 "{\n"
236 " struct p%s_%s p;\n"
237 " p.mID = __%s + mo%s_%s;\n"
238 , in->interfacename
239 , funclistit->name
240 , in->methodbase
241 , in->interfacename
242 , funclistit->name
245 for (arg = funclistit->arguments; arg; arg = arg->next)
247 const char *ptr;
248 char *name;
250 ptr = arg->arg + strlen(arg->arg) - 1;
251 while (isspace(*ptr))
252 ptr--;
254 while (isalnum(*ptr) || (*ptr == '_'))
255 ptr--;
257 if (*ptr == ')') {
258 /* Function prototype */
259 const char *cp;
260 ptr = strchr(arg->arg, '(');
261 while (!(isalnum(*ptr) || (*ptr == '_'))) ptr++;
262 cp = ptr;
263 while (isalnum(*ptr) || (*ptr == '_')) ptr++;
264 name = malloc(ptr - cp + 1);
265 memcpy(name, cp, ptr - cp);
266 name[ptr-cp] = 0;
267 } else {
268 name = strdup(ptr + 1);
271 fprintf(out,
272 " p.%s = %s;\n"
273 , name
274 , name
277 free(name);
280 fprintf(out,
281 " %s(%s)OOP_DoMethod(__obj, &p.mID);\n"
282 "}\n"
283 "\n"
284 , (strcasecmp(funclistit->type, "void") == 0) ? "" : "return "
285 , funclistit->type
289 done:
290 fprintf(out,
291 "#endif /* INTERFACE_%s_H */\n"
292 , in->interfacename
295 fclose(out);
298 void writeincinterfaces(struct config *cfg)
300 struct interfaceinfo *in;
302 for (in = cfg->interfacelist; in ; in = in->next) {
303 writeincinterface(cfg, in);