コード例 #1
0
        private void HandleVariableExpression(VariableSyntaxNode varNode, bool isRight, bool willBeMethodCall, ref IType?expressionResultType)
        {
            {
                if (!isRight && willBeMethodCall)
                {
                    throw new InvalidLValueException("Cannot have a method call on the left");
                }


                if (currentMethodInfo.Locals.TryGetValue(varNode.Name, out var localVar))
                {
                    if (isRight)
                    {
                        if (willBeMethodCall && localVar.LocalType.IsValueType)
                        {
                            generator.EmitLdloca(localVar);
                        }
                        else
                        {
                            generator.EmitLdloc(localVar);
                        }
                    }
                    else
                    {
                        generator.EmitStloc(localVar);
                    }
                    expressionResultType = localVar.LocalType;
                }
                else if (currentMethodInfo.Parameters.TryGetValue(varNode.Name, out var parameterVar))
                {
                    if (isRight)
                    {
                        if (willBeMethodCall && parameterVar.type.IsValueType)
                        {
                            generator.EmitLdarga(parameterVar.idx);
                        }
                        else
                        {
                            generator.EmitLdarg(parameterVar.idx);
                        }
                    }
                    else
                    {
                        generator.EmitStarg(parameterVar.idx);
                    }
                    expressionResultType = parameterVar.type;
                }
                else if (currentMethodInfo.Fields.TryGetValue(varNode.Name, out var fieldVar))
                {
                    if (fieldVar.IsStatic)
                    {
                        // If field is static, it can always be accessed
                        if (isRight)
                        {
                            if (willBeMethodCall && fieldVar.FieldType.IsValueType)
                            {
                                generator.EmitLdsflda(fieldVar);
                            }
                            else
                            {
                                generator.EmitLdsfld(fieldVar);
                            }
                        }
                        else
                        {
                            generator.EmitStsfld(fieldVar);
                        }
                    }
                    else
                    {
                        if (currentMethodInfo.IsStatic)
                        {
                            throw new InstanceFieldAccessException("Invalid to access instance field in static method");
                        }

                        if (isRight)
                        {
                            generator.EmitLdthis();
                            if (willBeMethodCall && fieldVar.FieldType.IsValueType)
                            {
                                generator.EmitLdflda(fieldVar);
                            }
                            else
                            {
                                generator.EmitLdfld(fieldVar);
                            }
                        }
                        else
                        {
                            generator.EmitStfld(fieldVar);
                        }
                    }
                    expressionResultType = fieldVar.FieldType;
                }
                else
                {
                    // Try to see if we are trying to grab a delegate
                    foreach (var method in store.Methods ![currentMethodInfo.Type])