Search in sources :

Example 16 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class KotlinCallableParameterTableModel method createRowItem.

@Override
protected ParameterTableModelItemBase<KotlinParameterInfo> createRowItem(@Nullable KotlinParameterInfo parameterInfo) {
    if (parameterInfo == null) {
        parameterInfo = new KotlinParameterInfo(methodDescriptor.getBaseDescriptor(), -1, "", new KotlinTypeInfo(false, null, null), null, null, KotlinValVar.None, null);
    }
    KtPsiFactory psiFactory = KtPsiFactoryKt.KtPsiFactory(project);
    PsiCodeFragment paramTypeCodeFragment = psiFactory.createTypeCodeFragment(KotlinTypeInfoKt.render(parameterInfo.getCurrentTypeInfo()), myTypeContext);
    KtExpression defaultValueForCall = parameterInfo.getDefaultValueForCall();
    PsiCodeFragment defaultValueCodeFragment = psiFactory.createExpressionCodeFragment(defaultValueForCall != null ? defaultValueForCall.getText() : "", myDefaultValueContext);
    return new ParameterTableModelItemBase<KotlinParameterInfo>(parameterInfo, paramTypeCodeFragment, defaultValueCodeFragment) {

        @Override
        public boolean isEllipsisType() {
            return false;
        }
    };
}
Also used : KtPsiFactory(org.jetbrains.kotlin.psi.KtPsiFactory) PsiCodeFragment(com.intellij.psi.PsiCodeFragment) KtExpression(org.jetbrains.kotlin.psi.KtExpression) ParameterTableModelItemBase(com.intellij.refactoring.changeSignature.ParameterTableModelItemBase)

Example 17 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class ExpectedResolveDataUtil method standardFunction.

@NotNull
private static FunctionDescriptor standardFunction(ClassDescriptor classDescriptor, String name, Project project, KotlinType... parameterTypes) {
    ModuleDescriptorImpl emptyModule = KotlinTestUtils.createEmptyModule();
    ContainerForTests container = InjectionKt.createContainerForTests(project, emptyModule);
    emptyModule.setDependencies(emptyModule);
    emptyModule.initialize(PackageFragmentProvider.Empty.INSTANCE);
    LexicalScopeImpl lexicalScope = new LexicalScopeImpl(ImportingScope.Empty.INSTANCE, classDescriptor, false, classDescriptor.getThisAsReceiverParameter(), LexicalScopeKind.SYNTHETIC);
    ExpressionTypingContext context = ExpressionTypingContext.newContext(new BindingTraceContext(), lexicalScope, DataFlowInfoFactory.EMPTY, TypeUtils.NO_EXPECTED_TYPE);
    KtExpression callElement = KtPsiFactory(project).createExpression(name);
    TemporaryBindingTrace traceWithFakeArgumentInfo = TemporaryBindingTrace.create(context.trace, "trace to store fake argument for", name);
    List<KtExpression> fakeArguments = new ArrayList<KtExpression>(parameterTypes.length);
    for (KotlinType type : parameterTypes) {
        fakeArguments.add(ExpressionTypingUtils.createFakeExpressionOfType(project, traceWithFakeArgumentInfo, "fakeArgument" + fakeArguments.size(), type));
    }
    OverloadResolutionResults<FunctionDescriptor> functions = container.getFakeCallResolver().resolveFakeCall(context, null, Name.identifier(name), callElement, callElement, FakeCallKind.OTHER, fakeArguments);
    for (ResolvedCall<? extends FunctionDescriptor> resolvedCall : functions.getResultingCalls()) {
        List<ValueParameterDescriptor> unsubstitutedValueParameters = resolvedCall.getResultingDescriptor().getValueParameters();
        for (int i = 0, unsubstitutedValueParametersSize = unsubstitutedValueParameters.size(); i < unsubstitutedValueParametersSize; i++) {
            ValueParameterDescriptor unsubstitutedValueParameter = unsubstitutedValueParameters.get(i);
            if (unsubstitutedValueParameter.getType().equals(parameterTypes[i])) {
                return resolvedCall.getResultingDescriptor();
            }
        }
    }
    throw new IllegalArgumentException("Not found: kotlin::" + classDescriptor.getName() + "." + name + "(" + Arrays.toString(parameterTypes) + ")");
}
Also used : LexicalScopeImpl(org.jetbrains.kotlin.resolve.scopes.LexicalScopeImpl) KotlinType(org.jetbrains.kotlin.types.KotlinType) ContainerForTests(org.jetbrains.kotlin.tests.di.ContainerForTests) ModuleDescriptorImpl(org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl) ExpressionTypingContext(org.jetbrains.kotlin.types.expressions.ExpressionTypingContext) KtExpression(org.jetbrains.kotlin.psi.KtExpression) Assert.assertNotNull(org.junit.Assert.assertNotNull) NotNull(org.jetbrains.annotations.NotNull)

Example 18 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class KotlinExpressionSurrounder method isApplicable.

@Override
public boolean isApplicable(@NotNull PsiElement[] elements) {
    if (elements.length != 1 || !(elements[0] instanceof KtExpression)) {
        return false;
    }
    KtExpression expression = (KtExpression) elements[0];
    if (expression instanceof KtCallExpression && expression.getParent() instanceof KtQualifiedExpression) {
        return false;
    }
    KotlinType type = ResolutionUtils.analyze(expression, BodyResolveMode.PARTIAL).getType(expression);
    if (type == null || isUnit(type)) {
        return false;
    }
    return isApplicable(expression);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) KtCallExpression(org.jetbrains.kotlin.psi.KtCallExpression) KtQualifiedExpression(org.jetbrains.kotlin.psi.KtQualifiedExpression)

