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 class_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);
103 static type_t std_bool = { "boolean" };
104 static type_t std_int = { "int" };
105 static type_t std_int64 = { "__int64" };
106 static type_t std_uhyper = { "MIDL_uhyper" };
123 %token <str> aIDENTIFIER
124 %token <str> aKNOWNTYPE
125 %token <num> aNUM aHEXNUM
130 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
131 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
132 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
133 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
134 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
136 %token tDEFAULTCOLLELEM
138 %token tDEFAULTVTABLE
140 %token tDISPINTERFACE
141 %token tDLLNAME tDOUBLE tDUAL
143 %token tENTRY tENUM tERRORSTATUST
144 %token tEXPLICITHANDLE tEXTERN
149 %token tHELPCONTEXT tHELPFILE
150 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
152 %token tHYPER tID tIDEMPOTENT
154 %token tIMMEDIATEBIND
155 %token tIMPLICITHANDLE
156 %token tIMPORT tIMPORTLIB
162 %token tLENGTHIS tLIBRARY
169 %token tNONEXTENSIBLE
170 %token tOBJECT tODL tOLEAUTOMATION
173 %token tPOINTERDEFAULT
175 %token tPROPGET tPROPPUT tPROPPUTREF
179 %token tREADONLY tREF
187 %token tSIZEIS tSIZEOF
191 %token tSTRING tSTRUCT
192 %token tSWITCH tSWITCHIS tSWITCHTYPE
204 %token tWCHAR tWIREMARSHAL
206 %type <attr> m_attributes attributes attrib_list attribute
207 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
208 %type <expr> array array_list
209 %type <type> inherit interface interfacehdr interfacedef interfacedec
210 %type <type> dispinterface dispinterfacehdr dispinterfacedef
211 %type <type> module modulehdr moduledef
212 %type <type> base_type int_std
213 %type <type> enumdef structdef typedef uniondef
214 %type <ifref> gbl_statements coclass_ints coclass_int
216 %type <var> m_args no_args args arg
217 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
218 %type <var> m_ident t_ident ident p_ident pident pident_list
219 %type <var> dispint_props
220 %type <func> funcdef int_statements
221 %type <func> dispint_meths
222 %type <clas> coclass coclasshdr coclassdef
223 %type <num> pointer_type version
224 %type <str> libraryhdr
240 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
243 gbl_statements: { $$ = NULL; }
244 | gbl_statements interfacedec { $$ = $1; }
245 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
246 | gbl_statements coclass ';' { $$ = $1; if (!parse_only && do_header) write_coclass_forward($2); }
247 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
248 | gbl_statements moduledef { $$ = $1; add_module($2); }
249 | gbl_statements librarydef { $$ = $1; }
250 | gbl_statements statement { $$ = $1; }
254 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
255 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
256 | imp_statements coclass ';' { reg_type(NULL, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
257 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
258 | imp_statements moduledef { if (!parse_only) add_module($2); }
259 | imp_statements statement {}
260 | imp_statements importlib {}
263 int_statements: { $$ = NULL; }
264 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
265 | int_statements statement { $$ = $1; }
269 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
271 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
272 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
274 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
276 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
279 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
281 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
282 if (!do_import($2)) yychar = aEOF; }
284 import: import_start imp_statements aEOF {}
287 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
290 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
292 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
293 if (!parse_only && do_header) write_library($2, $1); }
295 librarydef: library_start imp_statements '}' { end_typelib(); }
298 m_args: { $$ = NULL; }
302 no_args: tVOID { $$ = NULL; }
306 | args ',' arg { LINK($3, $1); $$ = $3; }
310 /* split into two rules to get bison to resolve a tVOID conflict */
311 arg: attributes type pident array { $$ = $3;
312 set_type($$, $2, $4);
315 | type pident array { $$ = $2;
316 set_type($$, $1, $3);
318 | attributes type pident '(' m_args ')' { $$ = $3;
320 set_type($$, $2, NULL);
324 | type pident '(' m_args ')' { $$ = $2;
326 set_type($$, $1, NULL);
331 array: { $$ = NULL; }
332 | '[' array_list ']' { $$ = $2; }
333 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
336 array_list: m_expr /* size of first dimension is optional */
337 | array_list ',' expr { LINK($3, $1); $$ = $3; }
338 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
341 m_attributes: { $$ = NULL; }
346 '[' attrib_list ']' { $$ = $2;
348 yyerror("empty attribute lists unsupported");
352 attrib_list: attribute
353 | attrib_list ',' attribute { if ($3) { LINK($3, $1); $$ = $3; }
356 | attrib_list ']' '[' attribute { if ($4) { LINK($4, $1); $$ = $4; }
361 attribute: { $$ = NULL; }
362 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
363 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
364 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
365 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
366 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
367 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
368 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
369 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
370 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
371 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
372 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
373 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
374 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
375 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
376 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
377 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
378 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
379 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
380 | tDUAL { $$ = make_attr(ATTR_DUAL); }
381 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
382 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
383 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
384 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
385 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
386 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
387 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
388 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
389 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
390 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
391 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
392 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
393 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
394 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
395 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
396 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
397 | tIN { $$ = make_attr(ATTR_IN); }
398 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
399 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
400 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
401 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
402 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
403 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
404 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
405 | tODL { $$ = make_attr(ATTR_ODL); }
406 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
407 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
408 | tOUT { $$ = make_attr(ATTR_OUT); }
409 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
410 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
411 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
412 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
413 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
414 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
415 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
416 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
417 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
418 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
419 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
420 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
421 | tSTRING { $$ = make_attr(ATTR_STRING); }
422 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
423 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
424 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
425 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
426 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
427 | tVARARG { $$ = make_attr(ATTR_VARARG); }
428 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
429 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
430 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
437 cases: { $$ = NULL; }
438 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
443 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
444 $$ = $4; if (!$$) $$ = make_var(NULL);
445 LINK(a, $$->attrs); $$->attrs = a;
447 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
448 $$ = $3; if (!$$) $$ = make_var(NULL);
449 LINK(a, $$->attrs); $$->attrs = a;
453 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
454 set_type($$, $2, NULL);
459 enums: { $$ = NULL; }
460 | enum_list ',' { $$ = $1; }
464 enum_list: enum { if (!$$->eval)
465 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
467 | enum_list ',' enum { LINK($3, $1); $$ = $3;
469 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
473 enum: ident '=' expr_const { $$ = reg_const($1);
475 $$->type = make_type(RPC_FC_LONG, &std_int);
477 | ident { $$ = reg_const($1);
478 $$->type = make_type(RPC_FC_LONG, &std_int);
482 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
491 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
495 exprs: { $$ = make_expr(EXPR_VOID); }
500 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
504 m_expr: { $$ = make_expr(EXPR_VOID); }
508 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
509 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
510 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
511 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
512 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
513 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
514 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
515 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
516 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
517 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
518 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
519 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
520 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
521 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
522 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
523 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
524 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
525 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
526 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
527 | '(' expr ')' { $$ = $2; }
530 expr_list_const: expr_const
531 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
534 expr_const: expr { $$ = $1;
536 yyerror("expression is not constant");
540 externdef: tEXTERN tCONST type ident { $$ = $4;
541 set_type($$, $3, NULL);
545 fields: { $$ = NULL; }
546 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
551 field: s_field ';' { $$ = $1; }
552 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
553 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
557 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
561 m_attributes type callconv pident
562 '(' m_args ')' { set_type($4, $2, NULL);
564 $$ = make_func($4, $6);
565 if (is_attr($4->attrs, ATTR_IN)) {
566 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
571 m_ident: { $$ = NULL; }
575 t_ident: { $$ = NULL; }
576 | aIDENTIFIER { $$ = make_var($1); }
577 | aKNOWNTYPE { $$ = make_var($1); }
580 ident: aIDENTIFIER { $$ = make_var($1); }
581 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
582 | aKNOWNTYPE { $$ = make_var($<str>1); }
583 | tASYNC { $$ = make_var($<str>1); }
584 | tID { $$ = make_var($<str>1); }
585 | tLCID { $$ = make_var($<str>1); }
586 | tOBJECT { $$ = make_var($<str>1); }
587 | tRANGE { $$ = make_var($<str>1); }
588 | tRETVAL { $$ = make_var($<str>1); }
589 | tUUID { $$ = make_var($<str>1); }
590 | tVERSION { $$ = make_var($<str>1); }
593 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
594 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
596 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
597 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
599 case RPC_FC_CHAR: break;
600 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
601 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
602 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
604 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
609 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
610 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
611 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
612 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
613 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
614 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
615 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
622 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
623 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
624 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
625 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
626 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
627 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
628 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
631 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
632 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
635 coclasshdr: attributes coclass { $$ = $2;
637 if (!parse_only && do_header)
642 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
647 coclass_ints: { $$ = NULL; }
648 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
652 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
655 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
656 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
659 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
661 if ($$->defined) yyerror("multiple definition error");
662 attrs = make_attr(ATTR_DISPINTERFACE);
665 $$->ref = find_type("IDispatch", 0);
666 if (!$$->ref) yyerror("IDispatch is undefined");
668 if (!parse_only && do_header) write_forward($$);
672 dispint_props: tPROPERTIES ':' { $$ = NULL; }
673 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
676 dispint_meths: tMETHODS ':' { $$ = NULL; }
677 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
680 dispinterfacedef: dispinterfacehdr '{'
686 if (!parse_only && do_header) write_dispinterface($$);
688 /* FIXME: not sure how to handle this yet
689 | dispinterfacehdr '{' interface '}' { $$ = $1;
690 if (!parse_only && do_header) write_interface($$);
695 inherit: { $$ = NULL; }
696 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
699 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
700 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
703 interfacehdr: attributes interface { $$ = $2;
704 if ($$->defined) yyerror("multiple definition error");
707 if (!parse_only && do_header) write_forward($$);
711 interfacedef: interfacehdr inherit
712 '{' int_statements '}' { $$ = $1;
715 if (!parse_only && do_header) write_interface($$);
717 /* MIDL is able to import the definition of a base class from inside the
718 * definition of a derived class, I'll try to support it with this rule */
719 | interfacehdr ':' aIDENTIFIER
720 '{' import int_statements '}' { $$ = $1;
721 $$->ref = find_type2($3, 0);
722 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
724 if (!parse_only && do_header) write_interface($$);
726 | dispinterfacedef { $$ = $1; }
730 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
731 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
734 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
735 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
738 modulehdr: attributes module { $$ = $2;
743 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
745 /* FIXME: if (!parse_only && do_header) write_module($$); */
749 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
750 | tCONST p_ident { $$ = $2; /* FIXME */ }
755 | '(' pident ')' { $$ = $2; }
760 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
764 tREF { $$ = RPC_FC_RP; }
765 | tUNIQUE { $$ = RPC_FC_UP; }
766 | tPTR { $$ = RPC_FC_FP; }
769 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
770 /* overwrite RPC_FC_STRUCT with a more exact type */
771 $$->type = get_struct_type( $4 );
779 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
780 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
781 | base_type { $$ = make_tref(NULL, $1); }
782 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
783 | enumdef { $$ = make_tref(NULL, $1); }
784 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
785 | structdef { $$ = make_tref(NULL, $1); }
786 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
787 | uniondef { $$ = make_tref(NULL, $1); }
788 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
789 | tSAFEARRAY '(' type ')' { $$ = make_tref(NULL, make_safearray()); }
792 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
793 $4->tname = tref->name;
797 if (!parse_only && do_header)
798 write_typedef($$, $4);
799 if (in_typelib && $$->attrs)
801 reg_types($$, $4, 0);
805 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
810 tSWITCH '(' s_field ')'
811 m_ident '{' cases '}' { var_t *u = $7;
812 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
813 if (!u) u = make_var("tagged_union");
814 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
815 u->type->fields = $9;
816 u->type->defined = TRUE;
817 LINK(u, $5); $$->fields = u;
823 aNUM { $$ = MAKELONG($1, 0); }
824 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
829 static attr_t *make_attr(enum attr_type type)
831 attr_t *a = xmalloc(sizeof(attr_t));
838 static attr_t *make_attrv(enum attr_type type, unsigned long val)
840 attr_t *a = xmalloc(sizeof(attr_t));
847 static attr_t *make_attrp(enum attr_type type, void *val)
849 attr_t *a = xmalloc(sizeof(attr_t));
856 static expr_t *make_expr(enum expr_type type)
858 expr_t *e = xmalloc(sizeof(expr_t));
867 static expr_t *make_exprl(enum expr_type type, long val)
869 expr_t *e = xmalloc(sizeof(expr_t));
875 /* check for numeric constant */
876 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
877 /* make sure true/false value is valid */
878 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
885 static expr_t *make_exprs(enum expr_type type, char *val)
888 e = xmalloc(sizeof(expr_t));
894 /* check for predefined constants */
895 if (type == EXPR_IDENTIFIER) {
896 var_t *c = find_const(val, 0);
901 e->cval = c->eval->cval;
907 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
910 e = xmalloc(sizeof(expr_t));
916 /* check for cast of constant expression */
917 if (type == EXPR_SIZEOF) {
918 switch (tref->ref->type) {
935 case RPC_FC_ERROR_STATUS_T:
946 if (type == EXPR_CAST && expr->is_const) {
948 e->cval = expr->cval;
953 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
956 e = xmalloc(sizeof(expr_t));
962 /* check for compile-time optimization */
963 if (expr->is_const) {
967 e->cval = -expr->cval;
970 e->cval = ~expr->cval;
980 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
983 e = xmalloc(sizeof(expr_t));
989 /* check for compile-time optimization */
990 if (expr1->is_const && expr2->is_const) {
994 e->cval = expr1->cval + expr2->cval;
997 e->cval = expr1->cval - expr2->cval;
1000 e->cval = expr1->cval * expr2->cval;
1003 e->cval = expr1->cval / expr2->cval;
1006 e->cval = expr1->cval | expr2->cval;
1009 e->cval = expr1->cval & expr2->cval;
1012 e->cval = expr1->cval << expr2->cval;
1015 e->cval = expr1->cval >> expr2->cval;
1018 e->is_const = FALSE;
1025 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1028 e = xmalloc(sizeof(expr_t));
1033 e->is_const = FALSE;
1035 /* check for compile-time optimization */
1036 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1040 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1043 e->is_const = FALSE;
1050 static type_t *make_type(unsigned char type, type_t *ref)
1052 type_t *t = xmalloc(sizeof(type_t));
1059 t->ignore = parse_only;
1060 t->is_const = FALSE;
1064 t->typelib_idx = -1;
1069 static typeref_t *make_tref(char *name, type_t *ref)
1071 typeref_t *t = xmalloc(sizeof(typeref_t));
1074 t->uniq = ref ? 0 : 1;
1078 static typeref_t *uniq_tref(typeref_t *ref)
1082 if (t->uniq) return t;
1083 tp = make_type(0, t->ref);
1091 static type_t *type_ref(typeref_t *ref)
1093 type_t *t = ref->ref;
1094 if (ref->name) free(ref->name);
1099 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1102 v->tname = ref->name;
1108 static ifref_t *make_ifref(type_t *iface)
1110 ifref_t *l = xmalloc(sizeof(ifref_t));
1117 static var_t *make_var(char *name)
1119 var_t *v = xmalloc(sizeof(var_t));
1131 static func_t *make_func(var_t *def, var_t *args)
1133 func_t *f = xmalloc(sizeof(func_t));
1136 f->ignore = parse_only;
1142 static class_t *make_class(char *name)
1144 class_t *c = xmalloc(sizeof(class_t));
1152 static type_t *make_safearray(void)
1154 return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
1159 static int hash_ident(const char *name)
1161 const char *p = name;
1163 /* a simple sum hash is probably good enough */
1168 return sum & (HASHMAX-1);
1171 /***** type repository *****/
1180 struct rtype *type_hash[HASHMAX];
1182 static type_t *reg_type(type_t *type, const char *name, int t)
1187 yyerror("registering named type without name");
1190 hash = hash_ident(name);
1191 nt = xmalloc(sizeof(struct rtype));
1195 nt->next = type_hash[hash];
1196 type_hash[hash] = nt;
1200 /* determine pointer type from attrs */
1201 static unsigned char get_pointer_type( type_t *type )
1204 if (is_attr( type->attrs, ATTR_STRING ))
1207 while( t->type == 0 && t->ref )
1212 return RPC_FC_C_CSTRING;
1214 return RPC_FC_C_WSTRING;
1217 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1222 static type_t *reg_types(type_t *type, var_t *names, int t)
1228 var_t *next = NEXT_LINK(names);
1231 int cptr = names->ptr_level;
1233 while (cptr > ptrc) {
1234 int t = get_pointer_type( cur );
1235 cur = ptr = make_type(t, cur);
1239 while (cptr < ptrc) {
1244 reg_type(cur, names->name, t);
1252 static type_t *find_type(const char *name, int t)
1254 struct rtype *cur = type_hash[hash_ident(name)];
1255 while (cur && (cur->t != t || strcmp(cur->name, name)))
1258 yyerror("type '%s' not found", name);
1264 static type_t *find_type2(char *name, int t)
1266 type_t *tp = find_type(name, t);
1271 int is_type(const char *name)
1273 struct rtype *cur = type_hash[hash_ident(name)];
1274 while (cur && (cur->t || strcmp(cur->name, name)))
1276 if (cur) return TRUE;
1280 static type_t *get_type(unsigned char type, char *name, int t)
1282 struct rtype *cur = NULL;
1285 cur = type_hash[hash_ident(name)];
1286 while (cur && (cur->t != t || strcmp(cur->name, name)))
1293 tp = make_type(type, NULL);
1295 if (!name) return tp;
1296 return reg_type(tp, name, t);
1299 static type_t *get_typev(unsigned char type, var_t *name, int t)
1306 return get_type(type, sname, t);
1309 static int get_struct_type(var_t *field)
1311 int has_pointer = 0;
1312 int has_conformance = 0;
1313 int has_variance = 0;
1315 for (; field; field = NEXT_LINK(field))
1317 type_t *t = field->type;
1319 /* get the base type */
1320 while( (t->type == 0) && t->ref )
1323 if (field->ptr_level > 0)
1329 if (is_string_type(field->attrs, 0, field->array))
1331 has_conformance = 1;
1336 if (is_array_type(field->attrs, 0, field->array))
1338 if (field->array && !field->array->is_const)
1340 has_conformance = 1;
1341 if (PREV_LINK(field))
1342 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1345 if (is_attr(field->attrs, ATTR_LENGTHIS))
1352 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1353 * Simple types don't effect the type of struct.
1354 * A struct containing a simple struct is still a simple struct.
1355 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1357 case 0: /* void pointer */
1367 case RPC_FC_INT3264:
1368 case RPC_FC_UINT3264:
1382 has_conformance = 1;
1383 if (PREV_LINK(field))
1384 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1387 case RPC_FC_C_CSTRING:
1388 case RPC_FC_C_WSTRING:
1389 has_conformance = 1;
1394 * Propagate member attributes
1395 * a struct should be at least as complex as its member
1397 case RPC_FC_CVSTRUCT:
1398 has_conformance = 1;
1403 case RPC_FC_CPSTRUCT:
1404 has_conformance = 1;
1405 if (PREV_LINK(field))
1406 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1411 case RPC_FC_CSTRUCT:
1412 has_conformance = 1;
1413 if (PREV_LINK(field))
1414 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1418 case RPC_FC_PSTRUCT:
1423 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1424 field->name, t->type);
1425 /* fallthru - treat it as complex */
1427 /* as soon as we see one of these these members, it's bogus... */
1429 case RPC_FC_ENCAPSULATED_UNION:
1430 case RPC_FC_NON_ENCAPSULATED_UNION:
1431 case RPC_FC_TRANSMIT_AS:
1432 case RPC_FC_REPRESENT_AS:
1434 case RPC_FC_EMBEDDED_COMPLEX:
1435 case RPC_FC_BOGUS_STRUCT:
1436 return RPC_FC_BOGUS_STRUCT;
1441 return RPC_FC_CVSTRUCT;
1442 if( has_conformance && has_pointer )
1443 return RPC_FC_CPSTRUCT;
1444 if( has_conformance )
1445 return RPC_FC_CSTRUCT;
1447 return RPC_FC_PSTRUCT;
1448 return RPC_FC_STRUCT;
1451 /***** constant repository *****/
1456 struct rconst *next;
1459 struct rconst *const_hash[HASHMAX];
1461 static var_t *reg_const(var_t *var)
1466 yyerror("registering constant without name");
1469 hash = hash_ident(var->name);
1470 nc = xmalloc(sizeof(struct rconst));
1471 nc->name = var->name;
1473 nc->next = const_hash[hash];
1474 const_hash[hash] = nc;
1478 static var_t *find_const(char *name, int f)
1480 struct rconst *cur = const_hash[hash_ident(name)];
1481 while (cur && strcmp(cur->name, name))
1484 if (f) yyerror("constant '%s' not found", name);