提交 802f6e1c 编写于 作者: D Dmitry Jemerov

function call generation works in some trivial cases

上级 a203886f
......@@ -127,6 +127,17 @@
<option name="LABEL_INDENT_ABSOLUTE" value="false" />
<option name="USE_RELATIVE_INDENTS" value="false" />
</ADDITIONAL_INDENT_OPTIONS>
<codeStyleSettings language="JavaScript">
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="ALIGN_MULTILINE_BINARY_OPERATION" value="true" />
<option name="IF_BRACE_FORCE" value="1" />
<option name="PARENT_SETTINGS_INSTALLED" value="true" />
</codeStyleSettings>
<codeStyleSettings language="PHP">
<option name="ELSE_ON_NEW_LINE" value="true" />
<option name="IF_BRACE_FORCE" value="1" />
<option name="PARENT_SETTINGS_INSTALLED" value="true" />
</codeStyleSettings>
</value>
</option>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
......
package org.jetbrains.jet.codegen;
import com.intellij.psi.*;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.types.DeclarationDescriptor;
import org.jetbrains.jet.lang.types.FunctionDescriptor;
import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor;
import org.objectweb.asm.Opcodes;
......@@ -257,7 +259,6 @@ public class ExpressionCodegen extends JetVisitor {
}
}
/*
@Override
public void visitCallExpression(JetCallExpression expression) {
List<JetArgument> args = expression.getValueArguments();
......@@ -270,6 +271,19 @@ public class ExpressionCodegen extends JetVisitor {
if (callee instanceof JetSimpleNameExpression) {
DeclarationDescriptor funDescriptor = bindingContext.resolveReferenceExpression((JetSimpleNameExpression) callee);
if (funDescriptor instanceof FunctionDescriptor) {
PsiElement declarationPsiElement = bindingContext.getDeclarationPsiElement(funDescriptor);
if (declarationPsiElement instanceof PsiMethod) {
PsiMethod method = (PsiMethod) declarationPsiElement;
if (method.hasModifierProperty(PsiModifier.STATIC)) {
PsiClass containingClass = method.getContainingClass();
String owner = containingClass.getQualifiedName().replace(".", "/");
v.visitMethodInsn(Opcodes.INVOKESTATIC, owner, method.getName(), getMethodDescriptor(method));
boxIfNeeded(method.getReturnType());
}
else {
throw new UnsupportedOperationException("don't know how to generate instance method calls");
}
}
}
else {
throw new CompilationException();
......@@ -280,10 +294,48 @@ public class ExpressionCodegen extends JetVisitor {
}
}
private void boxIfNeeded(PsiType type) {
if (type == PsiType.LONG) {
v.invokestatic("java/lang/Long", "valueOf", "(J)Ljava/lang/Long;");
}
else {
throw new UnsupportedOperationException("Don't know how to box type " + type);
}
}
private String getMethodDescriptor(PsiMethod method) {
Type returnType = psiTypeToAsm(method.getReturnType());
PsiParameter[] parameters = method.getParameterList().getParameters();
Type[] parameterTypes = new Type[parameters.length];
for (int i = 0; i < parameters.length; i++) {
parameterTypes[i] = psiTypeToAsm(parameters [i].getType());
}
return Type.getMethodDescriptor(returnType, parameterTypes);
}
private Type psiTypeToAsm(PsiType type) {
if (type instanceof PsiPrimitiveType) {
if (type == PsiType.VOID) {
return Type.VOID_TYPE;
}
if (type == PsiType.LONG) {
return Type.LONG_TYPE;
}
}
throw new UnsupportedOperationException("don't know how to map type " + type + " to ASM");
}
@Override
public void visitBinaryExpression(JetBinaryExpression expression) {
public void visitDotQualifiedExpression(JetDotQualifiedExpression expression) {
// gen(expression.getReceiverExpression());
expression.getSelectorExpression().accept(this);
}
*/
/*
@Override
public void visitBinaryExpression(JetBinaryExpression expression) {
}
*/
private Type getType(JetProperty var) {
return InstructionAdapter.OBJECT_TYPE; // TODO:
......
......@@ -117,6 +117,9 @@ public class FunctionCodegen {
if ("Int".equals(name)) {
return Type.getType(Integer.class);
}
if ("Long".equals(name)) {
return Type.getType(Long.class);
}
throw new UnsupportedOperationException("Unknown type " + name);
}
......
import java.lang.*
fun f() : Long { return System.currentTimeMillis(); }
\ No newline at end of file
package org.jetbrains.jet;
import com.intellij.openapi.projectRoots.Sdk;
import com.intellij.testFramework.fixtures.DefaultLightProjectDescriptor;
import org.jetbrains.jet.resolve.JetResolveTest;
/**
* @author yole
*/
public class JetLightProjectDescriptor extends DefaultLightProjectDescriptor {
public static JetLightProjectDescriptor INSTANCE = new JetLightProjectDescriptor();
@Override
public Sdk getSdk() {
return JetResolveTest.jdkFromIdeaHome();
}
}
package org.jetbrains.jet.codegen;
import com.intellij.testFramework.LightProjectDescriptor;
import com.intellij.testFramework.fixtures.LightCodeInsightFixtureTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetLightProjectDescriptor;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.parsing.JetParsingTest;
......@@ -24,7 +27,7 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
}
public void testPSVM() throws Exception {
myFixture.configureByFile(JetParsingTest.getTestDataDir() + "/codegen/PSVM.jet");
loadFile("PSVM.jet");
final String text = generateToText();
System.out.println(text);
......@@ -67,6 +70,19 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
assertEquals(new Integer(50), returnValue);
}
public void testCurrentTime() throws Exception {
loadFile("currentTime.jet");
final Class aClass = generateToClass();
final Method main = firstMethod(aClass);
final long returnValue = (Long) main.invoke(null);
long currentTime = System.currentTimeMillis();
assertTrue(Math.abs(returnValue - currentTime) <= 1L);
}
private void loadFile(final String name) {
myFixture.configureByFile(JetParsingTest.getTestDataDir() + "/codegen/" + name);
}
private String generateToText() {
StringWriter writer = new StringWriter();
JetFile jetFile = (JetFile) myFixture.getFile();
......@@ -104,4 +120,10 @@ public class NamespaceGenTest extends LightCodeInsightFixtureTestCase {
return super.loadClass(name);
}
}
@NotNull
@Override
protected LightProjectDescriptor getProjectDescriptor() {
return JetLightProjectDescriptor.INSTANCE;
}
}
......@@ -114,6 +114,10 @@ public class JetResolveTest extends ExtensibleResolveTestCase {
@Override
protected Sdk getProjectJDK() {
return jdkFromIdeaHome();
}
public static Sdk jdkFromIdeaHome() {
Properties properties = new Properties();
try {
FileReader reader = new FileReader(getHomeDirectory() + "/idea/idea.properties");
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册