blob: 9d6c53e7d0b191cc7878c50343a56f31fcc667ce [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 Carlstrom578bbdc2011-07-21 14:07:47 -070010#include "macros.h"
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070011#include "dex_file.h"
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070012#include "thread.h"
13#include "object.h"
14#include "gtest/gtest.h"
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070015
16namespace art {
17
18class ClassLinker {
19 public:
Carl Shapiro565f5072011-07-10 13:39:43 -070020 // Initializes the class linker.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070021 static ClassLinker* Create(std::vector<RawDexFile*> boot_class_path);
Carl Shapiro61e019d2011-07-14 16:53:09 -070022
23 ~ClassLinker() {}
Carl Shapiro565f5072011-07-10 13:39:43 -070024
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070025 DexCache* AllocDexCache();
26 Class* AllocClass(DexCache* dex_cache);
Brian Carlstroma0808032011-07-18 00:39:23 -070027 StaticField* AllocStaticField();
28 InstanceField* AllocInstanceField();
29 Method* AllocMethod();
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070030 ObjectArray* AllocObjectArray(size_t length);
Brian Carlstroma0808032011-07-18 00:39:23 -070031
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070032 // Finds a class by its descriptor name.
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070033 // If raw_dex_file is null, searches boot_class_path_.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070034 Class* FindClass(const StringPiece& descriptor,
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070035 Object* class_loader,
36 const RawDexFile* raw_dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070037
Brian Carlstrom6cc18452011-07-18 15:10:33 -070038 Class* FindSystemClass(const StringPiece& descriptor) {
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070039 return FindClass(descriptor, NULL, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070040 }
41
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070042 bool InitializeClass(Class* klass);
43
Brian Carlstrom6cc18452011-07-18 15:10:33 -070044 Class* LookupClass(const StringPiece& descriptor, Object* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070045
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070046 Class* ResolveClass(const Class* referring,
47 uint32_t class_idx,
48 const RawDexFile* raw_dex_file);
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070049
50 String* ResolveString(const Class* referring, uint32_t string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070051
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070052 void RegisterDexFile(RawDexFile* raw_dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
54 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070055 ClassLinker() {}
56
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070057 void Init(std::vector<RawDexFile*> boot_class_path_);
Carl Shapiro61e019d2011-07-14 16:53:09 -070058
Brian Carlstroma331b3c2011-07-18 17:47:56 -070059 Class* CreatePrimitiveClass(const StringPiece& descriptor);
60
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070061 Class* CreateArrayClass(const StringPiece& descriptor,
62 Object* class_loader,
63 const RawDexFile* raw_dex_file);
Brian Carlstroma331b3c2011-07-18 17:47:56 -070064
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070065 Class* FindPrimitiveClass(char type);
Carl Shapiro565f5072011-07-10 13:39:43 -070066
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070067 const RawDexFile* FindRawDexFile(const DexCache* dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070068
Brian Carlstrom7e49dca2011-07-22 18:07:34 -070069 DexCache* FindDexCache(const RawDexFile* raw_dex_file) const;
Brian Carlstrom934486c2011-07-12 23:42:50 -070070
Brian Carlstrom578bbdc2011-07-21 14:07:47 -070071 typedef std::pair<const RawDexFile*, const RawDexFile::ClassDef*> ClassPathEntry;
72
73 void AppendToBootClassPath(RawDexFile* raw_dex_file);
74
75 ClassPathEntry FindInBootClassPath(const StringPiece& descriptor);
76
77 void LoadClass(const RawDexFile& raw_dex_file,
78 const RawDexFile::ClassDef& class_def,
79 Class* klass);
80
81 void LoadInterfaces(const RawDexFile& raw_dex_file,
82 const RawDexFile::ClassDef& class_def,
83 Class *klass);
84
85 void LoadField(const RawDexFile& raw_dex_file,
86 const RawDexFile::Field& src,
87 Class* klass,
88 Field* dst);
89
90 void LoadMethod(const RawDexFile& raw_dex_file,
91 const RawDexFile::Method& src,
92 Class* klass,
93 Method* dst);
Brian Carlstrom934486c2011-07-12 23:42:50 -070094
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070095 // Inserts a class into the class table. Returns true if the class
96 // was inserted.
97 bool InsertClass(Class* klass);
98
99 bool InitializeSuperClass(Class* klass);
100
101 void InitializeStaticFields(Class* klass);
102
103 bool ValidateSuperClassDescriptors(const Class* klass);
104
105 bool HasSameDescriptorClasses(const char* descriptor,
106 const Class* klass1,
107 const Class* klass2);
108
109 bool HasSameMethodDescriptorClasses(const Method* descriptor,
110 const Class* klass1,
111 const Class* klass2);
112
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700113 bool HasSameNameAndPrototype(const Method* m1, const Method* m2) const {
114 return HasSameName(m1, m2) && HasSamePrototype(m1, m2);
115 }
116
117 bool HasSameName(const Method* m1, const Method* m2) const {
118 return m1->GetName() == m2->GetName();
119 }
120
121 bool HasSamePrototype(const Method* m1, const Method* m2) const {
122 return HasSameReturnType(m1, m2) && HasSameArgumentTypes(m1, m2);
123 }
124
125 bool HasSameReturnType(const Method* m1, const Method* m2) const;
126
127 bool HasSameArgumentTypes(const Method* m1, const Method* m2) const;
128
129 bool LinkClass(Class* klass, const RawDexFile* raw_dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700130
131 bool LinkSuperClass(Class* klass);
132
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700133 bool LinkInterfaces(Class* klass, const RawDexFile* raw_dex_file);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700134
135 bool LinkMethods(Class* klass);
136
137 bool LinkVirtualMethods(Class* klass);
138
139 bool LinkInterfaceMethods(Class* klass);
140
141 void LinkAbstractMethods(Class* klass);
142
143 bool LinkInstanceFields(Class* klass);
144
145 void CreateReferenceOffsets(Class* klass);
146
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700147 std::vector<RawDexFile*> boot_class_path_;
148
149 std::vector<RawDexFile*> raw_dex_files_;
150
Brian Carlstrom7e49dca2011-07-22 18:07:34 -0700151 std::vector<DexCache*> dex_caches_;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700152
153 // TODO: multimap
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700154 typedef std::map<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700155
156 Table classes_;
157
158 Mutex* classes_lock_;
159
160 // TODO: classpath
161
Brian Carlstroma331b3c2011-07-18 17:47:56 -0700162 Class* java_lang_Class_;
163 Class* java_lang_Object_;
164 Class* java_lang_ref_Field_;
165 Class* java_lang_ref_Method_;
166 Class* java_lang_Cloneable_;
167 Class* java_io_Serializable_;
168 Class* java_lang_String_;
169
Carl Shapiro565f5072011-07-10 13:39:43 -0700170 Class* primitive_boolean_;
171 Class* primitive_char_;
172 Class* primitive_float_;
173 Class* primitive_double_;
174 Class* primitive_byte_;
175 Class* primitive_short_;
176 Class* primitive_int_;
177 Class* primitive_long_;
178 Class* primitive_void_;
179
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700180 Class* object_array_class_;
Brian Carlstroma0808032011-07-18 00:39:23 -0700181 Class* char_array_class_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700182
Brian Carlstrom578bbdc2011-07-21 14:07:47 -0700183 FRIEND_TEST(ClassLinkerTest, ProtoCompare);
184 FRIEND_TEST(ClassLinkerTest, ProtoCompare2);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700185 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
186};
187
188} // namespace art
189
190#endif // ART_SRC_CLASS_LINKER_H_