Change next_proto member type.
[chromium-blink-merge.git] / tools / gn / functions.cc
blob84d5ea4312b4fab42ec7c037961a80e24b4d87f7
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.
5 #include "tools/gn/functions.h"
7 #include <iostream>
9 #include "base/environment.h"
10 #include "base/strings/string_util.h"
11 #include "tools/gn/config.h"
12 #include "tools/gn/config_values_generator.h"
13 #include "tools/gn/err.h"
14 #include "tools/gn/input_file.h"
15 #include "tools/gn/parse_tree.h"
16 #include "tools/gn/scheduler.h"
17 #include "tools/gn/scope.h"
18 #include "tools/gn/settings.h"
19 #include "tools/gn/template.h"
20 #include "tools/gn/token.h"
21 #include "tools/gn/value.h"
23 bool EnsureNotProcessingImport(const ParseNode* node,
24 const Scope* scope,
25 Err* err) {
26 if (scope->IsProcessingImport()) {
27 *err = Err(node, "Not valid from an import.",
28 "Imports are for defining defaults, variables, and rules. The\n"
29 "appropriate place for this kind of thing is really in a normal\n"
30 "BUILD file.");
31 return false;
33 return true;
36 bool EnsureNotProcessingBuildConfig(const ParseNode* node,
37 const Scope* scope,
38 Err* err) {
39 if (scope->IsProcessingBuildConfig()) {
40 *err = Err(node, "Not valid from the build config.",
41 "You can't do this kind of thing from the build config script, "
42 "silly!\nPut it in a regular BUILD file.");
43 return false;
45 return true;
48 bool FillTargetBlockScope(const Scope* scope,
49 const FunctionCallNode* function,
50 const std::string& target_type,
51 const BlockNode* block,
52 const std::vector<Value>& args,
53 Scope* block_scope,
54 Err* err) {
55 if (!block) {
56 FillNeedsBlockError(function, err);
57 return false;
60 // Copy the target defaults, if any, into the scope we're going to execute
61 // the block in.
62 const Scope* default_scope = scope->GetTargetDefaults(target_type);
63 if (default_scope) {
64 Scope::MergeOptions merge_options;
65 merge_options.skip_private_vars = true;
66 if (!default_scope->NonRecursiveMergeTo(block_scope, merge_options,
67 function, "target defaults", err))
68 return false;
71 // The name is the single argument to the target function.
72 if (!EnsureSingleStringArg(function, args, err))
73 return false;
75 // Set the target name variable to the current target, and mark it used
76 // because we don't want to issue an error if the script ignores it.
77 const base::StringPiece target_name("target_name");
78 block_scope->SetValue(target_name, Value(function, args[0].string_value()),
79 function);
80 block_scope->MarkUsed(target_name);
81 return true;
84 void FillNeedsBlockError(const FunctionCallNode* function, Err* err) {
85 *err = Err(function->function(), "This function call requires a block.",
86 "The block's \"{\" must be on the same line as the function "
87 "call's \")\".");
90 bool EnsureSingleStringArg(const FunctionCallNode* function,
91 const std::vector<Value>& args,
92 Err* err) {
93 if (args.size() != 1) {
94 *err = Err(function->function(), "Incorrect arguments.",
95 "This function requires a single string argument.");
96 return false;
98 return args[0].VerifyTypeIs(Value::STRING, err);
101 const Label& ToolchainLabelForScope(const Scope* scope) {
102 return scope->settings()->toolchain_label();
105 Label MakeLabelForScope(const Scope* scope,
106 const FunctionCallNode* function,
107 const std::string& name) {
108 const Label& toolchain_label = ToolchainLabelForScope(scope);
109 return Label(scope->GetSourceDir(), name, toolchain_label.dir(),
110 toolchain_label.name());
113 namespace functions {
115 // assert ----------------------------------------------------------------------
117 const char kAssert[] = "assert";
118 const char kAssert_HelpShort[] =
119 "assert: Assert an expression is true at generation time.";
120 const char kAssert_Help[] =
121 "assert: Assert an expression is true at generation time.\n"
122 "\n"
123 " assert(<condition> [, <error string>])\n"
124 "\n"
125 " If the condition is false, the build will fail with an error. If the\n"
126 " optional second argument is provided, that string will be printed\n"
127 " with the error message.\n"
128 "\n"
129 "Examples:\n"
130 " assert(is_win)\n"
131 " assert(defined(sources), \"Sources must be defined\")\n";
133 Value RunAssert(Scope* scope,
134 const FunctionCallNode* function,
135 const std::vector<Value>& args,
136 Err* err) {
137 if (args.size() != 1 && args.size() != 2) {
138 *err = Err(function->function(), "Wrong number of arguments.",
139 "assert() takes one or two argument, "
140 "were you expecting somethig else?");
141 } else if (args[0].type() != Value::BOOLEAN) {
142 *err = Err(function->function(), "Assertion value not a bool.");
143 } else if (!args[0].boolean_value()) {
144 if (args.size() == 2) {
145 // Optional string message.
146 if (args[1].type() != Value::STRING) {
147 *err = Err(function->function(), "Assertion failed.",
148 "<<<ERROR MESSAGE IS NOT A STRING>>>");
149 } else {
150 *err = Err(function->function(), "Assertion failed.",
151 args[1].string_value());
153 } else {
154 *err = Err(function->function(), "Assertion failed.");
157 if (args[0].origin()) {
158 // If you do "assert(foo)" we'd ideally like to show you where foo was
159 // set, and in this case the origin of the args will tell us that.
160 // However, if you do "assert(foo && bar)" the source of the value will
161 // be the assert like, which isn't so helpful.
163 // So we try to see if the args are from the same line or not. This will
164 // break if you do "assert(\nfoo && bar)" and we may show the second line
165 // as the source, oh well. The way around this is to check to see if the
166 // origin node is inside our function call block.
167 Location origin_location = args[0].origin()->GetRange().begin();
168 if (origin_location.file() != function->function().location().file() ||
169 origin_location.line_number() !=
170 function->function().location().line_number()) {
171 err->AppendSubErr(Err(args[0].origin()->GetRange(), "",
172 "This is where it was set."));
176 return Value();
179 // config ----------------------------------------------------------------------
181 const char kConfig[] = "config";
182 const char kConfig_HelpShort[] =
183 "config: Defines a configuration object.";
184 const char kConfig_Help[] =
185 "config: Defines a configuration object.\n"
186 "\n"
187 " Configuration objects can be applied to targets and specify sets of\n"
188 " compiler flags, includes, defines, etc. They provide a way to\n"
189 " conveniently group sets of this configuration information.\n"
190 "\n"
191 " A config is referenced by its label just like a target.\n"
192 "\n"
193 " The values in a config are additive only. If you want to remove a flag\n"
194 " you need to remove the corresponding config that sets it. The final\n"
195 " set of flags, defines, etc. for a target is generated in this order:\n"
196 "\n"
197 " 1. The values specified directly on the target (rather than using a\n"
198 " config.\n"
199 " 2. The configs specified in the target's \"configs\" list, in order.\n"
200 " 3. Public_configs from a breadth-first traversal of the dependency\n"
201 " tree in the order that the targets appear in \"deps\".\n"
202 " 4. All dependent configs from a breadth-first traversal of the\n"
203 " dependency tree in the order that the targets appear in \"deps\".\n"
204 "\n"
205 "Variables valid in a config definition:\n"
206 CONFIG_VALUES_VARS_HELP
207 "\n"
208 "Variables on a target used to apply configs:\n"
209 " all_dependent_configs, configs, public_configs,\n"
210 " forward_dependent_configs_from\n"
211 "\n"
212 "Example:\n"
213 " config(\"myconfig\") {\n"
214 " includes = [ \"include/common\" ]\n"
215 " defines = [ \"ENABLE_DOOM_MELON\" ]\n"
216 " }\n"
217 "\n"
218 " executable(\"mything\") {\n"
219 " configs = [ \":myconfig\" ]\n"
220 " }\n";
222 Value RunConfig(const FunctionCallNode* function,
223 const std::vector<Value>& args,
224 Scope* scope,
225 Err* err) {
226 if (!EnsureSingleStringArg(function, args, err) ||
227 !EnsureNotProcessingImport(function, scope, err))
228 return Value();
230 Label label(MakeLabelForScope(scope, function, args[0].string_value()));
232 if (g_scheduler->verbose_logging())
233 g_scheduler->Log("Defining config", label.GetUserVisibleName(true));
235 // Create the new config.
236 scoped_ptr<Config> config(new Config(scope->settings(), label));
237 config->set_defined_from(function);
238 if (!Visibility::FillItemVisibility(config.get(), scope, err))
239 return Value();
241 // Fill it.
242 const SourceDir& input_dir = scope->GetSourceDir();
243 ConfigValuesGenerator gen(&config->config_values(), scope, input_dir, err);
244 gen.Run();
245 if (err->has_error())
246 return Value();
248 // Save the generated item.
249 Scope::ItemVector* collector = scope->GetItemCollector();
250 if (!collector) {
251 *err = Err(function, "Can't define a config in this context.");
252 return Value();
254 collector->push_back(config.release());
256 return Value();
259 // declare_args ----------------------------------------------------------------
261 const char kDeclareArgs[] = "declare_args";
262 const char kDeclareArgs_HelpShort[] =
263 "declare_args: Declare build arguments.";
264 const char kDeclareArgs_Help[] =
265 "declare_args: Declare build arguments.\n"
266 "\n"
267 " Introduces the given arguments into the current scope. If they are\n"
268 " not specified on the command line or in a toolchain's arguments,\n"
269 " the default values given in the declare_args block will be used.\n"
270 " However, these defaults will not override command-line values.\n"
271 "\n"
272 " See also \"gn help buildargs\" for an overview.\n"
273 "\n"
274 "Example:\n"
275 " declare_args() {\n"
276 " enable_teleporter = true\n"
277 " enable_doom_melon = false\n"
278 " }\n"
279 "\n"
280 " If you want to override the (default disabled) Doom Melon:\n"
281 " gn --args=\"enable_doom_melon=true enable_teleporter=false\"\n"
282 " This also sets the teleporter, but it's already defaulted to on so\n"
283 " it will have no effect.\n";
285 Value RunDeclareArgs(Scope* scope,
286 const FunctionCallNode* function,
287 const std::vector<Value>& args,
288 BlockNode* block,
289 Err* err) {
290 Scope block_scope(scope);
291 block->ExecuteBlockInScope(&block_scope, err);
292 if (err->has_error())
293 return Value();
295 // Pass the values from our scope into the Args object for adding to the
296 // scope with the proper values (taking into account the defaults given in
297 // the block_scope, and arguments passed into the build).
298 Scope::KeyValueMap values;
299 block_scope.GetCurrentScopeValues(&values);
300 scope->settings()->build_settings()->build_args().DeclareArgs(
301 values, scope, err);
302 return Value();
305 // defined ---------------------------------------------------------------------
307 const char kDefined[] = "defined";
308 const char kDefined_HelpShort[] =
309 "defined: Returns whether an identifier is defined.";
310 const char kDefined_Help[] =
311 "defined: Returns whether an identifier is defined.\n"
312 "\n"
313 " Returns true if the given argument is defined. This is most useful in\n"
314 " templates to assert that the caller set things up properly.\n"
315 "\n"
316 " You can pass an identifier:\n"
317 " defined(foo)\n"
318 " which will return true or false depending on whether foo is defined in\n"
319 " the current scope.\n"
320 "\n"
321 " You can also check a named scope:\n"
322 " defined(foo.bar)\n"
323 " which returns true if both foo is defined and bar is defined on the\n"
324 " named scope foo. It will throw an error if foo is defined but is not\n"
325 " a scope.\n"
326 "\n"
327 "Example:\n"
328 "\n"
329 " template(\"mytemplate\") {\n"
330 " # To help users call this template properly...\n"
331 " assert(defined(invoker.sources), \"Sources must be defined\")\n"
332 "\n"
333 " # If we want to accept an optional \"values\" argument, we don't\n"
334 " # want to dereference something that may not be defined.\n"
335 " if (defined(invoker.values)) {\n"
336 " values = invoker.values\n"
337 " } else {\n"
338 " values = \"some default value\"\n"
339 " }\n"
340 " }\n";
342 Value RunDefined(Scope* scope,
343 const FunctionCallNode* function,
344 const ListNode* args_list,
345 Err* err) {
346 const std::vector<const ParseNode*>& args_vector = args_list->contents();
347 if (args_vector.size() != 1) {
348 *err = Err(function, "Wrong number of arguments to defined().",
349 "Expecting exactly one.");
350 return Value();
353 const IdentifierNode* identifier = args_vector[0]->AsIdentifier();
354 if (identifier) {
355 // Passed an identifier "defined(foo)".
356 if (scope->GetValue(identifier->value().value()))
357 return Value(function, true);
358 return Value(function, false);
361 const AccessorNode* accessor = args_vector[0]->AsAccessor();
362 if (accessor) {
363 // Passed an accessor "defined(foo.bar)".
364 if (accessor->member()) {
365 // The base of the accessor must be a scope if it's defined.
366 const Value* base = scope->GetValue(accessor->base().value());
367 if (!base)
368 return Value(function, false);
369 if (!base->VerifyTypeIs(Value::SCOPE, err))
370 return Value();
372 // Check the member inside the scope to see if its defined.
373 if (base->scope_value()->GetValue(accessor->member()->value().value()))
374 return Value(function, true);
375 return Value(function, false);
379 // Argument is invalid.
380 *err = Err(function, "Bad thing passed to defined().",
381 "It should be of the form defined(foo) or defined(foo.bar).");
382 return Value();
385 // getenv ----------------------------------------------------------------------
387 const char kGetEnv[] = "getenv";
388 const char kGetEnv_HelpShort[] =
389 "getenv: Get an environment variable.";
390 const char kGetEnv_Help[] =
391 "getenv: Get an environment variable.\n"
392 "\n"
393 " value = getenv(env_var_name)\n"
394 "\n"
395 " Returns the value of the given enironment variable. If the value is\n"
396 " not found, it will try to look up the variable with the \"opposite\"\n"
397 " case (based on the case of the first letter of the variable), but\n"
398 " is otherwise case-sensitive.\n"
399 "\n"
400 " If the environment variable is not found, the empty string will be\n"
401 " returned. Note: it might be nice to extend this if we had the concept\n"
402 " of \"none\" in the language to indicate lookup failure.\n"
403 "\n"
404 "Example:\n"
405 "\n"
406 " home_dir = getenv(\"HOME\")\n";
408 Value RunGetEnv(Scope* scope,
409 const FunctionCallNode* function,
410 const std::vector<Value>& args,
411 Err* err) {
412 if (!EnsureSingleStringArg(function, args, err))
413 return Value();
415 scoped_ptr<base::Environment> env(base::Environment::Create());
417 std::string result;
418 if (!env->GetVar(args[0].string_value().c_str(), &result))
419 return Value(function, ""); // Not found, return empty string.
420 return Value(function, result);
423 // import ----------------------------------------------------------------------
425 const char kImport[] = "import";
426 const char kImport_HelpShort[] =
427 "import: Import a file into the current scope.";
428 const char kImport_Help[] =
429 "import: Import a file into the current scope.\n"
430 "\n"
431 " The import command loads the rules and variables resulting from\n"
432 " executing the given file into the current scope.\n"
433 "\n"
434 " By convention, imported files are named with a .gni extension.\n"
435 "\n"
436 " An import is different than a C++ \"include\". The imported file is\n"
437 " executed in a standalone environment from the caller of the import\n"
438 " command. The results of this execution are cached for other files that\n"
439 " import the same .gni file.\n"
440 "\n"
441 " Note that you can not import a BUILD.gn file that's otherwise used\n"
442 " in the build. Files must either be imported or implicitly loaded as\n"
443 " a result of deps rules, but not both.\n"
444 "\n"
445 " The imported file's scope will be merged with the scope at the point\n"
446 " import was called. If there is a conflict (both the current scope and\n"
447 " the imported file define some variable or rule with the same name but\n"
448 " different value), a runtime error will be thrown. Therefore, it's good\n"
449 " practice to minimize the stuff that an imported file defines.\n"
450 "\n"
451 " Variables and templates beginning with an underscore '_' are\n"
452 " considered private and will not be imported. Imported files can use\n"
453 " such variables for internal computation without affecting other files.\n"
454 "\n"
455 "Examples:\n"
456 "\n"
457 " import(\"//build/rules/idl_compilation_rule.gni\")\n"
458 "\n"
459 " # Looks in the current directory.\n"
460 " import(\"my_vars.gni\")\n";
462 Value RunImport(Scope* scope,
463 const FunctionCallNode* function,
464 const std::vector<Value>& args,
465 Err* err) {
466 if (!EnsureSingleStringArg(function, args, err))
467 return Value();
469 const SourceDir& input_dir = scope->GetSourceDir();
470 SourceFile import_file =
471 input_dir.ResolveRelativeFile(args[0].string_value(),
472 scope->settings()->build_settings()->root_path_utf8());
473 scope->settings()->import_manager().DoImport(import_file, function,
474 scope, err);
475 return Value();
478 // set_sources_assignment_filter -----------------------------------------------
480 const char kSetSourcesAssignmentFilter[] = "set_sources_assignment_filter";
481 const char kSetSourcesAssignmentFilter_HelpShort[] =
482 "set_sources_assignment_filter: Set a pattern to filter source files.";
483 const char kSetSourcesAssignmentFilter_Help[] =
484 "set_sources_assignment_filter: Set a pattern to filter source files.\n"
485 "\n"
486 " The sources assignment filter is a list of patterns that remove files\n"
487 " from the list implicitly whenever the \"sources\" variable is\n"
488 " assigned to. This is intended to be used to globally filter out files\n"
489 " with platform-specific naming schemes when they don't apply, for\n"
490 " example, you may want to filter out all \"*_win.cc\" files on non-\n"
491 " Windows platforms.\n"
492 "\n"
493 " Typically this will be called once in the master build config script\n"
494 " to set up the filter for the current platform. Subsequent calls will\n"
495 " overwrite the previous values.\n"
496 "\n"
497 " If you want to bypass the filter and add a file even if it might\n"
498 " be filtered out, call set_sources_assignment_filter([]) to clear the\n"
499 " list of filters. This will apply until the current scope exits\n"
500 "\n"
501 "How to use patterns\n"
502 "\n"
503 " File patterns are VERY limited regular expressions. They must match\n"
504 " the entire input string to be counted as a match. In regular\n"
505 " expression parlance, there is an implicit \"^...$\" surrounding your\n"
506 " input. If you want to match a substring, you need to use wildcards at\n"
507 " the beginning and end.\n"
508 "\n"
509 " There are only two special tokens understood by the pattern matcher.\n"
510 " Everything else is a literal.\n"
511 "\n"
512 " * Matches zero or more of any character. It does not depend on the\n"
513 " preceding character (in regular expression parlance it is\n"
514 " equivalent to \".*\").\n"
515 "\n"
516 " \\b Matches a path boundary. This will match the beginning or end of\n"
517 " a string, or a slash.\n"
518 "\n"
519 "Pattern examples\n"
520 "\n"
521 " \"*asdf*\"\n"
522 " Matches a string containing \"asdf\" anywhere.\n"
523 "\n"
524 " \"asdf\"\n"
525 " Matches only the exact string \"asdf\".\n"
526 "\n"
527 " \"*.cc\"\n"
528 " Matches strings ending in the literal \".cc\".\n"
529 "\n"
530 " \"\\bwin/*\"\n"
531 " Matches \"win/foo\" and \"foo/win/bar.cc\" but not \"iwin/foo\".\n"
532 "\n"
533 "Sources assignment example\n"
534 "\n"
535 " # Filter out all _win files.\n"
536 " set_sources_assignment_filter([ \"*_win.cc\", \"*_win.h\" ])\n"
537 " sources = [ \"a.cc\", \"b_win.cc\" ]\n"
538 " print(sources)\n"
539 " # Will print [ \"a.cc\" ]. b_win one was filtered out.\n";
541 Value RunSetSourcesAssignmentFilter(Scope* scope,
542 const FunctionCallNode* function,
543 const std::vector<Value>& args,
544 Err* err) {
545 if (args.size() != 1) {
546 *err = Err(function, "set_sources_assignment_filter takes one argument.");
547 } else {
548 scoped_ptr<PatternList> f(new PatternList);
549 f->SetFromValue(args[0], err);
550 if (!err->has_error())
551 scope->set_sources_assignment_filter(f.Pass());
553 return Value();
556 // print -----------------------------------------------------------------------
558 const char kPrint[] = "print";
559 const char kPrint_HelpShort[] =
560 "print: Prints to the console.";
561 const char kPrint_Help[] =
562 "print: Prints to the console.\n"
563 "\n"
564 " Prints all arguments to the console separated by spaces. A newline is\n"
565 " automatically appended to the end.\n"
566 "\n"
567 " This function is intended for debugging. Note that build files are run\n"
568 " in parallel so you may get interleaved prints. A buildfile may also\n"
569 " be executed more than once in parallel in the context of different\n"
570 " toolchains so the prints from one file may be duplicated or\n"
571 " interleaved with itself.\n"
572 "\n"
573 "Examples:\n"
574 " print(\"Hello world\")\n"
575 "\n"
576 " print(sources, deps)\n";
578 Value RunPrint(Scope* scope,
579 const FunctionCallNode* function,
580 const std::vector<Value>& args,
581 Err* err) {
582 std::string output;
583 for (size_t i = 0; i < args.size(); i++) {
584 if (i != 0)
585 output.push_back(' ');
586 output.append(args[i].ToString(false));
588 output.push_back('\n');
590 const BuildSettings::PrintCallback& cb =
591 scope->settings()->build_settings()->print_callback();
592 if (cb.is_null())
593 printf("%s", output.c_str());
594 else
595 cb.Run(output);
597 return Value();
600 // -----------------------------------------------------------------------------
602 FunctionInfo::FunctionInfo()
603 : self_evaluating_args_runner(NULL),
604 generic_block_runner(NULL),
605 executed_block_runner(NULL),
606 no_block_runner(NULL),
607 help_short(NULL),
608 help(NULL),
609 is_target(false) {
612 FunctionInfo::FunctionInfo(SelfEvaluatingArgsFunction seaf,
613 const char* in_help_short,
614 const char* in_help,
615 bool in_is_target)
616 : self_evaluating_args_runner(seaf),
617 generic_block_runner(NULL),
618 executed_block_runner(NULL),
619 no_block_runner(NULL),
620 help_short(in_help_short),
621 help(in_help),
622 is_target(in_is_target) {
625 FunctionInfo::FunctionInfo(GenericBlockFunction gbf,
626 const char* in_help_short,
627 const char* in_help,
628 bool in_is_target)
629 : self_evaluating_args_runner(NULL),
630 generic_block_runner(gbf),
631 executed_block_runner(NULL),
632 no_block_runner(NULL),
633 help_short(in_help_short),
634 help(in_help),
635 is_target(in_is_target) {
638 FunctionInfo::FunctionInfo(ExecutedBlockFunction ebf,
639 const char* in_help_short,
640 const char* in_help,
641 bool in_is_target)
642 : self_evaluating_args_runner(NULL),
643 generic_block_runner(NULL),
644 executed_block_runner(ebf),
645 no_block_runner(NULL),
646 help_short(in_help_short),
647 help(in_help),
648 is_target(in_is_target) {
651 FunctionInfo::FunctionInfo(NoBlockFunction nbf,
652 const char* in_help_short,
653 const char* in_help,
654 bool in_is_target)
655 : self_evaluating_args_runner(NULL),
656 generic_block_runner(NULL),
657 executed_block_runner(NULL),
658 no_block_runner(nbf),
659 help_short(in_help_short),
660 help(in_help),
661 is_target(in_is_target) {
664 // Setup the function map via a static initializer. We use this because it
665 // avoids race conditions without having to do some global setup function or
666 // locking-heavy singleton checks at runtime. In practice, we always need this
667 // before we can do anything interesting, so it's OK to wait for the
668 // initializer.
669 struct FunctionInfoInitializer {
670 FunctionInfoMap map;
672 FunctionInfoInitializer() {
673 #define INSERT_FUNCTION(command, is_target) \
674 map[k##command] = FunctionInfo(&Run##command, \
675 k##command##_HelpShort, \
676 k##command##_Help, \
677 is_target);
679 INSERT_FUNCTION(Action, true)
680 INSERT_FUNCTION(ActionForEach, true)
681 INSERT_FUNCTION(Copy, true)
682 INSERT_FUNCTION(Executable, true)
683 INSERT_FUNCTION(Group, true)
684 INSERT_FUNCTION(SharedLibrary, true)
685 INSERT_FUNCTION(SourceSet, true)
686 INSERT_FUNCTION(StaticLibrary, true)
688 INSERT_FUNCTION(Assert, false)
689 INSERT_FUNCTION(Config, false)
690 INSERT_FUNCTION(DeclareArgs, false)
691 INSERT_FUNCTION(Defined, false)
692 INSERT_FUNCTION(ExecScript, false)
693 INSERT_FUNCTION(ForEach, false)
694 INSERT_FUNCTION(GetEnv, false)
695 INSERT_FUNCTION(GetLabelInfo, false)
696 INSERT_FUNCTION(GetPathInfo, false)
697 INSERT_FUNCTION(GetTargetOutputs, false)
698 INSERT_FUNCTION(Import, false)
699 INSERT_FUNCTION(Print, false)
700 INSERT_FUNCTION(ProcessFileTemplate, false)
701 INSERT_FUNCTION(ReadFile, false)
702 INSERT_FUNCTION(RebasePath, false)
703 INSERT_FUNCTION(SetDefaults, false)
704 INSERT_FUNCTION(SetDefaultToolchain, false)
705 INSERT_FUNCTION(SetSourcesAssignmentFilter, false)
706 INSERT_FUNCTION(Template, false)
707 INSERT_FUNCTION(Tool, false)
708 INSERT_FUNCTION(Toolchain, false)
709 INSERT_FUNCTION(ToolchainArgs, false)
710 INSERT_FUNCTION(WriteFile, false)
712 #undef INSERT_FUNCTION
715 const FunctionInfoInitializer function_info;
717 const FunctionInfoMap& GetFunctions() {
718 return function_info.map;
721 Value RunFunction(Scope* scope,
722 const FunctionCallNode* function,
723 const ListNode* args_list,
724 BlockNode* block,
725 Err* err) {
726 const Token& name = function->function();
728 const FunctionInfoMap& function_map = GetFunctions();
729 FunctionInfoMap::const_iterator found_function =
730 function_map.find(name.value());
731 if (found_function == function_map.end()) {
732 // No built-in function matching this, check for a template.
733 const Template* templ =
734 scope->GetTemplate(function->function().value().as_string());
735 if (templ) {
736 Value args = args_list->Execute(scope, err);
737 if (err->has_error())
738 return Value();
739 return templ->Invoke(scope, function, args.list_value(), block, err);
742 *err = Err(name, "Unknown function.");
743 return Value();
746 if (found_function->second.self_evaluating_args_runner) {
747 return found_function->second.self_evaluating_args_runner(
748 scope, function, args_list, err);
751 // All other function types take a pre-executed set of args.
752 Value args = args_list->Execute(scope, err);
753 if (err->has_error())
754 return Value();
756 if (found_function->second.generic_block_runner) {
757 if (!block) {
758 FillNeedsBlockError(function, err);
759 return Value();
761 return found_function->second.generic_block_runner(
762 scope, function, args.list_value(), block, err);
765 if (found_function->second.executed_block_runner) {
766 if (!block) {
767 FillNeedsBlockError(function, err);
768 return Value();
771 Scope block_scope(scope);
772 block->ExecuteBlockInScope(&block_scope, err);
773 if (err->has_error())
774 return Value();
776 Value result = found_function->second.executed_block_runner(
777 function, args.list_value(), &block_scope, err);
778 if (err->has_error())
779 return Value();
781 if (!block_scope.CheckForUnusedVars(err))
782 return Value();
783 return result;
786 // Otherwise it's a no-block function.
787 return found_function->second.no_block_runner(scope, function,
788 args.list_value(), err);
791 } // namespace functions