widl: Generate GUID (infile_i.c) files.
[wine/gsoc_dplay.git] / tools / widl / parser.y
blobca5db5c9cc783a11210e9a9f199e532c06d40ead
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <string.h>
30 #ifdef HAVE_ALLOCA_H
31 #include <alloca.h>
32 #endif
34 #include "windef.h"
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "typelib.h"
42 #if defined(YYBYACC)
43 /* Berkeley yacc (byacc) doesn't seem to know about these */
44 /* Some *BSD supplied versions do define these though */
45 # ifndef YYEMPTY
46 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
47 # endif
48 # ifndef YYLEX
49 # define YYLEX yylex()
50 # endif
52 #elif defined(YYBISON)
53 /* Bison was used for original development */
54 /* #define YYEMPTY -2 */
55 /* #define YYLEX yylex() */
57 #else
58 /* No yacc we know yet */
59 # if !defined(YYEMPTY) || !defined(YYLEX)
60 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
61 # elif defined(__GNUC__) /* gcc defines the #warning directive */
62 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
63 /* #else we just take a chance that it works... */
64 # endif
65 #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 static expr_t *make_expr(enum expr_type type);
71 static expr_t *make_exprl(enum expr_type type, long val);
72 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 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
75 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
76 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
77 static type_t *make_type(unsigned char type, type_t *ref);
78 static typeref_t *make_tref(char *name, type_t *ref);
79 static typeref_t *uniq_tref(typeref_t *ref);
80 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 static var_t *make_var(char *name);
84 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);
88 static type_t *reg_type(type_t *type, const char *name, int t);
89 static type_t *reg_types(type_t *type, var_t *names, int t);
90 static type_t *find_type(const char *name, int t);
91 static type_t *find_type2(char *name, int t);
92 static type_t *get_type(unsigned char type, char *name, int t);
93 static type_t *get_typev(unsigned char type, var_t *name, int t);
94 static int get_struct_type(var_t *fields);
96 static var_t *reg_const(var_t *var);
97 static var_t *find_const(char *name, int f);
99 static void write_libid(const char *name, const attr_t *attr);
100 static void write_clsid(type_t *cls);
101 static void write_diid(type_t *iface);
102 static void write_iid(type_t *iface);
104 #define tsENUM 1
105 #define tsSTRUCT 2
106 #define tsUNION 3
108 static type_t std_bool = { "boolean" };
109 static type_t std_int = { "int" };
110 static type_t std_int64 = { "__int64" };
111 static type_t std_uhyper = { "MIDL_uhyper" };
114 %union {
115 attr_t *attr;
116 expr_t *expr;
117 type_t *type;
118 typeref_t *tref;
119 var_t *var;
120 func_t *func;
121 ifref_t *ifref;
122 char *str;
123 UUID *uuid;
124 unsigned int num;
127 %token <str> aIDENTIFIER
128 %token <str> aKNOWNTYPE
129 %token <num> aNUM aHEXNUM
130 %token <str> aSTRING
131 %token <uuid> aUUID
132 %token aEOF
133 %token SHL SHR
134 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
135 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
136 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
137 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
138 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
139 %token tDEFAULT
140 %token tDEFAULTCOLLELEM
141 %token tDEFAULTVALUE
142 %token tDEFAULTVTABLE
143 %token tDISPLAYBIND
144 %token tDISPINTERFACE
145 %token tDLLNAME tDOUBLE tDUAL
146 %token tENDPOINT
147 %token tENTRY tENUM tERRORSTATUST
148 %token tEXPLICITHANDLE tEXTERN
149 %token tFALSE
150 %token tFLOAT
151 %token tHANDLE
152 %token tHANDLET
153 %token tHELPCONTEXT tHELPFILE
154 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
155 %token tHIDDEN
156 %token tHYPER tID tIDEMPOTENT
157 %token tIIDIS
158 %token tIMMEDIATEBIND
159 %token tIMPLICITHANDLE
160 %token tIMPORT tIMPORTLIB
161 %token tIN tINLINE
162 %token tINPUTSYNC
163 %token tINT tINT64
164 %token tINTERFACE
165 %token tLCID
166 %token tLENGTHIS tLIBRARY
167 %token tLOCAL
168 %token tLONG
169 %token tMETHODS
170 %token tMODULE
171 %token tNONBROWSABLE
172 %token tNONCREATABLE
173 %token tNONEXTENSIBLE
174 %token tOBJECT tODL tOLEAUTOMATION
175 %token tOPTIONAL
176 %token tOUT
177 %token tPOINTERDEFAULT
178 %token tPROPERTIES
179 %token tPROPGET tPROPPUT tPROPPUTREF
180 %token tPTR
181 %token tPUBLIC
182 %token tRANGE
183 %token tREADONLY tREF
184 %token tREQUESTEDIT
185 %token tRESTRICTED
186 %token tRETVAL
187 %token tSAFEARRAY
188 %token tSHORT
189 %token tSIGNED
190 %token tSINGLE
191 %token tSIZEIS tSIZEOF
192 %token tSMALL
193 %token tSOURCE
194 %token tSTDCALL
195 %token tSTRING tSTRUCT
196 %token tSWITCH tSWITCHIS tSWITCHTYPE
197 %token tTRANSMITAS
198 %token tTRUE
199 %token tTYPEDEF
200 %token tUNION
201 %token tUNIQUE
202 %token tUNSIGNED
203 %token tUUID
204 %token tV1ENUM
205 %token tVARARG
206 %token tVERSION
207 %token tVOID
208 %token tWCHAR tWIREMARSHAL
210 %type <attr> m_attributes attributes attrib_list attribute
211 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
212 %type <expr> array array_list
213 %type <type> inherit interface interfacehdr interfacedef interfacedec
214 %type <type> dispinterface dispinterfacehdr dispinterfacedef
215 %type <type> module modulehdr moduledef
216 %type <type> base_type int_std
217 %type <type> enumdef structdef typedef uniondef
218 %type <ifref> gbl_statements coclass_ints coclass_int
219 %type <tref> type
220 %type <var> m_args no_args args arg
221 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
222 %type <var> m_ident t_ident ident p_ident pident pident_list
223 %type <var> dispint_props
224 %type <func> funcdef int_statements
225 %type <func> dispint_meths
226 %type <type> coclass coclasshdr coclassdef
227 %type <num> pointer_type version
228 %type <str> libraryhdr
230 %left ','
231 %right '?' ':'
232 %left '|'
233 %left '&'
234 %left '-' '+'
235 %left '*' '/'
236 %left SHL SHR
237 %right '~'
238 %right CAST
239 %right PPTR
240 %right NEG
244 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
247 gbl_statements: { $$ = NULL; }
248 | gbl_statements interfacedec { $$ = $1; }
249 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
250 | gbl_statements coclass ';' { $$ = $1;
251 reg_type($2, $2->name, 0);
252 if (!parse_only && do_header) write_coclass_forward($2);
254 | gbl_statements coclassdef { $$ = $1;
255 add_coclass($2);
256 reg_type($2, $2->name, 0);
257 if (!parse_only && do_header) write_coclass_forward($2);
259 | gbl_statements moduledef { $$ = $1; add_module($2); }
260 | gbl_statements librarydef { $$ = $1; }
261 | gbl_statements statement { $$ = $1; }
264 imp_statements: {}
265 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
266 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
267 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
268 | imp_statements coclassdef { if (!parse_only) add_coclass($2);
269 reg_type($2, $2->name, 0);
270 if (!parse_only && do_header) write_coclass_forward($2);
272 | imp_statements moduledef { if (!parse_only) add_module($2); }
273 | imp_statements statement {}
274 | imp_statements importlib {}
277 int_statements: { $$ = NULL; }
278 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
279 | int_statements statement { $$ = $1; }
282 statement: ';' {}
283 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
284 | cppquote {}
285 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
286 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
287 | import {}
288 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
289 | typedef ';' {}
290 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
293 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
295 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
296 if (!do_import($2)) yychar = aEOF; }
298 import: import_start imp_statements aEOF {}
301 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
304 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
306 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
307 if (!parse_only && do_header) write_library($2, $1);
308 if (!parse_only && do_idfile) write_libid($2, $1);
311 librarydef: library_start imp_statements '}' { end_typelib(); }
314 m_args: { $$ = NULL; }
315 | args
318 no_args: tVOID { $$ = NULL; }
321 args: arg
322 | args ',' arg { LINK($3, $1); $$ = $3; }
323 | no_args
326 /* split into two rules to get bison to resolve a tVOID conflict */
327 arg: attributes type pident array { $$ = $3;
328 set_type($$, $2, $4);
329 $$->attrs = $1;
331 | type pident array { $$ = $2;
332 set_type($$, $1, $3);
334 | attributes type pident '(' m_args ')' { $$ = $3;
335 $$->ptr_level--;
336 set_type($$, $2, NULL);
337 $$->attrs = $1;
338 $$->args = $5;
340 | type pident '(' m_args ')' { $$ = $2;
341 $$->ptr_level--;
342 set_type($$, $1, NULL);
343 $$->args = $4;
347 array: { $$ = NULL; }
348 | '[' array_list ']' { $$ = $2; }
349 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
352 array_list: m_expr /* size of first dimension is optional */
353 | array_list ',' expr { LINK($3, $1); $$ = $3; }
354 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
357 m_attributes: { $$ = NULL; }
358 | attributes
361 attributes:
362 '[' attrib_list ']' { $$ = $2;
363 if (!$$)
364 yyerror("empty attribute lists unsupported");
368 attrib_list: attribute
369 | attrib_list ',' attribute { if ($3) { LINK($3, $1); $$ = $3; }
370 else { $$ = $1; }
372 | attrib_list ']' '[' attribute { if ($4) { LINK($4, $1); $$ = $4; }
373 else { $$ = $1; }
377 attribute: { $$ = NULL; }
378 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
379 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
380 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
381 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
382 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
383 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
384 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
385 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
386 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
387 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
388 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
389 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
390 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
391 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
392 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
393 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
394 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
395 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
396 | tDUAL { $$ = make_attr(ATTR_DUAL); }
397 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
398 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
399 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
400 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
401 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
402 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
403 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
404 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
405 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
406 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
407 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
408 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
409 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
410 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
411 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
412 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
413 | tIN { $$ = make_attr(ATTR_IN); }
414 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
415 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
416 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
417 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
418 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
419 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
420 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
421 | tODL { $$ = make_attr(ATTR_ODL); }
422 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
423 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
424 | tOUT { $$ = make_attr(ATTR_OUT); }
425 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
426 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
427 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
428 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
429 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
430 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
431 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
432 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
433 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
434 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
435 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
436 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
437 | tSTRING { $$ = make_attr(ATTR_STRING); }
438 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
439 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
440 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
441 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
442 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
443 | tVARARG { $$ = make_attr(ATTR_VARARG); }
444 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
445 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
446 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
449 callconv:
450 | tSTDCALL
453 cases: { $$ = NULL; }
454 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
455 else { $$ = $1; }
459 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
460 $$ = $4; if (!$$) $$ = make_var(NULL);
461 LINK(a, $$->attrs); $$->attrs = a;
463 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
464 $$ = $3; if (!$$) $$ = make_var(NULL);
465 LINK(a, $$->attrs); $$->attrs = a;
469 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
470 set_type($$, $2, NULL);
471 $$->eval = $5;
475 enums: { $$ = NULL; }
476 | enum_list ',' { $$ = $1; }
477 | enum_list
480 enum_list: enum { if (!$$->eval)
481 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
483 | enum_list ',' enum { LINK($3, $1); $$ = $3;
484 if (!$$->eval)
485 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
489 enum: ident '=' expr_const { $$ = reg_const($1);
490 $$->eval = $3;
491 $$->type = make_type(RPC_FC_LONG, &std_int);
493 | ident { $$ = reg_const($1);
494 $$->type = make_type(RPC_FC_LONG, &std_int);
498 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
499 $$->kind = TKIND_ENUM;
500 $$->fields = $4;
501 $$->defined = TRUE;
502 if(in_typelib)
503 add_enum($$);
507 m_exprs: m_expr
508 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
512 exprs: { $$ = make_expr(EXPR_VOID); }
513 | expr_list
516 expr_list: expr
517 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
521 m_expr: { $$ = make_expr(EXPR_VOID); }
522 | expr
525 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
526 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
527 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
528 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
529 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
530 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
531 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
532 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
533 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
534 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
535 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
536 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
537 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
538 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
539 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
540 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
541 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
542 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
543 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
544 | '(' expr ')' { $$ = $2; }
547 expr_list_const: expr_const
548 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
551 expr_const: expr { $$ = $1;
552 if (!$$->is_const)
553 yyerror("expression is not constant");
557 externdef: tEXTERN tCONST type ident { $$ = $4;
558 set_type($$, $3, NULL);
562 fields: { $$ = NULL; }
563 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
564 else { $$ = $1; }
568 field: s_field ';' { $$ = $1; }
569 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
570 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
571 | ';' { $$ = NULL; }
574 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
577 funcdef:
578 m_attributes type callconv pident
579 '(' m_args ')' { set_type($4, $2, NULL);
580 $4->attrs = $1;
581 $$ = make_func($4, $6);
582 if (is_attr($4->attrs, ATTR_IN)) {
583 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
588 m_ident: { $$ = NULL; }
589 | ident
592 t_ident: { $$ = NULL; }
593 | aIDENTIFIER { $$ = make_var($1); }
594 | aKNOWNTYPE { $$ = make_var($1); }
597 ident: aIDENTIFIER { $$ = make_var($1); }
598 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
599 | aKNOWNTYPE { $$ = make_var($<str>1); }
600 | tASYNC { $$ = make_var($<str>1); }
601 | tID { $$ = make_var($<str>1); }
602 | tLCID { $$ = make_var($<str>1); }
603 | tOBJECT { $$ = make_var($<str>1); }
604 | tRANGE { $$ = make_var($<str>1); }
605 | tRETVAL { $$ = make_var($<str>1); }
606 | tUUID { $$ = make_var($<str>1); }
607 | tVERSION { $$ = make_var($<str>1); }
610 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
611 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
612 | int_std
613 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
614 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
615 switch ($$->type) {
616 case RPC_FC_CHAR: break;
617 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
618 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
619 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
620 case RPC_FC_HYPER:
621 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
622 break;
623 default: break;
626 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
627 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
628 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
629 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
630 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
631 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
632 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
635 m_int:
636 | tINT
639 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
640 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
641 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
642 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
643 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
644 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
645 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
648 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
649 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
650 if ($$->defined) yyerror("multiple definition error");
651 if ($$->kind != TKIND_COCLASS) yyerror("%s was not declared a coclass", $2);
655 coclasshdr: attributes coclass { $$ = $2;
656 $$->attrs = $1;
657 if (!parse_only && do_header)
658 write_coclass($$);
659 if (!parse_only && do_idfile)
660 write_clsid($$);
664 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
665 $$->ifaces = $3;
666 $$->defined = TRUE;
670 coclass_ints: { $$ = NULL; }
671 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
674 coclass_int:
675 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
678 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
679 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
682 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
683 $$ = $2;
684 if ($$->defined) yyerror("multiple definition error");
685 attrs = make_attr(ATTR_DISPINTERFACE);
686 LINK(attrs, $1);
687 $$->attrs = attrs;
688 $$->ref = find_type("IDispatch", 0);
689 if (!$$->ref) yyerror("IDispatch is undefined");
690 $$->defined = TRUE;
691 if (!parse_only && do_header) write_forward($$);
695 dispint_props: tPROPERTIES ':' { $$ = NULL; }
696 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
699 dispint_meths: tMETHODS ':' { $$ = NULL; }
700 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
703 dispinterfacedef: dispinterfacehdr '{'
704 dispint_props
705 dispint_meths
706 '}' { $$ = $1;
707 $$->fields = $3;
708 $$->funcs = $4;
709 if (!parse_only && do_header) write_dispinterface($$);
710 if (!parse_only && do_idfile) write_diid($$);
712 /* FIXME: not sure how to handle this yet
713 | dispinterfacehdr '{' interface '}' { $$ = $1;
714 if (!parse_only && do_header) write_interface($$);
715 if (!parse_only && do_idfile) write_iid($$);
720 inherit: { $$ = NULL; }
721 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
724 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
725 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
728 interfacehdr: attributes interface { $$ = $2;
729 if ($$->defined) yyerror("multiple definition error");
730 $$->attrs = $1;
731 $$->defined = TRUE;
732 if (!parse_only && do_header) write_forward($$);
736 interfacedef: interfacehdr inherit
737 '{' int_statements '}' { $$ = $1;
738 $$->ref = $2;
739 $$->funcs = $4;
740 if (!parse_only && do_header) write_interface($$);
741 if (!parse_only && do_idfile) write_iid($$);
743 /* MIDL is able to import the definition of a base class from inside the
744 * definition of a derived class, I'll try to support it with this rule */
745 | interfacehdr ':' aIDENTIFIER
746 '{' import int_statements '}' { $$ = $1;
747 $$->ref = find_type2($3, 0);
748 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
749 $$->funcs = $6;
750 if (!parse_only && do_header) write_interface($$);
751 if (!parse_only && do_idfile) write_iid($$);
753 | dispinterfacedef { $$ = $1; }
756 interfacedec:
757 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
758 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
761 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
762 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
765 modulehdr: attributes module { $$ = $2;
766 $$->attrs = $1;
770 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
771 $$->funcs = $3;
772 /* FIXME: if (!parse_only && do_header) write_module($$); */
776 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
777 | tCONST p_ident { $$ = $2; /* FIXME */ }
780 pident: ident
781 | p_ident
782 | '(' pident ')' { $$ = $2; }
785 pident_list:
786 pident
787 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
790 pointer_type:
791 tREF { $$ = RPC_FC_RP; }
792 | tUNIQUE { $$ = RPC_FC_UP; }
793 | tPTR { $$ = RPC_FC_FP; }
796 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
797 /* overwrite RPC_FC_STRUCT with a more exact type */
798 $$->type = get_struct_type( $4 );
799 $$->kind = TKIND_RECORD;
800 $$->fields = $4;
801 $$->defined = TRUE;
802 if(in_typelib)
803 add_struct($$);
807 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
808 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
809 | base_type { $$ = make_tref(NULL, $1); }
810 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
811 | enumdef { $$ = make_tref(NULL, $1); }
812 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
813 | structdef { $$ = make_tref(NULL, $1); }
814 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
815 | uniondef { $$ = make_tref(NULL, $1); }
816 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
817 | tSAFEARRAY '(' type ')' { $$ = make_tref(NULL, make_safearray()); }
820 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
821 $4->tname = tref->name;
822 tref->name = NULL;
823 $$ = type_ref(tref);
824 $$->attrs = $2;
825 if (!parse_only && do_header)
826 write_typedef($$, $4);
827 if (in_typelib && $$->attrs)
828 add_typedef($$, $4);
829 reg_types($$, $4, 0);
833 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
834 $$->kind = TKIND_UNION;
835 $$->fields = $4;
836 $$->defined = TRUE;
838 | tUNION t_ident
839 tSWITCH '(' s_field ')'
840 m_ident '{' cases '}' { var_t *u = $7;
841 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
842 $$->kind = TKIND_UNION;
843 if (!u) u = make_var("tagged_union");
844 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
845 u->type->kind = TKIND_UNION;
846 u->type->fields = $9;
847 u->type->defined = TRUE;
848 LINK(u, $5); $$->fields = u;
849 $$->defined = TRUE;
853 version:
854 aNUM { $$ = MAKELONG($1, 0); }
855 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
860 static attr_t *make_attr(enum attr_type type)
862 attr_t *a = xmalloc(sizeof(attr_t));
863 a->type = type;
864 a->u.ival = 0;
865 INIT_LINK(a);
866 return a;
869 static attr_t *make_attrv(enum attr_type type, unsigned long val)
871 attr_t *a = xmalloc(sizeof(attr_t));
872 a->type = type;
873 a->u.ival = val;
874 INIT_LINK(a);
875 return a;
878 static attr_t *make_attrp(enum attr_type type, void *val)
880 attr_t *a = xmalloc(sizeof(attr_t));
881 a->type = type;
882 a->u.pval = val;
883 INIT_LINK(a);
884 return a;
887 static expr_t *make_expr(enum expr_type type)
889 expr_t *e = xmalloc(sizeof(expr_t));
890 e->type = type;
891 e->ref = NULL;
892 e->u.lval = 0;
893 e->is_const = FALSE;
894 INIT_LINK(e);
895 return e;
898 static expr_t *make_exprl(enum expr_type type, long val)
900 expr_t *e = xmalloc(sizeof(expr_t));
901 e->type = type;
902 e->ref = NULL;
903 e->u.lval = val;
904 e->is_const = FALSE;
905 INIT_LINK(e);
906 /* check for numeric constant */
907 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
908 /* make sure true/false value is valid */
909 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
910 e->is_const = TRUE;
911 e->cval = val;
913 return e;
916 static expr_t *make_exprs(enum expr_type type, char *val)
918 expr_t *e;
919 e = xmalloc(sizeof(expr_t));
920 e->type = type;
921 e->ref = NULL;
922 e->u.sval = val;
923 e->is_const = FALSE;
924 INIT_LINK(e);
925 /* check for predefined constants */
926 if (type == EXPR_IDENTIFIER) {
927 var_t *c = find_const(val, 0);
928 if (c) {
929 e->u.sval = c->name;
930 free(val);
931 e->is_const = TRUE;
932 e->cval = c->eval->cval;
935 return e;
938 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
940 expr_t *e;
941 e = xmalloc(sizeof(expr_t));
942 e->type = type;
943 e->ref = expr;
944 e->u.tref = tref;
945 e->is_const = FALSE;
946 INIT_LINK(e);
947 /* check for cast of constant expression */
948 if (type == EXPR_SIZEOF) {
949 switch (tref->ref->type) {
950 case RPC_FC_BYTE:
951 case RPC_FC_CHAR:
952 case RPC_FC_SMALL:
953 case RPC_FC_USMALL:
954 e->is_const = TRUE;
955 e->cval = 1;
956 break;
957 case RPC_FC_WCHAR:
958 case RPC_FC_USHORT:
959 case RPC_FC_SHORT:
960 e->is_const = TRUE;
961 e->cval = 2;
962 break;
963 case RPC_FC_LONG:
964 case RPC_FC_ULONG:
965 case RPC_FC_FLOAT:
966 case RPC_FC_ERROR_STATUS_T:
967 e->is_const = TRUE;
968 e->cval = 4;
969 break;
970 case RPC_FC_HYPER:
971 case RPC_FC_DOUBLE:
972 e->is_const = TRUE;
973 e->cval = 8;
974 break;
977 if (type == EXPR_CAST && expr->is_const) {
978 e->is_const = TRUE;
979 e->cval = expr->cval;
981 return e;
984 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
986 expr_t *e;
987 e = xmalloc(sizeof(expr_t));
988 e->type = type;
989 e->ref = expr;
990 e->u.lval = 0;
991 e->is_const = FALSE;
992 INIT_LINK(e);
993 /* check for compile-time optimization */
994 if (expr->is_const) {
995 e->is_const = TRUE;
996 switch (type) {
997 case EXPR_NEG:
998 e->cval = -expr->cval;
999 break;
1000 case EXPR_NOT:
1001 e->cval = ~expr->cval;
1002 break;
1003 default:
1004 e->is_const = FALSE;
1005 break;
1008 return e;
1011 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1013 expr_t *e;
1014 e = xmalloc(sizeof(expr_t));
1015 e->type = type;
1016 e->ref = expr1;
1017 e->u.ext = expr2;
1018 e->is_const = FALSE;
1019 INIT_LINK(e);
1020 /* check for compile-time optimization */
1021 if (expr1->is_const && expr2->is_const) {
1022 e->is_const = TRUE;
1023 switch (type) {
1024 case EXPR_ADD:
1025 e->cval = expr1->cval + expr2->cval;
1026 break;
1027 case EXPR_SUB:
1028 e->cval = expr1->cval - expr2->cval;
1029 break;
1030 case EXPR_MUL:
1031 e->cval = expr1->cval * expr2->cval;
1032 break;
1033 case EXPR_DIV:
1034 e->cval = expr1->cval / expr2->cval;
1035 break;
1036 case EXPR_OR:
1037 e->cval = expr1->cval | expr2->cval;
1038 break;
1039 case EXPR_AND:
1040 e->cval = expr1->cval & expr2->cval;
1041 break;
1042 case EXPR_SHL:
1043 e->cval = expr1->cval << expr2->cval;
1044 break;
1045 case EXPR_SHR:
1046 e->cval = expr1->cval >> expr2->cval;
1047 break;
1048 default:
1049 e->is_const = FALSE;
1050 break;
1053 return e;
1056 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1058 expr_t *e;
1059 e = xmalloc(sizeof(expr_t));
1060 e->type = type;
1061 e->ref = expr1;
1062 e->u.ext = expr2;
1063 e->ext2 = expr3;
1064 e->is_const = FALSE;
1065 INIT_LINK(e);
1066 /* check for compile-time optimization */
1067 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1068 e->is_const = TRUE;
1069 switch (type) {
1070 case EXPR_COND:
1071 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1072 break;
1073 default:
1074 e->is_const = FALSE;
1075 break;
1078 return e;
1081 static type_t *make_type(unsigned char type, type_t *ref)
1083 type_t *t = xmalloc(sizeof(type_t));
1084 t->name = NULL;
1085 t->kind = TKIND_PRIMITIVE;
1086 t->type = type;
1087 t->ref = ref;
1088 t->attrs = NULL;
1089 t->funcs = NULL;
1090 t->fields = NULL;
1091 t->ifaces = NULL;
1092 t->ignore = parse_only;
1093 t->is_const = FALSE;
1094 t->sign = 0;
1095 t->defined = FALSE;
1096 t->written = FALSE;
1097 t->typelib_idx = -1;
1098 INIT_LINK(t);
1099 return t;
1102 static typeref_t *make_tref(char *name, type_t *ref)
1104 typeref_t *t = xmalloc(sizeof(typeref_t));
1105 t->name = name;
1106 t->ref = ref;
1107 t->uniq = ref ? 0 : 1;
1108 return t;
1111 static typeref_t *uniq_tref(typeref_t *ref)
1113 typeref_t *t = ref;
1114 type_t *tp;
1115 if (t->uniq) return t;
1116 tp = make_type(0, t->ref);
1117 tp->name = t->name;
1118 t->name = NULL;
1119 t->ref = tp;
1120 t->uniq = 1;
1121 return t;
1124 static type_t *type_ref(typeref_t *ref)
1126 type_t *t = ref->ref;
1127 if (ref->name) free(ref->name);
1128 free(ref);
1129 return t;
1132 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1134 v->type = ref->ref;
1135 v->tname = ref->name;
1136 ref->name = NULL;
1137 free(ref);
1138 v->array = arr;
1141 static ifref_t *make_ifref(type_t *iface)
1143 ifref_t *l = xmalloc(sizeof(ifref_t));
1144 l->iface = iface;
1145 l->attrs = NULL;
1146 INIT_LINK(l);
1147 return l;
1150 static var_t *make_var(char *name)
1152 var_t *v = xmalloc(sizeof(var_t));
1153 v->name = name;
1154 v->ptr_level = 0;
1155 v->type = NULL;
1156 v->tname = NULL;
1157 v->attrs = NULL;
1158 v->array = NULL;
1159 v->eval = NULL;
1160 INIT_LINK(v);
1161 return v;
1164 static func_t *make_func(var_t *def, var_t *args)
1166 func_t *f = xmalloc(sizeof(func_t));
1167 f->def = def;
1168 f->args = args;
1169 f->ignore = parse_only;
1170 f->idx = -1;
1171 INIT_LINK(f);
1172 return f;
1175 static type_t *make_class(char *name)
1177 type_t *c = make_type(0, NULL);
1178 c->name = name;
1179 c->kind = TKIND_COCLASS;
1180 INIT_LINK(c);
1181 return c;
1184 static type_t *make_safearray(void)
1186 return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
1189 #define HASHMAX 64
1191 static int hash_ident(const char *name)
1193 const char *p = name;
1194 int sum = 0;
1195 /* a simple sum hash is probably good enough */
1196 while (*p) {
1197 sum += *p;
1198 p++;
1200 return sum & (HASHMAX-1);
1203 /***** type repository *****/
1205 struct rtype {
1206 const char *name;
1207 type_t *type;
1208 int t;
1209 struct rtype *next;
1212 struct rtype *type_hash[HASHMAX];
1214 static type_t *reg_type(type_t *type, const char *name, int t)
1216 struct rtype *nt;
1217 int hash;
1218 if (!name) {
1219 yyerror("registering named type without name");
1220 return type;
1222 hash = hash_ident(name);
1223 nt = xmalloc(sizeof(struct rtype));
1224 nt->name = name;
1225 nt->type = type;
1226 nt->t = t;
1227 nt->next = type_hash[hash];
1228 type_hash[hash] = nt;
1229 return type;
1232 /* determine pointer type from attrs */
1233 static unsigned char get_pointer_type( type_t *type )
1235 int t;
1236 if (is_attr( type->attrs, ATTR_STRING ))
1238 type_t *t = type;
1239 while( t->type == 0 && t->ref )
1240 t = t->ref;
1241 switch( t->type )
1243 case RPC_FC_CHAR:
1244 return RPC_FC_C_CSTRING;
1245 case RPC_FC_WCHAR:
1246 return RPC_FC_C_WSTRING;
1249 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1250 if (t) return t;
1251 return RPC_FC_FP;
1254 static type_t *reg_types(type_t *type, var_t *names, int t)
1256 type_t *ptr = type;
1257 int ptrc = 0;
1259 while (names) {
1260 var_t *next = NEXT_LINK(names);
1261 if (names->name) {
1262 type_t *cur = ptr;
1263 int cptr = names->ptr_level;
1264 if (cptr > ptrc) {
1265 while (cptr > ptrc) {
1266 int t = get_pointer_type( cur );
1267 cur = ptr = make_type(t, cur);
1268 ptrc++;
1270 } else {
1271 while (cptr < ptrc) {
1272 cur = cur->ref;
1273 cptr++;
1276 reg_type(cur, names->name, t);
1278 free(names);
1279 names = next;
1281 return type;
1284 static type_t *find_type(const char *name, int t)
1286 struct rtype *cur = type_hash[hash_ident(name)];
1287 while (cur && (cur->t != t || strcmp(cur->name, name)))
1288 cur = cur->next;
1289 if (!cur) {
1290 yyerror("type '%s' not found", name);
1291 return NULL;
1293 return cur->type;
1296 static type_t *find_type2(char *name, int t)
1298 type_t *tp = find_type(name, t);
1299 free(name);
1300 return tp;
1303 int is_type(const char *name)
1305 struct rtype *cur = type_hash[hash_ident(name)];
1306 while (cur && (cur->t || strcmp(cur->name, name)))
1307 cur = cur->next;
1308 if (cur) return TRUE;
1309 return FALSE;
1312 static type_t *get_type(unsigned char type, char *name, int t)
1314 struct rtype *cur = NULL;
1315 type_t *tp;
1316 if (name) {
1317 cur = type_hash[hash_ident(name)];
1318 while (cur && (cur->t != t || strcmp(cur->name, name)))
1319 cur = cur->next;
1321 if (cur) {
1322 free(name);
1323 return cur->type;
1325 tp = make_type(type, NULL);
1326 tp->name = name;
1327 if (!name) return tp;
1328 return reg_type(tp, name, t);
1331 static type_t *get_typev(unsigned char type, var_t *name, int t)
1333 char *sname = NULL;
1334 if (name) {
1335 sname = name->name;
1336 free(name);
1338 return get_type(type, sname, t);
1341 static int get_struct_type(var_t *field)
1343 int has_pointer = 0;
1344 int has_conformance = 0;
1345 int has_variance = 0;
1347 for (; field; field = NEXT_LINK(field))
1349 type_t *t = field->type;
1351 /* get the base type */
1352 while( (t->type == 0) && t->ref )
1353 t = t->ref;
1355 if (field->ptr_level > 0)
1357 has_pointer = 1;
1358 continue;
1361 if (is_string_type(field->attrs, 0, field->array))
1363 has_conformance = 1;
1364 has_variance = 1;
1365 continue;
1368 if (is_array_type(field->attrs, 0, field->array))
1370 if (field->array && !field->array->is_const)
1372 has_conformance = 1;
1373 if (PREV_LINK(field))
1374 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1375 field->name);
1377 if (is_attr(field->attrs, ATTR_LENGTHIS))
1378 has_variance = 1;
1381 switch (t->type)
1384 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1385 * Simple types don't effect the type of struct.
1386 * A struct containing a simple struct is still a simple struct.
1387 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1389 case 0: /* void pointer */
1390 case RPC_FC_BYTE:
1391 case RPC_FC_CHAR:
1392 case RPC_FC_SMALL:
1393 case RPC_FC_USMALL:
1394 case RPC_FC_WCHAR:
1395 case RPC_FC_SHORT:
1396 case RPC_FC_USHORT:
1397 case RPC_FC_LONG:
1398 case RPC_FC_ULONG:
1399 case RPC_FC_INT3264:
1400 case RPC_FC_UINT3264:
1401 case RPC_FC_HYPER:
1402 case RPC_FC_FLOAT:
1403 case RPC_FC_DOUBLE:
1404 case RPC_FC_STRUCT:
1405 case RPC_FC_ENUM16:
1406 case RPC_FC_ENUM32:
1407 break;
1409 case RPC_FC_UP:
1410 case RPC_FC_FP:
1411 has_pointer = 1;
1412 break;
1413 case RPC_FC_CARRAY:
1414 has_conformance = 1;
1415 if (PREV_LINK(field))
1416 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1417 field->name);
1418 break;
1419 case RPC_FC_C_CSTRING:
1420 case RPC_FC_C_WSTRING:
1421 has_conformance = 1;
1422 has_variance = 1;
1423 break;
1426 * Propagate member attributes
1427 * a struct should be at least as complex as its member
1429 case RPC_FC_CVSTRUCT:
1430 has_conformance = 1;
1431 has_variance = 1;
1432 has_pointer = 1;
1433 break;
1435 case RPC_FC_CPSTRUCT:
1436 has_conformance = 1;
1437 if (PREV_LINK(field))
1438 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1439 field->name);
1440 has_pointer = 1;
1441 break;
1443 case RPC_FC_CSTRUCT:
1444 has_conformance = 1;
1445 if (PREV_LINK(field))
1446 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1447 field->name);
1448 break;
1450 case RPC_FC_PSTRUCT:
1451 has_pointer = 1;
1452 break;
1454 default:
1455 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1456 field->name, t->type);
1457 /* fallthru - treat it as complex */
1459 /* as soon as we see one of these these members, it's bogus... */
1460 case RPC_FC_IP:
1461 case RPC_FC_ENCAPSULATED_UNION:
1462 case RPC_FC_NON_ENCAPSULATED_UNION:
1463 case RPC_FC_TRANSMIT_AS:
1464 case RPC_FC_REPRESENT_AS:
1465 case RPC_FC_PAD:
1466 case RPC_FC_EMBEDDED_COMPLEX:
1467 case RPC_FC_BOGUS_STRUCT:
1468 return RPC_FC_BOGUS_STRUCT;
1472 if( has_variance )
1473 return RPC_FC_CVSTRUCT;
1474 if( has_conformance && has_pointer )
1475 return RPC_FC_CPSTRUCT;
1476 if( has_conformance )
1477 return RPC_FC_CSTRUCT;
1478 if( has_pointer )
1479 return RPC_FC_PSTRUCT;
1480 return RPC_FC_STRUCT;
1483 /***** constant repository *****/
1485 struct rconst {
1486 char *name;
1487 var_t *var;
1488 struct rconst *next;
1491 struct rconst *const_hash[HASHMAX];
1493 static var_t *reg_const(var_t *var)
1495 struct rconst *nc;
1496 int hash;
1497 if (!var->name) {
1498 yyerror("registering constant without name");
1499 return var;
1501 hash = hash_ident(var->name);
1502 nc = xmalloc(sizeof(struct rconst));
1503 nc->name = var->name;
1504 nc->var = var;
1505 nc->next = const_hash[hash];
1506 const_hash[hash] = nc;
1507 return var;
1510 static var_t *find_const(char *name, int f)
1512 struct rconst *cur = const_hash[hash_ident(name)];
1513 while (cur && strcmp(cur->name, name))
1514 cur = cur->next;
1515 if (!cur) {
1516 if (f) yyerror("constant '%s' not found", name);
1517 return NULL;
1519 return cur->var;
1522 static void write_libid(const char *name, const attr_t *attr)
1524 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1525 write_guid(idfile, "LIBID", name, uuid);
1528 static void write_clsid(type_t *cls)
1530 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1531 write_guid(idfile, "CLSID", cls->name, uuid);
1534 static void write_diid(type_t *iface)
1536 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1537 write_guid(idfile, "DIID", iface->name, uuid);
1540 static void write_iid(type_t *iface)
1542 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1543 write_guid(idfile, "IID", iface->name, uuid);