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