001 /*
002 * Copyright 2010-2013 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.codegen;
018
019 import org.jetbrains.annotations.NotNull;
020 import org.jetbrains.jet.lang.descriptors.*;
021 import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
022 import org.jetbrains.jet.lang.descriptors.impl.SimpleFunctionDescriptorImpl;
023 import org.jetbrains.jet.lang.descriptors.impl.TypeParameterDescriptorImpl;
024 import org.jetbrains.jet.lang.resolve.DescriptorUtils;
025 import org.jetbrains.jet.lang.resolve.name.Name;
026 import org.jetbrains.jet.lang.types.JetType;
027
028 import java.util.ArrayList;
029 import java.util.List;
030
031 import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
032
033 public class AccessorForFunctionDescriptor extends SimpleFunctionDescriptorImpl {
034 public AccessorForFunctionDescriptor(
035 @NotNull FunctionDescriptor descriptor,
036 @NotNull DeclarationDescriptor containingDeclaration,
037 int index
038 ) {
039 super(containingDeclaration, null, Annotations.EMPTY,
040 Name.identifier((descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName()) + "$b$" + index),
041 Kind.DECLARATION);
042
043 initialize(DescriptorUtils.getReceiverParameterType(descriptor.getReceiverParameter()),
044 descriptor instanceof ConstructorDescriptor ? NO_RECEIVER_PARAMETER : descriptor.getExpectedThisObject(),
045 copyTypeParameters(descriptor),
046 copyValueParameters(descriptor),
047 descriptor.getReturnType(),
048 Modality.FINAL,
049 Visibilities.INTERNAL);
050 }
051
052 @NotNull
053 private List<TypeParameterDescriptor> copyTypeParameters(@NotNull FunctionDescriptor descriptor) {
054 List<TypeParameterDescriptor> typeParameters = descriptor.getTypeParameters();
055 List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
056 for (TypeParameterDescriptor typeParameter : typeParameters) {
057 TypeParameterDescriptorImpl copy = TypeParameterDescriptorImpl.createForFurtherModification(
058 this, typeParameter.getAnnotations(), typeParameter.isReified(),
059 typeParameter.getVariance(), typeParameter.getName(), typeParameter.getIndex()
060 );
061 for (JetType upperBound : typeParameter.getUpperBounds()) {
062 copy.addUpperBound(upperBound);
063 }
064 copy.setInitialized();
065 result.add(copy);
066 }
067 return result;
068 }
069
070 @NotNull
071 private List<ValueParameterDescriptor> copyValueParameters(@NotNull FunctionDescriptor descriptor) {
072 List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
073 List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(valueParameters.size());
074 for (ValueParameterDescriptor valueParameter : valueParameters) {
075 result.add(valueParameter.copy(this, valueParameter.getName()));
076 }
077 return result;
078 }
079 }