Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
硅谷海盗
kotlin
提交
c943525e
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,发现更多精彩内容 >>
提交
c943525e
编写于
5月 20, 2011
作者:
D
Dmitry Jemerov
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
generate new array creation
上级
0afddb4d
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
66 addition
and
29 deletion
+66
-29
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
+53
-28
idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java
idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java
+6
-1
idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java
idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java
+7
-0
未找到文件。
idea/src/org/jetbrains/jet/codegen/ExpressionCodegen.java
浏览文件 @
c943525e
...
...
@@ -26,6 +26,7 @@ import org.objectweb.asm.Type;
import
org.objectweb.asm.commons.InstructionAdapter
;
import
org.objectweb.asm.commons.Method
;
import
java.util.ArrayList
;
import
java.util.Iterator
;
import
java.util.List
;
import
java.util.Stack
;
...
...
@@ -1153,43 +1154,61 @@ public class ExpressionCodegen extends JetVisitor {
@Override
public
void
visitNewExpression
(
JetNewExpression
expression
)
{
final
JetUserType
constructorType
=
(
JetUserType
)
expression
.
getTypeReference
().
getTypeElement
();
JetTypeReference
typeReference
=
expression
.
getTypeReference
();
final
JetUserType
constructorType
=
(
JetUserType
)
typeReference
.
getTypeElement
();
final
JetSimpleNameExpression
constructorReference
=
constructorType
.
getReferenceExpression
();
DeclarationDescriptor
constructorDescriptor
=
bindingContext
.
resolveReferenceExpression
(
constructorReference
);
final
PsiElement
declaration
=
bindingContext
.
getDeclarationPsiElement
(
constructorDescriptor
);
Type
type
;
if
(
declaration
instanceof
PsiMethod
)
{
final
PsiMethod
constructor
=
(
PsiMethod
)
declaration
;
PsiClass
javaClass
=
constructor
.
getContainingClass
();
Type
type
=
JetTypeMapper
.
psiClassType
(
javaClass
);
v
.
anew
(
type
);
v
.
dup
();
final
Method
jvmConstructor
=
getMethodDescriptor
(
constructor
);
pushMethodArguments
(
expression
,
jvmConstructor
);
v
.
invokespecial
(
JetTypeMapper
.
jvmName
(
javaClass
),
"<init>"
,
jvmConstructor
.
getDescriptor
());
myStack
.
push
(
StackValue
.
onStack
(
type
));
return
;
type
=
generateJavaConstructorCall
(
expression
,
(
PsiMethod
)
declaration
);
}
else
if
(
constructorDescriptor
instanceof
ConstructorDescriptor
)
{
ClassDescriptor
classDecl
=
(
ClassDescriptor
)
constructorDescriptor
.
getContainingDeclaration
();
Type
type
=
JetTypeMapper
.
jetImplementationType
(
classDecl
);
v
.
anew
(
type
);
v
.
dup
();
type
=
typeMapper
.
mapType
(
bindingContext
.
resolveTypeReference
(
typeReference
),
OwnerKind
.
IMPLEMENTATION
);
if
(
type
.
getSort
()
==
Type
.
ARRAY
)
{
generateNewArray
(
expression
,
type
);
}
else
{
v
.
anew
(
type
);
v
.
dup
();
Method
method
=
typeMapper
.
mapConstructorSignature
((
ConstructorDescriptor
)
constructorDescriptor
,
OwnerKind
.
IMPLEMENTATION
);
pushMethodArguments
(
expression
,
method
);
Method
method
=
typeMapper
.
mapConstructorSignature
((
ConstructorDescriptor
)
constructorDescriptor
,
OwnerKind
.
IMPLEMENTATION
);
pushMethodArguments
(
expression
,
method
);
for
(
JetTypeReference
typeArgumentReference
:
constructorType
.
getTypeArgumentsAsTypes
())
{
JetType
typeArgument
=
bindingContext
.
resolveTypeReference
(
typeArgumentReference
);
// TODO is the makeNullable() call correct here?
ClassCodegen
.
newTypeInfo
(
v
,
typeMapper
.
mapType
(
TypeUtils
.
makeNullable
(
typeArgument
)));
}
for
(
JetTypeReference
typeArgumentReference
:
constructorType
.
getTypeArgumentsAsTypes
())
{
JetType
typeArgument
=
bindingContext
.
resolveTypeReference
(
typeArgumentReference
);
// TODO is the makeNullable() call correct here?
ClassCodegen
.
newTypeInfo
(
v
,
typeMapper
.
mapType
(
TypeUtils
.
makeNullable
(
typeArgument
)));
}
v
.
invokespecial
(
JetTypeMapper
.
jvmNameForImplementation
(
classDecl
),
"<init>"
,
method
.
getDescriptor
());
myStack
.
push
(
StackValue
.
onStack
(
type
));
return
;
ClassDescriptor
classDecl
=
(
ClassDescriptor
)
constructorDescriptor
.
getContainingDeclaration
();
v
.
invokespecial
(
JetTypeMapper
.
jvmNameForImplementation
(
classDecl
),
"<init>"
,
method
.
getDescriptor
());
}
}
else
{
throw
new
UnsupportedOperationException
(
"don't know how to generate this new expression"
);
}
myStack
.
push
(
StackValue
.
onStack
(
type
));
}
throw
new
UnsupportedOperationException
(
"don't know how to generate this new expression"
);
private
Type
generateJavaConstructorCall
(
JetNewExpression
expression
,
PsiMethod
constructor
)
{
PsiClass
javaClass
=
constructor
.
getContainingClass
();
Type
type
=
JetTypeMapper
.
psiClassType
(
javaClass
);
v
.
anew
(
type
);
v
.
dup
();
final
Method
jvmConstructor
=
getMethodDescriptor
(
constructor
);
pushMethodArguments
(
expression
,
jvmConstructor
);
v
.
invokespecial
(
JetTypeMapper
.
jvmName
(
javaClass
),
"<init>"
,
jvmConstructor
.
getDescriptor
());
return
type
;
}
private
void
generateNewArray
(
JetNewExpression
expression
,
Type
type
)
{
List
<
JetArgument
>
args
=
expression
.
getValueArguments
();
if
(
args
.
size
()
!=
1
)
{
throw
new
CompilationException
(
"array constructor requires one value argument"
);
}
gen
(
args
.
get
(
0
).
getArgumentExpression
(),
Type
.
INT_TYPE
);
v
.
newarray
(
type
.
getElementType
());
}
@Override
...
...
@@ -1308,7 +1327,7 @@ public class ExpressionCodegen extends JetVisitor {
if
(!(
descriptor
instanceof
ClassDescriptor
))
{
throw
new
UnsupportedOperationException
(
"don't know how to handle non-class types in as/as?"
);
}
Type
type
=
typeMapper
.
jvmType
((
ClassDescriptor
)
descriptor
,
OwnerKind
.
INTERFACE
);
Type
type
=
typeMapper
.
mapType
(
jetType
,
OwnerKind
.
INTERFACE
);
gen
(
expression
.
getLeft
(),
OBJECT_TYPE
);
if
(
opToken
==
JetTokens
.
AS_SAFE
)
{
generateInstanceOf
(
new
Runnable
()
{
...
...
@@ -1380,7 +1399,7 @@ public class ExpressionCodegen extends JetVisitor {
if
(
leaveExpressionOnStack
)
{
v
.
dup
();
}
Type
type
=
typeMapper
.
jvmType
((
ClassDescriptor
)
descriptor
,
OwnerKind
.
INTERFACE
);
Type
type
=
typeMapper
.
mapType
(
jetType
,
OwnerKind
.
INTERFACE
);
v
.
instanceOf
(
type
);
}
}
...
...
@@ -1487,5 +1506,11 @@ public class ExpressionCodegen extends JetVisitor {
}
private
static
class
CompilationException
extends
RuntimeException
{
private
CompilationException
()
{
}
private
CompilationException
(
String
message
)
{
super
(
message
);
}
}
}
idea/src/org/jetbrains/jet/codegen/JetTypeMapper.java
浏览文件 @
c943525e
...
...
@@ -3,6 +3,7 @@ package org.jetbrains.jet.codegen;
import
com.intellij.psi.PsiClass
;
import
com.intellij.psi.PsiElement
;
import
jet.typeinfo.TypeInfo
;
import
org.jetbrains.annotations.Nullable
;
import
org.jetbrains.jet.lang.descriptors.*
;
import
org.jetbrains.jet.lang.psi.*
;
import
org.jetbrains.jet.lang.resolve.BindingContext
;
...
...
@@ -118,6 +119,10 @@ public class JetTypeMapper {
}
public
Type
mapType
(
final
JetType
jetType
)
{
return
mapType
(
jetType
,
OwnerKind
.
INTERFACE
);
}
public
Type
mapType
(
final
JetType
jetType
,
OwnerKind
kind
)
{
if
(
jetType
.
equals
(
JetStandardClasses
.
getUnitType
())
||
jetType
.
equals
(
JetStandardClasses
.
getNothingType
()))
{
return
Type
.
VOID_TYPE
;
}
...
...
@@ -191,7 +196,7 @@ public class JetTypeMapper {
if
(
declaration
instanceof
PsiClass
)
{
return
psiClassType
((
PsiClass
)
declaration
);
}
return
Type
.
getObjectType
(
j
vmNameForInterface
((
ClassDescriptor
)
descriptor
));
return
Type
.
getObjectType
(
j
etJvmName
((
ClassDescriptor
)
descriptor
,
kind
));
}
throw
new
UnsupportedOperationException
(
"Unknown type "
+
jetType
);
...
...
idea/tests/org/jetbrains/jet/codegen/NamespaceGenTest.java
浏览文件 @
c943525e
...
...
@@ -360,6 +360,13 @@ public class NamespaceGenTest extends CodegenTestCase {
assertEquals
(
10
,
data
[
0
]);
}
public
void
testArrayNew
()
throws
Exception
{
loadText
(
"fun foo() = new Array<Int>(4)"
);
final
Method
main
=
generateFunction
();
int
[]
result
=
(
int
[])
main
.
invoke
(
null
);
assertEquals
(
4
,
result
.
length
);
}
public
void
testIntRange
()
throws
Exception
{
loadText
(
"fun foo() = 1..10"
);
final
Method
main
=
generateFunction
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录