public static boolean isLocalNamedFun(DeclarationDescriptor fd) { if (fd instanceof FunctionDescriptor) { FunctionDescriptor descriptor = (FunctionDescriptor) fd; return descriptor.getVisibility() == Visibilities.LOCAL && !descriptor.getName().isSpecial(); } return false; }
public void serialize(FunctionDescriptor fun) { serialize(fun.getModality()); sb.append(" "); if (!fun.getAnnotations().isEmpty()) { new Serializer(sb).serializeSeparated(fun.getAnnotations(), " "); sb.append(" "); } if (!fun.getOverriddenDescriptors().isEmpty()) { sb.append("override /*" + fun.getOverriddenDescriptors().size() + "*/ "); } if (fun instanceof ConstructorDescriptor) { sb.append("/*constructor*/ "); } sb.append("fun "); if (!fun.getTypeParameters().isEmpty()) { sb.append("<"); new Serializer(sb).serializeCommaSeparated(fun.getTypeParameters()); sb.append(">"); } if (fun.getReceiverParameter().exists()) { new TypeSerializer(sb).serialize(fun.getReceiverParameter()); sb.append("."); } sb.append(fun.getName()); sb.append("("); new TypeSerializer(sb).serializeCommaSeparated(fun.getValueParameters()); sb.append("): "); new TypeSerializer(sb).serialize(fun.getReturnType()); }
@Override public StackValue innerValue( DeclarationDescriptor d, LocalLookup localLookup, GenerationState state, MutableClosure closure, Type classType) { FunctionDescriptor vd = (FunctionDescriptor) d; boolean idx = localLookup != null && localLookup.lookupLocal(vd); if (!idx) return null; BindingContext bindingContext = state.getBindingContext(); Type localType = asmTypeForAnonymousClass(bindingContext, vd); MutableClosure localFunClosure = bindingContext.get(CLOSURE, bindingContext.get(CLASS_FOR_FUNCTION, vd)); if (localFunClosure != null && JvmCodegenUtil.isConst(localFunClosure)) { // This is an optimization: we can obtain an instance of a const closure simply by // GETSTATIC ...$instance // (instead of passing this instance to the constructor and storing as a field) return StackValue.field(localType, localType, JvmAbi.INSTANCE_FIELD, true); } String fieldName = "$" + vd.getName(); StackValue innerValue = StackValue.field(localType, classType, fieldName, false); closure.recordField(fieldName, localType); closure.captureVariable(new EnclosedValueDescriptor(fieldName, d, innerValue, localType)); return innerValue; }
public static void genNotNullAssertionsForParameters( @NotNull InstructionAdapter v, @NotNull GenerationState state, @NotNull FunctionDescriptor descriptor, @NotNull FrameMap frameMap) { if (!state.isGenerateNotNullParamAssertions()) return; // Private method is not accessible from other classes, no assertions needed if (getVisibilityAccessFlag(descriptor) == ACC_PRIVATE) return; for (ValueParameterDescriptor parameter : descriptor.getValueParameters()) { JetType type = parameter.getReturnType(); if (type == null || isNullableType(type)) continue; int index = frameMap.getIndex(parameter); Type asmType = state.getTypeMapper().mapReturnType(type); if (asmType.getSort() == Type.OBJECT || asmType.getSort() == Type.ARRAY) { v.load(index, asmType); v.visitLdcInsn(descriptor.getName().asString()); v.invokestatic( "jet/runtime/Intrinsics", "checkParameterIsNotNull", "(Ljava/lang/Object;Ljava/lang/String;)V"); } } }
private static boolean isMethodOfAny(@NotNull FunctionDescriptor descriptor) { String name = descriptor.getName().asString(); List<ValueParameterDescriptor> parameters = descriptor.getValueParameters(); if (parameters.isEmpty()) { return name.equals("hashCode") || name.equals("toString"); } else if (parameters.size() == 1 && name.equals("equals")) { return isNullableAny(parameters.get(0).getType()); } return false; }
@Override protected void generateBody() { FunctionDescriptor erasedInterfaceFunction; if (samType == null) { erasedInterfaceFunction = getErasedInvokeFunction(funDescriptor); } else { erasedInterfaceFunction = samType.getAbstractMethod().getOriginal(); } generateBridge( typeMapper.mapSignature(erasedInterfaceFunction).getAsmMethod(), typeMapper.mapSignature(funDescriptor).getAsmMethod()); functionCodegen.generateMethod(OtherOrigin(element, funDescriptor), funDescriptor, strategy); // TODO: rewrite cause ugly hack if (samType != null) { SimpleFunctionDescriptorImpl descriptorForBridges = SimpleFunctionDescriptorImpl.create( funDescriptor.getContainingDeclaration(), funDescriptor.getAnnotations(), erasedInterfaceFunction.getName(), CallableMemberDescriptor.Kind.DECLARATION, funDescriptor.getSource()); descriptorForBridges.initialize( null, erasedInterfaceFunction.getDispatchReceiverParameter(), erasedInterfaceFunction.getTypeParameters(), erasedInterfaceFunction.getValueParameters(), erasedInterfaceFunction.getReturnType(), Modality.OPEN, erasedInterfaceFunction.getVisibility()); descriptorForBridges.addOverriddenDescriptor(erasedInterfaceFunction); functionCodegen.generateBridges(descriptorForBridges); } this.constructor = generateConstructor(superClassAsmType); if (isConst(closure)) { generateConstInstance(); } genClosureFields(closure, v, typeMapper); functionCodegen.generateDefaultIfNeeded( context.intoFunction(funDescriptor), funDescriptor, context.getContextKind(), DefaultParameterValueLoader.DEFAULT, null); }
public AccessorForFunctionDescriptor( @NotNull FunctionDescriptor descriptor, @NotNull DeclarationDescriptor containingDeclaration, int index) { super( containingDeclaration, Annotations.EMPTY, Name.identifier( (descriptor instanceof ConstructorDescriptor ? "$init" : descriptor.getName()) + "$b$" + index), Kind.DECLARATION); initialize( DescriptorUtils.getReceiverParameterType(descriptor.getReceiverParameter()), descriptor instanceof ConstructorDescriptor ? NO_RECEIVER_PARAMETER : descriptor.getExpectedThisObject(), Collections.<TypeParameterDescriptor>emptyList(), copyValueParameters(descriptor), descriptor.getReturnType(), Modality.FINAL, Visibilities.INTERNAL); }
private static JetValueArgumentList findCall(CreateParameterInfoContext context) { // todo: calls to this constructors, when we will have auxiliary constructors PsiFile file = context.getFile(); if (!(file instanceof JetFile)) return null; PsiElement element = file.findElementAt(context.getOffset()); while (element != null && !(element instanceof JetValueArgumentList)) { element = element.getParent(); } if (element == null) return null; JetValueArgumentList argumentList = (JetValueArgumentList) element; JetCallElement callExpression; if (element.getParent() instanceof JetCallElement) { callExpression = (JetCallElement) element.getParent(); } else { return null; } BindingContext bindingContext = AnalyzeSingleFileUtil.getContextForSingleFile((JetFile) file); JetExpression calleeExpression = callExpression.getCalleeExpression(); if (calleeExpression == null) return null; JetSimpleNameExpression refExpression = null; if (calleeExpression instanceof JetSimpleNameExpression) { refExpression = (JetSimpleNameExpression) calleeExpression; } else if (calleeExpression instanceof JetConstructorCalleeExpression) { JetConstructorCalleeExpression constructorCalleeExpression = (JetConstructorCalleeExpression) calleeExpression; if (constructorCalleeExpression.getConstructorReferenceExpression() instanceof JetSimpleNameExpression) { refExpression = (JetSimpleNameExpression) constructorCalleeExpression.getConstructorReferenceExpression(); } } if (refExpression != null) { JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, refExpression); DeclarationDescriptor placeDescriptor = null; if (scope != null) { placeDescriptor = scope.getContainingDeclaration(); } Collection<DeclarationDescriptor> variants = TipsManager.getReferenceVariants(refExpression, bindingContext); Name refName = refExpression.getReferencedNameAsName(); PsiReference[] references = refExpression.getReferences(); if (references.length == 0) return null; ArrayList<DeclarationDescriptor> itemsToShow = new ArrayList<DeclarationDescriptor>(); for (DeclarationDescriptor variant : variants) { if (variant instanceof FunctionDescriptor) { FunctionDescriptor functionDescriptor = (FunctionDescriptor) variant; if (functionDescriptor.getName().equals(refName)) { // todo: renamed functions? if (placeDescriptor != null && !JetVisibilityChecker.isVisible(placeDescriptor, functionDescriptor)) continue; itemsToShow.add(functionDescriptor); } } else if (variant instanceof ClassDescriptor) { ClassDescriptor classDescriptor = (ClassDescriptor) variant; if (classDescriptor.getName().equals(refName)) { // todo: renamed classes? for (ConstructorDescriptor constructorDescriptor : classDescriptor.getConstructors()) { if (placeDescriptor != null && !JetVisibilityChecker.isVisible(placeDescriptor, constructorDescriptor)) continue; itemsToShow.add(constructorDescriptor); } } } } context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow)); return argumentList; } return null; }
public static void generateMethodBody( @NotNull MethodVisitor mv, @NotNull FunctionDescriptor functionDescriptor, @NotNull MethodContext context, @NotNull JvmMethodSignature signature, @NotNull FunctionGenerationStrategy strategy, @NotNull MemberCodegen<?> parentCodegen) { mv.visitCode(); Label methodBegin = new Label(); mv.visitLabel(methodBegin); JetTypeMapper typeMapper = parentCodegen.typeMapper; if (BuiltinSpecialBridgesUtil.shouldHaveTypeSafeBarrier( functionDescriptor, getSignatureMapper(typeMapper))) { generateTypeCheckBarrierIfNeeded( new InstructionAdapter(mv), functionDescriptor, signature.getReturnType(), /* delegateParameterType = */ null); } Label methodEnd; int functionFakeIndex = -1; int lambdaFakeIndex = -1; if (context.getParentContext() instanceof MultifileClassFacadeContext) { generateFacadeDelegateMethodBody( mv, signature.getAsmMethod(), (MultifileClassFacadeContext) context.getParentContext()); methodEnd = new Label(); } else { FrameMap frameMap = createFrameMap( parentCodegen.state, functionDescriptor, signature, isStaticMethod(context.getContextKind(), functionDescriptor)); if (context.isInlineMethodContext()) { functionFakeIndex = frameMap.enterTemp(Type.INT_TYPE); } if (context instanceof InlineLambdaContext) { lambdaFakeIndex = frameMap.enterTemp(Type.INT_TYPE); } Label methodEntry = new Label(); mv.visitLabel(methodEntry); context.setMethodStartLabel(methodEntry); if (!JetTypeMapper.isAccessor(functionDescriptor)) { genNotNullAssertionsForParameters( new InstructionAdapter(mv), parentCodegen.state, functionDescriptor, frameMap); } methodEnd = new Label(); context.setMethodEndLabel(methodEnd); strategy.generateBody(mv, frameMap, signature, context, parentCodegen); } mv.visitLabel(methodEnd); Type thisType = getThisTypeForFunction(functionDescriptor, context, typeMapper); generateLocalVariableTable( mv, signature, functionDescriptor, thisType, methodBegin, methodEnd, context.getContextKind()); if (context.isInlineMethodContext() && functionFakeIndex != -1) { mv.visitLocalVariable( JvmAbi.LOCAL_VARIABLE_NAME_PREFIX_INLINE_FUNCTION + functionDescriptor.getName().asString(), Type.INT_TYPE.getDescriptor(), null, methodBegin, methodEnd, functionFakeIndex); } if (context instanceof InlineLambdaContext && thisType != null && lambdaFakeIndex != -1) { String name = thisType.getClassName(); int indexOfLambdaOrdinal = name.lastIndexOf("$"); if (indexOfLambdaOrdinal > 0) { int lambdaOrdinal = Integer.parseInt(name.substring(indexOfLambdaOrdinal + 1)); mv.visitLocalVariable( JvmAbi.LOCAL_VARIABLE_NAME_PREFIX_INLINE_ARGUMENT + lambdaOrdinal, Type.INT_TYPE.getDescriptor(), null, methodBegin, methodEnd, lambdaFakeIndex); } } }
private static JetValueArgumentList findCall(CreateParameterInfoContext context) { // todo: calls to this constructors, when we will have auxiliary constructors PsiFile file = context.getFile(); if (!(file instanceof JetFile)) { return null; } JetValueArgumentList argumentList = PsiTreeUtil.getParentOfType( file.findElementAt(context.getOffset()), JetValueArgumentList.class); if (argumentList == null) { return null; } JetSimpleNameExpression callNameExpression = getCallSimpleNameExpression(argumentList); if (callNameExpression == null) { return null; } PsiReference[] references = callNameExpression.getReferences(); if (references.length == 0) { return null; } CancelableResolveSession resolveSession = WholeProjectAnalyzerFacade.getLazyResolveResultForFile( (JetFile) callNameExpression.getContainingFile()); BindingContext bindingContext = resolveSession.resolveToElement(callNameExpression); JetScope scope = bindingContext.get(BindingContext.RESOLUTION_SCOPE, callNameExpression); DeclarationDescriptor placeDescriptor = null; if (scope != null) { placeDescriptor = scope.getContainingDeclaration(); } Collection<DeclarationDescriptor> variants = TipsManager.getReferenceVariants(callNameExpression, bindingContext); Name refName = callNameExpression.getReferencedNameAsName(); Collection<Pair<? extends DeclarationDescriptor, CancelableResolveSession>> itemsToShow = new ArrayList<Pair<? extends DeclarationDescriptor, CancelableResolveSession>>(); for (DeclarationDescriptor variant : variants) { if (variant instanceof FunctionDescriptor) { FunctionDescriptor functionDescriptor = (FunctionDescriptor) variant; if (functionDescriptor.getName().equals(refName)) { // todo: renamed functions? if (placeDescriptor != null && !JetVisibilityChecker.isVisible(placeDescriptor, functionDescriptor)) { continue; } itemsToShow.add(Pair.create(functionDescriptor, resolveSession)); } } else if (variant instanceof ClassDescriptor) { ClassDescriptor classDescriptor = (ClassDescriptor) variant; if (classDescriptor.getName().equals(refName)) { // todo: renamed classes? for (ConstructorDescriptor constructorDescriptor : classDescriptor.getConstructors()) { if (placeDescriptor != null && !JetVisibilityChecker.isVisible(placeDescriptor, constructorDescriptor)) { continue; } itemsToShow.add(Pair.create(constructorDescriptor, resolveSession)); } } } } context.setItemsToShow(ArrayUtil.toObjectArray(itemsToShow)); return argumentList; }