Exemplo n.º 1
0
  /* FUNCTIONS */
  private void renderFunction(
      @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(function, builder);
      renderVisibility(function.getVisibility(), builder);
      renderModalityForCallable(function, builder);
      renderOverride(function, builder);
      renderMemberKind(function, builder);

      builder.append(renderKeyword("fun")).append(" ");
      renderTypeParameters(function.getTypeParameters(), builder, true);

      ReceiverParameterDescriptor receiver = function.getReceiverParameter();
      if (receiver != null) {
        builder.append(escape(renderType(receiver.getType()))).append(".");
      }
    }

    renderName(function, builder);
    renderValueParameters(function, builder);
    JetType returnType = function.getReturnType();
    if (unitReturnType || !KotlinBuiltIns.getInstance().isUnit(returnType)) {
      builder.append(": ").append(returnType == null ? "[NULL]" : escape(renderType(returnType)));
    }
    renderWhereSuffix(function.getTypeParameters(), builder);
  }
Exemplo n.º 2
0
 @NotNull
 @Override
 public String renderFunctionParameters(@NotNull FunctionDescriptor functionDescriptor) {
   StringBuilder stringBuilder = new StringBuilder();
   renderValueParameters(functionDescriptor, stringBuilder);
   return stringBuilder.toString();
 }
Exemplo n.º 3
0
 @NotNull
 private String renderFqName(@NotNull List<Name> pathSegments) {
   StringBuilder buf = new StringBuilder();
   for (Name element : pathSegments) {
     if (buf.length() != 0) {
       buf.append(".");
     }
     buf.append(renderName(element));
   }
   return buf.toString();
 }
Exemplo n.º 4
0
  @NotNull
  @Override
  public String render(@NotNull DeclarationDescriptor declarationDescriptor) {
    StringBuilder stringBuilder = new StringBuilder();
    declarationDescriptor.accept(new RenderDeclarationDescriptorVisitor(), stringBuilder);

    if (withDefinedIn) {
      appendDefinedIn(declarationDescriptor, stringBuilder);
    }
    return stringBuilder.toString();
  }
Exemplo n.º 5
0
 private void appendTypeProjections(
     @NotNull List<TypeProjection> typeProjections, @NotNull StringBuilder builder) {
   for (Iterator<TypeProjection> iterator = typeProjections.iterator(); iterator.hasNext(); ) {
     TypeProjection typeProjection = iterator.next();
     if (typeProjection.getProjectionKind() != Variance.INVARIANT) {
       builder.append(typeProjection.getProjectionKind()).append(" ");
     }
     builder.append(renderType(typeProjection.getType()));
     if (iterator.hasNext()) {
       builder.append(", ");
     }
   }
 }
Exemplo n.º 6
0
  /* METHODS FOR ALL KINDS OF DESCRIPTORS */
  private void appendDefinedIn(
      @NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) {
    if (descriptor instanceof ModuleDescriptor) {
      builder.append(" is a module");
      return;
    }
    builder.append(" ").append(renderMessage("defined in")).append(" ");

    DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
    if (containingDeclaration != null) {
      FqNameUnsafe fqName = DescriptorUtils.getFQName(containingDeclaration);
      builder.append(FqName.ROOT.equalsTo(fqName) ? "root package" : renderFqName(fqName));
    }
  }
Exemplo n.º 7
0
 private void renderOverride(
     @NotNull CallableMemberDescriptor callableMember, @NotNull StringBuilder builder) {
   if (!modifiers.contains(Modifier.OVERRIDE)) return;
   if (overridesSomething(callableMember)) {
     if (overrideRenderingPolicy != OverrideRenderingPolicy.RENDER_OPEN) {
       builder.append("override ");
       if (verbose) {
         builder
             .append("/*")
             .append(callableMember.getOverriddenDescriptors().size())
             .append("*/ ");
       }
     }
   }
 }
