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

Add support for enums and attributes #13

Draft
wants to merge 5 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 3 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
69 changes: 49 additions & 20 deletions src/cfile/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class DataType(Element):
"""
Base class for all data types
"""
def __init__(self, name: str | None) -> None:
def __init__(self, name: str = "") -> None:
Copy link
Owner

Choose a reason for hiding this comment

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

Why change this? It's off topic

Copy link
Author

Choose a reason for hiding this comment

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

Less complicated API. Because what is difference between "" or None? You are checking for None but not for "". So it is simpler and easier to check for "" then for None and ""

Copy link
Author

Choose a reason for hiding this comment

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

So based on what you said bellow i understand why you are using None, but still i think it is redundant.

self.name = name


Expand All @@ -150,7 +150,7 @@ def __init__(self,
pointer: bool = False,
volatile: bool = False,
array: int | None = None) -> None: # Only used for typedefs to other array types
super().__init__(None)
super().__init__("")
self.base_type = base_type
self.const = const
self.volatile = volatile
Expand All @@ -168,6 +168,45 @@ def qualifier(self, name) -> bool:
else:
raise KeyError(name)

class EnumMember(Element):
Copy link
Owner

Choose a reason for hiding this comment

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

This should be named EnumValue

Copy link
Author

Choose a reason for hiding this comment

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

Actually not. It is like StructMember. This is also member of Enum type, which has name and value

Copy link
Author

Choose a reason for hiding this comment

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

Another point of view:
If this should be EnumValue, then StructMember should be StructVariable.

Copy link
Owner

Choose a reason for hiding this comment

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

How about StructElement?

Copy link
Author

Choose a reason for hiding this comment

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

Well it is up to you. But it should be usable for both cases. I am coauthor of eRPC project (similar to this) and at the beginning i was lead by USA architect who decided to use StructMember and EnumMember. Sounds good to me.

https://github.com/EmbeddedRPC/erpc/tree/develop/erpcgen/src/types

Copy link
Author

Choose a reason for hiding this comment

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

Could be also StructItem and EnumItem

"""
Enum element.
"""

def __init__(self,
name: str,
value: int | None) -> None:
self.name = name
self.value = value


class Enum(DataType):
"""
A enum definition
"""

def __init__(self, name: str, members: list[EnumMember] = [], attributes: list[str] = []) -> None:
Copy link
Owner

Choose a reason for hiding this comment

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

You must never use empty list as default value in functions. It's dangerous.

Copy link
Author

Choose a reason for hiding this comment

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

Why? It is valid case, where you have no members.

Copy link
Owner

Choose a reason for hiding this comment

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

t's a very common mistake among newcomers to Python. Google it, there are great explanations on the internet.

Copy link
Author

Choose a reason for hiding this comment

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

That just sad.
Ok we can use None. But i wouldn't use one membert type as an option.
so list[EnumMember] | None = None
in a code bellow:
self.members = list(working_list) if working_list else []

Copy link
Owner

Choose a reason for hiding this comment

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

It's safer to do a proper check against None.

def __init__(self, name: str, elements:  list[EnumElement] | None = None):
self.elements: list[EnumElement] = []

if elements is not None:
    self.elements = list(elements)

