public static void registerClassNameForScript( BindingTrace bindingTrace, @NotNull ScriptDescriptor scriptDescriptor, @NotNull JvmClassName className) { bindingTrace.record(SCRIPT_NAMES, className); ClassDescriptorImpl classDescriptor = new ClassDescriptorImpl( scriptDescriptor, Collections.<AnnotationDescriptor>emptyList(), Modality.FINAL, Name.special("<script-" + className + ">")); classDescriptor.initialize( false, Collections.<TypeParameterDescriptor>emptyList(), Collections.singletonList(KotlinBuiltIns.getInstance().getAnyType()), JetScope.EMPTY, Collections.<ConstructorDescriptor>emptySet(), null, false); recordClosure(bindingTrace, null, classDescriptor, null, className, false); assert PsiCodegenPredictor.checkPredictedClassNameForFun( bindingTrace.getBindingContext(), scriptDescriptor, classDescriptor); bindingTrace.record(CLASS_FOR_SCRIPT, scriptDescriptor, classDescriptor); }
@NotNull private Collection<PsiElement> getDeclarationsByDescriptor( @NotNull DeclarationDescriptor declarationDescriptor) { Collection<PsiElement> declarations; if (declarationDescriptor instanceof PackageViewDescriptor) { final PackageViewDescriptor aPackage = (PackageViewDescriptor) declarationDescriptor; Collection<JetFile> files = trace.get(BindingContext.PACKAGE_TO_FILES, aPackage.getFqName()); if (files == null) { return Collections .emptyList(); // package can be defined out of Kotlin sources, e. g. in library or Java // code } declarations = Collections2.transform( files, new Function<JetFile, PsiElement>() { @Override public PsiElement apply(@Nullable JetFile file) { assert file != null : "File is null for aPackage " + aPackage; return file.getPackageDirective().getNameIdentifier(); } }); } else { declarations = Collections.singletonList( BindingContextUtils.descriptorToDeclaration( trace.getBindingContext(), declarationDescriptor)); } return declarations; }
@NotNull @Override public Collection<? extends JetType> getSupertypes() { if (supertypes == null) { if (resolveSession.isClassSpecial(DescriptorUtils.getFQName(LazyClassDescriptor.this))) { this.supertypes = Collections.emptyList(); } else { JetClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject(); if (classOrObject == null) { this.supertypes = Collections.emptyList(); } else { List<JetType> allSupertypes = resolveSession .getInjector() .getDescriptorResolver() .resolveSupertypes( getScopeForClassHeaderResolution(), LazyClassDescriptor.this, classOrObject, resolveSession.getTrace()); List<JetType> validSupertypes = Lists.newArrayList(Collections2.filter(allSupertypes, VALID_SUPERTYPE)); this.supertypes = validSupertypes; findAndDisconnectLoopsInTypeHierarchy(validSupertypes); } } } return supertypes; }
@Override public Collection<JetType> invoke() { if (resolveSession.isClassSpecial( DescriptorUtils.getFQName(LazyClassDescriptor.this))) { return Collections.emptyList(); } else { JetClassOrObject classOrObject = declarationProvider.getOwnerInfo().getCorrespondingClassOrObject(); if (classOrObject == null) { return Collections.emptyList(); } else { List<JetType> allSupertypes = resolveSession .getInjector() .getDescriptorResolver() .resolveSupertypes( getScopeForClassHeaderResolution(), LazyClassDescriptor.this, classOrObject, resolveSession.getTrace()); return Lists.newArrayList( Collections2.filter(allSupertypes, VALID_SUPERTYPE)); } } }
static { ERROR_CLASS.initialize( true, Collections.<TypeParameterDescriptor>emptyList(), Collections.<JetType>emptyList(), createErrorScope("ERROR_CLASS"), ERROR_CONSTRUCTOR_GROUP, ERROR_CONSTRUCTOR, false); }
private static JetType createErrorTypeWithCustomDebugName( JetScope memberScope, String debugName) { return new ErrorTypeImpl( new TypeConstructorImpl( ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false, debugName, Collections.<TypeParameterDescriptorImpl>emptyList(), Collections.singleton(KotlinBuiltIns.getInstance().getAnyType())), memberScope); }
public static ConstructorDescriptor createErrorConstructor( int typeParameterCount, List<JetType> positionedValueParameterTypes) { ConstructorDescriptorImpl r = new ConstructorDescriptorImpl( ERROR_CLASS, Collections.<AnnotationDescriptor>emptyList(), false); r.initialize( Collections.<TypeParameterDescriptor>emptyList(), // TODO Collections.<ValueParameterDescriptor>emptyList(), // TODO Visibilities.INTERNAL); r.setReturnType(createErrorType("<ERROR RETURN TYPE>")); return r; }
private static SimpleFunctionDescriptor createErrorFunction(ErrorScope ownerScope) { ErrorSimpleFunctionDescriptorImpl function = new ErrorSimpleFunctionDescriptorImpl(ownerScope); function.initialize( null, ReceiverParameterDescriptor.NO_RECEIVER_PARAMETER, Collections.<TypeParameterDescriptorImpl>emptyList(), // TODO Collections.<ValueParameterDescriptor>emptyList(), // TODO createErrorType("<ERROR FUNCTION RETURN TYPE>"), Modality.OPEN, Visibilities.INTERNAL, /*isInline = */ false); return function; }
@NotNull public static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName( @NotNull ResolveSession resolveSession, @NotNull FqName fqName, boolean includeObjectDeclarations) { if (fqName.isRoot()) { return Collections.emptyList(); } Collection<ClassDescriptor> classDescriptors = Lists.newArrayList(); FqName packageFqName = fqName.parent(); while (true) { NamespaceDescriptor packageDescriptor = resolveSession.getPackageDescriptorByFqName(packageFqName); if (packageDescriptor != null) { FqName classInPackagePath = new FqName(QualifiedNamesUtil.tail(packageFqName, fqName)); Collection<ClassDescriptor> descriptors = getClassOrObjectDescriptorsByFqName( packageDescriptor, classInPackagePath, includeObjectDeclarations); classDescriptors.addAll(descriptors); } if (packageFqName.isRoot()) { break; } else { packageFqName = packageFqName.parent(); } } return classDescriptors; }
private static List<FunctionDescriptor> getSuperFunctionsForMethod( @NotNull PsiMethodWrapper method, @NotNull BindingTrace trace, @NotNull ClassDescriptor containingClass) { List<FunctionDescriptor> superFunctions = Lists.newArrayList(); Map<ClassDescriptor, JetType> superclassToSupertype = getSuperclassToSupertypeMap(containingClass); Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> superclassToFunctions = getSuperclassToFunctionsMultimap(method, trace.getBindingContext(), containingClass); for (HierarchicalMethodSignature superSignature : method.getPsiMethod().getHierarchicalMethodSignature().getSuperSignatures()) { PsiMethod superMethod = superSignature.getMethod(); PsiClass psiClass = superMethod.getContainingClass(); assert psiClass != null; String classFqNameString = psiClass.getQualifiedName(); assert classFqNameString != null; FqName classFqName = new FqName(classFqNameString); if (!JavaToKotlinClassMap.getInstance().mapPlatformClass(classFqName).isEmpty()) { for (FunctionDescriptor superFun : JavaToKotlinMethodMap.INSTANCE.getFunctions(superMethod, containingClass)) { superFunctions.add(substituteSuperFunction(superclassToSupertype, superFun)); } continue; } DeclarationDescriptor superFun = superMethod instanceof JetClsMethod ? trace.get( BindingContext.DECLARATION_TO_DESCRIPTOR, ((JetClsMethod) superMethod).getOrigin()) : findSuperFunction(superclassToFunctions.get(classFqName), superMethod); if (superFun == null) { reportCantFindSuperFunction(method); continue; } assert superFun instanceof FunctionDescriptor : superFun.getClass().getName(); superFunctions.add( substituteSuperFunction(superclassToSupertype, (FunctionDescriptor) superFun)); } // sorting for diagnostic stability Collections.sort( superFunctions, new Comparator<FunctionDescriptor>() { @Override public int compare(FunctionDescriptor fun1, FunctionDescriptor fun2) { FqNameUnsafe fqName1 = getFQName(fun1.getContainingDeclaration()); FqNameUnsafe fqName2 = getFQName(fun2.getContainingDeclaration()); return fqName1.getFqName().compareTo(fqName2.getFqName()); } }); return superFunctions; }
private void createTypeConstructors(@NotNull TopDownAnalysisContext c) { for (Map.Entry<JetClassOrObject, ClassDescriptorWithResolutionScopes> entry : c.getClasses().entrySet()) { JetClassOrObject classOrObject = entry.getKey(); MutableClassDescriptor descriptor = (MutableClassDescriptor) entry.getValue(); if (classOrObject instanceof JetClass) { descriptorResolver.resolveMutableClassDescriptor( (JetClass) classOrObject, descriptor, trace); } else if (classOrObject instanceof JetObjectDeclaration) { descriptor.setModality(Modality.FINAL); descriptor.setVisibility( resolveVisibilityFromModifiers(classOrObject, getDefaultClassVisibility(descriptor))); descriptor.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList()); } descriptor.createTypeConstructor(); ClassKind kind = descriptor.getKind(); if (kind == ClassKind.ENUM_ENTRY || kind == ClassKind.OBJECT || kind == ClassKind.ENUM_CLASS) { MutableClassDescriptorLite classObject = descriptor.getClassObjectDescriptor(); assert classObject != null : "Enum entries and named objects should have class objects: " + classOrObject.getText(); JetType supertype; if (kind == ClassKind.ENUM_CLASS) { supertype = KotlinBuiltIns.getInstance().getAnyType(); } else { // This is a clever hack: each enum entry and object declaration (i.e. singleton) has a // synthetic class object. // We make this class object inherit from the singleton here, thus allowing to use the // singleton's class object where // the instance of the singleton is applicable. Effectively all members of the singleton // would be present in its class // object as fake overrides, so you can access them via standard class object notation: // ObjectName.memberName() supertype = descriptor.getDefaultType(); } classObject.setSupertypes(Collections.singleton(supertype)); classObject.createTypeConstructor(); } } }
private List<DeclarationDescriptor> sortDeclarations(Collection<DeclarationDescriptor> input) { ArrayList<DeclarationDescriptor> r = new ArrayList<DeclarationDescriptor>(input); Collections.sort( r, new Comparator<DeclarationDescriptor>() { @Override public int compare(DeclarationDescriptor o1, DeclarationDescriptor o2) { return o1.getName().compareTo(o2.getName()); } }); return r; }
@NotNull private List<AnnotationDescriptor> resolveAnnotations() { JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo(); JetModifierList modifierList = classInfo.getModifierList(); if (modifierList != null) { AnnotationResolver annotationResolver = resolveSession.getInjector().getAnnotationResolver(); JetScope scopeForDeclaration = getScopeProvider().getResolutionScopeForDeclaration(classInfo.getScopeAnchor()); return annotationResolver.resolveAnnotationsWithArguments( scopeForDeclaration, modifierList, resolveSession.getTrace()); } else { return Collections.emptyList(); } }
public static Map<TypeConstructor, TypeProjection> createSubstitutionContext( @NotNull FunctionDescriptor functionDescriptor, List<JetType> typeArguments) { if (functionDescriptor.getTypeParameters().isEmpty()) return Collections.emptyMap(); Map<TypeConstructor, TypeProjection> result = new HashMap<TypeConstructor, TypeProjection>(); int typeArgumentsSize = typeArguments.size(); List<TypeParameterDescriptor> typeParameters = functionDescriptor.getTypeParameters(); assert typeArgumentsSize == typeParameters.size(); for (int i = 0; i < typeArgumentsSize; i++) { TypeParameterDescriptor typeParameterDescriptor = typeParameters.get(i); JetType typeArgument = typeArguments.get(i); result.put(typeParameterDescriptor.getTypeConstructor(), new TypeProjection(typeArgument)); } return result; }
private static BodyResolver createBodyResolver( DelegatingBindingTrace trace, JetFile file, BodyResolveContextForLazy bodyResolveContext, ModuleConfiguration moduleConfiguration) { TopDownAnalysisParameters parameters = new TopDownAnalysisParameters( Predicates.<PsiFile>alwaysTrue(), false, true, Collections.<AnalyzerScriptParameter>emptyList()); InjectorForBodyResolve bodyResolve = new InjectorForBodyResolve( file.getProject(), parameters, trace, bodyResolveContext, moduleConfiguration); return bodyResolve.getBodyResolver(); }
private static List<ValueParameterDescriptor> getValueParameters( FunctionDescriptor functionDescriptor, List<JetType> argumentTypes) { List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>(); for (int i = 0, argumentTypesSize = argumentTypes.size(); i < argumentTypesSize; i++) { result.add( new ValueParameterDescriptorImpl( functionDescriptor, i, Collections.<AnnotationDescriptor>emptyList(), Name.special("<ERROR VALUE_PARAMETER>"), ERROR_PARAMETER_TYPE, false, null)); } return result; }
public static void initializeFromFunctionType( @NotNull FunctionDescriptorImpl functionDescriptor, @NotNull JetType functionType, @Nullable ReceiverParameterDescriptor expectedThisObject, @NotNull Modality modality, @NotNull Visibility visibility) { assert KotlinBuiltIns.getInstance().isFunctionOrExtensionFunctionType(functionType); functionDescriptor.initialize( KotlinBuiltIns.getInstance().getReceiverType(functionType), expectedThisObject, Collections.<TypeParameterDescriptorImpl>emptyList(), KotlinBuiltIns.getInstance().getValueParameters(functionDescriptor, functionType), KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(functionType), modality, visibility); }
@NotNull private MutableClassDescriptor createSyntheticClassObject( @NotNull ClassDescriptor classDescriptor) { MutableClassDescriptor classObject = new MutableClassDescriptor( classDescriptor, outerScope, ClassKind.CLASS_OBJECT, false, getClassObjectName(classDescriptor.getName())); classObject.setModality(Modality.FINAL); classObject.setVisibility(DescriptorUtils.getSyntheticClassObjectVisibility()); classObject.setTypeParameterDescriptors(Collections.<TypeParameterDescriptor>emptyList()); createPrimaryConstructorForObject(null, classObject); return classObject; }
private static String serializedDeclarationSets(Collection<? extends DeclarationDescriptor> ds) { List<String> strings = new ArrayList<String>(); for (DeclarationDescriptor d : ds) { StringBuilder sb = new StringBuilder(); new Serializer(sb).serialize(d); strings.add(sb.toString()); } Collections.sort(strings, new MemberComparator()); StringBuilder r = new StringBuilder(); for (String string : strings) { r.append(string); r.append("\n"); } return r.toString(); }
@NotNull public static Collection<JetFile> allFilesInNamespaces( BindingContext bindingContext, Collection<JetFile> files) { // todo: we use Set and add given files but ignoring other scripts because something non-clear // kept in binding // for scripts especially in case of REPL HashSet<FqName> names = new HashSet<FqName>(); for (JetFile file : files) { if (!file.isScript()) { names.add(JetPsiUtil.getFQName(file)); } } HashSet<JetFile> answer = new HashSet<JetFile>(); answer.addAll(files); for (FqName name : names) { NamespaceDescriptor namespaceDescriptor = bindingContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, name); Collection<JetFile> jetFiles = bindingContext.get(NAMESPACE_TO_FILES, namespaceDescriptor); if (jetFiles != null) answer.addAll(jetFiles); } List<JetFile> sortedAnswer = new ArrayList<JetFile>(answer); Collections.sort( sortedAnswer, new Comparator<JetFile>() { @NotNull private String path(JetFile file) { VirtualFile virtualFile = file.getVirtualFile(); assert virtualFile != null : "VirtualFile is null for JetFile: " + file.getName(); return virtualFile.getPath(); } @Override public int compare(JetFile first, JetFile second) { return path(first).compareTo(path(second)); } }); return sortedAnswer; }
@Override public List<AnnotationDescriptor> getAnnotations() { if (annotations == null) { JetClassLikeInfo classInfo = declarationProvider.getOwnerInfo(); JetModifierList modifierList = classInfo.getModifierList(); if (modifierList != null) { AnnotationResolver annotationResolver = resolveSession.getInjector().getAnnotationResolver(); annotations = annotationResolver.resolveAnnotations( resolveSession.getResolutionScope(classInfo.getScopeAnchor()), modifierList, resolveSession.getTrace()); } else { annotations = Collections.emptyList(); } } return annotations; }
protected Void visitVariableDescriptor( VariableDescriptor descriptor, StringBuilder builder, boolean skipValVar) { JetType type = descriptor.getType(); if (descriptor instanceof ValueParameterDescriptor) { JetType varargElementType = ((ValueParameterDescriptor) descriptor).getVarargElementType(); if (varargElementType != null) { builder.append(renderKeyword("vararg")).append(" "); type = varargElementType; } } String typeString = renderPropertyPrefixAndComputeTypeString( builder, skipValVar ? null : descriptor.isVar(), Collections.<TypeParameterDescriptor>emptyList(), ReceiverDescriptor.NO_RECEIVER, type); renderName(descriptor, builder); builder.append(" : ").append(escape(typeString)); return null; }
private static Collection<ClassDescriptor> getClassOrObjectDescriptorsByFqName( NamespaceDescriptor packageDescriptor, FqName path, boolean includeObjectDeclarations) { if (path.isRoot()) { return Collections.emptyList(); } Collection<JetScope> scopes = Arrays.asList(packageDescriptor.getMemberScope()); List<Name> names = path.pathSegments(); if (names.size() > 1) { for (Name subName : path.pathSegments().subList(0, names.size() - 1)) { Collection<JetScope> tempScopes = Lists.newArrayList(); for (JetScope scope : scopes) { ClassifierDescriptor classifier = scope.getClassifier(subName); if (classifier instanceof ClassDescriptorBase) { ClassDescriptorBase classDescriptor = (ClassDescriptorBase) classifier; tempScopes.add(classDescriptor.getUnsubstitutedInnerClassesScope()); } } scopes = tempScopes; } } Name shortName = path.shortName(); Collection<ClassDescriptor> resultClassifierDescriptors = Lists.newArrayList(); for (JetScope scope : scopes) { ClassifierDescriptor classifier = scope.getClassifier(shortName); if (classifier instanceof ClassDescriptor) { resultClassifierDescriptors.add((ClassDescriptor) classifier); } if (includeObjectDeclarations) { ClassDescriptor objectDescriptor = scope.getObjectDescriptor(shortName); if (objectDescriptor != null) { resultClassifierDescriptors.add(objectDescriptor); } } } return resultClassifierDescriptors; }
@NotNull public Collection<ClassDescriptor> getTopLevelObjectsByName( @NotNull String name, @NotNull JetSimpleNameExpression expression, @NotNull ResolveSession resolveSession, @NotNull GlobalSearchScope scope) { BindingContext context = ResolveSessionUtils.resolveToExpression(resolveSession, expression); JetScope jetScope = context.get(BindingContext.RESOLUTION_SCOPE, expression); if (jetScope == null) { return Collections.emptyList(); } Set<ClassDescriptor> result = Sets.newHashSet(); Collection<JetObjectDeclaration> topObjects = JetTopLevelShortObjectNameIndex.getInstance().get(name, project, scope); for (JetObjectDeclaration objectDeclaration : topObjects) { FqName fqName = JetPsiUtil.getFQName(objectDeclaration); assert fqName != null : "Local object declaration in JetTopLevelShortObjectNameIndex:" + objectDeclaration.getText(); result.addAll( ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true)); } for (PsiClass psiClass : JetFromJavaDescriptorHelper.getCompiledClassesForTopLevelObjects( project, GlobalSearchScope.allScope(project))) { String qualifiedName = psiClass.getQualifiedName(); if (qualifiedName != null) { FqName fqName = new FqName(qualifiedName); result.addAll( ResolveSessionUtils.getClassOrObjectDescriptorsByFqName(resolveSession, fqName, true)); } } return result; }
@NotNull private String renderClassName(@NotNull ClassDescriptor klass) { if (ErrorUtils.isError(klass)) { return klass.getTypeConstructor().toString(); } if (shortNames) { List<Name> qualifiedNameElements = Lists.newArrayList(); // for nested classes qualified name should be used DeclarationDescriptor current = klass; do { if (((ClassDescriptor) current).getKind() != ClassKind.CLASS_OBJECT) { qualifiedNameElements.add(current.getName()); } current = current.getContainingDeclaration(); } while (current instanceof ClassDescriptor); Collections.reverse(qualifiedNameElements); return renderFqName(qualifiedNameElements); } return renderFqName(DescriptorUtils.getFQName(klass)); }
public void serialize(TypeParameterDescriptor param) { sb.append("/*"); sb.append(param.getIndex()); if (param.isReified()) { sb.append(",r"); } sb.append("*/ "); serialize(param.getVariance()); sb.append(param.getName()); if (!param.getUpperBounds().isEmpty()) { sb.append(" : "); List<String> list = new ArrayList<String>(); for (JetType upper : param.getUpperBounds()) { StringBuilder sb = new StringBuilder(); new TypeSerializer(sb).serialize(upper); list.add(sb.toString()); } Collections.sort(list); serializeSeparated(list, " & "); // TODO: use where } // TODO: lower bounds }
public JvmFunctionImplTypes(@NotNull ReflectionTypes reflectionTypes) { this.reflectionTypes = reflectionTypes; ModuleDescriptor fakeModule = new ModuleDescriptorImpl( Name.special("<fake module for functions impl>"), Collections.<ImportPath>emptyList(), JavaToKotlinClassMap.getInstance()); PackageFragmentDescriptor kotlinJvmInternal = new MutablePackageFragmentDescriptor(fakeModule, new FqName("kotlin.jvm.internal")); PackageFragmentDescriptor kotlinReflectJvmInternal = new MutablePackageFragmentDescriptor(fakeModule, new FqName("kotlin.reflect.jvm.internal")); this.functionImpl = createClass(kotlinJvmInternal, "FunctionImpl", "out R"); this.extensionFunctionImpl = createClass(kotlinJvmInternal, "ExtensionFunctionImpl", "in T", "out R"); this.kFunctionImpl = createClass(kotlinReflectJvmInternal, "KFunctionImpl", "out R"); this.kExtensionFunctionImpl = createClass(kotlinReflectJvmInternal, "KExtensionFunctionImpl", "in T", "out R"); this.kMemberFunctionImpl = createClass(kotlinReflectJvmInternal, "KMemberFunctionImpl", "in T", "out R"); }
@NotNull @Override public Set<? extends Class> getArgListStopSearchClasses() { return Collections.singleton(JetFunction.class); }
@NotNull @Override public Set<Class> getArgumentListAllowedParentClasses() { return Collections.singleton((Class) JetCallElement.class); }
@Override public List<AnnotationDescriptor> getAnnotations() { return Collections.emptyList(); // TODO }