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
017 package org.jetbrains.jet.lang.descriptors.impl;
018
019 import jet.Function0;
020 import org.jetbrains.annotations.NotNull;
021 import org.jetbrains.annotations.Nullable;
022 import org.jetbrains.jet.lang.descriptors.*;
023 import org.jetbrains.jet.lang.resolve.name.Name;
024 import org.jetbrains.jet.lang.resolve.scopes.InnerClassesScopeWrapper;
025 import org.jetbrains.jet.lang.resolve.scopes.JetScope;
026 import org.jetbrains.jet.lang.resolve.scopes.SubstitutingScope;
027 import org.jetbrains.jet.lang.types.*;
028 import org.jetbrains.jet.storage.NotNullLazyValue;
029 import org.jetbrains.jet.storage.StorageManager;
030
031 import java.util.List;
032 import java.util.Map;
033
034 public abstract class AbstractClassDescriptor implements ClassDescriptor {
035 private final Name name;
036 protected final NotNullLazyValue<JetType> defaultType;
037 private final NotNullLazyValue<JetScope> unsubstitutedInnerClassesScope;
038 private final NotNullLazyValue<ReceiverParameterDescriptor> thisAsReceiverParameter;
039
040 public AbstractClassDescriptor(@NotNull StorageManager storageManager, @NotNull Name name) {
041 this.name = name;
042 this.defaultType = storageManager.createLazyValue(new Function0<JetType>() {
043 @Override
044 public JetType invoke() {
045 return TypeUtils.makeUnsubstitutedType(AbstractClassDescriptor.this, getScopeForMemberLookup());
046 }
047 });
048 this.unsubstitutedInnerClassesScope = storageManager.createLazyValue(new Function0<JetScope>() {
049 @Override
050 public JetScope invoke() {
051 return new InnerClassesScopeWrapper(getScopeForMemberLookup());
052 }
053 });
054 this.thisAsReceiverParameter = storageManager.createLazyValue(new Function0<ReceiverParameterDescriptor>() {
055 @Override
056 public ReceiverParameterDescriptor invoke() {
057 return new LazyClassReceiverParameterDescriptor(AbstractClassDescriptor.this);
058 }
059 });
060 }
061
062 @NotNull
063 @Override
064 public Name getName() {
065 return name;
066 }
067
068 @NotNull
069 @Override
070 public DeclarationDescriptor getOriginal() {
071 return this;
072 }
073
074 @NotNull
075 protected abstract JetScope getScopeForMemberLookup();
076
077 @Nullable
078 @Override
079 public JetType getClassObjectType() {
080 ClassDescriptor classObject = getClassObjectDescriptor();
081 return classObject == null ? null : classObject.getDefaultType();
082 }
083
084 @NotNull
085 @Override
086 public JetScope getUnsubstitutedInnerClassesScope() {
087 return unsubstitutedInnerClassesScope.invoke();
088 }
089
090 @NotNull
091 @Override
092 public ReceiverParameterDescriptor getThisAsReceiverParameter() {
093 return thisAsReceiverParameter.invoke();
094 }
095
096 @NotNull
097 @Override
098 public JetScope getMemberScope(List<? extends TypeProjection> typeArguments) {
099 assert typeArguments.size() == getTypeConstructor().getParameters().size() : "Illegal number of type arguments: expected "
100 + getTypeConstructor().getParameters().size() + " but was " + typeArguments.size()
101 + " for " + getTypeConstructor() + " " + getTypeConstructor().getParameters();
102 if (typeArguments.isEmpty()) return getScopeForMemberLookup();
103
104 List<TypeParameterDescriptor> typeParameters = getTypeConstructor().getParameters();
105 Map<TypeConstructor, TypeProjection> substitutionContext = SubstitutionUtils.buildSubstitutionContext(typeParameters, typeArguments);
106
107 // Unsafe substitutor is OK, because no recursion can hurt us upon a trivial substitution:
108 // all the types are written explicitly in the code already, they can not get infinite.
109 // One exception is *-projections, but they need to be handled separately anyways.
110 TypeSubstitutor substitutor = TypeSubstitutor.createUnsafe(substitutionContext);
111 return new SubstitutingScope(getScopeForMemberLookup(), substitutor);
112 }
113
114 @NotNull
115 @Override
116 public ClassDescriptor substitute(@NotNull TypeSubstitutor substitutor) {
117 if (substitutor.isEmpty()) {
118 return this;
119 }
120 return new LazySubstitutingClassDescriptor(this, substitutor);
121 }
122
123 @NotNull
124 @Override
125 public JetType getDefaultType() {
126 return defaultType.invoke();
127 }
128
129 @Override
130 public void acceptVoid(DeclarationDescriptorVisitor<Void, Void> visitor) {
131 visitor.visitClassDescriptor(this, null);
132 }
133
134 @Override
135 public <R, D> R accept(DeclarationDescriptorVisitor<R, D> visitor, D data) {
136 return visitor.visitClassDescriptor(this, data);
137 }
138 }