1// Copyright (c) 2013 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "tools/gn/functions.h"
6#include "tools/gn/parse_tree.h"
7#include "tools/gn/scope.h"
8#include "tools/gn/value.h"
9
10namespace functions {
11
12const char kDefineRule[] = "define_rule";
13const char kDefileRule_Help[] =
14    "TODO(brettw) write this.";
15
16Value RunDefineRule(Scope* scope,
17                    const FunctionCallNode* function,
18                    const std::vector<Value>& args,
19                    BlockNode* block,
20                    Err* err) {
21  // TODO(brettw) determine if the function is built-in and throw an error if
22  // it is.
23  if (args.size() != 1) {
24    *err = Err(function->function(),
25               "Need exactly one string arg to define_rule.");
26    return Value();
27  }
28  if (!args[0].VerifyTypeIs(Value::STRING, err))
29    return Value();
30  std::string rule_name = args[0].string_value();
31
32  const FunctionCallNode* existing_rule = scope->GetRule(rule_name);
33  if (existing_rule) {
34    *err = Err(function, "Duplicate rule definition.",
35               "A rule with this name was already defined.");
36    err->AppendSubErr(Err(existing_rule->function(), "Previous definition."));
37    return Value();
38  }
39
40  scope->AddRule(rule_name, function);
41  return Value();
42}
43
44}  // namespace functions
45