push 071493016fe1a315c0eb45bb83b2bbe9c1381c65
[wine/hacks.git] / tools / widl / parser.y
bloba4f383fe1cd6ae3fd3695533fab71b2022230978
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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"
38 #include "typelib.h"
39 #include "typegen.h"
41 #if defined(YYBYACC)
42 /* Berkeley yacc (byacc) doesn't seem to know about these */
43 /* Some *BSD supplied versions do define these though */
44 # ifndef YYEMPTY
45 # define YYEMPTY (-1) /* Empty lookahead value of yychar */
46 # endif
47 # ifndef YYLEX
48 # define YYLEX yylex()
49 # endif
51 #elif defined(YYBISON)
52 /* Bison was used for original development */
53 /* #define YYEMPTY -2 */
54 /* #define YYLEX yylex() */
56 #else
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... */
63 # endif
64 #endif
66 unsigned char pointer_default = RPC_FC_UP;
68 typedef struct list typelist_t;
69 struct typenode {
70 type_t *type;
71 struct list entry;
74 typelist_t incomplete_types = LIST_INIT(incomplete_types);
76 static void add_incomplete(type_t *t);
77 static void fix_incomplete(void);
79 static str_list_t *append_str(str_list_t *list, char *str);
80 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr);
81 static attr_t *make_attr(enum attr_type type);
82 static attr_t *make_attrv(enum attr_type type, unsigned long val);
83 static attr_t *make_attrp(enum attr_type type, void *val);
84 static expr_t *make_expr(enum expr_type type);
85 static expr_t *make_exprl(enum expr_type type, long val);
86 static expr_t *make_exprd(enum expr_type type, double val);
87 static expr_t *make_exprs(enum expr_type type, char *val);
88 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr);
89 static expr_t *make_expr1(enum expr_type type, expr_t *expr);
90 static expr_t *make_expr2(enum expr_type type, expr_t *exp1, expr_t *exp2);
91 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3);
92 static type_t *make_type(unsigned char type, type_t *ref);
93 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr);
94 static array_dims_t *append_array(array_dims_t *list, expr_t *expr);
95 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr, int top);
96 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface);
97 static ifref_t *make_ifref(type_t *iface);
98 static var_list_t *append_var(var_list_t *list, var_t *var);
99 static var_t *make_var(char *name);
100 static pident_list_t *append_pident(pident_list_t *list, pident_t *p);
101 static pident_t *make_pident(var_t *var);
102 static func_list_t *append_func(func_list_t *list, func_t *func);
103 static func_t *make_func(var_t *def, var_list_t *args);
104 static type_t *make_class(char *name);
105 static type_t *make_safearray(type_t *type);
106 static type_t *make_builtin(char *name);
107 static type_t *make_int(int sign);
109 static type_t *reg_type(type_t *type, const char *name, int t);
110 static type_t *reg_typedefs(type_t *type, var_list_t *names, attr_list_t *attrs);
111 static type_t *find_type(const char *name, int t);
112 static type_t *find_type2(char *name, int t);
113 static type_t *get_type(unsigned char type, char *name, int t);
114 static type_t *get_typev(unsigned char type, var_t *name, int t);
115 static int get_struct_type(var_list_t *fields);
117 static var_t *reg_const(var_t *var);
118 static var_t *find_const(char *name, int f);
120 static void write_libid(const char *name, const attr_list_t *attr);
121 static void write_clsid(type_t *cls);
122 static void write_diid(type_t *iface);
123 static void write_iid(type_t *iface);
125 static int compute_method_indexes(type_t *iface);
126 static char *gen_name(void);
127 static void process_typedefs(var_list_t *names);
128 static void check_arg(var_t *arg);
129 static void check_all_user_types(ifref_list_t *ifaces);
131 #define tsENUM 1
132 #define tsSTRUCT 2
133 #define tsUNION 3
136 %union {
137 attr_t *attr;
138 attr_list_t *attr_list;
139 str_list_t *str_list;
140 expr_t *expr;
141 expr_list_t *expr_list;
142 array_dims_t *array_dims;
143 type_t *type;
144 var_t *var;
145 var_list_t *var_list;
146 pident_t *pident;
147 pident_list_t *pident_list;
148 func_t *func;
149 func_list_t *func_list;
150 ifref_t *ifref;
151 ifref_list_t *ifref_list;
152 char *str;
153 UUID *uuid;
154 unsigned int num;
155 double dbl;
156 interface_info_t ifinfo;
159 %token <str> aIDENTIFIER
160 %token <str> aKNOWNTYPE
161 %token <num> aNUM aHEXNUM
162 %token <dbl> aDOUBLE
163 %token <str> aSTRING
164 %token <uuid> aUUID
165 %token aEOF
166 %token SHL SHR
167 %token tAGGREGATABLE tALLOCATE tAPPOBJECT tASYNC tASYNCUUID
168 %token tAUTOHANDLE tBINDABLE tBOOLEAN tBROADCAST tBYTE tBYTECOUNT
169 %token tCALLAS tCALLBACK tCASE tCDECL tCHAR tCOCLASS tCODE tCOMMSTATUS
170 %token tCONST tCONTEXTHANDLE tCONTEXTHANDLENOSERIALIZE
171 %token tCONTEXTHANDLESERIALIZE tCONTROL tCPPQUOTE
172 %token tDEFAULT
173 %token tDEFAULTCOLLELEM
174 %token tDEFAULTVALUE
175 %token tDEFAULTVTABLE
176 %token tDISPLAYBIND
177 %token tDISPINTERFACE
178 %token tDLLNAME tDOUBLE tDUAL
179 %token tENDPOINT
180 %token tENTRY tENUM tERRORSTATUST
181 %token tEXPLICITHANDLE tEXTERN
182 %token tFALSE
183 %token tFLOAT
184 %token tHANDLE
185 %token tHANDLET
186 %token tHELPCONTEXT tHELPFILE
187 %token tHELPSTRING tHELPSTRINGCONTEXT tHELPSTRINGDLL
188 %token tHIDDEN
189 %token tHYPER tID tIDEMPOTENT
190 %token tIIDIS
191 %token tIMMEDIATEBIND
192 %token tIMPLICITHANDLE
193 %token tIMPORT tIMPORTLIB
194 %token tIN tINLINE
195 %token tINPUTSYNC
196 %token tINT tINT64
197 %token tINTERFACE
198 %token tLCID
199 %token tLENGTHIS tLIBRARY
200 %token tLOCAL
201 %token tLONG
202 %token tMETHODS
203 %token tMODULE
204 %token tNONBROWSABLE
205 %token tNONCREATABLE
206 %token tNONEXTENSIBLE
207 %token tOBJECT tODL tOLEAUTOMATION
208 %token tOPTIONAL
209 %token tOUT
210 %token tPOINTERDEFAULT
211 %token tPROPERTIES
212 %token tPROPGET tPROPPUT tPROPPUTREF
213 %token tPTR
214 %token tPUBLIC
215 %token tRANGE
216 %token tREADONLY tREF
217 %token tREQUESTEDIT
218 %token tRESTRICTED
219 %token tRETVAL
220 %token tSAFEARRAY
221 %token tSHORT
222 %token tSIGNED
223 %token tSINGLE
224 %token tSIZEIS tSIZEOF
225 %token tSMALL
226 %token tSOURCE
227 %token tSTDCALL
228 %token tSTRICTCONTEXTHANDLE
229 %token tSTRING tSTRUCT
230 %token tSWITCH tSWITCHIS tSWITCHTYPE
231 %token tTRANSMITAS
232 %token tTRUE
233 %token tTYPEDEF
234 %token tUNION
235 %token tUNIQUE
236 %token tUNSIGNED
237 %token tUUID
238 %token tV1ENUM
239 %token tVARARG
240 %token tVERSION
241 %token tVOID
242 %token tWCHAR tWIREMARSHAL
244 %type <attr> attribute
245 %type <attr_list> m_attributes attributes attrib_list
246 %type <str_list> str_list
247 %type <expr> m_expr expr expr_const
248 %type <expr_list> m_exprs /* exprs expr_list */ expr_list_const
249 %type <array_dims> array array_list
250 %type <ifinfo> interfacehdr
251 %type <type> inherit interface interfacedef interfacedec
252 %type <type> dispinterface dispinterfacehdr dispinterfacedef
253 %type <type> module modulehdr moduledef
254 %type <type> base_type int_std
255 %type <type> enumdef structdef uniondef
256 %type <type> type
257 %type <ifref> coclass_int
258 %type <ifref_list> gbl_statements coclass_ints
259 %type <var> arg field s_field case enum constdef externdef
260 %type <var_list> m_args no_args args fields cases enums enum_list dispint_props
261 %type <var> m_ident t_ident ident
262 %type <pident> p_ident pident
263 %type <pident_list> pident_list
264 %type <func> funcdef
265 %type <func_list> int_statements dispint_meths
266 %type <type> coclass coclasshdr coclassdef
267 %type <num> pointer_type version
268 %type <str> libraryhdr
269 %type <uuid> uuid_string
271 %left ','
272 %right '?' ':'
273 %left '|'
274 %left '&'
275 %left '-' '+'
276 %left '*' '/'
277 %left SHL SHR
278 %right '~'
279 %right CAST
280 %right PPTR
281 %right NEG
282 %right ADDRESSOF
286 input: gbl_statements { fix_incomplete();
287 check_all_user_types($1);
288 write_proxies($1);
289 write_client($1);
290 write_server($1);
291 write_dlldata($1);
295 gbl_statements: { $$ = NULL; }
296 | gbl_statements interfacedec { $$ = $1; }
297 | gbl_statements interfacedef { $$ = append_ifref( $1, make_ifref($2) ); }
298 | gbl_statements coclass ';' { $$ = $1;
299 reg_type($2, $2->name, 0);
300 if (!parse_only && do_header) write_coclass_forward($2);
302 | gbl_statements coclassdef { $$ = $1;
303 add_typelib_entry($2);
304 reg_type($2, $2->name, 0);
305 if (!parse_only && do_header) write_coclass_forward($2);
307 | gbl_statements moduledef { $$ = $1; add_typelib_entry($2); }
308 | gbl_statements librarydef { $$ = $1; }
309 | gbl_statements statement { $$ = $1; }
312 imp_statements: {}
313 | imp_statements interfacedec { if (!parse_only) add_typelib_entry($2); }
314 | imp_statements interfacedef { if (!parse_only) add_typelib_entry($2); }
315 | imp_statements coclass ';' { reg_type($2, $2->name, 0); if (!parse_only && do_header) write_coclass_forward($2); }
316 | imp_statements coclassdef { if (!parse_only) add_typelib_entry($2);
317 reg_type($2, $2->name, 0);
318 if (!parse_only && do_header) write_coclass_forward($2);
320 | imp_statements moduledef { if (!parse_only) add_typelib_entry($2); }
321 | imp_statements statement {}
322 | imp_statements importlib {}
323 | imp_statements librarydef {}
326 int_statements: { $$ = NULL; }
327 | int_statements funcdef ';' { $$ = append_func( $1, $2 ); }
328 | int_statements statement { $$ = $1; }
331 statement: ';' {}
332 | constdef ';' { if (!parse_only && do_header) { write_constdef($1); } }
333 | cppquote {}
334 | enumdef ';' { if (!parse_only && do_header) {
335 write_type_def_or_decl(header, $1, FALSE, NULL);
336 fprintf(header, ";\n\n");
339 | externdef ';' { if (!parse_only && do_header) { write_externdef($1); } }
340 | import {}
341 | structdef ';' { if (!parse_only && do_header) {
342 write_type_def_or_decl(header, $1, FALSE, NULL);
343 fprintf(header, ";\n\n");
346 | typedef ';' {}
347 | uniondef ';' { if (!parse_only && do_header) {
348 write_type_def_or_decl(header, $1, FALSE, NULL);
349 fprintf(header, ";\n\n");
354 cppquote: tCPPQUOTE '(' aSTRING ')' { if (!parse_only && do_header) fprintf(header, "%s\n", $3); }
356 import_start: tIMPORT aSTRING ';' { assert(yychar == YYEMPTY);
357 if (!do_import($2)) yychar = aEOF; }
359 import: import_start imp_statements aEOF {}
362 importlib: tIMPORTLIB '(' aSTRING ')' { if(!parse_only) add_importlib($3); }
365 libraryhdr: tLIBRARY aIDENTIFIER { $$ = $2; }
367 library_start: attributes libraryhdr '{' { if (!parse_only) start_typelib($2, $1);
368 if (!parse_only && do_header) write_library($2, $1);
369 if (!parse_only && do_idfile) write_libid($2, $1);
372 librarydef: library_start imp_statements '}' { if (!parse_only) end_typelib(); }
375 m_args: { $$ = NULL; }
376 | args
379 no_args: tVOID { $$ = NULL; }
382 args: arg { check_arg($1); $$ = append_var( NULL, $1 ); }
383 | args ',' arg { check_arg($3); $$ = append_var( $1, $3); }
384 | no_args
387 /* split into two rules to get bison to resolve a tVOID conflict */
388 arg: attributes type pident array { $$ = $3->var;
389 $$->attrs = $1;
390 set_type($$, $2, $3->ptr_level, $4, TRUE);
391 free($3);
393 | type pident array { $$ = $2->var;
394 set_type($$, $1, $2->ptr_level, $3, TRUE);
395 free($2);
397 | attributes type pident '(' m_args ')' { $$ = $3->var;
398 $$->attrs = $1;
399 set_type($$, $2, $3->ptr_level - 1, NULL, TRUE);
400 free($3);
401 $$->args = $5;
403 | type pident '(' m_args ')' { $$ = $2->var;
404 set_type($$, $1, $2->ptr_level - 1, NULL, TRUE);
405 free($2);
406 $$->args = $4;
410 array: { $$ = NULL; }
411 | '[' array_list ']' { $$ = $2; }
412 | '[' '*' ']' { $$ = append_array( NULL, make_expr(EXPR_VOID) ); }
415 array_list: m_expr /* size of first dimension is optional */ { $$ = append_array( NULL, $1 ); }
416 | array_list ',' expr { $$ = append_array( $1, $3 ); }
417 | array_list ']' '[' expr { $$ = append_array( $1, $4 ); }
420 m_attributes: { $$ = NULL; }
421 | attributes
424 attributes:
425 '[' attrib_list ']' { $$ = $2;
426 if (!$$)
427 error_loc("empty attribute lists unsupported\n");
431 attrib_list: attribute { $$ = append_attr( NULL, $1 ); }
432 | attrib_list ',' attribute { $$ = append_attr( $1, $3 ); }
433 | attrib_list ']' '[' attribute { $$ = append_attr( $1, $4 ); }
436 str_list: aSTRING { $$ = append_str( NULL, $1 ); }
437 | str_list ',' aSTRING { $$ = append_str( $1, $3 ); }
440 attribute: { $$ = NULL; }
441 | tAGGREGATABLE { $$ = make_attr(ATTR_AGGREGATABLE); }
442 | tAPPOBJECT { $$ = make_attr(ATTR_APPOBJECT); }
443 | tASYNC { $$ = make_attr(ATTR_ASYNC); }
444 | tAUTOHANDLE { $$ = make_attr(ATTR_AUTO_HANDLE); }
445 | tBINDABLE { $$ = make_attr(ATTR_BINDABLE); }
446 | tCALLAS '(' ident ')' { $$ = make_attrp(ATTR_CALLAS, $3); }
447 | tCASE '(' expr_list_const ')' { $$ = make_attrp(ATTR_CASE, $3); }
448 | tCONTEXTHANDLE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); }
449 | tCONTEXTHANDLENOSERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_DONT_SERIALIZE */ }
450 | tCONTEXTHANDLESERIALIZE { $$ = make_attrv(ATTR_CONTEXTHANDLE, 0); /* RPC_CONTEXT_HANDLE_SERIALIZE */ }
451 | tCONTROL { $$ = make_attr(ATTR_CONTROL); }
452 | tDEFAULT { $$ = make_attr(ATTR_DEFAULT); }
453 | tDEFAULTCOLLELEM { $$ = make_attr(ATTR_DEFAULTCOLLELEM); }
454 | tDEFAULTVALUE '(' expr_const ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_EXPR, $3); }
455 | tDEFAULTVALUE '(' aSTRING ')' { $$ = make_attrp(ATTR_DEFAULTVALUE_STRING, $3); }
456 | tDEFAULTVTABLE { $$ = make_attr(ATTR_DEFAULTVTABLE); }
457 | tDISPLAYBIND { $$ = make_attr(ATTR_DISPLAYBIND); }
458 | tDLLNAME '(' aSTRING ')' { $$ = make_attrp(ATTR_DLLNAME, $3); }
459 | tDUAL { $$ = make_attr(ATTR_DUAL); }
460 | tENDPOINT '(' str_list ')' { $$ = make_attrp(ATTR_ENDPOINT, $3); }
461 | tENTRY '(' aSTRING ')' { $$ = make_attrp(ATTR_ENTRY_STRING, $3); }
462 | tENTRY '(' expr_const ')' { $$ = make_attrp(ATTR_ENTRY_ORDINAL, $3); }
463 | tEXPLICITHANDLE { $$ = make_attr(ATTR_EXPLICIT_HANDLE); }
464 | tHANDLE { $$ = make_attr(ATTR_HANDLE); }
465 | tHELPCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPCONTEXT, $3); }
466 | tHELPFILE '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPFILE, $3); }
467 | tHELPSTRING '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRING, $3); }
468 | tHELPSTRINGCONTEXT '(' expr_const ')' { $$ = make_attrp(ATTR_HELPSTRINGCONTEXT, $3); }
469 | tHELPSTRINGDLL '(' aSTRING ')' { $$ = make_attrp(ATTR_HELPSTRINGDLL, $3); }
470 | tHIDDEN { $$ = make_attr(ATTR_HIDDEN); }
471 | tID '(' expr_const ')' { $$ = make_attrp(ATTR_ID, $3); }
472 | tIDEMPOTENT { $$ = make_attr(ATTR_IDEMPOTENT); }
473 | tIIDIS '(' expr ')' { $$ = make_attrp(ATTR_IIDIS, $3); }
474 | tIMMEDIATEBIND { $$ = make_attr(ATTR_IMMEDIATEBIND); }
475 | tIMPLICITHANDLE '(' tHANDLET aIDENTIFIER ')' { $$ = make_attrp(ATTR_IMPLICIT_HANDLE, $4); }
476 | tIN { $$ = make_attr(ATTR_IN); }
477 | tINPUTSYNC { $$ = make_attr(ATTR_INPUTSYNC); }
478 | tLENGTHIS '(' m_exprs ')' { $$ = make_attrp(ATTR_LENGTHIS, $3); }
479 | tLOCAL { $$ = make_attr(ATTR_LOCAL); }
480 | tNONBROWSABLE { $$ = make_attr(ATTR_NONBROWSABLE); }
481 | tNONCREATABLE { $$ = make_attr(ATTR_NONCREATABLE); }
482 | tNONEXTENSIBLE { $$ = make_attr(ATTR_NONEXTENSIBLE); }
483 | tOBJECT { $$ = make_attr(ATTR_OBJECT); }
484 | tODL { $$ = make_attr(ATTR_ODL); }
485 | tOLEAUTOMATION { $$ = make_attr(ATTR_OLEAUTOMATION); }
486 | tOPTIONAL { $$ = make_attr(ATTR_OPTIONAL); }
487 | tOUT { $$ = make_attr(ATTR_OUT); }
488 | tPOINTERDEFAULT '(' pointer_type ')' { $$ = make_attrv(ATTR_POINTERDEFAULT, $3); }
489 | tPROPGET { $$ = make_attr(ATTR_PROPGET); }
490 | tPROPPUT { $$ = make_attr(ATTR_PROPPUT); }
491 | tPROPPUTREF { $$ = make_attr(ATTR_PROPPUTREF); }
492 | tPUBLIC { $$ = make_attr(ATTR_PUBLIC); }
493 | tRANGE '(' expr_const ',' expr_const ')' { expr_list_t *list = append_expr( NULL, $3 );
494 list = append_expr( list, $5 );
495 $$ = make_attrp(ATTR_RANGE, list); }
496 | tREADONLY { $$ = make_attr(ATTR_READONLY); }
497 | tREQUESTEDIT { $$ = make_attr(ATTR_REQUESTEDIT); }
498 | tRESTRICTED { $$ = make_attr(ATTR_RESTRICTED); }
499 | tRETVAL { $$ = make_attr(ATTR_RETVAL); }
500 | tSIZEIS '(' m_exprs ')' { $$ = make_attrp(ATTR_SIZEIS, $3); }
501 | tSOURCE { $$ = make_attr(ATTR_SOURCE); }
502 | tSTRICTCONTEXTHANDLE { $$ = make_attr(ATTR_STRICTCONTEXTHANDLE); }
503 | tSTRING { $$ = make_attr(ATTR_STRING); }
504 | tSWITCHIS '(' expr ')' { $$ = make_attrp(ATTR_SWITCHIS, $3); }
505 | tSWITCHTYPE '(' type ')' { $$ = make_attrp(ATTR_SWITCHTYPE, $3); }
506 | tTRANSMITAS '(' type ')' { $$ = make_attrp(ATTR_TRANSMITAS, $3); }
507 | tUUID '(' uuid_string ')' { $$ = make_attrp(ATTR_UUID, $3); }
508 | tV1ENUM { $$ = make_attr(ATTR_V1ENUM); }
509 | tVARARG { $$ = make_attr(ATTR_VARARG); }
510 | tVERSION '(' version ')' { $$ = make_attrv(ATTR_VERSION, $3); }
511 | tWIREMARSHAL '(' type ')' { $$ = make_attrp(ATTR_WIREMARSHAL, $3); }
512 | pointer_type { $$ = make_attrv(ATTR_POINTERTYPE, $1); }
515 uuid_string:
516 aUUID
517 | aSTRING { if (!is_valid_uuid($1))
518 error_loc("invalid UUID: %s\n", $1);
519 $$ = parse_uuid($1); }
522 callconv:
523 | tSTDCALL
526 cases: { $$ = NULL; }
527 | cases case { $$ = append_var( $1, $2 ); }
530 case: tCASE expr ':' field { attr_t *a = make_attrp(ATTR_CASE, append_expr( NULL, $2 ));
531 $$ = $4; if (!$$) $$ = make_var(NULL);
532 $$->attrs = append_attr( $$->attrs, a );
534 | tDEFAULT ':' field { attr_t *a = make_attr(ATTR_DEFAULT);
535 $$ = $3; if (!$$) $$ = make_var(NULL);
536 $$->attrs = append_attr( $$->attrs, a );
540 constdef: tCONST type ident '=' expr_const { $$ = reg_const($3);
541 set_type($$, $2, 0, NULL, FALSE);
542 $$->eval = $5;
546 enums: { $$ = NULL; }
547 | enum_list ',' { $$ = $1; }
548 | enum_list
551 enum_list: enum { if (!$1->eval)
552 $1->eval = make_exprl(EXPR_NUM, 0 /* default for first enum entry */);
553 $$ = append_var( NULL, $1 );
555 | enum_list ',' enum { if (!$3->eval)
557 var_t *last = LIST_ENTRY( list_tail($$), var_t, entry );
558 $3->eval = make_exprl(EXPR_NUM, last->eval->cval + 1);
560 $$ = append_var( $1, $3 );
564 enum: ident '=' expr_const { $$ = reg_const($1);
565 $$->eval = $3;
566 $$->type = make_int(0);
568 | ident { $$ = reg_const($1);
569 $$->type = make_int(0);
573 enumdef: tENUM t_ident '{' enums '}' { $$ = get_typev(RPC_FC_ENUM16, $2, tsENUM);
574 $$->kind = TKIND_ENUM;
575 $$->fields = $4;
576 $$->defined = TRUE;
577 if(in_typelib)
578 add_typelib_entry($$);
582 m_exprs: m_expr { $$ = append_expr( NULL, $1 ); }
583 | m_exprs ',' m_expr { $$ = append_expr( $1, $3 ); }
587 exprs: { $$ = make_expr(EXPR_VOID); }
588 | expr_list
591 expr_list: expr
592 | expr_list ',' expr { LINK($3, $1); $$ = $3; }
596 m_expr: { $$ = make_expr(EXPR_VOID); }
597 | expr
600 expr: aNUM { $$ = make_exprl(EXPR_NUM, $1); }
601 | aHEXNUM { $$ = make_exprl(EXPR_HEXNUM, $1); }
602 | aDOUBLE { $$ = make_exprd(EXPR_DOUBLE, $1); }
603 | tFALSE { $$ = make_exprl(EXPR_TRUEFALSE, 0); }
604 | tTRUE { $$ = make_exprl(EXPR_TRUEFALSE, 1); }
605 | aIDENTIFIER { $$ = make_exprs(EXPR_IDENTIFIER, $1); }
606 | expr '?' expr ':' expr { $$ = make_expr3(EXPR_COND, $1, $3, $5); }
607 | expr '|' expr { $$ = make_expr2(EXPR_OR , $1, $3); }
608 | expr '&' expr { $$ = make_expr2(EXPR_AND, $1, $3); }
609 | expr '+' expr { $$ = make_expr2(EXPR_ADD, $1, $3); }
610 | expr '-' expr { $$ = make_expr2(EXPR_SUB, $1, $3); }
611 | expr '*' expr { $$ = make_expr2(EXPR_MUL, $1, $3); }
612 | expr '/' expr { $$ = make_expr2(EXPR_DIV, $1, $3); }
613 | expr SHL expr { $$ = make_expr2(EXPR_SHL, $1, $3); }
614 | expr SHR expr { $$ = make_expr2(EXPR_SHR, $1, $3); }
615 | '~' expr { $$ = make_expr1(EXPR_NOT, $2); }
616 | '-' expr %prec NEG { $$ = make_expr1(EXPR_NEG, $2); }
617 | '&' expr %prec ADDRESSOF { $$ = make_expr1(EXPR_ADDRESSOF, $2); }
618 | '*' expr %prec PPTR { $$ = make_expr1(EXPR_PPTR, $2); }
619 | '(' type ')' expr %prec CAST { $$ = make_exprt(EXPR_CAST, $2, $4); }
620 | tSIZEOF '(' type ')' { $$ = make_exprt(EXPR_SIZEOF, $3, NULL); }
621 | '(' expr ')' { $$ = $2; }
624 expr_list_const: expr_const { $$ = append_expr( NULL, $1 ); }
625 | expr_list_const ',' expr_const { $$ = append_expr( $1, $3 ); }
628 expr_const: expr { $$ = $1;
629 if (!$$->is_const)
630 error_loc("expression is not constant\n");
634 externdef: tEXTERN tCONST type ident { $$ = $4;
635 set_type($$, $3, 0, NULL, FALSE);
639 fields: { $$ = NULL; }
640 | fields field { $$ = append_var( $1, $2 ); }
643 field: s_field ';' { $$ = $1; }
644 | m_attributes uniondef ';' { $$ = make_var(NULL); $$->type = $2; $$->attrs = $1; }
645 | attributes ';' { $$ = make_var(NULL); $$->attrs = $1; }
646 | ';' { $$ = NULL; }
649 s_field: m_attributes type pident array { $$ = $3->var;
650 $$->attrs = $1;
651 set_type($$, $2, $3->ptr_level, $4, FALSE);
652 free($3);
656 funcdef:
657 m_attributes type callconv pident
658 '(' m_args ')' { var_t *v = $4->var;
659 v->attrs = $1;
660 set_type(v, $2, $4->ptr_level, NULL, FALSE);
661 free($4);
662 $$ = make_func(v, $6);
663 if (is_attr(v->attrs, ATTR_IN)) {
664 error_loc("inapplicable attribute [in] for function '%s'\n",$$->def->name);
669 m_ident: { $$ = NULL; }
670 | ident
673 t_ident: { $$ = NULL; }
674 | aIDENTIFIER { $$ = make_var($1); }
675 | aKNOWNTYPE { $$ = make_var($1); }
678 ident: aIDENTIFIER { $$ = make_var($1); }
679 /* some "reserved words" used in attributes are also used as field names in some MS IDL files */
680 | aKNOWNTYPE { $$ = make_var($<str>1); }
683 base_type: tBYTE { $$ = make_builtin($<str>1); }
684 | tWCHAR { $$ = make_builtin($<str>1); }
685 | int_std
686 | tSIGNED int_std { $$ = $2; $$->sign = 1; }
687 | tUNSIGNED int_std { $$ = $2; $$->sign = -1;
688 switch ($$->type) {
689 case RPC_FC_CHAR: break;
690 case RPC_FC_SMALL: $$->type = RPC_FC_USMALL; break;
691 case RPC_FC_SHORT: $$->type = RPC_FC_USHORT; break;
692 case RPC_FC_LONG: $$->type = RPC_FC_ULONG; break;
693 case RPC_FC_HYPER:
694 if ($$->name[0] == 'h') /* hyper, as opposed to __int64 */
696 $$ = alias($$, "MIDL_uhyper");
697 $$->sign = 0;
699 break;
700 default: break;
703 | tUNSIGNED { $$ = make_int(-1); }
704 | tFLOAT { $$ = make_builtin($<str>1); }
705 | tSINGLE { $$ = duptype(find_type("float", 0), 1); }
706 | tDOUBLE { $$ = make_builtin($<str>1); }
707 | tBOOLEAN { $$ = make_builtin($<str>1); }
708 | tERRORSTATUST { $$ = make_builtin($<str>1); }
709 | tHANDLET { $$ = make_builtin($<str>1); }
712 m_int:
713 | tINT
716 int_std: tINT { $$ = make_builtin($<str>1); }
717 | tSHORT m_int { $$ = make_builtin($<str>1); }
718 | tSMALL { $$ = make_builtin($<str>1); }
719 | tLONG m_int { $$ = make_builtin($<str>1); }
720 | tHYPER m_int { $$ = make_builtin($<str>1); }
721 | tINT64 { $$ = make_builtin($<str>1); }
722 | tCHAR { $$ = make_builtin($<str>1); }
725 coclass: tCOCLASS aIDENTIFIER { $$ = make_class($2); }
726 | tCOCLASS aKNOWNTYPE { $$ = find_type($2, 0);
727 if ($$->defined) error_loc("multiple definition error\n");
728 if ($$->kind != TKIND_COCLASS) error_loc("%s was not declared a coclass\n", $2);
732 coclasshdr: attributes coclass { $$ = $2;
733 $$->attrs = $1;
734 if (!parse_only && do_header)
735 write_coclass($$);
736 if (!parse_only && do_idfile)
737 write_clsid($$);
741 coclassdef: coclasshdr '{' coclass_ints '}' { $$ = $1;
742 $$->ifaces = $3;
743 $$->defined = TRUE;
747 coclass_ints: { $$ = NULL; }
748 | coclass_ints coclass_int { $$ = append_ifref( $1, $2 ); }
751 coclass_int:
752 m_attributes interfacedec { $$ = make_ifref($2); $$->attrs = $1; }
755 dispinterface: tDISPINTERFACE aIDENTIFIER { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
756 | tDISPINTERFACE aKNOWNTYPE { $$ = get_type(0, $2, 0); $$->kind = TKIND_DISPATCH; }
759 dispinterfacehdr: attributes dispinterface { attr_t *attrs;
760 $$ = $2;
761 if ($$->defined) error_loc("multiple definition error\n");
762 attrs = make_attr(ATTR_DISPINTERFACE);
763 $$->attrs = append_attr( $1, attrs );
764 $$->ref = find_type("IDispatch", 0);
765 if (!$$->ref) error_loc("IDispatch is undefined\n");
766 $$->defined = TRUE;
767 if (!parse_only && do_header) write_forward($$);
771 dispint_props: tPROPERTIES ':' { $$ = NULL; }
772 | dispint_props s_field ';' { $$ = append_var( $1, $2 ); }
775 dispint_meths: tMETHODS ':' { $$ = NULL; }
776 | dispint_meths funcdef ';' { $$ = append_func( $1, $2 ); }
779 dispinterfacedef: dispinterfacehdr '{'
780 dispint_props
781 dispint_meths
782 '}' { $$ = $1;
783 $$->fields = $3;
784 $$->funcs = $4;
785 if (!parse_only && do_header) write_dispinterface($$);
786 if (!parse_only && do_idfile) write_diid($$);
788 | dispinterfacehdr
789 '{' interface ';' '}' { $$ = $1;
790 $$->fields = $3->fields;
791 $$->funcs = $3->funcs;
792 if (!parse_only && do_header) write_dispinterface($$);
793 if (!parse_only && do_idfile) write_diid($$);
797 inherit: { $$ = NULL; }
798 | ':' aKNOWNTYPE { $$ = find_type2($2, 0); }
801 interface: tINTERFACE aIDENTIFIER { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
802 | tINTERFACE aKNOWNTYPE { $$ = get_type(RPC_FC_IP, $2, 0); $$->kind = TKIND_INTERFACE; }
805 interfacehdr: attributes interface { $$.interface = $2;
806 $$.old_pointer_default = pointer_default;
807 if (is_attr($1, ATTR_POINTERDEFAULT))
808 pointer_default = get_attrv($1, ATTR_POINTERDEFAULT);
809 if ($2->defined) error_loc("multiple definition error\n");
810 $2->attrs = $1;
811 $2->defined = TRUE;
812 if (!parse_only && do_header) write_forward($2);
816 interfacedef: interfacehdr inherit
817 '{' int_statements '}' { $$ = $1.interface;
818 $$->ref = $2;
819 $$->funcs = $4;
820 compute_method_indexes($$);
821 if (!parse_only && do_header) write_interface($$);
822 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
823 if (!parse_only && do_idfile) write_iid($$);
824 pointer_default = $1.old_pointer_default;
826 /* MIDL is able to import the definition of a base class from inside the
827 * definition of a derived class, I'll try to support it with this rule */
828 | interfacehdr ':' aIDENTIFIER
829 '{' import int_statements '}' { $$ = $1.interface;
830 $$->ref = find_type2($3, 0);
831 if (!$$->ref) error_loc("base class '%s' not found in import\n", $3);
832 $$->funcs = $6;
833 compute_method_indexes($$);
834 if (!parse_only && do_header) write_interface($$);
835 if (!parse_only && local_stubs) write_locals(local_stubs, $$, TRUE);
836 if (!parse_only && do_idfile) write_iid($$);
837 pointer_default = $1.old_pointer_default;
839 | dispinterfacedef { $$ = $1; }
842 interfacedec:
843 interface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
844 | dispinterface ';' { $$ = $1; if (!parse_only && do_header) write_forward($$); }
847 module: tMODULE aIDENTIFIER { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
848 | tMODULE aKNOWNTYPE { $$ = make_type(0, NULL); $$->name = $2; $$->kind = TKIND_MODULE; }
851 modulehdr: attributes module { $$ = $2;
852 $$->attrs = $1;
856 moduledef: modulehdr '{' int_statements '}' { $$ = $1;
857 $$->funcs = $3;
858 /* FIXME: if (!parse_only && do_header) write_module($$); */
862 p_ident: '*' pident %prec PPTR { $$ = $2; $$->ptr_level++; }
863 | tCONST p_ident { $$ = $2; /* FIXME */ }
866 pident: ident { $$ = make_pident($1); }
867 | p_ident
868 | '(' pident ')' { $$ = $2; }
871 pident_list:
872 pident { $$ = append_pident( NULL, $1 ); }
873 | pident_list ',' pident { $$ = append_pident( $1, $3 ); }
876 pointer_type:
877 tREF { $$ = RPC_FC_RP; }
878 | tUNIQUE { $$ = RPC_FC_UP; }
879 | tPTR { $$ = RPC_FC_FP; }
882 structdef: tSTRUCT t_ident '{' fields '}' { $$ = get_typev(RPC_FC_STRUCT, $2, tsSTRUCT);
883 /* overwrite RPC_FC_STRUCT with a more exact type */
884 $$->type = get_struct_type( $4 );
885 $$->kind = TKIND_RECORD;
886 $$->fields = $4;
887 $$->defined = TRUE;
888 if(in_typelib)
889 add_typelib_entry($$);
893 type: tVOID { $$ = duptype(find_type("void", 0), 1); }
894 | aKNOWNTYPE { $$ = find_type($1, 0); }
895 | base_type { $$ = $1; }
896 | tCONST type { $$ = duptype($2, 1); $$->is_const = TRUE; }
897 | enumdef { $$ = $1; }
898 | tENUM aIDENTIFIER { $$ = find_type2($2, tsENUM); }
899 | structdef { $$ = $1; }
900 | tSTRUCT aIDENTIFIER { $$ = get_type(RPC_FC_STRUCT, $2, tsSTRUCT); }
901 | uniondef { $$ = $1; }
902 | tUNION aIDENTIFIER { $$ = find_type2($2, tsUNION); }
903 | tSAFEARRAY '(' type ')' { $$ = make_safearray($3); }
906 typedef: tTYPEDEF m_attributes type pident_list { reg_typedefs($3, $4, $2);
907 process_typedefs($4);
911 uniondef: tUNION t_ident '{' fields '}' { $$ = get_typev(RPC_FC_NON_ENCAPSULATED_UNION, $2, tsUNION);
912 $$->kind = TKIND_UNION;
913 $$->fields = $4;
914 $$->defined = TRUE;
916 | tUNION t_ident
917 tSWITCH '(' s_field ')'
918 m_ident '{' cases '}' { var_t *u = $7;
919 $$ = get_typev(RPC_FC_ENCAPSULATED_UNION, $2, tsUNION);
920 $$->kind = TKIND_UNION;
921 if (!u) u = make_var( xstrdup("tagged_union") );
922 u->type = make_type(RPC_FC_NON_ENCAPSULATED_UNION, NULL);
923 u->type->kind = TKIND_UNION;
924 u->type->fields = $9;
925 u->type->defined = TRUE;
926 $$->fields = append_var( $$->fields, $5 );
927 $$->fields = append_var( $$->fields, u );
928 $$->defined = TRUE;
932 version:
933 aNUM { $$ = MAKEVERSION($1, 0); }
934 | aNUM '.' aNUM { $$ = MAKEVERSION($1, $3); }
939 static void decl_builtin(const char *name, unsigned char type)
941 type_t *t = make_type(type, NULL);
942 t->name = xstrdup(name);
943 reg_type(t, name, 0);
946 static type_t *make_builtin(char *name)
948 /* NAME is strdup'd in the lexer */
949 type_t *t = duptype(find_type(name, 0), 0);
950 t->name = name;
951 return t;
954 static type_t *make_int(int sign)
956 type_t *t = duptype(find_type("int", 0), 1);
958 t->sign = sign;
959 if (sign < 0)
960 t->type = t->type == RPC_FC_LONG ? RPC_FC_ULONG : RPC_FC_USHORT;
962 return t;
965 void init_types(void)
967 decl_builtin("void", 0);
968 decl_builtin("byte", RPC_FC_BYTE);
969 decl_builtin("wchar_t", RPC_FC_WCHAR);
970 decl_builtin("int", RPC_FC_LONG); /* win32 */
971 decl_builtin("short", RPC_FC_SHORT);
972 decl_builtin("small", RPC_FC_SMALL);
973 decl_builtin("long", RPC_FC_LONG);
974 decl_builtin("hyper", RPC_FC_HYPER);
975 decl_builtin("__int64", RPC_FC_HYPER);
976 decl_builtin("char", RPC_FC_CHAR);
977 decl_builtin("float", RPC_FC_FLOAT);
978 decl_builtin("double", RPC_FC_DOUBLE);
979 decl_builtin("boolean", RPC_FC_BYTE);
980 decl_builtin("error_status_t", RPC_FC_ERROR_STATUS_T);
981 decl_builtin("handle_t", RPC_FC_BIND_PRIMITIVE);
984 static str_list_t *append_str(str_list_t *list, char *str)
986 struct str_list_entry_t *entry;
988 if (!str) return list;
989 if (!list)
991 list = xmalloc( sizeof(*list) );
992 list_init( list );
994 entry = xmalloc( sizeof(*entry) );
995 entry->str = str;
996 list_add_tail( list, &entry->entry );
997 return list;
1000 static attr_list_t *append_attr(attr_list_t *list, attr_t *attr)
1002 if (!attr) return list;
1003 if (!list)
1005 list = xmalloc( sizeof(*list) );
1006 list_init( list );
1008 list_add_tail( list, &attr->entry );
1009 return list;
1012 static attr_t *make_attr(enum attr_type type)
1014 attr_t *a = xmalloc(sizeof(attr_t));
1015 a->type = type;
1016 a->u.ival = 0;
1017 return a;
1020 static attr_t *make_attrv(enum attr_type type, unsigned long val)
1022 attr_t *a = xmalloc(sizeof(attr_t));
1023 a->type = type;
1024 a->u.ival = val;
1025 return a;
1028 static attr_t *make_attrp(enum attr_type type, void *val)
1030 attr_t *a = xmalloc(sizeof(attr_t));
1031 a->type = type;
1032 a->u.pval = val;
1033 return a;
1036 static expr_t *make_expr(enum expr_type type)
1038 expr_t *e = xmalloc(sizeof(expr_t));
1039 e->type = type;
1040 e->ref = NULL;
1041 e->u.lval = 0;
1042 e->is_const = FALSE;
1043 e->cval = 0;
1044 return e;
1047 static expr_t *make_exprl(enum expr_type type, long val)
1049 expr_t *e = xmalloc(sizeof(expr_t));
1050 e->type = type;
1051 e->ref = NULL;
1052 e->u.lval = val;
1053 e->is_const = FALSE;
1054 /* check for numeric constant */
1055 if (type == EXPR_NUM || type == EXPR_HEXNUM || type == EXPR_TRUEFALSE) {
1056 /* make sure true/false value is valid */
1057 assert(type != EXPR_TRUEFALSE || val == 0 || val == 1);
1058 e->is_const = TRUE;
1059 e->cval = val;
1061 return e;
1064 static expr_t *make_exprd(enum expr_type type, double val)
1066 expr_t *e = xmalloc(sizeof(expr_t));
1067 e->type = type;
1068 e->ref = NULL;
1069 e->u.dval = val;
1070 e->is_const = TRUE;
1071 e->cval = val;
1072 return e;
1075 static expr_t *make_exprs(enum expr_type type, char *val)
1077 expr_t *e;
1078 e = xmalloc(sizeof(expr_t));
1079 e->type = type;
1080 e->ref = NULL;
1081 e->u.sval = val;
1082 e->is_const = FALSE;
1083 /* check for predefined constants */
1084 if (type == EXPR_IDENTIFIER) {
1085 var_t *c = find_const(val, 0);
1086 if (c) {
1087 e->u.sval = c->name;
1088 free(val);
1089 e->is_const = TRUE;
1090 e->cval = c->eval->cval;
1093 return e;
1096 static expr_t *make_exprt(enum expr_type type, type_t *tref, expr_t *expr)
1098 expr_t *e;
1099 e = xmalloc(sizeof(expr_t));
1100 e->type = type;
1101 e->ref = expr;
1102 e->u.tref = tref;
1103 e->is_const = FALSE;
1104 /* check for cast of constant expression */
1105 if (type == EXPR_SIZEOF) {
1106 switch (tref->type) {
1107 case RPC_FC_BYTE:
1108 case RPC_FC_CHAR:
1109 case RPC_FC_SMALL:
1110 case RPC_FC_USMALL:
1111 e->is_const = TRUE;
1112 e->cval = 1;
1113 break;
1114 case RPC_FC_WCHAR:
1115 case RPC_FC_USHORT:
1116 case RPC_FC_SHORT:
1117 e->is_const = TRUE;
1118 e->cval = 2;
1119 break;
1120 case RPC_FC_LONG:
1121 case RPC_FC_ULONG:
1122 case RPC_FC_FLOAT:
1123 case RPC_FC_ERROR_STATUS_T:
1124 e->is_const = TRUE;
1125 e->cval = 4;
1126 break;
1127 case RPC_FC_HYPER:
1128 case RPC_FC_DOUBLE:
1129 e->is_const = TRUE;
1130 e->cval = 8;
1131 break;
1134 if (type == EXPR_CAST && expr->is_const) {
1135 e->is_const = TRUE;
1136 e->cval = expr->cval;
1138 return e;
1141 static expr_t *make_expr1(enum expr_type type, expr_t *expr)
1143 expr_t *e;
1144 if (type == EXPR_ADDRESSOF && expr->type != EXPR_IDENTIFIER)
1145 error("address-of operator applied to invalid expression\n");
1146 e = xmalloc(sizeof(expr_t));
1147 e->type = type;
1148 e->ref = expr;
1149 e->u.lval = 0;
1150 e->is_const = FALSE;
1151 /* check for compile-time optimization */
1152 if (expr->is_const) {
1153 e->is_const = TRUE;
1154 switch (type) {
1155 case EXPR_NEG:
1156 e->cval = -expr->cval;
1157 break;
1158 case EXPR_NOT:
1159 e->cval = ~expr->cval;
1160 break;
1161 default:
1162 e->is_const = FALSE;
1163 break;
1166 return e;
1169 static expr_t *make_expr2(enum expr_type type, expr_t *expr1, expr_t *expr2)
1171 expr_t *e;
1172 e = xmalloc(sizeof(expr_t));
1173 e->type = type;
1174 e->ref = expr1;
1175 e->u.ext = expr2;
1176 e->is_const = FALSE;
1177 /* check for compile-time optimization */
1178 if (expr1->is_const && expr2->is_const) {
1179 e->is_const = TRUE;
1180 switch (type) {
1181 case EXPR_ADD:
1182 e->cval = expr1->cval + expr2->cval;
1183 break;
1184 case EXPR_SUB:
1185 e->cval = expr1->cval - expr2->cval;
1186 break;
1187 case EXPR_MUL:
1188 e->cval = expr1->cval * expr2->cval;
1189 break;
1190 case EXPR_DIV:
1191 e->cval = expr1->cval / expr2->cval;
1192 break;
1193 case EXPR_OR:
1194 e->cval = expr1->cval | expr2->cval;
1195 break;
1196 case EXPR_AND:
1197 e->cval = expr1->cval & expr2->cval;
1198 break;
1199 case EXPR_SHL:
1200 e->cval = expr1->cval << expr2->cval;
1201 break;
1202 case EXPR_SHR:
1203 e->cval = expr1->cval >> expr2->cval;
1204 break;
1205 default:
1206 e->is_const = FALSE;
1207 break;
1210 return e;
1213 static expr_t *make_expr3(enum expr_type type, expr_t *expr1, expr_t *expr2, expr_t *expr3)
1215 expr_t *e;
1216 e = xmalloc(sizeof(expr_t));
1217 e->type = type;
1218 e->ref = expr1;
1219 e->u.ext = expr2;
1220 e->ext2 = expr3;
1221 e->is_const = FALSE;
1222 /* check for compile-time optimization */
1223 if (expr1->is_const && expr2->is_const && expr3->is_const) {
1224 e->is_const = TRUE;
1225 switch (type) {
1226 case EXPR_COND:
1227 e->cval = expr1->cval ? expr2->cval : expr3->cval;
1228 break;
1229 default:
1230 e->is_const = FALSE;
1231 break;
1234 return e;
1237 static expr_list_t *append_expr(expr_list_t *list, expr_t *expr)
1239 if (!expr) return list;
1240 if (!list)
1242 list = xmalloc( sizeof(*list) );
1243 list_init( list );
1245 list_add_tail( list, &expr->entry );
1246 return list;
1249 static array_dims_t *append_array(array_dims_t *list, expr_t *expr)
1251 if (!expr) return list;
1252 if (!list)
1254 list = xmalloc( sizeof(*list) );
1255 list_init( list );
1257 list_add_tail( list, &expr->entry );
1258 return list;
1261 static struct list type_pool = LIST_INIT(type_pool);
1262 typedef struct
1264 type_t data;
1265 struct list link;
1266 } type_pool_node_t;
1268 type_t *alloc_type(void)
1270 type_pool_node_t *node = xmalloc(sizeof *node);
1271 list_add_tail(&type_pool, &node->link);
1272 return &node->data;
1275 void set_all_tfswrite(int val)
1277 type_pool_node_t *node;
1278 LIST_FOR_EACH_ENTRY(node, &type_pool, type_pool_node_t, link)
1279 node->data.tfswrite = val;
1282 static type_t *make_type(unsigned char type, type_t *ref)
1284 type_t *t = alloc_type();
1285 t->name = NULL;
1286 t->kind = TKIND_PRIMITIVE;
1287 t->type = type;
1288 t->ref = ref;
1289 t->attrs = NULL;
1290 t->orig = NULL;
1291 t->funcs = NULL;
1292 t->fields = NULL;
1293 t->ifaces = NULL;
1294 t->dim = 0;
1295 t->size_is = NULL;
1296 t->length_is = NULL;
1297 t->typestring_offset = 0;
1298 t->ptrdesc = 0;
1299 t->declarray = FALSE;
1300 t->ignore = (parse_only != 0);
1301 t->is_const = FALSE;
1302 t->sign = 0;
1303 t->defined = FALSE;
1304 t->written = FALSE;
1305 t->user_types_registered = FALSE;
1306 t->tfswrite = FALSE;
1307 t->typelib_idx = -1;
1308 return t;
1311 static void set_type(var_t *v, type_t *type, int ptr_level, array_dims_t *arr,
1312 int top)
1314 expr_list_t *sizes = get_attrp(v->attrs, ATTR_SIZEIS);
1315 expr_list_t *lengs = get_attrp(v->attrs, ATTR_LENGTHIS);
1316 int ptr_attr = get_attrv(v->attrs, ATTR_POINTERTYPE);
1317 int ptr_type = ptr_attr;
1318 int sizeless, has_varconf;
1319 expr_t *dim;
1320 type_t *atype, **ptype;
1322 v->type = type;
1324 if (!ptr_type && top)
1325 ptr_type = RPC_FC_RP;
1327 for ( ; 0 < ptr_level; --ptr_level)
1329 v->type = make_type(pointer_default, v->type);
1330 if (ptr_level == 1 && ptr_type && !arr)
1332 v->type->type = ptr_type;
1333 ptr_type = 0;
1337 if (ptr_type && !arr)
1339 if (is_ptr(v->type))
1341 if (v->type->type != ptr_type)
1343 v->type = duptype(v->type, 1);
1344 v->type->type = ptr_type;
1347 else if (!arr && ptr_attr)
1348 error("%s: pointer attribute applied to non-pointer type\n", v->name);
1351 sizeless = FALSE;
1352 if (arr) LIST_FOR_EACH_ENTRY_REV(dim, arr, expr_t, entry)
1354 if (sizeless)
1355 error("%s: only the first array dimension can be unspecified\n", v->name);
1357 if (dim->is_const)
1359 unsigned int align = 0;
1360 size_t size = type_memsize(v->type, &align);
1362 if (dim->cval <= 0)
1363 error("%s: array dimension must be positive\n", v->name);
1365 if (0xffffffffuL / size < (unsigned long) dim->cval)
1366 error("%s: total array size is too large\n", v->name);
1367 else if (0xffffuL < size * dim->cval)
1368 v->type = make_type(RPC_FC_LGFARRAY, v->type);
1369 else
1370 v->type = make_type(RPC_FC_SMFARRAY, v->type);
1372 else
1374 sizeless = TRUE;
1375 v->type = make_type(RPC_FC_CARRAY, v->type);
1378 v->type->declarray = TRUE;
1379 v->type->dim = dim->cval;
1382 ptype = &v->type;
1383 has_varconf = FALSE;
1384 if (sizes) LIST_FOR_EACH_ENTRY(dim, sizes, expr_t, entry)
1386 if (dim->type != EXPR_VOID)
1388 has_varconf = TRUE;
1389 atype = *ptype = duptype(*ptype, 0);
1391 if (atype->type == RPC_FC_SMFARRAY || atype->type == RPC_FC_LGFARRAY)
1392 error("%s: cannot specify size_is for a fixed sized array\n", v->name);
1394 if (atype->type != RPC_FC_CARRAY && !is_ptr(atype))
1395 error("%s: size_is attribute applied to illegal type\n", v->name);
1397 atype->type = RPC_FC_CARRAY;
1398 atype->size_is = dim;
1401 ptype = &(*ptype)->ref;
1402 if (*ptype == NULL)
1403 error("%s: too many expressions in size_is attribute\n", v->name);
1406 ptype = &v->type;
1407 if (lengs) LIST_FOR_EACH_ENTRY(dim, lengs, expr_t, entry)
1409 if (dim->type != EXPR_VOID)
1411 has_varconf = TRUE;
1412 atype = *ptype = duptype(*ptype, 0);
1414 if (atype->type == RPC_FC_SMFARRAY)
1415 atype->type = RPC_FC_SMVARRAY;
1416 else if (atype->type == RPC_FC_LGFARRAY)
1417 atype->type = RPC_FC_LGVARRAY;
1418 else if (atype->type == RPC_FC_CARRAY)
1419 atype->type = RPC_FC_CVARRAY;
1420 else
1421 error("%s: length_is attribute applied to illegal type\n", v->name);
1423 atype->length_is = dim;
1426 ptype = &(*ptype)->ref;
1427 if (*ptype == NULL)
1428 error("%s: too many expressions in length_is attribute\n", v->name);
1431 if (has_varconf && !last_array(v->type))
1433 ptype = &v->type;
1434 for (ptype = &v->type; is_array(*ptype); ptype = &(*ptype)->ref)
1436 *ptype = duptype(*ptype, 0);
1437 (*ptype)->type = RPC_FC_BOGUS_ARRAY;
1441 if (is_array(v->type))
1443 const type_t *rt = v->type->ref;
1444 if (is_user_type(rt))
1445 v->type->type = RPC_FC_BOGUS_ARRAY;
1446 else
1447 switch (rt->type)
1449 case RPC_FC_BOGUS_STRUCT:
1450 case RPC_FC_NON_ENCAPSULATED_UNION:
1451 case RPC_FC_ENCAPSULATED_UNION:
1452 case RPC_FC_ENUM16:
1453 v->type->type = RPC_FC_BOGUS_ARRAY;
1454 break;
1455 /* FC_RP should be above, but widl overuses these, and will break things. */
1456 case RPC_FC_UP:
1457 case RPC_FC_RP:
1458 if (rt->ref->type == RPC_FC_IP)
1459 v->type->type = RPC_FC_BOGUS_ARRAY;
1460 break;
1465 static ifref_list_t *append_ifref(ifref_list_t *list, ifref_t *iface)
1467 if (!iface) return list;
1468 if (!list)
1470 list = xmalloc( sizeof(*list) );
1471 list_init( list );
1473 list_add_tail( list, &iface->entry );
1474 return list;
1477 static ifref_t *make_ifref(type_t *iface)
1479 ifref_t *l = xmalloc(sizeof(ifref_t));
1480 l->iface = iface;
1481 l->attrs = NULL;
1482 return l;
1485 static var_list_t *append_var(var_list_t *list, var_t *var)
1487 if (!var) return list;
1488 if (!list)
1490 list = xmalloc( sizeof(*list) );
1491 list_init( list );
1493 list_add_tail( list, &var->entry );
1494 return list;
1497 static var_t *make_var(char *name)
1499 var_t *v = xmalloc(sizeof(var_t));
1500 v->name = name;
1501 v->type = NULL;
1502 v->args = NULL;
1503 v->attrs = NULL;
1504 v->eval = NULL;
1505 return v;
1508 static pident_list_t *append_pident(pident_list_t *list, pident_t *p)
1510 if (!p) return list;
1511 if (!list) {
1512 list = xmalloc(sizeof(*list));
1513 list_init(list);
1515 list_add_tail(list, &p->entry);
1516 return list;
1519 static pident_t *make_pident(var_t *var)
1521 pident_t *p = xmalloc(sizeof(*p));
1522 p->var = var;
1523 p->ptr_level = 0;
1524 return p;
1527 static func_list_t *append_func(func_list_t *list, func_t *func)
1529 if (!func) return list;
1530 if (!list)
1532 list = xmalloc( sizeof(*list) );
1533 list_init( list );
1535 list_add_tail( list, &func->entry );
1536 return list;
1539 static func_t *make_func(var_t *def, var_list_t *args)
1541 func_t *f = xmalloc(sizeof(func_t));
1542 f->def = def;
1543 f->args = args;
1544 f->ignore = parse_only;
1545 f->idx = -1;
1546 return f;
1549 static type_t *make_class(char *name)
1551 type_t *c = make_type(0, NULL);
1552 c->name = name;
1553 c->kind = TKIND_COCLASS;
1554 return c;
1557 static type_t *make_safearray(type_t *type)
1559 type_t *sa = duptype(find_type("SAFEARRAY", 0), 1);
1560 sa->ref = type;
1561 return make_type(pointer_default, sa);
1564 #define HASHMAX 64
1566 static int hash_ident(const char *name)
1568 const char *p = name;
1569 int sum = 0;
1570 /* a simple sum hash is probably good enough */
1571 while (*p) {
1572 sum += *p;
1573 p++;
1575 return sum & (HASHMAX-1);
1578 /***** type repository *****/
1580 struct rtype {
1581 const char *name;
1582 type_t *type;
1583 int t;
1584 struct rtype *next;
1587 struct rtype *type_hash[HASHMAX];
1589 static type_t *reg_type(type_t *type, const char *name, int t)
1591 struct rtype *nt;
1592 int hash;
1593 if (!name) {
1594 error_loc("registering named type without name\n");
1595 return type;
1597 hash = hash_ident(name);
1598 nt = xmalloc(sizeof(struct rtype));
1599 nt->name = name;
1600 nt->type = type;
1601 nt->t = t;
1602 nt->next = type_hash[hash];
1603 type_hash[hash] = nt;
1604 return type;
1607 static int is_incomplete(const type_t *t)
1609 return !t->defined && (is_struct(t->type) || is_union(t->type));
1612 static void add_incomplete(type_t *t)
1614 struct typenode *tn = xmalloc(sizeof *tn);
1615 tn->type = t;
1616 list_add_tail(&incomplete_types, &tn->entry);
1619 static void fix_type(type_t *t)
1621 if (t->kind == TKIND_ALIAS && is_incomplete(t)) {
1622 type_t *ot = t->orig;
1623 fix_type(ot);
1624 t->fields = ot->fields;
1625 t->defined = ot->defined;
1629 static void fix_incomplete(void)
1631 struct typenode *tn, *next;
1633 LIST_FOR_EACH_ENTRY_SAFE(tn, next, &incomplete_types, struct typenode, entry) {
1634 fix_type(tn->type);
1635 free(tn);
1639 static type_t *reg_typedefs(type_t *type, pident_list_t *pidents, attr_list_t *attrs)
1641 type_t *ptr = type;
1642 const pident_t *pident;
1643 int ptrc = 0;
1644 int is_str = is_attr(attrs, ATTR_STRING);
1645 unsigned char ptr_type = get_attrv(attrs, ATTR_POINTERTYPE);
1647 if (is_str)
1649 type_t *t = type;
1650 unsigned char c;
1652 while (is_ptr(t))
1653 t = t->ref;
1655 c = t->type;
1656 if (c != RPC_FC_CHAR && c != RPC_FC_BYTE && c != RPC_FC_WCHAR)
1658 pident = LIST_ENTRY( list_head( pidents ), const pident_t, entry );
1659 error_loc("'%s': [string] attribute is only valid on 'char', 'byte', or 'wchar_t' pointers and arrays\n",
1660 pident->var->name);
1664 /* We must generate names for tagless enum, struct or union.
1665 Typedef-ing a tagless enum, struct or union means we want the typedef
1666 to be included in a library whether it has other attributes or not,
1667 hence the public attribute. */
1668 if ((type->kind == TKIND_ENUM || type->kind == TKIND_RECORD
1669 || type->kind == TKIND_UNION) && ! type->name && ! parse_only)
1671 if (! is_attr(attrs, ATTR_PUBLIC))
1672 attrs = append_attr( attrs, make_attr(ATTR_PUBLIC) );
1673 type->name = gen_name();
1676 LIST_FOR_EACH_ENTRY( pident, pidents, const pident_t, entry )
1678 var_t *name = pident->var;
1680 if (name->name) {
1681 type_t *cur = ptr;
1682 int cptr = pident->ptr_level;
1683 if (cptr > ptrc) {
1684 while (cptr > ptrc) {
1685 cur = ptr = make_type(pointer_default, cur);
1686 ptrc++;
1688 } else {
1689 while (cptr < ptrc) {
1690 cur = cur->ref;
1691 cptr++;
1694 cur = alias(cur, name->name);
1695 cur->attrs = attrs;
1696 if (ptr_type)
1698 if (is_ptr(cur))
1699 cur->type = ptr_type;
1700 else
1701 error_loc("'%s': pointer attribute applied to non-pointer type\n",
1702 cur->name);
1704 else if (is_str && ! is_ptr(cur))
1705 error_loc("'%s': [string] attribute applied to non-pointer type\n",
1706 cur->name);
1708 if (is_incomplete(cur))
1709 add_incomplete(cur);
1710 reg_type(cur, cur->name, 0);
1713 return type;
1716 static type_t *find_type(const char *name, int t)
1718 struct rtype *cur = type_hash[hash_ident(name)];
1719 while (cur && (cur->t != t || strcmp(cur->name, name)))
1720 cur = cur->next;
1721 if (!cur) {
1722 error_loc("type '%s' not found\n", name);
1723 return NULL;
1725 return cur->type;
1728 static type_t *find_type2(char *name, int t)
1730 type_t *tp = find_type(name, t);
1731 free(name);
1732 return tp;
1735 int is_type(const char *name)
1737 struct rtype *cur = type_hash[hash_ident(name)];
1738 while (cur && (cur->t || strcmp(cur->name, name)))
1739 cur = cur->next;
1740 if (cur) return TRUE;
1741 return FALSE;
1744 static type_t *get_type(unsigned char type, char *name, int t)
1746 struct rtype *cur = NULL;
1747 type_t *tp;
1748 if (name) {
1749 cur = type_hash[hash_ident(name)];
1750 while (cur && (cur->t != t || strcmp(cur->name, name)))
1751 cur = cur->next;
1753 if (cur) {
1754 free(name);
1755 return cur->type;
1757 tp = make_type(type, NULL);
1758 tp->name = name;
1759 if (!name) return tp;
1760 return reg_type(tp, name, t);
1763 static type_t *get_typev(unsigned char type, var_t *name, int t)
1765 char *sname = NULL;
1766 if (name) {
1767 sname = name->name;
1768 free(name);
1770 return get_type(type, sname, t);
1773 static int get_struct_type(var_list_t *fields)
1775 int has_pointer = 0;
1776 int has_conformance = 0;
1777 int has_variance = 0;
1778 var_t *field;
1780 if (get_padding(fields))
1781 return RPC_FC_BOGUS_STRUCT;
1783 if (fields) LIST_FOR_EACH_ENTRY( field, fields, var_t, entry )
1785 type_t *t = field->type;
1787 if (is_user_type(t))
1788 return RPC_FC_BOGUS_STRUCT;
1790 if (is_ptr(t))
1793 t = t->ref;
1794 while (is_ptr(t));
1796 switch (t->type)
1798 case RPC_FC_IP:
1799 case RPC_FC_ENCAPSULATED_UNION:
1800 case RPC_FC_NON_ENCAPSULATED_UNION:
1801 case RPC_FC_BOGUS_STRUCT:
1802 return RPC_FC_BOGUS_STRUCT;
1805 has_pointer = 1;
1806 continue;
1809 if (field->type->declarray)
1811 if (is_string_type(field->attrs, field->type))
1813 if (is_conformant_array(field->type))
1814 has_conformance = 1;
1815 has_variance = 1;
1816 continue;
1819 if (is_array(field->type->ref))
1820 return RPC_FC_BOGUS_STRUCT;
1822 if (is_conformant_array(field->type))
1824 has_conformance = 1;
1825 if (field->type->declarray && list_next(fields, &field->entry))
1826 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1827 field->name);
1829 if (field->type->length_is)
1830 has_variance = 1;
1832 t = field->type->ref;
1835 switch (t->type)
1838 * RPC_FC_BYTE, RPC_FC_STRUCT, etc
1839 * Simple types don't effect the type of struct.
1840 * A struct containing a simple struct is still a simple struct.
1841 * So long as we can block copy the data, we return RPC_FC_STRUCT.
1843 case 0: /* void pointer */
1844 case RPC_FC_BYTE:
1845 case RPC_FC_CHAR:
1846 case RPC_FC_SMALL:
1847 case RPC_FC_USMALL:
1848 case RPC_FC_WCHAR:
1849 case RPC_FC_SHORT:
1850 case RPC_FC_USHORT:
1851 case RPC_FC_LONG:
1852 case RPC_FC_ULONG:
1853 case RPC_FC_INT3264:
1854 case RPC_FC_UINT3264:
1855 case RPC_FC_HYPER:
1856 case RPC_FC_FLOAT:
1857 case RPC_FC_DOUBLE:
1858 case RPC_FC_STRUCT:
1859 case RPC_FC_ENUM32:
1860 break;
1862 case RPC_FC_RP:
1863 case RPC_FC_UP:
1864 case RPC_FC_FP:
1865 case RPC_FC_OP:
1866 case RPC_FC_CARRAY:
1867 case RPC_FC_CVARRAY:
1868 case RPC_FC_BOGUS_ARRAY:
1869 has_pointer = 1;
1870 break;
1873 * Propagate member attributes
1874 * a struct should be at least as complex as its member
1876 case RPC_FC_CVSTRUCT:
1877 has_conformance = 1;
1878 has_variance = 1;
1879 has_pointer = 1;
1880 break;
1882 case RPC_FC_CPSTRUCT:
1883 has_conformance = 1;
1884 if (list_next( fields, &field->entry ))
1885 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1886 field->name);
1887 has_pointer = 1;
1888 break;
1890 case RPC_FC_CSTRUCT:
1891 has_conformance = 1;
1892 if (list_next( fields, &field->entry ))
1893 error_loc("field '%s' deriving from a conformant array must be the last field in the structure\n",
1894 field->name);
1895 break;
1897 case RPC_FC_PSTRUCT:
1898 has_pointer = 1;
1899 break;
1901 default:
1902 error_loc("Unknown struct member %s with type (0x%02x)\n", field->name, t->type);
1903 /* fallthru - treat it as complex */
1905 /* as soon as we see one of these these members, it's bogus... */
1906 case RPC_FC_ENCAPSULATED_UNION:
1907 case RPC_FC_NON_ENCAPSULATED_UNION:
1908 case RPC_FC_BOGUS_STRUCT:
1909 case RPC_FC_ENUM16:
1910 return RPC_FC_BOGUS_STRUCT;
1914 if( has_variance )
1916 if ( has_conformance )
1917 return RPC_FC_CVSTRUCT;
1918 else
1919 return RPC_FC_BOGUS_STRUCT;
1921 if( has_conformance && has_pointer )
1922 return RPC_FC_CPSTRUCT;
1923 if( has_conformance )
1924 return RPC_FC_CSTRUCT;
1925 if( has_pointer )
1926 return RPC_FC_PSTRUCT;
1927 return RPC_FC_STRUCT;
1930 /***** constant repository *****/
1932 struct rconst {
1933 char *name;
1934 var_t *var;
1935 struct rconst *next;
1938 struct rconst *const_hash[HASHMAX];
1940 static var_t *reg_const(var_t *var)
1942 struct rconst *nc;
1943 int hash;
1944 if (!var->name) {
1945 error_loc("registering constant without name\n");
1946 return var;
1948 hash = hash_ident(var->name);
1949 nc = xmalloc(sizeof(struct rconst));
1950 nc->name = var->name;
1951 nc->var = var;
1952 nc->next = const_hash[hash];
1953 const_hash[hash] = nc;
1954 return var;
1957 static var_t *find_const(char *name, int f)
1959 struct rconst *cur = const_hash[hash_ident(name)];
1960 while (cur && strcmp(cur->name, name))
1961 cur = cur->next;
1962 if (!cur) {
1963 if (f) error_loc("constant '%s' not found\n", name);
1964 return NULL;
1966 return cur->var;
1969 static void write_libid(const char *name, const attr_list_t *attr)
1971 const UUID *uuid = get_attrp(attr, ATTR_UUID);
1972 write_guid(idfile, "LIBID", name, uuid);
1975 static void write_clsid(type_t *cls)
1977 const UUID *uuid = get_attrp(cls->attrs, ATTR_UUID);
1978 write_guid(idfile, "CLSID", cls->name, uuid);
1981 static void write_diid(type_t *iface)
1983 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1984 write_guid(idfile, "DIID", iface->name, uuid);
1987 static void write_iid(type_t *iface)
1989 const UUID *uuid = get_attrp(iface->attrs, ATTR_UUID);
1990 write_guid(idfile, "IID", iface->name, uuid);
1993 static int compute_method_indexes(type_t *iface)
1995 int idx;
1996 func_t *f;
1998 if (iface->ref)
1999 idx = compute_method_indexes(iface->ref);
2000 else
2001 idx = 0;
2003 if (!iface->funcs)
2004 return idx;
2006 LIST_FOR_EACH_ENTRY( f, iface->funcs, func_t, entry )
2007 if (! is_callas(f->def->attrs))
2008 f->idx = idx++;
2010 return idx;
2013 static char *gen_name(void)
2015 static const char format[] = "__WIDL_%s_generated_name_%08lX";
2016 static unsigned long n = 0;
2017 static const char *file_id;
2018 static size_t size;
2019 char *name;
2021 if (! file_id)
2023 char *dst = dup_basename(input_name, ".idl");
2024 file_id = dst;
2026 for (; *dst; ++dst)
2027 if (! isalnum((unsigned char) *dst))
2028 *dst = '_';
2030 size = sizeof format - 7 + strlen(file_id) + 8;
2033 name = xmalloc(size);
2034 sprintf(name, format, file_id, n++);
2035 return name;
2038 static void process_typedefs(pident_list_t *pidents)
2040 pident_t *pident, *next;
2042 if (!pidents) return;
2043 LIST_FOR_EACH_ENTRY_SAFE( pident, next, pidents, pident_t, entry )
2045 var_t *var = pident->var;
2046 type_t *type = find_type(var->name, 0);
2048 if (! parse_only && do_header)
2049 write_typedef(type);
2050 if (in_typelib && type->attrs)
2051 add_typelib_entry(type);
2053 free(pident);
2054 free(var);
2058 static void check_arg(var_t *arg)
2060 type_t *t = arg->type;
2062 if (t->type == 0 && ! is_var_ptr(arg))
2063 error_loc("argument '%s' has void type\n", arg->name);
2066 static void check_all_user_types(ifref_list_t *ifrefs)
2068 const ifref_t *ifref;
2069 const func_t *f;
2071 if (ifrefs) LIST_FOR_EACH_ENTRY(ifref, ifrefs, const ifref_t, entry)
2073 const func_list_t *fs = ifref->iface->funcs;
2074 if (fs) LIST_FOR_EACH_ENTRY(f, fs, const func_t, entry)
2075 check_for_additional_prototype_types(f->args);
2079 int is_valid_uuid(const char *s)
2081 int i;
2083 for (i = 0; i < 36; ++i)
2084 if (i == 8 || i == 13 || i == 18 || i == 23)
2086 if (s[i] != '-')
2087 return FALSE;
2089 else
2090 if (!isxdigit(s[i]))
2091 return FALSE;
2093 return s[i] == '\0';