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 ConstructorDescriptorImpl extends FunctionDescriptorImpl implements ConstructorDescriptor {
031
032 protected final boolean isPrimary;
033
034 private static final Name NAME = Name.special("<init>");
035
036 protected ConstructorDescriptorImpl(
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 ConstructorDescriptorImpl create(
050 @NotNull ClassDescriptor containingDeclaration,
051 @NotNull Annotations annotations,
052 boolean isPrimary,
053 @NotNull SourceElement source
054 ) {
055 return new ConstructorDescriptorImpl(containingDeclaration, null, annotations, isPrimary, Kind.DECLARATION, source);
056 }
057
058 public ConstructorDescriptorImpl initialize(
059 @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
060 @NotNull Visibility visibility
061 ) {
062 super.initialize(
063 null, calculateDispatchReceiverParameter(),
064 getContainingDeclaration().getDeclaredTypeParameters(),
065 unsubstitutedValueParameters, null,
066 Modality.FINAL, visibility);
067 return this;
068 }
069
070 @Nullable
071 private ReceiverParameterDescriptor calculateDispatchReceiverParameter() {
072 ClassDescriptor classDescriptor = getContainingDeclaration();
073 if (classDescriptor.isInner()) {
074 DeclarationDescriptor classContainer = classDescriptor.getContainingDeclaration();
075 if (classContainer instanceof ClassDescriptor) {
076 return ((ClassDescriptor) classContainer).getThisAsReceiverParameter();
077 }
078 }
079 return null;
080 }
081
082 @NotNull
083 @Override
084 public ClassDescriptor getContainingDeclaration() {
085 return (ClassDescriptor) super.getContainingDeclaration();
086 }
087
088 @NotNull
089 @Override
090 public ConstructorDescriptor getOriginal() {
091 return (ConstructorDescriptor) super.getOriginal();
092 }
093
094 @NotNull
095 @Override
096 public ConstructorDescriptor substitute(@NotNull TypeSubstitutor originalSubstitutor) {
097 return (ConstructorDescriptor) super.substitute(originalSubstitutor);
098 }
099
100 @Override
101 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
102 return visitor.visitConstructorDescriptor(this, data);
103 }
104
105 @Override
106 public boolean isPrimary() {
107 return isPrimary;
108 }
109
110 @NotNull
111 @Override
112 public Collection<? extends FunctionDescriptor> getOverriddenDescriptors() {
113 return Collections.emptySet();
114 }
115
116 @Override
117 public void addOverriddenDescriptor(@NotNull CallableMemberDescriptor overriddenFunction) {
118 throw new UnsupportedOperationException("Constructors cannot override anything");
119 }
120
121 @NotNull
122 @Override
123 protected ConstructorDescriptorImpl createSubstitutedCopy(
124 @NotNull DeclarationDescriptor newOwner,
125 @Nullable FunctionDescriptor original,
126 @NotNull Kind kind,
127 @Nullable Name newName,
128 boolean preserveSource
129 ) {
130 if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
131 throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
132 "copy from: " + this + "\n" +
133 "newOwner: " + newOwner + "\n" +
134 "kind: " + kind);
135 }
136 assert newName == null : "Attempt to rename constructor: " + this;
137 return new ConstructorDescriptorImpl(
138 (ClassDescriptor) newOwner,
139 this,
140 getAnnotations(),
141 isPrimary,
142 Kind.DECLARATION,
143 getSourceToUseForCopy(preserveSource, original)
144 );
145 }
146
147 @NotNull
148 @Override
149 public ConstructorDescriptor copy(DeclarationDescriptor newOwner, Modality modality, Visibility visibility, Kind kind, boolean copyOverrides) {
150 //noinspection ConstantConditions
151 return (ConstructorDescriptor) doSubstitute(
152 newCopyBuilder()
153 .setOwner(newOwner)
154 .setModality(modality)
155 .setVisibility(visibility)
156 .setKind(kind)
157 .setCopyOverrides(copyOverrides)
158 );
159 }
160 }