use of org.jetbrains.kotlin.js.naming.SuggestedName in project kotlin by JetBrains.
the class Namer method getStableMangledNameForDescriptor.
// TODO: get rid of this function
@NotNull
public static String getStableMangledNameForDescriptor(@NotNull ClassDescriptor descriptor, @NotNull String functionName) {
Collection<SimpleFunctionDescriptor> functions = descriptor.getDefaultType().getMemberScope().getContributedFunctions(Name.identifier(functionName), NoLookupLocation.FROM_BACKEND);
assert functions.size() == 1 : "Can't select a single function: " + functionName + " in " + descriptor;
SuggestedName suggested = new NameSuggestion().suggest(functions.iterator().next());
assert suggested != null : "Suggested name for class members is always non-null: " + functions.iterator().next();
return suggested.getNames().get(0);
}
use of org.jetbrains.kotlin.js.naming.SuggestedName in project kotlin by JetBrains.
the class StaticContext method getNameForDescriptor.
@NotNull
public JsName getNameForDescriptor(@NotNull DeclarationDescriptor descriptor) {
if (descriptor instanceof ClassDescriptor && KotlinBuiltIns.isAny((ClassDescriptor) descriptor)) {
JsName result = rootScope.declareName("Object");
MetadataProperties.setDescriptor(result, descriptor);
return result;
}
SuggestedName suggested = nameSuggestion.suggest(descriptor);
if (suggested == null) {
throw new IllegalArgumentException("Can't generate name for root declarations: " + descriptor);
}
return getActualNameFromSuggested(suggested).get(0);
}
use of org.jetbrains.kotlin.js.naming.SuggestedName in project kotlin by JetBrains.
the class Namer method getFunctionTag.
@NotNull
public static String getFunctionTag(@NotNull CallableDescriptor functionDescriptor) {
functionDescriptor = (CallableDescriptor) JsDescriptorUtils.findRealInlineDeclaration(functionDescriptor);
String moduleName = getModuleName(functionDescriptor);
FqNameUnsafe fqNameParent = DescriptorUtils.getFqName(functionDescriptor).parent();
String qualifier = null;
if (!fqNameParent.isRoot()) {
qualifier = fqNameParent.asString();
}
SuggestedName suggestedName = new NameSuggestion().suggest(functionDescriptor);
assert suggestedName != null : "Suggested name can be null only for module descriptors: " + functionDescriptor;
String mangledName = suggestedName.getNames().get(0);
return StringUtil.join(Arrays.asList(moduleName, qualifier, mangledName), ".");
}
use of org.jetbrains.kotlin.js.naming.SuggestedName 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.js.naming.SuggestedName in project kotlin by JetBrains.
the class StaticContext method getNameForBackingField.
@NotNull
public JsName getNameForBackingField(@NotNull PropertyDescriptor property) {
JsName name = backingFieldNameCache.get(property);
if (name == null) {
SuggestedName fqn = nameSuggestion.suggest(property);
assert fqn != null : "Properties are non-root declarations: " + property;
assert fqn.getNames().size() == 1 : "Private names must always consist of exactly one name";
JsScope scope = getScopeForDescriptor(fqn.getScope());
String baseName = NameSuggestion.getPrivateMangledName(fqn.getNames().get(0), property) + "_0";
name = scope.declareFreshName(baseName);
backingFieldNameCache.put(property, name);
}
return name;
}
Aggregations