public override object TrackedVisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
 {
     if (constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.This)
     {
         TypeDeclaration   typeDeclaration = (TypeDeclaration)AstUtil.GetParentOfType(constructorInitializer, typeof(TypeDeclaration));
         List <Expression> args            = ReplaceNullArguments(typeDeclaration, constructorInitializer.Arguments, constructorInitializer);
         constructorInitializer.Arguments = args;
     }
     return(base.TrackedVisitConstructorInitializer(constructorInitializer, data));
 }
        public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
        {
            if (!(constructorDeclaration.Body.Statements.FirstOrDefault() is ExpressionStatement stmt))
            {
                return;
            }
            var currentCtor = (IMethod)constructorDeclaration.GetSymbol();
            ConstructorInitializer ci;

            switch (stmt.Expression)
            {
            // Pattern for reference types:
            // this..ctor(...);
            case InvocationExpression invocation:
                if (!(invocation.Target is MemberReferenceExpression mre) || mre.MemberName != ".ctor")
                {
                    return;
                }
                if (!(invocation.GetSymbol() is IMethod ctor && ctor.IsConstructor))
                {
                    return;
                }
                ci = new ConstructorInitializer();
                var target = mre.Target;
                // Ignore casts, those might be added if references are missing.
                if (target is CastExpression cast)
                {
                    target = cast.Expression;
                }
                if (target is ThisReferenceExpression or BaseReferenceExpression)
                {
                    if (ctor.DeclaringTypeDefinition == currentCtor.DeclaringTypeDefinition)
                    {
                        ci.ConstructorInitializerType = ConstructorInitializerType.This;
                    }
                    else
                    {
                        ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                    }
                }
                else
                {
                    return;
                }
                // Move arguments from invocation to initializer:
                invocation.Arguments.MoveTo(ci.Arguments);
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.CopyAnnotationsFrom(invocation);
                }
                // Remove the statement:
                stmt.Remove();
                break;
        public virtual object Visit(ConstructorInitializer constructorInitializer, object data)
        {
            Debug.Assert(constructorInitializer != null);
            Debug.Assert(constructorInitializer.Arguments != null);

            foreach (Expression expr in constructorInitializer.Arguments)
            {
                Debug.Assert(expr != null);
                expr.AcceptVisitor(this, data);
            }
            return(data);
        }
            public override void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
            {
                base.VisitConstructorInitializer(constructorInitializer);

                // Check if existing initializer is valid:
                var rr = ctx.Resolve(constructorInitializer) as CSharpInvocationResolveResult;

                if (rr != null && (rr.OverloadResolutionErrors & errorsIndicatingWrongNumberOfArguments) != 0)
                {
                    IType targetType = constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.Base ? baseType : currentType;
                    AddIssue(new CodeIssue(constructorInitializer.Keyword, GetIssueText(targetType, constructorInitializer.Arguments.Count)));
                }
            }
            public override void VisitConstructorInitializer(ConstructorInitializer initializer)
            {
                var result = ctx.Resolve(initializer);

                if (!result.IsError)
                {
                    this.initializerInvoked = true;
                }
                else
                {
                    this.initializer = initializer;
                }
            }
        private void AddProperConstructor(TypeDeclaration typeDeclaration, string instanceFieldName)
        {
            TypeReference type = new TypeReference(instanceFieldName);
            ParameterDeclarationExpression fieldParameter = new ParameterDeclarationExpression(type, instanceFieldName);

            FieldReferenceExpression fieldReference      = new FieldReferenceExpression(new ThisReferenceExpression(), instanceFieldName);
            IdentifierExpression     right               = new IdentifierExpression(instanceFieldName);
            AssignmentExpression     assignment          = new AssignmentExpression(fieldReference, AssignmentOperatorType.Assign, right);
            ExpressionStatement      expressionStatement = new ExpressionStatement(assignment);
            string fullName = GetFullName(typeDeclaration);

            IList constructors = AstUtil.GetChildrenWithType(typeDeclaration, typeof(ConstructorDeclaration));

            if (constructors.Count == 0)
            {
                string name = typeDeclaration.Name;
                List <ParameterDeclarationExpression> parameters = new List <ParameterDeclarationExpression>();
                parameters.Add(fieldParameter);
                ConstructorDeclaration constructorDeclaration = new ConstructorDeclaration(name, Modifiers.Public, parameters, null);

                constructorDeclaration.Body = new BlockStatement();
                constructorDeclaration.Body.AddChild(expressionStatement);
                constructorDeclaration.Parent = typeDeclaration;
                CodeBase.References.Add("Cons:" + fullName, null);

                typeDeclaration.Children.Add(constructorDeclaration);
            }
            else
            {
                foreach (ConstructorDeclaration constructor in constructors)
                {
                    if (!ContainsParameter(constructor.Parameters, fieldParameter))
                    {
                        constructor.Parameters.Add(fieldParameter);
                        if (constructor.ConstructorInitializer != null)
                        {
                            ConstructorInitializer ci = constructor.ConstructorInitializer;
                            if (ci.ConstructorInitializerType == ConstructorInitializerType.This)
                            {
                                ci.Arguments.Add(new IdentifierExpression(fieldParameter.ParameterName));
                            }
                        }
                        constructor.Body.Children.Insert(0, expressionStatement);
                    }
                }
                if (!CodeBase.References.Contains("Cons:" + fullName))
                {
                    CodeBase.References.Add("Cons:" + fullName, null);
                }
            }
        }
        public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
        {
            var stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement;

            if (stmt == null)
            {
                return(null);
            }

            var invocation = stmt.Expression as InvocationExpression;

            if (invocation == null)
            {
                return(null);
            }

            var mre = invocation.Target as MemberReferenceExpression;

            if (mre == null || mre.MemberName != ".ctor")
            {
                return(null);
            }

            var initializer = new ConstructorInitializer();

            if (mre.Target is ThisReferenceExpression)
            {
                initializer.ConstructorInitializerType = ConstructorInitializerType.This;
            }
            else if (mre.Target is BaseReferenceExpression)
            {
                initializer.ConstructorInitializerType = ConstructorInitializerType.Base;
            }
            else
            {
                return(null);
            }

            // Move arguments from invocation to initializer:
            invocation.Arguments.MoveTo(initializer.Arguments);
            // Add the initializer: (unless it is the default 'base()')
            if (initializer.ConstructorInitializerType != ConstructorInitializerType.Base || initializer.Arguments.Count > 0)
            {
                initializer.AddAnnotation(invocation.Annotation <MethodReference>());
                constructorDeclaration.Initializer = initializer;
            }
            // Remove the statement:
            stmt.Remove();
            return(null);
        }
        public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
        {
            ExpressionStatement stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement;

            if (stmt == null)
            {
                return(null);
            }
            InvocationExpression invocation = stmt.Expression as InvocationExpression;

            if (invocation == null)
            {
                return(null);
            }
            MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;

            if (mre != null && mre.MemberName == ".ctor")
            {
                ConstructorInitializer ci = new ConstructorInitializer();
                if (mre.Target is ThisReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                }
                else if (mre.Target is BaseReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                }
                else
                {
                    return(null);
                }
                // Move arguments from invocation to initializer:
                invocation.Arguments.MoveTo(ci.Arguments);
                var ilRanges = stmt.GetAllRecursiveILRanges();
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.WithAnnotation(invocation.Annotation <IMethod>());
                    ci.AddAnnotation(ilRanges);
                }
                else
                {
                    constructorDeclaration.Body.HiddenStart = NRefactoryExtensions.CreateHidden(ilRanges, constructorDeclaration.Body.HiddenStart);
                }
                // Remove the statement:
                stmt.Remove();
            }
            return(null);
        }
