Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Move visit_Call in Common Visitor #2013

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

Closed
wants to merge 5 commits into from

Conversation

Smit-create
Copy link
Collaborator

Fixes #1981

@Smit-create Smit-create requested review from certik and rebcabin June 27, 2023 04:02
@Smit-create Smit-create marked this pull request as ready for review June 27, 2023 04:17
class StringIONew:
_buf : str
_0cursor : i32 = i32(142)
_len : i32 = i32(2439)
Copy link
Contributor

Choose a reason for hiding this comment

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

This PR assigns this to the "value" member of Variable. I am not sure if our code assumes that this is a constant, so the backend doesn't even visit this variable and uses the value instead. If so, then this won't work, since any modifications to _len will be lost.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's add a test for modifying _len.

Copy link
Contributor

Choose a reason for hiding this comment

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

Here is documentation for Variable:

`symbolic_value` the optional symbolic expression to initialize the variable
(e.g. `2+3+4+x`), this value must be compile time, but it is not necessarily a
constant (e.g., can contain binary operations, other variables, etc.)

`value` the optional constant expression holding the compile time value
(e.g. `5`, or `5.5`), it is a compile time constant.

So value and symbolic_value must be compile time constants. So let's require that for now as well in LPython.

A separate question is if they are set, if the Variable itself has to be a constant or not. I would say if it has to be a constant, then it should have a type Const. Otherwise it can change, which means value is the initial value, but the runtime value can change.

Copy link
Collaborator Author

@Smit-create Smit-create Jun 29, 2023

Choose a reason for hiding this comment

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

EDIT: This does work on an assignment but makes things more complex as it requires more methods to be moved to CommonVisitor.

Now, this problem gets trickier as Structs don't have a body, and, so AnnAssign which is an Assignment to the struct variables isn't possible with the current design.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

One thing that we may do is add a field in the Struct to contain initializers for all it's variable members like the following:

     | StructType(symbol_table symtab, identifier name, identifier* dependencies,
         identifier* members, abi abi, access access, bool is_packed, bool is_abstract,
-        expr? alignment, symbol? parent)
+        expr* initializers, expr? alignment, symbol? parent)

@certik
Copy link
Contributor

certik commented Jun 28, 2023

TODO:

  • document that value in Variable is an initializer, but the runtime value can change.

# But since lpython ignores empty, the following is just a workaround.
# Follow the discussions here: https://github.com/lcompilers/lpython/issues/1809
mat: Mat = Mat(2.0)
vec: Vec = Vec(2.0)
Copy link
Contributor

Choose a reason for hiding this comment

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

This change is forced by moving visit_Call and visit_List into the common visitor. Both the original mat: Mat = Mat([f64(0.0), f64(0.0)]) and the new mat: Mat = Mat(2.0) is incorrect so I think it's fine for now. The proper fix is to use mat: Mat = Mat(empty((2, 2), dtype=float64)).

@certik
Copy link
Contributor

certik commented Jun 28, 2023

TODO:

  • inside a function, a statement x: i32 = i32(0) is now visited twice, which I think will cause problems.

@certik certik marked this pull request as draft June 28, 2023 16:07
@certik
Copy link
Contributor

certik commented Jun 28, 2023

I think x: i32 = i32(0) inside a function: symbol table visitor can declare the "x" variable, but it should not process the initializer. The body visitor processes the initializer and creates a regular assignment. The RHS is NOT required to be a constant.

The x: i32 = i32(0) inside a class (or dataclass) will get fully processed by the symbol table visitor (and the RHS is required to be a compile time constant, Const), and the RHS will become the "value", i.e., the ASR initializer.

@certik
Copy link
Contributor

certik commented Jun 28, 2023

Otherwise I think this is fine.

@Smit-create
Copy link
Collaborator Author

This is not required as #2056 is merged.

@Smit-create Smit-create deleted the i-1981 branch July 31, 2023 12:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LPython bogusly changes attribute value
2 participants