Commit 5d9787ee authored by Robert Bradshaw's avatar Robert Bradshaw

Type inference for stack-allocated C++ types.

parent 090f9ba2
......@@ -5362,6 +5362,9 @@ class CallNode(ExprNode):
return PyrexTypes.c_double_type
elif function.entry.name in Builtin.types_that_construct_their_instance:
return result_type
func_type = self.function.analyse_as_type(env)
if func_type.is_struct_or_union or func_type.is_cpp_class:
return func_type
return py_object_type
def type_dependencies(self, env):
......
......@@ -12,6 +12,11 @@ cdef extern from "shapes.h" namespace "shapes":
cdef cppclass Square(Shape):
Square(int)
cdef cppclass Empty(Shape):
Empty()
cdef Empty make_Empty "shapes::Empty"()
from cython cimport typeof
from cython.operator cimport dereference as d
......@@ -48,3 +53,35 @@ def test_derived_types(int size, bint round):
ptr = new Square(size)
print typeof(ptr)
del ptr
def test_stack_allocated(bint b=True):
"""
>>> test_stack_allocated()
"""
e1 = Empty()
e2 = Empty()
e = e1 if b else e2
assert typeof(e1) == "Empty", typeof(e1)
assert typeof(e2) == "Empty", typeof(e2)
assert typeof(e) == "Empty", typeof(e)
cdef extern from *:
"""
template <typename T>
struct MyTemplate {};
"""
cdef cppclass MyTemplate[T]:
MyTemplate()
def test_template_types():
"""
>>> test_template_types()
"""
t_stack = MyTemplate[int]()
assert typeof(t_stack) == "MyTemplate[int]", typeof(t_stack)
t_ptr = new MyTemplate[double]()
try:
assert typeof(t_ptr) == "MyTemplate[double] *", typeof(t_ptr)
finally:
del t_ptr
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment