Beispiel #1
0
  public static CodegenContext getContext(DeclarationDescriptor descriptor, GenerationState state) {
    if (descriptor instanceof PackageFragmentDescriptor) {
      return new PackageContext(
          (PackageFragmentDescriptor) descriptor, state.getRootContext(), null);
    }

    CodegenContext parent = getContext(descriptor.getContainingDeclaration(), state);

    if (descriptor instanceof ClassDescriptor) {
      OwnerKind kind =
          DescriptorUtils.isInterface(descriptor)
              ? OwnerKind.DEFAULT_IMPLS
              : OwnerKind.IMPLEMENTATION;
      return parent.intoClass((ClassDescriptor) descriptor, kind, state);
    } else if (descriptor instanceof ScriptDescriptor) {
      ClassDescriptor classDescriptorForScript =
          state.getBindingContext().get(CLASS_FOR_SCRIPT, (ScriptDescriptor) descriptor);
      assert classDescriptorForScript != null : "Can't find class for script: " + descriptor;
      List<ScriptDescriptor> earlierScripts = state.getEarlierScriptsForReplInterpreter();
      return parent.intoScript(
          (ScriptDescriptor) descriptor,
          earlierScripts == null ? Collections.emptyList() : earlierScripts,
          classDescriptorForScript);
    } else if (descriptor instanceof FunctionDescriptor) {
      return parent.intoFunction((FunctionDescriptor) descriptor);
    }

    throw new IllegalStateException("Couldn't build context for " + descriptorName(descriptor));
  }
Beispiel #2
0
 private static boolean isIllegalNestedClass(@NotNull DeclarationDescriptor descriptor) {
   if (!(descriptor instanceof ClassDescriptor)) return false;
   DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
   if (!(containingDeclaration instanceof ClassDescriptor)) return false;
   ClassDescriptor containingClass = (ClassDescriptor) containingDeclaration;
   return containingClass.isInner()
       || containingClass.getContainingDeclaration() instanceof FunctionDescriptor;
 }
Beispiel #3
0
  @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;
  }
Beispiel #4
0
  /*descriptor is null for captured vars*/
  public boolean shouldPutValue(
      @NotNull Type type,
      @Nullable StackValue stackValue,
      @Nullable ValueParameterDescriptor descriptor) {

    if (stackValue == null) {
      // default or vararg
      return true;
    }

    // remap only inline functions (and maybe non primitives)
    // TODO - clean asserion and remapping logic
    if (isPrimitive(type) != isPrimitive(stackValue.type)) {
      // don't remap boxing/unboxing primitives - lost identity and perfomance
      return true;
    }

    if (stackValue instanceof StackValue.Local) {
      return false;
    }

    StackValue field = stackValue;
    if (stackValue instanceof StackValue.FieldForSharedVar) {
      field = ((StackValue.FieldForSharedVar) stackValue).receiver;
    }

    // check that value corresponds to captured inlining parameter
    if (field instanceof StackValue.Field) {
      DeclarationDescriptor varDescriptor = ((StackValue.Field) field).descriptor;
      // check that variable is inline function parameter
      return !(varDescriptor instanceof ParameterDescriptor
          && InlineUtil.isInlineLambdaParameter((ParameterDescriptor) varDescriptor)
          && InlineUtil.isInline(varDescriptor.getContainingDeclaration()));
    }

    return true;
  }
Beispiel #5
0
  private void checkSupertypeList(
      @NotNull ClassDescriptor supertypeOwner,
      @NotNull Map<JetTypeReference, JetType> supertypes,
      @NotNull JetClassOrObject jetClass) {
    Set<TypeConstructor> allowedFinalSupertypes =
        getAllowedFinalSupertypes(supertypeOwner, jetClass);
    Set<TypeConstructor> typeConstructors = Sets.newHashSet();
    boolean classAppeared = false;
    for (Map.Entry<JetTypeReference, JetType> entry : supertypes.entrySet()) {
      JetTypeReference typeReference = entry.getKey();
      JetType supertype = entry.getValue();

      boolean addSupertype = true;

      ClassDescriptor classDescriptor = TypeUtils.getClassDescriptor(supertype);
      if (classDescriptor != null) {
        if (ErrorUtils.isError(classDescriptor)) continue;

        if (classDescriptor.getKind() != ClassKind.INTERFACE) {
          if (supertypeOwner.getKind() == ClassKind.ENUM_CLASS) {
            trace.report(CLASS_IN_SUPERTYPE_FOR_ENUM.on(typeReference));
            addSupertype = false;
          } else if (supertypeOwner.getKind() == ClassKind.INTERFACE
              && !classAppeared
              && !TypesPackage.isDynamic(supertype) /* avoid duplicate diagnostics */) {
            trace.report(TRAIT_WITH_SUPERCLASS.on(typeReference));
            addSupertype = false;
          }

          if (classAppeared) {
            trace.report(MANY_CLASSES_IN_SUPERTYPE_LIST.on(typeReference));
          } else {
            classAppeared = true;
          }
        }
      } else {
        trace.report(SUPERTYPE_NOT_A_CLASS_OR_TRAIT.on(typeReference));
      }

      TypeConstructor constructor = supertype.getConstructor();
      if (addSupertype && !typeConstructors.add(constructor)) {
        trace.report(SUPERTYPE_APPEARS_TWICE.on(typeReference));
      }

      if (DescriptorUtils.isSingleton(classDescriptor)) {
        trace.report(SINGLETON_IN_SUPERTYPE.on(typeReference));
      } else if (constructor.isFinal() && !allowedFinalSupertypes.contains(constructor)) {
        if (classDescriptor.getModality() == Modality.SEALED) {
          DeclarationDescriptor containingDescriptor = supertypeOwner.getContainingDeclaration();
          while (containingDescriptor != null && containingDescriptor != classDescriptor) {
            containingDescriptor = containingDescriptor.getContainingDeclaration();
          }
          if (containingDescriptor == null) {
            trace.report(SEALED_SUPERTYPE.on(typeReference));
          } else {
            trace.report(SEALED_SUPERTYPE_IN_LOCAL_CLASS.on(typeReference));
          }
        } else {
          trace.report(FINAL_SUPERTYPE.on(typeReference));
        }
      }
    }
  }