chore: remove legacy support for modification objects

This commit is contained in:
Leonardo de Moura 2020-10-26 08:10:51 -07:00
parent 289ba6583c
commit bad6233389
11 changed files with 3 additions and 221 deletions

View file

@ -444,45 +444,15 @@ def isTagged (ext : TagDeclarationExtension) (env : Environment) (n : Name) : Bo
end TagDeclarationExtension
/- Legacy support for Modification objects -/
/- Opaque modification object. It is essentially a C `void *`.
In Lean 3, a .olean file is essentially a collection of modification objects.
This type represents the modification objects implemented in C++.
We will eventually delete this type as soon as we port the remaining Lean 3
legacy code.
TODO: delete after we remove legacy code -/
def Modification := NonScalar
instance : Inhabited Modification := inferInstanceAs (Inhabited NonScalar)
builtin_initialize modListExtension : EnvExtension (List Modification) ← registerEnvExtension (pure [])
/- The C++ code uses this function to store the given modification object into the environment. -/
@[export lean_environment_add_modification]
def addModification (env : Environment) (mod : Modification) : Environment :=
modListExtension.modifyState env $ fun mods => mod :: mods
/- mkModuleData invokes this function to convert a list of modification objects into
a serialized byte array. -/
@[extern 2 "lean_serialize_modifications"]
constant serializeModifications : List Modification → IO ByteArray
@[extern 3 "lean_perform_serialized_modifications"]
constant performModifications : Environment → ByteArray → IO Environment
/- Content of a .olean file.
We use `compact.cpp` to generate the image of this object in disk. -/
structure ModuleData :=
(imports : Array Import)
(constants : Array ConstantInfo)
(entries : Array (Name × Array EnvExtensionEntry))
(serialized : ByteArray) -- Legacy support: serialized modification objects
instance : Inhabited ModuleData :=
⟨{imports := arbitrary _, constants := arbitrary _, entries := arbitrary _, serialized := arbitrary _}⟩
⟨{imports := arbitrary _, constants := arbitrary _, entries := arbitrary _}⟩
@[extern 3 "lean_save_module_data"]
constant saveModuleData (fname : @& String) (m : ModuleData) : IO Unit
@ -520,12 +490,10 @@ def mkModuleData (env : Environment) : IO ModuleData := do
let extName := (pExts.get! i).name
result.push (extName, exportEntriesFn state))
#[]
let bytes ← serializeModifications (modListExtension.getState env)
pure {
imports := env.header.imports,
constants := env.constants.foldStage2 (fun cs _ c => cs.push c) #[],
entries := entries,
serialized := bytes
entries := entries
}
@[export lean_write_module]
@ -599,8 +567,6 @@ def importModules (imports : List Import) (opts : Options) (trustLevel : UInt32
}
let env ← setImportedEntries env mods
let env ← finalizePersistentExtensions env opts
for mod in mods do
env ← performModifications env mod.serialized
pure env
/--

View file

