public override ICompilationResult GetResult()
        {
            if (MyChildToResult[myElementAccessExpression.Operand] is ExpressionCompilationResult
                expressionCompilationResult)
            {
                var argumentsResults   = MyChildToResult[myElementAccessExpression.ArgumentList];
                var argumentListResult = argumentsResults as ArgumentListCompilationResult;
                var singleArgument     = argumentListResult.ArgumentsResults[new ParameterIndex(0)];

                var argumentBlock     = singleArgument.TryConvertToGetter(MyParams);
                var argumentReference = argumentBlock.GetReference();
                // call getter

                var operandBlock     = expressionCompilationResult.TryConvertToGetter(MyParams);
                var operandReference = operandBlock.GetReference();
                if (operandReference == null)
                {
                    throw MyParams.CreateException($"null reference result in element access target");
                }

                return(new ElementAccessCompilationResult(
                           GetInstructionsConnectedSequentially(new [] { argumentBlock, operandBlock }),
                           argumentReference, GetLocation(myElementAccessExpression), operandReference));
            }
            return(base.GetResult());
        }
        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 })));
        }
Beispiel #3
0
        public VariableDeclarationCompiler(IVariableDeclaration variableDeclaration,
                                           AbstractILCompilerParams @params) : base(@params)
        {
            myVariableDeclaration = variableDeclaration;
            var variableName = myVariableDeclaration.DeclaredName;

            try
            {
                MyParams.LocalVariableIndexer.GetNextVariable(variableName);
            }
            catch (Exception e)
            {
                throw MyParams.CreateException($"var stacks exception");
            }
        }
Beispiel #4
0
        public override ICompilationResult GetResult()
        {
            if (myPropertyDeclaration.DeclaredElement == null)
            {
                throw MyParams.CreateException($"declaredElement of property is null");
            }

//            var accesors = MyResults.Where(result => result is AccessorDeclarationCompilationResult).Cast<AccessorDeclarationCompilationResult>().ToList();
//            if (!accesors.IsEmpty())
//            {
//                if (!accesors.Exists(x => x.KindOfAccesor == AccessorKind.GETTER))
//                {
//                    MyParams.CreateFakeProperty($"get_{myPropertyDeclaration.DeclaredElement.ShortName}");
//                }
//                if (!accesors.Exists(x => x.KindOfAccesor == AccessorKind.SETTER))
//                {
//                    MyParams.CreateFakeProperty($"set_{myPropertyDeclaration.DeclaredElement.ShortName}");
//                }
//            }
            return(new ElementCompilationResult());
        }
Beispiel #5
0
        public ArrowExpressionClauseCompiler(IArrowExpressionClause arrowExpressionClause, AbstractILCompilerParams @params) : base(@params)
        {
            myArrowExpressionClause = arrowExpressionClause;
            switch (myArrowExpressionClause.Parent)
            {
            case IPropertyDeclaration propertyDeclaration:
                var name = $"get_{propertyDeclaration.DeclaredName}";
                MyParams.CreateMethod(null, name);
                IsMethod = true;
                break;

            case IMethodDeclaration _:
            case ILocalFunctionDeclaration _:
            case IAccessorDeclaration _:
            case IConstructorDeclaration _:
            case IIndexerDeclaration _:
                IsMethod = false;
                break;

            default:
                throw MyParams.CreateException(
                          $"parent of ArrowExpression is {myArrowExpressionClause.Parent?.GetType()}");
            }
        }
Beispiel #6
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));
        }
Beispiel #7
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));
        }
