forked from rubinius/rubinius
/
type_info.hpp
168 lines (132 loc) · 4.48 KB
/
type_info.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifndef RBX_VM_TYPE_INFO_HPP
#define RBX_VM_TYPE_INFO_HPP
#include <map>
#include <stdexcept>
#include <vector>
#include "object_types.hpp"
#include "prelude.hpp"
#include "executor.hpp"
namespace rubinius {
class Class;
class Object;
class ObjectMark;
class ObjectMemory;
class ObjectHeader;
class GCTokenImpl {
private:
int dummy;
};
typedef GCTokenImpl& GCToken;
/**
* Static type information for the VM.
*
* Due to memory layout restrictions, virtual methods cannot exist
* on builtin types. This class abstracts those operations out of
* the classes themselves. Using virtual dispatch here allows the
* correct method implementation to be invoked, based on the object's
* obj_type field.
*
* @see doc/builtin_internal_resource_releasing.txt
* @see vm/object_types.hpp
* @see builtin/object.hpp
*/
class TypeInfo {
public: // Types
typedef std::map<native_int, long> Slots;
typedef std::vector<executor> AccessorPrimitives;
typedef std::vector<uintptr_t> SlotLocations;
private: /* Instance vars */
VM* state_;
public:
size_t instance_size;
static size_t instance_sizes[(int)LastObjectType];
Slots slots;
AccessorPrimitives slot_accessors;
SlotLocations slot_locations;
object_type type;
std::string type_name;
bool allow_user_allocate;
public: /* Class initializers */
static void init(ObjectMemory* om);
static void auto_init(ObjectMemory* om);
static void auto_learn_fields(STATE);
virtual void auto_mark(Object* obj, ObjectMark& mark) = 0;
public: /* Ctors */
/**
* Make a new TypeInfo.
*
* To support TypeInfo hierarchies where some classes may
* and some may not need e.g. a cleanup, the instantiation
* will set the cleanup flag if _any_ of the classes in
* the chain request it.
*/
TypeInfo(object_type type);
virtual ~TypeInfo();
public: /* Interface */
void set_state(STATE);
VM* state() {
return state_;
}
virtual void mark(Object* obj, ObjectMark& mark);
virtual void set_field(STATE, Object* target, size_t index, Object* val);
virtual Object* get_field(STATE, Object* target, size_t index);
virtual void populate_slot_locations() { }
/**
* Slow case, should be called only if instance_size is zero
*/
virtual size_t object_size(const ObjectHeader * obj);
/**
* Currently prints the same output as show_simple. Is specialized by
* complex classes to e.g. limit the recursion into nested
* objects to make the output more manageable. See e.g. Tuple
* and CompiledMethod. Immediates and numeric classes print
* their value for both show and show_simple.
*/
virtual void show(STATE, Object* self, int level);
/**
* Default output for any object. Prints just the class name
* and address. It is expected that classes will define their own
* show output.
*/
virtual void show_simple(STATE, Object* self, int level);
/**
* Prints spaces to indent the following text to the requested
* level. Levels are specified as 0, 1, 2, ... and the multiplier
* is 2. So, text at level 2 will have 2 * 2 = 4 spaces in front.
*/
virtual void indent(int level);
/**
* Indents the attribute name to the requested level. @see indent.
*/
virtual void indent_attribute(int level, const char* name);
/**
* Prints out the class name and address. Used for simple classes
* that may append other info on the same line.
*
* #<SomeClass:0x346882
*/
virtual void class_info(STATE, const Object* self, bool newline = false);
/**
* Prints out the class name and address followed by a newline. Used
* for complex classes that will print additional member info.
*
* #<SomeClass:0x3287648\n
*/
virtual void class_header(STATE, const Object* self);
/**
* Prints "..." + endl at the requested indent level.
*/
virtual void ellipsis(int level);
/**
* Indents to level-1 and prints ">" + endl.
*/
virtual void close_body(int level);
};
}
#define BASIC_TYPEINFO(super) \
Info(object_type type) : super(type) { } \
virtual void auto_mark(Object* obj, ObjectMark& mark); \
virtual void set_field(STATE, Object* target, size_t index, Object* val); \
virtual Object* get_field(STATE, Object* target, size_t index); \
virtual void populate_slot_locations();
#endif