diff --git a/.idea/modules.xml b/.idea/modules.xml index f68a4fe6146..69ece767994 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -51,6 +51,10 @@ + + + + diff --git a/compiler/ir/ir.backend.jvm/ir.backend.jvm.iml b/compiler/ir/ir.backend.jvm/ir.backend.jvm.iml new file mode 100644 index 00000000000..69abd825b7c --- /dev/null +++ b/compiler/ir/ir.backend.jvm/ir.backend.jvm.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/ir/ir.backend.jvm/src/org/jetbrains/kotlin/xbackend/jvm/Util.kt b/compiler/ir/ir.backend.jvm/src/org/jetbrains/kotlin/xbackend/jvm/Util.kt new file mode 100644 index 00000000000..88a2b47dceb --- /dev/null +++ b/compiler/ir/ir.backend.jvm/src/org/jetbrains/kotlin/xbackend/jvm/Util.kt @@ -0,0 +1,19 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.xbackend.jvm + + diff --git a/compiler/ir/ir.psi2ir/ir.psi2ir.iml b/compiler/ir/ir.psi2ir/ir.psi2ir.iml new file mode 100644 index 00000000000..1152e9530d3 --- /dev/null +++ b/compiler/ir/ir.psi2ir/ir.psi2ir.iml @@ -0,0 +1,14 @@ + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrElementFactory.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrElementFactory.kt new file mode 100644 index 00000000000..61719dac57a --- /dev/null +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrElementFactory.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.psi2ir + +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrFileImpl +import org.jetbrains.kotlin.ir.declarations.IrModule +import org.jetbrains.kotlin.psi.KtFile +import java.util.* + +class IrElementFactory(val irModule: IrModule, val sourceManager: PsiSourceManager) { + val ktFileToIrFile = LinkedHashMap() + + fun createIrFile(ktFile: KtFile, descriptor: PackageFragmentDescriptor): IrFile { + val fileEntry = sourceManager.getOrCreateFileEntry(ktFile) + val irFile = IrFileImpl(fileEntry.getRootSourceLocation(), irModule, fileEntry.getRecognizableName(), fileEntry, descriptor) + ktFileToIrFile.put(ktFile, irFile) + return irFile + } + +} \ No newline at end of file diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGenerator.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGenerator.kt new file mode 100644 index 00000000000..39c028326e8 --- /dev/null +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGenerator.kt @@ -0,0 +1,133 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.psi2ir + +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.descriptors.PropertyGetterDescriptor +import org.jetbrains.kotlin.descriptors.PropertySetterDescriptor +import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.expressions.IrDummyBody +import org.jetbrains.kotlin.psi.* +import org.jetbrains.kotlin.resolve.BindingContext +import org.jetbrains.kotlin.util.slicedMap.ReadOnlySlice + +interface IrGenerator { + val context: IrGeneratorContext + + operator fun ReadOnlySlice.get(key: K): V? = + context.bindingContext[this, key] +} + + +interface IrDeclarationGenerator : IrGenerator { + val irDeclaration: IrDeclaration + val parent: IrDeclarationGenerator? +} + +class IrModuleGenerator(override val context: IrGeneratorContext) : IrDeclarationGenerator { + override val irDeclaration: IrModule get() = context.irModule + override val parent: IrDeclarationGenerator? get() = null + + fun generateModuleContent() { + for (ktFile in context.inputFiles) { + val packageFragmentDescriptor = BindingContext.FILE_TO_PACKAGE_FRAGMENT[ktFile] ?: TODO("Handle unresolved code") + val irFile = context.irElementFactory.createIrFile(ktFile, packageFragmentDescriptor) + irDeclaration.addFile(irFile) + val generator = IrFileGenerator(context, ktFile, irFile, this) + generator.generateFileContent() + } + } +} + +abstract class IrDeclarationGeneratorBase( + override val context: IrGeneratorContext, + override val irDeclaration: IrDeclaration, + override val parent: IrDeclarationGenerator, + val file: PsiSourceManager.PsiFileEntry +) : IrDeclarationGenerator { + fun generateAnnotationEntries(annotationEntries: List) { + // TODO create IrAnnotation's for each KtAnnotationEntry + } + + fun generateMemberDeclaration(ktDeclaration: KtDeclaration, containingDeclaration: IrCompoundDeclaration) { + when (ktDeclaration) { + is KtNamedFunction -> + generateFunctionDeclaration(ktDeclaration, containingDeclaration) + is KtProperty -> + generatePropertyDeclaration(ktDeclaration, containingDeclaration) + is KtClassOrObject -> + TODO("classOrObject") + is KtTypeAlias -> + TODO("typealias") + } + } + + fun generateFunctionDeclaration(ktNamedFunction: KtNamedFunction, containingDeclaration: IrCompoundDeclaration) { + val sourceLocation = file.getSourceLocationForElement(ktNamedFunction) + val functionDescriptor = BindingContext.FUNCTION[ktNamedFunction] ?: TODO("unresolved fun") + val body = generateExpressionBody(ktNamedFunction.bodyExpression ?: TODO("function without body expression")) + val irFunction = IrFunctionImpl(sourceLocation, containingDeclaration, functionDescriptor, body) + containingDeclaration.addChildDeclaration(irFunction) + } + + fun generatePropertyDeclaration(ktProperty: KtProperty, containingDeclaration: IrCompoundDeclaration) { + val sourceLocation = file.getSourceLocationForElement(ktProperty) + val variableDescriptor = BindingContext.VARIABLE[ktProperty] ?: TODO("unresolved property") + val propertyDescriptor = variableDescriptor as? PropertyDescriptor ?: TODO("not a property?") + if (ktProperty.hasDelegate()) TODO("handle delegated property") + val initializer = ktProperty.initializer?.let { generateExpressionBody(it) } + val irProperty = IrSimplePropertyImpl(sourceLocation, containingDeclaration, propertyDescriptor, initializer) + containingDeclaration.addChildDeclaration(irProperty) + + val irGetter: IrPropertyGetter? = ktProperty.getter?.let { ktGetter -> + val getterLocation = file.getSourceLocationForElement(ktGetter) + val accessorDescriptor = BindingContext.PROPERTY_ACCESSOR[ktGetter] ?: TODO("unresolved getter") + val getterDescriptor = accessorDescriptor as? PropertyGetterDescriptor ?: TODO("not a getter?") + val getterBody = generateExpressionBody(ktGetter.bodyExpression ?: TODO("default getter")) + IrPropertyGetterImpl(getterLocation, irProperty, getterDescriptor, getterBody) + } + val irSetter: IrPropertySetter? = ktProperty.setter?.let { ktSetter -> + val getterLocation = file.getSourceLocationForElement(ktSetter) + val accessorDescriptor = BindingContext.PROPERTY_ACCESSOR[ktSetter] ?: TODO("unresolved setter") + val setterDescriptor = accessorDescriptor as? PropertySetterDescriptor ?: TODO("not a setter?") + val getterBody = generateExpressionBody(ktSetter.bodyExpression ?: TODO("default setter")) + IrPropertySetterImpl(getterLocation, irProperty, setterDescriptor, getterBody) + } + irProperty.initialize(irGetter, irSetter) + } + + fun generateExpressionBody(ktExpression: KtExpression): IrBody { + val sourceLocation = file.getSourceLocationForElement(ktExpression) + // TODO + return IrDummyBody(sourceLocation) + } +} + +class IrFileGenerator( + context: IrGeneratorContext, + val ktFile: KtFile, + override val irDeclaration: IrFile, + override val parent: IrModuleGenerator +) : IrDeclarationGeneratorBase(context, irDeclaration, parent, irDeclaration.fileEntry as PsiSourceManager.PsiFileEntry) { + fun generateFileContent() { + generateAnnotationEntries(ktFile.annotationEntries) + + for (topLevelDeclaration in ktFile.declarations) { + generateMemberDeclaration(topLevelDeclaration, irDeclaration) + } + } +} \ No newline at end of file diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGeneratorContext.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGeneratorContext.kt new file mode 100644 index 00000000000..278011c46a9 --- /dev/null +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/IrGeneratorContext.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.psi2ir + +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.ir.declarations.IrModule +import org.jetbrains.kotlin.psi.KtFile +import org.jetbrains.kotlin.resolve.BindingContext + +class IrGeneratorContext( + val inputFiles: List, + val irModule: IrModule, + val bindingContext: BindingContext +) { + val moduleDescriptor: ModuleDescriptor get() = irModule.descriptor + + val sourceManager = PsiSourceManager() + val irElementFactory = IrElementFactory(irModule, sourceManager) +} + diff --git a/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/PsiSourceManager.kt b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/PsiSourceManager.kt new file mode 100644 index 00000000000..2c091f42ffb --- /dev/null +++ b/compiler/ir/ir.psi2ir/src/org/jetbrains/kotlin/psi2ir/PsiSourceManager.kt @@ -0,0 +1,67 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.psi2ir + +import com.intellij.psi.PsiElement +import com.intellij.psi.PsiFile +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.SourceLocationManager +import org.jetbrains.kotlin.ir.fileIndex +import org.jetbrains.kotlin.ir.fileOffset +import java.util.* + +class PsiSourceManager : SourceLocationManager { + class PsiFileEntry (val index: Int, val file: PsiFile) : SourceLocationManager.FileEntry { + private val document = file.viewProvider.document + + override fun getLineNumber(sourceLocation: SourceLocation): Int = + document?.getLineNumber(sourceLocation.fileOffset) ?: -1 + + override fun getColumnNumber(sourceLocation: SourceLocation): Int = + document?.getLineStartOffset(sourceLocation.fileOffset) ?: -1 + + fun getRootSourceLocation(): SourceLocation = + SourceLocation(index, 0) + + fun getSourceLocationForElement(element: PsiElement): SourceLocation = + SourceLocation(index, element.textOffset) + + fun getRecognizableName(): String = file.virtualFile?.path ?: file.name + + override fun toString(): String = "#$index: ${getRecognizableName()}" + } + + private val fileEntries = ArrayList() + private val fileEntriesByPsiFile = HashMap() + + override fun getFileEntry(sourceLocation: Long): PsiFileEntry? = + fileEntries.getOrNull(sourceLocation.fileIndex) + + fun createFileEntry(psiFile: PsiFile): PsiFileEntry { + if (psiFile in fileEntriesByPsiFile) error("PsiFileEntry is already created for $psiFile") + + val newEntry = PsiFileEntry(fileEntries.size, psiFile) + fileEntries.add(newEntry) + fileEntriesByPsiFile[psiFile] = newEntry + return newEntry + } + + fun getOrCreateFileEntry(psiFile: PsiFile): PsiFileEntry = + fileEntriesByPsiFile.getOrElse(psiFile) { createFileEntry(psiFile) } + + fun getFileEntry(psiFile: PsiFile): PsiFileEntry? = fileEntriesByPsiFile[psiFile] +} diff --git a/compiler/ir/ir.tests/ir.tests.iml b/compiler/ir/ir.tests/ir.tests.iml new file mode 100644 index 00000000000..e30224002d3 --- /dev/null +++ b/compiler/ir/ir.tests/ir.tests.iml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt b/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt new file mode 100644 index 00000000000..666a197ad66 --- /dev/null +++ b/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrGeneratorTestCase.kt @@ -0,0 +1,51 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir + +import org.jetbrains.kotlin.codegen.CodegenTestCase +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.declarations.IrModuleImpl +import org.jetbrains.kotlin.psi2ir.IrGeneratorContext +import org.jetbrains.kotlin.psi2ir.IrModuleGenerator +import org.jetbrains.kotlin.resolve.AnalyzingUtils +import org.jetbrains.kotlin.resolve.lazy.JvmResolveUtil +import org.jetbrains.kotlin.test.ConfigurationKind +import java.io.File + +abstract class AbstractIrGeneratorTestCase : CodegenTestCase() { + override fun doMultiFileTest(wholeFile: File, files: List, javaFilesDir: File?) { + createEnvironmentWithMockJdkAndIdeaAnnotations(ConfigurationKind.ALL, files, javaFilesDir) + loadMultiFiles(files) + doTest(wholeFile, files) + } + + protected abstract fun doTest(wholeFile: File, testFiles: List) + + protected fun generateIrFilesAsSingleModule(testFiles: List): Map { + assert(myFiles != null) { "myFiles not initialized" } + assert(myEnvironment != null) { "myEnvironment not initialized" } + val analysisResult = JvmResolveUtil.analyzeAndCheckForErrors(myFiles.psiFiles, myEnvironment) + analysisResult.throwIfError() + AnalyzingUtils.throwExceptionOnErrors(analysisResult.bindingContext) + val irModule = IrModuleImpl(analysisResult.moduleDescriptor) + val irGeneratorContext = IrGeneratorContext(myFiles.psiFiles, irModule, analysisResult.bindingContext) + IrModuleGenerator(irGeneratorContext).generateModuleContent() + return testFiles.filter { it.name.endsWith(".kt") }.zip(irModule.files).toMap() + } +} + + diff --git a/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrTextTestCase.kt b/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrTextTestCase.kt new file mode 100644 index 00000000000..3423d4693a0 --- /dev/null +++ b/compiler/ir/ir.tests/tests/org/jetbrains/kotlin/ir/AbstractIrTextTestCase.kt @@ -0,0 +1,101 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir + +import com.intellij.openapi.util.text.StringUtil +import junit.framework.TestCase +import org.jetbrains.kotlin.ir.declarations.IrFile +import org.jetbrains.kotlin.ir.util.dump +import org.jetbrains.kotlin.test.KotlinTestUtils +import org.jetbrains.kotlin.utils.rethrow +import java.io.File +import java.io.FileWriter +import java.io.PrintWriter +import java.util.* +import java.util.regex.Pattern + +abstract class AbstractIrTextTestCase : AbstractIrGeneratorTestCase() { + override fun doTest(wholeFile: File, testFiles: List) { + val dir = wholeFile.parentFile + for ((testFile, irFile) in generateIrFilesAsSingleModule(testFiles)) { + doTestIrFileAgainstExpectations(dir, testFile, irFile) + } + } + + protected fun doTestIrFileAgainstExpectations(dir: File, testFile: TestFile, irFile: IrFile) { + val expectations = parseExpectations(dir, testFile) + val irFileDump = irFile.dump() + + expectations.irExpectedTextFile?.let { expectFile -> + KotlinTestUtils.assertEqualsToFile(expectFile, irFileDump) + } + + val expected = StringBuilder() + val actual = StringBuilder() + for (expectation in expectations.regexps) { + expected.append(expectation.numberOfOccurrences).append(" ").append(expectation.needle).append("\n") + val actualCount = StringUtil.findMatches(irFileDump, Pattern.compile("(" + expectation.needle + ")")).size + actual.append(actualCount).append(" ").append(expectation.needle).append("\n") + } + + try { + TestCase.assertEquals(irFileDump, expected.toString(), actual.toString()) + } + catch (e: Throwable) { + println(irFileDump) + throw rethrow(e) + } + } + + protected class Expectations(val irExpectedTextFile: File?, val regexps: List) + + protected class RegexpInText(val numberOfOccurrences: Int, val needle: String) { + constructor(countStr: String, needle: String) : this(Integer.valueOf(countStr), needle) + } + + companion object { + private val EXPECTED_OCCURRENCES_PATTERN = Regex("""^\s*//\s*(\d+)\s*(.*)$""") + private val EXPECT_TXT_PATTERN = Regex("""^\s*//\s*IR_FILE_TXT\s+(.*)$""") + + private fun parseExpectations(dir: File, testFile: TestFile): Expectations { + var expectedTextFileName: String? = null + val regexps = ArrayList() + for (line in testFile.content.split("\n")) { + EXPECTED_OCCURRENCES_PATTERN.matchEntire(line)?.let { matchResult -> + regexps.add(RegexpInText(matchResult.groupValues[1], matchResult.groupValues[2])) + } + EXPECT_TXT_PATTERN.matchEntire(line)?.let { matchResult -> + expectedTextFileName = matchResult.groupValues[1] + } + } + + val expectedTextFile: File? = expectedTextFileName?.let { + val textFile = File(dir, expectedTextFileName) + if (!textFile.exists()) { + TestCase.assertTrue("Can't create an IR expected text file: ${textFile.absolutePath}", textFile.createNewFile()) + PrintWriter(FileWriter(textFile)).use { + it.println("$expectedTextFileName: new IR expected text file for ${testFile.name}") + } + } + textFile + } + + return Expectations(expectedTextFile, regexps) + } + } + +} diff --git a/compiler/ir/ir.tree/ir.tree.iml b/compiler/ir/ir.tree/ir.tree.iml new file mode 100644 index 00000000000..afd2b356dda --- /dev/null +++ b/compiler/ir/ir.tree/ir.tree.iml @@ -0,0 +1,13 @@ + + + + + + + + + + + + + \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrElement.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrElement.kt new file mode 100644 index 00000000000..352109b2ddd --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/IrElement.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir + +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrElement { + val sourceLocation: SourceLocation + fun accept(visitor: IrElementVisitor, data: D): R + fun acceptChildren(visitor: IrElementVisitor, data: D): Unit +} + +abstract class IrElementBase(override val sourceLocation: SourceLocation) : IrElement diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocation.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocation.kt new file mode 100644 index 00000000000..ac954b9fb5a --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocation.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir + +typealias SourceLocation = Long + +fun SourceLocation(index: Int, offset: Int) = + (index.toLong() shl 32) + offset.toLong() + +const val NO_LOCATION: SourceLocation = -1L +const val UNDEFINED_INDEX: Int = -1 +const val UNDEFINED_OFFSET: Int = -1 + +val SourceLocation.fileIndex: Int + get() = if (this == NO_LOCATION) UNDEFINED_INDEX else (this ushr 32).toInt() + +val SourceLocation.fileOffset: Int + get() = if (this == NO_LOCATION) UNDEFINED_OFFSET else (this and 0xFFFFFFFFL).toInt() diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocationManager.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocationManager.kt new file mode 100644 index 00000000000..13c0a909044 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/SourceLocationManager.kt @@ -0,0 +1,28 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir + +import org.jetbrains.kotlin.ir.SourceLocation + +interface SourceLocationManager { + interface FileEntry { + fun getLineNumber(sourceLocation: SourceLocation): Int + fun getColumnNumber(sourceLocation: SourceLocation): Int + } + + fun getFileEntry(sourceLocation: SourceLocation): FileEntry? +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt new file mode 100644 index 00000000000..f5b9b07ae13 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrClass.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.ClassDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrClass : IrCompoundDeclaration { + override val descriptor: ClassDescriptor +} + +class IrClassImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration, + override val descriptor: ClassDescriptor +) : IrCompoundDeclarationBase(sourceLocation, containingDeclaration), IrClass { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitClass(this, data) +} + diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrDeclaration.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrDeclaration.kt new file mode 100644 index 00000000000..c1af5bee089 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrDeclaration.kt @@ -0,0 +1,63 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.DeclarationDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.IrElementBase +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor +import java.util.* + +interface IrDeclaration : IrElement { + val descriptor: DeclarationDescriptor + val containingDeclaration: IrDeclaration? +} + +interface IrCompoundDeclaration : IrDeclaration { + val childDeclarations: List + fun addChildDeclaration(child: IrDeclaration) +} + +interface IrDeclarationNonRoot : IrDeclaration { + override val containingDeclaration: IrDeclaration +} + +abstract class IrDeclarationBase( + sourceLocation: SourceLocation, + override val containingDeclaration: IrDeclaration? +) : IrElementBase(sourceLocation), IrDeclaration + +abstract class IrDeclarationNonRootBase( + sourceLocation: SourceLocation, + override val containingDeclaration: IrDeclaration +) : IrElementBase(sourceLocation), IrDeclarationNonRoot + +abstract class IrCompoundDeclarationBase( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration? +) : IrDeclarationBase(sourceLocation, containingDeclaration), IrCompoundDeclaration { + override val childDeclarations: MutableList = ArrayList() + + override fun addChildDeclaration(child: IrDeclaration) { + childDeclarations.add(child) + } + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + childDeclarations.forEach { it.accept(visitor, data) } + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFile.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFile.kt new file mode 100644 index 00000000000..961abe005ae --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFile.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.PackageFragmentDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.SourceLocationManager +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrFile : IrCompoundDeclaration { + val name: String + val fileEntry: SourceLocationManager.FileEntry + override val descriptor: PackageFragmentDescriptor +} + +class IrFileImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrModule, + override val name: String, + override val fileEntry: SourceLocationManager.FileEntry, + override val descriptor: PackageFragmentDescriptor +) : IrCompoundDeclarationBase(sourceLocation, containingDeclaration), IrFile { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitFile(this, data) +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFunction.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFunction.kt new file mode 100644 index 00000000000..843d6606d5d --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrFunction.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.FunctionDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrBody : IrElement + +interface IrFunction : IrDeclarationNonRoot { + override val descriptor: FunctionDescriptor + val body: IrBody +} + +abstract class IrFunctionBase( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration +) : IrDeclarationNonRootBase(sourceLocation, containingDeclaration), IrFunction { + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + body.accept(visitor, data) + } +} + +class IrFunctionImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration, + override val descriptor: FunctionDescriptor, + override val body: IrBody +) : IrFunctionBase(sourceLocation, containingDeclaration) { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitFunction(this, data) +} + diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt new file mode 100644 index 00000000000..8fa788525d1 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrModule.kt @@ -0,0 +1,50 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.ModuleDescriptor +import org.jetbrains.kotlin.ir.NO_LOCATION +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor +import java.util.* + +interface IrModule : IrCompoundDeclaration { + override val descriptor: ModuleDescriptor + + val files: List + fun addFile(file: IrFile) +} + +class IrModuleImpl(override val descriptor: ModuleDescriptor) : IrDeclarationBase(NO_LOCATION, null), IrModule { + override val files: MutableList = ArrayList() + override val childDeclarations: List get() = files + + override fun addFile(file: IrFile) { + files.add(file) + } + + override fun addChildDeclaration(child: IrDeclaration) { + if (child !is IrFile) throw IllegalArgumentException("Only IrFile can be contained in IrModule, got $child") + addFile(child) + } + + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitModule(this, data) + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + files.forEach { it.accept(visitor, data) } + } +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrProperty.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrProperty.kt new file mode 100644 index 00000000000..23fd2f16010 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrProperty.kt @@ -0,0 +1,84 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.PropertyDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrProperty : IrDeclarationNonRoot { + override val descriptor: PropertyDescriptor + val getter: IrPropertyGetter? + val setter: IrPropertySetter? +} + +interface IrSimpleProperty : IrProperty { + val valueInitializer: IrBody? +} + +interface IrDelegatedProperty : IrProperty { + val delegateInitializer: IrBody +} + +abstract class IrPropertyBase( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration, + override val descriptor: PropertyDescriptor +) : IrDeclarationNonRootBase(sourceLocation, containingDeclaration), IrProperty { + override var getter: IrPropertyGetter? = null + override var setter: IrPropertySetter? = null + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + getter?.accept(visitor, data) + setter?.accept(visitor, data) + } + + fun initialize(getter: IrPropertyGetter?, setter: IrPropertySetter?) { + this.getter = getter + this.setter = setter + } +} + +class IrSimplePropertyImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration, + descriptor: PropertyDescriptor, + override val valueInitializer: IrBody? +) : IrPropertyBase(sourceLocation, containingDeclaration, descriptor), IrSimpleProperty { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitSimpleProperty(this, data) + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + valueInitializer?.accept(visitor, data) + super.acceptChildren(visitor, data) + } +} + +class IrDelegatedPropertyImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrDeclaration, + descriptor: PropertyDescriptor, + override val delegateInitializer: IrBody +) : IrPropertyBase(sourceLocation, containingDeclaration, descriptor), IrDelegatedProperty { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitDelegatedProperty(this, data) + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + delegateInitializer.accept(visitor, data) + super.acceptChildren(visitor, data) + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrPropertyAccessor.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrPropertyAccessor.kt new file mode 100644 index 00000000000..e1eff45ea8e --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/declarations/IrPropertyAccessor.kt @@ -0,0 +1,62 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.declarations + +import org.jetbrains.kotlin.descriptors.PropertyAccessorDescriptor +import org.jetbrains.kotlin.descriptors.PropertyGetterDescriptor +import org.jetbrains.kotlin.descriptors.PropertySetterDescriptor +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +interface IrPropertyAccessor : IrFunction { + override val descriptor: PropertyAccessorDescriptor + val property: IrProperty get() = containingDeclaration as IrProperty +} + +interface IrPropertyGetter : IrPropertyAccessor { + override val descriptor: PropertyGetterDescriptor +} + +interface IrPropertySetter : IrPropertyAccessor { + override val descriptor: PropertySetterDescriptor +} + +abstract class IrPropertyAccessorBase( + sourceLocation: SourceLocation, + containingDeclaration: IrProperty, + override val body: IrBody +) : IrFunctionBase(sourceLocation, containingDeclaration), IrPropertyAccessor + +class IrPropertyGetterImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrProperty, + override val descriptor: PropertyGetterDescriptor, + body: IrBody +) : IrPropertyAccessorBase(sourceLocation, containingDeclaration, body), IrPropertyGetter { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitPropertyGetter(this, data) +} + +class IrPropertySetterImpl( + sourceLocation: SourceLocation, + containingDeclaration: IrProperty, + override val descriptor: PropertySetterDescriptor, + body: IrBody +) : IrPropertyAccessorBase(sourceLocation, containingDeclaration, body), IrPropertySetter { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitPropertySetter(this, data) +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrDummyBody.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrDummyBody.kt new file mode 100644 index 00000000000..f4c600e8e19 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrDummyBody.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.expressions + +import org.jetbrains.kotlin.ir.SourceLocation +import org.jetbrains.kotlin.ir.declarations.IrBody +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor + +class IrDummyBody(override val sourceLocation: SourceLocation) : IrBody { + override fun accept(visitor: IrElementVisitor, data: D): R = + visitor.visitElement(this, data) + + override fun acceptChildren(visitor: IrElementVisitor, data: D) { + // No children + } +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt new file mode 100644 index 00000000000..38e6720b559 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/expressions/IrExpression.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.expressions + +import org.jetbrains.kotlin.ir.IrElement + +interface IrExpression : IrElement + diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Dump.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Dump.kt new file mode 100644 index 00000000000..dc0f44ba62b --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Dump.kt @@ -0,0 +1,40 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.util + +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.IrDeclaration +import org.jetbrains.kotlin.ir.visitors.IrElementVisitorVoid +import org.jetbrains.kotlin.utils.Printer + +fun IrDeclaration.dump(): String { + val sb = StringBuilder() + accept(DumpIrTreeVisitor(sb), null) + return sb.toString() +} + +class DumpIrTreeVisitor(out: Appendable): IrElementVisitorVoid { + val printer = Printer(out, " ") + val elementRenderer = RenderIrElementVisitor() + + override fun visitElement(element: IrElement) { + printer.println(element.accept(elementRenderer, null)) + printer.pushIndent() + element.acceptChildren(this, null) + printer.popIndent() + } +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Render.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Render.kt new file mode 100644 index 00000000000..3555512abc9 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/util/Render.kt @@ -0,0 +1,65 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.util + +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.expressions.IrExpression +import org.jetbrains.kotlin.ir.visitors.IrElementVisitor +import org.jetbrains.kotlin.renderer.ClassifierNamePolicy +import org.jetbrains.kotlin.renderer.DescriptorRenderer +import org.jetbrains.kotlin.renderer.DescriptorRendererModifier +import org.jetbrains.kotlin.renderer.OverrideRenderingPolicy + +class RenderIrElementVisitor : IrElementVisitor { + override fun visitElement(element: IrElement, data: Nothing?): String = + "??? ${element.javaClass.simpleName}" + + override fun visitDeclaration(declaration: IrDeclaration, data: Nothing?): String = + "??? ${declaration.javaClass.simpleName} ${declaration.descriptor.name}" + + override fun visitFile(declaration: IrFile, data: Nothing?): String = + "IrFile ${declaration.name}" + + override fun visitFunction(declaration: IrFunction, data: Nothing?): String = + "IrFunction ${declaration.renderDescriptor()}" + + override fun visitProperty(declaration: IrProperty, data: Nothing?): String = + "IrProperty ${declaration.renderDescriptor()}" + + override fun visitPropertyGetter(declaration: IrPropertyGetter, data: Nothing?): String = + "IrPropertyGetter ${declaration.renderDescriptor()}" + + override fun visitPropertySetter(declaration: IrPropertySetter, data: Nothing?): String = + "IrPropertySetter ${declaration.renderDescriptor()}" + + override fun visitExpression(expression: IrExpression, data: Nothing?): String = + expression.javaClass.simpleName + + companion object { + private val DESCRIPTOR_RENDERER = DescriptorRenderer.withOptions { + withDefinedIn = false + overrideRenderingPolicy = OverrideRenderingPolicy.RENDER_OPEN_OVERRIDE + includePropertyConstant = true + classifierNamePolicy = ClassifierNamePolicy.FULLY_QUALIFIED + verbose = true + modifiers = DescriptorRendererModifier.ALL + } + + private fun IrDeclaration.renderDescriptor(): String = DESCRIPTOR_RENDERER.render(descriptor) + } +} \ No newline at end of file diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitor.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitor.kt new file mode 100644 index 00000000000..6a43328ff23 --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitor.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.visitors + +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.* +import org.jetbrains.kotlin.ir.expressions.IrExpression + +interface IrElementVisitor { + fun visitElement(element: IrElement, data: D): R + + fun visitDeclaration(declaration: IrDeclaration, data: D): R = visitElement(declaration, data) + fun visitCompoundDeclaration(declaration: IrCompoundDeclaration, data: D): R = visitDeclaration(declaration, data) + fun visitModule(declaration: IrModule, data: D): R = visitCompoundDeclaration(declaration, data) + fun visitFile(declaration: IrFile, data: D): R = visitCompoundDeclaration(declaration, data) + fun visitClass(declaration: IrClass, data: D): R = visitCompoundDeclaration(declaration, data) + fun visitFunction(declaration: IrFunction, data: D): R = visitDeclaration(declaration, data) + fun visitPropertyGetter(declaration: IrPropertyGetter, data: D): R = visitFunction(declaration, data) + fun visitPropertySetter(declaration: IrPropertySetter, data: D): R = visitFunction(declaration, data) + fun visitProperty(declaration: IrProperty, data: D): R = visitDeclaration(declaration, data) + fun visitSimpleProperty(declaration: IrSimpleProperty, data: D): R = visitProperty(declaration, data) + fun visitDelegatedProperty(declaration: IrDelegatedProperty, data: D): R = visitProperty(declaration, data) + + fun visitExpression(expression: IrExpression, data: D): R = visitElement(expression, data) +} diff --git a/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitorVoid.kt b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitorVoid.kt new file mode 100644 index 00000000000..69b022df45f --- /dev/null +++ b/compiler/ir/ir.tree/src/org/jetbrains/kotlin/ir/visitors/IrElementVisitorVoid.kt @@ -0,0 +1,45 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir.visitors + +import org.jetbrains.kotlin.ir.IrElement +import org.jetbrains.kotlin.ir.declarations.* + +interface IrElementVisitorVoid : IrElementVisitor { + fun visitElement(element: IrElement) + fun visitDeclaration(declaration: IrDeclaration) = visitElement(declaration) + fun visitCompoundDeclaration(declaration: IrCompoundDeclaration) = visitDeclaration(declaration) + fun visitModule(declaration: IrModule) = visitCompoundDeclaration(declaration) + fun visitFile(declaration: IrFile) = visitCompoundDeclaration(declaration) + fun visitClass(declaration: IrClass) = visitCompoundDeclaration(declaration) + fun visitFunction(declaration: IrFunction) = visitDeclaration(declaration) + fun visitPropertyGetter(declaration: IrPropertyGetter) = visitFunction(declaration) + fun visitPropertySetter(declaration: IrPropertySetter) = visitFunction(declaration) + fun visitProperty(declaration: IrProperty) = visitDeclaration(declaration) + + // Delegating methods from IrDeclarationVisitor + override fun visitElement(element: IrElement, data: Nothing?) = visitElement(element) + override fun visitDeclaration(declaration: IrDeclaration, data: Nothing?) = visitDeclaration(declaration) + override fun visitCompoundDeclaration(declaration: IrCompoundDeclaration, data: Nothing?) = visitCompoundDeclaration(declaration) + override fun visitModule(declaration: IrModule, data: Nothing?) = visitModule(declaration) + override fun visitFile(declaration: IrFile, data: Nothing?) = visitFile(declaration) + override fun visitClass(declaration: IrClass, data: Nothing?) = visitClass(declaration) + override fun visitFunction(declaration: IrFunction, data: Nothing?) = visitFunction(declaration) + override fun visitPropertyGetter(declaration: IrPropertyGetter, data: Nothing?) = visitPropertyGetter(declaration) + override fun visitPropertySetter(declaration: IrPropertySetter, data: Nothing?) = visitPropertySetter(declaration) + override fun visitProperty(declaration: IrProperty, data: Nothing?) = visitProperty(declaration) +} \ No newline at end of file diff --git a/compiler/testData/ir/irText/smoke.kt b/compiler/testData/ir/irText/smoke.kt new file mode 100644 index 00000000000..4ea9156c476 --- /dev/null +++ b/compiler/testData/ir/irText/smoke.kt @@ -0,0 +1,17 @@ +fun testFun() {} +val testSimpleVal = 1 +val testValWithGetter: Int get() = 42 +var testSimpleVar = 2 +var testVarWithAccessors: Int + get() = 42 + set(v) {} + +// 1 IrFunction public fun testFun +// 1 IrProperty public val testSimpleVal +// 1 IrProperty public val testValWithGetter +// 2 IrPropertyGetter +// 1 IrProperty public var testSimpleVar +// 1 IrProperty public var testVarWithAccessors +// 1 IrPropertyGetter + +// IR_FILE_TXT smoke.txt \ No newline at end of file diff --git a/compiler/testData/ir/irText/smoke.txt b/compiler/testData/ir/irText/smoke.txt new file mode 100644 index 00000000000..71c5910fec4 --- /dev/null +++ b/compiler/testData/ir/irText/smoke.txt @@ -0,0 +1,15 @@ +IrFile /smoke.kt + IrFunction public fun testFun(): kotlin.Unit + ??? IrDummyBody + IrProperty public val testSimpleVal: kotlin.Int = 1 + ??? IrDummyBody + IrProperty public val testValWithGetter: kotlin.Int + IrPropertyGetter public fun (): kotlin.Int + ??? IrDummyBody + IrProperty public var testSimpleVar: kotlin.Int + ??? IrDummyBody + IrProperty public var testVarWithAccessors: kotlin.Int + IrPropertyGetter public fun (): kotlin.Int + ??? IrDummyBody + IrPropertySetter public fun (/*0*/ v: kotlin.Int): kotlin.Unit + ??? IrDummyBody diff --git a/compiler/tests/compiler-tests.iml b/compiler/tests/compiler-tests.iml index 334d5a99231..69a3b2f9385 100644 --- a/compiler/tests/compiler-tests.iml +++ b/compiler/tests/compiler-tests.iml @@ -66,5 +66,6 @@ + \ No newline at end of file diff --git a/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java new file mode 100644 index 00000000000..73a9c33263c --- /dev/null +++ b/compiler/tests/org/jetbrains/kotlin/ir/IrTextTestCaseGenerated.java @@ -0,0 +1,43 @@ +/* + * Copyright 2010-2016 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.jetbrains.kotlin.ir; + +import com.intellij.testFramework.TestDataPath; +import org.jetbrains.kotlin.test.JUnit3RunnerWithInners; +import org.jetbrains.kotlin.test.KotlinTestUtils; +import org.jetbrains.kotlin.test.TestMetadata; +import org.junit.runner.RunWith; + +import java.io.File; +import java.util.regex.Pattern; + +/** This class is generated by {@link org.jetbrains.kotlin.generators.tests.TestsPackage}. DO NOT MODIFY MANUALLY */ +@SuppressWarnings("all") +@TestMetadata("compiler/testData/ir/irText") +@TestDataPath("$PROJECT_ROOT") +@RunWith(JUnit3RunnerWithInners.class) +public class IrTextTestCaseGenerated extends AbstractIrTextTestCase { + public void testAllFilesPresentInIrText() throws Exception { + KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/ir/irText"), Pattern.compile("^(.+)\\.kt$"), true); + } + + @TestMetadata("smoke.kt") + public void testSmoke() throws Exception { + String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/ir/irText/smoke.kt"); + doTest(fileName); + } +} diff --git a/generators/generators.iml b/generators/generators.iml index d590b297bfd..a1e8fc9b08a 100644 --- a/generators/generators.iml +++ b/generators/generators.iml @@ -40,5 +40,6 @@ + \ No newline at end of file diff --git a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt index 4ea1e35e1cc..0f69508c95b 100755 --- a/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt +++ b/generators/src/org/jetbrains/kotlin/generators/tests/GenerateTests.kt @@ -121,6 +121,7 @@ import org.jetbrains.kotlin.idea.stubs.AbstractMultiFileHighlightingTest import org.jetbrains.kotlin.idea.stubs.AbstractResolveByStubTest import org.jetbrains.kotlin.idea.stubs.AbstractStubBuilderTest import org.jetbrains.kotlin.integration.AbstractAntTaskTest +import org.jetbrains.kotlin.ir.AbstractIrTextTestCase import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterForWebDemoTest import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterMultiFileTest import org.jetbrains.kotlin.j2k.AbstractJavaToKotlinConverterSingleFileTest @@ -239,6 +240,10 @@ fun main(args: Array) { model("codegen/bytecodeText") } + testClass() { + model("ir/irText") + } + testClass() { model("codegen/bytecodeListing") }