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.descriptors.impl;
018    
019    import kotlin.KotlinPackage;
020    import kotlin.jvm.functions.Function1;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    import org.jetbrains.kotlin.descriptors.*;
024    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
025    import org.jetbrains.kotlin.name.Name;
026    import org.jetbrains.kotlin.resolve.constants.ConstantValue;
027    import org.jetbrains.kotlin.types.JetType;
028    import org.jetbrains.kotlin.types.TypeSubstitutor;
029    
030    import java.util.Collection;
031    
032    public class ValueParameterDescriptorImpl extends VariableDescriptorImpl implements ValueParameterDescriptor {
033        private final boolean declaresDefaultValue;
034        private final JetType varargElementType;
035        private final int index;
036        private final ValueParameterDescriptor original;
037    
038        public ValueParameterDescriptorImpl(
039                @NotNull CallableDescriptor containingDeclaration,
040                @Nullable ValueParameterDescriptor original,
041                int index,
042                @NotNull Annotations annotations,
043                @NotNull Name name,
044                @NotNull JetType outType,
045                boolean declaresDefaultValue,
046                @Nullable JetType varargElementType,
047                @NotNull SourceElement source
048        ) {
049            super(containingDeclaration, annotations, name, outType, source);
050            this.original = original == null ? this : original;
051            this.index = index;
052            this.declaresDefaultValue = declaresDefaultValue;
053            this.varargElementType = varargElementType;
054        }
055    
056        @NotNull
057        @Override
058        public CallableDescriptor getContainingDeclaration() {
059            return (CallableDescriptor) super.getContainingDeclaration();
060        }
061    
062        @Override
063        public int getIndex() {
064            return index;
065        }
066    
067        @Override
068        public boolean declaresDefaultValue() {
069            return declaresDefaultValue && ((CallableMemberDescriptor) getContainingDeclaration()).getKind().isReal();
070        }
071    
072        @Nullable
073        @Override
074        public JetType getVarargElementType() {
075            return varargElementType;
076        }
077    
078        @NotNull
079        @Override
080        public ValueParameterDescriptor getOriginal() {
081            return original == this ? this : original.getOriginal();
082        }
083    
084        @NotNull
085        @Override
086        public ValueParameterDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
087            if (substitutor.isEmpty()) return this;
088            throw new UnsupportedOperationException(); // TODO
089        }
090    
091        @Override
092        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
093            return visitor.visitValueParameterDescriptor(this, data);
094        }
095    
096        @Override
097        public boolean isVar() {
098            return false;
099        }
100    
101        @Nullable
102        @Override
103        public ConstantValue<?> getCompileTimeInitializer() {
104            return null;
105        }
106    
107        @NotNull
108        @Override
109        public ValueParameterDescriptor copy(@NotNull CallableDescriptor newOwner, @NotNull Name newName) {
110            return new ValueParameterDescriptorImpl(
111                    newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), varargElementType,
112                    SourceElement.NO_SOURCE
113            );
114        }
115    
116        @NotNull
117        @Override
118        public Visibility getVisibility() {
119            return Visibilities.LOCAL;
120        }
121    
122        @NotNull
123        @Override
124        public Collection<? extends ValueParameterDescriptor> getOverriddenDescriptors() {
125            return KotlinPackage.map(
126                    getContainingDeclaration().getOverriddenDescriptors(),
127                    new Function1<CallableDescriptor, ValueParameterDescriptor>() {
128                        @Override
129                        public ValueParameterDescriptor invoke(CallableDescriptor descriptor) {
130                            return descriptor.getValueParameters().get(getIndex());
131                        }
132                    });
133        }
134    }