parser.y 45.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
%{
/*
 * IDL Compiler
 *
 * Copyright 2002 Ove Kaaven
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
19
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 */

#include "config.h"

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <assert.h>
#include <ctype.h>
#include <string.h>
#ifdef HAVE_ALLOCA_H
#include <alloca.h>
#endif

34 35
#include "windef.h"

36 37 38 39
#include "widl.h"
#include "utils.h"
#include "parser.h"
#include "header.h"
40
#include "typelib.h"
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66

#if defined(YYBYACC)
	/* Berkeley yacc (byacc) doesn't seem to know about these */
	/* Some *BSD supplied versions do define these though */
# ifndef YYEMPTY
#  define YYEMPTY	(-1)	/* Empty lookahead value of yychar */
# endif
# ifndef YYLEX
#  define YYLEX		yylex()
# endif

#elif defined(YYBISON)
	/* Bison was used for original development */
	/* #define YYEMPTY -2 */
	/* #define YYLEX   yylex() */

#else
	/* No yacc we know yet */
# if !defined(YYEMPTY) || !defined(YYLEX)
#  error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
# elif defined(__GNUC__)	/* gcc defines the #warning directive */
#  warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
  /* #else we just take a chance that it works... */
# endif
#endif

67
static attr_t *make_attr(enum attr_type type);
68
static attr_t *make_attrv(enum attr_type type, unsigned long val);
69
static attr_t *make_attrp(enum attr_type type, void *val);
70 71 72
static expr_t *make_expr(enum expr_type type);
static expr_t *make_exprl(enum expr_type type, long val);
static expr_t *make_exprs(enum expr_type type, char *val);
73
static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
74 75
static expr_t *make_expr1(enum expr_type type, expr_t *expr);
static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
76 77
static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
static type_t *make_type(unsigned char type, type_t *ref);
78 79 80
static typeref_t *make_tref(char *name, type_t *ref);
static typeref_t *uniq_tref(typeref_t *ref);
static type_t *type_ref(typeref_t *ref);
81
static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
82
static ifref_t *make_ifref(type_t *iface);
83 84
static var_t *make_var(char *name);
static func_t *make_func(var_t *def, var_t *args);
85
static type_t *make_class(char *name);
86
static type_t *make_safearray(void);
87 88
static type_t *make_builtin(char *name);
static type_t *make_int(int sign);
89

90
static type_t *reg_type(type_t *type, const char *name, int t);
91
static type_t *reg_typedefs(type_t *type, var_t *names, attr_t *attrs);
92
static type_t *find_type(const char *name, int t);
93
static type_t *find_type2(char *name, int t);
94 95
static type_t *get_type(unsigned char type, char *name, int t);
static type_t *get_typev(unsigned char type, var_t *name, int t);
96
static int get_struct_type(var_t *fields);
97

98 99 100
static var_t *reg_const(var_t *var);
static var_t *find_const(char *name, int f);

101 102 103 104 105
static void write_libid(const char *name, const attr_t *attr);
static void write_clsid(type_t *cls);
static void write_diid(type_t *iface);
static void write_iid(type_t *iface);

106
static int compute_method_indexes(type_t *iface);
107
static char *gen_name(void);
108
static void process_typedefs(var_t *names);
109
static void check_arg(var_t *arg);
110

111 112 113 114 115 116 117
#define tsENUM   1
#define tsSTRUCT 2
#define tsUNION  3

%}
%union {
	attr_t *attr;
118
	expr_t *expr;
119 120 121 122
	type_t *type;
	typeref_t *tref;
	var_t *var;
	func_t *func;
123
	ifref_t *ifref;
124
	char *str;
125
	UUID *uuid;
126
	unsigned int num;
127 128 129 130
}

%token <str> aIDENTIFIER
%token <str> aKNOWNTYPE
131
%token <num> aNUM aHEXNUM
132 133
%token <str> aSTRING
%token <uuid> aUUID
134
%token aEOF
135
%token SHL SHR
136
%token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
137 138 139 140 141
%token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
%token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
%token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
%token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
%token tDEFAULT
142
%token tDEFAULTCOLLELEM
143
%token tDEFAULTVALUE
144
%token tDEFAULTVTABLE
145
%token tDISPLAYBIND
146 147
%token tDISPINTERFACE
%token tDLLNAME tDOUBLE tDUAL
148
%token tENDPOINT
149
%token tENTRY tENUM tERRORSTATUST
150
%token tEXPLICITHANDLE tEXTERN
151
%token tFALSE
152
%token tFLOAT
153
%token tHANDLE
154
%token tHANDLET
155 156
%token tHELPCONTEXT tHELPFILE
%token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
157
%token tHIDDEN
158
%token tHYPER tID tIDEMPOTENT
159
%token tIIDIS
160
%token tIMMEDIATEBIND
161
%token tIMPLICITHANDLE
162
%token tIMPORT tIMPORTLIB
163
%token tIN tINLINE
164
%token tINPUTSYNC
165 166
%token tINT tINT64
%token tINTERFACE
167
%token tLCID
168
%token tLENGTHIS tLIBRARY
169 170
%token tLOCAL
%token tLONG
171 172
%token tMETHODS
%token tMODULE
173
%token tNONBROWSABLE
Huw Davies's avatar
Huw Davies committed
174
%token tNONCREATABLE
175
%token tNONEXTENSIBLE
176
%token tOBJECT tODL tOLEAUTOMATION
177
%token tOPTIONAL
178 179
%token tOUT
%token tPOINTERDEFAULT
180
%token tPROPERTIES
181
%token tPROPGET tPROPPUT tPROPPUTREF
182
%token tPTR
183
%token tPUBLIC
184
%token tRANGE
185
%token tREADONLY tREF
186
%token tREQUESTEDIT
187
%token tRESTRICTED
188
%token tRETVAL
189
%token tSAFEARRAY
190 191
%token tSHORT
%token tSIGNED
192
%token tSINGLE
193
%token tSIZEIS tSIZEOF
194
%token tSMALL
195
%token tSOURCE
196 197 198
%token tSTDCALL
%token tSTRING tSTRUCT
%token tSWITCH tSWITCHIS tSWITCHTYPE
199
%token tTRANSMITAS
200
%token tTRUE
201 202 203 204 205 206
%token tTYPEDEF
%token tUNION
%token tUNIQUE
%token tUNSIGNED
%token tUUID
%token tV1ENUM
207
%token tVARARG
208 209 210 211 212
%token tVERSION
%token tVOID
%token tWCHAR tWIREMARSHAL

%type <attr> m_attributes attributes attrib_list attribute
213 214
%type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
%type <expr> array array_list
215 216 217
%type <type> inherit interface interfacehdr interfacedef interfacedec
%type <type> dispinterface dispinterfacehdr dispinterfacedef
%type <type> module modulehdr moduledef
218
%type <type> base_type int_std
219
%type <type> enumdef structdef uniondef
220
%type <ifref> gbl_statements coclass_ints coclass_int
221 222
%type <tref> type
%type <var> m_args no_args args arg
223
%type <var> fields field s_field cases case enums enum_list enum constdef externdef
224
%type <var> m_ident t_ident ident p_ident pident pident_list
225
%type <var> dispint_props
226
%type <func> funcdef int_statements
227
%type <func> dispint_meths
228
%type <type> coclass coclasshdr coclassdef
229
%type <num> pointer_type version
230
%type <str> libraryhdr
231 232

%left ','
233
%right '?' ':'
234 235 236 237 238
%left '|'
%left '&'
%left '-' '+'
%left '*' '/'
%left SHL SHR
239
%right '~'
240 241 242 243 244 245
%right CAST
%right PPTR
%right NEG

