+ syntaxerror("public classes only allowed inside a package");
+ }
+
+ if(as3_pass==1) {
+ state->cls = rfx_calloc(sizeof(classstate_t));
+ state->cls->init = rfx_calloc(sizeof(methodstate_t));
+ state->cls->static_init = rfx_calloc(sizeof(methodstate_t));
+ state->cls->static_init->is_static=FLAG_STATIC;
+ state->cls->static_init->variable_count=1;
+ /* 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);
+
+ state->method = state->cls->init;
+ parserassert(state->cls && state->cls->info);
+
+ function_initvars(state->cls->init, 0, 0, 0, 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);
+ }
+
+ NEW(parsedclass_t,p);
+ p->cls = state->cls->info;
+ p->abc = state->cls->abc;
+ list_append(global->classes, p);
+
+ /* 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)
+ 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);
+ 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);
+ 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;