Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
lwm1986
roslyn
提交
c40060f4
R
roslyn
项目概览
lwm1986
/
roslyn
与 Fork 源项目一致
从无法访问的项目Fork
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
roslyn
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
c40060f4
编写于
7月 19, 2017
作者:
H
Heejae Chang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
more refactoring
上级
c3e89aed
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
91 addition
and
74 deletion
+91
-74
src/Compilers/Core/Portable/Operations/Operation.cs
src/Compilers/Core/Portable/Operations/Operation.cs
+75
-69
src/Compilers/Core/Portable/Operations/OperationExtensions.cs
...Compilers/Core/Portable/Operations/OperationExtensions.cs
+16
-5
未找到文件。
src/Compilers/Core/Portable/Operations/Operation.cs
浏览文件 @
c40060f4
...
...
@@ -9,6 +9,7 @@
using
Microsoft.CodeAnalysis.Semantics
;
using
Microsoft.CodeAnalysis.Text
;
using
Roslyn.Utilities
;
using
Microsoft.CodeAnalysis.PooledObjects
;
namespace
Microsoft.CodeAnalysis
{
...
...
@@ -147,11 +148,23 @@ public override void Accept(OperationVisitor visitor)
return
visitor
.
VisitNoneOperation
(
this
,
argument
);
}
public
override
IEnumerable
<
IOperation
>
Children
=>
_getChildren
().
NullToEmpty
();
public
override
IEnumerable
<
IOperation
>
Children
{
get
{
foreach
(
var
child
in
_getChildren
().
NullToEmpty
().
WhereNotNull
())
{
yield
return
Operation
.
SetParentOperation
(
child
,
this
);
}
}
}
}
private
static
IOperation
WalkDownOperationToFindParent
(
HashSet
<
IOperation
>
operationAlreadyProcessed
,
IOperation
operation
,
TextSpan
span
)
private
static
readonly
ObjectPool
<
Queue
<
IOperation
>>
s_queuePool
=
new
ObjectPool
<
Queue
<
IOperation
>>(()
=>
new
Queue
<
IOperation
>(),
10
);
private
IOperation
WalkDownOperationToFindParent
(
HashSet
<
IOperation
>
operationAlreadyProcessed
,
IOperation
root
,
TextSpan
span
)
{
void
EnqueueChildOperations
(
Queue
<
IOperation
>
queue
,
IOperation
parent
)
{
...
...
@@ -162,96 +175,89 @@ void EnqueueChildOperations(Queue<IOperation> queue, IOperation parent)
}
}
// do we have a pool for queue?
var
parentChildQueue
=
new
Queue
<
IOperation
>();
EnqueueChildOperations
(
parentChildQueue
,
operation
);
var
operationQueue
=
s_queuePool
.
Allocate
();
// walk down the child operation to find parent operation
// every child returned by the queue should already have Parent operation set
IOperation
child
;
while
((
child
=
parentChildQueue
.
Dequeue
())
!=
null
)
try
{
if
(!
operationAlreadyProcessed
.
Add
(
child
))
{
// don't process IOperation we already processed otherwise,
// we can walk down same tree multiple times
continue
;
}
EnqueueChildOperations
(
operationQueue
,
root
);
if
(
child
==
operation
)
// walk down the tree to find parent operation
// every operation returned by the queue should already have Parent operation set
while
(
operationQueue
.
Count
>
0
)
{
// parent found
return
child
.
Parent
;
}
var
operation
=
operationQueue
.
Dequeue
();
if
(!
child
.
Syntax
.
FullSpan
.
IntersectsWith
(
span
))
{
// not related node, don't walk down
continue
;
if
(!
operationAlreadyProcessed
.
Add
(
operation
))
{
// don't process IOperation we already processed otherwise,
// we can walk down same tree multiple times
continue
;
}
if
(
operation
==
this
)
{
// parent found
return
operation
.
Parent
;
}
if
(!
operation
.
Syntax
.
FullSpan
.
IntersectsWith
(
span
))
{
// not related node, don't walk down
continue
;
}
// queue children so that we can do breadth first search
EnqueueChildOperations
(
operationQueue
,
operation
);
}
// queue children so that we can do breadth first search
EnqueueChildOperations
(
parentChildQueue
,
child
);
return
null
;
}
finally
{
operationQueue
.
Clear
();
s_queuePool
.
Free
(
operationQueue
);
}
return
null
;
}
private
IOperation
SearchParentOperation
()
{
// do we have a pool for hashset?
var
operationAlreadyProcessed
=
new
HashSet
<
IOperation
>();
var
operationAlreadyProcessed
=
PooledHashSet
<
IOperation
>.
GetInstance
();
var
targetNode
=
Syntax
;
var
currentCandidate
=
targetNode
.
Parent
;
while
(
currentCandidate
!=
null
)
{
Debug
.
Assert
(
currentCandidate
.
FullSpan
.
IntersectsWith
(
targetNode
.
FullSpan
));
// start from current node since one node can have multiple operations mapped to
var
currentCandidate
=
targetNode
;
foreach
(
var
childNode
in
currentCandidate
.
ChildNodes
())
try
{
while
(
currentCandidate
!=
null
)
{
if
(!
childNode
.
FullSpan
.
IntersectsWith
(
targetNode
.
FullSpan
))
{
// skip unrelated node
continue
;
}
Debug
.
Assert
(
currentCandidate
.
FullSpan
.
IntersectsWith
(
targetNode
.
FullSpan
));
// get
child
operation
var
childOperation
=
_semanticModel
.
GetOperationInternal
(
childNod
e
);
if
(
childOperation
!=
null
)
// get operation
var
tree
=
_semanticModel
.
GetOperationInternal
(
currentCandidat
e
);
if
(
tree
!=
null
)
{
// there is no operation for this node
continue
;
// walk down operation tree to see whether this tree contains parent of this operation
var
parent
=
WalkDownOperationToFindParent
(
operationAlreadyProcessed
,
tree
,
targetNode
.
FullSpan
);
if
(
parent
!=
null
)
{
return
parent
;
}
}
// record we have processed this node
if
(!
operationAlreadyProcessed
.
Add
(
childOperation
))
{
// we already processed this tree. no need to dig down
continue
;
}
// check easy case first
if
(
childOperation
==
this
)
{
// found parent, go up the spine until we found non-null parent Operation
return
currentCandidate
.
AncestorsAndSelf
().
Select
(
n
=>
_semanticModel
.
GetOperationInternal
(
n
)).
WhereNotNull
().
FirstOrDefault
();
}
// walk down child operation tree to see whether sub tree contains the given operation
var
parent
=
WalkDownOperationToFindParent
(
operationAlreadyProcessed
,
childOperation
,
targetNode
.
FullSpan
);
if
(
parent
!=
null
)
{
return
parent
;
}
// move up the tree
currentCandidate
=
currentCandidate
.
Parent
;
}
currentCandidate
=
currentCandidate
.
Parent
;
// root node. there is no parent
return
null
;
}
finally
{
// put the hashset back to the pool
operationAlreadyProcessed
.
Free
();
}
// root node. there is no parent
return
null
;
}
}
}
src/Compilers/Core/Portable/Operations/OperationExtensions.cs
浏览文件 @
c40060f4
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using
System
;
using
System.Collections.Generic
;
using
System.Collections.Immutable
;
using
System.Linq
;
...
...
@@ -27,6 +28,9 @@ public static bool HasErrors(this IOperation operation, Compilation compilation,
return
model
.
GetDiagnostics
(
operation
.
Syntax
.
Span
,
cancellationToken
).
Any
(
d
=>
d
.
DefaultSeverity
==
DiagnosticSeverity
.
Error
);
}
private
static
readonly
ObjectPool
<
Stack
<
IEnumerator
<
IOperation
>>>
s_childEnumeratorStackPool
=
new
ObjectPool
<
Stack
<
IEnumerator
<
IOperation
>>>(()
=>
new
Stack
<
IEnumerator
<
IOperation
>>(),
10
);
public
static
IEnumerable
<
IOperation
>
Descendants
(
this
IOperation
operation
)
{
if
(
operation
==
null
)
...
...
@@ -46,26 +50,33 @@ public static IEnumerable<IOperation> DescendantsAndSelf(this IOperation operati
yield
return
operation
;
var
stack
=
new
Stack
<
IEnumerator
<
IOperation
>>
();
var
stack
=
s_childEnumeratorStackPool
.
Allocate
();
stack
.
Push
(
operation
.
Children
.
GetEnumerator
());
IEnumerator
<
IOperation
>
iterator
;
while
((
iterator
=
stack
.
Pop
())
!=
null
)
while
(
stack
.
Count
>
0
)
{
var
iterator
=
stack
.
Pop
();
if
(!
iterator
.
MoveNext
())
{
continue
;
}
var
current
=
iterator
.
Current
;
yield
return
current
;
// push current iterator back in to the stack
stack
.
Push
(
iterator
);
// push children iterator to the stack
stack
.
Push
(
current
.
Children
.
GetEnumerator
());
if
(
current
!=
null
)
{
yield
return
current
;
stack
.
Push
(
current
.
Children
.
GetEnumerator
());
}
}
stack
.
Clear
();
s_childEnumeratorStackPool
.
Free
(
stack
);
}
public
static
IOperation
GetRootOperation
(
this
ISymbol
symbol
,
CancellationToken
cancellationToken
=
default
(
CancellationToken
))
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录