aboutsummaryrefslogtreecommitdiffhomepage
path: root/src/main/java/com/google/devtools/build/lib/syntax/Parameter.java
blob: b1e198b36f9be21dd6e28d8791cb9f3738410597 (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
// Copyright 2014 Google Inc. 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 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 String name;
  @Nullable protected final T type;

  private Parameter(@Nullable String name, @Nullable T type) {
    this.name = name;
    this.type = type;
  }
  private Parameter(@Nullable String name) {
    this.name = name;
    this.type = null;
  }

  public boolean isMandatory() {
    return false;
  }
  public boolean isOptional() {
    return false;
  }
  public boolean isStar() {
    return false;
  }
  public boolean isStarStar() {
    return false;
  }
  @Nullable public String getName() {
    return name;
  }
  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 */
  public static class Mandatory<V, T> extends Parameter<V, T> {

    public Mandatory(String name) {
      super(name);
    }

    public Mandatory(String name, @Nullable T type) {
      super(name, type);
    }

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

    @Override
    public String toString() {
      return name.toString();
    }
  }

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

    public Optional(String name, @Nullable V defaultValue) {
      super(name);
      this.defaultValue = defaultValue;
    }

    public Optional(String name, @Nullable T type, @Nullable V defaultValue) {
      super(name, type);
      this.defaultValue = defaultValue;
    }

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

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

    @Override
    public String toString() {
      return name + "=" + defaultValue;
    }
  }

  /** extra positionals parameter (star): *identifier */
  public static class Star<V, T> extends Parameter<V, T> {
    public Star(@Nullable String name, @Nullable T type) {
      super(name, type);
    }

    public Star(@Nullable String name) {
      super(name);
    }

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

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

    @Override public String toString() {
      if (name == null) {
        return "*";
      } else {
        return "*" + name;
      }
    }
  }

  /** extra keywords parameter (star_star): **identifier */
  public static class StarStar<V, T> extends Parameter<V, T> {
    public StarStar(String name, @Nullable T type) {
      super(name, type);
    }

    public StarStar(String name) {
      super(name);
    }

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

    @Override
    public String toString() {
      return "**" + name;
    }
  }

  @Override
  public void accept(SyntaxTreeVisitor visitor) {
    visitor.visit(this);
  }
}