Beispiel #9
0
        public ArgumentsInfo(IEmitter emitter, ConstructorInitializer initializer)
        {
            this.Emitter    = emitter;
            this.Expression = null;

            var arguments = initializer.Arguments.ToList();

            this.ResolveResult = emitter.Resolver.ResolveNode(initializer, emitter) as InvocationResolveResult;

            this.BuildArgumentsList(arguments);
            if (this.ResolveResult != null)
            {
                this.HasTypeArguments = ((IMethod)this.ResolveResult.Member).TypeArguments.Count > 0;
            }
        }
Beispiel #10
0
        public virtual async Task <TAggregateRoot> FindAsync(Guid aggregateId)
        {
            using (var eventStorage = _eventStorageFactory.Storage)
            {
                if (!await eventStorage.AggregateExistsAsync(aggregateId))
                {
                    throw new AggregateNotFoundException($"Aggregate with id {aggregateId} does not exist.");
                }

                var events = await eventStorage.GetAggregateEventsAsync(aggregateId);

                var aggregate = ConstructorInitializer.Construct <TAggregateRoot>(
                    new Type[] { aggregateId.GetType(), events.GetType() }, new object[] { aggregateId, events });
                return(aggregate);
            }
        }
Beispiel #11
0
        /// <summary>
        /// Create the syntax for a class constructor.
        /// </summary>
        /// <param name="className">Name of the class.</param>
        /// <param name="body">The generated body of the constructor.</param>
        /// <param name="parameters">A list with parameters.</param>
        /// <param name="modifiers">A list with modifiers.</param>
        /// <param name="attributes">A list with attributes.</param>
        /// <param name="constructorInitializer">The constructor initializer</param>
        /// <param name="summary">XML documentation summary</param>
        /// <returns>The declaration syntax for a constructor.</returns>
        public static ConstructorDeclarationSyntax Create(
            string className,
            BlockSyntax body,
            IEnumerable <Parameter> parameters            = null,
            IEnumerable <Modifiers> modifiers             = null,
            IEnumerable <Attribute> attributes            = null,
            ConstructorInitializer constructorInitializer = null,
            string summary = null)
        {
            if (className == null)
            {
                throw new ArgumentNullException(nameof(className));
            }

            var constructor = SyntaxFactory.ConstructorDeclaration(SyntaxFactory.Identifier(className))
                              .WithBody(body);

            if (parameters != null)
            {
                constructor = constructor.WithParameterList(ParameterGenerator.Create(parameters.ToArray()));
            }

            if (constructorInitializer != null)
            {
                constructor = constructor.WithInitializer(
                    SyntaxFactory.ConstructorInitializer(
                        constructorInitializer.ConstructorInitializerType == ConstructorInitializerTypes.Base ? SyntaxKind.BaseConstructorInitializer : SyntaxKind.ThisConstructorInitializer,
                        constructorInitializer.Arguments == null ? null : ArgumentGenerator.Create(constructorInitializer.Arguments.ToArray())));
            }

            if (attributes != null)
            {
                constructor = constructor.WithAttributeLists(AttributeGenerator.Create(attributes.ToArray()));
            }

            if (modifiers != null)
            {
                constructor = constructor.WithModifiers(ModifierGenerator.Create(modifiers.ToArray()));
            }

            if (!string.IsNullOrEmpty(summary))
            {
                constructor = constructor.WithSummary(summary, parameters);
            }

            return(constructor);
        }
        public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
        {
            var stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement;

            if (stmt == null)
            {
                return;
            }
            if (!(stmt.Expression is InvocationExpression invocation))
            {
                return;
            }
            if (invocation.Target is MemberReferenceExpression mre && mre.MemberName == ".ctor")
            {
                ConstructorInitializer ci = new ConstructorInitializer();
                var target = mre.Target;
                // Ignore casts, those might be added if references are missing.
                if (target is CastExpression cast)
                {
                    target = cast.Expression;
                }
                if (target is ThisReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                }
                else if (target is BaseReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                }
                else
                {
                    return;
                }
                // Move arguments from invocation to initializer:
                invocation.Arguments.MoveTo(ci.Arguments);
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.CopyAnnotationsFrom(invocation);
                }
                // Remove the statement:
                stmt.Remove();
            }
        }
