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.openapi.util.TextRange;
021    import com.intellij.psi.PsiElement;
022    import com.intellij.psi.PsiFile;
023    import org.jetbrains.annotations.NotNull;
024    import org.jetbrains.annotations.Nullable;
025    import org.jetbrains.kotlin.backend.common.bridges.BridgesPackage;
026    import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
027    import org.jetbrains.kotlin.descriptors.*;
028    import org.jetbrains.kotlin.name.Name;
029    import org.jetbrains.kotlin.psi.*;
030    import org.jetbrains.kotlin.resolve.BindingContext;
031    import org.jetbrains.kotlin.resolve.DescriptorUtils;
032    import org.jetbrains.kotlin.resolve.calls.CallResolverUtil;
033    import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilPackage;
034    import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
035    import org.jetbrains.kotlin.types.JetType;
036    import org.jetbrains.kotlin.types.TypeUtils;
037    import org.jetbrains.kotlin.types.checker.JetTypeChecker;
038    
039    import java.util.*;
040    
041    import static org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage.getBuiltIns;
042    
043    /**
044     * Backend-independent utility class.
045     */
046    public class CodegenUtil {
047    
048        private CodegenUtil() {
049        }
050    
051        // TODO: consider putting predefined method signatures here too.
052        public static final String EQUALS_METHOD_NAME = "equals";
053        public static final String TO_STRING_METHOD_NAME = "toString";
054        public static final String HASH_CODE_METHOD_NAME = "hashCode";
055    
056        @Nullable
057        public static FunctionDescriptor getDeclaredFunctionByRawSignature(
058                @NotNull ClassDescriptor owner,
059                @NotNull Name name,
060                @NotNull ClassifierDescriptor returnedClassifier,
061                @NotNull ClassifierDescriptor... valueParameterClassifiers
062        ) {
063            Collection<FunctionDescriptor> functions = owner.getDefaultType().getMemberScope().getFunctions(name);
064            for (FunctionDescriptor function : functions) {
065                if (!CallResolverUtil.isOrOverridesSynthesized(function)
066                    && function.getTypeParameters().isEmpty()
067                    && valueParameterClassesMatch(function.getValueParameters(), Arrays.asList(valueParameterClassifiers))
068                    && rawTypeMatches(function.getReturnType(), returnedClassifier)) {
069                    return function;
070                }
071            }
072            return null;
073        }
074    
075        public static FunctionDescriptor getAnyEqualsMethod(@NotNull KotlinBuiltIns builtIns) {
076            ClassDescriptor anyClass = builtIns.getAny();
077            FunctionDescriptor function = getDeclaredFunctionByRawSignature(
078                    anyClass, Name.identifier(EQUALS_METHOD_NAME), builtIns.getBoolean(), anyClass
079            );
080            assert function != null;
081            return function;
082        }
083    
084        public static FunctionDescriptor getAnyToStringMethod(@NotNull KotlinBuiltIns builtIns) {
085            ClassDescriptor anyClass = builtIns.getAny();
086            FunctionDescriptor function = getDeclaredFunctionByRawSignature(anyClass, Name.identifier(TO_STRING_METHOD_NAME), builtIns.getString());
087            assert function != null;
088            return function;
089        }
090    
091        public static FunctionDescriptor getAnyHashCodeMethod(@NotNull KotlinBuiltIns builtIns) {
092            ClassDescriptor anyClass = builtIns.getAny();
093            FunctionDescriptor function = getDeclaredFunctionByRawSignature(anyClass, Name.identifier(HASH_CODE_METHOD_NAME), builtIns.getInt());
094            assert function != null;
095            return function;
096        }
097    
098        @Nullable
099        public static PropertyDescriptor getDelegatePropertyIfAny(JetExpression expression, ClassDescriptor classDescriptor, BindingContext bindingContext) {
100            PropertyDescriptor propertyDescriptor = null;
101            if (expression instanceof JetSimpleNameExpression) {
102                ResolvedCall<?> call = CallUtilPackage.getResolvedCall(expression, bindingContext);
103                if (call != null) {
104                    CallableDescriptor callResultingDescriptor = call.getResultingDescriptor();
105                    if (callResultingDescriptor instanceof ValueParameterDescriptor) {
106                        ValueParameterDescriptor valueParameterDescriptor = (ValueParameterDescriptor) callResultingDescriptor;
107                        // constructor parameter
108                        if (valueParameterDescriptor.getContainingDeclaration() instanceof ConstructorDescriptor) {
109                            // constructor of my class
110                            if (valueParameterDescriptor.getContainingDeclaration().getContainingDeclaration() == classDescriptor) {
111                                propertyDescriptor = bindingContext.get(BindingContext.VALUE_PARAMETER_AS_PROPERTY, valueParameterDescriptor);
112                            }
113                        }
114                    }
115    
116                    // todo: when and if frontend will allow properties defined not as constructor parameters to be used in delegation specifier
117                }
118            }
119            return propertyDescriptor;
120        }
121    
122        public static boolean isFinalPropertyWithBackingField(PropertyDescriptor propertyDescriptor, BindingContext bindingContext) {
123            return propertyDescriptor != null &&
124                   !propertyDescriptor.isVar() &&
125                   Boolean.TRUE.equals(bindingContext.get(BindingContext.BACKING_FIELD_REQUIRED, propertyDescriptor));
126        }
127    
128        @NotNull
129        public static Map<FunctionDescriptor, FunctionDescriptor> getTraitMethods(ClassDescriptor descriptor) {
130            Map<FunctionDescriptor, FunctionDescriptor> result = new LinkedHashMap<FunctionDescriptor, FunctionDescriptor>();
131            for (DeclarationDescriptor declaration : descriptor.getDefaultType().getMemberScope().getAllDescriptors()) {
132                if (!(declaration instanceof CallableMemberDescriptor)) continue;
133    
134                CallableMemberDescriptor inheritedMember = (CallableMemberDescriptor) declaration;
135                CallableMemberDescriptor traitMember = BridgesPackage.findTraitImplementation(inheritedMember);
136                if (traitMember == null) continue;
137    
138                assert traitMember.getModality() != Modality.ABSTRACT : "Cannot delegate to abstract trait method: " + inheritedMember;
139    
140                // inheritedMember can be abstract here. In order for FunctionCodegen to generate the method body, we're creating a copy here
141                // with traitMember's modality
142                result.putAll(copyFunctions(inheritedMember, traitMember, inheritedMember.getContainingDeclaration(), traitMember.getModality(), Visibilities.PUBLIC,
143                                                         CallableMemberDescriptor.Kind.DECLARATION, true));
144            }
145            return result;
146        }
147    
148        @NotNull
149        public static Map<FunctionDescriptor, FunctionDescriptor> copyFunctions(
150                @NotNull CallableMemberDescriptor inheritedMember,
151                @NotNull CallableMemberDescriptor traitMember,
152                DeclarationDescriptor newOwner,
153                Modality modality,
154                Visibility visibility,
155                CallableMemberDescriptor.Kind kind,
156                boolean copyOverrides
157        ) {
158            CallableMemberDescriptor copy = inheritedMember.copy(newOwner, modality, visibility, kind, copyOverrides);
159            Map<FunctionDescriptor, FunctionDescriptor> result = new LinkedHashMap<FunctionDescriptor, FunctionDescriptor>(0);
160            if (traitMember instanceof SimpleFunctionDescriptor) {
161                result.put((FunctionDescriptor) traitMember, (FunctionDescriptor) copy);
162            }
163            else if (traitMember instanceof PropertyDescriptor) {
164                for (PropertyAccessorDescriptor traitAccessor : ((PropertyDescriptor) traitMember).getAccessors()) {
165                    for (PropertyAccessorDescriptor inheritedAccessor : ((PropertyDescriptor) copy).getAccessors()) {
166                        if (inheritedAccessor.getClass() == traitAccessor.getClass()) { // same accessor kind
167                            result.put(traitAccessor, inheritedAccessor);
168                        }
169                    }
170                }
171            }
172            return result;
173        }
174    
175        @NotNull
176        public static ClassDescriptor getSuperClassByDelegationSpecifier(@NotNull JetDelegationSpecifier specifier, @NotNull BindingContext bindingContext) {
177            JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
178            assert superType != null : "superType should not be null: " + specifier.getText();
179    
180            ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
181            assert superClassDescriptor != null : "superClassDescriptor should not be null: " + specifier.getText();
182            return superClassDescriptor;
183        }
184    
185        private static boolean valueParameterClassesMatch(
186                @NotNull List<ValueParameterDescriptor> parameters,
187                @NotNull List<ClassifierDescriptor> classifiers
188        ) {
189            if (parameters.size() != classifiers.size()) return false;
190            for (int i = 0; i < parameters.size(); i++) {
191                ValueParameterDescriptor parameterDescriptor = parameters.get(i);
192                ClassifierDescriptor classDescriptor = classifiers.get(i);
193                if (!rawTypeMatches(parameterDescriptor.getType(), classDescriptor)) {
194                    return false;
195                }
196            }
197            return true;
198        }
199    
200        private static boolean rawTypeMatches(JetType type, ClassifierDescriptor classifier) {
201            return type.getConstructor().equals(classifier.getTypeConstructor());
202        }
203    
204        public static boolean isEnumValueOfMethod(@NotNull FunctionDescriptor functionDescriptor) {
205            List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
206            JetType nullableString = TypeUtils.makeNullable(getBuiltIns(functionDescriptor).getStringType());
207            return DescriptorUtils.ENUM_VALUE_OF.equals(functionDescriptor.getName())
208                   && methodTypeParameters.size() == 1
209                   && JetTypeChecker.DEFAULT.isSubtypeOf(methodTypeParameters.get(0).getType(), nullableString);
210        }
211    
212        public static boolean isEnumValuesMethod(@NotNull FunctionDescriptor functionDescriptor) {
213            List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
214            return DescriptorUtils.ENUM_VALUES.equals(functionDescriptor.getName())
215                   && methodTypeParameters.isEmpty();
216        }
217    
218        @Nullable
219        public static Integer getLineNumberForElement(@NotNull PsiElement statement, boolean markEndOffset) {
220            PsiFile file = statement.getContainingFile();
221            if (file instanceof JetFile) {
222                if (PsiPackage.getDoNotAnalyze((JetFile) file) != null) {
223                    return null;
224                }
225            }
226            Document document = file.getViewProvider().getDocument();
227            return document != null ? document.getLineNumber(markEndOffset ? statement.getTextRange().getEndOffset() : statement.getTextOffset()) + 1 : null;
228        }
229    }