+ state->method->is_global = 1;
+ state->method->late_binding = 1; // for global methods, always push local_0 on the scope stack
+ }
+ if(state->method->is_constructor)
+ name = "__as3_constructor__";
+
+ state->method->info = registerfunction(getset, mod, name, params, return_type, 0);
+
+ function_initvars(state->method, params, mod->flags, 1);
+
+ dict_put(global->token2info, (void*)(ptroff_t)as3_tokencount, state->method);
+ }
+
+ if(as3_pass == 2) {
+ state->method = dict_lookup(global->token2info, (void*)(ptroff_t)as3_tokencount);
+ state->method->variable_count = 0;
+ parserassert(state->method);
+
+ if(state->cls) {
+ memberinfo_t*m = registry_findmember(state->cls->info, mod->ns, name, 2);
+ check_override(m, mod->flags);
+ }
+
+ if(state->cls) {
+ state->cls->has_constructor |= state->method->is_constructor;
+ }
+
+ state->method->info->return_type = return_type;
+ function_initvars(state->method, params, mod->flags, 1);
+ }
+}
+
+static abc_method_t* endfunction(modifiers_t*mod, enum yytokentype getset, char*name,
+ params_t*params, classinfo_t*return_type, code_t*body)
+{
+ if(as3_pass==1) {
+ // store inner methods in variables
+ function_initvars(state->method, 0, 0, 0);
+
+ methodstate_list_t*ml = state->method->innerfunctions;
+
+ dict_t*xvars = dict_new();
+
+ while(ml) {
+ methodstate_t*m = ml->methodstate;
+ parserassert(m->inner);
+ if(m->unresolved_variables) {
+ dict_t*d = m->unresolved_variables;
+ int t;
+ for(t=0;t<d->hashsize;t++) {
+ dictentry_t*l = d->slots[t];
+ while(l) {
+ /* check parent method's variables */
+ variable_t*v;
+ if((v=find_variable(state, l->key))) {
+ m->uses_parent_function = 1;
+ state->method->uses_slots = 1;
+ dict_put(xvars, l->key, 0);
+ }
+ l = l->next;
+ }
+ if(l) break;
+ }
+
+ dict_destroy(m->unresolved_variables);
+ m->unresolved_variables = 0;
+ }
+ ml = ml->next;
+ }
+
+ if(state->method->uses_slots) {
+ state->method->slots = dict_new();
+ int i = 1;
+ DICT_ITERATE_ITEMS(state->vars, char*, name, variable_t*, v) {
+ if(!name) syntaxerror("internal error");
+ if(v->index && dict_contains(xvars, name)) {
+ v->init = 0;
+ v->index = i++;
+ if(v->is_inner_method) {
+ v->is_inner_method->is_a_slot = 1;
+ }
+ //v->type = 0;
+ dict_put(state->method->slots, name, v);
+ }
+ }
+ state->method->uses_slots = i;
+ dict_destroy(state->vars);state->vars = 0;
+ }
+ old_state();
+ return 0;
+ }
+
+ if(as3_pass==2) {
+ /*if(state->method->uses_parent_function){
+ syntaxerror("accessing variables of parent function from inner functions not supported yet");
+ }*/
+
+ abc_method_t*f = 0;
+
+ multiname_t*type2 = sig2mname(return_type);
+ int slot = 0;
+ if(state->method->inner) {
+ f = state->method->abc;
+ abc_method_init(f, global->file, type2, 1);
+ } else if(state->method->is_constructor) {
+ f = abc_class_getconstructor(state->cls->abc, type2);
+ } else if(!state->method->is_global) {
+ namespace_t mname_ns = modifiers2access(mod);
+ multiname_t mname = {QNAME, &mname_ns, 0, name};
+
+ if(mod->flags&FLAG_STATIC)
+ f = abc_class_staticmethod(state->cls->abc, type2, &mname);
+ else
+ f = abc_class_method(state->cls->abc, type2, &mname);
+ slot = f->trait->slot_id;
+ } else {
+ namespace_t mname_ns = {state->method->info->access, state->package};
+ multiname_t mname = {QNAME, &mname_ns, 0, name};
+
+ f = abc_method_new(global->file, type2, 1);
+ trait_t*t = trait_new_method(&global->init->traits, multiname_clone(&mname), f);
+ //abc_code_t*c = global->init->method->body->code;
+ }
+ //flash doesn't seem to allow us to access function slots
+ //state->method->info->slot = slot;
+
+ if(mod && mod->flags&FLAG_OVERRIDE) f->trait->attributes |= TRAIT_ATTR_OVERRIDE;
+ if(getset == KW_GET) f->trait->kind = TRAIT_GETTER;
+ if(getset == KW_SET) f->trait->kind = TRAIT_SETTER;
+ if(params->varargs) f->flags |= METHOD_NEED_REST;
+
+ char opt=0;
+ param_list_t*p=0;
+ for(p=params->list;p;p=p->next) {
+ if(params->varargs && !p->next) {
+ break; //varargs: omit last parameter in function signature
+ }
+ multiname_t*m = sig2mname(p->param->type);
+ list_append(f->parameters, m);
+ if(p->param->value) {
+ check_constant_against_type(p->param->type, p->param->value);
+ opt=1;list_append(f->optional_parameters, p->param->value);
+ } else if(opt) {
+ syntaxerror("non-optional parameter not allowed after optional parameters");
+ }
+ }
+ if(state->method->slots) {
+ DICT_ITERATE_ITEMS(state->method->slots, char*, name, variable_t*, v) {
+ if(v->index) {
+ multiname_t*mname = multiname_new(namespace_new(ACCESS_PACKAGE, ""), name);
+ multiname_t*type = sig2mname(v->type);
+ trait_t*t = trait_new_member(&f->body->traits, type, mname, 0);
+ t->slot_id = v->index;
+ }
+ }
+ }
+
+ check_code_for_break(body);
+
+ /* Seems this works now.
+ if(state->method->exceptions && state->method->uses_slots) {
+ as3_warning("try/catch and activation not supported yet within the same method");
+ }*/
+
+ if(f->body) {
+ f->body->code = body;
+ f->body->exceptions = state->method->exceptions;
+ } else { //interface
+ if(body)
+ syntaxerror("interface methods can't have a method body");
+ }
+
+ old_state();
+ return f;
+ }
+
+ return 0;
+}
+
+void breakjumpsto(code_t*c, char*name, code_t*jump)
+{
+ while(c) {
+ if(c->opcode == OPCODE___BREAK__) {
+ string_t*name2 = c->data[0];
+ if(!name2->len || !strncmp(name2->str, name, name2->len)) {
+ c->opcode = OPCODE_JUMP;
+ c->branch = jump;
+ }
+ }
+ c=c->prev;
+ }
+}
+void continuejumpsto(code_t*c, char*name, code_t*jump)
+{
+ while(c) {
+ if(c->opcode == OPCODE___CONTINUE__) {
+ string_t*name2 = c->data[0];
+ if(!name2->len || !strncmp(name2->str, name, name2->len)) {
+ c->opcode = OPCODE_JUMP;
+ c->branch = jump;
+ }
+ }
+ c = c->prev;
+ }
+}
+
+#define IS_NUMBER_OR_INT(a) (TYPE_IS_INT((a)) || TYPE_IS_UINT((a)) || TYPE_IS_NUMBER((a)))
+
+code_t*converttype(code_t*c, classinfo_t*from, classinfo_t*to)
+{
+ if(from==to)
+ return c;
+ if(!to) {
+ return abc_coerce_a(c);
+ }
+ MULTINAME(m, to);
+ if(!from) {
+ // cast an "any" type to a specific type. subject to
+ // runtime exceptions
+ return abc_coerce2(c, &m);
+ }
+
+ if((TYPE_IS_NUMBER(from) || TYPE_IS_UINT(from) || TYPE_IS_INT(from)) &&
+ (TYPE_IS_NUMBER(to) || TYPE_IS_UINT(to) || TYPE_IS_INT(to))) {
+ // allow conversion between number types
+ if(TYPE_IS_UINT(to))
+ return abc_convert_u(c);
+ else if(TYPE_IS_INT(to))
+ return abc_convert_i(c);
+ else if(TYPE_IS_NUMBER(to))
+ return abc_convert_d(c);
+ return abc_coerce2(c, &m);
+ }
+
+ if(TYPE_IS_BOOLEAN(to))
+ return abc_convert_b(c);
+ if(TYPE_IS_STRING(to))
+ return abc_convert_s(c);
+ if(TYPE_IS_OBJECT(to))
+ return abc_convert_o(c);
+
+ classinfo_t*supertype = from;
+ while(supertype) {
+ if(supertype == to) {
+ // target type is one of from's superclasses
+ return abc_coerce2(c, &m);
+ }
+ int t=0;
+ while(supertype->interfaces[t]) {
+ if(supertype->interfaces[t]==to) {
+ // target type is one of from's interfaces
+ return abc_coerce2(c, &m);
+ }
+ t++;
+ }
+ supertype = supertype->superclass;
+ }
+ if(TYPE_IS_FUNCTION(from) && TYPE_IS_FUNCTION(to))
+ return c;
+ if(TYPE_IS_CLASS(from) && TYPE_IS_CLASS(to))
+ return c;
+ if(TYPE_IS_NULL(from) && !IS_NUMBER_OR_INT(to))
+ return c;
+
+ as3_error("can't convert type %s%s%s to %s%s%s",
+ from->package, from->package[0]?".":"", from->name,
+ to->package, to->package[0]?".":"", to->name);
+
+ return c;
+}
+/* move to ast.c todo end */
+
+char is_pushundefined(code_t*c)
+{
+ return (c && !c->prev && !c->next && c->opcode == OPCODE_PUSHUNDEFINED);
+}
+
+static const char* get_package_from_name(const char*name)
+{
+ /* try explicit imports */
+ dictentry_t* e = dict_get_slot(state->imports, name);
+ while(e) {
+ if(!strcmp(e->key, name)) {
+ slotinfo_t*c = (slotinfo_t*)e->data;
+ if(c) return c->package;
+ }
+ e = e->next;
+ }
+ return 0;
+}
+static namespace_list_t*get_current_imports()
+{
+ namespace_list_t*searchlist = 0;
+
+ list_append(searchlist, namespace_new_package(state->package));
+
+ import_list_t*l = state->wildcard_imports;
+ while(l) {
+ namespace_t*ns = namespace_new_package(l->import->package);
+ list_append(searchlist, ns);
+ l = l->next;
+ }
+ list_append(searchlist, namespace_new_package(""));
+ list_append(searchlist, namespace_new_package(internal_filename_package));
+ return searchlist;
+}
+
+static slotinfo_t* find_class(const char*name)
+{
+ slotinfo_t*c=0;
+
+ c = registry_find(state->package, name);
+ if(c) return c;
+
+ /* try explicit imports */
+ dictentry_t* e = dict_get_slot(state->imports, name);
+ if(c) return c;
+ while(e) {
+ if(!strcmp(e->key, name)) {
+ c = (slotinfo_t*)e->data;
+ if(c) return c;
+ }
+ e = e->next;
+ }
+
+ /* try package.* imports */
+ import_list_t*l = state->wildcard_imports;
+ while(l) {
+ //printf("does package %s contain a class %s?\n", l->import->package, name);
+ c = registry_find(l->import->package, name);
+ if(c) return c;
+ l = l->next;
+ }
+
+ /* try global package */
+ c = registry_find("", name);
+ if(c) return c;
+
+ /* try local "filename" package */
+ c = registry_find(internal_filename_package, name);
+ if(c) return c;
+
+ return 0;
+}
+typedcode_t push_class(slotinfo_t*a)
+{
+ typedcode_t x;
+ x.c = 0;
+ x.t = 0;
+ if(a->access == ACCESS_PACKAGEINTERNAL &&
+ strcmp(a->package, state->package) &&
+ strcmp(a->package, internal_filename_package)
+ ) {
+ syntaxerror("Can't access internal %s %s in package '%s' from package '%s'",
+ infotypename(a), a->name, a->package, state->package);
+ }
+
+ if(a->kind != INFOTYPE_CLASS) {
+ MULTINAME(m, a);
+ x.c = abc_findpropstrict2(x.c, &m);
+ x.c = abc_getproperty2(x.c, &m);
+ if(a->kind == INFOTYPE_METHOD) {
+ methodinfo_t*f = (methodinfo_t*)a;
+ x.t = TYPE_FUNCTION(f);
+ } else {
+ varinfo_t*v = (varinfo_t*)a;
+ x.t = v->type;