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