Skip to content

Commit

Permalink
[move-lang] propogate friend list to bytecode
Browse files Browse the repository at this point in the history
Closes: diem#7896
  • Loading branch information
meng-xu-cs authored and bors-libra committed Mar 12, 2021
1 parent b85673f commit a510b27
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 14 deletions.
6 changes: 6 additions & 0 deletions language/move-lang/src/cfgir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ pub struct ModuleDefinition {
pub is_source_module: bool,
/// `dependency_order` is the topological order/rank in the dependency graph.
pub dependency_order: usize,
pub friends: UniqueMap<ModuleIdent, Loc>,
pub structs: UniqueMap<StructName, StructDefinition>,
pub constants: UniqueMap<ConstantName, Constant>,
pub functions: UniqueMap<FunctionName, Function>,
Expand Down Expand Up @@ -200,6 +201,7 @@ impl AstDebug for ModuleDefinition {
let ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs,
constants,
functions,
Expand All @@ -210,6 +212,10 @@ impl AstDebug for ModuleDefinition {
w.writeln("source module")
}
w.writeln(&format!("dependency order #{}", dependency_order));
for (mident, _loc) in friends.key_cloned_iter() {
w.write(&format!("friend {};", mident));
w.new_line();
}
for sdef in structs.key_cloned_iter() {
sdef.ast_debug(w);
w.new_line();
Expand Down
17 changes: 12 additions & 5 deletions language/move-lang/src/cfgir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,16 +156,23 @@ fn module(
module_ident: ModuleIdent,
mdef: H::ModuleDefinition,
) -> (ModuleIdent, G::ModuleDefinition) {
let is_source_module = mdef.is_source_module;
let dependency_order = mdef.dependency_order;
let structs = mdef.structs;
let constants = mdef.constants.map(|name, c| constant(context, name, c));
let functions = mdef.functions.map(|name, f| function(context, name, f));
let H::ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs,
functions: hfunctions,
constants: hconstants,
} = mdef;

let constants = hconstants.map(|name, c| constant(context, name, c));
let functions = hfunctions.map(|name, f| function(context, name, f));
(
module_ident,
G::ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs,
constants,
functions,
Expand Down
6 changes: 6 additions & 0 deletions language/move-lang/src/hlir/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ pub struct ModuleDefinition {
pub is_source_module: bool,
/// `dependency_order` is the topological order/rank in the dependency graph.
pub dependency_order: usize,
pub friends: UniqueMap<ModuleIdent, Loc>,
pub structs: UniqueMap<StructName, StructDefinition>,
pub constants: UniqueMap<ConstantName, Constant>,
pub functions: UniqueMap<FunctionName, Function>,
Expand Down Expand Up @@ -576,6 +577,7 @@ impl AstDebug for ModuleDefinition {
let ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs,
constants,
functions,
Expand All @@ -586,6 +588,10 @@ impl AstDebug for ModuleDefinition {
w.writeln("source module")
}
w.writeln(&format!("dependency order #{}", dependency_order));
for (mident, _loc) in friends.key_cloned_iter() {
w.write(&format!("friend {};", mident));
w.new_line();
}
for sdef in structs.key_cloned_iter() {
sdef.ast_debug(w);
w.new_line();
Expand Down
17 changes: 12 additions & 5 deletions language/move-lang/src/hlir/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,21 +172,28 @@ fn module(
module_ident: ModuleIdent,
mdef: T::ModuleDefinition,
) -> (ModuleIdent, H::ModuleDefinition) {
let is_source_module = mdef.is_source_module;
let dependency_order = mdef.dependency_order;
let T::ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs: tstructs,
functions: tfunctions,
constants: tconstants,
} = mdef;

let structs = mdef.structs.map(|name, s| struct_def(context, name, s));
let structs = tstructs.map(|name, s| struct_def(context, name, s));
context.add_struct_fields(&structs);

let constants = mdef.constants.map(|name, c| constant(context, name, c));
let functions = mdef.functions.map(|name, f| function(context, name, f));
let constants = tconstants.map(|name, c| constant(context, name, c));
let functions = tfunctions.map(|name, f| function(context, name, f));

context.structs = UniqueMap::new();
(
module_ident,
H::ModuleDefinition {
is_source_module,
dependency_order,
friends,
structs,
constants,
functions,
Expand Down
2 changes: 1 addition & 1 deletion language/move-lang/src/to_bytecode/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'a> Context<'a> {
IR::ModuleName::new(format!("{}::{}", address, name))
}

fn translate_module_ident(ident: ModuleIdent) -> IR::ModuleIdent {
pub fn translate_module_ident(ident: ModuleIdent) -> IR::ModuleIdent {
let (address, name) = ident.value;
let name = Self::translate_module_name_(name);
IR::ModuleIdent::Qualified(IR::QualifiedModuleIdent::new(
Expand Down
9 changes: 7 additions & 2 deletions language/move-lang/src/to_bytecode/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,20 @@ fn module(

let addr = DiemAddress::new(ident.value.0.to_u8());
let mname = ident.value.1.clone();
let friends = mdef
.friends
.into_iter()
.map(|(mident, _loc)| Context::translate_module_ident(mident))
.collect();
let (imports, explicit_dependency_declarations) = context.materialize(
dependency_orderings,
struct_declarations,
function_declarations,
);

let ir_module = IR::ModuleDefinition {
name: IR::ModuleName::new(mname),
// TODO: add friends here
friends: vec![],
friends,
imports,
explicit_dependency_declarations,
structs,
Expand Down
1 change: 0 additions & 1 deletion language/move-lang/src/typing/translate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ fn module(
functions: n_functions,
constants: nconstants,
} = mdef;
// TODO: translate friends
structs
.iter_mut()
.for_each(|(_, _, s)| struct_def(context, s));
Expand Down

0 comments on commit a510b27

Please sign in to comment.