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 @NotNull List<TypeParameterDescriptor> typeParameterDescriptors
062 ) {
063 super.initialize(
064 null, calculateDispatchReceiverParameter(),
065 typeParameterDescriptors,
066 unsubstitutedValueParameters, null,
067 Modality.FINAL, visibility);
068 return this;
069 }
070
071 public ConstructorDescriptorImpl initialize(
072 @NotNull List<ValueParameterDescriptor> unsubstitutedValueParameters,
073 @NotNull Visibility visibility
074 ) {
075 initialize(unsubstitutedValueParameters, visibility, getContainingDeclaration().getDeclaredTypeParameters());
076 return this;
077 }
078
079 @Nullable
080 private ReceiverParameterDescriptor calculateDispatchReceiverParameter() {
081 ClassDescriptor classDescriptor = getContainingDeclaration();
082 if (classDescriptor.isInner()) {
083 DeclarationDescriptor classContainer = classDescriptor.getContainingDeclaration();
084 if (classContainer instanceof ClassDescriptor) {
085 return ((ClassDescriptor) classContainer).getThisAsReceiverParameter();
086 }
087 }
088 return null;
089 }
090
091 @NotNull
092 @Override
093 public ClassDescriptor getContainingDeclaration() {
094 return (ClassDescriptor) super.getContainingDeclaration();
095 }
096
097 @NotNull
098 @Override
099 public ConstructorDescriptor getOriginal() {
100 return (ConstructorDescriptor) super.getOriginal();
101 }
102
103 @NotNull
104 @Override
105 public ConstructorDescriptor substitute(@NotNull TypeSubstitutor originalSubstitutor) {
106 return (ConstructorDescriptor) super.substitute(originalSubstitutor);
107 }
108
109 @Override
110 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
111 return visitor.visitConstructorDescriptor(this, data);
112 }
113
114 @Override
115 public boolean isPrimary() {
116 return isPrimary;
117 }
118
119 @NotNull
120 @Override
121 public Collection<? extends FunctionDescriptor> getOverriddenDescriptors() {
122 return Collections.emptySet();
123 }
124
125 @Override
126 public void setOverriddenDescriptors(@NotNull Collection<? extends CallableMemberDescriptor> overriddenDescriptors) {
127 assert overriddenDescriptors.isEmpty() : "Constructors cannot override anything";
128 }
129
130 @NotNull
131 @Override
132 protected ConstructorDescriptorImpl createSubstitutedCopy(
133 @NotNull DeclarationDescriptor newOwner,
134 @Nullable FunctionDescriptor original,
135 @NotNull Kind kind,
136 @Nullable Name newName,
137 boolean preserveSource
138 ) {
139 if (kind != Kind.DECLARATION && kind != Kind.SYNTHESIZED) {
140 throw new IllegalStateException("Attempt at creating a constructor that is not a declaration: \n" +
141 "copy from: " + this + "\n" +
142 "newOwner: " + newOwner + "\n" +
143 "kind: " + kind);
144 }
145 assert newName == null : "Attempt to rename constructor: " + this;
146 return new ConstructorDescriptorImpl(
147 (ClassDescriptor) newOwner,
148 this,
149 getAnnotations(),
150 isPrimary,
151 Kind.DECLARATION,
152 getSourceToUseForCopy(preserveSource, original)
153 );
154 }
155
156 @NotNull
157 @Override
158 public ConstructorDescriptor copy(
159 DeclarationDescriptor newOwner,
160 Modality modality,
161 Visibility visibility,
162 Kind kind,
163 boolean copyOverrides
164 ) {
165 return (ConstructorDescriptor) super.copy(newOwner, modality, visibility, kind, copyOverrides);
166 }
167 }