widl: Remove the single keyword and type.
[wine.git] / tools / widl / parser.y
blobc921ab6d8778adb7dc587fb67c2e69f2f50af866
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>
32 #include "widl.h"
33 #include "utils.h"
34 #include "parser.h"
35 #include "header.h"
36 #include "typelib.h"
37 #include "typegen.h"
38 #include "expr.h"
39 #include "typetree.h"
41 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
63 # endif
64 #endif
66 #define YYERROR_VERBOSE
68 unsigned char pointer_default = RPC_FC_UP;
69 static int is_object_interface = FALSE;
71 typedef struct list typelist_t;
72 struct typenode {
73 type_t *type;
74 struct list entry;
77 struct _import_t
79 char *name;
80 int import_performed;
83 typedef struct _decl_spec_t
85 type_t *type;
86 attr_list_t *attrs;
87 enum storage_class stgclass;
88 } decl_spec_t;
90 typelist_t incomplete_types = LIST_INIT(incomplete_types);
92 static void add_incomplete(type_t *t);
93 static void fix_incomplete(void);
94 static void fix_incomplete_types(type_t *complete_type);
96 static str_list_t *append_str(str_list_t *list, char *str);
97 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
98 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list);
99 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);
100 static attr_t *make_attr(enum attr_type type);
101 static attr_t *make_attrv(enum attr_type type, unsigned long val);
102 static attr_t *make_attrp(enum attr_type type, void *val);
103 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
104 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
105 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl, int top);
106 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls);
107 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
108 static ifref_t *make_ifref(type_t *iface);
109 static var_list_t *append_var(var_list_t *list, var_t *var);
110 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars);
111 static var_t *make_var(char *name);
112 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *p);
113 static declarator_t *make_declarator(var_t *var);
114 static func_list_t *append_func(func_list_t *list, func_t *func);
115 static func_t *make_func(var_t *def);
116 static type_t *make_class(char *name);
117 static type_t *make_safearray(type_t *type);
118 static typelib_t *make_library(const char *name, const attr_list_t *attrs);
119 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type);
121 static type_t *type_new_enum(char *name, var_list_t *enums);
122 static type_t *type_new_struct(char *name, int defined, var_list_t *fields);
123 static type_t *type_new_nonencapsulated_union(char *name, var_list_t *fields);
124 static type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases);
126 static type_t *reg_type(type_t *type, const char *name, int t);
127 static type_t *reg_typedefs(decl_spec_t *decl_spec, var_list_t *names, attr_list_t *attrs);
128 static type_t *find_type_or_error(const char *name, int t);
129 static type_t *find_type_or_error2(char *name, int t);
130 static type_t *get_type(unsigned char type, char *name, int t);
132 static var_t *reg_const(var_t *var);
134 static char *gen_name(void);
135 static void check_arg(var_t *arg);
136 static void check_statements(const statement_list_t *stmts, int is_inside_library);
137 static void check_all_user_types(const statement_list_t *stmts);
138 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs);
139 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs);
140 static attr_list_t *check_typedef_attrs(attr_list_t *attrs);
141 static attr_list_t *check_enum_attrs(attr_list_t *attrs);
142 static attr_list_t *check_struct_attrs(attr_list_t *attrs);
143 static attr_list_t *check_union_attrs(attr_list_t *attrs);
144 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs);
145 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs);
146 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs);
147 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs);
148 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs);
149 const char *get_attr_display_name(enum attr_type type);
150 static void add_explicit_handle_if_necessary(var_t *func);
151 static void check_def(const type_t *t);
153 static statement_t *make_statement(enum statement_type type);
154 static statement_t *make_statement_type_decl(type_t *type);
155 static statement_t *make_statement_reference(type_t *type);
156 static statement_t *make_statement_declaration(var_t *var);
157 static statement_t *make_statement_library(typelib_t *typelib);
158 static statement_t *make_statement_cppquote(const char *str);
159 static statement_t *make_statement_importlib(const char *str);
160 static statement_t *make_statement_module(type_t *type);
161 static statement_t *make_statement_typedef(var_list_t *names);
162 static statement_t *make_statement_import(const char *str);
163 static statement_t *make_statement_typedef(var_list_t *names);
164 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt);
166 #define tsENUM 1
167 #define tsSTRUCT 2
168 #define tsUNION 3
171 %union {
172 attr_t *attr;
173 attr_list_t *attr_list;
174 str_list_t *str_list;
175 expr_t *expr;
176 expr_list_t *expr_list;
177 array_dims_t *array_dims;
178 type_t *type;
179 var_t *var;
180 var_list_t *var_list;
181 declarator_t *declarator;
182 declarator_list_t *declarator_list;
183 func_t *func;
184 func_list_t *func_list;
185 statement_t *statement;
186 statement_list_t *stmt_list;
187 ifref_t *ifref;
188 ifref_list_t *ifref_list;
189 char *str;
190 UUID *uuid;
191 unsigned int num;
192 double dbl;
193 interface_info_t ifinfo;
194 typelib_t *typelib;
195 struct _import_t *import;
196 struct _decl_spec_t *declspec;
197 enum storage_class stgclass;
200 %token <str> aIDENTIFIER
201 %token <str> aKNOWNTYPE
202 %token <num> aNUM aHEXNUM
203 %token <dbl> aDOUBLE
204 %token <str> aSTRING aWSTRING
205 %token <uuid> aUUID
206 %token aEOF
207 %token SHL SHR
208 %token MEMBERPTR
209 %token EQUALITY INEQUALITY
210 %token GREATEREQUAL LESSEQUAL
211 %token LOGICALOR LOGICALAND
212 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
213 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
214 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
215 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
216 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
217 %token tDEFAULT
218 %token tDEFAULTCOLLELEM
219 %token tDEFAULTVALUE
220 %token tDEFAULTVTABLE
221 %token tDISPLAYBIND
222 %token tDISPINTERFACE
223 %token tDLLNAME tDOUBLE tDUAL
224 %token tENDPOINT
225 %token tENTRY tENUM tERRORSTATUST
226 %token tEXPLICITHANDLE tEXTERN
227 %token tFALSE
228 %token tFASTCALL
229 %token tFLOAT
230 %token tHANDLE
231 %token tHANDLET
232 %token tHELPCONTEXT tHELPFILE
233 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
234 %token tHIDDEN
235 %token tHYPER tID tIDEMPOTENT
236 %token tIIDIS
237 %token tIMMEDIATEBIND
238 %token tIMPLICITHANDLE
239 %token tIMPORT tIMPORTLIB
240 %token tIN tIN_LINE tINLINE
241 %token tINPUTSYNC
242 %token tINT tINT64
243 %token tINTERFACE
244 %token tLCID
245 %token tLENGTHIS tLIBRARY
246 %token tLOCAL
247 %token tLONG
248 %token tMETHODS
249 %token tMODULE
250 %token tNONBROWSABLE
251 %token tNONCREATABLE
252 %token tNONEXTENSIBLE
253 %token tNULL
254 %token tOBJECT tODL tOLEAUTOMATION
255 %token tOPTIONAL
256 %token tOUT
257 %token tPASCAL
258 %token tPOINTERDEFAULT
259 %token tPROPERTIES
260 %token tPROPGET tPROPPUT tPROPPUTREF
261 %token tPTR
262 %token tPUBLIC
263 %token tRANGE
264 %token tREADONLY tREF
265 %token tREGISTER
266 %token tREQUESTEDIT
267 %token tRESTRICTED
268 %token tRETVAL
269 %token tSAFEARRAY
270 %token tSHORT
271 %token tSIGNED
272 %token tSIZEIS tSIZEOF
273 %token tSMALL
274 %token tSOURCE
275 %token tSTATIC
276 %token tSTDCALL
277 %token tSTRICTCONTEXTHANDLE
278 %token tSTRING tSTRUCT
279 %token tSWITCH tSWITCHIS tSWITCHTYPE
280 %token tTRANSMITAS
281 %token tTRUE
282 %token tTYPEDEF
283 %token tUNION
284 %token tUNIQUE
285 %token tUNSIGNED
286 %token tUUID
287 %token tV1ENUM
288 %token tVARARG
289 %token tVERSION
290 %token tVOID
291 %token tWCHAR tWIREMARSHAL
293 %type <attr> attribute type_qualifier function_specifier
294 %type <attr_list> m_attributes attributes attrib_list m_type_qual_list
295 %type <str_list> str_list
296 %type <expr> m_expr expr expr_const expr_int_const array
297 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_int_const
298 %type <ifinfo> interfacehdr
299 %type <stgclass> storage_cls_spec
300 %type <declspec> decl_spec decl_spec_no_type m_decl_spec_no_type
301 %type <type> inherit interface interfacedef interfacedec
302 %type <type> dispinterface dispinterfacehdr dispinterfacedef
303 %type <type> module modulehdr moduledef
304 %type <type> base_type int_std
305 %type <type> enumdef structdef uniondef typedecl
306 %type <type> type
307 %type <ifref> coclass_int
308 %type <ifref_list> coclass_ints
309 %type <var> arg ne_union_field union_field s_field case enum declaration
310 %type <var_list> m_args no_args args fields ne_union_fields cases enums enum_list dispint_props field
311 %type <var> m_ident ident
312 %type <declarator> declarator direct_declarator init_declarator
313 %type <declarator_list> declarator_list
314 %type <func> funcdef
315 %type <type> coclass coclasshdr coclassdef
316 %type <num> pointer_type version
317 %type <str> libraryhdr callconv cppquote importlib import t_ident
318 %type <uuid> uuid_string
319 %type <import> import_start
320 %type <typelib> library_start librarydef
321 %type <statement> statement typedef
322 %type <stmt_list> gbl_statements imp_statements int_statements dispint_meths
324 %left ','
325 %right '?' ':'
326 %left LOGICALOR
327 %left LOGICALAND
328 %left '|'
329 %left '^'
330 %left '&'
331 %left EQUALITY INEQUALITY
332 %left '<' '>' LESSEQUAL GREATEREQUAL
333 %left SHL SHR
334 %left '-' '+'
335 %left '*' '/' '%'
336 %right '!' '~' CAST PPTR POS NEG ADDRESSOF tSIZEOF
337 %left '.' MEMBERPTR '[' ']'
341 input: gbl_statements { fix_incomplete();
342 check_statements($1, FALSE);
343 check_all_user_types($1);
344 write_header($1);
345 write_id_data($1);
346 write_proxies($1);
347 write_client($1);
348 write_server($1);
349 write_dlldata($1);
350 write_local_stubs($1);
354 gbl_statements: { $$ = NULL; }
355 | gbl_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
356 | gbl_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
357 | gbl_statements coclass ';' { $$ = $1;
358 reg_type($2, $2->name, 0);
360 | gbl_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
361 reg_type($2, $2->name, 0);
363 | gbl_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
364 | gbl_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
365 | gbl_statements statement { $$ = append_statement($1, $2); }
368 imp_statements: { $$ = NULL; }
369 | imp_statements interfacedec { $$ = append_statement($1, make_statement_reference($2)); }
370 | imp_statements interfacedef { $$ = append_statement($1, make_statement_type_decl($2)); }
371 | imp_statements coclass ';' { $$ = $1; reg_type($2, $2->name, 0); }
372 | imp_statements coclassdef { $$ = append_statement($1, make_statement_type_decl($2));
373 reg_type($2, $2->name, 0);
375 | imp_statements moduledef { $$ = append_statement($1, make_statement_module($2)); }
376 | imp_statements statement { $$ = append_statement($1, $2); }
377 | imp_statements importlib { $$ = append_statement($1, make_statement_importlib($2)); }
378 | imp_statements librarydef { $$ = append_statement($1, make_statement_library($2)); }
381 int_statements: { $$ = NULL; }
382 | int_statements statement { $$ = append_statement($1, $2); }
385 semicolon_opt:
386 | ';'
389 statement:
390 cppquote { $$ = make_statement_cppquote($1); }
391 | typedecl ';' { $$ = make_statement_type_decl($1); }
392 | declaration ';' { $$ = make_statement_declaration($1); }
393 | import { $$ = make_statement_import($1); }
394 | typedef ';' { $$ = $1; }
397 typedecl:
398 enumdef
399 | structdef
400 | uniondef
401 | attributes enumdef { $$ = $2; $$->attrs = check_enum_attrs($1); }
402 | attributes structdef { $$ = $2; $$->attrs = check_struct_attrs($1); }
403 | attributes uniondef { $$ = $2; $$->attrs = check_union_attrs($1); }
406 cppquote: tCPPQUOTE '(' aSTRING ')' { $$ = $3; }
408 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
409 $$ = xmalloc(sizeof(struct _import_t));
410 $$->name = $2;
411 $$->import_performed = do_import($2);
412 if (!$$->import_performed) yychar = aEOF;
416 import: import_start imp_statements aEOF { $$ = $1->name;
417 if ($1->import_performed) pop_import();
418 free($1);
422 importlib: tIMPORTLIB '(' aSTRING ')'
423 semicolon_opt { $$ = $3; if(!parse_only) add_importlib($3); }
426 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
428 library_start: attributes libraryhdr '{' { $$ = make_library($2, check_library_attrs($2, $1));
429 if (!parse_only) start_typelib($$);
432 librarydef: library_start imp_statements '}'
433 semicolon_opt { $$ = $1;
434 $$->stmts = $2;
435 if (!parse_only) end_typelib();
439 m_args: { $$ = NULL; }
440 | args
443 no_args: tVOID { $$ = NULL; }
446 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
447 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
448 | no_args
451 /* split into two rules to get bison to resolve a tVOID conflict */
452 arg: attributes decl_spec declarator { $$ = $3->var;
453 $$->attrs = $1;
454 if ($2->stgclass != STG_NONE && $2->stgclass != STG_REGISTER)
455 error_loc("invalid storage class for function parameter\n");
456 set_type($$, $2, $3, TRUE);
457 free($3);
459 | decl_spec declarator { $$ = $2->var;
460 if ($1->stgclass != STG_NONE && $1->stgclass != STG_REGISTER)
461 error_loc("invalid storage class for function parameter\n");
462 set_type($$, $1, $2, TRUE);
463 free($2);
467 array: '[' m_expr ']' { $$ = $2; }
468 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
471 m_attributes: { $$ = NULL; }
472 | attributes
475 attributes:
476 '[' attrib_list ']' { $$ = $2; }
479 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
480 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
481 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
484 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
485 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
488 attribute: { $$ = NULL; }
489 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
490 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
491 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
492 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
493 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
494 | tBROADCAST { $$ = make_attr(ATTR_BROADCAST); }
495 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
496 | tCASE '(' expr_list_int_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
497 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
498 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
499 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
500 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
501 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
502 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
503 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
504 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
505 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
506 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
507 | tDUAL { $$ = make_attr(ATTR_DUAL); }
508 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
509 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY, $3); }
510 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
511 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
512 | tHELPCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
513 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
514 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
515 | tHELPSTRINGCONTEXT '(' expr_int_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
516 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
517 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
518 | tID '(' expr_int_const ')' { $$ = make_attrp(ATTR_ID, $3); }
519 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
520 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
521 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
522 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
523 | tIN { $$ = make_attr(ATTR_IN); }
524 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
525 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
526 | tLCID '(' expr_int_const ')' { $$ = make_attrp(ATTR_LIBLCID, $3); }
527 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
528 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
529 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
530 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
531 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
532 | tODL { $$ = make_attr(ATTR_ODL); }
533 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
534 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
535 | tOUT { $$ = make_attr(ATTR_OUT); }
536 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
537 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
538 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
539 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
540 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
541 | tRANGE '(' expr_int_const ',' expr_int_const ')'
542 { expr_list_t *list = append_expr( NULL, $3 );
543 list = append_expr( list, $5 );
544 $$ = make_attrp(ATTR_RANGE, list); }
545 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
546 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
547 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
548 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
549 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
550 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
551 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
552 | tSTRING { $$ = make_attr(ATTR_STRING); }
553 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
554 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
555 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
556 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
557 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
558 | tVARARG { $$ = make_attr(ATTR_VARARG); }
559 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
560 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
561 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
564 uuid_string:
565 aUUID
566 | aSTRING { if (!is_valid_uuid($1))
567 error_loc("invalid UUID: %s\n", $1);
568 $$ = parse_uuid($1); }
571 callconv: tCDECL { $$ = $<str>1; }
572 | tFASTCALL { $$ = $<str>1; }
573 | tPASCAL { $$ = $<str>1; }
574 | tSTDCALL { $$ = $<str>1; }
577 cases: { $$ = NULL; }
578 | cases case { $$ = append_var( $1, $2 ); }
581 case: tCASE expr_int_const ':' union_field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
582 $$ = $4; if (!$$) $$ = make_var(NULL);
583 $$->attrs = append_attr( $$->attrs, a );
585 | tDEFAULT ':' union_field { attr_t *a = make_attr(ATTR_DEFAULT);
586 $$ = $3; if (!$$) $$ = make_var(NULL);
587 $$->attrs = append_attr( $$->attrs, a );
591 enums: { $$ = NULL; }
592 | enum_list ',' { $$ = $1; }
593 | enum_list
596 enum_list: enum { if (!$1->eval)
597 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
598 $$ = append_var( NULL, $1 );
600 | enum_list ',' enum { if (!$3->eval)
602 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
603 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
605 $$ = append_var( $1, $3 );
609 enum: ident '=' expr_int_const { $$ = reg_const($1);
610 $$->eval = $3;
611 $$->type = type_new_int(TYPE_BASIC_INT, 0);
613 | ident { $$ = reg_const($1);
614 $$->type = type_new_int(TYPE_BASIC_INT, 0);
618 enumdef: tENUM t_ident '{' enums '}' { $$ = type_new_enum($2, $4); }
621 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
622 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
626 exprs: { $$ = make_expr(EXPR_VOID); }
627 | expr_list
630 expr_list: expr
631 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
635 m_expr: { $$ = make_expr(EXPR_VOID); }
636 | expr
639 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
640 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
641 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
642 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
643 | tNULL { $$ = make_exprl(EXPR_NUM, 0); }
644 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
645 | aSTRING { $$ = make_exprs(EXPR_STRLIT, $1); }
646 | aWSTRING { $$ = make_exprs(EXPR_WSTRLIT, $1); }
647 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
648 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
649 | expr LOGICALOR expr { $$ = make_expr2(EXPR_LOGOR, $1, $3); }
650 | expr LOGICALAND expr { $$ = make_expr2(EXPR_LOGAND, $1, $3); }
651 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
652 | expr '^' expr { $$ = make_expr2(EXPR_XOR, $1, $3); }
653 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
654 | expr EQUALITY expr { $$ = make_expr2(EXPR_EQUALITY, $1, $3); }
655 | expr INEQUALITY expr { $$ = make_expr2(EXPR_INEQUALITY, $1, $3); }
656 | expr '>' expr { $$ = make_expr2(EXPR_GTR, $1, $3); }
657 | expr '<' expr { $$ = make_expr2(EXPR_LESS, $1, $3); }
658 | expr GREATEREQUAL expr { $$ = make_expr2(EXPR_GTREQL, $1, $3); }
659 | expr LESSEQUAL expr { $$ = make_expr2(EXPR_LESSEQL, $1, $3); }
660 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
661 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
662 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
663 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
664 | expr '%' expr { $$ = make_expr2(EXPR_MOD, $1, $3); }
665 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
666 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
667 | '!' expr { $$ = make_expr1(EXPR_LOGNOT, $2); }
668 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
669 | '+' expr %prec POS { $$ = make_expr1(EXPR_POS, $2); }
670 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
671 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
672 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
673 | expr MEMBERPTR aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, make_expr1(EXPR_PPTR, $1), make_exprs(EXPR_IDENTIFIER, $3)); }
674 | expr '.' aIDENTIFIER { $$ = make_expr2(EXPR_MEMBER, $1, make_exprs(EXPR_IDENTIFIER, $3)); }
675 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
676 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
677 | expr '[' expr ']' { $$ = make_expr2(EXPR_ARRAY, $1, $3); }
678 | '(' expr ')' { $$ = $2; }
681 expr_list_int_const: expr_int_const { $$ = append_expr( NULL, $1 ); }
682 | expr_list_int_const ',' expr_int_const { $$ = append_expr( $1, $3 ); }
685 expr_int_const: expr { $$ = $1;
686 if (!$$->is_const)
687 error_loc("expression is not an integer constant\n");
691 expr_const: expr { $$ = $1;
692 if (!$$->is_const && $$->type != EXPR_STRLIT && $$->type != EXPR_WSTRLIT)
693 error_loc("expression is not constant\n");
697 fields: { $$ = NULL; }
698 | fields field { $$ = append_var_list($1, $2); }
701 field: m_attributes decl_spec declarator_list ';'
702 { const char *first = LIST_ENTRY(list_head($3), declarator_t, entry)->var->name;
703 check_field_attrs(first, $1);
704 $$ = set_var_types($1, $2, $3);
706 | m_attributes uniondef ';' { var_t *v = make_var(NULL);
707 v->type = $2; v->attrs = $1;
708 $$ = append_var(NULL, v);
712 ne_union_field:
713 s_field ';' { $$ = $1; }
714 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
717 ne_union_fields: { $$ = NULL; }
718 | ne_union_fields ne_union_field { $$ = append_var( $1, $2 ); }
721 union_field:
722 s_field ';' { $$ = $1; }
723 | ';' { $$ = NULL; }
726 s_field: m_attributes decl_spec declarator { $$ = $3->var;
727 $$->attrs = check_field_attrs($$->name, $1);
728 set_type($$, $2, $3, FALSE);
729 free($3);
733 funcdef:
734 m_attributes decl_spec declarator { var_t *v = $3->var;
735 v->attrs = check_function_attrs(v->name, $1);
736 set_type(v, $2, $3, FALSE);
737 free($3);
738 $$ = make_func(v);
742 declaration:
743 attributes decl_spec init_declarator
744 { $$ = $3->var;
745 $$->attrs = $1;
746 set_type($$, $2, $3, FALSE);
747 free($3);
749 | decl_spec init_declarator { $$ = $2->var;
750 set_type($$, $1, $2, FALSE);
751 free($2);
755 m_ident: { $$ = NULL; }
756 | ident
759 t_ident: { $$ = NULL; }
760 | aIDENTIFIER { $$ = $1; }
761 | aKNOWNTYPE { $$ = $1; }
764 ident: aIDENTIFIER { $$ = make_var($1); }
765 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
766 | aKNOWNTYPE { $$ = make_var($<str>1); }
769 base_type: tBYTE { $$ = find_type_or_error($<str>1, 0); }
770 | tWCHAR { $$ = find_type_or_error($<str>1, 0); }
771 | int_std
772 | tSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), -1); }
773 | tUNSIGNED int_std { $$ = type_new_int(type_basic_get_type($2), 1); }
774 | tUNSIGNED { $$ = type_new_int(TYPE_BASIC_INT, 1); }
775 | tFLOAT { $$ = find_type_or_error($<str>1, 0); }
776 | tDOUBLE { $$ = find_type_or_error($<str>1, 0); }
777 | tBOOLEAN { $$ = find_type_or_error($<str>1, 0); }
778 | tERRORSTATUST { $$ = find_type_or_error($<str>1, 0); }
779 | tHANDLET { $$ = find_type_or_error($<str>1, 0); }
782 m_int:
783 | tINT
786 int_std: tINT { $$ = type_new_int(TYPE_BASIC_INT, 0); }
787 | tSHORT m_int { $$ = type_new_int(TYPE_BASIC_INT16, 0); }
788 | tSMALL { $$ = type_new_int(TYPE_BASIC_INT8, 0); }
789 | tLONG m_int { $$ = type_new_int(TYPE_BASIC_INT32, 0); }
790 | tHYPER m_int { $$ = type_new_int(TYPE_BASIC_HYPER, 0); }
791 | tINT64 { $$ = type_new_int(TYPE_BASIC_INT64, 0); }
792 | tCHAR { $$ = type_new_int(TYPE_BASIC_CHAR, 0); }
795 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
796 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
797 if (type_get_type_detect_alias($$) != TYPE_COCLASS)
798 error_loc("%s was not declared a coclass at %s:%d\n",
799 $2, $$->loc_info.input_name,
800 $$->loc_info.line_number);
804 coclasshdr: attributes coclass { $$ = $2;
805 check_def($$);
806 $$->attrs = check_coclass_attrs($2->name, $1);
810 coclassdef: coclasshdr '{' coclass_ints '}' semicolon_opt
811 { $$ = type_coclass_define($1, $3); }
814 coclass_ints: { $$ = NULL; }
815 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
818 coclass_int:
819 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
822 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
823 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
826 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
827 is_object_interface = TRUE;
828 $$ = $2;
829 check_def($$);
830 attrs = make_attr(ATTR_DISPINTERFACE);
831 $$->attrs = append_attr( check_dispiface_attrs($2->name, $1), attrs );
832 $$->defined = TRUE;
836 dispint_props: tPROPERTIES ':' { $$ = NULL; }
837 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
840 dispint_meths: tMETHODS ':' { $$ = NULL; }
841 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
844 dispinterfacedef: dispinterfacehdr '{'
845 dispint_props
846 dispint_meths
847 '}' { $$ = $1;
848 type_dispinterface_define($$, $3, $4);
850 | dispinterfacehdr
851 '{' interface ';' '}' { $$ = $1;
852 type_dispinterface_define_from_iface($$, $3);
856 inherit: { $$ = NULL; }
857 | ':' aKNOWNTYPE { $$ = find_type_or_error2($2, 0); }
860 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
861 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
864 interfacehdr: attributes interface { $$.interface = $2;
865 $$.old_pointer_default = pointer_default;
866 if (is_attr($1, ATTR_POINTERDEFAULT))
867 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
868 is_object_interface = is_object($1);
869 check_def($2);
870 $2->attrs = check_iface_attrs($2->name, $1);
871 $2->defined = TRUE;
875 interfacedef: interfacehdr inherit
876 '{' int_statements '}' semicolon_opt { $$ = $1.interface;
877 type_interface_define($$, $2, $4);
878 pointer_default = $1.old_pointer_default;
880 /* MIDL is able to import the definition of a base class from inside the
881 * definition of a derived class, I'll try to support it with this rule */
882 | interfacehdr ':' aIDENTIFIER
883 '{' import int_statements '}'
884 semicolon_opt { $$ = $1.interface;
885 type_interface_define($$, find_type_or_error2($3, 0), $6);
886 pointer_default = $1.old_pointer_default;
888 | dispinterfacedef semicolon_opt { $$ = $1; }
891 interfacedec:
892 interface ';' { $$ = $1; }
893 | dispinterface ';' { $$ = $1; }
896 module: tMODULE aIDENTIFIER { $$ = type_new_module($2); }
897 | tMODULE aKNOWNTYPE { $$ = type_new_module($2); }
900 modulehdr: attributes module { $$ = $2;
901 $$->attrs = check_module_attrs($2->name, $1);
905 moduledef: modulehdr '{' int_statements '}'
906 semicolon_opt { $$ = $1;
907 type_module_define($$, $3);
911 storage_cls_spec:
912 tEXTERN { $$ = STG_EXTERN; }
913 | tSTATIC { $$ = STG_STATIC; }
914 | tREGISTER { $$ = STG_REGISTER; }
917 function_specifier:
918 tINLINE { $$ = make_attr(ATTR_INLINE); }
921 type_qualifier:
922 tCONST { $$ = make_attr(ATTR_CONST); }
925 m_type_qual_list: { $$ = NULL; }
926 | m_type_qual_list type_qualifier { $$ = append_attr($1, $2); }
929 decl_spec: type m_decl_spec_no_type { $$ = make_decl_spec($1, $2, NULL, NULL, STG_NONE); }
930 | decl_spec_no_type type m_decl_spec_no_type
931 { $$ = make_decl_spec($2, $1, $3, NULL, STG_NONE); }
934 m_decl_spec_no_type: { $$ = NULL; }
935 | decl_spec_no_type
938 decl_spec_no_type:
939 type_qualifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
940 | function_specifier m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, $1, STG_NONE); }
941 | storage_cls_spec m_decl_spec_no_type { $$ = make_decl_spec(NULL, $2, NULL, NULL, $1); }
944 declarator:
945 '*' m_type_qual_list declarator %prec PPTR
946 { $$ = $3; $$->type = append_ptrchain_type($$->type, type_new_pointer(NULL, $2)); }
947 | callconv declarator { $$ = $2; $$->type->attrs = append_attr($$->type->attrs, make_attrp(ATTR_CALLCONV, $1)); }
948 | direct_declarator
951 direct_declarator:
952 ident { $$ = make_declarator($1); }
953 | '(' declarator ')' { $$ = $2; }
954 | direct_declarator array { $$ = $1; $$->array = append_array($$->array, $2); }
955 | direct_declarator '(' m_args ')' { $$ = $1;
956 $$->func_type = append_ptrchain_type($$->type, type_new_function($3));
957 $$->type = NULL;
961 declarator_list:
962 declarator { $$ = append_declarator( NULL, $1 ); }
963 | declarator_list ',' declarator { $$ = append_declarator( $1, $3 ); }
966 init_declarator:
967 declarator { $$ = $1; }
968 | declarator '=' expr_const { $$ = $1; $1->var->eval = $3; }
971 pointer_type:
972 tREF { $$ = RPC_FC_RP; }
973 | tUNIQUE { $$ = RPC_FC_UP; }
974 | tPTR { $$ = RPC_FC_FP; }
977 structdef: tSTRUCT t_ident '{' fields '}' { $$ = type_new_struct($2, TRUE, $4); }
980 type: tVOID { $$ = type_new_void(); }
981 | aKNOWNTYPE { $$ = find_type_or_error($1, 0); }
982 | base_type { $$ = $1; }
983 | enumdef { $$ = $1; }
984 | tENUM aIDENTIFIER { $$ = find_type_or_error2($2, tsENUM); }
985 | structdef { $$ = $1; }
986 | tSTRUCT aIDENTIFIER { $$ = type_new_struct($2, FALSE, NULL); }
987 | uniondef { $$ = $1; }
988 | tUNION aIDENTIFIER { $$ = find_type_or_error2($2, tsUNION); }
989 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
992 typedef: tTYPEDEF m_attributes decl_spec declarator_list
993 { reg_typedefs($3, $4, check_typedef_attrs($2));
994 $$ = make_statement_typedef($4);
998 uniondef: tUNION t_ident '{' ne_union_fields '}'
999 { $$ = type_new_nonencapsulated_union($2, $4); }
1000 | tUNION t_ident
1001 tSWITCH '(' s_field ')'
1002 m_ident '{' cases '}' { $$ = type_new_encapsulated_union($2, $5, $7, $9); }
1005 version:
1006 aNUM { $$ = MAKEVERSION($1, 0); }
1007 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
1012 static void decl_builtin_basic(const char *name, enum type_basic_type type)
1014 type_t *t = type_new_basic(type);
1015 reg_type(t, name, 0);
1018 static void decl_builtin_alias(const char *name, type_t *t)
1020 reg_type(type_new_alias(t, name), name, 0);
1023 void init_types(void)
1025 decl_builtin_basic("byte", TYPE_BASIC_BYTE);
1026 decl_builtin_basic("wchar_t", TYPE_BASIC_WCHAR);
1027 decl_builtin_basic("float", TYPE_BASIC_FLOAT);
1028 decl_builtin_basic("double", TYPE_BASIC_DOUBLE);
1029 decl_builtin_basic("error_status_t", TYPE_BASIC_ERROR_STATUS_T);
1030 decl_builtin_basic("handle_t", TYPE_BASIC_HANDLE);
1031 decl_builtin_alias("boolean", type_new_basic(TYPE_BASIC_BYTE));
1034 static str_list_t *append_str(str_list_t *list, char *str)
1036 struct str_list_entry_t *entry;
1038 if (!str) return list;
1039 if (!list)
1041 list = xmalloc( sizeof(*list) );
1042 list_init( list );
1044 entry = xmalloc( sizeof(*entry) );
1045 entry->str = str;
1046 list_add_tail( list, &entry->entry );
1047 return list;
1050 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1052 attr_t *attr_existing;
1053 if (!attr) return list;
1054 if (!list)
1056 list = xmalloc( sizeof(*list) );
1057 list_init( list );
1059 LIST_FOR_EACH_ENTRY(attr_existing, list, attr_t, entry)
1060 if (attr_existing->type == attr->type)
1062 parser_warning("duplicate attribute %s\n", get_attr_display_name(attr->type));
1063 /* use the last attribute, like MIDL does */
1064 list_remove(&attr_existing->entry);
1065 break;
1067 list_add_tail( list, &attr->entry );
1068 return list;
1071 static attr_list_t *move_attr(attr_list_t *dst, attr_list_t *src, enum attr_type type)
1073 attr_t *attr;
1074 if (!src) return dst;
1075 LIST_FOR_EACH_ENTRY(attr, src, attr_t, entry)
1076 if (attr->type == type)
1078 list_remove(&attr->entry);
1079 return append_attr(dst, attr);
1081 return dst;
1084 static attr_list_t *append_attr_list(attr_list_t *new_list, attr_list_t *old_list)
1086 struct list *entry;
1088 if (!old_list) return new_list;
1090 while ((entry = list_head(old_list)))
1092 attr_t *attr = LIST_ENTRY(entry, attr_t, entry);
1093 list_remove(entry);
1094 new_list = append_attr(new_list, attr);
1096 return new_list;
1099 static attr_list_t *dupattrs(const attr_list_t *list)
1101 attr_list_t *new_list;
1102 const attr_t *attr;
1104 if (!list) return NULL;
1106 new_list = xmalloc( sizeof(*list) );
1107 list_init( new_list );
1108 LIST_FOR_EACH_ENTRY(attr, list, const attr_t, entry)
1110 attr_t *new_attr = xmalloc(sizeof(*new_attr));
1111 *new_attr = *attr;
1112 list_add_tail(new_list, &new_attr->entry);
1114 return new_list;
1117 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)
1119 decl_spec_t *declspec = left ? left : right;
1120 if (!declspec)
1122 declspec = xmalloc(sizeof(*declspec));
1123 declspec->type = NULL;
1124 declspec->attrs = NULL;
1125 declspec->stgclass = STG_NONE;
1127 declspec->type = type;
1128 if (left && declspec != left)
1130 declspec->attrs = append_attr_list(declspec->attrs, left->attrs);
1131 if (declspec->stgclass == STG_NONE)
1132 declspec->stgclass = left->stgclass;
1133 else if (left->stgclass != STG_NONE)
1134 error_loc("only one storage class can be specified\n");
1135 assert(!left->type);
1136 free(left);
1138 if (right && declspec != right)
1140 declspec->attrs = append_attr_list(declspec->attrs, right->attrs);
1141 if (declspec->stgclass == STG_NONE)
1142 declspec->stgclass = right->stgclass;
1143 else if (right->stgclass != STG_NONE)
1144 error_loc("only one storage class can be specified\n");
1145 assert(!right->type);
1146 free(right);
1149 declspec->attrs = append_attr(declspec->attrs, attr);
1150 if (declspec->stgclass == STG_NONE)
1151 declspec->stgclass = stgclass;
1152 else if (stgclass != STG_NONE)
1153 error_loc("only one storage class can be specified\n");
1155 /* apply attributes to type */
1156 if (type && declspec->attrs)
1158 attr_list_t *attrs;
1159 declspec->type = duptype(type, 1);
1160 attrs = dupattrs(type->attrs);
1161 declspec->type->attrs = append_attr_list(attrs, declspec->attrs);
1162 declspec->attrs = NULL;
1165 return declspec;
1168 static attr_t *make_attr(enum attr_type type)
1170 attr_t *a = xmalloc(sizeof(attr_t));
1171 a->type = type;
1172 a->u.ival = 0;
1173 return a;
1176 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1178 attr_t *a = xmalloc(sizeof(attr_t));
1179 a->type = type;
1180 a->u.ival = val;
1181 return a;
1184 static attr_t *make_attrp(enum attr_type type, void *val)
1186 attr_t *a = xmalloc(sizeof(attr_t));
1187 a->type = type;
1188 a->u.pval = val;
1189 return a;
1192 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1194 if (!expr) return list;
1195 if (!list)
1197 list = xmalloc( sizeof(*list) );
1198 list_init( list );
1200 list_add_tail( list, &expr->entry );
1201 return list;
1204 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1206 if (!expr) return list;
1207 if (!list)
1209 list = xmalloc( sizeof(*list) );
1210 list_init( list );
1212 list_add_tail( list, &expr->entry );
1213 return list;
1216 static struct list type_pool = LIST_INIT(type_pool);
1217 typedef struct
1219 type_t data;
1220 struct list link;
1221 } type_pool_node_t;
1223 type_t *alloc_type(void)
1225 type_pool_node_t *node = xmalloc(sizeof *node);
1226 list_add_tail(&type_pool, &node->link);
1227 return &node->data;
1230 void set_all_tfswrite(int val)
1232 type_pool_node_t *node;
1233 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1234 node->data.tfswrite = val;
1237 void clear_all_offsets(void)
1239 type_pool_node_t *node;
1240 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1241 node->data.typestring_offset = node->data.ptrdesc = 0;
1244 type_t *make_type(unsigned char type, type_t *ref)
1246 type_t *t = alloc_type();
1247 t->name = NULL;
1248 t->type = type;
1249 t->ref = ref;
1250 t->attrs = NULL;
1251 t->orig = NULL;
1252 memset(&t->details, 0, sizeof(t->details));
1253 t->typestring_offset = 0;
1254 t->ptrdesc = 0;
1255 t->ignore = (parse_only != 0);
1256 t->defined = FALSE;
1257 t->written = FALSE;
1258 t->user_types_registered = FALSE;
1259 t->tfswrite = FALSE;
1260 t->checked = FALSE;
1261 t->is_alias = FALSE;
1262 t->typelib_idx = -1;
1263 init_loc_info(&t->loc_info);
1264 return t;
1267 static type_t *type_new_enum(char *name, var_list_t *enums)
1269 type_t *t = get_type(RPC_FC_ENUM16, name, tsENUM);
1270 if (enums)
1272 t->details.enumeration = xmalloc(sizeof(*t->details.enumeration));
1273 t->details.enumeration->enums = enums;
1275 else
1276 t->details.enumeration = NULL;
1277 t->defined = TRUE;
1278 return t;
1281 static type_t *type_new_struct(char *name, int defined, var_list_t *fields)
1283 type_t *tag_type = name ? find_type(name, tsSTRUCT) : NULL;
1284 type_t *t = make_type(RPC_FC_STRUCT, NULL);
1285 t->name = name;
1286 if (defined || (tag_type && tag_type->details.structure))
1288 if (tag_type && tag_type->details.structure)
1290 t->details.structure = tag_type->details.structure;
1291 t->type = tag_type->type;
1293 else if (defined)
1295 t->details.structure = xmalloc(sizeof(*t->details.structure));
1296 t->details.structure->fields = fields;
1297 t->defined = TRUE;
1300 if (name)
1302 if (fields)
1303 reg_type(t, name, tsSTRUCT);
1304 else
1305 add_incomplete(t);
1307 return t;
1310 static type_t *type_new_nonencapsulated_union(char *name, var_list_t *fields)
1312 type_t *t = get_type(RPC_FC_NON_ENCAPSULATED_UNION, name, tsUNION);
1313 t->details.structure = xmalloc(sizeof(*t->details.structure));
1314 t->details.structure->fields = fields;
1315 t->defined = TRUE;
1316 return t;
1319 static type_t *type_new_encapsulated_union(char *name, var_t *switch_field, var_t *union_field, var_list_t *cases)
1321 type_t *t = get_type(RPC_FC_ENCAPSULATED_UNION, name, tsUNION);
1322 if (!union_field) union_field = make_var( xstrdup("tagged_union") );
1323 union_field->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
1324 union_field->type->details.structure = xmalloc(sizeof(*union_field->type->details.structure));
1325 union_field->type->details.structure->fields = cases;
1326 union_field->type->defined = TRUE;
1327 t->details.structure = xmalloc(sizeof(*t->details.structure));
1328 t->details.structure->fields = append_var( NULL, switch_field );
1329 t->details.structure->fields = append_var( t->details.structure->fields, union_field );
1330 t->defined = TRUE;
1331 return t;
1334 static void type_function_add_head_arg(type_t *type, var_t *arg)
1336 if (!type->details.function->args)
1338 type->details.function->args = xmalloc( sizeof(*type->details.function->args) );
1339 list_init( type->details.function->args );
1341 list_add_head( type->details.function->args, &arg->entry );
1344 static type_t *append_ptrchain_type(type_t *ptrchain, type_t *type)
1346 type_t *ptrchain_type;
1347 if (!ptrchain)
1348 return type;
1349 for (ptrchain_type = ptrchain; ptrchain_type->ref; ptrchain_type = ptrchain_type->ref)
1351 ptrchain_type->ref = type;
1352 return ptrchain;
1355 static void set_type(var_t *v, decl_spec_t *decl_spec, const declarator_t *decl,
1356 int top)
1358 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1359 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1360 int sizeless;
1361 expr_t *dim;
1362 type_t **ptype;
1363 array_dims_t *arr = decl ? decl->array : NULL;
1364 type_t *func_type = decl ? decl->func_type : NULL;
1365 type_t *type = decl_spec->type;
1367 if (is_attr(type->attrs, ATTR_INLINE))
1369 if (!func_type)
1370 error_loc("inline attribute applied to non-function type\n");
1371 else
1373 type_t *t;
1374 /* move inline attribute from return type node to function node */
1375 for (t = func_type; is_ptr(t); t = type_pointer_get_ref(t))
1377 t->attrs = move_attr(t->attrs, type->attrs, ATTR_INLINE);
1381 /* add type onto the end of the pointers in pident->type */
1382 v->type = append_ptrchain_type(decl ? decl->type : NULL, type);
1383 v->stgclass = decl_spec->stgclass;
1385 /* the highest level of pointer specified should default to the var's ptr attr
1386 * or (RPC_FC_RP if not specified and it's a top level ptr), not
1387 * pointer_default so we need to fix that up here */
1388 if (!arr)
1390 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1391 const type_t *ptr = NULL;
1392 /* pointer attributes on the left side of the type belong to the function
1393 * pointer, if one is being declared */
1394 type_t **pt = func_type ? &func_type : &v->type;
1395 for (ptr = *pt; ptr && !ptr_attr; )
1397 ptr_attr = get_attrv(ptr->attrs, ATTR_POINTERTYPE);
1398 if (!ptr_attr && type_is_alias(ptr))
1399 ptr = type_alias_get_aliasee(ptr);
1400 else
1401 break;
1403 if (ptr && is_ptr(ptr) && (ptr_attr || top))
1405 if (ptr_attr && ptr_attr != RPC_FC_UP &&
1406 type_get_type(type_pointer_get_ref(ptr)) == TYPE_INTERFACE)
1407 warning_loc_info(&v->loc_info,
1408 "%s: pointer attribute applied to interface "
1409 "pointer type has no effect\n", v->name);
1410 if (top && !ptr_attr)
1411 ptr_attr = RPC_FC_RP;
1412 if (ptr_attr != (*pt)->type)
1414 /* create new type to avoid changing original type */
1415 /* FIXME: this is a horrible hack - we might be changing the pointer
1416 * type of an alias here, so we also need corresponding hacks in
1417 * get_pointer_fc to handle this. The type of pointer that the type
1418 * ends up having is context sensitive and so we shouldn't be
1419 * setting it here, but rather determining it when it is used. */
1420 *pt = duptype(*pt, 1);
1421 (*pt)->type = ptr_attr;
1424 else if (ptr_attr)
1425 error_loc("%s: pointer attribute applied to non-pointer type\n", v->name);
1428 if (is_attr(v->attrs, ATTR_STRING) && !is_ptr(v->type) && !arr)
1429 error_loc("'%s': [string] attribute applied to non-pointer, non-array type\n",
1430 v->name);
1432 if (is_attr(v->attrs, ATTR_V1ENUM))
1434 if (type_get_type_detect_alias(v->type) == TYPE_ENUM)
1435 v->type->type = RPC_FC_ENUM32;
1436 else
1437 error_loc("'%s': [v1_enum] attribute applied to non-enum type\n", v->name);
1440 ptype = &v->type;
1441 sizeless = FALSE;
1442 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1444 if (sizeless)
1445 error_loc("%s: only the first array dimension can be unspecified\n", v->name);
1447 if (dim->is_const)
1449 if (dim->cval <= 0)
1450 error_loc("%s: array dimension must be positive\n", v->name);
1452 /* FIXME: should use a type_memsize that allows us to pass in a pointer size */
1453 if (0)
1455 unsigned int align = 0;
1456 unsigned int size = type_memsize(v->type, &align);
1458 if (0xffffffffu / size < dim->cval)
1459 error_loc("%s: total array size is too large\n", v->name);
1462 else
1463 sizeless = TRUE;
1465 *ptype = type_new_array(NULL, *ptype, FALSE,
1466 dim->is_const ? dim->cval : 0,
1467 dim->is_const ? NULL : dim, NULL);
1470 ptype = &v->type;
1471 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1473 if (dim->type != EXPR_VOID)
1475 if (is_array(*ptype))
1477 if (type_array_get_conformance(*ptype)->is_const)
1478 error_loc("%s: cannot specify size_is for a fixed sized array\n", v->name);
1479 else
1480 *ptype = type_new_array((*ptype)->name,
1481 type_array_get_element(*ptype), FALSE,
1482 0, dim, NULL);
1484 else if (is_ptr(*ptype))
1485 *ptype = type_new_array((*ptype)->name, type_pointer_get_ref(*ptype), TRUE,
1486 0, dim, NULL);
1487 else
1488 error_loc("%s: size_is attribute applied to illegal type\n", v->name);
1491 ptype = &(*ptype)->ref;
1492 if (*ptype == NULL)
1493 error_loc("%s: too many expressions in size_is attribute\n", v->name);
1496 ptype = &v->type;
1497 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1499 if (dim->type != EXPR_VOID)
1501 if (is_array(*ptype))
1503 *ptype = type_new_array((*ptype)->name,
1504 type_array_get_element(*ptype),
1505 type_array_is_decl_as_ptr(*ptype),
1506 type_array_get_dim(*ptype),
1507 type_array_get_conformance(*ptype),
1508 dim);
1510 else
1511 error_loc("%s: length_is attribute applied to illegal type\n", v->name);
1514 ptype = &(*ptype)->ref;
1515 if (*ptype == NULL)
1516 error_loc("%s: too many expressions in length_is attribute\n", v->name);
1519 /* v->type is currently pointing to the type on the left-side of the
1520 * declaration, so we need to fix this up so that it is the return type of the
1521 * function and make v->type point to the function side of the declaration */
1522 if (func_type)
1524 type_t *ft, *t;
1525 type_t *return_type = v->type;
1526 v->type = func_type;
1527 for (ft = v->type; is_ptr(ft); ft = type_pointer_get_ref(ft))
1529 assert(type_get_type_detect_alias(ft) == TYPE_FUNCTION);
1530 ft->ref = return_type;
1531 /* move calling convention attribute, if present, from pointer nodes to
1532 * function node */
1533 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1534 ft->attrs = move_attr(ft->attrs, t->attrs, ATTR_CALLCONV);
1535 if (is_object_interface && !is_attr(ft->attrs, ATTR_CALLCONV))
1537 static char *stdmethodcalltype;
1538 if (!stdmethodcalltype) stdmethodcalltype = strdup("STDMETHODCALLTYPE");
1539 ft->attrs = append_attr(NULL, make_attrp(ATTR_CALLCONV, stdmethodcalltype));
1542 else
1544 type_t *t;
1545 for (t = v->type; is_ptr(t); t = type_pointer_get_ref(t))
1546 if (is_attr(t->attrs, ATTR_CALLCONV))
1547 error_loc("calling convention applied to non-function-pointer type\n");
1551 static var_list_t *set_var_types(attr_list_t *attrs, decl_spec_t *decl_spec, declarator_list_t *decls)
1553 declarator_t *decl, *next;
1554 var_list_t *var_list = NULL;
1556 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
1558 var_t *var = decl->var;
1560 var->attrs = attrs;
1561 set_type(var, decl_spec, decl, 0);
1562 var_list = append_var(var_list, var);
1563 free(decl);
1565 return var_list;
1568 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1570 if (!iface) return list;
1571 if (!list)
1573 list = xmalloc( sizeof(*list) );
1574 list_init( list );
1576 list_add_tail( list, &iface->entry );
1577 return list;
1580 static ifref_t *make_ifref(type_t *iface)
1582 ifref_t *l = xmalloc(sizeof(ifref_t));
1583 l->iface = iface;
1584 l->attrs = NULL;
1585 return l;
1588 static var_list_t *append_var(var_list_t *list, var_t *var)
1590 if (!var) return list;
1591 if (!list)
1593 list = xmalloc( sizeof(*list) );
1594 list_init( list );
1596 list_add_tail( list, &var->entry );
1597 return list;
1600 static var_list_t *append_var_list(var_list_t *list, var_list_t *vars)
1602 if (!vars) return list;
1603 if (!list)
1605 list = xmalloc( sizeof(*list) );
1606 list_init( list );
1608 list_move_tail( list, vars );
1609 return list;
1612 static var_t *make_var(char *name)
1614 var_t *v = xmalloc(sizeof(var_t));
1615 v->name = name;
1616 v->type = NULL;
1617 v->attrs = NULL;
1618 v->eval = NULL;
1619 v->stgclass = STG_NONE;
1620 init_loc_info(&v->loc_info);
1621 return v;
1624 static declarator_list_t *append_declarator(declarator_list_t *list, declarator_t *d)
1626 if (!d) return list;
1627 if (!list) {
1628 list = xmalloc(sizeof(*list));
1629 list_init(list);
1631 list_add_tail(list, &d->entry);
1632 return list;
1635 static declarator_t *make_declarator(var_t *var)
1637 declarator_t *d = xmalloc(sizeof(*d));
1638 d->var = var;
1639 d->type = NULL;
1640 d->func_type = NULL;
1641 d->array = NULL;
1642 return d;
1645 static func_list_t *append_func(func_list_t *list, func_t *func)
1647 if (!func) return list;
1648 if (!list)
1650 list = xmalloc( sizeof(*list) );
1651 list_init( list );
1653 list_add_tail( list, &func->entry );
1654 return list;
1657 static func_t *make_func(var_t *def)
1659 func_t *f = xmalloc(sizeof(func_t));
1660 f->def = def;
1661 return f;
1664 static type_t *make_class(char *name)
1666 type_t *c = make_type(RPC_FC_COCLASS, NULL);
1667 c->name = name;
1668 return c;
1671 static type_t *make_safearray(type_t *type)
1673 type_t *sa = find_type_or_error("SAFEARRAY", 0);
1674 sa->ref = type;
1675 return make_type(pointer_default, sa);
1678 static typelib_t *make_library(const char *name, const attr_list_t *attrs)
1680 typelib_t *typelib = xmalloc(sizeof(*typelib));
1681 typelib->name = xstrdup(name);
1682 typelib->filename = NULL;
1683 typelib->attrs = attrs;
1684 list_init( &typelib->importlibs );
1685 return typelib;
1688 #define HASHMAX 64
1690 static int hash_ident(const char *name)
1692 const char *p = name;
1693 int sum = 0;
1694 /* a simple sum hash is probably good enough */
1695 while (*p) {
1696 sum += *p;
1697 p++;
1699 return sum & (HASHMAX-1);
1702 /***** type repository *****/
1704 struct rtype {
1705 const char *name;
1706 type_t *type;
1707 int t;
1708 struct rtype *next;
1711 struct rtype *type_hash[HASHMAX];
1713 static type_t *reg_type(type_t *type, const char *name, int t)
1715 struct rtype *nt;
1716 int hash;
1717 if (!name) {
1718 error_loc("registering named type without name\n");
1719 return type;
1721 hash = hash_ident(name);
1722 nt = xmalloc(sizeof(struct rtype));
1723 nt->name = name;
1724 nt->type = type;
1725 nt->t = t;
1726 nt->next = type_hash[hash];
1727 type_hash[hash] = nt;
1728 if ((t == tsSTRUCT || t == tsUNION))
1729 fix_incomplete_types(type);
1730 return type;
1733 static int is_incomplete(const type_t *t)
1735 return !t->defined &&
1736 (type_get_type_detect_alias(t) == TYPE_STRUCT ||
1737 type_get_type_detect_alias(t) == TYPE_UNION ||
1738 type_get_type_detect_alias(t) == TYPE_ENCAPSULATED_UNION);
1741 static void add_incomplete(type_t *t)
1743 struct typenode *tn = xmalloc(sizeof *tn);
1744 tn->type = t;
1745 list_add_tail(&incomplete_types, &tn->entry);
1748 static void fix_type(type_t *t)
1750 if (type_is_alias(t) && is_incomplete(t)) {
1751 type_t *ot = type_alias_get_aliasee(t);
1752 fix_type(ot);
1753 if (type_get_type_detect_alias(ot) == TYPE_STRUCT ||
1754 type_get_type_detect_alias(ot) == TYPE_UNION ||
1755 type_get_type_detect_alias(ot) == TYPE_ENCAPSULATED_UNION)
1756 t->details.structure = ot->details.structure;
1757 t->defined = ot->defined;
1761 static void fix_incomplete(void)
1763 struct typenode *tn, *next;
1765 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1766 fix_type(tn->type);
1767 list_remove(&tn->entry);
1768 free(tn);
1772 static void fix_incomplete_types(type_t *complete_type)
1774 struct typenode *tn, *next;
1776 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry)
1778 if (type_is_equal(complete_type, tn->type))
1780 tn->type->details.structure = complete_type->details.structure;
1781 list_remove(&tn->entry);
1782 free(tn);
1787 static type_t *reg_typedefs(decl_spec_t *decl_spec, declarator_list_t *decls, attr_list_t *attrs)
1789 const declarator_t *decl;
1790 int is_str = is_attr(attrs, ATTR_STRING);
1791 type_t *type = decl_spec->type;
1793 if (is_str)
1795 type_t *t = decl_spec->type;
1797 while (is_ptr(t))
1798 t = type_pointer_get_ref(t);
1800 if (type_get_type(t) != TYPE_BASIC &&
1801 (type_basic_get_fc(t) != RPC_FC_CHAR &&
1802 type_basic_get_fc(t) != RPC_FC_BYTE &&
1803 type_basic_get_fc(t) != RPC_FC_WCHAR))
1805 decl = LIST_ENTRY( list_head( decls ), const declarator_t, entry );
1806 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1807 decl->var->name);
1811 /* We must generate names for tagless enum, struct or union.
1812 Typedef-ing a tagless enum, struct or union means we want the typedef
1813 to be included in a library hence the public attribute. */
1814 if ((type_get_type_detect_alias(type) == TYPE_ENUM ||
1815 type_get_type_detect_alias(type) == TYPE_STRUCT ||
1816 type_get_type_detect_alias(type) == TYPE_UNION ||
1817 type_get_type_detect_alias(type) == TYPE_ENCAPSULATED_UNION) &&
1818 !type->name && !parse_only)
1820 if (! is_attr(attrs, ATTR_PUBLIC))
1821 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1822 type->name = gen_name();
1824 else if (is_attr(attrs, ATTR_UUID) && !is_attr(attrs, ATTR_PUBLIC))
1825 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1827 LIST_FOR_EACH_ENTRY( decl, decls, const declarator_t, entry )
1829 var_t *name = decl->var;
1831 if (name->name) {
1832 type_t *cur;
1834 cur = find_type(name->name, 0);
1835 if (cur)
1836 error_loc("%s: redefinition error; original definition was at %s:%d\n",
1837 cur->name, cur->loc_info.input_name,
1838 cur->loc_info.line_number);
1840 /* set the attributes to allow set_type to do some checks on them */
1841 name->attrs = attrs;
1842 set_type(name, decl_spec, decl, 0);
1843 cur = type_new_alias(name->type, name->name);
1844 cur->attrs = attrs;
1846 if (is_incomplete(cur))
1847 add_incomplete(cur);
1848 reg_type(cur, cur->name, 0);
1851 return type;
1854 type_t *find_type(const char *name, int t)
1856 struct rtype *cur = type_hash[hash_ident(name)];
1857 while (cur && (cur->t != t || strcmp(cur->name, name)))
1858 cur = cur->next;
1859 return cur ? cur->type : NULL;
1862 static type_t *find_type_or_error(const char *name, int t)
1864 type_t *type = find_type(name, t);
1865 if (!type) {
1866 error_loc("type '%s' not found\n", name);
1867 return NULL;
1869 return type;
1872 static type_t *find_type_or_error2(char *name, int t)
1874 type_t *tp = find_type_or_error(name, t);
1875 free(name);
1876 return tp;
1879 int is_type(const char *name)
1881 return find_type(name, 0) != NULL;
1884 static type_t *get_type(unsigned char type, char *name, int t)
1886 type_t *tp;
1887 if (name) {
1888 tp = find_type(name, t);
1889 if (tp) {
1890 free(name);
1891 return tp;
1894 tp = make_type(type, NULL);
1895 tp->name = name;
1896 if (!name) return tp;
1897 return reg_type(tp, name, t);
1900 /***** constant repository *****/
1902 struct rconst {
1903 char *name;
1904 var_t *var;
1905 struct rconst *next;
1908 struct rconst *const_hash[HASHMAX];
1910 static var_t *reg_const(var_t *var)
1912 struct rconst *nc;
1913 int hash;
1914 if (!var->name) {
1915 error_loc("registering constant without name\n");
1916 return var;
1918 hash = hash_ident(var->name);
1919 nc = xmalloc(sizeof(struct rconst));
1920 nc->name = var->name;
1921 nc->var = var;
1922 nc->next = const_hash[hash];
1923 const_hash[hash] = nc;
1924 return var;
1927 var_t *find_const(const char *name, int f)
1929 struct rconst *cur = const_hash[hash_ident(name)];
1930 while (cur && strcmp(cur->name, name))
1931 cur = cur->next;
1932 if (!cur) {
1933 if (f) error_loc("constant '%s' not found\n", name);
1934 return NULL;
1936 return cur->var;
1939 static char *gen_name(void)
1941 static const char format[] = "__WIDL_%s_generated_name_%08lX";
1942 static unsigned long n = 0;
1943 static const char *file_id;
1944 static size_t size;
1945 char *name;
1947 if (! file_id)
1949 char *dst = dup_basename(input_name, ".idl");
1950 file_id = dst;
1952 for (; *dst; ++dst)
1953 if (! isalnum((unsigned char) *dst))
1954 *dst = '_';
1956 size = sizeof format - 7 + strlen(file_id) + 8;
1959 name = xmalloc(size);
1960 sprintf(name, format, file_id, n++);
1961 return name;
1964 struct allowed_attr
1966 unsigned int dce_compatible : 1;
1967 unsigned int acf : 1;
1968 unsigned int on_interface : 1;
1969 unsigned int on_function : 1;
1970 unsigned int on_arg : 1;
1971 unsigned int on_type : 1;
1972 unsigned int on_enum : 1;
1973 unsigned int on_struct : 1;
1974 unsigned int on_union : 1;
1975 unsigned int on_field : 1;
1976 unsigned int on_library : 1;
1977 unsigned int on_dispinterface : 1;
1978 unsigned int on_module : 1;
1979 unsigned int on_coclass : 1;
1980 const char *display_name;
1983 struct allowed_attr allowed_attr[] =
1985 /* attr { D ACF I Fn ARG T En St Un Fi L DI M C <display name> } */
1986 /* ATTR_AGGREGATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "aggregatable" },
1987 /* ATTR_APPOBJECT */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "appobject" },
1988 /* ATTR_ASYNC */ { 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "async" },
1989 /* ATTR_AUTO_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "auto_handle" },
1990 /* ATTR_BINDABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "bindable" },
1991 /* ATTR_BROADCAST */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "broadcast" },
1992 /* ATTR_CALLAS */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "call_as" },
1993 /* ATTR_CALLCONV */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
1994 /* ATTR_CASE */ { 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "case" },
1995 /* ATTR_CONST */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "const" },
1996 /* ATTR_CONTEXTHANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "context_handle" },
1997 /* ATTR_CONTROL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, "control" },
1998 /* ATTR_DEFAULT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, "default" },
1999 /* ATTR_DEFAULTCOLLELEM */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultcollelem" },
2000 /* ATTR_DEFAULTVALUE */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "defaultvalue" },
2001 /* ATTR_DEFAULTVTABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "defaultvtable" },
2002 /* ATTR_DISPINTERFACE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL },
2003 /* ATTR_DISPLAYBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "displaybind" },
2004 /* ATTR_DLLNAME */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, "dllname" },
2005 /* ATTR_DUAL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "dual" },
2006 /* ATTR_ENDPOINT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "endpoint" },
2007 /* ATTR_ENTRY */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "entry" },
2008 /* ATTR_EXPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "explicit_handle" },
2009 /* ATTR_HANDLE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "handle" },
2010 /* ATTR_HELPCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpcontext" },
2011 /* ATTR_HELPFILE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpfile" },
2012 /* ATTR_HELPSTRING */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstring" },
2013 /* ATTR_HELPSTRINGCONTEXT */ { 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "helpstringcontext" },
2014 /* ATTR_HELPSTRINGDLL */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "helpstringdll" },
2015 /* ATTR_HIDDEN */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, "hidden" },
2016 /* ATTR_ID */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, "id" },
2017 /* ATTR_IDEMPOTENT */ { 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "idempotent" },
2018 /* ATTR_IIDIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "iid_is" },
2019 /* ATTR_IMMEDIATEBIND */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "immediatebind" },
2020 /* ATTR_IMPLICIT_HANDLE */ { 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "implicit_handle" },
2021 /* ATTR_IN */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "in" },
2022 /* ATTR_INLINE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inline" },
2023 /* ATTR_INPUTSYNC */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "inputsync" },
2024 /* ATTR_LENGTHIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "length_is" },
2025 /* ATTR_LIBLCID */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "lcid" },
2026 /* ATTR_LOCAL */ { 1, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "local" },
2027 /* ATTR_NONBROWSABLE */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonbrowsable" },
2028 /* ATTR_NONCREATABLE */ { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, "noncreatable" },
2029 /* ATTR_NONEXTENSIBLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "nonextensible" },
2030 /* ATTR_OBJECT */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "object" },
2031 /* ATTR_ODL */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "odl" },
2032 /* ATTR_OLEAUTOMATION */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "oleautomation" },
2033 /* ATTR_OPTIONAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "optional" },
2034 /* ATTR_OUT */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "out" },
2035 /* ATTR_POINTERDEFAULT */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "pointer_default" },
2036 /* ATTR_POINTERTYPE */ { 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "ref, unique or ptr" },
2037 /* ATTR_PROPGET */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propget" },
2038 /* ATTR_PROPPUT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propput" },
2039 /* ATTR_PROPPUTREF */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "propputref" },
2040 /* ATTR_PUBLIC */ { 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "public" },
2041 /* ATTR_RANGE */ { 0, 0, 0, 0, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, "range" },
2042 /* ATTR_READONLY */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "readonly" },
2043 /* ATTR_REQUESTEDIT */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "requestedit" },
2044 /* ATTR_RESTRICTED */ { 0, 0, 1, 1, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, "restricted" },
2045 /* ATTR_RETVAL */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, "retval" },
2046 /* ATTR_SIZEIS */ { 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "size_is" },
2047 /* ATTR_SOURCE */ { 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, "source" },
2048 /* ATTR_STRICTCONTEXTHANDLE */ { 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "strict_context_handle" },
2049 /* ATTR_STRING */ { 1, 0, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, "string" },
2050 /* ATTR_SWITCHIS */ { 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, "switch_is" },
2051 /* ATTR_SWITCHTYPE */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, "switch_type" },
2052 /* ATTR_TRANSMITAS */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "transmit_as" },
2053 /* ATTR_UUID */ { 1, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 1, "uuid" },
2054 /* ATTR_V1ENUM */ { 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, "v1_enum" },
2055 /* ATTR_VARARG */ { 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "vararg" },
2056 /* ATTR_VERSION */ { 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, "version" },
2057 /* ATTR_WIREMARSHAL */ { 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, "wire_marshal" },
2060 const char *get_attr_display_name(enum attr_type type)
2062 return allowed_attr[type].display_name;
2065 static attr_list_t *check_iface_attrs(const char *name, attr_list_t *attrs)
2067 const attr_t *attr;
2068 if (!attrs) return attrs;
2069 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2071 if (!allowed_attr[attr->type].on_interface)
2072 error_loc("inapplicable attribute %s for interface %s\n",
2073 allowed_attr[attr->type].display_name, name);
2075 return attrs;
2078 static attr_list_t *check_function_attrs(const char *name, attr_list_t *attrs)
2080 const attr_t *attr;
2081 if (!attrs) return attrs;
2082 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2084 if (!allowed_attr[attr->type].on_function)
2085 error_loc("inapplicable attribute %s for function %s\n",
2086 allowed_attr[attr->type].display_name, name);
2088 return attrs;
2091 static void check_arg(var_t *arg)
2093 const type_t *t = arg->type;
2094 const attr_t *attr;
2096 if (type_get_type(t) == TYPE_VOID)
2097 error_loc("argument '%s' has void type\n", arg->name);
2099 if (arg->attrs)
2101 LIST_FOR_EACH_ENTRY(attr, arg->attrs, const attr_t, entry)
2103 if (!allowed_attr[attr->type].on_arg)
2104 error_loc("inapplicable attribute %s for argument %s\n",
2105 allowed_attr[attr->type].display_name, arg->name);
2110 static attr_list_t *check_typedef_attrs(attr_list_t *attrs)
2112 const attr_t *attr;
2113 if (!attrs) return attrs;
2114 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2116 if (!allowed_attr[attr->type].on_type)
2117 error_loc("inapplicable attribute %s for typedef\n",
2118 allowed_attr[attr->type].display_name);
2120 return attrs;
2123 static attr_list_t *check_enum_attrs(attr_list_t *attrs)
2125 const attr_t *attr;
2126 if (!attrs) return attrs;
2127 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2129 if (!allowed_attr[attr->type].on_enum)
2130 error_loc("inapplicable attribute %s for enum\n",
2131 allowed_attr[attr->type].display_name);
2133 return attrs;
2136 static attr_list_t *check_struct_attrs(attr_list_t *attrs)
2138 const attr_t *attr;
2139 if (!attrs) return attrs;
2140 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2142 if (!allowed_attr[attr->type].on_struct)
2143 error_loc("inapplicable attribute %s for struct\n",
2144 allowed_attr[attr->type].display_name);
2146 return attrs;
2149 static attr_list_t *check_union_attrs(attr_list_t *attrs)
2151 const attr_t *attr;
2152 if (!attrs) return attrs;
2153 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2155 if (!allowed_attr[attr->type].on_union)
2156 error_loc("inapplicable attribute %s for union\n",
2157 allowed_attr[attr->type].display_name);
2159 return attrs;
2162 static attr_list_t *check_field_attrs(const char *name, attr_list_t *attrs)
2164 const attr_t *attr;
2165 if (!attrs) return attrs;
2166 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2168 if (!allowed_attr[attr->type].on_field)
2169 error_loc("inapplicable attribute %s for field %s\n",
2170 allowed_attr[attr->type].display_name, name);
2172 return attrs;
2175 static attr_list_t *check_library_attrs(const char *name, attr_list_t *attrs)
2177 const attr_t *attr;
2178 if (!attrs) return attrs;
2179 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2181 if (!allowed_attr[attr->type].on_library)
2182 error_loc("inapplicable attribute %s for library %s\n",
2183 allowed_attr[attr->type].display_name, name);
2185 return attrs;
2188 static attr_list_t *check_dispiface_attrs(const char *name, attr_list_t *attrs)
2190 const attr_t *attr;
2191 if (!attrs) return attrs;
2192 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2194 if (!allowed_attr[attr->type].on_dispinterface)
2195 error_loc("inapplicable attribute %s for dispinterface %s\n",
2196 allowed_attr[attr->type].display_name, name);
2198 return attrs;
2201 static attr_list_t *check_module_attrs(const char *name, attr_list_t *attrs)
2203 const attr_t *attr;
2204 if (!attrs) return attrs;
2205 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2207 if (!allowed_attr[attr->type].on_module)
2208 error_loc("inapplicable attribute %s for module %s\n",
2209 allowed_attr[attr->type].display_name, name);
2211 return attrs;
2214 static attr_list_t *check_coclass_attrs(const char *name, attr_list_t *attrs)
2216 const attr_t *attr;
2217 if (!attrs) return attrs;
2218 LIST_FOR_EACH_ENTRY(attr, attrs, const attr_t, entry)
2220 if (!allowed_attr[attr->type].on_coclass)
2221 error_loc("inapplicable attribute %s for coclass %s\n",
2222 allowed_attr[attr->type].display_name, name);
2224 return attrs;
2227 static int is_allowed_conf_type(const type_t *type)
2229 switch (type_get_type(type))
2231 case TYPE_ENUM:
2232 return TRUE;
2233 case TYPE_BASIC:
2234 switch (type_basic_get_fc(type))
2236 case RPC_FC_CHAR:
2237 case RPC_FC_SMALL:
2238 case RPC_FC_BYTE:
2239 case RPC_FC_USMALL:
2240 case RPC_FC_WCHAR:
2241 case RPC_FC_SHORT:
2242 case RPC_FC_USHORT:
2243 case RPC_FC_LONG:
2244 case RPC_FC_ULONG:
2245 case RPC_FC_ERROR_STATUS_T:
2246 return TRUE;
2247 default:
2248 return FALSE;
2250 case TYPE_ALIAS:
2251 /* shouldn't get here because of type_get_type call above */
2252 assert(0);
2253 /* fall through */
2254 case TYPE_STRUCT:
2255 case TYPE_UNION:
2256 case TYPE_ENCAPSULATED_UNION:
2257 case TYPE_ARRAY:
2258 case TYPE_POINTER:
2259 case TYPE_VOID:
2260 case TYPE_MODULE:
2261 case TYPE_COCLASS:
2262 case TYPE_FUNCTION:
2263 case TYPE_INTERFACE:
2264 return FALSE;
2266 return FALSE;
2269 static int is_ptr_guid_type(const type_t *type)
2271 unsigned int align = 0;
2273 /* first, make sure it is a pointer to something */
2274 if (!is_ptr(type)) return FALSE;
2276 /* second, make sure it is a pointer to something of size sizeof(GUID),
2277 * i.e. 16 bytes */
2278 return (type_memsize(type_pointer_get_ref(type), &align) == 16);
2281 static void check_conformance_expr_list(const char *attr_name, const var_t *arg, const type_t *container_type, expr_list_t *expr_list)
2283 expr_t *dim;
2284 struct expr_loc expr_loc;
2285 expr_loc.v = arg;
2286 expr_loc.attr = attr_name;
2287 if (expr_list) LIST_FOR_EACH_ENTRY(dim, expr_list, expr_t, entry)
2289 if (dim->type != EXPR_VOID)
2291 const type_t *expr_type = expr_resolve_type(&expr_loc, container_type, dim);
2292 if (!is_allowed_conf_type(expr_type))
2293 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2294 attr_name);
2299 static void check_remoting_fields(const var_t *var, type_t *type);
2301 /* checks that properties common to fields and arguments are consistent */
2302 static void check_field_common(const type_t *container_type,
2303 const char *container_name, const var_t *arg)
2305 type_t *type = arg->type;
2306 int more_to_do;
2307 const char *container_type_name = NULL;
2309 switch (type_get_type_detect_alias(type))
2311 case TYPE_STRUCT:
2312 container_type_name = "struct";
2313 break;
2314 case TYPE_UNION:
2315 container_type_name = "union";
2316 break;
2317 case TYPE_ENCAPSULATED_UNION:
2318 container_type_name = "encapsulated union";
2319 break;
2320 case TYPE_FUNCTION:
2321 container_type_name = "function";
2322 break;
2323 default:
2324 break;
2327 if (is_attr(arg->attrs, ATTR_LENGTHIS) &&
2328 (is_attr(arg->attrs, ATTR_STRING) || is_aliaschain_attr(arg->type, ATTR_STRING)))
2329 error_loc_info(&arg->loc_info,
2330 "string and length_is specified for argument %s are mutually exclusive attributes\n",
2331 arg->name);
2333 if (is_attr(arg->attrs, ATTR_SIZEIS))
2335 expr_list_t *size_is_exprs = get_attrp(arg->attrs, ATTR_SIZEIS);
2336 check_conformance_expr_list("size_is", arg, container_type, size_is_exprs);
2338 if (is_attr(arg->attrs, ATTR_LENGTHIS))
2340 expr_list_t *length_is_exprs = get_attrp(arg->attrs, ATTR_LENGTHIS);
2341 check_conformance_expr_list("length_is", arg, container_type, length_is_exprs);
2343 if (is_attr(arg->attrs, ATTR_IIDIS))
2345 struct expr_loc expr_loc;
2346 expr_t *expr = get_attrp(arg->attrs, ATTR_IIDIS);
2347 if (expr->type != EXPR_VOID)
2349 const type_t *expr_type;
2350 expr_loc.v = arg;
2351 expr_loc.attr = "iid_is";
2352 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2353 if (!expr_type || !is_ptr_guid_type(expr_type))
2354 error_loc_info(&arg->loc_info, "expression must resolve to pointer to GUID type for attribute iid_is\n");
2357 if (is_attr(arg->attrs, ATTR_SWITCHIS))
2359 struct expr_loc expr_loc;
2360 expr_t *expr = get_attrp(arg->attrs, ATTR_SWITCHIS);
2361 if (expr->type != EXPR_VOID)
2363 const type_t *expr_type;
2364 expr_loc.v = arg;
2365 expr_loc.attr = "switch_is";
2366 expr_type = expr_resolve_type(&expr_loc, container_type, expr);
2367 if (!expr_type || !is_allowed_conf_type(expr_type))
2368 error_loc_info(&arg->loc_info, "expression must resolve to integral type <= 32bits for attribute %s\n",
2369 expr_loc.attr);
2375 more_to_do = FALSE;
2377 switch (typegen_detect_type(type, arg->attrs, TDT_IGNORE_STRINGS))
2379 case TGT_STRUCT:
2380 case TGT_UNION:
2381 check_remoting_fields(arg, type);
2382 break;
2383 case TGT_INVALID:
2384 switch (type_get_type(type))
2386 case TYPE_VOID:
2387 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot derive from void *\n",
2388 arg->name, container_type_name, container_name);
2389 break;
2390 case TYPE_FUNCTION:
2391 error_loc_info(&arg->loc_info, "parameter \'%s\' of %s \'%s\' cannot be a function pointer\n",
2392 arg->name, container_type_name, container_name);
2393 break;
2394 case TYPE_COCLASS:
2395 case TYPE_INTERFACE:
2396 case TYPE_MODULE:
2397 /* FIXME */
2398 break;
2399 default:
2400 break;
2402 case TGT_CTXT_HANDLE:
2403 case TGT_CTXT_HANDLE_POINTER:
2404 /* FIXME */
2405 break;
2406 case TGT_POINTER:
2407 type = type_pointer_get_ref(type);
2408 more_to_do = TRUE;
2409 break;
2410 case TGT_ARRAY:
2411 type = type_array_get_element(type);
2412 more_to_do = TRUE;
2413 break;
2414 case TGT_USER_TYPE:
2415 case TGT_STRING:
2416 case TGT_IFACE_POINTER:
2417 case TGT_BASIC:
2418 case TGT_ENUM:
2419 /* nothing to do */
2420 break;
2422 } while (more_to_do);
2425 static void check_remoting_fields(const var_t *var, type_t *type)
2427 const var_t *field;
2428 const var_list_t *fields = NULL;
2430 type = type_get_real_type(type);
2432 if (type->checked)
2433 return;
2435 type->checked = TRUE;
2437 if (type_get_type(type) == TYPE_STRUCT)
2439 if (type_is_complete(type))
2440 fields = type_struct_get_fields(type);
2441 else
2442 error_loc_info(&var->loc_info, "undefined type declaration %s\n", type->name);
2444 else if (type_get_type(type) == TYPE_UNION || type_get_type(type) == TYPE_ENCAPSULATED_UNION)
2445 fields = type_union_get_cases(type);
2447 if (fields) LIST_FOR_EACH_ENTRY( field, fields, const var_t, entry )
2448 if (field->type) check_field_common(type, type->name, field);
2451 /* checks that arguments for a function make sense for marshalling and unmarshalling */
2452 static void check_remoting_args(const var_t *func)
2454 const char *funcname = func->name;
2455 const var_t *arg;
2457 if (func->type->details.function->args) LIST_FOR_EACH_ENTRY( arg, func->type->details.function->args, const var_t, entry )
2459 const type_t *type = arg->type;
2461 /* check that [out] parameters have enough pointer levels */
2462 if (is_attr(arg->attrs, ATTR_OUT))
2464 switch (typegen_detect_type(type, arg->attrs, TDT_ALL_TYPES))
2466 case TGT_BASIC:
2467 case TGT_ENUM:
2468 case TGT_STRUCT:
2469 case TGT_UNION:
2470 case TGT_CTXT_HANDLE:
2471 case TGT_USER_TYPE:
2472 error_loc_info(&arg->loc_info, "out parameter \'%s\' of function \'%s\' is not a pointer\n", arg->name, funcname);
2473 break;
2474 case TGT_IFACE_POINTER:
2475 error_loc_info(&arg->loc_info, "out interface pointer \'%s\' of function \'%s\' is not a double pointer\n", arg->name, funcname);
2476 break;
2477 case TGT_STRING:
2478 if (!is_array(type))
2480 /* FIXME */
2482 break;
2483 case TGT_INVALID:
2484 /* already error'd before we get here */
2485 case TGT_CTXT_HANDLE_POINTER:
2486 case TGT_POINTER:
2487 case TGT_ARRAY:
2488 /* OK */
2489 break;
2493 check_field_common(func->type, funcname, arg);
2497 static void add_explicit_handle_if_necessary(var_t *func)
2499 const var_t* explicit_handle_var;
2500 const var_t* explicit_generic_handle_var = NULL;
2501 const var_t* context_handle_var = NULL;
2503 /* check for a defined binding handle */
2504 explicit_handle_var = get_explicit_handle_var(func);
2505 if (!explicit_handle_var)
2507 explicit_generic_handle_var = get_explicit_generic_handle_var(func);
2508 if (!explicit_generic_handle_var)
2510 context_handle_var = get_context_handle_var(func);
2511 if (!context_handle_var)
2513 /* no explicit handle specified so add
2514 * "[in] handle_t IDL_handle" as the first parameter to the
2515 * function */
2516 var_t *idl_handle = make_var(xstrdup("IDL_handle"));
2517 idl_handle->attrs = append_attr(NULL, make_attr(ATTR_IN));
2518 idl_handle->type = find_type_or_error("handle_t", 0);
2519 type_function_add_head_arg(func->type, idl_handle);
2525 static void check_functions(const type_t *iface, int is_inside_library)
2527 const statement_t *stmt;
2528 if (is_attr(iface->attrs, ATTR_EXPLICIT_HANDLE))
2530 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2532 var_t *func = stmt->u.var;
2533 add_explicit_handle_if_necessary(func);
2536 if (!is_inside_library && !is_attr(iface->attrs, ATTR_LOCAL))
2538 STATEMENTS_FOR_EACH_FUNC( stmt, type_iface_get_stmts(iface) )
2540 const var_t *func = stmt->u.var;
2541 if (!is_attr(func->attrs, ATTR_LOCAL))
2542 check_remoting_args(func);
2547 static void check_statements(const statement_list_t *stmts, int is_inside_library)
2549 const statement_t *stmt;
2551 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2553 if (stmt->type == STMT_LIBRARY)
2554 check_statements(stmt->u.lib->stmts, TRUE);
2555 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE)
2556 check_functions(stmt->u.type, is_inside_library);
2560 static void check_all_user_types(const statement_list_t *stmts)
2562 const statement_t *stmt;
2564 if (stmts) LIST_FOR_EACH_ENTRY(stmt, stmts, const statement_t, entry)
2566 if (stmt->type == STMT_LIBRARY)
2567 check_all_user_types(stmt->u.lib->stmts);
2568 else if (stmt->type == STMT_TYPE && type_get_type(stmt->u.type) == TYPE_INTERFACE &&
2569 !is_local(stmt->u.type->attrs))
2571 const statement_t *stmt_func;
2572 STATEMENTS_FOR_EACH_FUNC(stmt_func, type_iface_get_stmts(stmt->u.type)) {
2573 const var_t *func = stmt_func->u.var;
2574 check_for_additional_prototype_types(func->type->details.function->args);
2580 int is_valid_uuid(const char *s)
2582 int i;
2584 for (i = 0; i < 36; ++i)
2585 if (i == 8 || i == 13 || i == 18 || i == 23)
2587 if (s[i] != '-')
2588 return FALSE;
2590 else
2591 if (!isxdigit(s[i]))
2592 return FALSE;
2594 return s[i] == '\0';
2597 static statement_t *make_statement(enum statement_type type)
2599 statement_t *stmt = xmalloc(sizeof(*stmt));
2600 stmt->type = type;
2601 return stmt;
2604 static statement_t *make_statement_type_decl(type_t *type)
2606 statement_t *stmt = make_statement(STMT_TYPE);
2607 stmt->u.type = type;
2608 return stmt;
2611 static statement_t *make_statement_reference(type_t *type)
2613 statement_t *stmt = make_statement(STMT_TYPEREF);
2614 stmt->u.type = type;
2615 return stmt;
2618 static statement_t *make_statement_declaration(var_t *var)
2620 statement_t *stmt = make_statement(STMT_DECLARATION);
2621 stmt->u.var = var;
2622 if (var->stgclass == STG_EXTERN && var->eval)
2623 warning("'%s' initialised and declared extern\n", var->name);
2624 if (is_const_decl(var))
2626 if (var->eval)
2627 reg_const(var);
2629 else if ((var->stgclass == STG_NONE || var->stgclass == STG_REGISTER) &&
2630 type_get_type(var->type) != TYPE_FUNCTION)
2631 error_loc("instantiation of data is illegal\n");
2632 return stmt;
2635 static statement_t *make_statement_library(typelib_t *typelib)
2637 statement_t *stmt = make_statement(STMT_LIBRARY);
2638 stmt->u.lib = typelib;
2639 return stmt;
2642 static statement_t *make_statement_cppquote(const char *str)
2644 statement_t *stmt = make_statement(STMT_CPPQUOTE);
2645 stmt->u.str = str;
2646 return stmt;
2649 static statement_t *make_statement_importlib(const char *str)
2651 statement_t *stmt = make_statement(STMT_IMPORTLIB);
2652 stmt->u.str = str;
2653 return stmt;
2656 static statement_t *make_statement_import(const char *str)
2658 statement_t *stmt = make_statement(STMT_IMPORT);
2659 stmt->u.str = str;
2660 return stmt;
2663 static statement_t *make_statement_module(type_t *type)
2665 statement_t *stmt = make_statement(STMT_MODULE);
2666 stmt->u.type = type;
2667 return stmt;
2670 static statement_t *make_statement_typedef(declarator_list_t *decls)
2672 declarator_t *decl, *next;
2673 statement_t *stmt;
2674 type_list_t **type_list;
2676 if (!decls) return NULL;
2678 stmt = make_statement(STMT_TYPEDEF);
2679 stmt->u.type_list = NULL;
2680 type_list = &stmt->u.type_list;
2682 LIST_FOR_EACH_ENTRY_SAFE( decl, next, decls, declarator_t, entry )
2684 var_t *var = decl->var;
2685 type_t *type = find_type_or_error(var->name, 0);
2686 *type_list = xmalloc(sizeof(type_list_t));
2687 (*type_list)->type = type;
2688 (*type_list)->next = NULL;
2690 type_list = &(*type_list)->next;
2691 free(decl);
2692 free(var);
2695 return stmt;
2698 static statement_list_t *append_statement(statement_list_t *list, statement_t *stmt)
2700 if (!stmt) return list;
2701 if (!list)
2703 list = xmalloc( sizeof(*list) );
2704 list_init( list );
2706 list_add_tail( list, &stmt->entry );
2707 return list;
2710 void init_loc_info(loc_info_t *i)
2712 i->input_name = input_name ? input_name : "stdin";
2713 i->line_number = line_number;
2714 i->near_text = parser_text;
2717 static void check_def(const type_t *t)
2719 if (t->defined)
2720 error_loc("%s: redefinition error; original definition was at %s:%d\n",
2721 t->name, t->loc_info.input_name, t->loc_info.line_number);