Exemplo n.º 8
0
  /* CLASSES */
  private void renderClass(@NotNull ClassDescriptor klass, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(klass, builder);
      renderVisibility(klass.getVisibility(), builder);
      if (!(klass.getKind() == ClassKind.TRAIT && klass.getModality() == Modality.ABSTRACT
          || klass.getKind().isObject() && klass.getModality() == Modality.FINAL)) {
        renderModality(klass.getModality(), builder);
      }
      renderInner(klass.isInner(), builder);
      builder.append(renderKeyword(getClassKindPrefix(klass)));
    }

    if (klass.getKind() != ClassKind.CLASS_OBJECT || verbose) {
      builder.append(" ");
      renderName(klass, builder);
    }

    List<TypeParameterDescriptor> typeParameters = klass.getTypeConstructor().getParameters();
    renderTypeParameters(typeParameters, builder, false);

    if (!klass.getKind().isObject() && classWithPrimaryConstructor) {
      ConstructorDescriptor primaryConstructor = klass.getUnsubstitutedPrimaryConstructor();
      if (primaryConstructor != null) {
        renderValueParameters(primaryConstructor, builder);
      }
    }

    if (!klass.equals(KotlinBuiltIns.getInstance().getNothing())) {
      Collection<JetType> supertypes = klass.getTypeConstructor().getSupertypes();
      if (supertypes.isEmpty()
          || !alwaysRenderAny
              && supertypes.size() == 1
              && KotlinBuiltIns.getInstance().isAny(supertypes.iterator().next())) {
      } else {
        builder.append(" : ");
        for (Iterator<JetType> iterator = supertypes.iterator(); iterator.hasNext(); ) {
          JetType supertype = iterator.next();
          builder.append(renderType(supertype));
          if (iterator.hasNext()) {
            builder.append(", ");
          }
        }
      }
    }

    renderWhereSuffix(typeParameters, builder);
  }
Exemplo n.º 9
0
 private void renderVisibility(@NotNull Visibility visibility, @NotNull StringBuilder builder) {
   if (!modifiers.contains(Modifier.VISIBILITY)) return;
   if (normalizedVisibilities) {
     visibility = visibility.normalize();
   }
   if (!showInternalKeyword && visibility == Visibilities.INTERNAL) return;
   builder.append(renderKeyword(visibility.toString())).append(" ");
 }
Exemplo n.º 10
0
  /* TYPE PARAMETERS */
  private void renderTypeParameter(
      @NotNull TypeParameterDescriptor typeParameter,
      @NotNull StringBuilder builder,
      boolean topLevel) {
    if (topLevel) {
      builder.append(lt());
    }

    if (verbose) {
      builder.append("/*").append(typeParameter.getIndex()).append("*/ ");
    }

    if (typeParameter.isReified()) {
      builder.append(renderKeyword("reified")).append(" ");
    }
    String variance = typeParameter.getVariance().toString();
    if (!variance.isEmpty()) {
      builder.append(renderKeyword(variance)).append(" ");
    }
    renderName(typeParameter, builder);
    int upperBoundsCount = typeParameter.getUpperBounds().size();
    if ((upperBoundsCount > 1 && !topLevel) || upperBoundsCount == 1) {
      JetType upperBound = typeParameter.getUpperBounds().iterator().next();
      if (!KotlinBuiltIns.getInstance().getDefaultBound().equals(upperBound) || alwaysRenderAny) {
        builder.append(" : ").append(renderType(upperBound));
      }
    } else if (topLevel) {
      boolean first = true;
      for (JetType upperBound : typeParameter.getUpperBounds()) {
        if (upperBound.equals(KotlinBuiltIns.getInstance().getDefaultBound())) {
          continue;
        }
        if (first) {
          builder.append(" : ");
        } else {
          builder.append(" & ");
        }
        builder.append(renderType(upperBound));
        first = false;
      }
    } else {
      // rendered with "where"
    }

    if (topLevel) {
      builder.append(">");
    }
  }
