コード例 #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);
  }
コード例 #2
0
ファイル: AsmUtil.java プロジェクト: bashor/kotlin
  public static void genNotNullAssertionsForParameters(
      @NotNull InstructionAdapter v,
      @NotNull GenerationState state,
      @NotNull FunctionDescriptor descriptor,
      @NotNull FrameMap frameMap) {
    if (!state.isGenerateNotNullParamAssertions()) return;

    // Private method is not accessible from other classes, no assertions needed
    if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return;

    for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) {
      JetType type = parameter.getReturnType();
      if (type == null || isNullableType(type)) continue;

      int index = frameMap.getIndex(parameter);
      Type asmType = state.getTypeMapper().mapReturnType(type);
      if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) {
        v.load(index, asmType);
        v.visitLdcInsn(descriptor.getName().asString());
        v.invokestatic(
            "jet/runtime/Intrinsics",
            "checkParameterIsNotNull",
            "(Ljava/lang/Object;Ljava/lang/String;)V");
      }
    }
  }
コード例 #3
0
      @Override
      public StackValue innerValue(
          DeclarationDescriptor d,
          LocalLookup localLookup,
          GenerationState state,
          MutableClosure closure,
          Type classType) {
        FunctionDescriptor vd = (FunctionDescriptor) d;

        boolean idx = localLookup != null && localLookup.lookupLocal(vd);
        if (!idx) return null;

        BindingContext bindingContext = state.getBindingContext();
        Type localType = asmTypeForAnonymousClass(bindingContext, vd);

        MutableClosure localFunClosure =
            bindingContext.get(CLOSURE, bindingContext.get(CLASS_FOR_FUNCTION, vd));
        if (localFunClosure != null && JvmCodegenUtil.isConst(localFunClosure)) {
          // This is an optimization: we can obtain an instance of a const closure simply by
          // GETSTATIC ...$instance
          // (instead of passing this instance to the constructor and storing as a field)
          return StackValue.field(localType, localType, JvmAbi.INSTANCE_FIELD, true);
        }

        String fieldName = "$" + vd.getName();
        StackValue innerValue = StackValue.field(localType, classType, fieldName, false);

        closure.recordField(fieldName, localType);
        closure.captureVariable(new EnclosedValueDescriptor(fieldName, d, innerValue, localType));

        return innerValue;
      }
コード例 #4
0
ファイル: AsmUtil.java プロジェクト: bashor/kotlin
  public static int getMethodAsmFlags(FunctionDescriptor functionDescriptor, OwnerKind kind) {
    int flags = getCommonCallableFlags(functionDescriptor);

    if (functionDescriptor.getModality() == Modality.FINAL
        && !(functionDescriptor instanceof ConstructorDescriptor)) {
      DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
      if (!(containingDeclaration instanceof ClassDescriptor)
          || ((ClassDescriptor) containingDeclaration).getKind() != ClassKind.TRAIT) {
        flags |= ACC_FINAL;
      }
    }

    if (isStaticMethod(kind, functionDescriptor)) {
      flags |= ACC_STATIC;
    }

    if (isAbstractMethod(functionDescriptor, kind)) {
      flags |= ACC_ABSTRACT;
    }

    if (JetTypeMapper.isAccessor(functionDescriptor)) {
      flags |= ACC_SYNTHETIC;
    }

    return flags;
  }
