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