blob: 5d856ede7535671d7487aedcbc31e9faad611267 [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;
janakr76de1ad2018-03-03 11:12:36 -080017import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
brandjone2ffd5d2017-06-27 18:14:54 +020018import java.io.IOException;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010019
janakr76de1ad2018-03-03 11:12:36 -080020/** Syntax node for a function definition. */
21@AutoCodec
brandjon296cd492017-05-15 16:17:16 +020022public final class FunctionDefStatement extends Statement {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010023
brandjon990622b2017-07-11 19:56:45 +020024 private final Identifier identifier;
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000025 private final FunctionSignature.WithValues<Expression, Expression> signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010026 private final ImmutableList<Statement> statements;
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000027 private final ImmutableList<Parameter<Expression, Expression>> parameters;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010028
brandjon990622b2017-07-11 19:56:45 +020029 public FunctionDefStatement(Identifier identifier,
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000030 Iterable<Parameter<Expression, Expression>> parameters,
31 FunctionSignature.WithValues<Expression, Expression> signature,
32 Iterable<Statement> statements) {
brandjon990622b2017-07-11 19:56:45 +020033 this.identifier = identifier;
brandjon540aac62017-06-12 23:08:09 +020034 this.parameters = ImmutableList.copyOf(parameters);
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000035 this.signature = signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010036 this.statements = ImmutableList.copyOf(statements);
37 }
38
39 @Override
brandjone2ffd5d2017-06-27 18:14:54 +020040 public void prettyPrint(Appendable buffer, int indentLevel) throws IOException {
41 printIndent(buffer, indentLevel);
42 buffer.append("def ");
brandjon990622b2017-07-11 19:56:45 +020043 identifier.prettyPrint(buffer);
brandjone2ffd5d2017-06-27 18:14:54 +020044 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 Nienhuysd08b27f2015-02-25 16:45:20 +010056 public String toString() {
brandjon990622b2017-07-11 19:56:45 +020057 return "def " + identifier + "(" + signature + "): ...\n";
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010058 }
59
brandjon990622b2017-07-11 19:56:45 +020060 public Identifier getIdentifier() {
61 return identifier;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010062 }
63
64 public ImmutableList<Statement> getStatements() {
65 return statements;
66 }
67
Laurent Le Brun4baefdc2015-09-04 11:27:46 +000068 public ImmutableList<Parameter<Expression, Expression>> getParameters() {
69 return parameters;
70 }
71
72 public FunctionSignature.WithValues<Expression, Expression> getSignature() {
73 return signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010074 }
75
76 @Override
77 public void accept(SyntaxTreeVisitor visitor) {
78 visitor.visit(this);
79 }
laurentlbaf682d12017-08-24 20:32:02 +020080
81 @Override
82 public Kind kind() {
83 return Kind.FUNCTION_DEF;
84 }
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010085}