blob: 7f85da576fc75c2d7c29208df26990b5926c13da [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
10#include "src/macros.h"
11#include "src/thread.h"
12#include "src/object.h"
13
14namespace art {
15
16class ClassLinker {
17 public:
Carl Shapiro565f5072011-07-10 13:39:43 -070018 // Initializes the class linker.
Carl Shapiro61e019d2011-07-14 16:53:09 -070019 static ClassLinker* Create();
20
21 ~ClassLinker() {}
Carl Shapiro565f5072011-07-10 13:39:43 -070022
Brian Carlstroma0808032011-07-18 00:39:23 -070023 Class* AllocClass(DexFile* dex_file);
24 StaticField* AllocStaticField();
25 InstanceField* AllocInstanceField();
26 Method* AllocMethod();
27
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070028 // Finds a class by its descriptor name.
Brian Carlstrom6cc18452011-07-18 15:10:33 -070029 Class* FindClass(const StringPiece& descriptor,
30 Object* class_loader);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070031
Brian Carlstrom6cc18452011-07-18 15:10:33 -070032 Class* FindSystemClass(const StringPiece& descriptor) {
33 return FindClass(descriptor, NULL);
Carl Shapiro565f5072011-07-10 13:39:43 -070034 }
35
Brian Carlstrom6cc18452011-07-18 15:10:33 -070036 bool LoadClass(const StringPiece& descriptor, Class* klass);
Brian Carlstrom934486c2011-07-12 23:42:50 -070037
38 bool LoadClass(const RawDexFile::ClassDef& class_def, Class* klass);
39
Carl Shapiro565f5072011-07-10 13:39:43 -070040 Class* FindPrimitiveClass(JType type);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070041
42 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
Carl Shapiro5fafe2b2011-07-09 15:34:41 -070046 Class* ResolveClass(const Class* referring, uint32_t class_idx);
47
48 String* ResolveString(const Class* referring, uint32_t string_idx);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070049
Brian Carlstroma0808032011-07-18 00:39:23 -070050 typedef std::pair<DexFile*, const RawDexFile::ClassDef*> ClassPathEntry;
51
Brian Carlstrom6cc18452011-07-18 15:10:33 -070052 ClassPathEntry FindInClassPath(const StringPiece& descriptor);
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070053
54 void AppendToClassPath(DexFile* dex_file);
55
56 private:
Carl Shapiro61e019d2011-07-14 16:53:09 -070057 ClassLinker() {}
58
59 void Init();
60
Carl Shapiro565f5072011-07-10 13:39:43 -070061 Class* CreatePrimitiveClass(JType type, const char* descriptor);
62
Brian Carlstrom934486c2011-07-12 23:42:50 -070063 void LoadInterfaces(const RawDexFile::ClassDef& class_def, Class *klass);
64
65 void LoadField(Class* klass, const RawDexFile::Field& src, Field* dst);
66
67 void LoadMethod(Class* klass, const RawDexFile::Method& src, Method* dst);
68
Carl Shapiro0e5d75d2011-07-06 18:28:37 -070069 // Inserts a class into the class table. Returns true if the class
70 // was inserted.
71 bool InsertClass(Class* klass);
72
73 bool InitializeSuperClass(Class* klass);
74
75 void InitializeStaticFields(Class* klass);
76
77 bool ValidateSuperClassDescriptors(const Class* klass);
78
79 bool HasSameDescriptorClasses(const char* descriptor,
80 const Class* klass1,
81 const Class* klass2);
82
83 bool HasSameMethodDescriptorClasses(const Method* descriptor,
84 const Class* klass1,
85 const Class* klass2);
86
87 bool LinkClass(Class* klass);
88
89 bool LinkSuperClass(Class* klass);
90
91 bool LinkInterfaces(Class* klass);
92
93 bool LinkMethods(Class* klass);
94
95 bool LinkVirtualMethods(Class* klass);
96
97 bool LinkInterfaceMethods(Class* klass);
98
99 void LinkAbstractMethods(Class* klass);
100
101 bool LinkInstanceFields(Class* klass);
102
103 void CreateReferenceOffsets(Class* klass);
104
105 std::vector<DexFile*> class_path_;
106
107 // TODO: multimap
Brian Carlstrom6cc18452011-07-18 15:10:33 -0700108 typedef std::map<const StringPiece, Class*> Table;
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700109
110 Table classes_;
111
112 Mutex* classes_lock_;
113
114 // TODO: classpath
115
Carl Shapiro565f5072011-07-10 13:39:43 -0700116 Class* primitive_boolean_;
117 Class* primitive_char_;
118 Class* primitive_float_;
119 Class* primitive_double_;
120 Class* primitive_byte_;
121 Class* primitive_short_;
122 Class* primitive_int_;
123 Class* primitive_long_;
124 Class* primitive_void_;
125
126 Class* java_lang_Class_;
Brian Carlstroma0808032011-07-18 00:39:23 -0700127 Class* java_lang_Object_;
128 Class* java_lang_ref_Field_;
129 Class* java_lang_ref_Method_;
130 Class* java_lang_String_;
131 Class* char_array_class_;
Carl Shapiro565f5072011-07-10 13:39:43 -0700132
Carl Shapiro0e5d75d2011-07-06 18:28:37 -0700133 DISALLOW_COPY_AND_ASSIGN(ClassLinker);
134};
135
136} // namespace art
137
138#endif // ART_SRC_CLASS_LINKER_H_