Beispiel #13
0
        public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
        {
            // make constructor public if visiblity is not set (unless constructor is static)
            if ((constructorDeclaration.Modifier & (Modifiers.Visibility | Modifiers.Static)) == 0)
            {
                constructorDeclaration.Modifier |= Modifiers.Public;
            }

            // MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
            BlockStatement body = constructorDeclaration.Body;

            if (body != null && body.Children.Count > 0)
            {
                ExpressionStatement se = body.Children[0] as ExpressionStatement;
                if (se != null)
                {
                    InvocationExpression ie = se.Expression as InvocationExpression;
                    if (ie != null)
                    {
                        MemberReferenceExpression fre = ie.TargetObject as MemberReferenceExpression;
                        if (fre != null && "New".Equals(fre.MemberName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            if (fre.TargetObject is BaseReferenceExpression || fre.TargetObject is ClassReferenceExpression || fre.TargetObject is ThisReferenceExpression)
                            {
                                body.Children.RemoveAt(0);
                                ConstructorInitializer ci = new ConstructorInitializer();
                                ci.Arguments = ie.Arguments;
                                if (fre.TargetObject is BaseReferenceExpression)
                                {
                                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                                }
                                else
                                {
                                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                                }
                                constructorDeclaration.ConstructorInitializer = ci;
                            }
                        }
                    }
                }
            }
            return(base.VisitConstructorDeclaration(constructorDeclaration, data));
        }
Beispiel #14
0
        public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
        {
            ExpressionStatement stmt = constructorDeclaration.Body.Statements.FirstOrDefault() as ExpressionStatement;

            if (stmt == null)
            {
                return;
            }
            InvocationExpression invocation = stmt.Expression as InvocationExpression;

            if (invocation == null)
            {
                return;
            }
            MemberReferenceExpression mre = invocation.Target as MemberReferenceExpression;

            if (mre != null && mre.MemberName == ".ctor")
            {
                ConstructorInitializer ci = new ConstructorInitializer();
                if (mre.Target is ThisReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                }
                else if (mre.Target is BaseReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                }
                else
                {
                    return;
                }
                // Move arguments from invocation to initializer:
                invocation.Arguments.MoveTo(ci.Arguments);
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.CopyAnnotationsFrom(invocation);
                }
                // Remove the statement:
                stmt.Remove();
            }
        }
        // The following conversions are implemented:
        //   MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()

        // The following conversions should be implemented in the future:
        //   Public Event EventName(param As String) -> automatic delegate declaration
        //   Function A() \n A = SomeValue \n End Function -> convert to return statement

        public override object Visit(ConstructorDeclaration constructorDeclaration, object data)
        {
            // MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
            BlockStatement body = constructorDeclaration.Body;

            if (body != null && body.Children.Count > 0)
            {
                StatementExpression se = body.Children[0] as StatementExpression;
                if (se != null)
                {
                    InvocationExpression ie = se.Expression as InvocationExpression;
                    if (ie != null)
                    {
                        FieldReferenceExpression fre = ie.TargetObject as FieldReferenceExpression;
                        if (fre != null && "New".Equals(fre.FieldName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            if (fre.TargetObject is BaseReferenceExpression || fre.TargetObject is ClassReferenceExpression)
                            {
                                body.Children.RemoveAt(0);
                                ConstructorInitializer ci = new ConstructorInitializer();
                                ci.Arguments = ie.Arguments;
                                if (fre.TargetObject is BaseReferenceExpression)
                                {
                                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                                }
                                else
                                {
                                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                                }
                                constructorDeclaration.ConstructorInitializer = ci;
                            }
                        }
                    }
                }
            }
            return(base.Visit(constructorDeclaration, data));
        }
 public override void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
 {
     throw NotSupportedToConsistency();
 }
Beispiel #17
0
        public void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
        {
            JsonObject initializer = new JsonObject();
            initializer.Comment = "VisitConstructorInitializer";
            if (constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.This)
            {
                initializer.AddJsonValue("keyword", GetKeyword(ConstructorInitializer.ThisKeywordRole));
            }
            else
            {
                initializer.AddJsonValue("keyword", GetKeyword(ConstructorInitializer.BaseKeywordRole));
            }
            initializer.AddJsonValue("arguments", GetCommaSeparatedList(constructorInitializer.Arguments));

            Push(initializer);
        }
Beispiel #18
0
 public UnifiedElement VisitConstructorInitializer(
     ConstructorInitializer constructorInitializer, object data)
 {
     throw new NotImplementedException("ConstructorInitializer");
 }
Beispiel #19
0
            protected override IEnumerable <string> GenerateCode(List <object> includedMembers)
            {
                bool gotConstructorOverrides = false;

                foreach (IMethod m in includedMembers.OfType <IMethod> ().Where(m => m.SymbolKind == SymbolKind.Constructor))
                {
                    gotConstructorOverrides = true;
                    var init = new ConstructorInitializer {
                        ConstructorInitializerType = ConstructorInitializerType.Base
                    };

                    var overridenConstructor = new ConstructorDeclaration {
                        Name      = Options.EnclosingType.Name,
                        Modifiers = Modifiers.Public,
                        Body      = new BlockStatement(),
                    };

                    if (m.Parameters.Count > 0)
                    {
                        overridenConstructor.Initializer = init;
                    }

                    foreach (var par in m.Parameters)
                    {
                        overridenConstructor.Parameters.Add(new ParameterDeclaration(Options.CreateShortType(par.Type), par.Name));
                        init.Arguments.Add(new IdentifierExpression(par.Name));
                    }
                    foreach (var member in includedMembers.OfType <IMember> ())
                    {
                        if (member.SymbolKind == SymbolKind.Constructor)
                        {
                            continue;
                        }
                        overridenConstructor.Parameters.Add(new ParameterDeclaration(Options.CreateShortType(member.ReturnType), CreateParameterName(member)));

                        var memberReference = new MemberReferenceExpression(new ThisReferenceExpression(), member.Name);
                        var assign          = new AssignmentExpression(memberReference, AssignmentOperatorType.Assign, new IdentifierExpression(CreateParameterName(member)));
                        overridenConstructor.Body.Statements.Add(new ExpressionStatement(assign));
                    }

                    yield return(overridenConstructor.ToString(Options.FormattingOptions));
                }
                if (gotConstructorOverrides)
                {
                    yield break;
                }
                var constructorDeclaration = new ConstructorDeclaration {
                    Name      = Options.EnclosingType.Name,
                    Modifiers = Modifiers.Public,
                    Body      = new BlockStatement()
                };

                foreach (IMember member in includedMembers)
                {
                    constructorDeclaration.Parameters.Add(new ParameterDeclaration(Options.CreateShortType(member.ReturnType), CreateParameterName(member)));

                    var memberReference = new MemberReferenceExpression(new ThisReferenceExpression(), member.Name);
                    var assign          = new AssignmentExpression(memberReference, AssignmentOperatorType.Assign, new IdentifierExpression(CreateParameterName(member)));
                    constructorDeclaration.Body.Statements.Add(new ExpressionStatement(assign));
                }

                yield return(constructorDeclaration.ToString(Options.FormattingOptions));
            }
 public virtual object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
 {
     throw new global::System.NotImplementedException("ConstructorInitializer");
 }
 public object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
 {
     throw new ApplicationException("ConstructorInitializer visited.");
 }
		public override object VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration, object data)
		{
			// make constructor public if visiblity is not set (unless constructor is static)
			if ((constructorDeclaration.Modifier & (Modifiers.Visibility | Modifiers.Static)) == 0)
				constructorDeclaration.Modifier |= Modifiers.Public;
			
			// MyBase.New() and MyClass.New() calls inside the constructor are converted to :base() and :this()
			BlockStatement body = constructorDeclaration.Body;
			if (body != null && body.Children.Count > 0) {
				ExpressionStatement se = body.Children[0] as ExpressionStatement;
				if (se != null) {
					InvocationExpression ie = se.Expression as InvocationExpression;
					if (ie != null) {
						MemberReferenceExpression fre = ie.TargetObject as MemberReferenceExpression;
						if (fre != null && "New".Equals(fre.MemberName, StringComparison.InvariantCultureIgnoreCase)) {
							if (fre.TargetObject is BaseReferenceExpression || fre.TargetObject is ClassReferenceExpression || fre.TargetObject is ThisReferenceExpression) {
								body.Children.RemoveAt(0);
								ConstructorInitializer ci = new ConstructorInitializer();
								ci.Arguments = ie.Arguments;
								if (fre.TargetObject is BaseReferenceExpression)
									ci.ConstructorInitializerType = ConstructorInitializerType.Base;
								else
									ci.ConstructorInitializerType = ConstructorInitializerType.This;
								constructorDeclaration.ConstructorInitializer = ci;
							}
						}
					}
				}
			}
			return base.VisitConstructorDeclaration(constructorDeclaration, data);
		}
