widl: Move creation of module type into a separate function, type_new_module and...
[wine/hacks.git] / tools / widl / parser.y
blobbc252e84ddda0813ef85186609f085deec22d6dc
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"
42 #include "typetree.h"
44 #if defined(YYBYACC)
45 /* Berkeley yacc (byacc) doesn't seem to know about these */
46 /* Some *BSD supplied versions do define these though */
47 # ifndef YYEMPTY
48 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
49 # endif
50 # ifndef YYLEX
51 # define YYLEX yylex()
52 # endif
54 #elif defined(YYBISON)
55 /* Bison was used for original development */
56 /* #define YYEMPTY -2 */
57 /* #define YYLEX yylex() */
59 #else
60 /* No yacc we know yet */
61 # if !defined(YYEMPTY) || !defined(YYLEX)
62 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
63 # elif defined(__GNUC__) /* gcc defines the #warning directive */
64 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
65 /* #else we just take a chance that it works... */
66 # endif
67 #endif
69 #define YYERROR_VERBOSE
71 unsigned char pointer_default = RPC_FC_UP;
72 static int is_object_interface = FALSE;
74 typedef struct list typelist_t;
75 struct typenode {
76 type_t *type;
77 struct list entry;
80 struct _import_t
82 char *name;
83 int import_performed;
86 typedef struct _decl_spec_t
88 type_t *type;
89 attr_list_t *attrs;
90 enum storage_class stgclass;
91 } decl_spec_t;
93 typelist_t incomplete_types = LIST_INIT(incomplete_types);
95 static void add_incomplete(type_t *t);
96 static void fix_incomplete(void);
97 static void fix_incomplete_types(type_t *complete_type);
99 static str_list_t *append_str(str_list_t *list, char *str);
100 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
101 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
102 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass);
103 static attr_t *make_attr(enum attr_type type);
104 static attr_t *make_attrv(enum attr_type type, unsigned long val);
105 static attr_t *make_attrp(enum attr_type type, void *val);
106 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
107 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
108 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl, int top);
109 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
110 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
111 static ifref_t *make_ifref(type_t *iface);
112 static var_list_t *append_var(var_list_t *list, var_t *var);
113 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
114 static var_t *make_var(char *name);
115 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
116 static declarator_t *make_declarator(var_t *var);
117 static func_list_t *append_func(func_list_t *list, func_t *func);
118 static func_t *make_func(var_t *def);
119 static type_t *make_class(char *name);
120 static type_t *make_safearray(type_t *type);
121 static type_t *make_builtin(char *name);
122 static type_t *make_int(int sign);
123 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
124 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
126 static type_t *type_new_enum(char *name, var_list_t *enums);
127 static type_t *type_new_struct(char *name, int defined, var_list_t *fields);
128 static type_t *type_new_nonencapsulated_union(char *name, var_list_t *fields);
129 static type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases);
131 static type_t *reg_type(type_t *type, const char *name, int t);
132 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
133 static type_t *find_type_or_error(const char *name, int t);
134 static type_t *find_type_or_error2(char *name, int t);
135 static type_t *get_type(unsigned char type, char *name, int t);
137 static var_t *reg_const(var_t *var);
139 static char *gen_name(void);
140 static void check_arg(var_t *arg);
141 static void check_statements(const statement_list_t *stmts, int is_inside_library);
142 static void check_all_user_types(const statement_list_t *stmts);
143 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
144 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
145 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
146 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
147 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
148 static attr_list_t *check_union_attrs(attr_list_t *attrs);
149 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
150 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
151 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
152 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
153 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
154 const char *get_attr_display_name(enum attr_type type);
155 static void add_explicit_handle_if_necessary(var_t *func);
156 static void check_def(const type_t *t);
158 static statement_t *make_statement(enum statement_type type);
159 static statement_t *make_statement_type_decl(type_t *type);
160 static statement_t *make_statement_reference(type_t *type);
161 static statement_t *make_statement_declaration(var_t *var);
162 static statement_t *make_statement_library(typelib_t *typelib);
163 static statement_t *make_statement_cppquote(const char *str);
164 static statement_t *make_statement_importlib(const char *str);
165 static statement_t *make_statement_module(type_t *type);
166 static statement_t *make_statement_typedef(var_list_t *names);
167 static statement_t *make_statement_import(const char *str);
168 static statement_t *make_statement_typedef(var_list_t *names);
169 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
171 #define tsENUM 1
172 #define tsSTRUCT 2
173 #define tsUNION 3
176 %union {
177 attr_t *attr;
178 attr_list_t *attr_list;
179 str_list_t *str_list;
180 expr_t *expr;
181 expr_list_t *expr_list;
182 array_dims_t *array_dims;
183 type_t *type;
184 var_t *var;
185 var_list_t *var_list;
186 declarator_t *declarator;
187 declarator_list_t *declarator_list;
188 func_t *func;
189 func_list_t *func_list;
190 statement_t *statement;
191 statement_list_t *stmt_list;
192 ifref_t *ifref;
193 ifref_list_t *ifref_list;
194 char *str;
195 UUID *uuid;
196 unsigned int num;
197 double dbl;
198 interface_info_t ifinfo;
199 typelib_t *typelib;
200 struct _import_t *import;
201 struct _decl_spec_t *declspec;
202 enum storage_class stgclass;
205 %token <str> aIDENTIFIER
206 %token <str> aKNOWNTYPE
207 %token <num> aNUM aHEXNUM
208 %token <dbl> aDOUBLE
209 %token <str> aSTRING aWSTRING
210 %token <uuid> aUUID
211 %token aEOF
212 %token SHL SHR
213 %token MEMBERPTR
214 %token EQUALITY INEQUALITY
215 %token GREATEREQUAL LESSEQUAL
216 %token LOGICALOR LOGICALAND
217 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
218 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
219 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
220 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
221 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
222 %token tDEFAULT
223 %token tDEFAULTCOLLELEM
224 %token tDEFAULTVALUE
225 %token tDEFAULTVTABLE
226 %token tDISPLAYBIND
227 %token tDISPINTERFACE
228 %token tDLLNAME tDOUBLE tDUAL
229 %token tENDPOINT
230 %token tENTRY tENUM tERRORSTATUST
231 %token tEXPLICITHANDLE tEXTERN
232 %token tFALSE
233 %token tFASTCALL
234 %token tFLOAT
235 %token tHANDLE
236 %token tHANDLET
237 %token tHELPCONTEXT tHELPFILE
238 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
239 %token tHIDDEN
240 %token tHYPER tID tIDEMPOTENT
241 %token tIIDIS
242 %token tIMMEDIATEBIND
243 %token tIMPLICITHANDLE
244 %token tIMPORT tIMPORTLIB
245 %token tIN tIN_LINE tINLINE
246 %token tINPUTSYNC
247 %token tINT tINT64
248 %token tINTERFACE
249 %token tLCID
250 %token tLENGTHIS tLIBRARY
251 %token tLOCAL
252 %token tLONG
253 %token tMETHODS
254 %token tMODULE
255 %token tNONBROWSABLE
256 %token tNONCREATABLE
257 %token tNONEXTENSIBLE
258 %token tNULL
259 %token tOBJECT tODL tOLEAUTOMATION
260 %token tOPTIONAL
261 %token tOUT
262 %token tPASCAL
263 %token tPOINTERDEFAULT
264 %token tPROPERTIES
265 %token tPROPGET tPROPPUT tPROPPUTREF
266 %token tPTR
267 %token tPUBLIC
268 %token tRANGE
269 %token tREADONLY tREF
270 %token tREGISTER
271 %token tREQUESTEDIT
272 %token tRESTRICTED
273 %token tRETVAL
274 %token tSAFEARRAY
275 %token tSHORT
276 %token tSIGNED
277 %token tSINGLE
278 %token tSIZEIS tSIZEOF
279 %token tSMALL
280 %token tSOURCE
281 %token tSTATIC
282 %token tSTDCALL
283 %token tSTRICTCONTEXTHANDLE
284 %token tSTRING tSTRUCT
285 %token tSWITCH tSWITCHIS tSWITCHTYPE
286 %token tTRANSMITAS
287 %token tTRUE
288 %token tTYPEDEF
289 %token tUNION
290 %token tUNIQUE
291 %token tUNSIGNED
292 %token tUUID
293 %token tV1ENUM
294 %token tVARARG
295 %token tVERSION
296 %token tVOID
297 %token tWCHAR tWIREMARSHAL
299 %type <attr> attribute type_qualifier function_specifier
300 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
301 %type <str_list> str_list
302 %type <expr> m_expr expr expr_const expr_int_const array
303 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
304 %type <ifinfo> interfacehdr
305 %type <stgclass> storage_cls_spec
306 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
307 %type <type> inherit interface interfacedef interfacedec
308 %type <type> dispinterface dispinterfacehdr dispinterfacedef
309 %type <type> module modulehdr moduledef
310 %type <type> base_type int_std
311 %type <type> enumdef structdef uniondef typedecl
312 %type <type> type
313 %type <ifref> coclass_int
314 %type <ifref_list> coclass_ints
315 %type <var> arg ne_union_field union_field s_field case enum declaration
316 %type <var_list> m_args no_args args fields ne_union_fields cases enums enum_list dispint_props field
317 %type <var> m_ident ident
318 %type <declarator> declarator direct_declarator init_declarator
319 %type <declarator_list> declarator_list
320 %type <func> funcdef
321 %type <type> coclass coclasshdr coclassdef
322 %type <num> pointer_type version
323 %type <str> libraryhdr callconv cppquote importlib import t_ident
324 %type <uuid> uuid_string
325 %type <import> import_start
326 %type <typelib> library_start librarydef
327 %type <statement> statement typedef
328 %type <stmt_list> gbl_statements imp_statements int_statements dispint_meths
330 %left ','
331 %right '?' ':'
332 %left LOGICALOR
333 %left LOGICALAND
334 %left '|'
335 %left '^'
336 %left '&'
337 %left EQUALITY INEQUALITY
338 %left '<' '>' LESSEQUAL GREATEREQUAL
339 %left SHL SHR
340 %left '-' '+'
341 %left '*' '/' '%'
342 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
343 %left '.' MEMBERPTR '[' ']'
347 input: gbl_statements { fix_incomplete();
348 check_statements($1, FALSE);
349 check_all_user_types($1);
350 write_header($1);
351 write_id_data($1);
352 write_proxies($1);
353 write_client($1);
354 write_server($1);
355 write_dlldata($1);
356 write_local_stubs($1);
360 gbl_statements: { $$ = NULL; }
361 | gbl_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
362 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
363 | gbl_statements coclass ';' { $$ = $1;
364 reg_type($2, $2->name, 0);
366 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
367 reg_type($2, $2->name, 0);
369 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
370 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
371 | gbl_statements statement { $$ = append_statement($1, $2); }
374 imp_statements: { $$ = NULL; }
375 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
376 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
377 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); }
378 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
379 reg_type($2, $2->name, 0);
381 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
382 | imp_statements statement { $$ = append_statement($1, $2); }
383 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
384 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
387 int_statements: { $$ = NULL; }
388 | int_statements statement { $$ = append_statement($1, $2); }
391 semicolon_opt:
392 | ';'
395 statement:
396 cppquote { $$ = make_statement_cppquote($1); }
397 | typedecl ';' { $$ = make_statement_type_decl($1); }
398 | declaration ';' { $$ = make_statement_declaration($1); }
399 | import { $$ = make_statement_import($1); }
400 | typedef ';' { $$ = $1; }
403 typedecl:
404 enumdef
405 | structdef
406 | uniondef
407 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
408 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
409 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
412 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
414 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
415 $$ = xmalloc(sizeof(struct _import_t));
416 $$->name = $2;
417 $$->import_performed = do_import($2);
418 if (!$$->import_performed) yychar = aEOF;
422 import: import_start imp_statements aEOF { $$ = $1->name;
423 if ($1->import_performed) pop_import();
424 free($1);
428 importlib: tIMPORTLIB '(' aSTRING ')'
429 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
432 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
434 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
435 if (!parse_only) start_typelib($$);
438 librarydef: library_start imp_statements '}'
439 semicolon_opt { $$ = $1;
440 $$->stmts = $2;
441 if (!parse_only) end_typelib();
445 m_args: { $$ = NULL; }
446 | args
449 no_args: tVOID { $$ = NULL; }
452 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
453 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
454 | no_args
457 /* split into two rules to get bison to resolve a tVOID conflict */
458 arg: attributes decl_spec declarator { $$ = $3->var;
459 $$->attrs = $1;
460 if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
461 error_loc("invalid storage class for function parameter\n");
462 set_type($$, $2, $3, TRUE);
463 free($3);
465 | decl_spec declarator { $$ = $2->var;
466 if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
467 error_loc("invalid storage class for function parameter\n");
468 set_type($$, $1, $2, TRUE);
469 free($2);
473 array: '[' m_expr ']' { $$ = $2; }
474 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
477 m_attributes: { $$ = NULL; }
478 | attributes
481 attributes:
482 '[' attrib_list ']' { $$ = $2; }
485 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
486 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
487 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
490 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
491 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
494 attribute: { $$ = NULL; }
495 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
496 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
497 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
498 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
499 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
500 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
501 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
502 | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
503 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
504 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
505 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
506 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
507 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
508 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
509 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
510 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
511 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
512 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
513 | tDUAL { $$ = make_attr(ATTR_DUAL); }
514 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
515 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
516 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
517 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
518 | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
519 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
520 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
521 | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
522 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
523 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
524 | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
525 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
526 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
527 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
528 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
529 | tIN { $$ = make_attr(ATTR_IN); }
530 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
531 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
532 | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
533 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
534 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
535 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
536 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
537 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
538 | tODL { $$ = make_attr(ATTR_ODL); }
539 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
540 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
541 | tOUT { $$ = make_attr(ATTR_OUT); }
542 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
543 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
544 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
545 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
546 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
547 | tRANGE '(' expr_int_const ',' expr_int_const ')'
548 { expr_list_t *list = append_expr( NULL, $3 );
549 list = append_expr( list, $5 );
550 $$ = make_attrp(ATTR_RANGE, list); }
551 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
552 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
553 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
554 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
555 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
556 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
557 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
558 | tSTRING { $$ = make_attr(ATTR_STRING); }
559 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
560 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
561 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
562 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
563 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
564 | tVARARG { $$ = make_attr(ATTR_VARARG); }
565 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
566 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
567 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
570 uuid_string:
571 aUUID
572 | aSTRING { if (!is_valid_uuid($1))
573 error_loc("invalid UUID: %s\n", $1);
574 $$ = parse_uuid($1); }
577 callconv: tCDECL { $$ = $<str>1; }
578 | tFASTCALL { $$ = $<str>1; }
579 | tPASCAL { $$ = $<str>1; }
580 | tSTDCALL { $$ = $<str>1; }
583 cases: { $$ = NULL; }
584 | cases case { $$ = append_var( $1, $2 ); }
587 case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
588 $$ = $4; if (!$$) $$ = make_var(NULL);
589 $$->attrs = append_attr( $$->attrs, a );
591 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
592 $$ = $3; if (!$$) $$ = make_var(NULL);
593 $$->attrs = append_attr( $$->attrs, a );
597 enums: { $$ = NULL; }
598 | enum_list ',' { $$ = $1; }
599 | enum_list
602 enum_list: enum { if (!$1->eval)
603 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
604 $$ = append_var( NULL, $1 );
606 | enum_list ',' enum { if (!$3->eval)
608 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
609 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
611 $$ = append_var( $1, $3 );
615 enum: ident '=' expr_int_const { $$ = reg_const($1);
616 $$->eval = $3;
617 $$->type = make_int(0);
619 | ident { $$ = reg_const($1);
620 $$->type = make_int(0);
624 enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, $4); }
627 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
628 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
632 exprs: { $$ = make_expr(EXPR_VOID); }
633 | expr_list
636 expr_list: expr
637 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
641 m_expr: { $$ = make_expr(EXPR_VOID); }
642 | expr
645 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
646 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
647 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
648 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
649 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
650 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
651 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
652 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
653 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
654 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
655 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
656 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
657 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
658 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
659 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
660 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
661 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
662 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
663 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
664 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
665 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
666 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
667 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
668 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
669 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
670 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
671 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
672 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
673 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
674 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
675 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
676 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
677 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
678 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
679 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
680 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
681 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
682 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
683 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
684 | '(' expr ')' { $$ = $2; }
687 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
688 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
691 expr_int_const: expr { $$ = $1;
692 if (!$$->is_const)
693 error_loc("expression is not an integer constant\n");
697 expr_const: expr { $$ = $1;
698 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
699 error_loc("expression is not constant\n");
703 fields: { $$ = NULL; }
704 | fields field { $$ = append_var_list($1, $2); }
707 field: m_attributes decl_spec declarator_list ';'
708 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
709 check_field_attrs(first, $1);
710 $$ = set_var_types($1, $2, $3);
712 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
713 v->type = $2; v->attrs = $1;
714 $$ = append_var(NULL, v);
718 ne_union_field:
719 s_field ';' { $$ = $1; }
720 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
723 ne_union_fields: { $$ = NULL; }
724 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
727 union_field:
728 s_field ';' { $$ = $1; }
729 | ';' { $$ = NULL; }
732 s_field: m_attributes decl_spec declarator { $$ = $3->var;
733 $$->attrs = check_field_attrs($$->name, $1);
734 set_type($$, $2, $3, FALSE);
735 free($3);
739 funcdef:
740 m_attributes decl_spec declarator { var_t *v = $3->var;
741 v->attrs = check_function_attrs(v->name, $1);
742 set_type(v, $2, $3, FALSE);
743 free($3);
744 $$ = make_func(v);
748 declaration:
749 attributes decl_spec init_declarator
750 { $$ = $3->var;
751 $$->attrs = $1;
752 set_type($$, $2, $3, FALSE);
753 free($3);
755 | decl_spec init_declarator { $$ = $2->var;
756 set_type($$, $1, $2, FALSE);
757 free($2);
761 m_ident: { $$ = NULL; }
762 | ident
765 t_ident: { $$ = NULL; }
766 | aIDENTIFIER { $$ = $1; }
767 | aKNOWNTYPE { $$ = $1; }
770 ident: aIDENTIFIER { $$ = make_var($1); }
771 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
772 | aKNOWNTYPE { $$ = make_var($<str>1); }
775 base_type: tBYTE { $$ = make_builtin($<str>1); }
776 | tWCHAR { $$ = make_builtin($<str>1); }
777 | int_std
778 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
779 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
780 switch ($$->type) {
781 case RPC_FC_CHAR: break;
782 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
783 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
784 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
785 case RPC_FC_HYPER:
786 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
788 $$ = type_new_alias($$, "MIDL_uhyper");
789 $$->sign = 0;
791 break;
792 default: break;
795 | tUNSIGNED { $$ = make_int(-1); }
796 | tFLOAT { $$ = make_builtin($<str>1); }
797 | tSINGLE { $$ = find_type("float", 0); }
798 | tDOUBLE { $$ = make_builtin($<str>1); }
799 | tBOOLEAN { $$ = make_builtin($<str>1); }
800 | tERRORSTATUST { $$ = make_builtin($<str>1); }
801 | tHANDLET { $$ = make_builtin($<str>1); }
804 m_int:
805 | tINT
808 int_std: tINT { $$ = make_builtin($<str>1); }
809 | tSHORT m_int { $$ = make_builtin($<str>1); }
810 | tSMALL { $$ = make_builtin($<str>1); }
811 | tLONG m_int { $$ = make_builtin($<str>1); }
812 | tHYPER m_int { $$ = make_builtin($<str>1); }
813 | tINT64 { $$ = make_builtin($<str>1); }
814 | tCHAR { $$ = make_builtin($<str>1); }
817 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
818 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
819 if ($$->kind != TKIND_COCLASS)
820 error_loc("%s was not declared a coclass at %s:%d\n",
821 $2, $$->loc_info.input_name,
822 $$->loc_info.line_number);
826 coclasshdr: attributes coclass { $$ = $2;
827 check_def($$);
828 $$->attrs = check_coclass_attrs($2->name, $1);
832 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
833 { $$ = $1;
834 $$->ifaces = $3;
835 $$->defined = TRUE;
839 coclass_ints: { $$ = NULL; }
840 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
843 coclass_int:
844 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
847 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
848 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_DISPATCH; }
851 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
852 is_object_interface = TRUE;
853 $$ = $2;
854 check_def($$);
855 attrs = make_attr(ATTR_DISPINTERFACE);
856 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
857 $$->defined = TRUE;
861 dispint_props: tPROPERTIES ':' { $$ = NULL; }
862 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
865 dispint_meths: tMETHODS ':' { $$ = NULL; }
866 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
869 dispinterfacedef: dispinterfacehdr '{'
870 dispint_props
871 dispint_meths
872 '}' { $$ = $1;
873 type_dispinterface_define($$, $3, $4);
875 | dispinterfacehdr
876 '{' interface ';' '}' { $$ = $1;
877 type_dispinterface_define_from_iface($$, $3);
881 inherit: { $$ = NULL; }
882 | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
885 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
886 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
889 interfacehdr: attributes interface { $$.interface = $2;
890 $$.old_pointer_default = pointer_default;
891 if (is_attr($1, ATTR_POINTERDEFAULT))
892 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
893 is_object_interface = is_object($1);
894 check_def($2);
895 $2->attrs = check_iface_attrs($2->name, $1);
896 $2->defined = TRUE;
900 interfacedef: interfacehdr inherit
901 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
902 type_interface_define($$, $2, $4);
903 pointer_default = $1.old_pointer_default;
905 /* MIDL is able to import the definition of a base class from inside the
906 * definition of a derived class, I'll try to support it with this rule */
907 | interfacehdr ':' aIDENTIFIER
908 '{' import int_statements '}'
909 semicolon_opt { $$ = $1.interface;
910 type_interface_define($$, find_type_or_error2($3, 0), $6);
911 pointer_default = $1.old_pointer_default;
913 | dispinterfacedef semicolon_opt { $$ = $1; }
916 interfacedec:
917 interface ';' { $$ = $1; }
918 | dispinterface ';' { $$ = $1; }
921 module: tMODULE aIDENTIFIER { $$ = type_new_module($2); }
922 | tMODULE aKNOWNTYPE { $$ = type_new_module($2); }
925 modulehdr: attributes module { $$ = $2;
926 $$->attrs = check_module_attrs($2->name, $1);
930 moduledef: modulehdr '{' int_statements '}'
931 semicolon_opt { $$ = $1;
932 type_module_define($$, $3);
936 storage_cls_spec:
937 tEXTERN { $$ = STG_EXTERN; }
938 | tSTATIC { $$ = STG_STATIC; }
939 | tREGISTER { $$ = STG_REGISTER; }
942 function_specifier:
943 tINLINE { $$ = make_attr(ATTR_INLINE); }
946 type_qualifier:
947 tCONST { $$ = make_attr(ATTR_CONST); }
950 m_type_qual_list: { $$ = NULL; }
951 | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
954 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
955 | decl_spec_no_type type m_decl_spec_no_type
956 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
959 m_decl_spec_no_type: { $$ = NULL; }
960 | decl_spec_no_type
963 decl_spec_no_type:
964 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
965 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
966 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
969 declarator:
970 '*' m_type_qual_list declarator %prec PPTR
971 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(NULL, $2)); }
972 | callconv declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
973 | direct_declarator
976 direct_declarator:
977 ident { $$ = make_declarator($1); }
978 | '(' declarator ')' { $$ = $2; }
979 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
980 | direct_declarator '(' m_args ')' { $$ = $1;
981 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
982 $$->type = NULL;
986 declarator_list:
987 declarator { $$ = append_declarator( NULL, $1 ); }
988 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
991 init_declarator:
992 declarator { $$ = $1; }
993 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
996 pointer_type:
997 tREF { $$ = RPC_FC_RP; }
998 | tUNIQUE { $$ = RPC_FC_UP; }
999 | tPTR { $$ = RPC_FC_FP; }
1002 structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, TRUE, $4); }
1005 type: tVOID { $$ = find_type_or_error("void", 0); }
1006 | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
1007 | base_type { $$ = $1; }
1008 | enumdef { $$ = $1; }
1009 | tENUM aIDENTIFIER { $$ = find_type_or_error2($2, tsENUM); }
1010 | structdef { $$ = $1; }
1011 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, FALSE, NULL); }
1012 | uniondef { $$ = $1; }
1013 | tUNION aIDENTIFIER { $$ = find_type_or_error2($2, tsUNION); }
1014 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
1017 typedef: tTYPEDEF m_attributes decl_spec declarator_list
1018 { reg_typedefs($3, $4, check_typedef_attrs($2));
1019 $$ = make_statement_typedef($4);
1023 uniondef: tUNION t_ident '{' ne_union_fields '}'
1024 { $$ = type_new_nonencapsulated_union($2, $4); }
1025 | tUNION t_ident
1026 tSWITCH '(' s_field ')'
1027 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1030 version:
1031 aNUM { $$ = MAKEVERSION($1, 0); }
1032 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1037 static void decl_builtin(const char *name, unsigned char type)
1039 type_t *t = make_type(type, NULL);
1040 t->name = xstrdup(name);
1041 reg_type(t, name, 0);
1044 static type_t *make_builtin(char *name)
1046 /* NAME is strdup'd in the lexer */
1047 type_t *t = duptype(find_type_or_error(name, 0), 0);
1048 t->name = name;
1049 return t;
1052 static type_t *make_int(int sign)
1054 type_t *t = duptype(find_type_or_error("int", 0), 1);
1056 t->sign = sign;
1057 if (sign < 0)
1058 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
1060 return t;
1063 void init_types(void)
1065 decl_builtin("void", 0);
1066 decl_builtin("byte", RPC_FC_BYTE);
1067 decl_builtin("wchar_t", RPC_FC_WCHAR);
1068 decl_builtin("int", RPC_FC_LONG); /* win32 */
1069 decl_builtin("short", RPC_FC_SHORT);
1070 decl_builtin("small", RPC_FC_SMALL);
1071 decl_builtin("long", RPC_FC_LONG);
1072 decl_builtin("hyper", RPC_FC_HYPER);
1073 decl_builtin("__int64", RPC_FC_HYPER);
1074 decl_builtin("char", RPC_FC_CHAR);
1075 decl_builtin("float", RPC_FC_FLOAT);
1076 decl_builtin("double", RPC_FC_DOUBLE);
1077 decl_builtin("boolean", RPC_FC_BYTE);
1078 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
1079 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
1082 static str_list_t *append_str(str_list_t *list, char *str)
1084 struct str_list_entry_t *entry;
1086 if (!str) return list;
1087 if (!list)
1089 list = xmalloc( sizeof(*list) );
1090 list_init( list );
1092 entry = xmalloc( sizeof(*entry) );
1093 entry->str = str;
1094 list_add_tail( list, &entry->entry );
1095 return list;
1098 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1100 attr_t *attr_existing;
1101 if (!attr) return list;
1102 if (!list)
1104 list = xmalloc( sizeof(*list) );
1105 list_init( list );
1107 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1108 if (attr_existing->type == attr->type)
1110 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1111 /* use the last attribute, like MIDL does */
1112 list_remove(&attr_existing->entry);
1113 break;
1115 list_add_tail( list, &attr->entry );
1116 return list;
1119 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1121 attr_t *attr;
1122 if (!src) return dst;
1123 LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1124 if (attr->type == type)
1126 list_remove(&attr->entry);
1127 return append_attr(dst, attr);
1129 return dst;
1132 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1134 struct list *entry;
1136 if (!old_list) return new_list;
1138 while ((entry = list_head(old_list)))
1140 attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1141 list_remove(entry);
1142 new_list = append_attr(new_list, attr);
1144 return new_list;
1147 static attr_list_t *dupattrs(const attr_list_t *list)
1149 attr_list_t *new_list;
1150 const attr_t *attr;
1152 if (!list) return NULL;
1154 new_list = xmalloc( sizeof(*list) );
1155 list_init( new_list );
1156 LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1158 attr_t *new_attr = xmalloc(sizeof(*new_attr));
1159 *new_attr = *attr;
1160 list_add_tail(new_list, &new_attr->entry);
1162 return new_list;
1165 static decl_spec_t *make_decl_spec(type_t *type, decl_spec_t *left, decl_spec_t *right, attr_t *attr, enum storage_class stgclass)
1167 decl_spec_t *declspec = left ? left : right;
1168 if (!declspec)
1170 declspec = xmalloc(sizeof(*declspec));
1171 declspec->type = NULL;
1172 declspec->attrs = NULL;
1173 declspec->stgclass = STG_NONE;
1175 declspec->type = type;
1176 if (left && declspec != left)
1178 declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1179 if (declspec->stgclass == STG_NONE)
1180 declspec->stgclass = left->stgclass;
1181 else if (left->stgclass != STG_NONE)
1182 error_loc("only one storage class can be specified\n");
1183 assert(!left->type);
1184 free(left);
1186 if (right && declspec != right)
1188 declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1189 if (declspec->stgclass == STG_NONE)
1190 declspec->stgclass = right->stgclass;
1191 else if (right->stgclass != STG_NONE)
1192 error_loc("only one storage class can be specified\n");
1193 assert(!right->type);
1194 free(right);
1197 declspec->attrs = append_attr(declspec->attrs, attr);
1198 if (declspec->stgclass == STG_NONE)
1199 declspec->stgclass = stgclass;
1200 else if (stgclass != STG_NONE)
1201 error_loc("only one storage class can be specified\n");
1203 /* apply attributes to type */
1204 if (type && declspec->attrs)
1206 attr_list_t *attrs;
1207 declspec->type = duptype(type, 1);
1208 attrs = dupattrs(type->attrs);
1209 declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1210 declspec->attrs = NULL;
1213 return declspec;
1216 static attr_t *make_attr(enum attr_type type)
1218 attr_t *a = xmalloc(sizeof(attr_t));
1219 a->type = type;
1220 a->u.ival = 0;
1221 return a;
1224 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1226 attr_t *a = xmalloc(sizeof(attr_t));
1227 a->type = type;
1228 a->u.ival = val;
1229 return a;
1232 static attr_t *make_attrp(enum attr_type type, void *val)
1234 attr_t *a = xmalloc(sizeof(attr_t));
1235 a->type = type;
1236 a->u.pval = val;
1237 return a;
1240 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1242 if (!expr) return list;
1243 if (!list)
1245 list = xmalloc( sizeof(*list) );
1246 list_init( list );
1248 list_add_tail( list, &expr->entry );
1249 return list;
1252 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1254 if (!expr) return list;
1255 if (!list)
1257 list = xmalloc( sizeof(*list) );
1258 list_init( list );
1260 list_add_tail( list, &expr->entry );
1261 return list;
1264 static struct list type_pool = LIST_INIT(type_pool);
1265 typedef struct
1267 type_t data;
1268 struct list link;
1269 } type_pool_node_t;
1271 type_t *alloc_type(void)
1273 type_pool_node_t *node = xmalloc(sizeof *node);
1274 list_add_tail(&type_pool, &node->link);
1275 return &node->data;
1278 void set_all_tfswrite(int val)
1280 type_pool_node_t *node;
1281 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1282 node->data.tfswrite = val;
1285 void clear_all_offsets(void)
1287 type_pool_node_t *node;
1288 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1289 node->data.typestring_offset = node->data.ptrdesc = 0;
1292 type_t *make_type(unsigned char type, type_t *ref)
1294 type_t *t = alloc_type();
1295 t->name = NULL;
1296 t->kind = TKIND_PRIMITIVE;
1297 t->type = type;
1298 t->ref = ref;
1299 t->attrs = NULL;
1300 t->orig = NULL;
1301 memset(&t->details, 0, sizeof(t->details));
1302 t->ifaces = NULL;
1303 t->typestring_offset = 0;
1304 t->ptrdesc = 0;
1305 t->declarray = FALSE;
1306 t->ignore = (parse_only != 0);
1307 t->sign = 0;
1308 t->defined = FALSE;
1309 t->written = FALSE;
1310 t->user_types_registered = FALSE;
1311 t->tfswrite = FALSE;
1312 t->checked = FALSE;
1313 t->typelib_idx = -1;
1314 init_loc_info(&t->loc_info);
1315 return t;
1318 static type_t *type_new_enum(char *name, var_list_t *enums)
1320 type_t *t = get_type(RPC_FC_ENUM16, name, tsENUM);
1321 t->kind = TKIND_ENUM;
1322 if (enums)
1324 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
1325 t->details.enumeration->enums = enums;
1327 else
1328 t->details.enumeration = NULL;
1329 t->defined = TRUE;
1330 return t;
1333 static type_t *type_new_struct(char *name, int defined, var_list_t *fields)
1335 type_t *tag_type = name ? find_type(name, tsSTRUCT) : NULL;
1336 type_t *t = make_type(RPC_FC_STRUCT, NULL);
1337 t->name = name;
1338 t->kind = TKIND_RECORD;
1339 if (defined || (tag_type && tag_type->details.structure))
1341 if (tag_type && tag_type->details.structure)
1343 t->details.structure = tag_type->details.structure;
1344 t->type = tag_type->type;
1346 else if (defined)
1348 t->details.structure = xmalloc(sizeof(*t->details.structure));
1349 t->details.structure->fields = fields;
1350 t->defined = TRUE;
1353 if (name)
1355 if (fields)
1356 reg_type(t, name, tsSTRUCT);
1357 else
1358 add_incomplete(t);
1360 return t;
1363 static type_t *type_new_nonencapsulated_union(char *name, var_list_t *fields)
1365 type_t *t = get_type(RPC_FC_NON_ENCAPSULATED_UNION, name, tsUNION);
1366 t->kind = TKIND_UNION;
1367 t->details.structure = xmalloc(sizeof(*t->details.structure));
1368 t->details.structure->fields = fields;
1369 t->defined = TRUE;
1370 return t;
1373 static type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
1375 type_t *t = get_type(RPC_FC_ENCAPSULATED_UNION, name, tsUNION);
1376 t->kind = TKIND_UNION;
1377 if (!union_field) union_field = make_var( xstrdup("tagged_union") );
1378 union_field->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
1379 union_field->type->kind = TKIND_UNION;
1380 union_field->type->details.structure = xmalloc(sizeof(*union_field->type->details.structure));
1381 union_field->type->details.structure->fields = cases;
1382 union_field->type->defined = TRUE;
1383 t->details.structure = xmalloc(sizeof(*t->details.structure));
1384 t->details.structure->fields = append_var( NULL, switch_field );
1385 t->details.structure->fields = append_var( t->details.structure->fields, union_field );
1386 t->defined = TRUE;
1387 return t;
1390 static void type_function_add_head_arg(type_t *type, var_t *arg)
1392 if (!type->details.function->args)
1394 type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
1395 list_init( type->details.function->args );
1397 list_add_head( type->details.function->args, &arg->entry );
1400 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1402 type_t *ptrchain_type;
1403 if (!ptrchain)
1404 return type;
1405 for (ptrchain_type = ptrchain; ptrchain_type->ref; ptrchain_type = ptrchain_type->ref)
1407 ptrchain_type->ref = type;
1408 return ptrchain;
1411 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl,
1412 int top)
1414 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1415 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1416 int sizeless, has_varconf;
1417 expr_t *dim;
1418 type_t *atype, **ptype;
1419 array_dims_t *arr = decl ? decl->array : NULL;
1420 type_t *func_type = decl ? decl->func_type : NULL;
1421 type_t *type = decl_spec->type;
1423 if (is_attr(type->attrs, ATTR_INLINE))
1425 if (!func_type)
1426 error_loc("inline attribute applied to non-function type\n");
1427 else
1429 type_t *t;
1430 /* move inline attribute from return type node to function node */
1431 for (t = func_type; is_ptr(t); t = t->ref)
1433 t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1437 /* add type onto the end of the pointers in pident->type */
1438 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1439 v->stgclass = decl_spec->stgclass;
1441 /* the highest level of pointer specified should default to the var's ptr attr
1442 * or (RPC_FC_RP if not specified and it's a top level ptr), not
1443 * pointer_default so we need to fix that up here */
1444 if (!arr)
1446 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1447 const type_t *ptr = NULL;
1448 /* pointer attributes on the left side of the type belong to the function
1449 * pointer, if one is being declared */
1450 type_t **pt = func_type ? &func_type : &v->type;
1451 for (ptr = *pt; ptr && !ptr_attr; )
1453 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1454 if (!ptr_attr && type_is_alias(ptr))
1455 ptr = ptr->orig;
1456 else
1457 break;
1459 if (ptr && is_ptr(ptr) && (ptr_attr || top))
1461 /* duplicate type to avoid changing original type */
1462 *pt = duptype(*pt, 1);
1463 (*pt)->type = ptr_attr ? ptr_attr : RPC_FC_RP;
1465 else if (ptr_attr)
1466 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1469 if (is_attr(v->attrs, ATTR_STRING) && !is_ptr(v->type) && !arr)
1470 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1471 v->name);
1473 if (is_attr(v->attrs, ATTR_V1ENUM))
1475 if (v->type->type == RPC_FC_ENUM16)
1476 v->type->type = RPC_FC_ENUM32;
1477 else
1478 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1481 sizeless = FALSE;
1482 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1484 if (sizeless)
1485 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1487 if (dim->is_const)
1489 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1491 else
1493 sizeless = TRUE;
1494 v->type = make_type(RPC_FC_CARRAY, v->type);
1497 v->type->declarray = TRUE;
1498 v->type->details.array.dim = dim->cval;
1501 ptype = &v->type;
1502 has_varconf = FALSE;
1503 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1505 if (dim->type != EXPR_VOID)
1507 has_varconf = TRUE;
1508 atype = *ptype = duptype(*ptype, 0);
1510 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1511 error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1513 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1514 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1516 atype->type = RPC_FC_CARRAY;
1517 atype->details.array.size_is = dim;
1520 ptype = &(*ptype)->ref;
1521 if (*ptype == NULL)
1522 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1525 ptype = &v->type;
1526 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1528 if (dim->type != EXPR_VOID)
1530 has_varconf = TRUE;
1531 atype = *ptype = duptype(*ptype, 0);
1533 if (atype->type == RPC_FC_SMFARRAY)
1534 atype->type = RPC_FC_SMVARRAY;
1535 else if (atype->type == RPC_FC_LGFARRAY)
1536 atype->type = RPC_FC_LGVARRAY;
1537 else if (atype->type == RPC_FC_CARRAY)
1538 atype->type = RPC_FC_CVARRAY;
1539 else
1540 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1542 atype->details.array.length_is = dim;
1545 ptype = &(*ptype)->ref;
1546 if (*ptype == NULL)
1547 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1550 if (has_varconf && !last_array(v->type))
1552 ptype = &v->type;
1553 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1555 *ptype = duptype(*ptype, 0);
1556 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1560 /* v->type is currently pointing to the type on the left-side of the
1561 * declaration, so we need to fix this up so that it is the return type of the
1562 * function and make v->type point to the function side of the declaration */
1563 if (func_type)
1565 type_t *ft, *t;
1566 type_t *return_type = v->type;
1567 v->type = func_type;
1568 for (ft = v->type; is_ptr(ft); ft = ft->ref)
1570 assert(ft->type == RPC_FC_FUNCTION);
1571 ft->ref = return_type;
1572 /* move calling convention attribute, if present, from pointer nodes to
1573 * function node */
1574 for (t = v->type; is_ptr(t); t = t->ref)
1575 ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1576 if (is_object_interface && !is_attr(ft->attrs, ATTR_CALLCONV))
1578 static char *stdmethodcalltype;
1579 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1580 ft->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1583 else
1585 type_t *t;
1586 for (t = v->type; is_ptr(t); t = t->ref)
1587 if (is_attr(t->attrs, ATTR_CALLCONV))
1588 error_loc("calling convention applied to non-function-pointer type\n");
1592 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1594 declarator_t *decl, *next;
1595 var_list_t *var_list = NULL;
1597 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1599 var_t *var = decl->var;
1601 var->attrs = attrs;
1602 set_type(var, decl_spec, decl, 0);
1603 var_list = append_var(var_list, var);
1604 free(decl);
1606 return var_list;
1609 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1611 if (!iface) return list;
1612 if (!list)
1614 list = xmalloc( sizeof(*list) );
1615 list_init( list );
1617 list_add_tail( list, &iface->entry );
1618 return list;
1621 static ifref_t *make_ifref(type_t *iface)
1623 ifref_t *l = xmalloc(sizeof(ifref_t));
1624 l->iface = iface;
1625 l->attrs = NULL;
1626 return l;
1629 static var_list_t *append_var(var_list_t *list, var_t *var)
1631 if (!var) return list;
1632 if (!list)
1634 list = xmalloc( sizeof(*list) );
1635 list_init( list );
1637 list_add_tail( list, &var->entry );
1638 return list;
1641 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1643 if (!vars) return list;
1644 if (!list)
1646 list = xmalloc( sizeof(*list) );
1647 list_init( list );
1649 list_move_tail( list, vars );
1650 return list;
1653 static var_t *make_var(char *name)
1655 var_t *v = xmalloc(sizeof(var_t));
1656 v->name = name;
1657 v->type = NULL;
1658 v->attrs = NULL;
1659 v->eval = NULL;
1660 v->stgclass = STG_NONE;
1661 init_loc_info(&v->loc_info);
1662 return v;
1665 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1667 if (!d) return list;
1668 if (!list) {
1669 list = xmalloc(sizeof(*list));
1670 list_init(list);
1672 list_add_tail(list, &d->entry);
1673 return list;
1676 static declarator_t *make_declarator(var_t *var)
1678 declarator_t *d = xmalloc(sizeof(*d));
1679 d->var = var;
1680 d->type = NULL;
1681 d->func_type = NULL;
1682 d->array = NULL;
1683 return d;
1686 static func_list_t *append_func(func_list_t *list, func_t *func)
1688 if (!func) return list;
1689 if (!list)
1691 list = xmalloc( sizeof(*list) );
1692 list_init( list );
1694 list_add_tail( list, &func->entry );
1695 return list;
1698 static func_t *make_func(var_t *def)
1700 func_t *f = xmalloc(sizeof(func_t));
1701 f->def = def;
1702 return f;
1705 static type_t *make_class(char *name)
1707 type_t *c = make_type(RPC_FC_COCLASS, NULL);
1708 c->name = name;
1709 c->kind = TKIND_COCLASS;
1710 return c;
1713 static type_t *make_safearray(type_t *type)
1715 type_t *sa = find_type_or_error("SAFEARRAY", 0);
1716 sa->ref = type;
1717 return make_type(pointer_default, sa);
1720 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1722 typelib_t *typelib = xmalloc(sizeof(*typelib));
1723 typelib->name = xstrdup(name);
1724 typelib->filename = NULL;
1725 typelib->attrs = attrs;
1726 list_init( &typelib->importlibs );
1727 return typelib;
1730 #define HASHMAX 64
1732 static int hash_ident(const char *name)
1734 const char *p = name;
1735 int sum = 0;
1736 /* a simple sum hash is probably good enough */
1737 while (*p) {
1738 sum += *p;
1739 p++;
1741 return sum & (HASHMAX-1);
1744 /***** type repository *****/
1746 struct rtype {
1747 const char *name;
1748 type_t *type;
1749 int t;
1750 struct rtype *next;
1753 struct rtype *type_hash[HASHMAX];
1755 static type_t *reg_type(type_t *type, const char *name, int t)
1757 struct rtype *nt;
1758 int hash;
1759 if (!name) {
1760 error_loc("registering named type without name\n");
1761 return type;
1763 hash = hash_ident(name);
1764 nt = xmalloc(sizeof(struct rtype));
1765 nt->name = name;
1766 nt->type = type;
1767 nt->t = t;
1768 nt->next = type_hash[hash];
1769 type_hash[hash] = nt;
1770 if ((t == tsSTRUCT || t == tsUNION))
1771 fix_incomplete_types(type);
1772 return type;
1775 static int is_incomplete(const type_t *t)
1777 return !t->defined && (is_struct(t->type) || is_union(t->type));
1780 static void add_incomplete(type_t *t)
1782 struct typenode *tn = xmalloc(sizeof *tn);
1783 tn->type = t;
1784 list_add_tail(&incomplete_types, &tn->entry);
1787 static void fix_type(type_t *t)
1789 if (type_is_alias(t) && is_incomplete(t)) {
1790 type_t *ot = t->orig;
1791 fix_type(ot);
1792 if (is_struct(ot->type) || is_union(ot->type))
1793 t->details.structure = ot->details.structure;
1794 t->defined = ot->defined;
1798 static void fix_incomplete(void)
1800 struct typenode *tn, *next;
1802 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1803 fix_type(tn->type);
1804 list_remove(&tn->entry);
1805 free(tn);
1809 static void fix_incomplete_types(type_t *complete_type)
1811 struct typenode *tn, *next;
1813 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry)
1815 if (((is_struct(complete_type->type) && is_struct(tn->type->type)) ||
1816 (is_union(complete_type->type) && is_union(tn->type->type))) &&
1817 !strcmp(complete_type->name, tn->type->name))
1819 tn->type->details.structure = complete_type->details.structure;
1820 tn->type->type = complete_type->type;
1821 list_remove(&tn->entry);
1822 free(tn);
1827 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1829 const declarator_t *decl;
1830 int is_str = is_attr(attrs, ATTR_STRING);
1831 type_t *type = decl_spec->type;
1833 if (is_str)
1835 type_t *t = decl_spec->type;
1836 unsigned char c;
1838 while (is_ptr(t))
1839 t = t->ref;
1841 c = t->type;
1842 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1844 decl = LIST_ENTRY( list_head( decls ), const declarator_t, entry );
1845 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1846 decl->var->name);
1850 /* We must generate names for tagless enum, struct or union.
1851 Typedef-ing a tagless enum, struct or union means we want the typedef
1852 to be included in a library hence the public attribute. */
1853 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1854 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1856 if (! is_attr(attrs, ATTR_PUBLIC))
1857 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1858 type->name = gen_name();
1860 else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1861 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1863 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1865 var_t *name = decl->var;
1867 if (name->name) {
1868 type_t *cur;
1870 cur = find_type(name->name, 0);
1871 if (cur)
1872 error_loc("%s: redefinition error; original definition was at %s:%d\n",
1873 cur->name, cur->loc_info.input_name,
1874 cur->loc_info.line_number);
1876 /* set the attributes to allow set_type to do some checks on them */
1877 name->attrs = attrs;
1878 set_type(name, decl_spec, decl, 0);
1879 cur = type_new_alias(name->type, name->name);
1880 cur->attrs = attrs;
1882 if (is_incomplete(cur))
1883 add_incomplete(cur);
1884 reg_type(cur, cur->name, 0);
1887 return type;
1890 type_t *find_type(const char *name, int t)
1892 struct rtype *cur = type_hash[hash_ident(name)];
1893 while (cur && (cur->t != t || strcmp(cur->name, name)))
1894 cur = cur->next;
1895 return cur ? cur->type : NULL;
1898 static type_t *find_type_or_error(const char *name, int t)
1900 type_t *type = find_type(name, t);
1901 if (!type) {
1902 error_loc("type '%s' not found\n", name);
1903 return NULL;
1905 return type;
1908 static type_t *find_type_or_error2(char *name, int t)
1910 type_t *tp = find_type_or_error(name, t);
1911 free(name);
1912 return tp;
1915 int is_type(const char *name)
1917 return find_type(name, 0) != NULL;
1920 static type_t *get_type(unsigned char type, char *name, int t)
1922 type_t *tp;
1923 if (name) {
1924 tp = find_type(name, t);
1925 if (tp) {
1926 free(name);
1927 return tp;
1930 tp = make_type(type, NULL);
1931 tp->name = name;
1932 if (!name) return tp;
1933 return reg_type(tp, name, t);
1936 /***** constant repository *****/
1938 struct rconst {
1939 char *name;
1940 var_t *var;
1941 struct rconst *next;
1944 struct rconst *const_hash[HASHMAX];
1946 static var_t *reg_const(var_t *var)
1948 struct rconst *nc;
1949 int hash;
1950 if (!var->name) {
1951 error_loc("registering constant without name\n");
1952 return var;
1954 hash = hash_ident(var->name);
1955 nc = xmalloc(sizeof(struct rconst));
1956 nc->name = var->name;
1957 nc->var = var;
1958 nc->next = const_hash[hash];
1959 const_hash[hash] = nc;
1960 return var;
1963 var_t *find_const(const char *name, int f)
1965 struct rconst *cur = const_hash[hash_ident(name)];
1966 while (cur && strcmp(cur->name, name))
1967 cur = cur->next;
1968 if (!cur) {
1969 if (f) error_loc("constant '%s' not found\n", name);
1970 return NULL;
1972 return cur->var;
1975 static char *gen_name(void)
1977 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1978 static unsigned long n = 0;
1979 static const char *file_id;
1980 static size_t size;
1981 char *name;
1983 if (! file_id)
1985 char *dst = dup_basename(input_name, ".idl");
1986 file_id = dst;
1988 for (; *dst; ++dst)
1989 if (! isalnum((unsigned char) *dst))
1990 *dst = '_';
1992 size = sizeof format - 7 + strlen(file_id) + 8;
1995 name = xmalloc(size);
1996 sprintf(name, format, file_id, n++);
1997 return name;
2000 struct allowed_attr
2002 unsigned int dce_compatible : 1;
2003 unsigned int acf : 1;
2004 unsigned int on_interface : 1;
2005 unsigned int on_function : 1;
2006 unsigned int on_arg : 1;
2007 unsigned int on_type : 1;
2008 unsigned int on_enum : 1;
2009 unsigned int on_struct : 1;
2010 unsigned int on_union : 1;
2011 unsigned int on_field : 1;
2012 unsigned int on_library : 1;
2013 unsigned int on_dispinterface : 1;
2014 unsigned int on_module : 1;
2015 unsigned int on_coclass : 1;
2016 const char *display_name;
2019 struct allowed_attr allowed_attr[] =
2021 /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
2022 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
2023 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
2024 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
2025 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
2026 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
2027 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
2028 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
2029 /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2030 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
2031 /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
2032 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
2033 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
2034 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
2035 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2036 /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2037 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2038 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2039 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2040 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2041 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2042 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2043 /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2044 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2045 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2046 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2047 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2048 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2049 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2050 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2051 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2052 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
2053 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2054 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2055 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2056 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2057 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2058 /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2059 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2060 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2061 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2062 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2063 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2064 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2065 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2066 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2067 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2068 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2069 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2070 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2071 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2072 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2073 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2074 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2075 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2076 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2077 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2078 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2079 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2080 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2081 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2082 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2083 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2084 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2085 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2086 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2087 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2088 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2089 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
2090 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2091 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2092 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2093 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2096 const char *get_attr_display_name(enum attr_type type)
2098 return allowed_attr[type].display_name;
2101 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2103 const attr_t *attr;
2104 if (!attrs) return attrs;
2105 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2107 if (!allowed_attr[attr->type].on_interface)
2108 error_loc("inapplicable attribute %s for interface %s\n",
2109 allowed_attr[attr->type].display_name, name);
2111 return attrs;
2114 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2116 const attr_t *attr;
2117 if (!attrs) return attrs;
2118 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2120 if (!allowed_attr[attr->type].on_function)
2121 error_loc("inapplicable attribute %s for function %s\n",
2122 allowed_attr[attr->type].display_name, name);
2124 return attrs;
2127 static void check_arg(var_t *arg)
2129 const type_t *t = arg->type;
2130 const attr_t *attr;
2132 if (t->type == 0 && ! is_var_ptr(arg))
2133 error_loc("argument '%s' has void type\n", arg->name);
2135 if (arg->attrs)
2137 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2139 if (!allowed_attr[attr->type].on_arg)
2140 error_loc("inapplicable attribute %s for argument %s\n",
2141 allowed_attr[attr->type].display_name, arg->name);
2146 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2148 const attr_t *attr;
2149 if (!attrs) return attrs;
2150 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2152 if (!allowed_attr[attr->type].on_type)
2153 error_loc("inapplicable attribute %s for typedef\n",
2154 allowed_attr[attr->type].display_name);
2156 return attrs;
2159 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2161 const attr_t *attr;
2162 if (!attrs) return attrs;
2163 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2165 if (!allowed_attr[attr->type].on_enum)
2166 error_loc("inapplicable attribute %s for enum\n",
2167 allowed_attr[attr->type].display_name);
2169 return attrs;
2172 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2174 const attr_t *attr;
2175 if (!attrs) return attrs;
2176 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2178 if (!allowed_attr[attr->type].on_struct)
2179 error_loc("inapplicable attribute %s for struct\n",
2180 allowed_attr[attr->type].display_name);
2182 return attrs;
2185 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2187 const attr_t *attr;
2188 if (!attrs) return attrs;
2189 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2191 if (!allowed_attr[attr->type].on_union)
2192 error_loc("inapplicable attribute %s for union\n",
2193 allowed_attr[attr->type].display_name);
2195 return attrs;
2198 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2200 const attr_t *attr;
2201 if (!attrs) return attrs;
2202 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2204 if (!allowed_attr[attr->type].on_field)
2205 error_loc("inapplicable attribute %s for field %s\n",
2206 allowed_attr[attr->type].display_name, name);
2208 return attrs;
2211 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2213 const attr_t *attr;
2214 if (!attrs) return attrs;
2215 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2217 if (!allowed_attr[attr->type].on_library)
2218 error_loc("inapplicable attribute %s for library %s\n",
2219 allowed_attr[attr->type].display_name, name);
2221 return attrs;
2224 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2226 const attr_t *attr;
2227 if (!attrs) return attrs;
2228 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2230 if (!allowed_attr[attr->type].on_dispinterface)
2231 error_loc("inapplicable attribute %s for dispinterface %s\n",
2232 allowed_attr[attr->type].display_name, name);
2234 return attrs;
2237 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2239 const attr_t *attr;
2240 if (!attrs) return attrs;
2241 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2243 if (!allowed_attr[attr->type].on_module)
2244 error_loc("inapplicable attribute %s for module %s\n",
2245 allowed_attr[attr->type].display_name, name);
2247 return attrs;
2250 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2252 const attr_t *attr;
2253 if (!attrs) return attrs;
2254 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2256 if (!allowed_attr[attr->type].on_coclass)
2257 error_loc("inapplicable attribute %s for coclass %s\n",
2258 allowed_attr[attr->type].display_name, name);
2260 return attrs;
2263 static int is_allowed_conf_type(const type_t *type)
2265 switch (type->type)
2267 case RPC_FC_CHAR:
2268 case RPC_FC_SMALL:
2269 case RPC_FC_BYTE:
2270 case RPC_FC_USMALL:
2271 case RPC_FC_WCHAR:
2272 case RPC_FC_SHORT:
2273 case RPC_FC_ENUM16:
2274 case RPC_FC_USHORT:
2275 case RPC_FC_LONG:
2276 case RPC_FC_ENUM32:
2277 case RPC_FC_ULONG:
2278 return TRUE;
2279 default:
2280 return FALSE;
2284 static int is_ptr_guid_type(const type_t *type)
2286 unsigned int align = 0;
2288 /* first, make sure it is a pointer to something */
2289 if (!is_ptr(type)) return FALSE;
2291 /* second, make sure it is a pointer to something of size sizeof(GUID),
2292 * i.e. 16 bytes */
2293 return (type_memsize(type->ref, &align) == 16);
2296 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2298 expr_t *dim;
2299 struct expr_loc expr_loc;
2300 expr_loc.v = arg;
2301 expr_loc.attr = attr_name;
2302 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2304 if (dim->type != EXPR_VOID)
2306 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2307 if (!is_allowed_conf_type(expr_type))
2308 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2309 attr_name);
2314 static void check_remoting_fields(const var_t *var, type_t *type);
2316 /* checks that properties common to fields and arguments are consistent */
2317 static void check_field_common(const type_t *container_type,
2318 const char *container_name, const var_t *arg)
2320 type_t *type = arg->type;
2321 int is_wire_marshal = 0;
2322 int is_context_handle = 0;
2323 const char *container_type_name = NULL;
2325 if (is_struct(container_type->type))
2326 container_type_name = "struct";
2327 else if (is_union(container_type->type))
2328 container_type_name = "union";
2329 else if (container_type->type == RPC_FC_FUNCTION)
2330 container_type_name = "function";
2332 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2333 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2334 error_loc_info(&arg->loc_info,
2335 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2336 arg->name);
2338 if (is_attr(arg->attrs, ATTR_SIZEIS))
2340 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2341 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2343 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2345 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2346 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2348 if (is_attr(arg->attrs, ATTR_IIDIS))
2350 struct expr_loc expr_loc;
2351 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2352 if (expr->type != EXPR_VOID)
2354 const type_t *expr_type;
2355 expr_loc.v = arg;
2356 expr_loc.attr = "iid_is";
2357 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2358 if (!expr_type || !is_ptr_guid_type(expr_type))
2359 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2362 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2364 struct expr_loc expr_loc;
2365 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2366 if (expr->type != EXPR_VOID)
2368 const type_t *expr_type;
2369 expr_loc.v = arg;
2370 expr_loc.attr = "switch_is";
2371 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2372 if (!expr_type || !is_allowed_conf_type(expr_type))
2373 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2374 expr_loc.attr);
2378 /* get fundamental type for the argument */
2379 for (;;)
2381 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2383 is_wire_marshal = 1;
2384 break;
2386 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2388 is_context_handle = 1;
2389 break;
2391 if (type_is_alias(type))
2392 type = type->orig;
2393 else if (is_ptr(type) || is_array(type))
2394 type = type->ref;
2395 else
2396 break;
2399 if (type->type == 0 && !is_attr(arg->attrs, ATTR_IIDIS) && !is_wire_marshal && !is_context_handle)
2400 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot derive from void *\n", arg->name, container_type_name, container_name);
2401 else if (type->type == RPC_FC_FUNCTION)
2402 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot be a function pointer\n", arg->name, container_type_name, container_name);
2403 else if (!is_wire_marshal && (is_struct(type->type) || is_union(type->type)))
2404 check_remoting_fields(arg, type);
2407 static void check_remoting_fields(const var_t *var, type_t *type)
2409 const var_t *field;
2410 const var_list_t *fields = NULL;
2412 type = type_get_real_type(type);
2414 if (type->checked)
2415 return;
2417 type->checked = TRUE;
2419 if (is_struct(type->type))
2421 if (type_is_complete(type))
2422 fields = type_struct_get_fields(type);
2423 else
2424 error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2426 else if (is_union(type->type))
2427 fields = type_union_get_cases(type);
2429 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2430 if (field->type) check_field_common(type, type->name, field);
2433 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2434 static void check_remoting_args(const var_t *func)
2436 const char *funcname = func->name;
2437 const var_t *arg;
2439 if (func->type->details.function->args) LIST_FOR_EACH_ENTRY( arg, func->type->details.function->args, const var_t, entry )
2441 int ptr_level = 0;
2442 const type_t *type = arg->type;
2444 /* get pointer level and fundamental type for the argument */
2445 for (;;)
2447 if (is_attr(type->attrs, ATTR_WIREMARSHAL))
2448 break;
2449 if (is_attr(type->attrs, ATTR_CONTEXTHANDLE))
2450 break;
2451 if (type_is_alias(type))
2452 type = type->orig;
2453 else if (is_ptr(type))
2455 ptr_level++;
2456 type = type->ref;
2458 else
2459 break;
2462 /* check that [out] parameters have enough pointer levels */
2463 if (is_attr(arg->attrs, ATTR_OUT))
2465 if (!is_array(type))
2467 if (!ptr_level)
2468 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2469 if (type->type == RPC_FC_IP && ptr_level == 1)
2470 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2474 check_field_common(func->type, funcname, arg);
2478 static void add_explicit_handle_if_necessary(var_t *func)
2480 const var_t* explicit_handle_var;
2481 const var_t* explicit_generic_handle_var = NULL;
2482 const var_t* context_handle_var = NULL;
2484 /* check for a defined binding handle */
2485 explicit_handle_var = get_explicit_handle_var(func);
2486 if (!explicit_handle_var)
2488 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2489 if (!explicit_generic_handle_var)
2491 context_handle_var = get_context_handle_var(func);
2492 if (!context_handle_var)
2494 /* no explicit handle specified so add
2495 * "[in] handle_t IDL_handle" as the first parameter to the
2496 * function */
2497 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2498 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2499 idl_handle->type = find_type_or_error("handle_t", 0);
2500 type_function_add_head_arg(func->type, idl_handle);
2506 static void check_functions(const type_t *iface, int is_inside_library)
2508 const statement_t *stmt;
2509 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
2511 STATEMENTS_FOR_EACH_FUNC( stmt, iface->details.iface->stmts )
2513 var_t *func = stmt->u.var;
2514 add_explicit_handle_if_necessary(func);
2517 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2519 STATEMENTS_FOR_EACH_FUNC( stmt, iface->details.iface->stmts )
2521 const var_t *func = stmt->u.var;
2522 if (!is_attr(func->attrs, ATTR_LOCAL))
2523 check_remoting_args(func);
2528 static void check_statements(const statement_list_t *stmts, int is_inside_library)
2530 const statement_t *stmt;
2532 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2534 if (stmt->type == STMT_LIBRARY)
2535 check_statements(stmt->u.lib->stmts, TRUE);
2536 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP)
2537 check_functions(stmt->u.type, is_inside_library);
2541 static void check_all_user_types(const statement_list_t *stmts)
2543 const statement_t *stmt;
2545 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2547 if (stmt->type == STMT_LIBRARY)
2548 check_all_user_types(stmt->u.lib->stmts);
2549 else if (stmt->type == STMT_TYPE && stmt->u.type->type == RPC_FC_IP &&
2550 !is_local(stmt->u.type->attrs))
2552 const statement_t *stmt_func;
2553 STATEMENTS_FOR_EACH_FUNC(stmt_func, stmt->u.type->details.iface->stmts) {
2554 const var_t *func = stmt_func->u.var;
2555 check_for_additional_prototype_types(func->type->details.function->args);
2561 int is_valid_uuid(const char *s)
2563 int i;
2565 for (i = 0; i < 36; ++i)
2566 if (i == 8 || i == 13 || i == 18 || i == 23)
2568 if (s[i] != '-')
2569 return FALSE;
2571 else
2572 if (!isxdigit(s[i]))
2573 return FALSE;
2575 return s[i] == '\0';
2578 static statement_t *make_statement(enum statement_type type)
2580 statement_t *stmt = xmalloc(sizeof(*stmt));
2581 stmt->type = type;
2582 return stmt;
2585 static statement_t *make_statement_type_decl(type_t *type)
2587 statement_t *stmt = make_statement(STMT_TYPE);
2588 stmt->u.type = type;
2589 return stmt;
2592 static statement_t *make_statement_reference(type_t *type)
2594 statement_t *stmt = make_statement(STMT_TYPEREF);
2595 stmt->u.type = type;
2596 return stmt;
2599 static statement_t *make_statement_declaration(var_t *var)
2601 statement_t *stmt = make_statement(STMT_DECLARATION);
2602 stmt->u.var = var;
2603 if (var->stgclass == STG_EXTERN && var->eval)
2604 warning("'%s' initialised and declared extern\n", var->name);
2605 if (is_const_decl(var))
2607 if (var->eval)
2608 reg_const(var);
2610 else if ((var->stgclass == STG_NONE || var->stgclass == STG_REGISTER) &&
2611 var->type->type != RPC_FC_FUNCTION)
2612 error_loc("instantiation of data is illegal\n");
2613 return stmt;
2616 static statement_t *make_statement_library(typelib_t *typelib)
2618 statement_t *stmt = make_statement(STMT_LIBRARY);
2619 stmt->u.lib = typelib;
2620 return stmt;
2623 static statement_t *make_statement_cppquote(const char *str)
2625 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2626 stmt->u.str = str;
2627 return stmt;
2630 static statement_t *make_statement_importlib(const char *str)
2632 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2633 stmt->u.str = str;
2634 return stmt;
2637 static statement_t *make_statement_import(const char *str)
2639 statement_t *stmt = make_statement(STMT_IMPORT);
2640 stmt->u.str = str;
2641 return stmt;
2644 static statement_t *make_statement_module(type_t *type)
2646 statement_t *stmt = make_statement(STMT_MODULE);
2647 stmt->u.type = type;
2648 return stmt;
2651 static statement_t *make_statement_typedef(declarator_list_t *decls)
2653 declarator_t *decl, *next;
2654 statement_t *stmt;
2655 type_list_t **type_list;
2657 if (!decls) return NULL;
2659 stmt = make_statement(STMT_TYPEDEF);
2660 stmt->u.type_list = NULL;
2661 type_list = &stmt->u.type_list;
2663 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2665 var_t *var = decl->var;
2666 type_t *type = find_type_or_error(var->name, 0);
2667 *type_list = xmalloc(sizeof(type_list_t));
2668 (*type_list)->type = type;
2669 (*type_list)->next = NULL;
2671 type_list = &(*type_list)->next;
2672 free(decl);
2673 free(var);
2676 return stmt;
2679 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2681 if (!stmt) return list;
2682 if (!list)
2684 list = xmalloc( sizeof(*list) );
2685 list_init( list );
2687 list_add_tail( list, &stmt->entry );
2688 return list;
2691 void init_loc_info(loc_info_t *i)
2693 i->input_name = input_name ? input_name : "stdin";
2694 i->line_number = line_number;
2695 i->near_text = parser_text;
2698 static void check_def(const type_t *t)
2700 if (t->defined)
2701 error_loc("%s: redefinition error; original definition was at %s:%d\n",
2702 t->name, t->loc_info.input_name, t->loc_info.line_number);