Beispiel #8
0
        public override ICompilationResult GetResult()
        {
            // TODO: refactor
            var resultsWithoutTypeParameter = MyChildToResult.Where(kvp => !(kvp.Key is ITypeArgumentList))
                                              .Select(kvp => kvp.Value)
                                              .ToList();
            var instructionBlock0 = GetInstructionsConnectedSequentially(resultsWithoutTypeParameter);
            var reference0        = GetReferenceFromResults(resultsWithoutTypeParameter);
            var withOutGetter     = new ExpressionCompilationResult(instructionBlock0, null, reference0);
            //
            var resultWithGetter = withOutGetter.TryConvertToGetter(MyParams);
            var instructions     = resultWithGetter.GetInstructionBlock();
            var childReference   = resultWithGetter.GetReference();


            var declaredElement = myReferenceExpression.Reference.Resolve().DeclaredElement;

            if (declaredElement == null)
            {
                return(new ExpressionCompilationResult(instructions, GetLocation(myReferenceExpression)));
            }
            var referenceName = declaredElement.GetNameWithHash();

            switch (declaredElement)
            {
            case IField field:
                var defaultFieldType = field.Type.IsClassType() ? new ClassId(field.Type.ToString()) : null;
                myReference = new ClassFieldReference(GetReferenceToOwner(childReference), referenceName, defaultFieldType);
                break;

            case ILocalFunctionDeclaration _:
                if (childReference != null)
                {
                    throw MyParams.CreateException($"local function invocation({referenceName}) has owner({childReference})");
                }
                myReference = new ClassMethodReference(new ClassReference(new ClassId(MyParams.GetOwnerForLocalFunction())), referenceName);
                break;

            case IMethod _:
                myReference = new ClassMethodReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IProperty _:
                myReference = new ClassPropertyReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IParameter param:
                var owner           = param.ContainingParametersOwner ?? throw MyParams.CreateException("method parameter's owner is null");
                var parameterNumber = owner.Parameters.IndexOf(param);

                var defaultType = param.Type.IsClassType() ? new ClassId(param.Type.ToString()) : null;
                myReference = new LocalVariableReference(parameterNumber, defaultType);

                break;

            case IClass @class:
                myReference = @class.GetClassReference();
                break;

            case IStruct @struct:
                myReference = @struct.GetClassReference();
                break;

            case IEnum @enum:
                myReference = @enum.GetClassReference();
                break;

            case IExternAlias _:
            case INamespace _:
                myReference = null;
                break;

            case IInterface @interface:
                myReference = @interface.GetClassReference();
                break;

            case IEvent @event:
                myReference = new ClassFieldReference(GetReferenceToOwner(childReference), referenceName);
                break;

            case IAnonymousTypeProperty _:
                //todo
                break;

            case ITypeParameter _:
                //todo
                break;

//                case ISingleVariableDesignation _:
//                case ILocalConstantDeclaration _:
//                case ILocalVariableDeclaration _:
//                case ICatchVariableDeclaration _:
            case IVariableDeclaration variableDeclaration:
                if (childReference != null)
                {
                    throw MyParams.CreateException("");
                }
                myReference = MyParams.LocalVariableIndexer.GetVariableIndex(referenceName);
                ((LocalVariableReference)myReference).DefaultType =
                    variableDeclaration.Type.IsClassType() ? new ClassId(variableDeclaration.Type.ToString()) : null;
                break;

            case IDelegate @delegate:
                myReference = new ClassMethodReference(GetReferenceToOwner(childReference), referenceName);
                break;

            default:
            {
                throw MyParams.CreateException($"unsupported reference type {declaredElement.GetType()}");
                // reference to method or field
//                    var nameOfIdentifier = myReferenceExpression.NameIdentifier.Name;
//
//                    if (myReferenceExpression.FirstChild != null)
//                    {
//                        if (MyChildToResult[myReferenceExpression.FirstChild] is IExpressionCompilationResult varOwner)
//                        {
//                            myReference = new ClassMemberReference(varOwner.GetReference(), nameOfIdentifier);
//                        }
//                        else
//                        {
//                            throw MyParams.GetException("Owner of class member is not an expression");
//                        }
//                    }
//                    else
//                    {
//                        throw MyParams.GetException("Class member has no no owner");
//                    }
//
//                    break;
            }
            }

            if (myReference is LocalVariableReference localVariable)
            {
            }

            return(new ExpressionCompilationResult(instructions, GetLocation(myReferenceExpression), 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));
        }