Chromium Code Reviews| 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 |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 58 | 58 |
| 59 // Returns a non-null value if the given value can be programmatically | 59 // Returns a non-null value if the given value can be programmatically |
| 60 // generated, or NULL if there is none. | 60 // generated, or NULL if there is none. |
| 61 virtual const Value* GetProgrammaticValue( | 61 virtual const Value* GetProgrammaticValue( |
| 62 const base::StringPiece& ident) = 0; | 62 const base::StringPiece& ident) = 0; |
| 63 | 63 |
| 64 protected: | 64 protected: |
| 65 Scope* scope_; | 65 Scope* scope_; |
| 66 }; | 66 }; |
| 67 | 67 |
| 68 // Options for configuring scope merges. | |
| 69 struct MergeOptions { | |
| 70 // Defaults are the things least likely to cause errors. | |
| 71 MergeOptions() | |
| 72 : clobber_existing(false), | |
| 73 copy_private_vars(true), | |
|
cjhopman
2014/05/14 16:42:43
nit: how about clear_private_vars so that the defa
brettw
2014/05/15 19:44:15
Done.
| |
| 74 mark_used(false) { | |
| 75 } | |
| 76 | |
| 77 // When set, all existing avlues in the destination scope will be | |
| 78 // overwritten. | |
| 79 // | |
| 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; | |
| 86 | |
| 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 copy_private_vars; | |
| 91 | |
| 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; | |
| 97 }; | |
| 98 | |
| 68 // Creates an empty toplevel scope. | 99 // Creates an empty toplevel scope. |
| 69 Scope(const Settings* settings); | 100 Scope(const Settings* settings); |
| 70 | 101 |
| 71 // Creates a dependent scope. | 102 // Creates a dependent scope. |
| 72 Scope(Scope* parent); | 103 Scope(Scope* parent); |
| 73 Scope(const Scope* parent); | 104 Scope(const Scope* parent); |
| 74 | 105 |
| 75 ~Scope(); | 106 ~Scope(); |
| 76 | 107 |
| 77 const Settings* settings() const { return settings_; } | 108 const Settings* settings() const { return settings_; } |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 127 Value* GetValueForcedToCurrentScope(const base::StringPiece& ident, | 158 Value* GetValueForcedToCurrentScope(const base::StringPiece& ident, |
| 128 const ParseNode* set_node); | 159 const ParseNode* set_node); |
| 129 | 160 |
| 130 // The set_node indicates the statement that caused the set, for displaying | 161 // The set_node indicates the statement that caused the set, for displaying |
| 131 // errors later. Returns a pointer to the value in the current scope (a copy | 162 // errors later. Returns a pointer to the value in the current scope (a copy |
| 132 // is made for storage). | 163 // is made for storage). |
| 133 Value* SetValue(const base::StringPiece& ident, | 164 Value* SetValue(const base::StringPiece& ident, |
| 134 const Value& v, | 165 const Value& v, |
| 135 const ParseNode* set_node); | 166 const ParseNode* set_node); |
| 136 | 167 |
| 168 // Deletes the given named value from the current scope if it exists. | |
| 169 void DeleteValue(const base::StringPiece& ident); | |
| 170 | |
| 137 // Templates associated with this scope. A template can only be set once, so | 171 // Templates associated with this scope. A template can only be set once, so |
| 138 // AddTemplate will fail and return false if a rule with that name already | 172 // AddTemplate will fail and return false if a rule with that name already |
| 139 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will | 173 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will |
| 140 // check all containing scoped rescursively. | 174 // check all containing scoped rescursively. |
| 141 bool AddTemplate(const std::string& name, const Template* templ); | 175 bool AddTemplate(const std::string& name, const Template* templ); |
| 142 const Template* GetTemplate(const std::string& name) const; | 176 const Template* GetTemplate(const std::string& name) const; |
| 143 | 177 |
| 144 // Marks the given identifier as (un)used in the current scope. | 178 // Marks the given identifier as (un)used in the current scope. |
| 145 void MarkUsed(const base::StringPiece& ident); | 179 void MarkUsed(const base::StringPiece& ident); |
| 146 void MarkUnused(const base::StringPiece& ident); | 180 void MarkUnused(const base::StringPiece& ident); |
| 147 | 181 |
| 148 // Checks to see if the scope has a var set that hasn't been used. This is | 182 // Checks to see if the scope has a var set that hasn't been used. This is |
| 149 // called before replacing the var with a different one. It does not check | 183 // called before replacing the var with a different one. It does not check |
| 150 // containing scopes. | 184 // containing scopes. |
| 151 // | 185 // |
| 152 // If the identifier is present but hasnn't been used, return true. | 186 // If the identifier is present but hasnn't been used, return true. |
| 153 bool IsSetButUnused(const base::StringPiece& ident) const; | 187 bool IsSetButUnused(const base::StringPiece& ident) const; |
| 154 | 188 |
| 155 // Checks the scope to see if any values were set but not used, and fills in | 189 // Checks the scope to see if any values were set but not used, and fills in |
| 156 // the error and returns false if they were. | 190 // the error and returns false if they were. |
| 157 bool CheckForUnusedVars(Err* err) const; | 191 bool CheckForUnusedVars(Err* err) const; |
| 158 | 192 |
| 159 // Returns all values set in the current scope, without going to the parent | 193 // Returns all values set in the current scope, without going to the parent |
| 160 // scopes. | 194 // scopes. |
| 161 void GetCurrentScopeValues(KeyValueMap* output) const; | 195 void GetCurrentScopeValues(KeyValueMap* output) const; |
| 196 void GetCurrentScopeVariables(std::vector<base::StringPiece>* output) const; | |
| 162 | 197 |
| 163 // Copies this scope's values into the destination. Values from the | 198 // Copies this scope's values into the destination. Values from the |
| 164 // containing scope(s) (normally shadowed into the current one) will not be | 199 // containing scope(s) (normally shadowed into the current one) will not be |
| 165 // copied, neither will the reference to the containing scope (this is why | 200 // copied, neither will the reference to the containing scope (this is why |
| 166 // it's "non-recursive"). | 201 // it's "non-recursive"). |
| 167 // | 202 // |
| 168 // If clobber_existing is true, any existing values will be overwritten. In | |
| 169 // this mode, this function will never fail. | |
| 170 // | |
| 171 // If clobber_existing is false, it will be an error to merge a variable into | |
| 172 // a scope that already has something with that name in scope (meaning in | |
| 173 // that scope or in any of its containing scopes). If this happens, the error | |
| 174 // will be set and the function will return false. | |
| 175 // | |
| 176 // This is used in different contexts. When generating the error, the given | 203 // This is used in different contexts. When generating the error, the given |
| 177 // parse node will be blamed, and the given desc will be used to describe | 204 // parse node will be blamed, and the given desc will be used to describe |
| 178 // the operation that doesn't support doing this. For example, desc_for_err | 205 // the operation that doesn't support doing this. For example, desc_for_err |
| 179 // would be "import" when doing an import, and the error string would say | 206 // would be "import" when doing an import, and the error string would say |
| 180 // something like "The import contains...". | 207 // something like "The import contains...". |
| 181 bool NonRecursiveMergeTo(Scope* dest, | 208 bool NonRecursiveMergeTo(Scope* dest, |
| 182 bool clobber_existing, | 209 const MergeOptions& options, |
| 183 const ParseNode* node_for_err, | 210 const ParseNode* node_for_err, |
| 184 const char* desc_for_err, | 211 const char* desc_for_err, |
| 185 Err* err) const; | 212 Err* err) const; |
| 186 | 213 |
| 187 // Constructs a scope that is a copy of the current one. Nested scopes will | 214 // Constructs a scope that is a copy of the current one. Nested scopes will |
| 188 // be collapsed until we reach a const containing scope. The resulting | 215 // be collapsed until we reach a const containing scope. Private values will |
| 189 // closure will reference the const containing scope as its containing scope | 216 // be included. The resulting closure will reference the const containing |
| 190 // (since we assume the const scope won't change, we don't have to copy its | 217 // scope as its containing scope (since we assume the const scope won't |
| 191 // values). | 218 // change, we don't have to copy its values). |
| 192 scoped_ptr<Scope> MakeClosure() const; | 219 scoped_ptr<Scope> MakeClosure() const; |
| 193 | 220 |
| 194 // Makes an empty scope with the given name. Returns NULL if the name is | 221 // Makes an empty scope with the given name. Returns NULL if the name is |
| 195 // already set. | 222 // already set. |
| 196 Scope* MakeTargetDefaults(const std::string& target_type); | 223 Scope* MakeTargetDefaults(const std::string& target_type); |
| 197 | 224 |
| 198 // Gets the scope associated with the given target name, or null if it hasn't | 225 // Gets the scope associated with the given target name, or null if it hasn't |
| 199 // been set. | 226 // been set. |
| 200 const Scope* GetTargetDefaults(const std::string& target_type) const; | 227 const Scope* GetTargetDefaults(const std::string& target_type) const; |
| 201 | 228 |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 256 // The key should be a pointer to some use-case-specific object (to avoid | 283 // The key should be a pointer to some use-case-specific object (to avoid |
| 257 // collisions, otherwise it doesn't matter). Memory management is up to the | 284 // collisions, otherwise it doesn't matter). Memory management is up to the |
| 258 // setter. Setting the value to NULL will delete the property. | 285 // setter. Setting the value to NULL will delete the property. |
| 259 // | 286 // |
| 260 // Getting a property recursively searches all scopes, and the optional | 287 // Getting a property recursively searches all scopes, and the optional |
| 261 // |found_on_scope| variable will be filled with the actual scope containing | 288 // |found_on_scope| variable will be filled with the actual scope containing |
| 262 // the key (if the pointer is non-NULL). | 289 // the key (if the pointer is non-NULL). |
| 263 void SetProperty(const void* key, void* value); | 290 void SetProperty(const void* key, void* value); |
| 264 void* GetProperty(const void* key, const Scope** found_on_scope) const; | 291 void* GetProperty(const void* key, const Scope** found_on_scope) const; |
| 265 | 292 |
| 293 // Returns true if this variable name should be considered private. Private | |
| 294 // values start with an underscore, and are not imported from "gni" files | |
| 295 // when processing an import. | |
| 296 static bool IsPrivateVar(const base::StringPiece& name); | |
| 297 | |
| 266 private: | 298 private: |
| 267 friend class ProgrammaticProvider; | 299 friend class ProgrammaticProvider; |
| 268 | 300 |
| 269 struct Record { | 301 struct Record { |
| 270 Record() : used(false) {} | 302 Record() : used(false) {} |
| 271 Record(const Value& v) : used(false), value(v) {} | 303 Record(const Value& v) : used(false), value(v) {} |
| 272 | 304 |
| 273 bool used; // Set to true when the variable is used. | 305 bool used; // Set to true when the variable is used. |
| 274 Value value; | 306 Value value; |
| 275 }; | 307 }; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 317 | 349 |
| 318 typedef std::set<ProgrammaticProvider*> ProviderSet; | 350 typedef std::set<ProgrammaticProvider*> ProviderSet; |
| 319 ProviderSet programmatic_providers_; | 351 ProviderSet programmatic_providers_; |
| 320 | 352 |
| 321 SourceDir source_dir_; | 353 SourceDir source_dir_; |
| 322 | 354 |
| 323 DISALLOW_COPY_AND_ASSIGN(Scope); | 355 DISALLOW_COPY_AND_ASSIGN(Scope); |
| 324 }; | 356 }; |
| 325 | 357 |
| 326 #endif // TOOLS_GN_SCOPE_H_ | 358 #endif // TOOLS_GN_SCOPE_H_ |
| OLD | NEW |