Skip to content

Commit a8fbc2d

Browse files
author
Ary Borenszweig
committedDec 14, 2016
Compiler: add primitive to get a class' instance type id
Useful when overriding allocate to correctly set the type id
1 parent ddc61d7 commit a8fbc2d

File tree

5 files changed

+23
-1
lines changed

5 files changed

+23
-1
lines changed
 

‎src/compiler/crystal/codegen/primitives.cr

+6
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ class Crystal::CodeGenVisitor
4747
codegen_primitive_object_id node, target_def, call_args
4848
when "object_crystal_type_id"
4949
codegen_primitive_object_crystal_type_id node, target_def, call_args
50+
when "class_crystal_instance_type_id"
51+
codegen_primitive_class_crystal_instance_type_id node, target_def, call_args
5052
when "symbol_to_s"
5153
codegen_primitive_symbol_to_s node, target_def, call_args
5254
when "class"
@@ -566,6 +568,10 @@ class Crystal::CodeGenVisitor
566568
end
567569
end
568570

571+
def codegen_primitive_class_crystal_instance_type_id(node, target_def, call_args)
572+
type_id(context.type.instance_type)
573+
end
574+
569575
def codegen_primitive_symbol_to_s(node, target_def, call_args)
570576
load(gep @llvm_mod.globals[SYMBOL_TABLE_NAME], int(0), call_args[0])
571577
end

‎src/compiler/crystal/semantic/main_visitor.cr

+2
Original file line numberDiff line numberDiff line change
@@ -2175,6 +2175,8 @@ module Crystal
21752175
node.type = program.uint64
21762176
when "object_crystal_type_id"
21772177
node.type = program.int32
2178+
when "class_crystal_instance_type_id"
2179+
node.type = program.int32
21782180
when "symbol_to_s"
21792181
node.type = program.string
21802182
when "class"

‎src/object.cr

+5
Original file line numberDiff line numberDiff line change
@@ -1144,4 +1144,9 @@ class Object
11441144
\{% end %}
11451145
end
11461146
end
1147+
1148+
protected def self.set_crystal_type_id(ptr)
1149+
ptr.as(LibC::SizeT*).value = LibC::SizeT.new(crystal_instance_type_id)
1150+
ptr
1151+
end
11471152
end

‎src/primitives.cr

+6
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class Reference
5151
end
5252
end
5353

54+
class Class
55+
@[Primitive(:class_crystal_instance_type_id)]
56+
def crystal_instance_type_id
57+
end
58+
end
59+
5460
struct Bool
5561
# Returns true if *self* is equal to *other*.
5662
@[Primitive(:binary)]

‎src/weak_ref.cr

+4-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,10 @@ class WeakRef(T)
99
end
1010

1111
def self.allocate
12-
GC.malloc_atomic(sizeof(self)).as(self)
12+
ptr = GC.malloc_atomic(sizeof(self)).as(self)
13+
# TODO: uncomment after 0.20.1
14+
# set_crystal_type_id(ptr)
15+
ptr
1316
end
1417

1518
# Returns the referenced object or `Nil` if it has been garbage-collected.

1 commit comments

Comments
 (1)

asterite commented on Dec 14, 2016

@asterite
Member

@BlaXpirit I finally decided to do it like this, so when overriding allocate you'll have to call set_crystal_type_id(allocate_memory_here). It's just a bit more of work, but overriding allocate shouldn't be that frequently needed.

Please sign in to comment.