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