ole32: More tests and fixes for STGM_PRIORITY.
[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                                                 }
440         ;
441
442 enums:                                          { $$ = NULL; }
443         | enum_list ','                         { $$ = $1; }
444         | enum_list
445         ;
446
447 enum_list: enum                                 { if (!$$->eval)
448                                                     $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
449                                                 }
450         | enum_list ',' enum                    { LINK($3, $1); $$ = $3;
451                                                   if (!$$->eval)
452                                                     $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
453                                                 }
454         ;
455
456 enum:     ident '=' expr_const                  { $$ = reg_const($1);
457                                                   $$->eval = $3;
458                                                   $$->type = make_type(RPC_FC_LONG, &std_int);
459                                                 }
460         | ident                                 { $$ = reg_const($1);
461                                                   $$->type = make_type(RPC_FC_LONG, &std_int);
462                                                 }
463         ;
464
465 enumdef: tENUM t_ident '{' enums '}'            { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
466                                                   $$->fields = $4;
467                                                   $$->defined = TRUE;
468                                                   if(in_typelib)
469                                                       add_enum($$);
470                                                 }
471         ;
472
473 m_exprs:  m_expr
474         | m_exprs ',' m_expr                    { LINK($3, $1); $$ = $3; }
475         ;
476
477 /*
478 exprs:                                          { $$ = make_expr(EXPR_VOID); }
479         | expr_list
480         ;
481
482 expr_list: expr
483         | expr_list ',' expr                    { LINK($3, $1); $$ = $3; }
484         ;
485 */
486
487 m_expr:                                         { $$ = make_expr(EXPR_VOID); }
488         | expr
489         ;
490
491 expr:     aNUM                                  { $$ = make_exprl(EXPR_NUM, $1); }
492         | aHEXNUM                               { $$ = make_exprl(EXPR_HEXNUM, $1); }
493         | aIDENTIFIER                           { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
494         | expr '?' expr ':' expr                { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
495         | expr '|' expr                         { $$ = make_expr2(EXPR_OR , $1, $3); }
496         | expr '&' expr                         { $$ = make_expr2(EXPR_AND, $1, $3); }
497         | expr '+' expr                         { $$ = make_expr2(EXPR_ADD, $1, $3); }
498         | expr '-' expr                         { $$ = make_expr2(EXPR_SUB, $1, $3); }
499         | expr '*' expr                         { $$ = make_expr2(EXPR_MUL, $1, $3); }
500         | expr '/' expr                         { $$ = make_expr2(EXPR_DIV, $1, $3); }
501         | expr SHL expr                         { $$ = make_expr2(EXPR_SHL, $1, $3); }
502         | expr SHR expr                         { $$ = make_expr2(EXPR_SHR, $1, $3); }
503         | '~' expr                              { $$ = make_expr1(EXPR_NOT, $2); }
504         | '-' expr %prec NEG                    { $$ = make_expr1(EXPR_NEG, $2); }
505         | '*' expr %prec PPTR                   { $$ = make_expr1(EXPR_PPTR, $2); }
506         | '(' type ')' expr %prec CAST          { $$ = make_exprt(EXPR_CAST, $2, $4); }
507         | tSIZEOF '(' type ')'                  { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
508         | '(' expr ')'                          { $$ = $2; }
509         ;
510
511 expr_list_const: expr_const
512         | expr_list_const ',' expr_const        { LINK($3, $1); $$ = $3; }
513         ;
514
515 expr_const: expr                                { $$ = $1;
516                                                   if (!$$->is_const)
517                                                       yyerror("expression is not constant");
518                                                 }
519         ;
520
521 externdef: tEXTERN tCONST type ident            { $$ = $4;
522                                                   set_type($$, $3, NULL);
523                                                 }
524         ;
525
526 fields:                                         { $$ = NULL; }
527         | fields field                          { if ($2) { LINK($2, $1); $$ = $2; }
528                                                   else { $$ = $1; }
529                                                 }
530         ;
531
532 field:    s_field ';'                           { $$ = $1; }
533         | m_attributes uniondef ';'             { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
534         | attributes ';'                        { $$ = make_var(NULL); $$->attrs = $1; }
535         | ';'                                   { $$ = NULL; }
536         ;
537
538 s_field:  m_attributes type pident array        { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
539         ;
540
541 funcdef:
542           m_attributes type callconv pident
543           '(' m_args ')'                        { set_type($4, $2, NULL);
544                                                   $4->attrs = $1;
545                                                   $$ = make_func($4, $6);
546                                                   if (is_attr($4->attrs, ATTR_IN)) {
547                                                     yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
548                                                   }
549                                                 }
550         ;
551
552 m_ident:                                        { $$ = NULL; }
553         | ident
554         ;
555
556 t_ident:                                        { $$ = NULL; }
557         | aIDENTIFIER                           { $$ = make_var($1); }
558         | aKNOWNTYPE                            { $$ = make_var($1); }
559         ;
560
561 ident:    aIDENTIFIER                           { $$ = make_var($1); }
562 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
563         | aKNOWNTYPE                            { $$ = make_var($<str>1); }
564         | tASYNC                                { $$ = make_var($<str>1); }
565         | tID                                   { $$ = make_var($<str>1); }
566         | tLCID                                 { $$ = make_var($<str>1); }
567         | tRANGE                                { $$ = make_var($<str>1); }
568         | tRETVAL                               { $$ = make_var($<str>1); }
569         | tVERSION                              { $$ = make_var($<str>1); }
570         ;
571
572 base_type: tBYTE                                { $$ = make_type(RPC_FC_BYTE, NULL); }
573         | tWCHAR                                { $$ = make_type(RPC_FC_WCHAR, NULL); }
574         | int_std
575         | tSIGNED int_std                       { $$ = $2; $$->sign = 1; }
576         | tUNSIGNED int_std                     { $$ = $2; $$->sign = -1;
577                                                   switch ($$->type) {
578                                                   case RPC_FC_CHAR:  break;
579                                                   case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
580                                                   case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
581                                                   case RPC_FC_LONG:  $$->type = RPC_FC_ULONG;  break;
582                                                   case RPC_FC_HYPER:
583                                                     if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
584                                                     break;
585                                                   default: break;
586                                                   }
587                                                 }
588         | tUNSIGNED                             { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
589         | tFLOAT                                { $$ = make_type(RPC_FC_FLOAT, NULL); }
590         | tSINGLE                               { $$ = make_type(RPC_FC_FLOAT, NULL); }
591         | tDOUBLE                               { $$ = make_type(RPC_FC_DOUBLE, NULL); }
592         | tBOOLEAN                              { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
593         | tERRORSTATUST                         { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
594         | tHANDLET                              { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
595         ;
596
597 m_int:
598         | tINT
599         ;
600
601 int_std:  tINT                                  { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
602         | tSHORT m_int                          { $$ = make_type(RPC_FC_SHORT, NULL); }
603         | tSMALL                                { $$ = make_type(RPC_FC_SMALL, NULL); }
604         | tLONG m_int                           { $$ = make_type(RPC_FC_LONG, NULL); }
605         | tHYPER m_int                          { $$ = make_type(RPC_FC_HYPER, NULL); }
606         | tINT64                                { $$ = make_type(RPC_FC_HYPER, &std_int64); }
607         | tCHAR                                 { $$ = make_type(RPC_FC_CHAR, NULL); }
608         ;
609
610 coclass:  tCOCLASS aIDENTIFIER                  { $$ = make_class($2); }
611         | tCOCLASS aKNOWNTYPE                   { $$ = make_class($2); }
612         ;
613
614 coclasshdr: attributes coclass                  { $$ = $2;
615                                                   $$->attrs = $1;
616                                                   if (!parse_only && do_header)
617                                                     write_coclass($$);
618                                                 }
619         ;
620
621 coclassdef: coclasshdr '{' coclass_ints '}'     { $$ = $1;
622                                                   $$->ifaces = $3;
623                                                 }
624         ;
625
626 coclass_ints:                                   { $$ = NULL; }
627         | coclass_ints coclass_int              { LINK($2, $1); $$ = $2; }
628         ;
629
630 coclass_int:
631           m_attributes interfacedec             { $$ = make_ifref($2); $$->attrs = $1; }
632         ;
633
634 dispinterface: tDISPINTERFACE aIDENTIFIER       { $$ = get_type(0, $2, 0); }
635         |      tDISPINTERFACE aKNOWNTYPE        { $$ = get_type(0, $2, 0); }
636         ;
637
638 dispinterfacehdr: attributes dispinterface      { attr_t *attrs;
639                                                   $$ = $2;
640                                                   if ($$->defined) yyerror("multiple definition error");
641                                                   attrs = make_attr(ATTR_DISPINTERFACE);
642                                                   LINK(attrs, $1);
643                                                   $$->attrs = attrs;
644                                                   $$->ref = find_type("IDispatch", 0);
645                                                   if (!$$->ref) yyerror("IDispatch is undefined");
646                                                   $$->defined = TRUE;
647                                                   if (!parse_only && do_header) write_forward($$);
648                                                 }
649         ;
650
651 dispint_props: tPROPERTIES ':'                  { $$ = NULL; }
652         | dispint_props s_field ';'             { LINK($2, $1); $$ = $2; }
653         ;
654
655 dispint_meths: tMETHODS ':'                     { $$ = NULL; }
656         | dispint_meths funcdef ';'             { LINK($2, $1); $$ = $2; }
657         ;
658
659 dispinterfacedef: dispinterfacehdr '{'
660           dispint_props
661           dispint_meths
662           '}'                                   { $$ = $1;
663                                                   $$->fields = $3;
664                                                   $$->funcs = $4;
665                                                   if (!parse_only && do_header) write_dispinterface($$);
666                                                 }
667 /* FIXME: not sure how to handle this yet
668         | dispinterfacehdr '{' interface '}'    { $$ = $1;
669                                                   if (!parse_only && do_header) write_interface($$);
670                                                 }
671 */
672         ;
673
674 inherit:                                        { $$ = NULL; }
675         | ':' aKNOWNTYPE                        { $$ = find_type2($2, 0); }
676         ;
677
678 interface: tINTERFACE aIDENTIFIER               { $$ = get_type(RPC_FC_IP, $2, 0); }
679         |  tINTERFACE aKNOWNTYPE                { $$ = get_type(RPC_FC_IP, $2, 0); }
680         ;
681
682 interfacehdr: attributes interface              { $$ = $2;
683                                                   if ($$->defined) yyerror("multiple definition error");
684                                                   $$->attrs = $1;
685                                                   $$->defined = TRUE;
686                                                   if (!parse_only && do_header) write_forward($$);
687                                                 }
688         ;
689
690 interfacedef: interfacehdr inherit
691           '{' int_statements '}'                { $$ = $1;
692                                                   $$->ref = $2;
693                                                   $$->funcs = $4;
694                                                   if (!parse_only && do_header) write_interface($$);
695                                                 }
696 /* MIDL is able to import the definition of a base class from inside the
697  * definition of a derived class, I'll try to support it with this rule */
698         | interfacehdr ':' aIDENTIFIER
699           '{' import int_statements '}'         { $$ = $1;
700                                                   $$->ref = find_type2($3, 0);
701                                                   if (!$$->ref) yyerror("base class '%s' not found in import", $3);
702                                                   $$->funcs = $6;
703                                                   if (!parse_only && do_header) write_interface($$);
704                                                 }
705         | dispinterfacedef                      { $$ = $1; }
706         ;
707
708 interfacedec:
709           interface ';'                         { $$ = $1; if (!parse_only && do_header) write_forward($$); }
710         | dispinterface ';'                     { $$ = $1; if (!parse_only && do_header) write_forward($$); }
711         ;
712
713 module:   tMODULE aIDENTIFIER                   { $$ = make_type(0, NULL); $$->name = $2; }
714         | tMODULE aKNOWNTYPE                    { $$ = make_type(0, NULL); $$->name = $2; }
715         ;
716
717 modulehdr: attributes module                    { $$ = $2;
718                                                   $$->attrs = $1;
719                                                 }
720         ;
721
722 moduledef: modulehdr '{' int_statements '}'     { $$ = $1;
723                                                   $$->funcs = $3;
724                                                   /* FIXME: if (!parse_only && do_header) write_module($$); */
725                                                 }
726         ;
727
728 p_ident:  '*' pident %prec PPTR                 { $$ = $2; $$->ptr_level++; }
729         | tCONST p_ident                        { $$ = $2; /* FIXME */ }
730         ;
731
732 pident:   ident
733         | p_ident
734         | '(' pident ')'                        { $$ = $2; }
735         ;
736
737 pident_list:
738           pident
739         | pident_list ',' pident                { LINK($3, $1); $$ = $3; }
740         ;
741
742 pointer_type:
743           tREF                                  { $$ = RPC_FC_RP; }
744         | tUNIQUE                               { $$ = RPC_FC_UP; }
745         | tPTR                                  { $$ = RPC_FC_FP; }
746         ;
747
748 structdef: tSTRUCT t_ident '{' fields '}'       { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
749                                                   /* overwrite RPC_FC_STRUCT with a more exact type */
750                                                   $$->type = get_struct_type( $4 );
751                                                   $$->fields = $4;
752                                                   $$->defined = TRUE;
753                                                   if(in_typelib)
754                                                       add_struct($$);
755                                                 }
756         ;
757
758 type:     tVOID                                 { $$ = make_tref(NULL, make_type(0, NULL)); }
759         | aKNOWNTYPE                            { $$ = make_tref($1, find_type($1, 0)); }
760         | base_type                             { $$ = make_tref(NULL, $1); }
761         | tCONST type                           { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
762         | enumdef                               { $$ = make_tref(NULL, $1); }
763         | tENUM aIDENTIFIER                     { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
764         | structdef                             { $$ = make_tref(NULL, $1); }
765         | tSTRUCT aIDENTIFIER                   { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
766         | uniondef                              { $$ = make_tref(NULL, $1); }
767         | tUNION aIDENTIFIER                    { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
768         ;
769
770 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
771                                                   $4->tname = tref->name;
772                                                   tref->name = NULL;
773                                                   $$ = type_ref(tref);
774                                                   $$->attrs = $2;
775                                                   if (!parse_only && do_header)
776                                                     write_typedef($$, $4);
777                                                   if (in_typelib && $$->attrs)
778                                                     add_typedef($$, $4);
779                                                   reg_types($$, $4, 0);
780                                                 }
781         ;
782
783 uniondef: tUNION t_ident '{' fields '}'         { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
784                                                   $$->fields = $4;
785                                                   $$->defined = TRUE;
786                                                 }
787         | tUNION t_ident
788           tSWITCH '(' s_field ')'
789           m_ident '{' cases '}'                 { var_t *u = $7;
790                                                   $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
791                                                   if (!u) u = make_var("tagged_union");
792                                                   u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
793                                                   u->type->fields = $9;
794                                                   u->type->defined = TRUE;
795                                                   LINK(u, $5); $$->fields = u;
796                                                   $$->defined = TRUE;
797                                                 }
798         ;
799
800 version:
801           aNUM                                  { $$ = MAKELONG($1, 0); }
802         | aNUM '.' aNUM                         { $$ = MAKELONG($1, $3); }
803         ;
804
805 %%
806
807 static attr_t *make_attr(enum attr_type type)
808 {
809   attr_t *a = xmalloc(sizeof(attr_t));
810   a->type = type;
811   a->u.ival = 0;
812   INIT_LINK(a);
813   return a;
814 }
815
816 static attr_t *make_attrv(enum attr_type type, unsigned long val)
817 {
818   attr_t *a = xmalloc(sizeof(attr_t));
819   a->type = type;
820   a->u.ival = val;
821   INIT_LINK(a);
822   return a;
823 }
824
825 static attr_t *make_attrp(enum attr_type type, void *val)
826 {
827   attr_t *a = xmalloc(sizeof(attr_t));
828   a->type = type;
829   a->u.pval = val;
830   INIT_LINK(a);
831   return a;
832 }
833
834 static expr_t *make_expr(enum expr_type type)
835 {
836   expr_t *e = xmalloc(sizeof(expr_t));
837   e->type = type;
838   e->ref = NULL;
839   e->u.lval = 0;
840   e->is_const = FALSE;
841   INIT_LINK(e);
842   return e;
843 }
844
845 static expr_t *make_exprl(enum expr_type type, long val)
846 {
847   expr_t *e = xmalloc(sizeof(expr_t));
848   e->type = type;
849   e->ref = NULL;
850   e->u.lval = val;
851   e->is_const = FALSE;
852   INIT_LINK(e);
853   /* check for numeric constant */
854   if (type == EXPR_NUM || type == EXPR_HEXNUM) {
855     e->is_const = TRUE;
856     e->cval = val;
857   }
858   return e;
859 }
860
861 static expr_t *make_exprs(enum expr_type type, char *val)
862 {
863   expr_t *e;
864   e = xmalloc(sizeof(expr_t));
865   e->type = type;
866   e->ref = NULL;
867   e->u.sval = val;
868   e->is_const = FALSE;
869   INIT_LINK(e);
870   /* check for predefined constants */
871   if (type == EXPR_IDENTIFIER) {
872     var_t *c = find_const(val, 0);
873     if (c) {
874       e->u.sval = c->name;
875       free(val);
876       e->is_const = TRUE;
877       e->cval = c->eval->cval;
878     }
879   }
880   return e;
881 }
882
883 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
884 {
885   expr_t *e;
886   e = xmalloc(sizeof(expr_t));
887   e->type = type;
888   e->ref = expr;
889   e->u.tref = tref;
890   e->is_const = FALSE;
891   INIT_LINK(e);
892   /* check for cast of constant expression */
893   if (type == EXPR_SIZEOF) {
894     switch (tref->ref->type) {
895       case RPC_FC_BYTE:
896       case RPC_FC_CHAR:
897       case RPC_FC_SMALL:
898       case RPC_FC_USMALL:
899         e->is_const = TRUE;
900         e->cval = 1;
901         break;
902       case RPC_FC_WCHAR:
903       case RPC_FC_USHORT:
904       case RPC_FC_SHORT:
905         e->is_const = TRUE;
906         e->cval = 2;
907         break;
908       case RPC_FC_LONG:
909       case RPC_FC_ULONG:
910       case RPC_FC_FLOAT:
911       case RPC_FC_ERROR_STATUS_T:
912         e->is_const = TRUE;
913         e->cval = 4;
914         break;
915       case RPC_FC_HYPER:
916       case RPC_FC_DOUBLE:
917         e->is_const = TRUE;
918         e->cval = 8;
919         break;
920     }
921   }
922   if (type == EXPR_CAST && expr->is_const) {
923     e->is_const = TRUE;
924     e->cval = expr->cval;
925   }
926   return e;
927 }
928
929 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
930 {
931   expr_t *e;
932   e = xmalloc(sizeof(expr_t));
933   e->type = type;
934   e->ref = expr;
935   e->u.lval = 0;
936   e->is_const = FALSE;
937   INIT_LINK(e);
938   /* check for compile-time optimization */
939   if (expr->is_const) {
940     e->is_const = TRUE;
941     switch (type) {
942     case EXPR_NEG:
943       e->cval = -expr->cval;
944       break;
945     case EXPR_NOT:
946       e->cval = ~expr->cval;
947       break;
948     default:
949       e->is_const = FALSE;
950       break;
951     }
952   }
953   return e;
954 }
955
956 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
957 {
958   expr_t *e;
959   e = xmalloc(sizeof(expr_t));
960   e->type = type;
961   e->ref = expr1;
962   e->u.ext = expr2;
963   e->is_const = FALSE;
964   INIT_LINK(e);
965   /* check for compile-time optimization */
966   if (expr1->is_const && expr2->is_const) {
967     e->is_const = TRUE;
968     switch (type) {
969     case EXPR_ADD:
970       e->cval = expr1->cval + expr2->cval;
971       break;
972     case EXPR_SUB:
973       e->cval = expr1->cval - expr2->cval;
974       break;
975     case EXPR_MUL:
976       e->cval = expr1->cval * expr2->cval;
977       break;
978     case EXPR_DIV:
979       e->cval = expr1->cval / expr2->cval;
980       break;
981     case EXPR_OR:
982       e->cval = expr1->cval | expr2->cval;
983       break;
984     case EXPR_AND:
985       e->cval = expr1->cval & expr2->cval;
986       break;
987     case EXPR_SHL:
988       e->cval = expr1->cval << expr2->cval;
989       break;
990     case EXPR_SHR:
991       e->cval = expr1->cval >> expr2->cval;
992       break;
993     default:
994       e->is_const = FALSE;
995       break;
996     }
997   }
998   return e;
999 }
1000
1001 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1002 {
1003   expr_t *e;
1004   e = xmalloc(sizeof(expr_t));
1005   e->type = type;
1006   e->ref = expr1;
1007   e->u.ext = expr2;
1008   e->ext2 = expr3;
1009   e->is_const = FALSE;
1010   INIT_LINK(e);
1011   /* check for compile-time optimization */
1012   if (expr1->is_const && expr2->is_const && expr3->is_const) {
1013     e->is_const = TRUE;
1014     switch (type) {
1015     case EXPR_COND:
1016       e->cval = expr1->cval ? expr2->cval : expr3->cval;
1017       break;
1018     default:
1019       e->is_const = FALSE;
1020       break;
1021     }
1022   }
1023   return e;
1024 }
1025
1026 static type_t *make_type(unsigned char type, type_t *ref)
1027 {
1028   type_t *t = xmalloc(sizeof(type_t));
1029   t->name = NULL;
1030   t->type = type;
1031   t->ref = ref;
1032   t->attrs = NULL;
1033   t->funcs = NULL;
1034   t->fields = NULL;
1035   t->ignore = parse_only;
1036   t->is_const = FALSE;
1037   t->sign = 0;
1038   t->defined = FALSE;
1039   t->written = FALSE;
1040   t->typelib_idx = -1;
1041   INIT_LINK(t);
1042   return t;
1043 }
1044
1045 static typeref_t *make_tref(char *name, type_t *ref)
1046 {
1047   typeref_t *t = xmalloc(sizeof(typeref_t));
1048   t->name = name;
1049   t->ref = ref;
1050   t->uniq = ref ? 0 : 1;
1051   return t;
1052 }
1053
1054 static typeref_t *uniq_tref(typeref_t *ref)
1055 {
1056   typeref_t *t = ref;
1057   type_t *tp;
1058   if (t->uniq) return t;
1059   tp = make_type(0, t->ref);
1060   tp->name = t->name;
1061   t->name = NULL;
1062   t->ref = tp;
1063   t->uniq = 1;
1064   return t;
1065 }
1066
1067 static type_t *type_ref(typeref_t *ref)
1068 {
1069   type_t *t = ref->ref;
1070   if (ref->name) free(ref->name);
1071   free(ref);
1072   return t;
1073 }
1074
1075 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1076 {
1077   v->type = ref->ref;
1078   v->tname = ref->name;
1079   ref->name = NULL;
1080   free(ref);
1081   v->array = arr;
1082 }
1083
1084 static ifref_t *make_ifref(type_t *iface)
1085 {
1086   ifref_t *l = xmalloc(sizeof(ifref_t));
1087   l->iface = iface;
1088   l->attrs = NULL;
1089   INIT_LINK(l);
1090   return l;
1091 }
1092
1093 static var_t *make_var(char *name)
1094 {
1095   var_t *v = xmalloc(sizeof(var_t));
1096   v->name = name;
1097   v->ptr_level = 0;
1098   v->type = NULL;
1099   v->tname = NULL;
1100   v->attrs = NULL;
1101   v->array = NULL;
1102   v->eval = NULL;
1103   INIT_LINK(v);
1104   return v;
1105 }
1106
1107 static func_t *make_func(var_t *def, var_t *args)
1108 {
1109   func_t *f = xmalloc(sizeof(func_t));
1110   f->def = def;
1111   f->args = args;
1112   f->ignore = parse_only;
1113   f->idx = -1;
1114   INIT_LINK(f);
1115   return f;
1116 }
1117
1118 static class_t *make_class(char *name)
1119 {
1120   class_t *c = xmalloc(sizeof(class_t));
1121   c->name = name;
1122   c->attrs = NULL;
1123   c->ifaces = NULL;
1124   INIT_LINK(c);
1125   return c;
1126 }
1127
1128 #define HASHMAX 64
1129
1130 static int hash_ident(const char *name)
1131 {
1132   const char *p = name;
1133   int sum = 0;
1134   /* a simple sum hash is probably good enough */
1135   while (*p) {
1136     sum += *p;
1137     p++;
1138   }
1139   return sum & (HASHMAX-1);
1140 }
1141
1142 /***** type repository *****/
1143
1144 struct rtype {
1145   const char *name;
1146   type_t *type;
1147   int t;
1148   struct rtype *next;
1149 };
1150
1151 struct rtype *type_hash[HASHMAX];
1152
1153 static type_t *reg_type(type_t *type, const char *name, int t)
1154 {
1155   struct rtype *nt;
1156   int hash;
1157   if (!name) {
1158     yyerror("registering named type without name");
1159     return type;
1160   }
1161   hash = hash_ident(name);
1162   nt = xmalloc(sizeof(struct rtype));
1163   nt->name = name;
1164   nt->type = type;
1165   nt->t = t;
1166   nt->next = type_hash[hash];
1167   type_hash[hash] = nt;
1168   return type;
1169 }
1170
1171 /* determine pointer type from attrs */
1172 static unsigned char get_pointer_type( type_t *type )
1173 {
1174   int t;
1175   if (is_attr( type->attrs, ATTR_STRING ))
1176   {
1177     type_t *t = type;
1178     while( t->type == 0 && t->ref )
1179       t = t->ref;
1180     switch( t->type )
1181     {
1182     case RPC_FC_CHAR:
1183       return RPC_FC_C_CSTRING;
1184     case RPC_FC_WCHAR:
1185       return RPC_FC_C_WSTRING;
1186     }
1187   }
1188   t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1189   if (t) return t;
1190   return RPC_FC_FP;
1191 }
1192
1193 static type_t *reg_types(type_t *type, var_t *names, int t)
1194 {
1195   type_t *ptr = type;
1196   int ptrc = 0;
1197
1198   while (names) {
1199     var_t *next = NEXT_LINK(names);
1200     if (names->name) {
1201       type_t *cur = ptr;
1202       int cptr = names->ptr_level;
1203       if (cptr > ptrc) {
1204         while (cptr > ptrc) {
1205           int t = get_pointer_type( cur );
1206           cur = ptr = make_type(t, cur);
1207           ptrc++;
1208         }
1209       } else {
1210         while (cptr < ptrc) {
1211           cur = cur->ref;
1212           cptr++;
1213         }
1214       }
1215       reg_type(cur, names->name, t);
1216     }
1217     free(names);
1218     names = next;
1219   }
1220   return type;
1221 }
1222
1223 static type_t *find_type(const char *name, int t)
1224 {
1225   struct rtype *cur = type_hash[hash_ident(name)];
1226   while (cur && (cur->t != t || strcmp(cur->name, name)))
1227     cur = cur->next;
1228   if (!cur) {
1229     yyerror("type '%s' not found", name);
1230     return NULL;
1231   }
1232   return cur->type;
1233 }
1234
1235 static type_t *find_type2(char *name, int t)
1236 {
1237   type_t *tp = find_type(name, t);
1238   free(name);
1239   return tp;
1240 }
1241
1242 int is_type(const char *name)
1243 {
1244   struct rtype *cur = type_hash[hash_ident(name)];
1245   while (cur && (cur->t || strcmp(cur->name, name)))
1246     cur = cur->next;
1247   if (cur) return TRUE;
1248   return FALSE;
1249 }
1250
1251 static type_t *get_type(unsigned char type, char *name, int t)
1252 {
1253   struct rtype *cur = NULL;
1254   type_t *tp;
1255   if (name) {
1256     cur = type_hash[hash_ident(name)];
1257     while (cur && (cur->t != t || strcmp(cur->name, name)))
1258       cur = cur->next;
1259   }
1260   if (cur) {
1261     free(name);
1262     return cur->type;
1263   }
1264   tp = make_type(type, NULL);
1265   tp->name = name;
1266   if (!name) return tp;
1267   return reg_type(tp, name, t);
1268 }
1269
1270 static type_t *get_typev(unsigned char type, var_t *name, int t)
1271 {
1272   char *sname = NULL;
1273   if (name) {
1274     sname = name->name;
1275     free(name);
1276   }
1277   return get_type(type, sname, t);
1278 }
1279
1280 static int get_struct_type(var_t *field)
1281 {
1282   int has_pointer = 0;
1283   int has_conformance = 0;
1284   int has_variance = 0;
1285
1286   for (; field; field = NEXT_LINK(field))
1287   {
1288     type_t *t = field->type;
1289
1290     /* get the base type */
1291     while( (t->type == 0) && t->ref )
1292       t = t->ref;
1293
1294     if (field->ptr_level > 0)
1295     {
1296         has_pointer = 1;
1297         continue;
1298     }
1299
1300     if (is_string_type(field->attrs, 0, field->array))
1301     {
1302         has_conformance = 1;
1303         has_variance = 1;
1304         continue;
1305     }
1306
1307     if (is_array_type(field->attrs, 0, field->array))
1308     {
1309         if (field->array && !field->array->is_const)
1310         {
1311             has_conformance = 1;
1312             if (PREV_LINK(field))
1313                 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1314                         field->name);
1315         }
1316         if (is_attr(field->attrs, ATTR_LENGTHIS))
1317             has_variance = 1;
1318     }
1319
1320     switch (t->type)
1321     {
1322     /*
1323      * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1324      *  Simple types don't effect the type of struct.
1325      *  A struct containing a simple struct is still a simple struct.
1326      *  So long as we can block copy the data, we return RPC_FC_STRUCT.
1327      */
1328     case 0: /* void pointer */
1329     case RPC_FC_BYTE:
1330     case RPC_FC_CHAR:
1331     case RPC_FC_SMALL:
1332     case RPC_FC_USMALL:
1333     case RPC_FC_WCHAR:
1334     case RPC_FC_SHORT:
1335     case RPC_FC_USHORT:
1336     case RPC_FC_LONG:
1337     case RPC_FC_ULONG:
1338     case RPC_FC_INT3264:
1339     case RPC_FC_UINT3264:
1340     case RPC_FC_HYPER:
1341     case RPC_FC_FLOAT:
1342     case RPC_FC_DOUBLE:
1343     case RPC_FC_STRUCT:
1344     case RPC_FC_ENUM16:
1345     case RPC_FC_ENUM32:
1346       break;
1347
1348     case RPC_FC_UP:
1349     case RPC_FC_FP:
1350       has_pointer = 1;
1351       break;
1352     case RPC_FC_CARRAY:
1353       has_conformance = 1;
1354       if (PREV_LINK(field))
1355           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1356                   field->name);
1357       break;
1358     case RPC_FC_C_CSTRING:
1359     case RPC_FC_C_WSTRING:
1360       has_conformance = 1;
1361       has_variance = 1;
1362       break;
1363
1364     /*
1365      * Propagate member attributes
1366      *  a struct should be at least as complex as its member
1367      */
1368     case RPC_FC_CVSTRUCT:
1369       has_conformance = 1;
1370       has_variance = 1;
1371       has_pointer = 1;
1372       break;
1373
1374     case RPC_FC_CPSTRUCT:
1375       has_conformance = 1;
1376       if (PREV_LINK(field))
1377           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1378                   field->name);
1379       has_pointer = 1;
1380       break;
1381
1382     case RPC_FC_CSTRUCT:
1383       has_conformance = 1;
1384       if (PREV_LINK(field))
1385           yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1386                   field->name);
1387       break;
1388
1389     case RPC_FC_PSTRUCT:
1390       has_pointer = 1;
1391       break;
1392
1393     default:
1394       fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1395               field->name, t->type);
1396       /* fallthru - treat it as complex */
1397
1398     /* as soon as we see one of these these members, it's bogus... */
1399     case RPC_FC_IP:
1400     case RPC_FC_ENCAPSULATED_UNION:
1401     case RPC_FC_NON_ENCAPSULATED_UNION:
1402     case RPC_FC_TRANSMIT_AS:
1403     case RPC_FC_REPRESENT_AS:
1404     case RPC_FC_PAD:
1405     case RPC_FC_EMBEDDED_COMPLEX:
1406     case RPC_FC_BOGUS_STRUCT:
1407       return RPC_FC_BOGUS_STRUCT;
1408     }
1409   }
1410
1411   if( has_variance )
1412     return RPC_FC_CVSTRUCT;
1413   if( has_conformance && has_pointer )
1414     return RPC_FC_CPSTRUCT;
1415   if( has_conformance )
1416     return RPC_FC_CSTRUCT;
1417   if( has_pointer )
1418     return RPC_FC_PSTRUCT;
1419   return RPC_FC_STRUCT;
1420 }
1421
1422 /***** constant repository *****/
1423
1424 struct rconst {
1425   char *name;
1426   var_t *var;
1427   struct rconst *next;
1428 };
1429
1430 struct rconst *const_hash[HASHMAX];
1431
1432 static var_t *reg_const(var_t *var)
1433 {
1434   struct rconst *nc;
1435   int hash;
1436   if (!var->name) {
1437     yyerror("registering constant without name");
1438     return var;
1439   }
1440   hash = hash_ident(var->name);
1441   nc = xmalloc(sizeof(struct rconst));
1442   nc->name = var->name;
1443   nc->var = var;
1444   nc->next = const_hash[hash];
1445   const_hash[hash] = nc;
1446   return var;
1447 }
1448
1449 static var_t *find_const(char *name, int f)
1450 {
1451   struct rconst *cur = const_hash[hash_ident(name)];
1452   while (cur && strcmp(cur->name, name))
1453     cur = cur->next;
1454   if (!cur) {
1455     if (f) yyerror("constant '%s' not found", name);
1456     return NULL;
1457   }
1458   return cur->var;
1459 }