blob: fb684190a52fd67d1b1e0676960e8b01945a4bb1 [file] [log] [blame]
Carl Shapiro1fb86202011-06-27 17:43:13 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_OBJECT_H_
4#define ART_SRC_OBJECT_H_
5
6#include "src/globals.h"
Carl Shapiro3ee755d2011-06-28 12:11:04 -07007#include "src/macros.h"
Carl Shapiro1fb86202011-06-27 17:43:13 -07008
9namespace art {
10
11class Array;
12class Class;
13class DexFile;
14class IField;
15class InterfaceEntry;
16class Monitor;
17class Method;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070018class Object;
Carl Shapiro1fb86202011-06-27 17:43:13 -070019class SField;
20
Carl Shapiro3ee755d2011-06-28 12:11:04 -070021union JValue {
22 uint8_t z;
23 int8_t b;
24 uint16_t c;
25 int16_t s;
26 int32_t i;
27 int64_t j;
28 float f;
29 double d;
30 Object* l;
31};
32
33static const uint32_t kAccPublic = 0x0001; // class, field, method, ic
34static const uint32_t kAccPrivate = 0x0002; // field, method, ic
35static const uint32_t kAccProtected = 0x0004; // field, method, ic
36static const uint32_t kAccStatic = 0x0008; // field, method, ic
37static const uint32_t kAccFinal = 0x0010; // class, field, method, ic
38static const uint32_t kAccSynchronized = 0x0020; // method (only allowed on natives)
39static const uint32_t kAccSuper = 0x0020; // class (not used in Dalvik)
40static const uint32_t kAccVolatile = 0x0040; // field
41static const uint32_t kAccBridge = 0x0040; // method (1.5)
42static const uint32_t kAccTransient = 0x0080; // field
43static const uint32_t kAccVarargs = 0x0080; // method (1.5)
44static const uint32_t kAccNative = 0x0100; // method
45static const uint32_t kAccInterface = 0x0200; // class, ic
46static const uint32_t kAccAbstract = 0x0400; // class, method, ic
47static const uint32_t kAccStrict = 0x0800; // method
48static const uint32_t kAccSynthetic = 0x1000; // field, method, ic
49static const uint32_t kAccAnnotation = 0x2000; // class, ic (1.5)
50static const uint32_t kAccEnum = 0x4000; // class, field, ic (1.5)
51
52static const uint32_t kAccConstructor = 0x00010000; // method (Dalvik only)
53static const uint32_t kAccDeclaredSynchronized = 0x00020000; // method (Dalvik only)
54
55
Carl Shapiro1fb86202011-06-27 17:43:13 -070056class Object {
Carl Shapiro3ee755d2011-06-28 12:11:04 -070057 public:
Carl Shapiro1fb86202011-06-27 17:43:13 -070058 Class* klass_;
59 Monitor* lock_;
60};
61
62class Field {
Carl Shapiro3ee755d2011-06-28 12:11:04 -070063 public:
64 // The class in which this field is declared.
Carl Shapiro1fb86202011-06-27 17:43:13 -070065 Class* klass_;
Carl Shapiro3ee755d2011-06-28 12:11:04 -070066
67 const char* name_;
68
69 // e.g. "I", "[C", "Landroid/os/Debug;"
70 const char* signature_;
71
72 uint32_t access_flags_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070073};
74
75// Instance fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070076class IField : public Field {
77 uint32_t offset_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070078};
79
80// Static fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070081class SField : public Field {
82 JValue value_;
Carl Shapiro1fb86202011-06-27 17:43:13 -070083};
84
85// Class objects.
Carl Shapiro3ee755d2011-06-28 12:11:04 -070086class Class : public Object {
Carl Shapiro1fb86202011-06-27 17:43:13 -070087 public:
88 enum ClassStatus {
Carl Shapiro894d0fa2011-06-30 14:48:49 -070089 kStatusError = -1,
90 kStatusNotReady = 0,
91 kStatusIdx = 1, // loaded, DEX idx in super or ifaces
92 kStatusLoaded = 2, // DEX idx values resolved
93 kStatusResolved = 3, // part of linking
94 kStatusVerifying = 4, // in the process of being verified
95 kStatusVerified = 5, // logically part of linking; done pre-init
96 kStatusInitializing = 6, // class init in progress
97 kStatusInitialized = 7, // ready to go
Carl Shapiro1fb86202011-06-27 17:43:13 -070098 };
99
100 enum PrimitiveType {
101 kPrimNot = -1
102 };
103
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700104 // Returns true if this class is in the same packages as that class.
105 bool IsInSamePackage(const Class* that) const;
106
107 static bool IsInSamePackage(const char* descriptor1, const char* descriptor2);
108
109 // Returns true if this class represents an array class.
110 bool IsArray() const {
111 return descriptor_[0] == '['; // TODO: avoid parsing the descriptor
112 }
113
114 // Returns true if the class is an interface.
115 bool IsInterface() const {
116 return (access_flags_ & kAccInterface) != 0;
117 }
118
119 // Returns true if the class is declared public.
120 bool IsPublic() const {
121 return (access_flags_ & kAccPublic) != 0;
122 }
123
124 // Returns true if the class is declared final.
125 bool IsFinal() const {
126 return (access_flags_ & kAccFinal) != 0;
127 }
128
129 // Returns true if the class is abstract.
130 bool IsAbstract() const {
131 return (access_flags_ & kAccAbstract) != 0;
132 }
133
134 // Returns true if the class is an annotation.
135 bool IsAnnotation() const {
136 return (access_flags_ & kAccAnnotation) != 0;
137 }
138
139 // Returns true if the class is a primitive type.
140 bool IsPrimitive() const {
141 return primitive_type_ != kPrimNot;
142 }
143
144 // Returns true if this class can access that class.
145 bool CanAccess(const Class* that) const {
146 return that->IsPublic() && this->IsInSamePackage(that);
147 }
148
Carl Shapiro1fb86202011-06-27 17:43:13 -0700149 // Returns the size in bytes of a class object instance with the
150 // given number of static fields.
151 static size_t Size(size_t num_sfields) {
152 return OFFSETOF_MEMBER(Class, sfields_) + sizeof(SField) * num_sfields;
153 }
154
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700155 uint32_t NumDirectMethods() {
156 return num_dmethods_;
157 }
158
159 uint32_t NumVirtualMethods() {
160 return num_vmethods_;
161 }
162
Carl Shapiro1fb86202011-06-27 17:43:13 -0700163 public: // TODO: private
164 // leave space for instance data; we could access fields directly if
165 // we freeze the definition of java/lang/Class
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700166#define CLASS_FIELD_SLOTS 4
Carl Shapiro1fb86202011-06-27 17:43:13 -0700167 uint32_t instance_data_[CLASS_FIELD_SLOTS];
168#undef CLASS_FIELD_SLOTS
169
170 // UTF-8 descriptor for the class from constant pool
171 // ("Ljava/lang/Class;"), or on heap if generated ("[C")
172 const char* descriptor_;
173
174 // Proxy classes have their descriptor allocated on the native heap.
175 // When this field is non-NULL it must be explicitly freed.
176 char* descriptor_alloc_;
177
178 // access flags; low 16 bits are defined by VM spec
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700179 uint32_t access_flags_; // TODO: make an instance field?
Carl Shapiro1fb86202011-06-27 17:43:13 -0700180
181 // VM-unique class serial number, nonzero, set very early
182 //uint32_t serial_number_;
183
184 // DexFile from which we came; needed to resolve constant pool entries
185 // (will be NULL for VM-generated, e.g. arrays and primitive classes)
186 DexFile* dex_file_;
187
188 // state of class initialization
189 ClassStatus status_;
190
191 // if class verify fails, we must return same error on subsequent tries
192 Class* verify_error_class_;
193
194 // threadId, used to check for recursive <clinit> invocation
195 uint32_t clinit_thread_id_;
196
197 // Total object size; used when allocating storage on gc heap. (For
198 // interfaces and abstract classes this will be zero.)
199 uint32_t object_size_;
200
201 // For array classes, the class object for base element, for
202 // instanceof/checkcast (for String[][][], this will be String).
203 // Otherwise, NULL.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700204 Class* array_element_class_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700205
206 // For array classes, the number of array dimensions, e.g. int[][]
207 // is 2. Otherwise 0.
208 int32_t array_rank_;
209
210 // primitive type index, or PRIM_NOT (-1); set for generated prim classes
211 PrimitiveType primitive_type_;
212
213 // The superclass, or NULL if this is java.lang.Object or a
214 // primitive type.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700215 Class* super_; // TODO: make an instance field
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700216 uint32_t super_idx_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700217
218 // defining class loader, or NULL for the "bootstrap" system loader
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700219 Object* class_loader_; // TODO: make an instance field
Carl Shapiro1fb86202011-06-27 17:43:13 -0700220
221 // initiating class loader list
222 // NOTE: for classes with low serialNumber, these are unused, and the
223 // values are kept in a table in gDvm.
224 //InitiatingLoaderList initiating_loader_list_;
225
226 // array of interfaces this class implements directly
Carl Shapiro894d0fa2011-06-30 14:48:49 -0700227 uint32_t interface_count_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700228 Class** interfaces_;
229
230 // static, private, and <init> methods
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700231 uint32_t num_dmethods_;
232 Method* dmethods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700233
234 // virtual methods defined in this class; invoked through vtable
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700235 uint32_t num_vmethods_;
236 Method* vmethods_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700237
238 // Virtual method table (vtable), for use by "invoke-virtual". The
239 // vtable from the superclass is copied in, and virtual methods from
240 // our class either replace those from the super or are appended.
241 int32_t vtable_count_;
242 Method** vtable_;
243
244 // Interface table (iftable), one entry per interface supported by
245 // this class. That means one entry for each interface we support
246 // directly, indirectly via superclass, or indirectly via
247 // superinterface. This will be null if neither we nor our
248 // superclass implement any interfaces.
249 //
250 // Why we need this: given "class Foo implements Face", declare
251 // "Face faceObj = new Foo()". Invoke faceObj.blah(), where "blah"
252 // is part of the Face interface. We can't easily use a single
253 // vtable.
254 //
255 // For every interface a concrete class implements, we create a list
256 // of virtualMethod indices for the methods in the interface.
257 int iftable_count_;
258 InterfaceEntry* iftable_;
259
260 // The interface vtable indices for iftable get stored here. By
261 // placing them all in a single pool for each class that implements
262 // interfaces, we decrease the number of allocations.
263 int ifvipool_count;
264 int* ifvipool_;
265
266 // instance fields
267 //
268 // These describe the layout of the contents of a
269 // DataObject-compatible Object. Note that only the fields directly
270 // declared by this class are listed in ifields; fields declared by
271 // a superclass are listed in the superclass's ClassObject.ifields.
272 //
273 // All instance fields that refer to objects are guaranteed to be at
274 // the beginning of the field list. ifieldRefCount specifies the
275 // number of reference fields.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700276 uint32_t num_ifields_;
277
278 // number of fields that are object refs
279 uint32_t num_reference_ifields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700280 IField* ifields_;
281
282 // bitmap of offsets of ifields
283 uint32_t ref_offsets_;
284
285 // source file name, if known. Otherwise, NULL.
286 const char* source_file_;
287
288 // Static fields
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700289 uint32_t num_sfields_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700290 SField sfields_[]; // MUST be last item
291};
292
293class String : Object {
294 public:
295 Array* array_;
296
297 uint32_t hash_code_;
298
299 uint32_t offset_;
300
301 uint32_t count_;
302};
303
304class Array : Object {
305 public:
306 // The number of array elements.
307 uint32_t length_;
308};
309
310class Method {
311 public:
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700312 // Returns true if the method is declared public.
313 bool IsPublic() {
314 return (access_flags_ & kAccPublic) != 0;
315 }
316
317 // Returns true if the method is declared private.
318 bool IsPrivate() {
319 return (access_flags_ & kAccPrivate) != 0;
320 }
321
322 // Returns true if the method is declared static.
323 bool IsStatic() {
324 return (access_flags_ & kAccStatic) != 0;
325 }
326
Carl Shapiro64f13402011-06-28 16:34:41 -0700327 // Returns true if the method is declared synchronized.
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700328 bool IsSynchronized() {
Carl Shapiro64f13402011-06-28 16:34:41 -0700329 uint32_t synchonized = kAccSynchronized | kAccDeclaredSynchronized;
330 return (access_flags_ & synchonized) != 0;
Carl Shapiro3ee755d2011-06-28 12:11:04 -0700331 }
332
333 // Returns true if the method is declared final.
334 bool IsFinal() {
335 return (access_flags_ & kAccFinal) != 0;
336 }
337
338 // Returns true if the method is declared native.
339 bool IsNative() {
340 return (access_flags_ & kAccNative) != 0;
341 }
342
343 // Returns true if the method is declared abstract.
344 bool IsAbstract() {
345 return (access_flags_ & kAccAbstract) != 0;
346 }
347
348 bool IsSynthetic() {
349 return (access_flags_ & kAccSynthetic) != 0;
350 }
351
352 // Number of argument registers required by the prototype.
353 uint32_t NumArgRegisters();
354
355 public: // TODO: private
356 // the class we are a part of
357 Class* klass_;
358
359 // access flags; low 16 bits are defined by spec (could be uint16_t?)
360 uint32_t access_flags_;
361
362 // For concrete virtual methods, this is the offset of the method
363 // in "vtable".
364 //
365 // For abstract methods in an interface class, this is the offset
366 // of the method in "iftable[n]->methodIndexArray".
367 uint16_t method_index_;
368
369 // Method bounds; not needed for an abstract method.
370 //
371 // For a native method, we compute the size of the argument list, and
372 // set "insSize" and "registerSize" equal to it.
373 uint16_t num_registers_; // ins + locals
374 uint16_t num_outs_;
375 uint16_t num_ins_;
376
377 // method name, e.g. "<init>" or "eatLunch"
378 const char* name_;
379
380 // A pointer to the DEX file this class was loaded from or NULL for
381 // proxy objects.
382 DexFile* dex_file_;
383
384 // Method prototype descriptor string (return and argument types).
385 uint32_t proto_idx_;
386
387 // The short-form method descriptor string.
388 const char* shorty_;
389
390 // A pointer to the memory-mapped DEX code.
391 const uint16_t* insns_;
Carl Shapiro1fb86202011-06-27 17:43:13 -0700392};
393
394} // namespace art
395
396#endif // ART_SRC_OBJECT_H_