private static NRefactory.ConstructorDeclaration FiltertConstructorStatements(NRefactory.ConstructorDeclaration constructorDeclaration, Type baseType) {
            var block = new NRefactory.BlockStatement();

            foreach (var statement in constructorDeclaration.Body.Statements) {
                var expression = statement as NRefactory.ExpressionStatement;

                if (expression != null) {
                    if (expression.Expression is NRefactory.AssignmentExpression) {
                        continue;
                    }

                    var invocation = expression.Expression as NRefactory.InvocationExpression;

                    block.Add(statement.Clone());

                    if (invocation != null && invocation.HasAnnotationOf<Cecil.MethodReference>()) {
                        var methodReference = invocation.Annotation<Cecil.MethodReference>();

                        if (methodReference.Name.Equals(".ctor") && methodReference.DeclaringType.GetActualType().Equals(baseType)) {
                            block.AddReturnStatement(new NRefactory.ThisReferenceExpression());
                            constructorDeclaration.Body = block;
                            break;
                        }
                    }
                }
                else {
                    block.Add(statement.Clone());
                }
            }

            return constructorDeclaration;
        }
Exemplo n.º 2
0
        protected internal Index(NRefactory.IndexerExpression indexerExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            bool isAssignment = false;
            PropertyReference propertyReference = null;

            _indexerExpression = indexerExpression;
            isAssignment = IsAssignment();
            Target = indexerExpression.Target.AcceptVisitor(Visitor, ParentScope);
            TryGetArguments();

            if (indexerExpression.HasAnnotationOf<PropertyReference>(out propertyReference)) {
                var propertyInfo = Target.Type.GetProperty(propertyReference.Name);

                InternalType = propertyInfo.PropertyType;
                _indexer = propertyInfo;
            }
            else {
                _isArrayIndex = true;
                var targetType = Target.Type;

                if (targetType.HasElementType) {
                    targetType = Target.Type.GetElementType();
                }

                InternalType = targetType;
                _methodCall = _isAssignment ? Expression.ArrayAccess : (Func<Expression, IEnumerable<Expression>, Expression>)Expression.ArrayAccess;
            }
        }
        protected internal ArrayCreation(NRefactory.ArrayCreateExpression arrayCreateExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            
            _arrayCreateExpression = arrayCreateExpression;
            _isJaggeedArray = IsJaggedArray();
            _isVector = IsVector();
            InternalType = GetArrayType();

            if (InternalType.GetArrayRank() == 1 && !_isJaggeedArray) {
                if (TryGetOneDimensionalArrayBounds()) {
                    BuildEmptyOneDimensionalArray();
                }
                else {
                    BuildOneDimensionalArray();
                }
            }
            else {
                if (TryGetBounds()) {
                    BuildEmptyMultiDimensionalArray();
                }
                else {
                    BuildMultiDimensionalArrayAccess();
                }
            }
        }
Exemplo n.º 4
0
 protected internal Switch(NRefactory.SwitchStatement switchStatement, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     _switchStatement = switchStatement;
     SwitchValue = switchStatement.Expression.AcceptVisitor(Visitor, scope);
     BuildSwitchCases();
     InternalType = TypeSystem.Void;
 }
