Added partial support for function pointers.
[wine/multimedia.git] / tools / widl / parser.y
bloba427e6b863f4a7498029510a46f25e6f86dcbc00
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 unsigned 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);
246 | attributes type pident '(' m_args ')' { $$ = $3;
247 $$->ptr_level--;
248 set_type($$, $2, NULL);
249 $$->attrs = $1;
250 $$->args = $5;
252 | type pident '(' m_args ')' { $$ = $2;
253 $$->ptr_level--;
254 set_type($$, $1, NULL);
255 $$->args = $4;
259 array: { $$ = NULL; }
260 | '[' array_list ']' { $$ = $2; }
261 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
264 array_list: m_expr /* size of first dimension is optional */
265 | array_list ',' expr { LINK($3, $1); $$ = $3; }
266 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
269 m_attributes: { $$ = NULL; }
270 | attributes
273 attributes:
274 '[' attrib_list ']' { $$ = $2; }
277 attrib_list: attribute
278 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
279 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
282 attribute:
283 tASYNC { $$ = make_attr(ATTR_ASYNC); }
284 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
285 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
286 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
287 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
288 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
289 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
290 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
291 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
292 | tIN { $$ = make_attr(ATTR_IN); }
293 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
294 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
295 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
296 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
297 | tOUT { $$ = make_attr(ATTR_OUT); }
298 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
299 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
300 | tSTRING { $$ = make_attr(ATTR_STRING); }
301 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
302 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
303 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
304 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
305 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
306 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
307 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
310 callconv:
311 | tSTDCALL
314 cases: { $$ = NULL; }
315 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
316 else { $$ = $1; }
320 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
321 $$ = $4; if (!$$) $$ = make_var(NULL);
322 LINK(a, $$->attrs); $$->attrs = a;
324 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
325 $$ = $3; if (!$$) $$ = make_var(NULL);
326 LINK(a, $$->attrs); $$->attrs = a;
330 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
331 set_type($$, $2, NULL);
332 $$->eval = $5;
333 $$->lval = $5->cval;
337 enums: { $$ = NULL; }
338 | enum_list ',' { $$ = $1; }
339 | enum_list
342 enum_list: enum
343 | enum_list ',' enum { LINK($3, $1); $$ = $3;
344 if ($1 && !$3->eval)
345 $3->lval = $1->lval + 1;
349 enum: ident '=' expr_const { $$ = reg_const($1);
350 $$->eval = $3;
351 $$->lval = $3->cval;
353 | ident { $$ = reg_const($1);
354 $$->lval = 0; /* default for first enum entry */
358 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
359 $$->fields = $4;
360 $$->defined = TRUE;
364 m_exprs: m_expr
365 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
369 exprs: { $$ = make_expr(EXPR_VOID); }
370 | expr_list
373 expr_list: expr
374 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
378 m_expr: { $$ = make_expr(EXPR_VOID); }
379 | expr
382 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
383 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
384 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
385 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
386 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
387 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
388 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
389 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
390 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
391 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
392 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
393 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
394 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
395 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
396 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
397 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
398 | '(' expr ')' { $$ = $2; }
401 expr_list_const: expr_const
402 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
405 expr_const: expr { $$ = $1;
406 if (!$$->is_const) yyerror("expression is not constant\n");
410 externdef: tEXTERN tCONST type ident { $$ = $4;
411 set_type($$, $3, NULL);
415 fields: { $$ = NULL; }
416 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
417 else { $$ = $1; }
421 field: s_field ';' { $$ = $1; }
422 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
423 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
424 | ';' { $$ = NULL; }
427 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
430 funcdef:
431 m_attributes type callconv pident
432 '(' m_args ')' { set_type($4, $2, NULL);
433 $4->attrs = $1;
434 $$ = make_func($4, $6);
438 m_ident: { $$ = NULL; }
439 | ident
442 t_ident: { $$ = NULL; }
443 | aIDENTIFIER { $$ = make_var($1); }
444 | aKNOWNTYPE { $$ = make_var($1); }
447 ident: aIDENTIFIER { $$ = make_var($1); }
448 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
449 | tVERSION { $$ = make_var($<str>1); }
452 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
453 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
454 | int_std
455 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
456 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
457 switch ($$->type) {
458 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
459 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
460 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
461 case RPC_FC_HYPER:
462 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
463 break;
464 default: break;
467 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
468 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
469 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
470 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
471 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
474 m_int:
475 | tINT
478 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
479 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
480 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
481 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
482 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
483 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
486 inherit: { $$ = NULL; }
487 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
490 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
491 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
494 interfacehdr: attributes interface { $$ = $2;
495 if ($$->defined) yyerror("multiple definition error\n");
496 $$->attrs = $1;
497 $$->defined = TRUE;
498 if (!parse_only) write_forward($$);
502 interfacedef: interfacehdr inherit
503 '{' int_statements '}' { $$ = $1;
504 $$->ref = $2;
505 $$->funcs = $4;
506 if (!parse_only) write_interface($$);
508 /* MIDL is able to import the definition of a base class from inside the
509 * definition of a derived class, I'll try to support it with this rule */
510 | interfacehdr ':' aIDENTIFIER
511 '{' import int_statements '}' { $$ = $1;
512 $$->ref = find_type2($3, 0);
513 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
514 $$->funcs = $6;
515 if (!parse_only) write_interface($$);
519 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
520 | tCONST p_ident { $$ = $2; /* FIXME */ }
523 pident: ident
524 | p_ident
525 | '(' pident ')' { $$ = $2; }
528 pident_list:
529 pident
530 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
533 pointer_type:
534 tREF { $$ = RPC_FC_RP; }
535 | tUNIQUE { $$ = RPC_FC_UP; }
538 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
539 $$->fields = $4;
540 $$->defined = TRUE;
544 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
545 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
546 | base_type { $$ = make_tref(NULL, $1); }
547 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
548 | enumdef { $$ = make_tref(NULL, $1); }
549 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
550 | structdef { $$ = make_tref(NULL, $1); }
551 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
552 | uniondef { $$ = make_tref(NULL, $1); }
553 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
556 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
557 $4->tname = tref->name;
558 tref->name = NULL;
559 $$ = type_ref(tref);
560 $$->attrs = $2;
561 if (!parse_only) write_typedef($$, $4);
562 reg_types($$, $4, 0);
566 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
567 $$->fields = $4;
568 $$->defined = TRUE;
570 | tUNION t_ident
571 tSWITCH '(' s_field ')'
572 m_ident '{' cases '}' { var_t *u = $7;
573 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
574 if (!u) u = make_var("tagged_union");
575 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
576 u->type->fields = $9;
577 u->type->defined = TRUE;
578 LINK(u, $5); $$->fields = u;
579 $$->defined = TRUE;
583 version:
584 aNUM { $$ = MAKELONG($1, 0); }
585 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
590 static attr_t *make_attr(enum attr_type type)
592 attr_t *a = xmalloc(sizeof(attr_t));
593 a->type = type;
594 a->u.ival = 0;
595 INIT_LINK(a);
596 return a;
599 static attr_t *make_attrv(enum attr_type type, DWORD val)
601 attr_t *a = xmalloc(sizeof(attr_t));
602 a->type = type;
603 a->u.ival = val;
604 INIT_LINK(a);
605 return a;
608 static attr_t *make_attrp(enum attr_type type, void *val)
610 attr_t *a = xmalloc(sizeof(attr_t));
611 a->type = type;
612 a->u.pval = val;
613 INIT_LINK(a);
614 return a;
617 static expr_t *make_expr(enum expr_type type)
619 expr_t *e = xmalloc(sizeof(expr_t));
620 e->type = type;
621 e->ref = NULL;
622 e->u.lval = 0;
623 e->is_const = FALSE;
624 INIT_LINK(e);
625 return e;
628 static expr_t *make_exprl(enum expr_type type, long val)
630 expr_t *e = xmalloc(sizeof(expr_t));
631 e->type = type;
632 e->ref = NULL;
633 e->u.lval = val;
634 e->is_const = FALSE;
635 INIT_LINK(e);
636 /* check for numeric constant */
637 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
638 e->is_const = TRUE;
639 e->cval = val;
641 return e;
644 static expr_t *make_exprs(enum expr_type type, char *val)
646 expr_t *e;
647 e = xmalloc(sizeof(expr_t));
648 e->type = type;
649 e->ref = NULL;
650 e->u.sval = val;
651 e->is_const = FALSE;
652 INIT_LINK(e);
653 /* check for predefined constants */
654 if (type == EXPR_IDENTIFIER) {
655 var_t *c = find_const(val, 0);
656 if (c) {
657 e->u.sval = c->name;
658 free(val);
659 e->is_const = TRUE;
660 e->cval = c->lval;
663 return e;
666 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
668 expr_t *e;
669 e = xmalloc(sizeof(expr_t));
670 e->type = type;
671 e->ref = expr;
672 e->u.tref = tref;
673 e->is_const = FALSE;
674 INIT_LINK(e);
675 /* check for cast of constant expression */
676 if (type == EXPR_CAST && expr->is_const) {
677 e->is_const = TRUE;
678 e->cval = expr->cval;
680 return e;
683 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
685 expr_t *e;
686 e = xmalloc(sizeof(expr_t));
687 e->type = type;
688 e->ref = expr;
689 e->u.lval = 0;
690 e->is_const = FALSE;
691 INIT_LINK(e);
692 /* check for compile-time optimization */
693 if (expr->is_const) {
694 e->is_const = TRUE;
695 switch (type) {
696 case EXPR_NEG:
697 e->cval = -expr->cval;
698 break;
699 case EXPR_NOT:
700 e->cval = ~expr->cval;
701 break;
702 default:
703 e->is_const = FALSE;
704 break;
707 return e;
710 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
712 expr_t *e;
713 e = xmalloc(sizeof(expr_t));
714 e->type = type;
715 e->ref = expr1;
716 e->u.ext = expr2;
717 e->is_const = FALSE;
718 INIT_LINK(e);
719 /* check for compile-time optimization */
720 if (expr1->is_const && expr2->is_const) {
721 e->is_const = TRUE;
722 switch (type) {
723 case EXPR_ADD:
724 e->cval = expr1->cval + expr2->cval;
725 break;
726 case EXPR_SUB:
727 e->cval = expr1->cval - expr2->cval;
728 break;
729 case EXPR_MUL:
730 e->cval = expr1->cval * expr2->cval;
731 break;
732 case EXPR_DIV:
733 e->cval = expr1->cval / expr2->cval;
734 break;
735 case EXPR_OR:
736 e->cval = expr1->cval | expr2->cval;
737 break;
738 case EXPR_AND:
739 e->cval = expr1->cval & expr2->cval;
740 break;
741 case EXPR_SHL:
742 e->cval = expr1->cval << expr2->cval;
743 break;
744 case EXPR_SHR:
745 e->cval = expr1->cval >> expr2->cval;
746 break;
747 default:
748 e->is_const = FALSE;
749 break;
752 return e;
755 static type_t *make_type(BYTE type, type_t *ref)
757 type_t *t = xmalloc(sizeof(type_t));
758 t->name = NULL;
759 t->type = type;
760 t->ref = ref;
761 t->rname = NULL;
762 t->attrs = NULL;
763 t->funcs = NULL;
764 t->fields = NULL;
765 t->ignore = parse_only;
766 t->is_const = FALSE;
767 t->sign = 0;
768 t->defined = FALSE;
769 t->written = FALSE;
770 INIT_LINK(t);
771 return t;
774 static typeref_t *make_tref(char *name, type_t *ref)
776 typeref_t *t = xmalloc(sizeof(typeref_t));
777 t->name = name;
778 t->ref = ref;
779 t->uniq = ref ? 0 : 1;
780 return t;
783 static typeref_t *uniq_tref(typeref_t *ref)
785 typeref_t *t = ref;
786 type_t *tp;
787 if (t->uniq) return t;
788 tp = make_type(0, t->ref);
789 tp->name = t->name;
790 t->name = NULL;
791 t->ref = tp;
792 t->uniq = 1;
793 return t;
796 static type_t *type_ref(typeref_t *ref)
798 type_t *t = ref->ref;
799 if (ref->name) free(ref->name);
800 free(ref);
801 return t;
804 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
806 v->type = ref->ref;
807 v->tname = ref->name;
808 ref->name = NULL;
809 free(ref);
810 v->array = arr;
813 static var_t *make_var(char *name)
815 var_t *v = xmalloc(sizeof(var_t));
816 v->name = name;
817 v->ptr_level = 0;
818 v->type = NULL;
819 v->tname = NULL;
820 v->attrs = NULL;
821 v->array = NULL;
822 v->eval = NULL;
823 v->lval = 0;
824 INIT_LINK(v);
825 return v;
828 static func_t *make_func(var_t *def, var_t *args)
830 func_t *f = xmalloc(sizeof(func_t));
831 f->def = def;
832 f->args = args;
833 f->ignore = parse_only;
834 f->idx = -1;
835 INIT_LINK(f);
836 return f;
839 #define HASHMAX 64
841 static int hash_ident(const char *name)
843 const char *p = name;
844 int sum = 0;
845 /* a simple sum hash is probably good enough */
846 while (*p) {
847 sum += *p;
848 p++;
850 return sum & (HASHMAX-1);
853 /***** type repository *****/
855 struct rtype {
856 char *name;
857 type_t *type;
858 int t;
859 struct rtype *next;
862 struct rtype *type_hash[HASHMAX];
864 static type_t *reg_type(type_t *type, char *name, int t)
866 struct rtype *nt;
867 int hash;
868 if (!name) {
869 yyerror("registering named type without name\n");
870 return type;
872 hash = hash_ident(name);
873 nt = xmalloc(sizeof(struct rtype));
874 nt->name = name;
875 nt->type = type;
876 nt->t = t;
877 nt->next = type_hash[hash];
878 type_hash[hash] = nt;
879 return type;
882 static type_t *reg_types(type_t *type, var_t *names, int t)
884 type_t *ptr = type;
885 int ptrc = 0;
887 while (names) {
888 var_t *next = NEXT_LINK(names);
889 if (names->name) {
890 type_t *cur = ptr;
891 int cptr = names->ptr_level;
892 if (cptr > ptrc) {
893 while (cptr > ptrc) {
894 cur = ptr = make_type(RPC_FC_FP, cur); /* FIXME: pointer type from attrs? */
895 ptrc++;
897 } else {
898 while (cptr < ptrc) {
899 cur = cur->ref;
900 cptr++;
903 reg_type(cur, names->name, t);
905 free(names);
906 names = next;
908 return type;
911 static type_t *find_type(char *name, int t)
913 struct rtype *cur = type_hash[hash_ident(name)];
914 while (cur && (cur->t != t || strcmp(cur->name, name)))
915 cur = cur->next;
916 if (!cur) {
917 yyerror("type %s not found\n", name);
918 return NULL;
920 return cur->type;
923 static type_t *find_type2(char *name, int t)
925 type_t *tp = find_type(name, t);
926 free(name);
927 return tp;
930 int is_type(const char *name)
932 struct rtype *cur = type_hash[hash_ident(name)];
933 while (cur && (cur->t || strcmp(cur->name, name)))
934 cur = cur->next;
935 if (cur) return TRUE;
936 return FALSE;
939 static type_t *get_type(BYTE type, char *name, int t)
941 struct rtype *cur = NULL;
942 type_t *tp;
943 if (name) {
944 cur = type_hash[hash_ident(name)];
945 while (cur && (cur->t != t || strcmp(cur->name, name)))
946 cur = cur->next;
948 if (cur) {
949 free(name);
950 return cur->type;
952 tp = make_type(type, NULL);
953 tp->name = name;
954 if (!name) return tp;
955 return reg_type(tp, name, t);
958 static type_t *get_typev(BYTE type, var_t *name, int t)
960 char *sname = NULL;
961 if (name) {
962 sname = name->name;
963 free(name);
965 return get_type(type, sname, t);
968 /***** constant repository *****/
970 struct rconst {
971 char *name;
972 var_t *var;
973 struct rconst *next;
976 struct rconst *const_hash[HASHMAX];
978 static var_t *reg_const(var_t *var)
980 struct rconst *nc;
981 int hash;
982 if (!var->name) {
983 yyerror("registering constant without name\n");
984 return var;
986 hash = hash_ident(var->name);
987 nc = xmalloc(sizeof(struct rconst));
988 nc->name = var->name;
989 nc->var = var;
990 nc->next = const_hash[hash];
991 const_hash[hash] = nc;
992 return var;
995 static var_t *find_const(char *name, int f)
997 struct rconst *cur = const_hash[hash_ident(name)];
998 while (cur && strcmp(cur->name, name))
999 cur = cur->next;
1000 if (!cur) {
1001 if (f) yyerror("constant %s not found\n", name);
1002 return NULL;
1004 return cur->var;