-
Notifications
You must be signed in to change notification settings - Fork 102
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
Python frontend: Class definition and instantiation #1552
Conversation
b9d5641
to
6911218
Compare
@@ -234,6 +234,9 @@ void clang_c_adjust::adjust_side_effect(side_effect_exprt &expr) | |||
else if (statement == "gcc_conditional_expression") | |||
{ | |||
} | |||
else if (statement == "nondet") |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we have this statement?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A sideeffect
with nondet
as statement is generated at:
https://github.com/brcfarias/esbmc/blob/69112185c12d40be1eed5d87ea57c4eb6e9e23ac/src/python-frontend/python_converter.cpp#L352C5-L364C6
Due to the addition of the adjust step (see https://github.com/brcfarias/esbmc/blob/69112185c12d40be1eed5d87ea57c4eb6e9e23ac/src/python-frontend/python_language.cpp#L101) to manage pointer to struct usage, it was necessary to add it to avoid the abort at https://github.com/brcfarias/esbmc/blob/69112185c12d40be1eed5d87ea57c4eb6e9e23ac/src/clang-c-frontend/clang_c_adjust_expr.cpp#L243
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add some comments here?
@@ -125,8 +134,17 @@ class python_annotation | |||
|
|||
// lhs | |||
auto target = element["targets"][0]; | |||
int col_offset = target["col_offset"].template get<int>() + | |||
target["id"].template get<std::string>().size() + 1; | |||
std::string id; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add some comments here?
@@ -156,6 +174,26 @@ class python_annotation | |||
element["value"]["end_col_offset"] = | |||
element["value"]["end_col_offset"].template get<int>() + type.size() + | |||
1; | |||
|
|||
if (element["value"].contains("args")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add some comments here?
{ | ||
std::string symbol_id = "py:" + python_filename; | ||
if (!current_class_name.empty()) | ||
symbol_id += "@C@" + current_class_name; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The + operator is usually okay for small string operations. However, it might be more efficiently done using std::stringstream or std::string::append to avoid unnecessary copying.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed to stringstream. Thanks.
if (it.first == "identifier") | ||
obj_type_name = it.second.id_string(); | ||
} | ||
assert(!obj_type_name.empty()); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shall we really use assert statements here? What happens with the release mode when we disable these assertions?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In release we would have a crash when dereferecing class_symbol at https://github.com/brcfarias/esbmc/blob/69112185c12d40be1eed5d87ea57c4eb6e9e23ac/src/python-frontend/python_converter.cpp#L502C1-L503C1.
I will remove the asserts, check if class_symbol is valid before dereferencing, and call abort()
if so.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent! Thanks, @brcfarias.
6911218
to
4192ac0
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing my comments, @brcfarias.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general looks like a good approach to me.
Since supporting the full dynamic nature of the language is probably out of scope, I'd suggest to start documenting the particular Python dialect supported by this frontend. Some things like the following reflect existing practice, but are not required by the language:
- name
self
isn't fixed (could also beme
or any other valid identifier) - restriction about
int
values we talked about before - computed instance attibutes not supported (e.g. non-literal access via get/setattr())
- variable declarations need type annotations
Thanks for submitting this PR, @brcfarias. Can you please submit another PR that addresses @fbrausse's suggestion about the documentation? |
This pull request adds support for class definition and instantiation.
Some relevant details:
obj1 = MyClass(..)
,obj1
is passed as the first argument to the constructor.