Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Labels
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Commits
Open sidebar
nexedi
cython
Commits
f401af05
Commit
f401af05
authored
May 26, 2020
by
Xavier Thompson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
WIP cypclass method wrapping
parent
fc49383c
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
54 additions
and
21 deletions
+54
-21
Cython/Compiler/Nodes.py
Cython/Compiler/Nodes.py
+54
-21
No files found.
Cython/Compiler/Nodes.py
View file @
f401af05
...
@@ -1636,52 +1636,85 @@ class CppClassNode(CStructOrUnionDefNode, BlockNode):
...
@@ -1636,52 +1636,85 @@ class CppClassNode(CStructOrUnionDefNode, BlockNode):
self
.
cyp_wrapper
.
body
.
stats
.
append
(
py_method_wrapper
)
self
.
cyp_wrapper
.
body
.
stats
.
append
(
py_method_wrapper
)
def
synthesize_cypclass_method_wrapper
(
self
,
cfunc_method
):
def
synthesize_cypclass_method_wrapper
(
self
,
cfunc_method
):
from
.CypclassWrapper
import
underlying_name
,
cycplass_special_entry_names
if
cfunc_method
.
is_static_method
:
if
cfunc_method
.
is_static_method
:
return
# for now skip static methods
return
# for now skip static methods
# C++ methods have an implict 'this', so the 'self' argument is skipped in the declarator
skipped_self
=
cfunc_method
.
cfunc_declarator
.
skipped_self
if
not
skipped_self
:
return
# if this ever happens (?), skip non-static methods without a self argument
from
.CypclassWrapper
import
underlying_name
,
cycplass_special_entry_names
from
.
import
ExprNodes
cfunc_declarator
=
cfunc_method
.
cfunc_declarator
cfunc_declarator
=
cfunc_method
.
cfunc_declarator
py_name
=
cfunc_method
.
entry
.
name
# transform e.g. <init> back into __init__
# > name of the wrapping method: same name
cfunc_name
=
cfunc_method
.
entry
.
name
py_name
=
cfunc_name
try
:
try
:
# transform e.g. <init> back into __init__
py_name
=
cycplass_special_entry_names
[
py_name
]
py_name
=
cycplass_special_entry_names
[
py_name
]
except
KeyError
:
except
KeyError
:
# no need to transform this name
pass
pass
py_args
=
[
arg
.
clone_node
()
for
arg
in
cfunc_declarator
.
args
]
# > self argument of the wrapper method: same name, but type of the wrapper cclass
self_name
,
self_type
,
self_pos
,
self_arg
=
skipped_self
py_doc
=
cfunc_method
.
doc
py_self_arg
=
CArgDeclNode
(
self_pos
,
base_type
=
CSimpleBaseTypeNode
(
self_pos
,
name
=
self
.
cyp_wrapper
.
class_name
,
module_path
=
[],
signed
=
1
,
is_basic_c_type
=
0
,
longness
=
0
,
is_self_arg
=
0
,
# only true for C methods
templates
=
None
),
declarator
=
CNameDeclaratorNode
(
self_pos
,
name
=
self_name
,
cname
=
None
),
not_none
=
0
,
or_none
=
0
,
default
=
None
,
annotation
=
None
,
kw_only
=
0
)
arg_names
=
[
arg
.
name
for
arg
in
py_args
]
# > all arguments of the wrapper method declaration
py_args
=
[
py_self_arg
]
for
arg
in
cfunc_declarator
.
args
:
py_args
.
append
(
arg
.
clone_node
())
# C++ methods have an implict 'this', so the 'self' argument is skipped in the declarator
# > same docstring
skipped_self
=
cfunc_method
.
cfunc_declarator
.
skipped_self
py_doc
=
cfunc_method
.
doc
if
not
skipped_self
:
return
# if this ever happens (?), skip non-static methods without a self argument
from
.
import
ExprNodes
# > names of the arguments passed when calling the underlying method; self not included
arg_objs
=
[
ExprNodes
.
NameNode
(
arg
.
pos
,
name
=
arg
.
name
)
for
arg
in
cfunc_declarator
.
args
]
# self_name, self_type, self_pos, self_arg = skipped_self
# > reference to the self argument of the wrapper method
type_entry
=
self
.
cyp_wrapper
.
entry
self_obj
=
ExprNodes
.
NameNode
(
self_pos
,
name
=
self_name
)
type_arg
=
ExprNodes
.
NameNode
(
self
.
pos
,
name
=
type_entry
.
name
)
type_arg
.
entry
=
type_entry
cfunc
=
ExprNodes
.
AttributeNode
(
cfunc_method
.
pos
,
obj
=
type_arg
,
attribute
=
underlying_name
)
# > access the method of the underlying cyobject from the self argument of the wrapper method
underlying_obj
=
ExprNodes
.
AttributeNode
(
cfunc_method
.
pos
,
obj
=
self_obj
,
attribute
=
underlying_name
)
cfunc
=
ExprNodes
.
AttributeNode
(
cfunc_method
.
pos
,
obj
=
underlying_obj
,
attribute
=
cfunc_name
)
# > call to the underlying method
c_call
=
ExprNodes
.
SimpleCallNode
(
c_call
=
ExprNodes
.
SimpleCallNode
(
cfunc_method
.
pos
,
cfunc_method
.
pos
,
function
=
cfunc
,
function
=
cfunc
,
args
=
[
ExprNodes
.
NameNode
(
cfunc_method
.
pos
,
name
=
n
)
for
n
in
arg_names
]
args
=
arg_objs
)
)
# > return the result of the call if the underlying return type is not void
if
cfunc_method
.
type
.
return_type
.
is_void
:
if
cfunc_method
.
type
.
return_type
.
is_void
:
py_stat
=
ExprStatNode
(
pos
=
cfunc_method
.
pos
,
expr
=
c_call
)
py_stat
=
ExprStatNode
(
pos
=
cfunc_method
.
pos
,
expr
=
c_call
)
else
:
else
:
py_stat
=
ReturnStatNode
(
pos
=
cfunc_method
.
pos
,
return_type
=
PyrexTypes
.
py_object_type
,
value
=
c_call
)
py_stat
=
ReturnStatNode
(
pos
=
cfunc_method
.
pos
,
return_type
=
PyrexTypes
.
py_object_type
,
value
=
c_call
)
py_body
=
StatListNode
(
cfunc_method
.
pos
,
stats
=
[
py_stat
])
py_body
=
StatListNode
(
cfunc_method
.
pos
,
stats
=
[
py_stat
])
# > the wrapper method
return
DefNode
(
return
DefNode
(
cfunc_method
.
pos
,
cfunc_method
.
pos
,
name
=
py_name
,
name
=
py_name
,
...
...
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