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