Esempio n. 1
0
        public static Expression CreateParenthesized(Context cx, VarPatternSyntax varPattern, ParenthesizedVariableDesignationSyntax designation, IExpressionParentEntity parent, int child)
        {
            var type  = NullType.Create(cx); // Should ideally be a corresponding tuple type
            var tuple = new Expression(new ExpressionInfo(cx, type, cx.Create(varPattern.GetLocation()), ExprKind.TUPLE, parent, child, false, null));

            cx.Try(null, null, () =>
            {
                var child0 = 0;
                foreach (var variable in designation.Variables)
                {
                    switch (variable)
                    {
                    case ParenthesizedVariableDesignationSyntax paren:
                        CreateParenthesized(cx, varPattern, paren, tuple, child0++);
                        break;

                    case SingleVariableDesignationSyntax single:
                        if (cx.GetModel(variable).GetDeclaredSymbol(single) is ILocalSymbol local)
                        {
                            var decl     = Create(cx, variable, Entities.Type.Create(cx, local.GetAnnotatedType()), true, tuple, child0++);
                            var id       = single.Identifier;
                            var location = cx.Create(id.GetLocation());
                            LocalVariable.Create(cx, local, decl, true, location);
                        }
                        else
                        {
                            throw new InternalError(single, "Failed to access local variable");
                        }
                        break;

                    case DiscardDesignationSyntax discard:
                        new Discard(cx, discard, tuple, child0++);
                        break;

                    default:
                        throw new InternalError(variable, "Unhandled designation type");
                    }
                }
            });

            return(tuple);
        }
Esempio n. 2
0
        public static Expression CreateParenthesized(Context cx, VarPatternSyntax varPattern, ParenthesizedVariableDesignationSyntax designation, IExpressionParentEntity parent, int child)
        {
            var tuple = new Expression(
                new ExpressionInfo(cx, null, cx.CreateLocation(varPattern.GetLocation()), ExprKind.TUPLE, parent, child, false, null),
                shouldPopulate: false);

            var elementTypes = new List <ITypeSymbol?>();

            cx.Try(null, null, () =>
            {
                var child0 = 0;
                foreach (var variable in designation.Variables)
                {
                    Expression sub;
                    switch (variable)
                    {
                    case ParenthesizedVariableDesignationSyntax paren:
                        sub = CreateParenthesized(cx, varPattern, paren, tuple, child0++);
                        break;

                    case SingleVariableDesignationSyntax single:
                        if (cx.GetModel(variable).GetDeclaredSymbol(single) is ILocalSymbol local)
                        {
                            var decl = Create(cx, variable, local.GetAnnotatedType(), tuple, child0++);
                            var l    = LocalVariable.Create(cx, local);
                            l.PopulateManual(decl, true);
                            sub = decl;
                        }
                        else
                        {
                            throw new InternalError(single, "Failed to access local variable");
                        }
                        break;

                    case DiscardDesignationSyntax discard:
                        sub = new Discard(cx, discard, tuple, child0++);
                        if (!sub.Type.HasValue || sub.Type.Value.Symbol is null)
                        {
                            // The type is only updated in memory, it will not be written to the trap file.
                            sub.SetType(cx.Compilation.GetSpecialType(SpecialType.System_Object));
                        }
                        break;

                    default:
                        var type = variable.GetType().ToString() ?? "null";
                        throw new InternalError(variable, $"Unhandled designation type {type}");
                    }

                    elementTypes.Add(sub.Type.HasValue && sub.Type.Value.Symbol?.Kind != SymbolKind.ErrorType
                        ? sub.Type.Value.Symbol
                        : null);
                }
            });

            INamedTypeSymbol?tupleType = null;

            if (!elementTypes.Any(et => et is null))
            {
                tupleType = cx.Compilation.CreateTupleTypeSymbol(elementTypes.ToImmutableArray() !);
            }

            tuple.SetType(tupleType);
            tuple.TryPopulate();

            return(tuple);
        }