@Override void validate(final ValidationEnvironment env) throws EvalException { ValidationEnvironment localEnv = new ValidationEnvironment(env); FunctionSignature sig = signature.getSignature(); FunctionSignature.Shape shape = sig.getShape(); ImmutableList<String> names = sig.getNames(); List<Expression> defaultExpressions = signature.getDefaultValues(); int positionals = shape.getPositionals(); int mandatoryPositionals = shape.getMandatoryPositionals(); int namedOnly = shape.getNamedOnly(); int mandatoryNamedOnly = shape.getMandatoryNamedOnly(); boolean starArg = shape.hasStarArg(); boolean kwArg = shape.hasKwArg(); int named = positionals + namedOnly; int args = named + (starArg ? 1 : 0) + (kwArg ? 1 : 0); int startOptionals = mandatoryPositionals; int endOptionals = named - mandatoryNamedOnly; int j = 0; // index for the defaultExpressions for (int i = 0; i < args; i++) { String name = names.get(i); if (startOptionals <= i && i < endOptionals) { defaultExpressions.get(j++).validate(env); } localEnv.declare(name, getLocation()); } for (Statement stmts : statements) { stmts.validate(localEnv); } }
/** Create a function using a signature with defaults */ public MixedModeFunction( String name, FunctionSignature.WithValues<Object, SkylarkType> signature, Location location) { super(name); // TODO(bazel-team): lift the following limitations, by actually implementing // the full function call protocol. FunctionSignature sig = signature.getSignature(); FunctionSignature.Shape shape = sig.getShape(); Preconditions.checkArgument( !shape.hasKwArg() && !shape.hasStarArg() && shape.getNamedOnly() == 0, "no star, star-star or named-only parameters (for now)"); this.signature = signature; this.parameters = ImmutableList.copyOf(sig.getNames()); this.numMandatoryParameters = shape.getMandatoryPositionals(); this.onlyNamedArguments = false; this.location = location; }