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.CollectionsKt;
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.KotlinType;
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 boolean isCrossinline;
035 private final boolean isNoinline;
036 private final KotlinType varargElementType;
037 private final int index;
038 private final ValueParameterDescriptor original;
039
040 public ValueParameterDescriptorImpl(
041 @NotNull CallableDescriptor containingDeclaration,
042 @Nullable ValueParameterDescriptor original,
043 int index,
044 @NotNull Annotations annotations,
045 @NotNull Name name,
046 @NotNull KotlinType outType,
047 boolean declaresDefaultValue,
048 boolean isCrossinline,
049 boolean isNoinline,
050 @Nullable KotlinType varargElementType,
051 @NotNull SourceElement source
052 ) {
053 super(containingDeclaration, annotations, name, outType, source);
054 this.original = original == null ? this : original;
055 this.index = index;
056 this.declaresDefaultValue = declaresDefaultValue;
057 this.isCrossinline = isCrossinline;
058 this.isNoinline = isNoinline;
059 this.varargElementType = varargElementType;
060 }
061
062 @NotNull
063 @Override
064 public CallableDescriptor getContainingDeclaration() {
065 return (CallableDescriptor) super.getContainingDeclaration();
066 }
067
068 @Override
069 public int getIndex() {
070 return index;
071 }
072
073 @Override
074 public boolean declaresDefaultValue() {
075 return declaresDefaultValue && ((CallableMemberDescriptor) getContainingDeclaration()).getKind().isReal();
076 }
077
078 @Override
079 public boolean isCrossinline() {
080 return isCrossinline;
081 }
082
083 @Override
084 public boolean isNoinline() {
085 return isNoinline;
086 }
087
088 @Nullable
089 @Override
090 public KotlinType getVarargElementType() {
091 return varargElementType;
092 }
093
094 @NotNull
095 @Override
096 public ValueParameterDescriptor getOriginal() {
097 return original == this ? this : original.getOriginal();
098 }
099
100 @NotNull
101 @Override
102 public ValueParameterDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
103 if (substitutor.isEmpty()) return this;
104 throw new UnsupportedOperationException(); // TODO
105 }
106
107 @Override
108 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
109 return visitor.visitValueParameterDescriptor(this, data);
110 }
111
112 @Override
113 public boolean isVar() {
114 return false;
115 }
116
117 @Nullable
118 @Override
119 public ConstantValue<?> getCompileTimeInitializer() {
120 return null;
121 }
122
123 @NotNull
124 @Override
125 public ValueParameterDescriptor copy(@NotNull CallableDescriptor newOwner, @NotNull Name newName) {
126 return new ValueParameterDescriptorImpl(
127 newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), isCrossinline(), isNoinline(), varargElementType,
128 SourceElement.NO_SOURCE
129 );
130 }
131
132 @NotNull
133 @Override
134 public Visibility getVisibility() {
135 return Visibilities.LOCAL;
136 }
137
138 @NotNull
139 @Override
140 public Collection<? extends ValueParameterDescriptor> getOverriddenDescriptors() {
141 return CollectionsKt.map(
142 getContainingDeclaration().getOverriddenDescriptors(),
143 new Function1<CallableDescriptor, ValueParameterDescriptor>() {
144 @Override
145 public ValueParameterDescriptor invoke(CallableDescriptor descriptor) {
146 return descriptor.getValueParameters().get(getIndex());
147 }
148 });
149 }
150 }