public static CodegenContext getContext(DeclarationDescriptor descriptor, GenerationState state) { if (descriptor instanceof PackageFragmentDescriptor) { return new PackageContext( (PackageFragmentDescriptor) descriptor, state.getRootContext(), null); } CodegenContext parent = getContext(descriptor.getContainingDeclaration(), state); if (descriptor instanceof ClassDescriptor) { OwnerKind kind = DescriptorUtils.isInterface(descriptor) ? OwnerKind.DEFAULT_IMPLS : OwnerKind.IMPLEMENTATION; return parent.intoClass((ClassDescriptor) descriptor, kind, state); } else if (descriptor instanceof ScriptDescriptor) { ClassDescriptor classDescriptorForScript = state.getBindingContext().get(CLASS_FOR_SCRIPT, (ScriptDescriptor) descriptor); assert classDescriptorForScript != null : "Can't find class for script: " + descriptor; List<ScriptDescriptor> earlierScripts = state.getEarlierScriptsForReplInterpreter(); return parent.intoScript( (ScriptDescriptor) descriptor, earlierScripts == null ? Collections.emptyList() : earlierScripts, classDescriptorForScript); } else if (descriptor instanceof FunctionDescriptor) { return parent.intoFunction((FunctionDescriptor) descriptor); } throw new IllegalStateException("Couldn't build context for " + descriptorName(descriptor)); }
@Override protected void generateBody() { List<KtObjectDeclaration> companions = new ArrayList<KtObjectDeclaration>(); if (kind != OwnerKind.DEFAULT_IMPLS) { // generate nested classes first and only then generate class body. It necessary to access to // nested CodegenContexts for (KtDeclaration declaration : myClass.getDeclarations()) { if (shouldProcessFirst(declaration)) { // Generate companions after class body generation (need to record all synthetic // accessors) if (declaration instanceof KtObjectDeclaration && ((KtObjectDeclaration) declaration).isCompanion()) { companions.add((KtObjectDeclaration) declaration); CodegenUtilKt.populateCompanionBackingFieldNamesToOuterContextIfNeeded( (KtObjectDeclaration) declaration, context, state); } else { generateDeclaration(declaration); } } } } for (KtDeclaration declaration : myClass.getDeclarations()) { if (!shouldProcessFirst(declaration)) { generateDeclaration(declaration); } } generatePrimaryConstructorProperties(); generateConstructors(); generateDefaultImplsIfNeeded(); for (KtObjectDeclaration companion : companions) { generateDeclaration(companion); } if (!DescriptorUtils.isInterface(descriptor)) { for (DeclarationDescriptor memberDescriptor : DescriptorUtils.getAllDescriptors(descriptor.getDefaultType().getMemberScope())) { if (memberDescriptor instanceof CallableMemberDescriptor) { CallableMemberDescriptor member = (CallableMemberDescriptor) memberDescriptor; if (!member.getKind().isReal() && ImplKt.findInterfaceImplementation(member) == null) { if (member instanceof FunctionDescriptor) { functionCodegen.generateBridges((FunctionDescriptor) member); } else if (member instanceof PropertyDescriptor) { PropertyGetterDescriptor getter = ((PropertyDescriptor) member).getGetter(); if (getter != null) { functionCodegen.generateBridges(getter); } PropertySetterDescriptor setter = ((PropertyDescriptor) member).getSetter(); if (setter != null) { functionCodegen.generateBridges(setter); } } } } } } }