Remove the now unused TextButton code.
[chromium-blink-merge.git] / tools / gn / scope.h
blob035d45b6a26360764610851b93a5b636172a01a2
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_SCOPE_H_
6 #define TOOLS_GN_SCOPE_H_
8 #include <map>
9 #include <set>
11 #include "base/basictypes.h"
12 #include "base/containers/hash_tables.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/scoped_vector.h"
16 #include "tools/gn/err.h"
17 #include "tools/gn/pattern.h"
18 #include "tools/gn/source_dir.h"
19 #include "tools/gn/value.h"
21 class FunctionCallNode;
22 class ImportManager;
23 class Item;
24 class ParseNode;
25 class Settings;
26 class TargetManager;
27 class Template;
29 // Scope for the script execution.
31 // Scopes are nested. Writing goes into the toplevel scope, reading checks
32 // values resursively down the stack until a match is found or there are no
33 // more containing scopes.
35 // A containing scope can be const or non-const. The const containing scope is
36 // used primarily to refer to the master build config which is shared across
37 // many invocations. A const containing scope, however, prevents us from
38 // marking variables "used" which prevents us from issuing errors on unused
39 // variables. So you should use a non-const containing scope whenever possible.
40 class Scope {
41 public:
42 typedef base::hash_map<base::StringPiece, Value> KeyValueMap;
43 // Holds an owning list of scoped_ptrs of Items (since we can't make a vector
44 // of scoped_ptrs).
45 typedef ScopedVector< scoped_ptr<Item> > ItemVector;
47 // Allows code to provide values for built-in variables. This class will
48 // automatically register itself on construction and deregister itself on
49 // destruction.
50 class ProgrammaticProvider {
51 public:
52 ProgrammaticProvider(Scope* scope) : scope_(scope) {
53 scope_->AddProvider(this);
55 ~ProgrammaticProvider() {
56 scope_->RemoveProvider(this);
59 // Returns a non-null value if the given value can be programmatically
60 // generated, or NULL if there is none.
61 virtual const Value* GetProgrammaticValue(
62 const base::StringPiece& ident) = 0;
64 protected:
65 Scope* scope_;
68 // Options for configuring scope merges.
69 struct MergeOptions {
70 // Defaults to all false, which are the things least likely to cause errors.
71 MergeOptions()
72 : clobber_existing(false),
73 skip_private_vars(false),
74 mark_used(false) {
77 // When set, all existing avlues in the destination scope will be
78 // overwritten.
80 // When false, it will be an error to merge a variable into another scope
81 // where a variable with the same name is already set. The exception is
82 // if both of the variables have the same value (which happens if you
83 // somehow multiply import the same file, for example). This case will be
84 // ignored since there is nothing getting lost.
85 bool clobber_existing;
87 // When true, private variables (names beginning with an underscore) will
88 // be copied to the destination scope. When false, private values will be
89 // skipped.
90 bool skip_private_vars;
92 // When set, values copied to the destination scope will be marked as used
93 // so won't trigger an unused variable warning. You want this when doing an
94 // import, for example, or files that don't need a variable from the .gni
95 // file will throw an error.
96 bool mark_used;
99 // Creates an empty toplevel scope.
100 Scope(const Settings* settings);
102 // Creates a dependent scope.
103 Scope(Scope* parent);
104 Scope(const Scope* parent);
106 ~Scope();
108 const Settings* settings() const { return settings_; }
110 // See the const_/mutable_containing_ var declaraions below. Yes, it's a
111 // bit weird that we can have a const pointer to the "mutable" one.
112 Scope* mutable_containing() { return mutable_containing_; }
113 const Scope* mutable_containing() const { return mutable_containing_; }
114 const Scope* const_containing() const { return const_containing_; }
115 const Scope* containing() const {
116 return mutable_containing_ ? mutable_containing_ : const_containing_;
119 // Returns NULL if there's no such value.
121 // counts_as_used should be set if the variable is being read in a way that
122 // should count for unused variable checking.
123 const Value* GetValue(const base::StringPiece& ident,
124 bool counts_as_used);
125 const Value* GetValue(const base::StringPiece& ident) const;
127 // Returns the requested value as a mutable one if possible. If the value
128 // is not found in a mutable scope, then returns null. Note that the value
129 // could still exist in a const scope, so GetValue() could still return
130 // non-null in this case.
132 // Say you have a local scope that then refers to the const root scope from
133 // the master build config. You can't change the values from the master
134 // build config (it's read-only so it can be read from multiple threads
135 // without locking). Read-only operations would work on values from the root
136 // scope, but write operations would only work on values in the derived
137 // scope(s).
139 // Be careful when calling this. It's not normally correct to modify values,
140 // but you should instead do a new Set each time.
142 // Consider this code:
143 // a = 5
144 // {
145 // a = 6
146 // }
147 // The 6 should get set on the nested scope rather than modify the value
148 // in the outer one.
149 Value* GetMutableValue(const base::StringPiece& ident, bool counts_as_used);
151 // Same as GetValue, but if the value exists in a parent scope, we'll copy
152 // it to the current scope. If the return value is non-null, the value is
153 // guaranteed to be set in the current scope. Generatlly this will be used
154 // if the calling code is planning on modifying the value in-place.
156 // Since this is used when doing read-modifies, we never count this access
157 // as reading the variable, since we assume it will be written to.
158 Value* GetValueForcedToCurrentScope(const base::StringPiece& ident,
159 const ParseNode* set_node);
161 // The set_node indicates the statement that caused the set, for displaying
162 // errors later. Returns a pointer to the value in the current scope (a copy
163 // is made for storage).
164 Value* SetValue(const base::StringPiece& ident,
165 const Value& v,
166 const ParseNode* set_node);
168 // Removes the value with the given identifier if it exists on the current
169 // scope. This does not search recursive scopes. Does nothing if not found.
170 void RemoveIdentifier(const base::StringPiece& ident);
172 // Removes from this scope all identifiers and templates that are considered
173 // private.
174 void RemovePrivateIdentifiers();
176 // Templates associated with this scope. A template can only be set once, so
177 // AddTemplate will fail and return false if a rule with that name already
178 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will
179 // check all containing scoped rescursively.
180 bool AddTemplate(const std::string& name, const Template* templ);
181 const Template* GetTemplate(const std::string& name) const;
183 // Marks the given identifier as (un)used in the current scope.
184 void MarkUsed(const base::StringPiece& ident);
185 void MarkUnused(const base::StringPiece& ident);
187 // Checks to see if the scope has a var set that hasn't been used. This is
188 // called before replacing the var with a different one. It does not check
189 // containing scopes.
191 // If the identifier is present but hasnn't been used, return true.
192 bool IsSetButUnused(const base::StringPiece& ident) const;
194 // Checks the scope to see if any values were set but not used, and fills in
195 // the error and returns false if they were.
196 bool CheckForUnusedVars(Err* err) const;
198 // Returns all values set in the current scope, without going to the parent
199 // scopes.
200 void GetCurrentScopeValues(KeyValueMap* output) const;
202 // Copies this scope's values into the destination. Values from the
203 // containing scope(s) (normally shadowed into the current one) will not be
204 // copied, neither will the reference to the containing scope (this is why
205 // it's "non-recursive").
207 // This is used in different contexts. When generating the error, the given
208 // parse node will be blamed, and the given desc will be used to describe
209 // the operation that doesn't support doing this. For example, desc_for_err
210 // would be "import" when doing an import, and the error string would say
211 // something like "The import contains...".
212 bool NonRecursiveMergeTo(Scope* dest,
213 const MergeOptions& options,
214 const ParseNode* node_for_err,
215 const char* desc_for_err,
216 Err* err) const;
218 // Constructs a scope that is a copy of the current one. Nested scopes will
219 // be collapsed until we reach a const containing scope. Private values will
220 // be included. The resulting closure will reference the const containing
221 // scope as its containing scope (since we assume the const scope won't
222 // change, we don't have to copy its values).
223 scoped_ptr<Scope> MakeClosure() const;
225 // Makes an empty scope with the given name. Returns NULL if the name is
226 // already set.
227 Scope* MakeTargetDefaults(const std::string& target_type);
229 // Gets the scope associated with the given target name, or null if it hasn't
230 // been set.
231 const Scope* GetTargetDefaults(const std::string& target_type) const;
233 // Filter to apply when the sources variable is assigned. May return NULL.
234 const PatternList* GetSourcesAssignmentFilter() const;
235 void set_sources_assignment_filter(
236 scoped_ptr<PatternList> f) {
237 sources_assignment_filter_ = f.Pass();
240 // Indicates if we're currently processing the build configuration file.
241 // This is true when processing the config file for any toolchain.
243 // To set or clear the flag, it must currently be in the opposite state in
244 // the current scope. Note that querying the state of the flag recursively
245 // checks all containing scopes until it reaches the top or finds the flag
246 // set.
247 void SetProcessingBuildConfig();
248 void ClearProcessingBuildConfig();
249 bool IsProcessingBuildConfig() const;
251 // Indicates if we're currently processing an import file.
253 // See SetProcessingBaseConfig for how flags work.
254 void SetProcessingImport();
255 void ClearProcessingImport();
256 bool IsProcessingImport() const;
258 // The source directory associated with this scope. This will check embedded
259 // scopes until it finds a nonempty source directory. This will default to
260 // an empty dir if no containing scope has a source dir set.
261 const SourceDir& GetSourceDir() const;
262 void set_source_dir(const SourceDir& d) { source_dir_ = d; }
264 // The item collector is where Items (Targets, Configs, etc.) go that have
265 // been defined. If a scope can generate items, this non-owning pointer will
266 // point to the storage for such items. The creator of this scope will be
267 // responsible for setting up the collector and then dealing with the
268 // collected items once execution of the context is complete.
270 // The items in a scope are collected as we go and then dispatched at the end
271 // of execution of a scope so that we can query the previously-generated
272 // targets (like getting the outputs).
274 // This can be null if the current scope can not generate items (like for
275 // imports and such).
277 // When retrieving the collector, the non-const scopes are recursively
278 // queried. The collector is not copied for closures, etc.
279 void set_item_collector(ItemVector* collector) {
280 item_collector_ = collector;
282 ItemVector* GetItemCollector();
284 // Properties are opaque pointers that code can use to set state on a Scope
285 // that it can retrieve later.
287 // The key should be a pointer to some use-case-specific object (to avoid
288 // collisions, otherwise it doesn't matter). Memory management is up to the
289 // setter. Setting the value to NULL will delete the property.
291 // Getting a property recursively searches all scopes, and the optional
292 // |found_on_scope| variable will be filled with the actual scope containing
293 // the key (if the pointer is non-NULL).
294 void SetProperty(const void* key, void* value);
295 void* GetProperty(const void* key, const Scope** found_on_scope) const;
297 private:
298 friend class ProgrammaticProvider;
300 struct Record {
301 Record() : used(false) {}
302 Record(const Value& v) : used(false), value(v) {}
304 bool used; // Set to true when the variable is used.
305 Value value;
308 void AddProvider(ProgrammaticProvider* p);
309 void RemoveProvider(ProgrammaticProvider* p);
311 // Scopes can have no containing scope (both null), a mutable containing
312 // scope, or a const containing scope. The reason is that when we're doing
313 // a new target, we want to refer to the base_config scope which will be read
314 // by multiple threads at the same time, so we REALLY want it to be const.
315 // When you jsut do a nested {}, however, we sometimes want to be able to
316 // change things (especially marking unused vars).
317 const Scope* const_containing_;
318 Scope* mutable_containing_;
320 const Settings* settings_;
322 // Bits set for different modes. See the flag definitions in the .cc file
323 // for more.
324 unsigned mode_flags_;
326 typedef base::hash_map<base::StringPiece, Record> RecordMap;
327 RecordMap values_;
329 // Owning pointers. Note that this can't use string pieces since the names
330 // are constructed from Values which might be deallocated before this goes
331 // out of scope.
332 typedef base::hash_map<std::string, Scope*> NamedScopeMap;
333 NamedScopeMap target_defaults_;
335 // Null indicates not set and that we should fallback to the containing
336 // scope's filter.
337 scoped_ptr<PatternList> sources_assignment_filter_;
339 // Owning pointers, must be deleted.
340 typedef std::map<std::string, scoped_refptr<const Template> > TemplateMap;
341 TemplateMap templates_;
343 ItemVector* item_collector_;
345 // Opaque pointers. See SetProperty() above.
346 typedef std::map<const void*, void*> PropertyMap;
347 PropertyMap properties_;
349 typedef std::set<ProgrammaticProvider*> ProviderSet;
350 ProviderSet programmatic_providers_;
352 SourceDir source_dir_;
354 DISALLOW_COPY_AND_ASSIGN(Scope);
357 #endif // TOOLS_GN_SCOPE_H_