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 #ifndef TOOLS_GN_FUNCTIONS_H_
6 #define TOOLS_GN_FUNCTIONS_H_
11 #include "base/containers/hash_tables.h"
12 #include "base/strings/string_piece.h"
16 class FunctionCallNode
;
24 // -----------------------------------------------------------------------------
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.
31 typedef Value (*SelfEvaluatingArgsFunction
)(Scope
* scope
,
32 const FunctionCallNode
* function
,
33 const ListNode
* args_list
,
36 // This type of function invocation takes a block node that it will execute.
37 typedef Value (*GenericBlockFunction
)(Scope
* scope
,
38 const FunctionCallNode
* function
,
39 const std::vector
<Value
>& args
,
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.
46 typedef Value(*ExecutedBlockFunction
)(const FunctionCallNode
* function
,
47 const std::vector
<Value
>& args
,
51 // This type of function does not take a block. It just has arguments.
52 typedef Value (*NoBlockFunction
)(Scope
* scope
,
53 const FunctionCallNode
* function
,
54 const std::vector
<Value
>& args
,
57 extern const char kAssert
[];
58 extern const char kAssert_Help
[];
59 Value
RunAssert(Scope
* scope
,
60 const FunctionCallNode
* function
,
61 const std::vector
<Value
>& args
,
64 extern const char kComponent
[];
65 extern const char kComponent_Help
[];
66 Value
RunComponent(Scope
* scope
,
67 const FunctionCallNode
* function
,
68 const std::vector
<Value
>& args
,
72 extern const char kConfig
[];
73 extern const char kConfig_Help
[];
74 Value
RunConfig(const FunctionCallNode
* function
,
75 const std::vector
<Value
>& args
,
79 extern const char kCopy
[];
80 extern const char kCopy_Help
[];
81 Value
RunCopy(const FunctionCallNode
* function
,
82 const std::vector
<Value
>& args
,
86 extern const char kCustom
[];
87 extern const char kCustom_Help
[];
88 Value
RunCustom(Scope
* scope
,
89 const FunctionCallNode
* function
,
90 const std::vector
<Value
>& args
,
94 extern const char kDeclareArgs
[];
95 extern const char kDeclareArgs_Help
[];
96 Value
RunDeclareArgs(Scope
* scope
,
97 const FunctionCallNode
* function
,
98 const std::vector
<Value
>& args
,
102 extern const char kDefined
[];
103 extern const char kDefined_Help
[];
104 Value
RunDefined(Scope
* scope
,
105 const FunctionCallNode
* function
,
106 const ListNode
* args_list
,
109 extern const char kExecScript
[];
110 extern const char kExecScript_Help
[];
111 Value
RunExecScript(Scope
* scope
,
112 const FunctionCallNode
* function
,
113 const std::vector
<Value
>& args
,
116 extern const char kExecutable
[];
117 extern const char kExecutable_Help
[];
118 Value
RunExecutable(Scope
* scope
,
119 const FunctionCallNode
* function
,
120 const std::vector
<Value
>& args
,
124 extern const char kGroup
[];
125 extern const char kGroup_Help
[];
126 Value
RunGroup(Scope
* scope
,
127 const FunctionCallNode
* function
,
128 const std::vector
<Value
>& args
,
132 extern const char kImport
[];
133 extern const char kImport_Help
[];
134 Value
RunImport(Scope
* scope
,
135 const FunctionCallNode
* function
,
136 const std::vector
<Value
>& args
,
139 extern const char kPrint
[];
140 extern const char kPrint_Help
[];
141 Value
RunPrint(Scope
* scope
,
142 const FunctionCallNode
* function
,
143 const std::vector
<Value
>& args
,
146 extern const char kProcessFileTemplate
[];
147 extern const char kProcessFileTemplate_Help
[];
148 Value
RunProcessFileTemplate(Scope
* scope
,
149 const FunctionCallNode
* function
,
150 const std::vector
<Value
>& args
,
153 extern const char kReadFile
[];
154 extern const char kReadFile_Help
[];
155 Value
RunReadFile(Scope
* scope
,
156 const FunctionCallNode
* function
,
157 const std::vector
<Value
>& args
,
160 extern const char kRebasePath
[];
161 extern const char kRebasePath_Help
[];
162 Value
RunRebasePath(Scope
* scope
,
163 const FunctionCallNode
* function
,
164 const std::vector
<Value
>& args
,
167 extern const char kSetDefaults
[];
168 extern const char kSetDefaults_Help
[];
169 Value
RunSetDefaults(Scope
* scope
,
170 const FunctionCallNode
* function
,
171 const std::vector
<Value
>& args
,
175 extern const char kSetDefaultToolchain
[];
176 extern const char kSetDefaultToolchain_Help
[];
177 Value
RunSetDefaultToolchain(Scope
* scope
,
178 const FunctionCallNode
* function
,
179 const std::vector
<Value
>& args
,
182 extern const char kSetSourcesAssignmentFilter
[];
183 extern const char kSetSourcesAssignmentFilter_Help
[];
184 Value
RunSetSourcesAssignmentFilter(Scope
* scope
,
185 const FunctionCallNode
* function
,
186 const std::vector
<Value
>& args
,
189 extern const char kSharedLibrary
[];
190 extern const char kSharedLibrary_Help
[];
191 Value
RunSharedLibrary(Scope
* scope
,
192 const FunctionCallNode
* function
,
193 const std::vector
<Value
>& args
,
197 extern const char kSourceSet
[];
198 extern const char kSourceSet_Help
[];
199 Value
RunSourceSet(Scope
* scope
,
200 const FunctionCallNode
* function
,
201 const std::vector
<Value
>& args
,
205 extern const char kStaticLibrary
[];
206 extern const char kStaticLibrary_Help
[];
207 Value
RunStaticLibrary(Scope
* scope
,
208 const FunctionCallNode
* function
,
209 const std::vector
<Value
>& args
,
213 extern const char kTemplate
[];
214 extern const char kTemplate_Help
[];
215 Value
RunTemplate(Scope
* scope
,
216 const FunctionCallNode
* function
,
217 const std::vector
<Value
>& args
,
221 extern const char kTest
[];
222 extern const char kTest_Help
[];
223 Value
RunTest(Scope
* scope
,
224 const FunctionCallNode
* function
,
225 const std::vector
<Value
>& args
,
229 extern const char kTool
[];
230 extern const char kTool_Help
[];
231 Value
RunTool(Scope
* scope
,
232 const FunctionCallNode
* function
,
233 const std::vector
<Value
>& args
,
237 extern const char kToolchain
[];
238 extern const char kToolchain_Help
[];
239 Value
RunToolchain(Scope
* scope
,
240 const FunctionCallNode
* function
,
241 const std::vector
<Value
>& args
,
245 extern const char kToolchainArgs
[];
246 extern const char kToolchainArgs_Help
[];
247 Value
RunToolchainArgs(Scope
* scope
,
248 const FunctionCallNode
* function
,
249 const std::vector
<Value
>& args
,
253 extern const char kWriteFile
[];
254 extern const char kWriteFile_Help
[];
255 Value
RunWriteFile(Scope
* scope
,
256 const FunctionCallNode
* function
,
257 const std::vector
<Value
>& args
,
260 // -----------------------------------------------------------------------------
262 // One function record. Only one of the given runner types will be non-null
263 // which indicates the type of function it is.
264 struct FunctionInfo
{
266 FunctionInfo(SelfEvaluatingArgsFunction seaf
, const char* in_help
);
267 FunctionInfo(GenericBlockFunction gbf
, const char* in_help
);
268 FunctionInfo(ExecutedBlockFunction ebf
, const char* in_help
);
269 FunctionInfo(NoBlockFunction nbf
, const char* in_help
);
271 SelfEvaluatingArgsFunction self_evaluating_args_runner
;
272 GenericBlockFunction generic_block_runner
;
273 ExecutedBlockFunction executed_block_runner
;
274 NoBlockFunction no_block_runner
;
279 typedef base::hash_map
<base::StringPiece
, FunctionInfo
> FunctionInfoMap
;
281 // Returns the mapping of all built-in functions.
282 const FunctionInfoMap
& GetFunctions();
284 // Runs the given function.
285 Value
RunFunction(Scope
* scope
,
286 const FunctionCallNode
* function
,
287 const ListNode
* args_list
,
288 BlockNode
* block
, // Optional.
291 } // namespace functions
293 // Helper functions -----------------------------------------------------------
295 // Verifies that the current scope is not processing an import. If it is, it
296 // will set the error, blame the given parse node for it, and return false.
297 bool EnsureNotProcessingImport(const ParseNode
* node
,
301 // Like EnsureNotProcessingImport but checks for running the build config.
302 bool EnsureNotProcessingBuildConfig(const ParseNode
* node
,
306 // Sets up the |block_scope| for executing a target (or something like it).
307 // The |scope| is the containing scope. It should have been already set as the
308 // parent for the |block_scope| when the |block_scope| was created.
310 // This will set up the target defaults and set the |target_name| variable in
311 // the block scope to the current target name, which is assumed to be the first
312 // argument to the function.
314 // On success, returns true. On failure, sets the error and returns false.
315 bool FillTargetBlockScope(const Scope
* scope
,
316 const FunctionCallNode
* function
,
317 const std::string
& target_type
,
318 const BlockNode
* block
,
319 const std::vector
<Value
>& args
,
323 // Sets the given error to a message explaining that the function call requires
325 void FillNeedsBlockError(const FunctionCallNode
* function
, Err
* err
);
327 // Validates that the given function call has one string argument. This is
328 // the most common function signature, so it saves space to have this helper.
329 // Returns false and sets the error on failure.
330 bool EnsureSingleStringArg(const FunctionCallNode
* function
,
331 const std::vector
<Value
>& args
,
334 // Returns the name of the toolchain for the given scope.
335 const Label
& ToolchainLabelForScope(const Scope
* scope
);
337 // Generates a label for the given scope, using the current directory and
338 // toolchain, and the given name.
339 Label
MakeLabelForScope(const Scope
* scope
,
340 const FunctionCallNode
* function
,
341 const std::string
& name
);
343 #endif // TOOLS_GN_FUNCTIONS_H_