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

org.jetbrains.kotlin.resolve.calls.tasks.ResolutionTaskHolder.kt Maven / Gradle / Ivy

/*
 * Copyright 2010-2015 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.kotlin.resolve.calls.tasks

import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.psi.JetPsiUtil
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.storage.StorageManager
import java.util.ArrayList
import org.jetbrains.kotlin.utils.toReadOnlyList

public class ResolutionTaskHolder(
        private val storageManager: StorageManager,
        private val basicCallResolutionContext: BasicCallResolutionContext,
        private val priorityProvider: ResolutionTaskHolder.PriorityProvider>,
        private val tracing: TracingStrategy
) {
    private val candidatesList = ArrayList<() -> Collection>>()
    private var internalTasks: List>? = null

    public fun addCandidates(lazyCandidates: () -> Collection>) {
        assertNotFinished()
        candidatesList.add(storageManager.createLazyValue { lazyCandidates().toReadOnlyList() })
    }

    private fun assertNotFinished() {
        assert(internalTasks == null, "Can't add candidates after the resulting tasks were computed.")
    }

    public fun getTasks(): List> {
        if (internalTasks == null) {
            val tasks = ArrayList>()
            for (priority in (0..priorityProvider.getMaxPriority()).reversed()) {
                for (candidateIndex in candidatesList.indices) {
                    val lazyCandidates = {
                        candidatesList[candidateIndex]().filter { priorityProvider.getPriority(it) == priority }.toReadOnlyList()
                    }
                    tasks.add(ResolutionTask(basicCallResolutionContext, tracing, lazyCandidates))
                }
            }

            internalTasks = tasks
        }
        return internalTasks!!
    }

    public interface PriorityProvider {
        public fun getPriority(candidate: D): Int

        public fun getMaxPriority(): Int
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy