blob: 086234d7606f3fb9f500efbebe4f9017f0860567 [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 Carlstrom7e49dca2011-07-22 18:07:34 -070026 DexCache* AllocDexCache();
27 Class* AllocClass(DexCache* dex_cache);
Brian Carlstroma0808032011-07-18 00:39:23 -070028 StaticField* AllocStaticField();
29 InstanceField* AllocInstanceField();
30 Method* AllocMethod();
Brian Carlstrom4a96b602011-07-26 16:40:23 -070031 template <class C> ObjectArray<C>* AllocObjectArray(size_t length) {
32 return Heap::AllocObjectArray<C>(object_array_class_, length);
33 }
34
Brian Carlstroma0808032011-07-18 00:39:23 -070035
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070036 // Finds a class by its descriptor name.
Brian Carlstromf615a612011-07-23 12:50:34 -070037 // If dex_file is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070038 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070039 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070040 const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
Brian Carlstrom6cc18452011-07-18 15:10:33 -070042 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070043 return FindClass(descriptor, NULL, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070044 }
45
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046 bool InitializeClass(Class* klass);
47
Brian Carlstrom6cc18452011-07-18 15:10:33 -070048 Class* LookupClass(const StringPiece& descriptor, Object* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070050 Class* ResolveClass(const Class* referring,
51 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -070052 const DexFile* dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070053
54 String* ResolveString(const Class* referring, uint32_t string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070055
Brian Carlstrom4a96b602011-07-26 16:40:23 -070056 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
58 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070059 ClassLinker() {}
60
Carl Shapiro2ed144c2011-07-26 16:52:08 -070061 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070062
Brian Carlstroma331b3c2011-07-18 17:47:56 -070063 Class* CreatePrimitiveClass(const StringPiece& descriptor);
64
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070065 Class* CreateArrayClass(const StringPiece& descriptor,
66 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070067 const DexFile* dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070068
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070069 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070070
Brian Carlstromf615a612011-07-23 12:50:34 -070071 const DexFile* FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070072
Brian Carlstromf615a612011-07-23 12:50:34 -070073 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070074
Brian Carlstromf615a612011-07-23 12:50:34 -070075 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070076
Brian Carlstromf615a612011-07-23 12:50:34 -070077 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078
79 ClassPathEntry FindInBootClassPath(const StringPiece& descriptor);
80
Brian Carlstromf615a612011-07-23 12:50:34 -070081 void LoadClass(const DexFile& dex_file,
82 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070083 Class* klass);
84
Brian Carlstromf615a612011-07-23 12:50:34 -070085 void LoadInterfaces(const DexFile& dex_file,
86 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087 Class *klass);
88
Brian Carlstromf615a612011-07-23 12:50:34 -070089 void LoadField(const DexFile& dex_file,
90 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070091 Class* klass,
92 Field* dst);
93
Brian Carlstromf615a612011-07-23 12:50:34 -070094 void LoadMethod(const DexFile& dex_file,
95 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070096 Class* klass,
97 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -070098
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070099 // Inserts a class into the class table. Returns true if the class
100 // was inserted.
101 bool InsertClass(Class* klass);
102
103 bool InitializeSuperClass(Class* klass);
104
105 void InitializeStaticFields(Class* klass);
106
107 bool ValidateSuperClassDescriptors(const Class* klass);
108
109 bool HasSameDescriptorClasses(const char* descriptor,
110 const Class* klass1,
111 const Class* klass2);
112
113 bool HasSameMethodDescriptorClasses(const Method* descriptor,
114 const Class* klass1,
115 const Class* klass2);
116
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700117 bool HasSameNameAndPrototype(const Method* m1, const Method* m2) const {
118 return HasSameName(m1, m2) && HasSamePrototype(m1, m2);
119 }
120
121 bool HasSameName(const Method* m1, const Method* m2) const {
122 return m1->GetName() == m2->GetName();
123 }
124
125 bool HasSamePrototype(const Method* m1, const Method* m2) const {
126 return HasSameReturnType(m1, m2) && HasSameArgumentTypes(m1, m2);
127 }
128
129 bool HasSameReturnType(const Method* m1, const Method* m2) const;
130
131 bool HasSameArgumentTypes(const Method* m1, const Method* m2) const;
132
Brian Carlstromf615a612011-07-23 12:50:34 -0700133 bool LinkClass(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134
135 bool LinkSuperClass(Class* klass);
136
Brian Carlstromf615a612011-07-23 12:50:34 -0700137 bool LinkInterfaces(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700138
139 bool LinkMethods(Class* klass);
140
141 bool LinkVirtualMethods(Class* klass);
142
143 bool LinkInterfaceMethods(Class* klass);
144
145 void LinkAbstractMethods(Class* klass);
146
147 bool LinkInstanceFields(Class* klass);
148
149 void CreateReferenceOffsets(Class* klass);
150
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700151 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700152
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700153 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700154
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700155 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700156
157 // TODO: multimap
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700158 typedef std::map<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700159
160 Table classes_;
161
162 Mutex* classes_lock_;
163
164 // TODO: classpath
165
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700166 Class* java_lang_Class_;
167 Class* java_lang_Object_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700168 Class* java_lang_reflect_Field_;
169 Class* java_lang_reflect_Method_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700170 Class* java_lang_Cloneable_;
171 Class* java_io_Serializable_;
172 Class* java_lang_String_;
173
Carl Shapiro565f5072011-07-10 13:39:43 -0700174 Class* primitive_boolean_;
175 Class* primitive_char_;
176 Class* primitive_float_;
177 Class* primitive_double_;
178 Class* primitive_byte_;
179 Class* primitive_short_;
180 Class* primitive_int_;
181 Class* primitive_long_;
182 Class* primitive_void_;
183
Brian Carlstroma0808032011-07-18 00:39:23 -0700184 Class* char_array_class_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700185 Class* class_array_class_;
186 Class* object_array_class_;
187 Class* field_array_class_;
188 Class* method_array_class_;
189
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700190 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700191 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700192
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700193 FRIEND_TEST(ClassLinkerTest, ProtoCompare);
194 FRIEND_TEST(ClassLinkerTest, ProtoCompare2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700195 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
196};
197
198} // namespace art
199
200#endif // ART_SRC_CLASS_LINKER_H_