Add support for the TextureAddress render state.
[wine/multimedia.git] / tools / widl / parser.y
blobf842f1566de60b70453d3802c10b19bc752a976e
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" };
97 static type_t std_int64 = { "__int64" };
98 static type_t std_uhyper = { "MIDL_uhyper" };
101 %union {
102 attr_t *attr;
103 expr_t *expr;
104 type_t *type;
105 typeref_t *tref;
106 var_t *var;
107 func_t *func;
108 char *str;
109 UUID *uuid;
110 int num;
113 %token <str> aIDENTIFIER
114 %token <str> aKNOWNTYPE
115 %token <num> aNUM aHEXNUM
116 %token <str> aSTRING
117 %token <uuid> aUUID
118 %token aEOF
119 %token SHL SHR
120 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
121 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
122 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
123 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
124 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
125 %token tDEFAULT
126 %token tDOUBLE
127 %token tENUM tERRORSTATUST
128 %token tEXTERN
129 %token tFLOAT
130 %token tHANDLET
131 %token tHYPER
132 %token tIDEMPOTENT
133 %token tIIDIS
134 %token tIMPORT tIMPORTLIB
135 %token tIN tINCLUDE tINLINE
136 %token tINT tINT64
137 %token tINTERFACE
138 %token tLENGTHIS
139 %token tLOCAL
140 %token tLONG
141 %token tOBJECT tODL tOLEAUTOMATION
142 %token tOUT
143 %token tPOINTERDEFAULT
144 %token tREF
145 %token tSHORT
146 %token tSIGNED
147 %token tSIZEIS tSIZEOF
148 %token tSTDCALL
149 %token tSTRING tSTRUCT
150 %token tSWITCH tSWITCHIS tSWITCHTYPE
151 %token tTYPEDEF
152 %token tUNION
153 %token tUNIQUE
154 %token tUNSIGNED
155 %token tUUID
156 %token tV1ENUM
157 %token tVERSION
158 %token tVOID
159 %token tWCHAR tWIREMARSHAL
161 /* used in attr_t */
162 %token tPOINTERTYPE
164 %type <attr> m_attributes attributes attrib_list attribute
165 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
166 %type <expr> array array_list
167 %type <type> inherit interface interfacehdr interfacedef lib_statements
168 %type <type> base_type int_std
169 %type <type> enumdef structdef typedef uniondef
170 %type <tref> type
171 %type <var> m_args no_args args arg
172 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
173 %type <var> m_ident t_ident ident p_ident pident pident_list
174 %type <func> funcdef int_statements
175 %type <num> pointer_type version
177 %left ','
178 %left '|'
179 %left '&'
180 %left '-' '+'
181 %left '*' '/'
182 %left SHL SHR
183 %right '~'
184 %right CAST
185 %right PPTR
186 %right NEG
190 input: lib_statements { /* FIXME */ }
193 lib_statements: { $$ = NULL; }
194 | lib_statements interface ';' { if (!parse_only) write_forward($2); }
195 | lib_statements interfacedef { LINK($2, $1); $$ = $2; }
196 /* | lib_statements librarydef (when implemented) */
197 | lib_statements statement
200 int_statements: { $$ = NULL; }
201 | int_statements funcdef ';' { LINK($2, $1); $$ = $2; }
202 | int_statements statement
205 statement: ';' {}
206 | constdef ';' { if (!parse_only) { write_constdef($1); } }
207 | cppquote {}
208 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
209 | externdef ';' { if (!parse_only) { write_externdef($1); } }
210 | import {}
211 /* | interface ';' {} */
212 /* | interfacedef {} */
213 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
214 | typedef ';' {}
215 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
218 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
220 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
221 if (!do_import($2)) yychar = aEOF; }
223 import: import_start input aEOF {}
226 m_args: { $$ = NULL; }
227 | args
230 no_args: tVOID { $$ = NULL; }
233 args: arg
234 | args ',' arg { LINK($3, $1); $$ = $3; }
235 | no_args
238 /* split into two rules to get bison to resolve a tVOID conflict */
239 arg: attributes type pident array { $$ = $3;
240 set_type($$, $2, $4);
241 $$->attrs = $1;
243 | type pident array { $$ = $2;
244 set_type($$, $1, $3);
248 array: { $$ = NULL; }
249 | '[' array_list ']' { $$ = $2; }
250 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
253 array_list: m_expr /* size of first dimension is optional */
254 | array_list ',' expr { LINK($3, $1); $$ = $3; }
255 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
258 m_attributes: { $$ = NULL; }
259 | attributes
262 attributes:
263 '[' attrib_list ']' { $$ = $2; }
266 attrib_list: attribute
267 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
268 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
271 attribute:
272 tASYNC { $$ = make_attr(ATTR_ASYNC); }
273 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
274 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
275 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
276 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
277 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
278 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
279 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
280 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
281 | tIN { $$ = make_attr(ATTR_IN); }
282 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
283 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
284 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
285 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
286 | tOUT { $$ = make_attr(ATTR_OUT); }
287 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
288 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
289 | tSTRING { $$ = make_attr(ATTR_STRING); }
290 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
291 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
292 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
293 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
294 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
295 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
296 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
299 callconv:
300 | tSTDCALL
303 cases: { $$ = NULL; }
304 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
305 else { $$ = $1; }
309 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
310 $$ = $4; if (!$$) $$ = make_var(NULL);
311 LINK(a, $$->attrs); $$->attrs = a;
313 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
314 $$ = $3; if (!$$) $$ = make_var(NULL);
315 LINK(a, $$->attrs); $$->attrs = a;
319 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
320 set_type($$, $2, NULL);
321 $$->eval = $5;
322 $$->lval = $5->cval;
326 enums: { $$ = NULL; }
327 | enum_list ',' { $$ = $1; }
328 | enum_list
331 enum_list: enum
332 | enum_list ',' enum { LINK($3, $1); $$ = $3;
333 if ($1 && !$3->eval)
334 $3->lval = $1->lval + 1;
338 enum: ident '=' expr_const { $$ = reg_const($1);
339 $$->eval = $3;
340 $$->lval = $3->cval;
342 | ident { $$ = reg_const($1);
343 $$->lval = 0; /* default for first enum entry */
347 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
348 $$->fields = $4;
349 $$->defined = TRUE;
353 m_exprs: m_expr
354 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
358 exprs: { $$ = make_expr(EXPR_VOID); }
359 | expr_list
362 expr_list: expr
363 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
367 m_expr: { $$ = make_expr(EXPR_VOID); }
368 | expr
371 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
372 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
373 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
374 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
375 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
376 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
377 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
378 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
379 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
380 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
381 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
382 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
383 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
384 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
385 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
386 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
387 | '(' expr ')' { $$ = $2; }
390 expr_list_const: expr_const
391 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
394 expr_const: expr { $$ = $1;
395 if (!$$->is_const) yyerror("expression is not constant\n");
399 externdef: tEXTERN tCONST type ident { $$ = $4;
400 set_type($$, $3, NULL);
404 fields: { $$ = NULL; }
405 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
406 else { $$ = $1; }
410 field: s_field ';' { $$ = $1; }
411 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
412 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
413 | ';' { $$ = NULL; }
416 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
419 funcdef:
420 m_attributes type callconv pident
421 '(' m_args ')' { set_type($4, $2, NULL);
422 $4->attrs = $1;
423 $$ = make_func($4, $6);
427 m_ident: { $$ = NULL; }
428 | ident
431 t_ident: { $$ = NULL; }
432 | aIDENTIFIER { $$ = make_var($1); }
433 | aKNOWNTYPE { $$ = make_var($1); }
436 ident: aIDENTIFIER { $$ = make_var($1); }
437 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
438 | tVERSION { $$ = make_var($<str>1); }
441 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
442 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
443 | int_std
444 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
445 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
446 switch ($$->type) {
447 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
448 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
449 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
450 case RPC_FC_HYPER:
451 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
452 break;
453 default: break;
456 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
457 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
458 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
459 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
460 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
463 m_int:
464 | tINT
467 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
468 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
469 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
470 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
471 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
472 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
475 inherit: { $$ = NULL; }
476 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
479 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
480 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
483 interfacehdr: attributes interface { $$ = $2;
484 if ($$->defined) yyerror("multiple definition error\n");
485 $$->attrs = $1;
486 $$->defined = TRUE;
487 if (!parse_only) write_forward($$);
491 interfacedef: interfacehdr inherit
492 '{' int_statements '}' { $$ = $1;
493 $$->ref = $2;
494 $$->funcs = $4;
495 if (!parse_only) write_interface($$);
497 /* MIDL is able to import the definition of a base class from inside the
498 * definition of a derived class, I'll try to support it with this rule */
499 | interfacehdr ':' aIDENTIFIER
500 '{' import int_statements '}' { $$ = $1;
501 $$->ref = find_type2($3, 0);
502 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
503 $$->funcs = $6;
504 if (!parse_only) write_interface($$);
508 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
509 | tCONST p_ident { $$ = $2; /* FIXME */ }
512 pident: ident
513 | p_ident
514 | '(' pident ')' { $$ = $2; }
517 pident_list:
518 pident
519 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
522 pointer_type:
523 tREF { $$ = RPC_FC_RP; }
524 | tUNIQUE { $$ = RPC_FC_UP; }
527 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
528 $$->fields = $4;
529 $$->defined = TRUE;
533 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
534 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
535 | base_type { $$ = make_tref(NULL, $1); }
536 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
537 | enumdef { $$ = make_tref(NULL, $1); }
538 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
539 | structdef { $$ = make_tref(NULL, $1); }
540 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
541 | uniondef { $$ = make_tref(NULL, $1); }
542 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
545 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
546 $4->tname = tref->name;
547 tref->name = NULL;
548 $$ = type_ref(tref);
549 $$->attrs = $2;
550 if (!parse_only) write_typedef($$, $4);
551 reg_types($$, $4, 0);
555 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
556 $$->fields = $4;
557 $$->defined = TRUE;
559 | tUNION t_ident
560 tSWITCH '(' s_field ')'
561 m_ident '{' cases '}' { var_t *u = $7;
562 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
563 if (!u) u = make_var("tagged_union");
564 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
565 u->type->fields = $9;
566 u->type->defined = TRUE;
567 LINK(u, $5); $$->fields = u;
568 $$->defined = TRUE;
572 version:
573 aNUM { $$ = MAKELONG($1, 0); }
574 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
579 static attr_t *make_attr(enum attr_type type)
581 attr_t *a = xmalloc(sizeof(attr_t));
582 a->type = type;
583 a->u.ival = 0;
584 INIT_LINK(a);
585 return a;
588 static attr_t *make_attrv(enum attr_type type, DWORD val)
590 attr_t *a = xmalloc(sizeof(attr_t));
591 a->type = type;
592 a->u.ival = val;
593 INIT_LINK(a);
594 return a;
597 static attr_t *make_attrp(enum attr_type type, void *val)
599 attr_t *a = xmalloc(sizeof(attr_t));
600 a->type = type;
601 a->u.pval = val;
602 INIT_LINK(a);
603 return a;
606 static expr_t *make_expr(enum expr_type type)
608 expr_t *e = xmalloc(sizeof(expr_t));
609 e->type = type;
610 e->ref = NULL;
611 e->u.lval = 0;
612 e->is_const = FALSE;
613 INIT_LINK(e);
614 return e;
617 static expr_t *make_exprl(enum expr_type type, long val)
619 expr_t *e = xmalloc(sizeof(expr_t));
620 e->type = type;
621 e->ref = NULL;
622 e->u.lval = val;
623 e->is_const = FALSE;
624 INIT_LINK(e);
625 /* check for numeric constant */
626 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
627 e->is_const = TRUE;
628 e->cval = val;
630 return e;
633 static expr_t *make_exprs(enum expr_type type, char *val)
635 expr_t *e;
636 e = xmalloc(sizeof(expr_t));
637 e->type = type;
638 e->ref = NULL;
639 e->u.sval = val;
640 e->is_const = FALSE;
641 INIT_LINK(e);
642 /* check for predefined constants */
643 if (type == EXPR_IDENTIFIER) {
644 var_t *c = find_const(val, 0);
645 if (c) {
646 e->u.sval = c->name;
647 free(val);
648 e->is_const = TRUE;
649 e->cval = c->lval;
652 return e;
655 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
657 expr_t *e;
658 e = xmalloc(sizeof(expr_t));
659 e->type = type;
660 e->ref = expr;
661 e->u.tref = tref;
662 e->is_const = FALSE;
663 INIT_LINK(e);
664 /* check for cast of constant expression */
665 if (type == EXPR_CAST && expr->is_const) {
666 e->is_const = TRUE;
667 e->cval = expr->cval;
669 return e;
672 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
674 expr_t *e;
675 e = xmalloc(sizeof(expr_t));
676 e->type = type;
677 e->ref = expr;
678 e->u.lval = 0;
679 e->is_const = FALSE;
680 INIT_LINK(e);
681 /* check for compile-time optimization */
682 if (expr->is_const) {
683 e->is_const = TRUE;
684 switch (type) {
685 case EXPR_NEG:
686 e->cval = -expr->cval;
687 break;
688 case EXPR_NOT:
689 e->cval = ~expr->cval;
690 break;
691 default:
692 e->is_const = FALSE;
693 break;
696 return e;
699 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
701 expr_t *e;
702 e = xmalloc(sizeof(expr_t));
703 e->type = type;
704 e->ref = expr1;
705 e->u.ext = expr2;
706 e->is_const = FALSE;
707 INIT_LINK(e);
708 /* check for compile-time optimization */
709 if (expr1->is_const && expr2->is_const) {
710 e->is_const = TRUE;
711 switch (type) {
712 case EXPR_ADD:
713 e->cval = expr1->cval + expr2->cval;
714 break;
715 case EXPR_SUB:
716 e->cval = expr1->cval - expr2->cval;
717 break;
718 case EXPR_MUL:
719 e->cval = expr1->cval * expr2->cval;
720 break;
721 case EXPR_DIV:
722 e->cval = expr1->cval / expr2->cval;
723 break;
724 case EXPR_OR:
725 e->cval = expr1->cval | expr2->cval;
726 break;
727 case EXPR_AND:
728 e->cval = expr1->cval & expr2->cval;
729 break;
730 case EXPR_SHL:
731 e->cval = expr1->cval << expr2->cval;
732 break;
733 case EXPR_SHR:
734 e->cval = expr1->cval >> expr2->cval;
735 break;
736 default:
737 e->is_const = FALSE;
738 break;
741 return e;
744 static type_t *make_type(BYTE type, type_t *ref)
746 type_t *t = xmalloc(sizeof(type_t));
747 t->name = NULL;
748 t->type = type;
749 t->ref = ref;
750 t->rname = NULL;
751 t->attrs = NULL;
752 t->funcs = NULL;
753 t->fields = NULL;
754 t->ignore = parse_only;
755 t->is_const = FALSE;
756 t->sign = 0;
757 t->defined = FALSE;
758 t->written = FALSE;
759 INIT_LINK(t);
760 return t;
763 static typeref_t *make_tref(char *name, type_t *ref)
765 typeref_t *t = xmalloc(sizeof(typeref_t));
766 t->name = name;
767 t->ref = ref;
768 t->uniq = ref ? 0 : 1;
769 return t;
772 static typeref_t *uniq_tref(typeref_t *ref)
774 typeref_t *t = ref;
775 type_t *tp;
776 if (t->uniq) return t;
777 tp = make_type(0, t->ref);
778 tp->name = t->name;
779 t->name = NULL;
780 t->ref = tp;
781 t->uniq = 1;
782 return t;
785 static type_t *type_ref(typeref_t *ref)
787 type_t *t = ref->ref;
788 if (ref->name) free(ref->name);
789 free(ref);
790 return t;
793 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
795 v->type = ref->ref;
796 v->tname = ref->name;
797 ref->name = NULL;
798 free(ref);
799 v->array = arr;
802 static var_t *make_var(char *name)
804 var_t *v = xmalloc(sizeof(var_t));
805 v->name = name;
806 v->ptr_level = 0;
807 v->type = NULL;
808 v->tname = NULL;
809 v->attrs = NULL;
810 v->array = NULL;
811 v->eval = NULL;
812 v->lval = 0;
813 INIT_LINK(v);
814 return v;
817 static func_t *make_func(var_t *def, var_t *args)
819 func_t *f = xmalloc(sizeof(func_t));
820 f->def = def;
821 f->args = args;
822 f->ignore = parse_only;
823 f->idx = -1;
824 INIT_LINK(f);
825 return f;
828 #define HASHMAX 64
830 static int hash_ident(const char *name)
832 const char *p = name;
833 int sum = 0;
834 /* a simple sum hash is probably good enough */
835 while (*p) {
836 sum += *p;
837 p++;
839 return sum & (HASHMAX-1);
842 /***** type repository *****/
844 struct rtype {
845 char *name;
846 type_t *type;
847 int t;
848 struct rtype *next;
851 struct rtype *type_hash[HASHMAX];
853 static type_t *reg_type(type_t *type, char *name, int t)
855 struct rtype *nt;
856 int hash;
857 if (!name) {
858 yyerror("registering named type without name\n");
859 return type;
861 hash = hash_ident(name);
862 nt = xmalloc(sizeof(struct rtype));
863 nt->name = name;
864 nt->type = type;
865 nt->t = t;
866 nt->next = type_hash[hash];
867 type_hash[hash] = nt;
868 return type;
871 static type_t *reg_types(type_t *type, var_t *names, int t)
873 type_t *ptr = type;
874 int ptrc = 0;
876 while (names) {
877 var_t *next = NEXT_LINK(names);
878 if (names->name) {
879 type_t *cur = ptr;
880 int cptr = names->ptr_level;
881 if (cptr > ptrc) {
882 while (cptr > ptrc) {
883 cur = ptr = make_type(RPC_FC_FP, cur); /* FIXME: pointer type from attrs? */
884 ptrc++;
886 } else {
887 while (cptr < ptrc) {
888 cur = cur->ref;
889 cptr++;
892 reg_type(cur, names->name, t);
894 free(names);
895 names = next;
897 return type;
900 static type_t *find_type(char *name, int t)
902 struct rtype *cur = type_hash[hash_ident(name)];
903 while (cur && (cur->t != t || strcmp(cur->name, name)))
904 cur = cur->next;
905 if (!cur) {
906 yyerror("type %s not found\n", name);
907 return NULL;
909 return cur->type;
912 static type_t *find_type2(char *name, int t)
914 type_t *tp = find_type(name, t);
915 free(name);
916 return tp;
919 int is_type(const char *name)
921 struct rtype *cur = type_hash[hash_ident(name)];
922 while (cur && (cur->t || strcmp(cur->name, name)))
923 cur = cur->next;
924 if (cur) return TRUE;
925 return FALSE;
928 static type_t *get_type(BYTE type, char *name, int t)
930 struct rtype *cur = NULL;
931 type_t *tp;
932 if (name) {
933 cur = type_hash[hash_ident(name)];
934 while (cur && (cur->t != t || strcmp(cur->name, name)))
935 cur = cur->next;
937 if (cur) {
938 free(name);
939 return cur->type;
941 tp = make_type(type, NULL);
942 tp->name = name;
943 if (!name) return tp;
944 return reg_type(tp, name, t);
947 static type_t *get_typev(BYTE type, var_t *name, int t)
949 char *sname = NULL;
950 if (name) {
951 sname = name->name;
952 free(name);
954 return get_type(type, sname, t);
957 /***** constant repository *****/
959 struct rconst {
960 char *name;
961 var_t *var;
962 struct rconst *next;
965 struct rconst *const_hash[HASHMAX];
967 static var_t *reg_const(var_t *var)
969 struct rconst *nc;
970 int hash;
971 if (!var->name) {
972 yyerror("registering constant without name\n");
973 return var;
975 hash = hash_ident(var->name);
976 nc = xmalloc(sizeof(struct rconst));
977 nc->name = var->name;
978 nc->var = var;
979 nc->next = const_hash[hash];
980 const_hash[hash] = nc;
981 return var;
984 static var_t *find_const(char *name, int f)
986 struct rconst *cur = const_hash[hash_ident(name)];
987 while (cur && strcmp(cur->name, name))
988 cur = cur->next;
989 if (!cur) {
990 if (f) yyerror("constant %s not found\n", name);
991 return NULL;
993 return cur->var;