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.backend.common;
018    
019    import com.intellij.openapi.editor.Document;
020    import com.intellij.psi.PsiElement;
021    import com.intellij.psi.PsiFile;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.kotlin.backend.common.bridges.ImplKt;
025    import org.jetbrains.kotlin.descriptors.*;
026    import org.jetbrains.kotlin.incremental.components.NoLookupLocation;
027    import org.jetbrains.kotlin.name.Name;
028    import org.jetbrains.kotlin.psi.*;
029    import org.jetbrains.kotlin.resolve.BindingContext;
030    import org.jetbrains.kotlin.resolve.DescriptorUtils;
031    import org.jetbrains.kotlin.resolve.calls.callResolverUtil.CallResolverUtilKt;
032    import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
033    import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
034    import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
035    import org.jetbrains.kotlin.types.KotlinType;
036    import org.jetbrains.kotlin.types.TypeUtils;
037    import org.jetbrains.kotlin.types.checker.KotlinTypeChecker;
038    
039    import java.util.*;
040    
041    public class CodegenUtil {
042    
043        private CodegenUtil() {
044        }
045    
046        @Nullable
047        public static FunctionDescriptor getDeclaredFunctionByRawSignature(
048                @NotNull ClassDescriptor owner,
049                @NotNull Name name,
050                @NotNull ClassifierDescriptor returnedClassifier,
051                @NotNull ClassifierDescriptor... valueParameterClassifiers
052        ) {
053            Collection<SimpleFunctionDescriptor> functions = owner.getDefaultType().getMemberScope().getContributedFunctions(name, NoLookupLocation.FROM_BACKEND);
054            for (FunctionDescriptor function : functions) {
055                if (!CallResolverUtilKt.isOrOverridesSynthesized(function)
056                    && function.getTypeParameters().isEmpty()
057                    && valueParameterClassesMatch(function.getValueParameters(), Arrays.asList(valueParameterClassifiers))
058                    && rawTypeMatches(function.getReturnType(), returnedClassifier)) {
059                    return function;
060                }
061            }
062            return null;
063        }
064    
065        @Nullable
066        public static PropertyDescriptor getDelegatePropertyIfAny(KtExpression expression, ClassDescriptor classDescriptor, BindingContext bindingContext) {
067            PropertyDescriptor propertyDescriptor = null;
068            if (expression instanceof KtSimpleNameExpression) {
069                ResolvedCall<?> call = CallUtilKt.getResolvedCall(expression, bindingContext);
070                if (call != null) {
071                    CallableDescriptor callResultingDescriptor = call.getResultingDescriptor();
072                    if (callResultingDescriptor instanceof ValueParameterDescriptor) {
073                        ValueParameterDescriptor valueParameterDescriptor = (ValueParameterDescriptor) callResultingDescriptor;
074                        // constructor parameter
075                        if (valueParameterDescriptor.getContainingDeclaration() instanceof ConstructorDescriptor) {
076                            // constructor of my class
077                            if (valueParameterDescriptor.getContainingDeclaration().getContainingDeclaration() == classDescriptor) {
078                                propertyDescriptor = bindingContext.get(BindingContext.VALUE_PARAMETER_AS_PROPERTY, valueParameterDescriptor);
079                            }
080                        }
081                    }
082    
083                    // todo: when and if frontend will allow properties defined not as constructor parameters to be used in delegation specifier
084                }
085            }
086            return propertyDescriptor;
087        }
088    
089        public static boolean isFinalPropertyWithBackingField(PropertyDescriptor propertyDescriptor, BindingContext bindingContext) {
090            return propertyDescriptor != null &&
091                   !propertyDescriptor.isVar() &&
092                   Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor));
093        }
094    
095        @NotNull
096        public static Map<FunctionDescriptor, FunctionDescriptor> getNonPrivateTraitMethods(ClassDescriptor descriptor) {
097            Map<FunctionDescriptor, FunctionDescriptor> result = new LinkedHashMap<FunctionDescriptor, FunctionDescriptor>();
098            for (DeclarationDescriptor declaration : DescriptorUtils.getAllDescriptors(descriptor.getDefaultType().getMemberScope())) {
099                if (!(declaration instanceof CallableMemberDescriptor)) continue;
100    
101                CallableMemberDescriptor inheritedMember = (CallableMemberDescriptor) declaration;
102                CallableMemberDescriptor traitMember = ImplKt.findTraitImplementation(inheritedMember);
103                if (traitMember == null || Visibilities.isPrivate(traitMember.getVisibility())) continue;
104    
105                assert traitMember.getModality() != Modality.ABSTRACT : "Cannot delegate to abstract trait method: " + inheritedMember;
106    
107                // inheritedMember can be abstract here. In order for FunctionCodegen to generate the method body, we're creating a copy here
108                // with traitMember's modality
109                result.putAll(copyFunctions(inheritedMember, traitMember, inheritedMember.getContainingDeclaration(), traitMember.getModality(), Visibilities.PUBLIC,
110                                                         CallableMemberDescriptor.Kind.DECLARATION, true));
111            }
112            return result;
113        }
114    
115        @NotNull
116        public static Map<FunctionDescriptor, FunctionDescriptor> copyFunctions(
117                @NotNull CallableMemberDescriptor inheritedMember,
118                @NotNull CallableMemberDescriptor traitMember,
119                DeclarationDescriptor newOwner,
120                Modality modality,
121                Visibility visibility,
122                CallableMemberDescriptor.Kind kind,
123                boolean copyOverrides
124        ) {
125            CallableMemberDescriptor copy = inheritedMember.copy(newOwner, modality, visibility, kind, copyOverrides);
126            Map<FunctionDescriptor, FunctionDescriptor> result = new LinkedHashMap<FunctionDescriptor, FunctionDescriptor>(0);
127            if (traitMember instanceof SimpleFunctionDescriptor) {
128                result.put((FunctionDescriptor) traitMember, (FunctionDescriptor) copy);
129            }
130            else if (traitMember instanceof PropertyDescriptor) {
131                for (PropertyAccessorDescriptor traitAccessor : ((PropertyDescriptor) traitMember).getAccessors()) {
132                    for (PropertyAccessorDescriptor inheritedAccessor : ((PropertyDescriptor) copy).getAccessors()) {
133                        if (inheritedAccessor.getClass() == traitAccessor.getClass()) { // same accessor kind
134                            result.put(traitAccessor, inheritedAccessor);
135                        }
136                    }
137                }
138            }
139            return result;
140        }
141    
142        @NotNull
143        public static ClassDescriptor getSuperClassBySuperTypeListEntry(@NotNull KtSuperTypeListEntry specifier, @NotNull BindingContext bindingContext) {
144            KotlinType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
145            assert superType != null : "superType should not be null: " + specifier.getText();
146    
147            ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
148            assert superClassDescriptor != null : "superClassDescriptor should not be null: " + specifier.getText();
149            return superClassDescriptor;
150        }
151    
152        private static boolean valueParameterClassesMatch(
153                @NotNull List<ValueParameterDescriptor> parameters,
154                @NotNull List<ClassifierDescriptor> classifiers
155        ) {
156            if (parameters.size() != classifiers.size()) return false;
157            for (int i = 0; i < parameters.size(); i++) {
158                ValueParameterDescriptor parameterDescriptor = parameters.get(i);
159                ClassifierDescriptor classDescriptor = classifiers.get(i);
160                if (!rawTypeMatches(parameterDescriptor.getType(), classDescriptor)) {
161                    return false;
162                }
163            }
164            return true;
165        }
166    
167        private static boolean rawTypeMatches(KotlinType type, ClassifierDescriptor classifier) {
168            return type.getConstructor().equals(classifier.getTypeConstructor());
169        }
170    
171        public static boolean isEnumValueOfMethod(@NotNull FunctionDescriptor functionDescriptor) {
172            List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
173            KotlinType nullableString = TypeUtils.makeNullable(DescriptorUtilsKt.getBuiltIns(functionDescriptor).getStringType());
174            return DescriptorUtils.ENUM_VALUE_OF.equals(functionDescriptor.getName())
175                   && methodTypeParameters.size() == 1
176                   && KotlinTypeChecker.DEFAULT.isSubtypeOf(methodTypeParameters.get(0).getType(), nullableString);
177        }
178    
179        @Nullable
180        public static Integer getLineNumberForElement(@NotNull PsiElement statement, boolean markEndOffset) {
181            PsiFile file = statement.getContainingFile();
182            if (file instanceof KtFile) {
183                if (KtPsiFactoryKt.getDoNotAnalyze((KtFile) file) != null) {
184                    return null;
185                }
186            }
187    
188            if (statement instanceof KtConstructorDelegationReferenceExpression && statement.getTextLength() == 0) {
189                // PsiElement for constructor delegation reference is always generated, so we shouldn't mark it's line number if it's empty
190                return null;
191            }
192    
193            Document document = file.getViewProvider().getDocument();
194            return document != null ? document.getLineNumber(markEndOffset ? statement.getTextRange().getEndOffset() : statement.getTextOffset()) + 1 : null;
195        }
196    }