001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
003     *
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.apache.org/licenses/LICENSE-2.0
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    
017    package org.jetbrains.kotlin.codegen;
018    
019    import com.intellij.openapi.progress.ProcessCanceledException;
020    import com.intellij.psi.PsiElement;
021    import kotlin.jvm.functions.Function0;
022    import org.jetbrains.annotations.NotNull;
023    import org.jetbrains.annotations.Nullable;
024    import org.jetbrains.kotlin.backend.common.CodegenUtil;
025    import org.jetbrains.kotlin.codegen.context.*;
026    import org.jetbrains.kotlin.codegen.inline.*;
027    import org.jetbrains.kotlin.codegen.state.GenerationState;
028    import org.jetbrains.kotlin.codegen.state.KotlinTypeMapper;
029    import org.jetbrains.kotlin.descriptors.*;
030    import org.jetbrains.kotlin.descriptors.annotations.Annotations;
031    import org.jetbrains.kotlin.descriptors.impl.SimpleFunctionDescriptorImpl;
032    import org.jetbrains.kotlin.fileClasses.FileClasses;
033    import org.jetbrains.kotlin.fileClasses.JvmFileClassesProvider;
034    import org.jetbrains.kotlin.load.java.JavaVisibilities;
035    import org.jetbrains.kotlin.load.java.JvmAbi;
036    import org.jetbrains.kotlin.name.Name;
037    import org.jetbrains.kotlin.name.SpecialNames;
038    import org.jetbrains.kotlin.psi.*;
039    import org.jetbrains.kotlin.resolve.*;
040    import org.jetbrains.kotlin.resolve.annotations.AnnotationUtilKt;
041    import org.jetbrains.kotlin.resolve.constants.ConstantValue;
042    import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
043    import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilsKt;
044    import org.jetbrains.kotlin.resolve.jvm.AsmTypes;
045    import org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKt;
046    import org.jetbrains.kotlin.resolve.jvm.jvmSignature.JvmMethodSignature;
047    import org.jetbrains.kotlin.resolve.source.KotlinSourceElementKt;
048    import org.jetbrains.kotlin.storage.LockBasedStorageManager;
049    import org.jetbrains.kotlin.storage.NotNullLazyValue;
050    import org.jetbrains.kotlin.types.ErrorUtils;
051    import org.jetbrains.kotlin.types.KotlinType;
052    import org.jetbrains.org.objectweb.asm.Label;
053    import org.jetbrains.org.objectweb.asm.MethodVisitor;
054    import org.jetbrains.org.objectweb.asm.Type;
055    import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
056    import org.jetbrains.org.objectweb.asm.commons.Method;
057    
058    import java.util.*;
059    
060    import static org.jetbrains.kotlin.codegen.AsmUtil.calculateInnerClassAccessFlags;
061    import static org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive;
062    import static org.jetbrains.kotlin.codegen.ClassBuilderModeUtilKt.shouldGenerateMetadata;
063    import static org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED;
064    import static org.jetbrains.kotlin.resolve.BindingContext.VARIABLE;
065    import static org.jetbrains.kotlin.resolve.DescriptorUtils.*;
066    import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.*;
067    import static org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin.NO_ORIGIN;
068    import static org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOriginKt.Synthetic;
069    import static org.jetbrains.org.objectweb.asm.Opcodes.*;
070    
071    public abstract class MemberCodegen<T extends KtElement/* TODO: & JetDeclarationContainer*/> {
072        protected final GenerationState state;
073        protected final T element;
074        protected final FieldOwnerContext context;
075        protected final ClassBuilder v;
076        protected final FunctionCodegen functionCodegen;
077        protected final PropertyCodegen propertyCodegen;
078        protected final KotlinTypeMapper typeMapper;
079        protected final BindingContext bindingContext;
080        protected final JvmFileClassesProvider fileClassesProvider;
081        private final MemberCodegen<?> parentCodegen;
082        private final ReifiedTypeParametersUsages reifiedTypeParametersUsages = new ReifiedTypeParametersUsages();
083        protected final Collection<ClassDescriptor> innerClasses = new LinkedHashSet<ClassDescriptor>();
084    
085        protected ExpressionCodegen clInit;
086        private NameGenerator inlineNameGenerator;
087    
088        private DefaultSourceMapper sourceMapper;
089    
090        private final ConstantExpressionEvaluator constantExpressionEvaluator;
091    
092        public MemberCodegen(
093                @NotNull GenerationState state,
094                @Nullable MemberCodegen<?> parentCodegen,
095                @NotNull FieldOwnerContext context,
096                T element,
097                @NotNull ClassBuilder builder
098        ) {
099            this.state = state;
100            this.typeMapper = state.getTypeMapper();
101            this.bindingContext = state.getBindingContext();
102            this.fileClassesProvider = state.getFileClassesProvider();
103            this.element = element;
104            this.context = context;
105            this.v = builder;
106            this.functionCodegen = new FunctionCodegen(context, v, state, this);
107            this.propertyCodegen = new PropertyCodegen(context, v, functionCodegen, this);
108            this.parentCodegen = parentCodegen;
109            this.constantExpressionEvaluator = new ConstantExpressionEvaluator(state.getModule().getBuiltIns());
110        }
111    
112        protected MemberCodegen(@NotNull MemberCodegen<T> wrapped, T declaration, FieldOwnerContext codegenContext) {
113            this(wrapped.state, wrapped.getParentCodegen(), codegenContext, declaration, wrapped.v);
114        }
115    
116        public void generate() {
117            generateDeclaration();
118    
119            generateBody();
120    
121            generateSyntheticParts();
122    
123            if (shouldGenerateMetadata(state.getClassBuilderMode())) {
124                generateKotlinMetadataAnnotation();
125            }
126    
127            done();
128        }
129    
130        protected abstract void generateDeclaration();
131    
132        protected abstract void generateBody();
133    
134        protected void generateSyntheticParts() {
135        }
136    
137        protected abstract void generateKotlinMetadataAnnotation();
138    
139        @Nullable
140        protected ClassDescriptor classForInnerClassRecord() {
141            return null;
142        }
143    
144        public static void markLineNumberForDescriptor(@Nullable ClassDescriptor declarationDescriptor, @NotNull InstructionAdapter v) {
145            if (declarationDescriptor == null) {
146                return;
147            }
148    
149            PsiElement classElement = DescriptorToSourceUtils.getSourceFromDescriptor(declarationDescriptor);
150            if (classElement != null) {
151                markLineNumberForElement(classElement, v);
152            }
153        }
154    
155        public static void markLineNumberForElement(@NotNull PsiElement element, @NotNull InstructionAdapter v) {
156            Integer lineNumber = CodegenUtil.getLineNumberForElement(element, false);
157            if (lineNumber != null) {
158                Label label = new Label();
159                v.visitLabel(label);
160                v.visitLineNumber(lineNumber, label);
161            }
162        }
163    
164        protected void done() {
165            if (clInit != null) {
166                clInit.v.visitInsn(RETURN);
167                FunctionCodegen.endVisit(clInit.v, "static initializer", element);
168            }
169    
170            writeInnerClasses();
171    
172            if (sourceMapper != null) {
173                SourceMapper.Companion.flushToClassBuilder(sourceMapper, v);
174            }
175    
176            v.done();
177        }
178    
179        public void genFunctionOrProperty(@NotNull KtDeclaration functionOrProperty) {
180            if (functionOrProperty instanceof KtNamedFunction) {
181                try {
182                    functionCodegen.gen((KtNamedFunction) functionOrProperty);
183                }
184                catch (ProcessCanceledException e) {
185                    throw e;
186                }
187                catch (CompilationException e) {
188                    throw e;
189                }
190                catch (Exception e) {
191                    throw new CompilationException("Failed to generate function " + functionOrProperty.getName(), e, functionOrProperty);
192                }
193            }
194            else if (functionOrProperty instanceof KtProperty) {
195                try {
196                    propertyCodegen.gen((KtProperty) functionOrProperty);
197                }
198                catch (ProcessCanceledException e) {
199                    throw e;
200                }
201                catch (CompilationException e) {
202                    throw e;
203                }
204                catch (Exception e) {
205                    throw new CompilationException("Failed to generate property " + functionOrProperty.getName(), e, functionOrProperty);
206                }
207            }
208            else {
209                throw new IllegalArgumentException("Unknown parameter: " + functionOrProperty);
210            }
211        }
212    
213        public static void genClassOrObject(
214                @NotNull CodegenContext parentContext,
215                @NotNull KtClassOrObject aClass,
216                @NotNull GenerationState state,
217                @Nullable MemberCodegen<?> parentCodegen
218        ) {
219            ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
220    
221            if (descriptor == null || ErrorUtils.isError(descriptor)) {
222                badDescriptor(descriptor, state.getClassBuilderMode());
223                return;
224            }
225    
226            if (descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED)) {
227                badDescriptor(descriptor, state.getClassBuilderMode());
228            }
229    
230            Type classType = state.getTypeMapper().mapClass(descriptor);
231            ClassBuilder classBuilder = state.getFactory().newVisitor(JvmDeclarationOriginKt.OtherOrigin(aClass, descriptor), classType, aClass.getContainingFile());
232            ClassContext classContext = parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION, state);
233            new ImplementationBodyCodegen(aClass, classContext, classBuilder, state, parentCodegen, false).generate();
234        }
235    
236        private static void badDescriptor(ClassDescriptor descriptor, ClassBuilderMode mode) {
237            if (mode == ClassBuilderMode.FULL) {
238                throw new IllegalStateException("Generating bad descriptor in ClassBuilderMode = " + mode + ": " + descriptor);
239            }
240        }
241    
242        public void genClassOrObject(KtClassOrObject aClass) {
243            genClassOrObject(context, aClass, state, this);
244        }
245    
246        private void writeInnerClasses() {
247            // JVMS7 (4.7.6): a nested class or interface member will have InnerClasses information
248            // for each enclosing class and for each immediate member
249            ClassDescriptor classDescriptor = classForInnerClassRecord();
250            if (classDescriptor != null) {
251                if (parentCodegen != null) {
252                    parentCodegen.innerClasses.add(classDescriptor);
253                }
254    
255                for (MemberCodegen<?> codegen = this; codegen != null; codegen = codegen.getParentCodegen()) {
256                    ClassDescriptor outerClass = codegen.classForInnerClassRecord();
257                    if (outerClass != null) {
258                        innerClasses.add(outerClass);
259                    }
260                }
261            }
262    
263            for (ClassDescriptor innerClass : innerClasses) {
264                writeInnerClass(innerClass);
265            }
266        }
267    
268        private void writeInnerClass(@NotNull ClassDescriptor innerClass) {
269            DeclarationDescriptor containing = innerClass.getContainingDeclaration();
270            String outerClassInternalName = null;
271            if (containing instanceof ClassDescriptor) {
272                outerClassInternalName = typeMapper.mapClass((ClassDescriptor) containing).getInternalName();
273            }
274            String innerName = innerClass.getName().isSpecial() ? null : innerClass.getName().asString();
275            String innerClassInternalName = typeMapper.mapClass(innerClass).getInternalName();
276            v.visitInnerClass(innerClassInternalName, outerClassInternalName, innerName, calculateInnerClassAccessFlags(innerClass));
277        }
278    
279        protected void writeOuterClassAndEnclosingMethod() {
280            CodegenContext context = this.context.getParentContext();
281    
282            while (context instanceof InlineLambdaContext) {
283                // If this is a lambda which will be inlined, skip its MethodContext and enclosing ClosureContext
284                //noinspection ConstantConditions
285                context = context.getParentContext().getParentContext();
286            }
287            assert context != null : "Outermost context can't be null: " + this.context;
288    
289            Type enclosingAsmType = computeOuterClass(context);
290            if (enclosingAsmType != null) {
291                Method method = computeEnclosingMethod(context);
292    
293                v.visitOuterClass(
294                        enclosingAsmType.getInternalName(),
295                        method == null ? null : method.getName(),
296                        method == null ? null : method.getDescriptor()
297                );
298            }
299        }
300    
301        @Nullable
302        private Type computeOuterClass(@NotNull CodegenContext<?> context) {
303            CodegenContext<? extends ClassOrPackageFragmentDescriptor> outermost = context.getClassOrPackageParentContext();
304            if (outermost instanceof ClassContext) {
305                return typeMapper.mapType(((ClassContext) outermost).getContextDescriptor());
306            }
307            else if (outermost instanceof MultifileClassFacadeContext || outermost instanceof DelegatingToPartContext) {
308                Type implementationOwnerType = CodegenContextUtil.getImplementationOwnerClassType(outermost);
309                if (implementationOwnerType != null) {
310                    return implementationOwnerType;
311                }
312                else {
313                    return FileClasses.getFileClassType(fileClassesProvider, element.getContainingKtFile());
314                }
315            }
316    
317            return null;
318        }
319    
320        @Nullable
321        private Method computeEnclosingMethod(@NotNull CodegenContext context) {
322            if (context instanceof MethodContext) {
323                Method method = typeMapper.mapAsmMethod(((MethodContext) context).getFunctionDescriptor());
324                if (!method.getName().equals("<clinit>")) {
325                    return method;
326                }
327            }
328            return null;
329        }
330    
331        @NotNull
332        public NameGenerator getInlineNameGenerator() {
333            if (inlineNameGenerator == null) {
334                String prefix = InlineCodegenUtil.getInlineName(context, typeMapper, fileClassesProvider);
335                inlineNameGenerator = new NameGenerator(prefix);
336            }
337            return inlineNameGenerator;
338        }
339    
340        @NotNull
341        protected final ExpressionCodegen createOrGetClInitCodegen() {
342            if (clInit == null) {
343                DeclarationDescriptor contextDescriptor = context.getContextDescriptor();
344                SimpleFunctionDescriptorImpl clInitDescriptor = createClInitFunctionDescriptor(contextDescriptor);
345                MethodVisitor mv = createClInitMethodVisitor(contextDescriptor);
346                clInit = new ExpressionCodegen(mv, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInitDescriptor), state, this);
347            }
348            return clInit;
349        }
350    
351        @NotNull
352        protected MethodVisitor createClInitMethodVisitor(@NotNull DeclarationDescriptor contextDescriptor) {
353            return v.newMethod(JvmDeclarationOriginKt.OtherOrigin(contextDescriptor), ACC_STATIC, "<clinit>", "()V", null, null);
354        }
355    
356        @NotNull
357        protected SimpleFunctionDescriptorImpl createClInitFunctionDescriptor(@NotNull DeclarationDescriptor descriptor) {
358            SimpleFunctionDescriptorImpl clInit = SimpleFunctionDescriptorImpl.create(descriptor, Annotations.Companion.getEMPTY(),
359                    Name.special("<clinit>"), SYNTHESIZED, KotlinSourceElementKt.toSourceElement(element));
360            clInit.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
361                              Collections.<ValueParameterDescriptor>emptyList(),
362                              DescriptorUtilsKt.getModule(descriptor).getBuiltIns().getUnitType(),
363                              null, Visibilities.PRIVATE);
364            return clInit;
365        }
366    
367        protected void generateInitializers(@NotNull Function0<ExpressionCodegen> createCodegen) {
368            NotNullLazyValue<ExpressionCodegen> codegen = LockBasedStorageManager.NO_LOCKS.createLazyValue(createCodegen);
369            for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) {
370                if (declaration instanceof KtProperty) {
371                    if (shouldInitializeProperty((KtProperty) declaration)) {
372                        initializeProperty(codegen.invoke(), (KtProperty) declaration);
373                    }
374                }
375                else if (declaration instanceof KtAnonymousInitializer) {
376                    KtExpression body = ((KtAnonymousInitializer) declaration).getBody();
377                    if (body != null) {
378                        codegen.invoke().gen(body, Type.VOID_TYPE);
379                    }
380                }
381            }
382        }
383    
384        public void beforeMethodBody(@NotNull MethodVisitor mv) {
385        }
386    
387        private void initializeProperty(@NotNull ExpressionCodegen codegen, @NotNull KtProperty property) {
388            PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
389            assert propertyDescriptor != null;
390    
391            KtExpression initializer = property.getDelegateExpressionOrInitializer();
392            assert initializer != null : "shouldInitializeProperty must return false if initializer is null";
393    
394            StackValue.Property propValue = codegen.intermediateValueForProperty(propertyDescriptor, true, false, null, true, StackValue.LOCAL_0);
395    
396            propValue.store(codegen.gen(initializer), codegen.v);
397        }
398    
399        protected boolean shouldInitializeProperty(@NotNull KtProperty property) {
400            if (!property.hasDelegateExpressionOrInitializer()) return false;
401    
402            PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
403            assert propertyDescriptor != null;
404    
405            if (propertyDescriptor.isConst()) {
406                //const initializer always inlined
407                return false;
408            }
409    
410            KtExpression initializer = property.getInitializer();
411    
412            ConstantValue<?> initializerValue =
413                    initializer != null ? ExpressionCodegen.getCompileTimeConstant(initializer, bindingContext) : null;
414            // we must write constant values for fields in light classes,
415            // because Java's completion for annotation arguments uses this information
416            if (initializerValue == null) return state.getClassBuilderMode() == ClassBuilderMode.FULL;
417    
418            //TODO: OPTIMIZATION: don't initialize static final fields
419            KotlinType jetType = getPropertyOrDelegateType(property, propertyDescriptor);
420            Type type = typeMapper.mapType(jetType);
421            return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type);
422        }
423    
424        @NotNull
425        private KotlinType getPropertyOrDelegateType(@NotNull KtProperty property, @NotNull PropertyDescriptor descriptor) {
426            KtExpression delegateExpression = property.getDelegateExpression();
427            if (delegateExpression != null) {
428                KotlinType delegateType = bindingContext.getType(delegateExpression);
429                assert delegateType != null : "Type of delegate expression should be recorded";
430                return delegateType;
431            }
432            return descriptor.getType();
433        }
434    
435        private static boolean skipDefaultValue(@NotNull PropertyDescriptor propertyDescriptor, Object value, @NotNull Type type) {
436            if (isPrimitive(type)) {
437                if (!propertyDescriptor.getType().isMarkedNullable() && value instanceof Number) {
438                    if (type == Type.INT_TYPE && ((Number) value).intValue() == 0) {
439                        return true;
440                    }
441                    if (type == Type.BYTE_TYPE && ((Number) value).byteValue() == 0) {
442                        return true;
443                    }
444                    if (type == Type.LONG_TYPE && ((Number) value).longValue() == 0L) {
445                        return true;
446                    }
447                    if (type == Type.SHORT_TYPE && ((Number) value).shortValue() == 0) {
448                        return true;
449                    }
450                    if (type == Type.DOUBLE_TYPE && ((Number) value).doubleValue() == 0d) {
451                        return true;
452                    }
453                    if (type == Type.FLOAT_TYPE && ((Number) value).floatValue() == 0f) {
454                        return true;
455                    }
456                }
457                if (type == Type.BOOLEAN_TYPE && value instanceof Boolean && !((Boolean) value)) {
458                    return true;
459                }
460                if (type == Type.CHAR_TYPE && value instanceof Character && ((Character) value) == 0) {
461                    return true;
462                }
463            }
464            else {
465                if (value == null) {
466                    return true;
467                }
468            }
469            return false;
470        }
471    
472        protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) {
473            List<KtProperty> delegatedProperties = new ArrayList<KtProperty>();
474            for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) {
475                if (declaration instanceof KtProperty) {
476                    KtProperty property = (KtProperty) declaration;
477                    if (property.hasDelegate()) {
478                        delegatedProperties.add(property);
479                    }
480                }
481            }
482            if (delegatedProperties.isEmpty()) return;
483    
484            v.newField(NO_ORIGIN, ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME,
485                       "[" + K_PROPERTY_TYPE, null, null);
486    
487            if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return;
488    
489            InstructionAdapter iv = createOrGetClInitCodegen().v;
490            iv.iconst(delegatedProperties.size());
491            iv.newarray(K_PROPERTY_TYPE);
492    
493            for (int i = 0, size = delegatedProperties.size(); i < size; i++) {
494                PropertyDescriptor property =
495                        (PropertyDescriptor) BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i));
496    
497                iv.dup();
498                iv.iconst(i);
499    
500                int receiverCount = (property.getDispatchReceiverParameter() != null ? 1 : 0) +
501                                    (property.getExtensionReceiverParameter() != null ? 1 : 0);
502                Type implType = property.isVar() ? MUTABLE_PROPERTY_REFERENCE_IMPL[receiverCount] : PROPERTY_REFERENCE_IMPL[receiverCount];
503                iv.anew(implType);
504                iv.dup();
505                // TODO: generate the container once and save to a local field instead (KT-10495)
506                ClosureCodegen.generateCallableReferenceDeclarationContainer(iv, property, state);
507                iv.aconst(property.getName().asString());
508                iv.aconst(PropertyReferenceCodegen.getPropertyReferenceSignature(property, state));
509                iv.invokespecial(
510                        implType.getInternalName(), "<init>",
511                        Type.getMethodDescriptor(Type.VOID_TYPE, K_DECLARATION_CONTAINER_TYPE, JAVA_STRING_TYPE, JAVA_STRING_TYPE), false
512                );
513                Method wrapper = PropertyReferenceCodegen.getWrapperMethodForPropertyReference(property, receiverCount);
514                iv.invokestatic(REFLECTION, wrapper.getName(), wrapper.getDescriptor(), false);
515    
516                StackValue.onStack(implType).put(K_PROPERTY_TYPE, iv);
517    
518                iv.astore(K_PROPERTY_TYPE);
519            }
520    
521            iv.putstatic(thisAsmType.getInternalName(), JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, "[" + K_PROPERTY_TYPE);
522        }
523    
524        public String getClassName() {
525            return v.getThisName();
526        }
527    
528        @NotNull
529        public FieldOwnerContext<?> getContext() {
530            return context;
531        }
532    
533        @NotNull
534        public ReifiedTypeParametersUsages getReifiedTypeParametersUsages() {
535            return reifiedTypeParametersUsages;
536        }
537    
538        public MemberCodegen<?> getParentCodegen() {
539            return parentCodegen;
540        }
541    
542        @Override
543        public String toString() {
544            return context.toString();
545        }
546    
547        @NotNull
548        public DefaultSourceMapper getOrCreateSourceMapper() {
549            if (sourceMapper == null) {
550                sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()));
551            }
552            return sourceMapper;
553        }
554    
555        protected void generateConstInstance(@NotNull Type thisAsmType, @NotNull Type fieldAsmType) {
556            v.newField(
557                    JvmDeclarationOriginKt.OtherOrigin(element), ACC_STATIC | ACC_FINAL | ACC_PUBLIC, JvmAbi.INSTANCE_FIELD,
558                    fieldAsmType.getDescriptor(), null, null
559            );
560    
561            if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
562                InstructionAdapter iv = createOrGetClInitCodegen().v;
563                iv.anew(thisAsmType);
564                iv.dup();
565                iv.invokespecial(thisAsmType.getInternalName(), "<init>", "()V", false);
566                iv.putstatic(thisAsmType.getInternalName(), JvmAbi.INSTANCE_FIELD, fieldAsmType.getDescriptor());
567            }
568        }
569    
570        protected void generateSyntheticAccessors() {
571            for (AccessorForCallableDescriptor<?> accessor : ((CodegenContext<?>) context).getAccessors()) {
572                generateSyntheticAccessor(accessor);
573            }
574        }
575    
576        private void generateSyntheticAccessor(@NotNull AccessorForCallableDescriptor<?> accessorForCallableDescriptor) {
577            if (accessorForCallableDescriptor instanceof FunctionDescriptor) {
578                final FunctionDescriptor accessor = (FunctionDescriptor) accessorForCallableDescriptor;
579                final FunctionDescriptor original = (FunctionDescriptor) accessorForCallableDescriptor.getCalleeDescriptor();
580                functionCodegen.generateMethod(
581                        Synthetic(null, original), accessor,
582                        new FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, accessor) {
583                            @Override
584                            public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
585                                markLineNumberForElement(element, codegen.v);
586    
587                                generateMethodCallTo(original, accessor, codegen.v).coerceTo(signature.getReturnType(), codegen.v);
588    
589                                codegen.v.areturn(signature.getReturnType());
590                            }
591                        }
592                );
593            }
594            else if (accessorForCallableDescriptor instanceof AccessorForPropertyDescriptor) {
595                final AccessorForPropertyDescriptor accessor = (AccessorForPropertyDescriptor) accessorForCallableDescriptor;
596                final PropertyDescriptor original = accessor.getCalleeDescriptor();
597    
598                class PropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased<PropertyAccessorDescriptor> {
599                    public PropertyAccessorStrategy(@NotNull PropertyAccessorDescriptor callableDescriptor) {
600                        super(MemberCodegen.this.state, callableDescriptor);
601                    }
602    
603                    @Override
604                    public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) {
605                        boolean syntheticBackingField = accessor instanceof AccessorForPropertyBackingFieldFromLocal;
606                        boolean forceFieldForCompanionProperty = JvmAbi.isPropertyWithBackingFieldInOuterClass(original) &&
607                                                                 !isCompanionObject(accessor.getContainingDeclaration());
608                        boolean forceField = forceFieldForCompanionProperty ||
609                                             syntheticBackingField ||
610                                             original.getVisibility() == JavaVisibilities.PROTECTED_STATIC_VISIBILITY;
611                        StackValue property = codegen.intermediateValueForProperty(
612                                original, forceField, syntheticBackingField, accessor.getSuperCallTarget(), forceFieldForCompanionProperty, StackValue.none()
613                        );
614    
615                        InstructionAdapter iv = codegen.v;
616    
617                        markLineNumberForElement(element, iv);
618    
619                        Type[] argTypes = signature.getAsmMethod().getArgumentTypes();
620                        for (int i = 0, reg = 0; i < argTypes.length; i++) {
621                            Type argType = argTypes[i];
622                            iv.load(reg, argType);
623                            //noinspection AssignmentToForLoopParameter
624                            reg += argType.getSize();
625                        }
626    
627                        if (callableDescriptor instanceof PropertyGetterDescriptor) {
628                            property.put(signature.getReturnType(), iv);
629                        }
630                        else {
631                            property.store(StackValue.onStack(property.type), iv, true);
632                        }
633    
634                        iv.areturn(signature.getReturnType());
635                    }
636                }
637    
638                if (accessor.isWithSyntheticGetterAccessor()) {
639                    PropertyGetterDescriptor getter = accessor.getGetter();
640                    assert getter != null;
641                    functionCodegen.generateMethod(Synthetic(null, original.getGetter() != null ? original.getGetter() : original),
642                                                   getter, new PropertyAccessorStrategy(getter));
643                }
644    
645                if (accessor.isVar() && accessor.isWithSyntheticSetterAccessor()) {
646                    PropertySetterDescriptor setter = accessor.getSetter();
647                    assert setter != null;
648    
649                    functionCodegen.generateMethod(Synthetic(null, original.getSetter() != null ? original.getSetter() : original),
650                                                   setter, new PropertyAccessorStrategy(setter));
651                }
652            }
653            else {
654                throw new UnsupportedOperationException();
655            }
656        }
657    
658        protected StackValue generateMethodCallTo(
659                @NotNull FunctionDescriptor functionDescriptor,
660                @Nullable FunctionDescriptor accessorDescriptor,
661                @NotNull InstructionAdapter iv
662        ) {
663            CallableMethod callableMethod = typeMapper.mapToCallableMethod(
664                    functionDescriptor,
665                    accessorDescriptor instanceof AccessorForCallableDescriptor &&
666                    ((AccessorForCallableDescriptor) accessorDescriptor).getSuperCallTarget() != null
667            );
668    
669            boolean hasDispatchReceiver = !isStaticDeclaration(functionDescriptor) && !isInterface(functionDescriptor.getContainingDeclaration());
670            int reg = hasDispatchReceiver ? 1 : 0;
671            boolean accessorIsConstructor = accessorDescriptor instanceof AccessorForConstructorDescriptor;
672            if (!accessorIsConstructor && functionDescriptor instanceof ConstructorDescriptor) {
673                iv.anew(callableMethod.getOwner());
674                iv.dup();
675                reg = 0;
676            }
677            else if (accessorIsConstructor || (accessorDescriptor != null && KotlinTypeMapper.isAccessor(accessorDescriptor) && hasDispatchReceiver)) {
678                if (!AnnotationUtilKt.isPlatformStaticInObjectOrClass(functionDescriptor)) {
679                    iv.load(0, OBJECT_TYPE);
680                }
681            }
682    
683            for (Type argType : callableMethod.getParameterTypes()) {
684                if (AsmTypes.DEFAULT_CONSTRUCTOR_MARKER.equals(argType)) {
685                    iv.aconst(null);
686                }
687                else {
688                    iv.load(reg, argType);
689                    reg += argType.getSize();
690                }
691            }
692    
693            callableMethod.genInvokeInstruction(iv);
694    
695            return StackValue.onStack(callableMethod.getReturnType());
696        }
697    }