blob: 46d4b1f7618001869ec10276bf017d1d98241ac9 [file] [log] [blame]
Carl Shapiro0e5d75d2011-07-06 18:28:37 -07001// Copyright 2011 Google Inc. All Rights Reserved.
2
3#ifndef ART_SRC_CLASS_LINKER_H_
4#define ART_SRC_CLASS_LINKER_H_
5
6#include <map>
7#include <utility>
8#include <vector>
9
Brian Carlstrom4a96b602011-07-26 16:40:23 -070010#include "heap.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070011#include "macros.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070012#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070013#include "thread.h"
14#include "object.h"
15#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070016
17namespace art {
18
19class ClassLinker {
20 public:
Carl Shapiro565f5072011-07-10 13:39:43 -070021 // Initializes the class linker.
Carl Shapiro2ed144c2011-07-26 16:52:08 -070022 static ClassLinker* Create(const std::vector<DexFile*>& boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070023
24 ~ClassLinker() {}
Carl Shapiro565f5072011-07-10 13:39:43 -070025
Brian Carlstrom0b138b22011-07-27 15:19:17 -070026 // Alloc* convenience functions to avoid needing to pass in Class*
27 // values that are known to the ClassLinker such as
28 // object_array_class_ and java_lang_String_ etc.
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070029 DexCache* AllocDexCache();
30 Class* AllocClass(DexCache* dex_cache);
Brian Carlstroma0808032011-07-18 00:39:23 -070031 StaticField* AllocStaticField();
32 InstanceField* AllocInstanceField();
33 Method* AllocMethod();
Brian Carlstrom0b138b22011-07-27 15:19:17 -070034 String* AllocStringFromModifiedUtf8(int32_t utf16_length, const char* utf8_data_in);
Brian Carlstroma40f9bc2011-07-26 21:26:07 -070035 template <class T>
36 ObjectArray<T>* AllocObjectArray(size_t length) {
37 return ObjectArray<T>::Alloc(object_array_class_, length);
Brian Carlstrom4a96b602011-07-26 16:40:23 -070038 }
39
Brian Carlstroma0808032011-07-18 00:39:23 -070040
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041 // Finds a class by its descriptor name.
Brian Carlstromf615a612011-07-23 12:50:34 -070042 // If dex_file is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070043 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070044 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070045 const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Brian Carlstrom6cc18452011-07-18 15:10:33 -070047 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070048 return FindClass(descriptor, NULL, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070049 }
50
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051 bool InitializeClass(Class* klass);
52
Brian Carlstrom6cc18452011-07-18 15:10:33 -070053 Class* LookupClass(const StringPiece& descriptor, Object* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070054
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070055 Class* ResolveClass(const Class* referring,
56 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -070057 const DexFile* dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070058
59 String* ResolveString(const Class* referring, uint32_t string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070060
Brian Carlstrom4a96b602011-07-26 16:40:23 -070061 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070062
63 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070064 ClassLinker() {}
65
Carl Shapiro2ed144c2011-07-26 16:52:08 -070066 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070067
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068 Class* CreatePrimitiveClass(const StringPiece& descriptor);
69
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070070 Class* CreateArrayClass(const StringPiece& descriptor,
71 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070072 const DexFile* dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070073
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070074 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070075
Brian Carlstromf615a612011-07-23 12:50:34 -070076 const DexFile* FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070077
Brian Carlstromf615a612011-07-23 12:50:34 -070078 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070079
Brian Carlstromf615a612011-07-23 12:50:34 -070080 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070081
Brian Carlstromf615a612011-07-23 12:50:34 -070082 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070083
84 ClassPathEntry FindInBootClassPath(const StringPiece& descriptor);
85
Brian Carlstromf615a612011-07-23 12:50:34 -070086 void LoadClass(const DexFile& dex_file,
87 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070088 Class* klass);
89
Brian Carlstromf615a612011-07-23 12:50:34 -070090 void LoadInterfaces(const DexFile& dex_file,
91 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092 Class *klass);
93
Brian Carlstromf615a612011-07-23 12:50:34 -070094 void LoadField(const DexFile& dex_file,
95 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070096 Class* klass,
97 Field* dst);
98
Brian Carlstromf615a612011-07-23 12:50:34 -070099 void LoadMethod(const DexFile& dex_file,
100 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700101 Class* klass,
102 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700103
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700104 // Inserts a class into the class table. Returns true if the class
105 // was inserted.
106 bool InsertClass(Class* klass);
107
108 bool InitializeSuperClass(Class* klass);
109
110 void InitializeStaticFields(Class* klass);
111
112 bool ValidateSuperClassDescriptors(const Class* klass);
113
114 bool HasSameDescriptorClasses(const char* descriptor,
115 const Class* klass1,
116 const Class* klass2);
117
118 bool HasSameMethodDescriptorClasses(const Method* descriptor,
119 const Class* klass1,
120 const Class* klass2);
121
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700122 bool HasSameNameAndPrototype(const Method* m1, const Method* m2) const {
123 return HasSameName(m1, m2) && HasSamePrototype(m1, m2);
124 }
125
126 bool HasSameName(const Method* m1, const Method* m2) const {
127 return m1->GetName() == m2->GetName();
128 }
129
130 bool HasSamePrototype(const Method* m1, const Method* m2) const {
131 return HasSameReturnType(m1, m2) && HasSameArgumentTypes(m1, m2);
132 }
133
134 bool HasSameReturnType(const Method* m1, const Method* m2) const;
135
136 bool HasSameArgumentTypes(const Method* m1, const Method* m2) const;
137
Brian Carlstromf615a612011-07-23 12:50:34 -0700138 bool LinkClass(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700139
140 bool LinkSuperClass(Class* klass);
141
Brian Carlstromf615a612011-07-23 12:50:34 -0700142 bool LinkInterfaces(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700143
144 bool LinkMethods(Class* klass);
145
146 bool LinkVirtualMethods(Class* klass);
147
148 bool LinkInterfaceMethods(Class* klass);
149
150 void LinkAbstractMethods(Class* klass);
151
152 bool LinkInstanceFields(Class* klass);
153
154 void CreateReferenceOffsets(Class* klass);
155
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700156 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700157
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700158 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700159
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700160 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161
162 // TODO: multimap
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700163 typedef std::map<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700164
165 Table classes_;
166
167 Mutex* classes_lock_;
168
169 // TODO: classpath
170
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700171 Class* java_lang_Class_;
172 Class* java_lang_Object_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700173 Class* java_lang_reflect_Field_;
174 Class* java_lang_reflect_Method_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700175 Class* java_lang_Cloneable_;
176 Class* java_io_Serializable_;
177 Class* java_lang_String_;
178
Carl Shapiro565f5072011-07-10 13:39:43 -0700179 Class* primitive_boolean_;
180 Class* primitive_char_;
181 Class* primitive_float_;
182 Class* primitive_double_;
183 Class* primitive_byte_;
184 Class* primitive_short_;
185 Class* primitive_int_;
186 Class* primitive_long_;
187 Class* primitive_void_;
188
Brian Carlstroma0808032011-07-18 00:39:23 -0700189 Class* char_array_class_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700190 Class* class_array_class_;
191 Class* object_array_class_;
192 Class* field_array_class_;
193 Class* method_array_class_;
194
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700195 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700196 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700197
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700198 FRIEND_TEST(ClassLinkerTest, ProtoCompare);
199 FRIEND_TEST(ClassLinkerTest, ProtoCompare2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700200 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
201};
202
203} // namespace art
204
205#endif // ART_SRC_CLASS_LINKER_H_