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.cfg.pseudocode.instructions.Instruction;
023 import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.ReadValueInstruction;
024 import org.jetbrains.jet.lang.cfg.pseudocode.instructions.eval.WriteValueInstruction;
025 import org.jetbrains.jet.lang.cfg.pseudocode.instructions.special.VariableDeclarationInstruction;
026 import org.jetbrains.jet.lang.descriptors.VariableDescriptor;
027 import org.jetbrains.jet.lang.diagnostics.Diagnostic;
028 import org.jetbrains.jet.lang.psi.JetDeclaration;
029 import org.jetbrains.jet.lang.psi.JetElement;
030 import org.jetbrains.jet.lang.resolve.BindingContext;
031 import org.jetbrains.jet.lang.resolve.BindingContextUtils;
032 import org.jetbrains.jet.lang.resolve.BindingTrace;
033 import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
034 import org.jetbrains.jet.util.slicedmap.WritableSlice;
035
036 import java.util.Collection;
037
038 public class PseudocodeUtil {
039 @NotNull
040 public static Pseudocode generatePseudocode(@NotNull JetDeclaration declaration, @NotNull final BindingContext bindingContext) {
041 BindingTrace mockTrace = new BindingTrace() {
042 @NotNull
043 @Override
044 public BindingContext getBindingContext() {
045 return bindingContext;
046 }
047
048 @Override
049 public <K, V> void record(WritableSlice<K, V> slice, K key, V value) {
050 }
051
052 @Override
053 public <K> void record(WritableSlice<K, Boolean> slice, K key) {
054 }
055
056 @Override
057 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) {
058 return bindingContext.get(slice, key);
059 }
060
061 @NotNull
062 @Override
063 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) {
064 return bindingContext.getKeys(slice);
065 }
066
067 @Override
068 public void report(@NotNull Diagnostic diagnostic) {
069 }
070 };
071 return new JetControlFlowProcessor(mockTrace).generatePseudocode(declaration);
072 }
073
074 @Nullable
075 public static VariableDescriptor extractVariableDescriptorIfAny(@NotNull Instruction instruction, boolean onlyReference, @NotNull BindingContext bindingContext) {
076 JetElement element = null;
077 if (instruction instanceof ReadValueInstruction) {
078 element = ((ReadValueInstruction) instruction).getElement();
079 }
080 else if (instruction instanceof WriteValueInstruction) {
081 element = ((WriteValueInstruction) instruction).getlValue();
082 }
083 else if (instruction instanceof VariableDeclarationInstruction) {
084 element = ((VariableDeclarationInstruction) instruction).getVariableDeclarationElement();
085 }
086 return BindingContextUtils.extractVariableDescriptorIfAny(bindingContext, element, onlyReference);
087 }
088 }