vbscript: Added On Error statement parser implementation.
[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 {
31     parser_ctx_t parser;
32
33     unsigned instr_cnt;
34     unsigned instr_size;
35     vbscode_t *code;
36
37     unsigned *labels;
38     unsigned labels_size;
39     unsigned labels_cnt;
40
41     unsigned while_end_label;
42     unsigned sub_end_label;
43     unsigned func_end_label;
44     unsigned prop_end_label;
45
46     dim_decl_t *dim_decls;
47     dynamic_var_t *global_vars;
48
49     function_t *func;
50     function_t *funcs;
51     function_decl_t *func_decls;
52
53     class_desc_t *classes;
54 } compile_ctx_t;
55
56 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
57 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
58
59 static const struct {
60     const char *op_str;
61     instr_arg_type_t arg1_type;
62     instr_arg_type_t arg2_type;
63 } instr_info[] = {
64 #define X(n,a,b,c) {#n,b,c},
65 OP_LIST
66 #undef X
67 };
68
69 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
70 {
71     switch(type) {
72     case ARG_STR:
73     case ARG_BSTR:
74         TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
75         break;
76     case ARG_INT:
77         TRACE_(vbscript_disas)("\t%d", arg->uint);
78         break;
79     case ARG_UINT:
80     case ARG_ADDR:
81         TRACE_(vbscript_disas)("\t%u", arg->uint);
82         break;
83     case ARG_DOUBLE:
84         TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
85         break;
86     case ARG_NONE:
87         break;
88     default:
89         assert(0);
90     }
91 }
92
93 static void dump_code(compile_ctx_t *ctx)
94 {
95     instr_t *instr;
96
97     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
98         TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
99         dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
100         dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
101         TRACE_(vbscript_disas)("\n");
102     }
103 }
104
105 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
106 {
107     return vbsheap_alloc(&vbscode->heap, size);
108 }
109
110 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
111 {
112     void *ret;
113
114     ret = vbsheap_alloc(&vbscode->heap, size);
115     if(ret)
116         memset(ret, 0, size);
117     return ret;
118 }
119
120 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
121 {
122     size_t size;
123     WCHAR *ret;
124
125     size = (strlenW(str)+1)*sizeof(WCHAR);
126     ret = compiler_alloc(vbscode, size);
127     if(ret)
128         memcpy(ret, str, size);
129     return ret;
130 }
131
132 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
133 {
134     assert(id < ctx->instr_cnt);
135     return ctx->code->instrs + id;
136 }
137
138 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
139 {
140     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
141
142     if(ctx->instr_size == ctx->instr_cnt) {
143         instr_t *new_instr;
144
145         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
146         if(!new_instr)
147             return -1;
148
149         ctx->code->instrs = new_instr;
150         ctx->instr_size *= 2;
151     }
152
153     ctx->code->instrs[ctx->instr_cnt].op = op;
154     return ctx->instr_cnt++;
155 }
156
157 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
158 {
159     unsigned ret;
160
161     ret = push_instr(ctx, op);
162     if(ret == -1)
163         return E_OUTOFMEMORY;
164
165     instr_ptr(ctx, ret)->arg1.lng = arg;
166     return S_OK;
167 }
168
169 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
170 {
171     unsigned ret;
172
173     ret = push_instr(ctx, op);
174     if(ret == -1)
175         return E_OUTOFMEMORY;
176
177     instr_ptr(ctx, ret)->arg1.uint = arg;
178     return S_OK;
179 }
180
181 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
182 {
183     unsigned instr;
184     WCHAR *str;
185
186     str = compiler_alloc_string(ctx->code, arg);
187     if(!str)
188         return E_OUTOFMEMORY;
189
190     instr = push_instr(ctx, op);
191     if(instr == -1)
192         return E_OUTOFMEMORY;
193
194     instr_ptr(ctx, instr)->arg1.str = str;
195     return S_OK;
196 }
197
198 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
199 {
200     unsigned instr;
201     double *d;
202
203     d = compiler_alloc(ctx->code, sizeof(double));
204     if(!d)
205         return E_OUTOFMEMORY;
206
207     instr = push_instr(ctx, op);
208     if(instr == -1)
209         return E_OUTOFMEMORY;
210
211     *d = arg;
212     instr_ptr(ctx, instr)->arg1.dbl = d;
213     return S_OK;
214 }
215
216 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
217 {
218     if(!ctx->code->bstr_pool_size) {
219         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
220         if(!ctx->code->bstr_pool)
221             return NULL;
222         ctx->code->bstr_pool_size = 8;
223     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
224         BSTR *new_pool;
225
226         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
227         if(!new_pool)
228             return NULL;
229
230         ctx->code->bstr_pool = new_pool;
231         ctx->code->bstr_pool_size *= 2;
232     }
233
234     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
235     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
236         return NULL;
237
238     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
239 }
240
241 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
242 {
243     unsigned instr;
244     BSTR bstr;
245
246     bstr = alloc_bstr_arg(ctx, arg);
247     if(!bstr)
248         return E_OUTOFMEMORY;
249
250     instr = push_instr(ctx, op);
251     if(instr == -1)
252         return E_OUTOFMEMORY;
253
254     instr_ptr(ctx, instr)->arg1.bstr = bstr;
255     return S_OK;
256 }
257
258 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
259 {
260     unsigned instr;
261     BSTR bstr;
262
263     bstr = alloc_bstr_arg(ctx, arg1);
264     if(!bstr)
265         return E_OUTOFMEMORY;
266
267     instr = push_instr(ctx, op);
268     if(instr == -1)
269         return E_OUTOFMEMORY;
270
271     instr_ptr(ctx, instr)->arg1.bstr = bstr;
272     instr_ptr(ctx, instr)->arg2.uint = arg2;
273     return S_OK;
274 }
275
276 #define LABEL_FLAG 0x80000000
277
278 static unsigned alloc_label(compile_ctx_t *ctx)
279 {
280     if(!ctx->labels_size) {
281         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
282         if(!ctx->labels)
283             return -1;
284         ctx->labels_size = 8;
285     }else if(ctx->labels_size == ctx->labels_cnt) {
286         unsigned *new_labels;
287
288         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
289         if(!new_labels)
290             return -1;
291
292         ctx->labels = new_labels;
293         ctx->labels_size *= 2;
294     }
295
296     return ctx->labels_cnt++ | LABEL_FLAG;
297 }
298
299 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
300 {
301     assert(label & LABEL_FLAG);
302     ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
303 }
304
305 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
306 {
307     unsigned arg_cnt = 0;
308     HRESULT hres;
309
310     while(args) {
311         hres = compile_expression(ctx, args);
312         if(FAILED(hres))
313             return hres;
314
315         arg_cnt++;
316         args = args->next;
317     }
318
319     *ret = arg_cnt;
320     return S_OK;
321 }
322
323 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
324 {
325     unsigned arg_cnt = 0;
326     HRESULT hres;
327
328     hres = compile_args(ctx, expr->args, &arg_cnt);
329     if(FAILED(hres))
330         return hres;
331
332     if(expr->obj_expr) {
333         hres = compile_expression(ctx, expr->obj_expr);
334         if(FAILED(hres))
335             return hres;
336
337         hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
338     }else {
339         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
340     }
341
342     return hres;
343 }
344
345 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
346 {
347     HRESULT hres;
348
349     hres = compile_expression(ctx, expr->subexpr);
350     if(FAILED(hres))
351         return hres;
352
353     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
354 }
355
356 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
357 {
358     HRESULT hres;
359
360     hres = compile_expression(ctx, expr->left);
361     if(FAILED(hres))
362         return hres;
363
364     hres = compile_expression(ctx, expr->right);
365     if(FAILED(hres))
366         return hres;
367
368     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
369 }
370
371 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
372 {
373     switch(expr->type) {
374     case EXPR_ADD:
375         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
376     case EXPR_AND:
377         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
378     case EXPR_BOOL:
379         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
380     case EXPR_CONCAT:
381         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
382     case EXPR_DIV:
383         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
384     case EXPR_DOUBLE:
385         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
386     case EXPR_EMPTY:
387         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
388     case EXPR_EQUAL:
389         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
390     case EXPR_EQV:
391         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
392     case EXPR_EXP:
393         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
394     case EXPR_GT:
395         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
396     case EXPR_GTEQ:
397         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
398     case EXPR_IDIV:
399         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
400     case EXPR_IS:
401         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
402     case EXPR_IMP:
403         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
404     case EXPR_LT:
405         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
406     case EXPR_LTEQ:
407         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
408     case EXPR_ME:
409         return push_instr(ctx, OP_me) != -1 ? S_OK : E_OUTOFMEMORY;
410     case EXPR_MEMBER:
411         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
412     case EXPR_MOD:
413         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
414     case EXPR_MUL:
415         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
416     case EXPR_NEG:
417         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
418     case EXPR_NEQUAL:
419         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
420     case EXPR_NEW:
421         return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
422     case EXPR_NOT:
423         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
424     case EXPR_NOTHING:
425         return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
426     case EXPR_NULL:
427         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
428     case EXPR_OR:
429         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
430     case EXPR_STRING:
431         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
432     case EXPR_SUB:
433         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
434     case EXPR_USHORT:
435         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
436     case EXPR_ULONG:
437         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
438     case EXPR_XOR:
439         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
440     default:
441         FIXME("Unimplemented expression type %d\n", expr->type);
442         return E_NOTIMPL;
443     }
444
445     return S_OK;
446 }
447
448 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
449 {
450     unsigned cnd_jmp, endif_label = -1;
451     elseif_decl_t *elseif_decl;
452     HRESULT hres;
453
454     hres = compile_expression(ctx, stat->expr);
455     if(FAILED(hres))
456         return hres;
457
458     cnd_jmp = push_instr(ctx, OP_jmp_false);
459     if(cnd_jmp == -1)
460         return E_OUTOFMEMORY;
461
462     hres = compile_statement(ctx, stat->if_stat);
463     if(FAILED(hres))
464         return hres;
465
466     if(stat->else_stat || stat->elseifs) {
467         endif_label = alloc_label(ctx);
468         if(endif_label == -1)
469             return E_OUTOFMEMORY;
470
471         hres = push_instr_addr(ctx, OP_jmp, endif_label);
472         if(FAILED(hres))
473             return hres;
474     }
475
476     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
477         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
478
479         hres = compile_expression(ctx, elseif_decl->expr);
480         if(FAILED(hres))
481             return hres;
482
483         cnd_jmp = push_instr(ctx, OP_jmp_false);
484         if(cnd_jmp == -1)
485             return E_OUTOFMEMORY;
486
487         hres = compile_statement(ctx, elseif_decl->stat);
488         if(FAILED(hres))
489             return hres;
490
491         hres = push_instr_addr(ctx, OP_jmp, endif_label);
492         if(FAILED(hres))
493             return hres;
494     }
495
496     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
497
498     if(stat->else_stat) {
499         hres = compile_statement(ctx, stat->else_stat);
500         if(FAILED(hres))
501             return hres;
502     }
503
504     if(endif_label != -1)
505         label_set_addr(ctx, endif_label);
506     return S_OK;
507 }
508
509 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
510 {
511     unsigned start_addr, prev_label;
512     unsigned jmp_end;
513     HRESULT hres;
514
515     start_addr = ctx->instr_cnt;
516
517     hres = compile_expression(ctx, stat->expr);
518     if(FAILED(hres))
519         return hres;
520
521     jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
522     if(jmp_end == -1)
523         return E_OUTOFMEMORY;
524
525     if(stat->stat.type != STAT_WHILE) {
526         prev_label = ctx->while_end_label;
527         if((ctx->while_end_label = alloc_label(ctx)) == -1)
528             return E_OUTOFMEMORY;
529     }
530
531     hres = compile_statement(ctx, stat->body);
532     if(FAILED(hres))
533         return hres;
534
535     hres = push_instr_addr(ctx, OP_jmp, start_addr);
536     if(FAILED(hres))
537         return hres;
538
539     instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
540
541     if(stat->stat.type != STAT_WHILE) {
542         label_set_addr(ctx, ctx->while_end_label);
543         ctx->while_end_label = prev_label;
544     }
545
546     return S_OK;
547 }
548
549 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
550 {
551     unsigned start_addr, prev_label;
552     HRESULT hres;
553
554     start_addr = ctx->instr_cnt;
555
556     prev_label = ctx->while_end_label;
557     if((ctx->while_end_label = alloc_label(ctx)) == -1)
558         return E_OUTOFMEMORY;
559
560     hres = compile_statement(ctx, stat->body);
561     if(FAILED(hres))
562         return hres;
563
564     hres = compile_expression(ctx, stat->expr);
565     if(FAILED(hres))
566         return hres;
567
568     hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
569     if(FAILED(hres))
570         return hres;
571
572     label_set_addr(ctx, ctx->while_end_label);
573     ctx->while_end_label = prev_label;
574     return S_OK;
575 }
576
577 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
578 {
579     HRESULT hres;
580
581     hres = compile_expression(ctx, stat->value_expr);
582     if(FAILED(hres))
583         return hres;
584
585     if(stat->member_expr->args) {
586         FIXME("arguments support not implemented\n");
587         return E_NOTIMPL;
588     }
589
590     if(stat->member_expr->obj_expr) {
591         hres = compile_expression(ctx, stat->member_expr->obj_expr);
592         if(FAILED(hres))
593             return hres;
594
595         hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
596     }else {
597         hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
598     }
599
600     return hres;
601 }
602
603 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
604 {
605     dim_decl_t *dim_decl;
606
607     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
608         if(!strcmpiW(dim_decl->name, name))
609             return TRUE;
610     }
611
612     return FALSE;
613 }
614
615 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
616 {
617     unsigned i;
618
619     for(i = 0; i < ctx->func->arg_cnt; i++) {
620         if(!strcmpiW(ctx->func->args[i].name, name))
621             return TRUE;
622     }
623
624     return FALSE;
625 }
626
627 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
628 {
629     dim_decl_t *dim_decl = stat->dim_decls;
630
631     while(1) {
632         if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
633             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
634             return E_FAIL;
635         }
636
637         if(!dim_decl->next)
638             break;
639         dim_decl = dim_decl->next;
640     }
641
642     dim_decl->next = ctx->dim_decls;
643     ctx->dim_decls = stat->dim_decls;
644     ctx->func->var_cnt++;
645     return S_OK;
646 }
647
648 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
649 {
650     if(ctx->func != &ctx->code->global_code) {
651         FIXME("Function is not in the global code\n");
652         return E_FAIL;
653     }
654
655     stat->func_decl->next = ctx->func_decls;
656     ctx->func_decls = stat->func_decl;
657     return S_OK;
658 }
659
660 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
661 {
662     if(ctx->while_end_label == -1) {
663         FIXME("Exit Do outside Do Loop\n");
664         return E_FAIL;
665     }
666
667     return push_instr_addr(ctx, OP_jmp, ctx->while_end_label);
668 }
669
670 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
671 {
672     if(ctx->sub_end_label == -1) {
673         FIXME("Exit Sub outside Sub?\n");
674         return E_FAIL;
675     }
676
677     return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
678 }
679
680 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
681 {
682     if(ctx->func_end_label == -1) {
683         FIXME("Exit Function outside Function?\n");
684         return E_FAIL;
685     }
686
687     return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
688 }
689
690 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
691 {
692     if(ctx->prop_end_label == -1) {
693         FIXME("Exit Property outside Property?\n");
694         return E_FAIL;
695     }
696
697     return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
698 }
699
700 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
701 {
702     return push_instr_int(ctx, OP_errmode, stat->resume_next);
703 }
704
705 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
706 {
707     HRESULT hres;
708
709     while(stat) {
710         switch(stat->type) {
711         case STAT_ASSIGN:
712             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
713             break;
714         case STAT_CALL:
715             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
716             break;
717         case STAT_DIM:
718             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
719             break;
720         case STAT_DOWHILE:
721         case STAT_DOUNTIL:
722             hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
723             break;
724         case STAT_EXITDO:
725             hres = compile_exitdo_statement(ctx);
726             break;
727         case STAT_EXITFUNC:
728             hres = compile_exitfunc_statement(ctx);
729             break;
730         case STAT_EXITPROP:
731             hres = compile_exitprop_statement(ctx);
732             break;
733         case STAT_EXITSUB:
734             hres = compile_exitsub_statement(ctx);
735             break;
736         case STAT_FUNC:
737             hres = compile_function_statement(ctx, (function_statement_t*)stat);
738             break;
739         case STAT_IF:
740             hres = compile_if_statement(ctx, (if_statement_t*)stat);
741             break;
742         case STAT_ONERROR:
743             hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
744             break;
745         case STAT_SET:
746             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
747             break;
748         case STAT_STOP:
749             hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
750             break;
751         case STAT_UNTIL:
752         case STAT_WHILE:
753         case STAT_WHILELOOP:
754             hres = compile_while_statement(ctx, (while_statement_t*)stat);
755             break;
756         default:
757             FIXME("Unimplemented statement type %d\n", stat->type);
758             hres = E_NOTIMPL;
759         }
760
761         if(FAILED(hres))
762             return hres;
763         stat = stat->next;
764     }
765
766     return S_OK;
767 }
768
769 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
770 {
771     instr_t *instr;
772
773     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
774         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
775             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
776             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
777         }
778         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
779     }
780
781     ctx->labels_cnt = 0;
782 }
783
784 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
785 {
786     HRESULT hres;
787
788     func->code_off = ctx->instr_cnt;
789
790     ctx->while_end_label = -1;
791     ctx->sub_end_label = -1;
792     ctx->func_end_label = -1;
793     ctx->prop_end_label = -1;
794
795     switch(func->type) {
796     case FUNC_FUNCTION:
797         ctx->func_end_label = alloc_label(ctx);
798         if(ctx->func_end_label == -1)
799             return E_OUTOFMEMORY; /* FIXME ! */
800         break;
801     case FUNC_SUB:
802         ctx->sub_end_label = alloc_label(ctx);
803         if(ctx->sub_end_label == -1)
804             return E_OUTOFMEMORY;
805         break;
806     case FUNC_PROPGET:
807     case FUNC_PROPLET:
808     case FUNC_PROPSET:
809     case FUNC_DEFGET:
810         ctx->prop_end_label = alloc_label(ctx);
811         if(ctx->prop_end_label == -1)
812             return E_OUTOFMEMORY;
813         break;
814     case FUNC_GLOBAL:
815         break;
816     }
817
818     ctx->func = func;
819     ctx->dim_decls = NULL;
820     hres = compile_statement(ctx, stat);
821     ctx->func = NULL;
822     if(FAILED(hres))
823         return hres;
824
825     assert(ctx->while_end_label == -1);
826
827     if(ctx->sub_end_label != -1)
828         label_set_addr(ctx, ctx->sub_end_label);
829     if(ctx->func_end_label != -1)
830         label_set_addr(ctx, ctx->func_end_label);
831     if(ctx->prop_end_label != -1)
832         label_set_addr(ctx, ctx->prop_end_label);
833
834     if(push_instr(ctx, OP_ret) == -1)
835         return E_OUTOFMEMORY;
836
837     resolve_labels(ctx, func->code_off);
838
839     if(func->var_cnt) {
840         dim_decl_t *dim_decl;
841
842         if(func->type == FUNC_GLOBAL) {
843             dynamic_var_t *new_var;
844
845             func->var_cnt = 0;
846
847             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
848                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
849                 if(!new_var)
850                     return E_OUTOFMEMORY;
851
852                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
853                 if(!new_var->name)
854                     return E_OUTOFMEMORY;
855
856                 V_VT(&new_var->v) = VT_EMPTY;
857
858                 new_var->next = ctx->global_vars;
859                 ctx->global_vars = new_var;
860             }
861         }else {
862             unsigned i;
863
864             func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
865             if(!func->vars)
866                 return E_OUTOFMEMORY;
867
868             for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
869                 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
870                 if(!func->vars[i].name)
871                     return E_OUTOFMEMORY;
872             }
873
874             assert(i == func->var_cnt);
875         }
876     }
877
878     return S_OK;
879 }
880
881 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
882 {
883     function_t *iter;
884
885     for(iter = ctx->funcs; iter; iter = iter->next) {
886         if(!strcmpiW(iter->name, name))
887             return TRUE;
888     }
889
890     return FALSE;
891 }
892
893 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
894 {
895     function_t *func;
896     HRESULT hres;
897
898     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
899         FIXME("%s: redefinition\n", debugstr_w(decl->name));
900         return E_FAIL;
901     }
902
903     func = compiler_alloc(ctx->code, sizeof(*func));
904     if(!func)
905         return E_OUTOFMEMORY;
906
907     func->name = compiler_alloc_string(ctx->code, decl->name);
908     if(!func->name)
909         return E_OUTOFMEMORY;
910
911     func->vars = NULL;
912     func->var_cnt = 0;
913     func->code_ctx = ctx->code;
914     func->type = decl->type;
915     func->is_public = decl->is_public;
916
917     func->arg_cnt = 0;
918     if(decl->args) {
919         arg_decl_t *arg;
920         unsigned i;
921
922         for(arg = decl->args; arg; arg = arg->next)
923             func->arg_cnt++;
924
925         func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
926         if(!func->args)
927             return E_OUTOFMEMORY;
928
929         for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
930             func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
931             if(!func->args[i].name)
932                 return E_OUTOFMEMORY;
933             func->args[i].by_ref = arg->by_ref;
934         }
935     }else {
936         func->args = NULL;
937     }
938
939     hres = compile_func(ctx, decl->body, func);
940     if(FAILED(hres))
941         return hres;
942
943     *ret = func;
944     return S_OK;
945 }
946
947 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
948 {
949     class_desc_t *iter;
950
951     for(iter = ctx->classes; iter; iter = iter->next) {
952         if(!strcmpiW(iter->name, name))
953             return TRUE;
954     }
955
956     return FALSE;
957 }
958
959 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
960 {
961     vbdisp_invoke_type_t invoke_type;
962     function_decl_t *funcprop_decl;
963     HRESULT hres;
964
965     desc->name = compiler_alloc_string(ctx->code, func_decl->name);
966     if(!desc->name)
967         return E_OUTOFMEMORY;
968
969     for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
970         switch(funcprop_decl->type) {
971         case FUNC_FUNCTION:
972         case FUNC_SUB:
973         case FUNC_PROPGET:
974         case FUNC_DEFGET:
975             invoke_type = VBDISP_CALLGET;
976             break;
977         case FUNC_PROPLET:
978             invoke_type = VBDISP_LET;
979             break;
980         case FUNC_PROPSET:
981             invoke_type = VBDISP_SET;
982             break;
983         default:
984             assert(0);
985         }
986
987         assert(!desc->entries[invoke_type]);
988
989         if(funcprop_decl->is_public)
990             desc->is_public = TRUE;
991
992         hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
993         if(FAILED(hres))
994             return hres;
995     }
996
997     return S_OK;
998 }
999
1000 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1001 {
1002     unsigned i;
1003
1004     for(i=0; i < class_desc->func_cnt; i++) {
1005         if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1006             return TRUE;
1007     }
1008
1009     return FALSE;
1010 }
1011
1012 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1013 {
1014     function_decl_t *func_decl, *func_prop_decl;
1015     class_prop_decl_t *prop_decl;
1016     class_desc_t *class_desc;
1017     unsigned i;
1018     HRESULT hres;
1019
1020     static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1021     static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1022
1023     if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1024             || lookup_class_name(ctx, class_decl->name)) {
1025         FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1026         return E_FAIL;
1027     }
1028
1029     class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1030     if(!class_desc)
1031         return E_OUTOFMEMORY;
1032
1033     class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1034     if(!class_desc->name)
1035         return E_OUTOFMEMORY;
1036
1037     class_desc->func_cnt = 1; /* always allocate slot for default getter */
1038
1039     for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1040         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1041             if(func_prop_decl->type == FUNC_DEFGET)
1042                 break;
1043         }
1044         if(!func_prop_decl)
1045             class_desc->func_cnt++;
1046     }
1047
1048     class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1049     if(!class_desc->funcs)
1050         return E_OUTOFMEMORY;
1051     memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1052
1053     for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1054         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1055             if(func_prop_decl->type == FUNC_DEFGET) {
1056                 i--;
1057                 break;
1058             }
1059         }
1060
1061         if(!strcmpiW(class_initializeW, func_decl->name)) {
1062             if(func_decl->type != FUNC_SUB) {
1063                 FIXME("class initializer is not sub\n");
1064                 return E_FAIL;
1065             }
1066
1067             class_desc->class_initialize_id = i;
1068         }else  if(!strcmpiW(class_terminateW, func_decl->name)) {
1069             if(func_decl->type != FUNC_SUB) {
1070                 FIXME("class terminator is not sub\n");
1071                 return E_FAIL;
1072             }
1073
1074             class_desc->class_terminate_id = i;
1075         }
1076
1077         hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1078         if(FAILED(hres))
1079             return hres;
1080     }
1081
1082     for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1083         class_desc->prop_cnt++;
1084
1085     class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1086     if(!class_desc->props)
1087         return E_OUTOFMEMORY;
1088
1089     for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1090         if(lookup_class_funcs(class_desc, prop_decl->name)) {
1091             FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1092             return E_FAIL;
1093         }
1094
1095         class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1096         if(!class_desc->props[i].name)
1097             return E_OUTOFMEMORY;
1098
1099         class_desc->props[i].is_public = prop_decl->is_public;
1100     }
1101
1102     class_desc->next = ctx->classes;
1103     ctx->classes = class_desc;
1104     return S_OK;
1105 }
1106
1107 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1108 {
1109     class_desc_t *class;
1110     dynamic_var_t *var;
1111     function_t *func;
1112
1113     for(var = script->global_vars; var; var = var->next) {
1114         if(!strcmpiW(var->name, identifier))
1115             return TRUE;
1116     }
1117
1118     for(func = script->global_funcs; func; func = func->next) {
1119         if(!strcmpiW(func->name, identifier))
1120             return TRUE;
1121     }
1122
1123     for(class = script->classes; class; class = class->next) {
1124         if(!strcmpiW(class->name, identifier))
1125             return TRUE;
1126     }
1127
1128     return FALSE;
1129 }
1130
1131 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1132 {
1133     class_desc_t *class;
1134     dynamic_var_t *var;
1135     function_t *func;
1136
1137     for(var = ctx->global_vars; var; var = var->next) {
1138         if(lookup_script_identifier(script, var->name)) {
1139             FIXME("%s: redefined\n", debugstr_w(var->name));
1140             return E_FAIL;
1141         }
1142     }
1143
1144     for(func = ctx->funcs; func; func = func->next) {
1145         if(lookup_script_identifier(script, func->name)) {
1146             FIXME("%s: redefined\n", debugstr_w(func->name));
1147             return E_FAIL;
1148         }
1149     }
1150
1151     for(class = ctx->classes; class; class = class->next) {
1152         if(lookup_script_identifier(script, class->name)) {
1153             FIXME("%s: redefined\n", debugstr_w(class->name));
1154             return E_FAIL;
1155         }
1156     }
1157
1158     return S_OK;
1159 }
1160
1161 void release_vbscode(vbscode_t *code)
1162 {
1163     unsigned i;
1164
1165     list_remove(&code->entry);
1166
1167     for(i=0; i < code->bstr_cnt; i++)
1168         SysFreeString(code->bstr_pool[i]);
1169
1170     vbsheap_free(&code->heap);
1171
1172     heap_free(code->bstr_pool);
1173     heap_free(code->source);
1174     heap_free(code->instrs);
1175     heap_free(code);
1176 }
1177
1178 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1179 {
1180     vbscode_t *ret;
1181
1182     ret = heap_alloc(sizeof(*ret));
1183     if(!ret)
1184         return NULL;
1185
1186     ret->source = heap_strdupW(source);
1187     if(!ret->source) {
1188         heap_free(ret);
1189         return NULL;
1190     }
1191
1192     ret->instrs = heap_alloc(32*sizeof(instr_t));
1193     if(!ret->instrs) {
1194         release_vbscode(ret);
1195         return NULL;
1196     }
1197
1198     ctx->instr_cnt = 0;
1199     ctx->instr_size = 32;
1200     vbsheap_init(&ret->heap);
1201
1202     ret->option_explicit = ctx->parser.option_explicit;
1203
1204     ret->bstr_pool = NULL;
1205     ret->bstr_pool_size = 0;
1206     ret->bstr_cnt = 0;
1207     ret->global_executed = FALSE;
1208
1209     ret->global_code.type = FUNC_GLOBAL;
1210     ret->global_code.name = NULL;
1211     ret->global_code.code_ctx = ret;
1212     ret->global_code.vars = NULL;
1213     ret->global_code.var_cnt = 0;
1214     ret->global_code.arg_cnt = 0;
1215     ret->global_code.args = NULL;
1216
1217     list_init(&ret->entry);
1218     return ret;
1219 }
1220
1221 static void release_compiler(compile_ctx_t *ctx)
1222 {
1223     parser_release(&ctx->parser);
1224     heap_free(ctx->labels);
1225     if(ctx->code)
1226         release_vbscode(ctx->code);
1227 }
1228
1229 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1230 {
1231     function_t *new_func;
1232     function_decl_t *func_decl;
1233     class_decl_t *class_decl;
1234     compile_ctx_t ctx;
1235     vbscode_t *code;
1236     HRESULT hres;
1237
1238     hres = parse_script(&ctx.parser, src);
1239     if(FAILED(hres))
1240         return hres;
1241
1242     code = ctx.code = alloc_vbscode(&ctx, src);
1243     if(!ctx.code)
1244         return E_OUTOFMEMORY;
1245
1246     ctx.funcs = NULL;
1247     ctx.func_decls = NULL;
1248     ctx.global_vars = NULL;
1249     ctx.dim_decls = NULL;
1250     ctx.classes = NULL;
1251     ctx.labels = NULL;
1252     ctx.labels_cnt = ctx.labels_size = 0;
1253
1254     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1255     if(FAILED(hres)) {
1256         release_compiler(&ctx);
1257         return hres;
1258     }
1259
1260     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1261         hres = create_function(&ctx, func_decl, &new_func);
1262         if(FAILED(hres)) {
1263             release_compiler(&ctx);
1264             return hres;
1265         }
1266
1267         new_func->next = ctx.funcs;
1268         ctx.funcs = new_func;
1269     }
1270
1271     for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1272         hres = compile_class(&ctx, class_decl);
1273         if(FAILED(hres)) {
1274             release_compiler(&ctx);
1275             return hres;
1276         }
1277     }
1278
1279     hres = check_script_collisions(&ctx, script);
1280     if(FAILED(hres)) {
1281         release_compiler(&ctx);
1282         return hres;
1283     }
1284
1285     if(ctx.global_vars) {
1286         dynamic_var_t *var;
1287
1288         for(var = ctx.global_vars; var->next; var = var->next);
1289
1290         var->next = script->global_vars;
1291         script->global_vars = ctx.global_vars;
1292     }
1293
1294     if(ctx.funcs) {
1295         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1296
1297         new_func->next = script->global_funcs;
1298         script->global_funcs = ctx.funcs;
1299     }
1300
1301     if(ctx.classes) {
1302         class_desc_t *class = ctx.classes;
1303
1304         while(1) {
1305             class->ctx = script;
1306             if(!class->next)
1307                 break;
1308             class = class->next;
1309         }
1310
1311         class->next = script->classes;
1312         script->classes = ctx.classes;
1313     }
1314
1315     if(TRACE_ON(vbscript_disas))
1316         dump_code(&ctx);
1317
1318     ctx.code = NULL;
1319     release_compiler(&ctx);
1320
1321     list_add_tail(&script->code_list, &code->entry);
1322     *ret = code;
1323     return S_OK;
1324 }