vbscipt: Added beginning support for arrays as class members.
[wine/multimedia.git] / dlls / vbscript / compile.c
blob48853e9d259c49f1bc832ffcea92111af9a75aa2
1 /*
2 * Copyright 2011 Jacek Caban for CodeWeavers
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.
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.
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
19 #include <assert.h>
21 #include "vbscript.h"
22 #include "parse.h"
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
30 typedef struct _statement_ctx_t {
31 unsigned stack_use;
33 unsigned while_end_label;
34 unsigned for_end_label;
36 struct _statement_ctx_t *next;
37 } statement_ctx_t;
39 typedef struct {
40 parser_ctx_t parser;
42 unsigned instr_cnt;
43 unsigned instr_size;
44 vbscode_t *code;
46 statement_ctx_t *stat_ctx;
48 unsigned *labels;
49 unsigned labels_size;
50 unsigned labels_cnt;
52 unsigned sub_end_label;
53 unsigned func_end_label;
54 unsigned prop_end_label;
56 dim_decl_t *dim_decls;
57 dim_decl_t *dim_decls_tail;
58 dynamic_var_t *global_vars;
60 const_decl_t *const_decls;
61 const_decl_t *global_consts;
63 function_t *func;
64 function_t *funcs;
65 function_decl_t *func_decls;
67 class_desc_t *classes;
68 } compile_ctx_t;
70 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
71 static HRESULT compile_statement(compile_ctx_t*,statement_ctx_t*,statement_t*);
73 static const struct {
74 const char *op_str;
75 instr_arg_type_t arg1_type;
76 instr_arg_type_t arg2_type;
77 } instr_info[] = {
78 #define X(n,a,b,c) {#n,b,c},
79 OP_LIST
80 #undef X
83 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
85 switch(type) {
86 case ARG_STR:
87 case ARG_BSTR:
88 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
89 break;
90 case ARG_INT:
91 TRACE_(vbscript_disas)("\t%d", arg->uint);
92 break;
93 case ARG_UINT:
94 case ARG_ADDR:
95 TRACE_(vbscript_disas)("\t%u", arg->uint);
96 break;
97 case ARG_DOUBLE:
98 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
99 break;
100 case ARG_NONE:
101 break;
102 DEFAULT_UNREACHABLE;
106 static void dump_code(compile_ctx_t *ctx)
108 instr_t *instr;
110 for(instr = ctx->code->instrs+1; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
111 assert(instr->op < OP_LAST);
112 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
113 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
114 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
115 TRACE_(vbscript_disas)("\n");
119 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
121 return heap_pool_alloc(&vbscode->heap, size);
124 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
126 void *ret;
128 ret = heap_pool_alloc(&vbscode->heap, size);
129 if(ret)
130 memset(ret, 0, size);
131 return ret;
134 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
136 size_t size;
137 WCHAR *ret;
139 size = (strlenW(str)+1)*sizeof(WCHAR);
140 ret = compiler_alloc(vbscode, size);
141 if(ret)
142 memcpy(ret, str, size);
143 return ret;
146 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
148 assert(id < ctx->instr_cnt);
149 return ctx->code->instrs + id;
152 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
154 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
156 if(ctx->instr_size == ctx->instr_cnt) {
157 instr_t *new_instr;
159 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
160 if(!new_instr)
161 return 0;
163 ctx->code->instrs = new_instr;
164 ctx->instr_size *= 2;
167 ctx->code->instrs[ctx->instr_cnt].op = op;
168 return ctx->instr_cnt++;
171 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
173 unsigned ret;
175 ret = push_instr(ctx, op);
176 if(!ret)
177 return E_OUTOFMEMORY;
179 instr_ptr(ctx, ret)->arg1.lng = arg;
180 return S_OK;
183 static HRESULT push_instr_uint(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
185 unsigned ret;
187 ret = push_instr(ctx, op);
188 if(!ret)
189 return E_OUTOFMEMORY;
191 instr_ptr(ctx, ret)->arg1.uint = arg;
192 return S_OK;
195 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
197 unsigned ret;
199 ret = push_instr(ctx, op);
200 if(!ret)
201 return E_OUTOFMEMORY;
203 instr_ptr(ctx, ret)->arg1.uint = arg;
204 return S_OK;
207 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
209 unsigned instr;
210 WCHAR *str;
212 str = compiler_alloc_string(ctx->code, arg);
213 if(!str)
214 return E_OUTOFMEMORY;
216 instr = push_instr(ctx, op);
217 if(!instr)
218 return E_OUTOFMEMORY;
220 instr_ptr(ctx, instr)->arg1.str = str;
221 return S_OK;
224 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
226 unsigned instr;
227 double *d;
229 d = compiler_alloc(ctx->code, sizeof(double));
230 if(!d)
231 return E_OUTOFMEMORY;
233 instr = push_instr(ctx, op);
234 if(!instr)
235 return E_OUTOFMEMORY;
237 *d = arg;
238 instr_ptr(ctx, instr)->arg1.dbl = d;
239 return S_OK;
242 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
244 if(!ctx->code->bstr_pool_size) {
245 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
246 if(!ctx->code->bstr_pool)
247 return NULL;
248 ctx->code->bstr_pool_size = 8;
249 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
250 BSTR *new_pool;
252 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
253 if(!new_pool)
254 return NULL;
256 ctx->code->bstr_pool = new_pool;
257 ctx->code->bstr_pool_size *= 2;
260 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
261 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
262 return NULL;
264 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
267 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
269 unsigned instr;
270 BSTR bstr;
272 bstr = alloc_bstr_arg(ctx, arg);
273 if(!bstr)
274 return E_OUTOFMEMORY;
276 instr = push_instr(ctx, op);
277 if(!instr)
278 return E_OUTOFMEMORY;
280 instr_ptr(ctx, instr)->arg1.bstr = bstr;
281 return S_OK;
284 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
286 unsigned instr;
287 BSTR bstr;
289 bstr = alloc_bstr_arg(ctx, arg1);
290 if(!bstr)
291 return E_OUTOFMEMORY;
293 instr = push_instr(ctx, op);
294 if(!instr)
295 return E_OUTOFMEMORY;
297 instr_ptr(ctx, instr)->arg1.bstr = bstr;
298 instr_ptr(ctx, instr)->arg2.uint = arg2;
299 return S_OK;
302 static HRESULT push_instr_uint_bstr(compile_ctx_t *ctx, vbsop_t op, unsigned arg1, const WCHAR *arg2)
304 unsigned instr;
305 BSTR bstr;
307 bstr = alloc_bstr_arg(ctx, arg2);
308 if(!bstr)
309 return E_OUTOFMEMORY;
311 instr = push_instr(ctx, op);
312 if(!instr)
313 return E_OUTOFMEMORY;
315 instr_ptr(ctx, instr)->arg1.uint = arg1;
316 instr_ptr(ctx, instr)->arg2.bstr = bstr;
317 return S_OK;
320 #define LABEL_FLAG 0x80000000
322 static unsigned alloc_label(compile_ctx_t *ctx)
324 if(!ctx->labels_size) {
325 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
326 if(!ctx->labels)
327 return 0;
328 ctx->labels_size = 8;
329 }else if(ctx->labels_size == ctx->labels_cnt) {
330 unsigned *new_labels;
332 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
333 if(!new_labels)
334 return 0;
336 ctx->labels = new_labels;
337 ctx->labels_size *= 2;
340 return ctx->labels_cnt++ | LABEL_FLAG;
343 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
345 assert(label & LABEL_FLAG);
346 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
349 static expression_t *lookup_const_decls(compile_ctx_t *ctx, const WCHAR *name, BOOL lookup_global)
351 const_decl_t *decl;
353 for(decl = ctx->const_decls; decl; decl = decl->next) {
354 if(!strcmpiW(decl->name, name))
355 return decl->value_expr;
358 if(!lookup_global)
359 return NULL;
361 for(decl = ctx->global_consts; decl; decl = decl->next) {
362 if(!strcmpiW(decl->name, name))
363 return decl->value_expr;
366 return NULL;
369 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
371 unsigned arg_cnt = 0;
372 HRESULT hres;
374 while(args) {
375 hres = compile_expression(ctx, args);
376 if(FAILED(hres))
377 return hres;
379 arg_cnt++;
380 args = args->next;
383 *ret = arg_cnt;
384 return S_OK;
387 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
389 unsigned arg_cnt = 0;
390 HRESULT hres;
392 if(ret_val && !expr->args) {
393 expression_t *const_expr;
395 const_expr = lookup_const_decls(ctx, expr->identifier, TRUE);
396 if(const_expr)
397 return compile_expression(ctx, const_expr);
400 hres = compile_args(ctx, expr->args, &arg_cnt);
401 if(FAILED(hres))
402 return hres;
404 if(expr->obj_expr) {
405 hres = compile_expression(ctx, expr->obj_expr);
406 if(FAILED(hres))
407 return hres;
409 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
410 }else {
411 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
414 return hres;
417 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
419 HRESULT hres;
421 hres = compile_expression(ctx, expr->subexpr);
422 if(FAILED(hres))
423 return hres;
425 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
428 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
430 HRESULT hres;
432 hres = compile_expression(ctx, expr->left);
433 if(FAILED(hres))
434 return hres;
436 hres = compile_expression(ctx, expr->right);
437 if(FAILED(hres))
438 return hres;
440 return push_instr(ctx, op) ? S_OK : E_OUTOFMEMORY;
443 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
445 switch(expr->type) {
446 case EXPR_ADD:
447 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
448 case EXPR_AND:
449 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
450 case EXPR_BOOL:
451 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
452 case EXPR_BRACKETS:
453 return compile_expression(ctx, ((unary_expression_t*)expr)->subexpr);
454 case EXPR_CONCAT:
455 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
456 case EXPR_DIV:
457 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
458 case EXPR_DOUBLE:
459 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
460 case EXPR_EMPTY:
461 return push_instr(ctx, OP_empty) ? S_OK : E_OUTOFMEMORY;
462 case EXPR_EQUAL:
463 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
464 case EXPR_EQV:
465 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
466 case EXPR_EXP:
467 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
468 case EXPR_GT:
469 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
470 case EXPR_GTEQ:
471 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
472 case EXPR_IDIV:
473 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
474 case EXPR_IS:
475 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
476 case EXPR_IMP:
477 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
478 case EXPR_LT:
479 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
480 case EXPR_LTEQ:
481 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
482 case EXPR_ME:
483 return push_instr(ctx, OP_me) ? S_OK : E_OUTOFMEMORY;
484 case EXPR_MEMBER:
485 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
486 case EXPR_MOD:
487 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
488 case EXPR_MUL:
489 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
490 case EXPR_NEG:
491 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
492 case EXPR_NEQUAL:
493 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
494 case EXPR_NEW:
495 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
496 case EXPR_NOT:
497 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
498 case EXPR_NOTHING:
499 return push_instr(ctx, OP_nothing) ? S_OK : E_OUTOFMEMORY;
500 case EXPR_NULL:
501 return push_instr(ctx, OP_null) ? S_OK : E_OUTOFMEMORY;
502 case EXPR_OR:
503 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
504 case EXPR_STRING:
505 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
506 case EXPR_SUB:
507 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
508 case EXPR_USHORT:
509 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
510 case EXPR_ULONG:
511 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
512 case EXPR_XOR:
513 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
514 default:
515 FIXME("Unimplemented expression type %d\n", expr->type);
516 return E_NOTIMPL;
519 return S_OK;
522 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
524 unsigned cnd_jmp, endif_label = 0;
525 elseif_decl_t *elseif_decl;
526 HRESULT hres;
528 hres = compile_expression(ctx, stat->expr);
529 if(FAILED(hres))
530 return hres;
532 cnd_jmp = push_instr(ctx, OP_jmp_false);
533 if(!cnd_jmp)
534 return E_OUTOFMEMORY;
536 hres = compile_statement(ctx, NULL, stat->if_stat);
537 if(FAILED(hres))
538 return hres;
540 if(stat->else_stat || stat->elseifs) {
541 endif_label = alloc_label(ctx);
542 if(!endif_label)
543 return E_OUTOFMEMORY;
545 hres = push_instr_addr(ctx, OP_jmp, endif_label);
546 if(FAILED(hres))
547 return hres;
550 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
551 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
553 hres = compile_expression(ctx, elseif_decl->expr);
554 if(FAILED(hres))
555 return hres;
557 cnd_jmp = push_instr(ctx, OP_jmp_false);
558 if(!cnd_jmp)
559 return E_OUTOFMEMORY;
561 hres = compile_statement(ctx, NULL, elseif_decl->stat);
562 if(FAILED(hres))
563 return hres;
565 hres = push_instr_addr(ctx, OP_jmp, endif_label);
566 if(FAILED(hres))
567 return hres;
570 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
572 if(stat->else_stat) {
573 hres = compile_statement(ctx, NULL, stat->else_stat);
574 if(FAILED(hres))
575 return hres;
578 if(endif_label)
579 label_set_addr(ctx, endif_label);
580 return S_OK;
583 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
585 statement_ctx_t stat_ctx = {0}, *loop_ctx;
586 unsigned start_addr;
587 unsigned jmp_end;
588 HRESULT hres;
590 start_addr = ctx->instr_cnt;
592 hres = compile_expression(ctx, stat->expr);
593 if(FAILED(hres))
594 return hres;
596 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
597 if(!jmp_end)
598 return E_OUTOFMEMORY;
600 if(stat->stat.type == STAT_WHILE) {
601 loop_ctx = NULL;
602 }else {
603 if(!(stat_ctx.while_end_label = alloc_label(ctx)))
604 return E_OUTOFMEMORY;
605 loop_ctx = &stat_ctx;
608 hres = compile_statement(ctx, loop_ctx, stat->body);
609 if(FAILED(hres))
610 return hres;
612 hres = push_instr_addr(ctx, OP_jmp, start_addr);
613 if(FAILED(hres))
614 return hres;
616 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
618 if(loop_ctx)
619 label_set_addr(ctx, stat_ctx.while_end_label);
621 return S_OK;
624 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
626 statement_ctx_t loop_ctx = {0};
627 unsigned start_addr;
628 vbsop_t jmp_op;
629 HRESULT hres;
631 start_addr = ctx->instr_cnt;
633 if(!(loop_ctx.while_end_label = alloc_label(ctx)))
634 return E_OUTOFMEMORY;
636 hres = compile_statement(ctx, &loop_ctx, stat->body);
637 if(FAILED(hres))
638 return hres;
640 if(stat->expr) {
641 hres = compile_expression(ctx, stat->expr);
642 if(FAILED(hres))
643 return hres;
645 jmp_op = stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true;
646 }else {
647 jmp_op = OP_jmp;
650 hres = push_instr_addr(ctx, jmp_op, start_addr);
651 if(FAILED(hres))
652 return hres;
654 label_set_addr(ctx, loop_ctx.while_end_label);
655 return S_OK;
658 static HRESULT compile_foreach_statement(compile_ctx_t *ctx, foreach_statement_t *stat)
660 statement_ctx_t loop_ctx = {1};
661 unsigned loop_start;
662 HRESULT hres;
664 hres = compile_expression(ctx, stat->group_expr);
665 if(FAILED(hres))
666 return hres;
668 if(!push_instr(ctx, OP_newenum))
669 return E_OUTOFMEMORY;
671 loop_start = ctx->instr_cnt;
672 if(!(loop_ctx.for_end_label = alloc_label(ctx)))
673 return E_OUTOFMEMORY;
675 hres = push_instr_uint_bstr(ctx, OP_enumnext, loop_ctx.for_end_label, stat->identifier);
676 if(FAILED(hres))
677 return hres;
679 hres = compile_statement(ctx, &loop_ctx, stat->body);
680 if(FAILED(hres))
681 return hres;
683 hres = push_instr_addr(ctx, OP_jmp, loop_start);
684 if(FAILED(hres))
685 return hres;
687 label_set_addr(ctx, loop_ctx.for_end_label);
688 return S_OK;
691 static HRESULT compile_forto_statement(compile_ctx_t *ctx, forto_statement_t *stat)
693 statement_ctx_t loop_ctx = {2};
694 unsigned step_instr, instr;
695 BSTR identifier;
696 HRESULT hres;
698 identifier = alloc_bstr_arg(ctx, stat->identifier);
699 if(!identifier)
700 return E_OUTOFMEMORY;
702 hres = compile_expression(ctx, stat->from_expr);
703 if(FAILED(hres))
704 return hres;
706 instr = push_instr(ctx, OP_assign_ident);
707 if(!instr)
708 return E_OUTOFMEMORY;
709 instr_ptr(ctx, instr)->arg1.bstr = identifier;
710 instr_ptr(ctx, instr)->arg2.uint = 0;
712 hres = compile_expression(ctx, stat->to_expr);
713 if(FAILED(hres))
714 return hres;
716 if(!push_instr(ctx, OP_val))
717 return E_OUTOFMEMORY;
719 if(stat->step_expr) {
720 hres = compile_expression(ctx, stat->step_expr);
721 if(FAILED(hres))
722 return hres;
724 if(!push_instr(ctx, OP_val))
725 return E_OUTOFMEMORY;
726 }else {
727 hres = push_instr_int(ctx, OP_short, 1);
728 if(FAILED(hres))
729 return hres;
732 loop_ctx.for_end_label = alloc_label(ctx);
733 if(!loop_ctx.for_end_label)
734 return E_OUTOFMEMORY;
736 step_instr = push_instr(ctx, OP_step);
737 if(!step_instr)
738 return E_OUTOFMEMORY;
739 instr_ptr(ctx, step_instr)->arg2.bstr = identifier;
740 instr_ptr(ctx, step_instr)->arg1.uint = loop_ctx.for_end_label;
742 hres = compile_statement(ctx, &loop_ctx, stat->body);
743 if(FAILED(hres))
744 return hres;
746 instr = push_instr(ctx, OP_incc);
747 if(!instr)
748 return E_OUTOFMEMORY;
749 instr_ptr(ctx, instr)->arg1.bstr = identifier;
751 hres = push_instr_addr(ctx, OP_jmp, step_instr);
752 if(FAILED(hres))
753 return hres;
755 hres = push_instr_uint(ctx, OP_pop, 2);
756 if(FAILED(hres))
757 return hres;
759 label_set_addr(ctx, loop_ctx.for_end_label);
760 return S_OK;
763 static HRESULT compile_select_statement(compile_ctx_t *ctx, select_statement_t *stat)
765 unsigned end_label, case_cnt = 0, *case_labels = NULL, i;
766 case_clausule_t *case_iter;
767 expression_t *expr_iter;
768 HRESULT hres;
770 hres = compile_expression(ctx, stat->expr);
771 if(FAILED(hres))
772 return hres;
774 if(!push_instr(ctx, OP_val))
775 return E_OUTOFMEMORY;
777 end_label = alloc_label(ctx);
778 if(!end_label)
779 return E_OUTOFMEMORY;
781 for(case_iter = stat->case_clausules; case_iter; case_iter = case_iter->next)
782 case_cnt++;
784 if(case_cnt) {
785 case_labels = heap_alloc(case_cnt*sizeof(*case_labels));
786 if(!case_labels)
787 return E_OUTOFMEMORY;
790 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
791 case_labels[i] = alloc_label(ctx);
792 if(!case_labels[i]) {
793 hres = E_OUTOFMEMORY;
794 break;
797 if(!case_iter->expr)
798 break;
800 for(expr_iter = case_iter->expr; expr_iter; expr_iter = expr_iter->next) {
801 hres = compile_expression(ctx, expr_iter);
802 if(FAILED(hres))
803 break;
805 hres = push_instr_addr(ctx, OP_case, case_labels[i]);
806 if(FAILED(hres))
807 break;
811 if(FAILED(hres)) {
812 heap_free(case_labels);
813 return hres;
816 hres = push_instr_uint(ctx, OP_pop, 1);
817 if(FAILED(hres)) {
818 heap_free(case_labels);
819 return hres;
822 hres = push_instr_addr(ctx, OP_jmp, case_iter ? case_labels[i] : end_label);
823 if(FAILED(hres)) {
824 heap_free(case_labels);
825 return hres;
828 for(case_iter = stat->case_clausules, i=0; case_iter; case_iter = case_iter->next, i++) {
829 label_set_addr(ctx, case_labels[i]);
830 hres = compile_statement(ctx, NULL, case_iter->stat);
831 if(FAILED(hres))
832 break;
834 if(!case_iter->next)
835 break;
837 hres = push_instr_addr(ctx, OP_jmp, end_label);
838 if(FAILED(hres))
839 break;
842 heap_free(case_labels);
843 if(FAILED(hres))
844 return hres;
846 label_set_addr(ctx, end_label);
847 return S_OK;
850 static HRESULT compile_assignment(compile_ctx_t *ctx, member_expression_t *member_expr, expression_t *value_expr, BOOL is_set)
852 unsigned args_cnt;
853 vbsop_t op;
854 HRESULT hres;
856 if(member_expr->obj_expr) {
857 hres = compile_expression(ctx, member_expr->obj_expr);
858 if(FAILED(hres))
859 return hres;
861 op = is_set ? OP_set_member : OP_assign_member;
862 }else {
863 op = is_set ? OP_set_ident : OP_assign_ident;
866 hres = compile_expression(ctx, value_expr);
867 if(FAILED(hres))
868 return hres;
870 hres = compile_args(ctx, member_expr->args, &args_cnt);
871 if(FAILED(hres))
872 return hres;
874 return push_instr_bstr_uint(ctx, op, member_expr->identifier, args_cnt);
877 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
879 return compile_assignment(ctx, stat->member_expr, stat->value_expr, is_set);
882 static HRESULT compile_call_statement(compile_ctx_t *ctx, call_statement_t *stat)
884 /* It's challenging for parser to distinguish parameterized assignment with one argument from call
885 * with equality expression argument, so we do it in compiler. */
886 if(!stat->is_strict && stat->expr->args && !stat->expr->args->next && stat->expr->args->type == EXPR_EQUAL) {
887 binary_expression_t *eqexpr = (binary_expression_t*)stat->expr->args;
889 if(eqexpr->left->type == EXPR_BRACKETS) {
890 member_expression_t new_member = *stat->expr;
892 WARN("converting call expr to assign expr\n");
894 new_member.args = ((unary_expression_t*)eqexpr->left)->subexpr;
895 return compile_assignment(ctx, &new_member, eqexpr->right, FALSE);
899 return compile_member_expression(ctx, stat->expr, FALSE);
902 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
904 dim_decl_t *dim_decl;
906 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
907 if(!strcmpiW(dim_decl->name, name))
908 return TRUE;
911 return FALSE;
914 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
916 unsigned i;
918 for(i = 0; i < ctx->func->arg_cnt; i++) {
919 if(!strcmpiW(ctx->func->args[i].name, name))
920 return TRUE;
923 return FALSE;
926 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
928 dim_decl_t *dim_decl = stat->dim_decls;
930 while(1) {
931 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)
932 || lookup_const_decls(ctx, dim_decl->name, FALSE)) {
933 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
934 return E_FAIL;
937 ctx->func->var_cnt++;
939 if(dim_decl->is_array) {
940 HRESULT hres = push_instr_bstr_uint(ctx, OP_dim, dim_decl->name, ctx->func->array_cnt++);
941 if(FAILED(hres))
942 return hres;
945 if(!dim_decl->next)
946 break;
947 dim_decl = dim_decl->next;
950 if(ctx->dim_decls_tail)
951 ctx->dim_decls_tail->next = stat->dim_decls;
952 else
953 ctx->dim_decls = stat->dim_decls;
954 ctx->dim_decls_tail = dim_decl;
955 return S_OK;
958 static HRESULT compile_const_statement(compile_ctx_t *ctx, const_statement_t *stat)
960 const_decl_t *decl, *next_decl = stat->decls;
962 do {
963 decl = next_decl;
965 if(lookup_const_decls(ctx, decl->name, FALSE) || lookup_args_name(ctx, decl->name)
966 || lookup_dim_decls(ctx, decl->name)) {
967 FIXME("%s redefined\n", debugstr_w(decl->name));
968 return E_FAIL;
971 if(ctx->func->type == FUNC_GLOBAL) {
972 HRESULT hres;
974 hres = compile_expression(ctx, decl->value_expr);
975 if(FAILED(hres))
976 return hres;
978 hres = push_instr_bstr(ctx, OP_const, decl->name);
979 if(FAILED(hres))
980 return hres;
983 next_decl = decl->next;
984 decl->next = ctx->const_decls;
985 ctx->const_decls = decl;
986 } while(next_decl);
988 return S_OK;
991 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
993 if(ctx->func != &ctx->code->main_code) {
994 FIXME("Function is not in the global code\n");
995 return E_FAIL;
998 stat->func_decl->next = ctx->func_decls;
999 ctx->func_decls = stat->func_decl;
1000 return S_OK;
1003 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
1005 statement_ctx_t *iter;
1006 unsigned pop_cnt = 0;
1008 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1009 pop_cnt += iter->stack_use;
1010 if(iter->while_end_label)
1011 break;
1013 if(!iter) {
1014 FIXME("Exit Do outside Do Loop\n");
1015 return E_FAIL;
1018 if(pop_cnt) {
1019 HRESULT hres;
1021 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1022 if(FAILED(hres))
1023 return hres;
1026 return push_instr_addr(ctx, OP_jmp, iter->while_end_label);
1029 static HRESULT compile_exitfor_statement(compile_ctx_t *ctx)
1031 statement_ctx_t *iter;
1032 unsigned pop_cnt = 0;
1034 for(iter = ctx->stat_ctx; iter; iter = iter->next) {
1035 pop_cnt += iter->stack_use;
1036 if(iter->for_end_label)
1037 break;
1039 if(!iter) {
1040 FIXME("Exit For outside For loop\n");
1041 return E_FAIL;
1044 if(pop_cnt) {
1045 HRESULT hres;
1047 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1048 if(FAILED(hres))
1049 return hres;
1052 return push_instr_addr(ctx, OP_jmp, iter->for_end_label);
1055 static HRESULT exit_label(compile_ctx_t *ctx, unsigned jmp_label)
1057 statement_ctx_t *iter;
1058 unsigned pop_cnt = 0;
1060 for(iter = ctx->stat_ctx; iter; iter = iter->next)
1061 pop_cnt += iter->stack_use;
1063 if(pop_cnt) {
1064 HRESULT hres;
1066 hres = push_instr_uint(ctx, OP_pop, pop_cnt);
1067 if(FAILED(hres))
1068 return hres;
1071 return push_instr_addr(ctx, OP_jmp, jmp_label);
1074 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
1076 if(!ctx->sub_end_label) {
1077 FIXME("Exit Sub outside Sub?\n");
1078 return E_FAIL;
1081 return exit_label(ctx, ctx->sub_end_label);
1084 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
1086 if(!ctx->func_end_label) {
1087 FIXME("Exit Function outside Function?\n");
1088 return E_FAIL;
1091 return exit_label(ctx, ctx->func_end_label);
1094 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
1096 if(!ctx->prop_end_label) {
1097 FIXME("Exit Property outside Property?\n");
1098 return E_FAIL;
1101 return exit_label(ctx, ctx->prop_end_label);
1104 static HRESULT compile_onerror_statement(compile_ctx_t *ctx, onerror_statement_t *stat)
1106 return push_instr_int(ctx, OP_errmode, stat->resume_next);
1109 static HRESULT compile_statement(compile_ctx_t *ctx, statement_ctx_t *stat_ctx, statement_t *stat)
1111 HRESULT hres;
1113 if(stat_ctx) {
1114 stat_ctx->next = ctx->stat_ctx;
1115 ctx->stat_ctx = stat_ctx;
1118 while(stat) {
1119 switch(stat->type) {
1120 case STAT_ASSIGN:
1121 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
1122 break;
1123 case STAT_CALL:
1124 hres = compile_call_statement(ctx, (call_statement_t*)stat);
1125 break;
1126 case STAT_CONST:
1127 hres = compile_const_statement(ctx, (const_statement_t*)stat);
1128 break;
1129 case STAT_DIM:
1130 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
1131 break;
1132 case STAT_DOWHILE:
1133 case STAT_DOUNTIL:
1134 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
1135 break;
1136 case STAT_EXITDO:
1137 hres = compile_exitdo_statement(ctx);
1138 break;
1139 case STAT_EXITFOR:
1140 hres = compile_exitfor_statement(ctx);
1141 break;
1142 case STAT_EXITFUNC:
1143 hres = compile_exitfunc_statement(ctx);
1144 break;
1145 case STAT_EXITPROP:
1146 hres = compile_exitprop_statement(ctx);
1147 break;
1148 case STAT_EXITSUB:
1149 hres = compile_exitsub_statement(ctx);
1150 break;
1151 case STAT_FOREACH:
1152 hres = compile_foreach_statement(ctx, (foreach_statement_t*)stat);
1153 break;
1154 case STAT_FORTO:
1155 hres = compile_forto_statement(ctx, (forto_statement_t*)stat);
1156 break;
1157 case STAT_FUNC:
1158 hres = compile_function_statement(ctx, (function_statement_t*)stat);
1159 break;
1160 case STAT_IF:
1161 hres = compile_if_statement(ctx, (if_statement_t*)stat);
1162 break;
1163 case STAT_ONERROR:
1164 hres = compile_onerror_statement(ctx, (onerror_statement_t*)stat);
1165 break;
1166 case STAT_SELECT:
1167 hres = compile_select_statement(ctx, (select_statement_t*)stat);
1168 break;
1169 case STAT_SET:
1170 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
1171 break;
1172 case STAT_STOP:
1173 hres = push_instr(ctx, OP_stop) ? S_OK : E_OUTOFMEMORY;
1174 break;
1175 case STAT_UNTIL:
1176 case STAT_WHILE:
1177 case STAT_WHILELOOP:
1178 hres = compile_while_statement(ctx, (while_statement_t*)stat);
1179 break;
1180 default:
1181 FIXME("Unimplemented statement type %d\n", stat->type);
1182 hres = E_NOTIMPL;
1185 if(FAILED(hres))
1186 return hres;
1187 stat = stat->next;
1190 if(stat_ctx) {
1191 assert(ctx->stat_ctx == stat_ctx);
1192 ctx->stat_ctx = stat_ctx->next;
1195 return S_OK;
1198 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
1200 instr_t *instr;
1202 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
1203 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
1204 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
1205 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
1207 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
1210 ctx->labels_cnt = 0;
1213 static HRESULT fill_array_desc(compile_ctx_t *ctx, dim_decl_t *dim_decl, array_desc_t *array_desc)
1215 unsigned dim_cnt = 0, i;
1216 dim_list_t *iter;
1218 for(iter = dim_decl->dims; iter; iter = iter->next)
1219 dim_cnt++;
1221 array_desc->bounds = compiler_alloc(ctx->code, dim_cnt * sizeof(SAFEARRAYBOUND));
1222 if(!array_desc->bounds)
1223 return E_OUTOFMEMORY;
1225 array_desc->dim_cnt = dim_cnt;
1227 for(iter = dim_decl->dims, i=0; iter; iter = iter->next, i++) {
1228 array_desc->bounds[i].cElements = iter->val+1;
1229 array_desc->bounds[i].lLbound = 0;
1232 return S_OK;
1235 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
1237 HRESULT hres;
1239 func->code_off = ctx->instr_cnt;
1241 ctx->sub_end_label = 0;
1242 ctx->func_end_label = 0;
1243 ctx->prop_end_label = 0;
1245 switch(func->type) {
1246 case FUNC_FUNCTION:
1247 ctx->func_end_label = alloc_label(ctx);
1248 if(!ctx->func_end_label)
1249 return E_OUTOFMEMORY;
1250 break;
1251 case FUNC_SUB:
1252 ctx->sub_end_label = alloc_label(ctx);
1253 if(!ctx->sub_end_label)
1254 return E_OUTOFMEMORY;
1255 break;
1256 case FUNC_PROPGET:
1257 case FUNC_PROPLET:
1258 case FUNC_PROPSET:
1259 case FUNC_DEFGET:
1260 ctx->prop_end_label = alloc_label(ctx);
1261 if(!ctx->prop_end_label)
1262 return E_OUTOFMEMORY;
1263 break;
1264 case FUNC_GLOBAL:
1265 break;
1268 ctx->func = func;
1269 ctx->dim_decls = ctx->dim_decls_tail = NULL;
1270 ctx->const_decls = NULL;
1271 hres = compile_statement(ctx, NULL, stat);
1272 ctx->func = NULL;
1273 if(FAILED(hres))
1274 return hres;
1276 if(ctx->sub_end_label)
1277 label_set_addr(ctx, ctx->sub_end_label);
1278 if(ctx->func_end_label)
1279 label_set_addr(ctx, ctx->func_end_label);
1280 if(ctx->prop_end_label)
1281 label_set_addr(ctx, ctx->prop_end_label);
1283 if(!push_instr(ctx, OP_ret))
1284 return E_OUTOFMEMORY;
1286 resolve_labels(ctx, func->code_off);
1288 if(func->var_cnt) {
1289 dim_decl_t *dim_decl;
1291 if(func->type == FUNC_GLOBAL) {
1292 dynamic_var_t *new_var;
1294 func->var_cnt = 0;
1296 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1297 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
1298 if(!new_var)
1299 return E_OUTOFMEMORY;
1301 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
1302 if(!new_var->name)
1303 return E_OUTOFMEMORY;
1305 V_VT(&new_var->v) = VT_EMPTY;
1306 new_var->is_const = FALSE;
1308 new_var->next = ctx->global_vars;
1309 ctx->global_vars = new_var;
1311 }else {
1312 unsigned i;
1314 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
1315 if(!func->vars)
1316 return E_OUTOFMEMORY;
1318 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
1319 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
1320 if(!func->vars[i].name)
1321 return E_OUTOFMEMORY;
1324 assert(i == func->var_cnt);
1328 if(func->array_cnt) {
1329 unsigned array_id = 0;
1330 dim_decl_t *dim_decl;
1332 func->array_descs = compiler_alloc(ctx->code, func->array_cnt * sizeof(array_desc_t));
1333 if(!func->array_descs)
1334 return E_OUTOFMEMORY;
1336 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
1337 if(dim_decl->is_array) {
1338 hres = fill_array_desc(ctx, dim_decl, func->array_descs + array_id++);
1339 if(FAILED(hres))
1340 return hres;
1344 assert(array_id == func->array_cnt);
1347 return S_OK;
1350 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
1352 function_t *iter;
1354 for(iter = ctx->funcs; iter; iter = iter->next) {
1355 if(!strcmpiW(iter->name, name))
1356 return TRUE;
1359 return FALSE;
1362 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
1364 function_t *func;
1365 HRESULT hres;
1367 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name) || lookup_const_decls(ctx, decl->name, FALSE)) {
1368 FIXME("%s: redefinition\n", debugstr_w(decl->name));
1369 return E_FAIL;
1372 func = compiler_alloc(ctx->code, sizeof(*func));
1373 if(!func)
1374 return E_OUTOFMEMORY;
1376 func->name = compiler_alloc_string(ctx->code, decl->name);
1377 if(!func->name)
1378 return E_OUTOFMEMORY;
1380 func->vars = NULL;
1381 func->var_cnt = 0;
1382 func->array_cnt = 0;
1383 func->code_ctx = ctx->code;
1384 func->type = decl->type;
1385 func->is_public = decl->is_public;
1387 func->arg_cnt = 0;
1388 if(decl->args) {
1389 arg_decl_t *arg;
1390 unsigned i;
1392 for(arg = decl->args; arg; arg = arg->next)
1393 func->arg_cnt++;
1395 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
1396 if(!func->args)
1397 return E_OUTOFMEMORY;
1399 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
1400 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
1401 if(!func->args[i].name)
1402 return E_OUTOFMEMORY;
1403 func->args[i].by_ref = arg->by_ref;
1405 }else {
1406 func->args = NULL;
1409 hres = compile_func(ctx, decl->body, func);
1410 if(FAILED(hres))
1411 return hres;
1413 *ret = func;
1414 return S_OK;
1417 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
1419 class_desc_t *iter;
1421 for(iter = ctx->classes; iter; iter = iter->next) {
1422 if(!strcmpiW(iter->name, name))
1423 return TRUE;
1426 return FALSE;
1429 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
1431 vbdisp_invoke_type_t invoke_type;
1432 function_decl_t *funcprop_decl;
1433 HRESULT hres;
1435 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
1436 if(!desc->name)
1437 return E_OUTOFMEMORY;
1439 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
1440 switch(funcprop_decl->type) {
1441 case FUNC_FUNCTION:
1442 case FUNC_SUB:
1443 case FUNC_PROPGET:
1444 case FUNC_DEFGET:
1445 invoke_type = VBDISP_CALLGET;
1446 break;
1447 case FUNC_PROPLET:
1448 invoke_type = VBDISP_LET;
1449 break;
1450 case FUNC_PROPSET:
1451 invoke_type = VBDISP_SET;
1452 break;
1453 DEFAULT_UNREACHABLE;
1456 assert(!desc->entries[invoke_type]);
1458 if(funcprop_decl->is_public)
1459 desc->is_public = TRUE;
1461 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
1462 if(FAILED(hres))
1463 return hres;
1466 return S_OK;
1469 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
1471 unsigned i;
1473 for(i=0; i < class_desc->func_cnt; i++) {
1474 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1475 return TRUE;
1478 return FALSE;
1481 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1483 function_decl_t *func_decl, *func_prop_decl;
1484 class_desc_t *class_desc;
1485 dim_decl_t *prop_decl;
1486 unsigned i;
1487 HRESULT hres;
1489 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1490 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1492 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1493 || lookup_const_decls(ctx, class_decl->name, FALSE) || lookup_class_name(ctx, class_decl->name)) {
1494 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1495 return E_FAIL;
1498 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1499 if(!class_desc)
1500 return E_OUTOFMEMORY;
1502 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1503 if(!class_desc->name)
1504 return E_OUTOFMEMORY;
1506 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1508 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1509 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1510 if(func_prop_decl->type == FUNC_DEFGET)
1511 break;
1513 if(!func_prop_decl)
1514 class_desc->func_cnt++;
1517 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1518 if(!class_desc->funcs)
1519 return E_OUTOFMEMORY;
1520 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1522 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1523 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1524 if(func_prop_decl->type == FUNC_DEFGET) {
1525 i--;
1526 break;
1530 if(!strcmpiW(class_initializeW, func_decl->name)) {
1531 if(func_decl->type != FUNC_SUB) {
1532 FIXME("class initializer is not sub\n");
1533 return E_FAIL;
1536 class_desc->class_initialize_id = i;
1537 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1538 if(func_decl->type != FUNC_SUB) {
1539 FIXME("class terminator is not sub\n");
1540 return E_FAIL;
1543 class_desc->class_terminate_id = i;
1546 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1547 if(FAILED(hres))
1548 return hres;
1551 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1552 class_desc->prop_cnt++;
1554 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1555 if(!class_desc->props)
1556 return E_OUTOFMEMORY;
1558 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1559 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1560 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1561 return E_FAIL;
1564 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1565 if(!class_desc->props[i].name)
1566 return E_OUTOFMEMORY;
1568 class_desc->props[i].is_public = prop_decl->is_public;
1570 if(prop_decl->is_array) {
1571 class_desc->props[i].is_array = TRUE;
1572 class_desc->array_cnt++;
1576 if(class_desc->array_cnt) {
1577 class_desc->array_descs = compiler_alloc(ctx->code, class_desc->array_cnt*sizeof(*class_desc->array_descs));
1578 if(!class_desc->array_descs)
1579 return E_OUTOFMEMORY;
1581 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next) {
1582 if(prop_decl->is_array) {
1583 hres = fill_array_desc(ctx, prop_decl, class_desc->array_descs + i++);
1584 if(FAILED(hres))
1585 return hres;
1590 class_desc->next = ctx->classes;
1591 ctx->classes = class_desc;
1592 return S_OK;
1595 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1597 class_desc_t *class;
1598 dynamic_var_t *var;
1599 function_t *func;
1601 for(var = script->global_vars; var; var = var->next) {
1602 if(!strcmpiW(var->name, identifier))
1603 return TRUE;
1606 for(func = script->global_funcs; func; func = func->next) {
1607 if(!strcmpiW(func->name, identifier))
1608 return TRUE;
1611 for(class = script->classes; class; class = class->next) {
1612 if(!strcmpiW(class->name, identifier))
1613 return TRUE;
1616 return FALSE;
1619 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1621 class_desc_t *class;
1622 dynamic_var_t *var;
1623 function_t *func;
1625 for(var = ctx->global_vars; var; var = var->next) {
1626 if(lookup_script_identifier(script, var->name)) {
1627 FIXME("%s: redefined\n", debugstr_w(var->name));
1628 return E_FAIL;
1632 for(func = ctx->funcs; func; func = func->next) {
1633 if(lookup_script_identifier(script, func->name)) {
1634 FIXME("%s: redefined\n", debugstr_w(func->name));
1635 return E_FAIL;
1639 for(class = ctx->classes; class; class = class->next) {
1640 if(lookup_script_identifier(script, class->name)) {
1641 FIXME("%s: redefined\n", debugstr_w(class->name));
1642 return E_FAIL;
1646 return S_OK;
1649 void release_vbscode(vbscode_t *code)
1651 unsigned i;
1653 list_remove(&code->entry);
1655 for(i=0; i < code->bstr_cnt; i++)
1656 SysFreeString(code->bstr_pool[i]);
1658 heap_pool_free(&code->heap);
1660 heap_free(code->bstr_pool);
1661 heap_free(code->source);
1662 heap_free(code->instrs);
1663 heap_free(code);
1666 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1668 vbscode_t *ret;
1670 ret = heap_alloc(sizeof(*ret));
1671 if(!ret)
1672 return NULL;
1674 ret->source = heap_strdupW(source);
1675 if(!ret->source) {
1676 heap_free(ret);
1677 return NULL;
1680 ret->instrs = heap_alloc(32*sizeof(instr_t));
1681 if(!ret->instrs) {
1682 release_vbscode(ret);
1683 return NULL;
1686 ctx->instr_cnt = 1;
1687 ctx->instr_size = 32;
1688 heap_pool_init(&ret->heap);
1690 ret->option_explicit = ctx->parser.option_explicit;
1692 ret->bstr_pool = NULL;
1693 ret->bstr_pool_size = 0;
1694 ret->bstr_cnt = 0;
1695 ret->pending_exec = FALSE;
1697 ret->main_code.type = FUNC_GLOBAL;
1698 ret->main_code.name = NULL;
1699 ret->main_code.code_ctx = ret;
1700 ret->main_code.vars = NULL;
1701 ret->main_code.var_cnt = 0;
1702 ret->main_code.array_cnt = 0;
1703 ret->main_code.arg_cnt = 0;
1704 ret->main_code.args = NULL;
1706 list_init(&ret->entry);
1707 return ret;
1710 static void release_compiler(compile_ctx_t *ctx)
1712 parser_release(&ctx->parser);
1713 heap_free(ctx->labels);
1714 if(ctx->code)
1715 release_vbscode(ctx->code);
1718 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, const WCHAR *delimiter, vbscode_t **ret)
1720 function_t *new_func;
1721 function_decl_t *func_decl;
1722 class_decl_t *class_decl;
1723 compile_ctx_t ctx;
1724 vbscode_t *code;
1725 HRESULT hres;
1727 hres = parse_script(&ctx.parser, src, delimiter);
1728 if(FAILED(hres))
1729 return hres;
1731 code = ctx.code = alloc_vbscode(&ctx, src);
1732 if(!ctx.code)
1733 return E_OUTOFMEMORY;
1735 ctx.funcs = NULL;
1736 ctx.func_decls = NULL;
1737 ctx.global_vars = NULL;
1738 ctx.classes = NULL;
1739 ctx.labels = NULL;
1740 ctx.global_consts = NULL;
1741 ctx.stat_ctx = NULL;
1742 ctx.labels_cnt = ctx.labels_size = 0;
1744 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->main_code);
1745 if(FAILED(hres)) {
1746 release_compiler(&ctx);
1747 return hres;
1750 ctx.global_consts = ctx.const_decls;
1752 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1753 hres = create_function(&ctx, func_decl, &new_func);
1754 if(FAILED(hres)) {
1755 release_compiler(&ctx);
1756 return hres;
1759 new_func->next = ctx.funcs;
1760 ctx.funcs = new_func;
1763 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1764 hres = compile_class(&ctx, class_decl);
1765 if(FAILED(hres)) {
1766 release_compiler(&ctx);
1767 return hres;
1771 hres = check_script_collisions(&ctx, script);
1772 if(FAILED(hres)) {
1773 release_compiler(&ctx);
1774 return hres;
1777 if(ctx.global_vars) {
1778 dynamic_var_t *var;
1780 for(var = ctx.global_vars; var->next; var = var->next);
1782 var->next = script->global_vars;
1783 script->global_vars = ctx.global_vars;
1786 if(ctx.funcs) {
1787 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1789 new_func->next = script->global_funcs;
1790 script->global_funcs = ctx.funcs;
1793 if(ctx.classes) {
1794 class_desc_t *class = ctx.classes;
1796 while(1) {
1797 class->ctx = script;
1798 if(!class->next)
1799 break;
1800 class = class->next;
1803 class->next = script->classes;
1804 script->classes = ctx.classes;
1807 if(TRACE_ON(vbscript_disas))
1808 dump_code(&ctx);
1810 ctx.code = NULL;
1811 release_compiler(&ctx);
1813 list_add_tail(&script->code_list, &code->entry);
1814 *ret = code;
1815 return S_OK;