[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / tools / gn / import_manager.cc
blob2d39846d981b89d99efa50eb8989cfd14b17f676
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 #include "tools/gn/import_manager.h"
7 #include "base/memory/scoped_ptr.h"
8 #include "base/stl_util.h"
9 #include "tools/gn/parse_tree.h"
10 #include "tools/gn/scheduler.h"
11 #include "tools/gn/scope_per_file_provider.h"
13 namespace {
15 // Returns a newly-allocated scope on success, null on failure.
16 Scope* UncachedImport(const Settings* settings,
17 const SourceFile& file,
18 const ParseNode* node_for_err,
19 Err* err) {
20 const ParseNode* node = g_scheduler->input_file_manager()->SyncLoadFile(
21 node_for_err->GetRange(), settings->build_settings(), file, err);
22 if (!node)
23 return nullptr;
25 scoped_ptr<Scope> scope(new Scope(settings->base_config()));
26 scope->set_source_dir(file.GetDir());
28 // Don't allow ScopePerFileProvider to provide target-related variables.
29 // These will be relative to the imported file, which is probably not what
30 // people mean when they use these.
31 ScopePerFileProvider per_file_provider(scope.get(), false);
33 scope->SetProcessingImport();
34 node->Execute(scope.get(), err);
35 if (err->has_error())
36 return nullptr;
37 scope->ClearProcessingImport();
39 return scope.release();
42 } // namesapce
44 ImportManager::ImportManager() {
47 ImportManager::~ImportManager() {
48 STLDeleteContainerPairSecondPointers(imports_.begin(), imports_.end());
51 bool ImportManager::DoImport(const SourceFile& file,
52 const ParseNode* node_for_err,
53 Scope* scope,
54 Err* err) {
55 // See if we have a cached import, but be careful to actually do the scope
56 // copying outside of the lock.
57 const Scope* imported_scope = nullptr;
59 base::AutoLock lock(lock_);
60 ImportMap::const_iterator found = imports_.find(file);
61 if (found != imports_.end())
62 imported_scope = found->second;
65 if (!imported_scope) {
66 // Do a new import of the file.
67 imported_scope = UncachedImport(scope->settings(), file,
68 node_for_err, err);
69 if (!imported_scope)
70 return false;
72 // We loaded the file outside the lock. This means that there could be a
73 // race and the file was already loaded on a background thread. Recover
74 // from this and use the existing one if that happens.
76 base::AutoLock lock(lock_);
77 ImportMap::const_iterator found = imports_.find(file);
78 if (found != imports_.end()) {
79 delete imported_scope;
80 imported_scope = found->second;
81 } else {
82 imports_[file] = imported_scope;
87 Scope::MergeOptions options;
88 options.skip_private_vars = true;
89 options.mark_dest_used = true; // Don't require all imported values be used.
90 return imported_scope->NonRecursiveMergeTo(scope, options, node_for_err,
91 "import", err);