|
| 1 | +/* |
| 2 | + * Copyright 2023 Google Inc. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); you may not |
| 5 | + * use this file except in compliance with the License. You may obtain a copy of |
| 6 | + * the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations under |
| 14 | + * the License. |
| 15 | + */ |
| 16 | +package com.google.j2cl.transpiler.backend.kotlin |
| 17 | + |
| 18 | +import com.google.common.base.CaseFormat |
| 19 | +import com.google.j2cl.transpiler.ast.CompilationUnit |
| 20 | +import com.google.j2cl.transpiler.ast.DeclaredTypeDescriptor |
| 21 | +import com.google.j2cl.transpiler.ast.FieldDescriptor |
| 22 | +import com.google.j2cl.transpiler.ast.Method |
| 23 | +import com.google.j2cl.transpiler.ast.MethodDescriptor |
| 24 | +import com.google.j2cl.transpiler.ast.PrimitiveTypeDescriptor |
| 25 | +import com.google.j2cl.transpiler.ast.PrimitiveTypes |
| 26 | +import com.google.j2cl.transpiler.ast.Type |
| 27 | +import com.google.j2cl.transpiler.ast.TypeDeclaration |
| 28 | +import com.google.j2cl.transpiler.ast.TypeDescriptor |
| 29 | +import com.google.j2cl.transpiler.ast.TypeDescriptors.isJavaLangObject |
| 30 | +import com.google.j2cl.transpiler.ast.TypeDescriptors.isPrimitiveVoid |
| 31 | +import com.google.j2cl.transpiler.ast.Variable |
| 32 | +import com.google.j2cl.transpiler.backend.kotlin.common.buildList |
| 33 | +import com.google.j2cl.transpiler.backend.kotlin.common.letIf |
| 34 | +import com.google.j2cl.transpiler.backend.kotlin.objc.Rendering |
| 35 | +import com.google.j2cl.transpiler.backend.kotlin.objc.bindSource |
| 36 | +import com.google.j2cl.transpiler.backend.kotlin.objc.classNameRendering |
| 37 | +import com.google.j2cl.transpiler.backend.kotlin.objc.classRendering |
| 38 | +import com.google.j2cl.transpiler.backend.kotlin.objc.combineSources |
| 39 | +import com.google.j2cl.transpiler.backend.kotlin.objc.comment |
| 40 | +import com.google.j2cl.transpiler.backend.kotlin.objc.dependency |
| 41 | +import com.google.j2cl.transpiler.backend.kotlin.objc.functionDeclaration |
| 42 | +import com.google.j2cl.transpiler.backend.kotlin.objc.idRendering |
| 43 | +import com.google.j2cl.transpiler.backend.kotlin.objc.localImport |
| 44 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsCopyingRendering |
| 45 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsEnumTypedef |
| 46 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsInlineRendering |
| 47 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsMutableArrayRendering |
| 48 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsMutableDictionaryRendering |
| 49 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsMutableSetRendering |
| 50 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsNumberRendering |
| 51 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsObjectRendering |
| 52 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsStringRendering |
| 53 | +import com.google.j2cl.transpiler.backend.kotlin.objc.nsUIntegerRendering |
| 54 | +import com.google.j2cl.transpiler.backend.kotlin.objc.orEmpty |
| 55 | +import com.google.j2cl.transpiler.backend.kotlin.objc.protocolNameRendering |
| 56 | +import com.google.j2cl.transpiler.backend.kotlin.objc.rendering |
| 57 | +import com.google.j2cl.transpiler.backend.kotlin.objc.renderingWith |
| 58 | +import com.google.j2cl.transpiler.backend.kotlin.objc.returnStatement |
| 59 | +import com.google.j2cl.transpiler.backend.kotlin.objc.sourceWithDependencies |
| 60 | +import com.google.j2cl.transpiler.backend.kotlin.source.dotSeparated |
| 61 | +import com.google.j2cl.transpiler.backend.kotlin.source.emptyLineSeparated |
| 62 | +import com.google.j2cl.transpiler.backend.kotlin.source.ifNotEmpty |
| 63 | +import com.google.j2cl.transpiler.backend.kotlin.source.inAngleBrackets |
| 64 | +import com.google.j2cl.transpiler.backend.kotlin.source.join |
| 65 | +import com.google.j2cl.transpiler.backend.kotlin.source.plusNewLine |
| 66 | +import com.google.j2cl.transpiler.backend.kotlin.source.source |
| 67 | +import com.google.j2cl.transpiler.backend.kotlin.source.spaceSeparated |
| 68 | + |
| 69 | +internal val CompilationUnit.j2ObjCCompatHeaderSource |
| 70 | + get() = |
| 71 | + dependenciesAndDeclarationsSource.ifNotEmpty { |
| 72 | + emptyLineSeparated(fileCommentSource, it).plusNewLine |
| 73 | + } |
| 74 | + |
| 75 | +private val CompilationUnit.fileCommentSource |
| 76 | + get() = comment(source("Generated by J2KT from \"${packageRelativePath}\"")) |
| 77 | + |
| 78 | +private val CompilationUnit.dependenciesAndDeclarationsSource |
| 79 | + get() = declarationsRendering.sourceWithDependencies |
| 80 | + |
| 81 | +private val CompilationUnit.declarationsRendering |
| 82 | + get() = declarationsRenderings.combineSources(::emptyLineSeparated) |
| 83 | + |
| 84 | +private val CompilationUnit.declarationsRenderings |
| 85 | + get() = types.flatMap { it.declarationsRenderings } |
| 86 | + |
| 87 | +private val Type.declarationsRenderings |
| 88 | + get() = |
| 89 | + buildList<Rendering> { |
| 90 | + if (visibility.isPrivate || declaration.isKtNative) { |
| 91 | + return@buildList |
| 92 | + } |
| 93 | + |
| 94 | + if (isEnum) { |
| 95 | + add(nsEnumTypedefRendering) |
| 96 | + addAll(enumGetFunctionRenderings) |
| 97 | + } |
| 98 | + |
| 99 | + addAll(methods.map { it.functionRendering }) |
| 100 | + } |
| 101 | + |
| 102 | +private val Type.nsEnumTypedefRendering |
| 103 | + get() = |
| 104 | + nsEnumTypedef( |
| 105 | + name = declaration.objCEnumName, |
| 106 | + type = nsUIntegerRendering, |
| 107 | + values = enumFields.map { it.descriptor.objCEnumName } |
| 108 | + ) |
| 109 | + |
| 110 | +private val Type.enumGetFunctionRenderings |
| 111 | + get() = enumFields.map { it.descriptor.enumGetFunctionRendering } |
| 112 | + |
| 113 | +private val FieldDescriptor.enumGetFunctionRendering |
| 114 | + get() = |
| 115 | + functionDeclaration( |
| 116 | + modifiers = listOf(nsInlineRendering), |
| 117 | + returnType = enclosingTypeDescriptor.objCRendering, |
| 118 | + name = enumGetFunctionName, |
| 119 | + parameters = listOf(), |
| 120 | + statements = listOf(returnStatement(enumGetExpressionRendering)) |
| 121 | + ) |
| 122 | + |
| 123 | +private val FieldDescriptor.enumGetFunctionName |
| 124 | + get() = enclosingTypeDescriptor.typeDeclaration.objCName(forMember = true) + "_get_" + name!! |
| 125 | + |
| 126 | +private val FieldDescriptor.enumObjCName |
| 127 | + get() = CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, name!!) |
| 128 | + |
| 129 | +private val FieldDescriptor.enumGetExpressionRendering |
| 130 | + get() = |
| 131 | + enclosingTypeDescriptor.typeDeclaration.objCNameRendering.bindSource { enumClassSource -> |
| 132 | + rendering(dotSeparated(enumClassSource, source(enumObjCName))) |
| 133 | + } |
| 134 | + |
| 135 | +private val Method.functionRendering |
| 136 | + get() = |
| 137 | + takeIf { it.isStatic && !it.isConstructor } |
| 138 | + ?.toObjCNames() |
| 139 | + ?.let { functionRendering(it) } |
| 140 | + .orEmpty |
| 141 | + |
| 142 | +private fun Method.functionRendering(objCNames: MethodObjCNames) = |
| 143 | + functionDeclaration( |
| 144 | + modifiers = listOf(nsInlineRendering), |
| 145 | + returnType = descriptor.returnTypeDescriptor.objCRendering, |
| 146 | + name = descriptor.functionName(objCNames), |
| 147 | + parameters = parameters.map { it.rendering }, |
| 148 | + statements = statementRenderings |
| 149 | + ) |
| 150 | + |
| 151 | +private fun MethodDescriptor.functionName(objCNames: MethodObjCNames) = |
| 152 | + enclosingTypeDescriptor |
| 153 | + .objCName(useId = true, forMember = true) |
| 154 | + .plus("_") |
| 155 | + .plus(objCNames.methodName ?: ktName) |
| 156 | + .letIf(objCNames.parameterNames.isNotEmpty()) { |
| 157 | + it.plus("_").plus(objCNames.parameterNames.map { it + "_" }.joinToString("")) |
| 158 | + } |
| 159 | + |
| 160 | +private val Method.statementRenderings |
| 161 | + get() = |
| 162 | + if (isPrimitiveVoid(descriptor.returnTypeDescriptor)) listOf() |
| 163 | + else listOf(returnStatement(rendering(source("0")))) |
| 164 | + |
| 165 | +private val Variable.rendering |
| 166 | + get() = |
| 167 | + typeDescriptor.objCRendering.bindSource { typeSource -> |
| 168 | + rendering(spaceSeparated(typeSource, source(name.objCName))) |
| 169 | + } |
| 170 | + |
| 171 | +private val TypeDeclaration.objCNameRendering |
| 172 | + get() = objectiveCNameRendering ?: mappedObjCNameRendering ?: defaultObjCNameRendering |
| 173 | + |
| 174 | +private val TypeDeclaration.objectiveCNameRendering |
| 175 | + get() = |
| 176 | + objectiveCName?.let { if (isInterface) protocolNameRendering(it) else classNameRendering(it) } |
| 177 | + |
| 178 | +private val TypeDeclaration.mappedObjCNameRendering |
| 179 | + get() = |
| 180 | + when (qualifiedBinaryName) { |
| 181 | + "java.lang.Object" -> nsObjectRendering |
| 182 | + "java.lang.String" -> nsStringRendering |
| 183 | + "java.lang.Class" -> classRendering |
| 184 | + "java.lang.Number" -> nsNumberRendering |
| 185 | + "java.lang.Cloneable" -> nsCopyingRendering |
| 186 | + "java.util.List" -> nsMutableArrayRendering |
| 187 | + "java.util.Set" -> nsMutableSetRendering |
| 188 | + "java.util.Map" -> nsMutableDictionaryRendering |
| 189 | + else -> null |
| 190 | + } |
| 191 | + |
| 192 | +private val TypeDeclaration.defaultObjCNameRendering |
| 193 | + get() = |
| 194 | + defaultObjCName(forMember = false).let { |
| 195 | + if (isInterface) protocolNameRendering(it) else classNameRendering(it) |
| 196 | + } |
| 197 | + |
| 198 | +private val TypeDescriptor.objCRendering |
| 199 | + get() = |
| 200 | + when (this) { |
| 201 | + is PrimitiveTypeDescriptor -> primitiveObjCRendering |
| 202 | + is DeclaredTypeDescriptor -> declaredObjCRendering |
| 203 | + // TODO: Handle TypeVariable and Array |
| 204 | + else -> idRendering |
| 205 | + } |
| 206 | + |
| 207 | +private val PrimitiveTypeDescriptor.primitiveObjCRendering |
| 208 | + get() = |
| 209 | + when (this) { |
| 210 | + PrimitiveTypes.VOID -> rendering(source("void")) |
| 211 | + else -> source("j$simpleSourceName") renderingWith dependency(j2ObjCTypesImport) |
| 212 | + } |
| 213 | + |
| 214 | +private val DeclaredTypeDescriptor.declaredObjCRendering |
| 215 | + get() = |
| 216 | + when { |
| 217 | + isJavaLangObject(this) -> idRendering |
| 218 | + isInterface -> |
| 219 | + typeDeclaration.objCNameRendering.bindSource { |
| 220 | + rendering(join(source("id"), inAngleBrackets(it))) |
| 221 | + } |
| 222 | + else -> |
| 223 | + typeDeclaration.objCNameRendering.bindSource { rendering(spaceSeparated(it, source("*"))) } |
| 224 | + } |
| 225 | + |
| 226 | +private val j2ObjCTypesImport |
| 227 | + get() = localImport("third_party/java_src/j2objc/jre_emul/Classes/J2ObjC_types.h") |
| 228 | + |
| 229 | +internal val TypeDeclaration.objCEnumName |
| 230 | + get() = "${objCName}_Enum" |
| 231 | + |
| 232 | +internal val FieldDescriptor.objCEnumName |
| 233 | + get() = "${enclosingTypeDescriptor.typeDeclaration.objCEnumName}_$name" |
0 commit comments