Exemple #1
0
        public static Method?Create(Context cx, IMethodSymbol?methodDecl)
        {
            if (methodDecl is null)
            {
                return(null);
            }

            var methodKind = methodDecl.MethodKind;

            if (methodKind == MethodKind.ExplicitInterfaceImplementation)
            {
                // Retrieve the original method kind
                methodKind = methodDecl.ExplicitInterfaceImplementations.Select(m => m.MethodKind).FirstOrDefault();
            }

            switch (methodKind)
            {
            case MethodKind.StaticConstructor:
            case MethodKind.Constructor:
                return(Constructor.Create(cx, methodDecl));

            case MethodKind.ReducedExtension:
                if (SymbolEqualityComparer.Default.Equals(methodDecl, methodDecl.ConstructedFrom))
                {
                    return(OrdinaryMethod.Create(cx, methodDecl.ReducedFrom !));
                }
                return(OrdinaryMethod.Create(cx, methodDecl.ReducedFrom !.Construct(methodDecl.TypeArguments, methodDecl.TypeArgumentNullableAnnotations)));

            case MethodKind.Ordinary:
            case MethodKind.DelegateInvoke:
                return(OrdinaryMethod.Create(cx, methodDecl));

            case MethodKind.Destructor:
                return(Destructor.Create(cx, methodDecl));

            case MethodKind.PropertyGet:
            case MethodKind.PropertySet:
                return(Accessor.GetPropertySymbol(methodDecl) is null?OrdinaryMethod.Create(cx, methodDecl) : (Method)Accessor.Create(cx, methodDecl));

            case MethodKind.EventAdd:
            case MethodKind.EventRemove:
                return(EventAccessor.Create(cx, methodDecl));

            case MethodKind.UserDefinedOperator:
            case MethodKind.BuiltinOperator:
                return(UserOperator.Create(cx, methodDecl));

            case MethodKind.Conversion:
                return(Conversion.Create(cx, methodDecl));

            case MethodKind.AnonymousFunction:
                throw new InternalError(methodDecl, "Attempt to create a lambda");

            case MethodKind.LocalFunction:
                return(LocalFunction.Create(cx, methodDecl));

            default:
                throw new InternalError(methodDecl, $"Unhandled method '{methodDecl}' of kind '{methodDecl.MethodKind}'");
            }
        }
Exemple #2
0
        /// <summary>
        /// Creates a method of the appropriate subtype.
        /// </summary>
        /// <param name="cx"></param>
        /// <param name="methodDecl"></param>
        /// <returns></returns>
        public static Method Create(Context cx, IMethodSymbol methodDecl)
        {
            if (methodDecl == null)
            {
                return(null);
            }

            var methodKind = methodDecl.MethodKind;

            if (methodKind == MethodKind.ExplicitInterfaceImplementation)
            {
                // Retrieve the original method kind
                methodKind = methodDecl.ExplicitInterfaceImplementations.Select(m => m.MethodKind).FirstOrDefault();
            }

            switch (methodKind)
            {
            case MethodKind.StaticConstructor:
            case MethodKind.Constructor:
                return(Constructor.Create(cx, methodDecl));

            case MethodKind.ReducedExtension:
            case MethodKind.Ordinary:
            case MethodKind.DelegateInvoke:
                return(OrdinaryMethod.Create(cx, methodDecl));

            case MethodKind.Destructor:
                return(Destructor.Create(cx, methodDecl));

            case MethodKind.PropertyGet:
            case MethodKind.PropertySet:
                return(methodDecl.AssociatedSymbol is null?OrdinaryMethod.Create(cx, methodDecl) : (Method)Accessor.Create(cx, methodDecl));

            case MethodKind.EventAdd:
            case MethodKind.EventRemove:
                return(EventAccessor.Create(cx, methodDecl));

            case MethodKind.UserDefinedOperator:
            case MethodKind.BuiltinOperator:
                return(UserOperator.Create(cx, methodDecl));

            case MethodKind.Conversion:
                return(Conversion.Create(cx, methodDecl));

            case MethodKind.AnonymousFunction:
                throw new InternalError(methodDecl, "Attempt to create a lambda");

            case MethodKind.LocalFunction:
                return(LocalFunction.Create(cx, methodDecl));

            default:
                throw new InternalError(methodDecl, "Unhandled method '{0}' of kind '{1}'", methodDecl, methodDecl.MethodKind);
            }
        }
Exemple #3
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++);
            }
        }