private String doCompareNamespaces( @NotNull NamespaceDescriptor nsa, @NotNull NamespaceDescriptor nsb) { StringBuilder sb = new StringBuilder(); Assert.assertEquals(nsa.getName(), nsb.getName()); sb.append("namespace " + nsa.getName() + "\n\n"); Assert.assertTrue(!nsa.getMemberScope().getAllDescriptors().isEmpty()); Set<String> classifierNames = new HashSet<String>(); Set<String> propertyNames = new HashSet<String>(); Set<String> functionNames = new HashSet<String>(); for (DeclarationDescriptor ad : nsa.getMemberScope().getAllDescriptors()) { if (ad instanceof ClassifierDescriptor) { classifierNames.add(ad.getName()); } else if (ad instanceof PropertyDescriptor) { propertyNames.add(ad.getName()); } else if (ad instanceof FunctionDescriptor) { functionNames.add(ad.getName()); } else { throw new AssertionError("unknown member: " + ad); } } for (String name : classifierNames) { ClassifierDescriptor ca = nsa.getMemberScope().getClassifier(name); ClassifierDescriptor cb = nsb.getMemberScope().getClassifier(name); Assert.assertTrue(ca != null); Assert.assertTrue(cb != null); compareClassifiers(ca, cb, sb); } for (String name : propertyNames) { Set<VariableDescriptor> pa = nsa.getMemberScope().getProperties(name); Set<VariableDescriptor> pb = nsb.getMemberScope().getProperties(name); compareDeclarationSets(pa, pb, sb); Assert.assertTrue( nsb.getMemberScope().getFunctions(PropertyCodegen.getterName(name)).isEmpty()); Assert.assertTrue( nsb.getMemberScope().getFunctions(PropertyCodegen.setterName(name)).isEmpty()); } for (String name : functionNames) { Set<FunctionDescriptor> fa = nsa.getMemberScope().getFunctions(name); Set<FunctionDescriptor> fb = nsb.getMemberScope().getFunctions(name); compareDeclarationSets(fa, fb, sb); } return sb.toString(); }
@NotNull private static FqNameUnsafe getFqNameUnsafe(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); assert containingDeclaration != null : "Not package/module descriptor doesn't have containing declaration: " + descriptor; return getFqName(containingDeclaration).child(descriptor.getName()); }
@Override public void visitClassObject(@NotNull JetClassObject classObject) { JetObjectDeclaration objectDeclaration = classObject.getObjectDeclaration(); DeclarationDescriptor container = owner.getOwnerForChildren(); MutableClassDescriptor classObjectDescriptor = createClassDescriptorForSingleton( objectDeclaration, getClassObjectName(container.getName()), ClassKind.CLASS_OBJECT); PackageLikeBuilder.ClassObjectStatus status = isEnumEntry(container) || isObject(container) || c.getTopDownAnalysisParameters().isDeclaredLocally() ? PackageLikeBuilder.ClassObjectStatus.NOT_ALLOWED : owner.setClassObjectDescriptor(classObjectDescriptor); switch (status) { case DUPLICATE: trace.report(MANY_CLASS_OBJECTS.on(classObject)); break; case NOT_ALLOWED: trace.report(CLASS_OBJECT_NOT_ALLOWED.on(classObject)); break; case OK: // Everything is OK so no errors to trace. break; } }
public void checkRedeclarationsInInnerClassNames(@NotNull TopDownAnalysisContext c) { for (ClassDescriptorWithResolutionScopes classDescriptor : c.getDeclaredClasses().values()) { if (classDescriptor.getKind() == ClassKind.CLASS_OBJECT) { // Class objects should be considered during analysing redeclarations in classes continue; } Collection<DeclarationDescriptor> allDescriptors = classDescriptor.getScopeForMemberLookup().getOwnDeclaredDescriptors(); ClassDescriptorWithResolutionScopes classObj = classDescriptor.getClassObjectDescriptor(); if (classObj != null) { Collection<DeclarationDescriptor> classObjDescriptors = classObj.getScopeForMemberLookup().getOwnDeclaredDescriptors(); if (!classObjDescriptors.isEmpty()) { allDescriptors = Lists.newArrayList(allDescriptors); allDescriptors.addAll(classObjDescriptors); } } Multimap<Name, DeclarationDescriptor> descriptorMap = HashMultimap.create(); for (DeclarationDescriptor desc : allDescriptors) { if (desc instanceof ClassDescriptor || desc instanceof PropertyDescriptor) { descriptorMap.put(desc.getName(), desc); } } reportRedeclarations(descriptorMap); } }
private String renderDefaultType(JetType type, boolean shortNamesOnly) { StringBuilder sb = new StringBuilder(); ClassifierDescriptor cd = type.getConstructor().getDeclarationDescriptor(); Object typeNameObject; if (cd == null || cd instanceof TypeParameterDescriptor) { typeNameObject = type.getConstructor(); } else { if (shortNamesOnly) { // for nested classes qualified name should be used typeNameObject = cd.getName(); DeclarationDescriptor parent = cd.getContainingDeclaration(); while (parent instanceof ClassDescriptor) { typeNameObject = parent.getName() + "." + typeNameObject; parent = parent.getContainingDeclaration(); } } else { typeNameObject = DescriptorUtils.getFQName(cd); } } sb.append(typeNameObject); if (!type.getArguments().isEmpty()) { sb.append("<"); appendTypeProjections(sb, type.getArguments(), shortNamesOnly); sb.append(">"); } if (type.isNullable()) { sb.append("?"); } return sb.toString(); }
@NotNull public static FqName getFqNameFromTopLevelClass(@NotNull DeclarationDescriptor descriptor) { DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); Name name = descriptor.getName(); if (!(containingDeclaration instanceof ClassDescriptor)) { return FqName.topLevel(name); } return getFqNameFromTopLevelClass(containingDeclaration).child(name); }
@NotNull protected static Set<String> getDeclarationLabels( @Nullable PsiElement lambdaOrFun, @NotNull DeclarationDescriptor descriptor) { Set<String> result = new HashSet<String>(); if (lambdaOrFun != null) { Name label = LabelResolver.INSTANCE.getLabelNameIfAny(lambdaOrFun); if (label != null) { result.add(label.asString()); } } if (!isFunctionLiteral(descriptor)) { if (!descriptor.getName().isSpecial()) { result.add(descriptor.getName().asString()); } result.add(InlineCodegenUtil.FIRST_FUN_LABEL); } return result; }
private static String getJvmInternalFQNameImpl( BindingTrace bindingTrace, DeclarationDescriptor descriptor) { if (descriptor instanceof FunctionDescriptor) { throw new IllegalStateException("requested fq name for function: " + descriptor); } if (descriptor.getContainingDeclaration() instanceof ModuleDescriptor || descriptor instanceof ScriptDescriptor) { return ""; } if (descriptor instanceof ModuleDescriptor) { throw new IllegalStateException("missed something"); } if (descriptor instanceof ClassDescriptor) { ClassDescriptor klass = (ClassDescriptor) descriptor; if (klass.getKind() == ClassKind.OBJECT || klass.getKind() == ClassKind.CLASS_OBJECT) { if (klass.getContainingDeclaration() instanceof ClassDescriptor) { ClassDescriptor containingKlass = (ClassDescriptor) klass.getContainingDeclaration(); if (containingKlass.getKind() == ClassKind.ENUM_CLASS) { return getJvmInternalName(bindingTrace, containingKlass).getInternalName(); } else { return getJvmInternalName(bindingTrace, containingKlass).getInternalName() + JvmAbi.CLASS_OBJECT_SUFFIX; } } } JvmClassName name = bindingTrace.getBindingContext().get(FQN, descriptor); if (name != null) { return name.getInternalName(); } } DeclarationDescriptor container = descriptor.getContainingDeclaration(); if (container == null) { throw new IllegalStateException("descriptor has no container: " + descriptor); } Name name = descriptor.getName(); String baseName = getJvmInternalName(bindingTrace, container).getInternalName(); if (!baseName.isEmpty()) { return baseName + (container instanceof NamespaceDescriptor ? "/" : "$") + name.getIdentifier(); } return name.getIdentifier(); }
private void reportRedeclarations(@NotNull Multimap<Name, DeclarationDescriptor> descriptorMap) { Set<Pair<PsiElement, Name>> redeclarations = Sets.newHashSet(); for (Name name : descriptorMap.keySet()) { Collection<DeclarationDescriptor> descriptors = descriptorMap.get(name); if (descriptors.size() > 1) { // We mustn't compare PropertyDescriptor with PropertyDescriptor because we do this at // OverloadResolver for (DeclarationDescriptor descriptor : descriptors) { if (descriptor instanceof ClassDescriptor) { for (DeclarationDescriptor descriptor2 : descriptors) { if (descriptor == descriptor2) { continue; } redeclarations.add( Pair.create( BindingContextUtils.classDescriptorToDeclaration( trace.getBindingContext(), (ClassDescriptor) descriptor), descriptor.getName())); if (descriptor2 instanceof PropertyDescriptor) { redeclarations.add( Pair.create( BindingContextUtils.descriptorToDeclaration( trace.getBindingContext(), descriptor2), descriptor2.getName())); } } } } } } for (Pair<PsiElement, Name> redeclaration : redeclarations) { trace.report( REDECLARATION.on(redeclaration.getFirst(), redeclaration.getSecond().asString())); } }
private void checkRedeclarationsInPackages(@NotNull TopDownAnalysisContext c) { for (MutablePackageFragmentDescriptor packageFragment : Sets.newHashSet(c.getPackageFragments().values())) { PackageViewDescriptor packageView = packageFragment.getContainingDeclaration().getPackage(packageFragment.getFqName()); JetScope packageViewScope = packageView.getMemberScope(); Multimap<Name, DeclarationDescriptor> simpleNameDescriptors = packageFragment.getMemberScope().getDeclaredDescriptorsAccessibleBySimpleName(); for (Name name : simpleNameDescriptors.keySet()) { // Keep only properties with no receiver Collection<DeclarationDescriptor> descriptors = Collections2.filter( simpleNameDescriptors.get(name), new Predicate<DeclarationDescriptor>() { @Override public boolean apply(@Nullable DeclarationDescriptor descriptor) { if (descriptor instanceof PropertyDescriptor) { PropertyDescriptor propertyDescriptor = (PropertyDescriptor) descriptor; return propertyDescriptor.getReceiverParameter() == null; } return true; } }); ContainerUtil.addIfNotNull(descriptors, packageViewScope.getPackage(name)); if (descriptors.size() > 1) { for (DeclarationDescriptor declarationDescriptor : descriptors) { for (PsiElement declaration : getDeclarationsByDescriptor(declarationDescriptor)) { assert declaration != null : "Null declaration for descriptor: " + declarationDescriptor + " " + (declarationDescriptor != null ? DescriptorRenderer.FQ_NAMES_IN_TYPES.render(declarationDescriptor) : ""); trace.report( REDECLARATION.on(declaration, declarationDescriptor.getName().asString())); } } } } } }
@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 static boolean isAnonymousObject(@NotNull DeclarationDescriptor descriptor) { return isClass(descriptor) && descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED); }
public void serialize(ClassDescriptor klass) { if (!klass.getAnnotations().isEmpty()) { new Serializer(sb).serializeSeparated(klass.getAnnotations(), " "); sb.append(" "); } serialize(klass.getModality()); sb.append(" "); serialize(klass.getKind()); sb.append(" "); new Serializer(sb).serialize(klass); if (!klass.getTypeConstructor().getParameters().isEmpty()) { sb.append("<"); serializeCommaSeparated(klass.getTypeConstructor().getParameters()); sb.append(">"); } if (!klass.getTypeConstructor().getSupertypes().isEmpty()) { sb.append(" : "); new TypeSerializer(sb) .serializeCommaSeparated( new ArrayList<JetType>(klass.getTypeConstructor().getSupertypes())); } sb.append(" {\n"); List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(); for (TypeParameterDescriptor param : klass.getTypeConstructor().getParameters()) { typeArguments.add(new TypeProjection(Variance.INVARIANT, param.getDefaultType())); } List<String> memberStrings = new ArrayList<String>(); for (ConstructorDescriptor constructor : klass.getConstructors()) { StringBuilder constructorSb = new StringBuilder(); new Serializer(constructorSb).serialize(constructor); memberStrings.add(constructorSb.toString()); } JetScope memberScope = klass.getMemberScope(typeArguments); for (DeclarationDescriptor member : memberScope.getAllDescriptors()) { if (!includeObject) { if (member .getName() .matches("equals|hashCode|finalize|wait|notify(All)?|toString|clone|getClass")) { continue; } } StringBuilder memberSb = new StringBuilder(); new FullContentSerialier(memberSb).serialize(member); memberStrings.add(memberSb.toString()); } Collections.sort(memberStrings, new MemberComparator()); for (String memberString : memberStrings) { sb.append(indent(memberString)); } if (klass.getClassObjectDescriptor() != null) { StringBuilder sbForClassObject = new StringBuilder(); new FullContentSerialier(sbForClassObject).serialize(klass.getClassObjectDescriptor()); sb.append(indent(sbForClassObject.toString())); } sb.append("}\n"); }
private void renderName( @NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) { builder.append(renderName(descriptor.getName())); }
protected void renderName(DeclarationDescriptor descriptor, StringBuilder stringBuilder) { stringBuilder.append(escape(descriptor.getName().getName())); }