blob: 2c4cbaebb6609b6fc01ffbe81b7a58d8586855d0 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2014 The Bazel Authors. All rights reserved.
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +00002//
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
16import javax.annotation.Nullable;
17
18/**
Googler73d942f2019-10-03 12:31:02 -070019 * Syntax node for a parameter in a function definition.
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000020 *
Googler73d942f2019-10-03 12:31:02 -070021 * <p>Parameters may be of four forms, as in {@code def f(a, b=c, *args, **kwargs)}. They are
22 * represented by the subclasses Mandatory, Optional, Star, and StarStar.
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000023 */
Googler35618a82019-10-04 07:00:05 -070024public abstract class Parameter extends Node {
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000025
adonovan07b15e62020-04-09 18:32:33 -070026 @Nullable private final Identifier id;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000027
adonovand51ac9b2020-04-10 10:15:08 -070028 private Parameter(FileLocations locs, @Nullable Identifier id) {
29 super(locs);
adonovan07b15e62020-04-09 18:32:33 -070030 this.id = id;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000031 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000032
brandjone2ffd5d2017-06-27 18:14:54 +020033 @Nullable
34 public String getName() {
adonovan07b15e62020-04-09 18:32:33 -070035 return id != null ? id.getName() : null;
Taras Tsugrii36941362018-06-08 16:31:53 -070036 }
37
38 @Nullable
39 public Identifier getIdentifier() {
adonovan07b15e62020-04-09 18:32:33 -070040 return id;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000041 }
brandjone2ffd5d2017-06-27 18:14:54 +020042
brandjone2ffd5d2017-06-27 18:14:54 +020043 @Nullable
Googler35618a82019-10-04 07:00:05 -070044 public Expression getDefaultValue() {
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000045 return null;
46 }
47
Googler73d942f2019-10-03 12:31:02 -070048 /**
49 * Syntax node for a mandatory parameter, {@code f(id)}. It may be positional or keyword-only
50 * depending on its position.
51 */
Googler35618a82019-10-04 07:00:05 -070052 public static final class Mandatory extends Parameter {
adonovand51ac9b2020-04-10 10:15:08 -070053 Mandatory(FileLocations locs, Identifier id) {
54 super(locs, id);
adonovan07b15e62020-04-09 18:32:33 -070055 }
56
57 @Override
58 public int getStartOffset() {
59 return getIdentifier().getStartOffset();
60 }
61
62 @Override
63 public int getEndOffset() {
64 return getIdentifier().getEndOffset();
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000065 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000066 }
67
Googler73d942f2019-10-03 12:31:02 -070068 /**
69 * Syntax node for an optional parameter, {@code f(id=expr).}. It may be positional or
70 * keyword-only depending on its position.
71 */
Googler35618a82019-10-04 07:00:05 -070072 public static final class Optional extends Parameter {
brandjone2ffd5d2017-06-27 18:14:54 +020073
Googler35618a82019-10-04 07:00:05 -070074 public final Expression defaultValue;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000075
adonovand51ac9b2020-04-10 10:15:08 -070076 Optional(FileLocations locs, Identifier id, @Nullable Expression defaultValue) {
77 super(locs, id);
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000078 this.defaultValue = defaultValue;
79 }
80
brandjone2ffd5d2017-06-27 18:14:54 +020081 @Override
82 @Nullable
Googler35618a82019-10-04 07:00:05 -070083 public Expression getDefaultValue() {
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000084 return defaultValue;
85 }
86
brandjone2ffd5d2017-06-27 18:14:54 +020087 @Override
adonovan07b15e62020-04-09 18:32:33 -070088 public int getStartOffset() {
89 return getIdentifier().getStartOffset();
90 }
91
92 @Override
93 public int getEndOffset() {
94 return getDefaultValue().getEndOffset();
95 }
96
97 @Override
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000098 public String toString() {
Taras Tsugrii36941362018-06-08 16:31:53 -070099 return getName() + "=" + defaultValue;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000100 }
101 }
102
adonovan07b15e62020-04-09 18:32:33 -0700103 /** Syntax node for a star parameter, {@code f(*id)} or or {@code f(..., *, ...)}. */
Googler35618a82019-10-04 07:00:05 -0700104 public static final class Star extends Parameter {
adonovan07b15e62020-04-09 18:32:33 -0700105 private final int starOffset;
106
adonovand51ac9b2020-04-10 10:15:08 -0700107 Star(FileLocations locs, int starOffset, @Nullable Identifier id) {
108 super(locs, id);
adonovan07b15e62020-04-09 18:32:33 -0700109 this.starOffset = starOffset;
110 }
111
112 @Override
113 public int getStartOffset() {
114 return starOffset;
115 }
116
117 @Override
118 public int getEndOffset() {
119 return getIdentifier().getEndOffset();
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000120 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000121 }
122
adonovan07b15e62020-04-09 18:32:33 -0700123 /** Syntax node for a parameter of the form {@code f(**id)}. */
Googler35618a82019-10-04 07:00:05 -0700124 public static final class StarStar extends Parameter {
adonovan07b15e62020-04-09 18:32:33 -0700125 private final int starStarOffset;
126
adonovand51ac9b2020-04-10 10:15:08 -0700127 StarStar(FileLocations locs, int starStarOffset, Identifier id) {
128 super(locs, id);
adonovan07b15e62020-04-09 18:32:33 -0700129 this.starStarOffset = starStarOffset;
130 }
131
132 @Override
133 public int getStartOffset() {
134 return starStarOffset;
135 }
136
137 @Override
138 public int getEndOffset() {
139 return getIdentifier().getEndOffset();
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000140 }
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000141 }
142
143 @Override
Googler4ace4652019-09-16 07:47:08 -0700144 public void accept(NodeVisitor visitor) {
Googler35618a82019-10-04 07:00:05 -0700145 visitor.visit(this);
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +0000146 }
147}