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
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
48 # define YYLEX yylex()
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
57 /* No yacc we know yet */
58 # if !defined(YYEMPTY) || !defined(YYLEX)
59 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
60 # elif defined(__GNUC__) /* gcc defines the #warning directive */
61 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
62 /* #else we just take a chance that it works... */
66 static attr_t *make_attr(enum attr_type type);
67 static attr_t *make_attrv(enum attr_type type, unsigned long val);
68 static attr_t *make_attrp(enum attr_type type, void *val);
69 static expr_t *make_expr(enum expr_type type);
70 static expr_t *make_exprl(enum expr_type type, long val);
71 static expr_t *make_exprs(enum expr_type type, char *val);
72 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
73 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
74 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
75 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
76 static type_t *make_type(unsigned char type, type_t *ref);
77 static typeref_t *make_tref(char *name, type_t *ref);
78 static typeref_t *uniq_tref(typeref_t *ref);
79 static type_t *type_ref(typeref_t *ref);
80 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
81 static ifref_t *make_ifref(type_t *iface);
82 static var_t *make_var(char *name);
83 static func_t *make_func(var_t *def, var_t *args);
84 static class_t *make_class(char *name);
86 static type_t *reg_type(type_t *type, char *name, int t);
87 static type_t *reg_types(type_t *type, var_t *names, int t);
88 static type_t *find_type(char *name, int t);
89 static type_t *find_type2(char *name, int t);
90 static type_t *get_type(unsigned char type, char *name, int t);
91 static type_t *get_typev(unsigned char type, var_t *name, int t);
93 static var_t *reg_const(var_t *var);
94 static var_t *find_const(char *name, int f);
100 static type_t std_bool = { "boolean" };
101 static type_t std_int = { "int" };
102 static type_t std_int64 = { "__int64" };
103 static type_t std_uhyper = { "MIDL_uhyper" };
120 %token <str> aIDENTIFIER
121 %token <str> aKNOWNTYPE
122 %token <num> aNUM aHEXNUM
127 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
128 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
129 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
130 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
131 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
133 %token tDISPINTERFACE
134 %token tDLLNAME tDOUBLE tDUAL
136 %token tENTRY tENUM tERRORSTATUST
142 %token tHYPER tID tIDEMPOTENT
144 %token tIMPORT tIMPORTLIB
145 %token tIN tINCLUDE tINLINE
149 %token tLENGTHIS tLIBRARY
154 %token tOBJECT tODL tOLEAUTOMATION
156 %token tPOINTERDEFAULT
159 %token tREADONLY tREF
163 %token tSIZEIS tSIZEOF
166 %token tSTRING tSTRUCT
167 %token tSWITCH tSWITCHIS tSWITCHTYPE
177 %token tWCHAR tWIREMARSHAL
182 %type <attr> m_attributes attributes attrib_list attribute
183 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
184 %type <expr> array array_list
185 %type <type> inherit interface interfacehdr interfacedef interfacedec
186 %type <type> dispinterface dispinterfacehdr dispinterfacedef
187 %type <type> module modulehdr moduledef
188 %type <type> base_type int_std
189 %type <type> enumdef structdef typedef uniondef
190 %type <ifref> gbl_statements coclass_ints coclass_int
192 %type <var> m_args no_args args arg
193 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
194 %type <var> m_ident t_ident ident p_ident pident pident_list
195 %type <var> dispint_props
196 %type <func> funcdef int_statements
197 %type <func> dispint_meths
198 %type <clas> coclass coclasshdr coclassdef
199 %type <num> pointer_type version
200 %type <str> libraryhdr
215 input: gbl_statements { write_proxies($1); }
218 gbl_statements: { $$ = NULL; }
219 | gbl_statements interfacedec { $$ = $1; }
220 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
221 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
222 | gbl_statements moduledef { $$ = $1; add_module($2); }
223 | gbl_statements librarydef { $$ = $1; }
224 | gbl_statements statement { $$ = $1; }
228 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
229 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
230 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
231 | imp_statements moduledef { if (!parse_only) add_module($2); }
232 | imp_statements statement {}
235 int_statements: { $$ = NULL; }
236 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
237 | int_statements statement { $$ = $1; }
241 | constdef ';' { if (!parse_only) { write_constdef($1); } }
243 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
244 | externdef ';' { if (!parse_only) { write_externdef($1); } }
246 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
248 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
251 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
253 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
254 if (!do_import($2)) yychar = aEOF; }
256 import: import_start imp_statements aEOF {}
259 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
261 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
263 librarydef: library_start imp_statements '}' { end_typelib(); }
266 m_args: { $$ = NULL; }
270 no_args: tVOID { $$ = NULL; }
274 | args ',' arg { LINK($3, $1); $$ = $3; }
278 /* split into two rules to get bison to resolve a tVOID conflict */
279 arg: attributes type pident array { $$ = $3;
280 set_type($$, $2, $4);
283 | type pident array { $$ = $2;
284 set_type($$, $1, $3);
286 | attributes type pident '(' m_args ')' { $$ = $3;
288 set_type($$, $2, NULL);
292 | type pident '(' m_args ')' { $$ = $2;
294 set_type($$, $1, NULL);
299 array: { $$ = NULL; }
300 | '[' array_list ']' { $$ = $2; }
301 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
304 array_list: m_expr /* size of first dimension is optional */
305 | array_list ',' expr { LINK($3, $1); $$ = $3; }
306 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
309 m_attributes: { $$ = NULL; }
314 '[' attrib_list ']' { $$ = $2; }
317 attrib_list: attribute
318 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
319 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
323 tASYNC { $$ = make_attr(ATTR_ASYNC); }
324 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
325 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
326 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
327 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
328 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
329 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
330 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
331 | tDUAL { $$ = make_attr(ATTR_DUAL); }
332 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
333 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
334 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
335 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
336 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
337 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
338 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
339 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
340 | tIN { $$ = make_attr(ATTR_IN); }
341 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
342 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
343 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
344 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
345 | tODL { $$ = make_attr(ATTR_ODL); }
346 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
347 | tOUT { $$ = make_attr(ATTR_OUT); }
348 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
349 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
350 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
351 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
352 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
353 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
354 | tSTRING { $$ = make_attr(ATTR_STRING); }
355 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
356 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
357 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
358 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
359 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
360 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
361 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
362 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
369 cases: { $$ = NULL; }
370 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
375 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
376 $$ = $4; if (!$$) $$ = make_var(NULL);
377 LINK(a, $$->attrs); $$->attrs = a;
379 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
380 $$ = $3; if (!$$) $$ = make_var(NULL);
381 LINK(a, $$->attrs); $$->attrs = a;
385 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
386 set_type($$, $2, NULL);
392 enums: { $$ = NULL; }
393 | enum_list ',' { $$ = $1; }
398 | enum_list ',' enum { LINK($3, $1); $$ = $3;
400 $3->lval = $1->lval + 1;
404 enum: ident '=' expr_const { $$ = reg_const($1);
408 | ident { $$ = reg_const($1);
409 $$->lval = 0; /* default for first enum entry */
413 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
420 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
424 exprs: { $$ = make_expr(EXPR_VOID); }
429 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
433 m_expr: { $$ = make_expr(EXPR_VOID); }
437 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
438 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
439 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
440 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
441 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
442 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
443 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
444 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
445 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
446 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
447 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
448 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
449 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
450 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
451 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
452 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
453 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
454 | '(' expr ')' { $$ = $2; }
457 expr_list_const: expr_const
458 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
461 expr_const: expr { $$ = $1;
462 if (!$$->is_const) yyerror("expression is not constant\n");
466 externdef: tEXTERN tCONST type ident { $$ = $4;
467 set_type($$, $3, NULL);
471 fields: { $$ = NULL; }
472 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
477 field: s_field ';' { $$ = $1; }
478 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
479 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
483 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
487 m_attributes type callconv pident
488 '(' m_args ')' { set_type($4, $2, NULL);
490 $$ = make_func($4, $6);
494 m_ident: { $$ = NULL; }
498 t_ident: { $$ = NULL; }
499 | aIDENTIFIER { $$ = make_var($1); }
500 | aKNOWNTYPE { $$ = make_var($1); }
503 ident: aIDENTIFIER { $$ = make_var($1); }
504 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
505 | tID { $$ = make_var($<str>1); }
506 | tRETVAL { $$ = make_var($<str>1); }
507 | tVERSION { $$ = make_var($<str>1); }
510 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
511 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
513 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
514 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
516 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
517 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
518 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
520 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
525 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
526 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
527 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
528 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
529 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
536 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
537 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
538 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
539 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
540 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
541 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
544 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
545 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
548 coclasshdr: attributes coclass { $$ = $2;
553 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
558 coclass_ints: { $$ = NULL; }
559 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
563 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
566 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
567 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
570 dispinterfacehdr: attributes dispinterface { $$ = $2;
571 if ($$->defined) yyerror("multiple definition error\n");
573 /* $$->attrs = make_attr(ATTR_DISPINTERFACE); */
574 /* LINK($$->attrs, $1); */
575 $$->ref = find_type("IDispatch", 0);
576 if (!$$->ref) yyerror("IDispatch is undefined\n");
578 if (!parse_only) write_forward($$);
582 dispint_props: tPROPERTIES ':' { $$ = NULL; }
583 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
586 dispint_meths: tMETHODS ':' { $$ = NULL; }
587 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
590 dispinterfacedef: dispinterfacehdr '{'
596 if (!parse_only) write_interface($$);
598 /* FIXME: not sure how to handle this yet
599 | dispinterfacehdr '{' interface '}' { $$ = $1;
600 if (!parse_only) write_interface($$);
605 inherit: { $$ = NULL; }
606 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
609 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
610 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
613 interfacehdr: attributes interface { $$ = $2;
614 if ($$->defined) yyerror("multiple definition error\n");
617 if (!parse_only) write_forward($$);
621 interfacedef: interfacehdr inherit
622 '{' int_statements '}' { $$ = $1;
625 if (!parse_only) write_interface($$);
627 /* MIDL is able to import the definition of a base class from inside the
628 * definition of a derived class, I'll try to support it with this rule */
629 | interfacehdr ':' aIDENTIFIER
630 '{' import int_statements '}' { $$ = $1;
631 $$->ref = find_type2($3, 0);
632 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
634 if (!parse_only) write_interface($$);
636 | dispinterfacedef { $$ = $1; }
640 interface ';' { $$ = $1; if (!parse_only) write_forward($$); }
641 | dispinterface ';' { $$ = $1; if (!parse_only) write_forward($$); }
644 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
645 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
648 modulehdr: attributes module { $$ = $2;
653 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
655 /* FIXME: if (!parse_only) write_module($$); */
659 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
660 | tCONST p_ident { $$ = $2; /* FIXME */ }
665 | '(' pident ')' { $$ = $2; }
670 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
674 tREF { $$ = RPC_FC_RP; }
675 | tUNIQUE { $$ = RPC_FC_UP; }
678 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
684 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
685 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
686 | base_type { $$ = make_tref(NULL, $1); }
687 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
688 | enumdef { $$ = make_tref(NULL, $1); }
689 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
690 | structdef { $$ = make_tref(NULL, $1); }
691 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
692 | uniondef { $$ = make_tref(NULL, $1); }
693 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
696 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
697 $4->tname = tref->name;
701 if (!parse_only) write_typedef($$, $4);
702 reg_types($$, $4, 0);
706 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
711 tSWITCH '(' s_field ')'
712 m_ident '{' cases '}' { var_t *u = $7;
713 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
714 if (!u) u = make_var("tagged_union");
715 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
716 u->type->fields = $9;
717 u->type->defined = TRUE;
718 LINK(u, $5); $$->fields = u;
724 aNUM { $$ = MAKELONG($1, 0); }
725 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
730 static attr_t *make_attr(enum attr_type type)
732 attr_t *a = xmalloc(sizeof(attr_t));
739 static attr_t *make_attrv(enum attr_type type, unsigned long val)
741 attr_t *a = xmalloc(sizeof(attr_t));
748 static attr_t *make_attrp(enum attr_type type, void *val)
750 attr_t *a = xmalloc(sizeof(attr_t));
757 static expr_t *make_expr(enum expr_type type)
759 expr_t *e = xmalloc(sizeof(expr_t));
768 static expr_t *make_exprl(enum expr_type type, long val)
770 expr_t *e = xmalloc(sizeof(expr_t));
776 /* check for numeric constant */
777 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
784 static expr_t *make_exprs(enum expr_type type, char *val)
787 e = xmalloc(sizeof(expr_t));
793 /* check for predefined constants */
794 if (type == EXPR_IDENTIFIER) {
795 var_t *c = find_const(val, 0);
806 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
809 e = xmalloc(sizeof(expr_t));
815 /* check for cast of constant expression */
816 if (type == EXPR_CAST && expr->is_const) {
818 e->cval = expr->cval;
823 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
826 e = xmalloc(sizeof(expr_t));
832 /* check for compile-time optimization */
833 if (expr->is_const) {
837 e->cval = -expr->cval;
840 e->cval = ~expr->cval;
850 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
853 e = xmalloc(sizeof(expr_t));
859 /* check for compile-time optimization */
860 if (expr1->is_const && expr2->is_const) {
864 e->cval = expr1->cval + expr2->cval;
867 e->cval = expr1->cval - expr2->cval;
870 e->cval = expr1->cval * expr2->cval;
873 e->cval = expr1->cval / expr2->cval;
876 e->cval = expr1->cval | expr2->cval;
879 e->cval = expr1->cval & expr2->cval;
882 e->cval = expr1->cval << expr2->cval;
885 e->cval = expr1->cval >> expr2->cval;
895 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
898 e = xmalloc(sizeof(expr_t));
905 /* check for compile-time optimization */
906 if (expr1->is_const && expr2->is_const && expr3->is_const) {
910 e->cval = expr1->cval ? expr2->cval : expr3->cval;
920 static type_t *make_type(unsigned char type, type_t *ref)
922 type_t *t = xmalloc(sizeof(type_t));
930 t->ignore = parse_only;
939 static typeref_t *make_tref(char *name, type_t *ref)
941 typeref_t *t = xmalloc(sizeof(typeref_t));
944 t->uniq = ref ? 0 : 1;
948 static typeref_t *uniq_tref(typeref_t *ref)
952 if (t->uniq) return t;
953 tp = make_type(0, t->ref);
961 static type_t *type_ref(typeref_t *ref)
963 type_t *t = ref->ref;
964 if (ref->name) free(ref->name);
969 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
972 v->tname = ref->name;
978 static ifref_t *make_ifref(type_t *iface)
980 ifref_t *l = xmalloc(sizeof(ifref_t));
987 static var_t *make_var(char *name)
989 var_t *v = xmalloc(sizeof(var_t));
1002 static func_t *make_func(var_t *def, var_t *args)
1004 func_t *f = xmalloc(sizeof(func_t));
1007 f->ignore = parse_only;
1013 static class_t *make_class(char *name)
1015 class_t *c = xmalloc(sizeof(class_t));
1025 static int hash_ident(const char *name)
1027 const char *p = name;
1029 /* a simple sum hash is probably good enough */
1034 return sum & (HASHMAX-1);
1037 /***** type repository *****/
1046 struct rtype *type_hash[HASHMAX];
1048 static type_t *reg_type(type_t *type, char *name, int t)
1053 yyerror("registering named type without name\n");
1056 hash = hash_ident(name);
1057 nt = xmalloc(sizeof(struct rtype));
1061 nt->next = type_hash[hash];
1062 type_hash[hash] = nt;
1066 /* determine pointer type from attrs */
1067 static unsigned char get_pointer_type( type_t *type )
1069 if( is_attr( type->attrs, ATTR_SIZEIS ) )
1070 return RPC_FC_CARRAY;
1072 return RPC_FC_CSTRUCT;
1076 static type_t *reg_types(type_t *type, var_t *names, int t)
1082 var_t *next = NEXT_LINK(names);
1085 int cptr = names->ptr_level;
1087 while (cptr > ptrc) {
1088 int t = get_pointer_type( cur );
1089 cur = ptr = make_type(t, cur);
1093 while (cptr < ptrc) {
1098 reg_type(cur, names->name, t);
1106 static type_t *find_type(char *name, int t)
1108 struct rtype *cur = type_hash[hash_ident(name)];
1109 while (cur && (cur->t != t || strcmp(cur->name, name)))
1112 yyerror("type %s not found\n", name);
1118 static type_t *find_type2(char *name, int t)
1120 type_t *tp = find_type(name, t);
1125 int is_type(const char *name)
1127 struct rtype *cur = type_hash[hash_ident(name)];
1128 while (cur && (cur->t || strcmp(cur->name, name)))
1130 if (cur) return TRUE;
1134 static type_t *get_type(unsigned char type, char *name, int t)
1136 struct rtype *cur = NULL;
1139 cur = type_hash[hash_ident(name)];
1140 while (cur && (cur->t != t || strcmp(cur->name, name)))
1147 tp = make_type(type, NULL);
1149 if (!name) return tp;
1150 return reg_type(tp, name, t);
1153 static type_t *get_typev(unsigned char type, var_t *name, int t)
1160 return get_type(type, sname, t);
1163 /***** constant repository *****/
1168 struct rconst *next;
1171 struct rconst *const_hash[HASHMAX];
1173 static var_t *reg_const(var_t *var)
1178 yyerror("registering constant without name\n");
1181 hash = hash_ident(var->name);
1182 nc = xmalloc(sizeof(struct rconst));
1183 nc->name = var->name;
1185 nc->next = const_hash[hash];
1186 const_hash[hash] = nc;
1190 static var_t *find_const(char *name, int f)
1192 struct rconst *cur = const_hash[hash_ident(name)];
1193 while (cur && strcmp(cur->name, name))
1196 if (f) yyerror("constant %s not found\n", name);