added better static support for as3compile
[swftools.git] / lib / as3 / registry.c
1 /* registry.c
2
3    Routines for compiling Flash2 AVM2 ABC Actionscript
4
5    Extension module for the rfxswf library.
6    Part of the swftools package.
7
8    Copyright (c) 2008 Matthias Kramm <kramm@quiss.org>
9  
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 2 of the License, or
13    (at your option) any later version.
14
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19
20    You should have received a copy of the GNU General Public License
21    along with this program; if not, write to the Free Software
22    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
23
24 #include <assert.h>
25 #include "pool.h"
26 #include "registry.h"
27 #include "builtin.h"
28
29 dict_t*registry_classes=0;
30
31 // ----------------------- class signature ------------------------------
32
33 char slotinfo_equals(slotinfo_t*c1, slotinfo_t*c2)
34 {
35     if(!!c1 != !!c2)
36         return 0;
37     /* notice: access right is *not* respected */
38     if(!strcmp(c1->name, c2->name) &&
39        !strcmp(c1->package, c2->package)) {
40         return 1;
41     }
42     return 0;
43 }
44 static unsigned int slotinfo_hash(slotinfo_t*c)
45 {
46     unsigned int hash = 0;
47     hash = crc32_add_string(hash, c->package);
48     hash = crc32_add_string(hash, c->name);
49     return hash;
50 }
51 static unsigned int memberinfo_hash(slotinfo_t*c)
52 {
53     unsigned int hash = 0;
54     hash = crc32_add_string(hash, c->name);
55     return hash;
56 }
57
58 static void* dummy_clone(void*other) {return other;}
59 static void dummy_destroy(slotinfo_t*c) {}
60
61 type_t slotinfo_type = {
62     hash: (hash_func)slotinfo_hash,
63     equals: (equals_func)slotinfo_equals,
64     dup: (dup_func)dummy_clone, // all signatures are static
65     free: (free_func)dummy_destroy,
66 };
67 type_t memberinfo_type = {
68     hash: (hash_func)memberinfo_hash,
69     equals: (equals_func)slotinfo_equals,
70     dup: (dup_func)dummy_clone, // all signatures are static
71     free: (free_func)dummy_destroy,
72 };
73
74 // ----------------------- resolving ----------------------------------
75 slotinfo_t* registry_resolve(slotinfo_t*_s)
76 {
77     if(!_s || _s->kind != INFOTYPE_UNRESOLVED)
78         return _s;
79     unresolvedinfo_t*s = (unresolvedinfo_t*)_s;
80
81     if(s->package)
82         return registry_find(s->package, s->name);
83
84     namespace_list_t*l = s->nsset;
85     while(l) {
86         slotinfo_t* n = registry_find(l->namespace->name, s->name);
87         if(n) return n;
88         l = l->next;
89     }
90     return 0;
91 }
92
93 static slotinfo_list_t*unresolved = 0;
94 static void schedule_for_resolve(slotinfo_t*s)
95 {
96     list_append(unresolved, s);
97 }
98 static void resolve_on_slot(slotinfo_t*_member)
99 {
100     if(_member->kind == INFOTYPE_VAR) {
101         varinfo_t*member = (varinfo_t*)_member;
102         member->type = (classinfo_t*)registry_resolve((slotinfo_t*)member->type);
103     } else if(_member->kind == INFOTYPE_METHOD) {
104         methodinfo_t*member = (methodinfo_t*)_member;
105         member->return_type = (classinfo_t*)registry_resolve((slotinfo_t*)member->return_type);
106         classinfo_list_t*l = member->params;
107         while(l) {
108             l->classinfo = (classinfo_t*)registry_resolve((slotinfo_t*)l->classinfo);
109             l = l->next;
110         }
111     } else fprintf(stderr, "Internal Error: bad slot %s", _member->name);
112 }
113 static void resolve_on_class(slotinfo_t*_cls)
114 {
115     classinfo_t*cls = (classinfo_t*)_cls;
116     cls->superclass = (classinfo_t*)registry_resolve((slotinfo_t*)cls->superclass);
117         
118     DICT_ITERATE_DATA(&cls->members,slotinfo_t*,m) {
119         resolve_on_slot(m);
120     }
121     DICT_ITERATE_DATA(&cls->static_members,slotinfo_t*,m2) {
122         resolve_on_slot(m2);
123     }
124
125     int t=0;
126     while(cls->interfaces[t]) {
127         cls->interfaces[t] = (classinfo_t*)registry_resolve((slotinfo_t*)cls->interfaces[t]);
128         t++;
129     }
130 }
131 void registry_resolve_all()
132 {
133     while(unresolved) {
134         slotinfo_t*_s = unresolved->slotinfo;
135         if(_s->kind == INFOTYPE_CLASS) {
136             resolve_on_class(_s);
137         } else if(_s->kind == INFOTYPE_METHOD || _s->kind == INFOTYPE_VAR) {
138             resolve_on_slot(_s);
139         } else {
140             fprintf(stderr, "Internal Error: object %s.%s has bad type\n", _s->package, _s->name);
141         }
142         slotinfo_list_t*tofree = unresolved;
143         unresolved = unresolved->next;
144         free(tofree);
145     }
146 }
147 // ------------------------- constructors --------------------------------
148
149 #define AVERAGE_NUMBER_OF_MEMBERS 8
150 classinfo_t* classinfo_register(int access, const char*package, const char*name, int num_interfaces)
151 {
152     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+(sizeof(classinfo_t*)*(num_interfaces+1)));
153     c->interfaces[0] = 0;
154     c->kind = INFOTYPE_CLASS;
155     c->access = access;
156     c->package = package;
157     c->name = name;
158     dict_put(registry_classes, c, c);
159     dict_init2(&c->members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
160     dict_init2(&c->static_members, &memberinfo_type, AVERAGE_NUMBER_OF_MEMBERS);
161
162     schedule_for_resolve((slotinfo_t*)c);
163     return c;
164 }
165 methodinfo_t* methodinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
166 {
167     NEW(methodinfo_t,m);
168     m->kind = INFOTYPE_METHOD;
169     m->access = access;
170     m->name = name;
171     m->package = ns;
172     m->parent = cls;
173     if(!is_static) 
174         dict_put(&cls->members, m, m);
175     else
176         dict_put(&cls->static_members, m, m);
177     return m;
178 }
179 varinfo_t* varinfo_register_onclass(classinfo_t*cls, U8 access, const char*ns, const char*name, char is_static)
180 {
181     NEW(varinfo_t,m);
182     m->kind = INFOTYPE_VAR;
183     m->access = access;
184     m->name = name;
185     m->package = ns;
186     m->parent = cls;
187     if(!is_static) 
188         dict_put(&cls->members, m, m);
189     else
190         dict_put(&cls->static_members, m, m);
191     return m;
192 }
193 methodinfo_t* methodinfo_register_global(U8 access, const char*package, const char*name)
194 {
195     NEW(methodinfo_t, m);
196     m->kind = INFOTYPE_METHOD;
197     m->flags = FLAG_STATIC;
198     m->access = access;
199     m->package = package;
200     m->name = name;
201     m->parent = 0;
202     dict_put(registry_classes, m, m);
203     
204     schedule_for_resolve((slotinfo_t*)m);
205     return m;
206 }
207 varinfo_t* varinfo_register_global(U8 access, const char*package, const char*name)
208 {
209     NEW(varinfo_t, m);
210     m->kind = INFOTYPE_VAR;
211     m->flags = FLAG_STATIC;
212     m->access = access;
213     m->package = package;
214     m->name = name;
215     m->parent = 0;
216     dict_put(registry_classes, m, m);
217     
218     schedule_for_resolve((slotinfo_t*)m);
219     return m;
220 }
221
222 // --------------- builtin classes (from builtin.c) ----------------------
223
224 void registry_init()
225 {
226     if(!registry_classes)
227         registry_classes = builtin_getclasses();
228 }
229 slotinfo_t* registry_find(const char*package, const char*name)
230 {
231     assert(registry_classes);
232     slotinfo_t tmp;
233     tmp.package = package;
234     tmp.name = name;
235     slotinfo_t* c = (slotinfo_t*)dict_lookup(registry_classes, &tmp);
236     /*if(c)
237         printf("%s.%s->%08x (%s.%s)\n", package, name, c, c->package, c->name);*/
238     return c;
239 }
240 slotinfo_t* registry_safefind(const char*package, const char*name)
241 {
242     slotinfo_t*c = registry_find(package, name);
243     if(!c) {
244         printf("%s.%s\n", package, name);
245     }
246     assert(c);
247     return c;
248 }
249 void registry_dump()
250 {
251     int t;
252     for(t=0;t<registry_classes->hashsize;t++) {
253         dictentry_t*e = registry_classes->slots[t];
254         while(e) {
255             slotinfo_t*i = (slotinfo_t*)e->key;
256             printf("[%s] %s.%s\n", access2str(i->access), i->package, i->name);
257             e = e->next;
258         }
259     }
260 }
261
262 memberinfo_t* registry_findmember(classinfo_t*cls, const char*ns, const char*name, char recursive, char is_static)
263 {
264     memberinfo_t tmp;
265     tmp.name = name;
266     tmp.package = ns?ns:"";
267
268     if(!recursive) {
269         if(!is_static) 
270             return (memberinfo_t*)dict_lookup(&cls->members, &tmp);
271         else
272             return (memberinfo_t*)dict_lookup(&cls->static_members, &tmp);
273     }
274     /* look at classes directly extended by this class */
275     slotinfo_t*m = 0;
276     classinfo_t*s = cls;
277
278     if(recursive>1) // check *only* superclasses
279         s = s->superclass;
280
281     while(s) {
282         if(s->kind == INFOTYPE_UNRESOLVED)
283             break;
284
285         if(!is_static) {
286             m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
287             if(m) return (memberinfo_t*)m;
288         }
289         m = (slotinfo_t*)dict_lookup(&s->static_members, &tmp);
290         if(m) return (memberinfo_t*)m;
291
292         s = s->superclass;
293     }
294     /* look at interfaces, and parent interfaces */
295     int t=0;
296     while(cls->interfaces[t]) {
297         classinfo_t*s = cls->interfaces[t];
298         if(s->kind != INFOTYPE_UNRESOLVED) {
299             while(s) {
300                 if(!is_static) {
301                     m = (slotinfo_t*)dict_lookup(&s->members, &tmp);
302                     if(m) return (memberinfo_t*)m;
303                 }
304                 m = (slotinfo_t*)dict_lookup(&s->static_members, &tmp);
305                 if(m) return (memberinfo_t*)m;
306
307                 s = s->superclass;
308             }
309         }
310         t++;
311     }
312     return 0;
313 }
314
315 memberinfo_t* registry_findmember_nsset(classinfo_t*cls, namespace_list_t*ns, const char*name, char superclasses, char is_static)
316 {
317     memberinfo_t*m = 0;
318     while(ns) {
319         m = registry_findmember(cls, ns->namespace->name, name, superclasses, is_static);
320         if(m) return m;
321         ns = ns->next;
322     }
323     m = registry_findmember(cls, "", name, superclasses, is_static);
324     if(m) return m;
325     /* TODO: it maybe would be faster to just store the builtin namespace as "" in
326              builtins.c (update: some members (e.g. XML.length) are present both for
327             "" and "http:...builtin") */
328     m = registry_findmember(cls, "http://adobe.com/AS3/2006/builtin", name, superclasses, is_static);
329     if(m) return m;
330     return 0;
331 }
332
333
334 void registry_fill_multiname(multiname_t*m, namespace_t*n, slotinfo_t*c)
335 {
336     m->type = QNAME;
337     m->ns = n;
338     m->ns->access = c->access;
339     m->ns->name = (char*)c->package;
340     m->name = c->name;
341     m->namespace_set = 0;
342 }
343 multiname_t* classinfo_to_multiname(slotinfo_t*cls)
344 {
345     if(!cls)
346         return 0;
347     multiname_t*m=0;
348     namespace_t ns = {cls->access, (char*)cls->package};
349     return multiname_new(&ns,cls->name);
350 }
351
352 // ----------------------- memberinfo methods ------------------------------
353
354 /* hacky code to wrap a variable or function into a "type"
355    object, but keep a pointer to the "value" */
356 static dict_t* functionobjects = 0;
357 classinfo_t* slotinfo_asclass(slotinfo_t*f) {
358     if(!functionobjects) {
359         functionobjects = dict_new2(&ptr_type);
360     } else {
361         classinfo_t*c = dict_lookup(functionobjects, f);
362         if(c)
363             return c;
364     }
365
366     classinfo_t*c = rfx_calloc(sizeof(classinfo_t)+sizeof(classinfo_t*));
367     c->access = ACCESS_PUBLIC;
368     c->package = "";
369     if(f->kind == INFOTYPE_METHOD) {
370         c->name = "Function";
371         c->superclass = registry_getobjectclass();
372     } else if(f->kind == INFOTYPE_CLASS) {
373         c->name = "Class";
374         c->superclass = registry_getobjectclass();
375     } else if(f->kind == INFOTYPE_VAR) {
376         c->name = "Object";
377     } else {
378         c->name = "undefined";
379     }
380     
381     dict_init2(&c->members, &memberinfo_type, 1);
382     dict_init2(&c->static_members, &memberinfo_type, 1);
383     c->data = f;
384     dict_put(functionobjects, f, c);
385     return c;
386 }
387
388 classinfo_t* slotinfo_gettype(slotinfo_t*f)
389 {
390     if(f) {
391        if(f->kind == INFOTYPE_METHOD) {
392            return slotinfo_asclass(f);
393        } else if(f->kind == INFOTYPE_VAR) {
394            varinfo_t*v = (varinfo_t*)f;
395            return v->type;
396        } else 
397            return 0;
398     } else {
399        return TYPE_ANY;
400     }
401 }
402
403 // ----------------------- package handling ---------------------------
404 char registry_ispackage(const char*package)
405 {
406     /* crude approximation of "the real thing", but sufficient for now */
407     return !strncmp(package, "flash", 5);
408 }
409 // ----------------------- builtin types ------------------------------
410
411 char registry_isfunctionclass(classinfo_t*c) {
412     return (c && c->package && c->name && 
413             !strcmp(c->package, "") && !strcmp(c->name, "Function"));
414 }
415 char registry_isclassclass(classinfo_t*c) {
416     return (c && c->package && c->name && 
417             !strcmp(c->package, "") && !strcmp(c->name, "Class"));
418 }
419
420 classinfo_t* registry_getobjectclass() {
421     static classinfo_t*c = 0;
422     if(!c) c = (classinfo_t*)registry_safefind("", "Object");
423     return c;
424 }
425 classinfo_t* registry_getstringclass() {
426     static classinfo_t*c = 0;
427     if(!c) c = (classinfo_t*)registry_safefind("", "String");
428     return c;
429 }
430 classinfo_t* registry_getarrayclass() {
431     static classinfo_t*c = 0;
432     if(!c) c = (classinfo_t*)registry_safefind("", "Array");
433     return c;
434 }
435 classinfo_t* registry_getintclass() {
436     static classinfo_t*c = 0;
437     if(!c) c = (classinfo_t*)registry_safefind("", "int");
438     return c;
439 }
440 classinfo_t* registry_getuintclass() {
441     static classinfo_t*c = 0;
442     if(!c) c = (classinfo_t*)registry_safefind("", "uint");
443     return c;
444 }
445 classinfo_t* registry_getbooleanclass() {
446     static classinfo_t*c = 0;
447     if(!c) c = (classinfo_t*)registry_safefind("", "Boolean");
448     return c;
449 }
450 classinfo_t* registry_getnumberclass() {
451     static classinfo_t*c = 0;
452     if(!c) c = (classinfo_t*)registry_safefind("", "Number");
453     return c;
454 }
455 classinfo_t* registry_getregexpclass() {
456     static classinfo_t*c = 0;
457     if(!c) c = (classinfo_t*)registry_safefind("", "RegExp");
458     return c;
459 }
460 classinfo_t* registry_getdateclass() {
461     static classinfo_t*c = 0;
462     if(!c) c = (classinfo_t*)registry_safefind("", "Date");
463     return c;
464 }
465 classinfo_t* registry_getxmlclass() {
466     static classinfo_t*c = 0;
467     if(!c) c = (classinfo_t*)registry_safefind("", "XML");
468     return c;
469 }
470 classinfo_t* registry_getxmllistclass() {
471     static classinfo_t*c = 0;
472     if(!c) c = (classinfo_t*)registry_safefind("", "XMLList");
473     return c;
474 }
475 classinfo_t* registry_getnamespaceclass() {
476     static classinfo_t*c = 0;
477     if(!c) c = (classinfo_t*)registry_safefind("", "Namespace");
478     return c;
479 }
480 classinfo_t* registry_getMovieClip() {
481     static classinfo_t*c = 0;
482     if(!c) c = (classinfo_t*)registry_safefind("flash.display", "MovieClip");
483     return c;
484 }
485
486 // ----------------------- builtin dummy types -------------------------
487 classinfo_t nullclass = {
488     INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "null", 0, 0, 0
489 };
490 classinfo_t* registry_getnullclass() {
491     return &nullclass;
492 }
493 classinfo_t voidclass = {
494     INFOTYPE_CLASS,0,0,ACCESS_PACKAGE, "", "void", 0, 0, 0
495 };
496 classinfo_t* registry_getvoidclass() {
497     return &voidclass;
498 }
499
500 namespace_t access2namespace(U8 access, char*package)
501 {
502     namespace_t ns;
503     ns.access = access;
504     ns.name = package;
505     return ns;
506 }
507
508 char* infotypename(slotinfo_t*s)
509 {
510     if(s->kind == INFOTYPE_CLASS) return "class";
511     else if(s->kind == INFOTYPE_VAR) return "var";
512     else if(s->kind == INFOTYPE_METHOD) return "function";
513     else return "object";
514 }
515
516 void slotinfo_dump(slotinfo_t*s)
517 {
518     if(s->package[0]) {
519         printf("%s %s.%s", infotypename(s), s->package, s->name);
520     } else {
521         printf("%s %s", infotypename(s), s->name);
522     }
523     if(s->kind == INFOTYPE_CLASS) {
524         classinfo_t*c = (classinfo_t*)s;
525     }
526     else if(s->kind == INFOTYPE_VAR) {
527         varinfo_t*v = (varinfo_t*)s;
528         printf(":%s", v->type?v->type->name:"*");
529         if(v->value)
530             printf("=%s", constant_tostring(v->value));
531         if(v->slot)
532             printf(" (slot:%d)", v->slot);
533     }
534     else if(s->kind == INFOTYPE_METHOD) {
535         methodinfo_t*m = (methodinfo_t*)s;
536     }
537     else {
538     }
539     printf("\n");
540 }
541