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 implements AccessorForCallableDescriptor<FunctionDescriptor> {
034
035 private final FunctionDescriptor calleeDescriptor;
036
037 public AccessorForFunctionDescriptor(
038 @NotNull FunctionDescriptor descriptor,
039 @NotNull DeclarationDescriptor containingDeclaration,
040 int index
041 ) {
042 super(containingDeclaration, null, Annotations.EMPTY,
043 Name.identifier((descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName()) + "$b$" + index),
044 Kind.DECLARATION, SourceElement.NO_SOURCE);
045 this.calleeDescriptor = descriptor;
046
047 initialize(DescriptorUtils.getReceiverParameterType(descriptor.getExtensionReceiverParameter()),
048 descriptor instanceof ConstructorDescriptor ? NO_RECEIVER_PARAMETER : descriptor.getDispatchReceiverParameter(),
049 copyTypeParameters(descriptor),
050 copyValueParameters(descriptor),
051 descriptor.getReturnType(),
052 Modality.FINAL,
053 Visibilities.INTERNAL);
054 }
055
056 @NotNull
057 private List<TypeParameterDescriptor> copyTypeParameters(@NotNull FunctionDescriptor descriptor) {
058 List<TypeParameterDescriptor> typeParameters = descriptor.getTypeParameters();
059 List<TypeParameterDescriptor> result = new ArrayList<TypeParameterDescriptor>(typeParameters.size());
060 for (TypeParameterDescriptor typeParameter : typeParameters) {
061 TypeParameterDescriptorImpl copy = TypeParameterDescriptorImpl.createForFurtherModification(
062 this, typeParameter.getAnnotations(), typeParameter.isReified(),
063 typeParameter.getVariance(), typeParameter.getName(), typeParameter.getIndex(), SourceElement.NO_SOURCE
064 );
065 for (JetType upperBound : typeParameter.getUpperBounds()) {
066 copy.addUpperBound(upperBound);
067 }
068 copy.setInitialized();
069 result.add(copy);
070 }
071 return result;
072 }
073
074 @NotNull
075 private List<ValueParameterDescriptor> copyValueParameters(@NotNull FunctionDescriptor descriptor) {
076 List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
077 List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(valueParameters.size());
078 for (ValueParameterDescriptor valueParameter : valueParameters) {
079 result.add(valueParameter.copy(this, valueParameter.getName()));
080 }
081 return result;
082 }
083
084 @NotNull
085 @Override
086 public FunctionDescriptor getCalleeDescriptor() {
087 return calleeDescriptor;
088 }
089 }