blob: 0c78eaf1e52a2de1e75711d8050d005126b0542e [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01002//
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.
14package com.google.devtools.build.lib.syntax;
15
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010016import com.google.common.collect.ImmutableList;
brandjone2ffd5d2017-06-27 18:14:54 +020017import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010018
janakr76de1ad2018-03-03 11:12:36 -080019/** Syntax node for a function definition. */
brandjon296cd492017-05-15 16:17:16 +020020public final class FunctionDefStatement extends Statement {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010021
brandjon990622b2017-07-11 19:56:45 +020022 private final Identifier identifier;
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000023 private final FunctionSignature.WithValues<Expression, Expression> signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024 private final ImmutableList<Statement> statements;
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000025 private final ImmutableList<Parameter<Expression, Expression>> parameters;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026
brandjon990622b2017-07-11 19:56:45 +020027 public FunctionDefStatement(Identifier identifier,
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000028 Iterable<Parameter<Expression, Expression>> parameters,
29 FunctionSignature.WithValues<Expression, Expression> signature,
30 Iterable<Statement> statements) {
brandjon990622b2017-07-11 19:56:45 +020031 this.identifier = identifier;
brandjon540aac62017-06-12 23:08:09 +020032 this.parameters = ImmutableList.copyOf(parameters);
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000033 this.signature = signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010034 this.statements = ImmutableList.copyOf(statements);
35 }
36
37 @Override
brandjone2ffd5d2017-06-27 18:14:54 +020038 public void prettyPrint(Appendable buffer, int indentLevel) throws IOException {
39 printIndent(buffer, indentLevel);
40 buffer.append("def ");
brandjon990622b2017-07-11 19:56:45 +020041 identifier.prettyPrint(buffer);
brandjone2ffd5d2017-06-27 18:14:54 +020042 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 Nienhuysd08b27f2015-02-25 16:45:20 +010054 public String toString() {
brandjon990622b2017-07-11 19:56:45 +020055 return "def " + identifier + "(" + signature + "): ...\n";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056 }
57
brandjon990622b2017-07-11 19:56:45 +020058 public Identifier getIdentifier() {
59 return identifier;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010060 }
61
62 public ImmutableList<Statement> getStatements() {
63 return statements;
64 }
65
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000066 public ImmutableList<Parameter<Expression, Expression>> getParameters() {
67 return parameters;
68 }
69
70 public FunctionSignature.WithValues<Expression, Expression> getSignature() {
71 return signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010072 }
73
74 @Override
75 public void accept(SyntaxTreeVisitor visitor) {
76 visitor.visit(this);
77 }
laurentlbaf682d12017-08-24 20:32:02 +020078
79 @Override
80 public Kind kind() {
81 return Kind.FUNCTION_DEF;
82 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010083}