JS: support internal visibility from friend modules
Friend modules should be provided using the -Xfriend-modules flag in the same format as -libraries. No manual configuration required for JPS, Gradle and Maven plugins. Friend modules could be switched off using the -Xfriend-modules-disabled flag. Doing that will * prevent internal declarations from being exported, * values provided by -Xfriend-modules ignored, * raise a compilation error on attemps to use internal declarations from other modules Fixes #KT-15135 and #KT-16568.
This commit is contained in:
@@ -39,10 +39,11 @@ object TopDownAnalyzerFacadeForJS {
|
||||
val context = ContextForNewModule(
|
||||
ProjectContext(config.project), Name.special("<${config.moduleId}>"), JsPlatform.builtIns, null
|
||||
)
|
||||
context.setDependencies(
|
||||
context.module.setDependencies(
|
||||
listOf(context.module) +
|
||||
config.moduleDescriptors.map { it.data } +
|
||||
listOf(JsPlatform.builtIns.builtInsModule)
|
||||
listOf(JsPlatform.builtIns.builtInsModule),
|
||||
config.friendModuleDescriptors.map { it.data }.toSet()
|
||||
)
|
||||
val trace = BindingTraceContext()
|
||||
trace.record(MODULE_KIND, context.module, config.moduleKind)
|
||||
|
||||
@@ -45,4 +45,10 @@ public class JSConfigurationKeys {
|
||||
CompilerConfigurationKey.create("fallback metadata");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> SERIALIZE_FRAGMENTS = CompilerConfigurationKey.create("serialize fragments");
|
||||
|
||||
public static final CompilerConfigurationKey<Boolean> FRIEND_PATHS_DISABLED =
|
||||
CompilerConfigurationKey.create("disable support for friend paths");
|
||||
|
||||
public static final CompilerConfigurationKey<List<String>> FRIEND_PATHS =
|
||||
CompilerConfigurationKey.create("friend module paths");
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ import com.intellij.util.SmartList;
|
||||
import com.intellij.util.io.URLUtil;
|
||||
import kotlin.Unit;
|
||||
import kotlin.collections.CollectionsKt;
|
||||
import kotlin.jvm.functions.Function1;
|
||||
import kotlin.jvm.functions.Function2;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.kotlin.config.*;
|
||||
@@ -63,10 +63,14 @@ public class JsConfig {
|
||||
private final LockBasedStorageManager storageManager = new LockBasedStorageManager();
|
||||
|
||||
private final List<KotlinJavascriptMetadata> metadata = new SmartList<>();
|
||||
private final List<KotlinJavascriptMetadata> friends = new SmartList<>();
|
||||
|
||||
@Nullable
|
||||
private List<JsModuleDescriptor<ModuleDescriptorImpl>> moduleDescriptors = null;
|
||||
|
||||
@Nullable
|
||||
private List<JsModuleDescriptor<ModuleDescriptorImpl>> friendModuleDescriptors = null;
|
||||
|
||||
private boolean initialized = false;
|
||||
|
||||
public JsConfig(@NotNull Project project, @NotNull CompilerConfiguration configuration) {
|
||||
@@ -99,6 +103,13 @@ public class JsConfig {
|
||||
return getConfiguration().getList(JSConfigurationKeys.LIBRARIES);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<String> getFriends() {
|
||||
if (getConfiguration().getBoolean(JSConfigurationKeys.FRIEND_PATHS_DISABLED)) return Collections.emptyList();
|
||||
return getConfiguration().getList(JSConfigurationKeys.FRIEND_PATHS);
|
||||
}
|
||||
|
||||
|
||||
public static abstract class Reporter {
|
||||
public void error(@NotNull String message) { /*Do nothing*/ }
|
||||
|
||||
@@ -109,8 +120,15 @@ public class JsConfig {
|
||||
return checkLibFilesAndReportErrors(report, null);
|
||||
}
|
||||
|
||||
private boolean checkLibFilesAndReportErrors(@NotNull JsConfig.Reporter report, @Nullable Function1<VirtualFile, Unit> action) {
|
||||
List<String> libraries = getLibraries();
|
||||
private boolean checkLibFilesAndReportErrors(@NotNull JsConfig.Reporter report, @Nullable Function2<VirtualFile, String, Unit> action) {
|
||||
return checkLibFilesAndReportErrors(getLibraries(), report, action);
|
||||
}
|
||||
|
||||
private boolean checkLibFilesAndReportErrors(
|
||||
@NotNull Collection<String> libraries,
|
||||
@NotNull JsConfig.Reporter report,
|
||||
@Nullable Function2<VirtualFile, String, Unit> action
|
||||
) {
|
||||
if (libraries.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
@@ -163,7 +181,7 @@ public class JsConfig {
|
||||
}
|
||||
|
||||
if (action != null) {
|
||||
action.invoke(file);
|
||||
action.invoke(file, path);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,49 +211,85 @@ public class JsConfig {
|
||||
return moduleDescriptors;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public List<JsModuleDescriptor<ModuleDescriptorImpl>> getFriendModuleDescriptors() {
|
||||
init();
|
||||
if (friendModuleDescriptors != null) return friendModuleDescriptors;
|
||||
|
||||
friendModuleDescriptors = new SmartList<>();
|
||||
for (KotlinJavascriptMetadata metadataEntry : friends) {
|
||||
JsModuleDescriptor<ModuleDescriptorImpl> descriptor = createModuleDescriptor(metadataEntry);
|
||||
friendModuleDescriptors.add(descriptor);
|
||||
}
|
||||
|
||||
friendModuleDescriptors = Collections.unmodifiableList(friendModuleDescriptors);
|
||||
|
||||
return friendModuleDescriptors;
|
||||
}
|
||||
|
||||
private void init() {
|
||||
if (initialized) return;
|
||||
|
||||
if (!getLibraries().isEmpty()) {
|
||||
Function1<VirtualFile, Unit> action = file -> {
|
||||
String libraryPath = PathUtil.getLocalPath(file);
|
||||
assert libraryPath != null : "libraryPath for " + file + " should not be null";
|
||||
metadata.addAll(KotlinJavascriptMetadataUtils.loadMetadata(libraryPath));
|
||||
|
||||
return Unit.INSTANCE;
|
||||
};
|
||||
|
||||
boolean hasErrors = checkLibFilesAndReportErrors(new Reporter() {
|
||||
JsConfig.Reporter reporter = new Reporter() {
|
||||
@Override
|
||||
public void error(@NotNull String message) {
|
||||
throw new IllegalStateException(message);
|
||||
}
|
||||
}, action);
|
||||
};
|
||||
|
||||
boolean hasErrors = checkLibFilesAndReportErrors(getFriends(), reporter, (file, path) -> {
|
||||
List<KotlinJavascriptMetadata> metaList = loadMetadata(file, "friendPath");
|
||||
metadata.addAll(metaList);
|
||||
friends.addAll(metaList);
|
||||
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
|
||||
|
||||
hasErrors |= checkLibFilesAndReportErrors(CollectionsKt.subtract(getLibraries(), getFriends()), reporter, (file, path) -> {
|
||||
metadata.addAll(loadMetadata(file, "libraryPath"));
|
||||
|
||||
|
||||
return Unit.INSTANCE;
|
||||
});
|
||||
|
||||
assert !hasErrors : "hasErrors should be false";
|
||||
}
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static List<KotlinJavascriptMetadata> loadMetadata(@NotNull VirtualFile file, @NotNull String name) {
|
||||
String libraryPath = PathUtil.getLocalPath(file);
|
||||
assert libraryPath != null : name + " for " + file + " should not be null";
|
||||
return KotlinJavascriptMetadataUtils.loadMetadata(libraryPath);
|
||||
}
|
||||
|
||||
private final IdentityHashMap<KotlinJavascriptMetadata, JsModuleDescriptor<ModuleDescriptorImpl>> factoryMap = new IdentityHashMap<>();
|
||||
|
||||
private JsModuleDescriptor<ModuleDescriptorImpl> createModuleDescriptor(KotlinJavascriptMetadata metadata) {
|
||||
LanguageVersionSettings languageVersionSettings = CommonConfigurationKeysKt.getLanguageVersionSettings(configuration);
|
||||
assert metadata.getVersion().isCompatible() ||
|
||||
languageVersionSettings.isFlagEnabled(AnalysisFlags.getSkipMetadataVersionCheck()) :
|
||||
"Expected JS metadata version " + JsMetadataVersion.INSTANCE + ", but actual metadata version is " + metadata.getVersion();
|
||||
return factoryMap.computeIfAbsent(metadata, m -> {
|
||||
LanguageVersionSettings languageVersionSettings = CommonConfigurationKeysKt.getLanguageVersionSettings(configuration);
|
||||
assert m.getVersion().isCompatible() ||
|
||||
languageVersionSettings.isFlagEnabled(AnalysisFlags.getSkipMetadataVersionCheck()) :
|
||||
"Expected JS metadata version " + JsMetadataVersion.INSTANCE + ", but actual metadata version is " + m.getVersion();
|
||||
|
||||
ModuleDescriptorImpl moduleDescriptor = new ModuleDescriptorImpl(
|
||||
Name.special("<" + metadata.getModuleName() + ">"), storageManager, JsPlatform.INSTANCE.getBuiltIns()
|
||||
);
|
||||
ModuleDescriptorImpl moduleDescriptor = new ModuleDescriptorImpl(
|
||||
Name.special("<" + m.getModuleName() + ">"), storageManager, JsPlatform.INSTANCE.getBuiltIns()
|
||||
);
|
||||
|
||||
JsModuleDescriptor<PackageFragmentProvider> rawDescriptor = KotlinJavascriptSerializationUtil.readModule(
|
||||
metadata.getBody(), storageManager, moduleDescriptor,
|
||||
new CompilerDeserializationConfiguration(languageVersionSettings)
|
||||
);
|
||||
JsModuleDescriptor<PackageFragmentProvider> rawDescriptor = KotlinJavascriptSerializationUtil.readModule(
|
||||
m.getBody(), storageManager, moduleDescriptor,
|
||||
new CompilerDeserializationConfiguration(languageVersionSettings)
|
||||
);
|
||||
|
||||
PackageFragmentProvider provider = rawDescriptor.getData();
|
||||
moduleDescriptor.initialize(provider != null ? provider : PackageFragmentProvider.Empty.INSTANCE);
|
||||
PackageFragmentProvider provider = rawDescriptor.getData();
|
||||
moduleDescriptor.initialize(provider != null ? provider : PackageFragmentProvider.Empty.INSTANCE);
|
||||
|
||||
return rawDescriptor.copy(moduleDescriptor);
|
||||
return rawDescriptor.copy(moduleDescriptor);
|
||||
});
|
||||
}
|
||||
|
||||
private static void setDependencies(ModuleDescriptorImpl module, List<ModuleDescriptorImpl> modules) {
|
||||
|
||||
@@ -273,38 +273,40 @@ class NameSuggestion {
|
||||
}
|
||||
|
||||
fun mangledAndStable() = NameAndStability(getStableMangledName(baseName, encodeSignature(descriptor)), true)
|
||||
fun mangledInternal() = NameAndStability(getInternalMangledName(baseName, encodeSignature(descriptor)), true)
|
||||
fun mangledPrivate() = NameAndStability(getPrivateMangledName(baseName, descriptor), false)
|
||||
|
||||
val effectiveVisibility = descriptor.ownEffectiveVisibility
|
||||
|
||||
val containingDeclaration = descriptor.containingDeclaration
|
||||
return when (containingDeclaration) {
|
||||
is PackageFragmentDescriptor -> if (effectiveVisibility.isPublicAPI) mangledAndStable() else regularAndUnstable()
|
||||
is ClassDescriptor -> {
|
||||
is PackageFragmentDescriptor -> when {
|
||||
effectiveVisibility.isPublicAPI -> mangledAndStable()
|
||||
|
||||
effectiveVisibility == Visibilities.INTERNAL -> mangledInternal()
|
||||
|
||||
else -> regularAndUnstable()
|
||||
}
|
||||
is ClassDescriptor -> when {
|
||||
// valueOf() is created in the library with a mangled name for every enum class
|
||||
if (descriptor is FunctionDescriptor && descriptor.isEnumValueOfMethod()) return mangledAndStable()
|
||||
descriptor is FunctionDescriptor && descriptor.isEnumValueOfMethod() -> mangledAndStable()
|
||||
|
||||
// Make all public declarations stable
|
||||
if (effectiveVisibility == Visibilities.PUBLIC) {
|
||||
return mangledAndStable()
|
||||
}
|
||||
effectiveVisibility == Visibilities.PUBLIC -> mangledAndStable()
|
||||
|
||||
if (descriptor is CallableMemberDescriptor && descriptor.isOverridableOrOverrides) return mangledAndStable()
|
||||
descriptor is CallableMemberDescriptor && descriptor.isOverridableOrOverrides -> mangledAndStable()
|
||||
|
||||
// Make all protected declarations of non-final public classes stable
|
||||
if (effectiveVisibility == Visibilities.PROTECTED &&
|
||||
effectiveVisibility == Visibilities.PROTECTED &&
|
||||
!containingDeclaration.isFinalClass &&
|
||||
containingDeclaration.visibility.isPublicAPI
|
||||
) {
|
||||
return mangledAndStable()
|
||||
}
|
||||
containingDeclaration.visibility.isPublicAPI -> mangledAndStable()
|
||||
|
||||
effectiveVisibility == Visibilities.INTERNAL -> mangledInternal()
|
||||
|
||||
// Mangle (but make unstable) all non-public API of public classes
|
||||
if (containingDeclaration.visibility.isPublicAPI && !containingDeclaration.isFinalClass) {
|
||||
return mangledPrivate()
|
||||
}
|
||||
containingDeclaration.visibility.isPublicAPI && !containingDeclaration.isFinalClass -> mangledPrivate()
|
||||
|
||||
regularAndUnstable()
|
||||
else -> regularAndUnstable()
|
||||
}
|
||||
else -> {
|
||||
assert(containingDeclaration is CallableMemberDescriptor) {
|
||||
@@ -323,6 +325,11 @@ class NameSuggestion {
|
||||
return getStableMangledName(baseName, ownerName + ":" + encodeSignature(descriptor))
|
||||
}
|
||||
|
||||
fun getInternalMangledName(suggestedName: String, forCalculateId: String): String {
|
||||
val suffix = "_${mangledId("internal:" + forCalculateId)}\$"
|
||||
return suggestedName + suffix
|
||||
}
|
||||
|
||||
@JvmStatic fun getStableMangledName(suggestedName: String, forCalculateId: String): String {
|
||||
val suffix = if (forCalculateId.isEmpty()) "" else "_${mangledId(forCalculateId)}\$"
|
||||
return suggestedName + suffix
|
||||
|
||||
Reference in New Issue
Block a user