Replace LPDDSURFACEDESC by LPDDSURFACEDESC2 in IDirectDrawSurface4
[wine/multimedia.git] / tools / widl / parser.y
blob3838b0ce757380840f2d15b73612f4509afdbf78
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 "widl.h"
35 #include "utils.h"
36 #include "parser.h"
37 #include "header.h"
39 #if defined(YYBYACC)
40 /* Berkeley yacc (byacc) doesn't seem to know about these */
41 /* Some *BSD supplied versions do define these though */
42 # ifndef YYEMPTY
43 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
44 # endif
45 # ifndef YYLEX
46 # define YYLEX yylex()
47 # endif
49 #elif defined(YYBISON)
50 /* Bison was used for original development */
51 /* #define YYEMPTY -2 */
52 /* #define YYLEX yylex() */
54 #else
55 /* No yacc we know yet */
56 # if !defined(YYEMPTY) || !defined(YYLEX)
57 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
58 # elif defined(__GNUC__) /* gcc defines the #warning directive */
59 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
60 /* #else we just take a chance that it works... */
61 # endif
62 #endif
64 static attr_t *make_attr(enum attr_type type);
65 static attr_t *make_attrv(enum attr_type type, DWORD val);
66 static attr_t *make_attrp(enum attr_type type, void *val);
67 static expr_t *make_expr(enum expr_type type);
68 static expr_t *make_exprl(enum expr_type type, long val);
69 static expr_t *make_exprs(enum expr_type type, char *val);
70 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
71 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
72 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
73 static type_t *make_type(BYTE type, type_t *ref);
74 static typeref_t *make_tref(char *name, type_t *ref);
75 static typeref_t *uniq_tref(typeref_t *ref);
76 static type_t *type_ref(typeref_t *ref);
77 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
78 static var_t *make_var(char *name);
79 static func_t *make_func(var_t *def, var_t *args);
81 static type_t *reg_type(type_t *type, char *name, int t);
82 static type_t *reg_types(type_t *type, var_t *names, int t);
83 static type_t *find_type(char *name, int t);
84 static type_t *find_type2(char *name, int t);
85 static type_t *get_type(BYTE type, char *name, int t);
86 static type_t *get_typev(BYTE type, var_t *name, int t);
88 static var_t *reg_const(var_t *var);
89 static var_t *find_const(char *name, int f);
91 #define tsENUM 1
92 #define tsSTRUCT 2
93 #define tsUNION 3
95 static type_t std_bool = { "boolean" };
96 static type_t std_int = { "int" };
99 %union {
100 attr_t *attr;
101 expr_t *expr;
102 type_t *type;
103 typeref_t *tref;
104 var_t *var;
105 func_t *func;
106 char *str;
107 uuid_t *uuid;
108 int num;
111 %token <str> aIDENTIFIER
112 %token <str> aKNOWNTYPE
113 %token <num> aNUM aHEXNUM
114 %token <str> aSTRING
115 %token <uuid> aUUID
116 %token aEOF
117 %token SHL SHR
118 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
119 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
120 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
121 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
122 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
123 %token tDEFAULT
124 %token tDOUBLE
125 %token tENUM
126 %token tEXTERN
127 %token tFLOAT
128 %token tHYPER
129 %token tIIDIS
130 %token tIMPORT tIMPORTLIB
131 %token tIN tINCLUDE tINLINE
132 %token tINT tINT64
133 %token tINTERFACE
134 %token tLENGTHIS
135 %token tLOCAL
136 %token tLONG
137 %token tOBJECT tODL tOLEAUTOMATION
138 %token tOUT
139 %token tPOINTERDEFAULT
140 %token tREF
141 %token tSHORT
142 %token tSIGNED
143 %token tSIZEIS tSIZEOF
144 %token tSTDCALL
145 %token tSTRING tSTRUCT
146 %token tSWITCH tSWITCHIS tSWITCHTYPE
147 %token tTYPEDEF
148 %token tUNION
149 %token tUNIQUE
150 %token tUNSIGNED
151 %token tUUID
152 %token tV1ENUM
153 %token tVERSION
154 %token tVOID
155 %token tWCHAR tWIREMARSHAL
157 /* used in attr_t */
158 %token tPOINTERTYPE
160 %type <attr> m_attributes attributes attrib_list attribute
161 %type <expr> exprs expr_list expr array expr_const
162 %type <type> inherit interface interfacehdr interfacedef lib_statements
163 %type <type> base_type int_std
164 %type <type> enumdef structdef typedef uniondef
165 %type <tref> type
166 %type <var> m_args no_args args arg
167 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
168 %type <var> m_ident t_ident ident p_ident pident pident_list
169 %type <func> funcdef int_statements
170 %type <num> pointer_type
172 %left ','
173 %left '|'
174 %left '&'
175 %left '-' '+'
176 %left '*' '/'
177 %left SHL SHR
178 %right CAST
179 %right PPTR
180 %right NEG
184 input: lib_statements { /* FIXME */ }
187 lib_statements: { $$ = NULL; }
188 | lib_statements interface ';' { if (!parse_only) write_forward($2); }
189 | lib_statements interfacedef { LINK($2, $1); $$ = $2; }
190 /* | lib_statements librarydef (when implemented) */
191 | lib_statements statement
194 int_statements: { $$ = NULL; }
195 | int_statements funcdef ';' { LINK($2, $1); $$ = $2; }
196 | int_statements statement
199 statement: ';' {}
200 | constdef ';' { if (!parse_only) { write_constdef($1); } }
201 | cppquote {}
202 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
203 | externdef ';' { if (!parse_only) { write_externdef($1); } }
204 | import {}
205 /* | interface ';' {} */
206 /* | interfacedef {} */
207 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
208 | typedef ';' {}
209 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
212 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
214 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
215 if (!do_import($2)) yychar = aEOF; }
217 import: import_start input aEOF {}
220 m_args: { $$ = NULL; }
221 | args
224 no_args: tVOID { $$ = NULL; }
227 args: arg
228 | args ',' arg { LINK($3, $1); $$ = $3; }
229 | no_args
232 /* split into two rules to get bison to resolve a tVOID conflict */
233 arg: attributes type pident array { $$ = $3;
234 set_type($$, $2, $4);
235 $$->attrs = $1;
237 | type pident array { $$ = $2;
238 set_type($$, $1, $3);
242 array: { $$ = NULL; }
243 | '[' exprs ']' { $$ = $2; }
244 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
247 m_attributes: { $$ = NULL; }
248 | attributes
251 attributes:
252 m_attributes '[' attrib_list ']' { LINK_LAST($3, $1); $$ = $3; }
255 attrib_list: attribute
256 | attrib_list ',' attribute { LINK_SAFE($3, $1); $$ = $3; /* FIXME: don't use SAFE */ }
259 attribute:
260 tASYNC { $$ = make_attr(ATTR_ASYNC); }
261 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
262 | tCASE '(' expr_list ')' { $$ = NULL; }
263 | tCONTEXTHANDLE { $$ = NULL; }
264 | tCONTEXTHANDLENOSERIALIZE { $$ = NULL; }
265 | tCONTEXTHANDLESERIALIZE { $$ = NULL; }
266 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
267 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
268 | tIN { $$ = make_attr(ATTR_IN); }
269 | tLENGTHIS '(' exprs ')' { $$ = NULL; }
270 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
271 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
272 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
273 | tOUT { $$ = make_attr(ATTR_OUT); }
274 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
275 | tSIZEIS '(' exprs ')' { $$ = NULL; }
276 | tSTRING { $$ = make_attr(ATTR_STRING); }
277 | tSWITCHIS '(' expr ')' { $$ = NULL; }
278 | tSWITCHTYPE '(' type ')' { $$ = NULL; }
279 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
280 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
281 | tVERSION '(' version ')' { $$ = NULL; }
282 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
283 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
286 callconv:
287 | tSTDCALL
290 cases: { $$ = NULL; }
291 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
292 else { $$ = $1; }
296 case: tCASE expr ':' field { /* attr_t *a = NULL; */ /* FIXME */
297 $$ = $4; if (!$$) $$ = make_var(NULL);
298 /* LINK(a, $$->attrs); $$->attrs = a; */
300 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
301 $$ = $3; if (!$$) $$ = make_var(NULL);
302 LINK(a, $$->attrs); $$->attrs = a;
306 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
307 set_type($$, $2, NULL);
308 $$->eval = $5;
309 $$->lval = $5->cval;
313 enums: { $$ = NULL; }
314 | enum_list ',' { $$ = $1; }
315 | enum_list
318 enum_list: enum
319 | enum_list ',' enum { LINK($3, $1); $$ = $3;
320 if ($1 && !$3->eval)
321 $3->lval = $1->lval + 1;
325 enum: ident '=' expr_const { $$ = reg_const($1);
326 $$->eval = $3;
327 $$->lval = $3->cval;
329 | ident { $$ = reg_const($1);
330 $$->lval = 0; /* default for first enum entry */
334 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
335 $$->fields = $4;
336 $$->defined = TRUE;
340 exprs: { $$ = make_expr(EXPR_VOID); }
341 | expr_list
344 expr_list: expr
345 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
348 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
349 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
350 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
351 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
352 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
353 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
354 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
355 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
356 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
357 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
358 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
359 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
360 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
361 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
362 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
363 | '(' expr ')' { $$ = $2; }
366 expr_const: expr { $$ = $1;
367 if (!$$->is_const) yyerror("expression is not constant\n");
371 externdef: tEXTERN tCONST type ident { $$ = $4;
372 set_type($$, $3, NULL);
376 fields: { $$ = NULL; }
377 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
378 else { $$ = $1; }
382 field: s_field ';' { $$ = $1; }
383 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
384 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
385 | ';' { $$ = NULL; }
388 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
391 funcdef:
392 m_attributes type callconv pident
393 '(' m_args ')' { set_type($4, $2, NULL);
394 $4->attrs = $1;
395 $$ = make_func($4, $6);
399 m_ident: { $$ = NULL; }
400 | ident
403 t_ident: { $$ = NULL; }
404 | aIDENTIFIER { $$ = make_var($1); }
405 | aKNOWNTYPE { $$ = make_var($1); }
408 ident: aIDENTIFIER { $$ = make_var($1); }
411 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
412 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
413 | int_std
414 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
415 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
416 switch ($$->type) {
417 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
418 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
419 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
420 default: break;
423 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
424 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
425 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
428 m_int:
429 | tINT
432 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
433 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
434 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
435 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
436 | tINT64 { $$ = make_type(RPC_FC_HYPER, NULL); }
437 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
440 inherit: { $$ = NULL; }
441 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
444 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
445 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
448 interfacehdr: attributes interface { $$ = $2;
449 if ($$->defined) yyerror("multiple definition error\n");
450 $$->attrs = $1;
451 $$->defined = TRUE;
452 if (!parse_only) write_forward($$);
456 interfacedef: interfacehdr inherit
457 '{' int_statements '}' { $$ = $1;
458 $$->ref = $2;
459 $$->funcs = $4;
460 if (!parse_only) write_interface($$);
462 /* MIDL is able to import the definition of a base class from inside the
463 * definition of a derived class, I'll try to support it with this rule */
464 | interfacehdr ':' aIDENTIFIER
465 '{' import int_statements '}' { $$ = $1;
466 $$->ref = find_type2($3, 0);
467 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
468 $$->funcs = $6;
469 if (!parse_only) write_interface($$);
473 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
474 | tCONST p_ident { $$ = $2; /* FIXME */ }
477 pident: ident
478 | p_ident
479 | '(' pident ')' { $$ = $2; }
482 pident_list:
483 pident
484 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
487 pointer_type:
488 tREF { $$ = RPC_FC_RP; }
489 | tUNIQUE { $$ = RPC_FC_UP; }
492 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
493 $$->fields = $4;
494 $$->defined = TRUE;
498 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
499 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
500 | base_type { $$ = make_tref(NULL, $1); }
501 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
502 | enumdef { $$ = make_tref(NULL, $1); }
503 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
504 | structdef { $$ = make_tref(NULL, $1); }
505 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
506 | uniondef { $$ = make_tref(NULL, $1); }
507 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
510 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
511 $4->tname = tref->name;
512 tref->name = NULL;
513 $$ = type_ref(tref);
514 $$->attrs = $2;
515 if (!parse_only) write_typedef($$, $4);
516 reg_types($$, $4, 0);
520 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
521 $$->fields = $4;
522 $$->defined = TRUE;
524 | tUNION t_ident
525 tSWITCH '(' s_field ')'
526 m_ident '{' cases '}' { var_t *u = $7;
527 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
528 if (!u) u = make_var("tagged_union");
529 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
530 u->type->fields = $9;
531 u->type->defined = TRUE;
532 LINK(u, $5); $$->fields = u;
533 $$->defined = TRUE;
537 version:
538 aNUM {}
539 | aNUM '.' aNUM {}
544 static attr_t *make_attr(enum attr_type type)
546 attr_t *a = xmalloc(sizeof(attr_t));
547 a->type = type;
548 a->u.ival = 0;
549 INIT_LINK(a);
550 return a;
553 static attr_t *make_attrv(enum attr_type type, DWORD val)
555 attr_t *a = xmalloc(sizeof(attr_t));
556 a->type = type;
557 a->u.ival = val;
558 INIT_LINK(a);
559 return a;
562 static attr_t *make_attrp(enum attr_type type, void *val)
564 attr_t *a = xmalloc(sizeof(attr_t));
565 a->type = type;
566 a->u.pval = val;
567 INIT_LINK(a);
568 return a;
571 static expr_t *make_expr(enum expr_type type)
573 expr_t *e = xmalloc(sizeof(expr_t));
574 e->type = type;
575 e->ref = NULL;
576 e->u.lval = 0;
577 e->is_const = FALSE;
578 INIT_LINK(e);
579 return e;
582 static expr_t *make_exprl(enum expr_type type, long val)
584 expr_t *e = xmalloc(sizeof(expr_t));
585 e->type = type;
586 e->ref = NULL;
587 e->u.lval = val;
588 e->is_const = FALSE;
589 INIT_LINK(e);
590 /* check for numeric constant */
591 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
592 e->is_const = TRUE;
593 e->cval = val;
595 return e;
598 static expr_t *make_exprs(enum expr_type type, char *val)
600 expr_t *e;
601 e = xmalloc(sizeof(expr_t));
602 e->type = type;
603 e->ref = NULL;
604 e->u.sval = val;
605 e->is_const = FALSE;
606 INIT_LINK(e);
607 /* check for predefined constants */
608 if (type == EXPR_IDENTIFIER) {
609 var_t *c = find_const(val, 0);
610 if (c) {
611 e->u.sval = c->name;
612 free(val);
613 e->is_const = TRUE;
614 e->cval = c->lval;
617 return e;
620 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
622 expr_t *e;
623 e = xmalloc(sizeof(expr_t));
624 e->type = type;
625 e->ref = expr;
626 e->u.tref = tref;
627 e->is_const = FALSE;
628 INIT_LINK(e);
629 /* check for cast of constant expression */
630 if (type == EXPR_CAST && expr->is_const) {
631 e->is_const = TRUE;
632 e->cval = expr->cval;
634 return e;
637 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
639 expr_t *e;
640 e = xmalloc(sizeof(expr_t));
641 e->type = type;
642 e->ref = expr;
643 e->u.lval = 0;
644 e->is_const = FALSE;
645 INIT_LINK(e);
646 /* check for compile-time optimization */
647 if (expr->is_const) {
648 e->is_const = TRUE;
649 switch (type) {
650 case EXPR_NEG:
651 e->cval = -expr->cval;
652 break;
653 default:
654 e->is_const = FALSE;
655 break;
658 return e;
661 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
663 expr_t *e;
664 e = xmalloc(sizeof(expr_t));
665 e->type = type;
666 e->ref = expr1;
667 e->u.ext = expr2;
668 e->is_const = FALSE;
669 INIT_LINK(e);
670 /* check for compile-time optimization */
671 if (expr1->is_const && expr2->is_const) {
672 e->is_const = TRUE;
673 switch (type) {
674 case EXPR_ADD:
675 e->cval = expr1->cval + expr2->cval;
676 break;
677 case EXPR_SUB:
678 e->cval = expr1->cval - expr2->cval;
679 break;
680 case EXPR_MUL:
681 e->cval = expr1->cval * expr2->cval;
682 break;
683 case EXPR_DIV:
684 e->cval = expr1->cval / expr2->cval;
685 break;
686 case EXPR_OR:
687 e->cval = expr1->cval | expr2->cval;
688 break;
689 case EXPR_AND:
690 e->cval = expr1->cval & expr2->cval;
691 break;
692 case EXPR_SHL:
693 e->cval = expr1->cval << expr2->cval;
694 break;
695 case EXPR_SHR:
696 e->cval = expr1->cval >> expr2->cval;
697 break;
698 default:
699 e->is_const = FALSE;
700 break;
703 return e;
706 static type_t *make_type(BYTE type, type_t *ref)
708 type_t *t = xmalloc(sizeof(type_t));
709 t->name = NULL;
710 t->type = type;
711 t->ref = ref;
712 t->rname = NULL;
713 t->attrs = NULL;
714 t->funcs = NULL;
715 t->fields = NULL;
716 t->ignore = parse_only;
717 t->is_const = FALSE;
718 t->sign = 0;
719 t->defined = FALSE;
720 t->written = FALSE;
721 INIT_LINK(t);
722 return t;
725 static typeref_t *make_tref(char *name, type_t *ref)
727 typeref_t *t = xmalloc(sizeof(typeref_t));
728 t->name = name;
729 t->ref = ref;
730 t->uniq = ref ? 0 : 1;
731 return t;
734 static typeref_t *uniq_tref(typeref_t *ref)
736 typeref_t *t = ref;
737 type_t *tp;
738 if (t->uniq) return t;
739 tp = make_type(0, t->ref);
740 tp->name = t->name;
741 t->name = NULL;
742 t->ref = tp;
743 t->uniq = 1;
744 return t;
747 static type_t *type_ref(typeref_t *ref)
749 type_t *t = ref->ref;
750 if (ref->name) free(ref->name);
751 free(ref);
752 return t;
755 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
757 v->type = ref->ref;
758 v->tname = ref->name;
759 ref->name = NULL;
760 free(ref);
761 v->array = arr;
764 static var_t *make_var(char *name)
766 var_t *v = xmalloc(sizeof(var_t));
767 v->name = name;
768 v->ptr_level = 0;
769 v->type = NULL;
770 v->tname = NULL;
771 v->attrs = NULL;
772 v->array = NULL;
773 v->eval = NULL;
774 v->lval = 0;
775 INIT_LINK(v);
776 return v;
779 static func_t *make_func(var_t *def, var_t *args)
781 func_t *f = xmalloc(sizeof(func_t));
782 f->def = def;
783 f->args = args;
784 f->ignore = parse_only;
785 f->idx = -1;
786 INIT_LINK(f);
787 return f;
790 #define HASHMAX 64
792 static int hash_ident(const char *name)
794 const char *p = name;
795 int sum = 0;
796 /* a simple sum hash is probably good enough */
797 while (*p) {
798 sum += *p;
799 p++;
801 return sum & (HASHMAX-1);
804 /***** type repository *****/
806 struct rtype {
807 char *name;
808 type_t *type;
809 int t;
810 struct rtype *next;
813 struct rtype *type_hash[HASHMAX];
815 static type_t *reg_type(type_t *type, char *name, int t)
817 struct rtype *nt;
818 int hash;
819 if (!name) {
820 yyerror("registering named type without name\n");
821 return type;
823 hash = hash_ident(name);
824 nt = xmalloc(sizeof(struct rtype));
825 nt->name = name;
826 nt->type = type;
827 nt->t = t;
828 nt->next = type_hash[hash];
829 type_hash[hash] = nt;
830 return type;
833 static type_t *reg_types(type_t *type, var_t *names, int t)
835 type_t *ptr = type;
836 int ptrc = 0;
838 while (names) {
839 var_t *next = NEXT_LINK(names);
840 if (names->name) {
841 type_t *cur = ptr;
842 int cptr = names->ptr_level;
843 if (cptr > ptrc) {
844 while (cptr > ptrc) {
845 cur = ptr = make_type(RPC_FC_FP, cur); /* FIXME: pointer type from attrs? */
846 ptrc++;
848 } else {
849 while (cptr < ptrc) {
850 cur = cur->ref;
851 cptr++;
854 reg_type(cur, names->name, t);
856 free(names);
857 names = next;
859 return type;
862 static type_t *find_type(char *name, int t)
864 struct rtype *cur = type_hash[hash_ident(name)];
865 while (cur && (cur->t != t || strcmp(cur->name, name)))
866 cur = cur->next;
867 if (!cur) {
868 yyerror("type %s not found\n", name);
869 return NULL;
871 return cur->type;
874 static type_t *find_type2(char *name, int t)
876 type_t *tp = find_type(name, t);
877 free(name);
878 return tp;
881 int is_type(const char *name)
883 struct rtype *cur = type_hash[hash_ident(name)];
884 while (cur && (cur->t || strcmp(cur->name, name)))
885 cur = cur->next;
886 if (cur) return TRUE;
887 return FALSE;
890 static type_t *get_type(BYTE type, char *name, int t)
892 struct rtype *cur = NULL;
893 type_t *tp;
894 if (name) {
895 cur = type_hash[hash_ident(name)];
896 while (cur && (cur->t != t || strcmp(cur->name, name)))
897 cur = cur->next;
899 if (cur) {
900 free(name);
901 return cur->type;
903 tp = make_type(type, NULL);
904 tp->name = name;
905 if (!name) return tp;
906 return reg_type(tp, name, t);
909 static type_t *get_typev(BYTE type, var_t *name, int t)
911 char *sname = NULL;
912 if (name) {
913 sname = name->name;
914 free(name);
916 return get_type(type, sname, t);
919 /***** constant repository *****/
921 struct rconst {
922 char *name;
923 var_t *var;
924 struct rconst *next;
927 struct rconst *const_hash[HASHMAX];
929 static var_t *reg_const(var_t *var)
931 struct rconst *nc;
932 int hash;
933 if (!var->name) {
934 yyerror("registering constant without name\n");
935 return var;
937 hash = hash_ident(var->name);
938 nc = xmalloc(sizeof(struct rconst));
939 nc->name = var->name;
940 nc->var = var;
941 nc->next = const_hash[hash];
942 const_hash[hash] = nc;
943 return var;
946 static var_t *find_const(char *name, int f)
948 struct rconst *cur = const_hash[hash_ident(name)];
949 while (cur && strcmp(cur->name, name))
950 cur = cur->next;
951 if (!cur) {
952 if (f) yyerror("constant %s not found\n", name);
953 return NULL;
955 return cur->var;