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 <memory> | 9 #include <memory> |
10 #include <set> | 10 #include <set> |
(...skipping 27 matching lines...) Expand all Loading... |
38 // many invocations. A const containing scope, however, prevents us from | 38 // many invocations. A const containing scope, however, prevents us from |
39 // marking variables "used" which prevents us from issuing errors on unused | 39 // marking variables "used" which prevents us from issuing errors on unused |
40 // variables. So you should use a non-const containing scope whenever possible. | 40 // variables. So you should use a non-const containing scope whenever possible. |
41 class Scope { | 41 class Scope { |
42 public: | 42 public: |
43 typedef base::hash_map<base::StringPiece, Value, base::StringPieceHash> | 43 typedef base::hash_map<base::StringPiece, Value, base::StringPieceHash> |
44 KeyValueMap; | 44 KeyValueMap; |
45 // Holds an owning list of Items. | 45 // Holds an owning list of Items. |
46 typedef ScopedVector<Item> ItemVector; | 46 typedef ScopedVector<Item> ItemVector; |
47 | 47 |
| 48 // A flag to indicate whether a function should recurse into nested scopes, |
| 49 // or only operate on the current scope. |
| 50 enum SearchNested { |
| 51 SEARCH_NESTED, |
| 52 SEARCH_CURRENT |
| 53 }; |
| 54 |
48 // Allows code to provide values for built-in variables. This class will | 55 // Allows code to provide values for built-in variables. This class will |
49 // automatically register itself on construction and deregister itself on | 56 // automatically register itself on construction and deregister itself on |
50 // destruction. | 57 // destruction. |
51 class ProgrammaticProvider { | 58 class ProgrammaticProvider { |
52 public: | 59 public: |
53 explicit ProgrammaticProvider(Scope* scope) : scope_(scope) { | 60 explicit ProgrammaticProvider(Scope* scope) : scope_(scope) { |
54 scope_->AddProvider(this); | 61 scope_->AddProvider(this); |
55 } | 62 } |
56 virtual ~ProgrammaticProvider(); | 63 virtual ~ProgrammaticProvider(); |
57 | 64 |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 | 114 |
108 // See the const_/mutable_containing_ var declaraions below. Yes, it's a | 115 // See the const_/mutable_containing_ var declaraions below. Yes, it's a |
109 // bit weird that we can have a const pointer to the "mutable" one. | 116 // bit weird that we can have a const pointer to the "mutable" one. |
110 Scope* mutable_containing() { return mutable_containing_; } | 117 Scope* mutable_containing() { return mutable_containing_; } |
111 const Scope* mutable_containing() const { return mutable_containing_; } | 118 const Scope* mutable_containing() const { return mutable_containing_; } |
112 const Scope* const_containing() const { return const_containing_; } | 119 const Scope* const_containing() const { return const_containing_; } |
113 const Scope* containing() const { | 120 const Scope* containing() const { |
114 return mutable_containing_ ? mutable_containing_ : const_containing_; | 121 return mutable_containing_ ? mutable_containing_ : const_containing_; |
115 } | 122 } |
116 | 123 |
| 124 // Clears any references to containing scopes. This scope will now be |
| 125 // self-sufficient. |
| 126 void DetachFromContaining(); |
| 127 |
117 // Returns NULL if there's no such value. | 128 // Returns NULL if there's no such value. |
118 // | 129 // |
119 // counts_as_used should be set if the variable is being read in a way that | 130 // counts_as_used should be set if the variable is being read in a way that |
120 // should count for unused variable checking. | 131 // should count for unused variable checking. |
121 const Value* GetValue(const base::StringPiece& ident, | 132 const Value* GetValue(const base::StringPiece& ident, |
122 bool counts_as_used); | 133 bool counts_as_used); |
123 const Value* GetValue(const base::StringPiece& ident) const; | 134 const Value* GetValue(const base::StringPiece& ident) const; |
124 | 135 |
| 136 // If the value exists in the current scope, destrictively moves it into the |
| 137 // return value. If it exists in a containing scope, copies it. |
| 138 // |
| 139 // This is for implementing modify-write operations where we want to read |
| 140 // the existing value and plan to immediately overwrite it. If the value is |
| 141 // in a containing scope, we never want to touch it (all writes go to the |
| 142 // current scope), but if it's in the current scope, avoid the copy since it |
| 143 // will be overwritten anyway. |
| 144 //Value DestructiveMoveOut(const base::StringPiece& ident); |
| 145 |
125 // Returns the requested value as a mutable one if possible. If the value | 146 // Returns the requested value as a mutable one if possible. If the value |
126 // is not found in a mutable scope, then returns null. Note that the value | 147 // is not found in a mutable scope, then returns null. Note that the value |
127 // could still exist in a const scope, so GetValue() could still return | 148 // could still exist in a const scope, so GetValue() could still return |
128 // non-null in this case. | 149 // non-null in this case. |
129 // | 150 // |
130 // Say you have a local scope that then refers to the const root scope from | 151 // Say you have a local scope that then refers to the const root scope from |
131 // the master build config. You can't change the values from the master | 152 // the master build config. You can't change the values from the master |
132 // build config (it's read-only so it can be read from multiple threads | 153 // build config (it's read-only so it can be read from multiple threads |
133 // without locking). Read-only operations would work on values from the root | 154 // without locking). Read-only operations would work on values from the root |
134 // scope, but write operations would only work on values in the derived | 155 // scope, but write operations would only work on values in the derived |
135 // scope(s). | 156 // scope(s). |
136 // | 157 // |
137 // Be careful when calling this. It's not normally correct to modify values, | 158 // Be careful when calling this. It's not normally correct to modify values, |
138 // but you should instead do a new Set each time. | 159 // but you should instead do a new Set each time. |
139 // | 160 // |
140 // Consider this code: | 161 // Consider this code: |
141 // a = 5 | 162 // a = 5 |
142 // { | 163 // { |
143 // a = 6 | 164 // a = 6 |
144 // } | 165 // } |
145 // The 6 should get set on the nested scope rather than modify the value | 166 // The 6 should get set on the nested scope rather than modify the value |
146 // in the outer one. | 167 // in the outer one. |
147 Value* GetMutableValue(const base::StringPiece& ident, bool counts_as_used); | 168 Value* GetMutableValue(const base::StringPiece& ident, |
148 | 169 SearchNested search_mode, |
149 // Same as GetValue, but if the value exists in a parent scope, we'll copy | 170 bool counts_as_used); |
150 // it to the current scope. If the return value is non-null, the value is | |
151 // guaranteed to be set in the current scope. Generatlly this will be used | |
152 // if the calling code is planning on modifying the value in-place. | |
153 // | |
154 // Since this is used when doing read-modifies, we never count this access | |
155 // as reading the variable, since we assume it will be written to. | |
156 Value* GetValueForcedToCurrentScope(const base::StringPiece& ident, | |
157 const ParseNode* set_node); | |
158 | 171 |
159 // Returns the StringPiece used to identify the value. This string piece | 172 // Returns the StringPiece used to identify the value. This string piece |
160 // will have the same contents as "ident" passed in, but may point to a | 173 // will have the same contents as "ident" passed in, but may point to a |
161 // different underlying buffer. This is useful because this StringPiece is | 174 // different underlying buffer. This is useful because this StringPiece is |
162 // static and won't be deleted for the life of the program, so it can be used | 175 // static and won't be deleted for the life of the program, so it can be used |
163 // as keys in places that may outlive a temporary. It will return an empty | 176 // as keys in places that may outlive a temporary. It will return an empty |
164 // string for programmatic and nonexistant values. | 177 // string for programmatic and nonexistant values. |
165 base::StringPiece GetStorageKey(const base::StringPiece& ident) const; | 178 base::StringPiece GetStorageKey(const base::StringPiece& ident) const; |
166 | 179 |
167 // The set_node indicates the statement that caused the set, for displaying | 180 // The set_node indicates the statement that caused the set, for displaying |
168 // errors later. Returns a pointer to the value in the current scope (a copy | 181 // errors later. Returns a pointer to the value in the current scope (a copy |
169 // is made for storage). | 182 // is made for storage). |
170 Value* SetValue(const base::StringPiece& ident, | 183 Value* SetValue(const base::StringPiece& ident, |
171 const Value& v, | 184 Value v, |
172 const ParseNode* set_node); | 185 const ParseNode* set_node); |
173 | 186 |
174 // Removes the value with the given identifier if it exists on the current | 187 // Removes the value with the given identifier if it exists on the current |
175 // scope. This does not search recursive scopes. Does nothing if not found. | 188 // scope. This does not search recursive scopes. Does nothing if not found. |
176 void RemoveIdentifier(const base::StringPiece& ident); | 189 void RemoveIdentifier(const base::StringPiece& ident); |
177 | 190 |
178 // Removes from this scope all identifiers and templates that are considered | 191 // Removes from this scope all identifiers and templates that are considered |
179 // private. | 192 // private. |
180 void RemovePrivateIdentifiers(); | 193 void RemovePrivateIdentifiers(); |
181 | 194 |
(...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
358 | 371 |
359 typedef std::set<ProgrammaticProvider*> ProviderSet; | 372 typedef std::set<ProgrammaticProvider*> ProviderSet; |
360 ProviderSet programmatic_providers_; | 373 ProviderSet programmatic_providers_; |
361 | 374 |
362 SourceDir source_dir_; | 375 SourceDir source_dir_; |
363 | 376 |
364 DISALLOW_COPY_AND_ASSIGN(Scope); | 377 DISALLOW_COPY_AND_ASSIGN(Scope); |
365 }; | 378 }; |
366 | 379 |
367 #endif // TOOLS_GN_SCOPE_H_ | 380 #endif // TOOLS_GN_SCOPE_H_ |
OLD | NEW |