These days however I always take it one step further and double-check that each individual element in the given argument list is of expected type. I usually do this by calling a helper function (usually called append or append_<something> that does the actual check.

super().__init__(name)
if isinstance(members, list):
self.members: list[EnumMember] = members.copy()
enumValue = -1
for enum in self.members:
if enum.value == None:
enumValue += 1
enum.value = enumValue
else:
enumValue = enum.value
else:
raise TypeError('Invalid argument type for "elements"')
self.attributes = attributes.copy()

def append(self, member: EnumMember) -> None:
"""
Appends new element to the struct definition
"""
if not isinstance(member, EnumMember):
raise TypeError(f'Invalid type, expected "EnumMember", got {str(type(member))}')
self.members.append(member)

class StructMember(Element):
"""
Expand Down Expand Up @@ -197,17 +236,13 @@ class Struct(DataType):
"""
A struct definition
"""
def __init__(self, name: str | None, members: StructMember | list[StructMember] | None = None) -> None:
def __init__(self, name: str = "", members: list[StructMember] = [], attributes: list[str] = []) -> None:
Copy link
Owner

Choose a reason for hiding this comment

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

Never use empty list as initializer.
Introducing attributes here needs some further design considerations. Attributes are compiler-specific for GCC. What if we are using MSVC? We should introduce the attribute mechanism separately.

Copy link
Author

Choose a reason for hiding this comment

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

I agree, i just need this one so i implemented them this way. But i was thinking to have something like cfiles attribute class

Copy link
Author

Choose a reason for hiding this comment

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

I agree, i just need this one so i implemented them this way. But i was thinking to have something like cfiles attribute class

super().__init__(name)
self.members: list[StructMember] = []
if members is not None:
if isinstance(members, StructMember):
self.append(members)
elif isinstance(members, list):
for member in members:
self.append(member)
else:
raise TypeError('Invalid argument type for "elements"')
if isinstance(members, list):
self.members: list[StructMember] = members.copy()
else:
raise TypeError('Invalid argument type for "elements"')
self.attributes = attributes.copy()
Copy link
Owner

Choose a reason for hiding this comment

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

Don't use copy. If you want to create a new list which is a copy of another, write it as list(attributes).
But as stated above, we should introduce attribute-mechanism later anyway.


def append(self, member: StructMember) -> None:
"""
Expand Down Expand Up @@ -322,7 +357,7 @@ def __init__(self,
static: bool = False,
const: bool = False, # const function (as seen in C++)
extern: bool = False,
params: Variable | list[Variable] | None = None) -> None:
params: list[Variable] = []) -> None:
Copy link
Owner

Choose a reason for hiding this comment

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

Empty list as default value

