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.TypeSubstitutor;
025    
026    import java.util.Collection;
027    import java.util.Collections;
028    import java.util.List;
029    
030    public class ClassConstructorDescriptorImpl extends FunctionDescriptorImpl implements ClassConstructorDescriptor {
031    
032        protected final boolean isPrimary;
033    
034        private static final Name NAME = Name.special("<init>");
035    
036        protected ClassConstructorDescriptorImpl(
037                @NotNull ClassDescriptor containingDeclaration,
038                @Nullable ConstructorDescriptor original,
039                @NotNull Annotations annotations,
040                boolean isPrimary,
041                @NotNull Kind kind,
042                @NotNull SourceElement source
043        ) {
044            super(containingDeclaration, original, annotations, NAME, kind, source);
045            this.isPrimary = isPrimary;
046        }
047    
048        @NotNull
049        public static ClassConstructorDescriptorImpl create(
050                @NotNull ClassDescriptor containingDeclaration,
051                @NotNull Annotations annotations,
052                boolean isPrimary,
053                @NotNull SourceElement source
054        ) {
055            return new ClassConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION, source);
056        }
057    
058        @NotNull
059        public static ClassConstructorDescriptorImpl createSynthesized(
060                @NotNull ClassDescriptor containingDeclaration,
061                @NotNull Annotations annotations,
062                boolean isPrimary,
063                @NotNull SourceElement source
064        ) {
065            return new ClassConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.SYNTHESIZED, source);
066        }
067    
068        public ClassConstructorDescriptorImpl initialize(
069                @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
070                @NotNull Visibility visibility,
071                @NotNull List<TypeParameterDescriptor> typeParameterDescriptors
072        ) {
073            super.initialize(
074                    null, calculateDispatchReceiverParameter(),
075                    typeParameterDescriptors,
076                    unsubstitutedValueParameters, null,
077                    Modality.FINAL, visibility);
078            return this;
079        }
080    
081        public ClassConstructorDescriptorImpl initialize(
082                @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
083                @NotNull Visibility visibility
084        ) {
085            initialize(unsubstitutedValueParameters, visibility, getContainingDeclaration().getDeclaredTypeParameters());
086            return this;
087        }
088    
089        @Nullable
090        private ReceiverParameterDescriptor calculateDispatchReceiverParameter() {
091            ClassDescriptor classDescriptor = getContainingDeclaration();
092            if (classDescriptor.isInner()) {
093                DeclarationDescriptor classContainer = classDescriptor.getContainingDeclaration();
094                if (classContainer instanceof ClassDescriptor) {
095                    return ((ClassDescriptor) classContainer).getThisAsReceiverParameter();
096                }
097            }
098            return null;
099        }
100    
101        @NotNull
102        @Override
103        public ClassDescriptor getContainingDeclaration() {
104            return (ClassDescriptor) super.getContainingDeclaration();
105        }
106    
107        @NotNull
108        @Override
109        public ClassDescriptor getConstructedClass() {
110            return getContainingDeclaration();
111        }
112    
113        @NotNull
114        @Override
115        public ClassConstructorDescriptor getOriginal() {
116            return (ClassConstructorDescriptor) super.getOriginal();
117        }
118    
119        @Nullable
120        @Override
121        public ClassConstructorDescriptor substitute(@NotNull TypeSubstitutor originalSubstitutor) {
122            return (ClassConstructorDescriptor) super.substitute(originalSubstitutor);
123        }
124    
125        @Override
126        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
127            return visitor.visitConstructorDescriptor(this, data);
128        }
129    
130        @Override
131        public boolean isPrimary() {
132            return isPrimary;
133        }
134    
135        @NotNull
136        @Override
137        public Collection<? extends FunctionDescriptor> getOverriddenDescriptors() {
138            return Collections.emptySet();
139        }
140    
141        @Override
142        public void setOverriddenDescriptors(@NotNull Collection<? extends CallableMemberDescriptor> overriddenDescriptors) {
143            assert overriddenDescriptors.isEmpty() : "Constructors cannot override anything";
144        }
145    
146        @NotNull
147        @Override
148        protected ClassConstructorDescriptorImpl createSubstitutedCopy(
149                @NotNull DeclarationDescriptor newOwner,
150                @Nullable FunctionDescriptor original,
151                @NotNull Kind kind,
152                @Nullable Name newName,
153                @NotNull Annotations annotations,
154                @NotNull SourceElement source
155        ) {
156            if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
157                throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
158                                                "copy from: " + this + "\n" +
159                                                "newOwner: " + newOwner + "\n" +
160                                                "kind: " + kind);
161            }
162            assert newName == null : "Attempt to rename constructor: " + this;
163            return new ClassConstructorDescriptorImpl(
164                    (ClassDescriptor) newOwner,
165                    this,
166                    annotations,
167                    isPrimary,
168                    Kind.DECLARATION,
169                    source
170            );
171        }
172    
173        @NotNull
174        @Override
175        public ClassConstructorDescriptor copy(
176                DeclarationDescriptor newOwner,
177                Modality modality,
178                Visibility visibility,
179                Kind kind,
180                boolean copyOverrides
181        ) {
182            return (ClassConstructorDescriptor) super.copy(newOwner, modality, visibility, kind, copyOverrides);
183        }
184    }