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

org.jetbrains.kotlin.resolve.TopDownAnalysisContext Maven / Gradle / Ivy

There is a newer version: 2.0.0
Show newest version
/*
 * Copyright 2010-2017 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;

import com.google.common.collect.Maps;
import kotlin.collections.CollectionsKt;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.kotlin.descriptors.*;
import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowInfo;
import org.jetbrains.kotlin.resolve.lazy.DeclarationScopeProvider;
import org.jetbrains.kotlin.resolve.scopes.LexicalScope;

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

public class TopDownAnalysisContext implements BodiesResolveContext {

    private final DataFlowInfo outerDataFlowInfo;

    private final Map classes = Maps.newLinkedHashMap();
    private final Map anonymousInitializers = Maps.newLinkedHashMap();
    private final Set files = new LinkedHashSet<>();
    private final Map secondaryConstructors = Maps.newLinkedHashMap();

    private final Map functions = Maps.newLinkedHashMap();
    private final Map properties = Maps.newLinkedHashMap();
    private final Map primaryConstructorParameterProperties = new HashMap<>();
    private final Map typeAliases = Maps.newLinkedHashMap();
    private final Map destructuringDeclarationEntries = Maps.newLinkedHashMap();
    private Map members = null;

    private final Map scripts = Maps.newLinkedHashMap();

    private final TopDownAnalysisMode topDownAnalysisMode;
    private final DeclarationScopeProvider declarationScopeProvider;

    private StringBuilder debugOutput;

    public TopDownAnalysisContext(
            @NotNull TopDownAnalysisMode topDownAnalysisMode,
            @NotNull DataFlowInfo outerDataFlowInfo,
            @NotNull DeclarationScopeProvider declarationScopeProvider
    ) {
        this.topDownAnalysisMode = topDownAnalysisMode;
        this.outerDataFlowInfo = outerDataFlowInfo;
        this.declarationScopeProvider = declarationScopeProvider;
    }

    @Override
    @NotNull
    public TopDownAnalysisMode getTopDownAnalysisMode() {
        return topDownAnalysisMode;
    }

    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 Map getDeclaredClasses() {
        return classes;
    }

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

    @Override
    public Map getSecondaryConstructors() {
        return secondaryConstructors;
    }

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

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

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

    public Map getPrimaryConstructorParameterProperties() {
        return primaryConstructorParameterProperties;
    }

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

    @Nullable
    @Override
    public LexicalScope getDeclaringScope(@NotNull KtDeclaration declaration) {
        return declarationScopeProvider.getResolutionScopeForDeclaration(declaration);
    }

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

    @Override
    public Map getTypeAliases() {
        return typeAliases;
    }

    @Override
    public Map getDestructuringDeclarationEntries() {
        return destructuringDeclarationEntries;
    }

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

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

    @NotNull
    public Collection getAllClasses() {
        return CollectionsKt.plus(getDeclaredClasses().values(), getScripts().values());
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy