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                CallableMemberDescriptor copy =
147                        inheritedMember.copy(inheritedMember.getContainingDeclaration(), traitMember.getModality(), Visibilities.PUBLIC,
148                                             CallableMemberDescriptor.Kind.DECLARATION, true);
149    
150                if (traitMember instanceof SimpleFunctionDescriptor) {
151                    result.put((FunctionDescriptor) traitMember, (FunctionDescriptor) copy);
152                }
153                else if (traitMember instanceof PropertyDescriptor) {
154                    for (PropertyAccessorDescriptor traitAccessor : ((PropertyDescriptor) traitMember).getAccessors()) {
155                        for (PropertyAccessorDescriptor inheritedAccessor : ((PropertyDescriptor) copy).getAccessors()) {
156                            if (inheritedAccessor.getClass() == traitAccessor.getClass()) { // same accessor kind
157                                result.put(traitAccessor, inheritedAccessor);
158                            }
159                        }
160                    }
161                }
162            }
163            return result;
164        }
165    
166        @NotNull
167        public static ClassDescriptor getSuperClassByDelegationSpecifier(@NotNull JetDelegationSpecifier specifier, @NotNull BindingContext bindingContext) {
168            JetType superType = bindingContext.get(BindingContext.TYPE, specifier.getTypeReference());
169            assert superType != null : "superType should not be null: " + specifier.getText();
170    
171            ClassDescriptor superClassDescriptor = (ClassDescriptor) superType.getConstructor().getDeclarationDescriptor();
172            assert superClassDescriptor != null : "superClassDescriptor should not be null: " + specifier.getText();
173            return superClassDescriptor;
174        }
175    
176        private static boolean valueParameterClassesMatch(
177                @NotNull List<ValueParameterDescriptor> parameters,
178                @NotNull List<ClassifierDescriptor> classifiers
179        ) {
180            if (parameters.size() != classifiers.size()) return false;
181            for (int i = 0; i < parameters.size(); i++) {
182                ValueParameterDescriptor parameterDescriptor = parameters.get(i);
183                ClassifierDescriptor classDescriptor = classifiers.get(i);
184                if (!rawTypeMatches(parameterDescriptor.getType(), classDescriptor)) {
185                    return false;
186                }
187            }
188            return true;
189        }
190    
191        private static boolean rawTypeMatches(JetType type, ClassifierDescriptor classifier) {
192            return type.getConstructor().equals(classifier.getTypeConstructor());
193        }
194    
195        public static boolean isEnumValueOfMethod(@NotNull FunctionDescriptor functionDescriptor) {
196            List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
197            JetType nullableString = TypeUtils.makeNullable(KotlinBuiltIns.getInstance().getStringType());
198            return DescriptorUtils.ENUM_VALUE_OF.equals(functionDescriptor.getName())
199                   && methodTypeParameters.size() == 1
200                   && JetTypeChecker.DEFAULT.isSubtypeOf(methodTypeParameters.get(0).getType(), nullableString);
201        }
202    
203        public static boolean isEnumValuesMethod(@NotNull FunctionDescriptor functionDescriptor) {
204            List<ValueParameterDescriptor> methodTypeParameters = functionDescriptor.getValueParameters();
205            return DescriptorUtils.ENUM_VALUES.equals(functionDescriptor.getName())
206                   && methodTypeParameters.isEmpty();
207        }
208    
209        @Nullable
210        public static Integer getLineNumberForElement(@NotNull PsiElement statement, boolean markEndOffset) {
211            Document document = statement.getContainingFile().getViewProvider().getDocument();
212            TextRange textRange = statement.getTextRange();
213            return document != null ? document.getLineNumber(markEndOffset ? textRange.getEndOffset() : textRange.getStartOffset()) + 1 : null;
214        }
215    }