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;
}
};
}
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) + ")");
}
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);
}
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);
}
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);
}
Aggregations