aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java
blob: 4f499d6d09dec521a71b95bf12fb11b61d3111ea (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
// Copyright 2014 The Bazel Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.devtools.build.lib.syntax;

import com.google.devtools.build.lib.skyframe.serialization.autocodec.AutoCodec;
import java.io.IOException;
import javax.annotation.Nullable;

/**
 * Syntax node for a Parameter in a function (or lambda) definition; it's a subclass of Argument,
 * and contrasts with the class Argument.Passed of arguments in a function call.
 *
 * <p>There are four concrete subclasses of Parameter: Mandatory, Optional, Star, StarStar.
 *
 * <p>See FunctionSignature for how a valid list of Parameter's is organized as a signature, e.g.
 * def foo(mandatory, optional = e1, *args, mandatorynamedonly, optionalnamedonly = e2, **kw): ...
 *
 * <p>V is the class of a defaultValue (Expression at compile-time, Object at runtime),
 * T is the class of a type (Expression at compile-time, SkylarkType at runtime).
 */
public abstract class Parameter<V, T> extends Argument {

  @Nullable protected final Identifier identifier;
  @Nullable protected final T type;

  private Parameter(@Nullable Identifier identifier, @Nullable T type) {
    this.identifier = identifier;
    this.type = type;
  }

  public boolean isMandatory() {
    return false;
  }

  public boolean isOptional() {
    return false;
  }

  @Override
  public boolean isStar() {
    return false;
  }

  @Override
  public boolean isStarStar() {
    return false;
  }

  @Nullable
  public String getName() {
    return identifier != null ? identifier.getName() : null;
  }

  @Nullable
  public Identifier getIdentifier() {
    return identifier;
  }

  public boolean hasName() {
    return true;
  }

  @Nullable
  public T getType() {
    return type;
  }

  @Nullable
  public V getDefaultValue() {
    return null;
  }

  /** mandatory parameter (positional or key-only depending on position): Ident */
  @AutoCodec
  public static final class Mandatory<V, T> extends Parameter<V, T> {

    public Mandatory(Identifier identifier) {
      this(identifier, null);
    }

    @AutoCodec.Instantiator
    public Mandatory(Identifier identifier, @Nullable T type) {
      super(identifier, type);
    }

    @Override
    public boolean isMandatory() {
      return true;
    }

    @Override
    public void prettyPrint(Appendable buffer) throws IOException {
      buffer.append(getName());
    }
  }

  /** optional parameter (positional or key-only depending on position): Ident = Value */
  @AutoCodec
  public static final class Optional<V, T> extends Parameter<V, T> {

    public final V defaultValue;

    public Optional(Identifier identifier, @Nullable V defaultValue) {
      this(identifier, null, defaultValue);
    }

    @AutoCodec.Instantiator
    public Optional(Identifier identifier, @Nullable T type, @Nullable V defaultValue) {
      super(identifier, type);
      this.defaultValue = defaultValue;
    }

    @Override
    @Nullable
    public V getDefaultValue() {
      return defaultValue;
    }

    @Override
    public boolean isOptional() {
      return true;
    }

    @Override
    public void prettyPrint(Appendable buffer) throws IOException {
      buffer.append(getName());
      buffer.append('=');
      // This should only ever be used on a parameter representing static information, i.e. with V
      // and T instantiated as Expression.
      ((Expression) defaultValue).prettyPrint(buffer);
    }

    // Keep this as a separate method so that it can be used regardless of what V and T are
    // parameterized with.
    @Override
    public String toString() {
      return getName() + "=" + defaultValue;
    }
  }

  /** extra positionals parameter (star): *identifier */
  @AutoCodec
  public static final class Star<V, T> extends Parameter<V, T> {

    @AutoCodec.Instantiator
    public Star(@Nullable Identifier identifier, @Nullable T type) {
      super(identifier, type);
    }

    public Star(@Nullable Identifier identifier) {
      this(identifier, null);
    }

    @Override
    public boolean hasName() {
      return getName() != null;
    }

    @Override
    public boolean isStar() {
      return true;
    }

    @Override
    public void prettyPrint(Appendable buffer) throws IOException {
      buffer.append('*');
      if (getName() != null) {
        buffer.append(getName());
      }
    }
  }

  /** extra keywords parameter (star_star): **identifier */
  @AutoCodec
  public static final class StarStar<V, T> extends Parameter<V, T> {

    @AutoCodec.Instantiator
    public StarStar(Identifier identifier, @Nullable T type) {
      super(identifier, type);
    }

    public StarStar(Identifier identifier) {
      this(identifier, null);
    }

    @Override
    public boolean isStarStar() {
      return true;
    }

    @Override
    public void prettyPrint(Appendable buffer) throws IOException {
      buffer.append("**");
      buffer.append(getName());
    }
  }

  @Override
  @SuppressWarnings("unchecked")
  public void accept(SyntaxTreeVisitor visitor) {
    visitor.visit((Parameter<Expression, Expression>) this);
  }
}