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.Deserialization;
032    import org.jetbrains.kotlin.serialization.deserialization.NameResolver;
033    import org.jetbrains.kotlin.serialization.deserialization.TypeTable;
034    
035    public class DeserializedSimpleFunctionDescriptor extends SimpleFunctionDescriptorImpl implements DeserializedCallableMemberDescriptor {
036    
037        private final ProtoBuf.Function proto;
038        private final NameResolver nameResolver;
039        private final TypeTable typeTable;
040    
041        private DeserializedSimpleFunctionDescriptor(
042                @NotNull DeclarationDescriptor containingDeclaration,
043                @Nullable SimpleFunctionDescriptor original,
044                @NotNull Annotations annotations,
045                @NotNull Name name,
046                @NotNull Kind kind,
047                @NotNull ProtoBuf.Function proto,
048                @NotNull NameResolver nameResolver,
049                @NotNull TypeTable typeTable
050        ) {
051            super(containingDeclaration, original, annotations, name, kind, SourceElement.NO_SOURCE);
052            this.proto = proto;
053            this.nameResolver = nameResolver;
054            this.typeTable = typeTable;
055        }
056    
057        @NotNull
058        @Override
059        protected FunctionDescriptorImpl createSubstitutedCopy(
060                @NotNull DeclarationDescriptor newOwner,
061                @Nullable FunctionDescriptor original,
062                @NotNull Kind kind,
063                @Nullable Name newName,
064                boolean preserveSource
065        ) {
066            return new DeserializedSimpleFunctionDescriptor(
067                    newOwner,
068                    (DeserializedSimpleFunctionDescriptor) original,
069                    getAnnotations(),
070                    newName != null ? newName : getName(),
071                    kind,
072                    proto,
073                    nameResolver,
074                    typeTable
075            );
076        }
077    
078        @NotNull
079        @Override
080        public DeserializedSimpleFunctionDescriptor getOriginal() {
081            return (DeserializedSimpleFunctionDescriptor) super.getOriginal();
082        }
083    
084        @NotNull
085        @Override
086        public ProtoBuf.Function getProto() {
087            return proto;
088        }
089    
090        @NotNull
091        @Override
092        public NameResolver getNameResolver() {
093            return nameResolver;
094        }
095    
096        @NotNull
097        @Override
098        public TypeTable getTypeTable() {
099            return typeTable;
100        }
101    
102        @NotNull
103        public static DeserializedSimpleFunctionDescriptor create(
104                @NotNull DeclarationDescriptor containingDeclaration,
105                @NotNull Annotations annotations,
106                @NotNull ProtoBuf.Function proto,
107                @NotNull NameResolver nameResolver,
108                @NotNull TypeTable typeTable
109        ) {
110            return new DeserializedSimpleFunctionDescriptor(
111                    containingDeclaration,
112                    null,
113                    annotations,
114                    nameResolver.getName(proto.getName()),
115                    Deserialization.memberKind(Flags.MEMBER_KIND.get(proto.getFlags())),
116                    proto,
117                    nameResolver,
118                    typeTable
119            );
120        }
121    }