|
| 1 | +/* |
| 2 | + * Minecraft Development for IntelliJ |
| 3 | + * |
| 4 | + * https://mcdev.io/ |
| 5 | + * |
| 6 | + * Copyright (C) 2025 minecraft-dev |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Lesser General Public License as published |
| 10 | + * by the Free Software Foundation, version 3.0 only. |
| 11 | + * |
| 12 | + * This program is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 19 | + */ |
| 20 | + |
| 21 | +package com.demonwav.mcdev.platform.mixin.handlers.injectionPoint |
| 22 | + |
| 23 | +import com.demonwav.mcdev.platform.mixin.reference.MixinSelector |
| 24 | +import com.demonwav.mcdev.util.MemberReference |
| 25 | +import com.intellij.openapi.editor.Editor |
| 26 | +import com.intellij.openapi.project.Project |
| 27 | +import com.intellij.psi.CommonClassNames |
| 28 | +import com.intellij.psi.PsiAnnotation |
| 29 | +import com.intellij.psi.PsiClass |
| 30 | +import com.intellij.psi.PsiElement |
| 31 | +import com.intellij.psi.PsiLiteral |
| 32 | +import com.intellij.psi.PsiMethod |
| 33 | +import com.intellij.psi.PsiMethodCallExpression |
| 34 | +import com.intellij.util.ArrayUtilRt |
| 35 | +import org.objectweb.asm.Opcodes |
| 36 | +import org.objectweb.asm.tree.ClassNode |
| 37 | +import org.objectweb.asm.tree.MethodInsnNode |
| 38 | +import org.objectweb.asm.tree.MethodNode |
| 39 | +import org.objectweb.asm.tree.VarInsnNode |
| 40 | +import org.objectweb.asm.util.Printer |
| 41 | + |
| 42 | +class InvokeAssignInjectionPoint : AbstractMethodInjectionPoint() { |
| 43 | + companion object { |
| 44 | + private val ARGS_KEYS = arrayOf("fuzz", "skip") |
| 45 | + private val SKIP_LIST_DELIMITER = "[ ,;]".toRegex() |
| 46 | + private val OPCODES_BY_NAME = Printer.OPCODES.withIndex().associate { it.value to it.index } |
| 47 | + private val DEFAULT_SKIP = setOf( |
| 48 | + // Opcodes which may appear if the targetted method is part of an |
| 49 | + // expression eg. int foo = 2 + this.bar(); |
| 50 | + Opcodes.DUP, Opcodes.IADD, Opcodes.LADD, Opcodes.FADD, Opcodes.DADD, |
| 51 | + Opcodes.ISUB, Opcodes.LSUB, Opcodes.FSUB, Opcodes.DSUB, Opcodes.IMUL, |
| 52 | + Opcodes.LMUL, Opcodes.FMUL, Opcodes.DMUL, Opcodes.IDIV, Opcodes.LDIV, |
| 53 | + Opcodes.FDIV, Opcodes.DDIV, Opcodes.IREM, Opcodes.LREM, Opcodes.FREM, |
| 54 | + Opcodes.DREM, Opcodes.INEG, Opcodes.LNEG, Opcodes.FNEG, Opcodes.DNEG, |
| 55 | + Opcodes.ISHL, Opcodes.LSHL, Opcodes.ISHR, Opcodes.LSHR, Opcodes.IUSHR, |
| 56 | + Opcodes.LUSHR, Opcodes.IAND, Opcodes.LAND, Opcodes.IOR, Opcodes.LOR, |
| 57 | + Opcodes.IXOR, Opcodes.LXOR, Opcodes.IINC, |
| 58 | + |
| 59 | + // Opcodes which may appear if the targetted method is cast before |
| 60 | + // assignment eg. int foo = (int)this.getFloat(); |
| 61 | + Opcodes.I2L, Opcodes.I2F, Opcodes.I2D, Opcodes.L2I, Opcodes.L2F, |
| 62 | + Opcodes.L2D, Opcodes.F2I, Opcodes.F2L, Opcodes.F2D, Opcodes.D2I, |
| 63 | + Opcodes.D2L, Opcodes.D2F, Opcodes.I2B, Opcodes.I2C, Opcodes.I2S, |
| 64 | + Opcodes.CHECKCAST, Opcodes.INSTANCEOF |
| 65 | + ) |
| 66 | + } |
| 67 | + |
| 68 | + override fun onCompleted(editor: Editor, reference: PsiLiteral) { |
| 69 | + completeExtraStringAtAttribute(editor, reference, "target") |
| 70 | + } |
| 71 | + |
| 72 | + override fun getArgsKeys(at: PsiAnnotation) = ARGS_KEYS |
| 73 | + |
| 74 | + override fun getArgsValues(at: PsiAnnotation, key: String): Array<out Any> { |
| 75 | + if (key == "skip") { |
| 76 | + return Printer.OPCODES |
| 77 | + } |
| 78 | + return ArrayUtilRt.EMPTY_OBJECT_ARRAY |
| 79 | + } |
| 80 | + |
| 81 | + override fun getArgValueListDelimiter(at: PsiAnnotation, key: String) = |
| 82 | + SKIP_LIST_DELIMITER.takeIf { key == "skip" } |
| 83 | + |
| 84 | + override fun isShiftDiscouraged(shift: Int): Boolean { |
| 85 | + // Allow shifting before INVOKE_ASSIGN |
| 86 | + return shift != 0 && shift != -1 |
| 87 | + } |
| 88 | + |
| 89 | + override fun createNavigationVisitor( |
| 90 | + at: PsiAnnotation, |
| 91 | + target: MixinSelector?, |
| 92 | + targetClass: PsiClass, |
| 93 | + ): NavigationVisitor? { |
| 94 | + return target?.let { MyNavigationVisitor(targetClass, it) } |
| 95 | + } |
| 96 | + |
| 97 | + override fun doCreateCollectVisitor( |
| 98 | + at: PsiAnnotation, |
| 99 | + target: MixinSelector?, |
| 100 | + targetClass: ClassNode, |
| 101 | + mode: CollectVisitor.Mode, |
| 102 | + ): CollectVisitor<PsiMethod>? { |
| 103 | + val args = AtResolver.getArgs(at) |
| 104 | + val fuzz = args["fuzz"]?.toIntOrNull()?.coerceAtLeast(1) ?: 1 |
| 105 | + val skip = args["skip"]?.let { parseSkip(it) } ?: DEFAULT_SKIP |
| 106 | + |
| 107 | + if (mode == CollectVisitor.Mode.COMPLETION) { |
| 108 | + return MyCollectVisitor(mode, at.project, MemberReference(""), fuzz, skip) |
| 109 | + } |
| 110 | + return target?.let { MyCollectVisitor(mode, at.project, it, fuzz, skip) } |
| 111 | + } |
| 112 | + |
| 113 | + private fun parseSkip(string: String): Set<Int> { |
| 114 | + return string.split(SKIP_LIST_DELIMITER) |
| 115 | + .asSequence() |
| 116 | + .mapNotNull { part -> |
| 117 | + val trimmedPart = part.trim() |
| 118 | + OPCODES_BY_NAME[trimmedPart.removePrefix("Opcodes.")] |
| 119 | + ?: trimmedPart.toIntOrNull()?.takeIf { it >= 0 && it < Printer.OPCODES.size } |
| 120 | + } |
| 121 | + .toSet() |
| 122 | + } |
| 123 | + |
| 124 | + private class MyNavigationVisitor( |
| 125 | + private val targetClass: PsiClass, |
| 126 | + private val selector: MixinSelector, |
| 127 | + ) : NavigationVisitor() { |
| 128 | + |
| 129 | + private fun visitMethodUsage(method: PsiMethod, qualifier: PsiClass?, expression: PsiElement) { |
| 130 | + if (selector.matchMethod(method, qualifier ?: targetClass)) { |
| 131 | + addResult(expression) |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + override fun visitMethodCallExpression(expression: PsiMethodCallExpression) { |
| 136 | + val method = expression.resolveMethod() |
| 137 | + if (method != null) { |
| 138 | + val containingClass = method.containingClass |
| 139 | + |
| 140 | + // Normally, Java uses the type of the instance to qualify the method calls |
| 141 | + // However, if the method is part of java.lang.Object (e.g. equals or toString) |
| 142 | + // and no class in the hierarchy of the instance overrides the method, Java will |
| 143 | + // insert the call using java.lang.Object as the owner |
| 144 | + val qualifier = |
| 145 | + if (method.isConstructor || containingClass?.qualifiedName == CommonClassNames.JAVA_LANG_OBJECT) { |
| 146 | + containingClass |
| 147 | + } else { |
| 148 | + QualifiedMember.resolveQualifier(expression.methodExpression) |
| 149 | + } |
| 150 | + |
| 151 | + visitMethodUsage(method, qualifier, expression) |
| 152 | + } |
| 153 | + |
| 154 | + super.visitMethodCallExpression(expression) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + private class MyCollectVisitor( |
| 159 | + mode: Mode, |
| 160 | + private val project: Project, |
| 161 | + private val selector: MixinSelector, |
| 162 | + private val fuzz: Int, |
| 163 | + private val skip: Set<Int>, |
| 164 | + ) : CollectVisitor<PsiMethod>(mode) { |
| 165 | + override fun accept(methodNode: MethodNode) { |
| 166 | + val insns = methodNode.instructions ?: return |
| 167 | + insns.iterator().forEachRemaining { insn -> |
| 168 | + if (insn !is MethodInsnNode) { |
| 169 | + return@forEachRemaining |
| 170 | + } |
| 171 | + |
| 172 | + val sourceMethod = nodeMatchesSelector(insn, mode, selector, project) ?: return@forEachRemaining |
| 173 | + |
| 174 | + val offset = insns.indexOf(insn) |
| 175 | + val maxOffset = (offset + fuzz + 1).coerceAtMost(insns.size()) |
| 176 | + var resultingInsn = insn |
| 177 | + for (i in offset + 1 until maxOffset) { |
| 178 | + val candidate = insns[i] |
| 179 | + if (candidate is VarInsnNode && candidate.opcode >= Opcodes.ISTORE) { |
| 180 | + resultingInsn = candidate |
| 181 | + break |
| 182 | + } else if (skip.isNotEmpty() && candidate.opcode !in skip) { |
| 183 | + break |
| 184 | + } |
| 185 | + } |
| 186 | + |
| 187 | + resultingInsn = resultingInsn.next ?: resultingInsn |
| 188 | + |
| 189 | + addResult( |
| 190 | + resultingInsn, |
| 191 | + sourceMethod, |
| 192 | + qualifier = insn.owner.replace('/', '.'), |
| 193 | + ) |
| 194 | + } |
| 195 | + } |
| 196 | + } |
| 197 | +} |
0 commit comments