| 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 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) { |
| 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 skip_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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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 |
| 137 // Removes the value with the given identifier if it exists on the current | 168 // Removes the value with the given identifier if it exists on the current |
| 138 // scope. This does not search recursive scopes. Does nothing if not found. | 169 // scope. This does not search recursive scopes. Does nothing if not found. |
| 139 void RemoveIdentifier(const base::StringPiece& ident); | 170 void RemoveIdentifier(const base::StringPiece& ident); |
| 140 | 171 |
| 172 // Removes from this scope all identifiers and templates that are considered |
| 173 // private. |
| 174 void RemovePrivateIdentifiers(); |
| 175 |
| 141 // Templates associated with this scope. A template can only be set once, so | 176 // Templates associated with this scope. A template can only be set once, so |
| 142 // AddTemplate will fail and return false if a rule with that name already | 177 // AddTemplate will fail and return false if a rule with that name already |
| 143 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will | 178 // exists. GetTemplate returns NULL if the rule doesn't exist, and it will |
| 144 // check all containing scoped rescursively. | 179 // check all containing scoped rescursively. |
| 145 bool AddTemplate(const std::string& name, const Template* templ); | 180 bool AddTemplate(const std::string& name, const Template* templ); |
| 146 const Template* GetTemplate(const std::string& name) const; | 181 const Template* GetTemplate(const std::string& name) const; |
| 147 | 182 |
| 148 // Marks the given identifier as (un)used in the current scope. | 183 // Marks the given identifier as (un)used in the current scope. |
| 149 void MarkUsed(const base::StringPiece& ident); | 184 void MarkUsed(const base::StringPiece& ident); |
| 150 void MarkUnused(const base::StringPiece& ident); | 185 void MarkUnused(const base::StringPiece& ident); |
| (...skipping 11 matching lines...) Expand all Loading... |
| 162 | 197 |
| 163 // Returns all values set in the current scope, without going to the parent | 198 // Returns all values set in the current scope, without going to the parent |
| 164 // scopes. | 199 // scopes. |
| 165 void GetCurrentScopeValues(KeyValueMap* output) const; | 200 void GetCurrentScopeValues(KeyValueMap* output) const; |
| 166 | 201 |
| 167 // Copies this scope's values into the destination. Values from the | 202 // Copies this scope's values into the destination. Values from the |
| 168 // containing scope(s) (normally shadowed into the current one) will not be | 203 // containing scope(s) (normally shadowed into the current one) will not be |
| 169 // copied, neither will the reference to the containing scope (this is why | 204 // copied, neither will the reference to the containing scope (this is why |
| 170 // it's "non-recursive"). | 205 // it's "non-recursive"). |
| 171 // | 206 // |
| 172 // If clobber_existing is true, any existing values will be overwritten. In | |
| 173 // this mode, this function will never fail. | |
| 174 // | |
| 175 // If clobber_existing is false, it will be an error to merge a variable into | |
| 176 // a scope that already has something with that name in scope (meaning in | |
| 177 // that scope or in any of its containing scopes). If this happens, the error | |
| 178 // will be set and the function will return false. | |
| 179 // | |
| 180 // This is used in different contexts. When generating the error, the given | 207 // This is used in different contexts. When generating the error, the given |
| 181 // parse node will be blamed, and the given desc will be used to describe | 208 // parse node will be blamed, and the given desc will be used to describe |
| 182 // the operation that doesn't support doing this. For example, desc_for_err | 209 // the operation that doesn't support doing this. For example, desc_for_err |
| 183 // would be "import" when doing an import, and the error string would say | 210 // would be "import" when doing an import, and the error string would say |
| 184 // something like "The import contains...". | 211 // something like "The import contains...". |
| 185 bool NonRecursiveMergeTo(Scope* dest, | 212 bool NonRecursiveMergeTo(Scope* dest, |
| 186 bool clobber_existing, | 213 const MergeOptions& options, |
| 187 const ParseNode* node_for_err, | 214 const ParseNode* node_for_err, |
| 188 const char* desc_for_err, | 215 const char* desc_for_err, |
| 189 Err* err) const; | 216 Err* err) const; |
| 190 | 217 |
| 191 // Constructs a scope that is a copy of the current one. Nested scopes will | 218 // Constructs a scope that is a copy of the current one. Nested scopes will |
| 192 // be collapsed until we reach a const containing scope. The resulting | 219 // be collapsed until we reach a const containing scope. Private values will |
| 193 // closure will reference the const containing scope as its containing scope | 220 // be included. The resulting closure will reference the const containing |
| 194 // (since we assume the const scope won't change, we don't have to copy its | 221 // scope as its containing scope (since we assume the const scope won't |
| 195 // values). | 222 // change, we don't have to copy its values). |
| 196 scoped_ptr<Scope> MakeClosure() const; | 223 scoped_ptr<Scope> MakeClosure() const; |
| 197 | 224 |
| 198 // Makes an empty scope with the given name. Returns NULL if the name is | 225 // Makes an empty scope with the given name. Returns NULL if the name is |
| 199 // already set. | 226 // already set. |
| 200 Scope* MakeTargetDefaults(const std::string& target_type); | 227 Scope* MakeTargetDefaults(const std::string& target_type); |
| 201 | 228 |
| 202 // Gets the scope associated with the given target name, or null if it hasn't | 229 // Gets the scope associated with the given target name, or null if it hasn't |
| 203 // been set. | 230 // been set. |
| 204 const Scope* GetTargetDefaults(const std::string& target_type) const; | 231 const Scope* GetTargetDefaults(const std::string& target_type) const; |
| 205 | 232 |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 321 | 348 |
| 322 typedef std::set<ProgrammaticProvider*> ProviderSet; | 349 typedef std::set<ProgrammaticProvider*> ProviderSet; |
| 323 ProviderSet programmatic_providers_; | 350 ProviderSet programmatic_providers_; |
| 324 | 351 |
| 325 SourceDir source_dir_; | 352 SourceDir source_dir_; |
| 326 | 353 |
| 327 DISALLOW_COPY_AND_ASSIGN(Scope); | 354 DISALLOW_COPY_AND_ASSIGN(Scope); |
| 328 }; | 355 }; |
| 329 | 356 |
| 330 #endif // TOOLS_GN_SCOPE_H_ | 357 #endif // TOOLS_GN_SCOPE_H_ |
| OLD | NEW |