Esempio n. 1
0
        public override ICompilationResult GetResult()
        {
            var baseResult       = base.GetResult();
            var resultExpression = myReturnStatement.Value;

            if (resultExpression == null || myReturnStatement.Value == null)
            {
                return(baseResult);
            }

            var result        = MyChildToResult[myReturnStatement.Value];
            var resWithGetter = (result as IExpressionCompilationResult).TryConvertToGetter(MyParams);

            var reference = resWithGetter.GetReference();

            if (reference == null)
            {
                return(new ElementCompilationResult());
            }
            var assignment  = new ReturnStatement(GetLocation(myReturnStatement), reference);
            var instruction = new Instruction(assignment, MyParams.GetNewInstructionId());

            return(new ElementCompilationResult(GetInstructionsConnectedSequentially(new List <IInstructionsContainer> {
                resWithGetter, instruction
            })));
        }
Esempio n. 2
0
        public override ICompilationResult GetResult()
        {
            if (!(base.GetResult() is ExpressionCompilationResult resultWithGetter))
            {
                return(new ElementCompilationResult());
            }

            var reference = resultWithGetter.GetReference();

            if (reference == null)
            {
                return(new ElementCompilationResult());
            }
            var assignment  = new ReturnStatement(GetLocation(myArrowExpressionClause), reference);
            var instruction = new Instruction(assignment, MyParams.GetNewInstructionId());

            var instructionBlock = GetInstructionsConnectedSequentially(new List <IInstructionsContainer> {
                resultWithGetter, instruction
            });

            if (IsMethod)
            {
                MyParams.GetCurrentMethod().FillWithInstructions(instructionBlock);
                MyParams.FinishCurrentMethod();
                return(new ElementCompilationResult());
            }
            return(new ExpressionCompilationResult(instructionBlock, GetLocation(myArrowExpressionClause), reference: myReference));
        }
        public override ICompilationResult GetResult()
        {
            var variableInitializer = myLocalVariableDeclaration.Initial;

            if (variableInitializer == null)
            {
                return(new ElementCompilationResult());
            }

            if (!(MyChildToResult[variableInitializer] is IExpressionCompilationResult
                  initialValueCompilationResult))
            {
                throw MyParams.CreateException("initial value of variable is not an expression");
            }

            // call getter
            var initialValueReference = initialValueCompilationResult.GetReference();

            if (initialValueReference == null)
            {
                return(new ElementCompilationResult());
            }

            var location            = GetLocation(myLocalVariableDeclaration);
            var assignmentStatement = new AssignmentStatement(location, initialValueReference, myLocalVariableReference);
            var instruction         = new Instruction(assignmentStatement, MyParams.GetNewInstructionId());

            var containingType = myLocalVariableDeclaration.DeclaredElement.Type;

            myLocalVariableReference.DefaultType = containingType.IsClassType() ? new ClassId(containingType.ToString()) : null;

            return(new ElementCompilationResult(GetInstructionsConnectedSequentially(
                                                    new IInstructionsContainer[] { initialValueCompilationResult, instruction })));
        }
