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 org.jetbrains.annotations.NotNull;
020 import org.jetbrains.annotations.Nullable;
021 import org.jetbrains.kotlin.descriptors.*;
022 import org.jetbrains.kotlin.descriptors.annotations.Annotations;
023 import org.jetbrains.kotlin.name.Name;
024 import org.jetbrains.kotlin.types.JetType;
025 import org.jetbrains.kotlin.types.TypeSubstitutor;
026
027 import java.util.Collections;
028 import java.util.LinkedHashSet;
029 import java.util.Set;
030
031 public class ValueParameterDescriptorImpl extends VariableDescriptorImpl implements ValueParameterDescriptor {
032 private Boolean hasDefaultValue;
033 private final boolean declaresDefaultValue;
034 private final JetType varargElementType;
035 private final int index;
036 private final ValueParameterDescriptor original;
037 private final Set<ValueParameterDescriptor> overriddenDescriptors = new LinkedHashSet<ValueParameterDescriptor>(); // Linked is essential
038 private boolean overriddenDescriptorsLocked = false;
039 private final Set<? extends ValueParameterDescriptor> readOnlyOverriddenDescriptors = Collections.unmodifiableSet(overriddenDescriptors);
040
041 public ValueParameterDescriptorImpl(
042 @NotNull DeclarationDescriptor containingDeclaration,
043 @Nullable ValueParameterDescriptor original,
044 int index,
045 @NotNull Annotations annotations,
046 @NotNull Name name,
047 @NotNull JetType outType,
048 boolean declaresDefaultValue,
049 @Nullable JetType varargElementType,
050 @NotNull SourceElement source
051 ) {
052 super(containingDeclaration, annotations, name, outType, source);
053 this.original = original == null ? this : original;
054 this.index = index;
055 this.declaresDefaultValue = declaresDefaultValue;
056 this.varargElementType = varargElementType;
057 }
058
059 public void setType(@NotNull JetType type) {
060 setOutType(type);
061 }
062
063 @Override
064 public int getIndex() {
065 return index;
066 }
067
068 @Override
069 public boolean hasDefaultValue() {
070 computeDefaultValuePresence();
071 return hasDefaultValue;
072 }
073
074 @Override
075 public boolean declaresDefaultValue() {
076 return declaresDefaultValue && ((CallableMemberDescriptor) getContainingDeclaration()).getKind().isReal();
077 }
078
079 private void computeDefaultValuePresence() {
080 if (hasDefaultValue != null) return;
081 overriddenDescriptorsLocked = true;
082 if (declaresDefaultValue) {
083 hasDefaultValue = true;
084 }
085 else {
086 for (ValueParameterDescriptor descriptor : overriddenDescriptors) {
087 if (descriptor.hasDefaultValue()) {
088 hasDefaultValue = true;
089 return;
090 }
091 }
092 hasDefaultValue = false;
093 }
094 }
095
096 @Nullable
097 @Override
098 public JetType getVarargElementType() {
099 return varargElementType;
100 }
101
102 @NotNull
103 @Override
104 public ValueParameterDescriptor getOriginal() {
105 return original == this ? this : original.getOriginal();
106 }
107
108 @NotNull
109 @Override
110 public ValueParameterDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
111 if (substitutor.isEmpty()) return this;
112 throw new UnsupportedOperationException(); // TODO
113 }
114
115 @Override
116 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
117 return visitor.visitValueParameterDescriptor(this, data);
118 }
119
120 @Override
121 public boolean isVar() {
122 return false;
123 }
124
125 @NotNull
126 @Override
127 public ValueParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner, @NotNull Name newName) {
128 return new ValueParameterDescriptorImpl(newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), varargElementType, SourceElement.NO_SOURCE);
129 }
130
131 @NotNull
132 @Override
133 public Visibility getVisibility() {
134 return Visibilities.LOCAL;
135 }
136
137 @NotNull
138 @Override
139 public Set<? extends ValueParameterDescriptor> getOverriddenDescriptors() {
140 return readOnlyOverriddenDescriptors;
141 }
142
143 @Override
144 public void addOverriddenDescriptor(@NotNull ValueParameterDescriptor overridden) {
145 assert !overriddenDescriptorsLocked : "Adding more overridden descriptors is not allowed at this point: " +
146 "the presence of the default value has already been calculated";
147 overriddenDescriptors.add(overridden);
148 }
149 }