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. 4 5 #ifndef TOOLS_GN_IMPORT_MANAGER_H_ 6 #define TOOLS_GN_IMPORT_MANAGER_H_ 7 8 #include <map> 9 #include <memory> 10 #include <mutex> 11 #include <string> 12 #include <unordered_set> 13 #include <vector> 14 15 #include "base/macros.h" 16 17 class Err; 18 class ParseNode; 19 class Scope; 20 class SourceFile; 21 22 // Provides a cache of the results of importing scopes so the results can 23 // be re-used rather than running the imported files multiple times. 24 class ImportManager { 25 public: 26 ImportManager(); 27 ~ImportManager(); 28 29 // Does an import of the given file into the given scope. On error, sets the 30 // error and returns false. 31 bool DoImport(const SourceFile& file, 32 const ParseNode* node_for_err, 33 Scope* scope, 34 Err* err); 35 36 std::vector<SourceFile> GetImportedFiles() const; 37 38 private: 39 struct ImportInfo; 40 41 // Protects access to imports_ and imports_in_progress_. Do not hold when 42 // actually executing imports. 43 std::mutex imports_lock_; 44 45 // Owning pointers to the scopes. 46 using ImportMap = std::map<SourceFile, std::unique_ptr<ImportInfo>>; 47 ImportMap imports_; 48 49 std::unordered_set<std::string> imports_in_progress_; 50 51 DISALLOW_COPY_AND_ASSIGN(ImportManager); 52 }; 53 54 #endif // TOOLS_GN_IMPORT_MANAGER_H_ 55