+// ----------------------- resolving ----------------------------------
+slotinfo_t* registry_resolve(slotinfo_t*_s)
+{
+ if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
+ return _s;
+ unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
+
+ if(s->package)
+ return registry_find(s->package, s->name);
+
+ namespace_list_t*l = s->nsset;
+ while(l) {
+ slotinfo_t* n = registry_find(l->namespace->name, s->name);
+ if(n) return n;
+ l = l->next;
+ }
+ return 0;
+}
+
+static slotinfo_list_t*unresolved = 0;
+static void schedule_for_resolve(slotinfo_t*s)
+{
+ list_append(unresolved, s);
+}
+static void resolve_on_slot(slotinfo_t*_member)
+{
+ if(_member->kind == INFOTYPE_VAR) {
+ varinfo_t*member = (varinfo_t*)_member;
+ member->type = (classinfo_t*)registry_resolve((slotinfo_t*)member->type);
+ } else if(_member->kind == INFOTYPE_METHOD) {
+ methodinfo_t*member = (methodinfo_t*)_member;
+ member->return_type = (classinfo_t*)registry_resolve((slotinfo_t*)member->return_type);
+ classinfo_list_t*l = member->params;
+ while(l) {
+ l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
+ l = l->next;
+ }
+ } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
+}
+static void resolve_on_class(slotinfo_t*_cls)
+{
+ classinfo_t*cls = (classinfo_t*)_cls;
+ cls->superclass = (classinfo_t*)registry_resolve((slotinfo_t*)cls->superclass);
+
+ DICT_ITERATE_DATA(&cls->members,slotinfo_t*,m) {
+ resolve_on_slot(m);
+ }
+ DICT_ITERATE_DATA(&cls->static_members,slotinfo_t*,m2) {
+ resolve_on_slot(m2);
+ }
+
+ int t=0;
+ while(cls->interfaces[t]) {
+ cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
+ t++;
+ }
+}
+void registry_resolve_all()
+{
+ while(unresolved) {
+ slotinfo_t*_s = unresolved->slotinfo;
+ if(_s->kind == INFOTYPE_CLASS) {
+ resolve_on_class(_s);
+ } else if(_s->kind == INFOTYPE_METHOD || _s->kind == INFOTYPE_VAR) {
+ resolve_on_slot(_s);
+ } else {
+ fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
+ }
+ slotinfo_list_t*tofree = unresolved;
+ unresolved = unresolved->next;
+ free(tofree);
+ }
+}
+// ------------------------- constructors --------------------------------
+
+#define AVERAGE_NUMBER_OF_MEMBERS 8
+classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
+{
+ classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
+ c->interfaces[0] = 0;
+ c->kind = INFOTYPE_CLASS;
+ c->access = access;
+ c->package = package;
+ c->name = name;
+ dict_put(registry_classes, c, c);
+ dict_init2(&c->members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
+ dict_init2(&c->static_members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
+
+ schedule_for_resolve((slotinfo_t*)c);
+ return c;
+}
+methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
+{
+ NEW(methodinfo_t,m);
+ m->kind = INFOTYPE_METHOD;
+ m->access = access;
+ m->name = name;
+ m->package = ns;
+ m->parent = cls;
+ if(!is_static)
+ dict_put(&cls->members, m, m);
+ else
+ dict_put(&cls->static_members, m, m);
+ return m;
+}
+varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
+{
+ NEW(varinfo_t,m);
+ m->kind = INFOTYPE_VAR;
+ m->access = access;
+ m->name = name;
+ m->package = ns;
+ m->parent = cls;
+ if(!is_static)
+ dict_put(&cls->members, m, m);
+ else
+ dict_put(&cls->static_members, m, m);
+ return m;
+}
+methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
+{
+ NEW(methodinfo_t, m);
+ m->kind = INFOTYPE_METHOD;
+ m->flags = FLAG_STATIC;
+ m->access = access;
+ m->package = package;
+ m->name = name;
+ m->parent = 0;
+ dict_put(registry_classes, m, m);
+
+ schedule_for_resolve((slotinfo_t*)m);
+ return m;
+}
+varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
+{
+ NEW(varinfo_t, m);
+ m->kind = INFOTYPE_VAR;
+ m->flags = FLAG_STATIC;
+ m->access = access;
+ m->package = package;
+ m->name = name;
+ m->parent = 0;
+ dict_put(registry_classes, m, m);
+
+ schedule_for_resolve((slotinfo_t*)m);
+ return m;
+}
+
+// --------------- builtin classes (from builtin.c) ----------------------
+
+void registry_init()
+{
+ if(!registry_classes)
+ registry_classes = builtin_getclasses();
+}
+slotinfo_t* registry_find(const char*package, const char*name)
+{
+ assert(registry_classes);
+ slotinfo_t tmp;
+ tmp.package = package;
+ tmp.name = name;
+ slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
+ /*if(c)
+ printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
+ return c;
+}
+slotinfo_t* registry_safefind(const char*package, const char*name)
+{
+ slotinfo_t*c = registry_find(package, name);
+ if(!c) {
+ printf("%s.%s\n", package, name);
+ }
+ assert(c);
+ return c;
+}
+void registry_dump()
+{
+ int t;
+ for(t=0;t<registry_classes->hashsize;t++) {
+ dictentry_t*e = registry_classes->slots[t];
+ while(e) {
+ slotinfo_t*i = (slotinfo_t*)e->key;
+ printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
+ e = e->next;
+ }
+ }
+}