self.name = name
self.static = static
self.const = const
Expand All @@ -335,13 +370,7 @@ def __init__(self,
self.return_type = Type("void")
else:
raise TypeError(str(type(return_type)))
self.params: list[Variable] = []
if params is not None:
if isinstance(params, Variable):
self.append(params)
elif isinstance(params, list):
for param in params:
self.append(param)
self.params = params.copy()

def append(self, param: Variable) -> "Function":
"""
Expand Down
29 changes: 24 additions & 5 deletions src/cfile/factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def function(self,
static: bool = False,
const: bool = False, # This is not const of the return type
extern: bool = False,
params: core.Variable | list[core.Variable] | None = None) -> core.Function:
params: list[core.Variable] = []) -> core.Function:
"""
New function
"""
Expand All @@ -137,6 +137,23 @@ def type(self,
"""
return core.Type(type_ref, const, pointer, volatile, array)

def enum_member(self,
name: str,
value: int | None = None):
"""
New EnumMember
"""
return core.EnumMember(name, value)

def enum(self,
name: str,
members: list[core.EnumMember] = [],
attributes: list[str] = []):
"""
New Enum
"""
return core.Enum(name, members, attributes)

def struct_member(self,
name: str,
data_type: str | core.Type | core.Struct,
Expand All @@ -150,12 +167,13 @@ def struct_member(self,

def struct(self,
name: str,
members: core.StructMember | list[core.StructMember] | None = None
members: list[core.StructMember] = [],
attributes: list[str] = []
) -> core.Struct:
"""
New Struct
"""
return core.Struct(name, members)
return core.Struct(name, members, attributes)

def variable(self,
name: str,
Expand Down Expand Up @@ -224,6 +242,7 @@ def func_return(self, expression: int | float | str | core.Element) -> core.Func
def declaration(self,
element: Union[core.Variable, core.Function, core.DataType],
init_value: Any | None = None) -> core.Declaration:

"""New declaration"""
"""
New declaration
"""
return core.Declaration(element, init_value)
83 changes: 68 additions & 15 deletions src/cfile/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,18 @@ class ElementType(Enum):
DIRECTIVE = 1
COMMENT = 2
TYPE_DECLARATION = 3
STRUCT_DECLARATION = 4 # Should this be separate from type declaration?
VARIABLE_DECLARATION = 5
FUNCTION_DECLARATION = 6
TYPEDEF = 7
TYPE_INSTANCE = 8
STRUCT_INSTANCE = 9
VARIABLE_USAGE = 10
FUNCTION_CALL = 11
STATEMENT = 12
BLOCK_START = 13
BLOCK_END = 14
ENUM_DECLARATION = 4 # Should this be separate from type declaration?
STRUCT_DECLARATION = 5 # Should this be separate from type declaration?
VARIABLE_DECLARATION = 6
FUNCTION_DECLARATION = 7
TYPEDEF = 8
TYPE_INSTANCE = 9
STRUCT_INSTANCE = 10
VARIABLE_USAGE = 11
FUNCTION_CALL = 12
STATEMENT = 13
BLOCK_START = 14
BLOCK_END = 15


class Formatter:
Expand Down Expand Up @@ -102,6 +103,7 @@ def __init__(self, style: c_style.StyleOptions) -> None:
self.switcher_all = {
"Type": self._write_base_type,
"TypeDef": self._write_typedef_usage,
"Enum": self._write_enum_usage,
"Struct": self._write_struct_usage,
"Variable": self._write_variable_usage,
"Function": self._write_function_usage,
Expand Down Expand Up @@ -280,6 +282,8 @@ def _write_declaration(self, elem: core.Declaration) -> None:
self._write_type_declaration(elem.element)
elif isinstance(elem.element, core.TypeDef):
self._write_typedef_declaration(elem.element)
elif isinstance(elem.element, core.Enum):
self._write_enum_declaration(elem.element)
elif isinstance(elem.element, core.Struct):
self._write_struct_declaration(elem.element)
elif isinstance(elem.element, core.Variable):
Expand Down Expand Up @@ -383,6 +387,8 @@ def _write_variable_declaration(self, elem: core.Variable) -> None:
self._write("extern ")
if isinstance(elem.data_type, core.Type):
self._write_type_declaration(elem.data_type)
elif isinstance(elem.data_type, core.Enum):
self._write_enum_usage(elem.data_type)
elif isinstance(elem.data_type, core.Struct):
self._write_struct_usage(elem.data_type)
elif isinstance(elem.data_type, core.Declaration):
Expand Down Expand Up @@ -432,7 +438,7 @@ def _write_typedef_usage(self, elem: core.TypeDef):
"""
Writes typedef usage
"""
if not elem.name:
if elem.name == "":
Copy link
Owner

Choose a reason for hiding this comment

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

This is making it worse. if not elem.name works equally well if name is an empty string or None. Comparing it to empty string is not the Python way.

Copy link
Author

Choose a reason for hiding this comment

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

Oh ok i didn't know this one.

raise ValueError("Typedef without name detected")
self._write(elem.name)

Expand All @@ -446,6 +452,8 @@ def _write_typedef_declaration(self, elem: core.TypeDef):
self._write("const ")
if isinstance(elem.base_type, core.Type):
self._write_type_declaration(elem.base_type)
elif isinstance(elem.base_type, core.Enum):
self._write_enum_usage(elem.base_type)
elif isinstance(elem.base_type, core.Struct):
self._write_struct_usage(elem.base_type)
elif isinstance(elem.base_type, core.Declaration):
Expand Down Expand Up @@ -493,7 +501,7 @@ def _write_function_usage(self, elem: core.Function) -> None:
"""
Writes function usage (name of the function)
"""
if not elem.name:
if elem.name == "":
Copy link
Owner

Choose a reason for hiding this comment

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

Don't change this. It's making it worse.

Copy link
Author

Choose a reason for hiding this comment

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

OK

raise ValueError("Function with no name detected")
self._write(elem.name)

Expand All @@ -507,6 +515,10 @@ def _write_function_declaration(self, elem: core.Function) -> None:
self._write("static ")
if isinstance(elem.return_type, core.Type):
self._write_type_declaration(elem.return_type)
if isinstance(elem.return_type, core.TypeDef):
Copy link
Owner

Choose a reason for hiding this comment

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

Not sure if this is supposed to be here without having a unit test that proves it. Nevertheless it should say elif here and not if.

Copy link
Author

Choose a reason for hiding this comment

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

I can chage it to elif. But should work similary.
Well i need it here otherwise i cannot generate my code :D

Copy link
Author

@Hadatko Hadatko Oct 22, 2024

Choose a reason for hiding this comment

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

I am doing something like this. Maybe it is not optional. Byt i would like to have function which will return STRING from declaration instead of this hack for cfile.Writer(cfile.StyleOptions()).write_str:

"sizeof({cfile.Writer(cfile.StyleOptions()).write_str(self.cF.sequence().append(self.paramsTypes[param["data"].type]["C"]))})"

Copy link
Author

Choose a reason for hiding this comment

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

Ok this change is not necessary as i should use self.cF.func_call("sizeof", [self.paramsTypes[param["data"].type]["C"]]) instead of what i mentioned. I am developing under pressure a bit and lack of documentation is not helping :D

self._write_typedef_usage(elem.return_type)
elif isinstance(elem.return_type, core.Enum):
self._write_enum_usage(elem.return_type)
elif isinstance(elem.return_type, core.Struct):
self._write_struct_usage(elem.return_type)
else:
Expand Down Expand Up @@ -599,19 +611,60 @@ def _write_func_call(self, elem: core.FunctionCall) -> None:
self._write_expression(arg)
self._write(")")

def _write_enum_usage(self, elem: core.Enum) -> None:
"""
Writes enum usage
"""
if elem.name == "":
raise ValueError("enum doesn't have a name. Did you mean to use a declaration?")
self._write(f"enum {elem.name}")

def _write_enum_declaration(self, elem: core.Enum):
"""
Writes enum declaration
"""
self._write(f"enum{" ".join([' __attribute__(('+attribute+'))' for attribute in elem.attributes])} {elem.name}")
Copy link
Owner

Choose a reason for hiding this comment

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

This change only works for GCC-like compilers. Needs some more consideration and design (for example how to specify compiler family).

Copy link
Author

Choose a reason for hiding this comment

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

Agree

Copy link
Author

Choose a reason for hiding this comment

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

Agree

if self.style.brace_wrapping.after_struct:
self._eol()
self._start_line()
self._write("{")
self._eol()
else:
self._write(" {")
self._eol()
if len(elem.members):
self._indent()
for member in elem.members:
self._start_line()
self._write_enum_member(member)
self._write(",")
self._eol()
if len(elem.members):
self._dedent()
self._start_line()
self._write("}")
self.last_element = ElementType.ENUM_DECLARATION

def _write_enum_member(self, elem: core.EnumMember) -> None:
"""
Writes enum member
"""
result = elem.name + " = " + str(elem.value)
self._write(result)

def _write_struct_usage(self, elem: core.Struct) -> None:
"""
Writes struct usage
"""
if not elem.name:
if elem.name == "":
Copy link
Owner

Choose a reason for hiding this comment

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

Don't change this

Copy link
Author

Choose a reason for hiding this comment

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

OK

raise ValueError("struct doesn't have a name. Did you mean to use a declaration?")
self._write(f"struct {elem.name}")

def _write_struct_declaration(self, elem: core.Struct) -> None:
"""
Writes struct declaration
"""
self._write(f"struct {elem.name}")
self._write(f"struct{" ".join([' __attribute__(('+attribute+'))' for attribute in elem.attributes])} {elem.name}")
Copy link
Owner

Choose a reason for hiding this comment

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

Same as before. Needs some further consideration.

Copy link
Author

Choose a reason for hiding this comment

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

Agree

if self.style.brace_wrapping.after_struct:
self._eol()
self._start_line()
Expand Down