All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jetbrains.jet.lang.resolve.TypeBinding.kt Maven / Gradle / Ivy

/*
 * Copyright 2010-2014 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.jet.lang.resolve.typeBinding

import org.jetbrains.jet.lang.types.JetType
import org.jetbrains.jet.lang.descriptors.TypeParameterDescriptor
import com.intellij.psi.PsiElement
import org.jetbrains.jet.lang.psi.JetTypeElement
import org.jetbrains.jet.lang.psi.JetCallableDeclaration
import org.jetbrains.jet.lang.resolve.BindingTrace
import org.jetbrains.jet.lang.resolve.BindingContext
import org.jetbrains.jet.lang.psi.JetTypeReference
import java.util.AbstractList
import org.jetbrains.jet.lang.types.TypeProjection
import org.jetbrains.jet.lang.descriptors.CallableDescriptor
import org.jetbrains.jet.lang.types.TypeProjectionImpl


trait TypeBinding {
    val psiElement: P
    val jetType: JetType
    fun getArgumentBindings(): List?>
}

trait TypeArgumentBinding {
    val typeProjection: TypeProjection
    val typeParameterDescriptor: TypeParameterDescriptor?
    val typeBinding: TypeBinding

} fun JetTypeReference.createTypeBinding(trace: BindingContext): TypeBinding? { val jetType = trace[BindingContext.TYPE, this] val psiElement = getTypeElement() if (jetType == null || psiElement == null) { return null } else { return ExplicitTypeBinding(trace, psiElement, jetType) } } fun JetCallableDeclaration.createTypeBindingForReturnType(trace: BindingContext): TypeBinding? { val jetTypeReference = getTypeReference() if (jetTypeReference != null) return jetTypeReference.createTypeBinding(trace) val descriptor = trace[BindingContext.DECLARATION_TO_DESCRIPTOR, this] if (descriptor !is CallableDescriptor) return null return descriptor.getReturnType()?.let { NoTypeElementBinding(trace, this, it) } } private class TypeArgumentBindingImpl( override val typeProjection: TypeProjection, override val typeParameterDescriptor: TypeParameterDescriptor?, override val typeBinding: TypeBinding

) : TypeArgumentBinding

private class ExplicitTypeBinding( private val trace: BindingContext, override val psiElement: JetTypeElement, override val jetType: JetType ) : TypeBinding { override fun getArgumentBindings(): List?> { val psiTypeArguments = psiElement.getTypeArgumentsAsTypes() val isErrorBinding = run { val sizeIsEqual = psiTypeArguments.size == jetType.getArguments().size && psiTypeArguments.size == jetType.getConstructor().getParameters().size jetType.isError() || !sizeIsEqual } return psiTypeArguments.indices.map { (index: Int): TypeArgumentBinding? -> // todo fix for List<*> val jetTypeReference = psiTypeArguments[index] val jetTypeElement = jetTypeReference?.getTypeElement() if (jetTypeElement == null) return@map null; if (isErrorBinding) { val nextJetType = trace[BindingContext.TYPE, jetTypeReference] if (nextJetType == null) return@map null; return@map TypeArgumentBindingImpl( TypeProjectionImpl(nextJetType), null, ExplicitTypeBinding(trace, jetTypeElement, nextJetType) ) } val typeProjection = jetType.getArguments()[index] return@map TypeArgumentBindingImpl( typeProjection, jetType.getConstructor().getParameters()[index], ExplicitTypeBinding(trace, jetTypeElement, typeProjection.getType()) ) } } } private class NoTypeElementBinding( private val trace: BindingContext, override val psiElement: P, override val jetType: JetType ): TypeBinding

{ override fun getArgumentBindings(): List?> { val isErrorBinding = jetType.isError() || jetType.getConstructor().getParameters().size != jetType.getArguments().size return jetType.getArguments().indices.map { val typeProjection = jetType.getArguments()[it] TypeArgumentBindingImpl( typeProjection, if (isErrorBinding) null else jetType.getConstructor().getParameters()[it], NoTypeElementBinding(trace, psiElement, typeProjection.getType()) ) } } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy