@SuppressWarnings("ConstantConditions") private JetScope addImports(JetScope scope) { WritableScopeImpl writableScope = new WritableScopeImpl( scope, scope.getContainingDeclaration(), RedeclarationHandler.DO_NOTHING, "JetTypeCheckerTest.addImports"); List<JetScope> scopeChain = new ArrayList<JetScope>(); scopeChain.add(writableScope); ModuleDescriptor module = LazyResolveTestUtil.resolveProject(getProject()); for (ImportPath defaultImport : module.getDefaultImports()) { FqName fqName = defaultImport.fqnPart(); if (defaultImport.isAllUnder()) { scopeChain.add(module.getPackage(fqName).getMemberScope()); } else { Name shortName = fqName.shortName(); assert shortName.equals(defaultImport.getImportedName()); writableScope.addClassifierDescriptor( module.getPackage(fqName.parent()).getMemberScope().getClassifier(shortName)); } } scopeChain.add(module.getPackage(FqName.ROOT).getMemberScope()); writableScope.changeLockLevel(WritableScope.LockLevel.BOTH); return new ChainedScope( scope.getContainingDeclaration(), "JetTypeCheckerTest.addImports scope with imports", scopeChain.toArray(new JetScope[scopeChain.size()])); }
@NotNull @Override public Collection<DeclarationDescriptor> getDeclarationsByLabel(@NotNull Name labelName) { ArrayList<DeclarationDescriptor> result = new ArrayList<DeclarationDescriptor>(); for (JetScope jetScope : scopeChain) { result.addAll(jetScope.getDeclarationsByLabel(labelName)); } result.trimToSize(); return result; }
@NotNull @Override public Set<VariableDescriptor> getProperties(@NotNull Name name) { checkMayRead(); Set<VariableDescriptor> properties = Sets.newLinkedHashSet(); for (JetScope imported : getImports()) { properties.addAll(imported.getProperties(name)); } return properties; }
@NotNull @Override public Collection<DeclarationDescriptor> getAllDescriptors() { if (allDescriptors == null) { allDescriptors = new HashSet<DeclarationDescriptor>(); for (JetScope scope : scopeChain) { allDescriptors.addAll(scope.getAllDescriptors()); } } return allDescriptors; }
@Override public ClassifierDescriptor getClassifier(@NotNull Name name) { checkMayRead(); for (JetScope imported : getImports()) { ClassifierDescriptor importedClassifier = imported.getClassifier(name); if (importedClassifier != null) { return importedClassifier; } } return null; }
@Override public ClassDescriptor getObjectDescriptor(@NotNull Name name) { checkMayRead(); for (JetScope imported : getImports()) { ClassDescriptor objectDescriptor = imported.getObjectDescriptor(name); if (objectDescriptor != null) { return objectDescriptor; } } return null; }
@Override public NamespaceDescriptor getNamespace(@NotNull Name name) { checkMayRead(); for (JetScope imported : getImports()) { NamespaceDescriptor importedDescriptor = imported.getNamespace(name); if (importedDescriptor != null) { return importedDescriptor; } } return null; }
@Override public void printScopeStructure(@NotNull Printer p) { p.println(getClass().getSimpleName(), ": ", debugName, " {"); p.pushIndent(); for (JetScope scope : scopeChain) { scope.printScopeStructure(p); } p.popIndent(); p.println("}"); }
@NotNull @Override public List<ReceiverParameterDescriptor> getImplicitReceiversHierarchy() { if (implicitReceiverHierarchy == null) { ArrayList<ReceiverParameterDescriptor> result = new ArrayList<ReceiverParameterDescriptor>(); for (JetScope jetScope : scopeChain) { result.addAll(jetScope.getImplicitReceiversHierarchy()); } result.trimToSize(); implicitReceiverHierarchy = result; } return implicitReceiverHierarchy; }
@Override public VariableDescriptor getLocalVariable(@NotNull Name name) { checkMayRead(); // Meaningful lookup goes here for (JetScope imported : getImports()) { VariableDescriptor importedDescriptor = imported.getLocalVariable(name); if (importedDescriptor != null) { return importedDescriptor; } } return null; }
@NotNull @Override public Set<FunctionDescriptor> getFunctions(@NotNull Name name) { checkMayRead(); if (getImports().isEmpty()) { return Collections.emptySet(); } Set<FunctionDescriptor> result = Sets.newLinkedHashSet(); for (JetScope imported : getImports()) { result.addAll(imported.getFunctions(name)); } return result; }
@Override public void getImplicitReceiversHierarchy(@NotNull List<ReceiverDescriptor> result) { checkMayRead(); super.getImplicitReceiversHierarchy(result); // Imported scopes come with their receivers // Example: class member resolution scope imports a scope of it's class object // members of the class object must be able to find it as an implicit receiver for (JetScope scope : getImports()) { ReceiverDescriptor definedReceiver = scope.getImplicitReceiver(); if (definedReceiver.exists()) { result.add(0, definedReceiver); } } }
@NotNull @Override public Collection<DeclarationDescriptor> getAllDescriptors() { if (allDescriptors == null) { allDescriptors = substitute(workerScope.getAllDescriptors()); } return allDescriptors; }
@TestOnly @Override public void printScopeStructure(@NotNull Printer p) { p.println(getClass().getSimpleName(), " {"); p.pushIndent(); p.println("substitutor = "); p.pushIndent(); p.println(substitutor); p.popIndent(); p.print("workerScope = "); workerScope.printScopeStructure(p.withholdIndentOnce()); p.popIndent(); p.println("}"); }
@SuppressWarnings("ConstantConditions") private WritableScopeImpl addImports(JetScope scope) { WritableScopeImpl writableScope = new WritableScopeImpl( scope, scope.getContainingDeclaration(), RedeclarationHandler.DO_NOTHING, "JetTypeCheckerTest.addImports"); InjectorForJavaDescriptorResolver injector = new InjectorForJavaDescriptorResolver(getProject(), new BindingTraceContext()); JavaDescriptorResolver javaDescriptorResolver = injector.getJavaDescriptorResolver(); writableScope.importScope( javaDescriptorResolver .resolveNamespace(FqName.ROOT, INCLUDE_KOTLIN_SOURCES) .getMemberScope()); writableScope.importScope( javaDescriptorResolver .resolveNamespace(new FqName("java.lang"), IGNORE_KOTLIN_SOURCES) .getMemberScope()); writableScope.changeLockLevel(WritableScope.LockLevel.BOTH); writableScope.importScope(builtIns.getBuiltInsScope()); return writableScope; }
@NotNull @Override public Collection<FunctionDescriptor> getFunctions(@NotNull Name name) { return substitute(workerScope.getFunctions(name)); }
@NotNull @Override public Collection<VariableDescriptor> getProperties(@NotNull Name name) { return substitute(workerScope.getProperties(name)); }
@Override public VariableDescriptor getLocalVariable(@NotNull Name name) { return substitute(workerScope.getLocalVariable(name)); }
@NotNull @Override public DeclarationDescriptor getContainingDeclaration() { return workerScope.getContainingDeclaration(); }
@Override public ClassifierDescriptor getClassifier(@NotNull Name name) { return substitute(workerScope.getClassifier(name)); }
@Override public PackageViewDescriptor getPackage(@NotNull Name name) { return workerScope.getPackage(name); }
@NotNull @Override public Collection<DeclarationDescriptor> getOwnDeclaredDescriptors() { return substitute(workerScope.getOwnDeclaredDescriptors()); }