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; |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame^] | 17 | import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec; |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 18 | import java.io.IOException; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 19 | |
janakr | 76de1ad | 2018-03-03 11:12:36 -0800 | [diff] [blame^] | 20 | /** Syntax node for a function definition. */ |
| 21 | @AutoCodec |
brandjon | 296cd49 | 2017-05-15 16:17:16 +0200 | [diff] [blame] | 22 | public final class FunctionDefStatement extends Statement { |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 23 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 24 | private final Identifier identifier; |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 25 | private final FunctionSignature.WithValues<Expression, Expression> signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 26 | private final ImmutableList<Statement> statements; |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 27 | private final ImmutableList<Parameter<Expression, Expression>> parameters; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 28 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 29 | public FunctionDefStatement(Identifier identifier, |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 30 | Iterable<Parameter<Expression, Expression>> parameters, |
| 31 | FunctionSignature.WithValues<Expression, Expression> signature, |
| 32 | Iterable<Statement> statements) { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 33 | this.identifier = identifier; |
brandjon | 540aac6 | 2017-06-12 23:08:09 +0200 | [diff] [blame] | 34 | this.parameters = ImmutableList.copyOf(parameters); |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 35 | this.signature = signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 36 | this.statements = ImmutableList.copyOf(statements); |
| 37 | } |
| 38 | |
| 39 | @Override |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 40 | public void prettyPrint(Appendable buffer, int indentLevel) throws IOException { |
| 41 | printIndent(buffer, indentLevel); |
| 42 | buffer.append("def "); |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 43 | identifier.prettyPrint(buffer); |
brandjon | e2ffd5d | 2017-06-27 18:14:54 +0200 | [diff] [blame] | 44 | buffer.append('('); |
| 45 | String sep = ""; |
| 46 | for (Parameter<?, ?> param : parameters) { |
| 47 | buffer.append(sep); |
| 48 | param.prettyPrint(buffer); |
| 49 | sep = ", "; |
| 50 | } |
| 51 | buffer.append("):\n"); |
| 52 | printSuite(buffer, statements, indentLevel); |
| 53 | } |
| 54 | |
| 55 | @Override |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 56 | public String toString() { |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 57 | return "def " + identifier + "(" + signature + "): ...\n"; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 58 | } |
| 59 | |
brandjon | 990622b | 2017-07-11 19:56:45 +0200 | [diff] [blame] | 60 | public Identifier getIdentifier() { |
| 61 | return identifier; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | public ImmutableList<Statement> getStatements() { |
| 65 | return statements; |
| 66 | } |
| 67 | |
Laurent Le Brun | 4baefdc | 2015-09-04 11:27:46 +0000 | [diff] [blame] | 68 | public ImmutableList<Parameter<Expression, Expression>> getParameters() { |
| 69 | return parameters; |
| 70 | } |
| 71 | |
| 72 | public FunctionSignature.WithValues<Expression, Expression> getSignature() { |
| 73 | return signature; |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | @Override |
| 77 | public void accept(SyntaxTreeVisitor visitor) { |
| 78 | visitor.visit(this); |
| 79 | } |
laurentlb | af682d1 | 2017-08-24 20:32:02 +0200 | [diff] [blame] | 80 | |
| 81 | @Override |
| 82 | public Kind kind() { |
| 83 | return Kind.FUNCTION_DEF; |
| 84 | } |
Han-Wen Nienhuys | d08b27f | 2015-02-25 16:45:20 +0100 | [diff] [blame] | 85 | } |