blob: 4a5968cd362eb23a316a0a3a87c60f5d0b0455e6 [file] [log] [blame]
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +01001// Copyright 2014 Google Inc. All rights reserved.
2//
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;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010017import com.google.devtools.build.lib.events.Location;
18
19/**
20 * The actual function registered in the environment. This function is defined in the
21 * parsed code using {@link FunctionDefStatement}.
22 */
Francois-Rene Rideau4feb1602015-03-18 19:49:13 +000023public class UserDefinedFunction extends BaseFunction {
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010024
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010025 private final ImmutableList<Statement> statements;
26 private final SkylarkEnvironment definitionEnv;
27
Florian Weikert6f864c32015-07-23 11:26:39 +000028 protected UserDefinedFunction(Identifier function,
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000029 FunctionSignature.WithValues<Object, SkylarkType> signature,
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010030 ImmutableList<Statement> statements, SkylarkEnvironment definitionEnv) {
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000031 super(function.getName(), signature, function.getLocation());
32
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010033 this.statements = statements;
34 this.definitionEnv = definitionEnv;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010035 }
36
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000037 public FunctionSignature.WithValues<Object, SkylarkType> getFunctionSignature() {
38 return signature;
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010039 }
40
41 ImmutableList<Statement> getStatements() {
42 return statements;
43 }
44
45 Location getLocation() {
46 return location;
47 }
48
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000049
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010050 @Override
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000051 public Object call(Object[] arguments, FuncallExpression ast, Environment env)
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010052 throws EvalException, InterruptedException {
53 SkylarkEnvironment functionEnv = SkylarkEnvironment.createEnvironmentForFunctionCalling(
54 env, definitionEnv, this);
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000055 ImmutableList<String> names = signature.getSignature().getNames();
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010056
57 // Registering the functions's arguments as variables in the local Environment
58 int i = 0;
Francois-Rene Rideau5dcdbf92015-02-19 18:36:17 +000059 for (String name : names) {
60 functionEnv.update(name, arguments[i++]);
Han-Wen Nienhuysd08b27f2015-02-25 16:45:20 +010061 }
62
63 try {
64 for (Statement stmt : statements) {
65 stmt.exec(functionEnv);
66 }
67 } catch (ReturnStatement.ReturnException e) {
68 return e.getValue();
69 }
70 return Environment.NONE;
71 }
72}