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

refactor IntegerExp #3495

Merged
merged 1 commit into from Apr 28, 2014
Merged

Conversation

WalterBright
Copy link
Member

Eliminates duplicate code, and some dead code. Simplifies the logic. Makes assumptions explicit with assert. Steps towards making IntegerExp copy-on-write. No semantic changes.

}

IntegerExp::IntegerExp(dinteger_t value)
: Expression(Loc(), TOKint64, sizeof(IntegerExp))
{
this->type = Type::tint32;
this->value = value;
this->value = (d_int32) value;
Copy link
Member

Choose a reason for hiding this comment

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

No assert that it fits?

Copy link
Member Author

Choose a reason for hiding this comment

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

No - the original didn't check, either.

Copy link
Member

Choose a reason for hiding this comment

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

plz add

Copy link
Member Author

Choose a reason for hiding this comment

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

No, because the 'normalization' is not an error. To simplify the logic, computations are done on a dinteger_t, then 'normalized' down to match the type. This is the way it has always worked. The method is not incorrect, it just moves all the normalization to IntegerExp itself rather than spreading it around.

Copy link
Member

Choose a reason for hiding this comment

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

Why is hiding the narrowing cast inside the constructor a good thing? Is this constructor used for anything other than creating small int constants? Is there correct code that doesn't explicitly set a type, but is still ok with out-of-range dinteger_t values being truncated?

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not hiding it. All normalization is done by IntegerExp, not by the caller. The 'truncation' is normal 2-s complement arithmetic, and this arithmetic happens, especially during constant folding. In order to avoid a complex mess of code, all calculations are done to dinteger_t precision and then truncated.

It's always worked this way, and it is not a bug, and it is not hiding bugs.

Copy link
Member

Choose a reason for hiding this comment

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

If this was being done during constant folding, then the constructor with a type would be being used, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

That constructor is a convenience function because most of the time the type is int. If C++ had forwarding constructors I'd have written it to simply forward to the other one.

@andralex
Copy link
Member

lgtm but value should be private now that it's nicely encapsulated

andralex added a commit that referenced this pull request Apr 28, 2014
@andralex andralex merged commit 6b7f28e into dlang:master Apr 28, 2014
@WalterBright WalterBright deleted the refactor-IntegerExp branch April 28, 2014 23:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants