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 036 public static Pseudocode generatePseudocode(@NotNull JetDeclaration declaration, @NotNull final BindingContext bindingContext) { 037 BindingTrace mockTrace = new BindingTrace() { 038 @Override 039 public BindingContext getBindingContext() { 040 return bindingContext; 041 } 042 043 @Override 044 public <K, V> void record(WritableSlice<K, V> slice, K key, V value) { 045 } 046 047 @Override 048 public <K> void record(WritableSlice<K, Boolean> slice, K key) { 049 } 050 051 @Override 052 public <K, V> V get(ReadOnlySlice<K, V> slice, K key) { 053 return bindingContext.get(slice, key); 054 } 055 056 @NotNull 057 @Override 058 public <K, V> Collection<K> getKeys(WritableSlice<K, V> slice) { 059 return bindingContext.getKeys(slice); 060 } 061 062 @Override 063 public void report(@NotNull Diagnostic diagnostic) { 064 } 065 }; 066 return new JetControlFlowProcessor(mockTrace).generatePseudocode(declaration); 067 } 068 069 @Nullable 070 public static VariableDescriptor extractVariableDescriptorIfAny(@NotNull Instruction instruction, boolean onlyReference, @NotNull BindingContext bindingContext) { 071 JetElement element = null; 072 if (instruction instanceof ReadValueInstruction) { 073 element = ((ReadValueInstruction) instruction).getElement(); 074 } 075 else if (instruction instanceof WriteValueInstruction) { 076 element = ((WriteValueInstruction) instruction).getlValue(); 077 } 078 else if (instruction instanceof VariableDeclarationInstruction) { 079 element = ((VariableDeclarationInstruction) instruction).getVariableDeclarationElement(); 080 } 081 return BindingContextUtils.extractVariableDescriptorIfAny(bindingContext, element, onlyReference); 082 } 083 }