【问题标题】:Lowered operations in roslyn降低罗斯林的操作
【发布时间】:2016-09-13 10:59:03
【问题描述】:

在 Roslyn 中引入操作时,其中一个目标是提供低级操作(我认为是在设计评审会议视频中),据我所知,它应该为高级操作上的隐式编译器操作提供显式操作。我在 Roslyn 看到了降低目录,但类是内部的。现在可以降低操作还是没有可用的公共 API?

在下面的示例中,操作已经删除了一些隐式部分 - 为表达式主体添加返回语句并为重载运算符公开符号。但前后增量仅在种类上有所不同。

using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Semantics;
using System.Linq;

namespace so39468373
{
    internal static class Program
    {
        private static void Main()
        {
            var tree = CSharpSyntaxTree.ParseText(@"
public class c
{
    public static c operator ++(c o) { return o; }
    static c pre(c o) => ++o;
    static c post(c o) => o++;
    public static void Main() {}
}");
            var mscorlib = MetadataReference.CreateFromFile(typeof(object).Assembly.Location);
            var compilation = CSharpCompilation.Create(null, new[] { tree }, new[] { mscorlib });
            var model = compilation.GetSemanticModel(tree);
            foreach (var node in tree.GetRoot().DescendantNodes().OfType<ArrowExpressionClauseSyntax>())
            {
                var operation = model.GetOperation(node);
                var block = (IBlockStatement)operation;
                var statement = (IReturnStatement)block.Statements.First();
                var increment = (IIncrementExpression)statement.ReturnedValue;
                // How to get lowered operations for increment here?
            }
        }
    }
}

github上的代码-https://github.com/isanych/so-39468373

【问题讨论】:

标签: c# roslyn


【解决方案1】:

这个答案的不同角度——编译器的这个方面呢?

InternalVisibleTo Attribute

链接:https://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.internalsvisibletoattribute(v=vs.110).aspx

可能会提出一个与“不同角度”相关的有趣话题来进行调试?

更多信息(来自文章):

版本信息

通用 Windows 平台 从 8 开始可用 .NET 框架
自 2.0 起可用 可移植类库
支持:便携式 .NET 平台 银光
自 2.0 起可用 Windows Phone Silverlight
自 7.0 起可用 Windows 手机
从 8.1 开始提供

【讨论】:

    【解决方案2】:

    检查这个例子将帮助你解决他的问题

    using System.Collections.Immutable;
    using System.Diagnostics;
    using Microsoft.CodeAnalysis.CSharp.Symbols;
    using Microsoft.CodeAnalysis.CSharp.Syntax;
    using Microsoft.CodeAnalysis.Text;
    
    namespace Microsoft.CodeAnalysis.CSharp
    {
        /// <summary>
        /// The dynamic operation factories below return this struct so that the caller
        /// have the option of separating the call-site initialization from its invocation.
        /// 
        /// Most callers just call <see cref="ToExpression"/> to get the combo but some (object and array initializers) 
        /// hoist all call-site initialization code and emit multiple invocations of the same site.
        /// </summary>
        internal struct LoweredDynamicOperation
        {
            private readonly SyntheticBoundNodeFactory _factory;
            private readonly TypeSymbol _resultType;
            private readonly ImmutableArray<LocalSymbol> _temps;
            public readonly BoundExpression SiteInitialization;
            public readonly BoundExpression SiteInvocation;
    
            public LoweredDynamicOperation(SyntheticBoundNodeFactory factory, BoundExpression siteInitialization, BoundExpression siteInvocation, TypeSymbol resultType, ImmutableArray<LocalSymbol> temps)
            {
                _factory = factory;
                _resultType = resultType;
                _temps = temps;
                this.SiteInitialization = siteInitialization;
                this.SiteInvocation = siteInvocation;
            }
    
            public static LoweredDynamicOperation Bad(
                BoundExpression loweredReceiver,
                ImmutableArray<BoundExpression> loweredArguments,
                BoundExpression loweredRight,
                TypeSymbol resultType)
            {
                var children = ArrayBuilder<BoundNode>.GetInstance();
                children.AddOptional(loweredReceiver);
                children.AddRange(loweredArguments);
                children.AddOptional(loweredRight);
    
                return LoweredDynamicOperation.Bad(resultType, children.ToImmutableAndFree());
            }
    
            public static LoweredDynamicOperation Bad(TypeSymbol resultType, ImmutableArray<BoundNode> children)
            {
                Debug.Assert(children.Length > 0);
                var bad = new BoundBadExpression(children[0].Syntax, LookupResultKind.Empty, ImmutableArray<Symbol>.Empty, children, resultType);
                return new LoweredDynamicOperation(null, null, bad, resultType, default(ImmutableArray<LocalSymbol>));
            }
    
            public BoundExpression ToExpression()
            {
                if (_factory == null)
                {
                    Debug.Assert(SiteInitialization == null && SiteInvocation is BoundBadExpression && _temps.IsDefaultOrEmpty);
                    return SiteInvocation;
                }
    
                // TODO (tomat): we might be able to use SiteInvocation.Type instead of resultType once we stop using GetLoweredType
                if (_temps.IsDefaultOrEmpty)
                {
                    return _factory.Sequence(new[] { SiteInitialization }, SiteInvocation, _resultType);
                }
                else
                {
                    return new BoundSequence(_factory.Syntax, _temps, ImmutableArray.Create(SiteInitialization), SiteInvocation, _resultType) { WasCompilerGenerated = true };
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-16
      • 2021-11-07
      • 2019-12-24
      • 2016-08-17
      • 1970-01-01
      相关资源
      最近更新 更多