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