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
167 %token tREADONLY tREF
173 %token tSIZEIS tSIZEOF
177 %token tSTRING tSTRUCT
178 %token tSWITCH tSWITCHIS tSWITCHTYPE
189 %token tWCHAR tWIREMARSHAL
194 %type <attr> m_attributes attributes attrib_list attribute
195 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
196 %type <expr> array array_list
197 %type <type> inherit interface interfacehdr interfacedef interfacedec
198 %type <type> dispinterface dispinterfacehdr dispinterfacedef
199 %type <type> module modulehdr moduledef
200 %type <type> base_type int_std
201 %type <type> enumdef structdef typedef uniondef
202 %type <ifref> gbl_statements coclass_ints coclass_int
204 %type <var> m_args no_args args arg
205 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
206 %type <var> m_ident t_ident ident p_ident pident pident_list
207 %type <var> dispint_props
208 %type <func> funcdef int_statements
209 %type <func> dispint_meths
210 %type <clas> coclass coclasshdr coclassdef
211 %type <num> pointer_type version
212 %type <str> libraryhdr
228 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
231 gbl_statements: { $$ = NULL; }
232 | gbl_statements interfacedec { $$ = $1; }
233 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
234 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
235 | gbl_statements moduledef { $$ = $1; add_module($2); }
236 | gbl_statements librarydef { $$ = $1; }
237 | gbl_statements statement { $$ = $1; }
241 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
242 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
243 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
244 | imp_statements moduledef { if (!parse_only) add_module($2); }
245 | imp_statements statement {}
248 int_statements: { $$ = NULL; }
249 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
250 | int_statements statement { $$ = $1; }
254 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
256 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
257 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
259 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
261 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
264 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
266 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
267 if (!do_import($2)) yychar = aEOF; }
269 import: import_start imp_statements aEOF {}
272 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
274 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
276 librarydef: library_start imp_statements '}' { end_typelib(); }
279 m_args: { $$ = NULL; }
283 no_args: tVOID { $$ = NULL; }
287 | args ',' arg { LINK($3, $1); $$ = $3; }
291 /* split into two rules to get bison to resolve a tVOID conflict */
292 arg: attributes type pident array { $$ = $3;
293 set_type($$, $2, $4);
296 | type pident array { $$ = $2;
297 set_type($$, $1, $3);
299 | attributes type pident '(' m_args ')' { $$ = $3;
301 set_type($$, $2, NULL);
305 | type pident '(' m_args ')' { $$ = $2;
307 set_type($$, $1, NULL);
312 array: { $$ = NULL; }
313 | '[' array_list ']' { $$ = $2; }
314 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
317 array_list: m_expr /* size of first dimension is optional */
318 | array_list ',' expr { LINK($3, $1); $$ = $3; }
319 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
322 m_attributes: { $$ = NULL; }
327 '[' attrib_list ']' { $$ = $2; }
330 attrib_list: attribute
331 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
332 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
336 tASYNC { $$ = make_attr(ATTR_ASYNC); }
337 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
338 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
339 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
340 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
341 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
342 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
343 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
344 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
345 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
346 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
347 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
348 | tDUAL { $$ = make_attr(ATTR_DUAL); }
349 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
350 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
351 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
352 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
353 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
354 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
355 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
356 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
357 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
358 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
359 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
360 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
361 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
362 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
363 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
364 | tIN { $$ = make_attr(ATTR_IN); }
365 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
366 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
367 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
368 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
369 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
370 | tODL { $$ = make_attr(ATTR_ODL); }
371 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
372 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
373 | tOUT { $$ = make_attr(ATTR_OUT); }
374 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
375 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
376 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
377 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
378 | tPTR { $$ = make_attr(ATTR_PTR); }
379 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
380 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
381 | tREF { $$ = make_attr(ATTR_REF); }
382 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
383 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
384 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
385 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
386 | tSTRING { $$ = make_attr(ATTR_STRING); }
387 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
388 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
389 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
390 | tUNIQUE { $$ = make_attr(ATTR_UNIQUE); }
391 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
392 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
393 | tVARARG { $$ = make_attr(ATTR_VARARG); }
394 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
395 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
396 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
403 cases: { $$ = NULL; }
404 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
409 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
410 $$ = $4; if (!$$) $$ = make_var(NULL);
411 LINK(a, $$->attrs); $$->attrs = a;
413 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
414 $$ = $3; if (!$$) $$ = make_var(NULL);
415 LINK(a, $$->attrs); $$->attrs = a;
419 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
420 set_type($$, $2, NULL);
426 enums: { $$ = NULL; }
427 | enum_list ',' { $$ = $1; }
432 | enum_list ',' enum { LINK($3, $1); $$ = $3;
434 $3->lval = $1->lval + 1;
438 enum: ident '=' expr_const { $$ = reg_const($1);
441 $$->type = make_type(RPC_FC_LONG, &std_int);
443 | ident { $$ = reg_const($1);
444 $$->lval = 0; /* default for first enum entry */
445 $$->type = make_type(RPC_FC_LONG, &std_int);
449 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
458 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
462 exprs: { $$ = make_expr(EXPR_VOID); }
467 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
471 m_expr: { $$ = make_expr(EXPR_VOID); }
475 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
476 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
477 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
478 | expr '?' expr ':' expr %prec COND { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
479 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
480 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
481 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
482 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
483 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
484 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
485 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
486 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
487 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
488 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
489 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
490 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
491 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
492 | '(' expr ')' { $$ = $2; }
495 expr_list_const: expr_const
496 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
499 expr_const: expr { $$ = $1;
501 yyerror("expression is not constant\n");
505 externdef: tEXTERN tCONST type ident { $$ = $4;
506 set_type($$, $3, NULL);
510 fields: { $$ = NULL; }
511 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
516 field: s_field ';' { $$ = $1; }
517 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
518 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
522 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
526 m_attributes type callconv pident
527 '(' m_args ')' { set_type($4, $2, NULL);
529 $$ = make_func($4, $6);
530 if (is_attr($4->attrs, ATTR_IN)) {
531 yyerror("Inapplicable attribute");
536 m_ident: { $$ = NULL; }
540 t_ident: { $$ = NULL; }
541 | aIDENTIFIER { $$ = make_var($1); }
542 | aKNOWNTYPE { $$ = make_var($1); }
545 ident: aIDENTIFIER { $$ = make_var($1); }
546 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
547 | aKNOWNTYPE { $$ = make_var($<str>1); }
548 | tID { $$ = make_var($<str>1); }
549 | tRETVAL { $$ = make_var($<str>1); }
550 | tVERSION { $$ = make_var($<str>1); }
553 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
554 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
556 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
557 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
559 case RPC_FC_CHAR: break;
560 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
561 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
562 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
564 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
569 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
570 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
571 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
572 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
573 | tBOOLEAN { $$ = make_type(RPC_FC_SMALL, &std_bool); }
574 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
575 | tHANDLET { $$ = make_type(RPC_FC_IGNORE, NULL); }
582 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
583 | tSMALL m_int { $$ = make_type(RPC_FC_SMALL, NULL); }
584 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
585 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
586 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
587 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
588 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
591 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
592 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
595 coclasshdr: attributes coclass { $$ = $2;
597 if (!parse_only && do_header)
602 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
607 coclass_ints: { $$ = NULL; }
608 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
612 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
615 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
616 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
619 dispinterfacehdr: attributes dispinterface { $$ = $2;
620 if ($$->defined) yyerror("multiple definition error\n");
622 $$->attrs = make_attr(ATTR_DISPINTERFACE);
624 $$->ref = find_type("IDispatch", 0);
625 if (!$$->ref) yyerror("IDispatch is undefined\n");
627 if (!parse_only && do_header) write_forward($$);
631 dispint_props: tPROPERTIES ':' { $$ = NULL; }
632 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
635 dispint_meths: tMETHODS ':' { $$ = NULL; }
636 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
639 dispinterfacedef: dispinterfacehdr '{'
645 if (!parse_only && do_header) write_dispinterface($$);
647 /* FIXME: not sure how to handle this yet
648 | dispinterfacehdr '{' interface '}' { $$ = $1;
649 if (!parse_only && do_header) write_interface($$);
654 inherit: { $$ = NULL; }
655 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
658 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
659 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
662 interfacehdr: attributes interface { $$ = $2;
663 if ($$->defined) yyerror("multiple definition error\n");
666 if (!parse_only && do_header) write_forward($$);
670 interfacedef: interfacehdr inherit
671 '{' int_statements '}' { $$ = $1;
674 if (!parse_only && do_header) write_interface($$);
676 /* MIDL is able to import the definition of a base class from inside the
677 * definition of a derived class, I'll try to support it with this rule */
678 | interfacehdr ':' aIDENTIFIER
679 '{' import int_statements '}' { $$ = $1;
680 $$->ref = find_type2($3, 0);
681 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
683 if (!parse_only && do_header) write_interface($$);
685 | dispinterfacedef { $$ = $1; }
689 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
690 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
693 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
694 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
697 modulehdr: attributes module { $$ = $2;
702 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
704 /* FIXME: if (!parse_only && do_header) write_module($$); */
708 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
709 | tCONST p_ident { $$ = $2; /* FIXME */ }
714 | '(' pident ')' { $$ = $2; }
719 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
723 tREF { $$ = RPC_FC_RP; }
724 | tUNIQUE { $$ = RPC_FC_UP; }
727 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
728 /* overwrite RPC_FC_STRUCT with a more exact type */
729 $$->type = get_struct_type( $4 );
737 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
738 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
739 | base_type { $$ = make_tref(NULL, $1); }
740 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
741 | enumdef { $$ = make_tref(NULL, $1); }
742 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
743 | structdef { $$ = make_tref(NULL, $1); }
744 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
745 | uniondef { $$ = make_tref(NULL, $1); }
746 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
749 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
750 $4->tname = tref->name;
754 if (!parse_only && do_header)
755 write_typedef($$, $4);
756 if (in_typelib && $$->attrs)
758 reg_types($$, $4, 0);
762 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
767 tSWITCH '(' s_field ')'
768 m_ident '{' cases '}' { var_t *u = $7;
769 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
770 if (!u) u = make_var("tagged_union");
771 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
772 u->type->fields = $9;
773 u->type->defined = TRUE;
774 LINK(u, $5); $$->fields = u;
780 aNUM { $$ = MAKELONG($1, 0); }
781 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
786 static attr_t *make_attr(enum attr_type type)
788 attr_t *a = xmalloc(sizeof(attr_t));
795 static attr_t *make_attrv(enum attr_type type, unsigned long val)
797 attr_t *a = xmalloc(sizeof(attr_t));
804 static attr_t *make_attrp(enum attr_type type, void *val)
806 attr_t *a = xmalloc(sizeof(attr_t));
813 static expr_t *make_expr(enum expr_type type)
815 expr_t *e = xmalloc(sizeof(expr_t));
824 static expr_t *make_exprl(enum expr_type type, long val)
826 expr_t *e = xmalloc(sizeof(expr_t));
832 /* check for numeric constant */
833 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
840 static expr_t *make_exprs(enum expr_type type, char *val)
843 e = xmalloc(sizeof(expr_t));
849 /* check for predefined constants */
850 if (type == EXPR_IDENTIFIER) {
851 var_t *c = find_const(val, 0);
862 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
865 e = xmalloc(sizeof(expr_t));
871 /* check for cast of constant expression */
872 if (type == EXPR_CAST && expr->is_const) {
874 e->cval = expr->cval;
879 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
882 e = xmalloc(sizeof(expr_t));
888 /* check for compile-time optimization */
889 if (expr->is_const) {
893 e->cval = -expr->cval;
896 e->cval = ~expr->cval;
906 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
909 e = xmalloc(sizeof(expr_t));
915 /* check for compile-time optimization */
916 if (expr1->is_const && expr2->is_const) {
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;
935 e->cval = expr1->cval & expr2->cval;
938 e->cval = expr1->cval << expr2->cval;
941 e->cval = expr1->cval >> expr2->cval;
951 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
954 e = xmalloc(sizeof(expr_t));
961 /* check for compile-time optimization */
962 if (expr1->is_const && expr2->is_const && expr3->is_const) {
966 e->cval = expr1->cval ? expr2->cval : expr3->cval;
976 static type_t *make_type(unsigned char type, type_t *ref)
978 type_t *t = xmalloc(sizeof(type_t));
986 t->ignore = parse_only;
996 static typeref_t *make_tref(char *name, type_t *ref)
998 typeref_t *t = xmalloc(sizeof(typeref_t));
1001 t->uniq = ref ? 0 : 1;
1005 static typeref_t *uniq_tref(typeref_t *ref)
1009 if (t->uniq) return t;
1010 tp = make_type(0, t->ref);
1018 static type_t *type_ref(typeref_t *ref)
1020 type_t *t = ref->ref;
1021 if (ref->name) free(ref->name);
1026 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1029 v->tname = ref->name;
1035 static ifref_t *make_ifref(type_t *iface)
1037 ifref_t *l = xmalloc(sizeof(ifref_t));
1044 static var_t *make_var(char *name)
1046 var_t *v = xmalloc(sizeof(var_t));
1059 static func_t *make_func(var_t *def, var_t *args)
1061 func_t *f = xmalloc(sizeof(func_t));
1064 f->ignore = parse_only;
1070 static class_t *make_class(char *name)
1072 class_t *c = xmalloc(sizeof(class_t));
1082 static int hash_ident(const char *name)
1084 const char *p = name;
1086 /* a simple sum hash is probably good enough */
1091 return sum & (HASHMAX-1);
1094 /***** type repository *****/
1103 struct rtype *type_hash[HASHMAX];
1105 static type_t *reg_type(type_t *type, char *name, int t)
1110 yyerror("registering named type without name\n");
1113 hash = hash_ident(name);
1114 nt = xmalloc(sizeof(struct rtype));
1118 nt->next = type_hash[hash];
1119 type_hash[hash] = nt;
1124 /* determine pointer type from attrs */
1125 static unsigned char get_pointer_type( type_t *type )
1128 if (is_attr( type->attrs, ATTR_STRING ))
1131 while( t->type == 0 && t->ref )
1136 return RPC_FC_C_CSTRING;
1138 return RPC_FC_C_WSTRING;
1141 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1144 if(is_attr( type->attrs, ATTR_PTR ))
1147 if(is_attr( type->attrs, ATTR_UNIQUE ))
1153 static type_t *reg_types(type_t *type, var_t *names, int t)
1159 var_t *next = NEXT_LINK(names);
1162 int cptr = names->ptr_level;
1164 while (cptr > ptrc) {
1165 int t = get_pointer_type( cur );
1166 cur = ptr = make_type(t, cur);
1170 while (cptr < ptrc) {
1175 reg_type(cur, names->name, t);
1183 static type_t *find_type(char *name, int t)
1185 struct rtype *cur = type_hash[hash_ident(name)];
1186 while (cur && (cur->t != t || strcmp(cur->name, name)))
1189 yyerror("type %s not found\n", name);
1195 static type_t *find_type2(char *name, int t)
1197 type_t *tp = find_type(name, t);
1202 int is_type(const char *name)
1204 struct rtype *cur = type_hash[hash_ident(name)];
1205 while (cur && (cur->t || strcmp(cur->name, name)))
1207 if (cur) return TRUE;
1211 static type_t *get_type(unsigned char type, char *name, int t)
1213 struct rtype *cur = NULL;
1216 cur = type_hash[hash_ident(name)];
1217 while (cur && (cur->t != t || strcmp(cur->name, name)))
1224 tp = make_type(type, NULL);
1226 if (!name) return tp;
1227 return reg_type(tp, name, t);
1230 static type_t *get_typev(unsigned char type, var_t *name, int t)
1237 return get_type(type, sname, t);
1240 static int get_struct_type(var_t *field)
1242 int has_pointer = 0;
1243 int has_conformant_array = 0;
1244 int has_conformant_string = 0;
1248 type_t *t = field->type;
1250 /* get the base type */
1251 while( (t->type == 0) && t->ref )
1257 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1258 * Simple types don't effect the type of struct.
1259 * A struct containing a simple struct is still a simple struct.
1260 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1262 case 0: /* void pointer */
1272 case RPC_FC_INT3264:
1273 case RPC_FC_UINT3264:
1287 has_conformant_array = 1;
1289 case RPC_FC_C_CSTRING:
1290 case RPC_FC_C_WSTRING:
1291 has_conformant_string = 1;
1295 * Propagate member attributes
1296 * a struct should be at least as complex as its member
1298 case RPC_FC_CVSTRUCT:
1299 has_conformant_string = 1;
1303 case RPC_FC_CPSTRUCT:
1304 has_conformant_array = 1;
1308 case RPC_FC_CSTRUCT:
1309 has_conformant_array = 1;
1312 case RPC_FC_PSTRUCT:
1317 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1318 field->name, t->type);
1319 /* fallthru - treat it as complex */
1321 /* as soon as we see one of these these members, it's bogus... */
1323 case RPC_FC_ENCAPSULATED_UNION:
1324 case RPC_FC_NON_ENCAPSULATED_UNION:
1325 case RPC_FC_TRANSMIT_AS:
1326 case RPC_FC_REPRESENT_AS:
1328 case RPC_FC_EMBEDDED_COMPLEX:
1329 case RPC_FC_BOGUS_STRUCT:
1330 return RPC_FC_BOGUS_STRUCT;
1332 field = NEXT_LINK(field);
1335 if( has_conformant_string && has_pointer )
1336 return RPC_FC_CVSTRUCT;
1337 if( has_conformant_array && has_pointer )
1338 return RPC_FC_CPSTRUCT;
1339 if( has_conformant_array )
1340 return RPC_FC_CSTRUCT;
1342 return RPC_FC_PSTRUCT;
1343 return RPC_FC_STRUCT;
1346 /***** constant repository *****/
1351 struct rconst *next;
1354 struct rconst *const_hash[HASHMAX];
1356 static var_t *reg_const(var_t *var)
1361 yyerror("registering constant without name\n");
1364 hash = hash_ident(var->name);
1365 nc = xmalloc(sizeof(struct rconst));
1366 nc->name = var->name;
1368 nc->next = const_hash[hash];
1369 const_hash[hash] = nc;
1373 static var_t *find_const(char *name, int f)
1375 struct rconst *cur = const_hash[hash_ident(name)];
1376 while (cur && strcmp(cur->name, name))
1379 if (f) yyerror("constant %s not found\n", name);