Пример #1
0
        protected override object VisitForEachStatement(ForEachStatementSyntax node)
        {
            var collection = InternalVisitExpression(node.Expression);

#if ROSLYN
            var info1 = ModelExtensions.GetDeclaredSymbol(context.RoslynModel, node);
            var info  = info1 as ILocalSymbol;
            if (info == null)
            {
                throw new ArgumentNullException("info");
            }
            var rosType          = context.Roslyn_ResolveType(info.Type);
            var loopVariableType = new LangType(rosType);
            var loopVariableName = info.Name;
#else
            var type = _VisitExpression(node.Type);
            if (type is UnknownIdentifierValue)
            {
                var xx = type as UnknownIdentifierValue;
                if (xx.Identifier == "var")
                {
                    var et = TypesUtil.GetEnumerateItemType(collection.ValueType);
                    type = new TypeValue(et);
                }
                else
                {
                    throw new NotSupportedException();
                }
            }
            Debug.Assert(type is TypeValue);
            Debug.Assert((type as TypeValue).DotnetType == rosType);
            var loopVariableType = new LangType((type as TypeValue).DotnetType);
            var loopVariableName = _Name(node.Identifier);
#endif

            var ps = new VariableDeclaration(
                loopVariableType,
                new[]
            {
                new VariableDeclarator(loopVariableName, null, null)
            }
                );
            return(context.DoWithLocalVariables(ps,
                                                () =>
            {
                var stat = Visit(node.Statement);
                Debug.Assert(stat is IStatement);
                var g = new ForEachStatement(loopVariableType, loopVariableName, collection, stat as IStatement);
                return g;
            }));
        }