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
44 /* Berkeley yacc (byacc) doesn't seem to know about these */
45 /* Some *BSD supplied versions do define these though */
47 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
50 # define YYLEX yylex()
53 #elif defined(YYBISON)
54 /* Bison was used for original development */
55 /* #define YYEMPTY -2 */
56 /* #define YYLEX yylex() */
59 /* No yacc we know yet */
60 # if !defined(YYEMPTY) || !defined(YYLEX)
61 # error Yacc version/type unknown. This version needs to be verified for settings of YYEMPTY and YYLEX.
62 # elif defined(__GNUC__) /* gcc defines the #warning directive */
63 # warning Yacc version/type unknown. It defines YYEMPTY and YYLEX, but is not tested
64 /* #else we just take a chance that it works... */
68 static attr_t *make_attr(enum attr_type type);
69 static attr_t *make_attrv(enum attr_type type, unsigned long val);
70 static attr_t *make_attrp(enum attr_type type, void *val);
71 static expr_t *make_expr(enum expr_type type);
72 static expr_t *make_exprl(enum expr_type type, long val);
73 static expr_t *make_exprs(enum expr_type type, char *val);
74 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr);
75 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
76 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
77 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
78 static type_t *make_type(unsigned char type, type_t *ref);
79 static typeref_t *make_tref(char *name, type_t *ref);
80 static typeref_t *uniq_tref(typeref_t *ref);
81 static type_t *type_ref(typeref_t *ref);
82 static void set_type(var_t *v, typeref_t *ref, expr_t *arr);
83 static ifref_t *make_ifref(type_t *iface);
84 static var_t *make_var(char *name);
85 static func_t *make_func(var_t *def, var_t *args);
86 static class_t *make_class(char *name);
88 static type_t *reg_type(type_t *type, char *name, int t);
89 static type_t *reg_types(type_t *type, var_t *names, int t);
90 static type_t *find_type(char *name, int t);
91 static type_t *find_type2(char *name, int t);
92 static type_t *get_type(unsigned char type, char *name, int t);
93 static type_t *get_typev(unsigned char type, var_t *name, int t);
94 static int get_struct_type(var_t *fields);
96 static var_t *reg_const(var_t *var);
97 static var_t *find_const(char *name, int f);
103 static type_t std_bool = { "boolean" };
104 static type_t std_int = { "int" };
105 static type_t std_int64 = { "__int64" };
106 static type_t std_uhyper = { "MIDL_uhyper" };
123 %token <str> aIDENTIFIER
124 %token <str> aKNOWNTYPE
125 %token <num> aNUM aHEXNUM
130 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
131 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
132 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
133 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
134 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
137 %token tDISPINTERFACE
138 %token tDLLNAME tDOUBLE tDUAL
140 %token tENTRY tENUM tERRORSTATUST
147 %token tHYPER tID tIDEMPOTENT
149 %token tIMPORT tIMPORTLIB
150 %token tIN tINCLUDE tINLINE
154 %token tLENGTHIS tLIBRARY
159 %token tOBJECT tODL tOLEAUTOMATION
162 %token tPOINTERDEFAULT
164 %token tPROPGET tPROPPUT
166 %token tREADONLY tREF
171 %token tSIZEIS tSIZEOF
174 %token tSTRING tSTRUCT
175 %token tSWITCH tSWITCHIS tSWITCHTYPE
186 %token tWCHAR tWIREMARSHAL
191 %type <attr> m_attributes attributes attrib_list attribute
192 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
193 %type <expr> array array_list
194 %type <type> inherit interface interfacehdr interfacedef interfacedec
195 %type <type> dispinterface dispinterfacehdr dispinterfacedef
196 %type <type> module modulehdr moduledef
197 %type <type> base_type int_std
198 %type <type> enumdef structdef typedef uniondef
199 %type <ifref> gbl_statements coclass_ints coclass_int
201 %type <var> m_args no_args args arg
202 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
203 %type <var> m_ident t_ident ident p_ident pident pident_list
204 %type <var> dispint_props
205 %type <func> funcdef int_statements
206 %type <func> dispint_meths
207 %type <clas> coclass coclasshdr coclassdef
208 %type <num> pointer_type version
209 %type <str> libraryhdr
225 input: gbl_statements { write_proxies($1); }
228 gbl_statements: { $$ = NULL; }
229 | gbl_statements interfacedec { $$ = $1; }
230 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
231 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
232 | gbl_statements moduledef { $$ = $1; add_module($2); }
233 | gbl_statements librarydef { $$ = $1; }
234 | gbl_statements statement { $$ = $1; }
238 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
239 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
240 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
241 | imp_statements moduledef { if (!parse_only) add_module($2); }
242 | imp_statements statement {}
245 int_statements: { $$ = NULL; }
246 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
247 | int_statements statement { $$ = $1; }
251 | constdef ';' { if (!parse_only) { write_constdef($1); } }
253 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
254 | externdef ';' { if (!parse_only) { write_externdef($1); } }
256 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
258 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
261 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
263 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
264 if (!do_import($2)) yychar = aEOF; }
266 import: import_start imp_statements aEOF {}
269 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
271 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
273 librarydef: library_start imp_statements '}' { end_typelib(); }
276 m_args: { $$ = NULL; }
280 no_args: tVOID { $$ = NULL; }
284 | args ',' arg { LINK($3, $1); $$ = $3; }
288 /* split into two rules to get bison to resolve a tVOID conflict */
289 arg: attributes type pident array { $$ = $3;
290 set_type($$, $2, $4);
292 if (!is_attr($$->attrs, ATTR_OUT) &&
293 !is_attr($$->attrs, ATTR_IN)) {
294 attr_t *a = make_attr(ATTR_IN);
295 LINK(a, $$->attrs); $$->attrs = a;
298 | type pident array { $$ = $2;
299 set_type($$, $1, $3);
300 $$->attrs = make_attr(ATTR_IN);
302 | attributes type pident '(' m_args ')' { $$ = $3;
304 set_type($$, $2, NULL);
307 if (!is_attr($$->attrs, ATTR_OUT) &&
308 !is_attr($$->attrs, ATTR_IN)) {
309 attr_t *a = make_attr(ATTR_IN);
310 LINK(a, $$->attrs); $$->attrs = a;
313 | type pident '(' m_args ')' { $$ = $2;
315 set_type($$, $1, NULL);
316 $$->attrs = make_attr(ATTR_IN);
321 array: { $$ = NULL; }
322 | '[' array_list ']' { $$ = $2; }
323 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
326 array_list: m_expr /* size of first dimension is optional */
327 | array_list ',' expr { LINK($3, $1); $$ = $3; }
328 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
331 m_attributes: { $$ = NULL; }
336 '[' attrib_list ']' { $$ = $2; }
339 attrib_list: attribute
340 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
341 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
345 tASYNC { $$ = make_attr(ATTR_ASYNC); }
346 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
347 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
348 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
349 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
350 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
351 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
352 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
353 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
354 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE, $3); }
355 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
356 | tDUAL { $$ = make_attr(ATTR_DUAL); }
357 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
358 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
359 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
360 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
361 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
362 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
363 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
364 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
365 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
366 | tIN { $$ = make_attr(ATTR_IN); }
367 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
368 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
369 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
370 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
371 | tODL { $$ = make_attr(ATTR_ODL); }
372 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
373 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
374 | tOUT { $$ = make_attr(ATTR_OUT); }
375 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
376 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
377 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
378 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
379 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
380 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
381 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
382 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
383 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
384 | tSTRING { $$ = make_attr(ATTR_STRING); }
385 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
386 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
387 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
388 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
389 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
390 | tVARARG { $$ = make_attr(ATTR_VARARG); }
391 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
392 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
393 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
400 cases: { $$ = NULL; }
401 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
406 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
407 $$ = $4; if (!$$) $$ = make_var(NULL);
408 LINK(a, $$->attrs); $$->attrs = a;
410 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
411 $$ = $3; if (!$$) $$ = make_var(NULL);
412 LINK(a, $$->attrs); $$->attrs = a;
416 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
417 set_type($$, $2, NULL);
423 enums: { $$ = NULL; }
424 | enum_list ',' { $$ = $1; }
429 | enum_list ',' enum { LINK($3, $1); $$ = $3;
431 $3->lval = $1->lval + 1;
435 enum: ident '=' expr_const { $$ = reg_const($1);
439 | ident { $$ = reg_const($1);
440 $$->lval = 0; /* default for first enum entry */
444 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
451 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
455 exprs: { $$ = make_expr(EXPR_VOID); }
460 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
464 m_expr: { $$ = make_expr(EXPR_VOID); }
468 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
469 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
470 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
471 | expr '?' expr ':' expr %prec COND { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
472 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
473 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
474 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
475 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
476 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
477 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
478 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
479 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
480 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
481 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
482 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
483 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
484 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
485 | '(' expr ')' { $$ = $2; }
488 expr_list_const: expr_const
489 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
492 expr_const: expr { $$ = $1;
493 if (!$$->is_const) yyerror("expression is not constant\n");
497 externdef: tEXTERN tCONST type ident { $$ = $4;
498 set_type($$, $3, NULL);
502 fields: { $$ = NULL; }
503 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
508 field: s_field ';' { $$ = $1; }
509 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
510 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
514 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
518 m_attributes type callconv pident
519 '(' m_args ')' { set_type($4, $2, NULL);
521 $$ = make_func($4, $6);
522 if (is_attr($4->attrs, ATTR_IN)) {
523 yyerror("Inapplicable attribute");
525 if (!is_attr($4->attrs, ATTR_OUT)) {
526 attr_t *a = make_attr(ATTR_OUT);
527 LINK(a, $4->attrs); $4->attrs = a;
532 m_ident: { $$ = NULL; }
536 t_ident: { $$ = NULL; }
537 | aIDENTIFIER { $$ = make_var($1); }
538 | aKNOWNTYPE { $$ = make_var($1); }
541 ident: aIDENTIFIER { $$ = make_var($1); }
542 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
543 | aKNOWNTYPE { $$ = make_var($<str>1); }
544 | tID { $$ = make_var($<str>1); }
545 | tRETVAL { $$ = make_var($<str>1); }
546 | tVERSION { $$ = make_var($<str>1); }
549 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
550 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
552 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
553 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
555 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
556 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
557 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
559 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
564 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
565 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
566 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
567 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
568 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
575 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
576 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
577 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
578 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
579 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
580 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
583 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
584 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
587 coclasshdr: attributes coclass { $$ = $2;
589 if (!parse_only) write_coclass($$);
593 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
598 coclass_ints: { $$ = NULL; }
599 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
603 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
606 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
607 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
610 dispinterfacehdr: attributes dispinterface { $$ = $2;
611 if ($$->defined) yyerror("multiple definition error\n");
613 $$->attrs = make_attr(ATTR_DISPINTERFACE);
615 $$->ref = find_type("IDispatch", 0);
616 if (!$$->ref) yyerror("IDispatch is undefined\n");
618 if (!parse_only) write_forward($$);
622 dispint_props: tPROPERTIES ':' { $$ = NULL; }
623 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
626 dispint_meths: tMETHODS ':' { $$ = NULL; }
627 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
630 dispinterfacedef: dispinterfacehdr '{'
636 if (!parse_only) write_dispinterface($$);
638 /* FIXME: not sure how to handle this yet
639 | dispinterfacehdr '{' interface '}' { $$ = $1;
640 if (!parse_only) write_interface($$);
645 inherit: { $$ = NULL; }
646 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
649 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
650 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
653 interfacehdr: attributes interface { $$ = $2;
654 if ($$->defined) yyerror("multiple definition error\n");
657 if (!parse_only) write_forward($$);
661 interfacedef: interfacehdr inherit
662 '{' int_statements '}' { $$ = $1;
665 if (!parse_only) write_interface($$);
667 /* MIDL is able to import the definition of a base class from inside the
668 * definition of a derived class, I'll try to support it with this rule */
669 | interfacehdr ':' aIDENTIFIER
670 '{' import int_statements '}' { $$ = $1;
671 $$->ref = find_type2($3, 0);
672 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
674 if (!parse_only) write_interface($$);
676 | dispinterfacedef { $$ = $1; }
680 interface ';' { $$ = $1; if (!parse_only) write_forward($$); }
681 | dispinterface ';' { $$ = $1; if (!parse_only) write_forward($$); }
684 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
685 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
688 modulehdr: attributes module { $$ = $2;
693 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
695 /* FIXME: if (!parse_only) write_module($$); */
699 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
700 | tCONST p_ident { $$ = $2; /* FIXME */ }
705 | '(' pident ')' { $$ = $2; }
710 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
714 tREF { $$ = RPC_FC_RP; }
715 | tUNIQUE { $$ = RPC_FC_UP; }
718 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
719 /* overwrite RPC_FC_STRUCT with a more exact type */
720 $$->type = get_struct_type( $4 );
728 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
729 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
730 | base_type { $$ = make_tref(NULL, $1); }
731 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
732 | enumdef { $$ = make_tref(NULL, $1); }
733 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
734 | structdef { $$ = make_tref(NULL, $1); }
735 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
736 | uniondef { $$ = make_tref(NULL, $1); }
737 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
740 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
741 $4->tname = tref->name;
745 if (!parse_only) write_typedef($$, $4);
746 reg_types($$, $4, 0);
750 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
755 tSWITCH '(' s_field ')'
756 m_ident '{' cases '}' { var_t *u = $7;
757 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
758 if (!u) u = make_var("tagged_union");
759 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
760 u->type->fields = $9;
761 u->type->defined = TRUE;
762 LINK(u, $5); $$->fields = u;
768 aNUM { $$ = MAKELONG($1, 0); }
769 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
774 static attr_t *make_attr(enum attr_type type)
776 attr_t *a = xmalloc(sizeof(attr_t));
783 static attr_t *make_attrv(enum attr_type type, unsigned long val)
785 attr_t *a = xmalloc(sizeof(attr_t));
792 static attr_t *make_attrp(enum attr_type type, void *val)
794 attr_t *a = xmalloc(sizeof(attr_t));
801 static expr_t *make_expr(enum expr_type type)
803 expr_t *e = xmalloc(sizeof(expr_t));
812 static expr_t *make_exprl(enum expr_type type, long val)
814 expr_t *e = xmalloc(sizeof(expr_t));
820 /* check for numeric constant */
821 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
828 static expr_t *make_exprs(enum expr_type type, char *val)
831 e = xmalloc(sizeof(expr_t));
837 /* check for predefined constants */
838 if (type == EXPR_IDENTIFIER) {
839 var_t *c = find_const(val, 0);
850 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
853 e = xmalloc(sizeof(expr_t));
859 /* check for cast of constant expression */
860 if (type == EXPR_CAST && expr->is_const) {
862 e->cval = expr->cval;
867 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
870 e = xmalloc(sizeof(expr_t));
876 /* check for compile-time optimization */
877 if (expr->is_const) {
881 e->cval = -expr->cval;
884 e->cval = ~expr->cval;
894 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
897 e = xmalloc(sizeof(expr_t));
903 /* check for compile-time optimization */
904 if (expr1->is_const && expr2->is_const) {
908 e->cval = expr1->cval + expr2->cval;
911 e->cval = expr1->cval - expr2->cval;
914 e->cval = expr1->cval * expr2->cval;
917 e->cval = expr1->cval / expr2->cval;
920 e->cval = expr1->cval | expr2->cval;
923 e->cval = expr1->cval & expr2->cval;
926 e->cval = expr1->cval << expr2->cval;
929 e->cval = expr1->cval >> expr2->cval;
939 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
942 e = xmalloc(sizeof(expr_t));
949 /* check for compile-time optimization */
950 if (expr1->is_const && expr2->is_const && expr3->is_const) {
954 e->cval = expr1->cval ? expr2->cval : expr3->cval;
964 static type_t *make_type(unsigned char type, type_t *ref)
966 type_t *t = xmalloc(sizeof(type_t));
974 t->ignore = parse_only;
984 static typeref_t *make_tref(char *name, type_t *ref)
986 typeref_t *t = xmalloc(sizeof(typeref_t));
989 t->uniq = ref ? 0 : 1;
993 static typeref_t *uniq_tref(typeref_t *ref)
997 if (t->uniq) return t;
998 tp = make_type(0, t->ref);
1006 static type_t *type_ref(typeref_t *ref)
1008 type_t *t = ref->ref;
1009 if (ref->name) free(ref->name);
1014 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
1017 v->tname = ref->name;
1023 static ifref_t *make_ifref(type_t *iface)
1025 ifref_t *l = xmalloc(sizeof(ifref_t));
1032 static var_t *make_var(char *name)
1034 var_t *v = xmalloc(sizeof(var_t));
1047 static func_t *make_func(var_t *def, var_t *args)
1049 func_t *f = xmalloc(sizeof(func_t));
1052 f->ignore = parse_only;
1058 static class_t *make_class(char *name)
1060 class_t *c = xmalloc(sizeof(class_t));
1070 static int hash_ident(const char *name)
1072 const char *p = name;
1074 /* a simple sum hash is probably good enough */
1079 return sum & (HASHMAX-1);
1082 /***** type repository *****/
1091 struct rtype *type_hash[HASHMAX];
1093 static type_t *reg_type(type_t *type, char *name, int t)
1098 yyerror("registering named type without name\n");
1101 hash = hash_ident(name);
1102 nt = xmalloc(sizeof(struct rtype));
1106 nt->next = type_hash[hash];
1107 type_hash[hash] = nt;
1111 /* determine pointer type from attrs */
1112 static unsigned char get_pointer_type( type_t *type )
1115 if (is_attr( type->attrs, ATTR_STRING ))
1118 while( t->type == 0 && t->ref )
1123 return RPC_FC_C_CSTRING;
1125 return RPC_FC_C_WSTRING;
1128 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1133 static type_t *reg_types(type_t *type, var_t *names, int t)
1139 var_t *next = NEXT_LINK(names);
1142 int cptr = names->ptr_level;
1144 while (cptr > ptrc) {
1145 int t = get_pointer_type( cur );
1146 cur = ptr = make_type(t, cur);
1150 while (cptr < ptrc) {
1155 reg_type(cur, names->name, t);
1163 static type_t *find_type(char *name, int t)
1165 struct rtype *cur = type_hash[hash_ident(name)];
1166 while (cur && (cur->t != t || strcmp(cur->name, name)))
1169 yyerror("type %s not found\n", name);
1175 static type_t *find_type2(char *name, int t)
1177 type_t *tp = find_type(name, t);
1182 int is_type(const char *name)
1184 struct rtype *cur = type_hash[hash_ident(name)];
1185 while (cur && (cur->t || strcmp(cur->name, name)))
1187 if (cur) return TRUE;
1191 static type_t *get_type(unsigned char type, char *name, int t)
1193 struct rtype *cur = NULL;
1196 cur = type_hash[hash_ident(name)];
1197 while (cur && (cur->t != t || strcmp(cur->name, name)))
1204 tp = make_type(type, NULL);
1206 if (!name) return tp;
1207 return reg_type(tp, name, t);
1210 static type_t *get_typev(unsigned char type, var_t *name, int t)
1217 return get_type(type, sname, t);
1220 static int get_struct_type(var_t *field)
1222 int has_pointer = 0;
1223 int has_conformant_array = 0;
1224 int has_conformant_string = 0;
1228 type_t *t = field->type;
1230 /* get the base type */
1231 while( (t->type == 0) && t->ref )
1237 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1238 * Simple types don't effect the type of struct.
1239 * A struct containing a simple struct is still a simple struct.
1240 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1242 case 0: /* void pointer */
1252 case RPC_FC_INT3264:
1253 case RPC_FC_UINT3264:
1267 has_conformant_array = 1;
1269 case RPC_FC_C_CSTRING:
1270 case RPC_FC_C_WSTRING:
1271 has_conformant_string = 1;
1275 * Propagate member attributes
1276 * a struct should be at least as complex as its member
1278 case RPC_FC_CVSTRUCT:
1279 has_conformant_string = 1;
1283 case RPC_FC_CPSTRUCT:
1284 has_conformant_array = 1;
1288 case RPC_FC_CSTRUCT:
1289 has_conformant_array = 1;
1292 case RPC_FC_PSTRUCT:
1297 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1298 field->name, t->type);
1299 /* fallthru - treat it as complex */
1301 /* as soon as we see one of these these members, it's bogus... */
1303 case RPC_FC_ENCAPSULATED_UNION:
1304 case RPC_FC_NON_ENCAPSULATED_UNION:
1305 case RPC_FC_TRANSMIT_AS:
1306 case RPC_FC_REPRESENT_AS:
1308 case RPC_FC_EMBEDDED_COMPLEX:
1309 case RPC_FC_BOGUS_STRUCT:
1310 return RPC_FC_BOGUS_STRUCT;
1312 field = NEXT_LINK(field);
1315 if( has_conformant_string && has_pointer )
1316 return RPC_FC_CVSTRUCT;
1317 if( has_conformant_array && has_pointer )
1318 return RPC_FC_CPSTRUCT;
1319 if( has_conformant_array )
1320 return RPC_FC_CSTRUCT;
1322 return RPC_FC_PSTRUCT;
1323 return RPC_FC_STRUCT;
1326 /***** constant repository *****/
1331 struct rconst *next;
1334 struct rconst *const_hash[HASHMAX];
1336 static var_t *reg_const(var_t *var)
1341 yyerror("registering constant without name\n");
1344 hash = hash_ident(var->name);
1345 nc = xmalloc(sizeof(struct rconst));
1346 nc->name = var->name;
1348 nc->next = const_hash[hash];
1349 const_hash[hash] = nc;
1353 static var_t *find_const(char *name, int f)
1355 struct rconst *cur = const_hash[hash_ident(name)];
1356 while (cur && strcmp(cur->name, name))
1359 if (f) yyerror("constant %s not found\n", name);