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 sub_end_label;
42 unsigned func_end_label;
43 unsigned prop_end_label;
45 dim_decl_t *dim_decls;
46 dynamic_var_t *global_vars;
50 function_decl_t *func_decls;
52 class_desc_t *classes;
55 static HRESULT compile_expression(compile_ctx_t*,expression_t*);
56 static HRESULT compile_statement(compile_ctx_t*,statement_t*);
60 instr_arg_type_t arg1_type;
61 instr_arg_type_t arg2_type;
63 #define X(n,a,b,c) {#n,b,c},
68 static void dump_instr_arg(instr_arg_type_t type, instr_arg_t *arg)
73 TRACE_(vbscript_disas)("\t%s", debugstr_w(arg->str));
76 TRACE_(vbscript_disas)("\t%d", arg->uint);
80 TRACE_(vbscript_disas)("\t%u", arg->uint);
83 TRACE_(vbscript_disas)("\t%lf", *arg->dbl);
92 static void dump_code(compile_ctx_t *ctx)
96 for(instr = ctx->code->instrs; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
97 TRACE_(vbscript_disas)("%d:\t%s", instr-ctx->code->instrs, instr_info[instr->op].op_str);
98 dump_instr_arg(instr_info[instr->op].arg1_type, &instr->arg1);
99 dump_instr_arg(instr_info[instr->op].arg2_type, &instr->arg2);
100 TRACE_(vbscript_disas)("\n");
104 static inline void *compiler_alloc(vbscode_t *vbscode, size_t size)
106 return vbsheap_alloc(&vbscode->heap, size);
109 static WCHAR *compiler_alloc_string(vbscode_t *vbscode, const WCHAR *str)
114 size = (strlenW(str)+1)*sizeof(WCHAR);
115 ret = compiler_alloc(vbscode, size);
117 memcpy(ret, str, size);
121 static inline instr_t *instr_ptr(compile_ctx_t *ctx, unsigned id)
123 assert(id < ctx->instr_cnt);
124 return ctx->code->instrs + id;
127 static unsigned push_instr(compile_ctx_t *ctx, vbsop_t op)
129 assert(ctx->instr_size && ctx->instr_size >= ctx->instr_cnt);
131 if(ctx->instr_size == ctx->instr_cnt) {
134 new_instr = heap_realloc(ctx->code->instrs, ctx->instr_size*2*sizeof(instr_t));
138 ctx->code->instrs = new_instr;
139 ctx->instr_size *= 2;
142 ctx->code->instrs[ctx->instr_cnt].op = op;
143 return ctx->instr_cnt++;
146 static HRESULT push_instr_int(compile_ctx_t *ctx, vbsop_t op, LONG arg)
150 ret = push_instr(ctx, op);
152 return E_OUTOFMEMORY;
154 instr_ptr(ctx, ret)->arg1.lng = arg;
158 static HRESULT push_instr_addr(compile_ctx_t *ctx, vbsop_t op, unsigned arg)
162 ret = push_instr(ctx, op);
164 return E_OUTOFMEMORY;
166 instr_ptr(ctx, ret)->arg1.uint = arg;
170 static HRESULT push_instr_str(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
175 str = compiler_alloc_string(ctx->code, arg);
177 return E_OUTOFMEMORY;
179 instr = push_instr(ctx, op);
181 return E_OUTOFMEMORY;
183 instr_ptr(ctx, instr)->arg1.str = str;
187 static HRESULT push_instr_double(compile_ctx_t *ctx, vbsop_t op, double arg)
192 d = compiler_alloc(ctx->code, sizeof(double));
194 return E_OUTOFMEMORY;
196 instr = push_instr(ctx, op);
198 return E_OUTOFMEMORY;
201 instr_ptr(ctx, instr)->arg1.dbl = d;
205 static BSTR alloc_bstr_arg(compile_ctx_t *ctx, const WCHAR *str)
207 if(!ctx->code->bstr_pool_size) {
208 ctx->code->bstr_pool = heap_alloc(8 * sizeof(BSTR));
209 if(!ctx->code->bstr_pool)
211 ctx->code->bstr_pool_size = 8;
212 }else if(ctx->code->bstr_pool_size == ctx->code->bstr_cnt) {
215 new_pool = heap_realloc(ctx->code->bstr_pool, ctx->code->bstr_pool_size*2*sizeof(BSTR));
219 ctx->code->bstr_pool = new_pool;
220 ctx->code->bstr_pool_size *= 2;
223 ctx->code->bstr_pool[ctx->code->bstr_cnt] = SysAllocString(str);
224 if(!ctx->code->bstr_pool[ctx->code->bstr_cnt])
227 return ctx->code->bstr_pool[ctx->code->bstr_cnt++];
230 static HRESULT push_instr_bstr(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg)
235 bstr = alloc_bstr_arg(ctx, arg);
237 return E_OUTOFMEMORY;
239 instr = push_instr(ctx, op);
241 return E_OUTOFMEMORY;
243 instr_ptr(ctx, instr)->arg1.bstr = bstr;
247 static HRESULT push_instr_bstr_uint(compile_ctx_t *ctx, vbsop_t op, const WCHAR *arg1, unsigned arg2)
252 bstr = alloc_bstr_arg(ctx, arg1);
254 return E_OUTOFMEMORY;
256 instr = push_instr(ctx, op);
258 return E_OUTOFMEMORY;
260 instr_ptr(ctx, instr)->arg1.bstr = bstr;
261 instr_ptr(ctx, instr)->arg2.uint = arg2;
265 #define LABEL_FLAG 0x80000000
267 static unsigned alloc_label(compile_ctx_t *ctx)
269 if(!ctx->labels_size) {
270 ctx->labels = heap_alloc(8 * sizeof(*ctx->labels));
273 ctx->labels_size = 8;
274 }else if(ctx->labels_size == ctx->labels_cnt) {
275 unsigned *new_labels;
277 new_labels = heap_realloc(ctx->labels, 2*ctx->labels_size*sizeof(*ctx->labels));
281 ctx->labels = new_labels;
282 ctx->labels_size *= 2;
285 return ctx->labels_cnt++ | LABEL_FLAG;
288 static inline void label_set_addr(compile_ctx_t *ctx, unsigned label)
290 assert(label & LABEL_FLAG);
291 ctx->labels[label & ~LABEL_FLAG] = ctx->instr_cnt;
294 static HRESULT compile_args(compile_ctx_t *ctx, expression_t *args, unsigned *ret)
296 unsigned arg_cnt = 0;
300 hres = compile_expression(ctx, args);
312 static HRESULT compile_member_expression(compile_ctx_t *ctx, member_expression_t *expr, BOOL ret_val)
314 unsigned arg_cnt = 0;
317 hres = compile_args(ctx, expr->args, &arg_cnt);
322 hres = compile_expression(ctx, expr->obj_expr);
326 hres = push_instr_bstr_uint(ctx, ret_val ? OP_mcall : OP_mcallv, expr->identifier, arg_cnt);
328 hres = push_instr_bstr_uint(ctx, ret_val ? OP_icall : OP_icallv, expr->identifier, arg_cnt);
334 static HRESULT compile_unary_expression(compile_ctx_t *ctx, unary_expression_t *expr, vbsop_t op)
338 hres = compile_expression(ctx, expr->subexpr);
342 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
345 static HRESULT compile_binary_expression(compile_ctx_t *ctx, binary_expression_t *expr, vbsop_t op)
349 hres = compile_expression(ctx, expr->left);
353 hres = compile_expression(ctx, expr->right);
357 return push_instr(ctx, op) == -1 ? E_OUTOFMEMORY : S_OK;
360 static HRESULT compile_expression(compile_ctx_t *ctx, expression_t *expr)
364 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_add);
366 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_and);
368 return push_instr_int(ctx, OP_bool, ((bool_expression_t*)expr)->value);
370 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_concat);
372 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_div);
374 return push_instr_double(ctx, OP_double, ((double_expression_t*)expr)->value);
376 return push_instr(ctx, OP_empty) != -1 ? S_OK : E_OUTOFMEMORY;
378 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_equal);
380 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_eqv);
382 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_exp);
384 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_idiv);
386 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_imp);
388 return compile_member_expression(ctx, (member_expression_t*)expr, TRUE);
390 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mod);
392 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_mul);
394 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_neg);
396 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_nequal);
398 return push_instr_str(ctx, OP_new, ((string_expression_t*)expr)->value);
400 return compile_unary_expression(ctx, (unary_expression_t*)expr, OP_not);
402 return push_instr(ctx, OP_nothing) != -1 ? S_OK : E_OUTOFMEMORY;
404 return push_instr(ctx, OP_null) != -1 ? S_OK : E_OUTOFMEMORY;
406 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_or);
408 return push_instr_str(ctx, OP_string, ((string_expression_t*)expr)->value);
410 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_sub);
412 return push_instr_int(ctx, OP_short, ((int_expression_t*)expr)->value);
414 return push_instr_int(ctx, OP_long, ((int_expression_t*)expr)->value);
416 return compile_binary_expression(ctx, (binary_expression_t*)expr, OP_xor);
418 FIXME("Unimplemented expression type %d\n", expr->type);
425 static HRESULT compile_if_statement(compile_ctx_t *ctx, if_statement_t *stat)
427 unsigned cnd_jmp, endif_label = -1;
428 elseif_decl_t *elseif_decl;
431 hres = compile_expression(ctx, stat->expr);
435 cnd_jmp = push_instr(ctx, OP_jmp_false);
437 return E_OUTOFMEMORY;
439 hres = compile_statement(ctx, stat->if_stat);
443 if(stat->else_stat || stat->elseifs) {
444 endif_label = alloc_label(ctx);
445 if(endif_label == -1)
446 return E_OUTOFMEMORY;
448 hres = push_instr_addr(ctx, OP_jmp, endif_label);
453 for(elseif_decl = stat->elseifs; elseif_decl; elseif_decl = elseif_decl->next) {
454 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
456 hres = compile_expression(ctx, elseif_decl->expr);
460 cnd_jmp = push_instr(ctx, OP_jmp_false);
462 return E_OUTOFMEMORY;
464 hres = compile_statement(ctx, elseif_decl->stat);
468 hres = push_instr_addr(ctx, OP_jmp, endif_label);
473 instr_ptr(ctx, cnd_jmp)->arg1.uint = ctx->instr_cnt;
475 if(stat->else_stat) {
476 hres = compile_statement(ctx, stat->else_stat);
481 if(endif_label != -1)
482 label_set_addr(ctx, endif_label);
486 static HRESULT compile_assign_statement(compile_ctx_t *ctx, assign_statement_t *stat, BOOL is_set)
490 hres = compile_expression(ctx, stat->value_expr);
494 if(stat->member_expr->args) {
495 FIXME("arguments support not implemented\n");
499 if(stat->member_expr->obj_expr) {
500 hres = compile_expression(ctx, stat->member_expr->obj_expr);
504 hres = push_instr_bstr(ctx, is_set ? OP_set_member : OP_assign_member, stat->member_expr->identifier);
506 hres = push_instr_bstr(ctx, is_set ? OP_set_ident : OP_assign_ident, stat->member_expr->identifier);
512 static BOOL lookup_dim_decls(compile_ctx_t *ctx, const WCHAR *name)
514 dim_decl_t *dim_decl;
516 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
517 if(!strcmpiW(dim_decl->name, name))
524 static BOOL lookup_args_name(compile_ctx_t *ctx, const WCHAR *name)
528 for(i = 0; i < ctx->func->arg_cnt; i++) {
529 if(!strcmpiW(ctx->func->args[i].name, name))
536 static HRESULT compile_dim_statement(compile_ctx_t *ctx, dim_statement_t *stat)
538 dim_decl_t *dim_decl = stat->dim_decls;
541 if(lookup_dim_decls(ctx, dim_decl->name) || lookup_args_name(ctx, dim_decl->name)) {
542 FIXME("dim %s name redefined\n", debugstr_w(dim_decl->name));
548 dim_decl = dim_decl->next;
551 dim_decl->next = ctx->dim_decls;
552 ctx->dim_decls = stat->dim_decls;
553 ctx->func->var_cnt++;
557 static HRESULT compile_function_statement(compile_ctx_t *ctx, function_statement_t *stat)
559 if(ctx->func != &ctx->code->global_code) {
560 FIXME("Function is not in the global code\n");
564 stat->func_decl->next = ctx->func_decls;
565 ctx->func_decls = stat->func_decl;
569 static HRESULT compile_exitsub_statement(compile_ctx_t *ctx)
571 if(ctx->sub_end_label == -1) {
572 FIXME("Exit Sub outside Sub?\n");
576 return push_instr_addr(ctx, OP_jmp, ctx->sub_end_label);
579 static HRESULT compile_exitfunc_statement(compile_ctx_t *ctx)
581 if(ctx->func_end_label == -1) {
582 FIXME("Exit Function outside Function?\n");
586 return push_instr_addr(ctx, OP_jmp, ctx->func_end_label);
589 static HRESULT compile_exitprop_statement(compile_ctx_t *ctx)
591 if(ctx->prop_end_label == -1) {
592 FIXME("Exit Property outside Property?\n");
596 return push_instr_addr(ctx, OP_jmp, ctx->prop_end_label);
599 static HRESULT compile_statement(compile_ctx_t *ctx, statement_t *stat)
606 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, FALSE);
609 hres = compile_member_expression(ctx, ((call_statement_t*)stat)->expr, FALSE);
612 hres = compile_dim_statement(ctx, (dim_statement_t*)stat);
615 hres = compile_exitfunc_statement(ctx);
618 hres = compile_exitprop_statement(ctx);
621 hres = compile_exitsub_statement(ctx);
624 hres = compile_function_statement(ctx, (function_statement_t*)stat);
627 hres = compile_if_statement(ctx, (if_statement_t*)stat);
630 hres = compile_assign_statement(ctx, (assign_statement_t*)stat, TRUE);
633 hres = push_instr(ctx, OP_stop) == -1 ? E_OUTOFMEMORY : S_OK;
636 FIXME("Unimplemented statement type %d\n", stat->type);
648 static void resolve_labels(compile_ctx_t *ctx, unsigned off)
652 for(instr = ctx->code->instrs+off; instr < ctx->code->instrs+ctx->instr_cnt; instr++) {
653 if(instr_info[instr->op].arg1_type == ARG_ADDR && (instr->arg1.uint & LABEL_FLAG)) {
654 assert((instr->arg1.uint & ~LABEL_FLAG) < ctx->labels_cnt);
655 instr->arg1.uint = ctx->labels[instr->arg1.uint & ~LABEL_FLAG];
657 assert(instr_info[instr->op].arg2_type != ARG_ADDR);
663 static HRESULT compile_func(compile_ctx_t *ctx, statement_t *stat, function_t *func)
667 func->code_off = ctx->instr_cnt;
669 ctx->sub_end_label = -1;
670 ctx->func_end_label = -1;
671 ctx->prop_end_label = -1;
675 ctx->func_end_label = alloc_label(ctx);
676 if(ctx->func_end_label == -1)
677 return E_OUTOFMEMORY; /* FIXME ! */
680 ctx->sub_end_label = alloc_label(ctx);
681 if(ctx->sub_end_label == -1)
682 return E_OUTOFMEMORY;
688 ctx->prop_end_label = alloc_label(ctx);
689 if(ctx->prop_end_label == -1)
690 return E_OUTOFMEMORY;
697 ctx->dim_decls = NULL;
698 hres = compile_statement(ctx, stat);
703 if(ctx->sub_end_label != -1)
704 label_set_addr(ctx, ctx->sub_end_label);
705 if(ctx->func_end_label != -1)
706 label_set_addr(ctx, ctx->func_end_label);
707 if(ctx->prop_end_label != -1)
708 label_set_addr(ctx, ctx->prop_end_label);
710 if(push_instr(ctx, OP_ret) == -1)
711 return E_OUTOFMEMORY;
713 resolve_labels(ctx, func->code_off);
716 dim_decl_t *dim_decl;
718 if(func->type == FUNC_GLOBAL) {
719 dynamic_var_t *new_var;
723 for(dim_decl = ctx->dim_decls; dim_decl; dim_decl = dim_decl->next) {
724 new_var = compiler_alloc(ctx->code, sizeof(*new_var));
726 return E_OUTOFMEMORY;
728 new_var->name = compiler_alloc_string(ctx->code, dim_decl->name);
730 return E_OUTOFMEMORY;
732 V_VT(&new_var->v) = VT_EMPTY;
734 new_var->next = ctx->global_vars;
735 ctx->global_vars = new_var;
740 func->vars = compiler_alloc(ctx->code, func->var_cnt * sizeof(var_desc_t));
742 return E_OUTOFMEMORY;
744 for(dim_decl = ctx->dim_decls, i=0; dim_decl; dim_decl = dim_decl->next, i++) {
745 func->vars[i].name = compiler_alloc_string(ctx->code, dim_decl->name);
746 if(!func->vars[i].name)
747 return E_OUTOFMEMORY;
750 assert(i == func->var_cnt);
757 static BOOL lookup_funcs_name(compile_ctx_t *ctx, const WCHAR *name)
761 for(iter = ctx->funcs; iter; iter = iter->next) {
762 if(!strcmpiW(iter->name, name))
769 static HRESULT create_function(compile_ctx_t *ctx, function_decl_t *decl, function_t **ret)
774 if(lookup_dim_decls(ctx, decl->name) || lookup_funcs_name(ctx, decl->name)) {
775 FIXME("%s: redefinition\n", debugstr_w(decl->name));
779 func = compiler_alloc(ctx->code, sizeof(*func));
781 return E_OUTOFMEMORY;
783 func->name = compiler_alloc_string(ctx->code, decl->name);
785 return E_OUTOFMEMORY;
789 func->code_ctx = ctx->code;
790 func->type = decl->type;
791 func->is_public = decl->is_public;
798 for(arg = decl->args; arg; arg = arg->next)
801 func->args = compiler_alloc(ctx->code, func->arg_cnt * sizeof(arg_desc_t));
803 return E_OUTOFMEMORY;
805 for(i = 0, arg = decl->args; arg; arg = arg->next, i++) {
806 func->args[i].name = compiler_alloc_string(ctx->code, arg->name);
807 if(!func->args[i].name)
808 return E_OUTOFMEMORY;
809 func->args[i].by_ref = arg->by_ref;
815 hres = compile_func(ctx, decl->body, func);
823 static BOOL lookup_class_name(compile_ctx_t *ctx, const WCHAR *name)
827 for(iter = ctx->classes; iter; iter = iter->next) {
828 if(!strcmpiW(iter->name, name))
835 static HRESULT create_class_funcprop(compile_ctx_t *ctx, function_decl_t *func_decl, vbdisp_funcprop_desc_t *desc)
837 vbdisp_invoke_type_t invoke_type;
838 function_decl_t *funcprop_decl;
841 desc->name = compiler_alloc_string(ctx->code, func_decl->name);
843 return E_OUTOFMEMORY;
845 for(funcprop_decl = func_decl; funcprop_decl; funcprop_decl = funcprop_decl->next_prop_func) {
846 switch(funcprop_decl->type) {
851 invoke_type = VBDISP_CALLGET;
854 invoke_type = VBDISP_LET;
857 invoke_type = VBDISP_SET;
863 assert(!desc->entries[invoke_type]);
865 if(funcprop_decl->is_public)
866 desc->is_public = TRUE;
868 hres = create_function(ctx, funcprop_decl, desc->entries+invoke_type);
876 static BOOL lookup_class_funcs(class_desc_t *class_desc, const WCHAR *name)
880 for(i=0; i < class_desc->func_cnt; i++) {
881 if(class_desc->funcs[i].name && !strcmpiW(class_desc->funcs[i].name, name))
888 static HRESULT compile_class(compile_ctx_t *ctx, class_decl_t *class_decl)
890 function_decl_t *func_decl, *func_prop_decl;
891 class_prop_decl_t *prop_decl;
892 class_desc_t *class_desc;
896 if(lookup_dim_decls(ctx, class_decl->name) || lookup_funcs_name(ctx, class_decl->name)
897 || lookup_class_name(ctx, class_decl->name)) {
898 FIXME("%s: redefinition\n", debugstr_w(class_decl->name));
902 class_desc = compiler_alloc(ctx->code, sizeof(*class_desc));
904 return E_OUTOFMEMORY;
906 class_desc->name = compiler_alloc_string(ctx->code, class_decl->name);
907 if(!class_desc->name)
908 return E_OUTOFMEMORY;
910 class_desc->func_cnt = 1; /* always allocate slot for default getter */
911 class_desc->prop_cnt = 0;
913 for(func_decl = class_decl->funcs; func_decl; func_decl = func_decl->next) {
914 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
915 if(func_prop_decl->type == FUNC_DEFGET)
919 class_desc->func_cnt++;
922 class_desc->funcs = compiler_alloc(ctx->code, class_desc->func_cnt*sizeof(*class_desc->funcs));
923 if(!class_desc->funcs)
924 return E_OUTOFMEMORY;
925 memset(class_desc->funcs, 0, class_desc->func_cnt*sizeof(*class_desc->funcs));
927 for(func_decl = class_decl->funcs, i=1; func_decl; func_decl = func_decl->next, i++) {
928 for(func_prop_decl = func_decl; func_prop_decl; func_prop_decl = func_prop_decl->next_prop_func) {
929 if(func_prop_decl->type == FUNC_DEFGET) {
935 hres = create_class_funcprop(ctx, func_decl, class_desc->funcs + (func_prop_decl ? 0 : i));
940 for(prop_decl = class_decl->props; prop_decl; prop_decl = prop_decl->next)
941 class_desc->prop_cnt++;
943 class_desc->props = compiler_alloc(ctx->code, class_desc->prop_cnt*sizeof(*class_desc->props));
944 if(!class_desc->props)
945 return E_OUTOFMEMORY;
947 for(prop_decl = class_decl->props, i=0; prop_decl; prop_decl = prop_decl->next, i++) {
948 if(lookup_class_funcs(class_desc, prop_decl->name)) {
949 FIXME("Property %s redefined\n", debugstr_w(prop_decl->name));
953 class_desc->props[i].name = compiler_alloc_string(ctx->code, prop_decl->name);
954 if(!class_desc->props[i].name)
955 return E_OUTOFMEMORY;
957 class_desc->props[i].is_public = prop_decl->is_public;
960 class_desc->next = ctx->classes;
961 ctx->classes = class_desc;
965 static BOOL lookup_script_identifier(script_ctx_t *script, const WCHAR *identifier)
971 for(var = script->global_vars; var; var = var->next) {
972 if(!strcmpiW(var->name, identifier))
976 for(func = script->global_funcs; func; func = func->next) {
977 if(!strcmpiW(func->name, identifier))
981 for(class = script->classes; class; class = class->next) {
982 if(!strcmpiW(class->name, identifier))
989 static HRESULT check_script_collisions(compile_ctx_t *ctx, script_ctx_t *script)
995 for(var = ctx->global_vars; var; var = var->next) {
996 if(lookup_script_identifier(script, var->name)) {
997 FIXME("%s: redefined\n", debugstr_w(var->name));
1002 for(func = ctx->funcs; func; func = func->next) {
1003 if(lookup_script_identifier(script, func->name)) {
1004 FIXME("%s: redefined\n", debugstr_w(func->name));
1009 for(class = ctx->classes; class; class = class->next) {
1010 if(lookup_script_identifier(script, class->name)) {
1011 FIXME("%s: redefined\n", debugstr_w(class->name));
1019 void release_vbscode(vbscode_t *code)
1023 list_remove(&code->entry);
1025 for(i=0; i < code->bstr_cnt; i++)
1026 SysFreeString(code->bstr_pool[i]);
1028 vbsheap_free(&code->heap);
1030 heap_free(code->bstr_pool);
1031 heap_free(code->source);
1032 heap_free(code->instrs);
1036 static vbscode_t *alloc_vbscode(compile_ctx_t *ctx, const WCHAR *source)
1040 ret = heap_alloc(sizeof(*ret));
1044 ret->source = heap_strdupW(source);
1050 ret->instrs = heap_alloc(32*sizeof(instr_t));
1052 release_vbscode(ret);
1057 ctx->instr_size = 32;
1058 vbsheap_init(&ret->heap);
1060 ret->option_explicit = ctx->parser.option_explicit;
1062 ret->bstr_pool = NULL;
1063 ret->bstr_pool_size = 0;
1065 ret->global_executed = FALSE;
1067 ret->global_code.type = FUNC_GLOBAL;
1068 ret->global_code.name = NULL;
1069 ret->global_code.code_ctx = ret;
1070 ret->global_code.vars = NULL;
1071 ret->global_code.var_cnt = 0;
1072 ret->global_code.arg_cnt = 0;
1073 ret->global_code.args = NULL;
1075 list_init(&ret->entry);
1079 static void release_compiler(compile_ctx_t *ctx)
1081 parser_release(&ctx->parser);
1082 heap_free(ctx->labels);
1084 release_vbscode(ctx->code);
1087 HRESULT compile_script(script_ctx_t *script, const WCHAR *src, vbscode_t **ret)
1089 function_t *new_func;
1090 function_decl_t *func_decl;
1091 class_decl_t *class_decl;
1096 hres = parse_script(&ctx.parser, src);
1100 code = ctx.code = alloc_vbscode(&ctx, src);
1102 return E_OUTOFMEMORY;
1105 ctx.func_decls = NULL;
1106 ctx.global_vars = NULL;
1107 ctx.dim_decls = NULL;
1110 ctx.labels_cnt = ctx.labels_size = 0;
1112 hres = compile_func(&ctx, ctx.parser.stats, &ctx.code->global_code);
1114 release_compiler(&ctx);
1118 for(func_decl = ctx.func_decls; func_decl; func_decl = func_decl->next) {
1119 hres = create_function(&ctx, func_decl, &new_func);
1121 release_compiler(&ctx);
1125 new_func->next = ctx.funcs;
1126 ctx.funcs = new_func;
1129 for(class_decl = ctx.parser.class_decls; class_decl; class_decl = class_decl->next) {
1130 hres = compile_class(&ctx, class_decl);
1132 release_compiler(&ctx);
1137 hres = check_script_collisions(&ctx, script);
1139 release_compiler(&ctx);
1143 if(ctx.global_vars) {
1146 for(var = ctx.global_vars; var->next; var = var->next);
1148 var->next = script->global_vars;
1149 script->global_vars = ctx.global_vars;
1153 for(new_func = ctx.funcs; new_func->next; new_func = new_func->next);
1155 new_func->next = script->global_funcs;
1156 script->global_funcs = ctx.funcs;
1160 class_desc_t *class = ctx.classes;
1163 class->ctx = script;
1166 class = class->next;
1169 class->next = script->classes;
1170 script->classes = ctx.classes;
1173 if(TRACE_ON(vbscript_disas))
1177 release_compiler(&ctx);
1179 list_add_tail(&script->code_list, &code->entry);