Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
ee22d0b9
K
kotlin
项目概览
硅谷海盗
/
kotlin
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kotlin
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
ee22d0b9
编写于
6月 06, 2020
作者:
I
Ilya Kirillov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
FIR IDE: introduce TypeInfo as a wrapper for types in high level API
Needed for correct handling types lifecycle
上级
115327b9
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
172 addition
and
38 deletion
+172
-38
idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/highlighter/visitors/ExpressionsSmartcastHighlightingVisitor.kt
...ghter/visitors/ExpressionsSmartcastHighlightingVisitor.kt
+2
-2
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/FrontendAnalysisSession.kt
...rains/kotlin/idea/frontend/api/FrontendAnalysisSession.kt
+4
-5
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/ImplicitReceiverSmartCast.kt
...ins/kotlin/idea/frontend/api/ImplicitReceiverSmartCast.kt
+1
-3
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/TypeInfo.kt
...pi/src/org/jetbrains/kotlin/idea/frontend/api/TypeInfo.kt
+28
-0
idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/ConeTypeInfo.kt
...rg/jetbrains/kotlin/idea/frontend/api/fir/ConeTypeInfo.kt
+98
-0
idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/FirAnalysisSession.kt
...brains/kotlin/idea/frontend/api/fir/FirAnalysisSession.kt
+39
-28
未找到文件。
idea/idea-fir/src/org/jetbrains/kotlin/idea/fir/highlighter/visitors/ExpressionsSmartcastHighlightingVisitor.kt
浏览文件 @
ee22d0b9
...
...
@@ -29,7 +29,7 @@ internal class ExpressionsSmartcastHighlightingVisitor(
KotlinIdeaAnalysisBundle
.
message
(
"0.smart.cast.to.1"
,
receiverName
,
analysisSession
.
renderType
(
type
)
type
.
asDenotableTypeStringRepresentation
(
)
)
).
textAttributes
=
org
.
jetbrains
.
kotlin
.
idea
.
highlighter
.
KotlinHighlightingColors
.
SMART_CAST_RECEIVER
}
...
...
@@ -39,7 +39,7 @@ internal class ExpressionsSmartcastHighlightingVisitor(
getSmartCastTarget
(
expression
),
KotlinIdeaAnalysisBundle
.
message
(
"smart.cast.to.0"
,
analysisSession
.
renderType
(
type
)
type
.
asDenotableTypeStringRepresentation
(
)
)
).
textAttributes
=
org
.
jetbrains
.
kotlin
.
idea
.
highlighter
.
KotlinHighlightingColors
.
SMART_CAST_VALUE
}
...
...
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/FrontendAnalysisSession.kt
浏览文件 @
ee22d0b9
...
...
@@ -5,20 +5,19 @@
package
org.jetbrains.kotlin.idea.frontend.api
import
com.intellij.psi.PsiElement
import
org.jetbrains.kotlin.diagnostics.Diagnostic
import
org.jetbrains.kotlin.name.ClassId
import
org.jetbrains.kotlin.psi.*
import
org.jetbrains.kotlin.types.model.KotlinTypeMarker
abstract
class
FrontendAnalysisSession
:
Invalidatable
{
abstract
fun
getSmartCastedToTypes
(
expression
:
KtExpression
):
Collection
<
TypeInfo
>?
abstract
fun
getImplicitReceiverSmartCasts
(
expression
:
KtExpression
):
Collection
<
ImplicitReceiverSmartCast
>
abstract
fun
getReturnTypeForKtDeclaration
(
declaration
:
KtDeclaration
):
KotlinTypeMarker
?
abstract
fun
getReturnTypeForKtDeclaration
(
declaration
:
KtDeclaration
):
TypeInfo
?
abstract
fun
renderType
(
type
:
KotlinTypeMarker
):
String
abstract
fun
getKtExpressionType
(
expression
:
KtExpression
):
KotlinTypeMarker
?
abstract
fun
getKtExpressionType
(
expression
:
KtExpression
):
TypeInfo
?
abstract
fun
isSubclassOf
(
klass
:
KtClassOrObject
,
superClassId
:
ClassId
):
Boolean
...
...
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/ImplicitReceiverSmartCast.kt
浏览文件 @
ee22d0b9
...
...
@@ -5,9 +5,7 @@
package
org.jetbrains.kotlin.idea.frontend.api
import
org.jetbrains.kotlin.types.model.KotlinTypeMarker
data class
ImplicitReceiverSmartCast
(
val
types
:
Collection
<
KotlinTypeMarker
>,
val
kind
:
ImplicitReceiverSmartcastKind
)
data class
ImplicitReceiverSmartCast
(
val
types
:
Collection
<
TypeInfo
>,
val
kind
:
ImplicitReceiverSmartcastKind
)
enum
class
ImplicitReceiverSmartcastKind
{
DISPATCH
,
EXTENSION
...
...
idea/idea-frontend-api/src/org/jetbrains/kotlin/idea/frontend/api/TypeInfo.kt
0 → 100644
浏览文件 @
ee22d0b9
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package
org.jetbrains.kotlin.idea.frontend.api
import
org.jetbrains.kotlin.name.ClassId
abstract
class
TypeInfo
:
Invalidatable
{
abstract
fun
isClassType
():
Boolean
abstract
fun
classIdIfClassTypeOrError
():
ClassId
abstract
fun
isErrorType
():
Boolean
abstract
fun
asDenotableTypeStringRepresentation
():
String
abstract
fun
isEqualTo
(
other
:
TypeInfo
):
Boolean
abstract
fun
isSubTypeOf
(
superType
:
TypeInfo
):
Boolean
abstract
fun
isDefinitelyNullable
():
Boolean
abstract
fun
isDefinitelyNotNull
():
Boolean
override
fun
toString
():
String
=
asDenotableTypeStringRepresentation
()
}
class
ErrorTypeClassIdAccessException
(
override
val
message
:
String
?
=
null
)
:
IllegalStateException
()
class
ClassTypeExpectedException
(
override
val
message
:
String
?
=
null
)
:
IllegalStateException
()
\ No newline at end of file
idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/ConeTypeInfo.kt
0 → 100644
浏览文件 @
ee22d0b9
/*
* Copyright 2010-2020 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package
org.jetbrains.kotlin.idea.frontend.api.fir
import
org.jetbrains.kotlin.fir.types.*
import
org.jetbrains.kotlin.fir.types.impl.ConeClassLikeTypeImpl
import
org.jetbrains.kotlin.idea.frontend.api.ClassTypeExpectedException
import
org.jetbrains.kotlin.idea.frontend.api.ErrorTypeClassIdAccessException
import
org.jetbrains.kotlin.idea.frontend.api.Invalidatable
import
org.jetbrains.kotlin.idea.frontend.api.TypeInfo
import
org.jetbrains.kotlin.name.ClassId
import
org.jetbrains.kotlin.types.AbstractTypeChecker
import
org.jetbrains.kotlin.types.AbstractTypeCheckerContext
import
java.lang.ref.WeakReference
internal
class
ConeTypeInfo
(
coneType
:
ConeKotlinType
,
private
val
typeCheckerContext
:
ConeTypeCheckerContext
,
private
val
validityToken
:
Invalidatable
)
:
TypeInfo
()
{
private
val
coneTypeWeakRef
=
WeakReference
(
coneType
)
override
fun
isClassType
():
Boolean
{
assertIsValid
()
return
coneType
is
ConeClassLikeType
}
override
fun
isErrorType
():
Boolean
{
assertIsValid
()
return
coneType
is
ConeClassErrorType
}
override
fun
classIdIfClassTypeOrError
():
ClassId
{
assertIsValid
()
return
when
(
val
coneType
=
coneType
)
{
is
ConeClassLikeTypeImpl
->
coneType
.
lookupTag
.
classId
is
ConeClassErrorType
->
throw
ErrorTypeClassIdAccessException
()
else
->
throw
ClassTypeExpectedException
()
}
}
override
fun
asDenotableTypeStringRepresentation
():
String
{
assertIsValid
()
return
coneType
.
render
()
//TODO
}
override
fun
isEqualTo
(
other
:
TypeInfo
):
Boolean
{
assertIsValid
()
other
.
assertIsValid
()
check
(
other
is
ConeTypeInfo
)
return
AbstractTypeChecker
.
equalTypes
(
typeCheckerContext
as
AbstractTypeCheckerContext
,
coneType
,
other
.
coneType
)
}
override
fun
isSubTypeOf
(
superType
:
TypeInfo
):
Boolean
{
assertIsValid
()
superType
.
assertIsValid
()
check
(
superType
is
ConeTypeInfo
)
return
AbstractTypeChecker
.
isSubtypeOf
(
typeCheckerContext
as
AbstractTypeCheckerContext
,
coneType
,
superType
.
coneType
)
}
override
fun
isDefinitelyNullable
():
Boolean
{
assertIsValid
()
return
coneType
.
nullability
==
ConeNullability
.
NULLABLE
}
override
fun
isDefinitelyNotNull
():
Boolean
{
assertIsValid
()
return
coneType
.
nullability
==
ConeNullability
.
NOT_NULL
}
override
fun
isValid
():
Boolean
{
if
(
coneTypeWeakRef
.
get
()
==
null
)
return
false
return
validityToken
.
isValid
()
}
override
fun
invalidationReason
():
String
{
if
(
coneTypeWeakRef
.
get
()
==
null
)
return
"Cone type was garbage collected"
return
validityToken
.
invalidationReason
()
}
private
inline
val
coneType
get
()
=
coneTypeWeakRef
.
get
()
?:
if
(
validityToken
.
isValid
())
{
error
(
"Cone type was garbage collected while analysis session is still valid"
)
}
else
{
error
(
"Accessing the invalid coneType"
)
}
}
\ No newline at end of file
idea/idea-frontend-fir/src/org/jetbrains/kotlin/idea/frontend/api/fir/FirAnalysisSession.kt
浏览文件 @
ee22d0b9
...
...
@@ -12,10 +12,8 @@ import com.intellij.psi.PsiMethod
import
org.jetbrains.kotlin.builtins.KotlinBuiltIns
import
org.jetbrains.kotlin.diagnostics.Diagnostic
import
org.jetbrains.kotlin.fir.FirElement
import
org.jetbrains.kotlin.fir.declarations.FirCallableDeclaration
import
org.jetbrains.kotlin.fir.declarations.FirClass
import
org.jetbrains.kotlin.fir.declarations.FirResolvePhase
import
org.jetbrains.kotlin.fir.declarations.isSuspend
import
org.jetbrains.kotlin.fir.FirSession
import
org.jetbrains.kotlin.fir.declarations.*
import
org.jetbrains.kotlin.fir.expressions.FirExpression
import
org.jetbrains.kotlin.fir.expressions.FirExpressionWithSmartcast
import
org.jetbrains.kotlin.fir.expressions.FirFunctionCall
...
...
@@ -25,10 +23,7 @@ import org.jetbrains.kotlin.fir.resolve.transformers.firClassLike
import
org.jetbrains.kotlin.fir.symbols.CallableId
import
org.jetbrains.kotlin.fir.symbols.impl.FirConstructorSymbol
import
org.jetbrains.kotlin.fir.symbols.impl.FirNamedFunctionSymbol
import
org.jetbrains.kotlin.fir.types.ConeKotlinType
import
org.jetbrains.kotlin.fir.types.FirResolvedTypeRef
import
org.jetbrains.kotlin.fir.types.coneTypeSafe
import
org.jetbrains.kotlin.fir.types.render
import
org.jetbrains.kotlin.fir.types.*
import
org.jetbrains.kotlin.idea.fir.getOrBuildFir
import
org.jetbrains.kotlin.idea.fir.getOrBuildFirSafe
import
org.jetbrains.kotlin.idea.fir.isImplicitFunctionCall
...
...
@@ -40,19 +35,24 @@ import org.jetbrains.kotlin.idea.references.FirReferenceResolveHelper.toTargetPs
import
org.jetbrains.kotlin.name.ClassId
import
org.jetbrains.kotlin.name.Name
import
org.jetbrains.kotlin.psi.*
import
org.jetbrains.kotlin.types.model.KotlinTypeMarker
internal
class
FirAnalysisSession
(
class
FirAnalysisSession
(
project
:
Project
)
:
FrontendAnalysisSession
(),
Invalidatable
by
ReadActionConfinementValidityToken
(
project
)
{
)
:
FrontendAnalysisSession
()
{
private
val
validityToken
=
ReadActionConfinementValidityToken
(
project
)
override
fun
isValid
():
Boolean
=
validityToken
.
isValid
()
override
fun
invalidationReason
():
String
=
validityToken
.
invalidationReason
()
constructor
(
element
:
KtElement
)
:
this
(
element
.
project
)
init
{
assertIsValid
()
}
override
fun
getSmartCastedToTypes
(
expression
:
KtExpression
):
Collection
<
KotlinTypeMarker
>?
{
override
fun
getSmartCastedToTypes
(
expression
:
KtExpression
):
Collection
<
TypeInfo
>?
{
assertIsValid
()
// TODO filter out not used smartcasts
return
expression
.
getOrBuildFirSafe
<
FirExpressionWithSmartcast
>()
?.
typesFromSmartCast
return
expression
.
getOrBuildFirSafe
<
FirExpressionWithSmartcast
>()
?.
typesFromSmartCast
?.
map
{
it
.
asTypeInfo
(
expression
.
session
)
}
}
@OptIn
(
ExperimentalStdlibApi
::
class
)
...
...
@@ -63,35 +63,39 @@ internal class FirAnalysisSession(
if
(
qualifiedExpression
.
dispatchReceiver
!
is
FirExpressionWithSmartcast
&&
qualifiedExpression
.
extensionReceiver
!
is
FirExpressionWithSmartcast
)
return
emptyList
()
val
session
=
expression
.
session
return
buildList
{
(
qualifiedExpression
.
dispatchReceiver
as
?
FirExpressionWithSmartcast
)
?.
let
{
smartCasted
->
ImplicitReceiverSmartCast
(
smartCasted
.
typesFromSmartCast
,
ImplicitReceiverSmartcastKind
.
DISPATCH
)
ImplicitReceiverSmartCast
(
smartCasted
.
typesFromSmartCast
.
map
{
it
.
asTypeInfo
(
session
)
},
ImplicitReceiverSmartcastKind
.
DISPATCH
)
}
?.
let
(
::
add
)
(
qualifiedExpression
.
extensionReceiver
as
?
FirExpressionWithSmartcast
)
?.
let
{
smartCasted
->
ImplicitReceiverSmartCast
(
smartCasted
.
typesFromSmartCast
,
ImplicitReceiverSmartcastKind
.
EXTENSION
)
ImplicitReceiverSmartCast
(
smartCasted
.
typesFromSmartCast
.
map
{
it
.
asTypeInfo
(
session
)
},
ImplicitReceiverSmartcastKind
.
EXTENSION
)
}
?.
let
(
::
add
)
}
}
override
fun
renderType
(
type
:
KotlinTypeMarker
):
String
{
assertIsValid
()
return
type
.
asConeType
().
render
()
}
override
fun
getReturnTypeForKtDeclaration
(
declaration
:
KtDeclaration
):
KotlinTypeMarker
?
{
override
fun
getReturnTypeForKtDeclaration
(
declaration
:
KtDeclaration
):
TypeInfo
?
{
assertIsValid
()
return
declaration
.
toFir
<
FirCallableDeclaration
<
*
>>()
?.
returnTypeRef
?.
coneTypeSafe
()
val
firDeclaration
=
declaration
.
toFir
<
FirCallableDeclaration
<
*
>>()
?:
return
null
return
firDeclaration
.
returnTypeRef
.
coneTypeSafe
<
ConeKotlinType
>()
?.
asTypeInfo
(
declaration
.
session
)
}
override
fun
getKtExpressionType
(
expression
:
KtExpression
):
ConeKotlinType
?
{
override
fun
getKtExpressionType
(
expression
:
KtExpression
):
TypeInfo
?
{
assertIsValid
()
return
expression
.
toFir
<
FirExpression
>()
?.
typeRef
?.
coneTypeSafe
(
)
return
expression
.
toFir
<
FirExpression
>()
?.
typeRef
?.
coneTypeSafe
<
ConeKotlinType
>()
?.
asTypeInfo
(
expression
.
session
)
}
override
fun
isSubclassOf
(
klass
:
KtClassOrObject
,
superClassId
:
ClassId
):
Boolean
{
assertIsValid
()
var
result
=
false
forEachSu
b
Class
(
klass
.
toFir
()
?:
return
false
)
{
type
->
forEachSu
per
Class
(
klass
.
toFir
()
?:
return
false
)
{
type
->
result
=
result
||
type
.
firClassLike
(
klass
.
session
)
?.
symbol
?.
classId
==
superClassId
}
return
result
...
...
@@ -155,16 +159,23 @@ internal class FirAnalysisSession(
private
inline
fun
<
reified
F
:
FirElement
>
KtElement
.
toFir
(
phase
:
FirResolvePhase
=
FirResolvePhase
.
BODY_RESOLVE
):
F
?
=
getOrBuildFir
(
phase
)
as
?
F
private
fun
forEachSu
b
Class
(
firClass
:
FirClass
<
*
>,
action
:
(
FirResolvedTypeRef
)
->
Unit
)
{
private
fun
forEachSu
per
Class
(
firClass
:
FirClass
<
*
>,
action
:
(
FirResolvedTypeRef
)
->
Unit
)
{
firClass
.
superTypeRefs
.
forEach
{
superType
->
(
superType
as
?
FirResolvedTypeRef
)
?.
let
(
action
)
(
superType
.
firClassLike
(
firClass
.
session
)
as
?
FirClass
<
*
>?)
?.
let
{
forEachSu
b
Class
(
it
,
action
)
}
(
superType
.
firClassLike
(
firClass
.
session
)
as
?
FirClass
<
*
>?)
?.
let
{
forEachSu
per
Class
(
it
,
action
)
}
}
}
private
fun
ConeKotlinType
.
asTypeInfo
(
session
:
FirSession
)
=
ConeTypeInfo
(
this
,
createTypeCheckingContext
(
session
),
validityToken
)
private
fun
createTypeCheckingContext
(
session
:
FirSession
)
=
ConeTypeCheckerContext
(
isErrorTypeEqualsToAnything
=
true
,
// TODO?
isStubTypeEqualsToAnything
=
true
,
// TODO?
session
=
session
)
companion
object
{
private
fun
KotlinTypeMarker
.
asConeType
():
ConeKotlinType
=
this
as
?
ConeKotlinType
?:
error
(
"$this should be ConeKotlinType"
)
private
val
kotlinFunctionInvokeCallableIds
=
(
0
..
23
).
flatMapTo
(
hashSetOf
())
{
arity
->
listOf
(
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录