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 SourceMapper sourceMapper; 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 (shouldGenerateMetadata(state.getClassBuilderMode())) { 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 == ClassBuilderMode.FULL) { 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 = computeInitializerValue(property, propertyDescriptor, initializer); 412 // we must write constant values for fields in light classes, 413 // because Java's completion for annotation arguments uses this information 414 if (initializerValue == null) return state.getClassBuilderMode() == ClassBuilderMode.FULL; 415 416 //TODO: OPTIMIZATION: don't initialize static final fields 417 KotlinType jetType = getPropertyOrDelegateType(property, propertyDescriptor); 418 Type type = typeMapper.mapType(jetType); 419 return !skipDefaultValue(propertyDescriptor, initializerValue.getValue(), type); 420 } 421 422 @Nullable 423 private ConstantValue<?> computeInitializerValue( 424 @NotNull KtProperty property, 425 @NotNull PropertyDescriptor propertyDescriptor, 426 @Nullable KtExpression initializer 427 ) { 428 if (property.isVar() && initializer != null) { 429 BindingTrace tempTrace = TemporaryBindingTrace.create(state.getBindingTrace(), "property initializer"); 430 return constantExpressionEvaluator.evaluateToConstantValue(initializer, tempTrace, propertyDescriptor.getType()); 431 } 432 return propertyDescriptor.getCompileTimeInitializer(); 433 } 434 435 @NotNull 436 private KotlinType getPropertyOrDelegateType(@NotNull KtProperty property, @NotNull PropertyDescriptor descriptor) { 437 KtExpression delegateExpression = property.getDelegateExpression(); 438 if (delegateExpression != null) { 439 KotlinType delegateType = bindingContext.getType(delegateExpression); 440 assert delegateType != null : "Type of delegate expression should be recorded"; 441 return delegateType; 442 } 443 return descriptor.getType(); 444 } 445 446 private static boolean skipDefaultValue(@NotNull PropertyDescriptor propertyDescriptor, Object value, @NotNull Type type) { 447 if (isPrimitive(type)) { 448 if (!propertyDescriptor.getType().isMarkedNullable() && value instanceof Number) { 449 if (type == Type.INT_TYPE && ((Number) value).intValue() == 0) { 450 return true; 451 } 452 if (type == Type.BYTE_TYPE && ((Number) value).byteValue() == 0) { 453 return true; 454 } 455 if (type == Type.LONG_TYPE && ((Number) value).longValue() == 0L) { 456 return true; 457 } 458 if (type == Type.SHORT_TYPE && ((Number) value).shortValue() == 0) { 459 return true; 460 } 461 if (type == Type.DOUBLE_TYPE && ((Number) value).doubleValue() == 0d) { 462 return true; 463 } 464 if (type == Type.FLOAT_TYPE && ((Number) value).floatValue() == 0f) { 465 return true; 466 } 467 } 468 if (type == Type.BOOLEAN_TYPE && value instanceof Boolean && !((Boolean) value)) { 469 return true; 470 } 471 if (type == Type.CHAR_TYPE && value instanceof Character && ((Character) value) == 0) { 472 return true; 473 } 474 } 475 else { 476 if (value == null) { 477 return true; 478 } 479 } 480 return false; 481 } 482 483 protected void generatePropertyMetadataArrayFieldIfNeeded(@NotNull Type thisAsmType) { 484 List<KtProperty> delegatedProperties = new ArrayList<KtProperty>(); 485 for (KtDeclaration declaration : ((KtDeclarationContainer) element).getDeclarations()) { 486 if (declaration instanceof KtProperty) { 487 KtProperty property = (KtProperty) declaration; 488 if (property.hasDelegate()) { 489 delegatedProperties.add(property); 490 } 491 } 492 } 493 if (delegatedProperties.isEmpty()) return; 494 495 v.newField(NO_ORIGIN, ACC_PRIVATE | ACC_STATIC | ACC_FINAL | ACC_SYNTHETIC, JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, 496 "[" + K_PROPERTY_TYPE, null, null); 497 498 if (state.getClassBuilderMode() != ClassBuilderMode.FULL) return; 499 500 InstructionAdapter iv = createOrGetClInitCodegen().v; 501 iv.iconst(delegatedProperties.size()); 502 iv.newarray(K_PROPERTY_TYPE); 503 504 for (int i = 0, size = delegatedProperties.size(); i < size; i++) { 505 PropertyDescriptor property = 506 (PropertyDescriptor) BindingContextUtils.getNotNull(bindingContext, VARIABLE, delegatedProperties.get(i)); 507 508 iv.dup(); 509 iv.iconst(i); 510 511 int receiverCount = (property.getDispatchReceiverParameter() != null ? 1 : 0) + 512 (property.getExtensionReceiverParameter() != null ? 1 : 0); 513 Type implType = property.isVar() ? MUTABLE_PROPERTY_REFERENCE_IMPL[receiverCount] : PROPERTY_REFERENCE_IMPL[receiverCount]; 514 iv.anew(implType); 515 iv.dup(); 516 // TODO: generate the container once and save to a local field instead (KT-10495) 517 ClosureCodegen.generateCallableReferenceDeclarationContainer(iv, property, state); 518 iv.aconst(property.getName().asString()); 519 iv.aconst(PropertyReferenceCodegen.getPropertyReferenceSignature(property, state)); 520 iv.invokespecial( 521 implType.getInternalName(), "<init>", 522 Type.getMethodDescriptor(Type.VOID_TYPE, K_DECLARATION_CONTAINER_TYPE, JAVA_STRING_TYPE, JAVA_STRING_TYPE), false 523 ); 524 Method wrapper = PropertyReferenceCodegen.getWrapperMethodForPropertyReference(property, receiverCount); 525 iv.invokestatic(REFLECTION, wrapper.getName(), wrapper.getDescriptor(), false); 526 527 StackValue.onStack(implType).put(K_PROPERTY_TYPE, iv); 528 529 iv.astore(K_PROPERTY_TYPE); 530 } 531 532 iv.putstatic(thisAsmType.getInternalName(), JvmAbi.DELEGATED_PROPERTIES_ARRAY_NAME, "[" + K_PROPERTY_TYPE); 533 } 534 535 public String getClassName() { 536 return v.getThisName(); 537 } 538 539 @NotNull 540 public FieldOwnerContext<?> getContext() { 541 return context; 542 } 543 544 @NotNull 545 public ReifiedTypeParametersUsages getReifiedTypeParametersUsages() { 546 return reifiedTypeParametersUsages; 547 } 548 549 public MemberCodegen<?> getParentCodegen() { 550 return parentCodegen; 551 } 552 553 @Override 554 public String toString() { 555 return context.toString(); 556 } 557 558 @NotNull 559 public SourceMapper getOrCreateSourceMapper() { 560 if (sourceMapper == null) { 561 sourceMapper = new DefaultSourceMapper(SourceInfo.Companion.createInfo(element, getClassName()), null); 562 } 563 return sourceMapper; 564 } 565 566 protected void generateConstInstance(@NotNull Type thisAsmType, @NotNull Type fieldAsmType) { 567 v.newField( 568 JvmDeclarationOriginKt.OtherOrigin(element), ACC_STATIC | ACC_FINAL | ACC_PUBLIC, JvmAbi.INSTANCE_FIELD, 569 fieldAsmType.getDescriptor(), null, null 570 ); 571 572 if (state.getClassBuilderMode() == ClassBuilderMode.FULL) { 573 InstructionAdapter iv = createOrGetClInitCodegen().v; 574 iv.anew(thisAsmType); 575 iv.dup(); 576 iv.invokespecial(thisAsmType.getInternalName(), "<init>", "()V", false); 577 iv.putstatic(thisAsmType.getInternalName(), JvmAbi.INSTANCE_FIELD, fieldAsmType.getDescriptor()); 578 } 579 } 580 581 protected void generateSyntheticAccessors() { 582 for (AccessorForCallableDescriptor<?> accessor : ((CodegenContext<?>) context).getAccessors()) { 583 generateSyntheticAccessor(accessor); 584 } 585 } 586 587 private void generateSyntheticAccessor(@NotNull AccessorForCallableDescriptor<?> accessorForCallableDescriptor) { 588 if (accessorForCallableDescriptor instanceof FunctionDescriptor) { 589 final FunctionDescriptor accessor = (FunctionDescriptor) accessorForCallableDescriptor; 590 final FunctionDescriptor original = (FunctionDescriptor) accessorForCallableDescriptor.getCalleeDescriptor(); 591 functionCodegen.generateMethod( 592 Synthetic(null, original), accessor, 593 new FunctionGenerationStrategy.CodegenBased<FunctionDescriptor>(state, accessor) { 594 @Override 595 public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { 596 markLineNumberForElement(element, codegen.v); 597 598 generateMethodCallTo(original, accessor, codegen.v).coerceTo(signature.getReturnType(), codegen.v); 599 600 codegen.v.areturn(signature.getReturnType()); 601 } 602 } 603 ); 604 } 605 else if (accessorForCallableDescriptor instanceof AccessorForPropertyDescriptor) { 606 final AccessorForPropertyDescriptor accessor = (AccessorForPropertyDescriptor) accessorForCallableDescriptor; 607 final PropertyDescriptor original = accessor.getCalleeDescriptor(); 608 609 class PropertyAccessorStrategy extends FunctionGenerationStrategy.CodegenBased<PropertyAccessorDescriptor> { 610 public PropertyAccessorStrategy(@NotNull PropertyAccessorDescriptor callableDescriptor) { 611 super(MemberCodegen.this.state, callableDescriptor); 612 } 613 614 @Override 615 public void doGenerateBody(@NotNull ExpressionCodegen codegen, @NotNull JvmMethodSignature signature) { 616 boolean syntheticBackingField = accessor instanceof AccessorForPropertyBackingFieldFromLocal; 617 boolean forceFieldForCompanionProperty = JvmAbi.isPropertyWithBackingFieldInOuterClass(original) && 618 !isCompanionObject(accessor.getContainingDeclaration()); 619 boolean forceField = forceFieldForCompanionProperty || 620 syntheticBackingField || 621 original.getVisibility() == JavaVisibilities.PROTECTED_STATIC_VISIBILITY; 622 StackValue property = codegen.intermediateValueForProperty( 623 original, forceField, syntheticBackingField, accessor.getSuperCallTarget(), forceFieldForCompanionProperty, StackValue.none() 624 ); 625 626 InstructionAdapter iv = codegen.v; 627 628 markLineNumberForElement(element, iv); 629 630 Type[] argTypes = signature.getAsmMethod().getArgumentTypes(); 631 for (int i = 0, reg = 0; i < argTypes.length; i++) { 632 Type argType = argTypes[i]; 633 iv.load(reg, argType); 634 //noinspection AssignmentToForLoopParameter 635 reg += argType.getSize(); 636 } 637 638 if (callableDescriptor instanceof PropertyGetterDescriptor) { 639 property.put(signature.getReturnType(), iv); 640 } 641 else { 642 property.store(StackValue.onStack(property.type), iv, true); 643 } 644 645 iv.areturn(signature.getReturnType()); 646 } 647 } 648 649 if (accessor.isWithSyntheticGetterAccessor()) { 650 PropertyGetterDescriptor getter = accessor.getGetter(); 651 assert getter != null; 652 functionCodegen.generateMethod(Synthetic(null, original.getGetter() != null ? original.getGetter() : original), 653 getter, new PropertyAccessorStrategy(getter)); 654 } 655 656 if (accessor.isVar() && accessor.isWithSyntheticSetterAccessor()) { 657 PropertySetterDescriptor setter = accessor.getSetter(); 658 assert setter != null; 659 660 functionCodegen.generateMethod(Synthetic(null, original.getSetter() != null ? original.getSetter() : original), 661 setter, new PropertyAccessorStrategy(setter)); 662 } 663 } 664 else { 665 throw new UnsupportedOperationException(); 666 } 667 } 668 669 protected StackValue generateMethodCallTo( 670 @NotNull FunctionDescriptor functionDescriptor, 671 @Nullable FunctionDescriptor accessorDescriptor, 672 @NotNull InstructionAdapter iv 673 ) { 674 CallableMethod callableMethod = typeMapper.mapToCallableMethod( 675 functionDescriptor, 676 accessorDescriptor instanceof AccessorForCallableDescriptor && 677 ((AccessorForCallableDescriptor) accessorDescriptor).getSuperCallTarget() != null 678 ); 679 680 boolean hasDispatchReceiver = !isStaticDeclaration(functionDescriptor) && !isInterface(functionDescriptor.getContainingDeclaration()); 681 int reg = hasDispatchReceiver ? 1 : 0; 682 boolean accessorIsConstructor = accessorDescriptor instanceof AccessorForConstructorDescriptor; 683 if (!accessorIsConstructor && functionDescriptor instanceof ConstructorDescriptor) { 684 iv.anew(callableMethod.getOwner()); 685 iv.dup(); 686 reg = 0; 687 } 688 else if (accessorIsConstructor || (accessorDescriptor != null && KotlinTypeMapper.isAccessor(accessorDescriptor) && hasDispatchReceiver)) { 689 if (!AnnotationUtilKt.isPlatformStaticInObjectOrClass(functionDescriptor)) { 690 iv.load(0, OBJECT_TYPE); 691 } 692 } 693 694 for (Type argType : callableMethod.getParameterTypes()) { 695 if (AsmTypes.DEFAULT_CONSTRUCTOR_MARKER.equals(argType)) { 696 iv.aconst(null); 697 } 698 else { 699 iv.load(reg, argType); 700 reg += argType.getSize(); 701 } 702 } 703 704 callableMethod.genInvokeInstruction(iv); 705 706 return StackValue.onStack(callableMethod.getReturnType()); 707 } 708 }