widl: Make the rules for parsing fields in structures, encapsulated unions and non...
[wine/gsoc_dplay.git] / tools / widl / parser.y
blob1732eaec0186b7d1bd22454b1edc47486e1cc282
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
6 * Copyright 2006-2008 Robert Shearman
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
23 #include "config.h"
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <stdarg.h>
28 #include <assert.h>
29 #include <ctype.h>
30 #include <string.h>
31 #ifdef HAVE_ALLOCA_H
32 #include <alloca.h>
33 #endif
35 #include "widl.h"
36 #include "utils.h"
37 #include "parser.h"
38 #include "header.h"
39 #include "typelib.h"
40 #include "typegen.h"
41 #include "expr.h"
43 #if defined(YYBYACC)
44 /* Berkeley yacc (byacc) doesn't seem to know about these */
45 /* Some *BSD supplied versions do define these though */
46 # ifndef YYEMPTY
47 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
48 # endif
49 # ifndef YYLEX
50 # define YYLEX yylex()
51 # endif
53 #elif defined(YYBISON)
54 /* Bison was used for original development */
55 /* #define YYEMPTY -2 */
56 /* #define YYLEX yylex() */
58 #else
59 /* No yacc we know yet */
60 # if !defined(YYEMPTY) || !defined(YYLEX)
61 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
62 # elif defined(__GNUC__) /* gcc defines the #warning directive */
63 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
64 /* #else we just take a chance that it works... */
65 # endif
66 #endif
68 #define YYERROR_VERBOSE
70 unsigned char pointer_default = RPC_FC_UP;
71 static int is_object_interface = FALSE;
72 /* are we inside a library block? */
73 static int is_inside_library = FALSE;
75 typedef struct list typelist_t;
76 struct typenode {
77 type_t *type;
78 struct list entry;
81 typelist_t incomplete_types = LIST_INIT(incomplete_types);
83 static void add_incomplete(type_t *t);
84 static void fix_incomplete(void);
86 static str_list_t *append_str(str_list_t *list, char *str);
87 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
88 static attr_t *make_attr(enum attr_type type);
89 static attr_t *make_attrv(enum attr_type type, unsigned long val);
90 static attr_t *make_attrp(enum attr_type type, void *val);
91 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
92 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
93 static void set_type(var_t *v, type_t *type, const declarator_t *decl, int top);
94 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
95 static ifref_t *make_ifref(type_t *iface);
96 static var_list_t *append_var(var_list_t *list, var_t *var);
97 static var_t *make_var(char *name);
98 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
99 static declarator_t *make_declarator(var_t *var);
100 static func_list_t *append_func(func_list_t *list, func_t *func);
101 static func_t *make_func(var_t *def);
102 static type_t *make_class(char *name);
103 static type_t *make_safearray(type_t *type);
104 static type_t *make_builtin(char *name);
105 static type_t *make_int(int sign);
106 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
107 static type_t *make_func_type(var_list_t *args);
108 static void type_set_function_callconv(type_t *type, char *callconv);
109 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
111 static type_t *reg_type(type_t *type, const char *name, int t);
112 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
113 static type_t *find_type2(char *name, int t);
114 static type_t *get_type(unsigned char type, char *name, int t);
115 static type_t *get_typev(unsigned char type, var_t *name, int t);
116 static int get_struct_type(var_list_t *fields);
118 static var_t *reg_const(var_t *var);
120 static void write_libid(const typelib_t *typelib);
121 static void write_clsid(type_t *cls);
122 static void write_diid(type_t *iface);
123 static void write_iid(type_t *iface);
125 static int compute_method_indexes(type_t *iface);
126 static char *gen_name(void);
127 static statement_t *process_typedefs(var_list_t *names);
128 static void check_arg(var_t *arg);
129 static void check_functions(const type_t *iface);
130 static void check_all_user_types(const statement_list_t *stmts);
131 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs);
132 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
133 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
134 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
135 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs);
136 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
137 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs);
138 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs);
139 const char *get_attr_display_name(enum attr_type type);
140 static void add_explicit_handle_if_necessary(func_t *func);
142 static statement_t *make_statement(enum statement_type type);
143 static statement_t *make_statement_type_decl(type_t *type);
144 static statement_t *make_statement_reference(type_t *type);
145 static statement_t *make_statement_init_decl(var_t *var);
146 static statement_t *make_statement_extern(var_t *var);
147 static statement_t *make_statement_library(typelib_t *typelib);
148 static statement_t *make_statement_cppquote(const char *str);
149 static statement_t *make_statement_importlib(const char *str);
150 static statement_t *make_statement_module(type_t *type);
151 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
153 #define tsENUM 1
154 #define tsSTRUCT 2
155 #define tsUNION 3
158 %union {
159 attr_t *attr;
160 attr_list_t *attr_list;
161 str_list_t *str_list;
162 expr_t *expr;
163 expr_list_t *expr_list;
164 array_dims_t *array_dims;
165 type_t *type;
166 var_t *var;
167 var_list_t *var_list;
168 declarator_t *declarator;
169 declarator_list_t *declarator_list;
170 func_t *func;
171 func_list_t *func_list;
172 statement_t *statement;
173 statement_list_t *stmt_list;
174 ifref_t *ifref;
175 ifref_list_t *ifref_list;
176 char *str;
177 UUID *uuid;
178 unsigned int num;
179 double dbl;
180 interface_info_t ifinfo;
181 typelib_t *typelib;
184 %token <str> aIDENTIFIER
185 %token <str> aKNOWNTYPE
186 %token <num> aNUM aHEXNUM
187 %token <dbl> aDOUBLE
188 %token <str> aSTRING
189 %token <uuid> aUUID
190 %token aEOF
191 %token SHL SHR
192 %token MEMBERPTR
193 %token EQUALITY INEQUALITY
194 %token GREATEREQUAL LESSEQUAL
195 %token LOGICALOR LOGICALAND
196 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
197 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
198 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
199 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
200 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
201 %token tDEFAULT
202 %token tDEFAULTCOLLELEM
203 %token tDEFAULTVALUE
204 %token tDEFAULTVTABLE
205 %token tDISPLAYBIND
206 %token tDISPINTERFACE
207 %token tDLLNAME tDOUBLE tDUAL
208 %token tENDPOINT
209 %token tENTRY tENUM tERRORSTATUST
210 %token tEXPLICITHANDLE tEXTERN
211 %token tFALSE
212 %token tFASTCALL
213 %token tFLOAT
214 %token tHANDLE
215 %token tHANDLET
216 %token tHELPCONTEXT tHELPFILE
217 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
218 %token tHIDDEN
219 %token tHYPER tID tIDEMPOTENT
220 %token tIIDIS
221 %token tIMMEDIATEBIND
222 %token tIMPLICITHANDLE
223 %token tIMPORT tIMPORTLIB
224 %token tIN tINLINE
225 %token tINPUTSYNC
226 %token tINT tINT64
227 %token tINTERFACE
228 %token tLCID
229 %token tLENGTHIS tLIBRARY
230 %token tLOCAL
231 %token tLONG
232 %token tMETHODS
233 %token tMODULE
234 %token tNONBROWSABLE
235 %token tNONCREATABLE
236 %token tNONEXTENSIBLE
237 %token tNULL
238 %token tOBJECT tODL tOLEAUTOMATION
239 %token tOPTIONAL
240 %token tOUT
241 %token tPASCAL
242 %token tPOINTERDEFAULT
243 %token tPROPERTIES
244 %token tPROPGET tPROPPUT tPROPPUTREF
245 %token tPTR
246 %token tPUBLIC
247 %token tRANGE
248 %token tREADONLY tREF
249 %token tREQUESTEDIT
250 %token tRESTRICTED
251 %token tRETVAL
252 %token tSAFEARRAY
253 %token tSHORT
254 %token tSIGNED
255 %token tSINGLE
256 %token tSIZEIS tSIZEOF
257 %token tSMALL
258 %token tSOURCE
259 %token tSTDCALL
260 %token tSTRICTCONTEXTHANDLE
261 %token tSTRING tSTRUCT
262 %token tSWITCH tSWITCHIS tSWITCHTYPE
263 %token tTRANSMITAS
264 %token tTRUE
265 %token tTYPEDEF
266 %token tUNION
267 %token tUNIQUE
268 %token tUNSIGNED
269 %token tUUID
270 %token tV1ENUM
271 %token tVARARG
272 %token tVERSION
273 %token tVOID
274 %token tWCHAR tWIREMARSHAL
276 %type <attr> attribute
277 %type <attr_list> m_attributes attributes attrib_list
278 %type <str_list> str_list
279 %type <expr> m_expr expr expr_const array
280 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
281 %type <ifinfo> interfacehdr
282 %type <type> inherit interface interfacedef interfacedec
283 %type <type> dispinterface dispinterfacehdr dispinterfacedef
284 %type <type> module modulehdr moduledef
285 %type <type> base_type int_std
286 %type <type> enumdef structdef uniondef
287 %type <type> type
288 %type <ifref> coclass_int
289 %type <ifref_list> coclass_ints
290 %type <var> arg field ne_union_field union_field s_field case enum constdef externdef
291 %type <var_list> m_args no_args args fields ne_union_fields cases enums enum_list dispint_props
292 %type <var> m_ident t_ident ident
293 %type <declarator> declarator func_declarator direct_declarator
294 %type <declarator_list> declarator_list
295 %type <func> funcdef
296 %type <func_list> int_statements dispint_meths
297 %type <type> coclass coclasshdr coclassdef
298 %type <num> pointer_type version
299 %type <str> libraryhdr callconv cppquote importlib
300 %type <uuid> uuid_string
301 %type <num> import_start
302 %type <typelib> library_start librarydef
303 %type <statement> statement typedef
304 %type <stmt_list> gbl_statements imp_statements
306 %left ','
307 %right '?' ':'
308 %left LOGICALOR
309 %left LOGICALAND
310 %left '|'
311 %left '^'
312 %left '&'
313 %left EQUALITY INEQUALITY
314 %left '<' '>' LESSEQUAL GREATEREQUAL
315 %left SHL SHR
316 %left '-' '+'
317 %left '*' '/' '%'
318 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
319 %left '.' MEMBERPTR '[' ']'
323 input: gbl_statements { fix_incomplete();
324 check_all_user_types($1);
325 write_proxies($1);
326 write_client($1);
327 write_server($1);
328 write_dlldata($1);
332 gbl_statements: { $$ = NULL; }
333 | gbl_statements interfacedec { $$ = $1; }
334 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
335 | gbl_statements coclass ';' { $$ = $1;
336 reg_type($2, $2->name, 0);
337 if (!parse_only && do_header) write_coclass_forward($2);
339 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
340 add_typelib_entry($2);
341 reg_type($2, $2->name, 0);
342 if (!parse_only && do_header) write_coclass_forward($2);
344 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2));
345 add_typelib_entry($2);
347 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
348 | gbl_statements statement { $$ = append_statement($1, $2); }
351 imp_statements: { $$ = NULL; }
352 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); if (!parse_only) add_typelib_entry($2); }
353 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); if (!parse_only) add_typelib_entry($2); }
354 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
355 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
356 if (!parse_only) add_typelib_entry($2);
357 reg_type($2, $2->name, 0);
358 if (!parse_only && do_header) write_coclass_forward($2);
360 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); if (!parse_only) add_typelib_entry($2); }
361 | imp_statements statement { $$ = append_statement($1, $2); }
362 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
363 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
366 int_statements: { $$ = NULL; }
367 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
368 | int_statements statement { $$ = $1; }
371 semicolon_opt:
372 | ';'
375 statement: constdef ';' { $$ = make_statement_init_decl($1);
376 if (!parse_only && do_header) { write_constdef($1); }
378 | cppquote { $$ = make_statement_cppquote($1); }
379 | enumdef ';' { $$ = make_statement_type_decl($1);
380 if (!parse_only && do_header) {
381 write_type_def_or_decl(header, $1, FALSE, NULL);
382 fprintf(header, ";\n\n");
385 | externdef ';' { $$ = make_statement_extern($1);
386 if (!parse_only && do_header) write_externdef($1);
388 | import { $$ = NULL; }
389 | structdef ';' { $$ = make_statement_type_decl($1);
390 if (!parse_only && do_header) {
391 write_type_def_or_decl(header, $1, FALSE, NULL);
392 fprintf(header, ";\n\n");
395 | typedef ';' { $$ = $1; }
396 | uniondef ';' { $$ = make_statement_type_decl($1);
397 if (!parse_only && do_header) {
398 write_type_def_or_decl(header, $1, FALSE, NULL);
399 fprintf(header, ";\n\n");
404 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
406 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
407 $$ = do_import($2);
408 if (!$$) yychar = aEOF;
412 import: import_start imp_statements aEOF
413 { if ($1) pop_import(); }
416 importlib: tIMPORTLIB '(' aSTRING ')'
417 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
420 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
422 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
423 if (!parse_only) start_typelib($$);
424 if (!parse_only && do_header) write_library($$);
425 if (!parse_only && do_idfile) write_libid($$);
426 is_inside_library = TRUE;
429 librarydef: library_start imp_statements '}'
430 semicolon_opt { $$ = $1;
431 $$->stmts = $2;
432 if (!parse_only) end_typelib();
433 is_inside_library = FALSE;
437 m_args: { $$ = NULL; }
438 | args
441 no_args: tVOID { $$ = NULL; }
444 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
445 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
446 | no_args
449 /* split into two rules to get bison to resolve a tVOID conflict */
450 arg: attributes type declarator { $$ = $3->var;
451 $$->attrs = $1;
452 set_type($$, $2, $3, TRUE);
453 free($3);
455 | type declarator { $$ = $2->var;
456 set_type($$, $1, $2, TRUE);
457 free($2);
461 array: '[' m_expr ']' { $$ = $2; }
462 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
465 m_attributes: { $$ = NULL; }
466 | attributes
469 attributes:
470 '[' attrib_list ']' { $$ = $2;
471 if (!$$)
472 error_loc("empty attribute lists unsupported\n");
476 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
477 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
478 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
481 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
482 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
485 attribute: { $$ = NULL; }
486 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
487 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
488 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
489 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
490 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
491 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
492 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
493 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
494 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
495 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
496 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
497 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
498 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
499 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
500 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
501 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
502 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
503 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
504 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
505 | tDUAL { $$ = make_attr(ATTR_DUAL); }
506 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
507 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
508 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
509 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
510 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
511 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
512 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
513 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
514 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
515 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
516 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
517 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
518 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
519 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
520 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
521 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
522 | tIN { $$ = make_attr(ATTR_IN); }
523 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
524 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
525 | tLCID '(' expr_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
526 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
527 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
528 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
529 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
530 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
531 | tODL { $$ = make_attr(ATTR_ODL); }
532 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
533 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
534 | tOUT { $$ = make_attr(ATTR_OUT); }
535 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
536 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
537 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
538 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
539 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
540 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
541 list = append_expr( list, $5 );
542 $$ = make_attrp(ATTR_RANGE, list); }
543 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
544 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
545 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
546 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
547 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
548 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
549 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
550 | tSTRING { $$ = make_attr(ATTR_STRING); }
551 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
552 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
553 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
554 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
555 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
556 | tVARARG { $$ = make_attr(ATTR_VARARG); }
557 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
558 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
559 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
562 uuid_string:
563 aUUID
564 | aSTRING { if (!is_valid_uuid($1))
565 error_loc("invalid UUID: %s\n", $1);
566 $$ = parse_uuid($1); }
569 callconv: tCDECL { $$ = $<str>1; }
570 | tFASTCALL { $$ = $<str>1; }
571 | tPASCAL { $$ = $<str>1; }
572 | tSTDCALL { $$ = $<str>1; }
575 cases: { $$ = NULL; }
576 | cases case { $$ = append_var( $1, $2 ); }
579 case: tCASE expr_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
580 $$ = $4; if (!$$) $$ = make_var(NULL);
581 $$->attrs = append_attr( $$->attrs, a );
583 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
584 $$ = $3; if (!$$) $$ = make_var(NULL);
585 $$->attrs = append_attr( $$->attrs, a );
589 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
590 set_type($$, $2, NULL, FALSE);
591 $$->eval = $5;
595 enums: { $$ = NULL; }
596 | enum_list ',' { $$ = $1; }
597 | enum_list
600 enum_list: enum { if (!$1->eval)
601 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
602 $$ = append_var( NULL, $1 );
604 | enum_list ',' enum { if (!$3->eval)
606 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
607 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
609 $$ = append_var( $1, $3 );
613 enum: ident '=' expr_const { $$ = reg_const($1);
614 $$->eval = $3;
615 $$->type = make_int(0);
617 | ident { $$ = reg_const($1);
618 $$->type = make_int(0);
622 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
623 $$->kind = TKIND_ENUM;
624 $$->fields_or_args = $4;
625 $$->defined = TRUE;
626 if(in_typelib)
627 add_typelib_entry($$);
631 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
632 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
636 exprs: { $$ = make_expr(EXPR_VOID); }
637 | expr_list
640 expr_list: expr
641 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
645 m_expr: { $$ = make_expr(EXPR_VOID); }
646 | expr
649 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
650 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
651 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
652 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
653 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
654 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
655 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
656 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
657 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
658 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
659 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
660 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
661 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
662 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
663 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
664 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
665 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
666 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
667 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
668 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
669 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
670 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
671 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
672 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
673 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
674 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
675 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
676 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
677 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
678 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
679 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
680 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
681 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
682 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
683 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
684 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
685 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
686 | '(' expr ')' { $$ = $2; }
689 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
690 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
693 expr_const: expr { $$ = $1;
694 if (!$$->is_const)
695 error_loc("expression is not constant\n");
699 externdef: tEXTERN tCONST type ident { $$ = $4;
700 set_type($$, $3, NULL, FALSE);
704 fields: { $$ = NULL; }
705 | fields field { $$ = append_var( $1, $2 ); }
708 field: s_field ';' { $$ = $1; }
709 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
712 ne_union_field:
713 s_field ';' { $$ = $1; }
714 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
716 ne_union_fields: { $$ = NULL; }
717 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
720 union_field:
721 s_field ';' { $$ = $1; }
722 | ';' { $$ = NULL; }
724 s_field: m_attributes type declarator { $$ = $3->var;
725 $$->attrs = check_field_attrs($$->name, $1);
726 set_type($$, $2, $3, FALSE);
727 free($3);
731 funcdef:
732 m_attributes type declarator { var_t *v = $3->var;
733 v->attrs = check_function_attrs(v->name, $1);
734 set_type(v, $2, $3, FALSE);
735 free($3);
736 $$ = make_func(v);
740 m_ident: { $$ = NULL; }
741 | ident
744 t_ident: { $$ = NULL; }
745 | aIDENTIFIER { $$ = make_var($1); }
746 | aKNOWNTYPE { $$ = make_var($1); }
749 ident: aIDENTIFIER { $$ = make_var($1); }
750 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
751 | aKNOWNTYPE { $$ = make_var($<str>1); }
754 base_type: tBYTE { $$ = make_builtin($<str>1); }
755 | tWCHAR { $$ = make_builtin($<str>1); }
756 | int_std
757 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
758 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
759 switch ($$->type) {
760 case RPC_FC_CHAR: break;
761 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
762 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
763 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
764 case RPC_FC_HYPER:
765 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
767 $$ = alias($$, "MIDL_uhyper");
768 $$->sign = 0;
770 break;
771 default: break;
774 | tUNSIGNED { $$ = make_int(-1); }
775 | tFLOAT { $$ = make_builtin($<str>1); }
776 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
777 | tDOUBLE { $$ = make_builtin($<str>1); }
778 | tBOOLEAN { $$ = make_builtin($<str>1); }
779 | tERRORSTATUST { $$ = make_builtin($<str>1); }
780 | tHANDLET { $$ = make_builtin($<str>1); }
783 m_int:
784 | tINT
787 int_std: tINT { $$ = make_builtin($<str>1); }
788 | tSHORT m_int { $$ = make_builtin($<str>1); }
789 | tSMALL { $$ = make_builtin($<str>1); }
790 | tLONG m_int { $$ = make_builtin($<str>1); }
791 | tHYPER m_int { $$ = make_builtin($<str>1); }
792 | tINT64 { $$ = make_builtin($<str>1); }
793 | tCHAR { $$ = make_builtin($<str>1); }
796 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
797 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
798 if ($$->defined) error_loc("multiple definition error\n");
799 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
803 coclasshdr: attributes coclass { $$ = $2;
804 $$->attrs = check_coclass_attrs($2->name, $1);
805 if (!parse_only && do_header)
806 write_coclass($$);
807 if (!parse_only && do_idfile)
808 write_clsid($$);
812 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
813 { $$ = $1;
814 $$->ifaces = $3;
815 $$->defined = TRUE;
819 coclass_ints: { $$ = NULL; }
820 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
823 coclass_int:
824 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
827 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
828 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
831 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
832 is_object_interface = TRUE;
833 $$ = $2;
834 if ($$->defined) error_loc("multiple definition error\n");
835 attrs = make_attr(ATTR_DISPINTERFACE);
836 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
837 $$->ref = find_type("IDispatch", 0);
838 if (!$$->ref) error_loc("IDispatch is undefined\n");
839 $$->defined = TRUE;
840 if (!parse_only && do_header) write_forward($$);
844 dispint_props: tPROPERTIES ':' { $$ = NULL; }
845 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
848 dispint_meths: tMETHODS ':' { $$ = NULL; }
849 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
852 dispinterfacedef: dispinterfacehdr '{'
853 dispint_props
854 dispint_meths
855 '}' { $$ = $1;
856 $$->fields_or_args = $3;
857 $$->funcs = $4;
858 if (!parse_only && do_header) write_dispinterface($$);
859 if (!parse_only && do_idfile) write_diid($$);
861 | dispinterfacehdr
862 '{' interface ';' '}' { $$ = $1;
863 $$->fields_or_args = $3->fields_or_args;
864 $$->funcs = $3->funcs;
865 if (!parse_only && do_header) write_dispinterface($$);
866 if (!parse_only && do_idfile) write_diid($$);
870 inherit: { $$ = NULL; }
871 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
874 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
875 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
878 interfacehdr: attributes interface { $$.interface = $2;
879 $$.old_pointer_default = pointer_default;
880 if (is_attr($1, ATTR_POINTERDEFAULT))
881 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
882 is_object_interface = is_object($1);
883 if ($2->defined) error_loc("multiple definition error\n");
884 $2->attrs = check_iface_attrs($2->name, $1);
885 $2->defined = TRUE;
886 if (!parse_only && do_header) write_forward($2);
890 interfacedef: interfacehdr inherit
891 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
892 $$->ref = $2;
893 $$->funcs = $4;
894 check_functions($$);
895 compute_method_indexes($$);
896 if (!parse_only && do_header) write_interface($$);
897 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
898 if (!parse_only && do_idfile) write_iid($$);
899 pointer_default = $1.old_pointer_default;
901 /* MIDL is able to import the definition of a base class from inside the
902 * definition of a derived class, I'll try to support it with this rule */
903 | interfacehdr ':' aIDENTIFIER
904 '{' import int_statements '}'
905 semicolon_opt { $$ = $1.interface;
906 $$->ref = find_type2($3, 0);
907 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
908 $$->funcs = $6;
909 compute_method_indexes($$);
910 if (!parse_only && do_header) write_interface($$);
911 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
912 if (!parse_only && do_idfile) write_iid($$);
913 pointer_default = $1.old_pointer_default;
915 | dispinterfacedef semicolon_opt { $$ = $1; }
918 interfacedec:
919 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
920 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
923 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
924 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
927 modulehdr: attributes module { $$ = $2;
928 $$->attrs = check_module_attrs($2->name, $1);
932 moduledef: modulehdr '{' int_statements '}'
933 semicolon_opt { $$ = $1;
934 $$->funcs = $3;
935 /* FIXME: if (!parse_only && do_header) write_module($$); */
939 declarator:
940 '*' declarator %prec PPTR { $$ = $2; $$->type = make_type(pointer_default, $$->type); }
941 | tCONST declarator { $$ = $2; /* FIXME */ }
942 | callconv declarator { $$ = $2; type_set_function_callconv($$->func_type, $1); }
943 | direct_declarator
946 func_declarator: direct_declarator '(' m_args ')'
947 { $$ = $1;
948 $$->type = append_ptrchain_type($$->type, make_func_type($3));
952 direct_declarator:
953 ident { $$ = make_declarator($1); }
954 | '(' declarator ')' { $$ = $2; }
955 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
956 | func_declarator { $$ = $1;
957 $$->func_type = $$->type;
958 $$->type = NULL;
962 declarator_list:
963 declarator { $$ = append_declarator( NULL, $1 ); }
964 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
967 pointer_type:
968 tREF { $$ = RPC_FC_RP; }
969 | tUNIQUE { $$ = RPC_FC_UP; }
970 | tPTR { $$ = RPC_FC_FP; }
973 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
974 /* overwrite RPC_FC_STRUCT with a more exact type */
975 $$->type = get_struct_type( $4 );
976 $$->kind = TKIND_RECORD;
977 $$->fields_or_args = $4;
978 $$->defined = TRUE;
979 if(in_typelib)
980 add_typelib_entry($$);
984 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
985 | aKNOWNTYPE { $$ = find_type($1, 0); }
986 | base_type { $$ = $1; }
987 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
988 | enumdef { $$ = $1; }
989 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
990 | structdef { $$ = $1; }
991 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
992 | uniondef { $$ = $1; }
993 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
994 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
997 typedef: tTYPEDEF m_attributes type declarator_list
998 { reg_typedefs($3, $4, check_typedef_attrs($2));
999 $$ = process_typedefs($4);
1003 uniondef: tUNION t_ident '{' ne_union_fields '}'
1004 { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
1005 $$->kind = TKIND_UNION;
1006 $$->fields_or_args = $4;
1007 $$->defined = TRUE;
1009 | tUNION t_ident
1010 tSWITCH '(' s_field ')'
1011 m_ident '{' cases '}' { var_t *u = $7;
1012 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
1013 $$->kind = TKIND_UNION;
1014 if (!u) u = make_var( xstrdup("tagged_union") );
1015 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
1016 u->type->kind = TKIND_UNION;
1017 u->type->fields_or_args = $9;
1018 u->type->defined = TRUE;
1019 $$->fields_or_args = append_var( $$->fields_or_args, $5 );
1020 $$->fields_or_args = append_var( $$->fields_or_args, u );
1021 $$->defined = TRUE;
1025 version:
1026 aNUM { $$ = MAKEVERSION($1, 0); }
1027 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1032 static void decl_builtin(const char *name, unsigned char type)
1034 type_t *t = make_type(type, NULL);
1035 t->name = xstrdup(name);
1036 reg_type(t, name, 0);
1039 static type_t *make_builtin(char *name)
1041 /* NAME is strdup'd in the lexer */
1042 type_t *t = duptype(find_type(name, 0), 0);
1043 t->name = name;
1044 return t;
1047 static type_t *make_int(int sign)
1049 type_t *t = duptype(find_type("int", 0), 1);
1051 t->sign = sign;
1052 if (sign < 0)
1053 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
1055 return t;
1058 void init_types(void)
1060 decl_builtin("void", 0);
1061 decl_builtin("byte", RPC_FC_BYTE);
1062 decl_builtin("wchar_t", RPC_FC_WCHAR);
1063 decl_builtin("int", RPC_FC_LONG); /* win32 */
1064 decl_builtin("short", RPC_FC_SHORT);
1065 decl_builtin("small", RPC_FC_SMALL);
1066 decl_builtin("long", RPC_FC_LONG);
1067 decl_builtin("hyper", RPC_FC_HYPER);
1068 decl_builtin("__int64", RPC_FC_HYPER);
1069 decl_builtin("char", RPC_FC_CHAR);
1070 decl_builtin("float", RPC_FC_FLOAT);
1071 decl_builtin("double", RPC_FC_DOUBLE);
1072 decl_builtin("boolean", RPC_FC_BYTE);
1073 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1074 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1077 static str_list_t *append_str(str_list_t *list, char *str)
1079 struct str_list_entry_t *entry;
1081 if (!str) return list;
1082 if (!list)
1084 list = xmalloc( sizeof(*list) );
1085 list_init( list );
1087 entry = xmalloc( sizeof(*entry) );
1088 entry->str = str;
1089 list_add_tail( list, &entry->entry );
1090 return list;
1093 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1095 attr_t *attr_existing;
1096 if (!attr) return list;
1097 if (!list)
1099 list = xmalloc( sizeof(*list) );
1100 list_init( list );
1102 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1103 if (attr_existing->type == attr->type)
1105 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1106 /* use the last attribute, like MIDL does */
1107 list_remove(&attr_existing->entry);
1108 break;
1110 list_add_tail( list, &attr->entry );
1111 return list;
1114 static attr_t *make_attr(enum attr_type type)
1116 attr_t *a = xmalloc(sizeof(attr_t));
1117 a->type = type;
1118 a->u.ival = 0;
1119 return a;
1122 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1124 attr_t *a = xmalloc(sizeof(attr_t));
1125 a->type = type;
1126 a->u.ival = val;
1127 return a;
1130 static attr_t *make_attrp(enum attr_type type, void *val)
1132 attr_t *a = xmalloc(sizeof(attr_t));
1133 a->type = type;
1134 a->u.pval = val;
1135 return a;
1138 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1140 if (!expr) return list;
1141 if (!list)
1143 list = xmalloc( sizeof(*list) );
1144 list_init( list );
1146 list_add_tail( list, &expr->entry );
1147 return list;
1150 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1152 if (!expr) return list;
1153 if (!list)
1155 list = xmalloc( sizeof(*list) );
1156 list_init( list );
1158 list_add_tail( list, &expr->entry );
1159 return list;
1162 static struct list type_pool = LIST_INIT(type_pool);
1163 typedef struct
1165 type_t data;
1166 struct list link;
1167 } type_pool_node_t;
1169 type_t *alloc_type(void)
1171 type_pool_node_t *node = xmalloc(sizeof *node);
1172 list_add_tail(&type_pool, &node->link);
1173 return &node->data;
1176 void set_all_tfswrite(int val)
1178 type_pool_node_t *node;
1179 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1180 node->data.tfswrite = val;
1183 type_t *make_type(unsigned char type, type_t *ref)
1185 type_t *t = alloc_type();
1186 t->name = NULL;
1187 t->kind = TKIND_PRIMITIVE;
1188 t->type = type;
1189 t->ref = ref;
1190 t->attrs = NULL;
1191 t->orig = NULL;
1192 t->funcs = NULL;
1193 t->fields_or_args = NULL;
1194 t->ifaces = NULL;
1195 t->dim = 0;
1196 t->size_is = NULL;
1197 t->length_is = NULL;
1198 t->typestring_offset = 0;
1199 t->ptrdesc = 0;
1200 t->declarray = FALSE;
1201 t->ignore = (parse_only != 0);
1202 t->is_const = FALSE;
1203 t->sign = 0;
1204 t->defined = FALSE;
1205 t->written = FALSE;
1206 t->user_types_registered = FALSE;
1207 t->tfswrite = FALSE;
1208 t->checked = FALSE;
1209 t->typelib_idx = -1;
1210 return t;
1213 static type_t *make_func_type(var_list_t *args)
1215 type_t *t = make_type(RPC_FC_FUNCTION, NULL);
1216 t->fields_or_args = args;
1217 return t;
1220 static void type_set_function_callconv(type_t *type, char *callconv)
1222 if (!type)
1223 error_loc("calling convention applied to non-function-pointer type\n");
1224 for (; is_ptr(type); type = type->ref)
1226 assert(type->type == RPC_FC_FUNCTION);
1227 type->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, callconv));
1230 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1232 type_t *ptrchain_type;
1233 if (!ptrchain)
1234 return type;
1235 for (ptrchain_type = ptrchain; ptrchain_type->ref; ptrchain_type = ptrchain_type->ref)
1237 ptrchain_type->ref = type;
1238 return ptrchain;
1241 static void set_type(var_t *v, type_t *type, const declarator_t *decl,
1242 int top)
1244 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1245 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1246 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1247 int sizeless, has_varconf;
1248 expr_t *dim;
1249 type_t *atype, **ptype;
1250 array_dims_t *arr = decl ? decl->array : NULL;
1252 /* add type onto the end of the pointers in pident->type */
1253 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1255 /* the highest level of pointer specified should default to the var's ptr attr
1256 * or (RPC_FC_RP if not specified and it's a top level ptr), not
1257 * pointer_default so we need to fix that up here */
1258 if (!arr)
1260 const type_t *ptr = NULL;
1261 for (ptr = v->type; ptr; )
1263 if (ptr->kind == TKIND_ALIAS)
1264 ptr = ptr->orig;
1265 else
1266 break;
1268 if (ptr && is_ptr(ptr) && (ptr_attr || top))
1270 /* duplicate type to avoid changing original type */
1271 v->type = duptype(v->type, 1);
1272 v->type->type = ptr_attr ? ptr_attr : RPC_FC_RP;
1274 else if (ptr_attr)
1275 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1278 if (is_attr(v->attrs, ATTR_STRING) && !is_ptr(v->type) && !arr)
1279 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1280 v->name);
1282 sizeless = FALSE;
1283 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1285 if (sizeless)
1286 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1288 if (dim->is_const)
1290 unsigned int align = 0;
1291 size_t size = type_memsize(v->type, &align);
1293 if (dim->cval <= 0)
1294 error_loc("%s: array dimension must be positive\n", v->name);
1296 if (0xffffffffuL / size < (unsigned long) dim->cval)
1297 error_loc("%s: total array size is too large\n", v->name);
1298 else if (0xffffuL < size * dim->cval)
1299 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1300 else
1301 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1303 else
1305 sizeless = TRUE;
1306 v->type = make_type(RPC_FC_CARRAY, v->type);
1309 v->type->declarray = TRUE;
1310 v->type->dim = dim->cval;
1313 ptype = &v->type;
1314 has_varconf = FALSE;
1315 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1317 if (dim->type != EXPR_VOID)
1319 has_varconf = TRUE;
1320 atype = *ptype = duptype(*ptype, 0);
1322 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1323 error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1325 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1326 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1328 atype->type = RPC_FC_CARRAY;
1329 atype->size_is = dim;
1332 ptype = &(*ptype)->ref;
1333 if (*ptype == NULL)
1334 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1337 ptype = &v->type;
1338 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1340 if (dim->type != EXPR_VOID)
1342 has_varconf = TRUE;
1343 atype = *ptype = duptype(*ptype, 0);
1345 if (atype->type == RPC_FC_SMFARRAY)
1346 atype->type = RPC_FC_SMVARRAY;
1347 else if (atype->type == RPC_FC_LGFARRAY)
1348 atype->type = RPC_FC_LGVARRAY;
1349 else if (atype->type == RPC_FC_CARRAY)
1350 atype->type = RPC_FC_CVARRAY;
1351 else
1352 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1354 atype->length_is = dim;
1357 ptype = &(*ptype)->ref;
1358 if (*ptype == NULL)
1359 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1362 if (has_varconf && !last_array(v->type))
1364 ptype = &v->type;
1365 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1367 *ptype = duptype(*ptype, 0);
1368 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1372 if (is_array(v->type))
1374 const type_t *rt = v->type->ref;
1375 if (is_user_type(rt))
1376 v->type->type = RPC_FC_BOGUS_ARRAY;
1377 else
1378 switch (rt->type)
1380 case RPC_FC_BOGUS_STRUCT:
1381 case RPC_FC_NON_ENCAPSULATED_UNION:
1382 case RPC_FC_ENCAPSULATED_UNION:
1383 case RPC_FC_ENUM16:
1384 v->type->type = RPC_FC_BOGUS_ARRAY;
1385 break;
1386 /* FC_RP should be above, but widl overuses these, and will break things. */
1387 case RPC_FC_UP:
1388 case RPC_FC_RP:
1389 if (rt->ref->type == RPC_FC_IP)
1390 v->type->type = RPC_FC_BOGUS_ARRAY;
1391 break;
1395 /* v->type is currently pointing the the type on the left-side of the
1396 * declaration, so we need to fix this up so that it is the return type of the
1397 * function and make v->type point to the function side of the declaration */
1398 if (decl && decl->func_type)
1400 type_t *t;
1401 type_t *return_type = v->type;
1402 v->type = decl->func_type;
1403 for (t = v->type; is_ptr(t); t = t->ref)
1405 assert(t->type == RPC_FC_FUNCTION);
1406 t->ref = return_type;
1407 if (is_object_interface && !is_attr(t->attrs, ATTR_CALLCONV))
1409 static char *stdmethodcalltype;
1410 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1411 t->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1416 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1418 if (!iface) return list;
1419 if (!list)
1421 list = xmalloc( sizeof(*list) );
1422 list_init( list );
1424 list_add_tail( list, &iface->entry );
1425 return list;
1428 static ifref_t *make_ifref(type_t *iface)
1430 ifref_t *l = xmalloc(sizeof(ifref_t));
1431 l->iface = iface;
1432 l->attrs = NULL;
1433 return l;
1436 static var_list_t *append_var(var_list_t *list, var_t *var)
1438 if (!var) return list;
1439 if (!list)
1441 list = xmalloc( sizeof(*list) );
1442 list_init( list );
1444 list_add_tail( list, &var->entry );
1445 return list;
1448 static var_t *make_var(char *name)
1450 var_t *v = xmalloc(sizeof(var_t));
1451 v->name = name;
1452 v->type = NULL;
1453 v->attrs = NULL;
1454 v->eval = NULL;
1455 v->loc_info.input_name = input_name ? input_name : "stdin";
1456 v->loc_info.line_number = line_number;
1457 v->loc_info.near_text = parser_text;
1458 return v;
1461 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1463 if (!d) return list;
1464 if (!list) {
1465 list = xmalloc(sizeof(*list));
1466 list_init(list);
1468 list_add_tail(list, &d->entry);
1469 return list;
1472 static declarator_t *make_declarator(var_t *var)
1474 declarator_t *d = xmalloc(sizeof(*d));
1475 d->var = var;
1476 d->type = NULL;
1477 d->func_type = NULL;
1478 d->array = NULL;
1479 return d;
1482 static func_list_t *append_func(func_list_t *list, func_t *func)
1484 if (!func) return list;
1485 if (!list)
1487 list = xmalloc( sizeof(*list) );
1488 list_init( list );
1490 list_add_tail( list, &func->entry );
1491 return list;
1494 static func_t *make_func(var_t *def)
1496 func_t *f = xmalloc(sizeof(func_t));
1497 f->def = def;
1498 f->args = def->type->fields_or_args;
1499 f->ignore = parse_only;
1500 f->idx = -1;
1501 return f;
1504 static type_t *make_class(char *name)
1506 type_t *c = make_type(RPC_FC_COCLASS, NULL);
1507 c->name = name;
1508 c->kind = TKIND_COCLASS;
1509 return c;
1512 static type_t *make_safearray(type_t *type)
1514 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1515 sa->ref = type;
1516 return make_type(pointer_default, sa);
1519 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1521 typelib_t *typelib = xmalloc(sizeof(*typelib));
1522 typelib->name = xstrdup(name);
1523 typelib->filename = NULL;
1524 typelib->attrs = attrs;
1525 list_init( &typelib->entries );
1526 list_init( &typelib->importlibs );
1527 return typelib;
1530 #define HASHMAX 64
1532 static int hash_ident(const char *name)
1534 const char *p = name;
1535 int sum = 0;
1536 /* a simple sum hash is probably good enough */
1537 while (*p) {
1538 sum += *p;
1539 p++;
1541 return sum & (HASHMAX-1);
1544 /***** type repository *****/
1546 struct rtype {
1547 const char *name;
1548 type_t *type;
1549 int t;
1550 struct rtype *next;
1553 struct rtype *type_hash[HASHMAX];
1555 static type_t *reg_type(type_t *type, const char *name, int t)
1557 struct rtype *nt;
1558 int hash;
1559 if (!name) {
1560 error_loc("registering named type without name\n");
1561 return type;
1563 hash = hash_ident(name);
1564 nt = xmalloc(sizeof(struct rtype));
1565 nt->name = name;
1566 nt->type = type;
1567 nt->t = t;
1568 nt->next = type_hash[hash];
1569 type_hash[hash] = nt;
1570 return type;
1573 static int is_incomplete(const type_t *t)
1575 return !t->defined && (is_struct(t->type) || is_union(t->type));
1578 static void add_incomplete(type_t *t)
1580 struct typenode *tn = xmalloc(sizeof *tn);
1581 tn->type = t;
1582 list_add_tail(&incomplete_types, &tn->entry);
1585 static void fix_type(type_t *t)
1587 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1588 type_t *ot = t->orig;
1589 fix_type(ot);
1590 t->fields_or_args = ot->fields_or_args;
1591 t->defined = ot->defined;
1595 static void fix_incomplete(void)
1597 struct typenode *tn, *next;
1599 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1600 fix_type(tn->type);
1601 free(tn);
1605 static type_t *reg_typedefs(type_t *type, declarator_list_t *decls, attr_list_t *attrs)
1607 const declarator_t *decl;
1608 int is_str = is_attr(attrs, ATTR_STRING);
1610 if (is_str)
1612 type_t *t = type;
1613 unsigned char c;
1615 while (is_ptr(t))
1616 t = t->ref;
1618 c = t->type;
1619 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1621 decl = LIST_ENTRY( list_head( decls ), const declarator_t, entry );
1622 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1623 decl->var->name);
1627 /* We must generate names for tagless enum, struct or union.
1628 Typedef-ing a tagless enum, struct or union means we want the typedef
1629 to be included in a library whether it has other attributes or not,
1630 hence the public attribute. */
1631 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1632 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1634 if (! is_attr(attrs, ATTR_PUBLIC))
1635 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1636 type->name = gen_name();
1639 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1641 var_t *name = decl->var;
1643 if (name->name) {
1644 type_t *cur;
1646 /* set the attributes to allow set_type to do some checks on them */
1647 name->attrs = attrs;
1648 set_type(name, type, decl, 0);
1649 cur = alias(name->type, name->name);
1650 cur->attrs = attrs;
1652 if (is_incomplete(cur))
1653 add_incomplete(cur);
1654 reg_type(cur, cur->name, 0);
1657 return type;
1660 type_t *find_type(const char *name, int t)
1662 struct rtype *cur = type_hash[hash_ident(name)];
1663 while (cur && (cur->t != t || strcmp(cur->name, name)))
1664 cur = cur->next;
1665 if (!cur) {
1666 error_loc("type '%s' not found\n", name);
1667 return NULL;
1669 return cur->type;
1672 static type_t *find_type2(char *name, int t)
1674 type_t *tp = find_type(name, t);
1675 free(name);
1676 return tp;
1679 int is_type(const char *name)
1681 struct rtype *cur = type_hash[hash_ident(name)];
1682 while (cur && (cur->t || strcmp(cur->name, name)))
1683 cur = cur->next;
1684 if (cur) return TRUE;
1685 return FALSE;
1688 static type_t *get_type(unsigned char type, char *name, int t)
1690 struct rtype *cur = NULL;
1691 type_t *tp;
1692 if (name) {
1693 cur = type_hash[hash_ident(name)];
1694 while (cur && (cur->t != t || strcmp(cur->name, name)))
1695 cur = cur->next;
1697 if (cur) {
1698 free(name);
1699 return cur->type;
1701 tp = make_type(type, NULL);
1702 tp->name = name;
1703 if (!name) return tp;
1704 return reg_type(tp, name, t);
1707 static type_t *get_typev(unsigned char type, var_t *name, int t)
1709 char *sname = NULL;
1710 if (name) {
1711 sname = name->name;
1712 free(name);
1714 return get_type(type, sname, t);
1717 static int get_struct_type(var_list_t *fields)
1719 int has_pointer = 0;
1720 int has_conformance = 0;
1721 int has_variance = 0;
1722 var_t *field;
1724 if (get_padding(fields))
1725 return RPC_FC_BOGUS_STRUCT;
1727 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1729 type_t *t = field->type;
1731 if (is_user_type(t))
1732 return RPC_FC_BOGUS_STRUCT;
1734 if (is_ptr(t))
1737 t = t->ref;
1738 while (is_ptr(t));
1740 switch (t->type)
1742 case RPC_FC_IP:
1743 case RPC_FC_ENCAPSULATED_UNION:
1744 case RPC_FC_NON_ENCAPSULATED_UNION:
1745 case RPC_FC_BOGUS_STRUCT:
1746 return RPC_FC_BOGUS_STRUCT;
1749 has_pointer = 1;
1750 continue;
1753 if (field->type->declarray)
1755 if (is_string_type(field->attrs, field->type))
1757 if (is_conformant_array(field->type))
1758 has_conformance = 1;
1759 has_variance = 1;
1760 continue;
1763 if (is_array(field->type->ref))
1764 return RPC_FC_BOGUS_STRUCT;
1766 if (is_conformant_array(field->type))
1768 has_conformance = 1;
1769 if (field->type->declarray && list_next(fields, &field->entry))
1770 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1771 field->name);
1773 if (field->type->length_is)
1774 has_variance = 1;
1776 t = field->type->ref;
1779 switch (t->type)
1782 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1783 * Simple types don't effect the type of struct.
1784 * A struct containing a simple struct is still a simple struct.
1785 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1787 case 0: /* void pointer */
1788 case RPC_FC_BYTE:
1789 case RPC_FC_CHAR:
1790 case RPC_FC_SMALL:
1791 case RPC_FC_USMALL:
1792 case RPC_FC_WCHAR:
1793 case RPC_FC_SHORT:
1794 case RPC_FC_USHORT:
1795 case RPC_FC_LONG:
1796 case RPC_FC_ULONG:
1797 case RPC_FC_INT3264:
1798 case RPC_FC_UINT3264:
1799 case RPC_FC_HYPER:
1800 case RPC_FC_FLOAT:
1801 case RPC_FC_DOUBLE:
1802 case RPC_FC_STRUCT:
1803 case RPC_FC_ENUM32:
1804 break;
1806 case RPC_FC_RP:
1807 case RPC_FC_UP:
1808 case RPC_FC_FP:
1809 case RPC_FC_OP:
1810 case RPC_FC_CARRAY:
1811 case RPC_FC_CVARRAY:
1812 case RPC_FC_BOGUS_ARRAY:
1813 has_pointer = 1;
1814 break;
1817 * Propagate member attributes
1818 * a struct should be at least as complex as its member
1820 case RPC_FC_CVSTRUCT:
1821 has_conformance = 1;
1822 has_variance = 1;
1823 has_pointer = 1;
1824 break;
1826 case RPC_FC_CPSTRUCT:
1827 has_conformance = 1;
1828 if (list_next( fields, &field->entry ))
1829 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1830 field->name);
1831 has_pointer = 1;
1832 break;
1834 case RPC_FC_CSTRUCT:
1835 has_conformance = 1;
1836 if (list_next( fields, &field->entry ))
1837 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1838 field->name);
1839 break;
1841 case RPC_FC_PSTRUCT:
1842 has_pointer = 1;
1843 break;
1845 default:
1846 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1847 /* fallthru - treat it as complex */
1849 /* as soon as we see one of these these members, it's bogus... */
1850 case RPC_FC_ENCAPSULATED_UNION:
1851 case RPC_FC_NON_ENCAPSULATED_UNION:
1852 case RPC_FC_BOGUS_STRUCT:
1853 case RPC_FC_ENUM16:
1854 return RPC_FC_BOGUS_STRUCT;
1858 if( has_variance )
1860 if ( has_conformance )
1861 return RPC_FC_CVSTRUCT;
1862 else
1863 return RPC_FC_BOGUS_STRUCT;
1865 if( has_conformance && has_pointer )
1866 return RPC_FC_CPSTRUCT;
1867 if( has_conformance )
1868 return RPC_FC_CSTRUCT;
1869 if( has_pointer )
1870 return RPC_FC_PSTRUCT;
1871 return RPC_FC_STRUCT;
1874 /***** constant repository *****/
1876 struct rconst {
1877 char *name;
1878 var_t *var;
1879 struct rconst *next;
1882 struct rconst *const_hash[HASHMAX];
1884 static var_t *reg_const(var_t *var)
1886 struct rconst *nc;
1887 int hash;
1888 if (!var->name) {
1889 error_loc("registering constant without name\n");
1890 return var;
1892 hash = hash_ident(var->name);
1893 nc = xmalloc(sizeof(struct rconst));
1894 nc->name = var->name;
1895 nc->var = var;
1896 nc->next = const_hash[hash];
1897 const_hash[hash] = nc;
1898 return var;
1901 var_t *find_const(const char *name, int f)
1903 struct rconst *cur = const_hash[hash_ident(name)];
1904 while (cur && strcmp(cur->name, name))
1905 cur = cur->next;
1906 if (!cur) {
1907 if (f) error_loc("constant '%s' not found\n", name);
1908 return NULL;
1910 return cur->var;
1913 static void write_libid(const typelib_t *typelib)
1915 const UUID *uuid = get_attrp(typelib->attrs, ATTR_UUID);
1916 write_guid(idfile, "LIBID", typelib->name, uuid);
1919 static void write_clsid(type_t *cls)
1921 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1922 write_guid(idfile, "CLSID", cls->name, uuid);
1925 static void write_diid(type_t *iface)
1927 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1928 write_guid(idfile, "DIID", iface->name, uuid);
1931 static void write_iid(type_t *iface)
1933 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1934 write_guid(idfile, "IID", iface->name, uuid);
1937 static int compute_method_indexes(type_t *iface)
1939 int idx;
1940 func_t *f;
1942 if (iface->ref)
1943 idx = compute_method_indexes(iface->ref);
1944 else
1945 idx = 0;
1947 if (!iface->funcs)
1948 return idx;
1950 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
1951 if (! is_callas(f->def->attrs))
1952 f->idx = idx++;
1954 return idx;
1957 static char *gen_name(void)
1959 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1960 static unsigned long n = 0;
1961 static const char *file_id;
1962 static size_t size;
1963 char *name;
1965 if (! file_id)
1967 char *dst = dup_basename(input_name, ".idl");
1968 file_id = dst;
1970 for (; *dst; ++dst)
1971 if (! isalnum((unsigned char) *dst))
1972 *dst = '_';
1974 size = sizeof format - 7 + strlen(file_id) + 8;
1977 name = xmalloc(size);
1978 sprintf(name, format, file_id, n++);
1979 return name;
1982 struct allowed_attr
1984 unsigned int dce_compatible : 1;
1985 unsigned int acf : 1;
1986 unsigned int on_interface : 1;
1987 unsigned int on_function : 1;
1988 unsigned int on_arg : 1;
1989 unsigned int on_type : 1;
1990 unsigned int on_field : 1;
1991 unsigned int on_library : 1;
1992 unsigned int on_dispinterface : 1;
1993 unsigned int on_module : 1;
1994 unsigned int on_coclass : 1;
1995 const char *display_name;
1998 struct allowed_attr allowed_attr[] =
2000 /* attr { D ACF I Fn ARG T Fi L DI M C <display name> } */
2001 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2002 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2003 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, "async" },
2004 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2005 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2006 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2007 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2008 /* ATTR_CALLCONV */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2009 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2010 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "context_handle" },
2011 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2012 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "default" },
2013 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2014 /* ATTR_DEFAULTVALUE_EXPR */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2015 /* ATTR_DEFAULTVALUE_STRING */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2016 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2017 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2018 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2019 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2020 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2021 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2022 /* ATTR_ENTRY_ORDINAL */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2023 /* ATTR_ENTRY_STRING */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "entry" },
2024 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2025 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "handle" },
2026 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpcontext" },
2027 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2028 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstring" },
2029 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, "helpstringcontext" },
2030 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2031 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2032 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, "id" },
2033 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2034 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "iid_is" },
2035 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2036 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2037 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "in" },
2038 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2039 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "length_is" },
2040 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2041 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "local" },
2042 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2043 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2044 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2045 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2046 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2047 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2048 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "optional" },
2049 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "out" },
2050 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2051 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2052 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propget" },
2053 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propput" },
2054 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2055 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "public" },
2056 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, "range" },
2057 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "readonly" },
2058 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2059 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, "restricted" },
2060 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, "retval" },
2061 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "size_is" },
2062 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, "source" },
2063 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2064 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, "string" },
2065 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, "switch_is" },
2066 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, "switch_type" },
2067 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "transmit_as" },
2068 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 0, 1, 1, 1, 1, "uuid" },
2069 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "v1_enum" },
2070 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2071 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2072 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, "wire_marshal" },
2075 const char *get_attr_display_name(enum attr_type type)
2077 return allowed_attr[type].display_name;
2080 static const attr_list_t *check_iface_attrs(const char *name, const attr_list_t *attrs)
2082 const attr_t *attr;
2083 if (!attrs) return attrs;
2084 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2086 if (!allowed_attr[attr->type].on_interface)
2087 error_loc("inapplicable attribute %s for interface %s\n",
2088 allowed_attr[attr->type].display_name, name);
2090 return attrs;
2093 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2095 const attr_t *attr;
2096 if (!attrs) return attrs;
2097 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2099 if (!allowed_attr[attr->type].on_function)
2100 error_loc("inapplicable attribute %s for function %s\n",
2101 allowed_attr[attr->type].display_name, name);
2103 return attrs;
2106 static void check_arg(var_t *arg)
2108 const type_t *t = arg->type;
2109 const attr_t *attr;
2111 if (t->type == 0 && ! is_var_ptr(arg))
2112 error_loc("argument '%s' has void type\n", arg->name);
2114 if (arg->attrs)
2116 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2118 if (!allowed_attr[attr->type].on_arg)
2119 error_loc("inapplicable attribute %s for argument %s\n",
2120 allowed_attr[attr->type].display_name, arg->name);
2125 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2127 const attr_t *attr;
2128 if (!attrs) return attrs;
2129 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2131 if (!allowed_attr[attr->type].on_type)
2132 error_loc("inapplicable attribute %s for typedef\n",
2133 allowed_attr[attr->type].display_name);
2135 return attrs;
2138 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2140 const attr_t *attr;
2141 if (!attrs) return attrs;
2142 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2144 if (!allowed_attr[attr->type].on_field)
2145 error_loc("inapplicable attribute %s for field %s\n",
2146 allowed_attr[attr->type].display_name, name);
2148 return attrs;
2151 static const attr_list_t *check_library_attrs(const char *name, const attr_list_t *attrs)
2153 const attr_t *attr;
2154 if (!attrs) return attrs;
2155 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2157 if (!allowed_attr[attr->type].on_library)
2158 error_loc("inapplicable attribute %s for library %s\n",
2159 allowed_attr[attr->type].display_name, name);
2161 return attrs;
2164 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2166 const attr_t *attr;
2167 if (!attrs) return attrs;
2168 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2170 if (!allowed_attr[attr->type].on_dispinterface)
2171 error_loc("inapplicable attribute %s for dispinterface %s\n",
2172 allowed_attr[attr->type].display_name, name);
2174 return attrs;
2177 static const attr_list_t *check_module_attrs(const char *name, const attr_list_t *attrs)
2179 const attr_t *attr;
2180 if (!attrs) return attrs;
2181 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2183 if (!allowed_attr[attr->type].on_module)
2184 error_loc("inapplicable attribute %s for module %s\n",
2185 allowed_attr[attr->type].display_name, name);
2187 return attrs;
2190 static const attr_list_t *check_coclass_attrs(const char *name, const attr_list_t *attrs)
2192 const attr_t *attr;
2193 if (!attrs) return attrs;
2194 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2196 if (!allowed_attr[attr->type].on_coclass)
2197 error_loc("inapplicable attribute %s for coclass %s\n",
2198 allowed_attr[attr->type].display_name, name);
2200 return attrs;
2203 static int is_allowed_conf_type(const type_t *type)
2205 switch (type->type)
2207 case RPC_FC_CHAR:
2208 case RPC_FC_SMALL:
2209 case RPC_FC_BYTE:
2210 case RPC_FC_USMALL:
2211 case RPC_FC_WCHAR:
2212 case RPC_FC_SHORT:
2213 case RPC_FC_ENUM16:
2214 case RPC_FC_USHORT:
2215 case RPC_FC_LONG:
2216 case RPC_FC_ENUM32:
2217 case RPC_FC_ULONG:
2218 return TRUE;
2219 default:
2220 return FALSE;
2224 static int is_ptr_guid_type(const type_t *type)
2226 unsigned int align = 0;
2227 for (;;)
2229 if (type->kind == TKIND_ALIAS)
2230 type = type->orig;
2231 else if (is_ptr(type))
2233 type = type->ref;
2234 break;
2236 else
2237 return FALSE;
2239 return (type_memsize(type, &align) == 16);
2242 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2244 expr_t *dim;
2245 struct expr_loc expr_loc;
2246 expr_loc.v = arg;
2247 expr_loc.attr = attr_name;
2248 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2250 if (dim->type != EXPR_VOID)
2252 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2253 if (!is_allowed_conf_type(expr_type))
2254 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2255 attr_name);
2260 static void check_remoting_fields(const var_t *var, type_t *type);
2262 /* checks that properties common to fields and arguments are consistent */
2263 static void check_field_common(const type_t *container_type,
2264 const char *container_name, const var_t *arg)
2266 type_t *type = arg->type;
2267 int is_wire_marshal = 0;
2268 int is_context_handle = 0;
2269 const char *container_type_name = NULL;
2271 if (is_struct(container_type->type))
2272 container_type_name = "struct";
2273 else if (is_union(container_type->type))
2274 container_type_name = "union";
2275 else if (container_type->type == RPC_FC_FUNCTION)
2276 container_type_name = "function";
2278 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2279 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2280 error_loc_info(&arg->loc_info,
2281 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2282 arg->name);
2284 if (is_attr(arg->attrs, ATTR_SIZEIS))
2286 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2287 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2289 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2291 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2292 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2294 if (is_attr(arg->attrs, ATTR_IIDIS))
2296 struct expr_loc expr_loc;
2297 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2298 if (expr->type != EXPR_VOID)
2300 const type_t *expr_type;
2301 expr_loc.v = arg;
2302 expr_loc.attr = "iid_is";
2303 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2304 if (!expr_type || !is_ptr_guid_type(expr_type))
2305 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2308 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2310 struct expr_loc expr_loc;
2311 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2312 if (expr->type != EXPR_VOID)
2314 const type_t *expr_type;
2315 expr_loc.v = arg;
2316 expr_loc.attr = "switch_is";
2317 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2318 if (!expr_type || !is_allowed_conf_type(expr_type))
2319 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2320 expr_loc.attr);
2324 /* get fundamental type for the argument */
2325 for (;;)
2327 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2329 is_wire_marshal = 1;
2330 break;
2332 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2334 is_context_handle = 1;
2335 break;
2337 if (type->kind == TKIND_ALIAS)
2338 type = type->orig;
2339 else if (is_ptr(type) || is_array(type))
2340 type = type->ref;
2341 else
2342 break;
2345 if (type->type == 0 && !is_attr(arg->attrs, ATTR_IIDIS) && !is_wire_marshal && !is_context_handle)
2346 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot derive from void *\n", arg->name, container_type_name, container_name);
2347 else if (type->type == RPC_FC_FUNCTION)
2348 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot be a function pointer\n", arg->name, container_type_name, container_name);
2349 else if (!is_wire_marshal && (is_struct(type->type) || is_union(type->type)))
2350 check_remoting_fields(arg, type);
2353 static void check_remoting_fields(const var_t *var, type_t *type)
2355 const var_t *field;
2356 const var_list_t *fields = NULL;
2358 if (type->checked)
2359 return;
2361 type->checked = TRUE;
2363 if (is_struct(type->type))
2364 fields = type->fields_or_args;
2365 else if (is_union(type->type))
2367 if (type->type == RPC_FC_ENCAPSULATED_UNION)
2369 const var_t *uv = LIST_ENTRY(list_tail(type->fields_or_args), const var_t, entry);
2370 fields = uv->type->fields_or_args;
2372 else
2373 fields = type->fields_or_args;
2376 if (fields) LIST_FOR_EACH_ENTRY( field, type->fields_or_args, const var_t, entry )
2377 if (field->type) check_field_common(type, type->name, field);
2380 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2381 static void check_remoting_args(const func_t *func)
2383 const char *funcname = func->def->name;
2384 const var_t *arg;
2386 if (func->args) LIST_FOR_EACH_ENTRY( arg, func->args, const var_t, entry )
2388 int ptr_level = 0;
2389 const type_t *type = arg->type;
2391 /* get pointer level and fundamental type for the argument */
2392 for (;;)
2394 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2395 break;
2396 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2397 break;
2398 if (type->kind == TKIND_ALIAS)
2399 type = type->orig;
2400 else if (is_ptr(type))
2402 ptr_level++;
2403 type = type->ref;
2405 else
2406 break;
2409 /* check that [out] parameters have enough pointer levels */
2410 if (is_attr(arg->attrs, ATTR_OUT))
2412 if (!is_array(type))
2414 if (!ptr_level)
2415 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2416 if (type->type == RPC_FC_IP && ptr_level == 1)
2417 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2421 check_field_common(func->def->type, funcname, arg);
2425 static void add_explicit_handle_if_necessary(func_t *func)
2427 const var_t* explicit_handle_var;
2428 const var_t* explicit_generic_handle_var = NULL;
2429 const var_t* context_handle_var = NULL;
2431 /* check for a defined binding handle */
2432 explicit_handle_var = get_explicit_handle_var(func);
2433 if (!explicit_handle_var)
2435 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2436 if (!explicit_generic_handle_var)
2438 context_handle_var = get_context_handle_var(func);
2439 if (!context_handle_var)
2441 /* no explicit handle specified so add
2442 * "[in] handle_t IDL_handle" as the first parameter to the
2443 * function */
2444 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2445 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2446 idl_handle->type = find_type("handle_t", 0);
2447 if (!func->def->type->fields_or_args)
2449 func->def->type->fields_or_args = xmalloc( sizeof(*func->def->type->fields_or_args) );
2450 list_init( func->def->type->fields_or_args );
2452 list_add_head( func->def->type->fields_or_args, &idl_handle->entry );
2453 func->args = func->def->type->fields_or_args;
2459 static void check_functions(const type_t *iface)
2461 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE) && iface->funcs)
2463 func_t *func;
2464 LIST_FOR_EACH_ENTRY( func, iface->funcs, func_t, entry )
2465 add_explicit_handle_if_necessary(func);
2467 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2469 const func_t *func;
2470 if (iface->funcs) LIST_FOR_EACH_ENTRY( func, iface->funcs, const func_t, entry )
2472 if (!is_attr(func->def->attrs, ATTR_LOCAL))
2473 check_remoting_args(func);
2478 static void check_all_user_types(const statement_list_t *stmts)
2480 const statement_t *stmt;
2482 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2484 if (stmt->type == STMT_LIBRARY)
2485 check_all_user_types(stmt->u.lib->stmts);
2486 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
2488 const func_t *f;
2489 const func_list_t *fs = stmt->u.type->funcs;
2490 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2491 check_for_additional_prototype_types(f->args);
2496 int is_valid_uuid(const char *s)
2498 int i;
2500 for (i = 0; i < 36; ++i)
2501 if (i == 8 || i == 13 || i == 18 || i == 23)
2503 if (s[i] != '-')
2504 return FALSE;
2506 else
2507 if (!isxdigit(s[i]))
2508 return FALSE;
2510 return s[i] == '\0';
2513 static statement_t *make_statement(enum statement_type type)
2515 statement_t *stmt = xmalloc(sizeof(*stmt));
2516 stmt->type = type;
2517 return stmt;
2520 static statement_t *make_statement_type_decl(type_t *type)
2522 statement_t *stmt = make_statement(STMT_TYPE);
2523 stmt->u.type = type;
2524 return stmt;
2527 static statement_t *make_statement_reference(type_t *type)
2529 statement_t *stmt = make_statement(STMT_TYPEREF);
2530 stmt->u.type = type;
2531 return stmt;
2534 static statement_t *make_statement_init_decl(var_t *var)
2536 statement_t *stmt = make_statement(STMT_INITDECL);
2537 stmt->u.var = var;
2538 return stmt;
2541 static statement_t *make_statement_extern(var_t *var)
2543 statement_t *stmt = make_statement(STMT_EXTERN);
2544 stmt->u.var = var;
2545 return stmt;
2548 static statement_t *make_statement_library(typelib_t *typelib)
2550 statement_t *stmt = make_statement(STMT_LIBRARY);
2551 stmt->u.lib = typelib;
2552 return stmt;
2555 static statement_t *make_statement_cppquote(const char *str)
2557 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2558 stmt->u.str = str;
2559 return stmt;
2562 static statement_t *make_statement_importlib(const char *str)
2564 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2565 stmt->u.str = str;
2566 return stmt;
2569 static statement_t *make_statement_module(type_t *type)
2571 statement_t *stmt = make_statement(STMT_MODULE);
2572 stmt->u.type = type;
2573 return stmt;
2576 static statement_t *process_typedefs(declarator_list_t *decls)
2578 declarator_t *decl, *next;
2579 statement_t *stmt;
2580 type_list_t **type_list;
2582 if (!decls) return NULL;
2584 stmt = make_statement(STMT_TYPEDEF);
2585 stmt->u.type_list = NULL;
2586 type_list = &stmt->u.type_list;
2588 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2590 var_t *var = decl->var;
2591 type_t *type = find_type(var->name, 0);
2592 *type_list = xmalloc(sizeof(type_list_t));
2593 (*type_list)->type = type;
2594 (*type_list)->next = NULL;
2596 if (! parse_only && do_header)
2597 write_typedef(type);
2598 if (in_typelib && type->attrs)
2599 add_typelib_entry(type);
2601 type_list = &(*type_list)->next;
2602 free(decl);
2603 free(var);
2606 return stmt;
2609 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2611 if (!stmt) return list;
2612 if (!list)
2614 list = xmalloc( sizeof(*list) );
2615 list_init( list );
2617 list_add_tail( list, &stmt->entry );
2618 return list;