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);
92 static int get_struct_type(var_t *fields);
94 static var_t *reg_const(var_t *var);
95 static var_t *find_const(char *name, int f);
101 static type_t std_bool = { "boolean" };
102 static type_t std_int = { "int" };
103 static type_t std_int64 = { "__int64" };
104 static type_t std_uhyper = { "MIDL_uhyper" };
121 %token <str> aIDENTIFIER
122 %token <str> aKNOWNTYPE
123 %token <num> aNUM aHEXNUM
128 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tARRAYS tASYNC tASYNCUUID
129 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
130 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
131 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
132 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
134 %token tDISPINTERFACE
135 %token tDLLNAME tDOUBLE tDUAL
137 %token tENTRY tENUM tERRORSTATUST
143 %token tHYPER tID tIDEMPOTENT
145 %token tIMPORT tIMPORTLIB
146 %token tIN tINCLUDE tINLINE
150 %token tLENGTHIS tLIBRARY
155 %token tOBJECT tODL tOLEAUTOMATION
157 %token tPOINTERDEFAULT
159 %token tPROPGET tPROPPUT
161 %token tREADONLY tREF
165 %token tSIZEIS tSIZEOF
168 %token tSTRING tSTRUCT
169 %token tSWITCH tSWITCHIS tSWITCHTYPE
179 %token tWCHAR tWIREMARSHAL
184 %type <attr> m_attributes attributes attrib_list attribute
185 %type <expr> m_exprs /* exprs expr_list */ m_expr expr expr_list_const expr_const
186 %type <expr> array array_list
187 %type <type> inherit interface interfacehdr interfacedef interfacedec
188 %type <type> dispinterface dispinterfacehdr dispinterfacedef
189 %type <type> module modulehdr moduledef
190 %type <type> base_type int_std
191 %type <type> enumdef structdef typedef uniondef
192 %type <ifref> gbl_statements coclass_ints coclass_int
194 %type <var> m_args no_args args arg
195 %type <var> fields field s_field cases case enums enum_list enum constdef externdef
196 %type <var> m_ident t_ident ident p_ident pident pident_list
197 %type <var> dispint_props
198 %type <func> funcdef int_statements
199 %type <func> dispint_meths
200 %type <clas> coclass coclasshdr coclassdef
201 %type <num> pointer_type version
202 %type <str> libraryhdr
217 input: gbl_statements { write_proxies($1); }
220 gbl_statements: { $$ = NULL; }
221 | gbl_statements interfacedec { $$ = $1; }
222 | gbl_statements interfacedef { $$ = make_ifref($2); LINK($$, $1); }
223 | gbl_statements coclassdef { $$ = $1; add_coclass($2); }
224 | gbl_statements moduledef { $$ = $1; add_module($2); }
225 | gbl_statements librarydef { $$ = $1; }
226 | gbl_statements statement { $$ = $1; }
230 | imp_statements interfacedec { if (!parse_only) add_interface($2); }
231 | imp_statements interfacedef { if (!parse_only) add_interface($2); }
232 | imp_statements coclassdef { if (!parse_only) add_coclass($2); }
233 | imp_statements moduledef { if (!parse_only) add_module($2); }
234 | imp_statements statement {}
237 int_statements: { $$ = NULL; }
238 | int_statements funcdef ';' { $$ = $2; LINK($$, $1); }
239 | int_statements statement { $$ = $1; }
243 | constdef ';' { if (!parse_only) { write_constdef($1); } }
245 | enumdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
246 | externdef ';' { if (!parse_only) { write_externdef($1); } }
248 | structdef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
250 | uniondef ';' { if (!parse_only) { write_type(header, $1, NULL, NULL); fprintf(header, ";\n\n"); } }
253 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only) fprintf(header, "%s\n", $3); }
255 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
256 if (!do_import($2)) yychar = aEOF; }
258 import: import_start imp_statements aEOF {}
261 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
263 library_start: attributes libraryhdr '{' { start_typelib($2, $1); }
265 librarydef: library_start imp_statements '}' { end_typelib(); }
268 m_args: { $$ = NULL; }
272 no_args: tVOID { $$ = NULL; }
276 | args ',' arg { LINK($3, $1); $$ = $3; }
280 /* split into two rules to get bison to resolve a tVOID conflict */
281 arg: attributes type pident array { $$ = $3;
282 set_type($$, $2, $4);
284 if (!is_attr($$->attrs, ATTR_OUT) &&
285 !is_attr($$->attrs, ATTR_IN)) {
286 attr_t *a = make_attr(ATTR_IN);
287 LINK(a, $$->attrs); $$->attrs = a;
290 | type pident array { $$ = $2;
291 set_type($$, $1, $3);
292 $$->attrs = make_attr(ATTR_IN);
294 | attributes type pident '(' m_args ')' { $$ = $3;
296 set_type($$, $2, NULL);
299 if (!is_attr($$->attrs, ATTR_OUT) &&
300 !is_attr($$->attrs, ATTR_IN)) {
301 attr_t *a = make_attr(ATTR_IN);
302 LINK(a, $$->attrs); $$->attrs = a;
305 | type pident '(' m_args ')' { $$ = $2;
307 set_type($$, $1, NULL);
308 $$->attrs = make_attr(ATTR_IN);
313 array: { $$ = NULL; }
314 | '[' array_list ']' { $$ = $2; }
315 | '[' '*' ']' { $$ = make_expr(EXPR_VOID); }
318 array_list: m_expr /* size of first dimension is optional */
319 | array_list ',' expr { LINK($3, $1); $$ = $3; }
320 | array_list ']' '[' expr { LINK($4, $1); $$ = $4; }
323 m_attributes: { $$ = NULL; }
328 '[' attrib_list ']' { $$ = $2; }
331 attrib_list: attribute
332 | attrib_list ',' attribute { LINK($3, $1); $$ = $3; }
333 | attrib_list ']' '[' attribute { LINK($4, $1); $$ = $4; }
337 tASYNC { $$ = make_attr(ATTR_ASYNC); }
338 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
339 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
340 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
341 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
342 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
343 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
344 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
345 | tDUAL { $$ = make_attr(ATTR_DUAL); }
346 | tENDPOINT '(' aSTRING ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
347 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
348 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
349 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
350 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
351 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
352 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
353 | tIIDIS '(' ident ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
354 | tIN { $$ = make_attr(ATTR_IN); }
355 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
356 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
357 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
358 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
359 | tODL { $$ = make_attr(ATTR_ODL); }
360 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
361 | tOUT { $$ = make_attr(ATTR_OUT); }
362 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
363 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
364 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
365 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
366 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
367 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
368 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
369 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
370 | tSTRING { $$ = make_attr(ATTR_STRING); }
371 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
372 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, type_ref($3)); }
373 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, type_ref($3)); }
374 | tUUID '(' aUUID ')' { $$ = make_attrp(ATTR_UUID, $3); }
375 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
376 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
377 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, type_ref($3)); }
378 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
385 cases: { $$ = NULL; }
386 | cases case { if ($2) { LINK($2, $1); $$ = $2; }
391 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, $2);
392 $$ = $4; if (!$$) $$ = make_var(NULL);
393 LINK(a, $$->attrs); $$->attrs = a;
395 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
396 $$ = $3; if (!$$) $$ = make_var(NULL);
397 LINK(a, $$->attrs); $$->attrs = a;
401 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
402 set_type($$, $2, NULL);
408 enums: { $$ = NULL; }
409 | enum_list ',' { $$ = $1; }
414 | enum_list ',' enum { LINK($3, $1); $$ = $3;
416 $3->lval = $1->lval + 1;
420 enum: ident '=' expr_const { $$ = reg_const($1);
424 | ident { $$ = reg_const($1);
425 $$->lval = 0; /* default for first enum entry */
429 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
436 | m_exprs ',' m_expr { LINK($3, $1); $$ = $3; }
440 exprs: { $$ = make_expr(EXPR_VOID); }
445 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
449 m_expr: { $$ = make_expr(EXPR_VOID); }
453 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
454 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
455 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
456 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
457 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
458 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
459 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
460 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
461 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
462 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
463 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
464 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
465 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
466 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
467 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
468 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
469 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
470 | '(' expr ')' { $$ = $2; }
473 expr_list_const: expr_const
474 | expr_list_const ',' expr_const { LINK($3, $1); $$ = $3; }
477 expr_const: expr { $$ = $1;
478 if (!$$->is_const) yyerror("expression is not constant\n");
482 externdef: tEXTERN tCONST type ident { $$ = $4;
483 set_type($$, $3, NULL);
487 fields: { $$ = NULL; }
488 | fields field { if ($2) { LINK($2, $1); $$ = $2; }
493 field: s_field ';' { $$ = $1; }
494 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
495 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
499 s_field: m_attributes type pident array { $$ = $3; set_type($$, $2, $4); $$->attrs = $1; }
503 m_attributes type callconv pident
504 '(' m_args ')' { set_type($4, $2, NULL);
506 $$ = make_func($4, $6);
507 if (is_attr($4->attrs, ATTR_IN)) {
508 yyerror("Inapplicable attribute");
510 if (!is_attr($4->attrs, ATTR_OUT)) {
511 attr_t *a = make_attr(ATTR_OUT);
512 LINK(a, $4->attrs); $4->attrs = a;
517 m_ident: { $$ = NULL; }
521 t_ident: { $$ = NULL; }
522 | aIDENTIFIER { $$ = make_var($1); }
523 | aKNOWNTYPE { $$ = make_var($1); }
526 ident: aIDENTIFIER { $$ = make_var($1); }
527 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
528 | aKNOWNTYPE { $$ = make_var($<str>1); }
529 | tID { $$ = make_var($<str>1); }
530 | tRETVAL { $$ = make_var($<str>1); }
531 | tVERSION { $$ = make_var($<str>1); }
534 base_type: tBYTE { $$ = make_type(RPC_FC_BYTE, NULL); }
535 | tWCHAR { $$ = make_type(RPC_FC_WCHAR, NULL); }
537 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
538 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
540 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
541 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
542 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
544 if (!$$->ref) { $$->ref = &std_uhyper; $$->sign = 0; }
549 | tFLOAT { $$ = make_type(RPC_FC_FLOAT, NULL); }
550 | tDOUBLE { $$ = make_type(RPC_FC_DOUBLE, NULL); }
551 | tBOOLEAN { $$ = make_type(RPC_FC_BYTE, &std_bool); /* ? */ }
552 | tERRORSTATUST { $$ = make_type(RPC_FC_ERROR_STATUS_T, NULL); }
553 | tHANDLET { $$ = make_type(RPC_FC_BIND_PRIMITIVE, NULL); /* ? */ }
560 int_std: tINT { $$ = make_type(RPC_FC_LONG, &std_int); } /* win32 only */
561 | tSHORT m_int { $$ = make_type(RPC_FC_SHORT, NULL); }
562 | tLONG m_int { $$ = make_type(RPC_FC_LONG, NULL); }
563 | tHYPER m_int { $$ = make_type(RPC_FC_HYPER, NULL); }
564 | tINT64 { $$ = make_type(RPC_FC_HYPER, &std_int64); }
565 | tCHAR { $$ = make_type(RPC_FC_CHAR, NULL); }
568 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
569 | tCOCLASS aKNOWNTYPE { $$ = make_class($2); }
572 coclasshdr: attributes coclass { $$ = $2;
574 if (!parse_only) write_coclass($$);
578 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
583 coclass_ints: { $$ = NULL; }
584 | coclass_ints coclass_int { LINK($2, $1); $$ = $2; }
588 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
591 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); }
592 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); }
595 dispinterfacehdr: attributes dispinterface { $$ = $2;
596 if ($$->defined) yyerror("multiple definition error\n");
598 $$->attrs = make_attr(ATTR_DISPINTERFACE);
599 /* LINK($$->attrs, $1); */
600 $$->ref = find_type("IDispatch", 0);
601 if (!$$->ref) yyerror("IDispatch is undefined\n");
603 if (!parse_only) write_forward($$);
607 dispint_props: tPROPERTIES ':' { $$ = NULL; }
608 | dispint_props s_field ';' { LINK($2, $1); $$ = $2; }
611 dispint_meths: tMETHODS ':' { $$ = NULL; }
612 | dispint_meths funcdef ';' { LINK($2, $1); $$ = $2; }
615 dispinterfacedef: dispinterfacehdr '{'
621 if (!parse_only) write_dispinterface($$);
623 /* FIXME: not sure how to handle this yet
624 | dispinterfacehdr '{' interface '}' { $$ = $1;
625 if (!parse_only) write_interface($$);
630 inherit: { $$ = NULL; }
631 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
634 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); }
635 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); }
638 interfacehdr: attributes interface { $$ = $2;
639 if ($$->defined) yyerror("multiple definition error\n");
642 if (!parse_only) write_forward($$);
646 interfacedef: interfacehdr inherit
647 '{' int_statements '}' { $$ = $1;
650 if (!parse_only) write_interface($$);
652 /* MIDL is able to import the definition of a base class from inside the
653 * definition of a derived class, I'll try to support it with this rule */
654 | interfacehdr ':' aIDENTIFIER
655 '{' import int_statements '}' { $$ = $1;
656 $$->ref = find_type2($3, 0);
657 if (!$$->ref) yyerror("base class %s not found in import\n", $3);
659 if (!parse_only) write_interface($$);
661 | dispinterfacedef { $$ = $1; }
665 interface ';' { $$ = $1; if (!parse_only) write_forward($$); }
666 | dispinterface ';' { $$ = $1; if (!parse_only) write_forward($$); }
669 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; }
670 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; }
673 modulehdr: attributes module { $$ = $2;
678 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
680 /* FIXME: if (!parse_only) write_module($$); */
684 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
685 | tCONST p_ident { $$ = $2; /* FIXME */ }
690 | '(' pident ')' { $$ = $2; }
695 | pident_list ',' pident { LINK($3, $1); $$ = $3; }
699 tREF { $$ = RPC_FC_RP; }
700 | tUNIQUE { $$ = RPC_FC_UP; }
703 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
704 /* overwrite RPC_FC_STRUCT with a more exact type */
705 $$->type = get_struct_type( $4 );
711 type: tVOID { $$ = make_tref(NULL, make_type(0, NULL)); }
712 | aKNOWNTYPE { $$ = make_tref($1, find_type($1, 0)); }
713 | base_type { $$ = make_tref(NULL, $1); }
714 | tCONST type { $$ = uniq_tref($2); $$->ref->is_const = TRUE; }
715 | enumdef { $$ = make_tref(NULL, $1); }
716 | tENUM aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsENUM)); }
717 | structdef { $$ = make_tref(NULL, $1); }
718 | tSTRUCT aIDENTIFIER { $$ = make_tref(NULL, get_type(RPC_FC_STRUCT, $2, tsSTRUCT)); }
719 | uniondef { $$ = make_tref(NULL, $1); }
720 | tUNION aIDENTIFIER { $$ = make_tref(NULL, find_type2($2, tsUNION)); }
723 typedef: tTYPEDEF m_attributes type pident_list { typeref_t *tref = uniq_tref($3);
724 $4->tname = tref->name;
728 if (!parse_only) write_typedef($$, $4);
729 reg_types($$, $4, 0);
733 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
738 tSWITCH '(' s_field ')'
739 m_ident '{' cases '}' { var_t *u = $7;
740 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
741 if (!u) u = make_var("tagged_union");
742 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
743 u->type->fields = $9;
744 u->type->defined = TRUE;
745 LINK(u, $5); $$->fields = u;
751 aNUM { $$ = MAKELONG($1, 0); }
752 | aNUM '.' aNUM { $$ = MAKELONG($1, $3); }
757 static attr_t *make_attr(enum attr_type type)
759 attr_t *a = xmalloc(sizeof(attr_t));
766 static attr_t *make_attrv(enum attr_type type, unsigned long val)
768 attr_t *a = xmalloc(sizeof(attr_t));
775 static attr_t *make_attrp(enum attr_type type, void *val)
777 attr_t *a = xmalloc(sizeof(attr_t));
784 static expr_t *make_expr(enum expr_type type)
786 expr_t *e = xmalloc(sizeof(expr_t));
795 static expr_t *make_exprl(enum expr_type type, long val)
797 expr_t *e = xmalloc(sizeof(expr_t));
803 /* check for numeric constant */
804 if (type == EXPR_NUM || type == EXPR_HEXNUM) {
811 static expr_t *make_exprs(enum expr_type type, char *val)
814 e = xmalloc(sizeof(expr_t));
820 /* check for predefined constants */
821 if (type == EXPR_IDENTIFIER) {
822 var_t *c = find_const(val, 0);
833 static expr_t *make_exprt(enum expr_type type, typeref_t *tref, expr_t *expr)
836 e = xmalloc(sizeof(expr_t));
842 /* check for cast of constant expression */
843 if (type == EXPR_CAST && expr->is_const) {
845 e->cval = expr->cval;
850 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
853 e = xmalloc(sizeof(expr_t));
859 /* check for compile-time optimization */
860 if (expr->is_const) {
864 e->cval = -expr->cval;
867 e->cval = ~expr->cval;
877 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
880 e = xmalloc(sizeof(expr_t));
886 /* check for compile-time optimization */
887 if (expr1->is_const && expr2->is_const) {
891 e->cval = expr1->cval + expr2->cval;
894 e->cval = expr1->cval - expr2->cval;
897 e->cval = expr1->cval * expr2->cval;
900 e->cval = expr1->cval / expr2->cval;
903 e->cval = expr1->cval | expr2->cval;
906 e->cval = expr1->cval & expr2->cval;
909 e->cval = expr1->cval << expr2->cval;
912 e->cval = expr1->cval >> expr2->cval;
922 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
925 e = xmalloc(sizeof(expr_t));
932 /* check for compile-time optimization */
933 if (expr1->is_const && expr2->is_const && expr3->is_const) {
937 e->cval = expr1->cval ? expr2->cval : expr3->cval;
947 static type_t *make_type(unsigned char type, type_t *ref)
949 type_t *t = xmalloc(sizeof(type_t));
957 t->ignore = parse_only;
966 static typeref_t *make_tref(char *name, type_t *ref)
968 typeref_t *t = xmalloc(sizeof(typeref_t));
971 t->uniq = ref ? 0 : 1;
975 static typeref_t *uniq_tref(typeref_t *ref)
979 if (t->uniq) return t;
980 tp = make_type(0, t->ref);
988 static type_t *type_ref(typeref_t *ref)
990 type_t *t = ref->ref;
991 if (ref->name) free(ref->name);
996 static void set_type(var_t *v, typeref_t *ref, expr_t *arr)
999 v->tname = ref->name;
1005 static ifref_t *make_ifref(type_t *iface)
1007 ifref_t *l = xmalloc(sizeof(ifref_t));
1014 static var_t *make_var(char *name)
1016 var_t *v = xmalloc(sizeof(var_t));
1029 static func_t *make_func(var_t *def, var_t *args)
1031 func_t *f = xmalloc(sizeof(func_t));
1034 f->ignore = parse_only;
1040 static class_t *make_class(char *name)
1042 class_t *c = xmalloc(sizeof(class_t));
1052 static int hash_ident(const char *name)
1054 const char *p = name;
1056 /* a simple sum hash is probably good enough */
1061 return sum & (HASHMAX-1);
1064 /***** type repository *****/
1073 struct rtype *type_hash[HASHMAX];
1075 static type_t *reg_type(type_t *type, char *name, int t)
1080 yyerror("registering named type without name\n");
1083 hash = hash_ident(name);
1084 nt = xmalloc(sizeof(struct rtype));
1088 nt->next = type_hash[hash];
1089 type_hash[hash] = nt;
1093 /* determine pointer type from attrs */
1094 static unsigned char get_pointer_type( type_t *type )
1097 if (is_attr( type->attrs, ATTR_STRING ))
1100 while( t->type == 0 && t->ref )
1105 return RPC_FC_C_CSTRING;
1107 return RPC_FC_C_WSTRING;
1110 t = get_attrv( type->attrs, ATTR_POINTERTYPE );
1115 static type_t *reg_types(type_t *type, var_t *names, int t)
1121 var_t *next = NEXT_LINK(names);
1124 int cptr = names->ptr_level;
1126 while (cptr > ptrc) {
1127 int t = get_pointer_type( cur );
1128 cur = ptr = make_type(t, cur);
1132 while (cptr < ptrc) {
1137 reg_type(cur, names->name, t);
1145 static type_t *find_type(char *name, int t)
1147 struct rtype *cur = type_hash[hash_ident(name)];
1148 while (cur && (cur->t != t || strcmp(cur->name, name)))
1151 yyerror("type %s not found\n", name);
1157 static type_t *find_type2(char *name, int t)
1159 type_t *tp = find_type(name, t);
1164 int is_type(const char *name)
1166 struct rtype *cur = type_hash[hash_ident(name)];
1167 while (cur && (cur->t || strcmp(cur->name, name)))
1169 if (cur) return TRUE;
1173 static type_t *get_type(unsigned char type, char *name, int t)
1175 struct rtype *cur = NULL;
1178 cur = type_hash[hash_ident(name)];
1179 while (cur && (cur->t != t || strcmp(cur->name, name)))
1186 tp = make_type(type, NULL);
1188 if (!name) return tp;
1189 return reg_type(tp, name, t);
1192 static type_t *get_typev(unsigned char type, var_t *name, int t)
1199 return get_type(type, sname, t);
1202 static int get_struct_type(var_t *field)
1204 int has_pointer = 0;
1205 int has_conformant_array = 0;
1206 int has_conformant_string = 0;
1210 type_t *t = field->type;
1212 /* get the base type */
1213 while( (t->type == 0) && t->ref )
1219 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1220 * Simple types don't effect the type of struct.
1221 * A struct containing a simple struct is still a simple struct.
1222 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1224 case 0: /* void pointer */
1234 case RPC_FC_INT3264:
1235 case RPC_FC_UINT3264:
1249 has_conformant_array = 1;
1251 case RPC_FC_C_CSTRING:
1252 case RPC_FC_C_WSTRING:
1253 has_conformant_string = 1;
1257 * Propagate member attributes
1258 * a struct should be at least as complex as its member
1260 case RPC_FC_CVSTRUCT:
1261 has_conformant_string = 1;
1265 case RPC_FC_CPSTRUCT:
1266 has_conformant_array = 1;
1270 case RPC_FC_CSTRUCT:
1271 has_conformant_array = 1;
1274 case RPC_FC_PSTRUCT:
1279 fprintf(stderr,"Unknown struct member %s with type (0x%02x)\n",
1280 field->name, t->type);
1281 /* fallthru - treat it as complex */
1283 /* as soon as we see one of these these members, it's bogus... */
1285 case RPC_FC_ENCAPSULATED_UNION:
1286 case RPC_FC_NON_ENCAPSULATED_UNION:
1287 case RPC_FC_TRANSMIT_AS:
1288 case RPC_FC_REPRESENT_AS:
1290 case RPC_FC_EMBEDDED_COMPLEX:
1291 case RPC_FC_BOGUS_STRUCT:
1292 return RPC_FC_BOGUS_STRUCT;
1294 field = NEXT_LINK(field);
1297 if( has_conformant_string && has_pointer )
1298 return RPC_FC_CVSTRUCT;
1299 if( has_conformant_array && has_pointer )
1300 return RPC_FC_CPSTRUCT;
1301 if( has_conformant_array )
1302 return RPC_FC_CSTRUCT;
1304 return RPC_FC_PSTRUCT;
1305 return RPC_FC_STRUCT;
1308 /***** constant repository *****/
1313 struct rconst *next;
1316 struct rconst *const_hash[HASHMAX];
1318 static var_t *reg_const(var_t *var)
1323 yyerror("registering constant without name\n");
1326 hash = hash_ident(var->name);
1327 nc = xmalloc(sizeof(struct rconst));
1328 nc->name = var->name;
1330 nc->next = const_hash[hash];
1331 const_hash[hash] = nc;
1335 static var_t *find_const(char *name, int f)
1337 struct rconst *cur = const_hash[hash_ident(name)];
1338 while (cur && strcmp(cur->name, name))
1341 if (f) yyerror("constant %s not found\n", name);