OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef TOOLS_GN_SCOPE_H_ | 5 #ifndef TOOLS_GN_SCOPE_H_ |
6 #define TOOLS_GN_SCOPE_H_ | 6 #define TOOLS_GN_SCOPE_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 | 10 |
11 #include "base/basictypes.h" | 11 #include "base/basictypes.h" |
12 #include "base/containers/hash_tables.h" | 12 #include "base/containers/hash_tables.h" |
13 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
14 #include "tools/gn/err.h" | 14 #include "tools/gn/err.h" |
15 #include "tools/gn/pattern.h" | 15 #include "tools/gn/pattern.h" |
16 #include "tools/gn/source_dir.h" | 16 #include "tools/gn/source_dir.h" |
17 #include "tools/gn/value.h" | 17 #include "tools/gn/value.h" |
18 | 18 |
19 class FunctionCallNode; | 19 class FunctionCallNode; |
20 class ImportManager; | 20 class ImportManager; |
21 class ParseNode; | 21 class ParseNode; |
22 class Settings; | 22 class Settings; |
23 class TargetManager; | 23 class TargetManager; |
| 24 class Template; |
24 | 25 |
25 // Scope for the script execution. | 26 // Scope for the script execution. |
26 // | 27 // |
27 // Scopes are nested. Writing goes into the toplevel scope, reading checks | 28 // Scopes are nested. Writing goes into the toplevel scope, reading checks |
28 // values resursively down the stack until a match is found or there are no | 29 // values resursively down the stack until a match is found or there are no |
29 // more containing scopes. | 30 // more containing scopes. |
30 // | 31 // |
31 // A containing scope can be const or non-const. The const containing scope is | 32 // A containing scope can be const or non-const. The const containing scope is |
32 // used primarily to refer to the master build config which is shared across | 33 // used primarily to refer to the master build config which is shared across |
33 // many invocations. A const containing scope, however, prevents us from | 34 // many invocations. A const containing scope, however, prevents us from |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 const ParseNode* set_node); | 122 const ParseNode* set_node); |
122 | 123 |
123 // The set_node indicates the statement that caused the set, for displaying | 124 // The set_node indicates the statement that caused the set, for displaying |
124 // errors later. Returns a pointer to the value in the current scope (a copy | 125 // errors later. Returns a pointer to the value in the current scope (a copy |
125 // is made for storage). | 126 // is made for storage). |
126 Value* SetValue(const base::StringPiece& ident, | 127 Value* SetValue(const base::StringPiece& ident, |
127 const Value& v, | 128 const Value& v, |
128 const ParseNode* set_node); | 129 const ParseNode* set_node); |
129 | 130 |
130 // Templates associated with this scope. A template can only be set once, so | 131 // Templates associated with this scope. A template can only be set once, so |
131 // AddTemplate will fail and return NULL if a rule with that name already | 132 // AddTemplate will fail and return false if a rule with that name already |
132 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will | 133 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will |
133 // check all containing scoped rescursively. | 134 // check all containing scoped rescursively. |
134 bool AddTemplate(const std::string& name, const FunctionCallNode* decl); | 135 bool AddTemplate(const std::string& name, scoped_ptr<Template> templ); |
135 const FunctionCallNode* GetTemplate(const std::string& name) const; | 136 const Template* GetTemplate(const std::string& name) const; |
136 | 137 |
137 // Marks the given identifier as (un)used in the current scope. | 138 // Marks the given identifier as (un)used in the current scope. |
138 void MarkUsed(const base::StringPiece& ident); | 139 void MarkUsed(const base::StringPiece& ident); |
139 void MarkUnused(const base::StringPiece& ident); | 140 void MarkUnused(const base::StringPiece& ident); |
140 | 141 |
141 // Checks to see if the scope has a var set that hasn't been used. This is | 142 // Checks to see if the scope has a var set that hasn't been used. This is |
142 // called before replacing the var with a different one. It does not check | 143 // called before replacing the var with a different one. It does not check |
143 // containing scopes. | 144 // containing scopes. |
144 // | 145 // |
145 // If the identifier is present but hasnn't been used, return true. | 146 // If the identifier is present but hasnn't been used, return true. |
146 bool IsSetButUnused(const base::StringPiece& ident) const; | 147 bool IsSetButUnused(const base::StringPiece& ident) const; |
147 | 148 |
148 // Checks the scope to see if any values were set but not used, and fills in | 149 // Checks the scope to see if any values were set but not used, and fills in |
149 // the error and returns false if they were. | 150 // the error and returns false if they were. |
150 bool CheckForUnusedVars(Err* err) const; | 151 bool CheckForUnusedVars(Err* err) const; |
151 | 152 |
152 // Returns all values set in the current scope, without going to the parent | 153 // Returns all values set in the current scope, without going to the parent |
153 // scopes. | 154 // scopes. |
154 void GetCurrentScopeValues(KeyValueMap* output) const; | 155 void GetCurrentScopeValues(KeyValueMap* output) const; |
155 | 156 |
156 // Copies this scope's values into the destination. Values from the | 157 // Copies this scope's values into the destination. Values from the |
157 // containing scope(s) (normally shadowed into the current one) will not be | 158 // containing scope(s) (normally shadowed into the current one) will not be |
158 // copied, neither will the reference to the containing scope (this is why | 159 // copied, neither will the reference to the containing scope (this is why |
159 // it's "non-recursive"). | 160 // it's "non-recursive"). |
160 // | 161 // |
161 // It is an error to merge a variable into a scope that already has something | 162 // If clobber_existing is true, any existing values will be overwritten. In |
162 // with that name in scope (meaning in that scope or in any of its containing | 163 // this mode, this function will never fail. |
163 // scopes). If this happens, the error will be set and the function will | 164 // |
164 // return false. | 165 // If clobber_existing is false, it will be an error to merge a variable into |
| 166 // a scope that already has something with that name in scope (meaning in |
| 167 // that scope or in any of its containing scopes). If this happens, the error |
| 168 // will be set and the function will return false. |
165 // | 169 // |
166 // This is used in different contexts. When generating the error, the given | 170 // This is used in different contexts. When generating the error, the given |
167 // parse node will be blamed, and the given desc will be used to describe | 171 // parse node will be blamed, and the given desc will be used to describe |
168 // the operation that doesn't support doing this. For example, desc_for_err | 172 // the operation that doesn't support doing this. For example, desc_for_err |
169 // would be "import" when doing an import, and the error string would say | 173 // would be "import" when doing an import, and the error string would say |
170 // something like "The import contains...". | 174 // something like "The import contains...". |
171 bool NonRecursiveMergeTo(Scope* dest, | 175 bool NonRecursiveMergeTo(Scope* dest, |
| 176 bool clobber_existing, |
172 const ParseNode* node_for_err, | 177 const ParseNode* node_for_err, |
173 const char* desc_for_err, | 178 const char* desc_for_err, |
174 Err* err) const; | 179 Err* err) const; |
175 | 180 |
| 181 // Constructs a scope that is a copy of the current one. Nested scopes will |
| 182 // be collapsed until we reach a const containing scope. The resulting |
| 183 // closure will reference the const containing scope as its containing scope |
| 184 // (since we assume the const scope won't change, we don't have to copy its |
| 185 // values). |
| 186 scoped_ptr<Scope> MakeClosure() const; |
| 187 |
176 // Makes an empty scope with the given name. Returns NULL if the name is | 188 // Makes an empty scope with the given name. Returns NULL if the name is |
177 // already set. | 189 // already set. |
178 Scope* MakeTargetDefaults(const std::string& target_type); | 190 Scope* MakeTargetDefaults(const std::string& target_type); |
179 | 191 |
180 // Gets the scope associated with the given target name, or null if it hasn't | 192 // Gets the scope associated with the given target name, or null if it hasn't |
181 // been set. | 193 // been set. |
182 const Scope* GetTargetDefaults(const std::string& target_type) const; | 194 const Scope* GetTargetDefaults(const std::string& target_type) const; |
183 | 195 |
184 // Filter to apply when the sources variable is assigned. May return NULL. | 196 // Filter to apply when the sources variable is assigned. May return NULL. |
185 const PatternList* GetSourcesAssignmentFilter() const; | 197 const PatternList* GetSourcesAssignmentFilter() const; |
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
260 // Owning pointers. Note that this can't use string pieces since the names | 272 // Owning pointers. Note that this can't use string pieces since the names |
261 // are constructed from Values which might be deallocated before this goes | 273 // are constructed from Values which might be deallocated before this goes |
262 // out of scope. | 274 // out of scope. |
263 typedef base::hash_map<std::string, Scope*> NamedScopeMap; | 275 typedef base::hash_map<std::string, Scope*> NamedScopeMap; |
264 NamedScopeMap target_defaults_; | 276 NamedScopeMap target_defaults_; |
265 | 277 |
266 // Null indicates not set and that we should fallback to the containing | 278 // Null indicates not set and that we should fallback to the containing |
267 // scope's filter. | 279 // scope's filter. |
268 scoped_ptr<PatternList> sources_assignment_filter_; | 280 scoped_ptr<PatternList> sources_assignment_filter_; |
269 | 281 |
270 // Non-owning pointers, the function calls are owned by the input file which | 282 // Owning pointers, must be deleted. |
271 // should be kept around by the input file manager. | 283 typedef std::map<std::string, const Template*> TemplateMap; |
272 typedef std::map<std::string, const FunctionCallNode*> TemplateMap; | |
273 TemplateMap templates_; | 284 TemplateMap templates_; |
274 | 285 |
275 // Opaque pointers. See SetProperty() above. | 286 // Opaque pointers. See SetProperty() above. |
276 typedef std::map<const void*, void*> PropertyMap; | 287 typedef std::map<const void*, void*> PropertyMap; |
277 PropertyMap properties_; | 288 PropertyMap properties_; |
278 | 289 |
279 typedef std::set<ProgrammaticProvider*> ProviderSet; | 290 typedef std::set<ProgrammaticProvider*> ProviderSet; |
280 ProviderSet programmatic_providers_; | 291 ProviderSet programmatic_providers_; |
281 | 292 |
282 SourceDir source_dir_; | 293 SourceDir source_dir_; |
283 | 294 |
284 DISALLOW_COPY_AND_ASSIGN(Scope); | 295 DISALLOW_COPY_AND_ASSIGN(Scope); |
285 }; | 296 }; |
286 | 297 |
287 #endif // TOOLS_GN_SCOPE_H_ | 298 #endif // TOOLS_GN_SCOPE_H_ |
OLD | NEW |