コード例 #5
0
  @NotNull
  public Collection<JetType> getSupertypesForClosure(@NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    if (receiverParameter != null) {
      classDescriptor = extensionFunctionImpl;
      typeArguments.add(new TypeProjectionImpl(receiverParameter.getType()));
    } else {
      classDescriptor = functionImpl;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType functionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType functionType =
        KotlinBuiltIns.getInstance()
            .getFunctionType(
                Annotations.EMPTY,
                receiverParameter == null ? null : receiverParameter.getType(),
                DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
                descriptor.getReturnType());

    return Arrays.asList(functionImplType, functionType);
  }
コード例 #6
0
 public static boolean isLocalNamedFun(DeclarationDescriptor fd) {
   if (fd instanceof FunctionDescriptor) {
     FunctionDescriptor descriptor = (FunctionDescriptor) fd;
     return descriptor.getVisibility() == Visibilities.LOCAL && !descriptor.getName().isSpecial();
   }
   return false;
 }
コード例 #7
0
 @NotNull
 public static JetScope getFunctionInnerScope(
     @NotNull JetScope outerScope,
     @NotNull FunctionDescriptor descriptor,
     @NotNull BindingTrace trace) {
   WritableScope parameterScope =
       new WritableScopeImpl(
           outerScope,
           descriptor,
           new TraceBasedRedeclarationHandler(trace),
           "Function inner scope");
   ReceiverParameterDescriptor receiver = descriptor.getReceiverParameter();
   if (receiver != null) {
     parameterScope.setImplicitReceiver(receiver);
   }
   for (TypeParameterDescriptor typeParameter : descriptor.getTypeParameters()) {
     parameterScope.addTypeParameterDescriptor(typeParameter);
   }
   for (ValueParameterDescriptor valueParameterDescriptor : descriptor.getValueParameters()) {
     parameterScope.addVariableDescriptor(valueParameterDescriptor);
   }
   parameterScope.addLabeledDeclaration(descriptor);
   parameterScope.changeLockLevel(WritableScope.LockLevel.READING);
   return parameterScope;
 }
コード例 #8
0
  @Nullable
  private static InsertHandler<LookupElement> getInsertHandler(
      @NotNull DeclarationDescriptor descriptor) {
    if (descriptor instanceof FunctionDescriptor) {
      FunctionDescriptor functionDescriptor = (FunctionDescriptor) descriptor;

      if (functionDescriptor.getValueParameters().isEmpty()) {
        return EMPTY_FUNCTION_HANDLER;
      }

      if (functionDescriptor.getValueParameters().size() == 1
          && KotlinBuiltIns.getInstance()
              .isFunctionOrExtensionFunctionType(
                  functionDescriptor.getValueParameters().get(0).getType())) {
        return PARAMS_BRACES_FUNCTION_HANDLER;
      }

      return PARAMS_PARENTHESIS_FUNCTION_HANDLER;
    }

    if (descriptor instanceof ClassDescriptor) {
      return JetClassInsertHandler.INSTANCE;
    }

    return null;
  }
コード例 #9
0
  @NotNull
  private static Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>>
      getSuperclassToFunctionsMultimap(
          @NotNull PsiMethodWrapper method,
          @NotNull BindingContext bindingContext,
          @NotNull ClassDescriptor containingClass) {
    Multimap<FqName, Pair<FunctionDescriptor, PsiMethod>> result = HashMultimap.create();

    Name functionName = Name.identifier(method.getName());
    int parameterCount = method.getParameters().size();

    for (JetType supertype : TypeUtils.getAllSupertypes(containingClass.getDefaultType())) {
      ClassifierDescriptor klass = supertype.getConstructor().getDeclarationDescriptor();
      assert klass != null;
      FqName fqName = DescriptorUtils.getFQName(klass).toSafe();

      for (FunctionDescriptor fun :
          klass.getDefaultType().getMemberScope().getFunctions(functionName)) {
        if (fun.getKind().isReal() && fun.getValueParameters().size() == parameterCount) {
          PsiElement declaration = BindingContextUtils.descriptorToDeclaration(bindingContext, fun);
          if (declaration instanceof PsiMethod) {
            result.put(fqName, Pair.create(fun, (PsiMethod) declaration));
          } // else declaration is null or JetNamedFunction: both cases are processed later
        }
      }
    }
    return result;
  }
コード例 #10
0
  private ClassDescriptor recordClassForFunction(FunctionDescriptor funDescriptor) {
    ClassDescriptor classDescriptor;
    int arity = funDescriptor.getValueParameters().size();

    classDescriptor =
        new ClassDescriptorImpl(
            funDescriptor.getContainingDeclaration(),
            Collections.<AnnotationDescriptor>emptyList(),
            Modality.FINAL,
            Name.special("<closure>"));
    ((ClassDescriptorImpl) classDescriptor)
        .initialize(
            false,
            Collections.<TypeParameterDescriptor>emptyList(),
            Collections.singleton(
                (funDescriptor.getReceiverParameter().exists()
                        ? JetStandardClasses.getReceiverFunction(arity)
                        : JetStandardClasses.getFunction(arity))
                    .getDefaultType()),
            JetScope.EMPTY,
            Collections.<ConstructorDescriptor>emptySet(),
            null);

    assert PsiCodegenPredictor.checkPredictedClassNameForFun(
        bindingContext, funDescriptor, classDescriptor);
    bindingTrace.record(CLASS_FOR_FUNCTION, funDescriptor, classDescriptor);
    return classDescriptor;
  }
コード例 #11
0
  @NotNull
  public JetTypeInfo getSimpleNameExpressionTypeInfo(
      @NotNull JetSimpleNameExpression nameExpression,
      @NotNull ReceiverValue receiver,
      @Nullable ASTNode callOperationNode,
      @NotNull ResolutionContext context) {
    boolean[] result = new boolean[1];

    TemporaryBindingTrace traceForVariable =
        TemporaryBindingTrace.create(context.trace, "trace to resolve as variable", nameExpression);
    JetType type =
        getVariableType(
            nameExpression,
            receiver,
            callOperationNode,
            context.replaceBindingTrace(traceForVariable),
            result);
    if (result[0]) {
      traceForVariable.commit();
      if (type instanceof NamespaceType && context.expressionPosition == ExpressionPosition.FREE) {
        type = null;
      }
      return JetTypeInfo.create(type, context.dataFlowInfo);
    }

    Call call =
        CallMaker.makeCall(
            nameExpression,
            receiver,
            callOperationNode,
            nameExpression,
            Collections.<ValueArgument>emptyList());
    TemporaryBindingTrace traceForFunction =
        TemporaryBindingTrace.create(context.trace, "trace to resolve as function", nameExpression);
    ResolvedCall<FunctionDescriptor> resolvedCall =
        getResolvedCallForFunction(
            call,
            nameExpression,
            receiver,
            context,
            ResolveMode.TOP_LEVEL_CALL,
            ResolutionResultsCache.create(),
            result);
    if (result[0]) {
      FunctionDescriptor functionDescriptor =
          resolvedCall != null ? resolvedCall.getResultingDescriptor() : null;
      traceForFunction.commit();
      boolean hasValueParameters =
          functionDescriptor == null || functionDescriptor.getValueParameters().size() > 0;
      context.trace.report(
          FUNCTION_CALL_EXPECTED.on(nameExpression, nameExpression, hasValueParameters));
      type = functionDescriptor != null ? functionDescriptor.getReturnType() : null;
      return JetTypeInfo.create(type, context.dataFlowInfo);
    }

    traceForVariable.commit();
    return JetTypeInfo.create(null, context.dataFlowInfo);
  }
コード例 #12
0
 protected void renderValueParameters(FunctionDescriptor descriptor, StringBuilder builder) {
   if (descriptor.getValueParameters().isEmpty()) {
     renderEmptyValueParameters(builder);
   }
   for (Iterator<ValueParameterDescriptor> iterator = descriptor.getValueParameters().iterator();
       iterator.hasNext(); ) {
     renderValueParameter(iterator.next(), !iterator.hasNext(), builder);
   }
 }
コード例 #13
0
  @NotNull
  private VarargCheckResult checkVarargInSuperFunctions(
      @NotNull ValueParameterDescriptor originalParam) {
    boolean someSupersVararg = false;
    boolean someSupersNotVararg = false;
    for (FunctionDescriptor superFunction : superFunctions) {
      if (superFunction.getValueParameters().get(originalParam.getIndex()).getVarargElementType()
          != null) {
        someSupersVararg = true;
      } else {
        someSupersNotVararg = true;
      }
    }

    JetType originalVarargElementType = originalParam.getVarargElementType();
    JetType originalType = originalParam.getType();

    if (someSupersVararg && someSupersNotVararg) {
      reportError("Incompatible super methods: some have vararg parameter, some have not");
      return new VarargCheckResult(originalType, originalVarargElementType != null);
    }

    KotlinBuiltIns builtIns = KotlinBuiltIns.getInstance();
    if (someSupersVararg && originalVarargElementType == null) {
      // convert to vararg

      assert isArrayType(originalType);

      if (builtIns.isPrimitiveArray(originalType)) {
        // replace IntArray? with IntArray
        return new VarargCheckResult(TypeUtils.makeNotNullable(originalType), true);
      }

      // replace Array<out Foo>? with Array<Foo>
      JetType varargElementType = builtIns.getArrayElementType(originalType);
      return new VarargCheckResult(builtIns.getArrayType(INVARIANT, varargElementType), true);
    } else if (someSupersNotVararg && originalVarargElementType != null) {
      // convert to non-vararg

      assert isArrayType(originalType);

      if (builtIns.isPrimitiveArray(originalType)) {
        // replace IntArray with IntArray?
        return new VarargCheckResult(TypeUtils.makeNullable(originalType), false);
      }

      // replace Array<Foo> with Array<out Foo>?
      return new VarargCheckResult(
          TypeUtils.makeNullable(
              builtIns.getArrayType(Variance.OUT_VARIANCE, originalVarargElementType)),
          false);
    }

    return new VarargCheckResult(originalType, originalVarargElementType != null);
  }
コード例 #14
0
ファイル: AsmUtil.java プロジェクト: bashor/kotlin
 private static int getVarargsFlag(FunctionDescriptor functionDescriptor) {
   if (!functionDescriptor.getValueParameters().isEmpty()
       && functionDescriptor
               .getValueParameters()
               .get(functionDescriptor.getValueParameters().size() - 1)
               .getVarargElementType()
           != null) {
     return ACC_VARARGS;
   }
   return 0;
 }
コード例 #15
0
 private static String renderNamedFunction(FunctionDescriptor descriptor) {
   String name = descriptor.getName().asString();
   String paramTypes =
       StringUtil.join(
           descriptor.getValueParameters(),
           new Function<ValueParameterDescriptor, String>() {
             @Override
             public String fun(ValueParameterDescriptor descriptor) {
               return DescriptorRenderer.SHORT_NAMES_IN_TYPES.renderType(descriptor.getType());
             }
           },
           ", ");
   return name + "(" + paramTypes + ")";
 }
コード例 #16
0
 @NotNull
 private static JetType getFunctionTypeForSamType(@NotNull JetType samType) {
   FunctionDescriptor function = getAbstractMethodOfSamType(samType);
   JetType returnType = function.getReturnType();
   assert returnType != null : "function is not initialized: " + function;
   List<JetType> parameterTypes = Lists.newArrayList();
   for (ValueParameterDescriptor parameter : function.getValueParameters()) {
     parameterTypes.add(parameter.getType());
   }
   JetType functionType =
       KotlinBuiltIns.getInstance()
           .getFunctionType(
               Collections.<AnnotationDescriptor>emptyList(), null, parameterTypes, returnType);
   return TypeUtils.makeNullableAsSpecified(functionType, samType.isNullable());
 }
コード例 #17
0
  @NotNull
  private static FunctionDescriptor substituteSuperFunction(
      @NotNull Map<ClassDescriptor, JetType> superclassToSupertype,
      @NotNull FunctionDescriptor superFun) {
    DeclarationDescriptor superFunContainer = superFun.getContainingDeclaration();
    assert superFunContainer instanceof ClassDescriptor : superFunContainer;

    JetType supertype = superclassToSupertype.get(superFunContainer);
    assert supertype != null : "Couldn't find super type for super function: " + superFun;
    TypeSubstitutor supertypeSubstitutor = TypeSubstitutor.create(supertype);

    FunctionDescriptor substitutedSuperFun = superFun.substitute(supertypeSubstitutor);
    assert substitutedSuperFun != null;
    return substitutedSuperFun;
  }
コード例 #18
0
  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;
  }
