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.DescriptorUtils;
024    import org.jetbrains.jet.lang.resolve.name.Name;
025    
026    import java.util.Collections;
027    import java.util.List;
028    import java.util.Set;
029    
030    import static org.jetbrains.jet.lang.descriptors.ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER;
031    
032    public class ConstructorDescriptorImpl extends FunctionDescriptorImpl implements ConstructorDescriptor {
033    
034        protected final boolean isPrimary;
035    
036        private static final Name NAME = Name.special("<init>");
037    
038        protected ConstructorDescriptorImpl(
039                @NotNull ClassDescriptor containingDeclaration,
040                @Nullable ConstructorDescriptor original,
041                @NotNull Annotations annotations,
042                boolean isPrimary,
043                @NotNull Kind kind
044        ) {
045            super(containingDeclaration, original, annotations, NAME, kind);
046            this.isPrimary = isPrimary;
047        }
048    
049        @NotNull
050        public static ConstructorDescriptorImpl create(
051                @NotNull ClassDescriptor containingDeclaration,
052                @NotNull Annotations annotations,
053                boolean isPrimary
054        ) {
055            return new ConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION);
056        }
057    
058        public ConstructorDescriptorImpl initialize(
059                @NotNull List<TypeParameterDescriptor> typeParameters,
060                @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
061                @NotNull Visibility visibility,
062                boolean isStatic
063        ) {
064            super.initialize(null, isStatic ? NO_RECEIVER_PARAMETER : getExpectedThisObject(getContainingDeclaration()), typeParameters,
065                             unsubstitutedValueParameters, null, Modality.FINAL, visibility);
066            return this;
067        }
068    
069        @Nullable
070        private static ReceiverParameterDescriptor getExpectedThisObject(@NotNull ClassDescriptor descriptor) {
071            DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
072            return DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration);
073        }
074    
075        @NotNull
076        @Override
077        public ClassDescriptor getContainingDeclaration() {
078            return (ClassDescriptor) super.getContainingDeclaration();
079        }
080    
081        @NotNull
082        @Override
083        public ConstructorDescriptor getOriginal() {
084            return (ConstructorDescriptor) super.getOriginal();
085        }
086    
087        @Override
088        public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
089            return visitor.visitConstructorDescriptor(this, data);
090        }
091    
092        @Override
093        public boolean isPrimary() {
094            return isPrimary;
095        }
096    
097        @NotNull
098        @Override
099        public Set<? extends FunctionDescriptor> getOverriddenDescriptors() {
100            return Collections.emptySet();
101        }
102    
103        @Override
104        public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overriddenFunction) {
105            throw new UnsupportedOperationException("Constructors cannot override anything");
106        }
107    
108        @NotNull
109        @Override
110        protected FunctionDescriptorImpl createSubstitutedCopy(
111                @NotNull DeclarationDescriptor newOwner,
112                @Nullable FunctionDescriptor original,
113                @NotNull Kind kind
114        ) {
115            if (kind != Kind.DECLARATION) {
116                throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
117                                                "copy from: " + this + "\n" +
118                                                "newOwner: " + newOwner + "\n" +
119                                                "kind: " + kind);
120            }
121            assert original != null : "Attempt to create copy of constructor without preserving original: " + this;
122            return new ConstructorDescriptorImpl(
123                    (ClassDescriptor) newOwner,
124                    this,
125                    Annotations.EMPTY, // TODO
126                    isPrimary,
127                    Kind.DECLARATION);
128        }
129    
130        @NotNull
131        @Override
132        public ConstructorDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
133            throw new UnsupportedOperationException("Constructors should not be copied for overriding");
134        }
135    }