vbscript: Added getters/setters 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 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         hres = compile_expression(ctx, expr->obj_expr);
322         if(FAILED(hres))
323             return hres;
324
325         hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
326     }else {
327         hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
328     }
329
330     return hres;
331 }
332
333 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
334 {
335     HRESULT hres;
336
337     hres = compile_expression(ctx, expr->subexpr);
338     if(FAILED(hres))
339         return hres;
340
341     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
342 }
343
344 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
345 {
346     HRESULT hres;
347
348     hres = compile_expression(ctx, expr->left);
349     if(FAILED(hres))
350         return hres;
351
352     hres = compile_expression(ctx, expr->right);
353     if(FAILED(hres))
354         return hres;
355
356     return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
357 }
358
359 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
360 {
361     switch(expr->type) {
362     case EXPR_ADD:
363         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
364     case EXPR_AND:
365         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
366     case EXPR_BOOL:
367         return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
368     case EXPR_CONCAT:
369         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
370     case EXPR_DIV:
371         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
372     case EXPR_DOUBLE:
373         return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
374     case EXPR_EMPTY:
375         return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
376     case EXPR_EQUAL:
377         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
378     case EXPR_EQV:
379         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
380     case EXPR_EXP:
381         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
382     case EXPR_IDIV:
383         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
384     case EXPR_IMP:
385         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
386     case EXPR_MEMBER:
387         return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
388     case EXPR_MOD:
389         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
390     case EXPR_MUL:
391         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
392     case EXPR_NEG:
393         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
394     case EXPR_NEQUAL:
395         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
396     case EXPR_NEW:
397         return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
398     case EXPR_NOT:
399         return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
400     case EXPR_NOTHING:
401         return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
402     case EXPR_NULL:
403         return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
404     case EXPR_OR:
405         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
406     case EXPR_STRING:
407         return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
408     case EXPR_SUB:
409         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
410     case EXPR_USHORT:
411         return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
412     case EXPR_ULONG:
413         return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
414     case EXPR_XOR:
415         return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
416     default:
417         FIXME("Unimplemented expression type %d\n", expr->type);
418         return E_NOTIMPL;
419     }
420
421     return S_OK;
422 }
423
424 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
425 {
426     unsigned cnd_jmp, endif_label = -1;
427     elseif_decl_t *elseif_decl;
428     HRESULT hres;
429
430     hres = compile_expression(ctx, stat->expr);
431     if(FAILED(hres))
432         return hres;
433
434     cnd_jmp = push_instr(ctx, OP_jmp_false);
435     if(cnd_jmp == -1)
436         return E_OUTOFMEMORY;
437
438     hres = compile_statement(ctx, stat->if_stat);
439     if(FAILED(hres))
440         return hres;
441
442     if(stat->else_stat || stat->elseifs) {
443         endif_label = alloc_label(ctx);
444         if(endif_label == -1)
445             return E_OUTOFMEMORY;
446
447         hres = push_instr_addr(ctx, OP_jmp, endif_label);
448         if(FAILED(hres))
449             return hres;
450     }
451
452     for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
453         instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
454
455         hres = compile_expression(ctx, elseif_decl->expr);
456         if(FAILED(hres))
457             return hres;
458
459         cnd_jmp = push_instr(ctx, OP_jmp_false);
460         if(cnd_jmp == -1)
461             return E_OUTOFMEMORY;
462
463         hres = compile_statement(ctx, elseif_decl->stat);
464         if(FAILED(hres))
465             return hres;
466
467         hres = push_instr_addr(ctx, OP_jmp, endif_label);
468         if(FAILED(hres))
469             return hres;
470     }
471
472     instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
473
474     if(stat->else_stat) {
475         hres = compile_statement(ctx, stat->else_stat);
476         if(FAILED(hres))
477             return hres;
478     }
479
480     if(endif_label != -1)
481         label_set_addr(ctx, endif_label);
482     return S_OK;
483 }
484
485 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
486 {
487     HRESULT hres;
488
489     hres = compile_expression(ctx, stat->value_expr);
490     if(FAILED(hres))
491         return hres;
492
493     if(stat->member_expr->args) {
494         FIXME("arguments support not implemented\n");
495         return E_NOTIMPL;
496     }
497
498     if(stat->member_expr->obj_expr) {
499         hres = compile_expression(ctx, stat->member_expr->obj_expr);
500         if(FAILED(hres))
501             return hres;
502
503         hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
504     }else {
505         hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
506     }
507
508     return hres;
509 }
510
511 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
512 {
513     dim_decl_t *dim_decl;
514
515     for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
516         if(!strcmpiW(dim_decl->name, name))
517             return TRUE;
518     }
519
520     return FALSE;
521 }
522
523 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
524 {
525     unsigned i;
526
527     for(i = 0; i < ctx->func->arg_cnt; i++) {
528         if(!strcmpiW(ctx->func->args[i].name, name))
529             return TRUE;
530     }
531
532     return FALSE;
533 }
534
535 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
536 {
537     dim_decl_t *dim_decl = stat->dim_decls;
538
539     while(1) {
540         if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
541             FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
542             return E_FAIL;
543         }
544
545         if(!dim_decl->next)
546             break;
547         dim_decl = dim_decl->next;
548     }
549
550     dim_decl->next = ctx->dim_decls;
551     ctx->dim_decls = stat->dim_decls;
552     ctx->func->var_cnt++;
553     return S_OK;
554 }
555
556 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
557 {
558     if(ctx->func != &ctx->code->global_code) {
559         FIXME("Function is not in the global code\n");
560         return E_FAIL;
561     }
562
563     stat->func_decl->next = ctx->func_decls;
564     ctx->func_decls = stat->func_decl;
565     return S_OK;
566 }
567
568 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
569 {
570     if(ctx->sub_end_label == -1) {
571         FIXME("Exit Sub outside Sub?\n");
572         return E_FAIL;
573     }
574
575     return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
576 }
577
578 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
579 {
580     if(ctx->func_end_label == -1) {
581         FIXME("Exit Function outside Function?\n");
582         return E_FAIL;
583     }
584
585     return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
586 }
587
588 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
589 {
590     HRESULT hres;
591
592     while(stat) {
593         switch(stat->type) {
594         case STAT_ASSIGN:
595             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
596             break;
597         case STAT_CALL:
598             hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
599             break;
600         case STAT_DIM:
601             hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
602             break;
603         case STAT_EXITFUNC:
604             hres = compile_exitfunc_statement(ctx);
605             break;
606         case STAT_EXITSUB:
607             hres = compile_exitsub_statement(ctx);
608             break;
609         case STAT_FUNC:
610             hres = compile_function_statement(ctx, (function_statement_t*)stat);
611             break;
612         case STAT_IF:
613             hres = compile_if_statement(ctx, (if_statement_t*)stat);
614             break;
615         case STAT_SET:
616             hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
617             break;
618         case STAT_STOP:
619             hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
620             break;
621         default:
622             FIXME("Unimplemented statement type %d\n", stat->type);
623             hres = E_NOTIMPL;
624         }
625
626         if(FAILED(hres))
627             return hres;
628         stat = stat->next;
629     }
630
631     return S_OK;
632 }
633
634 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
635 {
636     instr_t *instr;
637
638     for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
639         if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
640             assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
641             instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
642         }
643         assert(instr_info[instr->op].arg2_type != ARG_ADDR);
644     }
645
646     ctx->labels_cnt = 0;
647 }
648
649 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
650 {
651     HRESULT hres;
652
653     func->code_off = ctx->instr_cnt;
654
655     ctx->sub_end_label = -1;
656     ctx->func_end_label = -1;
657
658     switch(func->type) {
659     case FUNC_FUNCTION:
660         ctx->func_end_label = alloc_label(ctx);
661         if(ctx->func_end_label == -1)
662             return E_OUTOFMEMORY; /* FIXME ! */
663         break;
664     case FUNC_SUB:
665         ctx->sub_end_label = alloc_label(ctx);
666         if(ctx->sub_end_label == -1)
667             return E_OUTOFMEMORY;
668         break;
669     case FUNC_PROPGET:
670     case FUNC_PROPLET:
671     case FUNC_PROPSET:
672         /* FIXME */
673     case FUNC_GLOBAL:
674         break;
675     }
676
677     ctx->func = func;
678     ctx->dim_decls = NULL;
679     hres = compile_statement(ctx, stat);
680     ctx->func = NULL;
681     if(FAILED(hres))
682         return hres;
683
684     if(ctx->sub_end_label != -1)
685         label_set_addr(ctx, ctx->sub_end_label);
686     if(ctx->func_end_label != -1)
687         label_set_addr(ctx, ctx->func_end_label);
688
689     if(push_instr(ctx, OP_ret) == -1)
690         return E_OUTOFMEMORY;
691
692     resolve_labels(ctx, func->code_off);
693
694     if(func->var_cnt) {
695         dim_decl_t *dim_decl;
696
697         if(func->type == FUNC_GLOBAL) {
698             dynamic_var_t *new_var;
699
700             func->var_cnt = 0;
701
702             for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
703                 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
704                 if(!new_var)
705                     return E_OUTOFMEMORY;
706
707                 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
708                 if(!new_var->name)
709                     return E_OUTOFMEMORY;
710
711                 V_VT(&new_var->v) = VT_EMPTY;
712
713                 new_var->next = ctx->global_vars;
714                 ctx->global_vars = new_var;
715             }
716         }else {
717             unsigned i;
718
719             func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
720             if(!func->vars)
721                 return E_OUTOFMEMORY;
722
723             for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
724                 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
725                 if(!func->vars[i].name)
726                     return E_OUTOFMEMORY;
727             }
728
729             assert(i == func->var_cnt);
730         }
731     }
732
733     return S_OK;
734 }
735
736 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
737 {
738     function_t *iter;
739
740     for(iter = ctx->funcs; iter; iter = iter->next) {
741         if(!strcmpiW(iter->name, name))
742             return TRUE;
743     }
744
745     return FALSE;
746 }
747
748 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
749 {
750     function_t *func;
751     HRESULT hres;
752
753     if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
754         FIXME("%s: redefinition\n", debugstr_w(decl->name));
755         return E_FAIL;
756     }
757
758     func = compiler_alloc(ctx->code, sizeof(*func));
759     if(!func)
760         return E_OUTOFMEMORY;
761
762     func->name = compiler_alloc_string(ctx->code, decl->name);
763     if(!func->name)
764         return E_OUTOFMEMORY;
765
766     func->vars = NULL;
767     func->var_cnt = 0;
768     func->code_ctx = ctx->code;
769     func->type = decl->type;
770     func->is_public = decl->is_public;
771
772     func->arg_cnt = 0;
773     if(decl->args) {
774         arg_decl_t *arg;
775         unsigned i;
776
777         for(arg = decl->args; arg; arg = arg->next)
778             func->arg_cnt++;
779
780         func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
781         if(!func->args)
782             return E_OUTOFMEMORY;
783
784         for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
785             func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
786             if(!func->args[i].name)
787                 return E_OUTOFMEMORY;
788             func->args[i].by_ref = arg->by_ref;
789         }
790     }else {
791         func->args = NULL;
792     }
793
794     hres = compile_func(ctx, decl->body, func);
795     if(FAILED(hres))
796         return hres;
797
798     *ret = func;
799     return S_OK;
800 }
801
802 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
803 {
804     class_desc_t *iter;
805
806     for(iter = ctx->classes; iter; iter = iter->next) {
807         if(!strcmpiW(iter->name, name))
808             return TRUE;
809     }
810
811     return FALSE;
812 }
813
814 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
815 {
816     desc->name = compiler_alloc_string(ctx->code, func_decl->name);
817     if(!desc->name)
818         return E_OUTOFMEMORY;
819
820     assert(!desc->entries[0]);
821
822     if(func_decl->is_public)
823         desc->is_public = TRUE;
824
825     return create_function(ctx, func_decl, desc->entries);
826 }
827
828 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
829 {
830     unsigned i;
831
832     for(i=0; i < class_desc->func_cnt; i++) {
833         if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
834             return TRUE;
835     }
836
837     return FALSE;
838 }
839
840 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
841 {
842     function_decl_t *func_decl;
843     class_prop_decl_t *prop_decl;
844     class_desc_t *class_desc;
845     unsigned i;
846     HRESULT hres;
847
848     if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
849             || lookup_class_name(ctx, class_decl->name)) {
850         FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
851         return E_FAIL;
852     }
853
854     class_desc = compiler_alloc(ctx->code, sizeof(*class_desc));
855     if(!class_desc)
856         return E_OUTOFMEMORY;
857
858     class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
859     if(!class_desc->name)
860         return E_OUTOFMEMORY;
861
862     class_desc->func_cnt = 1; /* always allocate slot for default getter */
863     class_desc->prop_cnt = 0;
864
865     for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next)
866         class_desc->func_cnt++;
867
868     class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
869     if(!class_desc->funcs)
870         return E_OUTOFMEMORY;
871     memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
872
873     for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
874         hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + i);
875         if(FAILED(hres))
876             return hres;
877     }
878
879     for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
880         class_desc->prop_cnt++;
881
882     class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
883     if(!class_desc->props)
884         return E_OUTOFMEMORY;
885
886     for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
887         if(lookup_class_funcs(class_desc, prop_decl->name)) {
888             FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
889             return E_FAIL;
890         }
891
892         class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
893         if(!class_desc->props[i].name)
894             return E_OUTOFMEMORY;
895
896         class_desc->props[i].is_public = prop_decl->is_public;
897     }
898
899     class_desc->next = ctx->classes;
900     ctx->classes = class_desc;
901     return S_OK;
902 }
903
904 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
905 {
906     class_desc_t *class;
907     dynamic_var_t *var;
908     function_t *func;
909
910     for(var = script->global_vars; var; var = var->next) {
911         if(!strcmpiW(var->name, identifier))
912             return TRUE;
913     }
914
915     for(func = script->global_funcs; func; func = func->next) {
916         if(!strcmpiW(func->name, identifier))
917             return TRUE;
918     }
919
920     for(class = script->classes; class; class = class->next) {
921         if(!strcmpiW(class->name, identifier))
922             return TRUE;
923     }
924
925     return FALSE;
926 }
927
928 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
929 {
930     class_desc_t *class;
931     dynamic_var_t *var;
932     function_t *func;
933
934     for(var = ctx->global_vars; var; var = var->next) {
935         if(lookup_script_identifier(script, var->name)) {
936             FIXME("%s: redefined\n", debugstr_w(var->name));
937             return E_FAIL;
938         }
939     }
940
941     for(func = ctx->funcs; func; func = func->next) {
942         if(lookup_script_identifier(script, func->name)) {
943             FIXME("%s: redefined\n", debugstr_w(func->name));
944             return E_FAIL;
945         }
946     }
947
948     for(class = ctx->classes; class; class = class->next) {
949         if(lookup_script_identifier(script, class->name)) {
950             FIXME("%s: redefined\n", debugstr_w(class->name));
951             return E_FAIL;
952         }
953     }
954
955     return S_OK;
956 }
957
958 void release_vbscode(vbscode_t *code)
959 {
960     unsigned i;
961
962     list_remove(&code->entry);
963
964     for(i=0; i < code->bstr_cnt; i++)
965         SysFreeString(code->bstr_pool[i]);
966
967     vbsheap_free(&code->heap);
968
969     heap_free(code->bstr_pool);
970     heap_free(code->source);
971     heap_free(code->instrs);
972     heap_free(code);
973 }
974
975 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
976 {
977     vbscode_t *ret;
978
979     ret = heap_alloc(sizeof(*ret));
980     if(!ret)
981         return NULL;
982
983     ret->source = heap_strdupW(source);
984     if(!ret->source) {
985         heap_free(ret);
986         return NULL;
987     }
988
989     ret->instrs = heap_alloc(32*sizeof(instr_t));
990     if(!ret->instrs) {
991         release_vbscode(ret);
992         return NULL;
993     }
994
995     ctx->instr_cnt = 0;
996     ctx->instr_size = 32;
997     vbsheap_init(&ret->heap);
998
999     ret->option_explicit = ctx->parser.option_explicit;
1000
1001     ret->bstr_pool = NULL;
1002     ret->bstr_pool_size = 0;
1003     ret->bstr_cnt = 0;
1004     ret->global_executed = FALSE;
1005
1006     ret->global_code.type = FUNC_GLOBAL;
1007     ret->global_code.name = NULL;
1008     ret->global_code.code_ctx = ret;
1009     ret->global_code.vars = NULL;
1010     ret->global_code.var_cnt = 0;
1011     ret->global_code.arg_cnt = 0;
1012     ret->global_code.args = NULL;
1013
1014     list_init(&ret->entry);
1015     return ret;
1016 }
1017
1018 static void release_compiler(compile_ctx_t *ctx)
1019 {
1020     parser_release(&ctx->parser);
1021     heap_free(ctx->labels);
1022     if(ctx->code)
1023         release_vbscode(ctx->code);
1024 }
1025
1026 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1027 {
1028     function_t *new_func;
1029     function_decl_t *func_decl;
1030     class_decl_t *class_decl;
1031     compile_ctx_t ctx;
1032     vbscode_t *code;
1033     HRESULT hres;
1034
1035     hres = parse_script(&ctx.parser, src);
1036     if(FAILED(hres))
1037         return hres;
1038
1039     code = ctx.code = alloc_vbscode(&ctx, src);
1040     if(!ctx.code)
1041         return E_OUTOFMEMORY;
1042
1043     ctx.funcs = NULL;
1044     ctx.func_decls = NULL;
1045     ctx.global_vars = NULL;
1046     ctx.dim_decls = NULL;
1047     ctx.classes = NULL;
1048     ctx.labels = NULL;
1049     ctx.labels_cnt = ctx.labels_size = 0;
1050
1051     hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1052     if(FAILED(hres)) {
1053         release_compiler(&ctx);
1054         return hres;
1055     }
1056
1057     for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1058         hres = create_function(&ctx, func_decl, &new_func);
1059         if(FAILED(hres)) {
1060             release_compiler(&ctx);
1061             return hres;
1062         }
1063
1064         new_func->next = ctx.funcs;
1065         ctx.funcs = new_func;
1066     }
1067
1068     for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1069         hres = compile_class(&ctx, class_decl);
1070         if(FAILED(hres)) {
1071             release_compiler(&ctx);
1072             return hres;
1073         }
1074     }
1075
1076     hres = check_script_collisions(&ctx, script);
1077     if(FAILED(hres)) {
1078         release_compiler(&ctx);
1079         return hres;
1080     }
1081
1082     if(ctx.global_vars) {
1083         dynamic_var_t *var;
1084
1085         for(var = ctx.global_vars; var->next; var = var->next);
1086
1087         var->next = script->global_vars;
1088         script->global_vars = ctx.global_vars;
1089     }
1090
1091     if(ctx.funcs) {
1092         for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1093
1094         new_func->next = script->global_funcs;
1095         script->global_funcs = ctx.funcs;
1096     }
1097
1098     if(ctx.classes) {
1099         class_desc_t *class = ctx.classes;
1100
1101         while(1) {
1102             class->ctx = script;
1103             if(!class->next)
1104                 break;
1105             class = class->next;
1106         }
1107
1108         class->next = script->classes;
1109         script->classes = ctx.classes;
1110     }
1111
1112     if(TRACE_ON(vbscript_disas))
1113         dump_code(&ctx);
1114
1115     ctx.code = NULL;
1116     release_compiler(&ctx);
1117
1118     list_add_tail(&script->code_list, &code->entry);
1119     *ret = code;
1120     return S_OK;
1121 }