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,
063 Modality.FINAL, visibility);
064 return this;
065 }
066
067 @Nullable
068 private ReceiverParameterDescriptor calculateDispatchReceiverParameter() {
069 ClassDescriptor classDescriptor = getContainingDeclaration();
070 if (classDescriptor.isInner()) {
071 DeclarationDescriptor classContainer = classDescriptor.getContainingDeclaration();
072 if (classContainer instanceof ClassDescriptor) {
073 return ((ClassDescriptor) classContainer).getThisAsReceiverParameter();
074 }
075 }
076 return null;
077 }
078
079 @NotNull
080 @Override
081 public ClassDescriptor getContainingDeclaration() {
082 return (ClassDescriptor) super.getContainingDeclaration();
083 }
084
085 @NotNull
086 @Override
087 public ConstructorDescriptor getOriginal() {
088 return (ConstructorDescriptor) super.getOriginal();
089 }
090
091 @Override
092 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
093 return visitor.visitConstructorDescriptor(this, data);
094 }
095
096 @Override
097 public boolean isPrimary() {
098 return isPrimary;
099 }
100
101 @NotNull
102 @Override
103 public Collection<? extends FunctionDescriptor> getOverriddenDescriptors() {
104 return Collections.emptySet();
105 }
106
107 @Override
108 public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overriddenFunction) {
109 throw new UnsupportedOperationException("Constructors cannot override anything");
110 }
111
112 @NotNull
113 @Override
114 protected ConstructorDescriptorImpl createSubstitutedCopy(
115 @NotNull DeclarationDescriptor newOwner,
116 @Nullable FunctionDescriptor original,
117 @NotNull Kind kind,
118 @Nullable Name newName,
119 boolean preserveSource
120 ) {
121 if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
122 throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
123 "copy from: " + this + "\n" +
124 "newOwner: " + newOwner + "\n" +
125 "kind: " + kind);
126 }
127 assert original != null : "Attempt to create copy of constructor without preserving original: " + this;
128 assert newName == null : "Attempt to rename constructor: " + this;
129 return new ConstructorDescriptorImpl(
130 (ClassDescriptor) newOwner,
131 this,
132 getAnnotations(),
133 isPrimary,
134 Kind.DECLARATION,
135 getSourceToUseForCopy(preserveSource, original)
136 );
137 }
138
139 @NotNull
140 @Override
141 public ConstructorDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
142 throw new UnsupportedOperationException("Constructors should not be copied for overriding");
143 }
144 }