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.psi;
018
019 import com.google.common.collect.Lists;
020 import com.intellij.lang.ASTNode;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.annotations.Nullable;
023 import org.jetbrains.kotlin.lexer.JetToken;
024 import org.jetbrains.kotlin.lexer.JetTokens;
025 import org.jetbrains.kotlin.psi.stubs.KotlinPlaceHolderStub;
026 import org.jetbrains.kotlin.psi.stubs.elements.JetStubElementTypes;
027
028 import java.util.ArrayList;
029 import java.util.Collections;
030 import java.util.List;
031
032 public class JetFunctionType extends JetElementImplStub<KotlinPlaceHolderStub<JetFunctionType>> implements JetTypeElement {
033
034 public static final JetToken RETURN_TYPE_SEPARATOR = JetTokens.ARROW;
035
036 public JetFunctionType(@NotNull ASTNode node) {
037 super(node);
038 }
039
040 public JetFunctionType(@NotNull KotlinPlaceHolderStub<JetFunctionType> stub) {
041 super(stub, JetStubElementTypes.FUNCTION_TYPE);
042 }
043
044 @NotNull
045 @Override
046 public List<JetTypeReference> getTypeArgumentsAsTypes() {
047 ArrayList<JetTypeReference> result = Lists.newArrayList();
048 JetTypeReference receiverTypeRef = getReceiverTypeReference();
049 if (receiverTypeRef != null) {
050 result.add(receiverTypeRef);
051 }
052 for (JetParameter jetParameter : getParameters()) {
053 result.add(jetParameter.getTypeReference());
054 }
055 JetTypeReference returnTypeRef = getReturnTypeReference();
056 if (returnTypeRef != null) {
057 result.add(returnTypeRef);
058 }
059 return result;
060 }
061
062 @Override
063 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
064 return visitor.visitFunctionType(this, data);
065 }
066
067 @Nullable
068 public JetParameterList getParameterList() {
069 return getStubOrPsiChild(JetStubElementTypes.VALUE_PARAMETER_LIST);
070 }
071
072 @NotNull
073 public List<JetParameter> getParameters() {
074 JetParameterList list = getParameterList();
075 return list != null ? list.getParameters() : Collections.<JetParameter>emptyList();
076 }
077
078 @Nullable
079 public JetTypeReference getReceiverTypeReference() {
080 JetFunctionTypeReceiver receiverDeclaration = getStubOrPsiChild(JetStubElementTypes.FUNCTION_TYPE_RECEIVER);
081 if (receiverDeclaration == null) {
082 return null;
083 }
084 return receiverDeclaration.getTypeReference();
085 }
086
087 @Nullable
088 public JetTypeReference getReturnTypeReference() {
089 return getStubOrPsiChild(JetStubElementTypes.TYPE_REFERENCE);
090 }
091 }