diff --git a/plugins/uast-common/src/org/jetbrains/uast/UastContext.kt b/plugins/uast-common/src/org/jetbrains/uast/UastContext.kt new file mode 100644 index 00000000000..87624d88822 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/UastContext.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2000-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.uast + +interface UastContext { + val converters: List + + fun convert(element: Any?): UElement? { + if (element == null) { + return null + } + + for (converter in converters) { + val uelement = converter.convertWithParent(element) + if (uelement != null) { + return uelement + } + } + return null + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/UastConverter.kt b/plugins/uast-common/src/org/jetbrains/uast/UastConverter.kt new file mode 100644 index 00000000000..34a8248cc71 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/UastConverter.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2000-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.uast + +interface UastConverter { + fun convert(element: Any?, parent: UElement): UElement? + fun convertWithParent(element: Any?): UElement? + + fun isFileSupported(path: String): Boolean +} + +object UastConverterUtils { + @JvmStatic + fun isFileSupported(converters: List, path: String): Boolean { + return converters.any { it.isFileSupported(path) } + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/UastHandler.java b/plugins/uast-common/src/org/jetbrains/uast/UastHandler.java new file mode 100644 index 00000000000..dd7cf0f1db6 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/UastHandler.java @@ -0,0 +1,20 @@ +/* + * Copyright 2000-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.uast; + +public interface UastHandler { + void invoke(UElement element); +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/UastUtils.kt b/plugins/uast-common/src/org/jetbrains/uast/UastUtils.kt new file mode 100644 index 00000000000..2005bf01f6d --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/UastUtils.kt @@ -0,0 +1,129 @@ +/* + * Copyright 2000-2015 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. + */ +@file:JvmName("UastUtils") +package org.jetbrains.uast + +tailrec fun UElement?.getContainingClass(): UClass? { + val parent = this?.parent ?: return null + if (parent is UClass) return parent + return parent.getContainingClass() +} + +tailrec fun UElement?.getContainingFile(): UFile? { + val parent = this?.parent ?: return null + if (parent is UFile) return parent + return parent.getContainingFile() +} + +fun UElement?.getContainingClassOrEmpty() = getContainingClass() ?: UClassNotResolved + +tailrec fun UElement?.getContainingFunction(): UFunction? { + val parent = this?.parent ?: return null + if (parent is UFunction) return parent + return parent.getContainingFunction() +} + +tailrec fun UElement?.getContainingDeclaration(): UDeclaration? { + val parent = this?.parent ?: return null + if (parent is UDeclaration) return parent + return parent.getContainingDeclaration() +} + +fun UClass.findProperty(name: String) = declarations.firstOrNull { it is UVariable && it.name == name } as? UVariable + +fun UElement.handleTraverse(handler: UastHandler) { + handler(this) + this.traverse(handler) +} + +fun UDeclaration.isTopLevel() = parent is UFile + +fun UElement.log(firstLine: String, vararg nested: Any?): String { + return (if (firstLine.isBlank()) "" else firstLine + "\n") + nested.joinToString("\n") { + when (it) { + null -> "".withMargin + is List<*> -> + @Suppress("UNCHECKED_CAST") + (it as List).logString() + is UElement -> it.logString().withMargin + else -> error("Invalid element type: $it") + } + } +} + +val String.withMargin: String + get() = lines().joinToString("\n") { " " + it } + +fun List.handleTraverseList(handler: UastHandler) { + for (element in this) { + handler(element) + element.traverse(handler) + } +} + +@Suppress("UNCHECKED_CAST") +fun UClass.findFunctions(name: String) = declarations.filter { it is UFunction && it.matchesName(name) } as List + +fun UCallExpression.getReceiver(): UExpression? = (this.parent as? UQualifiedExpression)?.receiver +fun UCallExpression.getReceiverOrEmpty(): UExpression = (this.parent as? UQualifiedExpression)?.receiver ?: EmptyExpression(this) + +fun UElement.resolveIfCan(context: UastContext): UDeclaration? = (this as? UResolvable)?.resolve(context) + +fun UElement.isThrow() = this is USpecialExpressionList && this.kind == UastSpecialExpressionKind.THROW + +fun UAnnotated.findAnnotation(fqName: String) = annotations.firstOrNull { it.fqName == fqName } + +fun UClass.getAllDeclarations(context: UastContext): List = mutableListOf().apply { + this += declarations + for (superType in superTypes) { + superType.resolveClass(context)?.declarations?.let { this += it } + } +} + +fun UClass.getAllFunctions(context: UastContext) = getAllDeclarations(context).filterIsInstance() + +fun UCallExpression.getQualifiedCallElement(): UExpression { + fun findParent(element: UExpression?): UExpression? = when (element) { + is UQualifiedExpression -> findParent(element.parent as? UExpression) ?: element + else -> null + } + + return findParent(parent as? UExpression) ?: this +} + +val UDeclaration.fqName: String + get() { + val containingFqName = this.getContainingDeclaration()?.fqName + ?: this.getContainingFile()?.packageFqName + val containingFqNameWithDot = containingFqName?.let { it + "." } ?: "" + return containingFqNameWithDot + this.name + } + +inline fun UElement.getParentOfType(): T? = getParentOfType(T::class.java) + +fun UElement.getParentOfType(clazz: Class): T? { + tailrec fun findParent(element: UElement): UElement? { + val parent = element.parent + return when { + parent == null -> null + clazz.isInstance(parent) -> parent + else -> findParent(parent) + } + } + + @Suppress("UNCHECKED_CAST") + return findParent(this) as T +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/UastVisitor.kt b/plugins/uast-common/src/org/jetbrains/uast/UastVisitor.kt new file mode 100644 index 00000000000..81ee36a9ad4 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/UastVisitor.kt @@ -0,0 +1,109 @@ +/* + * Copyright 2000-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.uast.visitor + +import org.jetbrains.uast.* + +abstract class UastVisitor { + open fun visitFile(node: UFile): Boolean = false + + // Declarations + open fun visitClass(node: UClass): Boolean = false + open fun visitFunction(node: UFunction): Boolean = false + open fun visitVariable(node: UVariable): Boolean = false + open fun visitImportStatement(node: UImportStatement): Boolean = false + open fun visitAnnotation(node: UAnnotation): Boolean = false + + // Expressions + open fun visitLabeledExpression(node: ULabeledExpression): Boolean = false + open fun visitDeclarationsExpression(node: UDeclarationsExpression): Boolean = false + open fun visitBlockExpression(node: UBlockExpression): Boolean = false + open fun visitQualifiedExpression(node: UQualifiedExpression): Boolean = false + open fun visitSimpleReferenceExpression(node: USimpleReferenceExpression): Boolean = false + open fun visitCallExpression(node: UCallExpression): Boolean = false + open fun visitAssignmentExpression(node: UAssignmentExpression): Boolean = false + open fun visitBinaryExpression(node: UBinaryExpression): Boolean = false + open fun visitBinaryExpressionWithType(node: UBinaryExpressionWithType): Boolean = false + open fun visitParenthesizedExpression(node: UParenthesizedExpression): Boolean = false + open fun visitPrefixExpression(node: UPrefixExpression): Boolean = false + open fun visitPostfixExpression(node: UPostfixExpression): Boolean = false + open fun visitSpecialExpressionList(node: USpecialExpressionList): Boolean = false + open fun visitExpressionList(node: UExpressionList): Boolean = false + open fun visitIfExpression(node: UIfExpression): Boolean = false + open fun visitSwitchExpression(node: USwitchExpression): Boolean = false + open fun visitSwitchClauseExpression(node: USwitchClauseExpression): Boolean = false + open fun visitWhileExpression(node: UWhileExpression): Boolean = false + open fun visitDoWhileExpression(node: UDoWhileExpression): Boolean = false + open fun visitForExpression(node: UForExpression): Boolean = false + open fun visitForEachExpression(node: UForEachExpression): Boolean = false + open fun visitTryExpression(node: UTryExpression): Boolean = false + open fun visitLiteralExpression(node: ULiteralExpression): Boolean = false + open fun visitThisExpression(node: UThisExpression): Boolean = false + open fun visitSuperExpression(node: USuperExpression): Boolean = false + + fun handle(node: UElement): Boolean { + return when (node) { + is UFile -> visitFile(node) + + is UClass -> visitClass(node) + is UFunction -> visitFunction(node) + is UVariable -> visitVariable(node) + is UImportStatement -> visitImportStatement(node) + is UAnnotation -> visitAnnotation(node) + + is ULabeledExpression -> visitLabeledExpression(node) + is UDeclarationsExpression -> visitDeclarationsExpression(node) + is UBlockExpression -> visitBlockExpression(node) + is UQualifiedExpression -> visitQualifiedExpression(node) + is USimpleReferenceExpression -> visitSimpleReferenceExpression(node) + is UCallExpression -> visitCallExpression(node) + is UAssignmentExpression -> visitAssignmentExpression(node) + is UBinaryExpression -> visitBinaryExpression(node) + is UBinaryExpressionWithType -> visitBinaryExpressionWithType(node) + is UParenthesizedExpression -> visitParenthesizedExpression(node) + is UPrefixExpression -> visitPrefixExpression(node) + is UPostfixExpression -> visitPostfixExpression(node) + is USpecialExpressionList -> visitSpecialExpressionList(node) + is UExpressionList -> visitExpressionList(node) + is UIfExpression -> visitIfExpression(node) + is USwitchExpression -> visitSwitchExpression(node) + is USwitchClauseExpression -> visitSwitchClauseExpression(node) + is UWhileExpression -> visitWhileExpression(node) + is UDoWhileExpression -> visitDoWhileExpression(node) + is UForExpression -> visitForExpression(node) + is UForEachExpression -> visitForEachExpression(node) + is UTryExpression -> visitTryExpression(node) + is ULiteralExpression -> visitLiteralExpression(node) + is UThisExpression -> visitThisExpression(node) + is USuperExpression -> visitSuperExpression(node) + else -> true + } + } + + open fun process(element: UElement) { + if (!handle(element)) { + processChildren(element) + } + } + + fun processChildren(element: UElement) { + element.traverse(UastHandler { handle(it) }) + } +} + +object EmptyUastVisitor : UastVisitor() { + override fun process(element: UElement) {} +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/baseElements/UAnnotation.kt b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UAnnotation.kt new file mode 100644 index 00000000000..6b8683816fa --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UAnnotation.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-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.uast + +interface UAnnotation : UElement, UNamed, UFqNamed, NoTraverse { + fun resolve(context: UastContext): UClass? + + val valueArguments: List + fun getValue(name: String) = valueArguments.firstOrNull { it.name == name }?.expression?.evaluate() + fun getValues() = valueArguments.map { it.expression.evaluate() } + + override fun logString() = log("UAnnotation ($name)") + override fun renderString() = if (valueArguments.isEmpty()) "@$name" else "@$name(" + + valueArguments.joinToString { it.renderString() } + ")" +} + +interface UAnnotated { + val annotations: List +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/baseElements/UElement.kt b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UElement.kt new file mode 100644 index 00000000000..b11f99f63a4 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UElement.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2000-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.uast + +interface UElement { + val parent: UElement? + fun logString(): String + fun renderString(): String = logString() + fun traverse(handler: UastHandler) +} + +interface UNamed { + val name: String + + fun matchesName(name: String) = this.name == name +} + +interface UFqNamed : UNamed { + val fqName: String? + + val fqNameOrName: String + get() = fqName ?: name + + fun matchesFqName(fqName: String) = this.fqName == fqName +} + +interface UModifierOwner { + fun hasModifier(modifier: UastModifier): Boolean +} + +interface NoTraverse : UElement { + override fun traverse(handler: UastHandler) {} +} + +interface UResolvable { + fun resolve(context: UastContext): UDeclaration? + fun resolveOrEmpty(context: UastContext): UDeclaration = resolve(context) ?: UDeclarationNotResolved + + fun resolveClass(context: UastContext): UClass? = resolve(context) as? UClass +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/baseElements/UExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UExpression.kt new file mode 100644 index 00000000000..01a5ef928b7 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UExpression.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2000-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.uast + +interface UExpression : UElement { + fun evaluate(): Any? + fun evaluateString(): String? = evaluate() as? String + fun getExpressionType(): UType? = null +} + +interface NoEvaluate : UExpression { + override fun evaluate() = null +} + +interface NoAnnotations : UAnnotated { + override val annotations: List + get() = emptyList() +} + +interface NoModifiers : UModifierOwner { + override fun hasModifier(modifier: UastModifier) = false +} + +class EmptyExpression(override val parent: UElement) : UExpression, NoTraverse, NoEvaluate { + override fun logString() = "EmptyExpression" +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/baseElements/UFile.kt b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UFile.kt new file mode 100644 index 00000000000..b9ddb9560dd --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/baseElements/UFile.kt @@ -0,0 +1,44 @@ +/* + * Copyright 2000-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.uast + +interface UFile: UElement { + val packageFqName: String? + val importStatements: List + val declarations: List + + val classes: List + get() = declarations.filterIsInstance() + + override val parent: UElement? + get() = null + + override fun traverse(handler: UastHandler) { + declarations.handleTraverseList(handler) + importStatements.handleTraverseList(handler) + } + + override fun logString() = "UFile (package = $packageFqName)\n" + declarations.joinToString("\n") { it.logString().withMargin } + + override fun renderString() = buildString { + if (packageFqName != null) { + appendln("package $packageFqName") + appendln() + } + + declarations.forEach { appendln(it.renderString()) } + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UDoWhileExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UDoWhileExpression.kt new file mode 100644 index 00000000000..ef8e3e18481 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UDoWhileExpression.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2000-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.uast + +interface UDoWhileExpression : ULoopExpression { + val condition: UExpression + + override fun traverse(handler: UastHandler) { + condition.handleTraverse(handler) + body.handleTraverse(handler) + } + + override fun renderString() = buildString { + append("do ") + append(body.renderString()) + appendln("while (${condition.renderString()})") + } + + override fun logString() = log("UDoWhileExpression", condition, body) +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UForExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UForExpression.kt new file mode 100644 index 00000000000..6d6edd26883 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UForExpression.kt @@ -0,0 +1,42 @@ +/* + * Copyright 2000-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.uast + +interface UForExpression : ULoopExpression { + val declaration: UExpression? + val condition: UExpression? + val update: UExpression? + + override fun traverse(handler: UastHandler) { + declaration?.handleTraverse(handler) + condition?.handleTraverse(handler) + update?.handleTraverse(handler) + body.handleTraverse(handler) + } + + override fun renderString() = buildString { + append("for (") + declaration?.let { append(it.renderString()) } + append("; ") + condition?.let { append(it.renderString()) } + append("; ") + update?.let { append(it.renderString()) } + append(") ") + append(body.renderString()) + } + + override fun logString() = log("UForExpression", declaration, condition, update, body) +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UIfExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UIfExpression.kt new file mode 100644 index 00000000000..3c737a2ce83 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UIfExpression.kt @@ -0,0 +1,49 @@ +/* + * Copyright 2000-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.uast + +interface UIfExpression : UExpression { + val condition: UExpression + val thenBranch: UExpression? + val elseBranch: UExpression? + val isTernary: Boolean + + override fun traverse(handler: UastHandler) { + condition.handleTraverse(handler) + thenBranch?.handleTraverse(handler) + elseBranch?.handleTraverse(handler) + } + + override fun logString() = log("UIfExpression", condition, thenBranch, elseBranch) + override fun renderString() = buildString { + if (isTernary) { + append("(" + condition.renderString() + ")") + append(" ? ") + append("(" + (thenBranch?.renderString() ?: "") + ")") + append(" : ") + append("(" + (elseBranch?.renderString() ?: "") + ")") + } else { + append("if (${condition.renderString()}) ") + thenBranch?.let { append(it.renderString()) } + val elseBranch = elseBranch + if (elseBranch != null && elseBranch !is EmptyExpression) { + if (thenBranch !is UBlockExpression) append(" ") + append("else ") + append(elseBranch.renderString()) + } + } + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt new file mode 100644 index 00000000000..2a098d48a14 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/USwitchExpression.kt @@ -0,0 +1,54 @@ +/* + * Copyright 2000-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.uast + +interface USwitchExpression : UExpression { + val expression: UExpression? + val body: UExpression + + override fun traverse(handler: UastHandler) { + expression?.handleTraverse(handler) + body.handleTraverse(handler) + } + + override fun logString() = log("USwitchExpression", expression, body) + override fun renderString() = buildString { + val expr = expression?.let { "(" + it.renderString() + ") " } ?: "" + appendln("switch $expr") + appendln(body.renderString()) + } +} + +interface USwitchClauseExpression : UExpression + +interface UExpressionSwitchClauseExpression : USwitchClauseExpression { + val caseValue: UExpression + + override fun traverse(handler: UastHandler) { + caseValue.handleTraverse(handler) + } + + override fun renderString() = caseValue.renderString() + " -> " + override fun logString() = log("UExpressionSwitchClauseExpression", caseValue) +} + +interface UDefaultSwitchClauseExpression : USwitchClauseExpression { + override fun traverse(handler: UastHandler) {} + override fun logString() = "UDefaultSwitchClause" + override fun renderString() = "else -> " +} + +class SimpleUDefaultSwitchClauseExpression(override val parent: UElement) : UDefaultSwitchClauseExpression, NoEvaluate \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UTryExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UTryExpression.kt new file mode 100644 index 00000000000..b28e01e0feb --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UTryExpression.kt @@ -0,0 +1,53 @@ +/* + * Copyright 2000-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.uast + +interface UTryExpression : UExpression { + val tryClause: UExpression + val catchClauses: List + val finallyClause: UExpression? + + override fun traverse(handler: UastHandler) { + tryClause.handleTraverse(handler) + catchClauses.handleTraverseList(handler) + finallyClause?.handleTraverse(handler) + } + + override fun renderString() = buildString { + append("try ") + appendln(tryClause.renderString()) + catchClauses.forEach { appendln(it.renderString()) } + finallyClause?.let { append("finally ").append(it.renderString()) } + } + + override fun logString() = "UTryExpression\n" + + tryClause.logString().withMargin + + catchClauses.joinToString("\n") { it.logString().withMargin } + + (finallyClause?.let { it.logString().withMargin } ?: "" ) +} + +interface UCatchClause : UElement { + val body: UExpression + val parameters: List + val types: List + + override fun traverse(handler: UastHandler) { + body.handleTraverse(handler) + } + + override fun logString() = log("UCatchClause", body) + override fun renderString() = "catch (e) " + body.renderString() +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UWhileExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UWhileExpression.kt new file mode 100644 index 00000000000..eb8ff6f78bd --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/controlStructures/UWhileExpression.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-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.uast + +interface UWhileExpression : ULoopExpression { + val condition: UExpression + + override fun traverse(handler: UastHandler) { + condition.handleTraverse(handler) + body.handleTraverse(handler) + } + + override fun renderString() = buildString { + append("while (${condition.renderString()}) ") + append(body.renderString()) + } + + override fun logString() = log("UWhileExpression", condition, body) +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UClass.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UClass.kt new file mode 100644 index 00000000000..2d5b0b4d9ee --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UClass.kt @@ -0,0 +1,100 @@ +/* + * Copyright 2000-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.uast + +interface UClass : UDeclaration, UFqNamed, UModifierOwner, UAnnotated { + val isEnum: Boolean + val isInterface: Boolean + val isObject: Boolean + val isAnonymous: Boolean + val isAnnotation: Boolean + val visibility: UastVisibility + + val internalName: String? + + val superTypes: List + val declarations: List + + val nestedClasses: List + get() = declarations.filterIsInstance() + + val functions: List + get() = declarations.filterIsInstance() + + val properties: List + get() = declarations.filterIsInstance() + + @Suppress("UNCHECKED_CAST") + val constructors: List + get() = declarations.filter { it is UFunction && it.kind == UastFunctionKind.CONSTRUCTOR } as List + + fun isSubclassOf(name: String) : Boolean + + fun getSuperClass(context: UastContext): UClass? + + override fun traverse(handler: UastHandler) { + nameElement?.handleTraverse(handler) + declarations.handleTraverseList(handler) + annotations.handleTraverseList(handler) + } + + override fun renderString(): String { + val keyword = when { + isEnum -> "enum" + isInterface -> "interface" + isObject -> "object" + else -> "class" + } + + val modifiers = listOf(UastModifier.ABSTRACT, UastModifier.FINAL, UastModifier.INNER) + .filter { hasModifier(it) }.joinToString(" ") { it.name }.let { if (it.isBlank()) it else "$it " } + + val name = if (isAnonymous) "" else " $name" + + val declarations = if (declarations.isEmpty()) "" else buildString { + appendln("{") + append(declarations.joinToString("\n") { it.renderString() }.withMargin) + append("\n}") + } + + return "${visibility.name} " + modifiers + keyword + name + " " + declarations + } + + override fun logString() = "UClass ($name, enum = $isEnum, interface = $isInterface, object = $isObject)\n" + declarations.logString() +} + +object UClassNotResolved : UClass { + override val isEnum = false + override val isInterface = false + override val isObject = false + override val isAnonymous = true + override val isAnnotation = false + override val visibility = UastVisibility.PRIVATE + override val superTypes = emptyList() + override val declarations = emptyList() + override fun isSubclassOf(name: String) = false + + override val nameElement = null + override val parent = null + override val name = "" + override val fqName = null + override val internalName = null + + override fun hasModifier(modifier: UastModifier) = false + override val annotations = emptyList() + + override fun getSuperClass(context: UastContext) = null +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt new file mode 100644 index 00000000000..4d0056db72c --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UDeclaration.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +interface UDeclaration : UElement, UNamed { + val nameElement: UElement? +} + +object UDeclarationNotResolved : UDeclaration, NoTraverse { + override val name = "" + override val nameElement = null + override val parent = null + + override fun logString() = "[!] $name" + override fun renderString() = name +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt new file mode 100644 index 00000000000..41be03ca3dc --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UFunction.kt @@ -0,0 +1,70 @@ +/* + * Copyright 2000-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.uast + +interface UFunction : UDeclaration, UModifierOwner, UAnnotated { + val kind: UastFunctionKind + val valueParameters: List + val valueParameterCount: Int + val typeParameters: List + val typeParameterCount: Int + val returnType: UType? + val body: UExpression + val visibility: UastVisibility + + fun getSuperFunctions(context: UastContext): List + + override fun traverse(handler: UastHandler) { + nameElement?.handleTraverse(handler) + valueParameters.handleTraverseList(handler) + body.handleTraverse(handler) + annotations.handleTraverseList(handler) + typeParameters.handleTraverseList(handler) + returnType?.handleTraverse(handler) + } + + override fun renderString(): String { + val typeParameters = if (typeParameterCount == 0) "" else "<" + typeParameters.joinToString { it.renderString() } + "> " + val valueParameters = valueParameters.joinToString { it.renderString() } + val returnType = returnType?.let { ": " + it.renderString() } ?: "" + val body = when (body) { + is UBlockExpression -> " " + body.renderString() + else -> " = " + body.renderString() + } + return "${visibility.name} fun " + typeParameters + name + "(" + valueParameters + ")" + returnType + body + } + + override fun logString() = "UFunction ($name, kind = ${kind.name}, " + + "paramCount = $valueParameterCount)\n" + body.logString().withMargin +} + +object UFunctionNotResolved : UFunction { + override val kind = UastFunctionKind("") + override val valueParameters = emptyList() + override val valueParameterCount = 0 + override val typeParameters = emptyList() + override val typeParameterCount = 0 + override val returnType = null + override val body = EmptyExpression(this) + override val visibility = UastVisibility.PRIVATE + override val nameElement = null + override val parent = null + override val name = "" + + override fun hasModifier(modifier: UastModifier) = false + override fun getSuperFunctions(context: UastContext) = emptyList() + override val annotations = emptyList() +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UImportStatement.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UImportStatement.kt new file mode 100644 index 00000000000..b7c0df375a7 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UImportStatement.kt @@ -0,0 +1,25 @@ +/* + * Copyright 2000-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.uast + +interface UImportStatement : UElement { + val nameToImport: String? + val isStarImport: Boolean + + override fun traverse(handler: UastHandler) {} + override fun logString() = "UImport ($nameToImport)" + override fun renderString() = "import ${nameToImport ?: ""}" +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UType.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UType.kt new file mode 100644 index 00000000000..d9fea3298fc --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UType.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2000-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.uast + +interface UType : UElement, UNamed, UFqNamed, UAnnotated, UResolvable, NoTraverse { + override fun matchesName(name: String) = this.name == name || this.name.endsWith(".$name") + + val isInt: Boolean + val isBoolean: Boolean + + override fun logString() = "UType ($name)" + override fun renderString() = name + + override fun resolve(context: UastContext): UClass? + override fun resolveOrEmpty(context: UastContext) = resolve(context) ?: UClassNotResolved +} + +interface UTypeReference : UDeclaration, UResolvable, NoTraverse { + override fun renderString() = "" + override fun logString() = log("UTypeReference") + + override fun resolve(context: UastContext): UClass? + override fun resolveOrEmpty(context: UastContext) = resolve(context) ?: UClassNotResolved +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/declarations/UVariable.kt b/plugins/uast-common/src/org/jetbrains/uast/declarations/UVariable.kt new file mode 100644 index 00000000000..38e9003580b --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/declarations/UVariable.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2000-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.uast + +interface UVariable : UDeclaration, UModifierOwner, UAnnotated { + val initializer: UExpression? + val kind: UastVariableKind + val type: UType + + override fun traverse(handler: UastHandler) { + nameElement?.handleTraverse(handler) + initializer?.handleTraverse(handler) + annotations.handleTraverseList(handler) + type.handleTraverse(handler) + } + + override fun renderString(): String { + val initializer = if (initializer != null && initializer !is EmptyExpression) " = ${initializer!!.renderString()}" else "" + val prefix = if (kind == UastVariableKind.VALUE_PARAMETER) "" else "var " + val emptyLine = if (kind == UastVariableKind.MEMBER) "\n" else "" + return "$prefix$name: " + type.name + initializer + emptyLine + } + + override fun logString() = "UVariable ($name, kind = ${kind.name})\n" + + (initializer?.let { it.logString().withMargin } ?: "") +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UArrayAccessExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UArrayAccessExpression.kt new file mode 100644 index 00000000000..749a069f261 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UArrayAccessExpression.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +interface UArrayAccessExpression : UExpression { + val receiver: UExpression + val indices: List + + override fun traverse(handler: UastHandler) { + receiver.handleTraverse(handler) + indices.handleTraverseList(handler) + } + + override fun logString() = log("UArrayAccessExpression", receiver, indices) + override fun renderString() = receiver.renderString() + indices.joinToString(prefix = "[", postfix = "]") { it.renderString() } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UAssignmentExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UAssignmentExpression.kt new file mode 100644 index 00000000000..133ea82c24a --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UAssignmentExpression.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2000-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.uast + +interface UAssignmentExpression : UExpression { + val reference: UExpression + val operator: String + val value: UExpression + + override fun traverse(handler: UastHandler) { + reference.handleTraverse(handler) + value.handleTraverse(handler) + } + + override fun logString() = "UAssignmentExpression ($operator)\n" + reference.logString().withMargin + "\n" + value.logString().withMargin + + override fun renderString() = reference.renderString() + ' ' + operator + ' ' + value.renderString() +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpression.kt new file mode 100644 index 00000000000..b24019d3a2e --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpression.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-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.uast + +interface UBinaryExpression : UExpression { + val leftOperand: UExpression + val operator: UastBinaryOperator + val rightOperand: UExpression + + override fun traverse(handler: UastHandler) { + leftOperand.handleTraverse(handler) + rightOperand.handleTraverse(handler) + } + + override fun logString() = + "UBinaryExpression (${operator.text})\n" + leftOperand.logString().withMargin + "\n" + rightOperand.logString().withMargin + + override fun renderString() = leftOperand.renderString() + ' ' + operator.text + ' ' + rightOperand.renderString() +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpressionWithType.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpressionWithType.kt new file mode 100644 index 00000000000..bc1916a2517 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBinaryExpressionWithType.kt @@ -0,0 +1,30 @@ +/* + * Copyright 2000-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.uast + +interface UBinaryExpressionWithType : UExpression { + val operand: UExpression + val operationKind: UastBinaryExpressionWithTypeKind + val type: UType + + override fun logString() = log("UBinaryExpressionWithType (${getExpressionType()?.name}, ${operationKind.name})", operand) + override fun renderString() = "(${operand.renderString()}) ${operationKind.name} ${getExpressionType()?.name}" + + override fun traverse(handler: UastHandler) { + operand.handleTraverse(handler) + type.handleTraverse(handler) + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UBlockExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBlockExpression.kt new file mode 100644 index 00000000000..3a6fb9a7148 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UBlockExpression.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +interface UBlockExpression : UExpression { + val expressions: List + + override fun traverse(handler: UastHandler) = expressions.handleTraverseList(handler) + override fun logString() = log("UBlockExpression", expressions) + + override fun renderString() = buildString { + appendln("{") + expressions.forEach { appendln(it.renderString().withMargin) } + appendln("}") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallExpression.kt new file mode 100644 index 00000000000..41773356988 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallExpression.kt @@ -0,0 +1,52 @@ +/* + * Copyright 2000-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.uast + +interface UCallExpression : UExpression, UResolvable { + val functionReference: USimpleReferenceExpression? + + val classReference: USimpleReferenceExpression? + + val functionName: String? + val functionNameElement: UElement? + + fun functionNameMatches(name: String) = functionName == name + + val valueArgumentCount: Int + val valueArguments: List + + val typeArgumentCount: Int + val typeArguments: List + + val kind: UastCallKind + + override fun resolve(context: UastContext): UFunction? + override fun resolveOrEmpty(context: UastContext): UFunction = resolve(context) ?: UFunctionNotResolved + + override fun traverse(handler: UastHandler) { + functionReference?.handleTraverse(handler) + classReference?.handleTraverse(handler) + functionNameElement?.handleTraverse(handler) + valueArguments.handleTraverseList(handler) + typeArguments.handleTraverseList(handler) + } + + override fun logString() = log("UFunctionCallExpression ($kind, argCount = $valueArgumentCount)", functionReference, valueArguments) + override fun renderString(): String { + val ref = functionName ?: functionReference?.renderString() ?: classReference?.renderString() ?: "" + return ref + "(" + valueArguments.joinToString { it.renderString() } + ")" + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallableReferenceExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallableReferenceExpression.kt new file mode 100644 index 00000000000..42ef013bc47 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UCallableReferenceExpression.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2000-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.uast + +interface UCallableReferenceExpression : UExpression { + val qualifierType: UType + + override fun traverse(handler: UastHandler) { + qualifierType.handleTraverse(handler) + } + + override fun logString() = "UCallableReferenceExpression" + override fun renderString() = "::" + qualifierType.name +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UClassLiteralExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UClassLiteralExpression.kt new file mode 100644 index 00000000000..fc8f32e0957 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UClassLiteralExpression.kt @@ -0,0 +1,21 @@ +/* + * Copyright 2000-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.uast + +interface UClassLiteralExpression : UExpression, NoTraverse { + override fun logString() = "UClassLiteralExpression" + override fun renderString() = getExpressionType()?.name ?: "::class" +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UDeclarationsExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UDeclarationsExpression.kt new file mode 100644 index 00000000000..afd6bb83de5 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UDeclarationsExpression.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2000-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.uast + +interface UDeclarationsExpression : UExpression { + val declarations: List + + val variables: List + get() = declarations.filterIsInstance() + + override fun evaluate() = null + override fun traverse(handler: UastHandler) = declarations.handleTraverseList(handler) + + override fun renderString() = declarations.joinToString("\n") { it.renderString() } + override fun logString() = log("UDeclarationsExpression", declarations) +} + +class SimpleUDeclarationsExpression( + override val parent: UElement, + override val declarations: List +) : UDeclarationsExpression { + override fun evaluate() = null +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UExpressionList.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UExpressionList.kt new file mode 100644 index 00000000000..c4725a154e4 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UExpressionList.kt @@ -0,0 +1,35 @@ +/* + * Copyright 2000-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.uast + +interface UExpressionList : UExpression { + val expressions: List + + override fun evaluate(): Any? = null + override fun traverse(handler: UastHandler) = expressions.forEach { it.handleTraverse(handler) } + + override fun logString() = log("UExpressionList", expressions) + override fun renderString() = log("", expressions) +} + +interface USpecialExpressionList : UExpressionList { + val kind: UastSpecialExpressionKind + + fun firstOrNull(): UExpression? = expressions.firstOrNull() + + override fun logString() = log("USpecialExpressionList (${kind.name})", expressions) + override fun renderString() = kind.name + " " + expressions.joinToString(" : ") { it.renderString() } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UForEachExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UForEachExpression.kt new file mode 100644 index 00000000000..319c9891e8a --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UForEachExpression.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2000-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.uast + +interface UForEachExpression : ULoopExpression { + val variableName: String? + val iteratedValue: UExpression + + override fun traverse(handler: UastHandler) { + iteratedValue.handleTraverse(handler) + body.handleTraverse(handler) + } + + override fun renderString() = buildString { + append("for (") + append(variableName ?: "") + append(" : ") + append(iteratedValue.renderString()) + append(") ") + append(body.renderString()) + } + + override fun logString() = "UForEachExpression ($variableName)\n" + + iteratedValue.logString().withMargin + "\n" + + body.logString().withMargin +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/ULabeledExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULabeledExpression.kt new file mode 100644 index 00000000000..1be68152c70 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULabeledExpression.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +interface ULabeledExpression : UExpression { + val label: String + val expression: UExpression + + override fun traverse(handler: UastHandler) { + expression.handleTraverse(handler) + } + + override fun logString() = log("ULabeledExpression ($label)", expression) + + override fun renderString() = "$label@ ${expression.renderString()}" +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/ULambdaExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULambdaExpression.kt new file mode 100644 index 00000000000..deb3afc94c9 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULambdaExpression.kt @@ -0,0 +1,36 @@ +/* + * Copyright 2000-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.uast + +interface ULambdaExpression : UExpression { + val valueParameters: List + val body: UExpression + + override fun traverse(handler: UastHandler) { + valueParameters.handleTraverseList(handler) + body.handleTraverse(handler) + } + + override fun logString() = log("ULambdaExpression", valueParameters, body) + override fun renderString(): String { + val renderedValueParameters = if (valueParameters.isEmpty()) + "" + else + valueParameters.joinToString { it.renderString() } + " ->\n" + + return "{ " + renderedValueParameters + body.renderString().withMargin + "\n}" + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/ULiteralExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULiteralExpression.kt new file mode 100644 index 00000000000..9c1ab5055c4 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULiteralExpression.kt @@ -0,0 +1,34 @@ +/* + * Copyright 2000-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.uast + +interface ULiteralExpression : UExpression { + val text: String + + val value: Any? + + val isNull: Boolean + + val isString: Boolean + get() = evaluate() is String + + val isBoolean: Boolean + get() = evaluate() is Boolean + + override fun traverse(handler: UastHandler) {} + override fun logString() = "ULiteralExpression ($text)" + override fun renderString() = text +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/ULoopExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULoopExpression.kt new file mode 100644 index 00000000000..9c5028250a5 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/ULoopExpression.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2000-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.uast + +interface ULoopExpression : UExpression { + val body: UExpression +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UNamedExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UNamedExpression.kt new file mode 100644 index 00000000000..fdeaee98412 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UNamedExpression.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-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.uast + +class UNamedExpression( + override val name: String, + override val parent: UElement +): UExpression, UNamed { + lateinit var expression: UExpression + + override fun traverse(handler: UastHandler) { + expression.handleTraverse(handler) + } + + override fun logString() = log("UNamedExpression ($name)", expression) + override fun renderString() = "$name: $expression" + + override fun evaluate() = expression.evaluate() +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UObjectLiteralExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UObjectLiteralExpression.kt new file mode 100644 index 00000000000..f22d719d856 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UObjectLiteralExpression.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2000-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.uast + +interface UObjectLiteralExpression : UExpression { + val declaration: UClass + + override fun traverse(handler: UastHandler) { + declaration.handleTraverse(handler) + } + + override fun logString() = log("UObjectLiteralExpression", declaration) + override fun renderString() = "anonymous " + declaration.renderString() +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UParenthesizedExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UParenthesizedExpression.kt new file mode 100644 index 00000000000..0d373ea5bb2 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UParenthesizedExpression.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2000-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.uast + +interface UParenthesizedExpression : UExpression { + val expression: UExpression + + override fun traverse(handler: UastHandler) { + expression.handleTraverse(handler) + } + + override fun logString() = log("UParenthesizedExpression", expression) + override fun renderString() = '(' + expression.renderString() + ')' +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UQualifiedExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UQualifiedExpression.kt new file mode 100644 index 00000000000..3495c3c1826 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UQualifiedExpression.kt @@ -0,0 +1,33 @@ +/* + * Copyright 2000-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.uast + +interface UQualifiedExpression : UExpression, UResolvable { + val receiver: UExpression + val selector: UExpression + val accessType: UastQualifiedExpressionAccessType + + fun selectorMatches(identifier: String) = (selector as? USimpleReferenceExpression)?.identifier == identifier + + override fun renderString() = receiver.renderString() + accessType.name + selector.renderString() + + override fun traverse(handler: UastHandler) { + receiver.handleTraverse(handler) + selector.handleTraverse(handler) + } + + override fun logString() = log("UQualifiedExpression", receiver, selector) +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/USimpleReferenceExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/USimpleReferenceExpression.kt new file mode 100644 index 00000000000..ffb6155e4f6 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/USimpleReferenceExpression.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2000-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.uast + +interface USimpleReferenceExpression : UExpression, UResolvable, NoTraverse { + val identifier: String + + override fun logString() = "USimpleReferenceExpression ($identifier)" + override fun renderString() = identifier +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/USuperExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/USuperExpression.kt new file mode 100644 index 00000000000..36b4cc2519f --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/USuperExpression.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2000-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.uast + +interface USuperExpression : UExpression { + override fun logString() = "USuperExpression" + override fun renderString() = "super" + override fun traverse(handler: UastHandler) {} +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UThisExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UThisExpression.kt new file mode 100644 index 00000000000..ef4f8bf757d --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UThisExpression.kt @@ -0,0 +1,22 @@ +/* + * Copyright 2000-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.uast + +interface UThisExpression : UExpression { + override fun logString() = "UThisExpression" + override fun renderString() = "this" + override fun traverse(handler: UastHandler) {} +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/UUnaryExpression.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/UUnaryExpression.kt new file mode 100644 index 00000000000..fc8b777df2e --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/UUnaryExpression.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2000-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.uast + +import org.jetbrains.uast.kinds.UastOperator + +interface UUnaryExpression : UExpression { + val operand: UExpression + val operator: UastOperator + + override fun traverse(handler: UastHandler) { + operand.handleTraverse(handler) + } +} + +interface UPrefixExpression : UUnaryExpression { + override val operator: UastPrefixOperator + override fun logString() = log("UPrefixExpression (${operator.text})", operand) + override fun renderString() = operator.text + operand.renderString() +} + +interface UPostfixExpression : UUnaryExpression { + override val operator: UastPostfixOperator + override fun logString() = log("UPostfixExpression (${operator.text})", operand) + override fun renderString() = operand.renderString() + operator.text +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/expressions/uastLiteralUtils.kt b/plugins/uast-common/src/org/jetbrains/uast/expressions/uastLiteralUtils.kt new file mode 100644 index 00000000000..087be8bcc76 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/expressions/uastLiteralUtils.kt @@ -0,0 +1,48 @@ +/* + * Copyright 2000-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. + */ +@file:JvmName("UastLiteralUtils") +package org.jetbrains.uast + +fun UElement.isNullLiteral(): Boolean = this is ULiteralExpression && this.isNull + +fun UElement.isStringLiteral(): Boolean = this is ULiteralExpression && this.isString + +fun UElement.getValueIfStringLiteral(): String? = + if (isStringLiteral()) (this as ULiteralExpression).value as String else null + +fun UElement.isNumberLiteral(): Boolean = this is ULiteralExpression && this.value is Number + +fun UElement.isIntegralLiteral(): Boolean = this is ULiteralExpression && when (value) { + is Int -> true + is Long -> true + is Short -> true + is Char -> true + is Byte -> true + else -> false +} + +fun UElement.isBooleanLiteral(): Boolean = this is ULiteralExpression && this.isBoolean + +fun ULiteralExpression.getLongValue(): Long = value.let { + when (it) { + is Long -> it + is Int -> it.toLong() + is Short -> it.toLong() + is Char -> it.toLong() + is Byte -> it.toLong() + else -> 0 + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/internal/internalUastUtils.kt b/plugins/uast-common/src/org/jetbrains/uast/internal/internalUastUtils.kt new file mode 100644 index 00000000000..6a585432c06 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/internal/internalUastUtils.kt @@ -0,0 +1,18 @@ +/* + * Copyright 2000-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.uast + +internal fun List.logString() = joinToString("\n") { it.logString().withMargin } \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryExpressionWithTypeKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryExpressionWithTypeKind.kt new file mode 100644 index 00000000000..93643a3c1fb --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryExpressionWithTypeKind.kt @@ -0,0 +1,37 @@ +/* + * Copyright 2000-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. + */ +@file:JvmName("UastBinaryExpressionWithTypeUtils") +package org.jetbrains.uast + +open class UastBinaryExpressionWithTypeKind(val name: String) { + open class TypeCast(name: String) : UastBinaryExpressionWithTypeKind(name) + open class InstanceCheck(name: String) : UastBinaryExpressionWithTypeKind(name) + + companion object { + @JvmField + val TYPE_CAST = TypeCast("as") + + @JvmField + val INSTANCE_CHECK = InstanceCheck("is") + + @JvmField + val UNKNOWN = UastBinaryExpressionWithTypeKind("") + } +} + +fun UElement.isTypeCast() = this is UBinaryExpressionWithType && this.operationKind is UastBinaryExpressionWithTypeKind.TypeCast + +fun UElement.isInstanceCheck() = this is UBinaryExpressionWithType && this.operationKind is UastBinaryExpressionWithTypeKind.InstanceCheck \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryOperator.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryOperator.kt new file mode 100644 index 00000000000..7033838490a --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastBinaryOperator.kt @@ -0,0 +1,124 @@ +/* + * Copyright 2000-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.uast + +import org.jetbrains.uast.kinds.UastOperator + +class UastBinaryOperator(override val text: String): UastOperator { + companion object { + @JvmField + val ASSIGN = UastBinaryOperator("=") + + @JvmField + val PLUS = UastBinaryOperator("+") + + @JvmField + val MINUS = UastBinaryOperator("-") + + @JvmField + val MULT = UastBinaryOperator("*") + + @JvmField + val DIV = UastBinaryOperator("/") + + @JvmField + val MOD = UastBinaryOperator("%") + + @JvmField + val LOGICAL_OR = UastBinaryOperator("||") + + @JvmField + val LOGICAL_AND = UastBinaryOperator("&&") + + @JvmField + val BITWISE_OR = UastBinaryOperator("|") + + @JvmField + val BITWISE_AND = UastBinaryOperator("&") + + @JvmField + val BITWISE_XOR = UastBinaryOperator("^") + + @JvmField + val EQUALS = UastBinaryOperator("==") + + @JvmField + val NOT_EQUALS = UastBinaryOperator("!=") + + @JvmField + val IDENTITY_EQUALS = UastBinaryOperator("===") + + @JvmField + val IDENTITY_NOT_EQUALS = UastBinaryOperator("!==") + + @JvmField + val GREATER = UastBinaryOperator(">") + + @JvmField + val GREATER_OR_EQUAL = UastBinaryOperator(">=") + + @JvmField + val LESS = UastBinaryOperator("<") + + @JvmField + val LESS_OR_EQUAL = UastBinaryOperator("<=") + + @JvmField + val SHIFT_LEFT = UastBinaryOperator("<<") + + @JvmField + val SHIFT_RIGHT = UastBinaryOperator(">>") + + @JvmField + val BITWISE_SHIFT_RIGHT = UastBinaryOperator(">>>") + + @JvmField + val UNKNOWN = UastBinaryOperator("") + + @JvmField + val PLUS_ASSIGN = UastBinaryOperator("+=") + + @JvmField + val MINUS_ASSIGN = UastBinaryOperator("-=") + + @JvmField + val MULTIPLY_ASSIGN = UastBinaryOperator("*=") + + @JvmField + val DIVIDE_ASSIGN = UastBinaryOperator("/=") + + @JvmField + val REMAINDER_ASSIGN = UastBinaryOperator("%=") + + @JvmField + val AND_ASSIGN = UastBinaryOperator("&=") + + @JvmField + val XOR_ASSIGN = UastBinaryOperator("^=") + + @JvmField + val OR_ASSIGN = UastBinaryOperator("|=") + + @JvmField + val SHIFT_LEFT_ASSIGN = UastBinaryOperator("<<=") + + @JvmField + val SHIFT_RIGHT_ASSIGN = UastBinaryOperator(">>=") + + @JvmField + val BITWISE_SHIFT_RIGHT_ASSIGN = UastBinaryOperator(">>>=") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastCallKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastCallKind.kt new file mode 100644 index 00000000000..6336972fb24 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastCallKind.kt @@ -0,0 +1,26 @@ +/* + * Copyright 2000-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.uast + +class UastCallKind(val name: String) { + companion object { + @JvmField + val FUNCTION_CALL = UastCallKind("function_call") + + @JvmField + val CONSTRUCTOR_CALL = UastCallKind("constructor_call") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt new file mode 100644 index 00000000000..b18285fa62c --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastFunctionKind.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +class UastFunctionKind(val name: String) { + companion object { + @JvmField + val FUNCTION = UastFunctionKind("function") + + @JvmField + val CONSTRUCTOR = UastFunctionKind("CONSTRUCTOR") + + @JvmField + val INITIALIZER = UastFunctionKind("INITIALIZER") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastModifier.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastModifier.kt new file mode 100644 index 00000000000..d620ec1d431 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastModifier.kt @@ -0,0 +1,27 @@ +/* + * Copyright 2000-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.uast + +class UastModifier(val name: String) { + companion object { + @JvmField + val ABSTRACT = UastModifier("abstract") + @JvmField + val INNER = UastModifier("inner") + @JvmField + val FINAL = UastModifier("final") + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastOperator.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastOperator.kt new file mode 100644 index 00000000000..72022aa9087 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastOperator.kt @@ -0,0 +1,20 @@ +/* + * Copyright 2000-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.uast.kinds + +interface UastOperator { + val text: String +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPostfixOperator.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPostfixOperator.kt new file mode 100644 index 00000000000..c6696537447 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPostfixOperator.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2000-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.uast + +import org.jetbrains.uast.kinds.UastOperator + +class UastPostfixOperator(override val text: String): UastOperator { + companion object { + @JvmField + val INC = UastPostfixOperator("++") + + @JvmField + val DEC = UastPostfixOperator("--") + + @JvmField + val UNKNOWN = UastPostfixOperator("") + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPrefixOperator.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPrefixOperator.kt new file mode 100644 index 00000000000..2172aa41f04 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastPrefixOperator.kt @@ -0,0 +1,43 @@ +/* + * Copyright 2000-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.uast + +import org.jetbrains.uast.kinds.UastOperator + +class UastPrefixOperator(override val text: String): UastOperator { + companion object { + @JvmField + val INC = UastPrefixOperator("++") + + @JvmField + val DEC = UastPrefixOperator("--") + + @JvmField + val UNARY_MINUS = UastPrefixOperator("-") + + @JvmField + val UNARY_PLUS = UastPrefixOperator("+") + + @JvmField + val LOGICAL_NOT = UastPrefixOperator("!") + + @JvmField + val BITWISE_NOT = UastPrefixOperator("~") + + @JvmField + val UNKNOWN = UastPrefixOperator("") + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastQualifiedExpressionAccessType.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastQualifiedExpressionAccessType.kt new file mode 100644 index 00000000000..2c88bc12477 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastQualifiedExpressionAccessType.kt @@ -0,0 +1,23 @@ +/* + * Copyright 2000-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.uast + +class UastQualifiedExpressionAccessType(val name: String) { + companion object { + @JvmField + val SIMPLE = UastQualifiedExpressionAccessType(".") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastSpecialExpressionKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastSpecialExpressionKind.kt new file mode 100644 index 00000000000..653eecef1d4 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastSpecialExpressionKind.kt @@ -0,0 +1,32 @@ +/* + * Copyright 2000-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.uast + +class UastSpecialExpressionKind(val name: String) { + companion object { + @JvmField + val RETURN = UastSpecialExpressionKind("return") + + @JvmField + val THROW = UastSpecialExpressionKind("throw") + + @JvmField + val BREAK = UastSpecialExpressionKind("break") + + @JvmField + val CONTINUE = UastSpecialExpressionKind("continue") + } +} diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVariableKind.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVariableKind.kt new file mode 100644 index 00000000000..891c3f09978 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVariableKind.kt @@ -0,0 +1,29 @@ +/* + * Copyright 2000-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.uast + +class UastVariableKind(val name: String) { + companion object { + @JvmField + val LOCAL_VARIABLE = UastVariableKind("local") + + @JvmField + val MEMBER = UastVariableKind("member") + + @JvmField + val VALUE_PARAMETER = UastVariableKind("parameter") + } +} \ No newline at end of file diff --git a/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVisibility.kt b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVisibility.kt new file mode 100644 index 00000000000..88d84d507a6 --- /dev/null +++ b/plugins/uast-common/src/org/jetbrains/uast/kinds/UastVisibility.kt @@ -0,0 +1,31 @@ +/* + * Copyright 2000-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.uast + +class UastVisibility(val name: String) { + companion object { + @JvmField + val PUBLIC = UastVisibility("public") + @JvmField + val PRIVATE = UastVisibility("private") + @JvmField + val PROTECTED = UastVisibility("protected") + @JvmField + val LOCAL = UastVisibility("local") + } + + fun isPublic() = this == PUBLIC +} \ No newline at end of file diff --git a/plugins/uast-common/uast-common.iml b/plugins/uast-common/uast-common.iml new file mode 100644 index 00000000000..44071adbdcd --- /dev/null +++ b/plugins/uast-common/uast-common.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file