Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2014 The Bazel Authors. All rights reserved. |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | package com.google.devtools.build.lib.syntax; |
| 15 | |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 16 | import com.google.common.collect.ImmutableList; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 17 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 18 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame] | 19 | /** Syntax node for a function definition. */ |
brandjon | 296cd49 | 2017-05-15 16:17:16 +0200 | [diff] [blame] | 20 | public final class FunctionDefStatement extends Statement { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 21 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 22 | private final Identifier identifier; |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 23 | private final FunctionSignature.WithValues<Expression, Expression> signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 24 | private final ImmutableList<Statement> statements; |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 25 | private final ImmutableList<Parameter<Expression, Expression>> parameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 27 | public FunctionDefStatement(Identifier identifier, |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 28 | Iterable<Parameter<Expression, Expression>> parameters, |
| 29 | FunctionSignature.WithValues<Expression, Expression> signature, |
| 30 | Iterable<Statement> statements) { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 31 | this.identifier = identifier; |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 32 | this.parameters = ImmutableList.copyOf(parameters); |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 33 | this.signature = signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 34 | this.statements = ImmutableList.copyOf(statements); |
| 35 | } |
| 36 | |
| 37 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 38 | public void prettyPrint(Appendable buffer, int indentLevel) throws IOException { |
| 39 | printIndent(buffer, indentLevel); |
| 40 | buffer.append("def "); |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 41 | identifier.prettyPrint(buffer); |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 42 | buffer.append('('); |
| 43 | String sep = ""; |
| 44 | for (Parameter<?, ?> param : parameters) { |
| 45 | buffer.append(sep); |
| 46 | param.prettyPrint(buffer); |
| 47 | sep = ", "; |
| 48 | } |
| 49 | buffer.append("):\n"); |
| 50 | printSuite(buffer, statements, indentLevel); |
| 51 | } |
| 52 | |
| 53 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 54 | public String toString() { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 55 | return "def " + identifier + "(" + signature + "): ...\n"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | } |
| 57 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 58 | public Identifier getIdentifier() { |
| 59 | return identifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | public ImmutableList<Statement> getStatements() { |
| 63 | return statements; |
| 64 | } |
| 65 | |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 66 | public ImmutableList<Parameter<Expression, Expression>> getParameters() { |
| 67 | return parameters; |
| 68 | } |
| 69 | |
| 70 | public FunctionSignature.WithValues<Expression, Expression> getSignature() { |
| 71 | return signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | @Override |
| 75 | public void accept(SyntaxTreeVisitor visitor) { |
| 76 | visitor.visit(this); |
| 77 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 78 | |
| 79 | @Override |
| 80 | public Kind kind() { |
| 81 | return Kind.FUNCTION_DEF; |
| 82 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 83 | } |