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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
41 /* Berkeley yacc (byacc) doesn't seem to know about these */
42 /* Some *BSD supplied versions do define these though */
44 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
47 # define YYLEX yylex()
50 #elif defined(YYBISON)
51 /* Bison was used for original development */
52 /* #define YYEMPTY -2 */
53 /* #define YYLEX yylex() */
56 /* No yacc we know yet */
57 # if !defined(YYEMPTY) || !defined(YYLEX)
58 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
59 # elif defined(__GNUC__) /* gcc defines the #warning directive */
60 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
61 /* #else we just take a chance that it works... */
65 static attr_t *make_attr(enum attr_type type);
66 static attr_t *make_attrv(enum attr_type type, unsigned long val);
67 static attr_t *make_attrp(enum attr_type type, void *val);
68 static expr_t *make_expr(enum expr_type type);
69 static expr_t *make_exprl(enum expr_type type, long val);
70 static expr_t *make_exprs(enum expr_type type, char *val);
71 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
72 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
73 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
74 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
75 static type_t *make_type(unsigned char type, type_t *ref);
76 static typeref_t *make_tref(char *name, type_t *ref);
77 static typeref_t *uniq_tref(typeref_t *ref);
78 static type_t *type_ref(typeref_t *ref);
79 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
80 static ifref_t *make_ifref(type_t *iface);
81 static var_t *make_var(char *name);
82 static func_t *make_func(var_t *def, var_t *args);
83 static class_t *make_class(char *name);
85 static type_t *reg_type(type_t *type, char *name, int t);
86 static type_t *reg_types(type_t *type, var_t *names, int t);
87 static type_t *find_type(char *name, int t);
88 static type_t *find_type2(char *name, int t);
89 static type_t *get_type(unsigned char type, char *name, int t);
90 static type_t *get_typev(unsigned char type, var_t *name, int t);
91 static int get_struct_type(var_t *fields);
93 static var_t *reg_const(var_t *var);
94 static var_t *find_const(char *name, int f);
100 static type_t std_bool = { "boolean" };
101 static type_t std_int = { "int" };
102 static type_t std_int64 = { "__int64" };
103 static type_t std_uhyper = { "MIDL_uhyper" };
120 %token <str> aIDENTIFIER
121 %token <str> aKNOWNTYPE
122 %token <num> aNUM aHEXNUM
127 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
128 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
129 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
130 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
131 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
134 %token tDISPINTERFACE
135 %token tDLLNAME tDOUBLE tDUAL
137 %token tENTRY tENUM tERRORSTATUST
138 %token tEXPLICITHANDLE tEXTERN
142 %token tHELPCONTEXT tHELPFILE
143 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
145 %token tHYPER tID tIDEMPOTENT
147 %token tIMPLICITHANDLE
148 %token tIMPORT tIMPORTLIB
149 %token tIN tINCLUDE tINLINE
153 %token tLENGTHIS tLIBRARY
159 %token tOBJECT tODL tOLEAUTOMATION
162 %token tPOINTERDEFAULT
164 %token tPROPGET tPROPPUT tPROPPUTREF
166 %token tREADONLY tREF
171 %token tSIZEIS tSIZEOF
175 %token tSTRING tSTRUCT
176 %token tSWITCH tSWITCHIS tSWITCHTYPE
187 %token tWCHAR tWIREMARSHAL
192 %type <attr> m_attributes attributes attrib_list attribute
193 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
194 %type <expr> array array_list
195 %type <type> inherit interface interfacehdr interfacedef interfacedec
196 %type <type> dispinterface dispinterfacehdr dispinterfacedef
197 %type <type> module modulehdr moduledef
198 %type <type> base_type int_std
199 %type <type> enumdef structdef typedef uniondef
200 %type <ifref> gbl_statements coclass_ints coclass_int
202 %type <var> m_args no_args args arg
203 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
204 %type <var> m_ident t_ident ident p_ident pident pident_list
205 %type <var> dispint_props
206 %type <func> funcdef int_statements
207 %type <func> dispint_meths
208 %type <clas> coclass coclasshdr coclassdef
209 %type <num> pointer_type version
210 %type <str> libraryhdr
226 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
229 gbl_statements: { $$ = NULL; }
230 | gbl_statements interfacedec { $$ = $1; }
231 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
232 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
233 | gbl_statements moduledef { $$ = $1; add_module($2); }
234 | gbl_statements librarydef { $$ = $1; }
235 | gbl_statements statement { $$ = $1; }
239 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
240 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
241 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
242 | imp_statements moduledef { if (!parse_only) add_module($2); }
243 | imp_statements statement {}
246 int_statements: { $$ = NULL; }
247 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
248 | int_statements statement { $$ = $1; }
252 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
254 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
255 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
257 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
259 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
262 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
264 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
265 if (!do_import($2)) yychar = aEOF; }
267 import: import_start imp_statements aEOF {}
270 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
272 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
274 librarydef: library_start imp_statements '}' { end_typelib(); }
277 m_args: { $$ = NULL; }
281 no_args: tVOID { $$ = NULL; }
285 | args ',' arg { LINK($3, $1); $$ = $3; }
289 /* split into two rules to get bison to resolve a tVOID conflict */
290 arg: attributes type pident array { $$ = $3;
291 set_type($$, $2, $4);
294 | type pident array { $$ = $2;
295 set_type($$, $1, $3);
297 | attributes type pident '(' m_args ')' { $$ = $3;
299 set_type($$, $2, NULL);
303 | type pident '(' m_args ')' { $$ = $2;
305 set_type($$, $1, NULL);
310 array: { $$ = NULL; }
311 | '[' array_list ']' { $$ = $2; }
312 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
315 array_list: m_expr /* size of first dimension is optional */
316 | array_list ',' expr { LINK($3, $1); $$ = $3; }
317 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
320 m_attributes: { $$ = NULL; }
325 '[' attrib_list ']' { $$ = $2; }
328 attrib_list: attribute
329 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
330 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
334 tASYNC { $$ = make_attr(ATTR_ASYNC); }
335 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
336 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
337 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
338 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
339 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
340 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
341 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
342 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
343 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
344 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
345 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
346 | tDUAL { $$ = make_attr(ATTR_DUAL); }
347 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
348 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
349 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
350 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
351 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
352 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
353 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
354 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
355 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
356 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
357 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
358 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
359 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
360 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
361 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
362 | tIN { $$ = make_attr(ATTR_IN); }
363 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
364 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
365 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
366 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
367 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
368 | tODL { $$ = make_attr(ATTR_ODL); }
369 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
370 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
371 | tOUT { $$ = make_attr(ATTR_OUT); }
372 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
373 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
374 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
375 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
376 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
377 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
378 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
379 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
380 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
381 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
382 | tSTRING { $$ = make_attr(ATTR_STRING); }
383 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
384 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
385 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
386 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
387 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
388 | tVARARG { $$ = make_attr(ATTR_VARARG); }
389 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
390 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
391 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
398 cases: { $$ = NULL; }
399 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
404 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
405 $$ = $4; if (!$$) $$ = make_var(NULL);
406 LINK(a, $$->attrs); $$->attrs = a;
408 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
409 $$ = $3; if (!$$) $$ = make_var(NULL);
410 LINK(a, $$->attrs); $$->attrs = a;
414 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
415 set_type($$, $2, NULL);
421 enums: { $$ = NULL; }
422 | enum_list ',' { $$ = $1; }
427 | enum_list ',' enum { LINK($3, $1); $$ = $3;
429 $3->lval = $1->lval + 1;
433 enum: ident '=' expr_const { $$ = reg_const($1);
436 $$->type = make_type(RPC_FC_LONG, &std_int);
438 | ident { $$ = reg_const($1);
439 $$->lval = 0; /* default for first enum entry */
440 $$->type = make_type(RPC_FC_LONG, &std_int);
444 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
453 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
457 exprs: { $$ = make_expr(EXPR_VOID); }
462 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
466 m_expr: { $$ = make_expr(EXPR_VOID); }
470 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
471 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
472 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
473 | expr '?' expr ':' expr %prec COND { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
474 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
475 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
476 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
477 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
478 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
479 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
480 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
481 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
482 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
483 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
484 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
485 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
486 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
487 | '(' expr ')' { $$ = $2; }
490 expr_list_const: expr_const
491 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
494 expr_const: expr { $$ = $1;
496 yyerror("expression is not constant\n");
500 externdef: tEXTERN tCONST type ident { $$ = $4;
501 set_type($$, $3, NULL);
505 fields: { $$ = NULL; }
506 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
511 field: s_field ';' { $$ = $1; }
512 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
513 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
517 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
521 m_attributes type callconv pident
522 '(' m_args ')' { set_type($4, $2, NULL);
524 $$ = make_func($4, $6);
525 if (is_attr($4->attrs, ATTR_IN)) {
526 yyerror("Inapplicable attribute");
531 m_ident: { $$ = NULL; }
535 t_ident: { $$ = NULL; }
536 | aIDENTIFIER { $$ = make_var($1); }
537 | aKNOWNTYPE { $$ = make_var($1); }
540 ident: aIDENTIFIER { $$ = make_var($1); }
541 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
542 | aKNOWNTYPE { $$ = make_var($<str>1); }
543 | tID { $$ = make_var($<str>1); }
544 | tRETVAL { $$ = make_var($<str>1); }
545 | tVERSION { $$ = make_var($<str>1); }
548 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
549 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
551 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
552 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
554 case RPC_FC_CHAR: break;
555 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
556 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
557 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
559 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
564 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
565 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
566 | tBOOLEAN { $$ = make_type(RPC_FC_SMALL, &std_bool); }
567 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
568 | tHANDLET { $$ = make_type(RPC_FC_IGNORE, NULL); }
575 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
576 | tSMALL m_int { $$ = make_type(RPC_FC_SMALL, NULL); }
577 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
578 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
579 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
580 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
581 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
584 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
585 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
588 coclasshdr: attributes coclass { $$ = $2;
590 if (!parse_only && do_header) write_coclass($$);
594 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
599 coclass_ints: { $$ = NULL; }
600 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
604 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
607 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
608 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
611 dispinterfacehdr: attributes dispinterface { $$ = $2;
612 if ($$->defined) yyerror("multiple definition error\n");
614 $$->attrs = make_attr(ATTR_DISPINTERFACE);
616 $$->ref = find_type("IDispatch", 0);
617 if (!$$->ref) yyerror("IDispatch is undefined\n");
619 if (!parse_only && do_header) write_forward($$);
623 dispint_props: tPROPERTIES ':' { $$ = NULL; }
624 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
627 dispint_meths: tMETHODS ':' { $$ = NULL; }
628 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
631 dispinterfacedef: dispinterfacehdr '{'
637 if (!parse_only && do_header) write_dispinterface($$);
639 /* FIXME: not sure how to handle this yet
640 | dispinterfacehdr '{' interface '}' { $$ = $1;
641 if (!parse_only && do_header) write_interface($$);
646 inherit: { $$ = NULL; }
647 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
650 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
651 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
654 interfacehdr: attributes interface { $$ = $2;
655 if ($$->defined) yyerror("multiple definition error\n");
658 if (!parse_only && do_header) write_forward($$);
662 interfacedef: interfacehdr inherit
663 '{' int_statements '}' { $$ = $1;
666 if (!parse_only && do_header) write_interface($$);
668 /* MIDL is able to import the definition of a base class from inside the
669 * definition of a derived class, I'll try to support it with this rule */
670 | interfacehdr ':' aIDENTIFIER
671 '{' import int_statements '}' { $$ = $1;
672 $$->ref = find_type2($3, 0);
673 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
675 if (!parse_only && do_header) write_interface($$);
677 | dispinterfacedef { $$ = $1; }
681 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
682 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
685 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
686 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
689 modulehdr: attributes module { $$ = $2;
694 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
696 /* FIXME: if (!parse_only && do_header) write_module($$); */
700 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
701 | tCONST p_ident { $$ = $2; /* FIXME */ }
706 | '(' pident ')' { $$ = $2; }
711 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
715 tREF { $$ = RPC_FC_RP; }
716 | tUNIQUE { $$ = RPC_FC_UP; }
719 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
720 /* overwrite RPC_FC_STRUCT with a more exact type */
721 $$->type = get_struct_type( $4 );
729 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
730 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
731 | base_type { $$ = make_tref(NULL, $1); }
732 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
733 | enumdef { $$ = make_tref(NULL, $1); }
734 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
735 | structdef { $$ = make_tref(NULL, $1); }
736 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
737 | uniondef { $$ = make_tref(NULL, $1); }
738 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
741 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
742 $4->tname = tref->name;
746 if (!parse_only && do_header) write_typedef($$, $4);
747 if (in_typelib && $$->attrs)
749 reg_types($$, $4, 0);
753 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
758 tSWITCH '(' s_field ')'
759 m_ident '{' cases '}' { var_t *u = $7;
760 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
761 if (!u) u = make_var("tagged_union");
762 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
763 u->type->fields = $9;
764 u->type->defined = TRUE;
765 LINK(u, $5); $$->fields = u;
771 aNUM { $$ = MAKELONG($1, 0); }
772 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
777 static attr_t *make_attr(enum attr_type type)
779 attr_t *a = xmalloc(sizeof(attr_t));
786 static attr_t *make_attrv(enum attr_type type, unsigned long val)
788 attr_t *a = xmalloc(sizeof(attr_t));
795 static attr_t *make_attrp(enum attr_type type, void *val)
797 attr_t *a = xmalloc(sizeof(attr_t));
804 static expr_t *make_expr(enum expr_type type)
806 expr_t *e = xmalloc(sizeof(expr_t));
815 static expr_t *make_exprl(enum expr_type type, long val)
817 expr_t *e = xmalloc(sizeof(expr_t));
823 /* check for numeric constant */
824 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
831 static expr_t *make_exprs(enum expr_type type, char *val)
834 e = xmalloc(sizeof(expr_t));
840 /* check for predefined constants */
841 if (type == EXPR_IDENTIFIER) {
842 var_t *c = find_const(val, 0);
853 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
856 e = xmalloc(sizeof(expr_t));
862 /* check for cast of constant expression */
863 if (type == EXPR_CAST && expr->is_const) {
865 e->cval = expr->cval;
870 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
873 e = xmalloc(sizeof(expr_t));
879 /* check for compile-time optimization */
880 if (expr->is_const) {
884 e->cval = -expr->cval;
887 e->cval = ~expr->cval;
897 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
900 e = xmalloc(sizeof(expr_t));
906 /* check for compile-time optimization */
907 if (expr1->is_const && expr2->is_const) {
911 e->cval = expr1->cval + expr2->cval;
914 e->cval = expr1->cval - expr2->cval;
917 e->cval = expr1->cval * expr2->cval;
920 e->cval = expr1->cval / expr2->cval;
923 e->cval = expr1->cval | expr2->cval;
926 e->cval = expr1->cval & expr2->cval;
929 e->cval = expr1->cval << expr2->cval;
932 e->cval = expr1->cval >> expr2->cval;
942 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
945 e = xmalloc(sizeof(expr_t));
952 /* check for compile-time optimization */
953 if (expr1->is_const && expr2->is_const && expr3->is_const) {
957 e->cval = expr1->cval ? expr2->cval : expr3->cval;
967 static type_t *make_type(unsigned char type, type_t *ref)
969 type_t *t = xmalloc(sizeof(type_t));
977 t->ignore = parse_only;
987 static typeref_t *make_tref(char *name, type_t *ref)
989 typeref_t *t = xmalloc(sizeof(typeref_t));
992 t->uniq = ref ? 0 : 1;
996 static typeref_t *uniq_tref(typeref_t *ref)
1000 if (t->uniq) return t;
1001 tp = make_type(0, t->ref);
1009 static type_t *type_ref(typeref_t *ref)
1011 type_t *t = ref->ref;
1012 if (ref->name) free(ref->name);
1017 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1020 v->tname = ref->name;
1026 static ifref_t *make_ifref(type_t *iface)
1028 ifref_t *l = xmalloc(sizeof(ifref_t));
1035 static var_t *make_var(char *name)
1037 var_t *v = xmalloc(sizeof(var_t));
1050 static func_t *make_func(var_t *def, var_t *args)
1052 func_t *f = xmalloc(sizeof(func_t));
1055 f->ignore = parse_only;
1061 static class_t *make_class(char *name)
1063 class_t *c = xmalloc(sizeof(class_t));
1073 static int hash_ident(const char *name)
1075 const char *p = name;
1077 /* a simple sum hash is probably good enough */
1082 return sum & (HASHMAX-1);
1085 /***** type repository *****/
1094 struct rtype *type_hash[HASHMAX];
1096 static type_t *reg_type(type_t *type, char *name, int t)
1101 yyerror("registering named type without name\n");
1104 hash = hash_ident(name);
1105 nt = xmalloc(sizeof(struct rtype));
1109 nt->next = type_hash[hash];
1110 type_hash[hash] = nt;
1114 /* determine pointer type from attrs */
1115 static unsigned char get_pointer_type( type_t *type )
1118 if (is_attr( type->attrs, ATTR_STRING ))
1121 while( t->type == 0 && t->ref )
1126 return RPC_FC_C_CSTRING;
1128 return RPC_FC_C_WSTRING;
1131 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1136 static type_t *reg_types(type_t *type, var_t *names, int t)
1142 var_t *next = NEXT_LINK(names);
1145 int cptr = names->ptr_level;
1147 while (cptr > ptrc) {
1148 int t = get_pointer_type( cur );
1149 cur = ptr = make_type(t, cur);
1153 while (cptr < ptrc) {
1158 reg_type(cur, names->name, t);
1166 static type_t *find_type(char *name, int t)
1168 struct rtype *cur = type_hash[hash_ident(name)];
1169 while (cur && (cur->t != t || strcmp(cur->name, name)))
1172 yyerror("type %s not found\n", name);
1178 static type_t *find_type2(char *name, int t)
1180 type_t *tp = find_type(name, t);
1185 int is_type(const char *name)
1187 struct rtype *cur = type_hash[hash_ident(name)];
1188 while (cur && (cur->t || strcmp(cur->name, name)))
1190 if (cur) return TRUE;
1194 static type_t *get_type(unsigned char type, char *name, int t)
1196 struct rtype *cur = NULL;
1199 cur = type_hash[hash_ident(name)];
1200 while (cur && (cur->t != t || strcmp(cur->name, name)))
1207 tp = make_type(type, NULL);
1209 if (!name) return tp;
1210 return reg_type(tp, name, t);
1213 static type_t *get_typev(unsigned char type, var_t *name, int t)
1220 return get_type(type, sname, t);
1223 static int get_struct_type(var_t *field)
1225 int has_pointer = 0;
1226 int has_conformant_array = 0;
1227 int has_conformant_string = 0;
1231 type_t *t = field->type;
1233 /* get the base type */
1234 while( (t->type == 0) && t->ref )
1240 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1241 * Simple types don't effect the type of struct.
1242 * A struct containing a simple struct is still a simple struct.
1243 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1245 case 0: /* void pointer */
1255 case RPC_FC_INT3264:
1256 case RPC_FC_UINT3264:
1270 has_conformant_array = 1;
1272 case RPC_FC_C_CSTRING:
1273 case RPC_FC_C_WSTRING:
1274 has_conformant_string = 1;
1278 * Propagate member attributes
1279 * a struct should be at least as complex as its member
1281 case RPC_FC_CVSTRUCT:
1282 has_conformant_string = 1;
1286 case RPC_FC_CPSTRUCT:
1287 has_conformant_array = 1;
1291 case RPC_FC_CSTRUCT:
1292 has_conformant_array = 1;
1295 case RPC_FC_PSTRUCT:
1300 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1301 field->name, t->type);
1302 /* fallthru - treat it as complex */
1304 /* as soon as we see one of these these members, it's bogus... */
1306 case RPC_FC_ENCAPSULATED_UNION:
1307 case RPC_FC_NON_ENCAPSULATED_UNION:
1308 case RPC_FC_TRANSMIT_AS:
1309 case RPC_FC_REPRESENT_AS:
1311 case RPC_FC_EMBEDDED_COMPLEX:
1312 case RPC_FC_BOGUS_STRUCT:
1313 return RPC_FC_BOGUS_STRUCT;
1315 field = NEXT_LINK(field);
1318 if( has_conformant_string && has_pointer )
1319 return RPC_FC_CVSTRUCT;
1320 if( has_conformant_array && has_pointer )
1321 return RPC_FC_CPSTRUCT;
1322 if( has_conformant_array )
1323 return RPC_FC_CSTRUCT;
1325 return RPC_FC_PSTRUCT;
1326 return RPC_FC_STRUCT;
1329 /***** constant repository *****/
1334 struct rconst *next;
1337 struct rconst *const_hash[HASHMAX];
1339 static var_t *reg_const(var_t *var)
1344 yyerror("registering constant without name\n");
1347 hash = hash_ident(var->name);
1348 nc = xmalloc(sizeof(struct rconst));
1349 nc->name = var->name;
1351 nc->next = const_hash[hash];
1352 const_hash[hash] = nc;
1356 static var_t *find_const(char *name, int f)
1358 struct rconst *cur = const_hash[hash_ident(name)];
1359 while (cur && strcmp(cur->name, name))
1362 if (f) yyerror("constant %s not found\n", name);