+
+ if(as3_pass==1) {
+ state->cls = rfx_calloc(sizeof(classstate_t));
+ state->cls->init = methodstate_new();
+ state->cls->static_init = methodstate_new();
+ state->cls->static_init->is_static=FLAG_STATIC;
+ /* notice: we make no effort to initialize the top variable (local0) here,
+ even though it has special meaning. We just rely on the fact
+ that pass 1 won't do anything with variables */
+
+ dict_put(global->token2info, (void*)(ptroff_t)as3_tokencount, state->cls);
+
+ /* set current method to constructor- all code within the class-level (except
+ static variable initializations) will be executed during construction time */
+ state->method = state->cls->init;
+
+ if(registry_find(package, classname)) {
+ syntaxerror("Package \"%s\" already contains a class called \"%s\"", package, classname);
+ }
+ /* build info struct */
+ int num_interfaces = (list_length(implements));
+ state->cls->info = classinfo_register(access, package, classname, num_interfaces);
+ state->cls->info->flags |= mod->flags & (FLAG_DYNAMIC|FLAG_INTERFACE|FLAG_FINAL);
+ state->cls->info->superclass = extends;
+
+ int pos = 0;
+ classinfo_list_t*l = implements;
+ for(l=implements;l;l=l->next) {
+ state->cls->info->interfaces[pos++] = l->classinfo;
+ }
+ }
+
+ if(as3_pass == 2) {
+ state->cls = dict_lookup(global->token2info, (void*)(ptroff_t)as3_tokencount);
+
+ parserassert(state->cls && state->cls->info);
+
+ state->method = state->cls->static_init;
+
+ function_initvars(state->cls->init, 0, 0, 0, 1);
+ state->cls->static_init->variable_count=1;
+ function_initvars(state->cls->static_init, 0, 0, 0, 0);
+
+ if(extends && (extends->flags & FLAG_FINAL))
+ syntaxerror("Can't extend final class '%s'", extends->name);
+
+ int pos = 0;
+ while(state->cls->info->interfaces[pos]) {
+ if(!(state->cls->info->interfaces[pos]->flags & FLAG_INTERFACE))
+ syntaxerror("'%s' is not an interface",
+ state->cls->info->interfaces[pos]->name);
+ pos++;
+ }
+
+ /* generate the abc code for this class */
+ MULTINAME(classname2,state->cls->info);
+ multiname_t*extends2 = sig2mname(extends);
+
+ /* don't add the class to the class index just yet- that will be done later
+ by initscript */
+ state->cls->abc = abc_class_new(0, &classname2, extends2);
+ state->cls->abc->file = global->file;
+
+ multiname_destroy(extends2);
+ if(state->cls->info->flags&FLAG_FINAL) abc_class_final(state->cls->abc);
+ if(!(state->cls->info->flags&FLAG_DYNAMIC)) abc_class_sealed(state->cls->abc);
+ if(state->cls->info->flags&FLAG_INTERFACE) {
+ abc_class_interface(state->cls->abc);
+ }
+
+ for(mlist=implements;mlist;mlist=mlist->next) {
+ MULTINAME(m, mlist->classinfo);
+ abc_class_add_interface(state->cls->abc, &m);
+ }
+
+ state->cls->dependencies = parsedclass_new(state->cls->info, state->cls->abc);
+ list_append(global->classes, state->cls->dependencies);
+
+ /* flash.display.MovieClip handling */
+ if(!as3_globalclass && (mod->flags&FLAG_PUBLIC) && slotinfo_equals((slotinfo_t*)registry_getMovieClip(),(slotinfo_t*)extends)) {
+ if(state->package && state->package[0]) {
+ as3_globalclass = concat3(state->package, ".", classname);
+ } else {
+ as3_globalclass = strdup(classname);
+ }
+ }
+ }
+}
+
+static void endclass()
+{
+ if(as3_pass == 2) {
+ if(!state->cls->has_constructor && !(state->cls->info->flags&FLAG_INTERFACE)) {
+ code_t*c = 0;
+ c = abc_getlocal_0(c);
+ c = abc_constructsuper(c, 0);
+ state->cls->init->header = code_append(state->cls->init->header, c);
+ state->cls->has_constructor=1;
+ }
+ if(state->cls->init) {
+ if(state->cls->info->flags&FLAG_INTERFACE) {
+ if(state->cls->init->header)
+ syntaxerror("interface can not have class-level code");
+ } else {
+ abc_method_t*m = abc_class_getconstructor(state->cls->abc, 0);
+ code_t*c = method_header(state->cls->init);
+ m->body->code = wrap_function(c, 0, m->body->code);
+ }
+ }
+ if(state->cls->static_init) {
+ abc_method_t*m = abc_class_getstaticconstructor(state->cls->abc, 0);
+ code_t*c = method_header(state->cls->static_init);
+ m->body->code = wrap_function(c, 0, m->body->code);
+ }
+
+ trait_list_t*trait = state->cls->abc->traits;
+ /* switch all protected members to the protected ns of this class */
+ while(trait) {
+ trait_t*t = trait->trait;
+ if(t->name->ns->access == ACCESS_PROTECTED) {
+ if(!state->cls->abc->protectedNS) {
+ char*n = concat3(state->cls->info->package, ":", state->cls->info->name);
+ state->cls->abc->protectedNS = namespace_new_protected(n);
+ state->cls->abc->flags |= CLASS_PROTECTED_NS;
+ }
+ t->name->ns->name = strdup(state->cls->abc->protectedNS->name);
+ }
+ trait = trait->next;
+ }
+ }
+
+ old_state();
+}
+
+void check_code_for_break(code_t*c)
+{
+ while(c) {
+ if(c->opcode == OPCODE___BREAK__) {
+ char*name = string_cstr(c->data[0]);
+ syntaxerror("Unresolved \"break %s\"", name);
+ }
+ if(c->opcode == OPCODE___CONTINUE__) {
+ char*name = string_cstr(c->data[0]);
+ syntaxerror("Unresolved \"continue %s\"", name);
+ }
+ if(c->opcode == OPCODE___RETHROW__) {
+ syntaxerror("Unresolved \"rethrow\"");
+ }
+ if(c->opcode == OPCODE___FALLTHROUGH__) {
+ syntaxerror("Unresolved \"fallthrough\"");
+ }
+ if(c->opcode == OPCODE___PUSHPACKAGE__) {
+ char*name = string_cstr(c->data[0]);
+ syntaxerror("Can't reference a package (%s) as such", name);
+ }
+ c=c->prev;
+ }
+}
+
+static void check_constant_against_type(classinfo_t*t, constant_t*c)
+{
+#define xassert(b) if(!(b)) syntaxerror("Invalid default value %s for type '%s'", constant_tostring(c), t->name)
+ if(TYPE_IS_NUMBER(t)) {
+ xassert(c->type == CONSTANT_FLOAT
+ || c->type == CONSTANT_INT
+ || c->type == CONSTANT_UINT);
+ } else if(TYPE_IS_UINT(t)) {
+ xassert(c->type == CONSTANT_UINT ||
+ (c->type == CONSTANT_INT && c->i>=0));
+ } else if(TYPE_IS_INT(t)) {
+ xassert(c->type == CONSTANT_INT);
+ } else if(TYPE_IS_BOOLEAN(t)) {
+ xassert(c->type == CONSTANT_TRUE
+ || c->type == CONSTANT_FALSE);
+ }
+}
+
+static void check_override(memberinfo_t*m, int flags)
+{
+ if(!m)
+ return;
+ if(m->parent == state->cls->info && !((flags^m->flags)&FLAG_STATIC))
+ syntaxerror("class '%s' already contains a method/slot '%s'", m->parent->name, m->name);
+ if(!m->parent)
+ syntaxerror("internal error: overriding method %s, which doesn't have parent", m->name);
+ if(m->access==ACCESS_PRIVATE)
+ return;
+ if(m->flags & FLAG_FINAL)
+ syntaxerror("can't override final member %s", m->name);
+
+ /* allow this. it's no issue.
+ if((m->flags & FLAG_STATIC) && !(flags&FLAG_STATIC))
+ syntaxerror("can't override static member %s", m->name);*/
+
+ if(!(m->flags & FLAG_STATIC) && (flags&FLAG_STATIC))
+ syntaxerror("can't override non-static member %s with static declaration", m->name);
+
+ if(!(flags&FLAG_OVERRIDE) && !(flags&FLAG_STATIC) && !(m->flags&FLAG_STATIC)) {
+ if(m->parent && !(m->parent->flags&FLAG_INTERFACE)) {
+ if(m->kind == INFOTYPE_METHOD)
+ syntaxerror("can't override without explicit 'override' declaration");
+ else
+ syntaxerror("can't override '%s'", m->name);
+ }
+ }
+}
+
+static methodinfo_t*registerfunction(enum yytokentype getset, modifiers_t*mod, char*name, params_t*params, classinfo_t*return_type, int slot)
+{
+ methodinfo_t*minfo = 0;
+ namespace_t ns = modifiers2access(mod);
+ if(!state->cls) {
+ //package method
+ minfo = methodinfo_register_global(ns.access, state->package, name);
+ minfo->return_type = return_type;
+ } else if(getset != KW_GET && getset != KW_SET) {
+ //class method
+ memberinfo_t* m = registry_findmember(state->cls->info, ns.name, name, 0, mod->flags&FLAG_STATIC);
+ if(m) {
+ syntaxerror("class already contains a %s '%s'", infotypename((slotinfo_t*)m), m->name);
+ }
+ minfo = methodinfo_register_onclass(state->cls->info, ns.access, ns.name, name, mod->flags&FLAG_STATIC);
+ minfo->return_type = return_type;
+ // getslot on a member slot only returns "undefined", so no need
+ // to actually store these
+ //state->minfo->slot = state->method->abc->method->trait->slot_id;
+ } else {
+ //class getter/setter
+ int gs = getset==KW_GET?SUBTYPE_GET:SUBTYPE_SET;
+ classinfo_t*type=0;
+ if(getset == KW_GET) {
+ type = return_type;
+ } else if(params->list && params->list->param && !params->list->next) {
+ type = params->list->param->type;
+ } else
+ syntaxerror("setter function needs to take exactly one argument");
+ // not sure wether to look into superclasses here, too
+ minfo = (methodinfo_t*)registry_findmember(state->cls->info, ns.name, name, 1, mod->flags&FLAG_STATIC);
+ if(minfo) {
+ if(minfo->kind!=INFOTYPE_VAR)
+ syntaxerror("class already contains a method called '%s'", name);
+ if(!(minfo->subtype & (SUBTYPE_GETSET)))
+ syntaxerror("class already contains a field called '%s'", name);
+ if(minfo->subtype & gs)
+ syntaxerror("getter/setter for '%s' already defined", name);
+ /* make a setter or getter into a getset */
+ minfo->subtype |= gs;
+
+ /*
+ FIXME: this check needs to be done in pass 2
+
+ if((!minfo->return_type != !type) ||
+ (minfo->return_type && type &&
+ !strcmp(minfo->return_type->name, type->name))) {
+ syntaxerror("different type in getter and setter: %s and %s",
+ minfo->return_type?minfo->return_type->name:"*",
+ type?type->name:"*");
+ }*/
+ } else {
+ minfo = methodinfo_register_onclass(state->cls->info, ns.access, ns.name, name, mod->flags&FLAG_STATIC);
+ minfo->kind = INFOTYPE_VAR; //hack
+ minfo->subtype = gs;
+ minfo->return_type = type;
+ }
+
+ /* can't assign a slot as getter and setter might have different slots */
+ //minfo->slot = slot;
+ }
+ if(mod->flags&FLAG_FINAL) minfo->flags |= FLAG_FINAL;
+ if(mod->flags&FLAG_STATIC) minfo->flags |= FLAG_STATIC;
+ if(mod->flags&FLAG_OVERRIDE) minfo->flags |= FLAG_OVERRIDE;
+
+ return minfo;
+}
+
+static void innerfunction(char*name, params_t*params, classinfo_t*return_type)
+{
+ //parserassert(state->method && state->method->info);
+
+ methodstate_t*parent_method = state->method;
+ variable_t*v = 0;
+
+ if(as3_pass==1) {
+ return_type = 0; // not valid in pass 1
+ if(name) {
+ v = new_variable2(parent_method, name, 0, 0, 0);
+ }
+ }
+
+ new_state();
+ state->new_vars = 1;
+
+ if(as3_pass == 1) {
+ state->method = methodstate_new();
+ state->method->inner = 1;
+ state->method->is_static = parent_method->is_static;
+ state->method->variable_count = 0;
+ state->method->abc = rfx_calloc(sizeof(abc_method_t));
+ if(v) {
+ v->is_inner_method = state->method;
+ }
+
+ NEW(methodinfo_t,minfo);
+ minfo->kind = INFOTYPE_METHOD;
+ minfo->access = ACCESS_PACKAGEINTERNAL;
+ minfo->name = name;
+ state->method->info = minfo;
+
+ if(parent_method)
+ list_append(parent_method->innerfunctions, state->method);
+
+ dict_put(global->token2info, (void*)(ptroff_t)as3_tokencount, state->method);
+
+ function_initvars(state->method, 1, params, 0, 1);
+ }
+
+ if(as3_pass == 2) {
+ state->method = dict_lookup(global->token2info, (void*)(ptroff_t)as3_tokencount);
+ state->method->variable_count = 0;
+ parserassert(state->method);
+
+ state->method->info->return_type = return_type;
+ function_initvars(state->method, 1, params, 0, 1);
+ }
+}
+
+static void startfunction(modifiers_t*mod, enum yytokentype getset, char*name,
+ params_t*params, classinfo_t*return_type)
+{
+ if(state->method && state->method->info) {
+ syntaxerror("not able to start another method scope");
+ }
+ new_state();
+ state->new_vars = 1;
+
+ if(as3_pass == 1) {
+ state->method = methodstate_new();
+ state->method->has_super = 0;
+ state->method->is_static = mod->flags&FLAG_STATIC;
+
+ if(state->cls) {
+ state->method->is_constructor = !strcmp(state->cls->info->name,name);
+ } else {
+ 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, 1, 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, mod->flags&FLAG_STATIC);
+ check_override(m, mod->flags);
+ }
+
+ if(state->cls) {
+ state->cls->has_constructor |= state->method->is_constructor;
+ }
+
+ function_initvars(state->method, 1, params, mod->flags, 1);
+ }
+}
+
+static void insert_unresolved(methodstate_t*m, dict_t*xvars, dict_t*allvars)
+{
+ parserassert(m->inner);
+ if(m->unresolved_variables) {
+ dict_t*d = m->unresolved_variables;
+ int t;
+ DICT_ITERATE_KEY(d, char*, id) {
+ /* check parent method's variables */
+ variable_t*v;
+ if(dict_contains(allvars, id)) {
+ m->uses_parent_function = 1;
+ state->method->uses_slots = 1;
+ dict_put(xvars, id, 0);
+ }
+ }
+ }
+ methodstate_list_t*ml = m->innerfunctions;
+ while(ml) {
+ insert_unresolved(ml->methodstate, xvars, allvars);
+ ml = ml->next;
+ }
+}
+
+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) {
+ dict_t*xvars = dict_new();
+
+ if(state->method->unresolved_variables) {
+ DICT_ITERATE_KEY(state->method->unresolved_variables, char*, vname) {
+ if(!state->method->no_variable_scoping && dict_contains(state->method->allvars, vname)) {
+ variable_t*v = dict_lookup(state->method->allvars, vname);
+ if(!v->is_inner_method) {
+ state->method->no_variable_scoping = 1;
+ as3_warning("function %s uses forward or outer block variable references (%s): switching into compatibility mode", name, vname);
+ }
+ }
+ }
+ }
+
+ methodstate_list_t*ml = state->method->innerfunctions;
+ while(ml) {
+ insert_unresolved(ml->methodstate, xvars, state->method->allvars);
+ ml = ml->next;
+ }