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);
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 coclass ';' { $$ = $1; if (!parse_only && do_header) write_coclass_forward($2); }
243 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
244 | gbl_statements moduledef { $$ = $1; add_module($2); }
245 | gbl_statements librarydef { $$ = $1; }
246 | gbl_statements statement { $$ = $1; }
250 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
251 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
252 | imp_statements coclass ';' {}
253 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
254 | imp_statements moduledef { if (!parse_only) add_module($2); }
255 | imp_statements statement {}
256 | imp_statements importlib {}
259 int_statements: { $$ = NULL; }
260 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
261 | int_statements statement { $$ = $1; }
265 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
267 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
268 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
270 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
272 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
275 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
277 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
278 if (!do_import($2)) yychar = aEOF; }
280 import: import_start imp_statements aEOF {}
283 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
286 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
288 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
289 if (!parse_only && do_header) write_library($2, $1); }
291 librarydef: library_start imp_statements '}' { end_typelib(); }
294 m_args: { $$ = NULL; }
298 no_args: tVOID { $$ = NULL; }
302 | args ',' arg { LINK($3, $1); $$ = $3; }
306 /* split into two rules to get bison to resolve a tVOID conflict */
307 arg: attributes type pident array { $$ = $3;
308 set_type($$, $2, $4);
311 | type pident array { $$ = $2;
312 set_type($$, $1, $3);
314 | attributes type pident '(' m_args ')' { $$ = $3;
316 set_type($$, $2, NULL);
320 | type pident '(' m_args ')' { $$ = $2;
322 set_type($$, $1, NULL);
327 array: { $$ = NULL; }
328 | '[' array_list ']' { $$ = $2; }
329 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
332 array_list: m_expr /* size of first dimension is optional */
333 | array_list ',' expr { LINK($3, $1); $$ = $3; }
334 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
337 m_attributes: { $$ = NULL; }
342 '[' attrib_list ']' { $$ = $2; }
345 attrib_list: attribute
346 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
347 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
351 tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
352 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
353 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
354 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
355 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
356 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
357 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
358 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
359 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
360 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
361 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
362 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
363 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
364 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
365 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
366 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
367 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
368 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
369 | tDUAL { $$ = make_attr(ATTR_DUAL); }
370 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
371 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
372 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
373 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
374 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
375 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
376 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
377 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
378 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
379 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
380 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
381 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
382 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
383 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
384 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
385 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
386 | tIN { $$ = make_attr(ATTR_IN); }
387 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
388 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
389 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
390 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
391 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
392 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
393 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
394 | tODL { $$ = make_attr(ATTR_ODL); }
395 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
396 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
397 | tOUT { $$ = make_attr(ATTR_OUT); }
398 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
399 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
400 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
401 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
402 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
403 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
404 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
405 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
406 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
407 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
408 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
409 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
410 | tSTRING { $$ = make_attr(ATTR_STRING); }
411 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
412 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
413 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
414 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
415 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
416 | tVARARG { $$ = make_attr(ATTR_VARARG); }
417 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
418 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
419 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
426 cases: { $$ = NULL; }
427 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
432 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
433 $$ = $4; if (!$$) $$ = make_var(NULL);
434 LINK(a, $$->attrs); $$->attrs = a;
436 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
437 $$ = $3; if (!$$) $$ = make_var(NULL);
438 LINK(a, $$->attrs); $$->attrs = a;
442 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
443 set_type($$, $2, NULL);
448 enums: { $$ = NULL; }
449 | enum_list ',' { $$ = $1; }
453 enum_list: enum { if (!$$->eval)
454 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
456 | enum_list ',' enum { LINK($3, $1); $$ = $3;
458 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
462 enum: ident '=' expr_const { $$ = reg_const($1);
464 $$->type = make_type(RPC_FC_LONG, &std_int);
466 | ident { $$ = reg_const($1);
467 $$->type = make_type(RPC_FC_LONG, &std_int);
471 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
480 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
484 exprs: { $$ = make_expr(EXPR_VOID); }
489 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
493 m_expr: { $$ = make_expr(EXPR_VOID); }
497 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
498 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
499 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
500 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
501 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
502 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
503 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
504 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
505 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
506 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
507 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
508 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
509 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
510 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
511 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
512 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
513 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
514 | '(' expr ')' { $$ = $2; }
517 expr_list_const: expr_const
518 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
521 expr_const: expr { $$ = $1;
523 yyerror("expression is not constant");
527 externdef: tEXTERN tCONST type ident { $$ = $4;
528 set_type($$, $3, NULL);
532 fields: { $$ = NULL; }
533 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
538 field: s_field ';' { $$ = $1; }
539 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
540 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
544 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
548 m_attributes type callconv pident
549 '(' m_args ')' { set_type($4, $2, NULL);
551 $$ = make_func($4, $6);
552 if (is_attr($4->attrs, ATTR_IN)) {
553 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
558 m_ident: { $$ = NULL; }
562 t_ident: { $$ = NULL; }
563 | aIDENTIFIER { $$ = make_var($1); }
564 | aKNOWNTYPE { $$ = make_var($1); }
567 ident: aIDENTIFIER { $$ = make_var($1); }
568 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
569 | aKNOWNTYPE { $$ = make_var($<str>1); }
570 | tASYNC { $$ = make_var($<str>1); }
571 | tID { $$ = make_var($<str>1); }
572 | tLCID { $$ = make_var($<str>1); }
573 | tOBJECT { $$ = make_var($<str>1); }
574 | tRANGE { $$ = make_var($<str>1); }
575 | tRETVAL { $$ = make_var($<str>1); }
576 | tUUID { $$ = make_var($<str>1); }
577 | tVERSION { $$ = make_var($<str>1); }
580 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
581 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
583 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
584 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
586 case RPC_FC_CHAR: break;
587 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
588 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
589 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
591 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
596 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
597 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
598 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
599 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
600 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
601 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
602 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
609 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
610 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
611 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
612 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
613 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
614 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
615 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
618 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
619 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
622 coclasshdr: attributes coclass { $$ = $2;
624 if (!parse_only && do_header)
629 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
634 coclass_ints: { $$ = NULL; }
635 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
639 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
642 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
643 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
646 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
648 if ($$->defined) yyerror("multiple definition error");
649 attrs = make_attr(ATTR_DISPINTERFACE);
652 $$->ref = find_type("IDispatch", 0);
653 if (!$$->ref) yyerror("IDispatch is undefined");
655 if (!parse_only && do_header) write_forward($$);
659 dispint_props: tPROPERTIES ':' { $$ = NULL; }
660 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
663 dispint_meths: tMETHODS ':' { $$ = NULL; }
664 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
667 dispinterfacedef: dispinterfacehdr '{'
673 if (!parse_only && do_header) write_dispinterface($$);
675 /* FIXME: not sure how to handle this yet
676 | dispinterfacehdr '{' interface '}' { $$ = $1;
677 if (!parse_only && do_header) write_interface($$);
682 inherit: { $$ = NULL; }
683 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
686 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
687 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
690 interfacehdr: attributes interface { $$ = $2;
691 if ($$->defined) yyerror("multiple definition error");
694 if (!parse_only && do_header) write_forward($$);
698 interfacedef: interfacehdr inherit
699 '{' int_statements '}' { $$ = $1;
702 if (!parse_only && do_header) write_interface($$);
704 /* MIDL is able to import the definition of a base class from inside the
705 * definition of a derived class, I'll try to support it with this rule */
706 | interfacehdr ':' aIDENTIFIER
707 '{' import int_statements '}' { $$ = $1;
708 $$->ref = find_type2($3, 0);
709 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
711 if (!parse_only && do_header) write_interface($$);
713 | dispinterfacedef { $$ = $1; }
717 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
718 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
721 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
722 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
725 modulehdr: attributes module { $$ = $2;
730 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
732 /* FIXME: if (!parse_only && do_header) write_module($$); */
736 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
737 | tCONST p_ident { $$ = $2; /* FIXME */ }
742 | '(' pident ')' { $$ = $2; }
747 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
751 tREF { $$ = RPC_FC_RP; }
752 | tUNIQUE { $$ = RPC_FC_UP; }
753 | tPTR { $$ = RPC_FC_FP; }
756 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
757 /* overwrite RPC_FC_STRUCT with a more exact type */
758 $$->type = get_struct_type( $4 );
766 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
767 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
768 | base_type { $$ = make_tref(NULL, $1); }
769 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
770 | enumdef { $$ = make_tref(NULL, $1); }
771 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
772 | structdef { $$ = make_tref(NULL, $1); }
773 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
774 | uniondef { $$ = make_tref(NULL, $1); }
775 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
778 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
779 $4->tname = tref->name;
783 if (!parse_only && do_header)
784 write_typedef($$, $4);
785 if (in_typelib && $$->attrs)
787 reg_types($$, $4, 0);
791 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
796 tSWITCH '(' s_field ')'
797 m_ident '{' cases '}' { var_t *u = $7;
798 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
799 if (!u) u = make_var("tagged_union");
800 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
801 u->type->fields = $9;
802 u->type->defined = TRUE;
803 LINK(u, $5); $$->fields = u;
809 aNUM { $$ = MAKELONG($1, 0); }
810 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
815 static attr_t *make_attr(enum attr_type type)
817 attr_t *a = xmalloc(sizeof(attr_t));
824 static attr_t *make_attrv(enum attr_type type, unsigned long val)
826 attr_t *a = xmalloc(sizeof(attr_t));
833 static attr_t *make_attrp(enum attr_type type, void *val)
835 attr_t *a = xmalloc(sizeof(attr_t));
842 static expr_t *make_expr(enum expr_type type)
844 expr_t *e = xmalloc(sizeof(expr_t));
853 static expr_t *make_exprl(enum expr_type type, long val)
855 expr_t *e = xmalloc(sizeof(expr_t));
861 /* check for numeric constant */
862 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
869 static expr_t *make_exprs(enum expr_type type, char *val)
872 e = xmalloc(sizeof(expr_t));
878 /* check for predefined constants */
879 if (type == EXPR_IDENTIFIER) {
880 var_t *c = find_const(val, 0);
885 e->cval = c->eval->cval;
891 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
894 e = xmalloc(sizeof(expr_t));
900 /* check for cast of constant expression */
901 if (type == EXPR_SIZEOF) {
902 switch (tref->ref->type) {
919 case RPC_FC_ERROR_STATUS_T:
930 if (type == EXPR_CAST && expr->is_const) {
932 e->cval = expr->cval;
937 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
940 e = xmalloc(sizeof(expr_t));
946 /* check for compile-time optimization */
947 if (expr->is_const) {
951 e->cval = -expr->cval;
954 e->cval = ~expr->cval;
964 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
967 e = xmalloc(sizeof(expr_t));
973 /* check for compile-time optimization */
974 if (expr1->is_const && expr2->is_const) {
978 e->cval = expr1->cval + expr2->cval;
981 e->cval = expr1->cval - expr2->cval;
984 e->cval = expr1->cval * expr2->cval;
987 e->cval = expr1->cval / expr2->cval;
990 e->cval = expr1->cval | expr2->cval;
993 e->cval = expr1->cval & expr2->cval;
996 e->cval = expr1->cval << expr2->cval;
999 e->cval = expr1->cval >> expr2->cval;
1002 e->is_const = FALSE;
1009 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1012 e = xmalloc(sizeof(expr_t));
1017 e->is_const = FALSE;
1019 /* check for compile-time optimization */
1020 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1024 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1027 e->is_const = FALSE;
1034 static type_t *make_type(unsigned char type, type_t *ref)
1036 type_t *t = xmalloc(sizeof(type_t));
1043 t->ignore = parse_only;
1044 t->is_const = FALSE;
1048 t->typelib_idx = -1;
1053 static typeref_t *make_tref(char *name, type_t *ref)
1055 typeref_t *t = xmalloc(sizeof(typeref_t));
1058 t->uniq = ref ? 0 : 1;
1062 static typeref_t *uniq_tref(typeref_t *ref)
1066 if (t->uniq) return t;
1067 tp = make_type(0, t->ref);
1075 static type_t *type_ref(typeref_t *ref)
1077 type_t *t = ref->ref;
1078 if (ref->name) free(ref->name);
1083 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1086 v->tname = ref->name;
1092 static ifref_t *make_ifref(type_t *iface)
1094 ifref_t *l = xmalloc(sizeof(ifref_t));
1101 static var_t *make_var(char *name)
1103 var_t *v = xmalloc(sizeof(var_t));
1115 static func_t *make_func(var_t *def, var_t *args)
1117 func_t *f = xmalloc(sizeof(func_t));
1120 f->ignore = parse_only;
1126 static class_t *make_class(char *name)
1128 class_t *c = xmalloc(sizeof(class_t));
1138 static int hash_ident(const char *name)
1140 const char *p = name;
1142 /* a simple sum hash is probably good enough */
1147 return sum & (HASHMAX-1);
1150 /***** type repository *****/
1159 struct rtype *type_hash[HASHMAX];
1161 static type_t *reg_type(type_t *type, const char *name, int t)
1166 yyerror("registering named type without name");
1169 hash = hash_ident(name);
1170 nt = xmalloc(sizeof(struct rtype));
1174 nt->next = type_hash[hash];
1175 type_hash[hash] = nt;
1179 /* determine pointer type from attrs */
1180 static unsigned char get_pointer_type( type_t *type )
1183 if (is_attr( type->attrs, ATTR_STRING ))
1186 while( t->type == 0 && t->ref )
1191 return RPC_FC_C_CSTRING;
1193 return RPC_FC_C_WSTRING;
1196 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1201 static type_t *reg_types(type_t *type, var_t *names, int t)
1207 var_t *next = NEXT_LINK(names);
1210 int cptr = names->ptr_level;
1212 while (cptr > ptrc) {
1213 int t = get_pointer_type( cur );
1214 cur = ptr = make_type(t, cur);
1218 while (cptr < ptrc) {
1223 reg_type(cur, names->name, t);
1231 static type_t *find_type(const char *name, int t)
1233 struct rtype *cur = type_hash[hash_ident(name)];
1234 while (cur && (cur->t != t || strcmp(cur->name, name)))
1237 yyerror("type '%s' not found", name);
1243 static type_t *find_type2(char *name, int t)
1245 type_t *tp = find_type(name, t);
1250 int is_type(const char *name)
1252 struct rtype *cur = type_hash[hash_ident(name)];
1253 while (cur && (cur->t || strcmp(cur->name, name)))
1255 if (cur) return TRUE;
1259 static type_t *get_type(unsigned char type, char *name, int t)
1261 struct rtype *cur = NULL;
1264 cur = type_hash[hash_ident(name)];
1265 while (cur && (cur->t != t || strcmp(cur->name, name)))
1272 tp = make_type(type, NULL);
1274 if (!name) return tp;
1275 return reg_type(tp, name, t);
1278 static type_t *get_typev(unsigned char type, var_t *name, int t)
1285 return get_type(type, sname, t);
1288 static int get_struct_type(var_t *field)
1290 int has_pointer = 0;
1291 int has_conformance = 0;
1292 int has_variance = 0;
1294 for (; field; field = NEXT_LINK(field))
1296 type_t *t = field->type;
1298 /* get the base type */
1299 while( (t->type == 0) && t->ref )
1302 if (field->ptr_level > 0)
1308 if (is_string_type(field->attrs, 0, field->array))
1310 has_conformance = 1;
1315 if (is_array_type(field->attrs, 0, field->array))
1317 if (field->array && !field->array->is_const)
1319 has_conformance = 1;
1320 if (PREV_LINK(field))
1321 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1324 if (is_attr(field->attrs, ATTR_LENGTHIS))
1331 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1332 * Simple types don't effect the type of struct.
1333 * A struct containing a simple struct is still a simple struct.
1334 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1336 case 0: /* void pointer */
1346 case RPC_FC_INT3264:
1347 case RPC_FC_UINT3264:
1361 has_conformance = 1;
1362 if (PREV_LINK(field))
1363 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1366 case RPC_FC_C_CSTRING:
1367 case RPC_FC_C_WSTRING:
1368 has_conformance = 1;
1373 * Propagate member attributes
1374 * a struct should be at least as complex as its member
1376 case RPC_FC_CVSTRUCT:
1377 has_conformance = 1;
1382 case RPC_FC_CPSTRUCT:
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",
1390 case RPC_FC_CSTRUCT:
1391 has_conformance = 1;
1392 if (PREV_LINK(field))
1393 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1397 case RPC_FC_PSTRUCT:
1402 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1403 field->name, t->type);
1404 /* fallthru - treat it as complex */
1406 /* as soon as we see one of these these members, it's bogus... */
1408 case RPC_FC_ENCAPSULATED_UNION:
1409 case RPC_FC_NON_ENCAPSULATED_UNION:
1410 case RPC_FC_TRANSMIT_AS:
1411 case RPC_FC_REPRESENT_AS:
1413 case RPC_FC_EMBEDDED_COMPLEX:
1414 case RPC_FC_BOGUS_STRUCT:
1415 return RPC_FC_BOGUS_STRUCT;
1420 return RPC_FC_CVSTRUCT;
1421 if( has_conformance && has_pointer )
1422 return RPC_FC_CPSTRUCT;
1423 if( has_conformance )
1424 return RPC_FC_CSTRUCT;
1426 return RPC_FC_PSTRUCT;
1427 return RPC_FC_STRUCT;
1430 /***** constant repository *****/
1435 struct rconst *next;
1438 struct rconst *const_hash[HASHMAX];
1440 static var_t *reg_const(var_t *var)
1445 yyerror("registering constant without name");
1448 hash = hash_ident(var->name);
1449 nc = xmalloc(sizeof(struct rconst));
1450 nc->name = var->name;
1452 nc->next = const_hash[hash];
1453 const_hash[hash] = nc;
1457 static var_t *find_const(char *name, int f)
1459 struct rconst *cur = const_hash[hash_ident(name)];
1460 while (cur && strcmp(cur->name, name))
1463 if (f) yyerror("constant '%s' not found", name);