Exemplo n.º 11
0
  /* VARIABLES */
  private void renderValueParameter(
      @NotNull ValueParameterDescriptor valueParameter,
      @NotNull StringBuilder builder,
      boolean topLevel) {
    if (topLevel) {
      builder.append(renderKeyword("value-parameter")).append(" ");
    }

    if (verbose) {
      builder.append("/*").append(valueParameter.getIndex()).append("*/ ");
    }

    renderAnnotations(valueParameter, builder);
    renderVariable(valueParameter, builder, topLevel);
    boolean withDefaultValue =
        debugMode ? valueParameter.declaresDefaultValue() : valueParameter.hasDefaultValue();
    if (withDefaultValue) {
      builder.append(" = ...");
    }
  }
Exemplo n.º 12
0
 private void renderTypeParameters(
     @NotNull List<TypeParameterDescriptor> typeParameters,
     @NotNull StringBuilder builder,
     boolean withSpace) {
   if (!typeParameters.isEmpty()) {
     builder.append(lt());
     for (Iterator<TypeParameterDescriptor> iterator = typeParameters.iterator();
         iterator.hasNext(); ) {
       TypeParameterDescriptor typeParameterDescriptor = iterator.next();
       renderTypeParameter(typeParameterDescriptor, builder, false);
       if (iterator.hasNext()) {
         builder.append(", ");
       }
     }
     builder.append(">");
     if (withSpace) {
       builder.append(" ");
     }
   }
 }
Exemplo n.º 13
0
  private void renderAnnotations(@NotNull Annotated annotated, @NotNull StringBuilder builder) {
    if (!modifiers.contains(Modifier.ANNOTATIONS)) return;
    for (AnnotationDescriptor annotation : annotated.getAnnotations()) {
      ClassDescriptor annotationClass =
          (ClassDescriptor) annotation.getType().getConstructor().getDeclarationDescriptor();
      assert annotationClass != null;

      if (!excludedAnnotationClasses.contains(
          DescriptorUtils.getFQName(annotationClass).toSafe())) {
        builder.append(renderType(annotation.getType()));
        if (verbose) {
          builder
              .append("(")
              .append(
                  StringUtil.join(DescriptorUtils.getSortedValueArguments(annotation, this), ", "))
              .append(")");
        }
        builder.append(" ");
      }
    }
  }
Exemplo n.º 14
0
  private void renderWhereSuffix(
      @NotNull List<TypeParameterDescriptor> typeParameters, @NotNull StringBuilder builder) {
    List<String> upperBoundStrings = Lists.newArrayList();

    for (TypeParameterDescriptor typeParameter : typeParameters) {
      if (typeParameter.getUpperBounds().size() > 1) {
        boolean first = true;
        for (JetType upperBound : typeParameter.getUpperBounds()) {
          // first parameter is rendered by renderTypeParameter:
          if (!first) {
            upperBoundStrings.add(
                renderName(typeParameter.getName()) + " : " + escape(renderType(upperBound)));
          }
          first = false;
        }
      }
    }
    if (!upperBoundStrings.isEmpty()) {
      builder.append(" ").append(renderKeyword("where")).append(" ");
      builder.append(StringUtil.join(upperBoundStrings, ", "));
    }
  }
Exemplo n.º 15
0
  private void renderProperty(
      @NotNull PropertyDescriptor property, @NotNull StringBuilder builder) {
    if (!startFromName) {
      renderAnnotations(property, builder);
      renderVisibility(property.getVisibility(), builder);
      renderModalityForCallable(property, builder);
      renderOverride(property, builder);
      renderMemberKind(property, builder);

      renderValVarPrefix(property, builder);
    }

    renderTypeParameters(property.getTypeParameters(), builder, true);

    ReceiverParameterDescriptor receiver = property.getReceiverParameter();
    if (receiver != null) {
      builder.append(escape(renderType(receiver.getType()))).append(".");
    }
    renderName(property, builder);
    builder.append(": ").append(escape(renderType(property.getType())));

    renderWhereSuffix(property.getTypeParameters(), builder);
  }
Exemplo n.º 16
0
  private void renderConstructor(
      @NotNull ConstructorDescriptor constructor, @NotNull StringBuilder builder) {
    renderAnnotations(constructor, builder);
    renderVisibility(constructor.getVisibility(), builder);
    renderMemberKind(constructor, builder);

    builder.append(renderKeyword("constructor")).append(" ");

    ClassDescriptor classDescriptor = constructor.getContainingDeclaration();
    renderName(classDescriptor, builder);

    renderTypeParameters(classDescriptor.getTypeConstructor().getParameters(), builder, false);
    renderValueParameters(constructor, builder);
    renderWhereSuffix(constructor.getTypeParameters(), builder);
  }
