// file : build2/scope -*- C++ -*- // copyright : Copyright (c) 2014-2016 Code Synthesis Ltd // license : MIT; see accompanying LICENSE file #ifndef BUILD2_SCOPE #define BUILD2_SCOPE #include #include #include #include #include #include #include #include #include #include #include namespace build2 { class scope { public: // Absolute and normalized. // const dir_path& out_path () const {return *out_path_;} const dir_path& src_path () const {return *src_path_;} // The first is a pointer to the key in scope_map. The second is a pointer // to the src_root/base variable value, if any (i.e., it can be NULL). // const dir_path* out_path_ = nullptr; const dir_path* src_path_ = nullptr; bool root () const {return root_ == this;} scope* parent_scope () const {return parent_;} // Root scope of this scope or NULL if this scope is not (yet) // in any (known) project. Note that if the scope itself is // root, then this function return this. To get to the outer // root, query the root scope of the parent. // scope* root_scope () const {return root_;} // Root scope of a strong amalgamation of this scope or NULL if // this scope is not (yet) in any (known) project. If there is // no strong amalgamation, then this function returns the root // scope of the project (in other words, in this case a project // is treated as its own strong amalgamation). // scope* strong_scope () const { return root_ != nullptr ? root_->strong_ != nullptr ? root_->strong_ : root_ : nullptr; } // Root scope of the outermost amalgamation or NULL if this scope is not // (yet) in any (known) project. If there is no amalgamation, then this // function returns the root scope of the project (in other words, in this // case a project is treated as its own amalgamation). // scope* weak_scope () const { scope* r (root_); if (r != nullptr) for (; r->parent_->root_ != nullptr; r = r->parent_->root_) ; return r; } // Variables. // public: variable_map vars; // Lookup, including in outer scopes. If you only want to lookup in this // scope, do it on the the variables map directly (and note that there // will be no overrides). // lookup operator[] (const variable& var) const { return find (var).first; } lookup operator[] (const string& name) const { return operator[] (var_pool[name]); } // As above, but include target type/pattern-specific variables. // lookup find (const variable& var, const target_key& tk) const { return find (var, tk.type, tk.name).first; } lookup find (const string& var, const target_key& tk) const { return find (var_pool[var], tk); } lookup find (const variable& var, const target_type& tt, const string& tn) const { return find (var, &tt, &tn).first; } lookup find (const string& var, const target_type& tt, const string& tn) const { return find (var_pool[var], tt, tn); } pair find (const variable& var, const target_type* tt = nullptr, const string* tn = nullptr) const { auto p (find_original (var, tt, tn)); return var.override == nullptr ? p : find_override (var, move (p)); } // Implementation details (used by scope target lookup). The start_depth // can be used to skip a number of initial lookups. // pair find_original ( const variable&, const target_type* tt = nullptr, const string* tn = nullptr, const target_type* gt = nullptr, const string* gn = nullptr, size_t start_depth = 1) const; pair find_override (const variable&, pair original, bool target = false) const; // Return a value suitable for assignment (or append if you only // want to append to the value from this scope). If the variable // does not exist in this scope's map, then a new one with the // NULL value is added and returned. Otherwise the existing value // is returned. // value& assign (const variable& var) {return vars.assign (var);} value& assign (const string& name) {return vars.assign (name);} // Unlike the two above, assign a typed non-overridable variable with // normal visibility. // template value& assign (string name) {return vars.assign (move (name));} // Return a value suitable for appending. If the variable does not // exist in this scope's map, then outer scopes are searched for // the same variable. If found then a new variable with the found // value is added to this scope and returned. Otherwise this // function proceeds as assign(). // value& append (const variable&); value& append (const string& name) {return append (var_pool[name]);} // Target type/pattern-specific variables. // variable_type_map target_vars; // Prerequisite cache. // public: prerequisite_set prerequisites; // Meta/operations supported by this project (set on the root // scope only). // build2::meta_operations meta_operations; build2::operations operations; typedef build2::path path_type; // Set of buildfiles already loaded for this scope. The included // buildfiles are checked against the project's root scope while // imported -- against the global scope (global_scope). // std::unordered_set buildfiles; // Target types. // public: target_type_map target_types; const target_type* find_target_type (const string&, const scope** = nullptr) const; // Given a name, figure out its type, taking into account extensions, // special names (e.g., '.' and '..'), or anything else that might be // relevant. Also process the name (in place) by extracting the // extension, adjusting dir/value, etc., (note that the dir is not // necessarily normalized). Return NULL if not found. // const target_type* find_target_type (name&, const string*& ext) const; // Dynamically derive a new target type from an existing one. Return the // reference to the target type and an indicator of whether it was // actually created. // pair, bool> derive_target_type (const string& name, const target_type& base); template pair, bool> derive_target_type (const string& name) { return derive_target_type (name, T::static_type); } // Rules. // public: rule_map rules; // Modules. // public: loaded_module_map modules; // Only on root scope. private: friend class scope_map; friend class temp_scope; // These two from set strong_. // friend void create_bootstrap_outer (scope&); friend scope& create_bootstrap_inner (scope&, const dir_path&); scope () = default; scope* parent_; scope* root_; scope* strong_ = nullptr; // Only set on root sopes. // NULL means no strong amalgamtion. }; // Temporary scope. The idea is to be able to create a temporary // scope in order not to change the variables in the current scope. // Such a scope is not entered in to the scope map. As a result it // can only be used as a temporary set of variables. In particular, // defining targets/prerequisites directly in such a scope will surely // end up badly. Defining any nested scopes will be as if defining // such a scope in the parent (since path() returns parent's path). // class temp_scope: public scope { public: temp_scope (scope& p) { out_path_ = p.out_path_; src_path_ = p.src_path_; parent_ = &p; root_ = p.root_; // No need to copy strong_ since we are never root scope. } }; // Note that the scope map is only for paths from the out tree. // using scope_map_base = butl::dir_path_map; class scope_map: public scope_map_base { public: // Note that we assume the first insertion into the map is always the // global scope. // iterator insert (const dir_path&, bool root); // Find the most qualified scope that encompasses this path. // scope& find (const dir_path&); scope& find (const path& p) { // Natural thing to do here would be to call find (p.directory ()). // However, there could be a situation where the passed path is a // directory (i.e., the calling code does not know what it is dealing // with), so let's use the whole path. // return find (path_cast (p)); } }; extern scope_map scopes; extern scope* global_scope; } #endif // BUILD2_SCOPE