Beispiel #23
0
	void ConstructorInitializer(
#line  1317 "cs.ATG" 
out ConstructorInitializer ci) {

#line  1318 "cs.ATG" 
		Expression expr; ci = new ConstructorInitializer(); 
		Expect(9);
		if (la.kind == 51) {
			lexer.NextToken();

#line  1322 "cs.ATG" 
			ci.ConstructorInitializerType = ConstructorInitializerType.Base; 
		} else if (la.kind == 111) {
			lexer.NextToken();

#line  1323 "cs.ATG" 
			ci.ConstructorInitializerType = ConstructorInitializerType.This; 
		} else SynErr(184);
		Expect(20);
		if (StartOf(26)) {
			Argument(
#line  1326 "cs.ATG" 
out expr);

#line  1326 "cs.ATG" 
			SafeAdd(ci, ci.Arguments, expr); 
			while (la.kind == 14) {
				lexer.NextToken();
				Argument(
#line  1327 "cs.ATG" 
out expr);

#line  1327 "cs.ATG" 
				SafeAdd(ci, ci.Arguments, expr); 
			}
		}
		Expect(21);
	}
Beispiel #24
0
 public void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
 {
     throw new NotImplementedException();
 }
		public override void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
		{
			ForceSpacesBefore(constructorInitializer.LParToken, policy.SpaceBeforeMethodCallParentheses);
			if (constructorInitializer.Arguments.Any()) {
				ForceSpacesAfter(constructorInitializer.LParToken, policy.SpaceWithinMethodCallParentheses);
			} else {
				ForceSpacesAfter(constructorInitializer.LParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
				ForceSpacesBefore(constructorInitializer.RParToken, policy.SpaceBetweenEmptyMethodCallParentheses);
			}

			FormatArguments(constructorInitializer);

		}
Beispiel #26
0
 public override object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
 {
     WriteLine("VisitConstructorInitializer");
     return(base.VisitConstructorInitializer(constructorInitializer, data));
 }
        public override void VisitConstructorDeclaration(ConstructorDeclaration constructorDeclaration)
        {
            if (!(constructorDeclaration.Body.Statements.FirstOrDefault() is ExpressionStatement stmt))
            {
                return;
            }
            var currentCtor = (IMethod)constructorDeclaration.GetSymbol();
            ConstructorInitializer ci;

            switch (stmt.Expression)
            {
            // Pattern for reference types:
            // this..ctor(...);
            case InvocationExpression invocation:
                if (!(invocation.Target is MemberReferenceExpression mre) || mre.MemberName != ".ctor")
                {
                    return;
                }
                if (!(invocation.GetSymbol() is IMethod ctor && ctor.IsConstructor))
                {
                    return;
                }
                ci = new ConstructorInitializer();
                var target = mre.Target;
                // Ignore casts, those might be added if references are missing.
                if (target is CastExpression cast)
                {
                    target = cast.Expression;
                }
                if (target is ThisReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                }
                else if (target is BaseReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.Base;
                }
                else
                {
                    return;
                }
                // Move arguments from invocation to initializer:
                invocation.Arguments.MoveTo(ci.Arguments);
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.CopyAnnotationsFrom(invocation);
                }
                // Remove the statement:
                stmt.Remove();
                break;

            // Pattern for value types:
            // this = new TSelf(...);
            case AssignmentExpression assignment:
                if (!(assignment.Right is ObjectCreateExpression oce && oce.GetSymbol() is IMethod ctor2 && ctor2.DeclaringTypeDefinition == currentCtor.DeclaringTypeDefinition))
                {
                    return;
                }
                ci = new ConstructorInitializer();
                if (assignment.Left is ThisReferenceExpression)
                {
                    ci.ConstructorInitializerType = ConstructorInitializerType.This;
                }
                else
                {
                    return;
                }
                // Move arguments from invocation to initializer:
                oce.Arguments.MoveTo(ci.Arguments);
                // Add the initializer: (unless it is the default 'base()')
                if (!(ci.ConstructorInitializerType == ConstructorInitializerType.Base && ci.Arguments.Count == 0))
                {
                    constructorDeclaration.Initializer = ci.CopyAnnotationsFrom(oce);
                }
                // Remove the statement:
                stmt.Remove();
                break;

            default:
                return;
            }
            if (context.DecompileRun.RecordDecompilers.TryGetValue(currentCtor.DeclaringTypeDefinition, out var record) &&
                currentCtor.Equals(record.PrimaryConstructor) &&
                ci.ConstructorInitializerType == ConstructorInitializerType.Base)
            {
                if (constructorDeclaration.Parent is TypeDeclaration {
                    BaseTypes : { Count : >= 1 }
                } typeDecl)
                {
                    var baseType    = typeDecl.BaseTypes.First();
                    var newBaseType = new InvocationAstType();
                    baseType.ReplaceWith(newBaseType);
                    newBaseType.BaseType = baseType;
                    ci.Arguments.MoveTo(newBaseType.Arguments);
                }
                constructorDeclaration.Remove();
            }
        }
