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

References to a constant variable are replaced with their values in the generated code #84

Open
crmorello opened this issue Apr 4, 2023 · 1 comment
Assignees

Comments

@crmorello
Copy link

If you have a variable that is a constant in Cito, all references in the generated output are replaced with the value.

const int testVariable = 3;
int testFunc() {
    return testVariable;
}

becomes this in the output

static let testVariable = 3
func testFunc() {
    return 3
}

Seems to be specific to constants. I get the reasoning for it, they are constant so the value is good enough, but maybe it should then strip out the constant declarations. This, while on the surface is not that big of an issue, it does create unnecessary variables that are not referenced anywhere. It would be nice if the output would mirror the cito source more closely.

Member variables work fine

int test = 3;
int testFunc() {
    return test;
}
private var test : Int = 3

private func testFunc() -> Int
{
    return self.test
}
@pfusik pfusik self-assigned this Aug 17, 2023
@pfusik
Copy link
Collaborator

pfusik commented Aug 17, 2023

Thank you for reporting this!

Current approach is to perform constant folding during the semantic analysis, which among other things, replaces all constant references with their values.
Local and private constants end up unreferenced and they are omitted in C output because the compilers would warn. swiftc 5.8.1 doesn't warn.

My goal is to keep the constants and the references to them. This requires reworking the constant folder.
This is not trivial, because compile-time constants are required in some places, such as case values, array storage sizes and (obviously) values of constants. Fusion supports compile-time function execution (CTFE), unlike C/C#/Java/Python. C++ supports CTFE provided the functions are marked with constexpr.

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

No branches or pull requests

2 participants