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.builtins; 018 019 import kotlin.KotlinPackage; 020 import org.jetbrains.annotations.NotNull; 021 import org.jetbrains.annotations.Nullable; 022 import org.jetbrains.kotlin.descriptors.CallableDescriptor; 023 import org.jetbrains.kotlin.descriptors.ClassDescriptor; 024 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor; 025 import org.jetbrains.kotlin.descriptors.ValueParameterDescriptor; 026 import org.jetbrains.kotlin.descriptors.annotations.Annotated; 027 import org.jetbrains.kotlin.descriptors.annotations.AnnotationDescriptor; 028 import org.jetbrains.kotlin.resolve.DescriptorUtils; 029 import org.jetbrains.kotlin.resolve.constants.ArrayValue; 030 import org.jetbrains.kotlin.resolve.constants.CompileTimeConstant; 031 import org.jetbrains.kotlin.resolve.constants.EnumValue; 032 033 import java.util.List; 034 035 public class InlineUtil { 036 037 public static boolean hasNoinlineAnnotation(@NotNull CallableDescriptor valueParameterDescriptor) { 038 return KotlinBuiltIns.containsAnnotation(valueParameterDescriptor, KotlinBuiltIns.getNoinlineClassAnnotationFqName()); 039 } 040 041 @NotNull 042 public static InlineStrategy getInlineType(@NotNull DeclarationDescriptor descriptor) { 043 ClassDescriptor inlineAnnotation = KotlinBuiltIns.getInstance().getInlineClassAnnotation(); 044 AnnotationDescriptor annotation = descriptor.getAnnotations().findAnnotation(DescriptorUtils.getFqNameSafe(inlineAnnotation)); 045 if (annotation == null) { 046 return InlineStrategy.NOT_INLINE; 047 } 048 CompileTimeConstant<?> argument = getAnnotationSingleArgument(descriptor, inlineAnnotation); 049 if (argument == null) { 050 return InlineStrategy.AS_FUNCTION; 051 } 052 assert argument instanceof EnumValue : "Inline annotation parameter should be enum entry but was: " + argument; 053 String name = ((EnumValue) argument).getValue().getName().asString(); 054 return name.equals(InlineStrategy.IN_PLACE.name()) ? InlineStrategy.IN_PLACE : InlineStrategy.AS_FUNCTION; 055 } 056 057 public static boolean hasOnlyLocalContinueAndBreak(@NotNull ValueParameterDescriptor descriptor) { 058 return hasInlineOption(descriptor, InlineOption.LOCAL_CONTINUE_AND_BREAK); 059 } 060 061 public static boolean hasOnlyLocalReturn(@NotNull ValueParameterDescriptor descriptor) { 062 return hasInlineOption(descriptor, InlineOption.ONLY_LOCAL_RETURN); 063 } 064 065 private static boolean hasInlineOption(@NotNull ValueParameterDescriptor descriptor, @NotNull InlineOption option) { 066 CompileTimeConstant<?> argument = 067 getAnnotationSingleArgument(descriptor, KotlinBuiltIns.getInstance().getInlineOptionsClassAnnotation()); 068 069 if (argument instanceof ArrayValue) { 070 List<CompileTimeConstant<?>> values = ((ArrayValue) argument).getValue(); 071 072 for (CompileTimeConstant<?> value : values) { 073 if (value instanceof EnumValue) { 074 if (((EnumValue) value).getValue().getName().asString().equals(option.name())) { 075 return true; 076 } 077 } 078 } 079 } 080 081 return false; 082 } 083 084 @Nullable 085 private static CompileTimeConstant<?> getAnnotationSingleArgument( 086 @NotNull Annotated annotated, 087 @NotNull ClassDescriptor annotationClass 088 ) { 089 AnnotationDescriptor annotation = annotated.getAnnotations().findAnnotation(DescriptorUtils.getFqNameSafe(annotationClass)); 090 if (annotation != null) { 091 return KotlinPackage.firstOrNull(annotation.getAllValueArguments().values()); 092 } 093 return null; 094 } 095 }