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