Fixed some C/C++ compiler errors due to stricter checks.
[rubinius.git] / machine / type_info.hpp
blobeb82662f15fe1500b742d0308d22998d850b8574
1 #ifndef RBX_VM_TYPE_INFO_HPP
2 #define RBX_VM_TYPE_INFO_HPP
4 #include <stdlib.h>
5 #include <functional>
6 #include <map>
7 #include <stdexcept>
8 #include <vector>
9 #include <string>
11 #include "object_types.hpp"
12 #include "defines.hpp"
13 #include "executor.hpp"
15 namespace rubinius {
17 class Class;
18 class Object;
19 class Memory;
20 class DataHeader;
21 class ObjectHeader;
23 /**
24 * Static type information for the VM.
26 * Due to memory layout restrictions, virtual methods cannot exist
27 * on builtin types. This class abstracts those operations out of
28 * the classes themselves. Using virtual dispatch here allows the
29 * correct method implementation to be invoked, based on the object's
30 * obj_type field.
32 * @see doc/builtin_internal_resource_releasing.txt
33 * @see machine/object_types.hpp
34 * @see builtin/object.hpp
36 class TypeInfo {
37 public: // Types
39 typedef std::map<intptr_t, long> Slots;
40 typedef std::vector<object_type> SlotTypes;
41 typedef std::vector<executor> AccessorPrimitives;
42 typedef std::vector<uintptr_t> SlotLocations;
44 public:
46 size_t instance_size;
47 static size_t instance_sizes[(int)LastObjectType];
48 Slots slots;
49 SlotTypes slot_types;
50 AccessorPrimitives slot_accessors;
51 SlotLocations slot_locations;
52 std::string type_name;
53 object_type type;
54 bool allow_user_allocate;
56 public: /* Class initializers */
58 // TODO: Can these be reworked to be self-contained?
59 static void init(Memory* memory);
60 static void auto_init(Memory* memory);
61 static void auto_learn_fields(STATE);
63 virtual void auto_mark(STATE, Object* obj, std::function<void (STATE, Object**)> f) = 0;
64 virtual void update_weakref(STATE, Object* obj) {}
65 virtual void before_visit(STATE, Object* o, std::function<void (STATE, Object**)> f) {}
66 virtual void after_visit(STATE, Object* o, std::function<void (STATE, Object**)> f) {}
67 virtual void visit(STATE, Object* o, std::function<void (STATE, Object**)> f) {
68 before_visit(state, o, f);
69 after_visit(state, o, f);
72 public: /* Ctors */
74 /**
75 * Make a new TypeInfo.
77 * To support TypeInfo hierarchies where some classes may
78 * and some may not need e.g. a cleanup, the instantiation
79 * will set the cleanup flag if _any_ of the classes in
80 * the chain request it.
82 TypeInfo(object_type type);
84 virtual ~TypeInfo();
86 public: /* Interface */
88 virtual void mark(STATE, Object* obj, std::function<void (STATE, Object**)> f);
90 virtual void set_field(STATE, Object* target, size_t index, Object* val);
91 virtual Object* get_field(STATE, Object* target, size_t index);
93 virtual void populate_slot_locations() { }
95 /**
96 * Objects whose size is static generate a entry in the types data
97 * structure for that size, which is accessed directly via the data
98 * structure instead of via a method call. Objects whose size is defined
99 * at runtime must implement this method.
101 virtual size_t object_size(const ObjectHeader* obj) {
102 ::abort();
105 virtual size_t object_size(const DataHeader* obj) {
106 ::abort();
110 * Currently prints the same output as show_simple. Is specialized by
111 * complex classes to e.g. limit the recursion into nested
112 * objects to make the output more manageable. See e.g. Tuple
113 * and CompiledCode. Immediates and numeric classes print
114 * their value for both show and show_simple.
116 virtual void show(STATE, Object* self, int level);
119 * Default output for any object. Prints just the class name
120 * and address. It is expected that classes will define their own
121 * show output.
123 virtual void show_simple(STATE, Object* self, int level);
126 * Prints spaces to indent the following text to the requested
127 * level. Levels are specified as 0, 1, 2, ... and the multiplier
128 * is 2. So, text at level 2 will have 2 * 2 = 4 spaces in front.
130 virtual void indent(int level);
133 * Indents the attribute name to the requested level. @see indent.
135 virtual void indent_attribute(int level, const char* name);
138 * Prints out the class name and address. Used for simple classes
139 * that may append other info on the same line.
141 * #<SomeClass:0x346882
143 virtual void class_info(STATE, const Object* self, bool newline = false);
146 * Prints out the class name and address followed by a newline. Used
147 * for complex classes that will print additional member info.
149 * #<SomeClass:0x3287648\n
151 virtual void class_header(STATE, const Object* self);
154 * Prints "..." + endl at the requested indent level.
156 virtual void ellipsis(int level);
159 * Indents to level-1 and prints ">" + endl.
161 virtual void close_body(int level);
168 #define BASIC_TYPEINFO(super) \
169 Info(object_type type) : super(type) { } \
170 virtual void auto_mark(STATE, Object* obj, std::function<void (STATE, Object**)> f); \
171 virtual void set_field(STATE, Object* target, size_t index, Object* val); \
172 virtual Object* get_field(STATE, Object* target, size_t index); \
173 virtual void populate_slot_locations(); \
174 virtual void before_visit(STATE, Object* o, std::function<void (STATE, Object**)> f) {} \
175 virtual void after_visit(STATE, Object* o, std::function<void (STATE, Object**)> f) {} \
176 virtual void visit(STATE, Object* o, std::function<void (STATE, Object**)> f); \
178 #endif