Check that IExpression is lvalue
Hi,
I have an expression - IExpression object - and I want to check if it is
lvalue or not.
Now I do as follows:
private static bool IsLValue(IExpression expression)
{
IReferenceExpression refExpression = expression as IReferenceExpression;
if (refExpression != null)
{
ResolveResult resolveResult = refExpression.Reference.Resolve();
if (resolveResult.DeclaredElement != null)
{
IDeclaredElement refElement = resolveResult.DeclaredElement;
IValue value = refElement.ToValue(refExpression.Language);
if (value.IsLValue)
return true;
else
{
IField field = refElement as IField;
if (field != null && field.IsField)
{
IConstructorDeclaration ctor = expression.GetContainingTypeMemberDeclaration()
as IConstructorDeclaration;
if ((ctor != null) && (!field.IsStatic || ctor.IsStatic))
return true;
}
}
}
}
return expression.IsClassifiedAsVariable;
}
Is this correct?
Sincerely,
Ilya Ryzhenkov
Please sign in to leave a comment.
Hello Ilya,
I think the else branch in the value.IsLValue check will not execute. Besides,
you check for static fields in non-static constructor, but do not check for
non-static fields in static constructor. Why do you perform these checks?
Thanks,
Andrey Simanovsky
Hello Andrey,
AS> Hello Ilya,
AS>
AS> I think the else branch in the value.IsLValue check will not
AS> execute. Besides, you check for static fields in non-static
AS> constructor, but do not check for non-static fields in static
AS> constructor. Why do you perform these checks?
Check http://users.netix.ru/orangy/nerpa/
I need these checks to ensure, that swapping assignments source and destination
produces valid code. I'm not sure I must do such checks for such context
action, but I tried to learn OpenAPI.
Sincerely,
Ilya Ryzhenkov
AS>> Hello Ilya,
AS>>
AS>> I think the else branch in the value.IsLValue check will not
AS>> execute. Besides, you check for static fields in non-static
AS>> constructor, but do not check for non-static fields in static
AS>> constructor. Why do you perform these checks?
AS>>
I think it is enough to check for lvalue without constructor checks (consider
"garbage in - garbage out" senarios). Otherwise you may also need to check
for reachable assignments of the right part:
int i, j = 0;
i = j; // swarp result will not compile because i is not assigned up to here
I just fear that such an example makes people avoid the API rather than use
it.:)
Thanks,
Andrey Simanovsky
Also, you have to check IExpression.IsClassifiedAsVariable
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
news:5992f43be4e058c7e030aaf8879d@news.jetbrains.com...
>
>
>
>
>
Hello Eugene,
EP> Also, you have to check IExpression.IsClassifiedAsVariable
EP>
Isn't it what is written on return statement?
Sincerely,
Ilya Ryzhenkov
Yes, sorry my mistake
--
Eugene Pasynkov
Developer
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
"Ilya Ryzhenkov" <orangy@netix.ru> wrote in message
news:5992f43bef74e8c7e463b4c99db3@news.jetbrains.com...
>
>
>