Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Merge Requests
0
Merge Requests
0
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Commits
Open sidebar
nexedi
cython
Commits
17e2f2c5
Commit
17e2f2c5
authored
Aug 24, 2016
by
Robert Bradshaw
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add support for typedefs in C++ classes.
parent
8b642621
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
31 additions
and
10 deletions
+31
-10
Parsing.py
Cython/Compiler/Parsing.py
+2
-0
PyrexTypes.py
Cython/Compiler/PyrexTypes.py
+8
-5
Symtab.py
Cython/Compiler/Symtab.py
+7
-2
cpp_nested_classes.pyx
tests/run/cpp_nested_classes.pyx
+10
-3
cpp_nested_classes_support.h
tests/run/cpp_nested_classes_support.h
+4
-0
No files found.
Cython/Compiler/Parsing.py
View file @
17e2f2c5
...
...
@@ -3625,6 +3625,8 @@ def p_cpp_class_attribute(s, ctx):
decorators
=
p_decorators
(
s
)
if
s
.
systring
==
'cppclass'
:
return
p_cpp_class_definition
(
s
,
s
.
position
(),
ctx
)
if
s
.
systring
==
'ctypedef'
:
return
p_ctypedef_statement
(
s
,
ctx
)
else
:
node
=
p_c_func_or_var_declaration
(
s
,
s
.
position
(),
ctx
)
if
decorators
is
not
None
:
...
...
Cython/Compiler/PyrexTypes.py
View file @
17e2f2c5
...
...
@@ -316,7 +316,7 @@ def public_decl(base_code, dll_linkage):
else
:
return
base_code
def
create_typedef_type
(
name
,
base_type
,
cname
,
is_external
=
0
):
def
create_typedef_type
(
name
,
base_type
,
cname
,
is_external
=
0
,
namespace
=
None
):
is_fused
=
base_type
.
is_fused
if
base_type
.
is_complex
or
is_fused
:
if
is_external
:
...
...
@@ -329,7 +329,7 @@ def create_typedef_type(name, base_type, cname, is_external=0):
return
base_type
else
:
return
CTypedefType
(
name
,
base_type
,
cname
,
is_external
)
return
CTypedefType
(
name
,
base_type
,
cname
,
is_external
,
namespace
)
class
CTypedefType
(
BaseType
):
...
...
@@ -353,12 +353,13 @@ class CTypedefType(BaseType):
subtypes
=
[
'typedef_base_type'
]
def
__init__
(
self
,
name
,
base_type
,
cname
,
is_external
=
0
):
def
__init__
(
self
,
name
,
base_type
,
cname
,
is_external
=
0
,
namespace
=
None
):
assert
not
base_type
.
is_complex
self
.
typedef_name
=
name
self
.
typedef_cname
=
cname
self
.
typedef_base_type
=
base_type
self
.
typedef_is_external
=
is_external
self
.
typedef_namespace
=
namespace
def
invalid_value
(
self
):
return
self
.
typedef_base_type
.
invalid_value
()
...
...
@@ -372,6 +373,8 @@ class CTypedefType(BaseType):
base_code
=
self
.
typedef_name
else
:
base_code
=
public_decl
(
self
.
typedef_cname
,
dll_linkage
)
if
self
.
typedef_namespace
is
not
None
and
not
pyrex
:
base_code
=
"
%
s::
%
s"
%
(
self
.
typedef_namespace
.
empty_declaration_code
(),
base_code
)
return
self
.
base_declaration_code
(
base_code
,
entity_code
)
def
as_argument_type
(
self
):
...
...
@@ -482,8 +485,8 @@ class CTypedefType(BaseType):
def
error_condition
(
self
,
result_code
):
if
self
.
typedef_is_external
:
if
self
.
exception_value
:
condition
=
"(
%
s ==
(
%
s)
%
s)"
%
(
result_code
,
self
.
typedef_cname
,
self
.
exception_value
)
condition
=
"(
%
s ==
%
s)"
%
(
result_code
,
self
.
cast_code
(
self
.
exception_value
)
)
if
self
.
exception_check
:
condition
+=
" && PyErr_Occurred()"
return
condition
...
...
Cython/Compiler/Symtab.py
View file @
17e2f2c5
...
...
@@ -495,13 +495,18 @@ class Scope(object):
def
declare_typedef
(
self
,
name
,
base_type
,
pos
,
cname
=
None
,
visibility
=
'private'
,
api
=
0
):
if
not
cname
:
if
self
.
in_cinclude
or
(
visibility
==
'public
'
or
api
):
if
self
.
in_cinclude
or
(
visibility
!=
'private
'
or
api
):
cname
=
name
else
:
cname
=
self
.
mangle
(
Naming
.
type_prefix
,
name
)
try
:
if
self
.
is_cpp_class_scope
:
namespace
=
self
.
outer_scope
.
lookup
(
self
.
name
)
.
type
else
:
namespace
=
None
type
=
PyrexTypes
.
create_typedef_type
(
name
,
base_type
,
cname
,
(
visibility
==
'extern'
))
(
visibility
==
'extern'
),
namespace
)
except
ValueError
as
e
:
error
(
pos
,
e
.
args
[
0
])
type
=
PyrexTypes
.
error_type
...
...
tests/run/cpp_nested_classes.pyx
View file @
17e2f2c5
...
...
@@ -7,17 +7,24 @@ cdef extern from "cpp_nested_classes_support.h":
cppclass C:
int cube(int)
B* createB()
ctypedef int my_int
@staticmethod
my_int negate(my_int)
def test():
def test
_nested_classes
():
"""
>>> test()
>>> test
_nested_classes
()
"""
cdef A a
cdef A.B b
assert b.square(3) == 9
cdef A.B.C c
assert c.cube(3) == 27
cdef A.B *b_ptr = a.createB()
assert b_ptr.square(4) == 16
del b_ptr
def test_nested_typedef(py_x):
cdef A.my_int x = py_x
assert A.negate(x) == -py_x
tests/run/cpp_nested_classes_support.h
View file @
17e2f2c5
...
...
@@ -11,4 +11,8 @@ public:
B
*
createB
()
{
return
new
B
();
}
typedef
int
my_int
;
static
my_int
negate
(
my_int
x
)
{
return
-
x
;
}
};
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment