Skip to content
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

Fix issue #105 do not override the original incomplete array type siz… #106

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions ppci/lang/c/nodes/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,18 @@ def decay(self):
""" Decay into pointer type. """
return PointerType(self.element_type)

@property
def incomplete(self):
""" Test if this type is incomplete """
if self.size is None:
return True
return self.element_type.is_compound and self.element_type.incomplete

@property
def complete(self):
""" Test if this type is complete """
return not self.incomplete


class PointerType(IndexableType):
""" The famous pointer! """
Expand Down
10 changes: 9 additions & 1 deletion ppci/lang/c/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,22 @@ def parse_variable_declaration(self, decl_spec, declarator):
declarator.type_modifiers,
declarator.location,
)

self.semantics.register_declaration(variable)

# Handle the initial value:
if self.has_consumed("="):
initializer = self.parse_initializer(variable.typ)
self.semantics.on_variable_initialization(variable, initializer)

# Ensure that the variable type is complete
# TODO to avoid an error when computing the variable size in IR
# generation, we should detect there all type incompleteness
# struct/union, component type of arrays, etc.
if variable.typ.is_compound and variable.typ.incomplete:
self.error(
"Type of variable '{}' is incomplete".format(declarator.name),
declarator.location)

def parse_typedef(self, decl_spec, declarator):
""" Process typedefs """
self.typedefs.add(declarator.name)
Expand Down
19 changes: 15 additions & 4 deletions ppci/lang/c/semantics.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,30 @@ def on_variable_initialization(self, variable, expression):
if self.scope.is_definition(variable.name):
self.error("Invalid redefinition.", variable.location)

self.patch_size_from_initializer(variable.typ, expression)
variable.typ = self.patch_size_from_initializer(
variable.typ,
expression
)
variable.initial_value = expression

def patch_size_from_initializer(self, typ, initializer):
""" Fill array size from elements! """

""" Fill array type size from elements if the type size is unknown.
Return a new type if modified
"""
if typ.is_array and typ.size is None:
if isinstance(initializer, expressions.ArrayInitializer):
typ.size = len(initializer.values)
new_type = types.ArrayType(
typ.element_type,
len(initializer.values)
)
initializer.typ = new_type
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should not modify the type of the initializer here, right? It should already be good.

return new_type
else: # pragma: no cover
raise NotImplementedError(
"What else could be used to init an array?"
)
else:
return typ

def new_init_cursor(self):
return init.InitCursor(self.context)
Expand Down