widl: Use the same precendence rule for the conditional operator as in wpp.
[wine/wine64.git] / tools / widl / parser.y
bloba5561080046971e052078fcdf944adf548322e12
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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 class_t *make_class(char *name);
87 static type_t *reg_type(type_t *type, 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);
95 static var_t *reg_const(var_t *var);
96 static var_t *find_const(char *name, int f);
98 #define tsENUM 1
99 #define tsSTRUCT 2
100 #define tsUNION 3
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" };
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;
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 tARRAYS 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 tDEFAULTVALUE
136 %token tDISPLAYBIND
137 %token tDISPINTERFACE
138 %token tDLLNAME tDOUBLE tDUAL
139 %token tENDPOINT
140 %token tENTRY tENUM tERRORSTATUST
141 %token tEXPLICITHANDLE tEXTERN
142 %token tFLOAT
143 %token tHANDLE
144 %token tHANDLET
145 %token tHELPCONTEXT tHELPFILE
146 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
147 %token tHIDDEN
148 %token tHYPER tID tIDEMPOTENT
149 %token tIIDIS
150 %token tIMPLICITHANDLE
151 %token tIMPORT tIMPORTLIB
152 %token tIN tINCLUDE tINLINE
153 %token tINPUTSYNC
154 %token tINT tINT64
155 %token tINTERFACE
156 %token tLENGTHIS tLIBRARY
157 %token tLOCAL
158 %token tLONG
159 %token tMETHODS
160 %token tMODULE
161 %token tNONCREATABLE
162 %token tOBJECT tODL tOLEAUTOMATION
163 %token tOPTIONAL
164 %token tOUT
165 %token tPOINTERDEFAULT
166 %token tPROPERTIES
167 %token tPROPGET tPROPPUT tPROPPUTREF
168 %token tPTR
169 %token tPUBLIC
170 %token tRANGE
171 %token tREADONLY tREF
172 %token tRESTRICTED
173 %token tRETVAL
174 %token tSHORT
175 %token tSIGNED
176 %token tSINGLE
177 %token tSIZEIS tSIZEOF
178 %token tSMALL
179 %token tSOURCE
180 %token tSTDCALL
181 %token tSTRING tSTRUCT
182 %token tSWITCH tSWITCHIS tSWITCHTYPE
183 %token tTRANSMITAS
184 %token tTYPEDEF
185 %token tUNION
186 %token tUNIQUE
187 %token tUNSIGNED
188 %token tUUID
189 %token tV1ENUM
190 %token tVARARG
191 %token tVERSION
192 %token tVOID
193 %token tWCHAR tWIREMARSHAL
195 /* used in attr_t */
196 %token tPOINTERTYPE
198 %type <attr> m_attributes attributes attrib_list attribute
199 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
200 %type <expr> array array_list
201 %type <type> inherit interface interfacehdr interfacedef interfacedec
202 %type <type> dispinterface dispinterfacehdr dispinterfacedef
203 %type <type> module modulehdr moduledef
204 %type <type> base_type int_std
205 %type <type> enumdef structdef typedef uniondef
206 %type <ifref> gbl_statements coclass_ints coclass_int
207 %type <tref> type
208 %type <var> m_args no_args args arg
209 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
210 %type <var> m_ident t_ident ident p_ident pident pident_list
211 %type <var> dispint_props
212 %type <func> funcdef int_statements
213 %type <func> dispint_meths
214 %type <clas> coclass coclasshdr coclassdef
215 %type <num> pointer_type version
216 %type <str> libraryhdr
218 %left ','
219 %right '?' ':'
220 %left '|'
221 %left '&'
222 %left '-' '+'
223 %left '*' '/'
224 %left SHL SHR
225 %right '~'
226 %right CAST
227 %right PPTR
228 %right NEG
232 input: gbl_statements { write_proxies($1); write_client($1); write_server($1); }
235 gbl_statements: { $$ = NULL; }
236 | gbl_statements interfacedec { $$ = $1; }
237 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
238 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
239 | gbl_statements moduledef { $$ = $1; add_module($2); }
240 | gbl_statements librarydef { $$ = $1; }
241 | gbl_statements statement { $$ = $1; }
244 imp_statements: {}
245 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
246 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
247 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
248 | imp_statements moduledef { if (!parse_only) add_module($2); }
249 | imp_statements statement {}
252 int_statements: { $$ = NULL; }
253 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
254 | int_statements statement { $$ = $1; }
257 statement: ';' {}
258 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
259 | cppquote {}
260 | enumdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
261 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
262 | import {}
263 | structdef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
264 | typedef ';' {}
265 | uniondef ';' { if (!parse_only && do_header) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
268 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
270 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
271 if (!do_import($2)) yychar = aEOF; }
273 import: import_start imp_statements aEOF {}
276 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
278 library_start: attributes libraryhdr '{' { start_typelib($2, $1);
279 if (!parse_only && do_header) write_library($2, $1); }
281 librarydef: library_start imp_statements '}' { end_typelib(); }
284 m_args: { $$ = NULL; }
285 | args
288 no_args: tVOID { $$ = NULL; }
291 args: arg
292 | args ',' arg { LINK($3, $1); $$ = $3; }
293 | no_args
296 /* split into two rules to get bison to resolve a tVOID conflict */
297 arg: attributes type pident array { $$ = $3;
298 set_type($$, $2, $4);
299 $$->attrs = $1;
301 | type pident array { $$ = $2;
302 set_type($$, $1, $3);
304 | attributes type pident '(' m_args ')' { $$ = $3;
305 $$->ptr_level--;
306 set_type($$, $2, NULL);
307 $$->attrs = $1;
308 $$->args = $5;
310 | type pident '(' m_args ')' { $$ = $2;
311 $$->ptr_level--;
312 set_type($$, $1, NULL);
313 $$->args = $4;
317 array: { $$ = NULL; }
318 | '[' array_list ']' { $$ = $2; }
319 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
322 array_list: m_expr /* size of first dimension is optional */
323 | array_list ',' expr { LINK($3, $1); $$ = $3; }
324 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
327 m_attributes: { $$ = NULL; }
328 | attributes
331 attributes:
332 '[' attrib_list ']' { $$ = $2; }
335 attrib_list: attribute
336 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
337 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
340 attribute:
341 tASYNC { $$ = make_attr(ATTR_ASYNC); }
342 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
343 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
344 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
345 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
346 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
347 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
348 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
349 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
350 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
351 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
352 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
353 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
354 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
355 | tDUAL { $$ = make_attr(ATTR_DUAL); }
356 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
357 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
358 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
359 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
360 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
361 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
362 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
363 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
364 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
365 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
366 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
367 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
368 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
369 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
370 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
371 | tIN { $$ = make_attr(ATTR_IN); }
372 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
373 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
374 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
375 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
376 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
377 | tODL { $$ = make_attr(ATTR_ODL); }
378 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
379 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
380 | tOUT { $$ = make_attr(ATTR_OUT); }
381 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
382 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
383 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
384 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
385 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
386 | tRANGE '(' expr_const ',' expr_const ')' { LINK($5, $3); $$ = make_attrp(ATTR_RANGE, $5); }
387 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
388 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
389 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
390 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
391 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
392 | tSTRING { $$ = make_attr(ATTR_STRING); }
393 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
394 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
395 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
396 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
397 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
398 | tVARARG { $$ = make_attr(ATTR_VARARG); }
399 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
400 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
401 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
404 callconv:
405 | tSTDCALL
408 cases: { $$ = NULL; }
409 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
410 else { $$ = $1; }
414 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
415 $$ = $4; if (!$$) $$ = make_var(NULL);
416 LINK(a, $$->attrs); $$->attrs = a;
418 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
419 $$ = $3; if (!$$) $$ = make_var(NULL);
420 LINK(a, $$->attrs); $$->attrs = a;
424 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
425 set_type($$, $2, NULL);
426 $$->eval = $5;
427 $$->lval = $5->cval;
431 enums: { $$ = NULL; }
432 | enum_list ',' { $$ = $1; }
433 | enum_list
436 enum_list: enum
437 | enum_list ',' enum { LINK($3, $1); $$ = $3;
438 if ($1 && !$3->eval)
439 $3->lval = $1->lval + 1;
443 enum: ident '=' expr_const { $$ = reg_const($1);
444 $$->eval = $3;
445 $$->lval = $3->cval;
446 $$->type = make_type(RPC_FC_LONG, &std_int);
448 | ident { $$ = reg_const($1);
449 $$->lval = 0; /* default for first enum entry */
450 $$->type = make_type(RPC_FC_LONG, &std_int);
454 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
455 $$->fields = $4;
456 $$->defined = TRUE;
457 if(in_typelib)
458 add_enum($$);
462 m_exprs: m_expr
463 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
467 exprs: { $$ = make_expr(EXPR_VOID); }
468 | expr_list
471 expr_list: expr
472 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
476 m_expr: { $$ = make_expr(EXPR_VOID); }
477 | expr
480 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
481 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
482 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
483 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
484 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
485 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
486 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
487 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
488 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
489 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
490 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
491 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
492 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
493 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
494 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
495 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
496 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
497 | '(' expr ')' { $$ = $2; }
500 expr_list_const: expr_const
501 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
504 expr_const: expr { $$ = $1;
505 if (!$$->is_const)
506 yyerror("expression is not constant\n");
510 externdef: tEXTERN tCONST type ident { $$ = $4;
511 set_type($$, $3, NULL);
515 fields: { $$ = NULL; }
516 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
517 else { $$ = $1; }
521 field: s_field ';' { $$ = $1; }
522 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
523 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
524 | ';' { $$ = NULL; }
527 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
530 funcdef:
531 m_attributes type callconv pident
532 '(' m_args ')' { set_type($4, $2, NULL);
533 $4->attrs = $1;
534 $$ = make_func($4, $6);
535 if (is_attr($4->attrs, ATTR_IN)) {
536 yyerror("Inapplicable attribute");
541 m_ident: { $$ = NULL; }
542 | ident
545 t_ident: { $$ = NULL; }
546 | aIDENTIFIER { $$ = make_var($1); }
547 | aKNOWNTYPE { $$ = make_var($1); }
550 ident: aIDENTIFIER { $$ = make_var($1); }
551 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
552 | aKNOWNTYPE { $$ = make_var($<str>1); }
553 | tASYNC { $$ = make_var($<str>1); }
554 | tID { $$ = make_var($<str>1); }
555 | tRANGE { $$ = make_var($<str>1); }
556 | tRETVAL { $$ = make_var($<str>1); }
557 | tVERSION { $$ = make_var($<str>1); }
560 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
561 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
562 | int_std
563 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
564 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
565 switch ($$->type) {
566 case RPC_FC_CHAR: $$->type = RPC_FC_BYTE; $$->sign = 0; break;
567 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
568 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
569 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
570 case RPC_FC_HYPER:
571 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
572 break;
573 default: break;
576 | tUNSIGNED { $$ = make_type(RPC_FC_ULONG, &std_int); $$->sign = -1; }
577 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
578 | tSINGLE { $$ = make_type(RPC_FC_FLOAT, NULL); }
579 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
580 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
581 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
582 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
585 m_int:
586 | tINT
589 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
590 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
591 | tSMALL { $$ = make_type(RPC_FC_SMALL, NULL); }
592 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
593 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
594 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
595 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
598 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
599 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
602 coclasshdr: attributes coclass { $$ = $2;
603 $$->attrs = $1;
604 if (!parse_only && do_header)
605 write_coclass($$);
609 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
610 $$->ifaces = $3;
614 coclass_ints: { $$ = NULL; }
615 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
618 coclass_int:
619 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
622 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
623 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
626 dispinterfacehdr: attributes dispinterface { $$ = $2;
627 if ($$->defined) yyerror("multiple definition error\n");
628 $$->attrs = $1;
629 $$->attrs = make_attr(ATTR_DISPINTERFACE);
630 LINK($$->attrs, $1);
631 $$->ref = find_type("IDispatch", 0);
632 if (!$$->ref) yyerror("IDispatch is undefined\n");
633 $$->defined = TRUE;
634 if (!parse_only && do_header) write_forward($$);
638 dispint_props: tPROPERTIES ':' { $$ = NULL; }
639 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
642 dispint_meths: tMETHODS ':' { $$ = NULL; }
643 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
646 dispinterfacedef: dispinterfacehdr '{'
647 dispint_props
648 dispint_meths
649 '}' { $$ = $1;
650 $$->fields = $3;
651 $$->funcs = $4;
652 if (!parse_only && do_header) write_dispinterface($$);
654 /* FIXME: not sure how to handle this yet
655 | dispinterfacehdr '{' interface '}' { $$ = $1;
656 if (!parse_only && do_header) write_interface($$);
661 inherit: { $$ = NULL; }
662 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
665 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
666 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
669 interfacehdr: attributes interface { $$ = $2;
670 if ($$->defined) yyerror("multiple definition error\n");
671 $$->attrs = $1;
672 $$->defined = TRUE;
673 if (!parse_only && do_header) write_forward($$);
677 interfacedef: interfacehdr inherit
678 '{' int_statements '}' { $$ = $1;
679 $$->ref = $2;
680 $$->funcs = $4;
681 if (!parse_only && do_header) write_interface($$);
683 /* MIDL is able to import the definition of a base class from inside the
684 * definition of a derived class, I'll try to support it with this rule */
685 | interfacehdr ':' aIDENTIFIER
686 '{' import int_statements '}' { $$ = $1;
687 $$->ref = find_type2($3, 0);
688 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
689 $$->funcs = $6;
690 if (!parse_only && do_header) write_interface($$);
692 | dispinterfacedef { $$ = $1; }
695 interfacedec:
696 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
697 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
700 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
701 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
704 modulehdr: attributes module { $$ = $2;
705 $$->attrs = $1;
709 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
710 $$->funcs = $3;
711 /* FIXME: if (!parse_only && do_header) write_module($$); */
715 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
716 | tCONST p_ident { $$ = $2; /* FIXME */ }
719 pident: ident
720 | p_ident
721 | '(' pident ')' { $$ = $2; }
724 pident_list:
725 pident
726 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
729 pointer_type:
730 tREF { $$ = RPC_FC_RP; }
731 | tUNIQUE { $$ = RPC_FC_UP; }
732 | tPTR { $$ = RPC_FC_FP; }
735 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
736 /* overwrite RPC_FC_STRUCT with a more exact type */
737 $$->type = get_struct_type( $4 );
738 $$->fields = $4;
739 $$->defined = TRUE;
740 if(in_typelib)
741 add_struct($$);
745 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
746 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
747 | base_type { $$ = make_tref(NULL, $1); }
748 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
749 | enumdef { $$ = make_tref(NULL, $1); }
750 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
751 | structdef { $$ = make_tref(NULL, $1); }
752 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
753 | uniondef { $$ = make_tref(NULL, $1); }
754 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
757 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
758 $4->tname = tref->name;
759 tref->name = NULL;
760 $$ = type_ref(tref);
761 $$->attrs = $2;
762 if (!parse_only && do_header)
763 write_typedef($$, $4);
764 if (in_typelib && $$->attrs)
765 add_typedef($$, $4);
766 reg_types($$, $4, 0);
770 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
771 $$->fields = $4;
772 $$->defined = TRUE;
774 | tUNION t_ident
775 tSWITCH '(' s_field ')'
776 m_ident '{' cases '}' { var_t *u = $7;
777 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
778 if (!u) u = make_var("tagged_union");
779 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
780 u->type->fields = $9;
781 u->type->defined = TRUE;
782 LINK(u, $5); $$->fields = u;
783 $$->defined = TRUE;
787 version:
788 aNUM { $$ = MAKELONG($1, 0); }
789 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
794 static attr_t *make_attr(enum attr_type type)
796 attr_t *a = xmalloc(sizeof(attr_t));
797 a->type = type;
798 a->u.ival = 0;
799 INIT_LINK(a);
800 return a;
803 static attr_t *make_attrv(enum attr_type type, unsigned long val)
805 attr_t *a = xmalloc(sizeof(attr_t));
806 a->type = type;
807 a->u.ival = val;
808 INIT_LINK(a);
809 return a;
812 static attr_t *make_attrp(enum attr_type type, void *val)
814 attr_t *a = xmalloc(sizeof(attr_t));
815 a->type = type;
816 a->u.pval = val;
817 INIT_LINK(a);
818 return a;
821 static expr_t *make_expr(enum expr_type type)
823 expr_t *e = xmalloc(sizeof(expr_t));
824 e->type = type;
825 e->ref = NULL;
826 e->u.lval = 0;
827 e->is_const = FALSE;
828 INIT_LINK(e);
829 return e;
832 static expr_t *make_exprl(enum expr_type type, long val)
834 expr_t *e = xmalloc(sizeof(expr_t));
835 e->type = type;
836 e->ref = NULL;
837 e->u.lval = val;
838 e->is_const = FALSE;
839 INIT_LINK(e);
840 /* check for numeric constant */
841 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
842 e->is_const = TRUE;
843 e->cval = val;
845 return e;
848 static expr_t *make_exprs(enum expr_type type, char *val)
850 expr_t *e;
851 e = xmalloc(sizeof(expr_t));
852 e->type = type;
853 e->ref = NULL;
854 e->u.sval = val;
855 e->is_const = FALSE;
856 INIT_LINK(e);
857 /* check for predefined constants */
858 if (type == EXPR_IDENTIFIER) {
859 var_t *c = find_const(val, 0);
860 if (c) {
861 e->u.sval = c->name;
862 free(val);
863 e->is_const = TRUE;
864 e->cval = c->lval;
867 return e;
870 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
872 expr_t *e;
873 e = xmalloc(sizeof(expr_t));
874 e->type = type;
875 e->ref = expr;
876 e->u.tref = tref;
877 e->is_const = FALSE;
878 INIT_LINK(e);
879 /* check for cast of constant expression */
880 if (type == EXPR_CAST && expr->is_const) {
881 e->is_const = TRUE;
882 e->cval = expr->cval;
884 return e;
887 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
889 expr_t *e;
890 e = xmalloc(sizeof(expr_t));
891 e->type = type;
892 e->ref = expr;
893 e->u.lval = 0;
894 e->is_const = FALSE;
895 INIT_LINK(e);
896 /* check for compile-time optimization */
897 if (expr->is_const) {
898 e->is_const = TRUE;
899 switch (type) {
900 case EXPR_NEG:
901 e->cval = -expr->cval;
902 break;
903 case EXPR_NOT:
904 e->cval = ~expr->cval;
905 break;
906 default:
907 e->is_const = FALSE;
908 break;
911 return e;
914 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
916 expr_t *e;
917 e = xmalloc(sizeof(expr_t));
918 e->type = type;
919 e->ref = expr1;
920 e->u.ext = expr2;
921 e->is_const = FALSE;
922 INIT_LINK(e);
923 /* check for compile-time optimization */
924 if (expr1->is_const && expr2->is_const) {
925 e->is_const = TRUE;
926 switch (type) {
927 case EXPR_ADD:
928 e->cval = expr1->cval + expr2->cval;
929 break;
930 case EXPR_SUB:
931 e->cval = expr1->cval - expr2->cval;
932 break;
933 case EXPR_MUL:
934 e->cval = expr1->cval * expr2->cval;
935 break;
936 case EXPR_DIV:
937 e->cval = expr1->cval / expr2->cval;
938 break;
939 case EXPR_OR:
940 e->cval = expr1->cval | expr2->cval;
941 break;
942 case EXPR_AND:
943 e->cval = expr1->cval & expr2->cval;
944 break;
945 case EXPR_SHL:
946 e->cval = expr1->cval << expr2->cval;
947 break;
948 case EXPR_SHR:
949 e->cval = expr1->cval >> expr2->cval;
950 break;
951 default:
952 e->is_const = FALSE;
953 break;
956 return e;
959 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
961 expr_t *e;
962 e = xmalloc(sizeof(expr_t));
963 e->type = type;
964 e->ref = expr1;
965 e->u.ext = expr2;
966 e->ext2 = expr3;
967 e->is_const = FALSE;
968 INIT_LINK(e);
969 /* check for compile-time optimization */
970 if (expr1->is_const && expr2->is_const && expr3->is_const) {
971 e->is_const = TRUE;
972 switch (type) {
973 case EXPR_COND:
974 e->cval = expr1->cval ? expr2->cval : expr3->cval;
975 break;
976 default:
977 e->is_const = FALSE;
978 break;
981 return e;
984 static type_t *make_type(unsigned char type, type_t *ref)
986 type_t *t = xmalloc(sizeof(type_t));
987 t->name = NULL;
988 t->type = type;
989 t->ref = ref;
990 t->rname = NULL;
991 t->attrs = NULL;
992 t->funcs = NULL;
993 t->fields = NULL;
994 t->ignore = parse_only;
995 t->is_const = FALSE;
996 t->sign = 0;
997 t->defined = FALSE;
998 t->written = FALSE;
999 t->typelib_idx = -1;
1000 INIT_LINK(t);
1001 return t;
1004 static typeref_t *make_tref(char *name, type_t *ref)
1006 typeref_t *t = xmalloc(sizeof(typeref_t));
1007 t->name = name;
1008 t->ref = ref;
1009 t->uniq = ref ? 0 : 1;
1010 return t;
1013 static typeref_t *uniq_tref(typeref_t *ref)
1015 typeref_t *t = ref;
1016 type_t *tp;
1017 if (t->uniq) return t;
1018 tp = make_type(0, t->ref);
1019 tp->name = t->name;
1020 t->name = NULL;
1021 t->ref = tp;
1022 t->uniq = 1;
1023 return t;
1026 static type_t *type_ref(typeref_t *ref)
1028 type_t *t = ref->ref;
1029 if (ref->name) free(ref->name);
1030 free(ref);
1031 return t;
1034 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1036 v->type = ref->ref;
1037 v->tname = ref->name;
1038 ref->name = NULL;
1039 free(ref);
1040 v->array = arr;
1043 static ifref_t *make_ifref(type_t *iface)
1045 ifref_t *l = xmalloc(sizeof(ifref_t));
1046 l->iface = iface;
1047 l->attrs = NULL;
1048 INIT_LINK(l);
1049 return l;
1052 static var_t *make_var(char *name)
1054 var_t *v = xmalloc(sizeof(var_t));
1055 v->name = name;
1056 v->ptr_level = 0;
1057 v->type = NULL;
1058 v->tname = NULL;
1059 v->attrs = NULL;
1060 v->array = NULL;
1061 v->eval = NULL;
1062 v->lval = 0;
1063 INIT_LINK(v);
1064 return v;
1067 static func_t *make_func(var_t *def, var_t *args)
1069 func_t *f = xmalloc(sizeof(func_t));
1070 f->def = def;
1071 f->args = args;
1072 f->ignore = parse_only;
1073 f->idx = -1;
1074 INIT_LINK(f);
1075 return f;
1078 static class_t *make_class(char *name)
1080 class_t *c = xmalloc(sizeof(class_t));
1081 c->name = name;
1082 c->attrs = NULL;
1083 c->ifaces = NULL;
1084 INIT_LINK(c);
1085 return c;
1088 #define HASHMAX 64
1090 static int hash_ident(const char *name)
1092 const char *p = name;
1093 int sum = 0;
1094 /* a simple sum hash is probably good enough */
1095 while (*p) {
1096 sum += *p;
1097 p++;
1099 return sum & (HASHMAX-1);
1102 /***** type repository *****/
1104 struct rtype {
1105 char *name;
1106 type_t *type;
1107 int t;
1108 struct rtype *next;
1111 struct rtype *type_hash[HASHMAX];
1113 static type_t *reg_type(type_t *type, char *name, int t)
1115 struct rtype *nt;
1116 int hash;
1117 if (!name) {
1118 yyerror("registering named type without name\n");
1119 return type;
1121 hash = hash_ident(name);
1122 nt = xmalloc(sizeof(struct rtype));
1123 nt->name = name;
1124 nt->type = type;
1125 nt->t = t;
1126 nt->next = type_hash[hash];
1127 type_hash[hash] = nt;
1128 return type;
1131 /* determine pointer type from attrs */
1132 static unsigned char get_pointer_type( type_t *type )
1134 int t;
1135 if (is_attr( type->attrs, ATTR_STRING ))
1137 type_t *t = type;
1138 while( t->type == 0 && t->ref )
1139 t = t->ref;
1140 switch( t->type )
1142 case RPC_FC_CHAR:
1143 return RPC_FC_C_CSTRING;
1144 case RPC_FC_WCHAR:
1145 return RPC_FC_C_WSTRING;
1148 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1149 if (t) return t;
1150 return RPC_FC_FP;
1153 static type_t *reg_types(type_t *type, var_t *names, int t)
1155 type_t *ptr = type;
1156 int ptrc = 0;
1158 while (names) {
1159 var_t *next = NEXT_LINK(names);
1160 if (names->name) {
1161 type_t *cur = ptr;
1162 int cptr = names->ptr_level;
1163 if (cptr > ptrc) {
1164 while (cptr > ptrc) {
1165 int t = get_pointer_type( cur );
1166 cur = ptr = make_type(t, cur);
1167 ptrc++;
1169 } else {
1170 while (cptr < ptrc) {
1171 cur = cur->ref;
1172 cptr++;
1175 reg_type(cur, names->name, t);
1177 free(names);
1178 names = next;
1180 return type;
1183 static type_t *find_type(const char *name, int t)
1185 struct rtype *cur = type_hash[hash_ident(name)];
1186 while (cur && (cur->t != t || strcmp(cur->name, name)))
1187 cur = cur->next;
1188 if (!cur) {
1189 yyerror("type %s not found\n", name);
1190 return NULL;
1192 return cur->type;
1195 static type_t *find_type2(char *name, int t)
1197 type_t *tp = find_type(name, t);
1198 free(name);
1199 return tp;
1202 int is_type(const char *name)
1204 struct rtype *cur = type_hash[hash_ident(name)];
1205 while (cur && (cur->t || strcmp(cur->name, name)))
1206 cur = cur->next;
1207 if (cur) return TRUE;
1208 return FALSE;
1211 static type_t *get_type(unsigned char type, char *name, int t)
1213 struct rtype *cur = NULL;
1214 type_t *tp;
1215 if (name) {
1216 cur = type_hash[hash_ident(name)];
1217 while (cur && (cur->t != t || strcmp(cur->name, name)))
1218 cur = cur->next;
1220 if (cur) {
1221 free(name);
1222 return cur->type;
1224 tp = make_type(type, NULL);
1225 tp->name = name;
1226 if (!name) return tp;
1227 return reg_type(tp, name, t);
1230 static type_t *get_typev(unsigned char type, var_t *name, int t)
1232 char *sname = NULL;
1233 if (name) {
1234 sname = name->name;
1235 free(name);
1237 return get_type(type, sname, t);
1240 static int get_struct_type(var_t *field)
1242 int has_pointer = 0;
1243 int has_conformant_array = 0;
1244 int has_conformant_string = 0;
1246 while (field)
1248 type_t *t = field->type;
1250 /* get the base type */
1251 while( (t->type == 0) && t->ref )
1252 t = t->ref;
1254 switch (t->type)
1257 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1258 * Simple types don't effect the type of struct.
1259 * A struct containing a simple struct is still a simple struct.
1260 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1262 case 0: /* void pointer */
1263 case RPC_FC_BYTE:
1264 case RPC_FC_CHAR:
1265 case RPC_FC_SMALL:
1266 case RPC_FC_USMALL:
1267 case RPC_FC_WCHAR:
1268 case RPC_FC_SHORT:
1269 case RPC_FC_USHORT:
1270 case RPC_FC_LONG:
1271 case RPC_FC_ULONG:
1272 case RPC_FC_INT3264:
1273 case RPC_FC_UINT3264:
1274 case RPC_FC_HYPER:
1275 case RPC_FC_FLOAT:
1276 case RPC_FC_DOUBLE:
1277 case RPC_FC_STRUCT:
1278 case RPC_FC_ENUM16:
1279 case RPC_FC_ENUM32:
1280 break;
1282 case RPC_FC_UP:
1283 case RPC_FC_FP:
1284 has_pointer = 1;
1285 break;
1286 case RPC_FC_CARRAY:
1287 has_conformant_array = 1;
1288 break;
1289 case RPC_FC_C_CSTRING:
1290 case RPC_FC_C_WSTRING:
1291 has_conformant_string = 1;
1292 break;
1295 * Propagate member attributes
1296 * a struct should be at least as complex as its member
1298 case RPC_FC_CVSTRUCT:
1299 has_conformant_string = 1;
1300 has_pointer = 1;
1301 break;
1303 case RPC_FC_CPSTRUCT:
1304 has_conformant_array = 1;
1305 has_pointer = 1;
1306 break;
1308 case RPC_FC_CSTRUCT:
1309 has_conformant_array = 1;
1310 break;
1312 case RPC_FC_PSTRUCT:
1313 has_pointer = 1;
1314 break;
1316 default:
1317 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1318 field->name, t->type);
1319 /* fallthru - treat it as complex */
1321 /* as soon as we see one of these these members, it's bogus... */
1322 case RPC_FC_IP:
1323 case RPC_FC_ENCAPSULATED_UNION:
1324 case RPC_FC_NON_ENCAPSULATED_UNION:
1325 case RPC_FC_TRANSMIT_AS:
1326 case RPC_FC_REPRESENT_AS:
1327 case RPC_FC_PAD:
1328 case RPC_FC_EMBEDDED_COMPLEX:
1329 case RPC_FC_BOGUS_STRUCT:
1330 return RPC_FC_BOGUS_STRUCT;
1332 field = NEXT_LINK(field);
1335 if( has_conformant_string && has_pointer )
1336 return RPC_FC_CVSTRUCT;
1337 if( has_conformant_array && has_pointer )
1338 return RPC_FC_CPSTRUCT;
1339 if( has_conformant_array )
1340 return RPC_FC_CSTRUCT;
1341 if( has_pointer )
1342 return RPC_FC_PSTRUCT;
1343 return RPC_FC_STRUCT;
1346 /***** constant repository *****/
1348 struct rconst {
1349 char *name;
1350 var_t *var;
1351 struct rconst *next;
1354 struct rconst *const_hash[HASHMAX];
1356 static var_t *reg_const(var_t *var)
1358 struct rconst *nc;
1359 int hash;
1360 if (!var->name) {
1361 yyerror("registering constant without name\n");
1362 return var;
1364 hash = hash_ident(var->name);
1365 nc = xmalloc(sizeof(struct rconst));
1366 nc->name = var->name;
1367 nc->var = var;
1368 nc->next = const_hash[hash];
1369 const_hash[hash] = nc;
1370 return var;
1373 static var_t *find_const(char *name, int f)
1375 struct rconst *cur = const_hash[hash_ident(name)];
1376 while (cur && strcmp(cur->name, name))
1377 cur = cur->next;
1378 if (!cur) {
1379 if (f) yyerror("constant %s not found\n", name);
1380 return NULL;
1382 return cur->var;