Skip to content

Commit

Permalink
[move-lang][move-ir] Intern all the strings
Browse files Browse the repository at this point in the history
- Use move-symbol-pool's Symbol for all strings in the IR and source language ASTs

Closes: aptos-labs#8874
  • Loading branch information
Todd Nowacki authored and bors-libra committed Aug 12, 2021
1 parent d5af478 commit 8d21bd0
Show file tree
Hide file tree
Showing 61 changed files with 558 additions and 614 deletions.
3 changes: 3 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions language/compiler/bytecode-source-map/src/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use move_binary_format::{
};
use move_core_types::{account_address::AccountAddress, identifier::Identifier};
use move_ir_types::ast::{ConstantName, ModuleName, NopLabel, QualifiedModuleIdent};
use move_symbol_pool::Symbol;
use serde::{Deserialize, Serialize};
use std::{collections::BTreeMap, ops::Bound};

Expand Down Expand Up @@ -318,7 +319,7 @@ impl<Location: Clone + Eq> FunctionSourceMap<Location> {
impl<Location: Clone + Eq> SourceMap<Location> {
pub fn new(module_name_opt: Option<QualifiedModuleIdent>) -> Self {
let module_name_opt = module_name_opt.map(|module_name| {
let ident = Identifier::new(module_name.name.into_inner()).unwrap();
let ident = Identifier::new(module_name.name.0.as_str()).unwrap();
(module_name.address, ident)
});
Self {
Expand Down Expand Up @@ -535,7 +536,9 @@ impl<Location: Clone + Eq> SourceMap<Location> {
/// with generated or real names depending upon if the source map is available or not.
pub fn dummy_from_view(view: &BinaryIndexedView, default_loc: Location) -> Result<Self> {
let module_handle = view.module_handle_at(ModuleHandleIndex::new(0));
let module_name = ModuleName::new(view.identifier_at(module_handle.name).to_string());
let module_name = ModuleName(Symbol::from(
view.identifier_at(module_handle.name).as_str(),
));
let address = *view.address_identifier_at(module_handle.address);
let module_ident = QualifiedModuleIdent::new(module_name, address);

Expand Down Expand Up @@ -568,7 +571,7 @@ impl<Location: Clone + Eq> SourceMap<Location> {
for const_idx in 0..view.constant_pool().len() {
empty_source_map.add_const_mapping(
ConstantPoolIndex(const_idx as TableIndex),
ConstantName::new(format!("CONST{}", const_idx)),
ConstantName(Symbol::from(format!("CONST{}", const_idx))),
)?;
}

Expand Down
18 changes: 9 additions & 9 deletions language/compiler/ir-to-bytecode/src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ use std::{

macro_rules! record_src_loc {
(local: $context:expr, $var:expr) => {{
let source_name = ($var.value.clone().into_inner(), $var.loc);
let source_name = ($var.value.0.as_str().to_owned(), $var.loc);
$context
.source_map
.add_local_mapping($context.current_function_definition_index(), source_name)?;
}};
(parameter: $context:expr, $var:expr) => {{
let source_name = ($var.value.clone().into_inner(), $var.loc);
let source_name = ($var.value.0.as_str().to_owned(), $var.loc);
$context
.source_map
.add_parameter_mapping($context.current_function_definition_index(), source_name)?;
Expand All @@ -56,7 +56,7 @@ macro_rules! record_src_loc {
}};
(function_type_formals: $context:expr, $var:expr) => {
for (ty_var, _) in $var.iter() {
let source_name = (ty_var.value.clone().into_inner(), ty_var.loc);
let source_name = (ty_var.value.0.as_str().to_owned(), ty_var.loc);
$context.source_map.add_function_type_parameter_mapping(
$context.current_function_definition_index(),
source_name,
Expand All @@ -72,7 +72,7 @@ macro_rules! record_src_loc {
}};
(struct_type_formals: $context:expr, $var:expr) => {
for (_, ty_var, _) in $var.iter() {
let source_name = (ty_var.value.clone().into_inner(), ty_var.loc);
let source_name = (ty_var.value.0.as_str().to_owned(), ty_var.loc);
$context.source_map.add_struct_type_parameter_mapping(
$context.current_struct_definition_index(),
source_name,
Expand Down Expand Up @@ -505,7 +505,7 @@ pub fn compile_module<'a>(
let friend_decls = compile_friends(&mut context, Some(address), module.friends)?;

// Compile imports
let self_name = ModuleName::new(ModuleName::self_name().into());
let self_name = ModuleName::module_self();
let self_module_handle_idx = context.declare_import(current_module, self_name.clone())?;
// Explicitly declare all imports as they will be included even if not used
compile_imports(&mut context, Some(address), module.imports.clone())?;
Expand Down Expand Up @@ -888,7 +888,7 @@ fn compile_fields(
StructDefinitionFields::Move { fields } => {
let mut decl_fields = vec![];
for (decl_order, (f, ty)) in fields.into_iter().enumerate() {
let name = context.identifier_index(f.value.as_inner())?;
let name = context.identifier_index(f.value.0)?;
record_src_loc!(field: context, sd_idx, f);
let sig_token = compile_type(context, type_parameters, &ty)?;
context.declare_field(sh_idx, sd_idx, f.value, sig_token.clone(), decl_order);
Expand Down Expand Up @@ -1402,7 +1402,7 @@ fn compile_expression(
let type_actuals_id = context.signature_index(tokens)?;
let def_idx = context.struct_definition_index(&name)?;

let self_name = ModuleName::new(ModuleName::self_name().into());
let self_name = ModuleName::module_self();
let ident = QualifiedStructIdent {
module: self_name,
name: name.clone(),
Expand Down Expand Up @@ -1652,7 +1652,7 @@ fn compile_call(
function_frame.pop()?;
function_frame.push()?;

let self_name = ModuleName::new(ModuleName::self_name().into());
let self_name = ModuleName::module_self();
let ident = QualifiedStructIdent {
module: self_name,
name,
Expand Down Expand Up @@ -1682,7 +1682,7 @@ fn compile_call(
function_frame.pop()?; // pop the address
function_frame.push()?; // push the return value

let self_name = ModuleName::new(ModuleName::self_name().into());
let self_name = ModuleName::module_self();
let ident = QualifiedStructIdent {
module: self_name,
name,
Expand Down
38 changes: 22 additions & 16 deletions language/compiler/ir-to-bytecode/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,32 +123,38 @@ impl<'a> CompiledDependencyView<'a> {
let address = *self
.address_identifiers
.get(module_handle.address.0 as usize)?;
let module = ModuleName::new(
let module = ModuleName(
self.identifiers
.get(module_handle.name.0 as usize)?
.to_string(),
.as_str()
.into(),
);
assert!(module.as_inner() != ModuleName::self_name());
assert!(module != ModuleName::module_self());
let ident = QualifiedModuleIdent {
address,
name: module,
};
let name = StructName::new(self.identifiers.get(handle.name.0 as usize)?.to_string());
let name = StructName(
self.identifiers
.get(handle.name.0 as usize)?
.as_str()
.into(),
);
Some((ident, name))
}

fn struct_handle(&self, module: &ModuleName, name: &StructName) -> Option<&'a StructHandle> {
self.structs
.get(&(
ident_str(module.as_inner()).ok()?,
ident_str(name.as_inner()).ok()?,
ident_str(module.0.as_str()).ok()?,
ident_str(name.0.as_str()).ok()?,
))
.and_then(|idx| self.struct_pool.get(*idx as usize))
}

fn function_signature(&self, name: &FunctionName) -> Option<FunctionSignature> {
self.functions
.get(ident_str(name.as_inner()).ok()?)
.get(ident_str(name.0.as_str()).ok()?)
.and_then(|idx| {
let fh = self.function_pool.get(*idx as usize)?;
Some(FunctionSignature {
Expand Down Expand Up @@ -317,7 +323,7 @@ impl<'a> Context<'a> {
pub fn add_compiled_dependency(&mut self, compiled_dep: &'a CompiledModule) -> Result<()> {
let ident = QualifiedModuleIdent {
address: *compiled_dep.address(),
name: ModuleName::new(compiled_dep.name().to_string()),
name: ModuleName(compiled_dep.name().as_str().into()),
};
match self.dependencies.get(&ident) {
None => self
Expand Down Expand Up @@ -487,8 +493,8 @@ impl<'a> Context<'a> {
}

/// Get the identifier pool index, adds it if missing.
pub fn identifier_index(&mut self, s: &str) -> Result<IdentifierIndex> {
let ident = ident_str(s)?;
pub fn identifier_index(&mut self, s: impl AsRef<str>) -> Result<IdentifierIndex> {
let ident = ident_str(s.as_ref())?;
let m = &mut self.identifiers;
let idx: Result<TableIndex> = get_or_add_item_macro!(m, ident, ident.to_owned());
Ok(IdentifierIndex(idx?))
Expand Down Expand Up @@ -563,7 +569,7 @@ impl<'a> Context<'a> {
/// Add a friend. This creates a module handle for the friended module.
pub fn declare_friend(&mut self, id: QualifiedModuleIdent) -> Result<ModuleHandle> {
let address = self.address_index(id.address)?;
let name = self.identifier_index(id.name.as_inner())?;
let name = self.identifier_index(id.name.0)?;
Ok(ModuleHandle { address, name })
}

Expand All @@ -576,7 +582,7 @@ impl<'a> Context<'a> {
// We don't care about duplicate aliases, if they exist
self.aliases.insert(id.clone(), alias.clone());
let address = self.address_index(id.address)?;
let name = self.identifier_index(id.name.as_inner())?;
let name = self.identifier_index(id.name.0)?;
self.modules
.insert(alias.clone(), (id, ModuleHandle { address, name }));
Ok(ModuleHandleIndex(get_or_add_item_ref(
Expand All @@ -603,7 +609,7 @@ impl<'a> Context<'a> {
type_parameters: Vec<StructTypeParameter>,
) -> Result<StructHandleIndex> {
let module = self.module_handle_index(&sname.module)?;
let name = self.identifier_index(sname.name.as_inner())?;
let name = self.identifier_index(sname.name.0)?;
self.structs.insert(
sname.clone(),
StructHandle {
Expand Down Expand Up @@ -646,7 +652,7 @@ impl<'a> Context<'a> {
) -> Result<()> {
let m_f = (mname.clone(), fname.clone());
let module = self.module_handle_index(&mname)?;
let name = self.identifier_index(fname.as_inner())?;
let name = self.identifier_index(fname.0)?;

self.function_signatures
.insert(m_f.clone(), signature.clone());
Expand Down Expand Up @@ -723,7 +729,7 @@ impl<'a> Context<'a> {
&mut self,
s: &QualifiedStructIdent,
) -> Result<(AbilitySet, Vec<StructTypeParameter>)> {
if s.module.as_inner() == ModuleName::self_name() {
if s.module == ModuleName::module_self() {
bail!("Unbound struct {}", s)
}
let mident = self.module_ident(&s.module)?.clone();
Expand Down Expand Up @@ -833,7 +839,7 @@ impl<'a> Context<'a> {
m: &ModuleName,
f: &FunctionName,
) -> Result<FunctionSignature> {
if m.as_inner() == ModuleName::self_name() {
if m == &ModuleName::module_self() {
bail!("Unbound function {}.{}", m, f)
}
let mident = self.module_ident(m)?.clone();
Expand Down
2 changes: 1 addition & 1 deletion language/compiler/ir-to-bytecode/syntax/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ impl<'input> Lexer<'input> {
self.token
}

pub fn content(&self) -> &str {
pub fn content(&self) -> &'input str {
&self.text[self.cur_start..self.cur_end]
}

Expand Down
Loading

0 comments on commit 8d21bd0

Please sign in to comment.