Beispiel #28
0
		public virtual void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
		{
			StartNode(constructorInitializer);
			WriteToken(Roles.Colon, BoxedTextColor.Punctuation);
			Space();
			DebugStart(constructorInitializer);
			if (constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.This) {
				WriteKeyword(ConstructorInitializer.ThisKeywordRole);
			} else {
				WriteKeyword(ConstructorInitializer.BaseKeywordRole);
			}
			Space(policy.SpaceBeforeMethodCallParentheses);
			WriteCommaSeparatedListInParenthesis(constructorInitializer.Arguments, policy.SpaceWithinMethodCallParentheses, CodeBracesRangeFlags.Parentheses);
			DebugEnd(constructorInitializer);
			EndNode(constructorInitializer);
		}
 /// <inheritdoc/>
 public virtual void VisitConstructorInitializer(ConstructorInitializer syntax)
 {
     VisitNode(syntax);
 }
 public override void VisitConstructorInitializer(ConstructorInitializer syntax)
 {
     _underlyingVisitor.VisitConstructorInitializer(syntax);
 }
		public virtual object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data) {
			Debug.Assert((constructorInitializer != null));
			Debug.Assert((constructorInitializer.Arguments != null));
			foreach (Expression o in constructorInitializer.Arguments) {
				Debug.Assert(o != null);
				o.AcceptVisitor(this, data);
			}
			return null;
		}
			protected override IEnumerable<string> GenerateCode (List<object> includedMembers)
			{
				bool gotConstructorOverrides = false;
				foreach (IMethod m in includedMembers.OfType<IMethod> ().Where (m => m.SymbolKind == SymbolKind.Constructor)) {
					gotConstructorOverrides = true;
					var init = new ConstructorInitializer {
						ConstructorInitializerType = ConstructorInitializerType.Base
					};

					var overridenConstructor = new ConstructorDeclaration {
						Name = Options.EnclosingType.Name,
						Modifiers = Modifiers.Public,
						Body = new BlockStatement (),
					};

					if (m.Parameters.Count > 0)
						overridenConstructor.Initializer = init;

					foreach (var par in m.Parameters) {
						overridenConstructor.Parameters.Add (new ParameterDeclaration (Options.CreateShortType (par.Type), par.Name));
						init.Arguments.Add (new IdentifierExpression(par.Name)); 
					}
					foreach (var member in includedMembers.OfType<IMember> ()) {
						if (member.SymbolKind == SymbolKind.Constructor)
							continue;
						overridenConstructor.Parameters.Add (new ParameterDeclaration (Options.CreateShortType (member.ReturnType), CreateParameterName (member)));

						var memberReference = new MemberReferenceExpression (new ThisReferenceExpression (), member.Name);
						var assign = new AssignmentExpression (memberReference, AssignmentOperatorType.Assign, new IdentifierExpression (CreateParameterName (member)));
						overridenConstructor.Body.Statements.Add (new ExpressionStatement (assign));
					}

					yield return overridenConstructor.ToString (Options.FormattingOptions);
				}
				if (gotConstructorOverrides)
					yield break;
				var constructorDeclaration = new ConstructorDeclaration {
					Name = Options.EnclosingType.Name,
					Modifiers = Modifiers.Public,
					Body = new BlockStatement ()
				};

				foreach (IMember member in includedMembers) {
					constructorDeclaration.Parameters.Add (new ParameterDeclaration (Options.CreateShortType (member.ReturnType), CreateParameterName (member)));

					var memberReference = new MemberReferenceExpression (new ThisReferenceExpression (), member.Name);
					var assign = new AssignmentExpression (memberReference, AssignmentOperatorType.Assign, new IdentifierExpression (CreateParameterName (member)));
					constructorDeclaration.Body.Statements.Add (new ExpressionStatement (assign));
				}
				
				yield return constructorDeclaration.ToString (Options.FormattingOptions);
			}
 public override object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data)
 {
     return(null);
 }
		public IList<JsStatement> CompileConstructorInitializer(ConstructorInitializer initializer, bool currentIsStaticMethod) {
			SetRegion(initializer.GetRegion());
			try {
				var rr = _resolver.Resolve(initializer);
				if (rr is DynamicInvocationResolveResult) {
					_errorReporter.Message(Messages._7998, initializer.ConstructorInitializerType == ConstructorInitializerType.Base ? "dynamic invocation of base constructor" : "dynamic constructor chaining");
					return new JsStatement[0];
				}
				else {
					var csirr = (CSharpInvocationResolveResult)rr;
					return _expressionCompiler.CompileConstructorInitializer((IMethod)csirr.Member, csirr.GetArgumentsForCall(), csirr.GetArgumentToParameterMap(), csirr.InitializerStatements, currentIsStaticMethod);
				}
			}
			catch (Exception ex) {
				_errorReporter.InternalError(ex);
				return new JsStatement[0];
			}
		}
