41496edb642a8a005664138c1ccd8d287c955e71
[swftools.git] / lib / as3 / abc.h
1 /* abc.h
2
3    Routines for handling Flash2 AVM2 ABC Actionscript (header file)
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 #ifndef __swfabc_h__
25 #define __swfabc_h__
26
27 #include "../q.h"
28 #include "pool.h"
29
30 DECLARE(abc_file);
31 DECLARE(abc_method);
32 DECLARE(abc_method_body);
33 DECLARE(abc_interface);
34 DECLARE(abc_class);
35 DECLARE(abc_exception);
36 DECLARE(abc_asset);
37 DECLARE(asset_dependency);
38 DECLARE_LIST(abc_asset);
39 DECLARE_LIST(asset_dependency);
40 DECLARE_LIST(abc_exception);
41 DECLARE_LIST(TAG);
42
43 #include "code.h"
44 #include "opcodes.h"
45
46 DECLARE(trait);
47
48
49 #define METHOD_NEED_ARGUMENTS 1
50 #define METHOD_NEED_ACTIVATION 2
51 #define METHOD_NEED_REST 4
52 #define METHOD_HAS_OPTIONAL 8
53 #define METHOD_SET_DXNS 0x40
54 #define METHOD_HAS_PARAM_NAMES 0x80
55
56 struct _abc_method {
57     multiname_t*return_type;
58     multiname_list_t*parameters;
59     constant_list_t*optional_parameters;
60     const char*name;
61     U8 flags;
62     abc_method_body_t*body;
63
64     trait_t*trait;
65     
66     int index; //filled in during writing
67 };
68
69 #define ABCFILE_LAZY 1
70 struct _abc_file {
71     // abc_file
72
73     const char*name;
74     U32 flags;
75     array_t*metadata;
76     array_t*methods;
77     array_t*classes;
78     array_t*scripts;
79     array_t*method_bodies;
80 };
81
82 abc_file_t*abc_file_new();
83
84 #define TRAIT_SLOT 0
85 #define TRAIT_METHOD 1
86 #define TRAIT_GETTER 2
87 #define TRAIT_SETTER 3
88 #define TRAIT_CLASS 4
89 #define TRAIT_FUNCTION 5
90 #define TRAIT_CONST 6
91             
92 #define TRAIT_ATTR_FINAL 0x10
93 #define TRAIT_ATTR_OVERRIDE 0x20
94 #define TRAIT_ATTR_METADATA 0x40
95
96
97 struct _trait {
98     unsigned char kind;
99     unsigned char attributes;
100     multiname_t*name;
101
102     union {
103         int disp_id;
104         int slot_id;
105         int data1;
106     };
107     union {
108         abc_method_t*method;
109         abc_class_t*cls;
110         multiname_t*type_name;
111         int data2;
112     };
113     constant_t*value;
114 };
115
116 trait_t*trait_new_method(trait_list_t**traits, multiname_t*name, abc_method_t*m);
117 trait_t*trait_new_member(trait_list_t**traits, multiname_t*type, multiname_t*name, constant_t*v);
118
119 #define CLASS_SEALED 1
120 #define CLASS_FINAL 2
121 #define CLASS_INTERFACE 4
122 #define CLASS_PROTECTED_NS 8
123
124 struct _abc_class {
125     abc_file_t*file;
126     
127     multiname_t*classname;
128     multiname_t*superclass;
129     namespace_t*protectedNS;
130
131     multiname_list_t*interfaces;
132
133     abc_method_t*constructor;
134     trait_list_t*traits;
135     
136     abc_method_t*static_constructor;
137     trait_list_t*static_traits;
138     
139     U8 flags;
140
141     abc_asset_t*asset; // swf tags needed for this class
142
143     int init_scope_depth; // volatile, might be increased during code verification
144     int index; //filled in during writing
145 };
146
147 void abc_method_init(abc_method_t*m, abc_file_t*file, multiname_t*returntype, char body);
148 abc_method_t* abc_method_new(abc_file_t*file, multiname_t*returntype, char body);
149
150 abc_class_t* abc_class_new(abc_file_t*file, multiname_t*classname, multiname_t*superclass);
151 abc_class_t* abc_class_new2(abc_file_t*file, char*classname, char*superclass);
152 void abc_class_sealed(abc_class_t*c);
153 void abc_class_final(abc_class_t*c);
154 void abc_class_interface(abc_class_t*c);
155 void abc_class_protectedNS(abc_class_t*c, char*namespace);
156 void abc_class_add_interface(abc_class_t*c, multiname_t*interface);
157 char*abc_class_fullname(abc_class_t*cls);
158
159 trait_t* traits_find_slotid(trait_list_t*traits, int slotid);
160
161 abc_method_t* abc_class_getconstructor(abc_class_t*cls, multiname_t*returntype);
162 abc_method_t* abc_class_getstaticconstructor(abc_class_t*cls, multiname_t*returntype);
163
164 abc_method_t* abc_class_method(abc_class_t*cls, multiname_t*returntype, multiname_t*name);
165 abc_method_t* abc_class_staticmethod(abc_class_t*cls, multiname_t*returntype, multiname_t*name);
166 trait_t*      abc_class_slot(abc_class_t*cls, multiname_t*name, multiname_t*type);
167 trait_t*      abc_class_staticslot(abc_class_t*cls, multiname_t*name, multiname_t*type);
168
169 struct _abc_exception {
170     code_t*from;
171     code_t*to;
172     code_t*target;
173     multiname_t*exc_type;
174     multiname_t*var_name;
175 };
176
177 struct _abc_method_body {
178     abc_file_t*file;
179     //abc_class_t*cls;
180     abc_method_t*method;
181     code_t*code;
182
183     struct {
184         //for dumping: filled in during parsing
185         int max_stack;
186         int local_count;
187         int max_scope_depth;
188         int init_scope_depth;
189     } old;
190
191     int init_scope_depth; // volatile, might be increased during code verification
192
193     abc_exception_list_t* exceptions;
194
195     trait_list_t*traits;
196     
197     int index; // filled in during writing
198     codestats_t*stats; //filled in during writing
199 };
200
201 typedef struct _abc_script {
202     abc_method_t*method;
203     abc_file_t*file;
204     trait_list_t*traits;
205 } abc_script_t;
206
207 struct _asset_dependency {
208     abc_asset_t*asset;
209     int*patch;
210     int patch_size;
211 };
212 struct _abc_asset {
213     TAG_list_t*tags;
214     U16 id;
215     asset_dependency_list_t*dependencies;
216 };
217
218 abc_method_t* abc_nullmethod(abc_file_t*file);
219 abc_script_t* abc_initscript(abc_file_t*file);
220 trait_t*abc_initscript_addClassTrait(abc_script_t*script, multiname_t*multiname, abc_class_t*cls);
221
222 #define __ 
223
224 #endif