Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce DeclRef and remove excessive clones to DeclId #4037

Merged
merged 7 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::{
use sway_core::{
abi_generation::{evm_json_abi, fuel_json_abi},
asm_generation::ProgramABI,
decl_engine::{DeclEngine, DeclId},
decl_engine::{DeclEngine, DeclRef},
fuel_prelude::{
fuel_crypto,
fuel_tx::{self, Contract, ContractId, StorageSlot},
Expand Down Expand Up @@ -2617,9 +2617,9 @@ impl PkgEntry {
finalized_entry: &FinalizedEntry,
decl_engine: &DeclEngine,
) -> Result<Self> {
let pkg_entry_kind = match &finalized_entry.test_decl_id {
Some(test_decl_id) => {
let pkg_test_entry = PkgTestEntry::from_decl(test_decl_id.clone(), decl_engine)?;
let pkg_entry_kind = match &finalized_entry.test_decl_ref {
Some(test_decl_ref) => {
let pkg_test_entry = PkgTestEntry::from_decl(test_decl_ref.clone(), decl_engine)?;
PkgEntryKind::Test(pkg_test_entry)
}
None => PkgEntryKind::Main,
Expand All @@ -2643,9 +2643,9 @@ impl PkgEntryKind {
}

impl PkgTestEntry {
fn from_decl(decl_id: DeclId, decl_engine: &DeclEngine) -> Result<Self> {
let span = decl_id.span();
let test_function_decl = decl_engine.get_function(decl_id, &span)?;
fn from_decl(decl_ref: DeclRef, decl_engine: &DeclEngine) -> Result<Self> {
let span = decl_ref.span();
let test_function_decl = decl_engine.get_function(&decl_ref, &span)?;

let test_args: HashSet<String> = test_function_decl
.attributes
Expand Down
38 changes: 19 additions & 19 deletions forc-plugins/forc-doc/src/descriptor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ use sway_types::Spanned;
trait RequiredMethods {
fn to_methods(&self, decl_engine: &DeclEngine) -> Result<Vec<TyTraitFn>>;
}
impl RequiredMethods for Vec<sway_core::decl_engine::DeclId> {
impl RequiredMethods for Vec<sway_core::decl_engine::DeclRef> {
fn to_methods(&self, decl_engine: &DeclEngine) -> Result<Vec<TyTraitFn>> {
self.iter()
.map(|decl_id| {
.map(|decl_ref| {
decl_engine
.get_trait_fn(decl_id.clone(), &decl_id.span())
.get_trait_fn(decl_ref, &decl_ref.span())
.map_err(|e| anyhow::anyhow!("{}", e))
})
.collect::<anyhow::Result<_>>()
Expand All @@ -44,8 +44,8 @@ impl Descriptor {
use swayfmt::parse;
use TyDeclaration::*;
match ty_decl {
StructDeclaration(ref decl_id) => {
let struct_decl = decl_engine.get_struct(decl_id.clone(), &decl_id.span())?;
StructDeclaration(ref decl_ref) => {
let struct_decl = decl_engine.get_struct(decl_ref, &decl_ref.span())?;
if !document_private_items && struct_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down Expand Up @@ -76,8 +76,8 @@ impl Descriptor {
}))
}
}
EnumDeclaration(ref decl_id) => {
let enum_decl = decl_engine.get_enum(decl_id.clone(), &decl_id.span())?;
EnumDeclaration(ref decl_ref) => {
let enum_decl = decl_engine.get_enum(decl_ref, &decl_ref.span())?;
if !document_private_items && enum_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down Expand Up @@ -108,8 +108,8 @@ impl Descriptor {
}))
}
}
TraitDeclaration(ref decl_id) => {
let trait_decl = decl_engine.get_trait(decl_id.clone(), &decl_id.span())?;
TraitDeclaration(ref decl_ref) => {
let trait_decl = decl_engine.get_trait(decl_ref, &decl_ref.span())?;
if !document_private_items && trait_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down Expand Up @@ -143,8 +143,8 @@ impl Descriptor {
}))
}
}
AbiDeclaration(ref decl_id) => {
let abi_decl = decl_engine.get_abi(decl_id.clone(), &decl_id.span())?;
AbiDeclaration(ref decl_ref) => {
let abi_decl = decl_engine.get_abi(decl_ref, &decl_ref.span())?;
let item_name = abi_decl.name;
let attrs_opt =
(!abi_decl.attributes.is_empty()).then(|| abi_decl.attributes.to_html_string());
Expand Down Expand Up @@ -172,8 +172,8 @@ impl Descriptor {
raw_attributes: attrs_opt,
}))
}
StorageDeclaration(ref decl_id) => {
let storage_decl = decl_engine.get_storage(decl_id.clone(), &decl_id.span())?;
StorageDeclaration(ref decl_ref) => {
let storage_decl = decl_engine.get_storage(decl_ref, &decl_ref.span())?;
let item_name = sway_types::BaseIdent::new_no_trim(
sway_types::span::Span::from_string(CONTRACT_STORAGE.to_string()),
);
Expand Down Expand Up @@ -203,12 +203,12 @@ impl Descriptor {
}))
}
// Uncomment this when we decide how to handle ImplTraits
// ImplTrait(ref decl_id) => {
// ImplTrait(ref decl_ref) => {
// TODO: figure out how to use this, likely we don't want to document this directly.
//
// This declaration type may make more sense to document as part of another declaration
// much like how we document method functions for traits or fields on structs.
// let impl_trait = decl_engine.get_impl_trait(decl_id.clone(), &decl_id.span())?;
// let impl_trait = decl_engine.get_impl_trait(&decl_ref, &decl_ref.span())?;
// let item_name = impl_trait.trait_name.suffix;
// Ok(Descriptor::Documentable(Document {
// module_info: module_info.clone(),
Expand All @@ -230,8 +230,8 @@ impl Descriptor {
// raw_attributes: None,
// }))
// }
FunctionDeclaration(ref decl_id) => {
let fn_decl = decl_engine.get_function(decl_id.clone(), &decl_id.span())?;
FunctionDeclaration(ref decl_ref) => {
let fn_decl = decl_engine.get_function(decl_ref, &decl_ref.span())?;
if !document_private_items && fn_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down Expand Up @@ -260,8 +260,8 @@ impl Descriptor {
}))
}
}
ConstantDeclaration(ref decl_id) => {
let const_decl = decl_engine.get_constant(decl_id.clone(), &decl_id.span())?;
ConstantDeclaration(ref decl_ref) => {
let const_decl = decl_engine.get_constant(decl_ref, &decl_ref.span())?;
if !document_private_items && const_decl.visibility.is_private() {
Ok(Descriptor::NonDocumentable)
} else {
Expand Down
6 changes: 3 additions & 3 deletions forc-plugins/forc-doc/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,13 +661,13 @@ impl Renderable for TyTraitFn {
write!(fn_sig, ") -> {}", self.return_type_span.as_str())?;
let multiline = fn_sig.chars().count() >= 60;

let method_id = format!("tymethod.{}", self.name.as_str());
let method_ref = format!("tymethod.{}", self.name.as_str());
Ok(box_html! {
div(class="methods") {
div(id=&method_id, class="method has-srclink") {
div(id=&method_ref, class="method has-srclink") {
h4(class="code-header") {
: "fn ";
a(class="fnname", href=format!("{IDENTITY}{method_id}")) {
a(class="fnname", href=format!("{IDENTITY}{method_ref}")) {
: self.name.as_str();
}
: "(";
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::{
ProgramABI, ProgramKind,
};
use crate::asm_lang::allocated_ops::{AllocatedOp, AllocatedOpcode};
use crate::decl_engine::DeclId;
use crate::decl_engine::DeclRef;
use crate::error::*;
use crate::source_map::SourceMap;

Expand Down Expand Up @@ -36,7 +36,7 @@ pub struct FinalizedEntry {
pub selector: Option<[u8; 4]>,
/// If this entry is constructed from a test function contains the declaration id for that
/// function, otherwise contains `None`.
pub test_decl_id: Option<DeclId>,
pub test_decl_ref: Option<DeclRef>,
}

/// The bytecode for a sway program as well as the byte offsets of configuration-time constants in
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ fn compile_module_to_asm(
let (data_section, reg_seqr, entries, non_entries) = result;
let entries = entries
.into_iter()
.map(|(func, label, ops, test_decl_id)| {
.map(|(func, label, ops, test_decl_ref)| {
let selector = func.get_selector(context);
let name = func.get_name(context).to_string();
AbstractEntry {
test_decl_id,
test_decl_ref,
selector,
label,
ops,
Expand Down
10 changes: 5 additions & 5 deletions sway-core/src/asm_generation/fuel/fuel_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::{
ProgramKind,
},
asm_lang::{virtual_register::*, Label, Op, VirtualImmediate12, VirtualImmediate18, VirtualOp},
decl_engine::DeclId,
decl_engine::DeclRef,
error::*,
fuel_prelude::fuel_crypto::Hasher,
metadata::MetadataManager,
Expand Down Expand Up @@ -63,7 +63,7 @@ pub struct FuelAsmBuilder<'ir> {

// Final resulting VM bytecode ops; entry functions with their function and label, and regular
// non-entry functions.
pub(super) entries: Vec<(Function, Label, Vec<Op>, Option<DeclId>)>,
pub(super) entries: Vec<(Function, Label, Vec<Op>, Option<DeclRef>)>,
pub(super) non_entries: Vec<Vec<Op>>,

// In progress VM bytecode ops.
Expand All @@ -73,7 +73,7 @@ pub struct FuelAsmBuilder<'ir> {
pub type FuelAsmBuilderResult = (
DataSection,
RegisterSequencer,
Vec<(Function, Label, AbstractInstructionSet, Option<DeclId>)>,
Vec<(Function, Label, AbstractInstructionSet, Option<DeclRef>)>,
Vec<AbstractInstructionSet>,
);

Expand Down Expand Up @@ -140,8 +140,8 @@ impl<'ir> FuelAsmBuilder<'ir> {
self.entries
.clone()
.into_iter()
.map(|(f, l, ops, test_decl_id)| {
(f, l, AbstractInstructionSet { ops }, test_decl_id)
.map(|(f, l, ops, test_decl_ref)| {
(f, l, AbstractInstructionSet { ops }, test_decl_ref)
})
.collect(),
self.non_entries
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/asm_generation/fuel/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::{
virtual_register::*, Op, OrganizationalOp, VirtualImmediate12, VirtualImmediate18,
VirtualImmediate24, VirtualOp,
},
decl_engine::DeclId,
decl_engine::DeclRef,
error::*,
fuel_prelude::fuel_asm::GTFArgs,
size_bytes_in_words, size_bytes_round_up_to_word_alignment,
Expand Down Expand Up @@ -150,8 +150,8 @@ impl<'ir> FuelAsmBuilder<'ir> {
let md = function.get_metadata(self.context);
let span = self.md_mgr.md_to_span(self.context, md);
let test_decl_index = self.md_mgr.md_to_test_decl_index(self.context, md);
let test_decl_id = match (&span, &test_decl_index) {
(Some(span), Some(decl_index)) => Some(DeclId::new(
let test_decl_ref = match (&span, &test_decl_index) {
(Some(span), Some(decl_index)) => Some(DeclRef::new(
Ident::new(span.clone()),
*decl_index,
span.clone(),
Expand Down Expand Up @@ -265,7 +265,7 @@ impl<'ir> FuelAsmBuilder<'ir> {
ops.append(&mut self.cur_bytecode);
if func_is_entry {
self.entries
.push((function, start_label, ops, test_decl_id));
.push((function, start_label, ops, test_decl_ref));
} else {
self.non_entries.push(ops);
}
Expand Down
8 changes: 4 additions & 4 deletions sway-core/src/asm_generation/programs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use super::fuel::{

use crate::{
asm_lang::{allocated_ops::AllocatedOp, Label},
decl_engine::DeclId,
decl_engine::DeclRef,
};

type SelectorOpt = Option<[u8; 4]>;
Expand Down Expand Up @@ -44,7 +44,7 @@ pub(super) struct AbstractEntry {
pub(super) label: Label,
pub(super) ops: AbstractInstructionSet,
pub(super) name: FnName,
pub(super) test_decl_id: Option<DeclId>,
pub(super) test_decl_ref: Option<DeclRef>,
}

/// An AllocatedProgram represents code which has allocated registers but still has abstract
Expand All @@ -54,7 +54,7 @@ pub(super) struct AllocatedProgram {
data_section: DataSection,
prologue: AllocatedAbstractInstructionSet,
functions: Vec<AllocatedAbstractInstructionSet>,
entries: Vec<(SelectorOpt, Label, FnName, Option<DeclId>)>,
entries: Vec<(SelectorOpt, Label, FnName, Option<DeclRef>)>,
}

/// A FinalProgram represents code which may be serialized to VM bytecode.
Expand All @@ -63,7 +63,7 @@ pub(super) enum FinalProgram {
kind: ProgramKind,
data_section: DataSection,
ops: Vec<AllocatedOp>,
entries: Vec<(SelectorOpt, ImmOffset, FnName, Option<DeclId>)>,
entries: Vec<(SelectorOpt, ImmOffset, FnName, Option<DeclRef>)>,
},
Evm {
ops: Vec<etk_asm::ops::AbstractOp>,
Expand Down
2 changes: 1 addition & 1 deletion sway-core/src/asm_generation/programs/abstract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl AbstractProgram {
entry.selector,
entry.label,
entry.name.clone(),
entry.test_decl_id.clone(),
entry.test_decl_ref.clone(),
)
})
.collect();
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/programs/allocated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ impl AllocatedProgram {
let entries = self
.entries
.into_iter()
.map(|(selector, label, name, test_decl_id)| {
.map(|(selector, label, name, test_decl_ref)| {
let offset = label_offsets
.remove(&label)
.expect("no offset for entry")
.offs;
(selector, offset, name, test_decl_id)
(selector, offset, name, test_decl_ref)
})
.collect();

Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/programs/final.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ impl FinalProgram {
program_kind: kind,
entries: entries
.into_iter()
.map(|(selector, imm, fn_name, test_decl_id)| FinalizedEntry {
.map(|(selector, imm, fn_name, test_decl_ref)| FinalizedEntry {
imm,
fn_name,
selector,
test_decl_id,
test_decl_ref,
})
.collect(),
abi: None,
Expand Down
18 changes: 9 additions & 9 deletions sway-core/src/control_flow_analysis/analyze_return_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use crate::{
control_flow_analysis::*,
decl_engine::DeclId,
decl_engine::DeclRef,
language::{ty, CallPath},
type_system::*,
Engines,
Expand Down Expand Up @@ -195,21 +195,21 @@ fn connect_declaration<'eng: 'cfg, 'cfg>(
}
Ok(vec![entry_node])
}
FunctionDeclaration(decl_id) => {
let fn_decl = decl_engine.get_function(decl_id.clone(), &decl.span())?;
FunctionDeclaration(decl_ref) => {
let fn_decl = decl_engine.get_function(decl_ref, &decl.span())?;
let entry_node = graph.add_node(node.into());
for leaf in leaves {
graph.add_edge(*leaf, entry_node, "".into());
}
connect_typed_fn_decl(engines, &fn_decl, graph, entry_node, span)?;
Ok(leaves.to_vec())
}
ImplTrait(decl_id) => {
ImplTrait(decl_ref) => {
let ty::TyImplTrait {
trait_name,
methods,
..
} = decl_engine.get_impl_trait(decl_id.clone(), &span)?;
} = decl_engine.get_impl_trait(decl_ref, &span)?;
let entry_node = graph.add_node(node.into());
for leaf in leaves {
graph.add_edge(*leaf, entry_node, "".into());
Expand All @@ -231,18 +231,18 @@ fn connect_impl_trait<'eng: 'cfg, 'cfg>(
engines: Engines<'eng>,
trait_name: &CallPath,
graph: &mut ControlFlowGraph<'cfg>,
methods: &[DeclId],
methods: &[DeclRef],
entry_node: NodeIndex,
) -> Result<(), CompileError> {
let decl_engine = engines.de();
let mut methods_and_indexes = vec![];
// insert method declarations into the graph
for method_decl_id in methods {
let fn_decl = decl_engine.get_function(method_decl_id.clone(), &trait_name.span())?;
for method_decl_ref in methods {
let fn_decl = decl_engine.get_function(method_decl_ref, &trait_name.span())?;
let fn_decl_entry_node = graph.add_node(ControlFlowGraphNode::MethodDeclaration {
span: fn_decl.span.clone(),
method_name: fn_decl.name.clone(),
method_decl_id: method_decl_id.clone(),
method_decl_ref: method_decl_ref.clone(),
engines,
});
graph.add_edge(entry_node, fn_decl_entry_node, "".into());
Expand Down
Loading