Beispiel #35
0
        public void VisitTypeDeclaration(TypeDeclaration node)
        {
            nextEnumValue = 0;
            VisitChildren(node);

            // Automatically generate constructor bodies
            var result = resolver.Resolve(node) as TypeResolveResult;
            if (result != null) {
                foreach (var method in result.Type.GetConstructors()) {
                    if (!method.IsSynthetic) {
                        continue;
                    }

                    // Add the declaration to the tree so it will be emitted
                    var declaration = new ConstructorDeclaration();
                    var body = new BlockStatement();
                    if (result.Type.BaseClass() != null) {
                        var initializer = new ConstructorInitializer();
                        initializer.ConstructorInitializerType = ConstructorInitializerType.Base;
                        declaration.Initializer = initializer;
                    }
                    declaration.Name = result.Type.Name;
                    declaration.Body = body;
                    body.InsertChildAfter(null, new NewLineNode(), Roles.NewLine);
                    InsertFieldInitializersIntoConstructorBody(result.Type, declaration);
                    node.AddChild(declaration, Roles.TypeMemberRole);
                    context.constructors[method] = declaration;
                }
            }
        }
		public virtual object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data) {
			throw new global::System.NotImplementedException("ConstructorInitializer");
		}
Beispiel #37
0
 public void VisitConstructorInitializer(ConstructorInitializer node)
 {
     VisitChildren(node);
 }
