4 * Copyright 2002 Ove Kaaven
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
39 static int indentation
= 0;
41 static void indent(int delta
)
44 if (delta
< 0) indentation
+= delta
;
45 for (c
=0; c
<indentation
; c
++) fprintf(header
, " ");
46 if (delta
> 0) indentation
+= delta
;
49 int is_void(type_t
*t
, var_t
*v
)
51 if (v
&& v
->ptr_level
) return 0;
52 if (!t
->type
&& !t
->ref
) return 1;
56 static void write_pident(FILE *h
, var_t
*v
)
59 for (c
=0; c
<v
->ptr_level
; c
++) {
62 if (v
->name
) fprintf(h
, "%s", v
->name
);
65 void write_name(FILE *h
, var_t
*v
)
67 fprintf(h
, "%s", v
->name
);
70 char* get_name(var_t
*v
)
75 static void write_array(FILE *h
, expr_t
*v
)
78 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
81 if (v
->type
== EXPR_NUM
)
82 fprintf(h
, "%ld", v
->u
.lval
); /* statically sized array */
84 fprintf(h
, "1"); /* dynamically sized array */
92 static void write_field(FILE *h
, var_t
*v
)
97 write_type(h
, v
->type
, NULL
, v
->tname
);
102 write_array(h
, v
->array
);
107 static void write_fields(FILE *h
, var_t
*v
)
111 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
114 if (v
== first
) break;
119 static void write_enums(FILE *h
, var_t
*v
)
122 while (NEXT_LINK(v
)) v
= NEXT_LINK(v
);
128 fprintf(h
, " = %ld", v
->lval
);
137 void write_type(FILE *h
, type_t
*t
, var_t
*v
, char *n
)
141 if (n
) fprintf(h
, "%s", n
);
143 if (t
->is_const
) fprintf(h
, "const ");
145 if (t
->sign
> 0) fprintf(h
, "signed ");
146 else if (t
->sign
< 0) fprintf(h
, "unsigned ");
149 if (t
->ref
) fprintf(h
, t
->ref
->name
);
150 else fprintf(h
, "byte");
153 if (t
->ref
) fprintf(h
, t
->ref
->name
);
154 else fprintf(h
, "char");
157 fprintf(h
, "wchar_t");
161 if (t
->ref
) fprintf(h
, t
->ref
->name
);
162 else fprintf(h
, "short");
166 if (t
->ref
) fprintf(h
, t
->ref
->name
);
167 else fprintf(h
, "long");
170 if (t
->ref
) fprintf(h
, t
->ref
->name
);
171 else fprintf(h
, "__int64");
177 fprintf(h
, "double");
181 if (t
->defined
&& !t
->written
) {
182 if (t
->name
) fprintf(h
, "enum %s {\n", t
->name
);
183 else fprintf(h
, "enum {\n");
185 write_enums(h
, t
->fields
);
189 else fprintf(h
, "enum %s", t
->name
);
192 if (t
->defined
&& !t
->written
) {
193 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
194 else fprintf(h
, "struct {\n");
196 write_fields(h
, t
->fields
);
200 else fprintf(h
, "struct %s", t
->name
);
202 case RPC_FC_ENCAPSULATED_UNION
:
203 if (t
->defined
&& !t
->written
) {
204 var_t
*d
= t
->fields
;
205 if (t
->name
) fprintf(h
, "struct %s {\n", t
->name
);
206 else fprintf(h
, "struct {\n");
210 fprintf(h
, "union {\n");
212 write_fields(h
, NEXT_LINK(d
));
214 fprintf(h
, "} u;\n");
218 else fprintf(h
, "struct %s", t
->name
);
220 case RPC_FC_NON_ENCAPSULATED_UNION
:
221 if (t
->defined
&& !t
->written
) {
222 if (t
->name
) fprintf(h
, "union %s {\n", t
->name
);
223 else fprintf(h
, "union {\n");
225 write_fields(h
, t
->fields
);
229 else fprintf(h
, "union %s", t
->name
);
232 fprintf(h
, "(unknown-type:%d)", t
->type
);
237 write_type(h
, t
->ref
, NULL
, t
->name
);
239 else fprintf(h
, "void");
243 for (c
=0; c
<v
->ptr_level
; c
++) {
249 void write_typedef(type_t
*type
, var_t
*names
)
251 char *tname
= names
->tname
;
252 while (NEXT_LINK(names
)) names
= NEXT_LINK(names
);
253 fprintf(header
, "typedef ");
254 write_type(header
, type
, NULL
, tname
);
255 fprintf(header
, " ");
257 write_pident(header
, names
);
258 if (PREV_LINK(names
))
259 fprintf(header
, ", ");
260 names
= PREV_LINK(names
);
262 fprintf(header
, ";\n\n");
265 /********** INTERFACES **********/
267 uuid_t
*get_uuid(attr_t
*a
)
270 if (a
->type
== ATTR_UUID
) return a
->u
.pval
;
276 int is_object(attr_t
*a
)
279 if (a
->type
== ATTR_OBJECT
) return 1;
285 int is_local(attr_t
*a
)
288 if (a
->type
== ATTR_LOCAL
) return 1;
294 var_t
*is_callas(attr_t
*a
)
297 if (a
->type
== ATTR_CALLAS
) return a
->u
.pval
;
303 static void write_method_def(type_t
*iface
)
305 func_t
*cur
= iface
->funcs
;
306 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
307 fprintf(header
, "#define %s_METHODS", iface
->name
);
309 var_t
*def
= cur
->def
;
310 if (!is_callas(def
->attrs
)) {
311 var_t
*arg
= cur
->args
;
315 while (NEXT_LINK(arg
)) {
316 arg
= NEXT_LINK(arg
);
320 fprintf(header
, " \\\n");
321 if (!is_void(def
->type
, def
)) {
323 fprintf(header
, " ICOM_METHOD%d (", argc
);
325 fprintf(header
, " ICOM_METHOD (");
326 write_type(header
, def
->type
, def
, def
->tname
);
327 fprintf(header
, ",");
330 fprintf(header
, " ICOM_VMETHOD%d(", argc
);
332 fprintf(header
, " ICOM_VMETHOD (");
333 write_name(header
, def
);
335 fprintf(header
, ",");
336 write_type(header
, arg
->type
, arg
, arg
->tname
);
337 fprintf(header
, ",");
338 write_name(header
,arg
);
339 arg
= PREV_LINK(arg
);
341 fprintf(header
, ")");
343 cur
= PREV_LINK(cur
);
345 fprintf(header
, "\n");
348 static int write_method_macro(type_t
*iface
, char *name
)
351 func_t
*cur
= iface
->funcs
;
352 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
354 if (iface
->ref
) idx
= write_method_macro(iface
->ref
, name
);
356 fprintf(header
, "/*** %s methods ***/\n", iface
->name
);
358 var_t
*def
= cur
->def
;
359 if (!is_callas(def
->attrs
)) {
360 var_t
*arg
= cur
->args
;
364 arg
= NEXT_LINK(arg
);
368 fprintf(header
, "#define %s_", name
);
369 write_name(header
,def
);
370 fprintf(header
, "(p");
371 for (c
=0; c
<argc
; c
++)
372 fprintf(header
, ",%c", c
+'a');
373 fprintf(header
, ") ");
376 fprintf(header
, "ICOM_CALL%d(", argc
);
378 fprintf(header
, "ICOM_CALL(");
379 write_name(header
,def
);
380 fprintf(header
, ",p");
381 for (c
=0; c
<argc
; c
++)
382 fprintf(header
, ",%c", c
+'a');
383 fprintf(header
, ")\n");
384 if (cur
->idx
== -1) cur
->idx
= idx
;
385 else if (cur
->idx
!= idx
) yyerror("BUG: method index mismatch in write_method_macro");
388 cur
= PREV_LINK(cur
);
393 void write_method_args(FILE *h
, var_t
*arg
, char *name
)
396 while (NEXT_LINK(arg
))
397 arg
= NEXT_LINK(arg
);
399 fprintf(h
, " %s* This", name
);
402 write_type(h
, arg
->type
, arg
, arg
->tname
);
405 arg
= PREV_LINK(arg
);
409 static void write_method_proto(type_t
*iface
)
411 func_t
*cur
= iface
->funcs
;
412 while (NEXT_LINK(cur
)) cur
= NEXT_LINK(cur
);
414 var_t
*def
= cur
->def
;
415 var_t
*cas
= is_callas(def
->attrs
);
416 if (!is_local(def
->attrs
)) {
417 /* proxy prototype */
418 write_type(header
, def
->type
, def
, def
->tname
);
419 fprintf(header
, " CALLBACK %s_", iface
->name
);
420 write_name(header
,def
);
421 fprintf(header
, "_Proxy(\n");
422 write_method_args(header
, cur
->args
, iface
->name
);
423 fprintf(header
, ");\n");
425 fprintf(header
, "void __RPC_STUB %s_", iface
->name
);
426 write_name(header
,def
);
427 fprintf(header
, "_Stub(\n");
428 fprintf(header
, " struct IRpcStubBuffer* This,\n");
429 fprintf(header
, " struct IRpcChannelBuffer* pRpcChannelBuffer,\n");
430 fprintf(header
, " PRPC_MESSAGE pRpcMessage,\n");
431 fprintf(header
, " DWORD* pdwStubPhase);\n");
434 func_t
*m
= iface
->funcs
;
435 while (m
&& strcmp(get_name(m
->def
), cas
->name
))
438 var_t
*mdef
= m
->def
;
439 /* proxy prototype - use local prototype */
440 write_type(header
, mdef
->type
, mdef
, mdef
->tname
);
441 fprintf(header
, " CALLBACK %s_", iface
->name
);
442 write_name(header
, mdef
);
443 fprintf(header
, "_Proxy(\n");
444 write_method_args(header
, m
->args
, iface
->name
);
445 fprintf(header
, ");\n");
446 /* stub prototype - use remotable prototype */
447 write_type(header
, def
->type
, def
, def
->tname
);
448 fprintf(header
, " __RPC_STUB %s_", iface
->name
);
449 write_name(header
, mdef
);
450 fprintf(header
, "_Stub(\n");
451 write_method_args(header
, cur
->args
, iface
->name
);
452 fprintf(header
, ");\n");
455 yywarning("invalid call_as attribute (%s -> %s)\n", get_name(def
), cas
->name
);
459 cur
= PREV_LINK(cur
);
463 void write_forward(type_t
*iface
)
465 /* C/C++ forwards should only be written for object interfaces, so if we
466 * have a full definition we only write one if we find [object] among the
467 * attributes - however, if we don't have a full definition at this point
468 * (i.e. this is an IDL forward), then we also assume that it is an object
469 * interface, since non-object interfaces shouldn't need forwards */
470 if ((!iface
->defined
|| is_object(iface
->attrs
)) && !iface
->written
) {
471 fprintf(header
, "typedef struct %s %s;\n", iface
->name
, iface
->name
);
472 iface
->written
= TRUE
;
476 void write_guid(type_t
*iface
)
478 uuid_t
*uuid
= get_uuid(iface
->attrs
);
480 fprintf(header
, "DEFINE_GUID(IID_%s, 0x%08lx, 0x%04x, 0x%04x, 0x%02x,0x%02x, 0x%02x,0x%02x,0x%02x,0x%02x,0x%02x,0x%02x);\n",
481 iface
->name
, uuid
->Data1
, uuid
->Data2
, uuid
->Data3
, uuid
->Data4
[0], uuid
->Data4
[1],
482 uuid
->Data4
[2], uuid
->Data4
[3], uuid
->Data4
[4], uuid
->Data4
[5], uuid
->Data4
[6], uuid
->Data4
[7]);
485 void write_interface(type_t
*iface
)
487 if (!is_object(iface
->attrs
)) {
488 if (!iface
->funcs
) return;
489 yywarning("RPC interfaces not supported yet\n");
494 yywarning("%s has no methods", iface
->name
);
498 fprintf(header
, "/*****************************************************************************\n");
499 fprintf(header
, " * %s interface\n", iface
->name
);
500 fprintf(header
, " */\n");
502 write_forward(iface
);
503 fprintf(header
, "#define ICOM_INTERFACE %s\n", iface
->name
);
504 write_method_def(iface
);
505 fprintf(header
, "#define %s_IMETHODS \\\n", iface
->name
);
507 fprintf(header
, " %s_IMETHODS \\\n", iface
->ref
->name
);
508 fprintf(header
, " %s_METHODS\n", iface
->name
);
510 fprintf(header
, "ICOM_DEFINE(%s,%s)\n", iface
->name
, iface
->ref
->name
);
512 fprintf(header
, "ICOM_DEFINE1(%s)\n", iface
->name
);
513 fprintf(header
, "#undef ICOM_INTERFACE\n");
514 fprintf(header
, "\n");
515 write_method_macro(iface
, iface
->name
);
516 fprintf(header
, "\n");
517 write_method_proto(iface
);
518 fprintf(header
, "\n");
520 if (!is_local(iface
->attrs
))