Example 19 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class ForLoopConventionsChecker method checkIterableConvention.

@Nullable
public KotlinType checkIterableConvention(@NotNull ExpressionReceiver loopRange, @NotNull ExpressionTypingContext context) {
    KtExpression loopRangeExpression = loopRange.getExpression();
    // Make a fake call loopRange.iterator(), and try to resolve it
    OverloadResolutionResults<FunctionDescriptor> iteratorResolutionResults = fakeCallResolver.resolveFakeCall(context, loopRange, OperatorNameConventions.ITERATOR, loopRangeExpression, loopRangeExpression, FakeCallKind.ITERATOR, Collections.<KtExpression>emptyList());
    if (!iteratorResolutionResults.isSuccess())
        return null;
    ResolvedCall<FunctionDescriptor> iteratorResolvedCall = iteratorResolutionResults.getResultingCall();
    context.trace.record(LOOP_RANGE_ITERATOR_RESOLVED_CALL, loopRangeExpression, iteratorResolvedCall);
    FunctionDescriptor iteratorFunction = iteratorResolvedCall.getResultingDescriptor();
    checkIfOperatorModifierPresent(loopRangeExpression, iteratorFunction, context.trace);
    KotlinType iteratorType = iteratorFunction.getReturnType();
    //noinspection ConstantConditions
    KotlinType hasNextType = checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.HAS_NEXT, HAS_NEXT_FUNCTION_AMBIGUITY, HAS_NEXT_MISSING, HAS_NEXT_FUNCTION_NONE_APPLICABLE, LOOP_RANGE_HAS_NEXT_RESOLVED_CALL);
    if (hasNextType != null && !builtIns.isBooleanOrSubtype(hasNextType)) {
        context.trace.report(HAS_NEXT_FUNCTION_TYPE_MISMATCH.on(loopRangeExpression, hasNextType));
    }
    return checkConventionForIterator(context, loopRangeExpression, iteratorType, OperatorNameConventions.NEXT, NEXT_AMBIGUITY, NEXT_MISSING, NEXT_NONE_APPLICABLE, LOOP_RANGE_NEXT_RESOLVED_CALL);
}
Also used : KotlinType(org.jetbrains.kotlin.types.KotlinType) KtExpression(org.jetbrains.kotlin.psi.KtExpression) FunctionDescriptor(org.jetbrains.kotlin.descriptors.FunctionDescriptor) Nullable(org.jetbrains.annotations.Nullable)

Example 20 with KtExpression

use of org.jetbrains.kotlin.psi.KtExpression in project kotlin by JetBrains.

the class CompileTimeConstantUtils method canBeReducedToBooleanConstant.

public static boolean canBeReducedToBooleanConstant(@Nullable KtExpression expression, @NotNull BindingContext context, @Nullable Boolean expectedValue) {
    KtExpression effectiveExpression = KtPsiUtil.deparenthesize(expression);
    if (effectiveExpression == null)
        return false;
    CompileTimeConstant<?> compileTimeConstant = ConstantExpressionEvaluator.getConstant(effectiveExpression, context);
    if (!(compileTimeConstant instanceof TypedCompileTimeConstant) || compileTimeConstant.getUsesVariableAsConstant())
        return false;
    ConstantValue constantValue = ((TypedCompileTimeConstant) compileTimeConstant).getConstantValue();
    if (!(constantValue instanceof BooleanValue))
        return false;
    Boolean value = ((BooleanValue) constantValue).getValue();
    return expectedValue == null || expectedValue.equals(value);
}
Also used : BooleanValue(org.jetbrains.kotlin.resolve.constants.BooleanValue) KtExpression(org.jetbrains.kotlin.psi.KtExpression) TypedCompileTimeConstant(org.jetbrains.kotlin.resolve.constants.TypedCompileTimeConstant) ConstantValue(org.jetbrains.kotlin.resolve.constants.ConstantValue)

Aggregations

KtExpression (org.jetbrains.kotlin.psi.KtExpression)28 NotNull (org.jetbrains.annotations.NotNull)13 JsExpression (org.jetbrains.kotlin.js.backend.ast.JsExpression)6 KotlinType (org.jetbrains.kotlin.types.KotlinType)5 Nullable (org.jetbrains.annotations.Nullable)4 ValueArgument (org.jetbrains.kotlin.psi.ValueArgument)4 Project (com.intellij.openapi.project.Project)3 TextRange (com.intellij.openapi.util.TextRange)3 ValueParameterDescriptor (org.jetbrains.kotlin.descriptors.ValueParameterDescriptor)3 HashMap (java.util.HashMap)2 FunctionDescriptor (org.jetbrains.kotlin.descriptors.FunctionDescriptor)2 KtElement (org.jetbrains.kotlin.psi.KtElement)2 KtParenthesizedExpression (org.jetbrains.kotlin.psi.KtParenthesizedExpression)2 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)2 ExpressionValueArgument (org.jetbrains.kotlin.resolve.calls.model.ExpressionValueArgument)2 ResolvedValueArgument (org.jetbrains.kotlin.resolve.calls.model.ResolvedValueArgument)2 Type (org.jetbrains.org.objectweb.asm.Type)2 PsiCodeFragment (com.intellij.psi.PsiCodeFragment)1 IElementType (com.intellij.psi.tree.IElementType)1 ParameterTableModelItemBase (com.intellij.refactoring.changeSignature.ParameterTableModelItemBase)1