コード例 #19
0
 @Nullable
 public static List<ValueParameterDescriptor> getSubstitutedValueParameters(
     FunctionDescriptor substitutedDescriptor,
     @NotNull FunctionDescriptor functionDescriptor,
     @NotNull TypeSubstitutor substitutor) {
   List<ValueParameterDescriptor> result = new ArrayList<ValueParameterDescriptor>();
   List<ValueParameterDescriptor> unsubstitutedValueParameters =
       functionDescriptor.getValueParameters();
   for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size();
       i < unsubstitutedValueParametersSize;
       i++) {
     ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
     // TODO : Lazy?
     JetType substitutedType =
         substitutor.substitute(unsubstitutedValueParameter.getType(), Variance.IN_VARIANCE);
     JetType varargElementType = unsubstitutedValueParameter.getVarargElementType();
     JetType substituteVarargElementType =
         varargElementType == null
             ? null
             : substitutor.substitute(varargElementType, Variance.IN_VARIANCE);
     if (substitutedType == null) return null;
     result.add(
         new ValueParameterDescriptorImpl(
             substitutedDescriptor,
             unsubstitutedValueParameter,
             unsubstitutedValueParameter.getAnnotations(),
             unsubstitutedValueParameter.isVar(),
             substitutedType,
             substituteVarargElementType));
   }
   return result;
 }