Esempio n. 4
0
        public override ICompilationResult GetResult()
        {
            var location = GetLocation(myAssignmentExpression);

            var sourceExpression = myAssignmentExpression.Source;

            if (sourceExpression == null)
            {
                return(new ElementCompilationResult());
            }
            ;
            var assignmentSource = MyChildToResult[sourceExpression];

            if (!(assignmentSource is IExpressionCompilationResult source))
            {
                throw MyParams.CreateException($"source of assignment is not an expression");
            }
            // need getter
            source = source.TryConvertToGetter(MyParams);
            var sourceOfAssignment = source.GetReference();

            var referenceToTarget = myAssignmentExpression.Dest;

            if (referenceToTarget == null)
            {
                return(new ElementCompilationResult());
            }
            ;
            var assignmentTarget = MyChildToResult[referenceToTarget];

            if (!(assignmentTarget is IExpressionCompilationResult target))
            {
                throw MyParams.CreateException($"target of assignment is not an expression");
            }

            // !! setter
            var targetReference = target.GetReference();

            if (sourceOfAssignment == null || targetReference == null)
            {
                return(new ExpressionCompilationResult());
            }

            var instructions = new List <IInstructionsContainer> {
                source
            };

            // divide assignment on 2 parts: a = b ->> newVar = b; a = newVar
            if (sourceOfAssignment is ClassMemberReference)
            {
                var extraVariable    = MyParams.LocalVariableIndexer.GetNextVariable();
                var extraAssignment  = new AssignmentStatement(location, sourceOfAssignment, extraVariable);
                var extraInstruction = new Instruction(extraAssignment, MyParams.GetNewInstructionId());
                instructions.Add(extraInstruction);
                sourceOfAssignment = extraVariable;
            }

            var withSetter = target.TryGetSetter(MyParams, sourceOfAssignment);

            if (withSetter != null)
            {
                instructions.Add(withSetter);
                return(new ExpressionCompilationResult(GetInstructionsConnectedSequentially(instructions), location, withSetter.GetReference()));
            }

            instructions.Add(target);

            //!!! workaround for "this = x;"
            if (!(targetReference is ClassReference))
            {
                if (!(targetReference is ClassFieldReference || targetReference is LocalVariableReference))
                {
                    throw MyParams.CreateException($"assignment to {target.GetType()}");
                }
                var mainAssignmentStatement = new AssignmentStatement(location, sourceOfAssignment, targetReference);
                var instruction             = new Instruction(mainAssignmentStatement, MyParams.GetNewInstructionId());
                instructions.Add(instruction);
            }

            return(new ExpressionCompilationResult(GetInstructionsConnectedSequentially(instructions), location, targetReference));
        }
