|
| 1 | +/* |
| 2 | + * Scala.js (https://www.scala-js.org/) |
| 3 | + * |
| 4 | + * Copyright EPFL. |
| 5 | + * |
| 6 | + * Licensed under Apache License 2.0 |
| 7 | + * (https://www.apache.org/licenses/LICENSE-2.0). |
| 8 | + * |
| 9 | + * See the NOTICE file distributed with this work for |
| 10 | + * additional information regarding copyright ownership. |
| 11 | + */ |
| 12 | + |
| 13 | +package org.scalajs.linker.frontend |
| 14 | + |
| 15 | +import org.scalajs.ir.{ClassKind, Hashers, Position, SHA1, UTF8String, Version} |
| 16 | +import org.scalajs.ir.Names._ |
| 17 | +import org.scalajs.ir.OriginalName.NoOriginalName |
| 18 | +import org.scalajs.ir.Trees._ |
| 19 | +import org.scalajs.ir.Types._ |
| 20 | +import org.scalajs.ir.WellKnownNames._ |
| 21 | + |
| 22 | +import org.scalajs.linker.analyzer.Infos._ |
| 23 | + |
| 24 | +private[linker] object LambdaSynthesizer { |
| 25 | + /* Everything we create has a constant version because the names are derived |
| 26 | + * from the descriptors themselves. |
| 27 | + */ |
| 28 | + val constantVersion = Version.fromByte(0) |
| 29 | + |
| 30 | + private val ClosureTypeRefName = LabelName("c") |
| 31 | + |
| 32 | + /** Deterministically makes a class name for the lambda class given its descriptor. |
| 33 | + * |
| 34 | + * This computation is mildly expensive. Callers should cache it if possible. |
| 35 | + */ |
| 36 | + def makeClassName(descriptor: NewLambda.Descriptor): ClassName = { |
| 37 | + // Choose a base class name that will "makes sense" for debugging purposes |
| 38 | + val baseClassName = { |
| 39 | + if (descriptor.superClass == ObjectClass && descriptor.interfaces.nonEmpty) |
| 40 | + descriptor.interfaces.head |
| 41 | + else |
| 42 | + descriptor.superClass |
| 43 | + } |
| 44 | + |
| 45 | + val digestBuilder = new SHA1.DigestBuilder() |
| 46 | + digestBuilder.updateUTF8String(descriptor.superClass.encoded) |
| 47 | + for (intf <- descriptor.interfaces) |
| 48 | + digestBuilder.updateUTF8String(intf.encoded) |
| 49 | + |
| 50 | + // FIXME This is not efficient |
| 51 | + digestBuilder.updateUTF8String(UTF8String(descriptor.methodName.nameString)) |
| 52 | + |
| 53 | + // No need the hash the paramTypes and resultType because they derive from the method name |
| 54 | + |
| 55 | + val digest = digestBuilder.finalizeDigest() |
| 56 | + |
| 57 | + /* The "$$Lambda" segment is meant to match the way LambdaMetaFactory |
| 58 | + * names generated classes. This is mostly for test compatibility |
| 59 | + * purposes (like partest's that test the class name to tell whether a |
| 60 | + * lambda was indeed encoded as an LMF). |
| 61 | + */ |
| 62 | + val suffixBuilder = new java.lang.StringBuilder(".$$Lambda$") |
| 63 | + for (b <- digest) { |
| 64 | + val i = b & 0xff |
| 65 | + suffixBuilder.append(Character.forDigit(i >> 4, 16)).append(Character.forDigit(i & 0x0f, 16)) |
| 66 | + } |
| 67 | + |
| 68 | + ClassName(baseClassName.encoded ++ UTF8String(suffixBuilder.toString())) |
| 69 | + } |
| 70 | + |
| 71 | + /** Computes the constructor name for the lambda class of a descriptor. */ |
| 72 | + def makeConstructorName(descriptor: NewLambda.Descriptor): MethodName = { |
| 73 | + val closureTypeNonNull = |
| 74 | + ClosureType(descriptor.paramTypes, descriptor.resultType, nullable = false) |
| 75 | + MethodName.constructor(TransientTypeRef(ClosureTypeRefName)(closureTypeNonNull) :: Nil) |
| 76 | + } |
| 77 | + |
| 78 | + /** Computes the `ClassInfo` of a lambda class, for use by the `Analyzer`. |
| 79 | + * |
| 80 | + * The `className` must be the result of `makeClassName(descriptor)`. |
| 81 | + */ |
| 82 | + def makeClassInfo(descriptor: NewLambda.Descriptor, className: ClassName): ClassInfo = { |
| 83 | + val methodInfos = Array.fill(MemberNamespace.Count)(Map.empty[MethodName, MethodInfo]) |
| 84 | + |
| 85 | + val fFieldName = FieldName(className, SimpleFieldName("f")) |
| 86 | + val ctorName = makeConstructorName(descriptor) |
| 87 | + |
| 88 | + val ctorInfo: MethodInfo = { |
| 89 | + val b = new ReachabilityInfoBuilder(constantVersion) |
| 90 | + b.addFieldWritten(fFieldName) |
| 91 | + b.addMethodCalledStatically(descriptor.superClass, |
| 92 | + NamespacedMethodName(MemberNamespace.Constructor, NoArgConstructorName)) |
| 93 | + MethodInfo(isAbstract = false, b.result()) |
| 94 | + } |
| 95 | + methodInfos(MemberNamespace.Constructor.ordinal) = |
| 96 | + Map(ctorName -> ctorInfo) |
| 97 | + |
| 98 | + val implMethodInfo: MethodInfo = { |
| 99 | + val b = new ReachabilityInfoBuilder(constantVersion) |
| 100 | + b.addFieldRead(fFieldName) |
| 101 | + MethodInfo(isAbstract = false, b.result()) |
| 102 | + } |
| 103 | + methodInfos(MemberNamespace.Public.ordinal) = |
| 104 | + Map(descriptor.methodName -> implMethodInfo) |
| 105 | + |
| 106 | + new ClassInfo(className, ClassKind.Class, |
| 107 | + Some(descriptor.superClass), descriptor.interfaces, |
| 108 | + jsNativeLoadSpec = None, referencedFieldClasses = Map.empty, methodInfos, |
| 109 | + jsNativeMembers = Map.empty, jsMethodProps = Nil, topLevelExports = Nil) |
| 110 | + } |
| 111 | + |
| 112 | + /** Synthesizes the `ClassDef` for a lambda class, for use by the `BaseLinker`. |
| 113 | + * |
| 114 | + * The `className` must be the result of `makeClassName(descriptor)`. |
| 115 | + */ |
| 116 | + def makeClassDef(descriptor: NewLambda.Descriptor, className: ClassName): ClassDef = { |
| 117 | + implicit val pos = Position.NoPosition |
| 118 | + |
| 119 | + import descriptor._ |
| 120 | + |
| 121 | + val closureType = ClosureType(paramTypes, resultType, nullable = true) |
| 122 | + |
| 123 | + val thiz = This()(ClassType(className, nullable = false)) |
| 124 | + |
| 125 | + val fFieldIdent = FieldIdent(FieldName(className, SimpleFieldName("f"))) |
| 126 | + val fFieldDef = FieldDef(MemberFlags.empty, fFieldIdent, NoOriginalName, closureType) |
| 127 | + val fFieldSelect = Select(thiz, fFieldIdent)(closureType) |
| 128 | + |
| 129 | + val ctorParamDef = ParamDef(LocalIdent(LocalName("f")), NoOriginalName, |
| 130 | + closureType.toNonNullable, mutable = false) |
| 131 | + val ctorDef = MethodDef( |
| 132 | + MemberFlags.empty.withNamespace(MemberNamespace.Constructor), |
| 133 | + MethodIdent(makeConstructorName(descriptor)), |
| 134 | + NoOriginalName, |
| 135 | + ctorParamDef :: Nil, |
| 136 | + VoidType, |
| 137 | + Some( |
| 138 | + Block( |
| 139 | + Assign(fFieldSelect, ctorParamDef.ref), |
| 140 | + ApplyStatically(ApplyFlags.empty.withConstructor(true), thiz, |
| 141 | + superClass, MethodIdent(NoArgConstructorName), Nil)(VoidType) |
| 142 | + ) |
| 143 | + ) |
| 144 | + )(OptimizerHints.empty, constantVersion) |
| 145 | + |
| 146 | + val methodParamDefs = paramTypes.zipWithIndex.map { case (paramType, index) => |
| 147 | + ParamDef(LocalIdent(LocalName("x" + index)), NoOriginalName, paramType, mutable = false) |
| 148 | + } |
| 149 | + val methodDef = MethodDef( |
| 150 | + MemberFlags.empty, |
| 151 | + MethodIdent(methodName), |
| 152 | + NoOriginalName, |
| 153 | + methodParamDefs, |
| 154 | + resultType, |
| 155 | + Some( |
| 156 | + ApplyTypedClosure(ApplyFlags.empty, fFieldSelect, methodParamDefs.map(_.ref)) |
| 157 | + ) |
| 158 | + )(OptimizerHints.empty, constantVersion) |
| 159 | + |
| 160 | + val classDef = ClassDef( |
| 161 | + ClassIdent(className), |
| 162 | + NoOriginalName, |
| 163 | + ClassKind.Class, |
| 164 | + jsClassCaptures = None, |
| 165 | + superClass = Some(ClassIdent(superClass)), |
| 166 | + interfaces = interfaces.map(ClassIdent(_)), |
| 167 | + jsSuperClass = None, |
| 168 | + jsNativeLoadSpec = None, |
| 169 | + fields = List(fFieldDef), |
| 170 | + methods = List(ctorDef, methodDef), |
| 171 | + jsConstructor = None, |
| 172 | + jsMethodProps = Nil, |
| 173 | + jsNativeMembers = Nil, |
| 174 | + topLevelExportDefs = Nil |
| 175 | + )(OptimizerHints.empty.withInline(true)) |
| 176 | + |
| 177 | + //Hashers.hashClassDef(classDef) |
| 178 | + classDef |
| 179 | + } |
| 180 | +} |
0 commit comments