001    /*
002     * Copyright 2010-2015 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.kotlin.codegen.binding;
018    
019    import com.intellij.psi.PsiElement;
020    import com.intellij.psi.util.PsiTreeUtil;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.codegen.AsmUtil;
024    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
025    import org.jetbrains.kotlin.load.java.JvmAbi;
026    import org.jetbrains.kotlin.load.kotlin.PackagePartClassUtils;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.psi.*;
029    import org.jetbrains.org.objectweb.asm.Type;
030    
031    import static org.jetbrains.kotlin.resolve.DescriptorToSourceUtils.descriptorToDeclaration;
032    
033    public final class PsiCodegenPredictor {
034        private PsiCodegenPredictor() {
035        }
036    
037        public static boolean checkPredictedNameFromPsi(@NotNull DeclarationDescriptor descriptor, @Nullable Type nameFromDescriptors) {
038            PsiElement element = descriptorToDeclaration(descriptor);
039            if (element instanceof JetDeclaration) {
040                String classNameFromPsi = getPredefinedJvmInternalName((JetDeclaration) element);
041                assert classNameFromPsi == null || Type.getObjectType(classNameFromPsi).equals(nameFromDescriptors) :
042                        String.format("Invalid algorithm for getting qualified name from psi! Predicted: %s, actual %s\n" +
043                                      "Element: %s", classNameFromPsi, nameFromDescriptors, element.getText());
044            }
045    
046            return true;
047        }
048    
049        /**
050         * @return null if no prediction can be done.
051         */
052        @Nullable
053        public static String getPredefinedJvmInternalName(@NotNull JetDeclaration declaration) {
054            // TODO: Method won't work for declarations inside class objects
055            // TODO: Method won't give correct class name for traits implementations
056    
057            JetDeclaration parentDeclaration = JetStubbedPsiUtil.getContainingDeclaration(declaration);
058            if (parentDeclaration instanceof JetClassObject) {
059                assert declaration instanceof JetObjectDeclaration : "Only object declarations can be children of JetClassObject: " + declaration;
060                return getPredefinedJvmInternalName(parentDeclaration);
061            }
062    
063            String parentInternalName;
064            if (parentDeclaration != null) {
065                parentInternalName = getPredefinedJvmInternalName(parentDeclaration);
066                if (parentInternalName == null) {
067                    return null;
068                }
069            }
070            else {
071                JetFile containingFile = declaration.getContainingJetFile();
072    
073                if (declaration instanceof JetNamedFunction) {
074                    Name name = ((JetNamedFunction) declaration).getNameAsName();
075                    return name == null ? null : PackagePartClassUtils.getPackagePartInternalName(containingFile) + "$" + name.asString();
076                }
077    
078                parentInternalName = AsmUtil.internalNameByFqNameWithoutInnerClasses(containingFile.getPackageFqName());
079            }
080    
081            if (declaration instanceof JetClassObject) {
082                // Get parent and assign Class object prefix
083                return parentInternalName + JvmAbi.CLASS_OBJECT_SUFFIX;
084            }
085    
086            if (!PsiTreeUtil.instanceOf(declaration, JetClass.class, JetObjectDeclaration.class, JetNamedFunction.class, JetProperty.class) ||
087                    declaration instanceof JetEnumEntry) {
088                // Other subclasses are not valid for class name prediction.
089                // For example EnumEntry, JetFunctionLiteral
090                return null;
091            }
092    
093            Name name = ((JetNamedDeclaration) declaration).getNameAsName();
094            if (name == null) {
095                return null;
096            }
097    
098            if (declaration instanceof JetNamedFunction) {
099                if (!(parentDeclaration instanceof JetClass || parentDeclaration instanceof JetObjectDeclaration)) {
100                    // Can't generate predefined name for internal functions
101                    return null;
102                }
103            }
104    
105            // NOTE: looks like a bug - for class in getter of top level property class name will be $propertyName$ClassName but not
106            // PackageClassName$propertyName$ClassName
107            if (declaration instanceof JetProperty) {
108                return parentInternalName + "$" + name.asString();
109            }
110    
111            if (parentInternalName.isEmpty()) {
112                return name.asString();
113            }
114    
115            return parentInternalName + (parentDeclaration == null ? "/" : "$") + name.asString();
116        }
117    }