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
23 #include "parser.tab.h"
25 #include "wine/debug.h"
27 WINE_DEFAULT_DEBUG_CHANNEL(vbscript);
28 WINE_DECLARE_DEBUG_CHANNEL(vbscript_disas);
41 unsigned while_end_label;
42 unsigned sub_end_label;
43 unsigned func_end_label;
44 unsigned prop_end_label;
46 dim_decl_t *dim_decls;
47 dynamic_var_t *global_vars;
51 function_decl_t *func_decls;
53 class_desc_t *classes;
56 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
57 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
61 instr_arg_type_t arg1_type;
62 instr_arg_type_t arg2_type;
64 #define X(n,a,b,c) {#n,b,c},
69 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
74 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
77 TRACE_(vbscript_disas)("\t%d", arg->uint);
81 TRACE_(vbscript_disas)("\t%u", arg->uint);
84 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
93 static void dump_code(compile_ctx_t *ctx)
97 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
98 TRACE_(vbscript_disas)("%d:\t%s", (int)(instr-ctx->code->instrs), instr_info[instr->op].op_str);
99 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
100 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
101 TRACE_(vbscript_disas)("\n");
105 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
107 return vbsheap_alloc(&vbscode->heap, size);
110 static inline void *compiler_alloc_zero(vbscode_t *vbscode, size_t size)
114 ret = vbsheap_alloc(&vbscode->heap, size);
116 memset(ret, 0, size);
120 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
125 size = (strlenW(str)+1)*sizeof(WCHAR);
126 ret = compiler_alloc(vbscode, size);
128 memcpy(ret, str, size);
132 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
134 assert(id < ctx->instr_cnt);
135 return ctx->code->instrs + id;
138 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
140 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
142 if(ctx->instr_size == ctx->instr_cnt) {
145 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
149 ctx->code->instrs = new_instr;
150 ctx->instr_size *= 2;
153 ctx->code->instrs[ctx->instr_cnt].op = op;
154 return ctx->instr_cnt++;
157 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
161 ret = push_instr(ctx, op);
163 return E_OUTOFMEMORY;
165 instr_ptr(ctx, ret)->arg1.lng = arg;
169 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
173 ret = push_instr(ctx, op);
175 return E_OUTOFMEMORY;
177 instr_ptr(ctx, ret)->arg1.uint = arg;
181 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
186 str = compiler_alloc_string(ctx->code, arg);
188 return E_OUTOFMEMORY;
190 instr = push_instr(ctx, op);
192 return E_OUTOFMEMORY;
194 instr_ptr(ctx, instr)->arg1.str = str;
198 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
203 d = compiler_alloc(ctx->code, sizeof(double));
205 return E_OUTOFMEMORY;
207 instr = push_instr(ctx, op);
209 return E_OUTOFMEMORY;
212 instr_ptr(ctx, instr)->arg1.dbl = d;
216 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
218 if(!ctx->code->bstr_pool_size) {
219 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
220 if(!ctx->code->bstr_pool)
222 ctx->code->bstr_pool_size = 8;
223 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
226 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
230 ctx->code->bstr_pool = new_pool;
231 ctx->code->bstr_pool_size *= 2;
234 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
235 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
238 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
241 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
246 bstr = alloc_bstr_arg(ctx, arg);
248 return E_OUTOFMEMORY;
250 instr = push_instr(ctx, op);
252 return E_OUTOFMEMORY;
254 instr_ptr(ctx, instr)->arg1.bstr = bstr;
258 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
263 bstr = alloc_bstr_arg(ctx, arg1);
265 return E_OUTOFMEMORY;
267 instr = push_instr(ctx, op);
269 return E_OUTOFMEMORY;
271 instr_ptr(ctx, instr)->arg1.bstr = bstr;
272 instr_ptr(ctx, instr)->arg2.uint = arg2;
276 #define LABEL_FLAG 0x80000000
278 static unsigned alloc_label(compile_ctx_t *ctx)
280 if(!ctx->labels_size) {
281 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
284 ctx->labels_size = 8;
285 }else if(ctx->labels_size == ctx->labels_cnt) {
286 unsigned *new_labels;
288 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
292 ctx->labels = new_labels;
293 ctx->labels_size *= 2;
296 return ctx->labels_cnt++ | LABEL_FLAG;
299 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
301 assert(label & LABEL_FLAG);
302 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
305 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
307 unsigned arg_cnt = 0;
311 hres = compile_expression(ctx, args);
323 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
325 unsigned arg_cnt = 0;
328 hres = compile_args(ctx, expr->args, &arg_cnt);
333 hres = compile_expression(ctx, expr->obj_expr);
337 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
339 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
345 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
349 hres = compile_expression(ctx, expr->subexpr);
353 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
356 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
360 hres = compile_expression(ctx, expr->left);
364 hres = compile_expression(ctx, expr->right);
368 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
371 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
375 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
377 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
379 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
381 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
383 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
385 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
387 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
389 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
391 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
393 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
395 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gt);
397 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_gteq);
399 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
401 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_is);
403 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
405 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lt);
407 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_lteq);
409 return push_instr(ctx, OP_me) != -1 ? S_OK : E_OUTOFMEMORY;
411 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
413 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
415 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
417 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
419 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
421 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
423 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
425 return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
427 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
429 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
431 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
433 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
435 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
437 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
439 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
441 FIXME("Unimplemented expression type %d\n", expr->type);
448 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
450 unsigned cnd_jmp, endif_label = -1;
451 elseif_decl_t *elseif_decl;
454 hres = compile_expression(ctx, stat->expr);
458 cnd_jmp = push_instr(ctx, OP_jmp_false);
460 return E_OUTOFMEMORY;
462 hres = compile_statement(ctx, stat->if_stat);
466 if(stat->else_stat || stat->elseifs) {
467 endif_label = alloc_label(ctx);
468 if(endif_label == -1)
469 return E_OUTOFMEMORY;
471 hres = push_instr_addr(ctx, OP_jmp, endif_label);
476 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
477 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
479 hres = compile_expression(ctx, elseif_decl->expr);
483 cnd_jmp = push_instr(ctx, OP_jmp_false);
485 return E_OUTOFMEMORY;
487 hres = compile_statement(ctx, elseif_decl->stat);
491 hres = push_instr_addr(ctx, OP_jmp, endif_label);
496 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
498 if(stat->else_stat) {
499 hres = compile_statement(ctx, stat->else_stat);
504 if(endif_label != -1)
505 label_set_addr(ctx, endif_label);
509 static HRESULT compile_while_statement(compile_ctx_t *ctx, while_statement_t *stat)
511 unsigned start_addr, prev_label;
515 start_addr = ctx->instr_cnt;
517 hres = compile_expression(ctx, stat->expr);
521 jmp_end = push_instr(ctx, stat->stat.type == STAT_UNTIL ? OP_jmp_true : OP_jmp_false);
523 return E_OUTOFMEMORY;
525 if(stat->stat.type != STAT_WHILE) {
526 prev_label = ctx->while_end_label;
527 if((ctx->while_end_label = alloc_label(ctx)) == -1)
528 return E_OUTOFMEMORY;
531 hres = compile_statement(ctx, stat->body);
535 hres = push_instr_addr(ctx, OP_jmp, start_addr);
539 instr_ptr(ctx, jmp_end)->arg1.uint = ctx->instr_cnt;
541 if(stat->stat.type != STAT_WHILE) {
542 label_set_addr(ctx, ctx->while_end_label);
543 ctx->while_end_label = prev_label;
549 static HRESULT compile_dowhile_statement(compile_ctx_t *ctx, while_statement_t *stat)
551 unsigned start_addr, prev_label;
554 start_addr = ctx->instr_cnt;
556 prev_label = ctx->while_end_label;
557 if((ctx->while_end_label = alloc_label(ctx)) == -1)
558 return E_OUTOFMEMORY;
560 hres = compile_statement(ctx, stat->body);
564 hres = compile_expression(ctx, stat->expr);
568 hres = push_instr_addr(ctx, stat->stat.type == STAT_DOUNTIL ? OP_jmp_false : OP_jmp_true, start_addr);
572 label_set_addr(ctx, ctx->while_end_label);
573 ctx->while_end_label = prev_label;
577 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
581 hres = compile_expression(ctx, stat->value_expr);
585 if(stat->member_expr->args) {
586 FIXME("arguments support not implemented\n");
590 if(stat->member_expr->obj_expr) {
591 hres = compile_expression(ctx, stat->member_expr->obj_expr);
595 hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
597 hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
603 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
605 dim_decl_t *dim_decl;
607 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
608 if(!strcmpiW(dim_decl->name, name))
615 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
619 for(i = 0; i < ctx->func->arg_cnt; i++) {
620 if(!strcmpiW(ctx->func->args[i].name, name))
627 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
629 dim_decl_t *dim_decl = stat->dim_decls;
632 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
633 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
639 dim_decl = dim_decl->next;
642 dim_decl->next = ctx->dim_decls;
643 ctx->dim_decls = stat->dim_decls;
644 ctx->func->var_cnt++;
648 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
650 if(ctx->func != &ctx->code->global_code) {
651 FIXME("Function is not in the global code\n");
655 stat->func_decl->next = ctx->func_decls;
656 ctx->func_decls = stat->func_decl;
660 static HRESULT compile_exitdo_statement(compile_ctx_t *ctx)
662 if(ctx->while_end_label == -1) {
663 FIXME("Exit Do outside Do Loop\n");
667 return push_instr_addr(ctx, OP_jmp, ctx->while_end_label);
670 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
672 if(ctx->sub_end_label == -1) {
673 FIXME("Exit Sub outside Sub?\n");
677 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
680 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
682 if(ctx->func_end_label == -1) {
683 FIXME("Exit Function outside Function?\n");
687 return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
690 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
692 if(ctx->prop_end_label == -1) {
693 FIXME("Exit Property outside Property?\n");
697 return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
700 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
707 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
710 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
713 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
717 hres = compile_dowhile_statement(ctx, (while_statement_t*)stat);
720 hres = compile_exitdo_statement(ctx);
723 hres = compile_exitfunc_statement(ctx);
726 hres = compile_exitprop_statement(ctx);
729 hres = compile_exitsub_statement(ctx);
732 hres = compile_function_statement(ctx, (function_statement_t*)stat);
735 hres = compile_if_statement(ctx, (if_statement_t*)stat);
738 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
741 hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
746 hres = compile_while_statement(ctx, (while_statement_t*)stat);
749 FIXME("Unimplemented statement type %d\n", stat->type);
761 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
765 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
766 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
767 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
768 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
770 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
776 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
780 func->code_off = ctx->instr_cnt;
782 ctx->while_end_label = -1;
783 ctx->sub_end_label = -1;
784 ctx->func_end_label = -1;
785 ctx->prop_end_label = -1;
789 ctx->func_end_label = alloc_label(ctx);
790 if(ctx->func_end_label == -1)
791 return E_OUTOFMEMORY; /* FIXME ! */
794 ctx->sub_end_label = alloc_label(ctx);
795 if(ctx->sub_end_label == -1)
796 return E_OUTOFMEMORY;
802 ctx->prop_end_label = alloc_label(ctx);
803 if(ctx->prop_end_label == -1)
804 return E_OUTOFMEMORY;
811 ctx->dim_decls = NULL;
812 hres = compile_statement(ctx, stat);
817 assert(ctx->while_end_label == -1);
819 if(ctx->sub_end_label != -1)
820 label_set_addr(ctx, ctx->sub_end_label);
821 if(ctx->func_end_label != -1)
822 label_set_addr(ctx, ctx->func_end_label);
823 if(ctx->prop_end_label != -1)
824 label_set_addr(ctx, ctx->prop_end_label);
826 if(push_instr(ctx, OP_ret) == -1)
827 return E_OUTOFMEMORY;
829 resolve_labels(ctx, func->code_off);
832 dim_decl_t *dim_decl;
834 if(func->type == FUNC_GLOBAL) {
835 dynamic_var_t *new_var;
839 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
840 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
842 return E_OUTOFMEMORY;
844 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
846 return E_OUTOFMEMORY;
848 V_VT(&new_var->v) = VT_EMPTY;
850 new_var->next = ctx->global_vars;
851 ctx->global_vars = new_var;
856 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
858 return E_OUTOFMEMORY;
860 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
861 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
862 if(!func->vars[i].name)
863 return E_OUTOFMEMORY;
866 assert(i == func->var_cnt);
873 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
877 for(iter = ctx->funcs; iter; iter = iter->next) {
878 if(!strcmpiW(iter->name, name))
885 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
890 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
891 FIXME("%s: redefinition\n", debugstr_w(decl->name));
895 func = compiler_alloc(ctx->code, sizeof(*func));
897 return E_OUTOFMEMORY;
899 func->name = compiler_alloc_string(ctx->code, decl->name);
901 return E_OUTOFMEMORY;
905 func->code_ctx = ctx->code;
906 func->type = decl->type;
907 func->is_public = decl->is_public;
914 for(arg = decl->args; arg; arg = arg->next)
917 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
919 return E_OUTOFMEMORY;
921 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
922 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
923 if(!func->args[i].name)
924 return E_OUTOFMEMORY;
925 func->args[i].by_ref = arg->by_ref;
931 hres = compile_func(ctx, decl->body, func);
939 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
943 for(iter = ctx->classes; iter; iter = iter->next) {
944 if(!strcmpiW(iter->name, name))
951 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
953 vbdisp_invoke_type_t invoke_type;
954 function_decl_t *funcprop_decl;
957 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
959 return E_OUTOFMEMORY;
961 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
962 switch(funcprop_decl->type) {
967 invoke_type = VBDISP_CALLGET;
970 invoke_type = VBDISP_LET;
973 invoke_type = VBDISP_SET;
979 assert(!desc->entries[invoke_type]);
981 if(funcprop_decl->is_public)
982 desc->is_public = TRUE;
984 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
992 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
996 for(i=0; i < class_desc->func_cnt; i++) {
997 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
1004 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
1006 function_decl_t *func_decl, *func_prop_decl;
1007 class_prop_decl_t *prop_decl;
1008 class_desc_t *class_desc;
1012 static const WCHAR class_initializeW[] = {'c','l','a','s','s','_','i','n','i','t','i','a','l','i','z','e',0};
1013 static const WCHAR class_terminateW[] = {'c','l','a','s','s','_','t','e','r','m','i','n','a','t','e',0};
1015 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
1016 || lookup_class_name(ctx, class_decl->name)) {
1017 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
1021 class_desc = compiler_alloc_zero(ctx->code, sizeof(*class_desc));
1023 return E_OUTOFMEMORY;
1025 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
1026 if(!class_desc->name)
1027 return E_OUTOFMEMORY;
1029 class_desc->func_cnt = 1; /* always allocate slot for default getter */
1031 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
1032 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1033 if(func_prop_decl->type == FUNC_DEFGET)
1037 class_desc->func_cnt++;
1040 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
1041 if(!class_desc->funcs)
1042 return E_OUTOFMEMORY;
1043 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
1045 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
1046 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
1047 if(func_prop_decl->type == FUNC_DEFGET) {
1053 if(!strcmpiW(class_initializeW, func_decl->name)) {
1054 if(func_decl->type != FUNC_SUB) {
1055 FIXME("class initializer is not sub\n");
1059 class_desc->class_initialize_id = i;
1060 }else if(!strcmpiW(class_terminateW, func_decl->name)) {
1061 if(func_decl->type != FUNC_SUB) {
1062 FIXME("class terminator is not sub\n");
1066 class_desc->class_terminate_id = i;
1069 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
1074 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
1075 class_desc->prop_cnt++;
1077 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
1078 if(!class_desc->props)
1079 return E_OUTOFMEMORY;
1081 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
1082 if(lookup_class_funcs(class_desc, prop_decl->name)) {
1083 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
1087 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
1088 if(!class_desc->props[i].name)
1089 return E_OUTOFMEMORY;
1091 class_desc->props[i].is_public = prop_decl->is_public;
1094 class_desc->next = ctx->classes;
1095 ctx->classes = class_desc;
1099 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
1101 class_desc_t *class;
1105 for(var = script->global_vars; var; var = var->next) {
1106 if(!strcmpiW(var->name, identifier))
1110 for(func = script->global_funcs; func; func = func->next) {
1111 if(!strcmpiW(func->name, identifier))
1115 for(class = script->classes; class; class = class->next) {
1116 if(!strcmpiW(class->name, identifier))
1123 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
1125 class_desc_t *class;
1129 for(var = ctx->global_vars; var; var = var->next) {
1130 if(lookup_script_identifier(script, var->name)) {
1131 FIXME("%s: redefined\n", debugstr_w(var->name));
1136 for(func = ctx->funcs; func; func = func->next) {
1137 if(lookup_script_identifier(script, func->name)) {
1138 FIXME("%s: redefined\n", debugstr_w(func->name));
1143 for(class = ctx->classes; class; class = class->next) {
1144 if(lookup_script_identifier(script, class->name)) {
1145 FIXME("%s: redefined\n", debugstr_w(class->name));
1153 void release_vbscode(vbscode_t *code)
1157 list_remove(&code->entry);
1159 for(i=0; i < code->bstr_cnt; i++)
1160 SysFreeString(code->bstr_pool[i]);
1162 vbsheap_free(&code->heap);
1164 heap_free(code->bstr_pool);
1165 heap_free(code->source);
1166 heap_free(code->instrs);
1170 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1174 ret = heap_alloc(sizeof(*ret));
1178 ret->source = heap_strdupW(source);
1184 ret->instrs = heap_alloc(32*sizeof(instr_t));
1186 release_vbscode(ret);
1191 ctx->instr_size = 32;
1192 vbsheap_init(&ret->heap);
1194 ret->option_explicit = ctx->parser.option_explicit;
1196 ret->bstr_pool = NULL;
1197 ret->bstr_pool_size = 0;
1199 ret->global_executed = FALSE;
1201 ret->global_code.type = FUNC_GLOBAL;
1202 ret->global_code.name = NULL;
1203 ret->global_code.code_ctx = ret;
1204 ret->global_code.vars = NULL;
1205 ret->global_code.var_cnt = 0;
1206 ret->global_code.arg_cnt = 0;
1207 ret->global_code.args = NULL;
1209 list_init(&ret->entry);
1213 static void release_compiler(compile_ctx_t *ctx)
1215 parser_release(&ctx->parser);
1216 heap_free(ctx->labels);
1218 release_vbscode(ctx->code);
1221 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1223 function_t *new_func;
1224 function_decl_t *func_decl;
1225 class_decl_t *class_decl;
1230 hres = parse_script(&ctx.parser, src);
1234 code = ctx.code = alloc_vbscode(&ctx, src);
1236 return E_OUTOFMEMORY;
1239 ctx.func_decls = NULL;
1240 ctx.global_vars = NULL;
1241 ctx.dim_decls = NULL;
1244 ctx.labels_cnt = ctx.labels_size = 0;
1246 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1248 release_compiler(&ctx);
1252 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1253 hres = create_function(&ctx, func_decl, &new_func);
1255 release_compiler(&ctx);
1259 new_func->next = ctx.funcs;
1260 ctx.funcs = new_func;
1263 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1264 hres = compile_class(&ctx, class_decl);
1266 release_compiler(&ctx);
1271 hres = check_script_collisions(&ctx, script);
1273 release_compiler(&ctx);
1277 if(ctx.global_vars) {
1280 for(var = ctx.global_vars; var->next; var = var->next);
1282 var->next = script->global_vars;
1283 script->global_vars = ctx.global_vars;
1287 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1289 new_func->next = script->global_funcs;
1290 script->global_funcs = ctx.funcs;
1294 class_desc_t *class = ctx.classes;
1297 class->ctx = script;
1300 class = class->next;
1303 class->next = script->classes;
1304 script->classes = ctx.classes;
1307 if(TRACE_ON(vbscript_disas))
1311 release_compiler(&ctx);
1313 list_add_tail(&script->code_list, &code->entry);