blob: 738979b634310c406ab5fbdb1725874686b8118c [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 Carlstroma40f9bc2011-07-26 21:26:07 -070031
32 template <class T>
33 ObjectArray<T>* AllocObjectArray(size_t length) {
34 return ObjectArray<T>::Alloc(object_array_class_, length);
Brian Carlstrom4a96b602011-07-26 16:40:23 -070035 }
36
Brian Carlstroma0808032011-07-18 00:39:23 -070037
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038 // Finds a class by its descriptor name.
Brian Carlstromf615a612011-07-23 12:50:34 -070039 // If dex_file is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070040 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070041 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070042 const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070043
Brian Carlstrom6cc18452011-07-18 15:10:33 -070044 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070045 return FindClass(descriptor, NULL, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070046 }
47
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048 bool InitializeClass(Class* klass);
49
Brian Carlstrom6cc18452011-07-18 15:10:33 -070050 Class* LookupClass(const StringPiece& descriptor, Object* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 Class* ResolveClass(const Class* referring,
53 uint32_t class_idx,
Brian Carlstromf615a612011-07-23 12:50:34 -070054 const DexFile* dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070055
56 String* ResolveString(const Class* referring, uint32_t string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070057
Brian Carlstrom4a96b602011-07-26 16:40:23 -070058 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070059
60 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070061 ClassLinker() {}
62
Carl Shapiro2ed144c2011-07-26 16:52:08 -070063 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070064
Brian Carlstroma331b3c2011-07-18 17:47:56 -070065 Class* CreatePrimitiveClass(const StringPiece& descriptor);
66
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070067 Class* CreateArrayClass(const StringPiece& descriptor,
68 Object* class_loader,
Brian Carlstromf615a612011-07-23 12:50:34 -070069 const DexFile* dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070070
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070071 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070072
Brian Carlstromf615a612011-07-23 12:50:34 -070073 const DexFile* FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070074
Brian Carlstromf615a612011-07-23 12:50:34 -070075 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070076
Brian Carlstromf615a612011-07-23 12:50:34 -070077 typedef std::pair<const DexFile*, const DexFile::ClassDef*> ClassPathEntry;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070078
Brian Carlstromf615a612011-07-23 12:50:34 -070079 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080
81 ClassPathEntry FindInBootClassPath(const StringPiece& descriptor);
82
Brian Carlstromf615a612011-07-23 12:50:34 -070083 void LoadClass(const DexFile& dex_file,
84 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070085 Class* klass);
86
Brian Carlstromf615a612011-07-23 12:50:34 -070087 void LoadInterfaces(const DexFile& dex_file,
88 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070089 Class *klass);
90
Brian Carlstromf615a612011-07-23 12:50:34 -070091 void LoadField(const DexFile& dex_file,
92 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070093 Class* klass,
94 Field* dst);
95
Brian Carlstromf615a612011-07-23 12:50:34 -070096 void LoadMethod(const DexFile& dex_file,
97 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070098 Class* klass,
99 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700100
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700101 // Inserts a class into the class table. Returns true if the class
102 // was inserted.
103 bool InsertClass(Class* klass);
104
105 bool InitializeSuperClass(Class* klass);
106
107 void InitializeStaticFields(Class* klass);
108
109 bool ValidateSuperClassDescriptors(const Class* klass);
110
111 bool HasSameDescriptorClasses(const char* descriptor,
112 const Class* klass1,
113 const Class* klass2);
114
115 bool HasSameMethodDescriptorClasses(const Method* descriptor,
116 const Class* klass1,
117 const Class* klass2);
118
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700119 bool HasSameNameAndPrototype(const Method* m1, const Method* m2) const {
120 return HasSameName(m1, m2) && HasSamePrototype(m1, m2);
121 }
122
123 bool HasSameName(const Method* m1, const Method* m2) const {
124 return m1->GetName() == m2->GetName();
125 }
126
127 bool HasSamePrototype(const Method* m1, const Method* m2) const {
128 return HasSameReturnType(m1, m2) && HasSameArgumentTypes(m1, m2);
129 }
130
131 bool HasSameReturnType(const Method* m1, const Method* m2) const;
132
133 bool HasSameArgumentTypes(const Method* m1, const Method* m2) const;
134
Brian Carlstromf615a612011-07-23 12:50:34 -0700135 bool LinkClass(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700136
137 bool LinkSuperClass(Class* klass);
138
Brian Carlstromf615a612011-07-23 12:50:34 -0700139 bool LinkInterfaces(Class* klass, const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700140
141 bool LinkMethods(Class* klass);
142
143 bool LinkVirtualMethods(Class* klass);
144
145 bool LinkInterfaceMethods(Class* klass);
146
147 void LinkAbstractMethods(Class* klass);
148
149 bool LinkInstanceFields(Class* klass);
150
151 void CreateReferenceOffsets(Class* klass);
152
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700153 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700154
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700155 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700156
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700157 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700158
159 // TODO: multimap
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700160 typedef std::map<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700161
162 Table classes_;
163
164 Mutex* classes_lock_;
165
166 // TODO: classpath
167
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700168 Class* java_lang_Class_;
169 Class* java_lang_Object_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700170 Class* java_lang_reflect_Field_;
171 Class* java_lang_reflect_Method_;
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700172 Class* java_lang_Cloneable_;
173 Class* java_io_Serializable_;
174 Class* java_lang_String_;
175
Carl Shapiro565f5072011-07-10 13:39:43 -0700176 Class* primitive_boolean_;
177 Class* primitive_char_;
178 Class* primitive_float_;
179 Class* primitive_double_;
180 Class* primitive_byte_;
181 Class* primitive_short_;
182 Class* primitive_int_;
183 Class* primitive_long_;
184 Class* primitive_void_;
185
Brian Carlstroma0808032011-07-18 00:39:23 -0700186 Class* char_array_class_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700187 Class* class_array_class_;
188 Class* object_array_class_;
189 Class* field_array_class_;
190 Class* method_array_class_;
191
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700192 ObjectArray<Class>* array_interfaces_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700193 InterfaceEntry* array_iftable_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700194
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700195 FRIEND_TEST(ClassLinkerTest, ProtoCompare);
196 FRIEND_TEST(ClassLinkerTest, ProtoCompare2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700197 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
198};
199
200} // namespace art
201
202#endif // ART_SRC_CLASS_LINKER_H_