Exemplo n.º 5
0
        protected internal New(NRefactory.ObjectCreateExpression objectCreation, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            
            _objectCreation = objectCreation;

            if (!objectCreation.Type.IsNull) {
                InternalType = objectCreation.Type.AcceptVisitor(Visitor, ParentScope).Type;

                if (objectCreation.Initializer != null) {
                    if (objectCreation.Arguments.Count == 2) {
                        Expression expression;
                        NRefactory.Expression @this = objectCreation.Arguments.First();
                        NRefactory.Expression func = objectCreation.Arguments.Last();

                        if (TryHandleAnonymousMethod(@this, func as NRefactory.InvocationExpression, out expression)) {
                            Expression = expression;
                            return;
                        }
                    }

                    if (objectCreation.Initializer != NRefactory.ArrayInitializerExpression.Null) {
                        Expression = objectCreation.Initializer.AcceptVisitor(Visitor, ParentScope);
                        return;
                    }
                }

                Expression = BuildConstructor();
            }
            else {
                Expression = HandleAnonymousType();
            }
        }
 protected internal Direction(NRefactory.DirectionExpression directionExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     _directionExpression = directionExpression;
     IdentifierParameter = directionExpression.Expression.AcceptVisitor(Visitor, scope) as Identifier;
     _outParameter = ParentScope.Find(IdentifierParameter.Name);
     InternalType = IdentifierParameter.Type;
 }
 protected internal CaseLabel(NRefactory.CaseLabel caseLabel, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     var expression = caseLabel.Expression;
     _caseLabel = caseLabel;
     DefaultValue = expression.AcceptVisitor(Visitor, ParentScope);
     InternalType = DefaultValue.Type;
 }
 protected internal Identifier(NRefactory.IdentifierExpression identifierExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     _identifierExpression = identifierExpression;
     Expression = scope.Find(_identifierExpression.Identifier);
     InternalType = Expression.Type;
     Name = _identifierExpression.Identifier;
 }
        protected internal Invocation(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            
            var methodReference = invocationExpression.Annotation<Mono.Cecil.MethodReference>();

            _invocationExpression = invocationExpression;

            if (methodReference != null) {
                MethodInfo methodInfo = null;
                Member = methodInfo = methodReference.GetActualMethod<MethodInfo>();

                if (IsInitializeArray(methodInfo)) {
                    var first = _invocationExpression.Arguments.First().AcceptVisitor(Visitor, ParentScope);
                    var invocation = _invocationExpression.Arguments.Last() as NRefactory.InvocationExpression;
                    var second = invocation.Arguments.First();
                    var memberReference = invocationExpression.Target as NRefactory.MemberReferenceExpression;
                    var target = memberReference.Target as NRefactory.TypeReferenceExpression;
                    var type = target.Type.GetActualType();
                    var parameters = methodReference.Parameters;
                    return;
                }
            }

            BuildInvocation();
        }
        public MappedInvocation(NRefactory.ConstructorDeclaration ctor, MethodDefinition baseCtor) {
            var parameterTypes = new List<Type>();
            var ctorType = ctor.GetActualType();

            _parameters = ctor.Parameters
                              .Select((p, i) => {
                                  return new Parameter(p.Name, p.GetActualType(), i);
                              })
                              .ToArray();

            _parameterDeclarations = ctor.Parameters.ToArray();
            _visitor.VisitConstructorDeclaration(ctor, null);
            _outParameters = new List<Parameter>();
            parameterTypes = new List<Type>();

            _parameters.ForEach((p, i) => {
                var type = p.Type;

                if (_visitor.Contains(p.Name)) {
                    if (!type.IsByRef) {
                        type = type.MakeByRefType();
                        p = new Parameter(p.Name, type, p.Location);
                    }

                    _outParameters.Add(p);
                }

                parameterTypes.Add(type);
            });

            VerifyUniqueness(ctorType, parameterTypes);
            Parameters = parameterTypes.ToArray();
        }
 protected internal NamedExpression(NRefactory.NamedExpression namedExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
     : base(scope, visitor) {
     Name = namedExpression.Identifier;
     _namedExpression = namedExpression;
     Expression = _namedExpression.Expression.AcceptVisitor(Visitor, ParentScope);
     InternalType = Expression.Type;
 }
Exemplo n.º 12
0
        protected internal Init(NRefactory.VariableInitializer variableInitializer, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            NRefactory.Expression initializer = null;

            _variableInitializer = variableInitializer;
            initializer = variableInitializer.Initializer;
        }
 public static ConstructorBlock ConstructorBlock(NRefactory.BlockStatement blockStatement,
                                                 ParameterExpression contextParameter,
                                                 IScope scope,
                                                 INRefcatoryExpressionVisitor visitor,
                                                 IEnumerable<ParameterExpression> parameters = null,
                                                 IEnumerable<ParameterExpression> baseConstructorParameters = null) {
     return new ConstructorBlock(blockStatement, contextParameter, parameters: parameters, scope: scope, visitor: visitor, baseConstructorParameters: baseConstructorParameters);
 }
Exemplo n.º 14
0
        protected internal Block(NRefactory.BlockStatement blockStatement, IScope scope, INRefcatoryExpressionVisitor visitor, IEnumerable<ParameterExpression> parameters = null)
            : base(scope, visitor) {

            _blockStatement = blockStatement;
            InternalType = ResolveType(blockStatement);
            Parameters = parameters;
            BuildExpressions();
        }
        protected internal AnonymousType(NRefactory.AnonymousTypeCreateExpression anonymousTypeCreateExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            var typeInformation = anonymousTypeCreateExpression.Annotation<TypeInformation>();

            _anonymousTypeCreateExpression = anonymousTypeCreateExpression;
            _initializers = _anonymousTypeCreateExpression.Initializers.Select(i => i.AcceptVisitor(Visitor, ParentScope));
            InternalType = typeInformation.InferredType.GetActualType();
        }
Exemplo n.º 16
0
        protected internal Base(NRefactory.BaseReferenceExpression baseReferenceExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            var memberReference = baseReferenceExpression.Parent.Annotation<Mono.Cecil.MemberReference>();

            _baseReferenceExpression = baseReferenceExpression;
            Context = RootScope.Context.Expression;
            InternalType = memberReference != null ? memberReference.DeclaringType.GetActualType() : Context.Type;
        }
        public MethodBlock(NRefactory.BlockStatement blockStatement, IScope scope, INRefcatoryExpressionVisitor visitor, IEnumerable<ParameterExpression> parameters = null)
            : base(blockStatement, parameters: parameters, scope: scope, visitor: visitor) {
            var registry = RootScope.BranchingRegistry;

            if (registry.HasReturnLabel) {
                AddReturnLabelExpression();
            }
        }
        internal IEnumerable<Parameter> GetOutParameters(NRefactory.ConstructorDeclaration ctor) {
            var mapped = _mappedInvocations.FirstOrDefault(m => m.CanMapTo(ctor));

            if (mapped != null) {
                return mapped.OutParameters;
            }

            return null;
        }
        private Expression ReduceEvent(NRefactory.AssignmentExpression assignmentExpression) {
            ExpressionType expressionType;

            if (!Enum.TryParse<ExpressionType>(assignmentExpression.Operator.ToString(), out expressionType)) {
                throw new InvalidOperationException("Event registration must have an add/subtract operator");
            }

            return AstExpression.Event(Expression, Member as EventInfo, expressionType, ParentScope, Visitor);
        }
        private static NRefactory.ConstructorDeclaration ResolveConstructor(NRefactory.ConstructorDeclaration constructorDeclaration, Type baseType, ILGenerator iLGenerator, List<Cil.Instruction> instructions) {
            ConstructorEmitterVisitor constructorEmitter = null;

            constructorDeclaration = FiltertConstructorStatements(constructorDeclaration, baseType);
            constructorEmitter = new ConstructorEmitterVisitor(constructorDeclaration, new InstructionsIndexer(instructions));
            constructorDeclaration.AcceptVisitor(constructorEmitter, iLGenerator);

            return constructorDeclaration;
        }
        public override Type ResolveType(NRefactory.BlockStatement blockStatement) {
            var methodDeclaration = blockStatement.Parent as NRefactory.MethodDeclaration;

            if (methodDeclaration != null) {
                return methodDeclaration.GetReturnType();
            }

            return null;
        }
        public static Method Method(NRefactory.MethodDeclaration methodDeclaration, object context, INRefcatoryExpressionVisitor visitor, IScope scope = null) {
            Method methodBlock = null;

            if (!methodDeclaration.IsCompilerGeneratorEnumerator(ref methodBlock)) {
                methodBlock = new MethodDeclaration(methodDeclaration, context, scope, visitor);
            }

            return methodBlock;
        }
Exemplo n.º 23
0
        protected internal Binary(NRefactory.BinaryOperatorExpression binaryOperatorExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {

            _binaryOperatorExpression = binaryOperatorExpression;
            Left = binaryOperatorExpression.Left.AcceptVisitor(Visitor, ParentScope);
            Right = binaryOperatorExpression.Right.AcceptVisitor(Visitor, ParentScope);
            _binaryOperationType = GetBinaryOperator(binaryOperatorExpression.Operator);
            _promotionDecisions = BinaryNumericPromotionDecision.Decide(Left.Type, Right.Type);
            InternalType = binaryOperatorExpression.GetBinaryOperationType(_promotionDecisions, Left.Type);
        }
Exemplo n.º 24
0
        protected internal Return(NRefactory.ReturnStatement returnStatement, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            _returnStatement = returnStatement;
            Value = returnStatement.Expression.AcceptVisitor(Visitor, ParentScope);

            if (Value != null) {
                InternalType = Value.Type;
                Target = RootScope.BranchingRegistry.RegisterReturnStatementLabel(InternalType);
            }
        }
Exemplo n.º 25
0
        protected internal MethodOf(NRefactory.InvocationExpression invocationExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            var memberReference = invocationExpression.Arguments.First() as NRefactory.MemberReferenceExpression;
            var loadMethodTokenInvocation = memberReference.Target as NRefactory.InvocationExpression;
            var methodReference = loadMethodTokenInvocation.Arguments.First().Annotation<MethodReference>();

            _invocationExpression = invocationExpression;
            Member = methodReference.GetActualMethod<MethodInfo>();
            InternalType = typeof(RuntimeMethodHandle);
        }
        internal Func<Expression, Expression, BinaryExpression> this[NRefactory.BinaryOperatorType @operator, bool isChecked] {
            get {
                CheckedUncheckedEntry entry;
                Func<Expression, Expression, BinaryExpression> func = null;

                if (_methods.TryGetValue(@operator, out entry)) {
                    func = entry[isChecked];
                }

                return func;
            }
        }
Exemplo n.º 27
0
        protected internal Assign(NRefactory.AssignmentExpression assignmentExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {

            AstExpression left = null;

            _assignment = assignmentExpression;
            Right = _rightExpression = assignmentExpression.Right.AcceptVisitor(Visitor, ParentScope);
            left = assignmentExpression.Left.AcceptVisitor(Visitor, ParentScope);
            Left = left.Reduce();
            _isInvocable = IsInvocable(Left, out _invokedType);
            InternalType = left.Type;
        }
        public bool CanMapTo(NRefactory.ConstructorDeclaration ctor) {
            if (ctor.Parameters.Count != _parameterDeclarations.Length) {
                return false;
            }

            return ctor.Parameters
                       .All((p, i) => {
                           var parameter = _parameterDeclarations[i];
                           return p.Name.Equals(parameter.Name) &&
                                  p.GetActualType().Equals(parameter.GetActualType());
                       });
        }
        public ConstructorBlock(NRefactory.BlockStatement blockStatement,
                                ParameterExpression contextParameter,
                                IScope scope,
                                INRefcatoryExpressionVisitor visitor,
                                IEnumerable<ParameterExpression> parameters,
                                IEnumerable<ParameterExpression> baseConstructorParameters)
            : base(blockStatement, parameters: parameters, scope: scope, visitor: visitor) {

            _baseConstructorParameters = baseConstructorParameters;
            AddNewContextCreationExpression(contextParameter);
            InternalType = contextParameter.Type;
            AddReturnLabelExpression();
        }
        protected internal ArrayInitializer(NRefactory.ArrayInitializerExpression arrayInitializerExpression, IScope scope, INRefcatoryExpressionVisitor visitor)
            : base(scope, visitor) {
            MethodReference methodReference;

            _arrayInitializerExpression = arrayInitializerExpression;

            if (_arrayInitializerExpression.Parent.HasAnnotationOf<MethodReference>(out methodReference)) {
                _constructor = methodReference.GetActualMethod() as ConstructorInfo;
                InternalType = _constructor.DeclaringType;
            }

            _initializers = arrayInitializerExpression.Elements
                                                      .Select(e => e.AcceptVisitor(Visitor, ParentScope))
                                                      .ToList();
        }