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 str_list_t *append_str(str_list_t *list, char *str);
68 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
69 static attr_t *make_attr(enum attr_type type);
70 static attr_t *make_attrv(enum attr_type type, unsigned long val);
71 static attr_t *make_attrp(enum attr_type type, void *val);
72 static expr_t *make_expr(enum expr_type type);
73 static expr_t *make_exprl(enum expr_type type, long val);
74 static expr_t *make_exprs(enum expr_type type, char *val);
75 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
76 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
77 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
78 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
79 static type_t *make_type(unsigned char type, type_t *ref);
80 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
81 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
82 static void set_type(var_t *v, type_t *type, array_dims_t *arr);
83 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
84 static ifref_t *make_ifref(type_t *iface);
85 static var_list_t *append_var(var_list_t *list, var_t *var);
86 static var_t *make_var(char *name);
87 static func_list_t *append_func(func_list_t *list, func_t *func);
88 static func_t *make_func(var_t *def, var_list_t *args);
89 static type_t *make_class(char *name);
90 static type_t *make_safearray(type_t *type);
91 static type_t *make_builtin(char *name);
92 static type_t *make_int(int sign);
94 static type_t *reg_type(type_t *type, const char *name, int t);
95 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
96 static type_t *find_type(const char *name, int t);
97 static type_t *find_type2(char *name, int t);
98 static type_t *get_type(unsigned char type, char *name, int t);
99 static type_t *get_typev(unsigned char type, var_t *name, int t);
100 static int get_struct_type(var_list_t *fields);
102 static var_t *reg_const(var_t *var);
103 static var_t *find_const(char *name, int f);
105 static void write_libid(const char *name, const attr_list_t *attr);
106 static void write_clsid(type_t *cls);
107 static void write_diid(type_t *iface);
108 static void write_iid(type_t *iface);
110 static int compute_method_indexes(type_t *iface);
111 static char *gen_name(void);
112 static void process_typedefs(var_list_t *names);
113 static void check_arg(var_t *arg);
122 attr_list_t *attr_list;
123 str_list_t *str_list;
125 expr_list_t *expr_list;
126 array_dims_t *array_dims;
129 var_list_t *var_list;
131 func_list_t *func_list;
133 ifref_list_t *ifref_list;
139 %token <str> aIDENTIFIER
140 %token <str> aKNOWNTYPE
141 %token <num> aNUM aHEXNUM
146 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
147 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
148 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
149 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
150 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
152 %token tDEFAULTCOLLELEM
154 %token tDEFAULTVTABLE
156 %token tDISPINTERFACE
157 %token tDLLNAME tDOUBLE tDUAL
159 %token tENTRY tENUM tERRORSTATUST
160 %token tEXPLICITHANDLE tEXTERN
165 %token tHELPCONTEXT tHELPFILE
166 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
168 %token tHYPER tID tIDEMPOTENT
170 %token tIMMEDIATEBIND
171 %token tIMPLICITHANDLE
172 %token tIMPORT tIMPORTLIB
178 %token tLENGTHIS tLIBRARY
185 %token tNONEXTENSIBLE
186 %token tOBJECT tODL tOLEAUTOMATION
189 %token tPOINTERDEFAULT
191 %token tPROPGET tPROPPUT tPROPPUTREF
195 %token tREADONLY tREF
203 %token tSIZEIS tSIZEOF
207 %token tSTRING tSTRUCT
208 %token tSWITCH tSWITCHIS tSWITCHTYPE
220 %token tWCHAR tWIREMARSHAL
222 %type <attr> attribute
223 %type <attr_list> m_attributes attributes attrib_list
224 %type <str_list> str_list
225 %type <expr> m_expr expr expr_const
226 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
227 %type <array_dims> array array_list
228 %type <type> inherit interface interfacehdr interfacedef interfacedec
229 %type <type> dispinterface dispinterfacehdr dispinterfacedef
230 %type <type> module modulehdr moduledef
231 %type <type> base_type int_std
232 %type <type> enumdef structdef uniondef
234 %type <ifref> coclass_int
235 %type <ifref_list> gbl_statements coclass_ints
236 %type <var> arg field s_field case enum constdef externdef
237 %type <var_list> m_args no_args args fields cases enums enum_list pident_list dispint_props
238 %type <var> m_ident t_ident ident p_ident pident
240 %type <func_list> int_statements dispint_meths
241 %type <type> coclass coclasshdr coclassdef
242 %type <num> pointer_type version
243 %type <str> libraryhdr
259 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
262 gbl_statements: { $$ = NULL; }
263 | gbl_statements interfacedec { $$ = $1; }
264 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
265 | gbl_statements coclass ';' { $$ = $1;
266 reg_type($2, $2->name, 0);
267 if (!parse_only && do_header) write_coclass_forward($2);
269 | gbl_statements coclassdef { $$ = $1;
270 add_typelib_entry($2);
271 reg_type($2, $2->name, 0);
272 if (!parse_only && do_header) write_coclass_forward($2);
274 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
275 | gbl_statements librarydef { $$ = $1; }
276 | gbl_statements statement { $$ = $1; }
280 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
281 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
282 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
283 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
284 reg_type($2, $2->name, 0);
285 if (!parse_only && do_header) write_coclass_forward($2);
287 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
288 | imp_statements statement {}
289 | imp_statements importlib {}
292 int_statements: { $$ = NULL; }
293 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
294 | int_statements statement { $$ = $1; }
298 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
300 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL); fprintf(header, ";\n\n"); } }
301 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
303 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL); fprintf(header, ";\n\n"); } }
305 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL); fprintf(header, ";\n\n"); } }
308 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
310 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
311 if (!do_import($2)) yychar = aEOF; }
313 import: import_start imp_statements aEOF {}
316 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
319 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
321 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
322 if (!parse_only && do_header) write_library($2, $1);
323 if (!parse_only && do_idfile) write_libid($2, $1);
326 librarydef: library_start imp_statements '}' { end_typelib(); }
329 m_args: { $$ = NULL; }
333 no_args: tVOID { $$ = NULL; }
336 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
337 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
341 /* split into two rules to get bison to resolve a tVOID conflict */
342 arg: attributes type pident array { $$ = $3;
343 set_type($$, $2, $4);
346 | type pident array { $$ = $2;
347 set_type($$, $1, $3);
349 | attributes type pident '(' m_args ')' { $$ = $3;
351 set_type($$, $2, NULL);
355 | type pident '(' m_args ')' { $$ = $2;
357 set_type($$, $1, NULL);
362 array: { $$ = NULL; }
363 | '[' array_list ']' { $$ = $2; }
364 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
367 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
368 | array_list ',' expr { $$ = append_array( $1, $3 ); }
369 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
372 m_attributes: { $$ = NULL; }
377 '[' attrib_list ']' { $$ = $2;
379 yyerror("empty attribute lists unsupported");
383 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
384 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
385 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
388 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
389 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
392 attribute: { $$ = NULL; }
393 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
394 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
395 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
396 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
397 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
398 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
399 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
400 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
401 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
402 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
403 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
404 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
405 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
406 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
407 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
408 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
409 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
410 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
411 | tDUAL { $$ = make_attr(ATTR_DUAL); }
412 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
413 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
414 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
415 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
416 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
417 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
418 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
419 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
420 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
421 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
422 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
423 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
424 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
425 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
426 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
427 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
428 | tIN { $$ = make_attr(ATTR_IN); }
429 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
430 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
431 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
432 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
433 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
434 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
435 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
436 | tODL { $$ = make_attr(ATTR_ODL); }
437 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
438 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
439 | tOUT { $$ = make_attr(ATTR_OUT); }
440 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
441 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
442 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
443 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
444 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
445 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
446 list = append_expr( list, $5 );
447 $$ = make_attrp(ATTR_RANGE, list); }
448 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
449 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
450 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
451 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
452 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
453 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
454 | tSTRING { $$ = make_attr(ATTR_STRING); }
455 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
456 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
457 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
458 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
459 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
460 | tVARARG { $$ = make_attr(ATTR_VARARG); }
461 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
462 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
463 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
470 cases: { $$ = NULL; }
471 | cases case { $$ = append_var( $1, $2 ); }
474 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
475 $$ = $4; if (!$$) $$ = make_var(NULL);
476 $$->attrs = append_attr( $$->attrs, a );
478 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
479 $$ = $3; if (!$$) $$ = make_var(NULL);
480 $$->attrs = append_attr( $$->attrs, a );
484 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
485 set_type($$, $2, NULL);
490 enums: { $$ = NULL; }
491 | enum_list ',' { $$ = $1; }
495 enum_list: enum { if (!$1->eval)
496 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
497 $$ = append_var( NULL, $1 );
499 | enum_list ',' enum { if (!$3->eval)
501 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
502 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
504 $$ = append_var( $1, $3 );
508 enum: ident '=' expr_const { $$ = reg_const($1);
510 $$->type = make_int(0);
512 | ident { $$ = reg_const($1);
513 $$->type = make_int(0);
517 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
518 $$->kind = TKIND_ENUM;
522 add_typelib_entry($$);
526 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
527 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
531 exprs: { $$ = make_expr(EXPR_VOID); }
536 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
540 m_expr: { $$ = make_expr(EXPR_VOID); }
544 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
545 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
546 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
547 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
548 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
549 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
550 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
551 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
552 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
553 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
554 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
555 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
556 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
557 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
558 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
559 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
560 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
561 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
562 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
563 | '(' expr ')' { $$ = $2; }
566 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
567 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
570 expr_const: expr { $$ = $1;
572 yyerror("expression is not constant");
576 externdef: tEXTERN tCONST type ident { $$ = $4;
577 set_type($$, $3, NULL);
581 fields: { $$ = NULL; }
582 | fields field { $$ = append_var( $1, $2 ); }
585 field: s_field ';' { $$ = $1; }
586 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
587 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
591 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
595 m_attributes type callconv pident
596 '(' m_args ')' { set_type($4, $2, NULL);
598 $$ = make_func($4, $6);
599 if (is_attr($4->attrs, ATTR_IN)) {
600 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
605 m_ident: { $$ = NULL; }
609 t_ident: { $$ = NULL; }
610 | aIDENTIFIER { $$ = make_var($1); }
611 | aKNOWNTYPE { $$ = make_var($1); }
614 ident: aIDENTIFIER { $$ = make_var($1); }
615 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
616 | aKNOWNTYPE { $$ = make_var($<str>1); }
619 base_type: tBYTE { $$ = make_builtin($<str>1); }
620 | tWCHAR { $$ = make_builtin($<str>1); }
622 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
623 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
625 case RPC_FC_CHAR: break;
626 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
627 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
628 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
630 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
632 $$ = alias($$, "MIDL_uhyper");
639 | tUNSIGNED { $$ = make_int(-1); }
640 | tFLOAT { $$ = make_builtin($<str>1); }
641 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
642 | tDOUBLE { $$ = make_builtin($<str>1); }
643 | tBOOLEAN { $$ = make_builtin($<str>1); }
644 | tERRORSTATUST { $$ = make_builtin($<str>1); }
645 | tHANDLET { $$ = make_builtin($<str>1); }
652 int_std: tINT { $$ = make_builtin($<str>1); }
653 | tSHORT m_int { $$ = make_builtin($<str>1); }
654 | tSMALL { $$ = make_builtin($<str>1); }
655 | tLONG m_int { $$ = make_builtin($<str>1); }
656 | tHYPER m_int { $$ = make_builtin($<str>1); }
657 | tINT64 { $$ = make_builtin($<str>1); }
658 | tCHAR { $$ = make_builtin($<str>1); }
661 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
662 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
663 if ($$->defined) yyerror("multiple definition error");
664 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
668 coclasshdr: attributes coclass { $$ = $2;
670 if (!parse_only && do_header)
672 if (!parse_only && do_idfile)
677 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
683 coclass_ints: { $$ = NULL; }
684 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
688 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
691 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
692 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
695 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
697 if ($$->defined) yyerror("multiple definition error");
698 attrs = make_attr(ATTR_DISPINTERFACE);
699 $$->attrs = append_attr( $1, attrs );
700 $$->ref = find_type("IDispatch", 0);
701 if (!$$->ref) yyerror("IDispatch is undefined");
703 if (!parse_only && do_header) write_forward($$);
707 dispint_props: tPROPERTIES ':' { $$ = NULL; }
708 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
711 dispint_meths: tMETHODS ':' { $$ = NULL; }
712 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
715 dispinterfacedef: dispinterfacehdr '{'
721 if (!parse_only && do_header) write_dispinterface($$);
722 if (!parse_only && do_idfile) write_diid($$);
725 '{' interface ';' '}' { $$ = $1;
726 $$->fields = $3->fields;
727 $$->funcs = $3->funcs;
728 if (!parse_only && do_header) write_dispinterface($$);
729 if (!parse_only && do_idfile) write_diid($$);
733 inherit: { $$ = NULL; }
734 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
737 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
738 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
741 interfacehdr: attributes interface { $$ = $2;
742 if ($$->defined) yyerror("multiple definition error");
745 if (!parse_only && do_header) write_forward($$);
749 interfacedef: interfacehdr inherit
750 '{' int_statements '}' { $$ = $1;
753 compute_method_indexes($$);
754 if (!parse_only && do_header) write_interface($$);
755 if (!parse_only && do_idfile) write_iid($$);
757 /* MIDL is able to import the definition of a base class from inside the
758 * definition of a derived class, I'll try to support it with this rule */
759 | interfacehdr ':' aIDENTIFIER
760 '{' import int_statements '}' { $$ = $1;
761 $$->ref = find_type2($3, 0);
762 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
764 compute_method_indexes($$);
765 if (!parse_only && do_header) write_interface($$);
766 if (!parse_only && do_idfile) write_iid($$);
768 | dispinterfacedef { $$ = $1; }
772 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
773 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
776 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
777 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
780 modulehdr: attributes module { $$ = $2;
785 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
787 /* FIXME: if (!parse_only && do_header) write_module($$); */
791 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
792 | tCONST p_ident { $$ = $2; /* FIXME */ }
797 | '(' pident ')' { $$ = $2; }
801 pident { $$ = append_var( NULL, $1 ); }
802 | pident_list ',' pident { $$ = append_var( $1, $3 ); }
806 tREF { $$ = RPC_FC_RP; }
807 | tUNIQUE { $$ = RPC_FC_UP; }
808 | tPTR { $$ = RPC_FC_FP; }
811 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
812 /* overwrite RPC_FC_STRUCT with a more exact type */
813 $$->type = get_struct_type( $4 );
814 $$->kind = TKIND_RECORD;
818 add_typelib_entry($$);
822 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
823 | aKNOWNTYPE { $$ = find_type($1, 0); }
824 | base_type { $$ = $1; }
825 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
826 | enumdef { $$ = $1; }
827 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
828 | structdef { $$ = $1; }
829 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
830 | uniondef { $$ = $1; }
831 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
832 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
835 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
836 process_typedefs($4);
840 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
841 $$->kind = TKIND_UNION;
846 tSWITCH '(' s_field ')'
847 m_ident '{' cases '}' { var_t *u = $7;
848 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
849 $$->kind = TKIND_UNION;
850 if (!u) u = make_var( xstrdup("tagged_union") );
851 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
852 u->type->kind = TKIND_UNION;
853 u->type->fields = $9;
854 u->type->defined = TRUE;
855 $$->fields = append_var( $$->fields, $5 );
856 $$->fields = append_var( $$->fields, u );
862 aNUM { $$ = MAKELONG($1, 0); }
863 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
868 static void decl_builtin(const char *name, unsigned char type)
870 type_t *t = make_type(type, NULL);
871 t->name = xstrdup(name);
872 reg_type(t, name, 0);
875 static type_t *make_builtin(char *name)
877 /* NAME is strdup'd in the lexer */
878 type_t *t = duptype(find_type(name, 0), 0);
883 static type_t *make_int(int sign)
885 type_t *t = duptype(find_type("int", 0), 1);
889 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
894 void init_types(void)
896 decl_builtin("void", 0);
897 decl_builtin("byte", RPC_FC_BYTE);
898 decl_builtin("wchar_t", RPC_FC_WCHAR);
899 decl_builtin("int", RPC_FC_LONG); /* win32 */
900 decl_builtin("short", RPC_FC_SHORT);
901 decl_builtin("small", RPC_FC_SMALL);
902 decl_builtin("long", RPC_FC_LONG);
903 decl_builtin("hyper", RPC_FC_HYPER);
904 decl_builtin("__int64", RPC_FC_HYPER);
905 decl_builtin("char", RPC_FC_CHAR);
906 decl_builtin("float", RPC_FC_FLOAT);
907 decl_builtin("double", RPC_FC_DOUBLE);
908 decl_builtin("boolean", RPC_FC_BYTE);
909 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
910 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
913 static str_list_t *append_str(str_list_t *list, char *str)
915 struct str_list_entry_t *entry;
917 if (!str) return list;
920 list = xmalloc( sizeof(*list) );
923 entry = xmalloc( sizeof(*entry) );
925 list_add_tail( list, &entry->entry );
929 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
931 if (!attr) return list;
934 list = xmalloc( sizeof(*list) );
937 list_add_tail( list, &attr->entry );
941 static attr_t *make_attr(enum attr_type type)
943 attr_t *a = xmalloc(sizeof(attr_t));
949 static attr_t *make_attrv(enum attr_type type, unsigned long val)
951 attr_t *a = xmalloc(sizeof(attr_t));
957 static attr_t *make_attrp(enum attr_type type, void *val)
959 attr_t *a = xmalloc(sizeof(attr_t));
965 static expr_t *make_expr(enum expr_type type)
967 expr_t *e = xmalloc(sizeof(expr_t));
975 static expr_t *make_exprl(enum expr_type type, long val)
977 expr_t *e = xmalloc(sizeof(expr_t));
982 /* check for numeric constant */
983 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
984 /* make sure true/false value is valid */
985 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
992 static expr_t *make_exprs(enum expr_type type, char *val)
995 e = xmalloc(sizeof(expr_t));
1000 /* check for predefined constants */
1001 if (type == EXPR_IDENTIFIER) {
1002 var_t *c = find_const(val, 0);
1004 e->u.sval = c->name;
1007 e->cval = c->eval->cval;
1013 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1016 e = xmalloc(sizeof(expr_t));
1020 e->is_const = FALSE;
1021 /* check for cast of constant expression */
1022 if (type == EXPR_SIZEOF) {
1023 switch (tref->type) {
1040 case RPC_FC_ERROR_STATUS_T:
1051 if (type == EXPR_CAST && expr->is_const) {
1053 e->cval = expr->cval;
1058 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1061 e = xmalloc(sizeof(expr_t));
1065 e->is_const = FALSE;
1066 /* check for compile-time optimization */
1067 if (expr->is_const) {
1071 e->cval = -expr->cval;
1074 e->cval = ~expr->cval;
1077 e->is_const = FALSE;
1084 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1087 e = xmalloc(sizeof(expr_t));
1091 e->is_const = FALSE;
1092 /* check for compile-time optimization */
1093 if (expr1->is_const && expr2->is_const) {
1097 e->cval = expr1->cval + expr2->cval;
1100 e->cval = expr1->cval - expr2->cval;
1103 e->cval = expr1->cval * expr2->cval;
1106 e->cval = expr1->cval / expr2->cval;
1109 e->cval = expr1->cval | expr2->cval;
1112 e->cval = expr1->cval & expr2->cval;
1115 e->cval = expr1->cval << expr2->cval;
1118 e->cval = expr1->cval >> expr2->cval;
1121 e->is_const = FALSE;
1128 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1131 e = xmalloc(sizeof(expr_t));
1136 e->is_const = FALSE;
1137 /* check for compile-time optimization */
1138 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1142 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1145 e->is_const = FALSE;
1152 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1154 if (!expr) return list;
1157 list = xmalloc( sizeof(*list) );
1160 list_add_tail( list, &expr->entry );
1164 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1166 if (!expr) return list;
1169 list = xmalloc( sizeof(*list) );
1172 list_add_tail( list, &expr->entry );
1176 static type_t *make_type(unsigned char type, type_t *ref)
1178 type_t *t = xmalloc(sizeof(type_t));
1180 t->kind = TKIND_PRIMITIVE;
1188 t->ignore = parse_only;
1189 t->is_const = FALSE;
1193 t->user_types_registered = FALSE;
1194 t->typelib_idx = -1;
1198 static void set_type(var_t *v, type_t *type, array_dims_t *arr)
1204 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1206 if (!iface) return list;
1209 list = xmalloc( sizeof(*list) );
1212 list_add_tail( list, &iface->entry );
1216 static ifref_t *make_ifref(type_t *iface)
1218 ifref_t *l = xmalloc(sizeof(ifref_t));
1224 static var_list_t *append_var(var_list_t *list, var_t *var)
1226 if (!var) return list;
1229 list = xmalloc( sizeof(*list) );
1232 list_add_tail( list, &var->entry );
1236 static var_t *make_var(char *name)
1238 var_t *v = xmalloc(sizeof(var_t));
1249 static func_list_t *append_func(func_list_t *list, func_t *func)
1251 if (!func) return list;
1254 list = xmalloc( sizeof(*list) );
1257 list_add_tail( list, &func->entry );
1261 static func_t *make_func(var_t *def, var_list_t *args)
1263 func_t *f = xmalloc(sizeof(func_t));
1266 f->ignore = parse_only;
1271 static type_t *make_class(char *name)
1273 type_t *c = make_type(0, NULL);
1275 c->kind = TKIND_COCLASS;
1279 static type_t *make_safearray(type_t *type)
1281 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1283 return make_type(RPC_FC_FP, sa);
1288 static int hash_ident(const char *name)
1290 const char *p = name;
1292 /* a simple sum hash is probably good enough */
1297 return sum & (HASHMAX-1);
1300 /***** type repository *****/
1309 struct rtype *type_hash[HASHMAX];
1311 static type_t *reg_type(type_t *type, const char *name, int t)
1316 yyerror("registering named type without name");
1319 hash = hash_ident(name);
1320 nt = xmalloc(sizeof(struct rtype));
1324 nt->next = type_hash[hash];
1325 type_hash[hash] = nt;
1329 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs)
1334 int is_str = is_attr(attrs, ATTR_STRING);
1335 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1346 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1348 name = LIST_ENTRY( list_head( names ), const var_t, entry );
1349 yyerror("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays",
1354 /* We must generate names for tagless enum, struct or union.
1355 Typedef-ing a tagless enum, struct or union means we want the typedef
1356 to be included in a library whether it has other attributes or not,
1357 hence the public attribute. */
1358 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1359 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1361 if (! is_attr(attrs, ATTR_PUBLIC))
1362 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1363 type->name = gen_name();
1366 LIST_FOR_EACH_ENTRY( name, names, const var_t, entry )
1370 int cptr = name->ptr_level;
1372 while (cptr > ptrc) {
1373 cur = ptr = make_type(RPC_FC_RP, cur);
1377 while (cptr < ptrc) {
1382 cur = alias(cur, name->name);
1387 cur->type = ptr_type;
1389 yyerror("'%s': pointer attribute applied to non-pointer type",
1392 else if (is_str && ! is_ptr(cur))
1393 yyerror("'%s': [string] attribute applied to non-pointer type",
1396 reg_type(cur, cur->name, 0);
1402 static type_t *find_type(const char *name, int t)
1404 struct rtype *cur = type_hash[hash_ident(name)];
1405 while (cur && (cur->t != t || strcmp(cur->name, name)))
1408 yyerror("type '%s' not found", name);
1414 static type_t *find_type2(char *name, int t)
1416 type_t *tp = find_type(name, t);
1421 int is_type(const char *name)
1423 struct rtype *cur = type_hash[hash_ident(name)];
1424 while (cur && (cur->t || strcmp(cur->name, name)))
1426 if (cur) return TRUE;
1430 static type_t *get_type(unsigned char type, char *name, int t)
1432 struct rtype *cur = NULL;
1435 cur = type_hash[hash_ident(name)];
1436 while (cur && (cur->t != t || strcmp(cur->name, name)))
1443 tp = make_type(type, NULL);
1445 if (!name) return tp;
1446 return reg_type(tp, name, t);
1449 static type_t *get_typev(unsigned char type, var_t *name, int t)
1456 return get_type(type, sname, t);
1459 static int get_struct_type(var_list_t *fields)
1461 int has_pointer = 0;
1462 int has_conformance = 0;
1463 int has_variance = 0;
1466 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1468 type_t *t = field->type;
1470 if (field->ptr_level > 0)
1476 if (is_string_type(field->attrs, 0, field->array))
1478 has_conformance = 1;
1483 if (is_array_type(field->attrs, 0, field->array))
1485 if (field->array && is_conformant_array(field->array))
1487 has_conformance = 1;
1488 if (list_next( fields, &field->entry ))
1489 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1492 if (is_attr(field->attrs, ATTR_LENGTHIS))
1499 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1500 * Simple types don't effect the type of struct.
1501 * A struct containing a simple struct is still a simple struct.
1502 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1504 case 0: /* void pointer */
1514 case RPC_FC_INT3264:
1515 case RPC_FC_UINT3264:
1531 has_conformance = 1;
1532 if (list_next( fields, &field->entry ))
1533 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1538 * Propagate member attributes
1539 * a struct should be at least as complex as its member
1541 case RPC_FC_CVSTRUCT:
1542 has_conformance = 1;
1547 case RPC_FC_CPSTRUCT:
1548 has_conformance = 1;
1549 if (list_next( fields, &field->entry ))
1550 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1555 case RPC_FC_CSTRUCT:
1556 has_conformance = 1;
1557 if (list_next( fields, &field->entry ))
1558 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1562 case RPC_FC_PSTRUCT:
1567 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1568 field->name, t->type);
1569 /* fallthru - treat it as complex */
1571 /* as soon as we see one of these these members, it's bogus... */
1573 case RPC_FC_ENCAPSULATED_UNION:
1574 case RPC_FC_NON_ENCAPSULATED_UNION:
1575 case RPC_FC_TRANSMIT_AS:
1576 case RPC_FC_REPRESENT_AS:
1578 case RPC_FC_EMBEDDED_COMPLEX:
1579 case RPC_FC_BOGUS_STRUCT:
1580 return RPC_FC_BOGUS_STRUCT;
1585 return RPC_FC_CVSTRUCT;
1586 if( has_conformance && has_pointer )
1587 return RPC_FC_CPSTRUCT;
1588 if( has_conformance )
1589 return RPC_FC_CSTRUCT;
1591 return RPC_FC_PSTRUCT;
1592 return RPC_FC_STRUCT;
1595 /***** constant repository *****/
1600 struct rconst *next;
1603 struct rconst *const_hash[HASHMAX];
1605 static var_t *reg_const(var_t *var)
1610 yyerror("registering constant without name");
1613 hash = hash_ident(var->name);
1614 nc = xmalloc(sizeof(struct rconst));
1615 nc->name = var->name;
1617 nc->next = const_hash[hash];
1618 const_hash[hash] = nc;
1622 static var_t *find_const(char *name, int f)
1624 struct rconst *cur = const_hash[hash_ident(name)];
1625 while (cur && strcmp(cur->name, name))
1628 if (f) yyerror("constant '%s' not found", name);
1634 static void write_libid(const char *name, const attr_list_t *attr)
1636 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1637 write_guid(idfile, "LIBID", name, uuid);
1640 static void write_clsid(type_t *cls)
1642 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1643 write_guid(idfile, "CLSID", cls->name, uuid);
1646 static void write_diid(type_t *iface)
1648 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1649 write_guid(idfile, "DIID", iface->name, uuid);
1652 static void write_iid(type_t *iface)
1654 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1655 write_guid(idfile, "IID", iface->name, uuid);
1658 static int compute_method_indexes(type_t *iface)
1664 idx = compute_method_indexes(iface->ref);
1671 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
1672 if (! is_callas(f->def->attrs))
1678 static char *gen_name(void)
1680 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1681 static unsigned long n = 0;
1682 static const char *file_id;
1688 char *dst = dup_basename(input_name, ".idl");
1692 if (! isalnum((unsigned char) *dst))
1695 size = sizeof format - 7 + strlen(file_id) + 8;
1698 name = xmalloc(size);
1699 sprintf(name, format, file_id, n++);
1703 static void process_typedefs(var_list_t *names)
1708 LIST_FOR_EACH_ENTRY_SAFE( name, next, names, var_t, entry )
1710 type_t *type = find_type(name->name, 0);
1712 if (! parse_only && do_header)
1713 write_typedef(type);
1714 if (in_typelib && type->attrs)
1715 add_typelib_entry(type);
1721 static void check_arg(var_t *arg)
1723 type_t *t = arg->type;
1725 if (t->type == 0 && ! is_var_ptr(arg))
1726 yyerror("argument '%s' has void type", arg->name);