Esempio n. 5
0
        public override ICompilationResult GetResult()
        {
            var location = GetLocation(myInvocationExpression);

            var argumentListResult =
                MyChildToResult[myInvocationExpression.ArgumentList] as ArgumentListCompilationResult;
            var invokedExpressionCompilationResult = MyChildToResult[myInvocationExpression.InvokedExpression];
            var delegateParameters = new Dictionary <int, Reference>();
            var arguments          = new Dictionary <ParameterIndex, Reference>();

            foreach (var(inParameter, result) in argumentListResult.ArgumentsResults)
            {
                // call getter
                var passedVariable = result.GetReference();

                if (passedVariable != null)
                {
                    arguments.Add(inParameter, passedVariable);
                }
            }

            var argumentsInstructionBlock = GetInstructionsConnectedSequentially(argumentListResult.ArgumentsResults.Select(kv => kv.Value));

            if (!(invokedExpressionCompilationResult is IExpressionCompilationResult expressionCompilationResult))
            {
                throw MyParams.CreateException("invoked expression result in not an expression compilation result");
            }
            var returnedValues = GetVariablePairsFromInvoked(myInvocationExpression.InvokedExpression);
            // call getter
            var expressionResult = expressionCompilationResult.TryConvertToGetter(MyParams);
            var invokedVar       = expressionResult.GetReference();

            InvocationStatement fakeInvocation = null;
            var needToInvoke = true;

            if (invokedVar != null)
            {
                InvocationTarget invocationTarget;
                switch (invokedVar)
                {
                case LocalVariableReference localVariableReference:
                    invocationTarget = new LocalVariableTarget(localVariableReference);
                    break;

                case LocalFunctionReference localFunctionReference:
                    invocationTarget = new LocalFunctionTarget(localFunctionReference);
                    MyParams.CheckExistenceOfLocalFunction(localFunctionReference);
                    break;

                case ClassMethodReference classMethodReference:
                    invocationTarget = new ClassMethodTarget(classMethodReference);
                    if (myInvocationExpression.Reference == null)
                    {
                        throw MyParams.CreateException("invocation reference is null");
                    }
                    if (myInvocationExpression.Reference.Resolve().DeclaredElement == null ||
                        myInvocationExpression.Reference.Resolve().DeclaredElement.GetType().ToString() != "JetBrains.ReSharper.Psi.CSharp.Impl.DeclaredElement.CSharpMethod")
                    {
                        needToInvoke = false;
                        if (arguments.TryGetValue(new ParameterIndex(0), out var reference) &&
                            reference is LocalFunctionReference funRef)
                        {
                            fakeInvocation = new InvocationStatement(location,
                                                                     new LocalFunctionTarget(funRef),
                                                                     new Dictionary <ParameterIndex, Reference>(),
                                                                     new Dictionary <ParameterIndex, Reference>());
                        }
                    }
                    break;

                case ClassFieldReference classFieldReference:
                    invocationTarget = new ClassFieldTarget(classFieldReference);
                    break;

                case ClassPropertyReference classPropertyReference:
                    Trace.Assert(false, "invocation target is property");
                    invocationTarget = new ClassPropertyTarget(classPropertyReference);
                    break;

                default:
                    throw MyParams.CreateException($"{invokedVar.GetType()}: unsupported reference type to invoke");
                }

//                if (invocationTarget is ClassMethodTarget classMethodTarget &&
//                    CompilerUtils.IsAddToCollectionMethod(classMethodTarget))
//                {
//                    myInvocationExpression.InvokedExpression.
//                    var statement = new InvocationStatement(location,
//                        invocationTarget,
//                        arguments,
//                        returnedValues);
//
//                }
                myReference = returnedValues[ParameterIndex.ReturnValueIndex];
                var summaryBlock = GetInstructionsConnectedSequentially(new List <IInstructionsContainer>
                {
                    expressionResult, argumentsInstructionBlock
                });

                if (needToInvoke || fakeInvocation != null)
                {
                    var statement = fakeInvocation ?? new InvocationStatement(location,
                                                                              invocationTarget,
                                                                              arguments,
                                                                              returnedValues);

                    var invocationInstruction = new Instruction(statement, MyParams.GetNewInstructionId());

                    summaryBlock = GetInstructionsConnectedSequentially(new List <IInstructionsContainer>
                    {
                        summaryBlock, invocationInstruction
                    });
                }

                return(new ExpressionCompilationResult(summaryBlock, location, reference: myReference));
            }

            return(new ExpressionCompilationResult(expressionResult.GetInstructionBlock(), location, reference: myReference));
        }
        public override ICompilationResult GetResult()
        {
            IReference reference;
            var        location = GetLocation(myObjectCreationExpression);

            switch (myObjectCreationExpression.TypeUsage)
            {
            case IUserTypeUsage userDeclaredTypeUsage:
                reference = userDeclaredTypeUsage.ScalarTypeName.Reference;
                break;

            case IPredefinedTypeUsage predefinedDeclaredTypeUsage:
                reference = predefinedDeclaredTypeUsage.ScalarPredefinedTypeName.Reference;
                break;

            case IDynamicTypeUsage dynamicDeclaredTypeUsage:
                throw MyParams.CreateException("found dynamicDeclaredTypeUsage");

            case ITupleTypeUsage tupleDeclaredTypeUsage:
                throw MyParams.CreateException("found tupleDeclaredTypeUsage");
                break;

            default:
                throw MyParams.CreateException($"{myObjectCreationExpression.TypeUsage.GetType()} is unexpected type");
            }

            var declaredElement = reference.Resolve().DeclaredElement;

            if (declaredElement == null)
            {
                return(new ExpressionCompilationResult(new InstructionBlock(), location));
            }

            if (!(declaredElement is ITypeElement typeElement))
            {
                throw MyParams.CreateException("In object creation declared element " + declaredElement.GetElementType().PresentableName + " is not a type element");
            }

            var classRef = typeElement.GetClassReference();

            var constructor = myObjectCreationExpression.ConstructorReference.Resolve().DeclaredElement as IConstructor;

            if (constructor == null)
            {
                return(new ExpressionCompilationResult(new InstructionBlock(), location, reference: classRef));
            }

            var invokedFun = new ClassMethodTarget(new ClassMethodReference(classRef, constructor.GetNameWithHash()));


            var passedParameters = new Dictionary <ParameterIndex, Reference>();

            if (myObjectCreationExpression.ArgumentList != null)
            {
                if (MyChildToResult[myObjectCreationExpression.ArgumentList] is ArgumentListCompilationResult
                    argumentListResult)
                {
                    foreach (var(inParameter, result) in argumentListResult.ArgumentsResults)
                    {
                        // call getter
                        var passedVariable = result.GetReference();

                        if (passedVariable != null)
                        {
                            passedParameters.Add(inParameter, passedVariable);
                        }
                    }
                }
            }

            var returnedValues = GetVariablePairsFromInvoked(myObjectCreationExpression);

            var statement = new InvocationStatement(location, invokedFun, passedParameters, returnedValues, true);

            var instruction = new Instruction(statement, MyParams.GetNewInstructionId());

            var returned = (LocalVariableReference)statement.ReturnedValues[new ParameterIndex(-1)];

            returned.DefaultType = classRef.ClassId;

            return(new ExpressionCompilationResult(new InstructionBlock(instruction), location, reference: returned));
        }