vbscript: Added ME expression 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_statement(compile_ctx_t *ctx, statement_t *stat)
701 {
702     HRESULT hres;
703
704     while(stat) {
705         switch(stat->type) {
706         case STAT_ASSIGN:
707             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
708             break;
709         case STAT_CALL:
710             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
711             break;
712         case STAT_DIM:
713             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
714             break;
715         case STAT_DOWHILE:
716         case STAT_DOUNTIL:
717             hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
718             break;
719         case STAT_EXITDO:
720             hres = compile_exitdo_statement(ctx);
721             break;
722         case STAT_EXITFUNC:
723             hres = compile_exitfunc_statement(ctx);
724             break;
725         case STAT_EXITPROP:
726             hres = compile_exitprop_statement(ctx);
727             break;
728         case STAT_EXITSUB:
729             hres = compile_exitsub_statement(ctx);
730             break;
731         case STAT_FUNC:
732             hres = compile_function_statement(ctx, (function_statement_t*)stat);
733             break;
734         case STAT_IF:
735             hres = compile_if_statement(ctx, (if_statement_t*)stat);
736             break;
737         case STAT_SET:
738             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
739             break;
740         case STAT_STOP:
741             hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
742             break;
743         case STAT_UNTIL:
744         case STAT_WHILE:
745         case STAT_WHILELOOP:
746             hres = compile_while_statement(ctx, (while_statement_t*)stat);
747             break;
748         default:
749             FIXME("Unimplemented statement type %d\n", stat->type);
750             hres = E_NOTIMPL;
751         }
752
753         if(FAILED(hres))
754             return hres;
755         stat = stat->next;
756     }
757
758     return S_OK;
759 }
760
761 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
762 {
763     instr_t *instr;
764
765     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
766         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
767             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
768             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
769         }
770         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
771     }
772
773     ctx->labels_cnt = 0;
774 }
775
776 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
777 {
778     HRESULT hres;
779
780     func->code_off = ctx->instr_cnt;
781
782     ctx->while_end_label = -1;
783     ctx->sub_end_label = -1;
784     ctx->func_end_label = -1;
785     ctx->prop_end_label = -1;
786
787     switch(func->type) {
788     case FUNC_FUNCTION:
789         ctx->func_end_label = alloc_label(ctx);
790         if(ctx->func_end_label == -1)
791             return E_OUTOFMEMORY; /* FIXME ! */
792         break;
793     case FUNC_SUB:
794         ctx->sub_end_label = alloc_label(ctx);
795         if(ctx->sub_end_label == -1)
796             return E_OUTOFMEMORY;
797         break;
798     case FUNC_PROPGET:
799     case FUNC_PROPLET:
800     case FUNC_PROPSET:
801     case FUNC_DEFGET:
802         ctx->prop_end_label = alloc_label(ctx);
803         if(ctx->prop_end_label == -1)
804             return E_OUTOFMEMORY;
805         break;
806     case FUNC_GLOBAL:
807         break;
808     }
809
810     ctx->func = func;
811     ctx->dim_decls = NULL;
812     hres = compile_statement(ctx, stat);
813     ctx->func = NULL;
814     if(FAILED(hres))
815         return hres;
816
817     assert(ctx->while_end_label == -1);
818
819     if(ctx->sub_end_label != -1)
820         label_set_addr(ctx, ctx->sub_end_label);
821     if(ctx->func_end_label != -1)
822         label_set_addr(ctx, ctx->func_end_label);
823     if(ctx->prop_end_label != -1)
824         label_set_addr(ctx, ctx->prop_end_label);
825
826     if(push_instr(ctx, OP_ret) == -1)
827         return E_OUTOFMEMORY;
828
829     resolve_labels(ctx, func->code_off);
830
831     if(func->var_cnt) {
832         dim_decl_t *dim_decl;
833
834         if(func->type == FUNC_GLOBAL) {
835             dynamic_var_t *new_var;
836
837             func->var_cnt = 0;
838
839             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
840                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
841                 if(!new_var)
842                     return E_OUTOFMEMORY;
843
844                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
845                 if(!new_var->name)
846                     return E_OUTOFMEMORY;
847
848                 V_VT(&new_var->v) = VT_EMPTY;
849
850                 new_var->next = ctx->global_vars;
851                 ctx->global_vars = new_var;
852             }
853         }else {
854             unsigned i;
855
856             func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
857             if(!func->vars)
858                 return E_OUTOFMEMORY;
859
860             for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
861                 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
862                 if(!func->vars[i].name)
863                     return E_OUTOFMEMORY;
864             }
865
866             assert(i == func->var_cnt);
867         }
868     }
869
870     return S_OK;
871 }
872
873 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
874 {
875     function_t *iter;
876
877     for(iter = ctx->funcs; iter; iter = iter->next) {
878         if(!strcmpiW(iter->name, name))
879             return TRUE;
880     }
881
882     return FALSE;
883 }
884
885 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
886 {
887     function_t *func;
888     HRESULT hres;
889
890     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
891         FIXME("%s: redefinition\n", debugstr_w(decl->name));
892         return E_FAIL;
893     }
894
895     func = compiler_alloc(ctx->code, sizeof(*func));
896     if(!func)
897         return E_OUTOFMEMORY;
898
899     func->name = compiler_alloc_string(ctx->code, decl->name);
900     if(!func->name)
901         return E_OUTOFMEMORY;
902
903     func->vars = NULL;
904     func->var_cnt = 0;
905     func->code_ctx = ctx->code;
906     func->type = decl->type;
907     func->is_public = decl->is_public;
908
909     func->arg_cnt = 0;
910     if(decl->args) {
911         arg_decl_t *arg;
912         unsigned i;
913
914         for(arg = decl->args; arg; arg = arg->next)
915             func->arg_cnt++;
916
917         func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
918         if(!func->args)
919             return E_OUTOFMEMORY;
920
921         for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
922             func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
923             if(!func->args[i].name)
924                 return E_OUTOFMEMORY;
925             func->args[i].by_ref = arg->by_ref;
926         }
927     }else {
928         func->args = NULL;
929     }
930
931     hres = compile_func(ctx, decl->body, func);
932     if(FAILED(hres))
933         return hres;
934
935     *ret = func;
936     return S_OK;
937 }
938
939 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
940 {
941     class_desc_t *iter;
942
943     for(iter = ctx->classes; iter; iter = iter->next) {
944         if(!strcmpiW(iter->name, name))
945             return TRUE;
946     }
947
948     return FALSE;
949 }
950
951 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
952 {
953     vbdisp_invoke_type_t invoke_type;
954     function_decl_t *funcprop_decl;
955     HRESULT hres;
956
957     desc->name = compiler_alloc_string(ctx->code, func_decl->name);
958     if(!desc->name)
959         return E_OUTOFMEMORY;
960
961     for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
962         switch(funcprop_decl->type) {
963         case FUNC_FUNCTION:
964         case FUNC_SUB:
965         case FUNC_PROPGET:
966         case FUNC_DEFGET:
967             invoke_type = VBDISP_CALLGET;
968             break;
969         case FUNC_PROPLET:
970             invoke_type = VBDISP_LET;
971             break;
972         case FUNC_PROPSET:
973             invoke_type = VBDISP_SET;
974             break;
975         default:
976             assert(0);
977         }
978
979         assert(!desc->entries[invoke_type]);
980
981         if(funcprop_decl->is_public)
982             desc->is_public = TRUE;
983
984         hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
985         if(FAILED(hres))
986             return hres;
987     }
988
989     return S_OK;
990 }
991
992 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
993 {
994     unsigned i;
995
996     for(i=0; i < class_desc->func_cnt; i++) {
997         if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
998             return TRUE;
999     }
1000
1001     return FALSE;
1002 }
1003
1004 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1005 {
1006     function_decl_t *func_decl, *func_prop_decl;
1007     class_prop_decl_t *prop_decl;
1008     class_desc_t *class_desc;
1009     unsigned i;
1010     HRESULT hres;
1011
1012     static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1013     static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1014
1015     if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1016             || lookup_class_name(ctx, class_decl->name)) {
1017         FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1018         return E_FAIL;
1019     }
1020
1021     class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1022     if(!class_desc)
1023         return E_OUTOFMEMORY;
1024
1025     class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1026     if(!class_desc->name)
1027         return E_OUTOFMEMORY;
1028
1029     class_desc->func_cnt = 1; /* always allocate slot for default getter */
1030
1031     for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1032         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1033             if(func_prop_decl->type == FUNC_DEFGET)
1034                 break;
1035         }
1036         if(!func_prop_decl)
1037             class_desc->func_cnt++;
1038     }
1039
1040     class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1041     if(!class_desc->funcs)
1042         return E_OUTOFMEMORY;
1043     memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1044
1045     for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1046         for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1047             if(func_prop_decl->type == FUNC_DEFGET) {
1048                 i--;
1049                 break;
1050             }
1051         }
1052
1053         if(!strcmpiW(class_initializeW, func_decl->name)) {
1054             if(func_decl->type != FUNC_SUB) {
1055                 FIXME("class initializer is not sub\n");
1056                 return E_FAIL;
1057             }
1058
1059             class_desc->class_initialize_id = i;
1060         }else  if(!strcmpiW(class_terminateW, func_decl->name)) {
1061             if(func_decl->type != FUNC_SUB) {
1062                 FIXME("class terminator is not sub\n");
1063                 return E_FAIL;
1064             }
1065
1066             class_desc->class_terminate_id = i;
1067         }
1068
1069         hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1070         if(FAILED(hres))
1071             return hres;
1072     }
1073
1074     for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1075         class_desc->prop_cnt++;
1076
1077     class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1078     if(!class_desc->props)
1079         return E_OUTOFMEMORY;
1080
1081     for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1082         if(lookup_class_funcs(class_desc, prop_decl->name)) {
1083             FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1084             return E_FAIL;
1085         }
1086
1087         class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1088         if(!class_desc->props[i].name)
1089             return E_OUTOFMEMORY;
1090
1091         class_desc->props[i].is_public = prop_decl->is_public;
1092     }
1093
1094     class_desc->next = ctx->classes;
1095     ctx->classes = class_desc;
1096     return S_OK;
1097 }
1098
1099 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1100 {
1101     class_desc_t *class;
1102     dynamic_var_t *var;
1103     function_t *func;
1104
1105     for(var = script->global_vars; var; var = var->next) {
1106         if(!strcmpiW(var->name, identifier))
1107             return TRUE;
1108     }
1109
1110     for(func = script->global_funcs; func; func = func->next) {
1111         if(!strcmpiW(func->name, identifier))
1112             return TRUE;
1113     }
1114
1115     for(class = script->classes; class; class = class->next) {
1116         if(!strcmpiW(class->name, identifier))
1117             return TRUE;
1118     }
1119
1120     return FALSE;
1121 }
1122
1123 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1124 {
1125     class_desc_t *class;
1126     dynamic_var_t *var;
1127     function_t *func;
1128
1129     for(var = ctx->global_vars; var; var = var->next) {
1130         if(lookup_script_identifier(script, var->name)) {
1131             FIXME("%s: redefined\n", debugstr_w(var->name));
1132             return E_FAIL;
1133         }
1134     }
1135
1136     for(func = ctx->funcs; func; func = func->next) {
1137         if(lookup_script_identifier(script, func->name)) {
1138             FIXME("%s: redefined\n", debugstr_w(func->name));
1139             return E_FAIL;
1140         }
1141     }
1142
1143     for(class = ctx->classes; class; class = class->next) {
1144         if(lookup_script_identifier(script, class->name)) {
1145             FIXME("%s: redefined\n", debugstr_w(class->name));
1146             return E_FAIL;
1147         }
1148     }
1149
1150     return S_OK;
1151 }
1152
1153 void release_vbscode(vbscode_t *code)
1154 {
1155     unsigned i;
1156
1157     list_remove(&code->entry);
1158
1159     for(i=0; i < code->bstr_cnt; i++)
1160         SysFreeString(code->bstr_pool[i]);
1161
1162     vbsheap_free(&code->heap);
1163
1164     heap_free(code->bstr_pool);
1165     heap_free(code->source);
1166     heap_free(code->instrs);
1167     heap_free(code);
1168 }
1169
1170 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1171 {
1172     vbscode_t *ret;
1173
1174     ret = heap_alloc(sizeof(*ret));
1175     if(!ret)
1176         return NULL;
1177
1178     ret->source = heap_strdupW(source);
1179     if(!ret->source) {
1180         heap_free(ret);
1181         return NULL;
1182     }
1183
1184     ret->instrs = heap_alloc(32*sizeof(instr_t));
1185     if(!ret->instrs) {
1186         release_vbscode(ret);
1187         return NULL;
1188     }
1189
1190     ctx->instr_cnt = 0;
1191     ctx->instr_size = 32;
1192     vbsheap_init(&ret->heap);
1193
1194     ret->option_explicit = ctx->parser.option_explicit;
1195
1196     ret->bstr_pool = NULL;
1197     ret->bstr_pool_size = 0;
1198     ret->bstr_cnt = 0;
1199     ret->global_executed = FALSE;
1200
1201     ret->global_code.type = FUNC_GLOBAL;
1202     ret->global_code.name = NULL;
1203     ret->global_code.code_ctx = ret;
1204     ret->global_code.vars = NULL;
1205     ret->global_code.var_cnt = 0;
1206     ret->global_code.arg_cnt = 0;
1207     ret->global_code.args = NULL;
1208
1209     list_init(&ret->entry);
1210     return ret;
1211 }
1212
1213 static void release_compiler(compile_ctx_t *ctx)
1214 {
1215     parser_release(&ctx->parser);
1216     heap_free(ctx->labels);
1217     if(ctx->code)
1218         release_vbscode(ctx->code);
1219 }
1220
1221 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1222 {
1223     function_t *new_func;
1224     function_decl_t *func_decl;
1225     class_decl_t *class_decl;
1226     compile_ctx_t ctx;
1227     vbscode_t *code;
1228     HRESULT hres;
1229
1230     hres = parse_script(&ctx.parser, src);
1231     if(FAILED(hres))
1232         return hres;
1233
1234     code = ctx.code = alloc_vbscode(&ctx, src);
1235     if(!ctx.code)
1236         return E_OUTOFMEMORY;
1237
1238     ctx.funcs = NULL;
1239     ctx.func_decls = NULL;
1240     ctx.global_vars = NULL;
1241     ctx.dim_decls = NULL;
1242     ctx.classes = NULL;
1243     ctx.labels = NULL;
1244     ctx.labels_cnt = ctx.labels_size = 0;
1245
1246     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1247     if(FAILED(hres)) {
1248         release_compiler(&ctx);
1249         return hres;
1250     }
1251
1252     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1253         hres = create_function(&ctx, func_decl, &new_func);
1254         if(FAILED(hres)) {
1255             release_compiler(&ctx);
1256             return hres;
1257         }
1258
1259         new_func->next = ctx.funcs;
1260         ctx.funcs = new_func;
1261     }
1262
1263     for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1264         hres = compile_class(&ctx, class_decl);
1265         if(FAILED(hres)) {
1266             release_compiler(&ctx);
1267             return hres;
1268         }
1269     }
1270
1271     hres = check_script_collisions(&ctx, script);
1272     if(FAILED(hres)) {
1273         release_compiler(&ctx);
1274         return hres;
1275     }
1276
1277     if(ctx.global_vars) {
1278         dynamic_var_t *var;
1279
1280         for(var = ctx.global_vars; var->next; var = var->next);
1281
1282         var->next = script->global_vars;
1283         script->global_vars = ctx.global_vars;
1284     }
1285
1286     if(ctx.funcs) {
1287         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1288
1289         new_func->next = script->global_funcs;
1290         script->global_funcs = ctx.funcs;
1291     }
1292
1293     if(ctx.classes) {
1294         class_desc_t *class = ctx.classes;
1295
1296         while(1) {
1297             class->ctx = script;
1298             if(!class->next)
1299                 break;
1300             class = class->next;
1301         }
1302
1303         class->next = script->classes;
1304         script->classes = ctx.classes;
1305     }
1306
1307     if(TRACE_ON(vbscript_disas))
1308         dump_code(&ctx);
1309
1310     ctx.code = NULL;
1311     release_compiler(&ctx);
1312
1313     list_add_tail(&script->code_list, &code->entry);
1314     *ret = code;
1315     return S_OK;
1316 }