vbscript: Added nothing literal parser/compiler 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 sub_end_label;
42     unsigned func_end_label;
43
44     dim_decl_t *dim_decls;
45     dynamic_var_t *global_vars;
46
47     function_t *func;
48     function_t *funcs;
49     function_decl_t *func_decls;
50
51     class_desc_t *classes;
52 } compile_ctx_t;
53
54 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
55 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
56
57 static const struct {
58     const char *op_str;
59     instr_arg_type_t arg1_type;
60     instr_arg_type_t arg2_type;
61 } instr_info[] = {
62 #define X(n,a,b,c) {#n,b,c},
63 OP_LIST
64 #undef X
65 };
66
67 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
68 {
69     switch(type) {
70     case ARG_STR:
71     case ARG_BSTR:
72         TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
73         break;
74     case ARG_INT:
75         TRACE_(vbscript_disas)("\t%d", arg->uint);
76         break;
77     case ARG_UINT:
78     case ARG_ADDR:
79         TRACE_(vbscript_disas)("\t%u", arg->uint);
80         break;
81     case ARG_DOUBLE:
82         TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
83         break;
84     case ARG_NONE:
85         break;
86     default:
87         assert(0);
88     }
89 }
90
91 static void dump_code(compile_ctx_t *ctx)
92 {
93     instr_t *instr;
94
95     for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
96         TRACE_(vbscript_disas)("%d:\t%s", instr-ctx->code->instrs, instr_info[instr->op].op_str);
97         dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
98         dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
99         TRACE_(vbscript_disas)("\n");
100     }
101 }
102
103 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
104 {
105     return vbsheap_alloc(&vbscode->heap, size);
106 }
107
108 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
109 {
110     size_t size;
111     WCHAR *ret;
112
113     size = (strlenW(str)+1)*sizeof(WCHAR);
114     ret = compiler_alloc(vbscode, size);
115     if(ret)
116         memcpy(ret, str, size);
117     return ret;
118 }
119
120 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
121 {
122     assert(id < ctx->instr_cnt);
123     return ctx->code->instrs + id;
124 }
125
126 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
127 {
128     assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
129
130     if(ctx->instr_size == ctx->instr_cnt) {
131         instr_t *new_instr;
132
133         new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
134         if(!new_instr)
135             return -1;
136
137         ctx->code->instrs = new_instr;
138         ctx->instr_size *= 2;
139     }
140
141     ctx->code->instrs[ctx->instr_cnt].op = op;
142     return ctx->instr_cnt++;
143 }
144
145 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
146 {
147     unsigned ret;
148
149     ret = push_instr(ctx, op);
150     if(ret == -1)
151         return E_OUTOFMEMORY;
152
153     instr_ptr(ctx, ret)->arg1.lng = arg;
154     return S_OK;
155 }
156
157 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned 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.uint = arg;
166     return S_OK;
167 }
168
169 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
170 {
171     unsigned instr;
172     WCHAR *str;
173
174     str = compiler_alloc_string(ctx->code, arg);
175     if(!str)
176         return E_OUTOFMEMORY;
177
178     instr = push_instr(ctx, op);
179     if(instr == -1)
180         return E_OUTOFMEMORY;
181
182     instr_ptr(ctx, instr)->arg1.str = str;
183     return S_OK;
184 }
185
186 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
187 {
188     unsigned instr;
189     double *d;
190
191     d = compiler_alloc(ctx->code, sizeof(double));
192     if(!d)
193         return E_OUTOFMEMORY;
194
195     instr = push_instr(ctx, op);
196     if(instr == -1)
197         return E_OUTOFMEMORY;
198
199     *d = arg;
200     instr_ptr(ctx, instr)->arg1.dbl = d;
201     return S_OK;
202 }
203
204 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
205 {
206     if(!ctx->code->bstr_pool_size) {
207         ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
208         if(!ctx->code->bstr_pool)
209             return NULL;
210         ctx->code->bstr_pool_size = 8;
211     }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
212        BSTR *new_pool;
213
214         new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
215         if(!new_pool)
216             return NULL;
217
218         ctx->code->bstr_pool = new_pool;
219         ctx->code->bstr_pool_size *= 2;
220     }
221
222     ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
223     if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
224         return NULL;
225
226     return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
227 }
228
229 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
230 {
231     unsigned instr;
232     BSTR bstr;
233
234     bstr = alloc_bstr_arg(ctx, arg);
235     if(!bstr)
236         return E_OUTOFMEMORY;
237
238     instr = push_instr(ctx, op);
239     if(instr == -1)
240         return E_OUTOFMEMORY;
241
242     instr_ptr(ctx, instr)->arg1.bstr = bstr;
243     return S_OK;
244 }
245
246 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
247 {
248     unsigned instr;
249     BSTR bstr;
250
251     bstr = alloc_bstr_arg(ctx, arg1);
252     if(!bstr)
253         return E_OUTOFMEMORY;
254
255     instr = push_instr(ctx, op);
256     if(instr == -1)
257         return E_OUTOFMEMORY;
258
259     instr_ptr(ctx, instr)->arg1.bstr = bstr;
260     instr_ptr(ctx, instr)->arg2.uint = arg2;
261     return S_OK;
262 }
263
264 #define LABEL_FLAG 0x80000000
265
266 static unsigned alloc_label(compile_ctx_t *ctx)
267 {
268     if(!ctx->labels_size) {
269         ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
270         if(!ctx->labels)
271             return -1;
272         ctx->labels_size = 8;
273     }else if(ctx->labels_size == ctx->labels_cnt) {
274         unsigned *new_labels;
275
276         new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
277         if(!new_labels)
278             return -1;
279
280         ctx->labels = new_labels;
281         ctx->labels_size *= 2;
282     }
283
284     return ctx->labels_cnt++ | LABEL_FLAG;
285 }
286
287 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
288 {
289     assert(label & LABEL_FLAG);
290     ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
291 }
292
293 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
294 {
295     unsigned arg_cnt = 0;
296     HRESULT hres;
297
298     while(args) {
299         hres = compile_expression(ctx, args);
300         if(FAILED(hres))
301             return hres;
302
303         arg_cnt++;
304         args = args->next;
305     }
306
307     *ret = arg_cnt;
308     return S_OK;
309 }
310
311 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
312 {
313     unsigned arg_cnt = 0;
314     HRESULT hres;
315
316     hres = compile_args(ctx, expr->args, &arg_cnt);
317     if(FAILED(hres))
318         return hres;
319
320     if(expr->obj_expr) {
321         FIXME("obj_expr not implemented\n");
322         hres = E_NOTIMPL;
323     }else {
324         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
325     }
326
327     return hres;
328 }
329
330 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
331 {
332     HRESULT hres;
333
334     hres = compile_expression(ctx, expr->subexpr);
335     if(FAILED(hres))
336         return hres;
337
338     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
339 }
340
341 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
342 {
343     HRESULT hres;
344
345     hres = compile_expression(ctx, expr->left);
346     if(FAILED(hres))
347         return hres;
348
349     hres = compile_expression(ctx, expr->right);
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_expression(compile_ctx_t *ctx, expression_t *expr)
357 {
358     switch(expr->type) {
359     case EXPR_ADD:
360         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
361     case EXPR_AND:
362         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
363     case EXPR_BOOL:
364         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
365     case EXPR_CONCAT:
366         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
367     case EXPR_DIV:
368         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
369     case EXPR_DOUBLE:
370         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
371     case EXPR_EMPTY:
372         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
373     case EXPR_EQUAL:
374         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
375     case EXPR_EQV:
376         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
377     case EXPR_EXP:
378         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
379     case EXPR_IDIV:
380         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
381     case EXPR_IMP:
382         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
383     case EXPR_MEMBER:
384         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
385     case EXPR_MOD:
386         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
387     case EXPR_MUL:
388         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
389     case EXPR_NEG:
390         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
391     case EXPR_NEQUAL:
392         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
393     case EXPR_NEW:
394         return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
395     case EXPR_NOT:
396         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
397     case EXPR_NOTHING:
398         return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
399     case EXPR_NULL:
400         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
401     case EXPR_OR:
402         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
403     case EXPR_STRING:
404         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
405     case EXPR_SUB:
406         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
407     case EXPR_USHORT:
408         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
409     case EXPR_ULONG:
410         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
411     case EXPR_XOR:
412         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
413     default:
414         FIXME("Unimplemented expression type %d\n", expr->type);
415         return E_NOTIMPL;
416     }
417
418     return S_OK;
419 }
420
421 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
422 {
423     unsigned cnd_jmp, endif_label = -1;
424     elseif_decl_t *elseif_decl;
425     HRESULT hres;
426
427     hres = compile_expression(ctx, stat->expr);
428     if(FAILED(hres))
429         return hres;
430
431     cnd_jmp = push_instr(ctx, OP_jmp_false);
432     if(cnd_jmp == -1)
433         return E_OUTOFMEMORY;
434
435     hres = compile_statement(ctx, stat->if_stat);
436     if(FAILED(hres))
437         return hres;
438
439     if(stat->else_stat || stat->elseifs) {
440         endif_label = alloc_label(ctx);
441         if(endif_label == -1)
442             return E_OUTOFMEMORY;
443
444         hres = push_instr_addr(ctx, OP_jmp, endif_label);
445         if(FAILED(hres))
446             return hres;
447     }
448
449     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
450         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
451
452         hres = compile_expression(ctx, elseif_decl->expr);
453         if(FAILED(hres))
454             return hres;
455
456         cnd_jmp = push_instr(ctx, OP_jmp_false);
457         if(cnd_jmp == -1)
458             return E_OUTOFMEMORY;
459
460         hres = compile_statement(ctx, elseif_decl->stat);
461         if(FAILED(hres))
462             return hres;
463
464         hres = push_instr_addr(ctx, OP_jmp, endif_label);
465         if(FAILED(hres))
466             return hres;
467     }
468
469     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
470
471     if(stat->else_stat) {
472         hres = compile_statement(ctx, stat->else_stat);
473         if(FAILED(hres))
474             return hres;
475     }
476
477     if(endif_label != -1)
478         label_set_addr(ctx, endif_label);
479     return S_OK;
480 }
481
482 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
483 {
484     HRESULT hres;
485
486     hres = compile_expression(ctx, stat->value_expr);
487     if(FAILED(hres))
488         return hres;
489
490     if(stat->member_expr->args) {
491         FIXME("arguments support not implemented\n");
492         return E_NOTIMPL;
493     }
494
495     if(stat->member_expr->obj_expr) {
496         hres = compile_expression(ctx, stat->member_expr->obj_expr);
497         if(FAILED(hres))
498             return hres;
499
500         hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
501     }else {
502         hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
503     }
504
505     return hres;
506 }
507
508 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
509 {
510     dim_decl_t *dim_decl;
511
512     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
513         if(!strcmpiW(dim_decl->name, name))
514             return TRUE;
515     }
516
517     return FALSE;
518 }
519
520 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
521 {
522     unsigned i;
523
524     for(i = 0; i < ctx->func->arg_cnt; i++) {
525         if(!strcmpiW(ctx->func->args[i].name, name))
526             return TRUE;
527     }
528
529     return FALSE;
530 }
531
532 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
533 {
534     dim_decl_t *dim_decl = stat->dim_decls;
535
536     while(1) {
537         if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
538             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
539             return E_FAIL;
540         }
541
542         if(!dim_decl->next)
543             break;
544         dim_decl = dim_decl->next;
545     }
546
547     dim_decl->next = ctx->dim_decls;
548     ctx->dim_decls = stat->dim_decls;
549     ctx->func->var_cnt++;
550     return S_OK;
551 }
552
553 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
554 {
555     if(ctx->func != &ctx->code->global_code) {
556         FIXME("Function is not in the global code\n");
557         return E_FAIL;
558     }
559
560     stat->func_decl->next = ctx->func_decls;
561     ctx->func_decls = stat->func_decl;
562     return S_OK;
563 }
564
565 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
566 {
567     if(ctx->sub_end_label == -1) {
568         FIXME("Exit Sub outside Sub?\n");
569         return E_FAIL;
570     }
571
572     return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
573 }
574
575 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
576 {
577     if(ctx->func_end_label == -1) {
578         FIXME("Exit Function outside Function?\n");
579         return E_FAIL;
580     }
581
582     return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
583 }
584
585 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
586 {
587     HRESULT hres;
588
589     while(stat) {
590         switch(stat->type) {
591         case STAT_ASSIGN:
592             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
593             break;
594         case STAT_CALL:
595             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
596             break;
597         case STAT_DIM:
598             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
599             break;
600         case STAT_EXITFUNC:
601             hres = compile_exitfunc_statement(ctx);
602             break;
603         case STAT_EXITSUB:
604             hres = compile_exitsub_statement(ctx);
605             break;
606         case STAT_FUNC:
607             hres = compile_function_statement(ctx, (function_statement_t*)stat);
608             break;
609         case STAT_IF:
610             hres = compile_if_statement(ctx, (if_statement_t*)stat);
611             break;
612         case STAT_SET:
613             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
614             break;
615         default:
616             FIXME("Unimplemented statement type %d\n", stat->type);
617             hres = E_NOTIMPL;
618         }
619
620         if(FAILED(hres))
621             return hres;
622         stat = stat->next;
623     }
624
625     return S_OK;
626 }
627
628 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
629 {
630     instr_t *instr;
631
632     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
633         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
634             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
635             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
636         }
637         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
638     }
639
640     ctx->labels_cnt = 0;
641 }
642
643 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
644 {
645     HRESULT hres;
646
647     func->code_off = ctx->instr_cnt;
648
649     ctx->sub_end_label = -1;
650     ctx->func_end_label = -1;
651
652     switch(func->type) {
653     case FUNC_FUNCTION:
654         ctx->func_end_label = alloc_label(ctx);
655         if(ctx->func_end_label == -1)
656             return E_OUTOFMEMORY; /* FIXME ! */
657         break;
658     case FUNC_SUB:
659         ctx->sub_end_label = alloc_label(ctx);
660         if(ctx->sub_end_label == -1)
661             return E_OUTOFMEMORY;
662         break;
663     case FUNC_GLOBAL:
664         break;
665     }
666
667     ctx->func = func;
668     ctx->dim_decls = NULL;
669     hres = compile_statement(ctx, stat);
670     ctx->func = NULL;
671     if(FAILED(hres))
672         return hres;
673
674     if(ctx->sub_end_label != -1)
675         label_set_addr(ctx, ctx->sub_end_label);
676     if(ctx->func_end_label != -1)
677         label_set_addr(ctx, ctx->func_end_label);
678
679     if(push_instr(ctx, OP_ret) == -1)
680         return E_OUTOFMEMORY;
681
682     resolve_labels(ctx, func->code_off);
683
684     if(func->var_cnt) {
685         dim_decl_t *dim_decl;
686
687         if(func->type == FUNC_GLOBAL) {
688             dynamic_var_t *new_var;
689
690             func->var_cnt = 0;
691
692             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
693                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
694                 if(!new_var)
695                     return E_OUTOFMEMORY;
696
697                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
698                 if(!new_var->name)
699                     return E_OUTOFMEMORY;
700
701                 V_VT(&new_var->v) = VT_EMPTY;
702
703                 new_var->next = ctx->global_vars;
704                 ctx->global_vars = new_var;
705             }
706         }else {
707             unsigned i;
708
709             func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
710             if(!func->vars)
711                 return E_OUTOFMEMORY;
712
713             for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
714                 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
715                 if(!func->vars[i].name)
716                     return E_OUTOFMEMORY;
717             }
718
719             assert(i == func->var_cnt);
720         }
721     }
722
723     return S_OK;
724 }
725
726 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
727 {
728     function_t *iter;
729
730     for(iter = ctx->funcs; iter; iter = iter->next) {
731         if(!strcmpiW(iter->name, name))
732             return TRUE;
733     }
734
735     return FALSE;
736 }
737
738 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
739 {
740     function_t *func;
741     HRESULT hres;
742
743     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
744         FIXME("%s: redefinition\n", debugstr_w(decl->name));
745         return E_FAIL;
746     }
747
748     func = compiler_alloc(ctx->code, sizeof(*func));
749     if(!func)
750         return E_OUTOFMEMORY;
751
752     func->name = compiler_alloc_string(ctx->code, decl->name);
753     if(!func->name)
754         return E_OUTOFMEMORY;
755
756     func->vars = NULL;
757     func->var_cnt = 0;
758     func->code_ctx = ctx->code;
759     func->type = decl->type;
760
761     func->arg_cnt = 0;
762     if(decl->args) {
763         arg_decl_t *arg;
764         unsigned i;
765
766         for(arg = decl->args; arg; arg = arg->next)
767             func->arg_cnt++;
768
769         func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
770         if(!func->args)
771             return E_OUTOFMEMORY;
772
773         for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
774             func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
775             if(!func->args[i].name)
776                 return E_OUTOFMEMORY;
777             func->args[i].by_ref = arg->by_ref;
778         }
779     }else {
780         func->args = NULL;
781     }
782
783     hres = compile_func(ctx, decl->body, func);
784     if(FAILED(hres))
785         return hres;
786
787     *ret = func;
788     return S_OK;
789 }
790
791 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
792 {
793     class_desc_t *iter;
794
795     for(iter = ctx->classes; iter; iter = iter->next) {
796         if(!strcmpiW(iter->name, name))
797             return TRUE;
798     }
799
800     return FALSE;
801 }
802
803 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
804 {
805     class_desc_t *class_desc;
806
807     if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
808             || lookup_class_name(ctx, class_decl->name)) {
809         FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
810         return E_FAIL;
811     }
812
813     class_desc = compiler_alloc(ctx->code, sizeof(*class_desc));
814     if(!class_desc)
815         return E_OUTOFMEMORY;
816
817     class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
818     if(!class_desc->name)
819         return E_OUTOFMEMORY;
820
821     class_desc->next = ctx->classes;
822     ctx->classes = class_desc;
823     return S_OK;
824 }
825
826 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
827 {
828     class_desc_t *class;
829     dynamic_var_t *var;
830     function_t *func;
831
832     for(var = script->global_vars; var; var = var->next) {
833         if(!strcmpiW(var->name, identifier))
834             return TRUE;
835     }
836
837     for(func = script->global_funcs; func; func = func->next) {
838         if(!strcmpiW(func->name, identifier))
839             return TRUE;
840     }
841
842     for(class = script->classes; class; class = class->next) {
843         if(!strcmpiW(class->name, identifier))
844             return TRUE;
845     }
846
847     return FALSE;
848 }
849
850 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
851 {
852     class_desc_t *class;
853     dynamic_var_t *var;
854     function_t *func;
855
856     for(var = ctx->global_vars; var; var = var->next) {
857         if(lookup_script_identifier(script, var->name)) {
858             FIXME("%s: redefined\n", debugstr_w(var->name));
859             return E_FAIL;
860         }
861     }
862
863     for(func = ctx->funcs; func; func = func->next) {
864         if(lookup_script_identifier(script, func->name)) {
865             FIXME("%s: redefined\n", debugstr_w(func->name));
866             return E_FAIL;
867         }
868     }
869
870     for(class = ctx->classes; class; class = class->next) {
871         if(lookup_script_identifier(script, class->name)) {
872             FIXME("%s: redefined\n", debugstr_w(class->name));
873             return E_FAIL;
874         }
875     }
876
877     return S_OK;
878 }
879
880 void release_vbscode(vbscode_t *code)
881 {
882     unsigned i;
883
884     list_remove(&code->entry);
885
886     for(i=0; i < code->bstr_cnt; i++)
887         SysFreeString(code->bstr_pool[i]);
888
889     vbsheap_free(&code->heap);
890
891     heap_free(code->bstr_pool);
892     heap_free(code->source);
893     heap_free(code->instrs);
894     heap_free(code);
895 }
896
897 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
898 {
899     vbscode_t *ret;
900
901     ret = heap_alloc(sizeof(*ret));
902     if(!ret)
903         return NULL;
904
905     ret->source = heap_strdupW(source);
906     if(!ret->source) {
907         heap_free(ret);
908         return NULL;
909     }
910
911     ret->instrs = heap_alloc(32*sizeof(instr_t));
912     if(!ret->instrs) {
913         release_vbscode(ret);
914         return NULL;
915     }
916
917     ctx->instr_cnt = 0;
918     ctx->instr_size = 32;
919     vbsheap_init(&ret->heap);
920
921     ret->option_explicit = ctx->parser.option_explicit;
922
923     ret->bstr_pool = NULL;
924     ret->bstr_pool_size = 0;
925     ret->bstr_cnt = 0;
926     ret->global_executed = FALSE;
927
928     ret->global_code.type = FUNC_GLOBAL;
929     ret->global_code.name = NULL;
930     ret->global_code.code_ctx = ret;
931     ret->global_code.vars = NULL;
932     ret->global_code.var_cnt = 0;
933     ret->global_code.arg_cnt = 0;
934     ret->global_code.args = NULL;
935
936     list_init(&ret->entry);
937     return ret;
938 }
939
940 static void release_compiler(compile_ctx_t *ctx)
941 {
942     parser_release(&ctx->parser);
943     heap_free(ctx->labels);
944     if(ctx->code)
945         release_vbscode(ctx->code);
946 }
947
948 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
949 {
950     function_t *new_func;
951     function_decl_t *func_decl;
952     class_decl_t *class_decl;
953     compile_ctx_t ctx;
954     vbscode_t *code;
955     HRESULT hres;
956
957     hres = parse_script(&ctx.parser, src);
958     if(FAILED(hres))
959         return hres;
960
961     code = ctx.code = alloc_vbscode(&ctx, src);
962     if(!ctx.code)
963         return E_OUTOFMEMORY;
964
965     ctx.funcs = NULL;
966     ctx.func_decls = NULL;
967     ctx.global_vars = NULL;
968     ctx.dim_decls = NULL;
969     ctx.classes = NULL;
970     ctx.labels = NULL;
971     ctx.labels_cnt = ctx.labels_size = 0;
972
973     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
974     if(FAILED(hres)) {
975         release_compiler(&ctx);
976         return hres;
977     }
978
979     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
980         hres = create_function(&ctx, func_decl, &new_func);
981         if(FAILED(hres)) {
982             release_compiler(&ctx);
983             return hres;
984         }
985
986         new_func->next = ctx.funcs;
987         ctx.funcs = new_func;
988     }
989
990     for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
991         hres = compile_class(&ctx, class_decl);
992         if(FAILED(hres)) {
993             release_compiler(&ctx);
994             return hres;
995         }
996     }
997
998     hres = check_script_collisions(&ctx, script);
999     if(FAILED(hres)) {
1000         release_compiler(&ctx);
1001         return hres;
1002     }
1003
1004     if(ctx.global_vars) {
1005         dynamic_var_t *var;
1006
1007         for(var = ctx.global_vars; var->next; var = var->next);
1008
1009         var->next = script->global_vars;
1010         script->global_vars = ctx.global_vars;
1011     }
1012
1013     if(ctx.funcs) {
1014         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1015
1016         new_func->next = script->global_funcs;
1017         script->global_funcs = ctx.funcs;
1018     }
1019
1020     if(ctx.classes) {
1021         class_desc_t *class = ctx.classes;
1022
1023         while(1) {
1024             class->ctx = script;
1025             if(!class->next)
1026                 break;
1027             class = class->next;
1028         }
1029
1030         class->next = script->classes;
1031         script->classes = ctx.classes;
1032     }
1033
1034     if(TRACE_ON(vbscript_disas))
1035         dump_code(&ctx);
1036
1037     ctx.code = NULL;
1038     release_compiler(&ctx);
1039
1040     list_add_tail(&script->code_list, &code->entry);
1041     *ret = code;
1042     return S_OK;
1043 }