2014-06-13 02:33:02 +00:00
|
|
|
/*
|
|
|
|
Copyright (c) 2014 Microsoft Corporation. All rights reserved.
|
|
|
|
Released under Apache 2.0 license as described in the file LICENSE.
|
|
|
|
|
|
|
|
Author: Leonardo de Moura
|
|
|
|
*/
|
|
|
|
#include <vector>
|
|
|
|
#include <memory>
|
2014-07-08 01:17:10 +00:00
|
|
|
#include <string>
|
2014-08-07 23:59:08 +00:00
|
|
|
#include "util/sstream.h"
|
2014-06-13 02:33:02 +00:00
|
|
|
#include "library/scoped_ext.h"
|
|
|
|
#include "library/kernel_bindings.h"
|
|
|
|
|
|
|
|
namespace lean {
|
2014-09-04 01:37:01 +00:00
|
|
|
typedef std::tuple<name, using_namespace_fn, export_namespace_fn, push_scope_fn, pop_scope_fn> entry;
|
2014-06-13 02:33:02 +00:00
|
|
|
typedef std::vector<entry> scoped_exts;
|
2014-09-23 00:30:29 +00:00
|
|
|
static scoped_exts * g_exts = nullptr;
|
|
|
|
static scoped_exts & get_exts() { return *g_exts; }
|
2014-06-13 02:33:02 +00:00
|
|
|
|
2014-09-04 01:37:01 +00:00
|
|
|
void register_scoped_ext(name const & c, using_namespace_fn use, export_namespace_fn ex, push_scope_fn push, pop_scope_fn pop) {
|
|
|
|
get_exts().emplace_back(c, use, ex, push, pop);
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct scope_mng_ext : public environment_extension {
|
2014-08-23 22:44:28 +00:00
|
|
|
name_set m_namespace_set; // all namespaces registered in the system
|
|
|
|
list<name> m_namespaces; // stack of namespaces/sections
|
|
|
|
list<name> m_headers; // namespace/section header
|
|
|
|
list<scope_kind> m_scope_kinds;
|
2014-06-13 02:33:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct scope_mng_ext_reg {
|
|
|
|
unsigned m_ext_id;
|
|
|
|
scope_mng_ext_reg() { m_ext_id = environment::register_extension(std::make_shared<scope_mng_ext>()); }
|
|
|
|
};
|
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
static scope_mng_ext_reg * g_ext = nullptr;
|
2014-06-13 02:33:02 +00:00
|
|
|
static scope_mng_ext const & get_extension(environment const & env) {
|
2014-09-23 00:30:29 +00:00
|
|
|
return static_cast<scope_mng_ext const &>(env.get_extension(g_ext->m_ext_id));
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
|
|
|
static environment update(environment const & env, scope_mng_ext const & ext) {
|
2014-09-23 00:30:29 +00:00
|
|
|
return env.update(g_ext->m_ext_id, std::make_shared<scope_mng_ext>(ext));
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
name const & get_namespace(environment const & env) {
|
|
|
|
scope_mng_ext const & ext = get_extension(env);
|
|
|
|
return !is_nil(ext.m_namespaces) ? head(ext.m_namespaces) : name::anonymous();
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:56:51 +00:00
|
|
|
list<name> const & get_namespaces(environment const & env) {
|
|
|
|
return get_extension(env).m_namespaces;
|
|
|
|
}
|
|
|
|
|
2014-10-12 00:35:59 +00:00
|
|
|
bool in_section(environment const & env) {
|
2015-04-22 18:32:02 +00:00
|
|
|
scope_mng_ext const & ext = get_extension(env);
|
|
|
|
return !is_nil(ext.m_scope_kinds) && head(ext.m_scope_kinds) == scope_kind::Section;
|
2014-10-12 00:35:59 +00:00
|
|
|
}
|
|
|
|
|
2015-02-11 18:12:28 +00:00
|
|
|
void get_metaclasses(buffer<name> & r) {
|
|
|
|
for (auto const & t : get_exts()) {
|
|
|
|
name const & n = std::get<0>(t);
|
|
|
|
if (std::find(r.begin(), r.end(), n) == r.end())
|
|
|
|
r.push_back(n);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-02-11 18:35:04 +00:00
|
|
|
bool is_metaclass(name const & n) {
|
|
|
|
for (auto const & t : get_exts()) {
|
|
|
|
if (std::get<0>(t) == n)
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:02:59 +00:00
|
|
|
environment using_namespace(environment const & env, io_state const & ios, name const & n, buffer<name> const & metaclasses) {
|
2014-06-13 02:33:02 +00:00
|
|
|
environment r = env;
|
|
|
|
for (auto const & t : get_exts()) {
|
2015-02-11 19:02:59 +00:00
|
|
|
if (metaclasses.empty() ||
|
|
|
|
std::find(metaclasses.begin(), metaclasses.end(), std::get<0>(t)) != metaclasses.end())
|
2014-06-13 02:33:02 +00:00
|
|
|
r = std::get<1>(t)(r, ios, n);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:02:59 +00:00
|
|
|
static environment using_namespace(environment const & env, io_state const & ios, name const & n, name const & metaclass) {
|
|
|
|
buffer<name> tmp;
|
|
|
|
if (!metaclass.is_anonymous())
|
|
|
|
tmp.push_back(metaclass);
|
|
|
|
return using_namespace(env, ios, n, tmp);
|
|
|
|
}
|
|
|
|
|
|
|
|
environment using_namespace(environment const & env, io_state const & ios, name const & n) {
|
|
|
|
buffer<name> metaclasses;
|
|
|
|
return using_namespace(env, ios, n, metaclasses);
|
|
|
|
}
|
|
|
|
|
|
|
|
environment export_namespace(environment const & env, io_state const & ios, name const & n, buffer<name> const & metaclasses) {
|
2014-09-04 01:37:01 +00:00
|
|
|
environment r = env;
|
|
|
|
for (auto const & t : get_exts()) {
|
2015-02-11 19:02:59 +00:00
|
|
|
if (metaclasses.empty() ||
|
|
|
|
std::find(metaclasses.begin(), metaclasses.end(), std::get<0>(t)) != metaclasses.end())
|
2014-09-04 01:37:01 +00:00
|
|
|
r = std::get<2>(t)(r, ios, n);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-02-11 19:02:59 +00:00
|
|
|
environment export_namespace(environment const & env, io_state const & ios, name const & n) {
|
|
|
|
buffer<name> metaclasses;
|
|
|
|
return export_namespace(env, ios, n, metaclasses);
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:17:10 +00:00
|
|
|
optional<name> to_valid_namespace_name(environment const & env, name const & n) {
|
|
|
|
scope_mng_ext const & ext = get_extension(env);
|
|
|
|
if (ext.m_namespace_set.contains(n))
|
|
|
|
return optional<name>(n);
|
|
|
|
for (auto const & ns : ext.m_namespaces) {
|
|
|
|
name r = ns + n;
|
|
|
|
if (ext.m_namespace_set.contains(r))
|
|
|
|
return optional<name>(r);
|
|
|
|
}
|
|
|
|
return optional<name>();
|
|
|
|
}
|
|
|
|
|
2014-09-23 00:30:29 +00:00
|
|
|
static std::string * g_new_namespace_key = nullptr;
|
2014-11-02 02:15:46 +00:00
|
|
|
|
|
|
|
environment add_namespace(environment const & env, name const & ns) {
|
|
|
|
scope_mng_ext ext = get_extension(env);
|
|
|
|
if (!ext.m_namespace_set.contains(ns)) {
|
|
|
|
ext.m_namespace_set.insert(ns);
|
|
|
|
environment r = update(env, ext);
|
|
|
|
return module::add(r, *g_new_namespace_key, [=](serializer & s) { s << ns; });
|
|
|
|
} else {
|
|
|
|
return env;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-23 22:44:28 +00:00
|
|
|
environment push_scope(environment const & env, io_state const & ios, scope_kind k, name const & n) {
|
2015-04-22 18:32:02 +00:00
|
|
|
if (k == scope_kind::Namespace && in_section(env))
|
|
|
|
throw exception("invalid namespace declaration, a namespace cannot be declared inside a section");
|
2014-09-08 02:55:19 +00:00
|
|
|
name new_n = get_namespace(env);
|
|
|
|
if (k == scope_kind::Namespace)
|
|
|
|
new_n = new_n + n;
|
2014-06-13 02:33:02 +00:00
|
|
|
scope_mng_ext ext = get_extension(env);
|
2014-07-08 01:17:10 +00:00
|
|
|
bool save_ns = false;
|
|
|
|
if (!ext.m_namespace_set.contains(new_n)) {
|
|
|
|
save_ns = true;
|
|
|
|
ext.m_namespace_set.insert(new_n);
|
|
|
|
}
|
2014-08-23 22:44:28 +00:00
|
|
|
ext.m_namespaces = cons(new_n, ext.m_namespaces);
|
|
|
|
ext.m_headers = cons(n, ext.m_headers);
|
|
|
|
ext.m_scope_kinds = cons(k, ext.m_scope_kinds);
|
2014-06-13 02:33:02 +00:00
|
|
|
environment r = update(env, ext);
|
2014-06-15 05:13:25 +00:00
|
|
|
for (auto const & t : get_exts()) {
|
2015-04-22 01:56:28 +00:00
|
|
|
r = std::get<3>(t)(r, ios, k);
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|
2014-08-23 22:44:28 +00:00
|
|
|
if (k == scope_kind::Namespace)
|
2014-11-02 23:35:49 +00:00
|
|
|
r = using_namespace(r, ios, new_n);
|
2014-07-08 01:17:10 +00:00
|
|
|
if (save_ns)
|
2014-09-23 00:30:29 +00:00
|
|
|
r = module::add(r, *g_new_namespace_key, [=](serializer & s) { s << new_n; });
|
2014-06-13 02:33:02 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2014-07-08 01:17:10 +00:00
|
|
|
static void namespace_reader(deserializer & d, module_idx, shared_environment &,
|
|
|
|
std::function<void(asynch_update_fn const &)> &,
|
|
|
|
std::function<void(delayed_update_fn const &)> & add_delayed_update) {
|
|
|
|
name n;
|
|
|
|
d >> n;
|
|
|
|
add_delayed_update([=](environment const & env, io_state const &) -> environment {
|
|
|
|
scope_mng_ext ext = get_extension(env);
|
|
|
|
ext.m_namespace_set.insert(n);
|
|
|
|
return update(env, ext);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2015-04-22 01:56:28 +00:00
|
|
|
environment pop_scope_core(environment const & env, io_state const & ios) {
|
2014-06-13 02:33:02 +00:00
|
|
|
scope_mng_ext ext = get_extension(env);
|
|
|
|
if (is_nil(ext.m_namespaces))
|
2014-12-09 18:14:14 +00:00
|
|
|
return env;
|
2014-08-23 22:44:28 +00:00
|
|
|
scope_kind k = head(ext.m_scope_kinds);
|
|
|
|
ext.m_namespaces = tail(ext.m_namespaces);
|
|
|
|
ext.m_headers = tail(ext.m_headers);
|
|
|
|
ext.m_scope_kinds = tail(ext.m_scope_kinds);
|
2014-06-13 02:33:02 +00:00
|
|
|
environment r = update(env, ext);
|
2014-06-15 05:13:25 +00:00
|
|
|
for (auto const & t : get_exts()) {
|
2015-04-22 01:56:28 +00:00
|
|
|
r = std::get<4>(t)(r, ios, k);
|
2014-06-15 05:13:25 +00:00
|
|
|
}
|
2014-06-13 02:33:02 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2015-04-22 01:56:28 +00:00
|
|
|
environment pop_scope(environment const & env, io_state const & ios, name const & n) {
|
2014-12-09 18:14:14 +00:00
|
|
|
scope_mng_ext ext = get_extension(env);
|
|
|
|
if (is_nil(ext.m_namespaces))
|
2015-04-22 18:32:02 +00:00
|
|
|
throw exception("invalid end of scope, there are no open namespaces/sections");
|
2014-12-09 18:14:14 +00:00
|
|
|
if (n != head(ext.m_headers))
|
|
|
|
throw exception(sstream() << "invalid end of scope, begin/end mistmatch, scope starts with '"
|
|
|
|
<< head(ext.m_headers) << "', and ends with '" << n << "'");
|
2015-04-22 01:56:28 +00:00
|
|
|
return pop_scope_core(env, ios);
|
2014-12-09 18:14:14 +00:00
|
|
|
}
|
|
|
|
|
2014-08-08 00:08:59 +00:00
|
|
|
bool has_open_scopes(environment const & env) {
|
|
|
|
scope_mng_ext ext = get_extension(env);
|
|
|
|
return !is_nil(ext.m_namespaces);
|
|
|
|
}
|
|
|
|
|
2014-06-13 02:33:02 +00:00
|
|
|
static int using_namespace_objects(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
environment const & env = to_environment(L, 1);
|
|
|
|
name n = to_name_ext(L, 2);
|
|
|
|
if (nargs == 2)
|
|
|
|
return push_environment(L, using_namespace(env, get_io_state(L), n));
|
|
|
|
else if (nargs == 3)
|
|
|
|
return push_environment(L, using_namespace(env, get_io_state(L), n, to_name_ext(L, 3)));
|
|
|
|
else
|
|
|
|
return push_environment(L, using_namespace(env, to_io_state(L, 4), n, to_name_ext(L, 3)));
|
|
|
|
}
|
|
|
|
|
|
|
|
static int push_scope(lua_State * L) {
|
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
if (nargs == 1)
|
2015-04-22 18:32:02 +00:00
|
|
|
return push_environment(L, push_scope(to_environment(L, 1), get_io_state(L), scope_kind::Section));
|
2014-06-13 02:33:02 +00:00
|
|
|
else if (nargs == 2)
|
2014-08-23 22:44:28 +00:00
|
|
|
return push_environment(L, push_scope(to_environment(L, 1), get_io_state(L), scope_kind::Namespace, to_name_ext(L, 2)));
|
|
|
|
scope_kind k = static_cast<scope_kind>(lua_tonumber(L, 3));
|
|
|
|
if (nargs == 3)
|
|
|
|
return push_environment(L, push_scope(to_environment(L, 1), get_io_state(L), k, to_name_ext(L, 2)));
|
2014-06-13 02:33:02 +00:00
|
|
|
else
|
2014-08-23 22:44:28 +00:00
|
|
|
return push_environment(L, push_scope(to_environment(L, 1), to_io_state(L, 4), k, to_name_ext(L, 2)));
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int pop_scope(lua_State * L) {
|
2014-08-07 23:59:08 +00:00
|
|
|
int nargs = lua_gettop(L);
|
|
|
|
if (nargs == 1)
|
2015-04-22 01:56:28 +00:00
|
|
|
return push_environment(L, pop_scope(to_environment(L, 1), get_io_state(L)));
|
2014-08-07 23:59:08 +00:00
|
|
|
else
|
2015-04-22 01:56:28 +00:00
|
|
|
return push_environment(L, pop_scope(to_environment(L, 1), get_io_state(L), to_name_ext(L, 2)));
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void open_scoped_ext(lua_State * L) {
|
|
|
|
SET_GLOBAL_FUN(using_namespace_objects, "using_namespace_objects");
|
|
|
|
SET_GLOBAL_FUN(push_scope, "push_scope");
|
|
|
|
SET_GLOBAL_FUN(pop_scope, "pop_scope");
|
2014-08-23 22:44:28 +00:00
|
|
|
|
|
|
|
lua_newtable(L);
|
|
|
|
SET_ENUM("Namespace", scope_kind::Namespace);
|
|
|
|
SET_ENUM("Section", scope_kind::Section);
|
|
|
|
lua_setglobal(L, "scope_kind");
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|
2014-09-23 00:30:29 +00:00
|
|
|
|
|
|
|
void initialize_scoped_ext() {
|
|
|
|
g_exts = new scoped_exts();
|
|
|
|
g_ext = new scope_mng_ext_reg();
|
|
|
|
g_new_namespace_key = new std::string("nspace");
|
|
|
|
register_module_object_reader(*g_new_namespace_key, namespace_reader);
|
|
|
|
}
|
|
|
|
|
|
|
|
void finalize_scoped_ext() {
|
|
|
|
delete g_new_namespace_key;
|
|
|
|
delete g_exts;
|
|
|
|
delete g_ext;
|
|
|
|
}
|
2014-06-13 02:33:02 +00:00
|
|
|
}
|