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#ifndef TOOLS_GN_FUNCTIONS_H_ 6#define TOOLS_GN_FUNCTIONS_H_ 7 8#include <map> 9#include <string> 10#include <vector> 11 12#include "base/strings/string_piece.h" 13 14class Err; 15class BlockNode; 16class FunctionCallNode; 17class Label; 18class ListNode; 19class ParseNode; 20class Scope; 21class Token; 22class Value; 23 24// ----------------------------------------------------------------------------- 25 26namespace functions { 27 28// This type of function invocation has no block and evaluates its arguments 29// itself rather than taking a pre-executed list. This allows us to implement 30// certain built-in functions. 31typedef Value (*SelfEvaluatingArgsFunction)(Scope* scope, 32 const FunctionCallNode* function, 33 const ListNode* args_list, 34 Err* err); 35 36// This type of function invocation takes a block node that it will execute. 37typedef Value (*GenericBlockFunction)(Scope* scope, 38 const FunctionCallNode* function, 39 const std::vector<Value>& args, 40 BlockNode* block, 41 Err* err); 42 43// This type of function takes a block, but does not need to control execution 44// of it. The dispatch function will pre-execute the block and pass the 45// resulting block_scope to the function. 46typedef Value(*ExecutedBlockFunction)(const FunctionCallNode* function, 47 const std::vector<Value>& args, 48 Scope* block_scope, 49 Err* err); 50 51// This type of function does not take a block. It just has arguments. 52typedef Value (*NoBlockFunction)(Scope* scope, 53 const FunctionCallNode* function, 54 const std::vector<Value>& args, 55 Err* err); 56 57extern const char kAction[]; 58extern const char kAction_HelpShort[]; 59extern const char kAction_Help[]; 60Value RunAction(Scope* scope, 61 const FunctionCallNode* function, 62 const std::vector<Value>& args, 63 BlockNode* block, 64 Err* err); 65 66extern const char kActionForEach[]; 67extern const char kActionForEach_HelpShort[]; 68extern const char kActionForEach_Help[]; 69Value RunActionForEach(Scope* scope, 70 const FunctionCallNode* function, 71 const std::vector<Value>& args, 72 BlockNode* block, 73 Err* err); 74 75extern const char kAssert[]; 76extern const char kAssert_HelpShort[]; 77extern const char kAssert_Help[]; 78Value RunAssert(Scope* scope, 79 const FunctionCallNode* function, 80 const std::vector<Value>& args, 81 Err* err); 82 83extern const char kConfig[]; 84extern const char kConfig_HelpShort[]; 85extern const char kConfig_Help[]; 86Value RunConfig(const FunctionCallNode* function, 87 const std::vector<Value>& args, 88 Scope* block_scope, 89 Err* err); 90 91extern const char kCopy[]; 92extern const char kCopy_HelpShort[]; 93extern const char kCopy_Help[]; 94Value RunCopy(const FunctionCallNode* function, 95 const std::vector<Value>& args, 96 Scope* block_scope, 97 Err* err); 98 99extern const char kDeclareArgs[]; 100extern const char kDeclareArgs_HelpShort[]; 101extern const char kDeclareArgs_Help[]; 102Value RunDeclareArgs(Scope* scope, 103 const FunctionCallNode* function, 104 const std::vector<Value>& args, 105 BlockNode* block, 106 Err* err); 107 108extern const char kDefined[]; 109extern const char kDefined_HelpShort[]; 110extern const char kDefined_Help[]; 111Value RunDefined(Scope* scope, 112 const FunctionCallNode* function, 113 const ListNode* args_list, 114 Err* err); 115 116extern const char kExecScript[]; 117extern const char kExecScript_HelpShort[]; 118extern const char kExecScript_Help[]; 119Value RunExecScript(Scope* scope, 120 const FunctionCallNode* function, 121 const std::vector<Value>& args, 122 Err* err); 123 124extern const char kExecutable[]; 125extern const char kExecutable_HelpShort[]; 126extern const char kExecutable_Help[]; 127Value RunExecutable(Scope* scope, 128 const FunctionCallNode* function, 129 const std::vector<Value>& args, 130 BlockNode* block, 131 Err* err); 132 133extern const char kForEach[]; 134extern const char kForEach_HelpShort[]; 135extern const char kForEach_Help[]; 136Value RunForEach(Scope* scope, 137 const FunctionCallNode* function, 138 const ListNode* args_list, 139 Err* err); 140 141extern const char kGetEnv[]; 142extern const char kGetEnv_HelpShort[]; 143extern const char kGetEnv_Help[]; 144Value RunGetEnv(Scope* scope, 145 const FunctionCallNode* function, 146 const std::vector<Value>& args, 147 Err* err); 148 149extern const char kGetLabelInfo[]; 150extern const char kGetLabelInfo_HelpShort[]; 151extern const char kGetLabelInfo_Help[]; 152Value RunGetLabelInfo(Scope* scope, 153 const FunctionCallNode* function, 154 const std::vector<Value>& args, 155 Err* err); 156 157extern const char kGetPathInfo[]; 158extern const char kGetPathInfo_HelpShort[]; 159extern const char kGetPathInfo_Help[]; 160Value RunGetPathInfo(Scope* scope, 161 const FunctionCallNode* function, 162 const std::vector<Value>& args, 163 Err* err); 164 165extern const char kGetTargetOutputs[]; 166extern const char kGetTargetOutputs_HelpShort[]; 167extern const char kGetTargetOutputs_Help[]; 168Value RunGetTargetOutputs(Scope* scope, 169 const FunctionCallNode* function, 170 const std::vector<Value>& args, 171 Err* err); 172 173extern const char kGroup[]; 174extern const char kGroup_HelpShort[]; 175extern const char kGroup_Help[]; 176Value RunGroup(Scope* scope, 177 const FunctionCallNode* function, 178 const std::vector<Value>& args, 179 BlockNode* block, 180 Err* err); 181 182extern const char kImport[]; 183extern const char kImport_HelpShort[]; 184extern const char kImport_Help[]; 185Value RunImport(Scope* scope, 186 const FunctionCallNode* function, 187 const std::vector<Value>& args, 188 Err* err); 189 190extern const char kPrint[]; 191extern const char kPrint_HelpShort[]; 192extern const char kPrint_Help[]; 193Value RunPrint(Scope* scope, 194 const FunctionCallNode* function, 195 const std::vector<Value>& args, 196 Err* err); 197 198extern const char kProcessFileTemplate[]; 199extern const char kProcessFileTemplate_HelpShort[]; 200extern const char kProcessFileTemplate_Help[]; 201Value RunProcessFileTemplate(Scope* scope, 202 const FunctionCallNode* function, 203 const std::vector<Value>& args, 204 Err* err); 205 206extern const char kReadFile[]; 207extern const char kReadFile_HelpShort[]; 208extern const char kReadFile_Help[]; 209Value RunReadFile(Scope* scope, 210 const FunctionCallNode* function, 211 const std::vector<Value>& args, 212 Err* err); 213 214extern const char kRebasePath[]; 215extern const char kRebasePath_HelpShort[]; 216extern const char kRebasePath_Help[]; 217Value RunRebasePath(Scope* scope, 218 const FunctionCallNode* function, 219 const std::vector<Value>& args, 220 Err* err); 221 222extern const char kSetDefaults[]; 223extern const char kSetDefaults_HelpShort[]; 224extern const char kSetDefaults_Help[]; 225Value RunSetDefaults(Scope* scope, 226 const FunctionCallNode* function, 227 const std::vector<Value>& args, 228 BlockNode* block, 229 Err* err); 230 231extern const char kSetDefaultToolchain[]; 232extern const char kSetDefaultToolchain_HelpShort[]; 233extern const char kSetDefaultToolchain_Help[]; 234Value RunSetDefaultToolchain(Scope* scope, 235 const FunctionCallNode* function, 236 const std::vector<Value>& args, 237 Err* err); 238 239extern const char kSetSourcesAssignmentFilter[]; 240extern const char kSetSourcesAssignmentFilter_HelpShort[]; 241extern const char kSetSourcesAssignmentFilter_Help[]; 242Value RunSetSourcesAssignmentFilter(Scope* scope, 243 const FunctionCallNode* function, 244 const std::vector<Value>& args, 245 Err* err); 246 247extern const char kSharedLibrary[]; 248extern const char kSharedLibrary_HelpShort[]; 249extern const char kSharedLibrary_Help[]; 250Value RunSharedLibrary(Scope* scope, 251 const FunctionCallNode* function, 252 const std::vector<Value>& args, 253 BlockNode* block, 254 Err* err); 255 256extern const char kSourceSet[]; 257extern const char kSourceSet_HelpShort[]; 258extern const char kSourceSet_Help[]; 259Value RunSourceSet(Scope* scope, 260 const FunctionCallNode* function, 261 const std::vector<Value>& args, 262 BlockNode* block, 263 Err* err); 264 265extern const char kStaticLibrary[]; 266extern const char kStaticLibrary_HelpShort[]; 267extern const char kStaticLibrary_Help[]; 268Value RunStaticLibrary(Scope* scope, 269 const FunctionCallNode* function, 270 const std::vector<Value>& args, 271 BlockNode* block, 272 Err* err); 273 274extern const char kTemplate[]; 275extern const char kTemplate_HelpShort[]; 276extern const char kTemplate_Help[]; 277Value RunTemplate(Scope* scope, 278 const FunctionCallNode* function, 279 const std::vector<Value>& args, 280 BlockNode* block, 281 Err* err); 282 283extern const char kTool[]; 284extern const char kTool_HelpShort[]; 285extern const char kTool_Help[]; 286Value RunTool(Scope* scope, 287 const FunctionCallNode* function, 288 const std::vector<Value>& args, 289 BlockNode* block, 290 Err* err); 291 292extern const char kToolchain[]; 293extern const char kToolchain_HelpShort[]; 294extern const char kToolchain_Help[]; 295Value RunToolchain(Scope* scope, 296 const FunctionCallNode* function, 297 const std::vector<Value>& args, 298 BlockNode* block, 299 Err* err); 300 301extern const char kToolchainArgs[]; 302extern const char kToolchainArgs_HelpShort[]; 303extern const char kToolchainArgs_Help[]; 304Value RunToolchainArgs(Scope* scope, 305 const FunctionCallNode* function, 306 const std::vector<Value>& args, 307 BlockNode* block, 308 Err* err); 309 310extern const char kWriteFile[]; 311extern const char kWriteFile_HelpShort[]; 312extern const char kWriteFile_Help[]; 313Value RunWriteFile(Scope* scope, 314 const FunctionCallNode* function, 315 const std::vector<Value>& args, 316 Err* err); 317 318// ----------------------------------------------------------------------------- 319 320// One function record. Only one of the given runner types will be non-null 321// which indicates the type of function it is. 322struct FunctionInfo { 323 FunctionInfo(); 324 FunctionInfo(SelfEvaluatingArgsFunction seaf, 325 const char* in_help_short, 326 const char* in_help, 327 bool in_is_target); 328 FunctionInfo(GenericBlockFunction gbf, 329 const char* in_help_short, 330 const char* in_help, 331 bool in_is_target); 332 FunctionInfo(ExecutedBlockFunction ebf, 333 const char* in_help_short, 334 const char* in_help, 335 bool in_is_target); 336 FunctionInfo(NoBlockFunction nbf, 337 const char* in_help_short, 338 const char* in_help, 339 bool in_is_target); 340 341 SelfEvaluatingArgsFunction self_evaluating_args_runner; 342 GenericBlockFunction generic_block_runner; 343 ExecutedBlockFunction executed_block_runner; 344 NoBlockFunction no_block_runner; 345 346 const char* help_short; 347 const char* help; 348 349 bool is_target; 350}; 351 352typedef std::map<base::StringPiece, FunctionInfo> FunctionInfoMap; 353 354// Returns the mapping of all built-in functions. 355const FunctionInfoMap& GetFunctions(); 356 357// Runs the given function. 358Value RunFunction(Scope* scope, 359 const FunctionCallNode* function, 360 const ListNode* args_list, 361 BlockNode* block, // Optional. 362 Err* err); 363 364} // namespace functions 365 366// Helper functions ----------------------------------------------------------- 367 368// Verifies that the current scope is not processing an import. If it is, it 369// will set the error, blame the given parse node for it, and return false. 370bool EnsureNotProcessingImport(const ParseNode* node, 371 const Scope* scope, 372 Err* err); 373 374// Like EnsureNotProcessingImport but checks for running the build config. 375bool EnsureNotProcessingBuildConfig(const ParseNode* node, 376 const Scope* scope, 377 Err* err); 378 379// Sets up the |block_scope| for executing a target (or something like it). 380// The |scope| is the containing scope. It should have been already set as the 381// parent for the |block_scope| when the |block_scope| was created. 382// 383// This will set up the target defaults and set the |target_name| variable in 384// the block scope to the current target name, which is assumed to be the first 385// argument to the function. 386// 387// On success, returns true. On failure, sets the error and returns false. 388bool FillTargetBlockScope(const Scope* scope, 389 const FunctionCallNode* function, 390 const std::string& target_type, 391 const BlockNode* block, 392 const std::vector<Value>& args, 393 Scope* block_scope, 394 Err* err); 395 396// Sets the given error to a message explaining that the function call requires 397// a block. 398void FillNeedsBlockError(const FunctionCallNode* function, Err* err); 399 400// Validates that the given function call has one string argument. This is 401// the most common function signature, so it saves space to have this helper. 402// Returns false and sets the error on failure. 403bool EnsureSingleStringArg(const FunctionCallNode* function, 404 const std::vector<Value>& args, 405 Err* err); 406 407// Returns the name of the toolchain for the given scope. 408const Label& ToolchainLabelForScope(const Scope* scope); 409 410// Generates a label for the given scope, using the current directory and 411// toolchain, and the given name. 412Label MakeLabelForScope(const Scope* scope, 413 const FunctionCallNode* function, 414 const std::string& name); 415 416#endif // TOOLS_GN_FUNCTIONS_H_ 417