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
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
48 # define YYLEX yylex()
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
66 #define YYERROR_VERBOSE
68 unsigned char pointer_default = RPC_FC_UP;
69 static int is_object_interface = FALSE;
71 typedef struct list typelist_t;
77 typelist_t incomplete_types = LIST_INIT(incomplete_types);
79 static void add_incomplete(type_t *t);
80 static void fix_incomplete(void);
82 static str_list_t *append_str(str_list_t *list, char *str);
83 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
84 static attr_t *make_attr(enum attr_type type);
85 static attr_t *make_attrv(enum attr_type type, unsigned long val);
86 static attr_t *make_attrp(enum attr_type type, void *val);
87 static expr_t *make_expr(enum expr_type type);
88 static expr_t *make_exprl(enum expr_type type, long val);
89 static expr_t *make_exprd(enum expr_type type, double val);
90 static expr_t *make_exprs(enum expr_type type, char *val);
91 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
92 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
93 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
94 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
95 static type_t *make_type(unsigned char type, type_t *ref);
96 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
97 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
98 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr, int top);
99 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
100 static ifref_t *make_ifref(type_t *iface);
101 static var_list_t *append_var(var_list_t *list, var_t *var);
102 static var_t *make_var(char *name);
103 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
104 static pident_t *make_pident(var_t *var);
105 static func_list_t *append_func(func_list_t *list, func_t *func);
106 static func_t *make_func(var_t *def, var_list_t *args);
107 static type_t *make_class(char *name);
108 static type_t *make_safearray(type_t *type);
109 static type_t *make_builtin(char *name);
110 static type_t *make_int(int sign);
112 static type_t *reg_type(type_t *type, const char *name, int t);
113 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
114 static type_t *find_type(const char *name, int t);
115 static type_t *find_type2(char *name, int t);
116 static type_t *get_type(unsigned char type, char *name, int t);
117 static type_t *get_typev(unsigned char type, var_t *name, int t);
118 static int get_struct_type(var_list_t *fields);
120 static var_t *reg_const(var_t *var);
121 static var_t *find_const(char *name, int f);
123 static void write_libid(const char *name, const attr_list_t *attr);
124 static void write_clsid(type_t *cls);
125 static void write_diid(type_t *iface);
126 static void write_iid(type_t *iface);
128 static int compute_method_indexes(type_t *iface);
129 static char *gen_name(void);
130 static void process_typedefs(var_list_t *names);
131 static void check_arg(var_t *arg);
132 static void check_all_user_types(ifref_list_t *ifaces);
141 attr_list_t *attr_list;
142 str_list_t *str_list;
144 expr_list_t *expr_list;
145 array_dims_t *array_dims;
148 var_list_t *var_list;
150 pident_list_t *pident_list;
152 func_list_t *func_list;
154 ifref_list_t *ifref_list;
159 interface_info_t ifinfo;
162 %token <str> aIDENTIFIER
163 %token <str> aKNOWNTYPE
164 %token <num> aNUM aHEXNUM
170 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
171 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
172 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
173 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
174 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
176 %token tDEFAULTCOLLELEM
178 %token tDEFAULTVTABLE
180 %token tDISPINTERFACE
181 %token tDLLNAME tDOUBLE tDUAL
183 %token tENTRY tENUM tERRORSTATUST
184 %token tEXPLICITHANDLE tEXTERN
190 %token tHELPCONTEXT tHELPFILE
191 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
193 %token tHYPER tID tIDEMPOTENT
195 %token tIMMEDIATEBIND
196 %token tIMPLICITHANDLE
197 %token tIMPORT tIMPORTLIB
203 %token tLENGTHIS tLIBRARY
210 %token tNONEXTENSIBLE
211 %token tOBJECT tODL tOLEAUTOMATION
215 %token tPOINTERDEFAULT
217 %token tPROPGET tPROPPUT tPROPPUTREF
221 %token tREADONLY tREF
229 %token tSIZEIS tSIZEOF
233 %token tSTRICTCONTEXTHANDLE
234 %token tSTRING tSTRUCT
235 %token tSWITCH tSWITCHIS tSWITCHTYPE
247 %token tWCHAR tWIREMARSHAL
249 %type <attr> attribute
250 %type <attr_list> m_attributes attributes attrib_list
251 %type <str_list> str_list
252 %type <expr> m_expr expr expr_const
253 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
254 %type <array_dims> array array_list
255 %type <ifinfo> interfacehdr
256 %type <type> inherit interface interfacedef interfacedec
257 %type <type> dispinterface dispinterfacehdr dispinterfacedef
258 %type <type> module modulehdr moduledef
259 %type <type> base_type int_std
260 %type <type> enumdef structdef uniondef
262 %type <ifref> coclass_int
263 %type <ifref_list> gbl_statements coclass_ints
264 %type <var> arg field s_field case enum constdef externdef
265 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
266 %type <var> m_ident t_ident ident
267 %type <pident> pident func_ident direct_ident
268 %type <pident_list> pident_list
270 %type <func_list> int_statements dispint_meths
271 %type <type> coclass coclasshdr coclassdef
272 %type <num> pointer_type version
273 %type <str> libraryhdr callconv
274 %type <uuid> uuid_string
275 %type <num> import_start
292 input: gbl_statements { fix_incomplete();
293 check_all_user_types($1);
301 gbl_statements: { $$ = NULL; }
302 | gbl_statements interfacedec { $$ = $1; }
303 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
304 | gbl_statements coclass ';' { $$ = $1;
305 reg_type($2, $2->name, 0);
306 if (!parse_only && do_header) write_coclass_forward($2);
308 | gbl_statements coclassdef { $$ = $1;
309 add_typelib_entry($2);
310 reg_type($2, $2->name, 0);
311 if (!parse_only && do_header) write_coclass_forward($2);
313 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
314 | gbl_statements librarydef { $$ = $1; }
315 | gbl_statements statement { $$ = $1; }
319 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
320 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
321 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
322 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
323 reg_type($2, $2->name, 0);
324 if (!parse_only && do_header) write_coclass_forward($2);
326 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
327 | imp_statements statement {}
328 | imp_statements importlib {}
329 | imp_statements librarydef {}
332 int_statements: { $$ = NULL; }
333 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
334 | int_statements statement { $$ = $1; }
341 statement: constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
343 | enumdef ';' { if (!parse_only && do_header) {
344 write_type_def_or_decl(header, $1, FALSE, NULL);
345 fprintf(header, ";\n\n");
348 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
350 | structdef ';' { if (!parse_only && do_header) {
351 write_type_def_or_decl(header, $1, FALSE, NULL);
352 fprintf(header, ";\n\n");
356 | uniondef ';' { if (!parse_only && do_header) {
357 write_type_def_or_decl(header, $1, FALSE, NULL);
358 fprintf(header, ";\n\n");
363 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
365 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
367 if (!$$) yychar = aEOF;
371 import: import_start imp_statements aEOF
372 { if ($1) pop_import(); }
375 importlib: tIMPORTLIB '(' aSTRING ')'
376 semicolon_opt { if(!parse_only) add_importlib($3); }
379 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
381 library_start: attributes libraryhdr '{' { if (!parse_only) start_typelib($2, $1);
382 if (!parse_only && do_header) write_library($2, $1);
383 if (!parse_only && do_idfile) write_libid($2, $1);
386 librarydef: library_start imp_statements '}'
387 semicolon_opt { if (!parse_only) end_typelib(); }
390 m_args: { $$ = NULL; }
394 no_args: tVOID { $$ = NULL; }
397 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
398 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
402 /* split into two rules to get bison to resolve a tVOID conflict */
403 arg: attributes type pident array { $$ = $3->var;
405 set_type($$, $2, $3, $4, TRUE);
408 | type pident array { $$ = $2->var;
409 set_type($$, $1, $2, $3, TRUE);
414 array: { $$ = NULL; }
415 | '[' array_list ']' { $$ = $2; }
416 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
419 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
420 | array_list ',' expr { $$ = append_array( $1, $3 ); }
421 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
424 m_attributes: { $$ = NULL; }
429 '[' attrib_list ']' { $$ = $2;
431 error_loc("empty attribute lists unsupported\n");
435 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
436 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
437 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
440 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
441 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
444 attribute: { $$ = NULL; }
445 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
446 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
447 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
448 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
449 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
450 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
451 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
452 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
453 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
454 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
455 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
456 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
457 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
458 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
459 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
460 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
461 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
462 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
463 | tDUAL { $$ = make_attr(ATTR_DUAL); }
464 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
465 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
466 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
467 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
468 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
469 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
470 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
471 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
472 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
473 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
474 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
475 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
476 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
477 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
478 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
479 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
480 | tIN { $$ = make_attr(ATTR_IN); }
481 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
482 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
483 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
484 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
485 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
486 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
487 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
488 | tODL { $$ = make_attr(ATTR_ODL); }
489 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
490 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
491 | tOUT { $$ = make_attr(ATTR_OUT); }
492 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
493 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
494 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
495 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
496 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
497 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
498 list = append_expr( list, $5 );
499 $$ = make_attrp(ATTR_RANGE, list); }
500 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
501 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
502 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
503 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
504 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
505 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
506 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
507 | tSTRING { $$ = make_attr(ATTR_STRING); }
508 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
509 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
510 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
511 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
512 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
513 | tVARARG { $$ = make_attr(ATTR_VARARG); }
514 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
515 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
516 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
521 | aSTRING { if (!is_valid_uuid($1))
522 error_loc("invalid UUID: %s\n", $1);
523 $$ = parse_uuid($1); }
526 callconv: tCDECL { $$ = $<str>1; }
527 | tFASTCALL { $$ = $<str>1; }
528 | tPASCAL { $$ = $<str>1; }
529 | tSTDCALL { $$ = $<str>1; }
532 cases: { $$ = NULL; }
533 | cases case { $$ = append_var( $1, $2 ); }
536 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
537 $$ = $4; if (!$$) $$ = make_var(NULL);
538 $$->attrs = append_attr( $$->attrs, a );
540 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
541 $$ = $3; if (!$$) $$ = make_var(NULL);
542 $$->attrs = append_attr( $$->attrs, a );
546 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
547 set_type($$, $2, NULL, NULL, FALSE);
552 enums: { $$ = NULL; }
553 | enum_list ',' { $$ = $1; }
557 enum_list: enum { if (!$1->eval)
558 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
559 $$ = append_var( NULL, $1 );
561 | enum_list ',' enum { if (!$3->eval)
563 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
564 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
566 $$ = append_var( $1, $3 );
570 enum: ident '=' expr_const { $$ = reg_const($1);
572 $$->type = make_int(0);
574 | ident { $$ = reg_const($1);
575 $$->type = make_int(0);
579 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
580 $$->kind = TKIND_ENUM;
581 $$->fields_or_args = $4;
584 add_typelib_entry($$);
588 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
589 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
593 exprs: { $$ = make_expr(EXPR_VOID); }
598 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
602 m_expr: { $$ = make_expr(EXPR_VOID); }
606 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
607 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
608 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
609 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
610 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
611 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
612 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
613 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
614 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
615 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
616 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
617 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
618 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
619 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
620 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
621 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
622 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
623 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
624 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
625 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
626 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
627 | '(' expr ')' { $$ = $2; }
630 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
631 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
634 expr_const: expr { $$ = $1;
636 error_loc("expression is not constant\n");
640 externdef: tEXTERN tCONST type ident { $$ = $4;
641 set_type($$, $3, NULL, NULL, FALSE);
645 fields: { $$ = NULL; }
646 | fields field { $$ = append_var( $1, $2 ); }
649 field: s_field ';' { $$ = $1; }
650 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
651 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
655 s_field: m_attributes type pident array { $$ = $3->var;
657 set_type($$, $2, $3, $4, FALSE);
663 m_attributes type pident { var_t *v = $3->var;
664 var_list_t *args = $3->args;
666 set_type(v, $2, $3, NULL, FALSE);
668 $$ = make_func(v, args);
669 if (is_attr(v->attrs, ATTR_IN)) {
670 error_loc("inapplicable attribute [in] for function '%s'\n",$$->def->name);
675 m_ident: { $$ = NULL; }
679 t_ident: { $$ = NULL; }
680 | aIDENTIFIER { $$ = make_var($1); }
681 | aKNOWNTYPE { $$ = make_var($1); }
684 ident: aIDENTIFIER { $$ = make_var($1); }
685 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
686 | aKNOWNTYPE { $$ = make_var($<str>1); }
689 base_type: tBYTE { $$ = make_builtin($<str>1); }
690 | tWCHAR { $$ = make_builtin($<str>1); }
692 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
693 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
695 case RPC_FC_CHAR: break;
696 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
697 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
698 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
700 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
702 $$ = alias($$, "MIDL_uhyper");
709 | tUNSIGNED { $$ = make_int(-1); }
710 | tFLOAT { $$ = make_builtin($<str>1); }
711 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
712 | tDOUBLE { $$ = make_builtin($<str>1); }
713 | tBOOLEAN { $$ = make_builtin($<str>1); }
714 | tERRORSTATUST { $$ = make_builtin($<str>1); }
715 | tHANDLET { $$ = make_builtin($<str>1); }
722 int_std: tINT { $$ = make_builtin($<str>1); }
723 | tSHORT m_int { $$ = make_builtin($<str>1); }
724 | tSMALL { $$ = make_builtin($<str>1); }
725 | tLONG m_int { $$ = make_builtin($<str>1); }
726 | tHYPER m_int { $$ = make_builtin($<str>1); }
727 | tINT64 { $$ = make_builtin($<str>1); }
728 | tCHAR { $$ = make_builtin($<str>1); }
731 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
732 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
733 if ($$->defined) error_loc("multiple definition error\n");
734 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
738 coclasshdr: attributes coclass { $$ = $2;
740 if (!parse_only && do_header)
742 if (!parse_only && do_idfile)
747 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
754 coclass_ints: { $$ = NULL; }
755 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
759 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
762 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
763 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
766 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
767 is_object_interface = TRUE;
769 if ($$->defined) error_loc("multiple definition error\n");
770 attrs = make_attr(ATTR_DISPINTERFACE);
771 $$->attrs = append_attr( $1, attrs );
772 $$->ref = find_type("IDispatch", 0);
773 if (!$$->ref) error_loc("IDispatch is undefined\n");
775 if (!parse_only && do_header) write_forward($$);
779 dispint_props: tPROPERTIES ':' { $$ = NULL; }
780 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
783 dispint_meths: tMETHODS ':' { $$ = NULL; }
784 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
787 dispinterfacedef: dispinterfacehdr '{'
791 $$->fields_or_args = $3;
793 if (!parse_only && do_header) write_dispinterface($$);
794 if (!parse_only && do_idfile) write_diid($$);
797 '{' interface ';' '}' { $$ = $1;
798 $$->fields_or_args = $3->fields_or_args;
799 $$->funcs = $3->funcs;
800 if (!parse_only && do_header) write_dispinterface($$);
801 if (!parse_only && do_idfile) write_diid($$);
805 inherit: { $$ = NULL; }
806 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
809 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
810 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
813 interfacehdr: attributes interface { $$.interface = $2;
814 $$.old_pointer_default = pointer_default;
815 if (is_attr($1, ATTR_POINTERDEFAULT))
816 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
817 is_object_interface = is_object($1);
818 if ($2->defined) error_loc("multiple definition error\n");
821 if (!parse_only && do_header) write_forward($2);
825 interfacedef: interfacehdr inherit
826 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
829 compute_method_indexes($$);
830 if (!parse_only && do_header) write_interface($$);
831 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
832 if (!parse_only && do_idfile) write_iid($$);
833 pointer_default = $1.old_pointer_default;
835 /* MIDL is able to import the definition of a base class from inside the
836 * definition of a derived class, I'll try to support it with this rule */
837 | interfacehdr ':' aIDENTIFIER
838 '{' import int_statements '}'
839 semicolon_opt { $$ = $1.interface;
840 $$->ref = find_type2($3, 0);
841 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
843 compute_method_indexes($$);
844 if (!parse_only && do_header) write_interface($$);
845 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
846 if (!parse_only && do_idfile) write_iid($$);
847 pointer_default = $1.old_pointer_default;
849 | dispinterfacedef semicolon_opt { $$ = $1; }
853 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
854 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
857 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
858 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
861 modulehdr: attributes module { $$ = $2;
866 moduledef: modulehdr '{' int_statements '}'
867 semicolon_opt { $$ = $1;
869 /* FIXME: if (!parse_only && do_header) write_module($$); */
873 pident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
874 | tCONST pident { $$ = $2; /* FIXME */ }
875 | callconv pident { $$ = $2;
876 if ($$->callconv) parser_warning("multiple calling conventions %s, %s for function %s\n", $$->callconv, $1, $$->var->name);
882 func_ident: direct_ident '(' m_args ')'
889 direct_ident: ident { $$ = make_pident($1); }
890 | '(' pident ')' { $$ = $2; }
891 | func_ident { $$ = $1;
892 $$->func_ptr_level = $$->ptr_level;
898 pident { $$ = append_pident( NULL, $1 ); }
899 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
903 tREF { $$ = RPC_FC_RP; }
904 | tUNIQUE { $$ = RPC_FC_UP; }
905 | tPTR { $$ = RPC_FC_FP; }
908 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
909 /* overwrite RPC_FC_STRUCT with a more exact type */
910 $$->type = get_struct_type( $4 );
911 $$->kind = TKIND_RECORD;
912 $$->fields_or_args = $4;
915 add_typelib_entry($$);
919 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
920 | aKNOWNTYPE { $$ = find_type($1, 0); }
921 | base_type { $$ = $1; }
922 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
923 | enumdef { $$ = $1; }
924 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
925 | structdef { $$ = $1; }
926 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
927 | uniondef { $$ = $1; }
928 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
929 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
932 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
933 process_typedefs($4);
937 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
938 $$->kind = TKIND_UNION;
939 $$->fields_or_args = $4;
943 tSWITCH '(' s_field ')'
944 m_ident '{' cases '}' { var_t *u = $7;
945 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
946 $$->kind = TKIND_UNION;
947 if (!u) u = make_var( xstrdup("tagged_union") );
948 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
949 u->type->kind = TKIND_UNION;
950 u->type->fields_or_args = $9;
951 u->type->defined = TRUE;
952 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
953 $$->fields_or_args = append_var( $$->fields_or_args, u );
959 aNUM { $$ = MAKEVERSION($1, 0); }
960 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
965 static void decl_builtin(const char *name, unsigned char type)
967 type_t *t = make_type(type, NULL);
968 t->name = xstrdup(name);
969 reg_type(t, name, 0);
972 static type_t *make_builtin(char *name)
974 /* NAME is strdup'd in the lexer */
975 type_t *t = duptype(find_type(name, 0), 0);
980 static type_t *make_int(int sign)
982 type_t *t = duptype(find_type("int", 0), 1);
986 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
991 void init_types(void)
993 decl_builtin("void", 0);
994 decl_builtin("byte", RPC_FC_BYTE);
995 decl_builtin("wchar_t", RPC_FC_WCHAR);
996 decl_builtin("int", RPC_FC_LONG); /* win32 */
997 decl_builtin("short", RPC_FC_SHORT);
998 decl_builtin("small", RPC_FC_SMALL);
999 decl_builtin("long", RPC_FC_LONG);
1000 decl_builtin("hyper", RPC_FC_HYPER);
1001 decl_builtin("__int64", RPC_FC_HYPER);
1002 decl_builtin("char", RPC_FC_CHAR);
1003 decl_builtin("float", RPC_FC_FLOAT);
1004 decl_builtin("double", RPC_FC_DOUBLE);
1005 decl_builtin("boolean", RPC_FC_BYTE);
1006 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1007 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1010 static str_list_t *append_str(str_list_t *list, char *str)
1012 struct str_list_entry_t *entry;
1014 if (!str) return list;
1017 list = xmalloc( sizeof(*list) );
1020 entry = xmalloc( sizeof(*entry) );
1022 list_add_tail( list, &entry->entry );
1026 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1028 if (!attr) return list;
1031 list = xmalloc( sizeof(*list) );
1034 list_add_tail( list, &attr->entry );
1038 static attr_t *make_attr(enum attr_type type)
1040 attr_t *a = xmalloc(sizeof(attr_t));
1046 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1048 attr_t *a = xmalloc(sizeof(attr_t));
1054 static attr_t *make_attrp(enum attr_type type, void *val)
1056 attr_t *a = xmalloc(sizeof(attr_t));
1062 static expr_t *make_expr(enum expr_type type)
1064 expr_t *e = xmalloc(sizeof(expr_t));
1068 e->is_const = FALSE;
1073 static expr_t *make_exprl(enum expr_type type, long val)
1075 expr_t *e = xmalloc(sizeof(expr_t));
1079 e->is_const = FALSE;
1080 /* check for numeric constant */
1081 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1082 /* make sure true/false value is valid */
1083 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1090 static expr_t *make_exprd(enum expr_type type, double val)
1092 expr_t *e = xmalloc(sizeof(expr_t));
1101 static expr_t *make_exprs(enum expr_type type, char *val)
1104 e = xmalloc(sizeof(expr_t));
1108 e->is_const = FALSE;
1109 /* check for predefined constants */
1110 if (type == EXPR_IDENTIFIER) {
1111 var_t *c = find_const(val, 0);
1113 e->u.sval = c->name;
1116 e->cval = c->eval->cval;
1122 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1125 e = xmalloc(sizeof(expr_t));
1129 e->is_const = FALSE;
1130 /* check for cast of constant expression */
1131 if (type == EXPR_SIZEOF) {
1132 switch (tref->type) {
1149 case RPC_FC_ERROR_STATUS_T:
1160 if (type == EXPR_CAST && expr->is_const) {
1162 e->cval = expr->cval;
1167 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1170 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1171 error("address-of operator applied to invalid expression\n");
1172 e = xmalloc(sizeof(expr_t));
1176 e->is_const = FALSE;
1177 /* check for compile-time optimization */
1178 if (expr->is_const) {
1182 e->cval = -expr->cval;
1185 e->cval = ~expr->cval;
1188 e->is_const = FALSE;
1195 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1198 e = xmalloc(sizeof(expr_t));
1202 e->is_const = FALSE;
1203 /* check for compile-time optimization */
1204 if (expr1->is_const && expr2->is_const) {
1208 e->cval = expr1->cval + expr2->cval;
1211 e->cval = expr1->cval - expr2->cval;
1214 e->cval = expr1->cval * expr2->cval;
1217 e->cval = expr1->cval / expr2->cval;
1220 e->cval = expr1->cval | expr2->cval;
1223 e->cval = expr1->cval & expr2->cval;
1226 e->cval = expr1->cval << expr2->cval;
1229 e->cval = expr1->cval >> expr2->cval;
1232 e->is_const = FALSE;
1239 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1242 e = xmalloc(sizeof(expr_t));
1247 e->is_const = FALSE;
1248 /* check for compile-time optimization */
1249 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1253 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1256 e->is_const = FALSE;
1263 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1265 if (!expr) return list;
1268 list = xmalloc( sizeof(*list) );
1271 list_add_tail( list, &expr->entry );
1275 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1277 if (!expr) return list;
1280 list = xmalloc( sizeof(*list) );
1283 list_add_tail( list, &expr->entry );
1287 static struct list type_pool = LIST_INIT(type_pool);
1294 type_t *alloc_type(void)
1296 type_pool_node_t *node = xmalloc(sizeof *node);
1297 list_add_tail(&type_pool, &node->link);
1301 void set_all_tfswrite(int val)
1303 type_pool_node_t *node;
1304 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1305 node->data.tfswrite = val;
1308 static type_t *make_type(unsigned char type, type_t *ref)
1310 type_t *t = alloc_type();
1312 t->kind = TKIND_PRIMITIVE;
1318 t->fields_or_args = NULL;
1322 t->length_is = NULL;
1323 t->typestring_offset = 0;
1325 t->declarray = FALSE;
1326 t->ignore = (parse_only != 0);
1327 t->is_const = FALSE;
1331 t->user_types_registered = FALSE;
1332 t->tfswrite = FALSE;
1333 t->typelib_idx = -1;
1337 static void set_type(var_t *v, type_t *type, const pident_t *pident, array_dims_t *arr,
1340 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1341 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1342 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1343 int ptr_type = ptr_attr;
1344 int sizeless, has_varconf;
1346 type_t *atype, **ptype;
1347 int ptr_level = (pident ? pident->ptr_level : 0);
1351 if (!ptr_type && top)
1352 ptr_type = RPC_FC_RP;
1354 for ( ; 0 < ptr_level; --ptr_level)
1356 v->type = make_type(pointer_default, v->type);
1357 if (ptr_level == 1 && ptr_type && !arr)
1359 v->type->type = ptr_type;
1364 if (ptr_type && !arr)
1366 if (is_ptr(v->type))
1368 if (v->type->type != ptr_type)
1370 v->type = duptype(v->type, 1);
1371 v->type->type = ptr_type;
1374 else if (!arr && ptr_attr)
1375 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1378 if (pident && pident->is_func) {
1379 int func_ptr_level = pident->func_ptr_level;
1380 v->type = make_type(RPC_FC_FUNCTION, v->type);
1381 v->type->fields_or_args = pident->args;
1382 if (pident->callconv)
1383 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1384 else if (is_object_interface) {
1385 static char *stdmethodcalltype;
1386 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1387 v->type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1389 for (; func_ptr_level > 0; func_ptr_level--)
1390 v->type = make_type(ptr_type, v->type);
1394 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1397 error("%s: only the first array dimension can be unspecified\n", v->name);
1401 unsigned int align = 0;
1402 size_t size = type_memsize(v->type, &align);
1405 error("%s: array dimension must be positive\n", v->name);
1407 if (0xffffffffuL / size < (unsigned long) dim->cval)
1408 error("%s: total array size is too large\n", v->name);
1409 else if (0xffffuL < size * dim->cval)
1410 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1412 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1417 v->type = make_type(RPC_FC_CARRAY, v->type);
1420 v->type->declarray = TRUE;
1421 v->type->dim = dim->cval;
1425 has_varconf = FALSE;
1426 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1428 if (dim->type != EXPR_VOID)
1431 atype = *ptype = duptype(*ptype, 0);
1433 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1434 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1436 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1437 error("%s: size_is attribute applied to illegal type\n", v->name);
1439 atype->type = RPC_FC_CARRAY;
1440 atype->size_is = dim;
1443 ptype = &(*ptype)->ref;
1445 error("%s: too many expressions in size_is attribute\n", v->name);
1449 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1451 if (dim->type != EXPR_VOID)
1454 atype = *ptype = duptype(*ptype, 0);
1456 if (atype->type == RPC_FC_SMFARRAY)
1457 atype->type = RPC_FC_SMVARRAY;
1458 else if (atype->type == RPC_FC_LGFARRAY)
1459 atype->type = RPC_FC_LGVARRAY;
1460 else if (atype->type == RPC_FC_CARRAY)
1461 atype->type = RPC_FC_CVARRAY;
1463 error("%s: length_is attribute applied to illegal type\n", v->name);
1465 atype->length_is = dim;
1468 ptype = &(*ptype)->ref;
1470 error("%s: too many expressions in length_is attribute\n", v->name);
1473 if (has_varconf && !last_array(v->type))
1476 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1478 *ptype = duptype(*ptype, 0);
1479 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1483 if (is_array(v->type))
1485 const type_t *rt = v->type->ref;
1486 if (is_user_type(rt))
1487 v->type->type = RPC_FC_BOGUS_ARRAY;
1491 case RPC_FC_BOGUS_STRUCT:
1492 case RPC_FC_NON_ENCAPSULATED_UNION:
1493 case RPC_FC_ENCAPSULATED_UNION:
1495 v->type->type = RPC_FC_BOGUS_ARRAY;
1497 /* FC_RP should be above, but widl overuses these, and will break things. */
1500 if (rt->ref->type == RPC_FC_IP)
1501 v->type->type = RPC_FC_BOGUS_ARRAY;
1507 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1509 if (!iface) return list;
1512 list = xmalloc( sizeof(*list) );
1515 list_add_tail( list, &iface->entry );
1519 static ifref_t *make_ifref(type_t *iface)
1521 ifref_t *l = xmalloc(sizeof(ifref_t));
1527 static var_list_t *append_var(var_list_t *list, var_t *var)
1529 if (!var) return list;
1532 list = xmalloc( sizeof(*list) );
1535 list_add_tail( list, &var->entry );
1539 static var_t *make_var(char *name)
1541 var_t *v = xmalloc(sizeof(var_t));
1549 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1551 if (!p) return list;
1553 list = xmalloc(sizeof(*list));
1556 list_add_tail(list, &p->entry);
1560 static pident_t *make_pident(var_t *var)
1562 pident_t *p = xmalloc(sizeof(*p));
1566 p->func_ptr_level = 0;
1572 static func_list_t *append_func(func_list_t *list, func_t *func)
1574 if (!func) return list;
1577 list = xmalloc( sizeof(*list) );
1580 list_add_tail( list, &func->entry );
1584 static func_t *make_func(var_t *def, var_list_t *args)
1586 func_t *f = xmalloc(sizeof(func_t));
1589 f->ignore = parse_only;
1594 static type_t *make_class(char *name)
1596 type_t *c = make_type(0, NULL);
1598 c->kind = TKIND_COCLASS;
1602 static type_t *make_safearray(type_t *type)
1604 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1606 return make_type(pointer_default, sa);
1611 static int hash_ident(const char *name)
1613 const char *p = name;
1615 /* a simple sum hash is probably good enough */
1620 return sum & (HASHMAX-1);
1623 /***** type repository *****/
1632 struct rtype *type_hash[HASHMAX];
1634 static type_t *reg_type(type_t *type, const char *name, int t)
1639 error_loc("registering named type without name\n");
1642 hash = hash_ident(name);
1643 nt = xmalloc(sizeof(struct rtype));
1647 nt->next = type_hash[hash];
1648 type_hash[hash] = nt;
1652 static int is_incomplete(const type_t *t)
1654 return !t->defined && (is_struct(t->type) || is_union(t->type));
1657 static void add_incomplete(type_t *t)
1659 struct typenode *tn = xmalloc(sizeof *tn);
1661 list_add_tail(&incomplete_types, &tn->entry);
1664 static void fix_type(type_t *t)
1666 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1667 type_t *ot = t->orig;
1669 t->fields_or_args = ot->fields_or_args;
1670 t->defined = ot->defined;
1674 static void fix_incomplete(void)
1676 struct typenode *tn, *next;
1678 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1684 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1687 const pident_t *pident;
1689 int is_str = is_attr(attrs, ATTR_STRING);
1690 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1701 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1703 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1704 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1709 /* We must generate names for tagless enum, struct or union.
1710 Typedef-ing a tagless enum, struct or union means we want the typedef
1711 to be included in a library whether it has other attributes or not,
1712 hence the public attribute. */
1713 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1714 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1716 if (! is_attr(attrs, ATTR_PUBLIC))
1717 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1718 type->name = gen_name();
1721 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1723 var_t *name = pident->var;
1727 int cptr = pident->ptr_level;
1729 while (cptr > ptrc) {
1730 cur = ptr = make_type(pointer_default, cur);
1734 while (cptr < ptrc) {
1739 if (pident->is_func) {
1740 int func_ptr_level = pident->func_ptr_level;
1741 cur = make_type(RPC_FC_FUNCTION, cur);
1742 cur->fields_or_args = pident->args;
1743 if (pident->callconv)
1744 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, pident->callconv));
1745 else if (is_object_interface) {
1746 static char *stdmethodcalltype;
1747 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1748 cur->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1750 for (; func_ptr_level > 0; func_ptr_level--)
1751 cur = make_type(pointer_default, cur);
1753 cur = alias(cur, name->name);
1758 cur->type = ptr_type;
1760 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1763 else if (is_str && ! is_ptr(cur))
1764 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1767 if (is_incomplete(cur))
1768 add_incomplete(cur);
1769 reg_type(cur, cur->name, 0);
1775 static type_t *find_type(const char *name, int t)
1777 struct rtype *cur = type_hash[hash_ident(name)];
1778 while (cur && (cur->t != t || strcmp(cur->name, name)))
1781 error_loc("type '%s' not found\n", name);
1787 static type_t *find_type2(char *name, int t)
1789 type_t *tp = find_type(name, t);
1794 int is_type(const char *name)
1796 struct rtype *cur = type_hash[hash_ident(name)];
1797 while (cur && (cur->t || strcmp(cur->name, name)))
1799 if (cur) return TRUE;
1803 static type_t *get_type(unsigned char type, char *name, int t)
1805 struct rtype *cur = NULL;
1808 cur = type_hash[hash_ident(name)];
1809 while (cur && (cur->t != t || strcmp(cur->name, name)))
1816 tp = make_type(type, NULL);
1818 if (!name) return tp;
1819 return reg_type(tp, name, t);
1822 static type_t *get_typev(unsigned char type, var_t *name, int t)
1829 return get_type(type, sname, t);
1832 static int get_struct_type(var_list_t *fields)
1834 int has_pointer = 0;
1835 int has_conformance = 0;
1836 int has_variance = 0;
1839 if (get_padding(fields))
1840 return RPC_FC_BOGUS_STRUCT;
1842 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1844 type_t *t = field->type;
1846 if (is_user_type(t))
1847 return RPC_FC_BOGUS_STRUCT;
1858 case RPC_FC_ENCAPSULATED_UNION:
1859 case RPC_FC_NON_ENCAPSULATED_UNION:
1860 case RPC_FC_BOGUS_STRUCT:
1861 return RPC_FC_BOGUS_STRUCT;
1868 if (field->type->declarray)
1870 if (is_string_type(field->attrs, field->type))
1872 if (is_conformant_array(field->type))
1873 has_conformance = 1;
1878 if (is_array(field->type->ref))
1879 return RPC_FC_BOGUS_STRUCT;
1881 if (is_conformant_array(field->type))
1883 has_conformance = 1;
1884 if (field->type->declarray && list_next(fields, &field->entry))
1885 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1888 if (field->type->length_is)
1891 t = field->type->ref;
1897 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1898 * Simple types don't effect the type of struct.
1899 * A struct containing a simple struct is still a simple struct.
1900 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1902 case 0: /* void pointer */
1912 case RPC_FC_INT3264:
1913 case RPC_FC_UINT3264:
1926 case RPC_FC_CVARRAY:
1927 case RPC_FC_BOGUS_ARRAY:
1932 * Propagate member attributes
1933 * a struct should be at least as complex as its member
1935 case RPC_FC_CVSTRUCT:
1936 has_conformance = 1;
1941 case RPC_FC_CPSTRUCT:
1942 has_conformance = 1;
1943 if (list_next( fields, &field->entry ))
1944 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1949 case RPC_FC_CSTRUCT:
1950 has_conformance = 1;
1951 if (list_next( fields, &field->entry ))
1952 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1956 case RPC_FC_PSTRUCT:
1961 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1962 /* fallthru - treat it as complex */
1964 /* as soon as we see one of these these members, it's bogus... */
1965 case RPC_FC_ENCAPSULATED_UNION:
1966 case RPC_FC_NON_ENCAPSULATED_UNION:
1967 case RPC_FC_BOGUS_STRUCT:
1969 return RPC_FC_BOGUS_STRUCT;
1975 if ( has_conformance )
1976 return RPC_FC_CVSTRUCT;
1978 return RPC_FC_BOGUS_STRUCT;
1980 if( has_conformance && has_pointer )
1981 return RPC_FC_CPSTRUCT;
1982 if( has_conformance )
1983 return RPC_FC_CSTRUCT;
1985 return RPC_FC_PSTRUCT;
1986 return RPC_FC_STRUCT;
1989 /***** constant repository *****/
1994 struct rconst *next;
1997 struct rconst *const_hash[HASHMAX];
1999 static var_t *reg_const(var_t *var)
2004 error_loc("registering constant without name\n");
2007 hash = hash_ident(var->name);
2008 nc = xmalloc(sizeof(struct rconst));
2009 nc->name = var->name;
2011 nc->next = const_hash[hash];
2012 const_hash[hash] = nc;
2016 static var_t *find_const(char *name, int f)
2018 struct rconst *cur = const_hash[hash_ident(name)];
2019 while (cur && strcmp(cur->name, name))
2022 if (f) error_loc("constant '%s' not found\n", name);
2028 static void write_libid(const char *name, const attr_list_t *attr)
2030 const UUID *uuid = get_attrp(attr, ATTR_UUID);
2031 write_guid(idfile, "LIBID", name, uuid);
2034 static void write_clsid(type_t *cls)
2036 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
2037 write_guid(idfile, "CLSID", cls->name, uuid);
2040 static void write_diid(type_t *iface)
2042 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2043 write_guid(idfile, "DIID", iface->name, uuid);
2046 static void write_iid(type_t *iface)
2048 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
2049 write_guid(idfile, "IID", iface->name, uuid);
2052 static int compute_method_indexes(type_t *iface)
2058 idx = compute_method_indexes(iface->ref);
2065 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2066 if (! is_callas(f->def->attrs))
2072 static char *gen_name(void)
2074 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2075 static unsigned long n = 0;
2076 static const char *file_id;
2082 char *dst = dup_basename(input_name, ".idl");
2086 if (! isalnum((unsigned char) *dst))
2089 size = sizeof format - 7 + strlen(file_id) + 8;
2092 name = xmalloc(size);
2093 sprintf(name, format, file_id, n++);
2097 static void process_typedefs(pident_list_t *pidents)
2099 pident_t *pident, *next;
2101 if (!pidents) return;
2102 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2104 var_t *var = pident->var;
2105 type_t *type = find_type(var->name, 0);
2107 if (! parse_only && do_header)
2108 write_typedef(type);
2109 if (in_typelib && type->attrs)
2110 add_typelib_entry(type);
2117 static void check_arg(var_t *arg)
2119 type_t *t = arg->type;
2121 if (t->type == 0 && ! is_var_ptr(arg))
2122 error_loc("argument '%s' has void type\n", arg->name);
2125 static void check_all_user_types(ifref_list_t *ifrefs)
2127 const ifref_t *ifref;
2130 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2132 const func_list_t *fs = ifref->iface->funcs;
2133 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2134 check_for_additional_prototype_types(f->args);
2138 int is_valid_uuid(const char *s)
2142 for (i = 0; i < 36; ++i)
2143 if (i == 8 || i == 13 || i == 18 || i == 23)
2149 if (!isxdigit(s[i]))
2152 return s[i] == '\0';