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