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
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);
87 static type_t *reg_type(type_t *type, const char *name, int t);
88 static type_t *reg_types(type_t *type, var_t *names, int t);
89 static type_t *find_type(const char *name, int t);
90 static type_t *find_type2(char *name, int t);
91 static type_t *get_type(unsigned char type, char *name, int t);
92 static type_t *get_typev(unsigned char type, var_t *name, int t);
93 static int get_struct_type(var_t *fields);
95 static var_t *reg_const(var_t *var);
96 static var_t *find_const(char *name, int f);
102 static type_t std_bool = { "boolean" };
103 static type_t std_int = { "int" };
104 static type_t std_int64 = { "__int64" };
105 static type_t std_uhyper = { "MIDL_uhyper" };
122 %token <str> aIDENTIFIER
123 %token <str> aKNOWNTYPE
124 %token <num> aNUM aHEXNUM
129 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
130 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
131 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
132 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
133 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
135 %token tDEFAULTCOLLELEM
137 %token tDEFAULTVTABLE
139 %token tDISPINTERFACE
140 %token tDLLNAME tDOUBLE tDUAL
142 %token tENTRY tENUM tERRORSTATUST
143 %token tEXPLICITHANDLE tEXTERN
147 %token tHELPCONTEXT tHELPFILE
148 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
150 %token tHYPER tID tIDEMPOTENT
152 %token tIMMEDIATEBIND
153 %token tIMPLICITHANDLE
154 %token tIMPORT tIMPORTLIB
160 %token tLENGTHIS tLIBRARY
167 %token tNONEXTENSIBLE
168 %token tOBJECT tODL tOLEAUTOMATION
171 %token tPOINTERDEFAULT
173 %token tPROPGET tPROPPUT tPROPPUTREF
177 %token tREADONLY tREF
184 %token tSIZEIS tSIZEOF
188 %token tSTRING tSTRUCT
189 %token tSWITCH tSWITCHIS tSWITCHTYPE
200 %token tWCHAR tWIREMARSHAL
202 %type <attr> m_attributes attributes attrib_list attribute
203 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
204 %type <expr> array array_list
205 %type <type> inherit interface interfacehdr interfacedef interfacedec
206 %type <type> dispinterface dispinterfacehdr dispinterfacedef
207 %type <type> module modulehdr moduledef
208 %type <type> base_type int_std
209 %type <type> enumdef structdef typedef uniondef
210 %type <ifref> gbl_statements coclass_ints coclass_int
212 %type <var> m_args no_args args arg
213 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
214 %type <var> m_ident t_ident ident p_ident pident pident_list
215 %type <var> dispint_props
216 %type <func> funcdef int_statements
217 %type <func> dispint_meths
218 %type <clas> coclass coclasshdr coclassdef
219 %type <num> pointer_type version
220 %type <str> libraryhdr
236 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
239 gbl_statements: { $$ = NULL; }
240 | gbl_statements interfacedec { $$ = $1; }
241 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
242 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
243 | gbl_statements moduledef { $$ = $1; add_module($2); }
244 | gbl_statements librarydef { $$ = $1; }
245 | gbl_statements statement { $$ = $1; }
249 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
250 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
251 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
252 | imp_statements moduledef { if (!parse_only) add_module($2); }
253 | imp_statements statement {}
256 int_statements: { $$ = NULL; }
257 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
258 | int_statements statement { $$ = $1; }
262 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
264 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
265 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
267 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
269 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
272 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
274 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
275 if (!do_import($2)) yychar = aEOF; }
277 import: import_start imp_statements aEOF {}
280 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
282 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
283 if (!parse_only && do_header) write_library($2, $1); }
285 librarydef: library_start imp_statements '}' { end_typelib(); }
288 m_args: { $$ = NULL; }
292 no_args: tVOID { $$ = NULL; }
296 | args ',' arg { LINK($3, $1); $$ = $3; }
300 /* split into two rules to get bison to resolve a tVOID conflict */
301 arg: attributes type pident array { $$ = $3;
302 set_type($$, $2, $4);
305 | type pident array { $$ = $2;
306 set_type($$, $1, $3);
308 | attributes type pident '(' m_args ')' { $$ = $3;
310 set_type($$, $2, NULL);
314 | type pident '(' m_args ')' { $$ = $2;
316 set_type($$, $1, NULL);
321 array: { $$ = NULL; }
322 | '[' array_list ']' { $$ = $2; }
323 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
326 array_list: m_expr /* size of first dimension is optional */
327 | array_list ',' expr { LINK($3, $1); $$ = $3; }
328 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
331 m_attributes: { $$ = NULL; }
336 '[' attrib_list ']' { $$ = $2; }
339 attrib_list: attribute
340 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
341 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
345 tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
346 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
347 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
348 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
349 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
350 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
351 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
352 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
353 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
354 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
355 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
356 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
357 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
358 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
359 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
360 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
361 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
362 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
363 | tDUAL { $$ = make_attr(ATTR_DUAL); }
364 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
365 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
366 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
367 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
368 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
369 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
370 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
371 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
372 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
373 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
374 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
375 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
376 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
377 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
378 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
379 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
380 | tIN { $$ = make_attr(ATTR_IN); }
381 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
382 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
383 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
384 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
385 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
386 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
387 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
388 | tODL { $$ = make_attr(ATTR_ODL); }
389 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
390 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
391 | tOUT { $$ = make_attr(ATTR_OUT); }
392 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
393 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
394 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
395 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
396 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
397 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
398 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
399 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
400 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
401 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
402 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
403 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
404 | tSTRING { $$ = make_attr(ATTR_STRING); }
405 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
406 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
407 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
408 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
409 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
410 | tVARARG { $$ = make_attr(ATTR_VARARG); }
411 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
412 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
413 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
420 cases: { $$ = NULL; }
421 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
426 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
427 $$ = $4; if (!$$) $$ = make_var(NULL);
428 LINK(a, $$->attrs); $$->attrs = a;
430 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
431 $$ = $3; if (!$$) $$ = make_var(NULL);
432 LINK(a, $$->attrs); $$->attrs = a;
436 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
437 set_type($$, $2, NULL);
442 enums: { $$ = NULL; }
443 | enum_list ',' { $$ = $1; }
447 enum_list: enum { if (!$$->eval)
448 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
450 | enum_list ',' enum { LINK($3, $1); $$ = $3;
452 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
456 enum: ident '=' expr_const { $$ = reg_const($1);
458 $$->type = make_type(RPC_FC_LONG, &std_int);
460 | ident { $$ = reg_const($1);
461 $$->type = make_type(RPC_FC_LONG, &std_int);
465 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
474 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
478 exprs: { $$ = make_expr(EXPR_VOID); }
483 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
487 m_expr: { $$ = make_expr(EXPR_VOID); }
491 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
492 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
493 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
494 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
495 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
496 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
497 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
498 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
499 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
500 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
501 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
502 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
503 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
504 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
505 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
506 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
507 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
508 | '(' expr ')' { $$ = $2; }
511 expr_list_const: expr_const
512 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
515 expr_const: expr { $$ = $1;
517 yyerror("expression is not constant");
521 externdef: tEXTERN tCONST type ident { $$ = $4;
522 set_type($$, $3, NULL);
526 fields: { $$ = NULL; }
527 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
532 field: s_field ';' { $$ = $1; }
533 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
534 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
538 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
542 m_attributes type callconv pident
543 '(' m_args ')' { set_type($4, $2, NULL);
545 $$ = make_func($4, $6);
546 if (is_attr($4->attrs, ATTR_IN)) {
547 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
552 m_ident: { $$ = NULL; }
556 t_ident: { $$ = NULL; }
557 | aIDENTIFIER { $$ = make_var($1); }
558 | aKNOWNTYPE { $$ = make_var($1); }
561 ident: aIDENTIFIER { $$ = make_var($1); }
562 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
563 | aKNOWNTYPE { $$ = make_var($<str>1); }
564 | tASYNC { $$ = make_var($<str>1); }
565 | tID { $$ = make_var($<str>1); }
566 | tLCID { $$ = make_var($<str>1); }
567 | tRANGE { $$ = make_var($<str>1); }
568 | tRETVAL { $$ = make_var($<str>1); }
569 | tVERSION { $$ = make_var($<str>1); }
572 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
573 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
575 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
576 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
578 case RPC_FC_CHAR: break;
579 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
580 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
581 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
583 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
588 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
589 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
590 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
591 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
592 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
593 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
594 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
601 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
602 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
603 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
604 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
605 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
606 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
607 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
610 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
611 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
614 coclasshdr: attributes coclass { $$ = $2;
616 if (!parse_only && do_header)
621 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
626 coclass_ints: { $$ = NULL; }
627 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
631 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
634 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
635 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
638 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
640 if ($$->defined) yyerror("multiple definition error");
641 attrs = make_attr(ATTR_DISPINTERFACE);
644 $$->ref = find_type("IDispatch", 0);
645 if (!$$->ref) yyerror("IDispatch is undefined");
647 if (!parse_only && do_header) write_forward($$);
651 dispint_props: tPROPERTIES ':' { $$ = NULL; }
652 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
655 dispint_meths: tMETHODS ':' { $$ = NULL; }
656 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
659 dispinterfacedef: dispinterfacehdr '{'
665 if (!parse_only && do_header) write_dispinterface($$);
667 /* FIXME: not sure how to handle this yet
668 | dispinterfacehdr '{' interface '}' { $$ = $1;
669 if (!parse_only && do_header) write_interface($$);
674 inherit: { $$ = NULL; }
675 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
678 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
679 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
682 interfacehdr: attributes interface { $$ = $2;
683 if ($$->defined) yyerror("multiple definition error");
686 if (!parse_only && do_header) write_forward($$);
690 interfacedef: interfacehdr inherit
691 '{' int_statements '}' { $$ = $1;
694 if (!parse_only && do_header) write_interface($$);
696 /* MIDL is able to import the definition of a base class from inside the
697 * definition of a derived class, I'll try to support it with this rule */
698 | interfacehdr ':' aIDENTIFIER
699 '{' import int_statements '}' { $$ = $1;
700 $$->ref = find_type2($3, 0);
701 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
703 if (!parse_only && do_header) write_interface($$);
705 | dispinterfacedef { $$ = $1; }
709 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
710 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
713 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
714 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
717 modulehdr: attributes module { $$ = $2;
722 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
724 /* FIXME: if (!parse_only && do_header) write_module($$); */
728 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
729 | tCONST p_ident { $$ = $2; /* FIXME */ }
734 | '(' pident ')' { $$ = $2; }
739 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
743 tREF { $$ = RPC_FC_RP; }
744 | tUNIQUE { $$ = RPC_FC_UP; }
745 | tPTR { $$ = RPC_FC_FP; }
748 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
749 /* overwrite RPC_FC_STRUCT with a more exact type */
750 $$->type = get_struct_type( $4 );
758 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
759 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
760 | base_type { $$ = make_tref(NULL, $1); }
761 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
762 | enumdef { $$ = make_tref(NULL, $1); }
763 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
764 | structdef { $$ = make_tref(NULL, $1); }
765 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
766 | uniondef { $$ = make_tref(NULL, $1); }
767 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
770 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
771 $4->tname = tref->name;
775 if (!parse_only && do_header)
776 write_typedef($$, $4);
777 if (in_typelib && $$->attrs)
779 reg_types($$, $4, 0);
783 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
788 tSWITCH '(' s_field ')'
789 m_ident '{' cases '}' { var_t *u = $7;
790 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
791 if (!u) u = make_var("tagged_union");
792 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
793 u->type->fields = $9;
794 u->type->defined = TRUE;
795 LINK(u, $5); $$->fields = u;
801 aNUM { $$ = MAKELONG($1, 0); }
802 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
807 static attr_t *make_attr(enum attr_type type)
809 attr_t *a = xmalloc(sizeof(attr_t));
816 static attr_t *make_attrv(enum attr_type type, unsigned long val)
818 attr_t *a = xmalloc(sizeof(attr_t));
825 static attr_t *make_attrp(enum attr_type type, void *val)
827 attr_t *a = xmalloc(sizeof(attr_t));
834 static expr_t *make_expr(enum expr_type type)
836 expr_t *e = xmalloc(sizeof(expr_t));
845 static expr_t *make_exprl(enum expr_type type, long val)
847 expr_t *e = xmalloc(sizeof(expr_t));
853 /* check for numeric constant */
854 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
861 static expr_t *make_exprs(enum expr_type type, char *val)
864 e = xmalloc(sizeof(expr_t));
870 /* check for predefined constants */
871 if (type == EXPR_IDENTIFIER) {
872 var_t *c = find_const(val, 0);
877 e->cval = c->eval->cval;
883 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
886 e = xmalloc(sizeof(expr_t));
892 /* check for cast of constant expression */
893 if (type == EXPR_SIZEOF) {
894 switch (tref->ref->type) {
911 case RPC_FC_ERROR_STATUS_T:
922 if (type == EXPR_CAST && expr->is_const) {
924 e->cval = expr->cval;
929 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
932 e = xmalloc(sizeof(expr_t));
938 /* check for compile-time optimization */
939 if (expr->is_const) {
943 e->cval = -expr->cval;
946 e->cval = ~expr->cval;
956 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
959 e = xmalloc(sizeof(expr_t));
965 /* check for compile-time optimization */
966 if (expr1->is_const && expr2->is_const) {
970 e->cval = expr1->cval + expr2->cval;
973 e->cval = expr1->cval - expr2->cval;
976 e->cval = expr1->cval * expr2->cval;
979 e->cval = expr1->cval / expr2->cval;
982 e->cval = expr1->cval | expr2->cval;
985 e->cval = expr1->cval & expr2->cval;
988 e->cval = expr1->cval << expr2->cval;
991 e->cval = expr1->cval >> expr2->cval;
1001 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1004 e = xmalloc(sizeof(expr_t));
1009 e->is_const = FALSE;
1011 /* check for compile-time optimization */
1012 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1016 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1019 e->is_const = FALSE;
1026 static type_t *make_type(unsigned char type, type_t *ref)
1028 type_t *t = xmalloc(sizeof(type_t));
1035 t->ignore = parse_only;
1036 t->is_const = FALSE;
1040 t->typelib_idx = -1;
1045 static typeref_t *make_tref(char *name, type_t *ref)
1047 typeref_t *t = xmalloc(sizeof(typeref_t));
1050 t->uniq = ref ? 0 : 1;
1054 static typeref_t *uniq_tref(typeref_t *ref)
1058 if (t->uniq) return t;
1059 tp = make_type(0, t->ref);
1067 static type_t *type_ref(typeref_t *ref)
1069 type_t *t = ref->ref;
1070 if (ref->name) free(ref->name);
1075 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1078 v->tname = ref->name;
1084 static ifref_t *make_ifref(type_t *iface)
1086 ifref_t *l = xmalloc(sizeof(ifref_t));
1093 static var_t *make_var(char *name)
1095 var_t *v = xmalloc(sizeof(var_t));
1107 static func_t *make_func(var_t *def, var_t *args)
1109 func_t *f = xmalloc(sizeof(func_t));
1112 f->ignore = parse_only;
1118 static class_t *make_class(char *name)
1120 class_t *c = xmalloc(sizeof(class_t));
1130 static int hash_ident(const char *name)
1132 const char *p = name;
1134 /* a simple sum hash is probably good enough */
1139 return sum & (HASHMAX-1);
1142 /***** type repository *****/
1151 struct rtype *type_hash[HASHMAX];
1153 static type_t *reg_type(type_t *type, const char *name, int t)
1158 yyerror("registering named type without name");
1161 hash = hash_ident(name);
1162 nt = xmalloc(sizeof(struct rtype));
1166 nt->next = type_hash[hash];
1167 type_hash[hash] = nt;
1171 /* determine pointer type from attrs */
1172 static unsigned char get_pointer_type( type_t *type )
1175 if (is_attr( type->attrs, ATTR_STRING ))
1178 while( t->type == 0 && t->ref )
1183 return RPC_FC_C_CSTRING;
1185 return RPC_FC_C_WSTRING;
1188 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1193 static type_t *reg_types(type_t *type, var_t *names, int t)
1199 var_t *next = NEXT_LINK(names);
1202 int cptr = names->ptr_level;
1204 while (cptr > ptrc) {
1205 int t = get_pointer_type( cur );
1206 cur = ptr = make_type(t, cur);
1210 while (cptr < ptrc) {
1215 reg_type(cur, names->name, t);
1223 static type_t *find_type(const char *name, int t)
1225 struct rtype *cur = type_hash[hash_ident(name)];
1226 while (cur && (cur->t != t || strcmp(cur->name, name)))
1229 yyerror("type '%s' not found", name);
1235 static type_t *find_type2(char *name, int t)
1237 type_t *tp = find_type(name, t);
1242 int is_type(const char *name)
1244 struct rtype *cur = type_hash[hash_ident(name)];
1245 while (cur && (cur->t || strcmp(cur->name, name)))
1247 if (cur) return TRUE;
1251 static type_t *get_type(unsigned char type, char *name, int t)
1253 struct rtype *cur = NULL;
1256 cur = type_hash[hash_ident(name)];
1257 while (cur && (cur->t != t || strcmp(cur->name, name)))
1264 tp = make_type(type, NULL);
1266 if (!name) return tp;
1267 return reg_type(tp, name, t);
1270 static type_t *get_typev(unsigned char type, var_t *name, int t)
1277 return get_type(type, sname, t);
1280 static int get_struct_type(var_t *field)
1282 int has_pointer = 0;
1283 int has_conformance = 0;
1284 int has_variance = 0;
1286 for (; field; field = NEXT_LINK(field))
1288 type_t *t = field->type;
1290 /* get the base type */
1291 while( (t->type == 0) && t->ref )
1294 if (field->ptr_level > 0)
1300 if (is_string_type(field->attrs, 0, field->array))
1302 has_conformance = 1;
1307 if (is_array_type(field->attrs, 0, field->array))
1309 if (field->array && !field->array->is_const)
1311 has_conformance = 1;
1312 if (PREV_LINK(field))
1313 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1316 if (is_attr(field->attrs, ATTR_LENGTHIS))
1323 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1324 * Simple types don't effect the type of struct.
1325 * A struct containing a simple struct is still a simple struct.
1326 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1328 case 0: /* void pointer */
1338 case RPC_FC_INT3264:
1339 case RPC_FC_UINT3264:
1353 has_conformance = 1;
1354 if (PREV_LINK(field))
1355 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1358 case RPC_FC_C_CSTRING:
1359 case RPC_FC_C_WSTRING:
1360 has_conformance = 1;
1365 * Propagate member attributes
1366 * a struct should be at least as complex as its member
1368 case RPC_FC_CVSTRUCT:
1369 has_conformance = 1;
1374 case RPC_FC_CPSTRUCT:
1375 has_conformance = 1;
1376 if (PREV_LINK(field))
1377 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1382 case RPC_FC_CSTRUCT:
1383 has_conformance = 1;
1384 if (PREV_LINK(field))
1385 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1389 case RPC_FC_PSTRUCT:
1394 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1395 field->name, t->type);
1396 /* fallthru - treat it as complex */
1398 /* as soon as we see one of these these members, it's bogus... */
1400 case RPC_FC_ENCAPSULATED_UNION:
1401 case RPC_FC_NON_ENCAPSULATED_UNION:
1402 case RPC_FC_TRANSMIT_AS:
1403 case RPC_FC_REPRESENT_AS:
1405 case RPC_FC_EMBEDDED_COMPLEX:
1406 case RPC_FC_BOGUS_STRUCT:
1407 return RPC_FC_BOGUS_STRUCT;
1412 return RPC_FC_CVSTRUCT;
1413 if( has_conformance && has_pointer )
1414 return RPC_FC_CPSTRUCT;
1415 if( has_conformance )
1416 return RPC_FC_CSTRUCT;
1418 return RPC_FC_PSTRUCT;
1419 return RPC_FC_STRUCT;
1422 /***** constant repository *****/
1427 struct rconst *next;
1430 struct rconst *const_hash[HASHMAX];
1432 static var_t *reg_const(var_t *var)
1437 yyerror("registering constant without name");
1440 hash = hash_ident(var->name);
1441 nc = xmalloc(sizeof(struct rconst));
1442 nc->name = var->name;
1444 nc->next = const_hash[hash];
1445 const_hash[hash] = nc;
1449 static var_t *find_const(char *name, int f)
1451 struct rconst *cur = const_hash[hash_ident(name)];
1452 while (cur && strcmp(cur->name, name))
1455 if (f) yyerror("constant '%s' not found", name);