Esempio n. 1
0
        protected override void ExtractInitializers(TextWriter trapFile)
        {
            // Do not extract initializers for constructed types.
            if (!IsSourceDeclaration)
            {
                return;
            }

            var syntax      = Syntax;
            var initializer = syntax?.Initializer;

            if (initializer is null)
            {
                if (Symbol.MethodKind is MethodKind.Constructor)
                {
                    var baseType = Symbol.ContainingType.BaseType;
                    if (baseType is null)
                    {
                        if (Symbol.ContainingType.SpecialType != SpecialType.System_Object)
                        {
                            Context.ModelError(Symbol, "Unable to resolve base type in implicit constructor initializer");
                        }
                        return;
                    }

                    var baseConstructor = baseType.InstanceConstructors.FirstOrDefault(c => c.Arity is 0);

                    if (baseConstructor is null)
                    {
                        Context.ModelError(Symbol, "Unable to resolve implicit constructor initializer call");
                        return;
                    }

                    var baseConstructorTarget = Create(Context, baseConstructor);
                    var info = new ExpressionInfo(Context,
                                                  AnnotatedTypeSymbol.CreateNotAnnotated(baseType),
                                                  Location,
                                                  Kinds.ExprKind.CONSTRUCTOR_INIT,
                                                  this,
                                                  -1,
                                                  isCompilerGenerated: true,
                                                  null);

                    trapFile.expr_call(new Expression(info), baseConstructorTarget);
                }
                return;
            }

            ITypeSymbol initializerType;
            var         symbolInfo = Context.GetSymbolInfo(initializer);

            switch (initializer.Kind())
            {
            case SyntaxKind.BaseConstructorInitializer:
                initializerType = Symbol.ContainingType.BaseType !;
                break;

            case SyntaxKind.ThisConstructorInitializer:
                initializerType = Symbol.ContainingType;
                break;

            default:
                Context.ModelError(initializer, "Unknown initializer");
                return;
            }

            var initInfo = new ExpressionInfo(Context,
                                              AnnotatedTypeSymbol.CreateNotAnnotated(initializerType),
                                              Context.CreateLocation(initializer.ThisOrBaseKeyword.GetLocation()),
                                              Kinds.ExprKind.CONSTRUCTOR_INIT,
                                              this,
                                              -1,
                                              false,
                                              null);

            var init = new Expression(initInfo);

            var target = Constructor.Create(Context, (IMethodSymbol?)symbolInfo.Symbol);

            if (target is null)
            {
                Context.ModelError(Symbol, "Unable to resolve call");
                return;
            }

            trapFile.expr_call(init, target);

            var child = 0;

            foreach (var arg in initializer.ArgumentList.Arguments)
            {
                Expression.Create(Context, arg.Expression, init, child++);
            }
        }
        protected override void ExtractInitializers(TextWriter trapFile)
        {
            // Do not extract initializers for constructed types.
            if (!IsSourceDeclaration)
            {
                return;
            }

            var syntax      = Syntax;
            var initializer = syntax == null ? null : syntax.Initializer;

            if (initializer == null)
            {
                return;
            }

            Type initializerType;
            var  symbolInfo = Context.GetSymbolInfo(initializer);

            switch (initializer.Kind())
            {
            case SyntaxKind.BaseConstructorInitializer:
                initializerType = Type.Create(Context, symbol.ContainingType.BaseType);
                break;

            case SyntaxKind.ThisConstructorInitializer:
                initializerType = ContainingType;
                break;

            default:
                Context.ModelError(initializer, "Unknown initializer");
                return;
            }

            var initInfo = new ExpressionInfo(Context,
                                              new AnnotatedType(initializerType, NullableAnnotation.None),
                                              Context.Create(initializer.ThisOrBaseKeyword.GetLocation()),
                                              Kinds.ExprKind.CONSTRUCTOR_INIT,
                                              this,
                                              -1,
                                              false,
                                              null);

            var init = new Expression(initInfo);

            var target = Constructor.Create(Context, (IMethodSymbol)symbolInfo.Symbol);

            if (target == null)
            {
                Context.ModelError(symbol, "Unable to resolve call");
                return;
            }

            trapFile.expr_call(init, target);

            int child = 0;

            foreach (var arg in initializer.ArgumentList.Arguments)
            {
                Expression.Create(Context, arg.Expression, init, child++);
            }
        }