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.serialization.deserialization.descriptors;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
022    import org.jetbrains.kotlin.descriptors.FunctionDescriptor;
023    import org.jetbrains.kotlin.descriptors.SimpleFunctionDescriptor;
024    import org.jetbrains.kotlin.descriptors.SourceElement;
025    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
026    import org.jetbrains.kotlin.descriptors.impl.FunctionDescriptorImpl;
027    import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
028    import org.jetbrains.kotlin.name.Name;
029    import org.jetbrains.kotlin.serialization.Flags;
030    import org.jetbrains.kotlin.serialization.ProtoBuf;
031    import org.jetbrains.kotlin.serialization.deserialization.DeserializationPackage;
032    import org.jetbrains.kotlin.serialization.deserialization.NameResolver;
033    
034    public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor {
035    
036        private final ProtoBuf.Callable proto;
037        private final NameResolver nameResolver;
038    
039        private DeserializedSimpleFunctionDescriptor(
040                @NotNull DeclarationDescriptor containingDeclaration,
041                @Nullable SimpleFunctionDescriptor original,
042                @NotNull Annotations annotations,
043                @NotNull Name name,
044                @NotNull Kind kind,
045                @NotNull ProtoBuf.Callable proto,
046                @NotNull NameResolver nameResolver
047        ) {
048            super(containingDeclaration, original, annotations, name, kind, SourceElement.NO_SOURCE);
049            this.proto = proto;
050            this.nameResolver = nameResolver;
051        }
052    
053        @NotNull
054        @Override
055        protected FunctionDescriptorImpl createSubstitutedCopy(
056                @NotNull DeclarationDescriptor newOwner,
057                @Nullable FunctionDescriptor original,
058                @NotNull Kind kind
059        ) {
060            return new DeserializedSimpleFunctionDescriptor(
061                    newOwner,
062                    (DeserializedSimpleFunctionDescriptor) original,
063                    getAnnotations(),
064                    getName(),
065                    kind,
066                    proto,
067                    nameResolver
068            );
069        }
070    
071        @NotNull
072        @Override
073        public DeserializedSimpleFunctionDescriptor getOriginal() {
074            return (DeserializedSimpleFunctionDescriptor) super.getOriginal();
075        }
076    
077        @NotNull
078        @Override
079        public ProtoBuf.Callable getProto() {
080            return proto;
081        }
082    
083        @NotNull
084        @Override
085        public NameResolver getNameResolver() {
086            return nameResolver;
087        }
088    
089        @NotNull
090        public static DeserializedSimpleFunctionDescriptor create(
091                @NotNull DeclarationDescriptor containingDeclaration,
092                @NotNull ProtoBuf.Callable proto,
093                @NotNull NameResolver nameResolver,
094                @NotNull Annotations annotations
095        ) {
096            return new DeserializedSimpleFunctionDescriptor(
097                    containingDeclaration,
098                    null,
099                    annotations,
100                    nameResolver.getName(proto.getName()),
101                    DeserializationPackage.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())),
102                    proto,
103                    nameResolver
104            );
105        }
106    }