@ -1,7 +1,6 @@
add_library(library OBJECT expr_lt.cpp io_state.cpp
io_state_stream.cpp bin_app.cpp constants.cpp max_sharing.cpp
module.cpp placeholder.cpp
sorry.cpp replace_visitor.cpp num.cpp
module.cpp placeholder.cpp sorry.cpp replace_visitor.cpp num.cpp
class.cpp util.cpp print.cpp annotation.cpp
protected.cpp reducible.cpp init_module.cpp exception.cpp
pp_options.cpp projection.cpp

View file

@ -5,7 +5,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "kernel/instantiate.h"
#include "library/module.h"
#include "library/trace.h"
#include "library/annotation.h"
#include "library/compiler/util.h"

View file

@ -4,7 +4,6 @@ Released under Apache 2.0 license as described in the file LICENSE.
Author: Leonardo de Moura
*/
#include "library/module.h"
#include "library/util.h"
namespace lean {

View file

@ -10,7 +10,6 @@ Author: Leonardo de Moura
#include "kernel/for_each_fn.h"
#include "kernel/type_checker.h"
#include "library/trace.h"
#include "library/module.h"
#include "library/class.h"
#include "library/compiler/util.h"
#include "library/compiler/csimp.h"

View file

@ -11,7 +11,6 @@ Author: Leonardo de Moura
#include "kernel/abstract.h"
#include "library/class.h"
#include "library/trace.h"
#include "library/module.h"
#include "library/compiler/util.h"
#include "library/compiler/csimp.h"

View file

@ -9,7 +9,6 @@ Author: Leonardo de Moura
#include "library/class.h"
#include "library/num.h"
#include "library/annotation.h"
#include "library/module.h"
#include "library/protected.h"
#include "library/io_state.h"
#include "library/idx_metavar.h"
@ -38,11 +37,9 @@ void initialize_library_core_module() {
initialize_constants();
initialize_profiling();
initialize_trace();
initialize_module();
}
void finalize_library_core_module() {
finalize_module();
finalize_trace();
finalize_profiling();
finalize_constants();

View file

@ -103,102 +103,6 @@ extern "C" object * lean_read_module_data(object * fname, object *) {
}
}
// =======================================
// Legacy support for Lean3 modification objects
static void modification_finalizer(void * ext) {
delete static_cast<modification*>(ext);
}
static void modification_foreach(void * /* mod */, b_obj_arg /* fn */) {
}
static external_object_class * g_modification_class = nullptr;
static modification & to_modification(b_obj_arg o) {
lean_assert(external_class(o) == g_modification_class);
return *static_cast<modification *>(external_data(o));
}
static obj_res to_object(modification * ext) {
return alloc_external(g_modification_class, ext);
}
typedef std::unordered_map<std::string, module_modification_reader> object_readers;
static object_readers * g_object_readers = nullptr;
static object_readers & get_object_readers() { return *g_object_readers; }
void register_module_object_reader(std::string const & k, module_modification_reader && r) {
object_readers & readers = get_object_readers();
lean_assert(readers.find(k) == readers.end());
readers[k] = r;
}
static char const * g_olean_end_file = "EndFile";
extern "C" object * lean_serialize_modifications(object * mod_list, object *) {
object_ref mod_list_ref(mod_list);
try {
std::ostringstream out(std::ios_base::binary);
serializer s(out);
buffer<object *> mod_buffer;
while (!is_scalar(mod_list)) {
mod_buffer.push_back(cnstr_get(mod_list, 0));
mod_list = cnstr_get(mod_list, 1);
}
size_t i = mod_buffer.size();
while (i > 0) {
--i;
modification & mod = to_modification(mod_buffer[i]);
s << std::string(mod.get_key());
mod.serialize(s);
}
s << g_olean_end_file;
std::string bytes = out.str();
object * r = alloc_sarray(1, bytes.size(), bytes.size());
memcpy(sarray_cptr(r), bytes.data(), bytes.size());
return io_result_mk_ok(r);
} catch (exception & ex) {
return io_result_mk_error(ex.what());
}
}
extern "C" object * lean_perform_serialized_modifications(object * env0, object * bytes, object *) {
environment env(env0);
std::string code((char*)sarray_cptr(bytes), sarray_size(bytes));
dec_ref(bytes);
try {
std::istringstream in(code, std::ios_base::binary);
deserializer d(in);
object_readers & readers = get_object_readers();
while (true) {
std::string k;
unsigned offset = in.tellg();
d >> k;
if (k == g_olean_end_file) {
break;
}
auto it = readers.find(k);
if (it == readers.end())
throw exception(sstream() << "olean file has been corrupted at offset " << offset
<< ", unknown object: " << k);
modification * mod = it->second(d);
mod->perform(env);
delete mod;
}
if (!in.good()) {
throw exception(sstream() << "olean file has been corrupted");
}
return io_result_mk_ok(env.steal());
} catch (exception & ex) {
return io_result_mk_error(ex.what());
}
}
// =======================================
/*
@[export lean.write_module_core]
def writeModule (env : Environment) (fname : String) : IO Unit := */
@ -207,40 +111,4 @@ extern "C" object * lean_write_module(object * env, object * fname, object *);
void write_module(environment const & env, std::string const & olean_fn) {
consume_io_result(lean_write_module(env.to_obj_arg(), mk_string(olean_fn), io_mk_world()));
}
extern "C" object * lean_import_modules(object * imports, object * opts, uint32 trust_level, object * w);
environment import_modules(unsigned trust_lvl, options const & opts, object_ref const & imports) {
return get_io_result<environment>(lean_import_modules(imports.to_obj_arg(), opts.to_obj_arg(), trust_lvl, io_mk_world()));
}
extern "C" object * lean_environment_add_modification(object * env, object * mod);
namespace module {
environment add(environment const & env, modification* modf) {
return environment(lean_environment_add_modification(env.to_obj_arg(), to_object(modf)));
}
environment add_and_perform(environment const & env, modification * modf) {
auto new_env = env;
modf->perform(new_env);
return add(new_env, modf);
}
environment add(environment const & env, declaration const & d, bool check) {
return env.add(d, check);
}
} // end of namespace module
void initialize_module() {
// file header size should preserve alignment
// can't be a static_assert because strlen isn't constexpr...
lean_always_assert(strlen(g_olean_header) % sizeof(object *) == 0);
g_modification_class = register_external_object_class(modification_finalizer, modification_foreach);
g_object_readers = new object_readers();
}
void finalize_module() {
delete g_object_readers;
}
}

View file

@ -15,48 +15,6 @@ Authors: Leonardo de Moura, Gabriel Ebner, Sebastian Ullrich
#include "kernel/environment.h"
namespace lean {
using module_name = name;
/** \brief Return an environment where all modules in \c modules are imported.
Modules included directly or indirectly by them are also imported.
This procedure looks for imported files in the search path set using `set_search_path`. */
environment import_modules(unsigned trust_lvl, options const &, object_ref const & imports);
/** \brief Store module using \c env. */
void write_module(environment const & env, std::string const & olean_fn);
struct modification {
public:
virtual ~modification() {}
virtual const char * get_key() const = 0;
virtual void perform(environment &) const = 0;
virtual void serialize(serializer &) const = 0;
};
#define LEAN_MODIFICATION(k) \
static void init() { \
register_module_object_reader(k, module_modification_reader(deserialize)); \
} \
static void finalize() {} \
const char * get_key() const override { return k; }
using module_modification_reader = std::function<modification*(deserializer &)>;
/** \brief Register a module object reader. The key \c k is used to identify the class of objects
that can be read by the given reader.
*/
void register_module_object_reader(std::string const & k, module_modification_reader && r);
namespace module {
/** \brief Add a function that should be invoked when the environment is exported.
The key \c k identifies which module_object_reader should be used to deserialize the object
when the module is imported.
\see module_object_reader
*/
environment add(environment const & env, modification * modif);
environment add_and_perform(environment const & env, modification * modif);
}
void initialize_module();
void finalize_module();
}

View file

@ -8,7 +8,6 @@ Author: Leonardo de Moura
#include <string>
#include "util/name_set.h"
#include "library/protected.h"
#include "library/module.h"
namespace lean {
extern "C" object * lean_add_protected(object * env, object * n);

View file

@ -7,7 +7,6 @@ Author: Leonardo de Moura
#include <string>
#include "library/module_mgr.h"
#include "library/st_task_queue.h"
#include "library/module.h"
#include "library/type_context.h"
#include "frontends/lean/pp.h"
#include "frontends/lean/parser.h"