@@ -145,20 +145,22 @@ open class KotlinUsesExtractor(
145145 } ? : argsIncludingOuterClasses
146146 }
147147
148+ fun isStaticClass (c : IrClass ) = c.visibility != DescriptorVisibilities .LOCAL && ! c.isInner
149+
148150 // Gets nested inner classes starting at `c` and proceeding outwards to the innermost enclosing static class.
149151 // For example, for (java syntax) `class A { static class B { class C { class D { } } } }`,
150152 // `nonStaticParentsWithSelf(D)` = `[D, C, B]`.
151153 fun parentsWithTypeParametersInScope (c : IrClass ): List <IrDeclarationParent > {
152154 val parentsList = c.parentsWithSelf.toList()
153- val firstOuterClassIdx = parentsList.indexOfFirst { it is IrClass && ! it.isInner }
155+ val firstOuterClassIdx = parentsList.indexOfFirst { it is IrClass && isStaticClass(it) }
154156 return if (firstOuterClassIdx == - 1 ) parentsList else parentsList.subList(0 , firstOuterClassIdx + 1 )
155157 }
156158
157159 // Gets the type parameter symbols that are in scope for class `c` in Kotlin order (i.e. for
158160 // `class NotInScope<T> { static class OutermostInScope<A, B> { class QueryClass<C, D> { } } }`,
159161 // `getTypeParametersInScope(QueryClass)` = `[C, D, A, B]`.
160162 fun getTypeParametersInScope (c : IrClass ) =
161- parentsWithTypeParametersInScope(c).mapNotNull({ (it as ? IrClass )?.typeParameters }).flatten()
163+ parentsWithTypeParametersInScope(c).mapNotNull({ getTypeParameters (it) }).flatten()
162164
163165 // Returns a map from `c`'s type variables in scope to type arguments `argsIncludingOuterClasses`.
164166 // Hack for the time being: the substituted types are always nullable, to prevent downstream code
@@ -167,18 +169,17 @@ open class KotlinUsesExtractor(
167169 fun makeTypeGenericSubstitutionMap (c : IrClass , argsIncludingOuterClasses : List <IrTypeArgument >) =
168170 getTypeParametersInScope(c).map({ it.symbol }).zip(argsIncludingOuterClasses.map { it.withQuestionMark(true ) }).toMap()
169171
170- // The Kotlin compiler internal representation of Outer<A, B>.Inner<C, D>.InnerInner<E, F> is InnerInner< E, F, C, D, A, B>. This function returns [A, B, C, D, E, F].
172+ // The Kotlin compiler internal representation of Outer<A, B>.Inner<C, D>.InnerInner<E, F>.someFunction<G, H>.LocalClass<I, J> is LocalClass<I, J, G, H, E, F, C, D, A, B>. This function returns [A, B, C, D, E, F, G, H, I, J ].
171173 fun orderTypeArgsLeftToRight (c : IrClass , argsIncludingOuterClasses : List <IrTypeArgument >? ): List <IrTypeArgument >? {
172174 if (argsIncludingOuterClasses.isNullOrEmpty())
173175 return argsIncludingOuterClasses
174176 val ret = ArrayList <IrTypeArgument >()
175177 // Iterate over nested inner classes starting at `c`'s surrounding top-level or static nested class and ending at `c`, from the outermost inwards:
176178 val truncatedParents = parentsWithTypeParametersInScope(c)
177179 for (parent in truncatedParents.reversed()) {
178- if (parent is IrClass ) {
179- val firstArgIdx = argsIncludingOuterClasses.size - (ret.size + parent.typeParameters.size)
180- ret.addAll(argsIncludingOuterClasses.subList(firstArgIdx, firstArgIdx + parent.typeParameters.size))
181- }
180+ val parentTypeParameters = getTypeParameters(parent)
181+ val firstArgIdx = argsIncludingOuterClasses.size - (ret.size + parentTypeParameters.size)
182+ ret.addAll(argsIncludingOuterClasses.subList(firstArgIdx, firstArgIdx + parentTypeParameters.size))
182183 }
183184 return ret
184185 }
@@ -612,6 +613,13 @@ class X {
612613 return if (f is IrConstructor ) f.typeParameters else f.typeParameters.filter { it.parent == f }
613614 }
614615
616+ fun getTypeParameters (dp : IrDeclarationParent ): List <IrTypeParameter > =
617+ when (dp) {
618+ is IrClass -> dp.typeParameters
619+ is IrFunction -> getFunctionTypeParameters(dp)
620+ else -> listOf ()
621+ }
622+
615623 fun getFunctionLabel (f : IrFunction , classTypeArguments : List <IrTypeArgument >? = null) : String {
616624 return getFunctionLabel(f.parent, getFunctionShortName(f), f.valueParameters, f.returnType, f.extensionReceiverParameter, getFunctionTypeParameters(f), classTypeArguments)
617625 }
0 commit comments