5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
43 /* Berkeley yacc (byacc) doesn't seem to know about these */
44 /* Some *BSD supplied versions do define these though */
46 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
49 # define YYLEX yylex()
52 #elif defined(YYBISON)
53 /* Bison was used for original development */
54 /* #define YYEMPTY -2 */
55 /* #define YYLEX yylex() */
58 /* No yacc we know yet */
59 # if !defined(YYEMPTY) || !defined(YYLEX)
60 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
61 # elif defined(__GNUC__) /* gcc defines the #warning directive */
62 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
63 /* #else we just take a chance that it works... */
67 static attr_t *make_attr(enum attr_type type);
68 static attr_t *make_attrv(enum attr_type type, unsigned long val);
69 static attr_t *make_attrp(enum attr_type type, void *val);
70 static expr_t *make_expr(enum expr_type type);
71 static expr_t *make_exprl(enum expr_type type, long val);
72 static expr_t *make_exprs(enum expr_type type, char *val);
73 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
74 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
75 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
76 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
77 static type_t *make_type(unsigned char type, type_t *ref);
78 static typeref_t *make_tref(char *name, type_t *ref);
79 static typeref_t *uniq_tref(typeref_t *ref);
80 static type_t *type_ref(typeref_t *ref);
81 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
82 static ifref_t *make_ifref(type_t *iface);
83 static var_t *make_var(char *name);
84 static func_t *make_func(var_t *def, var_t *args);
85 static type_t *make_class(char *name);
86 static type_t *make_safearray(void);
88 static type_t *reg_type(type_t *type, const char *name, int t);
89 static type_t *reg_types(type_t *type, var_t *names, int t);
90 static type_t *find_type(const char *name, int t);
91 static type_t *find_type2(char *name, int t);
92 static type_t *get_type(unsigned char type, char *name, int t);
93 static type_t *get_typev(unsigned char type, var_t *name, int t);
94 static int get_struct_type(var_t *fields);
96 static var_t *reg_const(var_t *var);
97 static var_t *find_const(char *name, int f);
99 static void write_libid(const char *name, const attr_t *attr);
100 static void write_clsid(type_t *cls);
101 static void write_diid(type_t *iface);
102 static void write_iid(type_t *iface);
104 static int compute_method_indexes(type_t *iface);
110 static type_t std_bool = { "boolean" };
111 static type_t std_int = { "int" };
112 static type_t std_int64 = { "__int64" };
113 static type_t std_uhyper = { "MIDL_uhyper" };
129 %token <str> aIDENTIFIER
130 %token <str> aKNOWNTYPE
131 %token <num> aNUM aHEXNUM
136 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
137 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
138 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
139 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
140 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
142 %token tDEFAULTCOLLELEM
144 %token tDEFAULTVTABLE
146 %token tDISPINTERFACE
147 %token tDLLNAME tDOUBLE tDUAL
149 %token tENTRY tENUM tERRORSTATUST
150 %token tEXPLICITHANDLE tEXTERN
155 %token tHELPCONTEXT tHELPFILE
156 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
158 %token tHYPER tID tIDEMPOTENT
160 %token tIMMEDIATEBIND
161 %token tIMPLICITHANDLE
162 %token tIMPORT tIMPORTLIB
168 %token tLENGTHIS tLIBRARY
175 %token tNONEXTENSIBLE
176 %token tOBJECT tODL tOLEAUTOMATION
179 %token tPOINTERDEFAULT
181 %token tPROPGET tPROPPUT tPROPPUTREF
185 %token tREADONLY tREF
193 %token tSIZEIS tSIZEOF
197 %token tSTRING tSTRUCT
198 %token tSWITCH tSWITCHIS tSWITCHTYPE
210 %token tWCHAR tWIREMARSHAL
212 %type <attr> m_attributes attributes attrib_list attribute
213 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
214 %type <expr> array array_list
215 %type <type> inherit interface interfacehdr interfacedef interfacedec
216 %type <type> dispinterface dispinterfacehdr dispinterfacedef
217 %type <type> module modulehdr moduledef
218 %type <type> base_type int_std
219 %type <type> enumdef structdef typedef uniondef
220 %type <ifref> gbl_statements coclass_ints coclass_int
222 %type <var> m_args no_args args arg
223 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
224 %type <var> m_ident t_ident ident p_ident pident pident_list
225 %type <var> dispint_props
226 %type <func> funcdef int_statements
227 %type <func> dispint_meths
228 %type <type> coclass coclasshdr coclassdef
229 %type <num> pointer_type version
230 %type <str> libraryhdr
246 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
249 gbl_statements: { $$ = NULL; }
250 | gbl_statements interfacedec { $$ = $1; }
251 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
252 | gbl_statements coclass ';' { $$ = $1;
253 reg_type($2, $2->name, 0);
254 if (!parse_only && do_header) write_coclass_forward($2);
256 | gbl_statements coclassdef { $$ = $1;
258 reg_type($2, $2->name, 0);
259 if (!parse_only && do_header) write_coclass_forward($2);
261 | gbl_statements moduledef { $$ = $1; add_module($2); }
262 | gbl_statements librarydef { $$ = $1; }
263 | gbl_statements statement { $$ = $1; }
267 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
268 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
269 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
270 | imp_statements coclassdef { if (!parse_only) add_coclass($2);
271 reg_type($2, $2->name, 0);
272 if (!parse_only && do_header) write_coclass_forward($2);
274 | imp_statements moduledef { if (!parse_only) add_module($2); }
275 | imp_statements statement {}
276 | imp_statements importlib {}
279 int_statements: { $$ = NULL; }
280 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
281 | int_statements statement { $$ = $1; }
285 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
287 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
288 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
290 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
292 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
295 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
297 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
298 if (!do_import($2)) yychar = aEOF; }
300 import: import_start imp_statements aEOF {}
303 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
306 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
308 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
309 if (!parse_only && do_header) write_library($2, $1);
310 if (!parse_only && do_idfile) write_libid($2, $1);
313 librarydef: library_start imp_statements '}' { end_typelib(); }
316 m_args: { $$ = NULL; }
320 no_args: tVOID { $$ = NULL; }
324 | args ',' arg { LINK($3, $1); $$ = $3; }
328 /* split into two rules to get bison to resolve a tVOID conflict */
329 arg: attributes type pident array { $$ = $3;
330 set_type($$, $2, $4);
333 | type pident array { $$ = $2;
334 set_type($$, $1, $3);
336 | attributes type pident '(' m_args ')' { $$ = $3;
338 set_type($$, $2, NULL);
342 | type pident '(' m_args ')' { $$ = $2;
344 set_type($$, $1, NULL);
349 array: { $$ = NULL; }
350 | '[' array_list ']' { $$ = $2; }
351 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
354 array_list: m_expr /* size of first dimension is optional */
355 | array_list ',' expr { LINK($3, $1); $$ = $3; }
356 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
359 m_attributes: { $$ = NULL; }
364 '[' attrib_list ']' { $$ = $2;
366 yyerror("empty attribute lists unsupported");
370 attrib_list: attribute
371 | attrib_list ',' attribute { if ($3) { LINK($3, $1); $$ = $3; }
374 | attrib_list ']' '[' attribute { if ($4) { LINK($4, $1); $$ = $4; }
379 attribute: { $$ = NULL; }
380 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
381 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
382 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
383 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
384 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
385 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
386 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
387 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
388 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
389 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
390 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
391 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
392 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
393 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
394 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
395 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
396 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
397 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
398 | tDUAL { $$ = make_attr(ATTR_DUAL); }
399 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
400 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
401 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
402 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
403 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
404 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
405 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
406 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
407 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
408 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
409 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
410 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
411 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
412 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
413 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
414 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
415 | tIN { $$ = make_attr(ATTR_IN); }
416 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
417 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
418 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
419 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
420 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
421 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
422 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
423 | tODL { $$ = make_attr(ATTR_ODL); }
424 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
425 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
426 | tOUT { $$ = make_attr(ATTR_OUT); }
427 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
428 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
429 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
430 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
431 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
432 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
433 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
434 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
435 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
436 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
437 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
438 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
439 | tSTRING { $$ = make_attr(ATTR_STRING); }
440 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
441 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
442 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
443 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
444 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
445 | tVARARG { $$ = make_attr(ATTR_VARARG); }
446 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
447 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
448 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
455 cases: { $$ = NULL; }
456 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
461 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
462 $$ = $4; if (!$$) $$ = make_var(NULL);
463 LINK(a, $$->attrs); $$->attrs = a;
465 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
466 $$ = $3; if (!$$) $$ = make_var(NULL);
467 LINK(a, $$->attrs); $$->attrs = a;
471 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
472 set_type($$, $2, NULL);
477 enums: { $$ = NULL; }
478 | enum_list ',' { $$ = $1; }
482 enum_list: enum { if (!$$->eval)
483 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
485 | enum_list ',' enum { LINK($3, $1); $$ = $3;
487 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
491 enum: ident '=' expr_const { $$ = reg_const($1);
493 $$->type = make_type(RPC_FC_LONG, &std_int);
495 | ident { $$ = reg_const($1);
496 $$->type = make_type(RPC_FC_LONG, &std_int);
500 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
501 $$->kind = TKIND_ENUM;
510 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
514 exprs: { $$ = make_expr(EXPR_VOID); }
519 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
523 m_expr: { $$ = make_expr(EXPR_VOID); }
527 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
528 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
529 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
530 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
531 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
532 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
533 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
534 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
535 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
536 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
537 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
538 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
539 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
540 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
541 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
542 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
543 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
544 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
545 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
546 | '(' expr ')' { $$ = $2; }
549 expr_list_const: expr_const
550 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
553 expr_const: expr { $$ = $1;
555 yyerror("expression is not constant");
559 externdef: tEXTERN tCONST type ident { $$ = $4;
560 set_type($$, $3, NULL);
564 fields: { $$ = NULL; }
565 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
570 field: s_field ';' { $$ = $1; }
571 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
572 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
576 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
580 m_attributes type callconv pident
581 '(' m_args ')' { set_type($4, $2, NULL);
583 $$ = make_func($4, $6);
584 if (is_attr($4->attrs, ATTR_IN)) {
585 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
590 m_ident: { $$ = NULL; }
594 t_ident: { $$ = NULL; }
595 | aIDENTIFIER { $$ = make_var($1); }
596 | aKNOWNTYPE { $$ = make_var($1); }
599 ident: aIDENTIFIER { $$ = make_var($1); }
600 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
601 | aKNOWNTYPE { $$ = make_var($<str>1); }
602 | tASYNC { $$ = make_var($<str>1); }
603 | tID { $$ = make_var($<str>1); }
604 | tLCID { $$ = make_var($<str>1); }
605 | tOBJECT { $$ = make_var($<str>1); }
606 | tRANGE { $$ = make_var($<str>1); }
607 | tRETVAL { $$ = make_var($<str>1); }
608 | tUUID { $$ = make_var($<str>1); }
609 | tVERSION { $$ = make_var($<str>1); }
612 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
613 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
615 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
616 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
618 case RPC_FC_CHAR: break;
619 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
620 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
621 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
623 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
628 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
629 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
630 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
631 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
632 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
633 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
634 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
641 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
642 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
643 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
644 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
645 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
646 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
647 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
650 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
651 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
652 if ($$->defined) yyerror("multiple definition error");
653 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
657 coclasshdr: attributes coclass { $$ = $2;
659 if (!parse_only && do_header)
661 if (!parse_only && do_idfile)
666 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
672 coclass_ints: { $$ = NULL; }
673 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
677 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
680 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
681 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
684 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
686 if ($$->defined) yyerror("multiple definition error");
687 attrs = make_attr(ATTR_DISPINTERFACE);
690 $$->ref = find_type("IDispatch", 0);
691 if (!$$->ref) yyerror("IDispatch is undefined");
693 if (!parse_only && do_header) write_forward($$);
697 dispint_props: tPROPERTIES ':' { $$ = NULL; }
698 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
701 dispint_meths: tMETHODS ':' { $$ = NULL; }
702 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
705 dispinterfacedef: dispinterfacehdr '{'
711 if (!parse_only && do_header) write_dispinterface($$);
712 if (!parse_only && do_idfile) write_diid($$);
714 /* FIXME: not sure how to handle this yet
715 | dispinterfacehdr '{' interface '}' { $$ = $1;
716 compute_method_indexes($$);
717 if (!parse_only && do_header) write_interface($$);
718 if (!parse_only && do_idfile) write_iid($$);
723 inherit: { $$ = NULL; }
724 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
727 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
728 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
731 interfacehdr: attributes interface { $$ = $2;
732 if ($$->defined) yyerror("multiple definition error");
735 if (!parse_only && do_header) write_forward($$);
739 interfacedef: interfacehdr inherit
740 '{' int_statements '}' { $$ = $1;
743 compute_method_indexes($$);
744 if (!parse_only && do_header) write_interface($$);
745 if (!parse_only && do_idfile) write_iid($$);
747 /* MIDL is able to import the definition of a base class from inside the
748 * definition of a derived class, I'll try to support it with this rule */
749 | interfacehdr ':' aIDENTIFIER
750 '{' import int_statements '}' { $$ = $1;
751 $$->ref = find_type2($3, 0);
752 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
754 compute_method_indexes($$);
755 if (!parse_only && do_header) write_interface($$);
756 if (!parse_only && do_idfile) write_iid($$);
758 | dispinterfacedef { $$ = $1; }
762 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
763 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
766 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
767 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
770 modulehdr: attributes module { $$ = $2;
775 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
777 /* FIXME: if (!parse_only && do_header) write_module($$); */
781 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
782 | tCONST p_ident { $$ = $2; /* FIXME */ }
787 | '(' pident ')' { $$ = $2; }
792 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
796 tREF { $$ = RPC_FC_RP; }
797 | tUNIQUE { $$ = RPC_FC_UP; }
798 | tPTR { $$ = RPC_FC_FP; }
801 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
802 /* overwrite RPC_FC_STRUCT with a more exact type */
803 $$->type = get_struct_type( $4 );
804 $$->kind = TKIND_RECORD;
812 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
813 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
814 | base_type { $$ = make_tref(NULL, $1); }
815 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
816 | enumdef { $$ = make_tref(NULL, $1); }
817 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
818 | structdef { $$ = make_tref(NULL, $1); }
819 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
820 | uniondef { $$ = make_tref(NULL, $1); }
821 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
822 | tSAFEARRAY '(' type ')' { $$ = make_tref(NULL, make_safearray()); }
825 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
826 $4->tname = tref->name;
830 if (!parse_only && do_header)
831 write_typedef($$, $4);
832 if (in_typelib && $$->attrs)
834 reg_types($$, $4, 0);
838 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
839 $$->kind = TKIND_UNION;
844 tSWITCH '(' s_field ')'
845 m_ident '{' cases '}' { var_t *u = $7;
846 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
847 $$->kind = TKIND_UNION;
848 if (!u) u = make_var("tagged_union");
849 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
850 u->type->kind = TKIND_UNION;
851 u->type->fields = $9;
852 u->type->defined = TRUE;
853 LINK(u, $5); $$->fields = u;
859 aNUM { $$ = MAKELONG($1, 0); }
860 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
865 static attr_t *make_attr(enum attr_type type)
867 attr_t *a = xmalloc(sizeof(attr_t));
874 static attr_t *make_attrv(enum attr_type type, unsigned long val)
876 attr_t *a = xmalloc(sizeof(attr_t));
883 static attr_t *make_attrp(enum attr_type type, void *val)
885 attr_t *a = xmalloc(sizeof(attr_t));
892 static expr_t *make_expr(enum expr_type type)
894 expr_t *e = xmalloc(sizeof(expr_t));
903 static expr_t *make_exprl(enum expr_type type, long val)
905 expr_t *e = xmalloc(sizeof(expr_t));
911 /* check for numeric constant */
912 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
913 /* make sure true/false value is valid */
914 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
921 static expr_t *make_exprs(enum expr_type type, char *val)
924 e = xmalloc(sizeof(expr_t));
930 /* check for predefined constants */
931 if (type == EXPR_IDENTIFIER) {
932 var_t *c = find_const(val, 0);
937 e->cval = c->eval->cval;
943 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
946 e = xmalloc(sizeof(expr_t));
952 /* check for cast of constant expression */
953 if (type == EXPR_SIZEOF) {
954 switch (tref->ref->type) {
971 case RPC_FC_ERROR_STATUS_T:
982 if (type == EXPR_CAST && expr->is_const) {
984 e->cval = expr->cval;
989 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
992 e = xmalloc(sizeof(expr_t));
998 /* check for compile-time optimization */
999 if (expr->is_const) {
1003 e->cval = -expr->cval;
1006 e->cval = ~expr->cval;
1009 e->is_const = FALSE;
1016 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1019 e = xmalloc(sizeof(expr_t));
1023 e->is_const = FALSE;
1025 /* check for compile-time optimization */
1026 if (expr1->is_const && expr2->is_const) {
1030 e->cval = expr1->cval + expr2->cval;
1033 e->cval = expr1->cval - expr2->cval;
1036 e->cval = expr1->cval * expr2->cval;
1039 e->cval = expr1->cval / expr2->cval;
1042 e->cval = expr1->cval | expr2->cval;
1045 e->cval = expr1->cval & expr2->cval;
1048 e->cval = expr1->cval << expr2->cval;
1051 e->cval = expr1->cval >> expr2->cval;
1054 e->is_const = FALSE;
1061 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1064 e = xmalloc(sizeof(expr_t));
1069 e->is_const = FALSE;
1071 /* check for compile-time optimization */
1072 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1076 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1079 e->is_const = FALSE;
1086 static type_t *make_type(unsigned char type, type_t *ref)
1088 type_t *t = xmalloc(sizeof(type_t));
1090 t->kind = TKIND_PRIMITIVE;
1097 t->ignore = parse_only;
1098 t->is_const = FALSE;
1102 t->typelib_idx = -1;
1107 static typeref_t *make_tref(char *name, type_t *ref)
1109 typeref_t *t = xmalloc(sizeof(typeref_t));
1112 t->uniq = ref ? 0 : 1;
1116 static typeref_t *uniq_tref(typeref_t *ref)
1120 if (t->uniq) return t;
1121 tp = make_type(0, t->ref);
1129 static type_t *type_ref(typeref_t *ref)
1131 type_t *t = ref->ref;
1132 if (ref->name) free(ref->name);
1137 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1140 v->tname = ref->name;
1146 static ifref_t *make_ifref(type_t *iface)
1148 ifref_t *l = xmalloc(sizeof(ifref_t));
1155 static var_t *make_var(char *name)
1157 var_t *v = xmalloc(sizeof(var_t));
1169 static func_t *make_func(var_t *def, var_t *args)
1171 func_t *f = xmalloc(sizeof(func_t));
1174 f->ignore = parse_only;
1180 static type_t *make_class(char *name)
1182 type_t *c = make_type(0, NULL);
1184 c->kind = TKIND_COCLASS;
1189 static type_t *make_safearray(void)
1191 return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
1196 static int hash_ident(const char *name)
1198 const char *p = name;
1200 /* a simple sum hash is probably good enough */
1205 return sum & (HASHMAX-1);
1208 /***** type repository *****/
1217 struct rtype *type_hash[HASHMAX];
1219 static type_t *reg_type(type_t *type, const char *name, int t)
1224 yyerror("registering named type without name");
1227 hash = hash_ident(name);
1228 nt = xmalloc(sizeof(struct rtype));
1232 nt->next = type_hash[hash];
1233 type_hash[hash] = nt;
1237 /* determine pointer type from attrs */
1238 static unsigned char get_pointer_type( type_t *type )
1241 if (is_attr( type->attrs, ATTR_STRING ))
1243 switch( ref_type( type ) )
1246 return RPC_FC_C_CSTRING;
1248 return RPC_FC_C_WSTRING;
1251 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1256 static type_t *reg_types(type_t *type, var_t *names, int t)
1262 var_t *next = NEXT_LINK(names);
1265 int cptr = names->ptr_level;
1267 while (cptr > ptrc) {
1268 int t = get_pointer_type( cur );
1269 cur = ptr = make_type(t, cur);
1273 while (cptr < ptrc) {
1278 reg_type(cur, names->name, t);
1286 static type_t *find_type(const char *name, int t)
1288 struct rtype *cur = type_hash[hash_ident(name)];
1289 while (cur && (cur->t != t || strcmp(cur->name, name)))
1292 yyerror("type '%s' not found", name);
1298 static type_t *find_type2(char *name, int t)
1300 type_t *tp = find_type(name, t);
1305 int is_type(const char *name)
1307 struct rtype *cur = type_hash[hash_ident(name)];
1308 while (cur && (cur->t || strcmp(cur->name, name)))
1310 if (cur) return TRUE;
1314 static type_t *get_type(unsigned char type, char *name, int t)
1316 struct rtype *cur = NULL;
1319 cur = type_hash[hash_ident(name)];
1320 while (cur && (cur->t != t || strcmp(cur->name, name)))
1327 tp = make_type(type, NULL);
1329 if (!name) return tp;
1330 return reg_type(tp, name, t);
1333 static type_t *get_typev(unsigned char type, var_t *name, int t)
1340 return get_type(type, sname, t);
1343 static int get_struct_type(var_t *field)
1345 int has_pointer = 0;
1346 int has_conformance = 0;
1347 int has_variance = 0;
1349 for (; field; field = NEXT_LINK(field))
1351 type_t *t = field->type;
1353 /* get the base type */
1354 while( (t->type == 0) && t->ref )
1357 if (field->ptr_level > 0)
1363 if (is_string_type(field->attrs, 0, field->array))
1365 has_conformance = 1;
1370 if (is_array_type(field->attrs, 0, field->array))
1372 if (field->array && !field->array->is_const)
1374 has_conformance = 1;
1375 if (PREV_LINK(field))
1376 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1379 if (is_attr(field->attrs, ATTR_LENGTHIS))
1386 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1387 * Simple types don't effect the type of struct.
1388 * A struct containing a simple struct is still a simple struct.
1389 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1391 case 0: /* void pointer */
1401 case RPC_FC_INT3264:
1402 case RPC_FC_UINT3264:
1416 has_conformance = 1;
1417 if (PREV_LINK(field))
1418 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1421 case RPC_FC_C_CSTRING:
1422 case RPC_FC_C_WSTRING:
1423 has_conformance = 1;
1428 * Propagate member attributes
1429 * a struct should be at least as complex as its member
1431 case RPC_FC_CVSTRUCT:
1432 has_conformance = 1;
1437 case RPC_FC_CPSTRUCT:
1438 has_conformance = 1;
1439 if (PREV_LINK(field))
1440 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1445 case RPC_FC_CSTRUCT:
1446 has_conformance = 1;
1447 if (PREV_LINK(field))
1448 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1452 case RPC_FC_PSTRUCT:
1457 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1458 field->name, t->type);
1459 /* fallthru - treat it as complex */
1461 /* as soon as we see one of these these members, it's bogus... */
1463 case RPC_FC_ENCAPSULATED_UNION:
1464 case RPC_FC_NON_ENCAPSULATED_UNION:
1465 case RPC_FC_TRANSMIT_AS:
1466 case RPC_FC_REPRESENT_AS:
1468 case RPC_FC_EMBEDDED_COMPLEX:
1469 case RPC_FC_BOGUS_STRUCT:
1470 return RPC_FC_BOGUS_STRUCT;
1475 return RPC_FC_CVSTRUCT;
1476 if( has_conformance && has_pointer )
1477 return RPC_FC_CPSTRUCT;
1478 if( has_conformance )
1479 return RPC_FC_CSTRUCT;
1481 return RPC_FC_PSTRUCT;
1482 return RPC_FC_STRUCT;
1485 /***** constant repository *****/
1490 struct rconst *next;
1493 struct rconst *const_hash[HASHMAX];
1495 static var_t *reg_const(var_t *var)
1500 yyerror("registering constant without name");
1503 hash = hash_ident(var->name);
1504 nc = xmalloc(sizeof(struct rconst));
1505 nc->name = var->name;
1507 nc->next = const_hash[hash];
1508 const_hash[hash] = nc;
1512 static var_t *find_const(char *name, int f)
1514 struct rconst *cur = const_hash[hash_ident(name)];
1515 while (cur && strcmp(cur->name, name))
1518 if (f) yyerror("constant '%s' not found", name);
1524 static void write_libid(const char *name, const attr_t *attr)
1526 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1527 write_guid(idfile, "LIBID", name, uuid);
1530 static void write_clsid(type_t *cls)
1532 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1533 write_guid(idfile, "CLSID", cls->name, uuid);
1536 static void write_diid(type_t *iface)
1538 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1539 write_guid(idfile, "DIID", iface->name, uuid);
1542 static void write_iid(type_t *iface)
1544 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1545 write_guid(idfile, "IID", iface->name, uuid);
1548 static int compute_method_indexes(type_t *iface)
1551 func_t *f = iface->funcs;
1554 idx = compute_method_indexes(iface->ref);
1561 while (NEXT_LINK(f))
1564 for ( ; f ; f = PREV_LINK(f))
1565 if (! is_callas(f->def->attrs))