001    /*
002     * Copyright 2010-2013 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.jet.lang.descriptors.impl;
018    
019    import org.jetbrains.annotations.NotNull;
020    import org.jetbrains.annotations.Nullable;
021    import org.jetbrains.jet.lang.descriptors.*;
022    import org.jetbrains.jet.lang.descriptors.annotations.Annotations;
023    import org.jetbrains.jet.lang.resolve.name.Name;
024    import org.jetbrains.jet.lang.types.JetType;
025    import org.jetbrains.jet.lang.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            throw new UnsupportedOperationException(); // TODO
112        }
113    
114        @Override
115        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
116            return visitor.visitValueParameterDescriptor(this, data);
117        }
118    
119        @Override
120        public boolean isVar() {
121            return false;
122        }
123    
124        @NotNull
125        @Override
126        public ValueParameterDescriptor copy(@NotNull DeclarationDescriptor newOwner, @NotNull Name newName) {
127            return new ValueParameterDescriptorImpl(newOwner, null, index, getAnnotations(), newName, getType(), declaresDefaultValue(), varargElementType, SourceElement.NO_SOURCE);
128        }
129    
130        @NotNull
131        @Override
132        public Visibility getVisibility() {
133            return Visibilities.LOCAL;
134        }
135    
136        @NotNull
137        @Override
138        public Set<? extends ValueParameterDescriptor> getOverriddenDescriptors() {
139            return readOnlyOverriddenDescriptors;
140        }
141    
142        @Override
143        public void addOverriddenDescriptor(@NotNull ValueParameterDescriptor overridden) {
144            assert !overriddenDescriptorsLocked : "Adding more overridden descriptors is not allowed at this point: " +
145                                                  "the presence of the default value has already been calculated";
146            overriddenDescriptors.add(overridden);
147        }
148    }