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.codegen;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.codegen.coroutines.CoroutineCodegenUtilKt;
022    import org.jetbrains.kotlin.descriptors.*;
023    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
024    import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl;
025    import org.jetbrains.kotlin.name.Name;
026    import org.jetbrains.kotlin.resolve.DescriptorUtils;
027    
028    import java.util.LinkedHashMap;
029    
030    public class AccessorForFunctionDescriptor extends AbstractAccessorForFunctionDescriptor implements AccessorForCallableDescriptor<FunctionDescriptor> {
031        private final FunctionDescriptor calleeDescriptor;
032        private final ClassDescriptor superCallTarget;
033        private final String nameSuffix;
034    
035        public AccessorForFunctionDescriptor(
036                @NotNull FunctionDescriptor descriptor,
037                @NotNull DeclarationDescriptor containingDeclaration,
038                @Nullable ClassDescriptor superCallTarget,
039                @NotNull String nameSuffix
040        ) {
041            super(containingDeclaration,
042                  Name.identifier("access$" + nameSuffix));
043            this.calleeDescriptor = descriptor;
044            this.superCallTarget = superCallTarget;
045            this.nameSuffix = nameSuffix;
046    
047            initialize(DescriptorUtils.getReceiverParameterType(descriptor.getExtensionReceiverParameter()),
048                       descriptor instanceof ConstructorDescriptor || CodegenUtilKt.isJvmStaticInObjectOrClass(descriptor)
049                            ? null
050                            : descriptor.getDispatchReceiverParameter(),
051                       copyTypeParameters(descriptor),
052                       copyValueParameters(descriptor),
053                       descriptor.getReturnType(),
054                       Modality.FINAL,
055                       Visibilities.LOCAL);
056    
057            setSuspend(descriptor.isSuspend());
058            if (descriptor.getUserData(CoroutineCodegenUtilKt.INITIAL_DESCRIPTOR_FOR_SUSPEND_FUNCTION) != null) {
059                userDataMap = new LinkedHashMap<UserDataKey<?>, Object>();
060                userDataMap.put(
061                        CoroutineCodegenUtilKt.INITIAL_DESCRIPTOR_FOR_SUSPEND_FUNCTION,
062                        descriptor.getUserData(CoroutineCodegenUtilKt.INITIAL_DESCRIPTOR_FOR_SUSPEND_FUNCTION)
063                );
064            }
065        }
066    
067        @NotNull
068        @Override
069        protected FunctionDescriptorImpl createSubstitutedCopy(
070                @NotNull DeclarationDescriptor newOwner,
071                @Nullable FunctionDescriptor original,
072                @NotNull Kind kind,
073                @Nullable Name newName,
074                @NotNull Annotations annotations,
075                @NotNull SourceElement source
076        ) {
077            return new AccessorForFunctionDescriptor(calleeDescriptor, newOwner, superCallTarget, nameSuffix);
078        }
079    
080        @NotNull
081        @Override
082        public FunctionDescriptor getCalleeDescriptor() {
083            return calleeDescriptor;
084        }
085    
086        @Override
087        public ClassDescriptor getSuperCallTarget() {
088            return superCallTarget;
089        }
090    }