use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class TopLevelFIF method getReferenceToOnlyTypeParameter.
private static JsExpression getReferenceToOnlyTypeParameter(@NotNull CallInfo callInfo, @NotNull TranslationContext context) {
ResolvedCall<? extends CallableDescriptor> resolvedCall = callInfo.getResolvedCall();
Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
assert typeArguments.size() == 1;
KotlinType type = typeArguments.values().iterator().next();
return UtilsKt.getReferenceToJsClass(type, context);
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class BinaryOperationTranslator method translateEquals.
private JsExpression translateEquals() {
JsExpression left = Translation.translateAsExpression(leftKtExpression, context());
JsExpression right = Translation.translateAsExpression(rightKtExpression, context());
if (left == JsLiteral.NULL || right == JsLiteral.NULL) {
JsBinaryOperator operator = operationToken == KtTokens.EXCLEQ ? JsBinaryOperator.NEQ : JsBinaryOperator.EQ;
return new JsBinaryOperation(operator, left, right);
}
KotlinType leftType = context().bindingContext().getType(leftKtExpression);
KotlinType rightType = context().bindingContext().getType(rightKtExpression);
if (leftType != null && TypeUtils.isNullableType(leftType) || rightType != null && TypeUtils.isNullableType(rightType)) {
return mayBeWrapWithNegation(TopLevelFIF.KOTLIN_EQUALS.apply(left, Collections.singletonList(right), context()));
}
return translateAsOverloadedBinaryOperation();
}
use of org.jetbrains.kotlin.types.KotlinType in project kotlin by JetBrains.
the class StaticContext method buildQualifiedExpression.
@NotNull
private JsExpression buildQualifiedExpression(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) descriptor;
KotlinType type = classDescriptor.getDefaultType();
if (KotlinBuiltIns.isAny(classDescriptor)) {
return pureFqn("Object", null);
} else if (KotlinBuiltIns.isInt(type) || KotlinBuiltIns.isShort(type) || KotlinBuiltIns.isByte(type) || KotlinBuiltIns.isFloat(type) || KotlinBuiltIns.isDouble(type)) {
return pureFqn("Number", null);
} else if (KotlinBuiltIns.isLong(type)) {
return pureFqn("Long", Namer.kotlinObject());
} else if (KotlinBuiltIns.isChar(type)) {
return pureFqn("BoxedChar", Namer.kotlinObject());
} else if (KotlinBuiltIns.isString(type)) {
return pureFqn("String", null);
} else if (KotlinBuiltIns.isBoolean(type)) {
return pureFqn("Boolean", null);
} else if (KotlinBuiltIns.isArrayOrPrimitiveArray(classDescriptor)) {
return pureFqn("Array", null);
} else if (FunctionTypesKt.isBuiltinFunctionalType(type)) {
return pureFqn("Function", null);
} else if (TypeUtilsKt.isThrowable(classDescriptor.getDefaultType())) {
return pureFqn("Error", null);
}
}
SuggestedName suggested = nameSuggestion.suggest(descriptor);
if (suggested == null) {
ModuleDescriptor module = DescriptorUtils.getContainingModule(descriptor);
JsExpression result = getModuleExpressionFor(module);
return result != null ? result : pureFqn(Namer.getRootPackageName(), null);
}
if (config.getModuleKind() != ModuleKind.PLAIN) {
String moduleName = AnnotationsUtils.getModuleName(suggested.getDescriptor());
if (moduleName != null) {
return JsAstUtils.pureFqn(getImportedModule(moduleName, suggested.getDescriptor()).internalName, null);
}
}
JsExpression expression;
List<JsName> partNames = getActualNameFromSuggested(suggested);
if (isLibraryObject(suggested.getDescriptor())) {
expression = Namer.kotlinObject();
} else // Don't generate qualifier for local declarations
if (isNativeObject(suggested.getDescriptor()) && !isNativeObject(suggested.getScope()) || suggested.getDescriptor() instanceof CallableDescriptor && suggested.getScope() instanceof FunctionDescriptor) {
expression = null;
} else {
expression = getQualifiedExpression(suggested.getScope());
}
if (isNativeObject(suggested.getDescriptor()) && DescriptorUtils.isTopLevelDeclaration(suggested.getDescriptor())) {
String fileModuleName = AnnotationsUtils.getFileModuleName(getBindingContext(), suggested.getDescriptor());
if (fileModuleName != null) {
JsName moduleJsName = getImportedModule(fileModuleName, null).internalName;
expression = pureFqn(moduleJsName, expression);
}
String qualifier = AnnotationsUtils.getFileQualifier(getBindingContext(), suggested.getDescriptor());
if (qualifier != null) {
for (String qualifierPart : StringUtil.split(qualifier, ".")) {
expression = pureFqn(qualifierPart, expression);
}
}
}
for (JsName partName : partNames) {
expression = new JsNameRef(partName, expression);
applySideEffects(expression, suggested.getDescriptor());
}
assert expression != null : "Since partNames is not empty, expression must be non-null";
return expression;
}
use of org.jetbrains.kotlin.types.KotlinType 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.types.KotlinType in project kotlin by JetBrains.
the class TypeReconstructionUtil method reconstructBareType.
@NotNull
public static KotlinType reconstructBareType(@NotNull KtTypeReference right, @NotNull PossiblyBareType possiblyBareTarget, @Nullable KotlinType subjectType, @NotNull BindingTrace trace, @NotNull KotlinBuiltIns builtIns) {
if (subjectType == null) {
// Recovery: let's reconstruct as if we were casting from Any, to get some type there
subjectType = builtIns.getAnyType();
}
TypeReconstructionResult reconstructionResult = possiblyBareTarget.reconstruct(subjectType);
if (!reconstructionResult.isAllArgumentsInferred()) {
TypeConstructor typeConstructor = possiblyBareTarget.getBareTypeConstructor();
trace.report(NO_TYPE_ARGUMENTS_ON_RHS.on(right, typeConstructor.getParameters().size(), allStarProjectionsString(typeConstructor)));
}
KotlinType targetType = reconstructionResult.getResultingType();
if (targetType != null) {
if (possiblyBareTarget.isBare()) {
trace.record(BindingContext.TYPE, right, targetType);
}
return targetType;
}
return ErrorUtils.createErrorType("Failed to reconstruct type: " + right.getText());
}
Aggregations