Skip to content

[mypyc] Tweak how tuple structs are defined #7630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 11 additions & 13 deletions mypyc/emit.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,11 @@ def native_function_name(self, fn: FuncDecl) -> str:
return '{}{}'.format(NATIVE_PREFIX, fn.cname(self.names))

def tuple_c_declaration(self, rtuple: RTuple) -> List[str]:
result = ['struct {} {{'.format(rtuple.struct_name)]
result = [
'#ifndef MYPYC_DECLARED_{}'.format(rtuple.struct_name),
'#define MYPYC_DECLARED_{}'.format(rtuple.struct_name),
'typedef struct {} {{'.format(rtuple.struct_name),
]
if len(rtuple.types) == 0: # empty tuple
# Empty tuples contain a flag so that they can still indicate
# error values.
Expand All @@ -162,7 +166,11 @@ def tuple_c_declaration(self, rtuple: RTuple) -> List[str]:
for typ in rtuple.types:
result.append('{}f{};'.format(self.ctype_spaced(typ), i))
i += 1
result.append('};')
result.append('}} {};'.format(rtuple.struct_name))
values = self.tuple_undefined_value_helper(rtuple)
result.append('static {} {} = {{ {} }};'.format(
self.ctype(rtuple), self.tuple_undefined_value(rtuple), ''.join(values)))
result.append('#endif')
result.append('')

return result
Expand All @@ -183,17 +191,7 @@ def tuple_undefined_check_cond(
tuple_expr_in_c, compare, c_type_compare_val(item_type))

def tuple_undefined_value(self, rtuple: RTuple) -> str:
context = self.context
id = rtuple.unique_id
name = 'tuple_undefined_' + id
if name not in context.declarations:
values = self.tuple_undefined_value_helper(rtuple)
var = 'struct {} {}'.format(rtuple.struct_name, name)
decl = '{};'.format(var)
init = '{} = {{ {} }};'.format(var, ''.join(values))
context.declarations[name] = HeaderDeclaration(
set([rtuple.struct_name]), [decl], [init])
return name
return 'tuple_undefined_' + rtuple.unique_id

def tuple_undefined_value_helper(self, rtuple: RTuple) -> List[str]:
res = []
Expand Down
2 changes: 1 addition & 1 deletion mypyc/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ def __init__(self, types: List[RType]) -> None:
self.unique_id = self.accept(TupleNameVisitor())
# Nominally the max c length is 31 chars, but I'm not honestly worried about this.
self.struct_name = 'tuple_{}'.format(self.unique_id)
self._ctype = 'struct {}'.format(self.struct_name)
self._ctype = '{}'.format(self.struct_name)

def accept(self, visitor: 'RTypeVisitor[T]') -> T:
return visitor.visit_rtuple(self)
Expand Down