widl: Register types for coclasses.
[wine.git] / tools / widl / parser.y
bloba4efb66dc0f942a3950eadde2c9e06258c88b25b
1 %{
2 /*
3 * IDL Compiler
5 * Copyright 2002 Ove Kaaven
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 #include "config.h"
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <stdarg.h>
27 #include <assert.h>
28 #include <ctype.h>
29 #include <string.h>
30 #ifdef HAVE_ALLOCA_H
31 #include <alloca.h>
32 #endif
34 #include "windef.h"
36 #include "widl.h"
37 #include "utils.h"
38 #include "parser.h"
39 #include "header.h"
40 #include "typelib.h"
42 #if defined(YYBYACC)
43 /* Berkeley yacc (byacc) doesn't seem to know about these */
44 /* Some *BSD supplied versions do define these though */
45 # ifndef YYEMPTY
46 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
47 # endif
48 # ifndef YYLEX
49 # define YYLEX yylex()
50 # endif
52 #elif defined(YYBISON)
53 /* Bison was used for original development */
54 /* #define YYEMPTY -2 */
55 /* #define YYLEX yylex() */
57 #else
58 /* No yacc we know yet */
59 # if !defined(YYEMPTY) || !defined(YYLEX)
60 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
61 # elif defined(__GNUC__) /* gcc defines the #warning directive */
62 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
63 /* #else we just take a chance that it works... */
64 # endif
65 #endif
67 static attr_t *make_attr(enum attr_type type);
68 static attr_t *make_attrv(enum attr_type type, unsigned long val);
69 static attr_t *make_attrp(enum attr_type type, void *val);
70 static expr_t *make_expr(enum expr_type type);
71 static expr_t *make_exprl(enum expr_type type, long val);
72 static expr_t *make_exprs(enum expr_type type, char *val);
73 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
74 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
75 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
76 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
77 static type_t *make_type(unsigned char type, type_t *ref);
78 static typeref_t *make_tref(char *name, type_t *ref);
79 static typeref_t *uniq_tref(typeref_t *ref);
80 static type_t *type_ref(typeref_t *ref);
81 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
82 static ifref_t *make_ifref(type_t *iface);
83 static var_t *make_var(char *name);
84 static func_t *make_func(var_t *def, var_t *args);
85 static type_t *make_class(char *name);
86 static type_t *make_safearray(void);
88 static type_t *reg_type(type_t *type, const char *name, int t);
89 static type_t *reg_types(type_t *type, var_t *names, int t);
90 static type_t *find_type(const char *name, int t);
91 static type_t *find_type2(char *name, int t);
92 static type_t *get_type(unsigned char type, char *name, int t);
93 static type_t *get_typev(unsigned char type, var_t *name, int t);
94 static int get_struct_type(var_t *fields);
96 static var_t *reg_const(var_t *var);
97 static var_t *find_const(char *name, int f);
99 #define tsENUM 1
100 #define tsSTRUCT 2
101 #define tsUNION 3
103 static type_t std_bool = { "boolean" };
104 static type_t std_int = { "int" };
105 static type_t std_int64 = { "__int64" };
106 static type_t std_uhyper = { "MIDL_uhyper" };
109 %union {
110 attr_t *attr;
111 expr_t *expr;
112 type_t *type;
113 typeref_t *tref;
114 var_t *var;
115 func_t *func;
116 ifref_t *ifref;
117 char *str;
118 UUID *uuid;
119 unsigned int num;
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 tFALSE
145 %token tFLOAT
146 %token tHANDLE
147 %token tHANDLET
148 %token tHELPCONTEXT tHELPFILE
149 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
150 %token tHIDDEN
151 %token tHYPER tID tIDEMPOTENT
152 %token tIIDIS
153 %token tIMMEDIATEBIND
154 %token tIMPLICITHANDLE
155 %token tIMPORT tIMPORTLIB
156 %token tIN tINLINE
157 %token tINPUTSYNC
158 %token tINT tINT64
159 %token tINTERFACE
160 %token tLCID
161 %token tLENGTHIS tLIBRARY
162 %token tLOCAL
163 %token tLONG
164 %token tMETHODS
165 %token tMODULE
166 %token tNONBROWSABLE
167 %token tNONCREATABLE
168 %token tNONEXTENSIBLE
169 %token tOBJECT tODL tOLEAUTOMATION
170 %token tOPTIONAL
171 %token tOUT
172 %token tPOINTERDEFAULT
173 %token tPROPERTIES
174 %token tPROPGET tPROPPUT tPROPPUTREF
175 %token tPTR
176 %token tPUBLIC
177 %token tRANGE
178 %token tREADONLY tREF
179 %token tREQUESTEDIT
180 %token tRESTRICTED
181 %token tRETVAL
182 %token tSAFEARRAY
183 %token tSHORT
184 %token tSIGNED
185 %token tSINGLE
186 %token tSIZEIS tSIZEOF
187 %token tSMALL
188 %token tSOURCE
189 %token tSTDCALL
190 %token tSTRING tSTRUCT
191 %token tSWITCH tSWITCHIS tSWITCHTYPE
192 %token tTRANSMITAS
193 %token tTRUE
194 %token tTYPEDEF
195 %token tUNION
196 %token tUNIQUE
197 %token tUNSIGNED
198 %token tUUID
199 %token tV1ENUM
200 %token tVARARG
201 %token tVERSION
202 %token tVOID
203 %token tWCHAR tWIREMARSHAL
205 %type <attr> m_attributes attributes attrib_list attribute
206 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
207 %type <expr> array array_list
208 %type <type> inherit interface interfacehdr interfacedef interfacedec
209 %type <type> dispinterface dispinterfacehdr dispinterfacedef
210 %type <type> module modulehdr moduledef
211 %type <type> base_type int_std
212 %type <type> enumdef structdef typedef uniondef
213 %type <ifref> gbl_statements coclass_ints coclass_int
214 %type <tref> type
215 %type <var> m_args no_args args arg
216 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
217 %type <var> m_ident t_ident ident p_ident pident pident_list
218 %type <var> dispint_props
219 %type <func> funcdef int_statements
220 %type <func> dispint_meths
221 %type <type> coclass coclasshdr coclassdef
222 %type <num> pointer_type version
223 %type <str> libraryhdr
225 %left ','
226 %right '?' ':'
227 %left '|'
228 %left '&'
229 %left '-' '+'
230 %left '*' '/'
231 %left SHL SHR
232 %right '~'
233 %right CAST
234 %right PPTR
235 %right NEG
239 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
242 gbl_statements: { $$ = NULL; }
243 | gbl_statements interfacedec { $$ = $1; }
244 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
245 | gbl_statements coclass ';' { $$ = $1;
246 reg_type($2, $2->name, 0);
247 if (!parse_only && do_header) write_coclass_forward($2);
249 | gbl_statements coclassdef { $$ = $1;
250 add_coclass($2);
251 reg_type($2, $2->name, 0);
253 | gbl_statements moduledef { $$ = $1; add_module($2); }
254 | gbl_statements librarydef { $$ = $1; }
255 | gbl_statements statement { $$ = $1; }
258 imp_statements: {}
259 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
260 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
261 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
262 | imp_statements coclassdef { if (!parse_only) add_coclass($2);
263 reg_type($2, $2->name, 0);
265 | imp_statements moduledef { if (!parse_only) add_module($2); }
266 | imp_statements statement {}
267 | imp_statements importlib {}
270 int_statements: { $$ = NULL; }
271 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
272 | int_statements statement { $$ = $1; }
275 statement: ';' {}
276 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
277 | cppquote {}
278 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
279 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
280 | import {}
281 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
282 | typedef ';' {}
283 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
286 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
288 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
289 if (!do_import($2)) yychar = aEOF; }
291 import: import_start imp_statements aEOF {}
294 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
297 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
299 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
300 if (!parse_only && do_header) write_library($2, $1); }
302 librarydef: library_start imp_statements '}' { end_typelib(); }
305 m_args: { $$ = NULL; }
306 | args
309 no_args: tVOID { $$ = NULL; }
312 args: arg
313 | args ',' arg { LINK($3, $1); $$ = $3; }
314 | no_args
317 /* split into two rules to get bison to resolve a tVOID conflict */
318 arg: attributes type pident array { $$ = $3;
319 set_type($$, $2, $4);
320 $$->attrs = $1;
322 | type pident array { $$ = $2;
323 set_type($$, $1, $3);
325 | attributes type pident '(' m_args ')' { $$ = $3;
326 $$->ptr_level--;
327 set_type($$, $2, NULL);
328 $$->attrs = $1;
329 $$->args = $5;
331 | type pident '(' m_args ')' { $$ = $2;
332 $$->ptr_level--;
333 set_type($$, $1, NULL);
334 $$->args = $4;
338 array: { $$ = NULL; }
339 | '[' array_list ']' { $$ = $2; }
340 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
343 array_list: m_expr /* size of first dimension is optional */
344 | array_list ',' expr { LINK($3, $1); $$ = $3; }
345 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
348 m_attributes: { $$ = NULL; }
349 | attributes
352 attributes:
353 '[' attrib_list ']' { $$ = $2;
354 if (!$$)
355 yyerror("empty attribute lists unsupported");
359 attrib_list: attribute
360 | attrib_list ',' attribute { if ($3) { LINK($3, $1); $$ = $3; }
361 else { $$ = $1; }
363 | attrib_list ']' '[' attribute { if ($4) { LINK($4, $1); $$ = $4; }
364 else { $$ = $1; }
368 attribute: { $$ = NULL; }
369 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
370 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
371 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
372 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
373 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
374 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
375 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
376 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
377 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
378 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
379 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
380 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
381 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
382 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
383 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
384 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
385 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
386 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
387 | tDUAL { $$ = make_attr(ATTR_DUAL); }
388 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
389 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
390 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
391 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
392 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
393 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
394 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
395 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
396 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
397 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
398 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
399 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
400 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
401 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
402 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
403 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
404 | tIN { $$ = make_attr(ATTR_IN); }
405 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
406 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
407 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
408 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
409 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
410 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
411 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
412 | tODL { $$ = make_attr(ATTR_ODL); }
413 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
414 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
415 | tOUT { $$ = make_attr(ATTR_OUT); }
416 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
417 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
418 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
419 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
420 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
421 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
422 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
423 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
424 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
425 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
426 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
427 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
428 | tSTRING { $$ = make_attr(ATTR_STRING); }
429 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
430 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
431 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
432 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
433 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
434 | tVARARG { $$ = make_attr(ATTR_VARARG); }
435 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
436 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
437 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
440 callconv:
441 | tSTDCALL
444 cases: { $$ = NULL; }
445 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
446 else { $$ = $1; }
450 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
451 $$ = $4; if (!$$) $$ = make_var(NULL);
452 LINK(a, $$->attrs); $$->attrs = a;
454 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
455 $$ = $3; if (!$$) $$ = make_var(NULL);
456 LINK(a, $$->attrs); $$->attrs = a;
460 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
461 set_type($$, $2, NULL);
462 $$->eval = $5;
466 enums: { $$ = NULL; }
467 | enum_list ',' { $$ = $1; }
468 | enum_list
471 enum_list: enum { if (!$$->eval)
472 $$->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
474 | enum_list ',' enum { LINK($3, $1); $$ = $3;
475 if (!$$->eval)
476 $$->eval = make_exprl(EXPR_NUM, $1->eval->cval + 1);
480 enum: ident '=' expr_const { $$ = reg_const($1);
481 $$->eval = $3;
482 $$->type = make_type(RPC_FC_LONG, &std_int);
484 | ident { $$ = reg_const($1);
485 $$->type = make_type(RPC_FC_LONG, &std_int);
489 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
490 $$->fields = $4;
491 $$->defined = TRUE;
492 if(in_typelib)
493 add_enum($$);
497 m_exprs: m_expr
498 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
502 exprs: { $$ = make_expr(EXPR_VOID); }
503 | expr_list
506 expr_list: expr
507 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
511 m_expr: { $$ = make_expr(EXPR_VOID); }
512 | expr
515 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
516 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
517 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
518 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
519 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
520 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
521 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
522 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
523 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
524 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
525 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
526 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
527 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
528 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
529 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
530 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
531 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
532 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
533 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
534 | '(' expr ')' { $$ = $2; }
537 expr_list_const: expr_const
538 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
541 expr_const: expr { $$ = $1;
542 if (!$$->is_const)
543 yyerror("expression is not constant");
547 externdef: tEXTERN tCONST type ident { $$ = $4;
548 set_type($$, $3, NULL);
552 fields: { $$ = NULL; }
553 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
554 else { $$ = $1; }
558 field: s_field ';' { $$ = $1; }
559 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
560 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
561 | ';' { $$ = NULL; }
564 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
567 funcdef:
568 m_attributes type callconv pident
569 '(' m_args ')' { set_type($4, $2, NULL);
570 $4->attrs = $1;
571 $$ = make_func($4, $6);
572 if (is_attr($4->attrs, ATTR_IN)) {
573 yyerror("inapplicable attribute [in] for function '%s'",$$->def->name);
578 m_ident: { $$ = NULL; }
579 | ident
582 t_ident: { $$ = NULL; }
583 | aIDENTIFIER { $$ = make_var($1); }
584 | aKNOWNTYPE { $$ = make_var($1); }
587 ident: aIDENTIFIER { $$ = make_var($1); }
588 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
589 | aKNOWNTYPE { $$ = make_var($<str>1); }
590 | tASYNC { $$ = make_var($<str>1); }
591 | tID { $$ = make_var($<str>1); }
592 | tLCID { $$ = make_var($<str>1); }
593 | tOBJECT { $$ = make_var($<str>1); }
594 | tRANGE { $$ = make_var($<str>1); }
595 | tRETVAL { $$ = make_var($<str>1); }
596 | tUUID { $$ = make_var($<str>1); }
597 | tVERSION { $$ = make_var($<str>1); }
600 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
601 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
602 | int_std
603 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
604 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
605 switch ($$->type) {
606 case RPC_FC_CHAR: break;
607 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
608 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
609 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
610 case RPC_FC_HYPER:
611 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
612 break;
613 default: break;
616 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
617 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
618 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
619 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
620 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
621 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
622 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
625 m_int:
626 | tINT
629 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
630 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
631 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
632 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
633 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
634 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
635 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
638 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
639 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
640 if ($$->defined) yyerror("multiple definition error");
644 coclasshdr: attributes coclass { $$ = $2;
645 $$->attrs = $1;
646 if (!parse_only && do_header)
647 write_coclass($$);
651 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
652 $$->ifaces = $3;
656 coclass_ints: { $$ = NULL; }
657 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
660 coclass_int:
661 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
664 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
665 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
668 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
669 $$ = $2;
670 if ($$->defined) yyerror("multiple definition error");
671 attrs = make_attr(ATTR_DISPINTERFACE);
672 LINK(attrs, $1);
673 $$->attrs = attrs;
674 $$->ref = find_type("IDispatch", 0);
675 if (!$$->ref) yyerror("IDispatch is undefined");
676 $$->defined = TRUE;
677 if (!parse_only && do_header) write_forward($$);
681 dispint_props: tPROPERTIES ':' { $$ = NULL; }
682 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
685 dispint_meths: tMETHODS ':' { $$ = NULL; }
686 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
689 dispinterfacedef: dispinterfacehdr '{'
690 dispint_props
691 dispint_meths
692 '}' { $$ = $1;
693 $$->fields = $3;
694 $$->funcs = $4;
695 if (!parse_only && do_header) write_dispinterface($$);
697 /* FIXME: not sure how to handle this yet
698 | dispinterfacehdr '{' interface '}' { $$ = $1;
699 if (!parse_only && do_header) write_interface($$);
704 inherit: { $$ = NULL; }
705 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
708 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
709 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
712 interfacehdr: attributes interface { $$ = $2;
713 if ($$->defined) yyerror("multiple definition error");
714 $$->attrs = $1;
715 $$->defined = TRUE;
716 if (!parse_only && do_header) write_forward($$);
720 interfacedef: interfacehdr inherit
721 '{' int_statements '}' { $$ = $1;
722 $$->ref = $2;
723 $$->funcs = $4;
724 if (!parse_only && do_header) write_interface($$);
726 /* MIDL is able to import the definition of a base class from inside the
727 * definition of a derived class, I'll try to support it with this rule */
728 | interfacehdr ':' aIDENTIFIER
729 '{' import int_statements '}' { $$ = $1;
730 $$->ref = find_type2($3, 0);
731 if (!$$->ref) yyerror("base class '%s' not found in import", $3);
732 $$->funcs = $6;
733 if (!parse_only && do_header) write_interface($$);
735 | dispinterfacedef { $$ = $1; }
738 interfacedec:
739 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
740 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
743 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
744 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
747 modulehdr: attributes module { $$ = $2;
748 $$->attrs = $1;
752 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
753 $$->funcs = $3;
754 /* FIXME: if (!parse_only && do_header) write_module($$); */
758 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
759 | tCONST p_ident { $$ = $2; /* FIXME */ }
762 pident: ident
763 | p_ident
764 | '(' pident ')' { $$ = $2; }
767 pident_list:
768 pident
769 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
772 pointer_type:
773 tREF { $$ = RPC_FC_RP; }
774 | tUNIQUE { $$ = RPC_FC_UP; }
775 | tPTR { $$ = RPC_FC_FP; }
778 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
779 /* overwrite RPC_FC_STRUCT with a more exact type */
780 $$->type = get_struct_type( $4 );
781 $$->fields = $4;
782 $$->defined = TRUE;
783 if(in_typelib)
784 add_struct($$);
788 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
789 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
790 | base_type { $$ = make_tref(NULL, $1); }
791 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
792 | enumdef { $$ = make_tref(NULL, $1); }
793 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
794 | structdef { $$ = make_tref(NULL, $1); }
795 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
796 | uniondef { $$ = make_tref(NULL, $1); }
797 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
798 | tSAFEARRAY '(' type ')' { $$ = make_tref(NULL, make_safearray()); }
801 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
802 $4->tname = tref->name;
803 tref->name = NULL;
804 $$ = type_ref(tref);
805 $$->attrs = $2;
806 if (!parse_only && do_header)
807 write_typedef($$, $4);
808 if (in_typelib && $$->attrs)
809 add_typedef($$, $4);
810 reg_types($$, $4, 0);
814 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
815 $$->fields = $4;
816 $$->defined = TRUE;
818 | tUNION t_ident
819 tSWITCH '(' s_field ')'
820 m_ident '{' cases '}' { var_t *u = $7;
821 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
822 if (!u) u = make_var("tagged_union");
823 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
824 u->type->fields = $9;
825 u->type->defined = TRUE;
826 LINK(u, $5); $$->fields = u;
827 $$->defined = TRUE;
831 version:
832 aNUM { $$ = MAKELONG($1, 0); }
833 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
838 static attr_t *make_attr(enum attr_type type)
840 attr_t *a = xmalloc(sizeof(attr_t));
841 a->type = type;
842 a->u.ival = 0;
843 INIT_LINK(a);
844 return a;
847 static attr_t *make_attrv(enum attr_type type, unsigned long val)
849 attr_t *a = xmalloc(sizeof(attr_t));
850 a->type = type;
851 a->u.ival = val;
852 INIT_LINK(a);
853 return a;
856 static attr_t *make_attrp(enum attr_type type, void *val)
858 attr_t *a = xmalloc(sizeof(attr_t));
859 a->type = type;
860 a->u.pval = val;
861 INIT_LINK(a);
862 return a;
865 static expr_t *make_expr(enum expr_type type)
867 expr_t *e = xmalloc(sizeof(expr_t));
868 e->type = type;
869 e->ref = NULL;
870 e->u.lval = 0;
871 e->is_const = FALSE;
872 INIT_LINK(e);
873 return e;
876 static expr_t *make_exprl(enum expr_type type, long val)
878 expr_t *e = xmalloc(sizeof(expr_t));
879 e->type = type;
880 e->ref = NULL;
881 e->u.lval = val;
882 e->is_const = FALSE;
883 INIT_LINK(e);
884 /* check for numeric constant */
885 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
886 /* make sure true/false value is valid */
887 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
888 e->is_const = TRUE;
889 e->cval = val;
891 return e;
894 static expr_t *make_exprs(enum expr_type type, char *val)
896 expr_t *e;
897 e = xmalloc(sizeof(expr_t));
898 e->type = type;
899 e->ref = NULL;
900 e->u.sval = val;
901 e->is_const = FALSE;
902 INIT_LINK(e);
903 /* check for predefined constants */
904 if (type == EXPR_IDENTIFIER) {
905 var_t *c = find_const(val, 0);
906 if (c) {
907 e->u.sval = c->name;
908 free(val);
909 e->is_const = TRUE;
910 e->cval = c->eval->cval;
913 return e;
916 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
918 expr_t *e;
919 e = xmalloc(sizeof(expr_t));
920 e->type = type;
921 e->ref = expr;
922 e->u.tref = tref;
923 e->is_const = FALSE;
924 INIT_LINK(e);
925 /* check for cast of constant expression */
926 if (type == EXPR_SIZEOF) {
927 switch (tref->ref->type) {
928 case RPC_FC_BYTE:
929 case RPC_FC_CHAR:
930 case RPC_FC_SMALL:
931 case RPC_FC_USMALL:
932 e->is_const = TRUE;
933 e->cval = 1;
934 break;
935 case RPC_FC_WCHAR:
936 case RPC_FC_USHORT:
937 case RPC_FC_SHORT:
938 e->is_const = TRUE;
939 e->cval = 2;
940 break;
941 case RPC_FC_LONG:
942 case RPC_FC_ULONG:
943 case RPC_FC_FLOAT:
944 case RPC_FC_ERROR_STATUS_T:
945 e->is_const = TRUE;
946 e->cval = 4;
947 break;
948 case RPC_FC_HYPER:
949 case RPC_FC_DOUBLE:
950 e->is_const = TRUE;
951 e->cval = 8;
952 break;
955 if (type == EXPR_CAST && expr->is_const) {
956 e->is_const = TRUE;
957 e->cval = expr->cval;
959 return e;
962 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
964 expr_t *e;
965 e = xmalloc(sizeof(expr_t));
966 e->type = type;
967 e->ref = expr;
968 e->u.lval = 0;
969 e->is_const = FALSE;
970 INIT_LINK(e);
971 /* check for compile-time optimization */
972 if (expr->is_const) {
973 e->is_const = TRUE;
974 switch (type) {
975 case EXPR_NEG:
976 e->cval = -expr->cval;
977 break;
978 case EXPR_NOT:
979 e->cval = ~expr->cval;
980 break;
981 default:
982 e->is_const = FALSE;
983 break;
986 return e;
989 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
991 expr_t *e;
992 e = xmalloc(sizeof(expr_t));
993 e->type = type;
994 e->ref = expr1;
995 e->u.ext = expr2;
996 e->is_const = FALSE;
997 INIT_LINK(e);
998 /* check for compile-time optimization */
999 if (expr1->is_const && expr2->is_const) {
1000 e->is_const = TRUE;
1001 switch (type) {
1002 case EXPR_ADD:
1003 e->cval = expr1->cval + expr2->cval;
1004 break;
1005 case EXPR_SUB:
1006 e->cval = expr1->cval - expr2->cval;
1007 break;
1008 case EXPR_MUL:
1009 e->cval = expr1->cval * expr2->cval;
1010 break;
1011 case EXPR_DIV:
1012 e->cval = expr1->cval / expr2->cval;
1013 break;
1014 case EXPR_OR:
1015 e->cval = expr1->cval | expr2->cval;
1016 break;
1017 case EXPR_AND:
1018 e->cval = expr1->cval & expr2->cval;
1019 break;
1020 case EXPR_SHL:
1021 e->cval = expr1->cval << expr2->cval;
1022 break;
1023 case EXPR_SHR:
1024 e->cval = expr1->cval >> expr2->cval;
1025 break;
1026 default:
1027 e->is_const = FALSE;
1028 break;
1031 return e;
1034 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1036 expr_t *e;
1037 e = xmalloc(sizeof(expr_t));
1038 e->type = type;
1039 e->ref = expr1;
1040 e->u.ext = expr2;
1041 e->ext2 = expr3;
1042 e->is_const = FALSE;
1043 INIT_LINK(e);
1044 /* check for compile-time optimization */
1045 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1046 e->is_const = TRUE;
1047 switch (type) {
1048 case EXPR_COND:
1049 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1050 break;
1051 default:
1052 e->is_const = FALSE;
1053 break;
1056 return e;
1059 static type_t *make_type(unsigned char type, type_t *ref)
1061 type_t *t = xmalloc(sizeof(type_t));
1062 t->name = NULL;
1063 t->type = type;
1064 t->ref = ref;
1065 t->attrs = NULL;
1066 t->funcs = NULL;
1067 t->fields = NULL;
1068 t->ifaces = NULL;
1069 t->ignore = parse_only;
1070 t->is_const = FALSE;
1071 t->sign = 0;
1072 t->defined = FALSE;
1073 t->written = FALSE;
1074 t->typelib_idx = -1;
1075 INIT_LINK(t);
1076 return t;
1079 static typeref_t *make_tref(char *name, type_t *ref)
1081 typeref_t *t = xmalloc(sizeof(typeref_t));
1082 t->name = name;
1083 t->ref = ref;
1084 t->uniq = ref ? 0 : 1;
1085 return t;
1088 static typeref_t *uniq_tref(typeref_t *ref)
1090 typeref_t *t = ref;
1091 type_t *tp;
1092 if (t->uniq) return t;
1093 tp = make_type(0, t->ref);
1094 tp->name = t->name;
1095 t->name = NULL;
1096 t->ref = tp;
1097 t->uniq = 1;
1098 return t;
1101 static type_t *type_ref(typeref_t *ref)
1103 type_t *t = ref->ref;
1104 if (ref->name) free(ref->name);
1105 free(ref);
1106 return t;
1109 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1111 v->type = ref->ref;
1112 v->tname = ref->name;
1113 ref->name = NULL;
1114 free(ref);
1115 v->array = arr;
1118 static ifref_t *make_ifref(type_t *iface)
1120 ifref_t *l = xmalloc(sizeof(ifref_t));
1121 l->iface = iface;
1122 l->attrs = NULL;
1123 INIT_LINK(l);
1124 return l;
1127 static var_t *make_var(char *name)
1129 var_t *v = xmalloc(sizeof(var_t));
1130 v->name = name;
1131 v->ptr_level = 0;
1132 v->type = NULL;
1133 v->tname = NULL;
1134 v->attrs = NULL;
1135 v->array = NULL;
1136 v->eval = NULL;
1137 INIT_LINK(v);
1138 return v;
1141 static func_t *make_func(var_t *def, var_t *args)
1143 func_t *f = xmalloc(sizeof(func_t));
1144 f->def = def;
1145 f->args = args;
1146 f->ignore = parse_only;
1147 f->idx = -1;
1148 INIT_LINK(f);
1149 return f;
1152 static type_t *make_class(char *name)
1154 type_t *c = make_type(0, NULL);
1155 c->name = name;
1156 INIT_LINK(c);
1157 return c;
1160 static type_t *make_safearray(void)
1162 return make_type(RPC_FC_FP, find_type("SAFEARRAY", 0));
1165 #define HASHMAX 64
1167 static int hash_ident(const char *name)
1169 const char *p = name;
1170 int sum = 0;
1171 /* a simple sum hash is probably good enough */
1172 while (*p) {
1173 sum += *p;
1174 p++;
1176 return sum & (HASHMAX-1);
1179 /***** type repository *****/
1181 struct rtype {
1182 const char *name;
1183 type_t *type;
1184 int t;
1185 struct rtype *next;
1188 struct rtype *type_hash[HASHMAX];
1190 static type_t *reg_type(type_t *type, const char *name, int t)
1192 struct rtype *nt;
1193 int hash;
1194 if (!name) {
1195 yyerror("registering named type without name");
1196 return type;
1198 hash = hash_ident(name);
1199 nt = xmalloc(sizeof(struct rtype));
1200 nt->name = name;
1201 nt->type = type;
1202 nt->t = t;
1203 nt->next = type_hash[hash];
1204 type_hash[hash] = nt;
1205 return type;
1208 /* determine pointer type from attrs */
1209 static unsigned char get_pointer_type( type_t *type )
1211 int t;
1212 if (is_attr( type->attrs, ATTR_STRING ))
1214 type_t *t = type;
1215 while( t->type == 0 && t->ref )
1216 t = t->ref;
1217 switch( t->type )
1219 case RPC_FC_CHAR:
1220 return RPC_FC_C_CSTRING;
1221 case RPC_FC_WCHAR:
1222 return RPC_FC_C_WSTRING;
1225 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1226 if (t) return t;
1227 return RPC_FC_FP;
1230 static type_t *reg_types(type_t *type, var_t *names, int t)
1232 type_t *ptr = type;
1233 int ptrc = 0;
1235 while (names) {
1236 var_t *next = NEXT_LINK(names);
1237 if (names->name) {
1238 type_t *cur = ptr;
1239 int cptr = names->ptr_level;
1240 if (cptr > ptrc) {
1241 while (cptr > ptrc) {
1242 int t = get_pointer_type( cur );
1243 cur = ptr = make_type(t, cur);
1244 ptrc++;
1246 } else {
1247 while (cptr < ptrc) {
1248 cur = cur->ref;
1249 cptr++;
1252 reg_type(cur, names->name, t);
1254 free(names);
1255 names = next;
1257 return type;
1260 static type_t *find_type(const char *name, int t)
1262 struct rtype *cur = type_hash[hash_ident(name)];
1263 while (cur && (cur->t != t || strcmp(cur->name, name)))
1264 cur = cur->next;
1265 if (!cur) {
1266 yyerror("type '%s' not found", name);
1267 return NULL;
1269 return cur->type;
1272 static type_t *find_type2(char *name, int t)
1274 type_t *tp = find_type(name, t);
1275 free(name);
1276 return tp;
1279 int is_type(const char *name)
1281 struct rtype *cur = type_hash[hash_ident(name)];
1282 while (cur && (cur->t || strcmp(cur->name, name)))
1283 cur = cur->next;
1284 if (cur) return TRUE;
1285 return FALSE;
1288 static type_t *get_type(unsigned char type, char *name, int t)
1290 struct rtype *cur = NULL;
1291 type_t *tp;
1292 if (name) {
1293 cur = type_hash[hash_ident(name)];
1294 while (cur && (cur->t != t || strcmp(cur->name, name)))
1295 cur = cur->next;
1297 if (cur) {
1298 free(name);
1299 return cur->type;
1301 tp = make_type(type, NULL);
1302 tp->name = name;
1303 if (!name) return tp;
1304 return reg_type(tp, name, t);
1307 static type_t *get_typev(unsigned char type, var_t *name, int t)
1309 char *sname = NULL;
1310 if (name) {
1311 sname = name->name;
1312 free(name);
1314 return get_type(type, sname, t);
1317 static int get_struct_type(var_t *field)
1319 int has_pointer = 0;
1320 int has_conformance = 0;
1321 int has_variance = 0;
1323 for (; field; field = NEXT_LINK(field))
1325 type_t *t = field->type;
1327 /* get the base type */
1328 while( (t->type == 0) && t->ref )
1329 t = t->ref;
1331 if (field->ptr_level > 0)
1333 has_pointer = 1;
1334 continue;
1337 if (is_string_type(field->attrs, 0, field->array))
1339 has_conformance = 1;
1340 has_variance = 1;
1341 continue;
1344 if (is_array_type(field->attrs, 0, field->array))
1346 if (field->array && !field->array->is_const)
1348 has_conformance = 1;
1349 if (PREV_LINK(field))
1350 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1351 field->name);
1353 if (is_attr(field->attrs, ATTR_LENGTHIS))
1354 has_variance = 1;
1357 switch (t->type)
1360 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1361 * Simple types don't effect the type of struct.
1362 * A struct containing a simple struct is still a simple struct.
1363 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1365 case 0: /* void pointer */
1366 case RPC_FC_BYTE:
1367 case RPC_FC_CHAR:
1368 case RPC_FC_SMALL:
1369 case RPC_FC_USMALL:
1370 case RPC_FC_WCHAR:
1371 case RPC_FC_SHORT:
1372 case RPC_FC_USHORT:
1373 case RPC_FC_LONG:
1374 case RPC_FC_ULONG:
1375 case RPC_FC_INT3264:
1376 case RPC_FC_UINT3264:
1377 case RPC_FC_HYPER:
1378 case RPC_FC_FLOAT:
1379 case RPC_FC_DOUBLE:
1380 case RPC_FC_STRUCT:
1381 case RPC_FC_ENUM16:
1382 case RPC_FC_ENUM32:
1383 break;
1385 case RPC_FC_UP:
1386 case RPC_FC_FP:
1387 has_pointer = 1;
1388 break;
1389 case RPC_FC_CARRAY:
1390 has_conformance = 1;
1391 if (PREV_LINK(field))
1392 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1393 field->name);
1394 break;
1395 case RPC_FC_C_CSTRING:
1396 case RPC_FC_C_WSTRING:
1397 has_conformance = 1;
1398 has_variance = 1;
1399 break;
1402 * Propagate member attributes
1403 * a struct should be at least as complex as its member
1405 case RPC_FC_CVSTRUCT:
1406 has_conformance = 1;
1407 has_variance = 1;
1408 has_pointer = 1;
1409 break;
1411 case RPC_FC_CPSTRUCT:
1412 has_conformance = 1;
1413 if (PREV_LINK(field))
1414 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1415 field->name);
1416 has_pointer = 1;
1417 break;
1419 case RPC_FC_CSTRUCT:
1420 has_conformance = 1;
1421 if (PREV_LINK(field))
1422 yyerror("field '%s' deriving from a conformant array must be the last field in the structure",
1423 field->name);
1424 break;
1426 case RPC_FC_PSTRUCT:
1427 has_pointer = 1;
1428 break;
1430 default:
1431 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1432 field->name, t->type);
1433 /* fallthru - treat it as complex */
1435 /* as soon as we see one of these these members, it's bogus... */
1436 case RPC_FC_IP:
1437 case RPC_FC_ENCAPSULATED_UNION:
1438 case RPC_FC_NON_ENCAPSULATED_UNION:
1439 case RPC_FC_TRANSMIT_AS:
1440 case RPC_FC_REPRESENT_AS:
1441 case RPC_FC_PAD:
1442 case RPC_FC_EMBEDDED_COMPLEX:
1443 case RPC_FC_BOGUS_STRUCT:
1444 return RPC_FC_BOGUS_STRUCT;
1448 if( has_variance )
1449 return RPC_FC_CVSTRUCT;
1450 if( has_conformance && has_pointer )
1451 return RPC_FC_CPSTRUCT;
1452 if( has_conformance )
1453 return RPC_FC_CSTRUCT;
1454 if( has_pointer )
1455 return RPC_FC_PSTRUCT;
1456 return RPC_FC_STRUCT;
1459 /***** constant repository *****/
1461 struct rconst {
1462 char *name;
1463 var_t *var;
1464 struct rconst *next;
1467 struct rconst *const_hash[HASHMAX];
1469 static var_t *reg_const(var_t *var)
1471 struct rconst *nc;
1472 int hash;
1473 if (!var->name) {
1474 yyerror("registering constant without name");
1475 return var;
1477 hash = hash_ident(var->name);
1478 nc = xmalloc(sizeof(struct rconst));
1479 nc->name = var->name;
1480 nc->var = var;
1481 nc->next = const_hash[hash];
1482 const_hash[hash] = nc;
1483 return var;
1486 static var_t *find_const(char *name, int f)
1488 struct rconst *cur = const_hash[hash_ident(name)];
1489 while (cur && strcmp(cur->name, name))
1490 cur = cur->next;
1491 if (!cur) {
1492 if (f) yyerror("constant '%s' not found", name);
1493 return NULL;
1495 return cur->var;