001    /*
002     * Copyright 2010-2013 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.jet.lang.cfg.pseudocode;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.cfg.JetControlFlowProcessor;
022    import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
023    import org.jetbrains.jet.lang.diagnostics.Diagnostic;
024    import org.jetbrains.jet.lang.psi.JetDeclaration;
025    import org.jetbrains.jet.lang.psi.JetElement;
026    import org.jetbrains.jet.lang.resolve.BindingContext;
027    import org.jetbrains.jet.lang.resolve.BindingContextUtils;
028    import org.jetbrains.jet.lang.resolve.BindingTrace;
029    import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
030    import org.jetbrains.jet.util.slicedmap.WritableSlice;
031    
032    import java.util.Collection;
033    
034    public class PseudocodeUtil {
035        @NotNull
036        public static Pseudocode generatePseudocode(@NotNull JetDeclaration declaration, @NotNull final BindingContext bindingContext) {
037            BindingTrace mockTrace = new BindingTrace() {
038                @NotNull
039                @Override
040                public BindingContext getBindingContext() {
041                    return bindingContext;
042                }
043    
044                @Override
045                public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
046                }
047    
048                @Override
049                public <K> void record(WritableSlice<K, Boolean> slice, K key) {
050                }
051    
052                @Override
053                public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
054                    return bindingContext.get(slice, key);
055                }
056    
057                @NotNull
058                @Override
059                public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
060                    return bindingContext.getKeys(slice);
061                }
062    
063                @Override
064                public void report(@NotNull Diagnostic diagnostic) {
065                }
066            };
067            return new JetControlFlowProcessor(mockTrace).generatePseudocode(declaration);
068        }
069    
070        @Nullable
071        public static VariableDescriptor extractVariableDescriptorIfAny(@NotNull Instruction instruction, boolean onlyReference, @NotNull BindingContext bindingContext) {
072            JetElement element = null;
073            if (instruction instanceof ReadValueInstruction) {
074                element = ((ReadValueInstruction) instruction).getElement();
075            }
076            else if (instruction instanceof WriteValueInstruction) {
077                element = ((WriteValueInstruction) instruction).getlValue();
078            }
079            else if (instruction instanceof VariableDeclarationInstruction) {
080                element = ((VariableDeclarationInstruction) instruction).getVariableDeclarationElement();
081            }
082            return BindingContextUtils.extractVariableDescriptorIfAny(bindingContext, element, onlyReference);
083        }
084    }