Exemplo n.º 17
0
  private void renderVariable(
      @NotNull VariableDescriptor variable, @NotNull StringBuilder builder, boolean topLevel) {
    JetType realType = variable.getType();

    JetType varargElementType =
        variable instanceof ValueParameterDescriptor
            ? ((ValueParameterDescriptor) variable).getVarargElementType()
            : null;
    JetType typeToRender = varargElementType != null ? varargElementType : realType;

    if (varargElementType != null) {
      builder.append(renderKeyword("vararg")).append(" ");
    }
    if (topLevel && !startFromName) {
      renderValVarPrefix(variable, builder);
    }

    renderName(variable, builder);
    builder.append(": ").append(escape(renderType(typeToRender)));

    if (verbose && varargElementType != null) {
      builder.append(" /*").append(escape(renderType(realType))).append("*/");
    }
  }
Exemplo n.º 18
0
  @NotNull
  private String renderDefaultType(@NotNull JetType type) {
    StringBuilder sb = new StringBuilder();

    sb.append(renderTypeName(type.getConstructor()));
    if (!type.getArguments().isEmpty()) {
      sb.append("<");
      appendTypeProjections(type.getArguments(), sb);
      sb.append(">");
    }
    if (type.isNullable()) {
      sb.append("?");
    }
    return sb.toString();
  }
Exemplo n.º 19
0
  @NotNull
  private String renderFunctionType(@NotNull JetType type) {
    StringBuilder sb = new StringBuilder();

    JetType receiverType = KotlinBuiltIns.getInstance().getReceiverType(type);
    if (receiverType != null) {
      sb.append(renderType(receiverType));
      sb.append(".");
    }

    sb.append("(");
    appendTypeProjections(
        KotlinBuiltIns.getInstance().getParameterTypeProjectionsFromFunctionType(type), sb);
    sb.append(") " + arrow() + " ");
    sb.append(renderType(KotlinBuiltIns.getInstance().getReturnTypeFromFunctionType(type)));

    if (type.isNullable()) {
      return "(" + sb + ")?";
    }
    return sb.toString();
  }
Exemplo n.º 20
0
 private void renderMemberKind(CallableMemberDescriptor callableMember, StringBuilder builder) {
   if (!modifiers.contains(Modifier.MEMBER_KIND)) return;
   if (verbose && callableMember.getKind() != CallableMemberDescriptor.Kind.DECLARATION) {
     builder.append("/*").append(callableMember.getKind().name().toLowerCase()).append("*/ ");
   }
 }
Exemplo n.º 21
0
 private void renderValVarPrefix(
     @NotNull VariableDescriptor variable, @NotNull StringBuilder builder) {
   builder.append(renderKeyword(variable.isVar() ? "var" : "val")).append(" ");
 }
Exemplo n.º 22
0
 private void renderModality(@NotNull Modality modality, @NotNull StringBuilder builder) {
   if (!modifiers.contains(Modifier.MODALITY)) return;
   String keyword = modality.name().toLowerCase();
   builder.append(renderKeyword(keyword)).append(" ");
 }
Exemplo n.º 23
0
 private void renderInner(boolean isInner, @NotNull StringBuilder builder) {
   if (!modifiers.contains(Modifier.INNER)) return;
   if (isInner) {
     builder.append(renderKeyword("inner")).append(" ");
   }
 }
Exemplo n.º 24
0
 private void renderNamespace(
     @NotNull NamespaceDescriptor namespace, @NotNull StringBuilder builder) {
   builder.append(renderKeyword("package")).append(" ");
   renderName(namespace, builder);
 }
Exemplo n.º 25
0
 private void renderName(
     @NotNull DeclarationDescriptor descriptor, @NotNull StringBuilder builder) {
   builder.append(renderName(descriptor.getName()));
 }