Beispiel #38
0
 public JNode VisitConstructorInitializer(ConstructorInitializer node)
 {
     throw new NotImplementedException();
 }
		public virtual object TrackedVisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data) {
			return base.VisitConstructorInitializer(constructorInitializer, data);
		}
 public Node VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
 {
     return(CreateDummy(constructorInitializer));
 }
 public virtual void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
 {
     if (this.ThrowException)
     {
         throw (Exception)this.CreateException(constructorInitializer);
     }
 }
Beispiel #42
0
 public StringBuilder VisitConstructorInitializer(ConstructorInitializer constructorInitializer, int data)
 {
     throw new NotSupportedException();
 }
		public virtual void VisitConstructorInitializer (ConstructorInitializer constructorInitializer)
		{
			VisitChildren (constructorInitializer);
		}
		public sealed override object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data) {
			BeginVisit(constructorInitializer);
			object result = TrackedVisitConstructorInitializer(constructorInitializer, data);
			EndVisit(constructorInitializer);
			return result;
		}
Beispiel #45
0
 public StringBuilder VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
 {
     throw new NotImplementedException();
 }
		public virtual object VisitConstructorInitializer(ConstructorInitializer constructorInitializer, object data) {
			Debug.Assert((constructorInitializer != null));
			Debug.Assert((constructorInitializer.Arguments != null));
			for (int i = 0; i < constructorInitializer.Arguments.Count; i++) {
				Expression o = constructorInitializer.Arguments[i];
				Debug.Assert(o != null);
				nodeStack.Push(o);
				o.AcceptVisitor(this, data);
				o = (Expression)nodeStack.Pop();
				if (o == null)
					constructorInitializer.Arguments.RemoveAt(i--);
				else
					constructorInitializer.Arguments[i] = o;
			}
			return null;
		}
Beispiel #47
0
		public void VisitConstructorInitializer(ConstructorInitializer constructorInitializer)
		{
			StartNode(constructorInitializer);
			WriteToken(Roles.Colon);
			Space();
			if (constructorInitializer.ConstructorInitializerType == ConstructorInitializerType.This) {
				WriteKeyword(ConstructorInitializer.ThisKeywordRole);
			} else {
				WriteKeyword(ConstructorInitializer.BaseKeywordRole);
			}
			Space(policy.SpaceBeforeMethodCallParentheses);
			WriteCommaSeparatedListInParenthesis(constructorInitializer.Arguments, policy.SpaceWithinMethodCallParentheses);
			EndNode(constructorInitializer);
		}
 public ConstructorDeclaration(IEnumerable<ParameterDeclaration> parameters, ConstructorInitializer initializer)
     : base(parameters)
 {
     Initializer = initializer;
 }