Sync to trunk revision 61757.
[reactos.git] / dll / win32 / vbscript / compile.c
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17 */
18
19 #include "vbscript.h"
20
21 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
22
23 typedef struct _statement_ctx_t {
24 unsigned stack_use;
25
26 unsigned while_end_label;
27 unsigned for_end_label;
28
29 struct _statement_ctx_t *next;
30 } statement_ctx_t;
31
32 typedef struct {
33 parser_ctx_t parser;
34
35 unsigned instr_cnt;
36 unsigned instr_size;
37 vbscode_t *code;
38
39 statement_ctx_t *stat_ctx;
40
41 unsigned *labels;
42 unsigned labels_size;
43 unsigned labels_cnt;
44
45 unsigned sub_end_label;
46 unsigned func_end_label;
47 unsigned prop_end_label;
48
49 dim_decl_t *dim_decls;
50 dynamic_var_t *global_vars;
51
52 const_decl_t *const_decls;
53 const_decl_t *global_consts;
54
55 function_t *func;
56 function_t *funcs;
57 function_decl_t *func_decls;
58
59 class_desc_t *classes;
60 } compile_ctx_t;
61
62 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
63 static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_t*);
64
65 static const struct {
66 const char *op_str;
67 instr_arg_type_t arg1_type;
68 instr_arg_type_t arg2_type;
69 } instr_info[] = {
70 #define X(n,a,b,c) {#n,b,c},
71 OP_LIST
72 #undef X
73 };
74
75 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
76 {
77 switch(type) {
78 case ARG_STR:
79 case ARG_BSTR:
80 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
81 break;
82 case ARG_INT:
83 TRACE_(vbscript_disas)("\t%d", arg->uint);
84 break;
85 case ARG_UINT:
86 case ARG_ADDR:
87 TRACE_(vbscript_disas)("\t%u", arg->uint);
88 break;
89 case ARG_DOUBLE:
90 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
91 break;
92 case ARG_NONE:
93 break;
94 DEFAULT_UNREACHABLE;
95 }
96 }
97
98 static void dump_code(compile_ctx_t *ctx)
99 {
100 instr_t *instr;
101
102 for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
103 assert(instr->op < OP_LAST);
104 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
105 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
106 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
107 TRACE_(vbscript_disas)("\n");
108 }
109 }
110
111 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
112 {
113 return heap_pool_alloc(&vbscode->heap, size);
114 }
115
116 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
117 {
118 void *ret;
119
120 ret = heap_pool_alloc(&vbscode->heap, size);
121 if(ret)
122 memset(ret, 0, size);
123 return ret;
124 }
125
126 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
127 {
128 size_t size;
129 WCHAR *ret;
130
131 size = (strlenW(str)+1)*sizeof(WCHAR);
132 ret = compiler_alloc(vbscode, size);
133 if(ret)
134 memcpy(ret, str, size);
135 return ret;
136 }
137
138 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
139 {
140 assert(id < ctx->instr_cnt);
141 return ctx->code->instrs + id;
142 }
143
144 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
145 {
146 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
147
148 if(ctx->instr_size == ctx->instr_cnt) {
149 instr_t *new_instr;
150
151 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
152 if(!new_instr)
153 return 0;
154
155 ctx->code->instrs = new_instr;
156 ctx->instr_size *= 2;
157 }
158
159 ctx->code->instrs[ctx->instr_cnt].op = op;
160 return ctx->instr_cnt++;
161 }
162
163 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
164 {
165 unsigned ret;
166
167 ret = push_instr(ctx, op);
168 if(!ret)
169 return E_OUTOFMEMORY;
170
171 instr_ptr(ctx, ret)->arg1.lng = arg;
172 return S_OK;
173 }
174
175 static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
176 {
177 unsigned ret;
178
179 ret = push_instr(ctx, op);
180 if(!ret)
181 return E_OUTOFMEMORY;
182
183 instr_ptr(ctx, ret)->arg1.uint = arg;
184 return S_OK;
185 }
186
187 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
188 {
189 unsigned ret;
190
191 ret = push_instr(ctx, op);
192 if(!ret)
193 return E_OUTOFMEMORY;
194
195 instr_ptr(ctx, ret)->arg1.uint = arg;
196 return S_OK;
197 }
198
199 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
200 {
201 unsigned instr;
202 WCHAR *str;
203
204 str = compiler_alloc_string(ctx->code, arg);
205 if(!str)
206 return E_OUTOFMEMORY;
207
208 instr = push_instr(ctx, op);
209 if(!instr)
210 return E_OUTOFMEMORY;
211
212 instr_ptr(ctx, instr)->arg1.str = str;
213 return S_OK;
214 }
215
216 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
217 {
218 unsigned instr;
219 double *d;
220
221 d = compiler_alloc(ctx->code, sizeof(double));
222 if(!d)
223 return E_OUTOFMEMORY;
224
225 instr = push_instr(ctx, op);
226 if(!instr)
227 return E_OUTOFMEMORY;
228
229 *d = arg;
230 instr_ptr(ctx, instr)->arg1.dbl = d;
231 return S_OK;
232 }
233
234 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
235 {
236 if(!ctx->code->bstr_pool_size) {
237 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
238 if(!ctx->code->bstr_pool)
239 return NULL;
240 ctx->code->bstr_pool_size = 8;
241 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
242 BSTR *new_pool;
243
244 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
245 if(!new_pool)
246 return NULL;
247
248 ctx->code->bstr_pool = new_pool;
249 ctx->code->bstr_pool_size *= 2;
250 }
251
252 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
253 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
254 return NULL;
255
256 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
257 }
258
259 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
260 {
261 unsigned instr;
262 BSTR bstr;
263
264 bstr = alloc_bstr_arg(ctx, arg);
265 if(!bstr)
266 return E_OUTOFMEMORY;
267
268 instr = push_instr(ctx, op);
269 if(!instr)
270 return E_OUTOFMEMORY;
271
272 instr_ptr(ctx, instr)->arg1.bstr = bstr;
273 return S_OK;
274 }
275
276 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
277 {
278 unsigned instr;
279 BSTR bstr;
280
281 bstr = alloc_bstr_arg(ctx, arg1);
282 if(!bstr)
283 return E_OUTOFMEMORY;
284
285 instr = push_instr(ctx, op);
286 if(!instr)
287 return E_OUTOFMEMORY;
288
289 instr_ptr(ctx, instr)->arg1.bstr = bstr;
290 instr_ptr(ctx, instr)->arg2.uint = arg2;
291 return S_OK;
292 }
293
294 static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
295 {
296 unsigned instr;
297 BSTR bstr;
298
299 bstr = alloc_bstr_arg(ctx, arg2);
300 if(!bstr)
301 return E_OUTOFMEMORY;
302
303 instr = push_instr(ctx, op);
304 if(!instr)
305 return E_OUTOFMEMORY;
306
307 instr_ptr(ctx, instr)->arg1.uint = arg1;
308 instr_ptr(ctx, instr)->arg2.bstr = bstr;
309 return S_OK;
310 }
311
312 #define LABEL_FLAG 0x80000000
313
314 static unsigned alloc_label(compile_ctx_t *ctx)
315 {
316 if(!ctx->labels_size) {
317 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
318 if(!ctx->labels)
319 return 0;
320 ctx->labels_size = 8;
321 }else if(ctx->labels_size == ctx->labels_cnt) {
322 unsigned *new_labels;
323
324 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
325 if(!new_labels)
326 return 0;
327
328 ctx->labels = new_labels;
329 ctx->labels_size *= 2;
330 }
331
332 return ctx->labels_cnt++ | LABEL_FLAG;
333 }
334
335 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
336 {
337 assert(label & LABEL_FLAG);
338 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
339 }
340
341 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
342 {
343 const_decl_t *decl;
344
345 for(decl = ctx->const_decls; decl; decl = decl->next) {
346 if(!strcmpiW(decl->name, name))
347 return decl->value_expr;
348 }
349
350 if(!lookup_global)
351 return NULL;
352
353 for(decl = ctx->global_consts; decl; decl = decl->next) {
354 if(!strcmpiW(decl->name, name))
355 return decl->value_expr;
356 }
357
358 return NULL;
359 }
360
361 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
362 {
363 unsigned arg_cnt = 0;
364 HRESULT hres;
365
366 while(args) {
367 hres = compile_expression(ctx, args);
368 if(FAILED(hres))
369 return hres;
370
371 arg_cnt++;
372 args = args->next;
373 }
374
375 *ret = arg_cnt;
376 return S_OK;
377 }
378
379 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
380 {
381 unsigned arg_cnt = 0;
382 HRESULT hres;
383
384 if(ret_val && !expr->args) {
385 expression_t *const_expr;
386
387 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
388 if(const_expr)
389 return compile_expression(ctx, const_expr);
390 }
391
392 hres = compile_args(ctx, expr->args, &arg_cnt);
393 if(FAILED(hres))
394 return hres;
395
396 if(expr->obj_expr) {
397 hres = compile_expression(ctx, expr->obj_expr);
398 if(FAILED(hres))
399 return hres;
400
401 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
402 }else {
403 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
404 }
405
406 return hres;
407 }
408
409 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
410 {
411 HRESULT hres;
412
413 hres = compile_expression(ctx, expr->subexpr);
414 if(FAILED(hres))
415 return hres;
416
417 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
418 }
419
420 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
421 {
422 HRESULT hres;
423
424 hres = compile_expression(ctx, expr->left);
425 if(FAILED(hres))
426 return hres;
427
428 hres = compile_expression(ctx, expr->right);
429 if(FAILED(hres))
430 return hres;
431
432 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
433 }
434
435 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
436 {
437 switch(expr->type) {
438 case EXPR_ADD:
439 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
440 case EXPR_AND:
441 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
442 case EXPR_BOOL:
443 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
444 case EXPR_BRACKETS:
445 return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
446 case EXPR_CONCAT:
447 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
448 case EXPR_DIV:
449 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
450 case EXPR_DOUBLE:
451 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
452 case EXPR_EMPTY:
453 return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
454 case EXPR_EQUAL:
455 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
456 case EXPR_EQV:
457 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
458 case EXPR_EXP:
459 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
460 case EXPR_GT:
461 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
462 case EXPR_GTEQ:
463 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
464 case EXPR_IDIV:
465 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
466 case EXPR_IS:
467 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
468 case EXPR_IMP:
469 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
470 case EXPR_LT:
471 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
472 case EXPR_LTEQ:
473 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
474 case EXPR_ME:
475 return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
476 case EXPR_MEMBER:
477 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
478 case EXPR_MOD:
479 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
480 case EXPR_MUL:
481 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
482 case EXPR_NEG:
483 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
484 case EXPR_NEQUAL:
485 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
486 case EXPR_NEW:
487 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
488 case EXPR_NOT:
489 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
490 case EXPR_NOTHING:
491 return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
492 case EXPR_NULL:
493 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
494 case EXPR_OR:
495 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
496 case EXPR_STRING:
497 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
498 case EXPR_SUB:
499 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
500 case EXPR_USHORT:
501 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
502 case EXPR_ULONG:
503 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
504 case EXPR_XOR:
505 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
506 default:
507 FIXME("Unimplemented expression type %d\n", expr->type);
508 return E_NOTIMPL;
509 }
510
511 return S_OK;
512 }
513
514 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
515 {
516 unsigned cnd_jmp, endif_label = 0;
517 elseif_decl_t *elseif_decl;
518 HRESULT hres;
519
520 hres = compile_expression(ctx, stat->expr);
521 if(FAILED(hres))
522 return hres;
523
524 cnd_jmp = push_instr(ctx, OP_jmp_false);
525 if(!cnd_jmp)
526 return E_OUTOFMEMORY;
527
528 hres = compile_statement(ctx, NULL, stat->if_stat);
529 if(FAILED(hres))
530 return hres;
531
532 if(stat->else_stat || stat->elseifs) {
533 endif_label = alloc_label(ctx);
534 if(!endif_label)
535 return E_OUTOFMEMORY;
536
537 hres = push_instr_addr(ctx, OP_jmp, endif_label);
538 if(FAILED(hres))
539 return hres;
540 }
541
542 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
543 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
544
545 hres = compile_expression(ctx, elseif_decl->expr);
546 if(FAILED(hres))
547 return hres;
548
549 cnd_jmp = push_instr(ctx, OP_jmp_false);
550 if(!cnd_jmp)
551 return E_OUTOFMEMORY;
552
553 hres = compile_statement(ctx, NULL, elseif_decl->stat);
554 if(FAILED(hres))
555 return hres;
556
557 hres = push_instr_addr(ctx, OP_jmp, endif_label);
558 if(FAILED(hres))
559 return hres;
560 }
561
562 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
563
564 if(stat->else_stat) {
565 hres = compile_statement(ctx, NULL, stat->else_stat);
566 if(FAILED(hres))
567 return hres;
568 }
569
570 if(endif_label)
571 label_set_addr(ctx, endif_label);
572 return S_OK;
573 }
574
575 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
576 {
577 statement_ctx_t stat_ctx = {0}, *loop_ctx;
578 unsigned start_addr;
579 unsigned jmp_end;
580 HRESULT hres;
581
582 start_addr = ctx->instr_cnt;
583
584 hres = compile_expression(ctx, stat->expr);
585 if(FAILED(hres))
586 return hres;
587
588 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
589 if(!jmp_end)
590 return E_OUTOFMEMORY;
591
592 if(stat->stat.type == STAT_WHILE) {
593 loop_ctx = NULL;
594 }else {
595 if(!(stat_ctx.while_end_label = alloc_label(ctx)))
596 return E_OUTOFMEMORY;
597 loop_ctx = &stat_ctx;
598 }
599
600 hres = compile_statement(ctx, loop_ctx, stat->body);
601 if(FAILED(hres))
602 return hres;
603
604 hres = push_instr_addr(ctx, OP_jmp, start_addr);
605 if(FAILED(hres))
606 return hres;
607
608 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
609
610 if(loop_ctx)
611 label_set_addr(ctx, stat_ctx.while_end_label);
612
613 return S_OK;
614 }
615
616 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
617 {
618 statement_ctx_t loop_ctx = {0};
619 unsigned start_addr;
620 vbsop_t jmp_op;
621 HRESULT hres;
622
623 start_addr = ctx->instr_cnt;
624
625 if(!(loop_ctx.while_end_label = alloc_label(ctx)))
626 return E_OUTOFMEMORY;
627
628 hres = compile_statement(ctx, &loop_ctx, stat->body);
629 if(FAILED(hres))
630 return hres;
631
632 if(stat->expr) {
633 hres = compile_expression(ctx, stat->expr);
634 if(FAILED(hres))
635 return hres;
636
637 jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
638 }else {
639 jmp_op = OP_jmp;
640 }
641
642 hres = push_instr_addr(ctx, jmp_op, start_addr);
643 if(FAILED(hres))
644 return hres;
645
646 label_set_addr(ctx, loop_ctx.while_end_label);
647 return S_OK;
648 }
649
650 static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
651 {
652 statement_ctx_t loop_ctx = {1};
653 unsigned loop_start;
654 HRESULT hres;
655
656 hres = compile_expression(ctx, stat->group_expr);
657 if(FAILED(hres))
658 return hres;
659
660 if(!push_instr(ctx, OP_newenum))
661 return E_OUTOFMEMORY;
662
663 loop_start = ctx->instr_cnt;
664 if(!(loop_ctx.for_end_label = alloc_label(ctx)))
665 return E_OUTOFMEMORY;
666
667 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
668 if(FAILED(hres))
669 return hres;
670
671 hres = compile_statement(ctx, &loop_ctx, stat->body);
672 if(FAILED(hres))
673 return hres;
674
675 hres = push_instr_addr(ctx, OP_jmp, loop_start);
676 if(FAILED(hres))
677 return hres;
678
679 label_set_addr(ctx, loop_ctx.for_end_label);
680 return S_OK;
681 }
682
683 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
684 {
685 statement_ctx_t loop_ctx = {2};
686 unsigned step_instr, instr;
687 BSTR identifier;
688 HRESULT hres;
689
690 identifier = alloc_bstr_arg(ctx, stat->identifier);
691 if(!identifier)
692 return E_OUTOFMEMORY;
693
694 hres = compile_expression(ctx, stat->from_expr);
695 if(FAILED(hres))
696 return hres;
697
698 instr = push_instr(ctx, OP_assign_ident);
699 if(!instr)
700 return E_OUTOFMEMORY;
701 instr_ptr(ctx, instr)->arg1.bstr = identifier;
702 instr_ptr(ctx, instr)->arg2.uint = 0;
703
704 hres = compile_expression(ctx, stat->to_expr);
705 if(FAILED(hres))
706 return hres;
707
708 if(!push_instr(ctx, OP_val))
709 return E_OUTOFMEMORY;
710
711 if(stat->step_expr) {
712 hres = compile_expression(ctx, stat->step_expr);
713 if(FAILED(hres))
714 return hres;
715
716 if(!push_instr(ctx, OP_val))
717 return E_OUTOFMEMORY;
718 }else {
719 hres = push_instr_int(ctx, OP_short, 1);
720 if(FAILED(hres))
721 return hres;
722 }
723
724 loop_ctx.for_end_label = alloc_label(ctx);
725 if(!loop_ctx.for_end_label)
726 return E_OUTOFMEMORY;
727
728 step_instr = push_instr(ctx, OP_step);
729 if(!step_instr)
730 return E_OUTOFMEMORY;
731 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
732 instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
733
734 hres = compile_statement(ctx, &loop_ctx, stat->body);
735 if(FAILED(hres))
736 return hres;
737
738 instr = push_instr(ctx, OP_incc);
739 if(!instr)
740 return E_OUTOFMEMORY;
741 instr_ptr(ctx, instr)->arg1.bstr = identifier;
742
743 hres = push_instr_addr(ctx, OP_jmp, step_instr);
744 if(FAILED(hres))
745 return hres;
746
747 hres = push_instr_uint(ctx, OP_pop, 2);
748 if(FAILED(hres))
749 return hres;
750
751 label_set_addr(ctx, loop_ctx.for_end_label);
752 return S_OK;
753 }
754
755 static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
756 {
757 unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
758 case_clausule_t *case_iter;
759 expression_t *expr_iter;
760 HRESULT hres;
761
762 hres = compile_expression(ctx, stat->expr);
763 if(FAILED(hres))
764 return hres;
765
766 if(!push_instr(ctx, OP_val))
767 return E_OUTOFMEMORY;
768
769 end_label = alloc_label(ctx);
770 if(!end_label)
771 return E_OUTOFMEMORY;
772
773 for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
774 case_cnt++;
775
776 if(case_cnt) {
777 case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
778 if(!case_labels)
779 return E_OUTOFMEMORY;
780 }
781
782 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
783 case_labels[i] = alloc_label(ctx);
784 if(!case_labels[i]) {
785 hres = E_OUTOFMEMORY;
786 break;
787 }
788
789 if(!case_iter->expr)
790 break;
791
792 for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
793 hres = compile_expression(ctx, expr_iter);
794 if(FAILED(hres))
795 break;
796
797 hres = push_instr_addr(ctx, OP_case, case_labels[i]);
798 if(FAILED(hres))
799 break;
800 }
801 }
802
803 if(FAILED(hres)) {
804 heap_free(case_labels);
805 return hres;
806 }
807
808 hres = push_instr_uint(ctx, OP_pop, 1);
809 if(FAILED(hres)) {
810 heap_free(case_labels);
811 return hres;
812 }
813
814 hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
815 if(FAILED(hres)) {
816 heap_free(case_labels);
817 return hres;
818 }
819
820 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
821 label_set_addr(ctx, case_labels[i]);
822 hres = compile_statement(ctx, NULL, case_iter->stat);
823 if(FAILED(hres))
824 break;
825
826 if(!case_iter->next)
827 break;
828
829 hres = push_instr_addr(ctx, OP_jmp, end_label);
830 if(FAILED(hres))
831 break;
832 }
833
834 heap_free(case_labels);
835 if(FAILED(hres))
836 return hres;
837
838 label_set_addr(ctx, end_label);
839 return S_OK;
840 }
841
842 static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
843 {
844 unsigned args_cnt;
845 vbsop_t op;
846 HRESULT hres;
847
848 if(member_expr->obj_expr) {
849 hres = compile_expression(ctx, member_expr->obj_expr);
850 if(FAILED(hres))
851 return hres;
852
853 op = is_set ? OP_set_member : OP_assign_member;
854 }else {
855 op = is_set ? OP_set_ident : OP_assign_ident;
856 }
857
858 hres = compile_expression(ctx, value_expr);
859 if(FAILED(hres))
860 return hres;
861
862 hres = compile_args(ctx, member_expr->args, &args_cnt);
863 if(FAILED(hres))
864 return hres;
865
866 return push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
867 }
868
869 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
870 {
871 return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
872 }
873
874 static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
875 {
876 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
877 * with equality expression argument, so we do it in compiler. */
878 if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
879 binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;
880
881 if(eqexpr->left->type == EXPR_BRACKETS) {
882 member_expression_t new_member = *stat->expr;
883
884 WARN("converting call expr to assign expr\n");
885
886 new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
887 return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
888 }
889 }
890
891 return compile_member_expression(ctx, stat->expr, FALSE);
892 }
893
894 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
895 {
896 dim_decl_t *dim_decl;
897
898 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
899 if(!strcmpiW(dim_decl->name, name))
900 return TRUE;
901 }
902
903 return FALSE;
904 }
905
906 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
907 {
908 unsigned i;
909
910 for(i = 0; i < ctx->func->arg_cnt; i++) {
911 if(!strcmpiW(ctx->func->args[i].name, name))
912 return TRUE;
913 }
914
915 return FALSE;
916 }
917
918 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
919 {
920 dim_decl_t *dim_decl = stat->dim_decls;
921
922 while(1) {
923 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
924 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
925 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
926 return E_FAIL;
927 }
928
929 ctx->func->var_cnt++;
930 if(!dim_decl->next)
931 break;
932 dim_decl = dim_decl->next;
933 }
934
935 dim_decl->next = ctx->dim_decls;
936 ctx->dim_decls = stat->dim_decls;
937 return S_OK;
938 }
939
940 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
941 {
942 const_decl_t *decl, *next_decl = stat->decls;
943
944 do {
945 decl = next_decl;
946
947 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
948 || lookup_dim_decls(ctx, decl->name)) {
949 FIXME("%s redefined\n", debugstr_w(decl->name));
950 return E_FAIL;
951 }
952
953 if(ctx->func->type == FUNC_GLOBAL) {
954 HRESULT hres;
955
956 hres = compile_expression(ctx, decl->value_expr);
957 if(FAILED(hres))
958 return hres;
959
960 hres = push_instr_bstr(ctx, OP_const, decl->name);
961 if(FAILED(hres))
962 return hres;
963 }
964
965 next_decl = decl->next;
966 decl->next = ctx->const_decls;
967 ctx->const_decls = decl;
968 } while(next_decl);
969
970 return S_OK;
971 }
972
973 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
974 {
975 if(ctx->func != &ctx->code->main_code) {
976 FIXME("Function is not in the global code\n");
977 return E_FAIL;
978 }
979
980 stat->func_decl->next = ctx->func_decls;
981 ctx->func_decls = stat->func_decl;
982 return S_OK;
983 }
984
985 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
986 {
987 statement_ctx_t *iter;
988 unsigned pop_cnt = 0;
989
990 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
991 pop_cnt += iter->stack_use;
992 if(iter->while_end_label)
993 break;
994 }
995 if(!iter) {
996 FIXME("Exit Do outside Do Loop\n");
997 return E_FAIL;
998 }
999
1000 if(pop_cnt) {
1001 HRESULT hres;
1002
1003 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1004 if(FAILED(hres))
1005 return hres;
1006 }
1007
1008 return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1009 }
1010
1011 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
1012 {
1013 statement_ctx_t *iter;
1014 unsigned pop_cnt = 0;
1015
1016 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1017 pop_cnt += iter->stack_use;
1018 if(iter->for_end_label)
1019 break;
1020 }
1021 if(!iter) {
1022 FIXME("Exit For outside For loop\n");
1023 return E_FAIL;
1024 }
1025
1026 if(pop_cnt) {
1027 HRESULT hres;
1028
1029 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1030 if(FAILED(hres))
1031 return hres;
1032 }
1033
1034 return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1035 }
1036
1037 static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
1038 {
1039 statement_ctx_t *iter;
1040 unsigned pop_cnt = 0;
1041
1042 for(iter = ctx->stat_ctx; iter; iter = iter->next)
1043 pop_cnt += iter->stack_use;
1044
1045 if(pop_cnt) {
1046 HRESULT hres;
1047
1048 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1049 if(FAILED(hres))
1050 return hres;
1051 }
1052
1053 return push_instr_addr(ctx, OP_jmp, jmp_label);
1054 }
1055
1056 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
1057 {
1058 if(!ctx->sub_end_label) {
1059 FIXME("Exit Sub outside Sub?\n");
1060 return E_FAIL;
1061 }
1062
1063 return exit_label(ctx, ctx->sub_end_label);
1064 }
1065
1066 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
1067 {
1068 if(!ctx->func_end_label) {
1069 FIXME("Exit Function outside Function?\n");
1070 return E_FAIL;
1071 }
1072
1073 return exit_label(ctx, ctx->func_end_label);
1074 }
1075
1076 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
1077 {
1078 if(!ctx->prop_end_label) {
1079 FIXME("Exit Property outside Property?\n");
1080 return E_FAIL;
1081 }
1082
1083 return exit_label(ctx, ctx->prop_end_label);
1084 }
1085
1086 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
1087 {
1088 return push_instr_int(ctx, OP_errmode, stat->resume_next);
1089 }
1090
1091 static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1092 {
1093 HRESULT hres;
1094
1095 if(stat_ctx) {
1096 stat_ctx->next = ctx->stat_ctx;
1097 ctx->stat_ctx = stat_ctx;
1098 }
1099
1100 while(stat) {
1101 switch(stat->type) {
1102 case STAT_ASSIGN:
1103 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1104 break;
1105 case STAT_CALL:
1106 hres = compile_call_statement(ctx, (call_statement_t*)stat);
1107 break;
1108 case STAT_CONST:
1109 hres = compile_const_statement(ctx, (const_statement_t*)stat);
1110 break;
1111 case STAT_DIM:
1112 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
1113 break;
1114 case STAT_DOWHILE:
1115 case STAT_DOUNTIL:
1116 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
1117 break;
1118 case STAT_EXITDO:
1119 hres = compile_exitdo_statement(ctx);
1120 break;
1121 case STAT_EXITFOR:
1122 hres = compile_exitfor_statement(ctx);
1123 break;
1124 case STAT_EXITFUNC:
1125 hres = compile_exitfunc_statement(ctx);
1126 break;
1127 case STAT_EXITPROP:
1128 hres = compile_exitprop_statement(ctx);
1129 break;
1130 case STAT_EXITSUB:
1131 hres = compile_exitsub_statement(ctx);
1132 break;
1133 case STAT_FOREACH:
1134 hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
1135 break;
1136 case STAT_FORTO:
1137 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
1138 break;
1139 case STAT_FUNC:
1140 hres = compile_function_statement(ctx, (function_statement_t*)stat);
1141 break;
1142 case STAT_IF:
1143 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1144 break;
1145 case STAT_ONERROR:
1146 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
1147 break;
1148 case STAT_SELECT:
1149 hres = compile_select_statement(ctx, (select_statement_t*)stat);
1150 break;
1151 case STAT_SET:
1152 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
1153 break;
1154 case STAT_STOP:
1155 hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1156 break;
1157 case STAT_UNTIL:
1158 case STAT_WHILE:
1159 case STAT_WHILELOOP:
1160 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1161 break;
1162 default:
1163 FIXME("Unimplemented statement type %d\n", stat->type);
1164 hres = E_NOTIMPL;
1165 }
1166
1167 if(FAILED(hres))
1168 return hres;
1169 stat = stat->next;
1170 }
1171
1172 if(stat_ctx) {
1173 assert(ctx->stat_ctx == stat_ctx);
1174 ctx->stat_ctx = stat_ctx->next;
1175 }
1176
1177 return S_OK;
1178 }
1179
1180 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1181 {
1182 instr_t *instr;
1183
1184 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1185 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1186 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1187 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1188 }
1189 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1190 }
1191
1192 ctx->labels_cnt = 0;
1193 }
1194
1195 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
1196 {
1197 HRESULT hres;
1198
1199 func->code_off = ctx->instr_cnt;
1200
1201 ctx->sub_end_label = 0;
1202 ctx->func_end_label = 0;
1203 ctx->prop_end_label = 0;
1204
1205 switch(func->type) {
1206 case FUNC_FUNCTION:
1207 ctx->func_end_label = alloc_label(ctx);
1208 if(!ctx->func_end_label)
1209 return E_OUTOFMEMORY;
1210 break;
1211 case FUNC_SUB:
1212 ctx->sub_end_label = alloc_label(ctx);
1213 if(!ctx->sub_end_label)
1214 return E_OUTOFMEMORY;
1215 break;
1216 case FUNC_PROPGET:
1217 case FUNC_PROPLET:
1218 case FUNC_PROPSET:
1219 case FUNC_DEFGET:
1220 ctx->prop_end_label = alloc_label(ctx);
1221 if(!ctx->prop_end_label)
1222 return E_OUTOFMEMORY;
1223 break;
1224 case FUNC_GLOBAL:
1225 break;
1226 }
1227
1228 ctx->func = func;
1229 ctx->dim_decls = NULL;
1230 ctx->const_decls = NULL;
1231 hres = compile_statement(ctx, NULL, stat);
1232 ctx->func = NULL;
1233 if(FAILED(hres))
1234 return hres;
1235
1236 if(ctx->sub_end_label)
1237 label_set_addr(ctx, ctx->sub_end_label);
1238 if(ctx->func_end_label)
1239 label_set_addr(ctx, ctx->func_end_label);
1240 if(ctx->prop_end_label)
1241 label_set_addr(ctx, ctx->prop_end_label);
1242
1243 if(!push_instr(ctx, OP_ret))
1244 return E_OUTOFMEMORY;
1245
1246 resolve_labels(ctx, func->code_off);
1247
1248 if(func->var_cnt) {
1249 dim_decl_t *dim_decl;
1250
1251 if(func->type == FUNC_GLOBAL) {
1252 dynamic_var_t *new_var;
1253
1254 func->var_cnt = 0;
1255
1256 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1257 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
1258 if(!new_var)
1259 return E_OUTOFMEMORY;
1260
1261 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
1262 if(!new_var->name)
1263 return E_OUTOFMEMORY;
1264
1265 V_VT(&new_var->v) = VT_EMPTY;
1266 new_var->is_const = FALSE;
1267
1268 new_var->next = ctx->global_vars;
1269 ctx->global_vars = new_var;
1270 }
1271 }else {
1272 unsigned i;
1273
1274 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1275 if(!func->vars)
1276 return E_OUTOFMEMORY;
1277
1278 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1279 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1280 if(!func->vars[i].name)
1281 return E_OUTOFMEMORY;
1282 }
1283
1284 assert(i == func->var_cnt);
1285 }
1286 }
1287
1288 return S_OK;
1289 }
1290
1291 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1292 {
1293 function_t *iter;
1294
1295 for(iter = ctx->funcs; iter; iter = iter->next) {
1296 if(!strcmpiW(iter->name, name))
1297 return TRUE;
1298 }
1299
1300 return FALSE;
1301 }
1302
1303 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1304 {
1305 function_t *func;
1306 HRESULT hres;
1307
1308 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1309 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1310 return E_FAIL;
1311 }
1312
1313 func = compiler_alloc(ctx->code, sizeof(*func));
1314 if(!func)
1315 return E_OUTOFMEMORY;
1316
1317 func->name = compiler_alloc_string(ctx->code, decl->name);
1318 if(!func->name)
1319 return E_OUTOFMEMORY;
1320
1321 func->vars = NULL;
1322 func->var_cnt = 0;
1323 func->code_ctx = ctx->code;
1324 func->type = decl->type;
1325 func->is_public = decl->is_public;
1326
1327 func->arg_cnt = 0;
1328 if(decl->args) {
1329 arg_decl_t *arg;
1330 unsigned i;
1331
1332 for(arg = decl->args; arg; arg = arg->next)
1333 func->arg_cnt++;
1334
1335 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1336 if(!func->args)
1337 return E_OUTOFMEMORY;
1338
1339 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1340 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1341 if(!func->args[i].name)
1342 return E_OUTOFMEMORY;
1343 func->args[i].by_ref = arg->by_ref;
1344 }
1345 }else {
1346 func->args = NULL;
1347 }
1348
1349 hres = compile_func(ctx, decl->body, func);
1350 if(FAILED(hres))
1351 return hres;
1352
1353 *ret = func;
1354 return S_OK;
1355 }
1356
1357 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1358 {
1359 class_desc_t *iter;
1360
1361 for(iter = ctx->classes; iter; iter = iter->next) {
1362 if(!strcmpiW(iter->name, name))
1363 return TRUE;
1364 }
1365
1366 return FALSE;
1367 }
1368
1369 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1370 {
1371 vbdisp_invoke_type_t invoke_type;
1372 function_decl_t *funcprop_decl;
1373 HRESULT hres;
1374
1375 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1376 if(!desc->name)
1377 return E_OUTOFMEMORY;
1378
1379 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1380 switch(funcprop_decl->type) {
1381 case FUNC_FUNCTION:
1382 case FUNC_SUB:
1383 case FUNC_PROPGET:
1384 case FUNC_DEFGET:
1385 invoke_type = VBDISP_CALLGET;
1386 break;
1387 case FUNC_PROPLET:
1388 invoke_type = VBDISP_LET;
1389 break;
1390 case FUNC_PROPSET:
1391 invoke_type = VBDISP_SET;
1392 break;
1393 DEFAULT_UNREACHABLE;
1394 }
1395
1396 assert(!desc->entries[invoke_type]);
1397
1398 if(funcprop_decl->is_public)
1399 desc->is_public = TRUE;
1400
1401 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1402 if(FAILED(hres))
1403 return hres;
1404 }
1405
1406 return S_OK;
1407 }
1408
1409 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1410 {
1411 unsigned i;
1412
1413 for(i=0; i < class_desc->func_cnt; i++) {
1414 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1415 return TRUE;
1416 }
1417
1418 return FALSE;
1419 }
1420
1421 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1422 {
1423 function_decl_t *func_decl, *func_prop_decl;
1424 class_prop_decl_t *prop_decl;
1425 class_desc_t *class_desc;
1426 unsigned i;
1427 HRESULT hres;
1428
1429 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1430 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1431
1432 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1433 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1434 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1435 return E_FAIL;
1436 }
1437
1438 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1439 if(!class_desc)
1440 return E_OUTOFMEMORY;
1441
1442 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1443 if(!class_desc->name)
1444 return E_OUTOFMEMORY;
1445
1446 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1447
1448 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1449 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1450 if(func_prop_decl->type == FUNC_DEFGET)
1451 break;
1452 }
1453 if(!func_prop_decl)
1454 class_desc->func_cnt++;
1455 }
1456
1457 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1458 if(!class_desc->funcs)
1459 return E_OUTOFMEMORY;
1460 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1461
1462 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1463 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1464 if(func_prop_decl->type == FUNC_DEFGET) {
1465 i--;
1466 break;
1467 }
1468 }
1469
1470 if(!strcmpiW(class_initializeW, func_decl->name)) {
1471 if(func_decl->type != FUNC_SUB) {
1472 FIXME("class initializer is not sub\n");
1473 return E_FAIL;
1474 }
1475
1476 class_desc->class_initialize_id = i;
1477 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1478 if(func_decl->type != FUNC_SUB) {
1479 FIXME("class terminator is not sub\n");
1480 return E_FAIL;
1481 }
1482
1483 class_desc->class_terminate_id = i;
1484 }
1485
1486 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1487 if(FAILED(hres))
1488 return hres;
1489 }
1490
1491 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1492 class_desc->prop_cnt++;
1493
1494 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1495 if(!class_desc->props)
1496 return E_OUTOFMEMORY;
1497
1498 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1499 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1500 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1501 return E_FAIL;
1502 }
1503
1504 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1505 if(!class_desc->props[i].name)
1506 return E_OUTOFMEMORY;
1507
1508 class_desc->props[i].is_public = prop_decl->is_public;
1509 }
1510
1511 class_desc->next = ctx->classes;
1512 ctx->classes = class_desc;
1513 return S_OK;
1514 }
1515
1516 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1517 {
1518 class_desc_t *class;
1519 dynamic_var_t *var;
1520 function_t *func;
1521
1522 for(var = script->global_vars; var; var = var->next) {
1523 if(!strcmpiW(var->name, identifier))
1524 return TRUE;
1525 }
1526
1527 for(func = script->global_funcs; func; func = func->next) {
1528 if(!strcmpiW(func->name, identifier))
1529 return TRUE;
1530 }
1531
1532 for(class = script->classes; class; class = class->next) {
1533 if(!strcmpiW(class->name, identifier))
1534 return TRUE;
1535 }
1536
1537 return FALSE;
1538 }
1539
1540 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1541 {
1542 class_desc_t *class;
1543 dynamic_var_t *var;
1544 function_t *func;
1545
1546 for(var = ctx->global_vars; var; var = var->next) {
1547 if(lookup_script_identifier(script, var->name)) {
1548 FIXME("%s: redefined\n", debugstr_w(var->name));
1549 return E_FAIL;
1550 }
1551 }
1552
1553 for(func = ctx->funcs; func; func = func->next) {
1554 if(lookup_script_identifier(script, func->name)) {
1555 FIXME("%s: redefined\n", debugstr_w(func->name));
1556 return E_FAIL;
1557 }
1558 }
1559
1560 for(class = ctx->classes; class; class = class->next) {
1561 if(lookup_script_identifier(script, class->name)) {
1562 FIXME("%s: redefined\n", debugstr_w(class->name));
1563 return E_FAIL;
1564 }
1565 }
1566
1567 return S_OK;
1568 }
1569
1570 void release_vbscode(vbscode_t *code)
1571 {
1572 unsigned i;
1573
1574 list_remove(&code->entry);
1575
1576 for(i=0; i < code->bstr_cnt; i++)
1577 SysFreeString(code->bstr_pool[i]);
1578
1579 heap_pool_free(&code->heap);
1580
1581 heap_free(code->bstr_pool);
1582 heap_free(code->source);
1583 heap_free(code->instrs);
1584 heap_free(code);
1585 }
1586
1587 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1588 {
1589 vbscode_t *ret;
1590
1591 ret = heap_alloc(sizeof(*ret));
1592 if(!ret)
1593 return NULL;
1594
1595 ret->source = heap_strdupW(source);
1596 if(!ret->source) {
1597 heap_free(ret);
1598 return NULL;
1599 }
1600
1601 ret->instrs = heap_alloc(32*sizeof(instr_t));
1602 if(!ret->instrs) {
1603 release_vbscode(ret);
1604 return NULL;
1605 }
1606
1607 ctx->instr_cnt = 1;
1608 ctx->instr_size = 32;
1609 heap_pool_init(&ret->heap);
1610
1611 ret->option_explicit = ctx->parser.option_explicit;
1612
1613 ret->bstr_pool = NULL;
1614 ret->bstr_pool_size = 0;
1615 ret->bstr_cnt = 0;
1616 ret->pending_exec = FALSE;
1617
1618 ret->main_code.type = FUNC_GLOBAL;
1619 ret->main_code.name = NULL;
1620 ret->main_code.code_ctx = ret;
1621 ret->main_code.vars = NULL;
1622 ret->main_code.var_cnt = 0;
1623 ret->main_code.arg_cnt = 0;
1624 ret->main_code.args = NULL;
1625
1626 list_init(&ret->entry);
1627 return ret;
1628 }
1629
1630 static void release_compiler(compile_ctx_t *ctx)
1631 {
1632 parser_release(&ctx->parser);
1633 heap_free(ctx->labels);
1634 if(ctx->code)
1635 release_vbscode(ctx->code);
1636 }
1637
1638 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
1639 {
1640 function_t *new_func;
1641 function_decl_t *func_decl;
1642 class_decl_t *class_decl;
1643 compile_ctx_t ctx;
1644 vbscode_t *code;
1645 HRESULT hres;
1646
1647 hres = parse_script(&ctx.parser, src, delimiter);
1648 if(FAILED(hres))
1649 return hres;
1650
1651 code = ctx.code = alloc_vbscode(&ctx, src);
1652 if(!ctx.code)
1653 return E_OUTOFMEMORY;
1654
1655 ctx.funcs = NULL;
1656 ctx.func_decls = NULL;
1657 ctx.global_vars = NULL;
1658 ctx.dim_decls = NULL;
1659 ctx.classes = NULL;
1660 ctx.labels = NULL;
1661 ctx.global_consts = NULL;
1662 ctx.stat_ctx = NULL;
1663 ctx.labels_cnt = ctx.labels_size = 0;
1664
1665 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
1666 if(FAILED(hres)) {
1667 release_compiler(&ctx);
1668 return hres;
1669 }
1670
1671 ctx.global_consts = ctx.const_decls;
1672
1673 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1674 hres = create_function(&ctx, func_decl, &new_func);
1675 if(FAILED(hres)) {
1676 release_compiler(&ctx);
1677 return hres;
1678 }
1679
1680 new_func->next = ctx.funcs;
1681 ctx.funcs = new_func;
1682 }
1683
1684 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1685 hres = compile_class(&ctx, class_decl);
1686 if(FAILED(hres)) {
1687 release_compiler(&ctx);
1688 return hres;
1689 }
1690 }
1691
1692 hres = check_script_collisions(&ctx, script);
1693 if(FAILED(hres)) {
1694 release_compiler(&ctx);
1695 return hres;
1696 }
1697
1698 if(ctx.global_vars) {
1699 dynamic_var_t *var;
1700
1701 for(var = ctx.global_vars; var->next; var = var->next);
1702
1703 var->next = script->global_vars;
1704 script->global_vars = ctx.global_vars;
1705 }
1706
1707 if(ctx.funcs) {
1708 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1709
1710 new_func->next = script->global_funcs;
1711 script->global_funcs = ctx.funcs;
1712 }
1713
1714 if(ctx.classes) {
1715 class_desc_t *class = ctx.classes;
1716
1717 while(1) {
1718 class->ctx = script;
1719 if(!class->next)
1720 break;
1721 class = class->next;
1722 }
1723
1724 class->next = script->classes;
1725 script->classes = ctx.classes;
1726 }
1727
1728 if(TRACE_ON(vbscript_disas))
1729 dump_code(&ctx);
1730
1731 ctx.code = NULL;
1732 release_compiler(&ctx);
1733
1734 list_add_tail(&script->code_list, &code->entry);
1735 *ret = code;
1736 return S_OK;
1737 }