+
+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 memberinfo_t*registerfunction(enum yytokentype getset, int flags, char*name, params_t*params, classinfo_t*return_type, int slot)
+{
+ memberinfo_t*minfo = 0;
+ if(getset != KW_GET && getset != KW_SET) {
+ if(registry_findmember(state->cls->info, name)) {
+ syntaxerror("class already contains a member/method called '%s'", name);
+ }
+ minfo = memberinfo_register(state->cls->info, name, MEMBER_METHOD);
+ 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 {
+ int gs = getset==KW_GET?MEMBER_GET:MEMBER_SET;
+ classinfo_t*type=0;
+ if(getset == KW_GET)
+ type = return_type;
+ else if(params->list)
+ type = params->list->param->type;
+ if((minfo=registry_findmember(state->cls->info, name))) {
+ if(minfo->kind & ~(MEMBER_GET|MEMBER_SET))
+ syntaxerror("class already contains a member or method called '%s'", name);
+ if(minfo->kind & gs)
+ syntaxerror("getter/setter for '%s' already defined", name);
+ /* make a setter or getter into a getset */
+ minfo->kind |= gs;
+ if(!minfo->type)
+ minfo->type = type;
+ else
+ if(type && minfo->type != type)
+ syntaxerror("different type in getter and setter");
+ } else {
+ minfo = memberinfo_register(state->cls->info, name, gs);
+ minfo->type = type;
+ }
+ /* can't assign a slot as getter and setter might have different slots */
+ //minfo->slot = slot;
+ }
+ if(flags&FLAG_STATIC) minfo->flags |= FLAG_STATIC;
+ if(flags&FLAG_PUBLIC) minfo->flags |= FLAG_PUBLIC;
+ if(flags&FLAG_PRIVATE) minfo->flags |= FLAG_PRIVATE;
+ if(flags&FLAG_PROTECTED) minfo->flags |= FLAG_PROTECTED;
+ if(flags&FLAG_INTERNAL) minfo->flags |= FLAG_INTERNAL;
+ return minfo;
+}
+
+static int flags2access(int flags)
+{
+ int access = 0;
+ if(flags&FLAG_PUBLIC) {
+ if(access&(FLAG_PRIVATE|FLAG_PROTECTED|FLAG_INTERNAL)) syntaxerror("invalid combination of access levels");
+ access = ACCESS_PACKAGE;
+ } else if(flags&FLAG_PRIVATE) {
+ if(access&(FLAG_PUBLIC|FLAG_PROTECTED|FLAG_INTERNAL)) syntaxerror("invalid combination of access levels");
+ access = ACCESS_PRIVATE;
+ } else if(flags&FLAG_PROTECTED) {
+ if(access&(FLAG_PUBLIC|FLAG_PRIVATE|FLAG_INTERNAL)) syntaxerror("invalid combination of access levels");
+ access = ACCESS_PROTECTED;
+ } else {
+ access = ACCESS_PACKAGEINTERNAL;
+ }
+ return access;
+}
+
+static void startfunction(token_t*ns, int flags, enum yytokentype getset, char*name,
+ params_t*params, classinfo_t*return_type)
+{
+ if(state->method) {
+ syntaxerror("not able to start another method scope");
+ }
+ new_state();
+ state->method = rfx_calloc(sizeof(methodstate_t));
+ state->method->initcode = 0;
+ state->method->is_constructor = !strcmp(state->cls->info->name,name);
+ state->method->has_super = 0;
+
+ global->variable_count = 0;
+
+ /* state->vars is initialized by state_new */
+ if(new_variable((flags&FLAG_STATIC)?"class":"this", state->cls->info)!=0) syntaxerror("Internal error");
+ param_list_t*p=0;
+ for(p=params->list;p;p=p->next) {
+ new_variable(p->param->name, p->param->type);
+ }
+ if(state->method->is_constructor)
+ name = "__as3_constructor__";
+ state->method->info = registerfunction(getset, flags, name, params, return_type, 0);
+}
+
+static void endfunction(token_t*ns, int flags, enum yytokentype getset, char*name,
+ params_t*params, classinfo_t*return_type, code_t*body)
+{
+ namespace_t mname_ns = {flags2access(flags), ""};
+ multiname_t mname = {QNAME, &mname_ns, 0, name};
+
+ abc_method_t*f = 0;
+
+ multiname_t*type2 = sig2mname(return_type);
+ int slot = 0;
+ if(state->method->is_constructor) {
+ f = abc_class_constructor(state->cls->abc, type2);
+ } else {
+ if(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;
+ }
+ //flash doesn't seem to allow us to access function slots
+ //state->method->info->slot = slot;
+
+ 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");
+ }
+ }
+ f->body->code = body;
+
+ old_state();
+}
+
+
+