From 65be7de7aca4bdac05e0acf68ec86a351e779839 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Wed, 18 Mar 2020 07:19:41 +0200 Subject: Add $visibility() function for querying variable visibility --- libbuild2/functions-builtin.cxx | 15 +++++++++++++++ libbuild2/variable.cxx | 18 +++++++++--------- libbuild2/variable.hxx | 10 ++++++++-- 3 files changed, 32 insertions(+), 11 deletions(-) diff --git a/libbuild2/functions-builtin.cxx b/libbuild2/functions-builtin.cxx index c4f0314..4e28741 100644 --- a/libbuild2/functions-builtin.cxx +++ b/libbuild2/functions-builtin.cxx @@ -27,6 +27,21 @@ namespace build2 return (*s)[convert (move (name))].defined (); }; + // Return variable visibility if it exists and NULL otherwise. + // + f["visibility"] = [](const scope* s, names name) + { + if (s == nullptr) + fail << "visibility() called out of scope" << endf; + + const variable* var ( + s->ctx.var_pool.find (convert (move (name)))); + + return (var != nullptr + ? optional (to_string (var->visibility)) + : nullopt); + }; + f["type"] = [](value* v) {return v->type != nullptr ? v->type->name : "";}; f["null"] = [](value* v) {return v->null;}; f["empty"] = [](value* v) {return v->null || v->empty ();}; diff --git a/libbuild2/variable.cxx b/libbuild2/variable.cxx index 0460344..7b55357 100644 --- a/libbuild2/variable.cxx +++ b/libbuild2/variable.cxx @@ -15,21 +15,21 @@ namespace build2 { // variable_visibility // - ostream& - operator<< (ostream& o, variable_visibility v) + string + to_string (variable_visibility v) { - const char* s (nullptr); + string r; switch (v) { - case variable_visibility::normal: s = "normal"; break; - case variable_visibility::project: s = "project"; break; - case variable_visibility::scope: s = "scope"; break; - case variable_visibility::target: s = "target"; break; - case variable_visibility::prereq: s = "prerequisite"; break; + case variable_visibility::normal: r = "normal"; break; + case variable_visibility::project: r = "project"; break; + case variable_visibility::scope: r = "scope"; break; + case variable_visibility::target: r = "target"; break; + case variable_visibility::prereq: r = "prerequisite"; break; } - return o << s; + return r; } // value diff --git a/libbuild2/variable.hxx b/libbuild2/variable.hxx index 071cb5f..b293215 100644 --- a/libbuild2/variable.hxx +++ b/libbuild2/variable.hxx @@ -136,8 +136,14 @@ namespace build2 } #endif - LIBBUILD2_SYMEXPORT ostream& - operator<< (ostream&, variable_visibility); + LIBBUILD2_SYMEXPORT string + to_string (variable_visibility); + + inline ostream& + operator<< (ostream& o, variable_visibility v) + { + return o << to_string (v); + } // variable // -- cgit v1.1