KT-689 Allow to put Java and Kotlin files in the same packages

This commit is contained in:
Andrey Breslav
2011-12-06 14:43:10 +04:00
parent 47050a11d1
commit deb8d5ea20
27 changed files with 219 additions and 171 deletions
@@ -4,7 +4,7 @@ import com.intellij.psi.PsiElement;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.resolve.DescriptorRenderer; import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.objectweb.asm.AnnotationVisitor; import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Label; import org.objectweb.asm.Label;
import org.objectweb.asm.MethodVisitor; import org.objectweb.asm.MethodVisitor;
@@ -199,7 +199,7 @@ public class FunctionCodegen {
int flags = ACC_PUBLIC; // TODO. int flags = ACC_PUBLIC; // TODO.
String ownerInternalName = contextClass instanceof NamespaceDescriptor ? String ownerInternalName = contextClass instanceof NamespaceDescriptor ?
NamespaceCodegen.getJVMClassName(DescriptorRenderer.getFQName(contextClass)) : NamespaceCodegen.getJVMClassName(DescriptorUtils.getFQName(contextClass)) :
state.getTypeMapper().mapType(((ClassDescriptor) contextClass).getDefaultType(), OwnerKind.IMPLEMENTATION).getInternalName(); state.getTypeMapper().mapType(((ClassDescriptor) contextClass).getDefaultType(), OwnerKind.IMPLEMENTATION).getInternalName();
String descriptor = jvmSignature.getDescriptor().replace(")","I)"); String descriptor = jvmSignature.getDescriptor().replace(")","I)");
@@ -15,7 +15,7 @@ import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.jetbrains.jet.lang.types.JetStandardLibrary;
import java.util.Collections; import java.util.Collections;
@@ -84,7 +84,7 @@ public class GenerationState {
public void compile(JetFile psiFile) { public void compile(JetFile psiFile) {
final JetNamespace namespace = psiFile.getRootNamespace(); final JetNamespace namespace = psiFile.getRootNamespace();
final BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).analyzeNamespace(namespace, JetControlFlowDataTraceFactory.EMPTY); final BindingContext bindingContext = AnalyzerFacade.analyzeOneNamespaceWithJavaIntegration(namespace, JetControlFlowDataTraceFactory.EMPTY);
AnalyzingUtils.throwExceptionOnErrors(bindingContext); AnalyzingUtils.throwExceptionOnErrors(bindingContext);
compileCorrectNamespaces(bindingContext, Collections.singletonList(namespace)); compileCorrectNamespaces(bindingContext, Collections.singletonList(namespace));
// NamespaceCodegen codegen = forNamespace(namespace); // NamespaceCodegen codegen = forNamespace(namespace);
@@ -8,12 +8,12 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver; import org.jetbrains.jet.lang.resolve.java.JavaDescriptorResolver;
import org.jetbrains.jet.lang.resolve.java.JavaNamespaceDescriptor; import org.jetbrains.jet.lang.resolve.java.JavaNamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor; import org.jetbrains.jet.lang.resolve.scopes.receivers.ReceiverDescriptor;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.lexer.JetTokens; import org.jetbrains.jet.lexer.JetTokens;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import org.objectweb.asm.Opcodes; import org.objectweb.asm.Opcodes;
import org.objectweb.asm.Type; import org.objectweb.asm.Type;
import org.objectweb.asm.commons.Method; import org.objectweb.asm.commons.Method;
@@ -130,7 +130,7 @@ public class JetTypeMapper {
String owner; String owner;
DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration(); DeclarationDescriptor containingDeclaration = descriptor.getContainingDeclaration();
if (containingDeclaration instanceof NamespaceDescriptor) { if (containingDeclaration instanceof NamespaceDescriptor) {
owner = NamespaceCodegen.getJVMClassName(DescriptorRenderer.getFQName((NamespaceDescriptor) containingDeclaration)); owner = NamespaceCodegen.getJVMClassName(DescriptorUtils.getFQName((NamespaceDescriptor) containingDeclaration));
} }
else if (containingDeclaration instanceof ClassDescriptor) { else if (containingDeclaration instanceof ClassDescriptor) {
ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration; ClassDescriptor classDescriptor = (ClassDescriptor) containingDeclaration;
@@ -305,7 +305,7 @@ public class JetTypeMapper {
ClassDescriptor thisClass; ClassDescriptor thisClass;
if (functionParent instanceof NamespaceDescriptor) { if (functionParent instanceof NamespaceDescriptor) {
assert !superCall; assert !superCall;
owner = NamespaceCodegen.getJVMClassName(DescriptorRenderer.getFQName(functionParent)); owner = NamespaceCodegen.getJVMClassName(DescriptorUtils.getFQName(functionParent));
invokeOpcode = INVOKESTATIC; invokeOpcode = INVOKESTATIC;
thisClass = null; thisClass = null;
} }
@@ -11,9 +11,8 @@ import org.jetbrains.jet.codegen.GenerationState;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
import java.io.File; import java.io.File;
@@ -93,10 +92,10 @@ public class CompileSession {
} }
return false; return false;
} }
final AnalyzingUtils instance = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS);
List<JetNamespace> allNamespaces = new ArrayList<JetNamespace>(mySourceFileNamespaces); List<JetNamespace> allNamespaces = new ArrayList<JetNamespace>(mySourceFileNamespaces);
allNamespaces.addAll(myLibrarySourceFileNamespaces); allNamespaces.addAll(myLibrarySourceFileNamespaces);
myBindingContext = instance.analyzeNamespaces(myEnvironment.getProject(), allNamespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY); myBindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(
myEnvironment.getProject(), allNamespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
ErrorCollector errorCollector = new ErrorCollector(myBindingContext); ErrorCollector errorCollector = new ErrorCollector(myBindingContext);
errorCollector.report(out); errorCollector.report(out);
return !errorCollector.hasErrors; return !errorCollector.hasErrors;
@@ -1,7 +1,9 @@
package org.jetbrains.jet.lang.resolve.java; package org.jetbrains.jet.lang.resolve.java;
import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.intellij.openapi.progress.ProcessCanceledException; import com.intellij.openapi.progress.ProcessCanceledException;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Key; import com.intellij.openapi.util.Key;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import com.intellij.psi.util.CachedValue; import com.intellij.psi.util.CachedValue;
@@ -20,8 +22,10 @@ import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTraceContext; import org.jetbrains.jet.lang.resolve.BindingTraceContext;
import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import java.util.List;
/** /**
* @author abreslav * @author abreslav
@@ -34,22 +38,11 @@ public class AnalyzerFacade {
return Collections.<JetDeclaration>singleton(file.getRootNamespace()); return Collections.<JetDeclaration>singleton(file.getRootNamespace());
} }
}; };
private static final AnalyzingUtils ANALYZING_UTILS = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS);
private final static Key<CachedValue<BindingContext>> BINDING_CONTEXT = Key.create("BINDING_CONTEXT"); private final static Key<CachedValue<BindingContext>> BINDING_CONTEXT = Key.create("BINDING_CONTEXT");
private static final Object lock = new Object(); private static final Object lock = new Object();
public static BindingContext analyzeNamespace(@NotNull JetNamespace namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
return ANALYZING_UTILS.analyzeNamespace(namespace, flowDataTraceFactory);
}
public static BindingContext analyzeFileWithCache(@NotNull final JetFile file, @NotNull final Function<JetFile, Collection<JetDeclaration>> declarationProvider) { public static BindingContext analyzeFileWithCache(@NotNull final JetFile file, @NotNull final Function<JetFile, Collection<JetDeclaration>> declarationProvider) {
return analyzeFileWithCache(ANALYZING_UTILS, file, declarationProvider);
}
public static BindingContext analyzeFileWithCache(@NotNull final AnalyzingUtils analyzingUtils,
@NotNull final JetFile file,
@NotNull final Function<JetFile, Collection<JetDeclaration>> declarationProvider) {
// TODO : Synchronization? // TODO : Synchronization?
CachedValue<BindingContext> bindingContextCachedValue = file.getUserData(BINDING_CONTEXT); CachedValue<BindingContext> bindingContextCachedValue = file.getUserData(BINDING_CONTEXT);
if (bindingContextCachedValue == null) { if (bindingContextCachedValue == null) {
@@ -58,11 +51,7 @@ public class AnalyzerFacade {
public Result<BindingContext> compute() { public Result<BindingContext> compute() {
synchronized (lock) { synchronized (lock) {
try { try {
BindingContext bindingContext = analyzingUtils.analyzeNamespaces( BindingContext bindingContext = analyzeNamespacesWithJavaIntegration(file.getProject(), declarationProvider.fun(file), Predicates.<PsiFile>equalTo(file), JetControlFlowDataTraceFactory.EMPTY);
file.getProject(),
declarationProvider.fun(file),
Predicates.<PsiFile>equalTo(file),
JetControlFlowDataTraceFactory.EMPTY);
return new Result<BindingContext>(bindingContext, PsiModificationTracker.MODIFICATION_COUNT); return new Result<BindingContext>(bindingContext, PsiModificationTracker.MODIFICATION_COUNT);
} }
catch (ProcessCanceledException e) { catch (ProcessCanceledException e) {
@@ -84,4 +73,39 @@ public class AnalyzerFacade {
} }
return bindingContextCachedValue.getValue(); return bindingContextCachedValue.getValue();
} }
public static BindingContext analyzeOneNamespaceWithJavaIntegration(JetNamespace namespace, JetControlFlowDataTraceFactory flowDataTraceFactory) {
return analyzeNamespacesWithJavaIntegration(namespace.getProject(), Collections.singleton(namespace), Predicates.<PsiFile>alwaysTrue(), flowDataTraceFactory);
}
public static BindingContext analyzeNamespacesWithJavaIntegration(Project project, Collection<? extends JetDeclaration> declarations, Predicate<PsiFile> filesToAnalyzeCompletely, JetControlFlowDataTraceFactory flowDataTraceFactory) {
BindingTraceContext bindingTraceContext = new BindingTraceContext();
return AnalyzingUtils.getInstance().analyzeNamespacesWithGivenTrace(
project,
JavaBridgeConfiguration.createJavaBridgeConfiguration(project, bindingTraceContext),
declarations,
filesToAnalyzeCompletely,
flowDataTraceFactory, bindingTraceContext);
}
public static BindingContext shallowAnalyzeFiles(Collection<PsiFile> files) {
assert files.size() > 0;
Project project = files.iterator().next().getProject();
Collection<JetNamespace> namespaces = collectRootNamespaces(files);
return analyzeNamespacesWithJavaIntegration(project, namespaces, Predicates.<PsiFile>alwaysFalse(), JetControlFlowDataTraceFactory.EMPTY);
}
public static List<JetNamespace> collectRootNamespaces(Collection<PsiFile> files) {
List<JetNamespace> namespaces = new ArrayList<JetNamespace>();
for (PsiFile file : files) {
if (file instanceof JetFile) {
namespaces.add(((JetFile) file).getRootNamespace());
}
}
return namespaces;
}
} }
@@ -0,0 +1,37 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.project.Project;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
/**
* @author abreslav
*/
public class JavaBridgeConfiguration implements Configuration {
public static Configuration createJavaBridgeConfiguration(@NotNull Project project, @NotNull BindingTrace trace) {
return new JavaBridgeConfiguration(project, trace);
}
private final JavaSemanticServices javaSemanticServices;
private JavaBridgeConfiguration(Project project, BindingTrace trace) {
this.javaSemanticServices = new JavaSemanticServices(project, JetSemanticServices.createSemanticServices(project), trace);
}
@Override
public void addDefaultImports(BindingTrace trace, WritableScope rootScope) {
rootScope.importScope(new JavaPackageScope("", null, javaSemanticServices));
rootScope.importScope(new JavaPackageScope("java.lang", null, javaSemanticServices));
}
@Override
public void extendNamespaceScope(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
namespaceMemberScope.importScope(new JavaPackageScope(DescriptorUtils.getFQName(namespaceDescriptor), namespaceDescriptor, javaSemanticServices));
}
}
@@ -1,24 +0,0 @@
package org.jetbrains.jet.lang.resolve.java;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.ImportingStrategy;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
/**
* @author abreslav
*/
public class JavaDefaultImports {
public static final ImportingStrategy JAVA_DEFAULT_IMPORTS = new ImportingStrategy() {
@Override
public void addImports(Project project, JetSemanticServices semanticServices, BindingTrace trace, WritableScope rootScope) {
JavaSemanticServices javaSemanticServices = new JavaSemanticServices(project, semanticServices, trace);
rootScope.importScope(new JavaPackageScope("", null, javaSemanticServices));
rootScope.importScope(new JavaPackageScope("java.lang", null, javaSemanticServices));
}
};
private JavaDefaultImports() {
}
}
@@ -13,7 +13,6 @@ import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor; import org.jetbrains.jet.lang.descriptors.annotations.AnnotationDescriptor;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.DescriptorUtils; import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.types.*; import org.jetbrains.jet.lang.types.*;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
@@ -53,7 +52,7 @@ public class JavaDescriptorResolver {
} }
}; };
protected final Map<String, ClassDescriptor> classDescriptorCache = new HashMap<String, ClassDescriptor>(); protected final Map<String, ClassDescriptor> classDescriptorCache = Maps.newHashMap();
protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap(); protected final Map<PsiTypeParameter, TypeParameterDescriptor> typeParameterDescriptorCache = Maps.newHashMap();
protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap(); protected final Map<PsiMethod, FunctionDescriptor> methodDescriptorCache = Maps.newHashMap();
protected final Map<PsiField, VariableDescriptor> fieldDescriptorCache = Maps.newHashMap(); protected final Map<PsiField, VariableDescriptor> fieldDescriptorCache = Maps.newHashMap();
@@ -122,7 +121,7 @@ public class JavaDescriptorResolver {
psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(), psiClass.hasModifierProperty(PsiModifier.ABSTRACT) || psiClass.isInterface(),
!psiClass.hasModifierProperty(PsiModifier.FINAL)) !psiClass.hasModifierProperty(PsiModifier.FINAL))
); );
classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), psiClass)); classDescriptor.setVisibility(resolveVisibilityFromPsiModifiers(psiClass));
classDescriptorCache.put(psiClass.getQualifiedName(), classDescriptor); classDescriptorCache.put(psiClass.getQualifiedName(), classDescriptor);
classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false)); classDescriptor.setUnsubstitutedMemberScope(new JavaClassMembersScope(classDescriptor, psiClass, semanticServices, false));
@@ -163,7 +162,7 @@ public class JavaDescriptorResolver {
Collections.<AnnotationDescriptor>emptyList(), // TODO Collections.<AnnotationDescriptor>emptyList(), // TODO
false); false);
constructorDescriptor.initialize(typeParameters, resolveParameterDescriptors(constructorDescriptor, constructor.getParameterList().getParameters()), Modality.FINAL, constructorDescriptor.initialize(typeParameters, resolveParameterDescriptors(constructorDescriptor, constructor.getParameterList().getParameters()), Modality.FINAL,
resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), constructor)); resolveVisibilityFromPsiModifiers(constructor));
constructorDescriptor.setReturnType(classDescriptor.getDefaultType()); constructorDescriptor.setReturnType(classDescriptor.getDefaultType());
classDescriptor.addConstructor(constructorDescriptor); classDescriptor.addConstructor(constructorDescriptor);
semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, constructor, constructorDescriptor); semanticServices.getTrace().record(BindingContext.CONSTRUCTOR, constructor, constructorDescriptor);
@@ -368,7 +367,7 @@ public class JavaDescriptorResolver {
containingDeclaration, containingDeclaration,
Collections.<AnnotationDescriptor>emptyList(), Collections.<AnnotationDescriptor>emptyList(),
Modality.FINAL, Modality.FINAL,
resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), field), resolveVisibilityFromPsiModifiers(field),
!isFinal, !isFinal,
null, null,
DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration), DescriptorUtils.getExpectedThisObjectIfNeeded(containingDeclaration),
@@ -454,7 +453,7 @@ public class JavaDescriptorResolver {
semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters), semanticServices.getDescriptorResolver().resolveParameterDescriptors(functionDescriptorImpl, parameters),
semanticServices.getTypeTransformer().transformToType(returnType), semanticServices.getTypeTransformer().transformToType(returnType),
Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)), Modality.convertFromFlags(method.hasModifierProperty(PsiModifier.ABSTRACT), !method.hasModifierProperty(PsiModifier.FINAL)),
resolveVisibilityFromPsiModifiers(semanticServices.getTrace(), method) resolveVisibilityFromPsiModifiers(method)
); );
semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl); semanticServices.getTrace().record(BindingContext.FUNCTION, method, functionDescriptorImpl);
FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl; FunctionDescriptor substitutedFunctionDescriptor = functionDescriptorImpl;
@@ -464,7 +463,7 @@ public class JavaDescriptorResolver {
return substitutedFunctionDescriptor; return substitutedFunctionDescriptor;
} }
private static Visibility resolveVisibilityFromPsiModifiers(BindingTrace trace, PsiModifierListOwner modifierListOwner) { private static Visibility resolveVisibilityFromPsiModifiers(PsiModifierListOwner modifierListOwner) {
//TODO report error //TODO report error
return modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC) ? Visibility.PUBLIC : return modifierListOwner.hasModifierProperty(PsiModifier.PUBLIC) ? Visibility.PUBLIC :
(modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE) ? Visibility.PRIVATE : (modifierListOwner.hasModifierProperty(PsiModifier.PRIVATE) ? Visibility.PRIVATE :
@@ -0,0 +1,29 @@
package org.jetbrains.jet.lang;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.NamespaceDescriptor;
import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
/**
* @author abreslav
*/
public interface Configuration {
Configuration EMPTY = new Configuration() {
@Override
public void addDefaultImports(BindingTrace trace, WritableScope rootScope) {
}
@Override
public void extendNamespaceScope(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope) {
}
};
void addDefaultImports(BindingTrace trace, WritableScope rootScope);
/**
*
* This method is called every time a namespace descriptor is created. Use it to add extra descriptors to the namespace, e.g. merge a Java package with a Kotlin one
*/
void extendNamespaceScope(BindingTrace trace, @NotNull NamespaceDescriptor namespaceDescriptor, @NotNull WritableScope namespaceMemberScope);
}
@@ -1,7 +1,6 @@
package org.jetbrains.jet.lang.resolve; package org.jetbrains.jet.lang.resolve;
import com.google.common.base.Predicate; import com.google.common.base.Predicate;
import com.google.common.base.Predicates;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.intellij.openapi.project.Project; import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.TextRange;
@@ -10,6 +9,7 @@ import com.intellij.psi.PsiElementVisitor;
import com.intellij.psi.PsiErrorElement; import com.intellij.psi.PsiErrorElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -17,20 +17,24 @@ import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.DiagnosticHolder; import org.jetbrains.jet.lang.diagnostics.DiagnosticHolder;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
import org.jetbrains.jet.lang.psi.JetDeclaration; import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope; import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl; import org.jetbrains.jet.lang.resolve.scopes.WritableScopeImpl;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
/** /**
* @author abreslav * @author abreslav
*/ */
public class AnalyzingUtils { public class AnalyzingUtils {
public static AnalyzingUtils getInstance(@NotNull ImportingStrategy importingStrategy) {
return new AnalyzingUtils(importingStrategy); private static final AnalyzingUtils INSTANCE = new AnalyzingUtils();
public static AnalyzingUtils getInstance() {
return INSTANCE;
} }
public static void checkForSyntacticErrors(@NotNull PsiElement root) { public static void checkForSyntacticErrors(@NotNull PsiElement root) {
@@ -69,32 +73,26 @@ public class AnalyzingUtils {
} }
} }
private final ImportingStrategy importingStrategy; // --------------------------------------------------------------------------------------------------------------------------
private AnalyzingUtils(ImportingStrategy importingStrategy) {
this.importingStrategy = importingStrategy;
}
public BindingContext analyzeNamespace(@NotNull JetNamespace namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
Project project = namespace.getProject();
List<JetDeclaration> declarations = Collections.<JetDeclaration>singletonList(namespace);
return analyzeNamespaces(project, declarations, Predicates.equalTo(namespace.getContainingFile()), flowDataTraceFactory);
}
public BindingContext analyzeNamespaces( public BindingContext analyzeNamespaces(
@NotNull Project project, @NotNull Project project,
@NotNull Configuration configuration,
@NotNull Collection<? extends JetDeclaration> declarations, @NotNull Collection<? extends JetDeclaration> declarations,
@NotNull Predicate<PsiFile> filesToAnalyzeCompletely, @NotNull Predicate<PsiFile> filesToAnalyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
BindingTraceContext bindingTraceContext = new BindingTraceContext(); BindingTraceContext bindingTraceContext = new BindingTraceContext();
return analyzeNamespacesWithGivenTrace(project, configuration, declarations, filesToAnalyzeCompletely, flowDataTraceFactory, bindingTraceContext);
}
public BindingContext analyzeNamespacesWithGivenTrace(Project project, Configuration configuration, Collection<? extends JetDeclaration> declarations, Predicate<PsiFile> filesToAnalyzeCompletely, JetControlFlowDataTraceFactory flowDataTraceFactory, BindingTraceContext bindingTraceContext) {
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(project);
JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope(); JetScope libraryScope = semanticServices.getStandardLibrary().getLibraryScope();
ModuleDescriptor owner = new ModuleDescriptor("<module>"); ModuleDescriptor owner = new ModuleDescriptor("<module>");
final WritableScope scope = new WritableScopeImpl(JetScope.EMPTY, owner, new TraceBasedRedeclarationHandler(bindingTraceContext)).setDebugName("Root scope in analyzeNamespace"); final WritableScope scope = new WritableScopeImpl(JetScope.EMPTY, owner, new TraceBasedRedeclarationHandler(bindingTraceContext)).setDebugName("Root scope in analyzeNamespace");
importingStrategy.addImports(project, semanticServices, bindingTraceContext, scope); // configuration.addImports(project, semanticServices, bindingTraceContext, scope);
scope.importScope(libraryScope); scope.importScope(libraryScope);
scope.changeLockLevel(WritableScope.LockLevel.BOTH); scope.changeLockLevel(WritableScope.LockLevel.BOTH);
@@ -132,29 +130,8 @@ public class AnalyzingUtils {
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptor classObjectDescriptor) { public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptor classObjectDescriptor) {
throw new IllegalStateException("Must be guaranteed not to happen by the parser"); throw new IllegalStateException("Must be guaranteed not to happen by the parser");
} }
}, declarations, filesToAnalyzeCompletely, flowDataTraceFactory); }, declarations, filesToAnalyzeCompletely, flowDataTraceFactory, configuration);
return bindingTraceContext.getBindingContext(); return bindingTraceContext.getBindingContext();
} }
public BindingContext shallowAnalyzeFiles(Collection<PsiFile> files) {
assert files.size() > 0;
Project project = files.iterator().next().getProject();
Collection<JetNamespace> namespaces = collectRootNamespaces(files);
return analyzeNamespaces(project, namespaces, Predicates.<PsiFile>alwaysFalse(), JetControlFlowDataTraceFactory.EMPTY);
}
public static List<JetNamespace> collectRootNamespaces(Collection<PsiFile> files) {
List<JetNamespace> namespaces = new ArrayList<JetNamespace>();
for (PsiFile file : files) {
if (file instanceof JetFile) {
namespaces.add(((JetFile) file).getRootNamespace());
}
}
return namespaces;
}
} }
@@ -154,4 +154,14 @@ public class DescriptorUtils {
} }
return false; return false;
} }
public static String getFQName(DeclarationDescriptor descriptor) {
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container != null && !(container instanceof ModuleDescriptor)) {
String baseName = getFQName(container);
if (!baseName.isEmpty()) return baseName + "." + descriptor.getName();
}
return descriptor.getName();
}
} }
@@ -1,19 +0,0 @@
package org.jetbrains.jet.lang.resolve;
import com.intellij.openapi.project.Project;
import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.resolve.scopes.WritableScope;
/**
* @author abreslav
*/
public interface ImportingStrategy {
ImportingStrategy NONE = new ImportingStrategy() {
@Override
public void addImports(Project project, JetSemanticServices semanticServices, BindingTrace trace, WritableScope rootScope) {
}
};
void addImports(Project project, JetSemanticServices semanticServices, BindingTrace trace, WritableScope rootScope);
}
@@ -1,18 +1,13 @@
package org.jetbrains.jet.lang.resolve; package org.jetbrains.jet.lang.resolve;
import com.intellij.openapi.util.Pair; import com.intellij.openapi.util.Pair;
import com.intellij.psi.PsiElement;
import com.intellij.util.containers.MultiMap; import com.intellij.util.containers.MultiMap;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.diagnostics.Errors; import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.resolve.DescriptorRenderer;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import static org.jetbrains.jet.lang.resolve.BindingContext.DELEGATED; import static org.jetbrains.jet.lang.resolve.BindingContext.DELEGATED;
@@ -51,7 +46,7 @@ public class OverloadResolver {
} }
Key(NamespaceDescriptor namespaceDescriptor, String name) { Key(NamespaceDescriptor namespaceDescriptor, String name) {
this(DescriptorRenderer.getFQName(namespaceDescriptor), name); this(DescriptorUtils.getFQName(namespaceDescriptor), name);
} }
public String getNamespace() { public String getNamespace() {
@@ -6,6 +6,7 @@ import com.google.common.collect.Sets;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
@@ -23,31 +24,31 @@ import java.util.Set;
private final ObservableBindingTrace trace; private final ObservableBindingTrace trace;
private final JetSemanticServices semanticServices; private final JetSemanticServices semanticServices;
private final DescriptorResolver descriptorResolver; private final Configuration configuration;
private final DescriptorResolver descriptorResolver;
private final Map<JetClass, MutableClassDescriptor> classes = Maps.newLinkedHashMap(); private final Map<JetClass, MutableClassDescriptor> classes = Maps.newLinkedHashMap();
private final Map<JetObjectDeclaration, MutableClassDescriptor> objects = Maps.newLinkedHashMap(); private final Map<JetObjectDeclaration, MutableClassDescriptor> objects = Maps.newLinkedHashMap();
protected final Map<JetNamespace, WritableScope> namespaceScopes = Maps.newHashMap(); protected final Map<JetNamespace, WritableScope> namespaceScopes = Maps.newHashMap();
protected final Map<JetNamespace, NamespaceDescriptorImpl> namespaceDescriptors = Maps.newHashMap(); protected final Map<JetNamespace, NamespaceDescriptorImpl> namespaceDescriptors = Maps.newHashMap();
private final Map<JetDeclaration, JetScope> declaringScopes = Maps.newHashMap(); private final Map<JetDeclaration, JetScope> declaringScopes = Maps.newHashMap();
private final Map<JetNamedFunction, FunctionDescriptorImpl> functions = Maps.newLinkedHashMap(); private final Map<JetNamedFunction, FunctionDescriptorImpl> functions = Maps.newLinkedHashMap();
private final Map<JetSecondaryConstructor, ConstructorDescriptor> constructors = Maps.newLinkedHashMap(); private final Map<JetSecondaryConstructor, ConstructorDescriptor> constructors = Maps.newLinkedHashMap();
private final Map<JetProperty, PropertyDescriptor> properties = Maps.newLinkedHashMap(); private final Map<JetProperty, PropertyDescriptor> properties = Maps.newLinkedHashMap();
private final Set<PropertyDescriptor> primaryConstructorParameterProperties = Sets.newHashSet(); private final Set<PropertyDescriptor> primaryConstructorParameterProperties = Sets.newHashSet();
private final Predicate<PsiFile> analyzeCompletely; private final Predicate<PsiFile> analyzeCompletely;
private StringBuilder debugOutput;
private StringBuilder debugOutput;
private boolean analyzingBootstrapLibrary = false; private boolean analyzingBootstrapLibrary = false;
public TopDownAnalysisContext(JetSemanticServices semanticServices, BindingTrace trace, Predicate<PsiFile> analyzeCompletely) { public TopDownAnalysisContext(JetSemanticServices semanticServices, BindingTrace trace, Predicate<PsiFile> analyzeCompletely, @NotNull Configuration configuration) {
this.trace = new ObservableBindingTrace(trace); this.trace = new ObservableBindingTrace(trace);
this.semanticServices = semanticServices; this.semanticServices = semanticServices;
this.descriptorResolver = semanticServices.getClassDescriptorResolver(trace); this.descriptorResolver = semanticServices.getClassDescriptorResolver(trace);
this.analyzeCompletely = analyzeCompletely; this.analyzeCompletely = analyzeCompletely;
this.configuration = configuration;
} }
public void debug(Object message) { public void debug(Object message) {
@@ -133,4 +134,8 @@ import java.util.Set;
return functions; return functions;
} }
@NotNull
public Configuration getConfiguration() {
return configuration;
}
} }
@@ -4,6 +4,7 @@ import com.google.common.base.Predicate;
import com.google.common.base.Predicates; import com.google.common.base.Predicates;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -30,8 +31,10 @@ public class TopDownAnalyzer {
@NotNull NamespaceLike owner, @NotNull NamespaceLike owner,
@NotNull Collection<? extends JetDeclaration> declarations, @NotNull Collection<? extends JetDeclaration> declarations,
@NotNull Predicate<PsiFile> analyzeCompletely, @NotNull Predicate<PsiFile> analyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) { @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
process(semanticServices, trace, outerScope, owner, declarations, analyzeCompletely, flowDataTraceFactory, false); @NotNull Configuration configuration
) {
process(semanticServices, trace, outerScope, owner, declarations, analyzeCompletely, flowDataTraceFactory, configuration, false);
} }
private static void process( private static void process(
@@ -42,8 +45,9 @@ public class TopDownAnalyzer {
@NotNull Collection<? extends JetDeclaration> declarations, @NotNull Collection<? extends JetDeclaration> declarations,
@NotNull Predicate<PsiFile> analyzeCompletely, @NotNull Predicate<PsiFile> analyzeCompletely,
@NotNull JetControlFlowDataTraceFactory flowDataTraceFactory, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory,
@NotNull Configuration configuration,
boolean declaredLocally) { boolean declaredLocally) {
TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace, analyzeCompletely); TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace, analyzeCompletely, configuration);
doProcess(context, outerScope, owner, declarations, flowDataTraceFactory, declaredLocally); doProcess(context, outerScope, owner, declarations, flowDataTraceFactory, declaredLocally);
} }
@@ -89,7 +93,7 @@ public class TopDownAnalyzer {
@NotNull JetSemanticServices semanticServices, @NotNull JetSemanticServices semanticServices,
@NotNull BindingTrace trace, @NotNull BindingTrace trace,
@NotNull WritableScope outerScope, @NotNull NamespaceDescriptorImpl standardLibraryNamespace, @NotNull JetNamespace namespace) { @NotNull WritableScope outerScope, @NotNull NamespaceDescriptorImpl standardLibraryNamespace, @NotNull JetNamespace namespace) {
TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace, Predicates.<PsiFile>alwaysTrue()); TopDownAnalysisContext context = new TopDownAnalysisContext(semanticServices, trace, Predicates.<PsiFile>alwaysTrue(), Configuration.EMPTY);
context.getNamespaceScopes().put(namespace, standardLibraryNamespace.getMemberScope()); context.getNamespaceScopes().put(namespace, standardLibraryNamespace.getMemberScope());
context.getNamespaceDescriptors().put(namespace, standardLibraryNamespace); context.getNamespaceDescriptors().put(namespace, standardLibraryNamespace);
context.getDeclaringScopes().put(namespace, outerScope); context.getDeclaringScopes().put(namespace, outerScope);
@@ -135,7 +139,7 @@ public class TopDownAnalyzer {
public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptor classObjectDescriptor) { public ClassObjectStatus setClassObjectDescriptor(@NotNull MutableClassDescriptor classObjectDescriptor) {
return ClassObjectStatus.NOT_ALLOWED; return ClassObjectStatus.NOT_ALLOWED;
} }
}, Collections.<JetDeclaration>singletonList(object), Predicates.equalTo(object.getContainingFile()), JetControlFlowDataTraceFactory.EMPTY, true); }, Collections.<JetDeclaration>singletonList(object), Predicates.equalTo(object.getContainingFile()), JetControlFlowDataTraceFactory.EMPTY, Configuration.EMPTY, true);
} }
} }
@@ -75,6 +75,7 @@ public class TypeHierarchyResolver {
WriteThroughScope namespaceScope = new WriteThroughScope(outerScope, namespaceDescriptor.getMemberScope(), new TraceBasedRedeclarationHandler(context.getTrace())); WriteThroughScope namespaceScope = new WriteThroughScope(outerScope, namespaceDescriptor.getMemberScope(), new TraceBasedRedeclarationHandler(context.getTrace()));
namespaceScope.changeLockLevel(WritableScope.LockLevel.BOTH); namespaceScope.changeLockLevel(WritableScope.LockLevel.BOTH);
context.getConfiguration().addDefaultImports(context.getTrace(), namespaceScope);
context.getNamespaceScopes().put(namespace, namespaceScope); context.getNamespaceScopes().put(namespace, namespaceScope);
context.getDeclaringScopes().put(namespace, outerScope); context.getDeclaringScopes().put(namespace, outerScope);
@@ -215,6 +216,7 @@ public class TypeHierarchyResolver {
WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, namespaceDescriptor, new TraceBasedRedeclarationHandler(context.getTrace())).setDebugName("Namespace member scope"); WritableScopeImpl scope = new WritableScopeImpl(JetScope.EMPTY, namespaceDescriptor, new TraceBasedRedeclarationHandler(context.getTrace())).setDebugName("Namespace member scope");
scope.changeLockLevel(WritableScope.LockLevel.BOTH); scope.changeLockLevel(WritableScope.LockLevel.BOTH);
namespaceDescriptor.initialize(scope); namespaceDescriptor.initialize(scope);
context.getConfiguration().extendNamespaceScope(context.getTrace(), namespaceDescriptor, scope);
owner.addNamespace(namespaceDescriptor); owner.addNamespace(namespaceDescriptor);
if (namespace != null) { if (namespace != null) {
context.getTrace().record(BindingContext.NAMESPACE, namespace, namespaceDescriptor); context.getTrace().record(BindingContext.NAMESPACE, namespace, namespaceDescriptor);
@@ -38,7 +38,7 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
@Override @Override
public WritableScope changeLockLevel(LockLevel lockLevel) { public WritableScope changeLockLevel(LockLevel lockLevel) {
if (lockLevel.ordinal() < this.lockLevel.ordinal()) { if (lockLevel.ordinal() < this.lockLevel.ordinal()) {
throw new IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel); throw new IllegalStateException("cannot lower lock level from " + this.lockLevel + " to " + lockLevel + " at " + debugName);
} }
this.lockLevel = lockLevel; this.lockLevel = lockLevel;
return this; return this;
@@ -46,13 +46,13 @@ public abstract class WritableScopeWithImports extends JetScopeAdapter implement
protected void checkMayRead() { protected void checkMayRead() {
if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) { if (lockLevel != LockLevel.READING && lockLevel != LockLevel.BOTH) {
throw new IllegalStateException("cannot read with lock level " + lockLevel); throw new IllegalStateException("cannot read with lock level " + lockLevel + " at " + debugName);
} }
} }
protected void checkMayWrite() { protected void checkMayWrite() {
if (lockLevel != LockLevel.WRITING && lockLevel != LockLevel.BOTH) { if (lockLevel != LockLevel.WRITING && lockLevel != LockLevel.BOTH) {
throw new IllegalStateException("cannot write with lock level " + lockLevel); throw new IllegalStateException("cannot write with lock level " + lockLevel + " at " + debugName);
} }
} }
@@ -18,16 +18,6 @@ import java.util.List;
*/ */
public class DescriptorRenderer implements Renderer { public class DescriptorRenderer implements Renderer {
public static String getFQName(DeclarationDescriptor descriptor) {
DeclarationDescriptor container = descriptor.getContainingDeclaration();
if (container != null && !(container instanceof ModuleDescriptor)) {
String baseName = getFQName(container);
if (!baseName.isEmpty()) return baseName + "." + descriptor.getName();
}
return descriptor.getName();
}
public static final DescriptorRenderer TEXT = new DescriptorRenderer(); public static final DescriptorRenderer TEXT = new DescriptorRenderer();
public static final DescriptorRenderer HTML = new DescriptorRenderer() { public static final DescriptorRenderer HTML = new DescriptorRenderer() {
@@ -0,0 +1,8 @@
// KT-689 Allow to put Java and Kotlin files in the same packages
// This is a stub test. One should not extend Java packages that come from libraries.
namespace java
val c : lang.Class<*>? = null
val <T> Array<T>?.length : Int get() = if (this != null) this.size else throw NullPointerException()
@@ -1,11 +1,14 @@
package org.jetbrains.jet; package org.jetbrains.jet;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.diagnostics.Severity; import org.jetbrains.jet.lang.diagnostics.Severity;
import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic; import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic;
import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingTrace; import org.jetbrains.jet.lang.resolve.BindingTrace;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.util.slicedmap.ReadOnlySlice; import org.jetbrains.jet.util.slicedmap.ReadOnlySlice;
import org.jetbrains.jet.util.slicedmap.WritableSlice; import org.jetbrains.jet.util.slicedmap.WritableSlice;
@@ -120,4 +123,8 @@ public class JetTestUtils {
} }
} }
}; };
public static BindingContext analyzeNamespace(@NotNull JetNamespace namespace, @NotNull JetControlFlowDataTraceFactory flowDataTraceFactory) {
return AnalyzerFacade.analyzeOneNamespaceWithJavaIntegration(namespace, flowDataTraceFactory);
}
} }
@@ -11,10 +11,10 @@ import junit.framework.TestSuite;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.cfg.LoopInfo; import org.jetbrains.jet.lang.cfg.LoopInfo;
import org.jetbrains.jet.lang.cfg.pseudocode.*; import org.jetbrains.jet.lang.cfg.pseudocode.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
@@ -72,7 +72,7 @@ public class JetControlFlowTest extends JetLiteFixture {
}; };
AnalyzerFacade.analyzeNamespace(file.getRootNamespace(), new JetControlFlowDataTraceFactory() { JetTestUtils.analyzeNamespace(file.getRootNamespace(), new JetControlFlowDataTraceFactory() {
@NotNull @NotNull
@Override @Override
public JetPseudocodeTrace createTrace(JetElement element) { public JetPseudocodeTrace createTrace(JetElement element) {
@@ -3,11 +3,10 @@ package org.jetbrains.jet.checkers;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFile;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.ImportingStrategy;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import java.util.Collections; import java.util.Collections;
@@ -81,7 +80,10 @@ public class CheckerTestUtilTest extends JetLiteFixture {
} }
public void test(PsiFile psiFile) { public void test(PsiFile psiFile) {
BindingContext bindingContext = AnalyzerFacade.analyzeFileWithCache(AnalyzingUtils.getInstance(ImportingStrategy.NONE), (JetFile) psiFile, AnalyzerFacade.SINGLE_DECLARATION_PROVIDER); BindingContext bindingContext = AnalyzerFacade.analyzeOneNamespaceWithJavaIntegration(
((JetFile) psiFile).getRootNamespace(),
JetControlFlowDataTraceFactory.EMPTY);
String expectedText = CheckerTestUtil.addDiagnosticMarkersToText(psiFile, CheckerTestUtil.getDiagnosticsIncludingSyntaxErrors(bindingContext, psiFile)).toString(); String expectedText = CheckerTestUtil.addDiagnosticMarkersToText(psiFile, CheckerTestUtil.getDiagnosticsIncludingSyntaxErrors(bindingContext, psiFile)).toString();
List<CheckerTestUtil.DiagnosedRange> diagnosedRanges = Lists.newArrayList(); List<CheckerTestUtil.DiagnosedRange> diagnosedRanges = Lists.newArrayList();
@@ -9,14 +9,14 @@ import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.JetLiteFixture; import org.jetbrains.jet.JetLiteFixture;
import org.jetbrains.jet.JetTestCaseBuilder; import org.jetbrains.jet.JetTestCaseBuilder;
import org.jetbrains.jet.lang.Configuration;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils; import org.jetbrains.jet.lang.diagnostics.DiagnosticUtils;
import org.jetbrains.jet.lang.psi.JetDeclaration; import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.ImportingStrategy; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports;
import java.io.File; import java.io.File;
import java.util.List; import java.util.List;
@@ -82,14 +82,20 @@ public class JetDiagnosticsTest extends JetLiteFixture {
List<TestFile> testFileFiles = createTestFiles(testFileName, expectedText); List<TestFile> testFileFiles = createTestFiles(testFileName, expectedText);
boolean importJdk = expectedText.contains("+JDK"); boolean importJdk = expectedText.contains("+JDK");
ImportingStrategy importingStrategy = importJdk ? JavaDefaultImports.JAVA_DEFAULT_IMPORTS : ImportingStrategy.NONE; // Configuration configuration = importJdk ? JavaBridgeConfiguration.createJavaBridgeConfiguration(getProject()) : Configuration.EMPTY;
List<JetDeclaration> namespaces = Lists.newArrayList(); List<JetDeclaration> namespaces = Lists.newArrayList();
for (TestFile testFileFile : testFileFiles) { for (TestFile testFileFile : testFileFiles) {
namespaces.add(testFileFile.jetFile.getRootNamespace()); namespaces.add(testFileFile.jetFile.getRootNamespace());
} }
BindingContext bindingContext = AnalyzingUtils.getInstance(importingStrategy).analyzeNamespaces(getProject(), namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY); BindingContext bindingContext;
if (importJdk) {
bindingContext = AnalyzerFacade.analyzeNamespacesWithJavaIntegration(getProject(), namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
}
else {
bindingContext = AnalyzingUtils.getInstance().analyzeNamespaces(getProject(), Configuration.EMPTY, namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
}
StringBuilder actualText = new StringBuilder(); StringBuilder actualText = new StringBuilder();
for (TestFile testFileFile : testFileFiles) { for (TestFile testFileFile : testFileFiles) {
@@ -2,6 +2,7 @@ package org.jetbrains.jet.resolve;
import com.intellij.psi.PsiElement; import com.intellij.psi.PsiElement;
import com.intellij.psi.util.PsiTreeUtil; import com.intellij.psi.util.PsiTreeUtil;
import org.jetbrains.jet.JetTestUtils;
import org.jetbrains.jet.lang.JetSemanticServices; import org.jetbrains.jet.lang.JetSemanticServices;
import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory; import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
@@ -11,7 +12,6 @@ import org.jetbrains.jet.lang.diagnostics.UnresolvedReferenceDiagnostic;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.BindingContextUtils; import org.jetbrains.jet.lang.resolve.BindingContextUtils;
import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.lang.types.ErrorUtils; import org.jetbrains.jet.lang.types.ErrorUtils;
import org.jetbrains.jet.lang.types.JetStandardLibrary; import org.jetbrains.jet.lang.types.JetStandardLibrary;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
@@ -84,7 +84,7 @@ public class ExpectedResolveData {
JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(file.getProject()); JetSemanticServices semanticServices = JetSemanticServices.createSemanticServices(file.getProject());
JetStandardLibrary lib = semanticServices.getStandardLibrary(); JetStandardLibrary lib = semanticServices.getStandardLibrary();
BindingContext bindingContext = AnalyzerFacade.analyzeNamespace(file.getRootNamespace(), JetControlFlowDataTraceFactory.EMPTY); BindingContext bindingContext = JetTestUtils.analyzeNamespace(file.getRootNamespace(), JetControlFlowDataTraceFactory.EMPTY);
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) { for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
if (diagnostic instanceof UnresolvedReferenceDiagnostic) { if (diagnostic instanceof UnresolvedReferenceDiagnostic) {
UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (UnresolvedReferenceDiagnostic) diagnostic; UnresolvedReferenceDiagnostic unresolvedReferenceDiagnostic = (UnresolvedReferenceDiagnostic) diagnostic;
@@ -24,9 +24,8 @@ import org.jetbrains.jet.lang.cfg.pseudocode.JetControlFlowDataTraceFactory;
import org.jetbrains.jet.lang.diagnostics.Diagnostic; import org.jetbrains.jet.lang.diagnostics.Diagnostic;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
import java.io.File; import java.io.File;
@@ -59,7 +58,7 @@ public class JetCompiler implements TranslatingCompiler {
public void compile(final CompileContext compileContext, Chunk<Module> moduleChunk, final VirtualFile[] virtualFiles, OutputSink outputSink) { public void compile(final CompileContext compileContext, Chunk<Module> moduleChunk, final VirtualFile[] virtualFiles, OutputSink outputSink) {
if (virtualFiles.length == 0) return; if (virtualFiles.length == 0) return;
Module module = compileContext.getModuleByFile(virtualFiles[0]); final Module module = compileContext.getModuleByFile(virtualFiles[0]);
final VirtualFile outputDir = compileContext.getModuleOutputDirectory(module); final VirtualFile outputDir = compileContext.getModuleOutputDirectory(module);
if (outputDir == null) { if (outputDir == null) {
compileContext.addMessage(ERROR, "[Internal Error] No output directory", "", -1, -1); compileContext.addMessage(ERROR, "[Internal Error] No output directory", "", -1, -1);
@@ -80,10 +79,8 @@ public class JetCompiler implements TranslatingCompiler {
} }
} }
BindingContext bindingContext = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).analyzeNamespaces( BindingContext bindingContext =
compileContext.getProject(), namespaces, AnalyzerFacade.analyzeNamespacesWithJavaIntegration(compileContext.getProject(), namespaces, Predicates.<PsiFile>alwaysTrue(), JetControlFlowDataTraceFactory.EMPTY);
Predicates.<PsiFile>alwaysTrue(),
JetControlFlowDataTraceFactory.EMPTY);
boolean errors = false; boolean errors = false;
for (Diagnostic diagnostic : bindingContext.getDiagnostics()) { for (Diagnostic diagnostic : bindingContext.getDiagnostics()) {
@@ -35,9 +35,8 @@ import org.jetbrains.jet.lang.psi.JetClassOrObject;
import org.jetbrains.jet.lang.psi.JetDeclaration; import org.jetbrains.jet.lang.psi.JetDeclaration;
import org.jetbrains.jet.lang.psi.JetFile; import org.jetbrains.jet.lang.psi.JetFile;
import org.jetbrains.jet.lang.psi.JetNamespace; import org.jetbrains.jet.lang.psi.JetNamespace;
import org.jetbrains.jet.lang.resolve.AnalyzingUtils;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.java.JavaDefaultImports; import org.jetbrains.jet.lang.resolve.java.AnalyzerFacade;
import org.jetbrains.jet.plugin.JetFileType; import org.jetbrains.jet.plugin.JetFileType;
import java.util.*; import java.util.*;
@@ -211,8 +210,8 @@ public class JavaElementFinder extends PsiElementFinder {
if (dirty.size() > 0) { if (dirty.size() > 0) {
final BindingContext context = AnalyzingUtils.getInstance(JavaDefaultImports.JAVA_DEFAULT_IMPORTS).shallowAnalyzeFiles(dirty); final BindingContext context = AnalyzerFacade.shallowAnalyzeFiles(dirty);
state.compileCorrectNamespaces(context, AnalyzingUtils.collectRootNamespaces(dirty)); state.compileCorrectNamespaces(context, AnalyzerFacade.collectRootNamespaces(dirty));
state.getFactory().files(); state.getFactory().files();
} }
@@ -13,6 +13,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.jet.lang.descriptors.*; import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.BindingContext; import org.jetbrains.jet.lang.resolve.BindingContext;
import org.jetbrains.jet.lang.resolve.DescriptorUtils;
import org.jetbrains.jet.lang.resolve.scopes.JetScope; import org.jetbrains.jet.lang.resolve.scopes.JetScope;
import org.jetbrains.jet.lang.types.JetType; import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.plugin.compiler.WholeProjectAnalyzerFacade; import org.jetbrains.jet.plugin.compiler.WholeProjectAnalyzerFacade;
@@ -100,7 +101,7 @@ class JetSimpleNameReference extends JetPsiReference {
typeText = DescriptorRenderer.TEXT.renderType(outType); typeText = DescriptorRenderer.TEXT.renderType(outType);
} }
else if (descriptor instanceof ClassDescriptor) { else if (descriptor instanceof ClassDescriptor) {
tailText = " (" + DescriptorRenderer.getFQName(descriptor.getContainingDeclaration()) + ")"; tailText = " (" + DescriptorUtils.getFQName(descriptor.getContainingDeclaration()) + ")";
tailTextGrayed = true; tailTextGrayed = true;
} }
else { else {