%%

246
input:   gbl_statements                        { write_proxies($1); write_client($1); write_server($1); }
247 248
	;

249 250 251
gbl_statements:					{ $$ = NULL; }
	| gbl_statements interfacedec		{ $$ = $1; }
	| gbl_statements interfacedef		{ $$ = make_ifref($2); LINK($$, $1); }
252 253 254 255 256
	| gbl_statements coclass ';'		{ $$ = $1;
						  reg_type($2, $2->name, 0);
						  if (!parse_only && do_header) write_coclass_forward($2);
						}
	| gbl_statements coclassdef		{ $$ = $1;
257
						  add_typelib_entry($2);
258
						  reg_type($2, $2->name, 0);
259
						  if (!parse_only && do_header) write_coclass_forward($2);
260
						}
261
	| gbl_statements moduledef		{ $$ = $1; add_typelib_entry($2); }
262 263 264 265 266
	| gbl_statements librarydef		{ $$ = $1; }
	| gbl_statements statement		{ $$ = $1; }
	;

imp_statements:					{}
267 268
	| imp_statements interfacedec		{ if (!parse_only) add_typelib_entry($2); }
	| imp_statements interfacedef		{ if (!parse_only) add_typelib_entry($2); }
269
	| imp_statements coclass ';'		{ reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
270
	| imp_statements coclassdef		{ if (!parse_only) add_typelib_entry($2);
271
						  reg_type($2, $2->name, 0);
272
						  if (!parse_only && do_header) write_coclass_forward($2);
273
						}
274
	| imp_statements moduledef		{ if (!parse_only) add_typelib_entry($2); }
275
	| imp_statements statement		{}
276
	| imp_statements importlib		{}
277 278 279
	;

int_statements:					{ $$ = NULL; }
280 281
	| int_statements funcdef ';'		{ $$ = $2; LINK($$, $1); }
	| int_statements statement		{ $$ = $1; }
282 283 284
	;

statement: ';'					{}
Huw Davies's avatar
Huw Davies committed
285
	| constdef ';'				{ if (!parse_only && do_header) { write_constdef($1); } }
286
	| cppquote				{}
Huw Davies's avatar
Huw Davies committed
287 288
	| enumdef ';'				{ if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
	| externdef ';'				{ if (!parse_only && do_header) { write_externdef($1); } }
289
	| import				{}
Huw Davies's avatar
Huw Davies committed
290
	| structdef ';'				{ if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
291
	| typedef ';'				{}
Huw Davies's avatar
Huw Davies committed
292
	| uniondef ';'				{ if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
293 294
	;

Huw Davies's avatar
Huw Davies committed
295
cppquote: tCPPQUOTE '(' aSTRING ')'		{ if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
296
	;
297
import_start: tIMPORT aSTRING ';'		{ assert(yychar == YYEMPTY);
298
						  if (!do_import($2)) yychar = aEOF; }
299
	;
300 301 302
import:   import_start imp_statements aEOF	{}
	;

303
importlib: tIMPORTLIB '(' aSTRING ')'		{ if(!parse_only) add_importlib($3); }
Jacek Caban's avatar
Jacek Caban committed
304
	;
305

306 307
libraryhdr: tLIBRARY aIDENTIFIER		{ $$ = $2; }
	;
308
library_start: attributes libraryhdr '{'	{ start_typelib($2, $1);
309 310 311
						  if (!parse_only && do_header) write_library($2, $1);
						  if (!parse_only && do_idfile) write_libid($2, $1);
						}
312 313
	;
librarydef: library_start imp_statements '}'	{ end_typelib(); }
314
	;
315 316 317 318 319 320 321 322

m_args:						{ $$ = NULL; }
	| args
	;

no_args:  tVOID					{ $$ = NULL; }
	;

323 324
args:	  arg					{ check_arg($1); $$ = $1; }
	| args ',' arg				{ check_arg($3); LINK($3, $1); $$ = $3; }
325 326 327 328 329
	| no_args
	;

/* split into two rules to get bison to resolve a tVOID conflict */
arg:	  attributes type pident array		{ $$ = $3;
330 331
						  set_type($$, $2, $4);
						  $$->attrs = $1;
332 333
						}
	| type pident array			{ $$ = $2;
334
						  set_type($$, $1, $3);
335
						}
336 337 338 339 340 341 342 343 344 345 346
	| attributes type pident '(' m_args ')'	{ $$ = $3;
						  $$->ptr_level--;
						  set_type($$, $2, NULL);
						  $$->attrs = $1;
						  $$->args = $5;
						}
	| type pident '(' m_args ')'		{ $$ = $2;
						  $$->ptr_level--;
						  set_type($$, $1, NULL);
						  $$->args = $4;
						}
347 348
	;

349
array:						{ $$ = NULL; }
350
	| '[' array_list ']'			{ $$ = $2; }
351
	| '[' '*' ']'				{ $$ = make_expr(EXPR_VOID); }
352 353
	;

354 355 356 357 358
array_list: m_expr /* size of first dimension is optional */
	| array_list ',' expr			{ LINK($3, $1); $$ = $3; }
	| array_list ']' '[' expr		{ LINK($4, $1); $$ = $4; }
	;

359 360 361 362 363
m_attributes:					{ $$ = NULL; }
	| attributes
	;

attributes:
364 365 366 367
	  '[' attrib_list ']'			{ $$ = $2;
						  if (!$$)
						    yyerror("empty attribute lists unsupported");
						}
368 369 370
	;

attrib_list: attribute
371 372 373 374 375 376
	| attrib_list ',' attribute		{ if ($3) { LINK($3, $1); $$ = $3; }
						  else { $$ = $1; }
						}
	| attrib_list ']' '[' attribute		{ if ($4) { LINK($4, $1); $$ = $4; }
						  else { $$ = $1; }
						}
377 378
	;

379 380
attribute:					{ $$ = NULL; }
	| tAGGREGATABLE				{ $$ = make_attr(ATTR_AGGREGATABLE); }
381 382
	| tAPPOBJECT				{ $$ = make_attr(ATTR_APPOBJECT); }
	| tASYNC				{ $$ = make_attr(ATTR_ASYNC); }
383
	| tAUTOHANDLE				{ $$ = make_attr(ATTR_AUTO_HANDLE); }
384
	| tBINDABLE				{ $$ = make_attr(ATTR_BINDABLE); }
385
	| tCALLAS '(' ident ')'			{ $$ = make_attrp(ATTR_CALLAS, $3); }
386 387 388 389
	| tCASE '(' expr_list_const ')'		{ $$ = make_attrp(ATTR_CASE, $3); }
	| tCONTEXTHANDLE			{ $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
	| tCONTEXTHANDLENOSERIALIZE		{ $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
	| tCONTEXTHANDLESERIALIZE		{ $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
390
	| tCONTROL				{ $$ = make_attr(ATTR_CONTROL); }
391
	| tDEFAULT				{ $$ = make_attr(ATTR_DEFAULT); }
392
	| tDEFAULTCOLLELEM			{ $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
393 394
	| tDEFAULTVALUE '(' expr_const ')'	{ $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
	| tDEFAULTVALUE '(' aSTRING ')'		{ $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
395
	| tDEFAULTVTABLE			{ $$ = make_attr(ATTR_DEFAULTVTABLE); }
396
	| tDISPLAYBIND				{ $$ = make_attr(ATTR_DISPLAYBIND); }
397 398
	| tDLLNAME '(' aSTRING ')'		{ $$ = make_attrp(ATTR_DLLNAME, $3); }
	| tDUAL					{ $$ = make_attr(ATTR_DUAL); }
399
	| tENDPOINT '(' aSTRING ')'		{ $$ = make_attrp(ATTR_ENDPOINT, $3); }
400 401
	| tENTRY '(' aSTRING ')'		{ $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
	| tENTRY '(' expr_const ')'		{ $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
402
	| tEXPLICITHANDLE			{ $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
403
	| tHANDLE				{ $$ = make_attr(ATTR_HANDLE); }
404 405
	| tHELPCONTEXT '(' expr_const ')'	{ $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
	| tHELPFILE '(' aSTRING ')'		{ $$ = make_attrp(ATTR_HELPFILE, $3); }
406
	| tHELPSTRING '(' aSTRING ')'		{ $$ = make_attrp(ATTR_HELPSTRING, $3); }
407 408
	| tHELPSTRINGCONTEXT '(' expr_const ')'	{ $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
	| tHELPSTRINGDLL '(' aSTRING ')'	{ $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
409
	| tHIDDEN				{ $$ = make_attr(ATTR_HIDDEN); }
410
	| tID '(' expr_const ')'		{ $$ = make_attrp(ATTR_ID, $3); }
411
	| tIDEMPOTENT				{ $$ = make_attr(ATTR_IDEMPOTENT); }
412
	| tIIDIS '(' ident ')'			{ $$ = make_attrp(ATTR_IIDIS, $3); }
413
	| tIMMEDIATEBIND			{ $$ = make_attr(ATTR_IMMEDIATEBIND); }
414
	| tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')'	{ $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
415
	| tIN					{ $$ = make_attr(ATTR_IN); }
416
	| tINPUTSYNC				{ $$ = make_attr(ATTR_INPUTSYNC); }
417
	| tLENGTHIS '(' m_exprs ')'		{ $$ = make_attrp(ATTR_LENGTHIS, $3); }
418
	| tLOCAL				{ $$ = make_attr(ATTR_LOCAL); }
419
	| tNONBROWSABLE				{ $$ = make_attr(ATTR_NONBROWSABLE); }
Huw Davies's avatar
Huw Davies committed
420
	| tNONCREATABLE				{ $$ = make_attr(ATTR_NONCREATABLE); }
421
	| tNONEXTENSIBLE			{ $$ = make_attr(ATTR_NONEXTENSIBLE); }
422
	| tOBJECT				{ $$ = make_attr(ATTR_OBJECT); }
423
	| tODL					{ $$ = make_attr(ATTR_ODL); }
424
	| tOLEAUTOMATION			{ $$ = make_attr(ATTR_OLEAUTOMATION); }
425
	| tOPTIONAL                             { $$ = make_attr(ATTR_OPTIONAL); }
426 427
	| tOUT					{ $$ = make_attr(ATTR_OUT); }
	| tPOINTERDEFAULT '(' pointer_type ')'	{ $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
428 429
	| tPROPGET				{ $$ = make_attr(ATTR_PROPGET); }
	| tPROPPUT				{ $$ = make_attr(ATTR_PROPPUT); }
430
	| tPROPPUTREF				{ $$ = make_attr(ATTR_PROPPUTREF); }
431
	| tPUBLIC				{ $$ = make_attr(ATTR_PUBLIC); }
432
	| tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
433
	| tREADONLY				{ $$ = make_attr(ATTR_READONLY); }
434
	| tREQUESTEDIT				{ $$ = make_attr(ATTR_REQUESTEDIT); }
435
	| tRESTRICTED				{ $$ = make_attr(ATTR_RESTRICTED); }
436
	| tRETVAL				{ $$ = make_attr(ATTR_RETVAL); }
437
	| tSIZEIS '(' m_exprs ')'		{ $$ = make_attrp(ATTR_SIZEIS, $3); }
438
	| tSOURCE				{ $$ = make_attr(ATTR_SOURCE); }
439
	| tSTRING				{ $$ = make_attr(ATTR_STRING); }
440 441
	| tSWITCHIS '(' expr ')'		{ $$ = make_attrp(ATTR_SWITCHIS, $3); }
	| tSWITCHTYPE '(' type ')'		{ $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
442
	| tTRANSMITAS '(' type ')'		{ $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
443
	| tUUID '(' aUUID ')'			{ $$ = make_attrp(ATTR_UUID, $3); }
444
	| tV1ENUM				{ $$ = make_attr(ATTR_V1ENUM); }
445
	| tVARARG				{ $$ = make_attr(ATTR_VARARG); }
446
	| tVERSION '(' version ')'		{ $$ = make_attrv(ATTR_VERSION, $3); }
447 448
	| tWIREMARSHAL '(' type ')'		{ $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
	| pointer_type				{ $$ = make_attrv(ATTR_POINTERTYPE, $1); }
449 450 451 452 453 454
	;

callconv:
	| tSTDCALL
	;

455
cases:						{ $$ = NULL; }
456 457 458
	| cases case				{ if ($2) { LINK($2, $1); $$ = $2; }
						  else { $$ = $1; }
						}
459 460
	;

461
case:	  tCASE expr ':' field			{ attr_t *a = make_attrp(ATTR_CASE, $2);
462
						  $$ = $4; if (!$$) $$ = make_var(NULL);
463
						  LINK(a, $$->attrs); $$->attrs = a;
464 465 466 467 468
						}
	| tDEFAULT ':' field			{ attr_t *a = make_attr(ATTR_DEFAULT);
						  $$ = $3; if (!$$) $$ = make_var(NULL);
						  LINK(a, $$->attrs); $$->attrs = a;
						}
469 470
	;

471
constdef: tCONST type ident '=' expr_const	{ $$ = reg_const($3);
472
						  set_type($$, $2, NULL);
473
						  $$->eval = $5;
474
						}
475 476
	;

477 478
enums:						{ $$ = NULL; }
	| enum_list ','				{ $$ = $1; }
479 480 481
	| enum_list
	;

482 483 484
enum_list: enum					{ if (!$$->eval)
						    $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
						}
485
	| enum_list ',' enum			{ LINK($3, $1); $$ = $3;
486 487
						  if (!$$->eval)
						    $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
488
						}
489 490
	;

491 492
enum:	  ident '=' expr_const			{ $$ = reg_const($1);
						  $$->eval = $3;
493
                                                  $$->type = make_int(0);
494 495
						}
	| ident					{ $$ = reg_const($1);
496
                                                  $$->type = make_int(0);
497
						}
498 499
	;

500
enumdef: tENUM t_ident '{' enums '}'		{ $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
501
						  $$->kind = TKIND_ENUM;
502
						  $$->fields = $4;
503
						  $$->defined = TRUE;
Huw Davies's avatar
Huw Davies committed
504
                                                  if(in_typelib)
505
                                                      add_typelib_entry($$);
506 507 508
						}
	;

509 510 511 512 513
m_exprs:  m_expr
	| m_exprs ',' m_expr			{ LINK($3, $1); $$ = $3; }
	;

/*
514 515
exprs:						{ $$ = make_expr(EXPR_VOID); }
	| expr_list
516 517
	;

518 519
expr_list: expr
	| expr_list ',' expr			{ LINK($3, $1); $$ = $3; }
520
	;
521 522 523 524 525
*/

m_expr:						{ $$ = make_expr(EXPR_VOID); }
	| expr
	;
526

527 528
expr:	  aNUM					{ $$ = make_exprl(EXPR_NUM, $1); }
	| aHEXNUM				{ $$ = make_exprl(EXPR_HEXNUM, $1); }
529 530
	| tFALSE				{ $$ = make_exprl(EXPR_TRUEFALSE, 0); }
	| tTRUE					{ $$ = make_exprl(EXPR_TRUEFALSE, 1); }
531
	| aIDENTIFIER				{ $$ = make_exprs(EXPR_IDENTIFIER, $1); }
532
	| expr '?' expr ':' expr		{ $$ = make_expr3(EXPR_COND, $1, $3, $5); }
533 534 535 536 537 538 539 540
	| expr '|' expr				{ $$ = make_expr2(EXPR_OR , $1, $3); }
	| expr '&' expr				{ $$ = make_expr2(EXPR_AND, $1, $3); }
	| expr '+' expr				{ $$ = make_expr2(EXPR_ADD, $1, $3); }
	| expr '-' expr				{ $$ = make_expr2(EXPR_SUB, $1, $3); }
	| expr '*' expr				{ $$ = make_expr2(EXPR_MUL, $1, $3); }
	| expr '/' expr				{ $$ = make_expr2(EXPR_DIV, $1, $3); }
	| expr SHL expr				{ $$ = make_expr2(EXPR_SHL, $1, $3); }
	| expr SHR expr				{ $$ = make_expr2(EXPR_SHR, $1, $3); }
541
	| '~' expr				{ $$ = make_expr1(EXPR_NOT, $2); }
542 543 544 545 546 547 548
	| '-' expr %prec NEG			{ $$ = make_expr1(EXPR_NEG, $2); }
	| '*' expr %prec PPTR			{ $$ = make_expr1(EXPR_PPTR, $2); }
	| '(' type ')' expr %prec CAST		{ $$ = make_exprt(EXPR_CAST, $2, $4); }
	| tSIZEOF '(' type ')'			{ $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
	| '(' expr ')'				{ $$ = $2; }
	;

549 550 551 552
expr_list_const: expr_const
	| expr_list_const ',' expr_const	{ LINK($3, $1); $$ = $3; }
	;

553
expr_const: expr				{ $$ = $1;
554
						  if (!$$->is_const)
555
						      yyerror("expression is not constant");
556 557 558 559 560 561
						}
	;

externdef: tEXTERN tCONST type ident		{ $$ = $4;
						  set_type($$, $3, NULL);
						}
562 563 564
	;

fields:						{ $$ = NULL; }
565 566 567
	| fields field				{ if ($2) { LINK($2, $1); $$ = $2; }
						  else { $$ = $1; }
						}
568 569
	;

570
field:	  s_field ';'				{ $$ = $1; }
571 572 573 574 575
	| m_attributes uniondef ';'		{ $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
	| attributes ';'			{ $$ = make_var(NULL); $$->attrs = $1; }
	| ';'					{ $$ = NULL; }
	;

576 577 578
s_field:  m_attributes type pident array	{ $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
	;

579 580
funcdef:
	  m_attributes type callconv pident
581
	  '(' m_args ')'			{ set_type($4, $2, NULL);
582 583
						  $4->attrs = $1;
						  $$ = make_func($4, $6);
584
						  if (is_attr($4->attrs, ATTR_IN)) {
585
						    yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
586
						  }
587 588 589 590 591 592 593 594 595 596 597 598 599
						}
	;

m_ident:					{ $$ = NULL; }
	| ident
	;

t_ident:					{ $$ = NULL; }
	| aIDENTIFIER				{ $$ = make_var($1); }
	| aKNOWNTYPE				{ $$ = make_var($1); }
	;

ident:	  aIDENTIFIER				{ $$ = make_var($1); }
600
/* some "reserved words" used in attributes are also used as field names in some MS IDL files */
601
	| aKNOWNTYPE				{ $$ = make_var($<str>1); }
602
	| tASYNC				{ $$ = make_var($<str>1); }
603
	| tID					{ $$ = make_var($<str>1); }
604
	| tLCID					{ $$ = make_var($<str>1); }
605
	| tOBJECT				{ $$ = make_var($<str>1); }
606
	| tRANGE				{ $$ = make_var($<str>1); }
607
	| tRETVAL				{ $$ = make_var($<str>1); }
608
	| tUUID					{ $$ = make_var($<str>1); }
609
	| tVERSION				{ $$ = make_var($<str>1); }
610 611
	;

612 613
base_type: tBYTE				{ $$ = make_builtin($<str>1); }
	| tWCHAR				{ $$ = make_builtin($<str>1); }
614
	| int_std
615 616 617
	| tSIGNED int_std			{ $$ = $2; $$->sign = 1; }
	| tUNSIGNED int_std			{ $$ = $2; $$->sign = -1;
						  switch ($$->type) {
618
						  case RPC_FC_CHAR:  break;
619 620 621
						  case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
						  case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
						  case RPC_FC_LONG:  $$->type = RPC_FC_ULONG;  break;
622
						  case RPC_FC_HYPER:
623 624 625 626 627
						    if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
                                                    {
                                                      $$ = alias($$, "MIDL_uhyper");
                                                      $$->sign = 0;
                                                    }
628
						    break;
629 630 631
						  default: break;
						  }
						}
632 633 634 635 636 637 638
	| tUNSIGNED				{ $$ = make_int(-1); }
	| tFLOAT				{ $$ = make_builtin($<str>1); }
	| tSINGLE				{ $$ = duptype(find_type("float", 0), 1); }
	| tDOUBLE				{ $$ = make_builtin($<str>1); }
	| tBOOLEAN				{ $$ = make_builtin($<str>1); }
	| tERRORSTATUST				{ $$ = make_builtin($<str>1); }
	| tHANDLET				{ $$ = make_builtin($<str>1); }
639 640 641 642 643 644
	;

m_int:
	| tINT
	;

645 646 647 648 649 650 651
int_std:  tINT					{ $$ = make_builtin($<str>1); }
	| tSHORT m_int				{ $$ = make_builtin($<str>1); }
	| tSMALL				{ $$ = make_builtin($<str>1); }
	| tLONG m_int				{ $$ = make_builtin($<str>1); }
	| tHYPER m_int				{ $$ = make_builtin($<str>1); }
	| tINT64				{ $$ = make_builtin($<str>1); }
	| tCHAR					{ $$ = make_builtin($<str>1); }
652 653
	;

654
coclass:  tCOCLASS aIDENTIFIER			{ $$ = make_class($2); }
655 656
	| tCOCLASS aKNOWNTYPE			{ $$ = find_type($2, 0);
						  if ($$->defined) yyerror("multiple definition error");
657
						  if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
658
						}
659 660 661 662
	;

coclasshdr: attributes coclass			{ $$ = $2;
						  $$->attrs = $1;
663 664
						  if (!parse_only && do_header)
						    write_coclass($$);
665 666
						  if (!parse_only && do_idfile)
						    write_clsid($$);
667 668 669 670 671
						}
	;

coclassdef: coclasshdr '{' coclass_ints '}'	{ $$ = $1;
						  $$->ifaces = $3;
672
						  $$->defined = TRUE;
673 674 675 676 677 678 679 680 681 682 683
						}
	;

coclass_ints:					{ $$ = NULL; }
	| coclass_ints coclass_int		{ LINK($2, $1); $$ = $2; }
	;

coclass_int:
	  m_attributes interfacedec		{ $$ = make_ifref($2); $$->attrs = $1; }
	;

684 685
dispinterface: tDISPINTERFACE aIDENTIFIER	{ $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
	|      tDISPINTERFACE aKNOWNTYPE	{ $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
686 687
	;

688 689
dispinterfacehdr: attributes dispinterface	{ attr_t *attrs;
						  $$ = $2;
690
						  if ($$->defined) yyerror("multiple definition error");
691 692 693
						  attrs = make_attr(ATTR_DISPINTERFACE);
						  LINK(attrs, $1);
						  $$->attrs = attrs;
694
						  $$->ref = find_type("IDispatch", 0);
695
						  if (!$$->ref) yyerror("IDispatch is undefined");
696
						  $$->defined = TRUE;
Huw Davies's avatar
Huw Davies committed
697
						  if (!parse_only && do_header) write_forward($$);
698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
						}
	;

dispint_props: tPROPERTIES ':'			{ $$ = NULL; }
	| dispint_props s_field ';'		{ LINK($2, $1); $$ = $2; }
	;

dispint_meths: tMETHODS ':'			{ $$ = NULL; }
	| dispint_meths funcdef ';'		{ LINK($2, $1); $$ = $2; }
	;

dispinterfacedef: dispinterfacehdr '{'
	  dispint_props
	  dispint_meths
	  '}'					{ $$ = $1;
						  $$->fields = $3;
						  $$->funcs = $4;
Huw Davies's avatar
Huw Davies committed
715
						  if (!parse_only && do_header) write_dispinterface($$);
716
						  if (!parse_only && do_idfile) write_diid($$);
717 718 719
						}
/* FIXME: not sure how to handle this yet
	| dispinterfacehdr '{' interface '}'	{ $$ = $1;
720
						  compute_method_indexes($$);
Huw Davies's avatar
Huw Davies committed
721
						  if (!parse_only && do_header) write_interface($$);
722
						  if (!parse_only && do_idfile) write_iid($$);
723 724 725 726
						}
*/
	;

727 728 729 730
inherit:					{ $$ = NULL; }
	| ':' aKNOWNTYPE			{ $$ = find_type2($2, 0); }
	;

731 732
interface: tINTERFACE aIDENTIFIER		{ $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
	|  tINTERFACE aKNOWNTYPE		{ $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
733 734
	;

735
interfacehdr: attributes interface		{ $$ = $2;
736
						  if ($$->defined) yyerror("multiple definition error");
737 738
						  $$->attrs = $1;
						  $$->defined = TRUE;
Huw Davies's avatar
Huw Davies committed
739
						  if (!parse_only && do_header) write_forward($$);
740 741 742 743 744 745 746
						}
	;

interfacedef: interfacehdr inherit
	  '{' int_statements '}'		{ $$ = $1;
						  $$->ref = $2;
						  $$->funcs = $4;
747
						  compute_method_indexes($$);
Huw Davies's avatar
Huw Davies committed
748
						  if (!parse_only && do_header) write_interface($$);
749
						  if (!parse_only && do_idfile) write_iid($$);
750 751 752 753 754 755
						}
/* MIDL is able to import the definition of a base class from inside the
 * definition of a derived class, I'll try to support it with this rule */
	| interfacehdr ':' aIDENTIFIER
	  '{' import int_statements '}'		{ $$ = $1;
						  $$->ref = find_type2($3, 0);
756
						  if (!$$->ref) yyerror("base class '%s' not found in import", $3);
757
						  $$->funcs = $6;
758
						  compute_method_indexes($$);
Huw Davies's avatar
Huw Davies committed
759
						  if (!parse_only && do_header) write_interface($$);
760
						  if (!parse_only && do_idfile) write_iid($$);
761
						}
762 763 764 765
	| dispinterfacedef			{ $$ = $1; }
	;

interfacedec:
Huw Davies's avatar
Huw Davies committed
766 767
	  interface ';'				{ $$ = $1; if (!parse_only && do_header) write_forward($$); }
	| dispinterface ';'			{ $$ = $1; if (!parse_only && do_header) write_forward($$); }
768 769
	;

770 771
module:   tMODULE aIDENTIFIER			{ $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
	| tMODULE aKNOWNTYPE			{ $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
772 773 774 775 776 777 778 779 780
	;

modulehdr: attributes module			{ $$ = $2;
						  $$->attrs = $1;
						}
	;

moduledef: modulehdr '{' int_statements '}'	{ $$ = $1;
						  $$->funcs = $3;
Huw Davies's avatar
Huw Davies committed
781
						  /* FIXME: if (!parse_only && do_header) write_module($$); */
782
						}
783 784 785
	;

p_ident:  '*' pident %prec PPTR			{ $$ = $2; $$->ptr_level++; }
786
	| tCONST p_ident			{ $$ = $2; /* FIXME */ }
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
	;

pident:	  ident
	| p_ident
	| '(' pident ')'			{ $$ = $2; }
	;

pident_list:
	  pident
	| pident_list ',' pident		{ LINK($3, $1); $$ = $3; }
	;

pointer_type:
	  tREF					{ $$ = RPC_FC_RP; }
	| tUNIQUE				{ $$ = RPC_FC_UP; }
802
	| tPTR					{ $$ = RPC_FC_FP; }
803 804 805
	;

structdef: tSTRUCT t_ident '{' fields '}'	{ $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
806 807
                                                  /* overwrite RPC_FC_STRUCT with a more exact type */
						  $$->type = get_struct_type( $4 );
808
						  $$->kind = TKIND_RECORD;
809 810
						  $$->fields = $4;
						  $$->defined = TRUE;
811
                                                  if(in_typelib)
812
                                                      add_typelib_entry($$);
813
                                                }
814 815
	;

816
type:	  tVOID					{ $$ = make_tref(NULL, duptype(find_type("void", 0), 1)); }
817 818 819 820 821 822 823 824 825
	| aKNOWNTYPE				{ $$ = make_tref($1, find_type($1, 0)); }
	| base_type				{ $$ = make_tref(NULL, $1); }
	| tCONST type				{ $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
	| enumdef				{ $$ = make_tref(NULL, $1); }
	| tENUM aIDENTIFIER			{ $$ = make_tref(NULL, find_type2($2, tsENUM)); }
	| structdef				{ $$ = make_tref(NULL, $1); }
	| tSTRUCT aIDENTIFIER			{ $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
	| uniondef				{ $$ = make_tref(NULL, $1); }
	| tUNION aIDENTIFIER			{ $$ = make_tref(NULL, find_type2($2, tsUNION)); }
826
	| tSAFEARRAY '(' type ')'		{ $$ = make_tref(NULL, make_safearray()); }
827 828
	;

829 830
typedef: tTYPEDEF m_attributes type pident_list	{ reg_typedefs(type_ref($3), $4, $2);
						  process_typedefs($4);
831 832 833 834
						}
	;

uniondef: tUNION t_ident '{' fields '}'		{ $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
835
						  $$->kind = TKIND_UNION;
836 837 838 839
						  $$->fields = $4;
						  $$->defined = TRUE;
						}
	| tUNION t_ident
840
	  tSWITCH '(' s_field ')'
841 842
	  m_ident '{' cases '}'			{ var_t *u = $7;
						  $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
843
						  $$->kind = TKIND_UNION;
844
						  if (!u) u = make_var( xstrdup("tagged_union") );
845
						  u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
846
						  u->type->kind = TKIND_UNION;
847 848 849
						  u->type->fields = $9;
						  u->type->defined = TRUE;
						  LINK(u, $5); $$->fields = u;
850 851 852 853 854
						  $$->defined = TRUE;
						}
	;

version:
855 856
	  aNUM					{ $$ = MAKELONG($1, 0); }
	| aNUM '.' aNUM				{ $$ = MAKELONG($1, $3); }
857 858 859 860
	;

%%

861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
static void decl_builtin(const char *name, unsigned char type)
{
  type_t *t = make_type(type, NULL);
  t->name = xstrdup(name);
  reg_type(t, name, 0);
}

static type_t *make_builtin(char *name)
{
  /* NAME is strdup'd in the lexer */
  type_t *t = duptype(find_type(name, 0), 0);
  t->name = name;
  return t;
}

static type_t *make_int(int sign)
{
  type_t *t = duptype(find_type("int", 0), 1);

  t->sign = sign;
  if (sign < 0)
    t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;

  return t;
}

void init_types(void)
{
889
  decl_builtin("void", 0);
890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905
  decl_builtin("byte", RPC_FC_BYTE);
  decl_builtin("wchar_t", RPC_FC_WCHAR);
  decl_builtin("int", RPC_FC_LONG);     /* win32 */
  decl_builtin("short", RPC_FC_SHORT);
  decl_builtin("small", RPC_FC_SMALL);
  decl_builtin("long", RPC_FC_LONG);
  decl_builtin("hyper", RPC_FC_HYPER);
  decl_builtin("__int64", RPC_FC_HYPER);
  decl_builtin("char", RPC_FC_CHAR);
  decl_builtin("float", RPC_FC_FLOAT);
  decl_builtin("double", RPC_FC_DOUBLE);
  decl_builtin("boolean", RPC_FC_BYTE);
  decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
  decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
}

906
static attr_t *make_attr(enum attr_type type)
907 908 909 910
{
  attr_t *a = xmalloc(sizeof(attr_t));
  a->type = type;
  a->u.ival = 0;
911
  INIT_LINK(a);
912 913 914
  return a;
}

915
static attr_t *make_attrv(enum attr_type type, unsigned long val)
916 917 918 919
{
  attr_t *a = xmalloc(sizeof(attr_t));
  a->type = type;
  a->u.ival = val;
920
  INIT_LINK(a);
921 922 923
  return a;
}

924
static attr_t *make_attrp(enum attr_type type, void *val)
925 926 927 928
{
  attr_t *a = xmalloc(sizeof(attr_t));
  a->type = type;
  a->u.pval = val;
929
  INIT_LINK(a);
930 931 932
  return a;
}

933 934 935 936 937 938
static expr_t *make_expr(enum expr_type type)
{
  expr_t *e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = NULL;
  e->u.lval = 0;
939
  e->is_const = FALSE;
940 941 942 943 944 945 946 947 948 949
  INIT_LINK(e);
  return e;
}

static expr_t *make_exprl(enum expr_type type, long val)
{
  expr_t *e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = NULL;
  e->u.lval = val;
950
  e->is_const = FALSE;
951
  INIT_LINK(e);
952
  /* check for numeric constant */
953 954 955
  if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
    /* make sure true/false value is valid */
    assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
956 957 958
    e->is_const = TRUE;
    e->cval = val;
  }
959 960 961 962 963
  return e;
}

static expr_t *make_exprs(enum expr_type type, char *val)
{
964 965
  expr_t *e;
  e = xmalloc(sizeof(expr_t));
966 967 968
  e->type = type;
  e->ref = NULL;
  e->u.sval = val;
969
  e->is_const = FALSE;
970
  INIT_LINK(e);
971 972 973 974 975 976 977
  /* check for predefined constants */
  if (type == EXPR_IDENTIFIER) {
    var_t *c = find_const(val, 0);
    if (c) {
      e->u.sval = c->name;
      free(val);
      e->is_const = TRUE;
978
      e->cval = c->eval->cval;
979 980 981 982 983 984 985 986 987 988 989 990 991 992 993
    }
  }
  return e;
}

static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
{
  expr_t *e;
  e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = expr;
  e->u.tref = tref;
  e->is_const = FALSE;
  INIT_LINK(e);
  /* check for cast of constant expression */
994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
  if (type == EXPR_SIZEOF) {
    switch (tref->ref->type) {
      case RPC_FC_BYTE:
      case RPC_FC_CHAR:
      case RPC_FC_SMALL:
      case RPC_FC_USMALL:
        e->is_const = TRUE;
        e->cval = 1;
        break;
      case RPC_FC_WCHAR:
      case RPC_FC_USHORT:
      case RPC_FC_SHORT:
        e->is_const = TRUE;
        e->cval = 2;
        break;
      case RPC_FC_LONG:
      case RPC_FC_ULONG:
      case RPC_FC_FLOAT:
      case RPC_FC_ERROR_STATUS_T:
        e->is_const = TRUE;
        e->cval = 4;
        break;
      case RPC_FC_HYPER:
      case RPC_FC_DOUBLE:
        e->is_const = TRUE;
        e->cval = 8;
        break;
    }
  }
1023 1024 1025 1026
  if (type == EXPR_CAST && expr->is_const) {
    e->is_const = TRUE;
    e->cval = expr->cval;
  }
1027 1028 1029 1030 1031 1032
  return e;
}

static expr_t *make_expr1(enum expr_type type, expr_t *expr)
{
  expr_t *e;
1033 1034 1035 1036 1037 1038
  e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = expr;
  e->u.lval = 0;
  e->is_const = FALSE;
  INIT_LINK(e);
1039
  /* check for compile-time optimization */
1040 1041
  if (expr->is_const) {
    e->is_const = TRUE;
1042 1043
    switch (type) {
    case EXPR_NEG:
1044 1045
      e->cval = -expr->cval;
      break;
1046 1047 1048
    case EXPR_NOT:
      e->cval = ~expr->cval;
      break;
1049
    default:
1050
      e->is_const = FALSE;
1051
      break;
1052 1053 1054 1055 1056 1057 1058 1059
    }
  }
  return e;
}

static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
{
  expr_t *e;
1060 1061 1062 1063 1064 1065
  e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = expr1;
  e->u.ext = expr2;
  e->is_const = FALSE;
  INIT_LINK(e);
1066
  /* check for compile-time optimization */
1067 1068
  if (expr1->is_const && expr2->is_const) {
    e->is_const = TRUE;
1069 1070
    switch (type) {
    case EXPR_ADD:
1071 1072
      e->cval = expr1->cval + expr2->cval;
      break;
1073
    case EXPR_SUB:
1074 1075
      e->cval = expr1->cval - expr2->cval;
      break;
1076
    case EXPR_MUL:
1077 1078
      e->cval = expr1->cval * expr2->cval;
      break;
1079
    case EXPR_DIV:
1080 1081
      e->cval = expr1->cval / expr2->cval;
      break;
1082
    case EXPR_OR:
1083 1084
      e->cval = expr1->cval | expr2->cval;
      break;
1085
    case EXPR_AND:
1086 1087
      e->cval = expr1->cval & expr2->cval;
      break;
1088
    case EXPR_SHL:
1089 1090
      e->cval = expr1->cval << expr2->cval;
      break;
1091
    case EXPR_SHR:
1092 1093
      e->cval = expr1->cval >> expr2->cval;
      break;
1094
    default:
1095
      e->is_const = FALSE;
1096
      break;
1097
    }
1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122
  }
  return e;
}

static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
{
  expr_t *e;
  e = xmalloc(sizeof(expr_t));
  e->type = type;
  e->ref = expr1;
  e->u.ext = expr2;
  e->ext2 = expr3;
  e->is_const = FALSE;
  INIT_LINK(e);
  /* check for compile-time optimization */
  if (expr1->is_const && expr2->is_const && expr3->is_const) {
    e->is_const = TRUE;
    switch (type) {
    case EXPR_COND:
      e->cval = expr1->cval ? expr2->cval : expr3->cval;
      break;
    default:
      e->is_const = FALSE;
      break;
    }
1123 1124 1125 1126
  }
  return e;
}

1127
static type_t *make_type(unsigned char type, type_t *ref)
1128 1129 1130
{
  type_t *t = xmalloc(sizeof(type_t));
  t->name = NULL;
1131
  t->kind = TKIND_PRIMITIVE;
1132 1133 1134
  t->type = type;
  t->ref = ref;
  t->attrs = NULL;
1135
  t->orig = NULL;
1136 1137
  t->funcs = NULL;
  t->fields = NULL;
1138
  t->ifaces = NULL;
1139 1140
  t->ignore = parse_only;
  t->is_const = FALSE;
1141
  t->sign = 0;
1142 1143
  t->defined = FALSE;
  t->written = FALSE;
1144
  t->user_types_registered = FALSE;
1145
  t->typelib_idx = -1;
1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
  INIT_LINK(t);
  return t;
}

static typeref_t *make_tref(char *name, type_t *ref)
{
  typeref_t *t = xmalloc(sizeof(typeref_t));
  t->name = name;
  t->ref = ref;
  t->uniq = ref ? 0 : 1;
  return t;
}

static typeref_t *uniq_tref(typeref_t *ref)
{
  typeref_t *t = ref;
  type_t *tp;
  if (t->uniq) return t;
1164 1165 1166 1167 1168 1169 1170 1171 1172

  if (t->name)
  {
    tp = duptype(t->ref, 0);
    tp->name = t->name;
  }
  else
    tp = duptype(t->ref, 1);

1173 1174 1175 1176 1177 1178 1179 1180 1181
  t->name = NULL;
  t->ref = tp;
  t->uniq = 1;
  return t;
}

static type_t *type_ref(typeref_t *ref)
{
  type_t *t = ref->ref;
1182
  free(ref->name);
1183 1184 1185 1186
  free(ref);
  return t;
}

1187
static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1188 1189 1190 1191 1192
{
  v->type = ref->ref;
  v->tname = ref->name;
  ref->name = NULL;
  free(ref);
1193
  v->array = arr;
1194 1195
}

1196 1197 1198 1199 1200 1201 1202 1203 1204
static ifref_t *make_ifref(type_t *iface)
{
  ifref_t *l = xmalloc(sizeof(ifref_t));
  l->iface = iface;
  l->attrs = NULL;
  INIT_LINK(l);
  return l;
}

1205 1206 1207 1208 1209 1210
static var_t *make_var(char *name)
{
  var_t *v = xmalloc(sizeof(var_t));
  v->name = name;
  v->ptr_level = 0;
  v->type = NULL;
1211
  v->args = NULL;
1212 1213
  v->tname = NULL;
  v->attrs = NULL;
1214
  v->array = NULL;
1215
  v->eval = NULL;
1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230
  INIT_LINK(v);
  return v;
}

static func_t *make_func(var_t *def, var_t *args)
{
  func_t *f = xmalloc(sizeof(func_t));
  f->def = def;
  f->args = args;
  f->ignore = parse_only;
  f->idx = -1;
  INIT_LINK(f);
  return f;
}

1231
static type_t *make_class(char *name)
1232
{
1233
  type_t *c = make_type(0, NULL);
1234
  c->name = name;
1235
  c->kind = TKIND_COCLASS;
1236 1237 1238 1239
  INIT_LINK(c);
  return c;
}

1240 1241 1242 1243 1244
static type_t *make_safearray(void)
{
  return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
}

1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260
#define HASHMAX 64

static int hash_ident(const char *name)
{
  const char *p = name;
  int sum = 0;
  /* a simple sum hash is probably good enough */
  while (*p) {
    sum += *p;
    p++;
  }
  return sum & (HASHMAX-1);
}

/***** type repository *****/

1261
struct rtype {
1262
  const char *name;
1263 1264 1265 1266 1267
  type_t *type;
  int t;
  struct rtype *next;
};

1268
struct rtype *type_hash[HASHMAX];
1269

1270
static type_t *reg_type(type_t *type, const char *name, int t)
1271 1272
{
  struct rtype *nt;
1273
  int hash;
1274
  if (!name) {
1275
    yyerror("registering named type without name");
1276 1277
    return type;
  }
1278
  hash = hash_ident(name);
1279 1280 1281 1282
  nt = xmalloc(sizeof(struct rtype));
  nt->name = name;
  nt->type = type;
  nt->t = t;
1283 1284
  nt->next = type_hash[hash];
  type_hash[hash] = nt;
1285 1286 1287
  return type;
}

1288
static type_t *reg_typedefs(type_t *type, var_t *names, attr_t *attrs)
1289 1290 1291
{
  type_t *ptr = type;
  int ptrc = 0;
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
  int is_str = is_attr(attrs, ATTR_STRING);
  unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);

  if (is_str)
  {
    type_t *t = type;
    unsigned char c;

    while (is_ptr(t))
      t = t->ref;

    c = t->type;
    if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
      yyerror("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays",
              names->name);
  }
1308

1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  /* We must generate names for tagless enum, struct or union.
     Typedef-ing a tagless enum, struct or union means we want the typedef
     to be included in a library whether it has other attributes or not,
     hence the public attribute.  */
  if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
       || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
  {
    if (! is_attr(attrs, ATTR_PUBLIC))
    {
      attr_t *new_attrs = make_attr(ATTR_PUBLIC);
      LINK(new_attrs, attrs);
      attrs = new_attrs;
    }
    type->name = gen_name();
  }

1325 1326 1327 1328 1329 1330 1331
  while (names) {
    var_t *next = NEXT_LINK(names);
    if (names->name) {
      type_t *cur = ptr;
      int cptr = names->ptr_level;
      if (cptr > ptrc) {
        while (cptr > ptrc) {
1332
          cur = ptr = make_type(RPC_FC_RP, cur);
1333 1334 1335 1336 1337 1338 1339 1340
          ptrc++;
        }
      } else {
        while (cptr < ptrc) {
          cur = cur->ref;
          cptr++;
        }
      }
1341 1342
      cur = alias(cur, names->name);
      cur->attrs = attrs;
1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354
      if (ptr_type)
      {
        if (is_ptr(cur))
          cur->type = ptr_type;
        else
          yyerror("'%s': pointer attribute applied to non-pointer type",
                  cur->name);
      }
      else if (is_str && ! is_ptr(cur))
        yyerror("'%s': [string] attribute applied to non-pointer type",
                cur->name);

1355
      reg_type(cur, cur->name, 0);
1356 1357 1358 1359 1360 1361
    }
    names = next;
  }
  return type;
}

1362
static type_t *find_type(const char *name, int t)
1363
{
1364
  struct rtype *cur = type_hash[hash_ident(name)];
1365 1366 1367
  while (cur && (cur->t != t || strcmp(cur->name, name)))
    cur = cur->next;
  if (!cur) {
1368
    yyerror("type '%s' not found", name);
1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382
    return NULL;
  }
  return cur->type;
}

static type_t *find_type2(char *name, int t)
{
  type_t *tp = find_type(name, t);
  free(name);
  return tp;
}

int is_type(const char *name)
{
1383
  struct rtype *cur = type_hash[hash_ident(name)];
1384 1385 1386 1387 1388 1389
  while (cur && (cur->t || strcmp(cur->name, name)))
    cur = cur->next;
  if (cur) return TRUE;
  return FALSE;
}

1390
static type_t *get_type(unsigned char type, char *name, int t)
1391 1392 1393 1394
{
  struct rtype *cur = NULL;
  type_t *tp;
  if (name) {
1395
    cur = type_hash[hash_ident(name)];
1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408
    while (cur && (cur->t != t || strcmp(cur->name, name)))
      cur = cur->next;
  }
  if (cur) {
    free(name);
    return cur->type;
  }
  tp = make_type(type, NULL);
  tp->name = name;
  if (!name) return tp;
  return reg_type(tp, name, t);
}

1409
static type_t *get_typev(unsigned char type, var_t *name, int t)
1410 1411 1412 1413 1414 1415 1416 1417
{
  char *sname = NULL;
  if (name) {
    sname = name->name;
    free(name);
  }
  return get_type(type, sname, t);
}
1418

1419 1420 1421
static int get_struct_type(var_t *field)
{
  int has_pointer = 0;
1422 1423
  int has_conformance = 0;
  int has_variance = 0;
1424

1425
  for (; field; field = NEXT_LINK(field))
1426 1427 1428
  {
    type_t *t = field->type;

1429 1430 1431 1432 1433 1434 1435
    if (field->ptr_level > 0)
    {
        has_pointer = 1;
        continue;
    }

    if (is_string_type(field->attrs, 0, field->array))
1436
    {
1437 1438
        has_conformance = 1;
        has_variance = 1;
1439 1440 1441
        continue;
    }

1442
    if (is_array_type(field->attrs, 0, field->array))
1443
    {
1444 1445 1446 1447
        if (field->array && !field->array->is_const)
        {
            has_conformance = 1;
            if (PREV_LINK(field))
1448
                yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1449 1450 1451 1452
                        field->name);
        }
        if (is_attr(field->attrs, ATTR_LENGTHIS))
            has_variance = 1;
1453 1454
    }

1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
    switch (t->type)
    {
    /*
     * RPC_FC_BYTE, RPC_FC_STRUCT, etc
     *  Simple types don't effect the type of struct.
     *  A struct containing a simple struct is still a simple struct.
     *  So long as we can block copy the data, we return RPC_FC_STRUCT.
     */
    case 0: /* void pointer */
    case RPC_FC_BYTE:
    case RPC_FC_CHAR:
    case RPC_FC_SMALL:
    case RPC_FC_USMALL:
    case RPC_FC_WCHAR:
    case RPC_FC_SHORT:
    case RPC_FC_USHORT:
    case RPC_FC_LONG:
    case RPC_FC_ULONG:
    case RPC_FC_INT3264:
    case RPC_FC_UINT3264:
    case RPC_FC_HYPER:
    case RPC_FC_FLOAT:
    case RPC_FC_DOUBLE:
    case RPC_FC_STRUCT:
    case RPC_FC_ENUM16:
    case RPC_FC_ENUM32:
      break;

1483
    case RPC_FC_RP:
1484 1485
    case RPC_FC_UP:
    case RPC_FC_FP:
1486
    case RPC_FC_OP:
1487 1488 1489
      has_pointer = 1;
      break;
    case RPC_FC_CARRAY:
1490
      has_conformance = 1;
1491
      if (PREV_LINK(field))
1492
          yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1493
                  field->name);
1494 1495 1496 1497 1498 1499 1500
      break;

    /*
     * Propagate member attributes
     *  a struct should be at least as complex as its member
     */
    case RPC_FC_CVSTRUCT:
1501 1502
      has_conformance = 1;
      has_variance = 1;
1503 1504 1505 1506
      has_pointer = 1;
      break;

    case RPC_FC_CPSTRUCT:
1507
      has_conformance = 1;
1508
      if (PREV_LINK(field))
1509
          yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1510
                  field->name);
1511 1512 1513 1514
      has_pointer = 1;
      break;

    case RPC_FC_CSTRUCT:
1515
      has_conformance = 1;
1516
      if (PREV_LINK(field))
1517
          yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1518
                  field->name);
1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542
      break;

    case RPC_FC_PSTRUCT:
      has_pointer = 1;
      break;

    default:
      fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
              field->name, t->type);
      /* fallthru - treat it as complex */

    /* as soon as we see one of these these members, it's bogus... */
    case RPC_FC_IP:
    case RPC_FC_ENCAPSULATED_UNION:
    case RPC_FC_NON_ENCAPSULATED_UNION:
    case RPC_FC_TRANSMIT_AS:
    case RPC_FC_REPRESENT_AS:
    case RPC_FC_PAD:
    case RPC_FC_EMBEDDED_COMPLEX:
    case RPC_FC_BOGUS_STRUCT:
      return RPC_FC_BOGUS_STRUCT;
    }
  }

1543
  if( has_variance )
1544
    return RPC_FC_CVSTRUCT;
1545
  if( has_conformance && has_pointer )
1546
    return RPC_FC_CPSTRUCT;
1547
  if( has_conformance )
1548 1549 1550 1551 1552 1553
    return RPC_FC_CSTRUCT;
  if( has_pointer )
    return RPC_FC_PSTRUCT;
  return RPC_FC_STRUCT;
}

1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568
/***** constant repository *****/

struct rconst {
  char *name;
  var_t *var;
  struct rconst *next;
};

struct rconst *const_hash[HASHMAX];

static var_t *reg_const(var_t *var)
{
  struct rconst *nc;
  int hash;
  if (!var->name) {
1569
    yyerror("registering constant without name");
1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586
    return var;
  }
  hash = hash_ident(var->name);
  nc = xmalloc(sizeof(struct rconst));
  nc->name = var->name;
  nc->var = var;
  nc->next = const_hash[hash];
  const_hash[hash] = nc;
  return var;
}

static var_t *find_const(char *name, int f)
{
  struct rconst *cur = const_hash[hash_ident(name)];
  while (cur && strcmp(cur->name, name))
    cur = cur->next;
  if (!cur) {
1587
    if (f) yyerror("constant '%s' not found", name);
1588 1589 1590 1591
    return NULL;
  }
  return cur->var;
}
1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615

static void write_libid(const char *name, const attr_t *attr)
{
  const UUID *uuid = get_attrp(attr, ATTR_UUID);
  write_guid(idfile, "LIBID", name, uuid);
}

static void write_clsid(type_t *cls)
{
  const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
  write_guid(idfile, "CLSID", cls->name, uuid);
}

static void write_diid(type_t *iface)
{
  const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
  write_guid(idfile, "DIID", iface->name, uuid);
}

static void write_iid(type_t *iface)
{
  const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
  write_guid(idfile, "IID", iface->name, uuid);
}
1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638

static int compute_method_indexes(type_t *iface)
{
  int idx;
  func_t *f = iface->funcs;

  if (iface->ref)
    idx = compute_method_indexes(iface->ref);
  else
    idx = 0;

  if (! f)
    return idx;

  while (NEXT_LINK(f))
    f = NEXT_LINK(f);

  for ( ; f ; f = PREV_LINK(f))
    if (! is_callas(f->def->attrs))
      f->idx = idx++;

  return idx;
}
1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663

static char *gen_name(void)
{
  static const char format[] = "__WIDL_%s_generated_name_%08lX";
  static unsigned long n = 0;
  static const char *file_id;
  static size_t size;
  char *name;

  if (! file_id)
  {
    char *dst = dup_basename(input_name, ".idl");
    file_id = dst;

    for (; *dst; ++dst)
      if (! isalnum((unsigned char) *dst))
        *dst = '_';

    size = sizeof format - 7 + strlen(file_id) + 8;
  }

  name = xmalloc(size);
  sprintf(name, format, file_id, n++);
  return name;
}
1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675

static void process_typedefs(var_t *names)
{
  END_OF_LIST(names);
  while (names)
  {
    var_t *next = PREV_LINK(names);
    type_t *type = find_type(names->name, 0);

    if (! parse_only && do_header)
      write_typedef(type);
    if (in_typelib && type->attrs)
1676
      add_typelib_entry(type);
1677 1678 1679 1680 1681

    free(names);
    names = next;
  }
}
1682 1683 1684 1685 1686 1687 1688 1689

static void check_arg(var_t *arg)
{
  type_t *t = arg->type;

  if (t->type == 0 && ! is_var_ptr(arg))
    yyerror("argument '%s' has void type", arg->name);
}