001 /*
002 * Copyright 2010-2013 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.jet.lang.psi;
018
019 import com.intellij.lang.ASTNode;
020 import com.intellij.psi.PsiElement;
021 import com.intellij.util.SmartList;
022 import org.jetbrains.annotations.NotNull;
023 import org.jetbrains.annotations.Nullable;
024 import org.jetbrains.jet.JetNodeTypes;
025 import org.jetbrains.jet.lexer.JetTokens;
026
027 import java.util.Collections;
028 import java.util.List;
029
030 public class JetCallExpression extends JetReferenceExpression implements JetCallElement {
031 public JetCallExpression(@NotNull ASTNode node) {
032 super(node);
033 }
034
035 @Override
036 public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
037 return visitor.visitCallExpression(this, data);
038 }
039
040 @Override
041 @Nullable
042 public JetExpression getCalleeExpression() {
043 return findChildByClass(JetExpression.class);
044 }
045
046 @Override
047 @Nullable
048 public JetValueArgumentList getValueArgumentList() {
049 return (JetValueArgumentList) findChildByType(JetNodeTypes.VALUE_ARGUMENT_LIST);
050 }
051
052 @Nullable
053 public JetTypeArgumentList getTypeArgumentList() {
054 return (JetTypeArgumentList) findChildByType(JetNodeTypes.TYPE_ARGUMENT_LIST);
055 }
056
057 @Override
058 @NotNull
059 public List<JetExpression> getFunctionLiteralArguments() {
060 JetExpression calleeExpression = getCalleeExpression();
061 ASTNode node;
062 if (calleeExpression instanceof JetFunctionLiteralExpression) {
063 node = calleeExpression.getNode().getTreeNext();
064 }
065 else {
066 node = getNode().getFirstChildNode();
067 }
068 List<JetExpression> result = new SmartList<JetExpression>();
069 while (node != null) {
070 PsiElement psi = node.getPsi();
071 if (psi instanceof JetFunctionLiteralExpression) {
072 result.add((JetFunctionLiteralExpression) psi);
073 }
074 else if (psi instanceof JetPrefixExpression) {
075 JetPrefixExpression prefixExpression = (JetPrefixExpression) psi;
076 if (JetTokens.LABELS.contains(prefixExpression.getOperationReference().getReferencedNameElementType())) {
077 JetExpression labeledExpression = prefixExpression.getBaseExpression();
078 if (labeledExpression instanceof JetFunctionLiteralExpression) {
079 result.add(prefixExpression);
080 }
081 }
082 }
083 node = node.getTreeNext();
084 }
085 return result;
086 }
087
088 @Override
089 @NotNull
090 public List<? extends ValueArgument> getValueArguments() {
091 JetValueArgumentList list = getValueArgumentList();
092 return list != null ? list.getArguments() : Collections.<JetValueArgument>emptyList();
093 }
094
095 @NotNull
096 public List<JetTypeProjection> getTypeArguments() {
097 JetTypeArgumentList list = getTypeArgumentList();
098 return list != null ? list.getArguments() : Collections.<JetTypeProjection>emptyList();
099 }
100 }