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

org.jetbrains.jet.lang.resolve.TopDownAnalysisContext Maven / Gradle / Ivy

/*
 * Copyright 2010-2013 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;

import com.google.common.base.Function;
import com.google.common.base.Functions;
import com.google.common.collect.Maps;
import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile;
import kotlin.Function1;
import kotlin.KotlinPackage;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.impl.MutableClassDescriptor;
import org.jetbrains.jet.lang.descriptors.impl.MutablePackageFragmentDescriptor;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.storage.ExceptionTracker;
import org.jetbrains.jet.storage.StorageManager;

import java.io.PrintStream;
import java.util.*;

public class TopDownAnalysisContext implements BodiesResolveContext {

    private DataFlowInfo outerDataFlowInfo = DataFlowInfo.EMPTY;

    private final Map classes = Maps.newLinkedHashMap();
    private final Map anonymousInitializers = Maps.newLinkedHashMap();
    protected final Map packageFragments = Maps.newHashMap();
    protected final Set files = new LinkedHashSet();
    private List classesTopologicalOrder = null;

    private final Map declaringScopes = Maps.newHashMap();
    private final Map functions = Maps.newLinkedHashMap();
    private final Map properties = Maps.newLinkedHashMap();
    private final Map primaryConstructorParameterProperties = Maps.newHashMap();
    private Map members = null;

    // File scopes - package scope extended with imports
    protected final Map fileScopes = Maps.newHashMap();

    public final Map forDeferredResolver = Maps.newHashMap();

    public final Map normalScope = Maps.newHashMap();

    private final Map scripts = Maps.newLinkedHashMap();

    private final TopDownAnalysisParameters topDownAnalysisParameters;

    private StringBuilder debugOutput;

    public TopDownAnalysisContext(@NotNull TopDownAnalysisParameters topDownAnalysisParameters) {
        this.topDownAnalysisParameters = topDownAnalysisParameters;
    }

    @Override
    @NotNull
    public TopDownAnalysisParameters getTopDownAnalysisParameters() {
        return topDownAnalysisParameters;
    }

    public void debug(Object message) {
        if (debugOutput != null) {
            debugOutput.append(message).append("\n");
        }
    }

    @SuppressWarnings("UnusedDeclaration")
    /*package*/ void enableDebugOutput() {
        if (debugOutput == null) {
            debugOutput = new StringBuilder();
        }
    }
    
    /*package*/ void printDebugOutput(PrintStream out) {
        if (debugOutput != null) {
            out.print(debugOutput);
        }
    }

    @Override
    public boolean completeAnalysisNeeded(@NotNull PsiElement element) {
        PsiFile containingFile = element.getContainingFile();
        boolean result = containingFile != null && topDownAnalysisParameters.getAnalyzeCompletely().apply(containingFile);
        if (!result) {
            debug(containingFile);
        }
        return result;
    }

    @Override
    public Map getDeclaredClasses() {
        return classes;
    }

    @Override
    public Map getAnonymousInitializers() {
        return anonymousInitializers;
    }

    public Map getFileScopes() {
        return fileScopes;
    }

    public Map getPackageFragments() {
        return packageFragments;
    }

    @NotNull
    @Override
    public StorageManager getStorageManager() {
        return topDownAnalysisParameters.getStorageManager();
    }

    @NotNull
    @Override
    public ExceptionTracker getExceptionTracker() {
        return topDownAnalysisParameters.getExceptionTracker();
    }

    @Override
    public Collection getFiles() {
        return files;
    }

    public void addFile(@NotNull JetFile file) {
        files.add(file);
    }

    @Override
    @NotNull
    public Map getScripts() {
        return scripts;
    }

    public Map getPrimaryConstructorParameterProperties() {
        return primaryConstructorParameterProperties;
    }

    @Override
    public Map getProperties() {
        return properties;
    }

    @Override
    public Function getDeclaringScopes() {
        return Functions.forMap(declaringScopes);
    }

    public void registerDeclaringScope(@NotNull JetDeclaration declaration, @NotNull JetScope scope) {
        declaringScopes.put(declaration, scope);
    }

    @Override
    public Map getFunctions() {
        return functions;
    }

    public Map getMembers() {
        if (members == null) {
            members = Maps.newHashMap();
            members.putAll(functions);
            members.putAll(properties);
            members.putAll(primaryConstructorParameterProperties);
        }
        return members;
    }

    @NotNull
    public List getClassesTopologicalOrder() {
        return classesTopologicalOrder;
    }

    public void setClassesTopologicalOrder(@NotNull List classesTopologicalOrder) {
        this.classesTopologicalOrder = classesTopologicalOrder;
    }

    @Override
    @NotNull
    public DataFlowInfo getOuterDataFlowInfo() {
        return outerDataFlowInfo;
    }

    public void setOuterDataFlowInfo(@NotNull DataFlowInfo outerDataFlowInfo) {
        this.outerDataFlowInfo = outerDataFlowInfo;
    }

    @NotNull
    public Collection getAllClasses() {
        // SCRIPT: all classes are declared classes + script classes
        Collection scriptClasses = KotlinPackage.map(
                getScripts().values(),
                new Function1() {
                    @Override
                    public ClassDescriptorWithResolutionScopes invoke(ScriptDescriptor scriptDescriptor) {
                        return (ClassDescriptorWithResolutionScopes) scriptDescriptor.getClassDescriptor();
                    }
                }
        );
        return KotlinPackage.plus(getDeclaredClasses().values(), scriptClasses);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy