blob: 70d268d131afa5304d7da24b9852ae474ab4598b [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
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070026 // Finds a class by its descriptor name.
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070027 // If class_loader is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070028 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070029 ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070030
Brian Carlstrom6cc18452011-07-18 15:10:33 -070031 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070032 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070033 }
34
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070035 bool InitializeClass(Class* klass);
36
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070037 Class* LookupClass(const StringPiece& descriptor, ClassLoader* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070038
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070039 Class* ResolveClass(const Class* referring,
40 uint32_t class_idx,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070041 const DexFile& dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070042
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070043 String* ResolveString(const Class* referring,
44 uint32_t string_idx,
45 const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070046
Brian Carlstrom4a96b602011-07-26 16:40:23 -070047 void RegisterDexFile(const DexFile* dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070048
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070049 // TODO replace with heap interface
50 typedef void (RootVistor)(Object* root, void* arg);
Brian Carlstromb88e9442011-07-28 15:15:51 -070051 void VisitRoots(RootVistor* root_visitor, void* arg);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070052
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070054 ClassLinker() {}
55
Carl Shapiro2ed144c2011-07-26 16:52:08 -070056 void Init(const std::vector<DexFile*>& boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070057
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070058 // For early bootstrapping by Init
59 Class* AllocClass(Class* java_lang_Class);
60
61 // Alloc* convenience functions to avoid needing to pass in Class*
62 // values that are known to the ClassLinker such as
63 // kObjectArrayClass and kJavaLangString etc.
64 Class* AllocClass();
65 DexCache* AllocDexCache();
66 StaticField* AllocStaticField();
67 InstanceField* AllocInstanceField();
68 Method* AllocMethod();
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070069 template <class T>
70 ObjectArray<T>* AllocObjectArray(size_t length) {
71 return ObjectArray<T>::Alloc(class_roots_->Get(kObjectArrayClass), length);
72 }
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070073 PathClassLoader* AllocPathClassLoader(std::vector<const DexFile*> dex_files);
Brian Carlstrom75cb3b42011-07-28 02:13:36 -070074
Brian Carlstroma331b3c2011-07-18 17:47:56 -070075 Class* CreatePrimitiveClass(const StringPiece& descriptor);
76
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070077 Class* CreateArrayClass(const StringPiece& descriptor,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070078 ClassLoader* class_loader);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070079
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070080 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070081
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070082 const DexFile& FindDexFile(const DexCache* dex_cache) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070083
Brian Carlstromf615a612011-07-23 12:50:34 -070084 DexCache* FindDexCache(const DexFile* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070085
Brian Carlstromf615a612011-07-23 12:50:34 -070086 void AppendToBootClassPath(DexFile* dex_file);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070087
Brian Carlstromf615a612011-07-23 12:50:34 -070088 void LoadClass(const DexFile& dex_file,
89 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -070090 Class* klass,
91 ClassLoader* class_loader);
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070092
Brian Carlstromf615a612011-07-23 12:50:34 -070093 void LoadInterfaces(const DexFile& dex_file,
94 const DexFile::ClassDef& dex_class_def,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070095 Class *klass);
96
Brian Carlstromf615a612011-07-23 12:50:34 -070097 void LoadField(const DexFile& dex_file,
98 const DexFile::Field& dex_field,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070099 Class* klass,
100 Field* dst);
101
Brian Carlstromf615a612011-07-23 12:50:34 -0700102 void LoadMethod(const DexFile& dex_file,
103 const DexFile::Method& dex_method,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700104 Class* klass,
105 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -0700106
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700107 // Inserts a class into the class table. Returns true if the class
108 // was inserted.
109 bool InsertClass(Class* klass);
110
111 bool InitializeSuperClass(Class* klass);
112
113 void InitializeStaticFields(Class* klass);
114
115 bool ValidateSuperClassDescriptors(const Class* klass);
116
117 bool HasSameDescriptorClasses(const char* descriptor,
118 const Class* klass1,
119 const Class* klass2);
120
121 bool HasSameMethodDescriptorClasses(const Method* descriptor,
122 const Class* klass1,
123 const Class* klass2);
124
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700125 bool LinkClass(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700126
127 bool LinkSuperClass(Class* klass);
128
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700129 bool LoadSuperAndInterfaces(Class* klass, const DexFile& dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700130
131 bool LinkMethods(Class* klass);
132
133 bool LinkVirtualMethods(Class* klass);
134
135 bool LinkInterfaceMethods(Class* klass);
136
137 void LinkAbstractMethods(Class* klass);
138
139 bool LinkInstanceFields(Class* klass);
140
141 void CreateReferenceOffsets(Class* klass);
142
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700143 std::vector<const DexFile*> boot_class_path_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700144
Brian Carlstrom4a96b602011-07-26 16:40:23 -0700145 std::vector<const DexFile*> dex_files_;
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700146
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700147 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700148
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700149 // multimap from String::descriptor_ to Class* instances. Results
150 // should be compared for a matching Class::descriptor_ and
151 // Class::class_loader_.
Brian Carlstromb0460ea2011-07-29 10:08:05 -0700152 // TODO: unordered_multimap
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700153 typedef std::multimap<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700154
155 Table classes_;
156
157 Mutex* classes_lock_;
158
159 // TODO: classpath
160
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700161 // indexes into class_roots_
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700162 enum ClassRoot {
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700163 kJavaLangClass,
164 kJavaLangObject,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700165 kObjectArrayClass,
166 kJavaLangString,
167 kCharArrayClass,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700168 kJavaLangReflectField,
169 kJavaLangReflectMethod,
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700170 kJavaLangClassLoader,
171 kDalvikSystemBaseDexClassLoader,
172 kDalvikSystemPathClassLoader,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700173 kPrimitiveBoolean,
174 kPrimitiveChar,
175 kPrimitiveFloat,
176 kPrimitiveDouble,
177 kPrimitiveByte,
178 kPrimitiveShort,
179 kPrimitiveInt,
180 kPrimitiveLong,
181 kPrimitiveVoid,
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700182 kClassRootsMax,
183 };
184 ObjectArray<Class>* class_roots_;
Brian Carlstrom913af1b2011-07-23 21:41:13 -0700185
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700186 Class* GetClassRoot(ClassRoot class_root) {
187 Class* klass = class_roots_->Get(class_root);
188 DCHECK(klass != NULL);
189 return klass;
190 }
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 Carlstrom75cb3b42011-07-28 02:13:36 -0700195 bool init_done_;
196
Brian Carlstrom74eb46a2011-08-02 20:10:14 -0700197 friend class RuntimeTest;
Brian Carlstrom75cb3b42011-07-28 02:13:36 -0700198 FRIEND_TEST(DexCacheTest, Open);
199 friend class ObjectTest;
200 FRIEND_TEST(ObjectTest, AllocObjectArray);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700201 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
202};
203
204} // namespace art
205
206#endif // ART_SRC_CLASS_LINKER_H_