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