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 kotlin.Unit;
021 import kotlin.jvm.functions.Function0;
022 import kotlin.jvm.functions.Function1;
023 import org.jetbrains.annotations.NotNull;
024 import org.jetbrains.annotations.Nullable;
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.JetTypeMapper;
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.FileClassesPackage;
033 import org.jetbrains.kotlin.fileClasses.JvmFileClassesProvider;
034 import org.jetbrains.kotlin.load.java.JvmAbi;
035 import org.jetbrains.kotlin.name.Name;
036 import org.jetbrains.kotlin.name.SpecialNames;
037 import org.jetbrains.kotlin.psi.*;
038 import org.jetbrains.kotlin.resolve.BindingContext;
039 import org.jetbrains.kotlin.resolve.BindingContextUtils;
040 import org.jetbrains.kotlin.resolve.BindingTrace;
041 import org.jetbrains.kotlin.resolve.TemporaryBindingTrace;
042 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
043 import org.jetbrains.kotlin.resolve.constants.ConstantValue;
044 import org.jetbrains.kotlin.resolve.constants.evaluate.ConstantExpressionEvaluator;
045 import org.jetbrains.kotlin.resolve.descriptorUtil.DescriptorUtilPackage;
046 import org.jetbrains.kotlin.resolve.jvm.AsmTypes;
047 import org.jetbrains.kotlin.resolve.source.SourcePackage;
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.JetType;
052 import org.jetbrains.org.objectweb.asm.MethodVisitor;
053 import org.jetbrains.org.objectweb.asm.Type;
054 import org.jetbrains.org.objectweb.asm.commons.InstructionAdapter;
055 import org.jetbrains.org.objectweb.asm.commons.Method;
056
057 import java.util.*;
058
059 import static org.jetbrains.kotlin.codegen.AsmUtil.calculateInnerClassAccessFlags;
060 import static org.jetbrains.kotlin.codegen.AsmUtil.isPrimitive;
061 import static org.jetbrains.kotlin.descriptors.CallableMemberDescriptor.Kind.SYNTHESIZED;
062 import static org.jetbrains.kotlin.resolve.BindingContext.VARIABLE;
063 import static org.jetbrains.kotlin.resolve.jvm.AsmTypes.*;
064 import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.OtherOrigin;
065 import static org.jetbrains.kotlin.resolve.jvm.diagnostics.DiagnosticsPackage.TraitImpl;
066 import static org.jetbrains.kotlin.resolve.jvm.diagnostics.JvmDeclarationOrigin.NO_ORIGIN;
067 import static org.jetbrains.org.objectweb.asm.Opcodes.*;
068
069 public abstract class MemberCodegen<T extends JetElement/* TODO: & JetDeclarationContainer*/> {
070 protected final GenerationState state;
071 protected final T element;
072 protected final FieldOwnerContext context;
073 protected final ClassBuilder v;
074 protected final FunctionCodegen functionCodegen;
075 protected final PropertyCodegen propertyCodegen;
076 protected final JetTypeMapper typeMapper;
077 protected final BindingContext bindingContext;
078 protected final JvmFileClassesProvider fileClassesProvider;
079 private final MemberCodegen<?> parentCodegen;
080 private final ReifiedTypeParametersUsages reifiedTypeParametersUsages = new ReifiedTypeParametersUsages();
081 protected final Collection<ClassDescriptor> innerClasses = new LinkedHashSet<ClassDescriptor>();
082
083 protected ExpressionCodegen clInit;
084 private NameGenerator inlineNameGenerator;
085
086 private SourceMapper sourceMapper;
087 private final ConstantExpressionEvaluator constantExpressionEvaluator;
088
089 public MemberCodegen(
090 @NotNull GenerationState state,
091 @Nullable MemberCodegen<?> parentCodegen,
092 @NotNull FieldOwnerContext context,
093 T element,
094 @NotNull ClassBuilder builder
095 ) {
096 this.state = state;
097 this.typeMapper = state.getTypeMapper();
098 this.bindingContext = state.getBindingContext();
099 this.fileClassesProvider = state.getFileClassesProvider();
100 this.element = element;
101 this.context = context;
102 this.v = builder;
103 this.functionCodegen = new FunctionCodegen(context, v, state, this);
104 this.propertyCodegen = new PropertyCodegen(context, v, functionCodegen, this);
105 this.parentCodegen = parentCodegen;
106 this.constantExpressionEvaluator = new ConstantExpressionEvaluator(state.getModule().getBuiltIns());
107 }
108
109 protected MemberCodegen(@NotNull MemberCodegen<T> wrapped, T declaration, FieldOwnerContext codegenContext) {
110 this(wrapped.state, wrapped.getParentCodegen(), codegenContext, declaration, wrapped.v);
111 }
112
113 public void generate() {
114 generateDeclaration();
115
116 generateBody();
117
118 generateSyntheticParts();
119
120 generateKotlinAnnotation();
121
122 done();
123 }
124
125 protected abstract void generateDeclaration();
126
127 protected abstract void generateBody();
128
129 protected void generateSyntheticParts() {
130 }
131
132 protected abstract void generateKotlinAnnotation();
133
134 @Nullable
135 protected ClassDescriptor classForInnerClassRecord() {
136 return null;
137 }
138
139 protected void done() {
140 if (clInit != null) {
141 clInit.v.visitInsn(RETURN);
142 FunctionCodegen.endVisit(clInit.v, "static initializer", element);
143 }
144
145 writeInnerClasses();
146
147 if (sourceMapper != null) {
148 SourceMapper.Companion.flushToClassBuilder(sourceMapper, v);
149 }
150
151 v.done();
152 }
153
154 public void genFunctionOrProperty(@NotNull JetDeclaration functionOrProperty) {
155 if (functionOrProperty instanceof JetNamedFunction) {
156 try {
157 functionCodegen.gen((JetNamedFunction) functionOrProperty);
158 }
159 catch (ProcessCanceledException e) {
160 throw e;
161 }
162 catch (CompilationException e) {
163 throw e;
164 }
165 catch (Exception e) {
166 throw new CompilationException("Failed to generate function " + functionOrProperty.getName(), e, functionOrProperty);
167 }
168 }
169 else if (functionOrProperty instanceof JetProperty) {
170 try {
171 propertyCodegen.gen((JetProperty) functionOrProperty);
172 }
173 catch (ProcessCanceledException e) {
174 throw e;
175 }
176 catch (CompilationException e) {
177 throw e;
178 }
179 catch (Exception e) {
180 throw new CompilationException("Failed to generate property " + functionOrProperty.getName(), e, functionOrProperty);
181 }
182 }
183 else {
184 throw new IllegalArgumentException("Unknown parameter: " + functionOrProperty);
185 }
186 }
187
188 public static void genClassOrObject(
189 @NotNull CodegenContext parentContext,
190 @NotNull JetClassOrObject aClass,
191 @NotNull GenerationState state,
192 @Nullable MemberCodegen<?> parentCodegen
193 ) {
194 ClassDescriptor descriptor = state.getBindingContext().get(BindingContext.CLASS, aClass);
195
196 if (descriptor == null || ErrorUtils.isError(descriptor)) {
197 badDescriptor(descriptor, state.getClassBuilderMode());
198 return;
199 }
200
201 if (descriptor.getName().equals(SpecialNames.NO_NAME_PROVIDED)) {
202 badDescriptor(descriptor, state.getClassBuilderMode());
203 }
204
205 Type classType = state.getTypeMapper().mapClass(descriptor);
206 ClassBuilder classBuilder = state.getFactory().newVisitor(OtherOrigin(aClass, descriptor), classType, aClass.getContainingFile());
207 ClassContext classContext = parentContext.intoClass(descriptor, OwnerKind.IMPLEMENTATION, state);
208 new ImplementationBodyCodegen(aClass, classContext, classBuilder, state, parentCodegen).generate();
209
210 if (aClass instanceof JetClass && ((JetClass) aClass).isInterface()) {
211 Type traitImplType = state.getTypeMapper().mapTraitImpl(descriptor);
212 ClassBuilder traitImplBuilder = state.getFactory().newVisitor(TraitImpl(aClass, descriptor), traitImplType, aClass.getContainingFile());
213 ClassContext traitImplContext = parentContext.intoClass(descriptor, OwnerKind.TRAIT_IMPL, state);
214 new TraitImplBodyCodegen(aClass, traitImplContext, traitImplBuilder, state, parentCodegen).generate();
215 }
216 }
217
218 private static void badDescriptor(ClassDescriptor descriptor, ClassBuilderMode mode) {
219 if (mode != ClassBuilderMode.LIGHT_CLASSES) {
220 throw new IllegalStateException("Generating bad descriptor in ClassBuilderMode = " + mode + ": " + descriptor);
221 }
222 }
223
224 public void genClassOrObject(JetClassOrObject aClass) {
225 genClassOrObject(context, aClass, state, this);
226 }
227
228 private void writeInnerClasses() {
229 // JVMS7 (4.7.6): a nested class or interface member will have InnerClasses information
230 // for each enclosing class and for each immediate member
231 ClassDescriptor classDescriptor = classForInnerClassRecord();
232 if (classDescriptor != null) {
233 if (parentCodegen != null) {
234 parentCodegen.innerClasses.add(classDescriptor);
235 }
236
237 for (MemberCodegen<?> codegen = this; codegen != null; codegen = codegen.getParentCodegen()) {
238 ClassDescriptor outerClass = codegen.classForInnerClassRecord();
239 if (outerClass != null) {
240 innerClasses.add(outerClass);
241 }
242 }
243 }
244
245 for (ClassDescriptor innerClass : innerClasses) {
246 writeInnerClass(innerClass);
247 }
248 }
249
250 private void writeInnerClass(@NotNull ClassDescriptor innerClass) {
251 DeclarationDescriptor containing = innerClass.getContainingDeclaration();
252 String outerClassInternalName = null;
253 if (containing instanceof ClassDescriptor) {
254 outerClassInternalName = typeMapper.mapClass((ClassDescriptor) containing).getInternalName();
255 } /* disabled cause of KT-7775
256 else if (containing instanceof ScriptDescriptor) {
257 outerClassInternalName = asmTypeForScriptDescriptor(bindingContext, (ScriptDescriptor) containing).getInternalName();
258 }*/
259
260 String innerName = innerClass.getName().isSpecial() ? null : innerClass.getName().asString();
261
262 String innerClassInternalName = typeMapper.mapClass(innerClass).getInternalName();
263 v.visitInnerClass(innerClassInternalName, outerClassInternalName, innerName, calculateInnerClassAccessFlags(innerClass));
264 }
265
266 protected void writeOuterClassAndEnclosingMethod() {
267 CodegenContext context = this.context.getParentContext();
268 while (context instanceof MethodContext && ((MethodContext) context).isInliningLambda()) {
269 // If this is a lambda which will be inlined, skip its MethodContext and enclosing ClosureContext
270 //noinspection ConstantConditions
271 context = context.getParentContext().getParentContext();
272 }
273 assert context != null : "Outermost context can't be null: " + this.context;
274
275 Type enclosingAsmType = computeOuterClass(context);
276 if (enclosingAsmType != null) {
277 Method method = computeEnclosingMethod(context);
278
279 v.visitOuterClass(
280 enclosingAsmType.getInternalName(),
281 method == null ? null : method.getName(),
282 method == null ? null : method.getDescriptor()
283 );
284 }
285 }
286
287 @Nullable
288 private Type computeOuterClass(@NotNull CodegenContext<?> context) {
289 CodegenContext<? extends ClassOrPackageFragmentDescriptor> outermost = context.getClassOrPackageParentContext();
290 if (outermost instanceof ClassContext) {
291 return typeMapper.mapType(((ClassContext) outermost).getContextDescriptor());
292 }
293 else if (outermost instanceof DelegatingFacadeContext || outermost instanceof DelegatingToPartContext) {
294 Type implementationOwnerType = CodegenContextUtil.getImplementationOwnerClassType(outermost);
295 if (implementationOwnerType != null) {
296 return implementationOwnerType;
297 }
298 else {
299 return FileClassesPackage.getFileClassType(fileClassesProvider, element.getContainingJetFile());
300 }
301 }
302 /*disabled cause of KT-7775
303 else if (outermost instanceof ScriptContext) {
304 return asmTypeForScriptDescriptor(bindingContext, ((ScriptContext) outermost).getScriptDescriptor());
305 }*/
306 return null;
307 }
308
309 @Nullable
310 private Method computeEnclosingMethod(@NotNull CodegenContext context) {
311 if (context instanceof MethodContext) {
312 Method method = typeMapper.mapSignature(((MethodContext) context).getFunctionDescriptor()).getAsmMethod();
313 if (!method.getName().equals("<clinit>")) {
314 return method;
315 }
316 }
317 return null;
318 }
319
320 @NotNull
321 public NameGenerator getInlineNameGenerator() {
322 if (inlineNameGenerator == null) {
323 String prefix = InlineCodegenUtil.getInlineName(context, typeMapper, fileClassesProvider);
324 inlineNameGenerator = new NameGenerator(prefix);
325 }
326 return inlineNameGenerator;
327 }
328
329 @NotNull
330 protected ExpressionCodegen createOrGetClInitCodegen() {
331 DeclarationDescriptor descriptor = context.getContextDescriptor();
332 if (clInit == null) {
333 MethodVisitor mv = v.newMethod(OtherOrigin(descriptor), ACC_STATIC, "<clinit>", "()V", null, null);
334 SimpleFunctionDescriptorImpl clInit =
335 SimpleFunctionDescriptorImpl.create(descriptor, Annotations.EMPTY, Name.special("<clinit>"), SYNTHESIZED,
336 SourcePackage.toSourceElement(element));
337 clInit.initialize(null, null, Collections.<TypeParameterDescriptor>emptyList(),
338 Collections.<ValueParameterDescriptor>emptyList(),
339 DescriptorUtilPackage.getModule(descriptor).getBuiltIns().getUnitType(),
340 null, Visibilities.PRIVATE);
341
342 this.clInit = new ExpressionCodegen(mv, new FrameMap(), Type.VOID_TYPE, context.intoFunction(clInit), state, this);
343 }
344 return clInit;
345 }
346
347 protected void generateInitializers(@NotNull Function0<ExpressionCodegen> createCodegen) {
348 NotNullLazyValue<ExpressionCodegen> codegen = LockBasedStorageManager.NO_LOCKS.createLazyValue(createCodegen);
349 for (JetDeclaration declaration : ((JetDeclarationContainer) element).getDeclarations()) {
350 if (declaration instanceof JetProperty) {
351 if (shouldInitializeProperty((JetProperty) declaration)) {
352 initializeProperty(codegen.invoke(), (JetProperty) declaration);
353 }
354 }
355 else if (declaration instanceof JetClassInitializer) {
356 JetExpression body = ((JetClassInitializer) declaration).getBody();
357 if (body != null) {
358 codegen.invoke().gen(body, Type.VOID_TYPE);
359 }
360 }
361 }
362 }
363
364 private void initializeProperty(@NotNull ExpressionCodegen codegen, @NotNull JetProperty property) {
365 PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
366 assert propertyDescriptor != null;
367
368 JetExpression initializer = property.getDelegateExpressionOrInitializer();
369 assert initializer != null : "shouldInitializeProperty must return false if initializer is null";
370
371 StackValue.Property propValue = codegen.intermediateValueForProperty(propertyDescriptor, true, null, true, StackValue.LOCAL_0);
372
373 propValue.store(codegen.gen(initializer), codegen.v);
374
375 ResolvedCall<FunctionDescriptor> pdResolvedCall =
376 bindingContext.get(BindingContext.DELEGATED_PROPERTY_PD_RESOLVED_CALL, propertyDescriptor);
377 if (pdResolvedCall != null) {
378 int index = PropertyCodegen.indexOfDelegatedProperty(property);
379 StackValue lastValue = PropertyCodegen.invokeDelegatedPropertyConventionMethod(
380 propertyDescriptor, codegen, typeMapper, pdResolvedCall, index, 0
381 );
382 lastValue.put(Type.VOID_TYPE, codegen.v);
383 }
384 }
385
386 private boolean shouldInitializeProperty(@NotNull JetProperty property) {
387 if (!property.hasDelegateExpressionOrInitializer()) return false;
388
389 PropertyDescriptor propertyDescriptor = (PropertyDescriptor) bindingContext.get(VARIABLE, property);
390 assert propertyDescriptor != null;
391
392 JetExpression initializer = property.getInitializer();
393
394 ConstantValue<?> initializerValue = computeInitializerValue(property, propertyDescriptor, initializer);
395 // we must write constant values for fields in light classes,
396 // because Java's completion for annotation arguments uses this information
397 if (initializerValue == null) return state.getClassBuilderMode() != ClassBuilderMode.LIGHT_CLASSES;
398
399 //TODO: OPTIMIZATION: don't initialize static final fields
400 JetType jetType = getPropertyOrDelegateType(property, propertyDescriptor);
401 Type type = typeMapper.mapType(jetType);
402 return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type);
403 }
404
405 @Nullable
406 private ConstantValue<?> computeInitializerValue(
407 @NotNull JetProperty property,
408 @NotNull PropertyDescriptor propertyDescriptor,
409 @Nullable JetExpression initializer
410 ) {
411 if (property.isVar() && initializer != null) {
412 BindingTrace tempTrace = TemporaryBindingTrace.create(state.getBindingTrace(), "property initializer");
413 return constantExpressionEvaluator.evaluateToConstantValue(initializer, tempTrace, propertyDescriptor.getType());
414 }
415 return propertyDescriptor.getCompileTimeInitializer();
416 }
417
418 @NotNull
419 private JetType getPropertyOrDelegateType(@NotNull JetProperty property, @NotNull PropertyDescriptor descriptor) {
420 JetExpression delegateExpression = property.getDelegateExpression();
421 if (delegateExpression != null) {
422 JetType delegateType = bindingContext.getType(delegateExpression);
423 assert delegateType != null : "Type of delegate expression should be recorded";
424 return delegateType;
425 }
426 return descriptor.getType();
427 }
428
429 private static boolean skipDefaultValue(@NotNull PropertyDescriptor propertyDescriptor, Object value, @NotNull Type type) {
430 if (isPrimitive(type)) {
431 if (!propertyDescriptor.getType().isMarkedNullable() && value instanceof Number) {
432 if (type == Type.INT_TYPE && ((Number) value).intValue() == 0) {
433 return true;
434 }
435 if (type == Type.BYTE_TYPE && ((Number) value).byteValue() == 0) {
436 return true;
437 }
438 if (type == Type.LONG_TYPE && ((Number) value).longValue() == 0L) {
439 return true;
440 }
441 if (type == Type.SHORT_TYPE && ((Number) value).shortValue() == 0) {
442 return true;
443 }
444 if (type == Type.DOUBLE_TYPE && ((Number) value).doubleValue() == 0d) {
445 return true;
446 }
447 if (type == Type.FLOAT_TYPE && ((Number) value).floatValue() == 0f) {
448 return true;
449 }
450 }
451 if (type == Type.BOOLEAN_TYPE && value instanceof Boolean && !((Boolean) value)) {
452 return true;
453 }
454 if (type == Type.CHAR_TYPE && value instanceof Character && ((Character) value) == 0) {
455 return true;
456 }
457 }
458 else {
459 if (value == null) {
460 return true;
461 }
462 }
463 return false;
464 }
465
466 public static void generateReflectionObjectField(
467 @NotNull GenerationState state,
468 @NotNull Type thisAsmType,
469 @NotNull ClassBuilder classBuilder,
470 @NotNull Method factory,
471 @NotNull String fieldName,
472 @NotNull InstructionAdapter v
473 ) {
474 String type = factory.getReturnType().getDescriptor();
475 // TODO: generic signature
476 classBuilder.newField(NO_ORIGIN, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, fieldName, type, null, null);
477
478 if (state.getClassBuilderMode() == ClassBuilderMode.LIGHT_CLASSES) return;
479
480 v.aconst(thisAsmType);
481 if (factory.getArgumentTypes().length == 2) {
482 v.aconst(state.getModuleName());
483 }
484 v.invokestatic(REFLECTION, factory.getName(), factory.getDescriptor(), false);
485 v.putstatic(thisAsmType.getInternalName(), fieldName, type);
486 }
487
488 public static void generateModuleNameField(
489 @NotNull GenerationState state,
490 @NotNull ClassBuilder classBuilder
491 ) {
492 classBuilder.newField(NO_ORIGIN, ACC_PUBLIC | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.MODULE_NAME_FIELD,
493 AsmTypes.JAVA_STRING_TYPE.getDescriptor(), null, state.getModuleName());
494 }
495
496 protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) {
497 List<JetProperty> delegatedProperties = new ArrayList<JetProperty>();
498 for (JetDeclaration declaration : ((JetDeclarationContainer) element).getDeclarations()) {
499 if (declaration instanceof JetProperty) {
500 JetProperty property = (JetProperty) declaration;
501 if (property.hasDelegate()) {
502 delegatedProperties.add(property);
503 }
504 }
505 }
506 if (delegatedProperties.isEmpty()) return;
507
508 v.newField(NO_ORIGIN, ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.PROPERTY_METADATA_ARRAY_NAME,
509 "[" + PROPERTY_METADATA_TYPE, null, null);
510
511 if (state.getClassBuilderMode() == ClassBuilderMode.LIGHT_CLASSES) return;
512
513 InstructionAdapter iv = createOrGetClInitCodegen().v;
514 iv.iconst(delegatedProperties.size());
515 iv.newarray(PROPERTY_METADATA_TYPE);
516
517 for (int i = 0, size = delegatedProperties.size(); i < size; i++) {
518 VariableDescriptor property = BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i));
519
520 iv.dup();
521 iv.iconst(i);
522 iv.anew(PROPERTY_METADATA_IMPL_TYPE);
523 iv.dup();
524 iv.visitLdcInsn(property.getName().asString());
525 iv.invokespecial(PROPERTY_METADATA_IMPL_TYPE.getInternalName(), "<init>", "(Ljava/lang/String;)V", false);
526 iv.astore(PROPERTY_METADATA_IMPL_TYPE);
527 }
528
529 iv.putstatic(thisAsmType.getInternalName(), JvmAbi.PROPERTY_METADATA_ARRAY_NAME, "[" + PROPERTY_METADATA_TYPE);
530 }
531
532 public String getClassName() {
533 return v.getThisName();
534 }
535
536 @NotNull
537 public FieldOwnerContext<?> getContext() {
538 return context;
539 }
540
541 @NotNull
542 public ReifiedTypeParametersUsages getReifiedTypeParametersUsages() {
543 return reifiedTypeParametersUsages;
544 }
545
546 public MemberCodegen<?> getParentCodegen() {
547 return parentCodegen;
548 }
549
550 @Override
551 public String toString() {
552 return context.toString();
553 }
554
555 @NotNull
556 public SourceMapper getOrCreateSourceMapper() {
557 if (sourceMapper == null) {
558 sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()), null);
559 }
560 return sourceMapper;
561 }
562
563 protected void generateConstInstance(
564 @NotNull Type thisAsmType,
565 @NotNull Type fieldAsmType,
566 @NotNull Function1<InstructionAdapter, Unit> initialization
567 ) {
568 v.newField(OtherOrigin(element), ACC_STATIC | ACC_FINAL | ACC_PUBLIC, JvmAbi.INSTANCE_FIELD, fieldAsmType.getDescriptor(),
569 null, null);
570
571 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) {
572 InstructionAdapter iv = createOrGetClInitCodegen().v;
573 iv.anew(thisAsmType);
574 iv.dup();
575 iv.invokespecial(thisAsmType.getInternalName(), "<init>", "()V", false);
576 initialization.invoke(iv);
577 iv.putstatic(thisAsmType.getInternalName(), JvmAbi.INSTANCE_FIELD, fieldAsmType.getDescriptor());
578 }
579 }
580 }