コード例 #20
0
 @Nullable
 public static FunctionDescriptor substituteFunctionDescriptor(
     @NotNull List<JetType> typeArguments, @NotNull FunctionDescriptor functionDescriptor) {
   Map<TypeConstructor, TypeProjection> substitutionContext =
       createSubstitutionContext(functionDescriptor, typeArguments);
   return functionDescriptor.substitute(TypeSubstitutor.create(substitutionContext));
 }
コード例 #21
0
  @NotNull
  public Collection<JetType> getSupertypesForCallableReference(
      @NotNull FunctionDescriptor descriptor) {
    ReceiverParameterDescriptor receiverParameter = descriptor.getReceiverParameter();
    ReceiverParameterDescriptor expectedThisObject = descriptor.getExpectedThisObject();

    List<TypeProjection> typeArguments = new ArrayList<TypeProjection>(2);

    ClassDescriptor classDescriptor;
    JetType receiverType;
    if (receiverParameter != null) {
      classDescriptor = kExtensionFunctionImpl;
      receiverType = receiverParameter.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else if (expectedThisObject != null) {
      classDescriptor = kMemberFunctionImpl;
      receiverType = expectedThisObject.getType();
      typeArguments.add(new TypeProjectionImpl(receiverType));
    } else {
      classDescriptor = kFunctionImpl;
      receiverType = null;
    }

    //noinspection ConstantConditions
    typeArguments.add(new TypeProjectionImpl(descriptor.getReturnType()));

    JetType kFunctionImplType =
        new JetTypeImpl(
            classDescriptor.getDefaultType().getAnnotations(),
            classDescriptor.getTypeConstructor(),
            false,
            typeArguments,
            classDescriptor.getMemberScope(typeArguments));

    JetType kFunctionType =
        reflectionTypes.getKFunctionType(
            Annotations.EMPTY,
            receiverType,
            DescriptorUtils.getValueParametersTypes(descriptor.getValueParameters()),
            descriptor.getReturnType(),
            receiverParameter != null);

    return Arrays.asList(kFunctionImplType, kFunctionType);
  }
コード例 #22
0
 private void renderValueParameters(
     @NotNull FunctionDescriptor function, @NotNull StringBuilder builder) {
   handler.appendBeforeValueParameters(function, builder);
   for (ValueParameterDescriptor parameter : function.getValueParameters()) {
     handler.appendBeforeValueParameter(parameter, builder);
     renderValueParameter(parameter, builder, false);
     handler.appendAfterValueParameter(parameter, builder);
   }
   handler.appendAfterValueParameters(function, builder);
 }
コード例 #23
0
    @Override
    public Void visitFunctionDescriptor(FunctionDescriptor descriptor, StringBuilder builder) {
      renderVisibility(descriptor.getVisibility(), builder);
      renderModality(descriptor.getModality(), builder);
      builder.append(renderKeyword("fun")).append(" ");
      if (renderTypeParameters(descriptor.getTypeParameters(), builder)) {
        builder.append(" ");
      }

      ReceiverDescriptor receiver = descriptor.getReceiverParameter();
      if (receiver.exists()) {
        builder.append(escape(renderType(receiver.getType()))).append(".");
      }

      renderName(descriptor, builder);
      renderValueParameters(descriptor, builder);
      builder.append(" : ").append(escape(renderType(descriptor.getReturnType())));
      renderWhereSuffix(descriptor, builder);
      return null;
    }
コード例 #24
0
 @NotNull
 private List<ValueParameterDescriptor> copyValueParameters(
     @NotNull FunctionDescriptor descriptor) {
   List<ValueParameterDescriptor> valueParameters = descriptor.getValueParameters();
   List<ValueParameterDescriptor> result =
       new ArrayList<ValueParameterDescriptor>(valueParameters.size());
   for (ValueParameterDescriptor valueParameter : valueParameters) {
     result.add(valueParameter.copy(this, valueParameter.getName()));
   }
   return result;
 }
コード例 #25
0
ファイル: ArrayIterator.java プロジェクト: djcsdy/kotlin
 @Override
 public StackValue generate(
     ExpressionCodegen codegen,
     InstructionAdapter v,
     @NotNull Type expectedType,
     PsiElement element,
     List<JetExpression> arguments,
     StackValue receiver,
     @NotNull GenerationState state) {
   receiver.put(AsmTypeConstants.OBJECT_TYPE, v);
   JetCallExpression call = (JetCallExpression) element;
   FunctionDescriptor funDescriptor =
       (FunctionDescriptor)
           codegen
               .getBindingContext()
               .get(
                   BindingContext.REFERENCE_TARGET,
                   (JetSimpleNameExpression) call.getCalleeExpression());
   assert funDescriptor != null;
   ClassDescriptor containingDeclaration =
       (ClassDescriptor) funDescriptor.getContainingDeclaration().getOriginal();
   if (containingDeclaration.equals(JetStandardLibrary.getInstance().getArray())) {
     v.invokestatic(
         "jet/runtime/ArrayIterator", "iterator", "([Ljava/lang/Object;)Ljava/util/Iterator;");
     return StackValue.onStack(AsmTypeConstants.JET_ITERATOR_TYPE);
   } else {
     for (JvmPrimitiveType jvmPrimitiveType : JvmPrimitiveType.values()) {
       PrimitiveType primitiveType = jvmPrimitiveType.getPrimitiveType();
       if (primitiveType.getArrayClassName().is(containingDeclaration)) {
         String methodSignature =
             "(["
                 + jvmPrimitiveType.getJvmLetter()
                 + ")"
                 + jvmPrimitiveType.getIterator().getDescriptor();
         v.invokestatic("jet/runtime/ArrayIterator", "iterator", methodSignature);
         return StackValue.onStack(jvmPrimitiveType.getIterator().getAsmType());
       }
     }
     throw new UnsupportedOperationException(containingDeclaration.toString());
   }
 }
コード例 #26
0
ファイル: JsDescriptorUtils.java プロジェクト: hhariri/kotlin
 @Nullable
 public static FunctionDescriptor getOverriddenDescriptor(
     @NotNull FunctionDescriptor functionDescriptor) {
   Set<? extends FunctionDescriptor> overriddenDescriptors =
       functionDescriptor.getOverriddenDescriptors();
   if (overriddenDescriptors.isEmpty()) {
     return null;
   } else {
     // TODO: for now translator can't deal with multiple inheritance good enough
     return overriddenDescriptors.iterator().next();
   }
 }
コード例 #27
0
  private List<TypeParameterDescriptor> modifyTypeParametersAccordingToSuperMethods(
      List<TypeParameterDescriptor> autoTypeParameters) {
    List<TypeParameterDescriptor> result = Lists.newArrayList();

    for (TypeParameterDescriptor autoParameter : autoTypeParameters) {
      int index = autoParameter.getIndex();
      TypeParameterDescriptorImpl modifiedTypeParameter =
          autoTypeParameterToModified.get(autoParameter);

      List<Iterator<JetType>> upperBoundFromSuperFunctionsIterators = Lists.newArrayList();
      for (FunctionDescriptor superFunction : superFunctions) {
        upperBoundFromSuperFunctionsIterators.add(
            superFunction.getTypeParameters().get(index).getUpperBounds().iterator());
      }

      for (JetType autoUpperBound : autoParameter.getUpperBounds()) {
        List<TypeAndVariance> upperBoundsFromSuperFunctions = Lists.newArrayList();

        for (Iterator<JetType> iterator : upperBoundFromSuperFunctionsIterators) {
          assert iterator.hasNext();
          upperBoundsFromSuperFunctions.add(new TypeAndVariance(iterator.next(), INVARIANT));
        }

        JetType modifiedUpperBound =
            modifyTypeAccordingToSuperMethods(
                autoUpperBound, upperBoundsFromSuperFunctions, UPPER_BOUND);
        modifiedTypeParameter.addUpperBound(modifiedUpperBound);
      }

      for (Iterator<JetType> iterator : upperBoundFromSuperFunctionsIterators) {
        assert !iterator.hasNext();
      }

      modifiedTypeParameter.setInitialized();
      result.add(modifiedTypeParameter);
    }

    return result;
  }
コード例 #28
0
  @Override
  public void visitNamedFunction(JetNamedFunction function) {
    FunctionDescriptor functionDescriptor =
        (FunctionDescriptor) bindingContext.get(DECLARATION_TO_DESCRIPTOR, function);
    // working around a problem with shallow analysis
    if (functionDescriptor == null) return;
    DeclarationDescriptor containingDeclaration = functionDescriptor.getContainingDeclaration();
    if (containingDeclaration instanceof ClassDescriptor) {
      nameStack.push(peekFromStack(nameStack) + '$' + function.getName());
      super.visitNamedFunction(function);
      nameStack.pop();
    } else if (containingDeclaration instanceof NamespaceDescriptor) {
      String peek = peekFromStack(nameStack);
      if (peek.isEmpty()) {
        peek = JvmAbi.PACKAGE_CLASS;
      } else {
        peek += "/" + JvmAbi.PACKAGE_CLASS;
      }
      nameStack.push(peek + '$' + function.getName());
      super.visitNamedFunction(function);
      nameStack.pop();
    } else {
      String name = inventAnonymousClassName(function);
      ClassDescriptor classDescriptor = recordClassForFunction(functionDescriptor);
      recordClosure(
          bindingTrace,
          function,
          classDescriptor,
          peekFromStack(classStack),
          JvmClassName.byInternalName(name),
          true);

      classStack.push(classDescriptor);
      nameStack.push(name);
      super.visitNamedFunction(function);
      nameStack.pop();
      classStack.pop();
    }
  }
コード例 #29
0
ファイル: ErrorUtils.java プロジェクト: iamsteveholmes/kotlin
  public static boolean containsErrorType(@NotNull FunctionDescriptor function) {
    if (containsErrorType(function.getReturnType())) {
      return true;
    }
    ReceiverParameterDescriptor receiverParameter = function.getReceiverParameter();
    if (receiverParameter != null && containsErrorType(receiverParameter.getType())) {
      return true;
    }
    for (ValueParameterDescriptor parameter : function.getValueParameters()) {
      if (containsErrorType(parameter.getType())) {
        return true;
      }
    }
    for (TypeParameterDescriptor parameter : function.getTypeParameters()) {
      for (JetType upperBound : parameter.getUpperBounds()) {
        if (containsErrorType(upperBound)) {
          return true;
        }
      }
    }

    return false;
  }
コード例 #30
0
ファイル: BodyResolver.java プロジェクト: raph-amiard/kotlin
  private void resolveFunctionBody(
      @NotNull BindingTrace trace,
      @NotNull JetDeclarationWithBody function,
      @NotNull FunctionDescriptor functionDescriptor,
      @NotNull JetScope declaringScope) {
    if (!context.completeAnalysisNeeded(function)) return;

    JetExpression bodyExpression = function.getBodyExpression();
    JetScope functionInnerScope =
        FunctionDescriptorUtil.getFunctionInnerScope(declaringScope, functionDescriptor, trace);
    if (bodyExpression != null) {
      expressionTypingServices.checkFunctionReturnType(
          functionInnerScope, function, functionDescriptor, trace);
    }

    List<JetParameter> valueParameters = function.getValueParameters();
    List<ValueParameterDescriptor> valueParameterDescriptors =
        functionDescriptor.getValueParameters();

    checkDefaultParameterValues(valueParameters, valueParameterDescriptors, functionInnerScope);

    assert functionDescriptor.getReturnType() != null;
  }