@Override public void visitJetFile(@NotNull JetFile file) { MutablePackageFragmentDescriptor packageFragment = getOrCreatePackageFragmentForFile(file); c.getPackageFragments().put(file, packageFragment); c.addFile(file); PackageViewDescriptor packageView = packageFragment.getContainingDeclaration().getPackage(packageFragment.getFqName()); ChainedScope rootPlusPackageScope = new ChainedScope( packageView, "Root scope for " + file, packageView.getMemberScope(), outerScope); WriteThroughScope packageScope = new WriteThroughScope( rootPlusPackageScope, packageFragment.getMemberScope(), new TraceBasedRedeclarationHandler(trace), "package in file " + file.getName()); packageScope.changeLockLevel(WritableScope.LockLevel.BOTH); c.getFileScopes().put(file, packageScope); if (file.isScript()) { scriptHeaderResolver.processScriptHierarchy(c, file.getScript(), packageScope); } prepareForDeferredCall(packageScope, packageFragment, file); }
@NotNull private MutablePackageFragmentDescriptor getOrCreatePackageFragmentForFile( @NotNull JetFile file) { JetPackageDirective packageDirective = file.getPackageDirective(); assert packageDirective != null : "scripts are not supported"; MutablePackageFragmentDescriptor fragment = packageFragmentProvider.getOrCreateFragment(packageDirective.getFqName()); ModuleDescriptor module = packageFragmentProvider.getModule(); DescriptorResolver.resolvePackageHeader( packageDirective, module, TypeHierarchyResolver.this.trace); trace.record(BindingContext.FILE_TO_PACKAGE_FRAGMENT, file, fragment); // Register files corresponding to this package // The trace currently does not support bi-di multimaps that would handle this task nicer FqName fqName = fragment.getFqName(); Collection<JetFile> files = trace.get(PACKAGE_TO_FILES, fqName); if (files == null) { files = Sets.newIdentityHashSet(); } files.add(file); trace.record(BindingContext.PACKAGE_TO_FILES, fqName, files); return fragment; }
@Override public void visitJetFile(JetFile file) { if (file.isScript()) { //noinspection ConstantConditions final ClassDescriptor classDescriptor = bindingContext.get(CLASS_FOR_FUNCTION, bindingContext.get(SCRIPT, file.getScript())); classStack.push(classDescriptor); //noinspection ConstantConditions nameStack.push(classNameForScriptPsi(bindingContext, file.getScript()).getInternalName()); } else { nameStack.push(JetPsiUtil.getFQName(file).getFqName().replace('.', '/')); } file.acceptChildren(this); nameStack.pop(); if (file.isScript()) { classStack.pop(); } }
@NotNull public static Collection<JetFile> allFilesInNamespaces( BindingContext bindingContext, Collection<JetFile> files) { // todo: we use Set and add given files but ignoring other scripts because something non-clear // kept in binding // for scripts especially in case of REPL HashSet<FqName> names = new HashSet<FqName>(); for (JetFile file : files) { if (!file.isScript()) { names.add(JetPsiUtil.getFQName(file)); } } HashSet<JetFile> answer = new HashSet<JetFile>(); answer.addAll(files); for (FqName name : names) { NamespaceDescriptor namespaceDescriptor = bindingContext.get(BindingContext.FQNAME_TO_NAMESPACE_DESCRIPTOR, name); Collection<JetFile> jetFiles = bindingContext.get(NAMESPACE_TO_FILES, namespaceDescriptor); if (jetFiles != null) answer.addAll(jetFiles); } List<JetFile> sortedAnswer = new ArrayList<JetFile>(answer); Collections.sort( sortedAnswer, new Comparator<JetFile>() { @NotNull private String path(JetFile file) { VirtualFile virtualFile = file.getVirtualFile(); assert virtualFile != null : "VirtualFile is null for JetFile: " + file.getName(); return virtualFile.getPath(); } @Override public int compare(JetFile first, JetFile second) { return path(first).compareTo(path(second)); } }); return sortedAnswer; }
public static void initTrace(BindingTrace bindingTrace, Collection<JetFile> files) { CodegenAnnotatingVisitor visitor = new CodegenAnnotatingVisitor(bindingTrace); for (JetFile file : allFilesInNamespaces(bindingTrace.getBindingContext(), files)) { file.accept(visitor); } }