comctl32: A couple fixes for tab icon offsets.
[wine] / tools / widl / parser.y
1 %{
2 /*
3  * IDL Compiler
4  *
5  * Copyright 2002 Ove Kaaven
6  *
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.
11  *
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.
16  *
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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 #include "config.h"
23
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
33
34 #include "windef.h"
35
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "typelib.h"
41
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
51
52 #elif defined(YYBISON)
53         /* Bison was used for original development */
54         /* #define YYEMPTY -2 */
55         /* #define YYLEX   yylex() */
56
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
66
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 class_t *make_class(char *name);
86
87 static type_t *reg_type(type_t *type, const char *name, int t);
88 static type_t *reg_types(type_t *type, var_t *names, int t);
89 static type_t *find_type(const char *name, int t);
90 static type_t *find_type2(char *name, int t);
91 static type_t *get_type(unsigned char type, char *name, int t);
92 static type_t *get_typev(unsigned char type, var_t *name, int t);
93 static int get_struct_type(var_t *fields);
94
95 static var_t *reg_const(var_t *var);
96 static var_t *find_const(char *name, int f);
97
98 #define tsENUM   1
99 #define tsSTRUCT 2
100 #define tsUNION  3
101
102 static type_t std_bool = { "boolean" };
103 static type_t std_int = { "int" };
104 static type_t std_int64 = { "__int64" };
105 static type_t std_uhyper = { "MIDL_uhyper" };
106
107 %}
108 %union {
109         attr_t *attr;
110         expr_t *expr;
111         type_t *type;
112         typeref_t *tref;
113         var_t *var;
114         func_t *func;
115         ifref_t *ifref;
116         class_t *clas;
117         char *str;
118         UUID *uuid;
119         unsigned int num;
120 }
121
122 %token <str> aIDENTIFIER
123 %token <str> aKNOWNTYPE
124 %token <num> aNUM aHEXNUM
125 %token <str> aSTRING
126 %token <uuid> aUUID
127 %token aEOF
128 %token SHL SHR
129 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
130 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
131 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
132 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
133 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
134 %token tDEFAULT
135 %token tDEFAULTCOLLELEM
136 %token tDEFAULTVALUE
137 %token tDEFAULTVTABLE
138 %token tDISPLAYBIND
139 %token tDISPINTERFACE
140 %token tDLLNAME tDOUBLE tDUAL
141 %token tENDPOINT
142 %token tENTRY tENUM tERRORSTATUST
143 %token tEXPLICITHANDLE tEXTERN
144 %token tFLOAT
145 %token tHANDLE
146 %token tHANDLET
147 %token tHELPCONTEXT tHELPFILE
148 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
149 %token tHIDDEN
150 %token tHYPER tID tIDEMPOTENT
151 %token tIIDIS
152 %token tIMMEDIATEBIND
153 %token tIMPLICITHANDLE
154 %token tIMPORT tIMPORTLIB
155 %token tIN tINLINE
156 %token tINPUTSYNC
157 %token tINT tINT64
158 %token tINTERFACE
159 %token tLCID
160 %token tLENGTHIS tLIBRARY
161 %token tLOCAL
162 %token tLONG
163 %token tMETHODS
164 %token tMODULE
165 %token tNONBROWSABLE
166 %token tNONCREATABLE
167 %token tNONEXTENSIBLE
168 %token tOBJECT tODL tOLEAUTOMATION
169 %token tOPTIONAL
170 %token tOUT
171 %token tPOINTERDEFAULT
172 %token tPROPERTIES
173 %token tPROPGET tPROPPUT tPROPPUTREF
174 %token tPTR
175 %token tPUBLIC
176 %token tRANGE
177 %token tREADONLY tREF
178 %token tREQUESTEDIT
179 %token tRESTRICTED
180 %token tRETVAL
181 %token tSHORT
182 %token tSIGNED
183 %token tSINGLE
184 %token tSIZEIS tSIZEOF
185 %token tSMALL
186 %token tSOURCE
187 %token tSTDCALL
188 %token tSTRING tSTRUCT
189 %token tSWITCH tSWITCHIS tSWITCHTYPE
190 %token tTRANSMITAS
191 %token tTYPEDEF
192 %token tUNION
193 %token tUNIQUE
194 %token tUNSIGNED
195 %token tUUID
196 %token tV1ENUM
197 %token tVARARG
198 %token tVERSION
199 %token tVOID
200 %token tWCHAR tWIREMARSHAL
201
202 %type <attr> m_attributes attributes attrib_list attribute
203 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
204 %type <expr> array array_list
205 %type <type> inherit interface interfacehdr interfacedef interfacedec
206 %type <type> dispinterface dispinterfacehdr dispinterfacedef
207 %type <type> module modulehdr moduledef
208 %type <type> base_type int_std
209 %type <type> enumdef structdef typedef uniondef
210 %type <ifref> gbl_statements coclass_ints coclass_int
211 %type <tref> type
212 %type <var> m_args no_args args arg
213 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
214 %type <var> m_ident t_ident ident p_ident pident pident_list
215 %type <var> dispint_props
216 %type <func> funcdef int_statements
217 %type <func> dispint_meths
218 %type <clas> coclass coclasshdr coclassdef
219 %type <num> pointer_type version
220 %type <str> libraryhdr
221
222 %left ','
223 %right '?' ':'
224 %left '|'
225 %left '&'
226 %left '-' '+'
227 %left '*' '/'
228 %left SHL SHR
229 %right '~'
230 %right CAST
231 %right PPTR
232 %right NEG
233
234 %%
235
236 input:   gbl_statements                        { write_proxies($1); write_client($1); write_server($1); }
237         ;
238
239 gbl_statements:                                 { $$ = NULL; }
240         | gbl_statements interfacedec           { $$ = $1; }
241         | gbl_statements interfacedef           { $$ = make_ifref($2); LINK($$, $1); }
242         | gbl_statements coclassdef             { $$ = $1; add_coclass($2); }
243         | gbl_statements moduledef              { $$ = $1; add_module($2); }
244         | gbl_statements librarydef             { $$ = $1; }
245         | gbl_statements statement              { $$ = $1; }
246         ;
247
248 imp_statements:                                 {}
249         | imp_statements interfacedec           { if (!parse_only) add_interface($2); }
250         | imp_statements interfacedef           { if (!parse_only) add_interface($2); }
251         | imp_statements coclassdef             { if (!parse_only) add_coclass($2); }
252         | imp_statements moduledef              { if (!parse_only) add_module($2); }
253         | imp_statements statement              {}
254         ;
255
256 int_statements:                                 { $$ = NULL; }
257         | int_statements funcdef ';'            { $$ = $2; LINK($$, $1); }
258         | int_statements statement              { $$ = $1; }
259         ;
260
261 statement: ';'                                  {}
262         | constdef ';'                          { if (!parse_only && do_header) { write_constdef($1); } }
263         | cppquote                              {}
264         | enumdef ';'                           { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
265         | externdef ';'                         { if (!parse_only && do_header) { write_externdef($1); } }
266         | import                                {}
267         | structdef ';'                         { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
268         | typedef ';'                           {}
269         | uniondef ';'                          { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
270         ;
271
272 cppquote: tCPPQUOTE '(' aSTRING ')'             { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
273         ;
274 import_start: tIMPORT aSTRING ';'               { assert(yychar == YYEMPTY);
275                                                   if (!do_import($2)) yychar = aEOF; }
276         ;
277 import:   import_start imp_statements aEOF      {}
278         ;
279
280 libraryhdr: tLIBRARY aIDENTIFIER                { $$ = $2; }
281         ;
282 library_start: attributes libraryhdr '{'        { start_typelib($2, $1);
283                                                   if (!parse_only && do_header) write_library($2, $1); }
284         ;
285 librarydef: library_start imp_statements '}'    { end_typelib(); }
286         ;
287
288 m_args:                                         { $$ = NULL; }
289         | args
290         ;
291
292 no_args:  tVOID                                 { $$ = NULL; }
293         ;
294
295 args:     arg
296         | args ',' arg                          { LINK($3, $1); $$ = $3; }
297         | no_args
298         ;
299
300 /* split into two rules to get bison to resolve a tVOID conflict */
301 arg:      attributes type pident array          { $$ = $3;
302                                                   set_type($$, $2, $4);
303                                                   $$->attrs = $1;
304                                                 }
305         | type pident array                     { $$ = $2;
306                                                   set_type($$, $1, $3);
307                                                 }
308         | attributes type pident '(' m_args ')' { $$ = $3;
309                                                   $$->ptr_level--;
310                                                   set_type($$, $2, NULL);
311                                                   $$->attrs = $1;
312                                                   $$->args = $5;
313                                                 }
314         | type pident '(' m_args ')'            { $$ = $2;
315                                                   $$->ptr_level--;
316                                                   set_type($$, $1, NULL);
317                                                   $$->args = $4;
318                                                 }
319         ;
320
321 array:                                          { $$ = NULL; }
322         | '[' array_list ']'                    { $$ = $2; }
323         | '[' '*' ']'                           { $$ = make_expr(EXPR_VOID); }
324         ;
325
326 array_list: m_expr /* size of first dimension is optional */
327         | array_list ',' expr                   { LINK($3, $1); $$ = $3; }
328         | array_list ']' '[' expr               { LINK($4, $1); $$ = $4; }
329         ;
330
331 m_attributes:                                   { $$ = NULL; }
332         | attributes
333         ;
334
335 attributes:
336           '[' attrib_list ']'                   { $$ = $2; }
337         ;
338
339 attrib_list: attribute
340         | attrib_list ',' attribute             { LINK($3, $1); $$ = $3; }
341         | attrib_list ']' '[' attribute         { LINK($4, $1); $$ = $4; }
342         ;
343
344 attribute:
345           tAGGREGATABLE                         { $$ = make_attr(ATTR_AGGREGATABLE); }
346         | tAPPOBJECT                            { $$ = make_attr(ATTR_APPOBJECT); }
347         | tASYNC                                { $$ = make_attr(ATTR_ASYNC); }
348         | tAUTOHANDLE                           { $$ = make_attr(ATTR_AUTO_HANDLE); }
349         | tBINDABLE                             { $$ = make_attr(ATTR_BINDABLE); }
350         | tCALLAS '(' ident ')'                 { $$ = make_attrp(ATTR_CALLAS, $3); }
351         | tCASE '(' expr_list_const ')'         { $$ = make_attrp(ATTR_CASE, $3); }
352         | tCONTEXTHANDLE                        { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
353         | tCONTEXTHANDLENOSERIALIZE             { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
354         | tCONTEXTHANDLESERIALIZE               { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
355         | tCONTROL                              { $$ = make_attr(ATTR_CONTROL); }
356         | tDEFAULT                              { $$ = make_attr(ATTR_DEFAULT); }
357         | tDEFAULTCOLLELEM                      { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
358         | tDEFAULTVALUE '(' expr_const ')'      { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
359         | tDEFAULTVALUE '(' aSTRING ')'         { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
360         | tDEFAULTVTABLE                        { $$ = make_attr(ATTR_DEFAULTVTABLE); }
361         | tDISPLAYBIND                          { $$ = make_attr(ATTR_DISPLAYBIND); }
362         | tDLLNAME '(' aSTRING ')'              { $$ = make_attrp(ATTR_DLLNAME, $3); }
363         | tDUAL                                 { $$ = make_attr(ATTR_DUAL); }
364         | tENDPOINT '(' aSTRING ')'             { $$ = make_attrp(ATTR_ENDPOINT, $3); }
365         | tENTRY '(' aSTRING ')'                { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
366         | tENTRY '(' expr_const ')'             { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
367         | tEXPLICITHANDLE                       { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
368         | tHANDLE                               { $$ = make_attr(ATTR_HANDLE); }
369         | tHELPCONTEXT '(' expr_const ')'       { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
370         | tHELPFILE '(' aSTRING ')'             { $$ = make_attrp(ATTR_HELPFILE, $3); }
371         | tHELPSTRING '(' aSTRING ')'           { $$ = make_attrp(ATTR_HELPSTRING, $3); }
372         | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
373         | tHELPSTRINGDLL '(' aSTRING ')'        { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
374         | tHIDDEN                               { $$ = make_attr(ATTR_HIDDEN); }
375         | tID '(' expr_const ')'                { $$ = make_attrp(ATTR_ID, $3); }
376         | tIDEMPOTENT                           { $$ = make_attr(ATTR_IDEMPOTENT); }
377         | tIIDIS '(' ident ')'                  { $$ = make_attrp(ATTR_IIDIS, $3); }
378         | tIMMEDIATEBIND                        { $$ = make_attr(ATTR_IMMEDIATEBIND); }
379         | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')'  { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
380         | tIN                                   { $$ = make_attr(ATTR_IN); }
381         | tINPUTSYNC                            { $$ = make_attr(ATTR_INPUTSYNC); }
382         | tLENGTHIS '(' m_exprs ')'             { $$ = make_attrp(ATTR_LENGTHIS, $3); }
383         | tLOCAL                                { $$ = make_attr(ATTR_LOCAL); }
384         | tNONBROWSABLE                         { $$ = make_attr(ATTR_NONBROWSABLE); }
385         | tNONCREATABLE                         { $$ = make_attr(ATTR_NONCREATABLE); }
386         | tNONEXTENSIBLE                        { $$ = make_attr(ATTR_NONEXTENSIBLE); }
387         | tOBJECT                               { $$ = make_attr(ATTR_OBJECT); }
388         | tODL                                  { $$ = make_attr(ATTR_ODL); }
389         | tOLEAUTOMATION                        { $$ = make_attr(ATTR_OLEAUTOMATION); }
390         | tOPTIONAL                             { $$ = make_attr(ATTR_OPTIONAL); }
391         | tOUT                                  { $$ = make_attr(ATTR_OUT); }
392         | tPOINTERDEFAULT '(' pointer_type ')'  { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
393         | tPROPGET                              { $$ = make_attr(ATTR_PROPGET); }
394         | tPROPPUT                              { $$ = make_attr(ATTR_PROPPUT); }
395         | tPROPPUTREF                           { $$ = make_attr(ATTR_PROPPUTREF); }
396         | tPUBLIC                               { $$ = make_attr(ATTR_PUBLIC); }
397         | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
398         | tREADONLY                             { $$ = make_attr(ATTR_READONLY); }
399         | tREQUESTEDIT                          { $$ = make_attr(ATTR_REQUESTEDIT); }
400         | tRESTRICTED                           { $$ = make_attr(ATTR_RESTRICTED); }
401         | tRETVAL                               { $$ = make_attr(ATTR_RETVAL); }
402         | tSIZEIS '(' m_exprs ')'               { $$ = make_attrp(ATTR_SIZEIS, $3); }
403         | tSOURCE                               { $$ = make_attr(ATTR_SOURCE); }
404         | tSTRING                               { $$ = make_attr(ATTR_STRING); }
405         | tSWITCHIS '(' expr ')'                { $$ = make_attrp(ATTR_SWITCHIS, $3); }
406         | tSWITCHTYPE '(' type ')'              { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
407         | tTRANSMITAS '(' type ')'              { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
408         | tUUID '(' aUUID ')'                   { $$ = make_attrp(ATTR_UUID, $3); }
409         | tV1ENUM                               { $$ = make_attr(ATTR_V1ENUM); }
410         | tVARARG                               { $$ = make_attr(ATTR_VARARG); }
411         | tVERSION '(' version ')'              { $$ = make_attrv(ATTR_VERSION, $3); }
412         | tWIREMARSHAL '(' type ')'             { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
413         | pointer_type                          { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
414         ;
415
416 callconv:
417         | tSTDCALL
418         ;
419
420 cases:                                          { $$ = NULL; }
421         | cases case                            { if ($2) { LINK($2, $1); $$ = $2; }
422                                                   else { $$ = $1; }
423                                                 }
424         ;
425
426 case:     tCASE expr ':' field                  { attr_t *a = make_attrp(ATTR_CASE, $2);
427                                                   $$ = $4; if (!$$) $$ = make_var(NULL);
428                                                   LINK(a, $$->attrs); $$->attrs = a;
429                                                 }
430         | tDEFAULT ':' field                    { attr_t *a = make_attr(ATTR_DEFAULT);
431                                                   $$ = $3; if (!$$) $$ = make_var(NULL);
432                                                   LINK(a, $$->attrs); $$->attrs = a;
433                                                 }
434         ;
435
436 constdef: tCONST type ident '=' expr_const      { $$ = reg_const($3);
437                                                   set_type($$, $2, NULL);
438                                                   $$->eval = $5;
439                                                   $$->lval = $5->cval;
440                                                 }
441         ;
442
443 enums:                                          { $$ = NULL; }
444         | enum_list ','                         { $$ = $1; }
445         | enum_list
446         ;
447
448 enum_list: enum
449         | enum_list ',' enum                    { LINK($3, $1); $$ = $3;
450                                                   if ($1 && !$3->eval)
451                                                     $3->lval = $1->lval + 1;
452                                                 }
453         ;
454
455 enum:     ident '=' expr_const                  { $$ = reg_const($1);
456                                                   $$->eval = $3;
457                                                   $$->lval = $3->cval;
458                                                   $$->type = make_type(RPC_FC_LONG, &std_int);
459                                                 }
460         | ident                                 { $$ = reg_const($1);
461                                                   $$->lval = 0; /* default for first enum entry */
462                                                   $$->type = make_type(RPC_FC_LONG, &std_int);
463                                                 }
464         ;
465
466 enumdef: tENUM t_ident '{' enums '}'            { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
467                                                   $$->fields = $4;
468                                                   $$->defined = TRUE;
469                                                   if(in_typelib)
470                                                       add_enum($$);
471                                                 }
472         ;
473
474 m_exprs:  m_expr
475         | m_exprs ',' m_expr                    { LINK($3, $1); $$ = $3; }
476         ;
477
478 /*
479 exprs:                                          { $$ = make_expr(EXPR_VOID); }
480         | expr_list
481         ;
482
483 expr_list: expr
484         | expr_list ',' expr                    { LINK($3, $1); $$ = $3; }
485         ;
486 */
487
488 m_expr:                                         { $$ = make_expr(EXPR_VOID); }
489         | expr
490         ;
491
492 expr:     aNUM                                  { $$ = make_exprl(EXPR_NUM, $1); }
493         | aHEXNUM                               { $$ = make_exprl(EXPR_HEXNUM, $1); }
494         | aIDENTIFIER                           { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
495         | expr '?' expr ':' expr                { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
496         | expr '|' expr                         { $$ = make_expr2(EXPR_OR , $1, $3); }
497         | expr '&' expr                         { $$ = make_expr2(EXPR_AND, $1, $3); }
498         | expr '+' expr                         { $$ = make_expr2(EXPR_ADD, $1, $3); }
499         | expr '-' expr                         { $$ = make_expr2(EXPR_SUB, $1, $3); }
500         | expr '*' expr                         { $$ = make_expr2(EXPR_MUL, $1, $3); }
501         | expr '/' expr                         { $$ = make_expr2(EXPR_DIV, $1, $3); }
502         | expr SHL expr                         { $$ = make_expr2(EXPR_SHL, $1, $3); }
503         | expr SHR expr                         { $$ = make_expr2(EXPR_SHR, $1, $3); }
504         | '~' expr                              { $$ = make_expr1(EXPR_NOT, $2); }
505         | '-' expr %prec NEG                    { $$ = make_expr1(EXPR_NEG, $2); }
506         | '*' expr %prec PPTR                   { $$ = make_expr1(EXPR_PPTR, $2); }
507         | '(' type ')' expr %prec CAST          { $$ = make_exprt(EXPR_CAST, $2, $4); }
508         | tSIZEOF '(' type ')'                  { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
509         | '(' expr ')'                          { $$ = $2; }
510         ;
511
512 expr_list_const: expr_const
513         | expr_list_const ',' expr_const        { LINK($3, $1); $$ = $3; }
514         ;
515
516 expr_const: expr                                { $$ = $1;
517                                                   if (!$$->is_const)
518                                                       yyerror("expression is not constant");
519                                                 }
520         ;
521
522 externdef: tEXTERN tCONST type ident            { $$ = $4;
523                                                   set_type($$, $3, NULL);
524                                                 }
525         ;
526
527 fields:                                         { $$ = NULL; }
528         | fields field                          { if ($2) { LINK($2, $1); $$ = $2; }
529                                                   else { $$ = $1; }
530                                                 }
531         ;
532
533 field:    s_field ';'                           { $$ = $1; }
534         | m_attributes uniondef ';'             { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
535         | attributes ';'                        { $$ = make_var(NULL); $$->attrs = $1; }
536         | ';'                                   { $$ = NULL; }
537         ;
538
539 s_field:  m_attributes type pident array        { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
540         ;
541
542 funcdef:
543           m_attributes type callconv pident
544           '(' m_args ')'                        { set_type($4, $2, NULL);
545                                                   $4->attrs = $1;
546                                                   $$ = make_func($4, $6);
547                                                   if (is_attr($4->attrs, ATTR_IN)) {
548                                                     yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
549                                                   }
550                                                 }
551         ;
552
553 m_ident:                                        { $$ = NULL; }
554         | ident
555         ;
556
557 t_ident:                                        { $$ = NULL; }
558         | aIDENTIFIER                           { $$ = make_var($1); }
559         | aKNOWNTYPE                            { $$ = make_var($1); }
560         ;
561
562 ident:    aIDENTIFIER                           { $$ = make_var($1); }
563 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
564         | aKNOWNTYPE                            { $$ = make_var($<str>1); }
565         | tASYNC                                { $$ = make_var($<str>1); }
566         | tID                                   { $$ = make_var($<str>1); }
567         | tLCID                                 { $$ = make_var($<str>1); }
568         | tRANGE                                { $$ = make_var($<str>1); }
569         | tRETVAL                               { $$ = make_var($<str>1); }
570         | tVERSION                              { $$ = make_var($<str>1); }
571         ;
572
573 base_type: tBYTE                                { $$ = make_type(RPC_FC_BYTE, NULL); }
574         | tWCHAR                                { $$ = make_type(RPC_FC_WCHAR, NULL); }
575         | int_std
576         | tSIGNED int_std                       { $$ = $2; $$->sign = 1; }
577         | tUNSIGNED int_std                     { $$ = $2; $$->sign = -1;
578                                                   switch ($$->type) {
579                                                   case RPC_FC_CHAR:  break;
580                                                   case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
581                                                   case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
582                                                   case RPC_FC_LONG:  $$->type = RPC_FC_ULONG;  break;
583                                                   case RPC_FC_HYPER:
584                                                     if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
585                                                     break;
586                                                   default: break;
587                                                   }
588                                                 }
589         | tUNSIGNED                             { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
590         | tFLOAT                                { $$ = make_type(RPC_FC_FLOAT, NULL); }
591         | tSINGLE                               { $$ = make_type(RPC_FC_FLOAT, NULL); }
592         | tDOUBLE                               { $$ = make_type(RPC_FC_DOUBLE, NULL); }
593         | tBOOLEAN                              { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
594         | tERRORSTATUST                         { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
595         | tHANDLET                              { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
596         ;
597
598 m_int:
599         | tINT
600         ;
601
602 int_std:  tINT                                  { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
603         | tSHORT m_int                          { $$ = make_type(RPC_FC_SHORT, NULL); }
604         | tSMALL                                { $$ = make_type(RPC_FC_SMALL, NULL); }
605         | tLONG m_int                           { $$ = make_type(RPC_FC_LONG, NULL); }
606         | tHYPER m_int                          { $$ = make_type(RPC_FC_HYPER, NULL); }
607         | tINT64                                { $$ = make_type(RPC_FC_HYPER, &std_int64); }
608         | tCHAR                                 { $$ = make_type(RPC_FC_CHAR, NULL); }
609         ;
610
611 coclass:  tCOCLASS aIDENTIFIER                  { $$ = make_class($2); }
612         | tCOCLASS aKNOWNTYPE                   { $$ = make_class($2); }
613         ;
614
615 coclasshdr: attributes coclass                  { $$ = $2;
616                                                   $$->attrs = $1;
617                                                   if (!parse_only && do_header)
618                                                     write_coclass($$);
619                                                 }
620         ;
621
622 coclassdef: coclasshdr '{' coclass_ints '}'     { $$ = $1;
623                                                   $$->ifaces = $3;
624                                                 }
625         ;
626
627 coclass_ints:                                   { $$ = NULL; }
628         | coclass_ints coclass_int              { LINK($2, $1); $$ = $2; }
629         ;
630
631 coclass_int:
632           m_attributes interfacedec             { $$ = make_ifref($2); $$->attrs = $1; }
633         ;
634
635 dispinterface: tDISPINTERFACE aIDENTIFIER       { $$ = get_type(0, $2, 0); }
636         |      tDISPINTERFACE aKNOWNTYPE        { $$ = get_type(0, $2, 0); }
637         ;
638
639 dispinterfacehdr: attributes dispinterface      { attr_t *attrs;
640                                                   $$ = $2;
641                                                   if ($$->defined) yyerror("multiple definition error");
642                                                   attrs = make_attr(ATTR_DISPINTERFACE);
643                                                   LINK(attrs, $1);
644                                                   $$->attrs = attrs;
645                                                   $$->ref = find_type("IDispatch", 0);
646                                                   if (!$$->ref) yyerror("IDispatch is undefined");
647                                                   $$->defined = TRUE;
648                                                   if (!parse_only && do_header) write_forward($$);
649                                                 }
650         ;
651
652 dispint_props: tPROPERTIES ':'                  { $$ = NULL; }
653         | dispint_props s_field ';'             { LINK($2, $1); $$ = $2; }
654         ;
655
656 dispint_meths: tMETHODS ':'                     { $$ = NULL; }
657         | dispint_meths funcdef ';'             { LINK($2, $1); $$ = $2; }
658         ;
659
660 dispinterfacedef: dispinterfacehdr '{'
661           dispint_props
662           dispint_meths
663           '}'                                   { $$ = $1;
664                                                   $$->fields = $3;
665                                                   $$->funcs = $4;
666                                                   if (!parse_only && do_header) write_dispinterface($$);
667                                                 }
668 /* FIXME: not sure how to handle this yet
669         | dispinterfacehdr '{' interface '}'    { $$ = $1;
670                                                   if (!parse_only && do_header) write_interface($$);
671                                                 }
672 */
673         ;
674
675 inherit:                                        { $$ = NULL; }
676         | ':' aKNOWNTYPE                        { $$ = find_type2($2, 0); }
677         ;
678
679 interface: tINTERFACE aIDENTIFIER               { $$ = get_type(RPC_FC_IP, $2, 0); }
680         |  tINTERFACE aKNOWNTYPE                { $$ = get_type(RPC_FC_IP, $2, 0); }
681         ;
682
683 interfacehdr: attributes interface              { $$ = $2;
684                                                   if ($$->defined) yyerror("multiple definition error");
685                                                   $$->attrs = $1;
686                                                   $$->defined = TRUE;
687                                                   if (!parse_only && do_header) write_forward($$);
688                                                 }
689         ;
690
691 interfacedef: interfacehdr inherit
692           '{' int_statements '}'                { $$ = $1;
693                                                   $$->ref = $2;
694                                                   $$->funcs = $4;
695                                                   if (!parse_only && do_header) write_interface($$);
696                                                 }
697 /* MIDL is able to import the definition of a base class from inside the
698  * definition of a derived class, I'll try to support it with this rule */
699         | interfacehdr ':' aIDENTIFIER
700           '{' import int_statements '}'         { $$ = $1;
701                                                   $$->ref = find_type2($3, 0);
702                                                   if (!$$->ref) yyerror("base class '%s' not found in import", $3);
703                                                   $$->funcs = $6;
704                                                   if (!parse_only && do_header) write_interface($$);
705                                                 }
706         | dispinterfacedef                      { $$ = $1; }
707         ;
708
709 interfacedec:
710           interface ';'                         { $$ = $1; if (!parse_only && do_header) write_forward($$); }
711         | dispinterface ';'                     { $$ = $1; if (!parse_only && do_header) write_forward($$); }
712         ;
713
714 module:   tMODULE aIDENTIFIER                   { $$ = make_type(0, NULL); $$->name = $2; }
715         | tMODULE aKNOWNTYPE                    { $$ = make_type(0, NULL); $$->name = $2; }
716         ;
717
718 modulehdr: attributes module                    { $$ = $2;
719                                                   $$->attrs = $1;
720                                                 }
721         ;
722
723 moduledef: modulehdr '{' int_statements '}'     { $$ = $1;
724                                                   $$->funcs = $3;
725                                                   /* FIXME: if (!parse_only && do_header) write_module($$); */
726                                                 }
727         ;
728
729 p_ident:  '*' pident %prec PPTR                 { $$ = $2; $$->ptr_level++; }
730         | tCONST p_ident                        { $$ = $2; /* FIXME */ }
731         ;
732
733 pident:   ident
734         | p_ident
735         | '(' pident ')'                        { $$ = $2; }
736         ;
737
738 pident_list:
739           pident
740         | pident_list ',' pident                { LINK($3, $1); $$ = $3; }
741         ;
742
743 pointer_type:
744           tREF                                  { $$ = RPC_FC_RP; }
745         | tUNIQUE                               { $$ = RPC_FC_UP; }
746         | tPTR                                  { $$ = RPC_FC_FP; }
747         ;
748
749 structdef: tSTRUCT t_ident '{' fields '}'       { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
750                                                   /* overwrite RPC_FC_STRUCT with a more exact type */
751                                                   $$->type = get_struct_type( $4 );
752                                                   $$->fields = $4;
753                                                   $$->defined = TRUE;
754                                                   if(in_typelib)
755                                                       add_struct($$);
756                                                 }
757         ;
758
759 type:     tVOID                                 { $$ = make_tref(NULL, make_type(0, NULL)); }
760         | aKNOWNTYPE                            { $$ = make_tref($1, find_type($1, 0)); }
761         | base_type                             { $$ = make_tref(NULL, $1); }
762         | tCONST type                           { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
763         | enumdef                               { $$ = make_tref(NULL, $1); }
764         | tENUM aIDENTIFIER                     { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
765         | structdef                             { $$ = make_tref(NULL, $1); }
766         | tSTRUCT aIDENTIFIER                   { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
767         | uniondef                              { $$ = make_tref(NULL, $1); }
768         | tUNION aIDENTIFIER                    { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
769         ;
770
771 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
772                                                   $4->tname = tref->name;
773                                                   tref->name = NULL;
774                                                   $$ = type_ref(tref);
775                                                   $$->attrs = $2;
776                                                   if (!parse_only && do_header)
777                                                     write_typedef($$, $4);
778                                                   if (in_typelib && $$->attrs)
779                                                     add_typedef($$, $4);
780                                                   reg_types($$, $4, 0);
781                                                 }
782         ;
783
784 uniondef: tUNION t_ident '{' fields '}'         { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
785                                                   $$->fields = $4;
786                                                   $$->defined = TRUE;
787                                                 }
788         | tUNION t_ident
789           tSWITCH '(' s_field ')'
790           m_ident '{' cases '}'                 { var_t *u = $7;
791                                                   $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
792                                                   if (!u) u = make_var("tagged_union");
793                                                   u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
794                                                   u->type->fields = $9;
795                                                   u->type->defined = TRUE;
796                                                   LINK(u, $5); $$->fields = u;
797                                                   $$->defined = TRUE;
798                                                 }
799         ;
800
801 version:
802           aNUM                                  { $$ = MAKELONG($1, 0); }
803         | aNUM '.' aNUM                         { $$ = MAKELONG($1, $3); }
804         ;
805
806 %%
807
808 static attr_t *make_attr(enum attr_type type)
809 {
810   attr_t *a = xmalloc(sizeof(attr_t));
811   a->type = type;
812   a->u.ival = 0;
813   INIT_LINK(a);
814   return a;
815 }
816
817 static attr_t *make_attrv(enum attr_type type, unsigned long val)
818 {
819   attr_t *a = xmalloc(sizeof(attr_t));
820   a->type = type;
821   a->u.ival = val;
822   INIT_LINK(a);
823   return a;
824 }
825
826 static attr_t *make_attrp(enum attr_type type, void *val)
827 {
828   attr_t *a = xmalloc(sizeof(attr_t));
829   a->type = type;
830   a->u.pval = val;
831   INIT_LINK(a);
832   return a;
833 }
834
835 static expr_t *make_expr(enum expr_type type)
836 {
837   expr_t *e = xmalloc(sizeof(expr_t));
838   e->type = type;
839   e->ref = NULL;
840   e->u.lval = 0;
841   e->is_const = FALSE;
842   INIT_LINK(e);
843   return e;
844 }
845
846 static expr_t *make_exprl(enum expr_type type, long val)
847 {
848   expr_t *e = xmalloc(sizeof(expr_t));
849   e->type = type;
850   e->ref = NULL;
851   e->u.lval = val;
852   e->is_const = FALSE;
853   INIT_LINK(e);
854   /* check for numeric constant */
855   if (type == EXPR_NUM || type == EXPR_HEXNUM) {
856     e->is_const = TRUE;
857     e->cval = val;
858   }
859   return e;
860 }
861
862 static expr_t *make_exprs(enum expr_type type, char *val)
863 {
864   expr_t *e;
865   e = xmalloc(sizeof(expr_t));
866   e->type = type;
867   e->ref = NULL;
868   e->u.sval = val;
869   e->is_const = FALSE;
870   INIT_LINK(e);
871   /* check for predefined constants */
872   if (type == EXPR_IDENTIFIER) {
873     var_t *c = find_const(val, 0);
874     if (c) {
875       e->u.sval = c->name;
876       free(val);
877       e->is_const = TRUE;
878       e->cval = c->lval;
879     }
880   }
881   return e;
882 }
883
884 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
885 {
886   expr_t *e;
887   e = xmalloc(sizeof(expr_t));
888   e->type = type;
889   e->ref = expr;
890   e->u.tref = tref;
891   e->is_const = FALSE;
892   INIT_LINK(e);
893   /* check for cast of constant expression */
894   if (type == EXPR_SIZEOF) {
895     switch (tref->ref->type) {
896       case RPC_FC_BYTE:
897       case RPC_FC_CHAR:
898       case RPC_FC_SMALL:
899       case RPC_FC_USMALL:
900         e->is_const = TRUE;
901         e->cval = 1;
902         break;
903       case RPC_FC_WCHAR:
904       case RPC_FC_USHORT:
905       case RPC_FC_SHORT:
906         e->is_const = TRUE;
907         e->cval = 2;
908         break;
909       case RPC_FC_LONG:
910       case RPC_FC_ULONG:
911       case RPC_FC_FLOAT:
912       case RPC_FC_ERROR_STATUS_T:
913         e->is_const = TRUE;
914         e->cval = 4;
915         break;
916       case RPC_FC_HYPER:
917       case RPC_FC_DOUBLE:
918         e->is_const = TRUE;
919         e->cval = 8;
920         break;
921     }
922   }
923   if (type == EXPR_CAST && expr->is_const) {
924     e->is_const = TRUE;
925     e->cval = expr->cval;
926   }
927   return e;
928 }
929
930 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
931 {
932   expr_t *e;
933   e = xmalloc(sizeof(expr_t));
934   e->type = type;
935   e->ref = expr;
936   e->u.lval = 0;
937   e->is_const = FALSE;
938   INIT_LINK(e);
939   /* check for compile-time optimization */
940   if (expr->is_const) {
941     e->is_const = TRUE;
942     switch (type) {
943     case EXPR_NEG:
944       e->cval = -expr->cval;
945       break;
946     case EXPR_NOT:
947       e->cval = ~expr->cval;
948       break;
949     default:
950       e->is_const = FALSE;
951       break;
952     }
953   }
954   return e;
955 }
956
957 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
958 {
959   expr_t *e;
960   e = xmalloc(sizeof(expr_t));
961   e->type = type;
962   e->ref = expr1;
963   e->u.ext = expr2;
964   e->is_const = FALSE;
965   INIT_LINK(e);
966   /* check for compile-time optimization */
967   if (expr1->is_const && expr2->is_const) {
968     e->is_const = TRUE;
969     switch (type) {
970     case EXPR_ADD:
971       e->cval = expr1->cval + expr2->cval;
972       break;
973     case EXPR_SUB:
974       e->cval = expr1->cval - expr2->cval;
975       break;
976     case EXPR_MUL:
977       e->cval = expr1->cval * expr2->cval;
978       break;
979     case EXPR_DIV:
980       e->cval = expr1->cval / expr2->cval;
981       break;
982     case EXPR_OR:
983       e->cval = expr1->cval | expr2->cval;
984       break;
985     case EXPR_AND:
986       e->cval = expr1->cval & expr2->cval;
987       break;
988     case EXPR_SHL:
989       e->cval = expr1->cval << expr2->cval;
990       break;
991     case EXPR_SHR:
992       e->cval = expr1->cval >> expr2->cval;
993       break;
994     default:
995       e->is_const = FALSE;
996       break;
997     }
998   }
999   return e;
1000 }
1001
1002 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1003 {
1004   expr_t *e;
1005   e = xmalloc(sizeof(expr_t));
1006   e->type = type;
1007   e->ref = expr1;
1008   e->u.ext = expr2;
1009   e->ext2 = expr3;
1010   e->is_const = FALSE;
1011   INIT_LINK(e);
1012   /* check for compile-time optimization */
1013   if (expr1->is_const && expr2->is_const && expr3->is_const) {
1014     e->is_const = TRUE;
1015     switch (type) {
1016     case EXPR_COND:
1017       e->cval = expr1->cval ? expr2->cval : expr3->cval;
1018       break;
1019     default:
1020       e->is_const = FALSE;
1021       break;
1022     }
1023   }
1024   return e;
1025 }
1026
1027 static type_t *make_type(unsigned char type, type_t *ref)
1028 {
1029   type_t *t = xmalloc(sizeof(type_t));
1030   t->name = NULL;
1031   t->type = type;
1032   t->ref = ref;
1033   t->attrs = NULL;
1034   t->funcs = NULL;
1035   t->fields = NULL;
1036   t->ignore = parse_only;
1037   t->is_const = FALSE;
1038   t->sign = 0;
1039   t->defined = FALSE;
1040   t->written = FALSE;
1041   t->typelib_idx = -1;
1042   INIT_LINK(t);
1043   return t;
1044 }
1045
1046 static typeref_t *make_tref(char *name, type_t *ref)
1047 {
1048   typeref_t *t = xmalloc(sizeof(typeref_t));
1049   t->name = name;
1050   t->ref = ref;
1051   t->uniq = ref ? 0 : 1;
1052   return t;
1053 }
1054
1055 static typeref_t *uniq_tref(typeref_t *ref)
1056 {
1057   typeref_t *t = ref;
1058   type_t *tp;
1059   if (t->uniq) return t;
1060   tp = make_type(0, t->ref);
1061   tp->name = t->name;
1062   t->name = NULL;
1063   t->ref = tp;
1064   t->uniq = 1;
1065   return t;
1066 }
1067
1068 static type_t *type_ref(typeref_t *ref)
1069 {
1070   type_t *t = ref->ref;
1071   if (ref->name) free(ref->name);
1072   free(ref);
1073   return t;
1074 }
1075
1076 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1077 {
1078   v->type = ref->ref;
1079   v->tname = ref->name;
1080   ref->name = NULL;
1081   free(ref);
1082   v->array = arr;
1083 }
1084
1085 static ifref_t *make_ifref(type_t *iface)
1086 {
1087   ifref_t *l = xmalloc(sizeof(ifref_t));
1088   l->iface = iface;
1089   l->attrs = NULL;
1090   INIT_LINK(l);
1091   return l;
1092 }
1093
1094 static var_t *make_var(char *name)
1095 {
1096   var_t *v = xmalloc(sizeof(var_t));
1097   v->name = name;
1098   v->ptr_level = 0;
1099   v->type = NULL;
1100   v->tname = NULL;
1101   v->attrs = NULL;
1102   v->array = NULL;
1103   v->eval = NULL;
1104   v->lval = 0;
1105   INIT_LINK(v);
1106   return v;
1107 }
1108
1109 static func_t *make_func(var_t *def, var_t *args)
1110 {
1111   func_t *f = xmalloc(sizeof(func_t));
1112   f->def = def;
1113   f->args = args;
1114   f->ignore = parse_only;
1115   f->idx = -1;
1116   INIT_LINK(f);
1117   return f;
1118 }
1119
1120 static class_t *make_class(char *name)
1121 {
1122   class_t *c = xmalloc(sizeof(class_t));
1123   c->name = name;
1124   c->attrs = NULL;
1125   c->ifaces = NULL;
1126   INIT_LINK(c);
1127   return c;
1128 }
1129
1130 #define HASHMAX 64
1131
1132 static int hash_ident(const char *name)
1133 {
1134   const char *p = name;
1135   int sum = 0;
1136   /* a simple sum hash is probably good enough */
1137   while (*p) {
1138     sum += *p;
1139     p++;
1140   }
1141   return sum & (HASHMAX-1);
1142 }
1143
1144 /***** type repository *****/
1145
1146 struct rtype {
1147   const char *name;
1148   type_t *type;
1149   int t;
1150   struct rtype *next;
1151 };
1152
1153 struct rtype *type_hash[HASHMAX];
1154
1155 static type_t *reg_type(type_t *type, const char *name, int t)
1156 {
1157   struct rtype *nt;
1158   int hash;
1159   if (!name) {
1160     yyerror("registering named type without name");
1161     return type;
1162   }
1163   hash = hash_ident(name);
1164   nt = xmalloc(sizeof(struct rtype));
1165   nt->name = name;
1166   nt->type = type;
1167   nt->t = t;
1168   nt->next = type_hash[hash];
1169   type_hash[hash] = nt;
1170   return type;
1171 }
1172
1173 /* determine pointer type from attrs */
1174 static unsigned char get_pointer_type( type_t *type )
1175 {
1176   int t;
1177   if (is_attr( type->attrs, ATTR_STRING ))
1178   {
1179     type_t *t = type;
1180     while( t->type == 0 && t->ref )
1181       t = t->ref;
1182     switch( t->type )
1183     {
1184     case RPC_FC_CHAR:
1185       return RPC_FC_C_CSTRING;
1186     case RPC_FC_WCHAR:
1187       return RPC_FC_C_WSTRING;
1188     }
1189   }
1190   t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1191   if (t) return t;
1192   return RPC_FC_FP;
1193 }
1194
1195 static type_t *reg_types(type_t *type, var_t *names, int t)
1196 {
1197   type_t *ptr = type;
1198   int ptrc = 0;
1199
1200   while (names) {
1201     var_t *next = NEXT_LINK(names);
1202     if (names->name) {
1203       type_t *cur = ptr;
1204       int cptr = names->ptr_level;
1205       if (cptr > ptrc) {
1206         while (cptr > ptrc) {
1207           int t = get_pointer_type( cur );
1208           cur = ptr = make_type(t, cur);
1209           ptrc++;
1210         }
1211       } else {
1212         while (cptr < ptrc) {
1213           cur = cur->ref;
1214           cptr++;
1215         }
1216       }
1217       reg_type(cur, names->name, t);
1218     }
1219     free(names);
1220     names = next;
1221   }
1222   return type;
1223 }
1224
1225 static type_t *find_type(const char *name, int t)
1226 {
1227   struct rtype *cur = type_hash[hash_ident(name)];
1228   while (cur && (cur->t != t || strcmp(cur->name, name)))
1229     cur = cur->next;
1230   if (!cur) {
1231     yyerror("type '%s' not found", name);
1232     return NULL;
1233   }
1234   return cur->type;
1235 }
1236
1237 static type_t *find_type2(char *name, int t)
1238 {
1239   type_t *tp = find_type(name, t);
1240   free(name);
1241   return tp;
1242 }
1243
1244 int is_type(const char *name)
1245 {
1246   struct rtype *cur = type_hash[hash_ident(name)];
1247   while (cur && (cur->t || strcmp(cur->name, name)))
1248     cur = cur->next;
1249   if (cur) return TRUE;
1250   return FALSE;
1251 }
1252
1253 static type_t *get_type(unsigned char type, char *name, int t)
1254 {
1255   struct rtype *cur = NULL;
1256   type_t *tp;
1257   if (name) {
1258     cur = type_hash[hash_ident(name)];
1259     while (cur && (cur->t != t || strcmp(cur->name, name)))
1260       cur = cur->next;
1261   }
1262   if (cur) {
1263     free(name);
1264     return cur->type;
1265   }
1266   tp = make_type(type, NULL);
1267   tp->name = name;
1268   if (!name) return tp;
1269   return reg_type(tp, name, t);
1270 }
1271
1272 static type_t *get_typev(unsigned char type, var_t *name, int t)
1273 {
1274   char *sname = NULL;
1275   if (name) {
1276     sname = name->name;
1277     free(name);
1278   }
1279   return get_type(type, sname, t);
1280 }
1281
1282 static int get_struct_type(var_t *field)
1283 {
1284   int has_pointer = 0;
1285   int has_conformance = 0;
1286   int has_variance = 0;
1287
1288   for (; field; field = NEXT_LINK(field))
1289   {
1290     type_t *t = field->type;
1291
1292     /* get the base type */
1293     while( (t->type == 0) && t->ref )
1294       t = t->ref;
1295
1296     if (field->ptr_level > 0)
1297     {
1298         has_pointer = 1;
1299         continue;
1300     }
1301
1302     if (is_string_type(field->attrs, 0, field->array))
1303     {
1304         has_conformance = 1;
1305         has_variance = 1;
1306         continue;
1307     }
1308
1309     if (is_array_type(field->attrs, 0, field->array))
1310     {
1311         if (field->array && !field->array->is_const)
1312         {
1313             has_conformance = 1;
1314             if (PREV_LINK(field))
1315                 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1316                         field->name);
1317         }
1318         if (is_attr(field->attrs, ATTR_LENGTHIS))
1319             has_variance = 1;
1320     }
1321
1322     switch (t->type)
1323     {
1324     /*
1325      * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1326      *  Simple types don't effect the type of struct.
1327      *  A struct containing a simple struct is still a simple struct.
1328      *  So long as we can block copy the data, we return RPC_FC_STRUCT.
1329      */
1330     case 0: /* void pointer */
1331     case RPC_FC_BYTE:
1332     case RPC_FC_CHAR:
1333     case RPC_FC_SMALL:
1334     case RPC_FC_USMALL:
1335     case RPC_FC_WCHAR:
1336     case RPC_FC_SHORT:
1337     case RPC_FC_USHORT:
1338     case RPC_FC_LONG:
1339     case RPC_FC_ULONG:
1340     case RPC_FC_INT3264:
1341     case RPC_FC_UINT3264:
1342     case RPC_FC_HYPER:
1343     case RPC_FC_FLOAT:
1344     case RPC_FC_DOUBLE:
1345     case RPC_FC_STRUCT:
1346     case RPC_FC_ENUM16:
1347     case RPC_FC_ENUM32:
1348       break;
1349
1350     case RPC_FC_UP:
1351     case RPC_FC_FP:
1352       has_pointer = 1;
1353       break;
1354     case RPC_FC_CARRAY:
1355       has_conformance = 1;
1356       if (PREV_LINK(field))
1357           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1358                   field->name);
1359       break;
1360     case RPC_FC_C_CSTRING:
1361     case RPC_FC_C_WSTRING:
1362       has_conformance = 1;
1363       has_variance = 1;
1364       break;
1365
1366     /*
1367      * Propagate member attributes
1368      *  a struct should be at least as complex as its member
1369      */
1370     case RPC_FC_CVSTRUCT:
1371       has_conformance = 1;
1372       has_variance = 1;
1373       has_pointer = 1;
1374       break;
1375
1376     case RPC_FC_CPSTRUCT:
1377       has_conformance = 1;
1378       if (PREV_LINK(field))
1379           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1380                   field->name);
1381       has_pointer = 1;
1382       break;
1383
1384     case RPC_FC_CSTRUCT:
1385       has_conformance = 1;
1386       if (PREV_LINK(field))
1387           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1388                   field->name);
1389       break;
1390
1391     case RPC_FC_PSTRUCT:
1392       has_pointer = 1;
1393       break;
1394
1395     default:
1396       fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1397               field->name, t->type);
1398       /* fallthru - treat it as complex */
1399
1400     /* as soon as we see one of these these members, it's bogus... */
1401     case RPC_FC_IP:
1402     case RPC_FC_ENCAPSULATED_UNION:
1403     case RPC_FC_NON_ENCAPSULATED_UNION:
1404     case RPC_FC_TRANSMIT_AS:
1405     case RPC_FC_REPRESENT_AS:
1406     case RPC_FC_PAD:
1407     case RPC_FC_EMBEDDED_COMPLEX:
1408     case RPC_FC_BOGUS_STRUCT:
1409       return RPC_FC_BOGUS_STRUCT;
1410     }
1411   }
1412
1413   if( has_variance )
1414     return RPC_FC_CVSTRUCT;
1415   if( has_conformance && has_pointer )
1416     return RPC_FC_CPSTRUCT;
1417   if( has_conformance )
1418     return RPC_FC_CSTRUCT;
1419   if( has_pointer )
1420     return RPC_FC_PSTRUCT;
1421   return RPC_FC_STRUCT;
1422 }
1423
1424 /***** constant repository *****/
1425
1426 struct rconst {
1427   char *name;
1428   var_t *var;
1429   struct rconst *next;
1430 };
1431
1432 struct rconst *const_hash[HASHMAX];
1433
1434 static var_t *reg_const(var_t *var)
1435 {
1436   struct rconst *nc;
1437   int hash;
1438   if (!var->name) {
1439     yyerror("registering constant without name");
1440     return var;
1441   }
1442   hash = hash_ident(var->name);
1443   nc = xmalloc(sizeof(struct rconst));
1444   nc->name = var->name;
1445   nc->var = var;
1446   nc->next = const_hash[hash];
1447   const_hash[hash] = nc;
1448   return var;
1449 }
1450
1451 static var_t *find_const(char *name, int f)
1452 {
1453   struct rconst *cur = const_hash[hash_ident(name)];
1454   while (cur && strcmp(cur->name, name))
1455     cur = cur->next;
1456   if (!cur) {
1457     if (f) yyerror("constant '%s' not found", name);
1458     return NULL;
1459   }
1460   return cur->var;
1461 }