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 void accept(@NotNull JetVisitorVoid visitor) {
037            visitor.visitCallExpression(this);
038        }
039    
040        @Override
041        public <R, D> R accept(@NotNull JetVisitor<R, D> visitor, D data) {
042            return visitor.visitCallExpression(this, data);
043        }
044    
045        @Override
046        @Nullable
047        public JetExpression getCalleeExpression() {
048            return findChildByClass(JetExpression.class);
049        }
050    
051        @Override
052        @Nullable
053        public JetValueArgumentList getValueArgumentList() {
054            return (JetValueArgumentList) findChildByType(JetNodeTypes.VALUE_ARGUMENT_LIST);
055        }
056    
057        @Nullable
058        public JetTypeArgumentList getTypeArgumentList() {
059            return (JetTypeArgumentList) findChildByType(JetNodeTypes.TYPE_ARGUMENT_LIST);
060        }
061    
062        @Override
063        @NotNull
064        public List<JetExpression> getFunctionLiteralArguments() {
065            JetExpression calleeExpression = getCalleeExpression();
066            ASTNode node;
067            if (calleeExpression instanceof JetFunctionLiteralExpression) {
068                node = calleeExpression.getNode().getTreeNext();
069            }
070            else {
071                node = getNode().getFirstChildNode();
072            }
073            List<JetExpression> result = new SmartList<JetExpression>();
074            while (node != null) {
075                PsiElement psi = node.getPsi();
076                if (psi instanceof JetFunctionLiteralExpression) {
077                    result.add((JetFunctionLiteralExpression) psi);
078                }
079                else if (psi instanceof JetPrefixExpression) {
080                    JetPrefixExpression prefixExpression = (JetPrefixExpression) psi;
081                    if (JetTokens.LABELS.contains(prefixExpression.getOperationReference().getReferencedNameElementType())) {
082                        JetExpression labeledExpression = prefixExpression.getBaseExpression();
083                        if (labeledExpression instanceof JetFunctionLiteralExpression) {
084                            result.add(prefixExpression);
085                        }
086                    }
087                }
088                node = node.getTreeNext();
089            }
090            return result;
091        }
092    
093        @Override
094        @NotNull
095        public List<? extends ValueArgument> getValueArguments() {
096            JetValueArgumentList list = getValueArgumentList();
097            return list != null ? list.getArguments() : Collections.<JetValueArgument>emptyList();
098        }
099    
100        @NotNull
101        public List<JetTypeProjection> getTypeArguments() {
102            JetTypeArgumentList list = getTypeArgumentList();
103            return list != null ? list.getArguments() : Collections.<JetTypeProjection>emptyList();
104        }
105    }