Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 6 additions & 5 deletions core/src/analyzer/definitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ pub(crate) fn process_class_node(
class_node: &ruby_prism::ClassNode,
) -> Option<VertexId> {
let class_name = extract_class_name(class_node);
install_class(genv, class_name);
let superclass = class_node.superclass().and_then(|sup| extract_constant_path(&sup));
install_class(genv, class_name, superclass.as_deref());

if let Some(body) = class_node.body() {
if let Some(statements) = body.as_statements_node() {
Expand Down Expand Up @@ -126,8 +127,8 @@ pub(crate) fn process_def_node(
}

/// Install class definition
fn install_class(genv: &mut GlobalEnv, class_name: String) {
genv.enter_class(class_name);
fn install_class(genv: &mut GlobalEnv, class_name: String, superclass: Option<&str>) {
genv.enter_class(class_name, superclass);
}

/// Install module definition
Expand Down Expand Up @@ -203,7 +204,7 @@ mod tests {
fn test_enter_exit_class_scope() {
let mut genv = GlobalEnv::new();

install_class(&mut genv, "User".to_string());
install_class(&mut genv, "User".to_string(), None);
assert_eq!(
genv.scope_manager.current_class_name(),
Some("User".to_string())
Expand Down Expand Up @@ -231,7 +232,7 @@ mod tests {
fn test_nested_method_scope() {
let mut genv = GlobalEnv::new();

install_class(&mut genv, "User".to_string());
install_class(&mut genv, "User".to_string(), None);
install_method(&mut genv, "greet".to_string());

// Still in User class context
Expand Down
72 changes: 41 additions & 31 deletions core/src/analyzer/dispatch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,45 @@ use super::variables::{
install_self,
};

/// Collect positional and keyword arguments from AST argument nodes.
///
/// Shared by method calls (`dispatch.rs`) and super calls (`super_calls.rs`).
pub(crate) fn collect_arguments<'a>(
genv: &mut GlobalEnv,
lenv: &mut LocalEnv,
changes: &mut ChangeSet,
source: &str,
args: impl Iterator<Item = ruby_prism::Node<'a>>,
) -> (Vec<VertexId>, Option<HashMap<String, VertexId>>) {
let mut positional: Vec<VertexId> = Vec::new();
let mut keyword: HashMap<String, VertexId> = HashMap::new();

for arg in args {
if let Some(kw_hash) = arg.as_keyword_hash_node() {
for element in kw_hash.elements().iter() {
let assoc = match element.as_assoc_node() {
Some(a) => a,
None => continue,
};
let name = match assoc.key().as_symbol_node() {
Some(sym) => bytes_to_name(sym.unescaped()),
None => continue,
};
if let Some(vtx) =
super::install::install_node(genv, lenv, changes, source, &assoc.value())
{
keyword.insert(name, vtx);
}
}
} else if let Some(vtx) = super::install::install_node(genv, lenv, changes, source, &arg) {
positional.push(vtx);
}
}

let kw = (!keyword.is_empty()).then_some(keyword);
(positional, kw)
}

/// Kind of attr_* declaration
#[derive(Debug, Clone, Copy)]
pub enum AttrKind {
Expand Down Expand Up @@ -343,31 +382,8 @@ fn process_method_call_common<'a>(
return Some(super::operators::process_not_operator(genv));
}

// Separate positional arguments and keyword arguments
let mut positional_arg_vtxs: Vec<VertexId> = Vec::new();
let mut keyword_arg_vtxs: HashMap<String, VertexId> = HashMap::new();

for arg in &arguments {
if let Some(kw_hash) = arg.as_keyword_hash_node() {
for element in kw_hash.elements().iter() {
let assoc = match element.as_assoc_node() {
Some(a) => a,
None => continue,
};
let name = match assoc.key().as_symbol_node() {
Some(sym) => bytes_to_name(sym.unescaped()),
None => continue,
};
if let Some(vtx) =
super::install::install_node(genv, lenv, changes, source, &assoc.value())
{
keyword_arg_vtxs.insert(name, vtx);
}
}
} else if let Some(vtx) = super::install::install_node(genv, lenv, changes, source, arg) {
positional_arg_vtxs.push(vtx);
}
}
let (positional_arg_vtxs, kwarg_vtxs) =
collect_arguments(genv, lenv, changes, source, arguments.into_iter());

if let Some(block_node) = block {
if let Some(block) = block_node.as_block_node() {
Expand All @@ -388,12 +404,6 @@ fn process_method_call_common<'a>(
}
}

let kwarg_vtxs = if keyword_arg_vtxs.is_empty() {
None
} else {
Some(keyword_arg_vtxs)
};

Some(finish_method_call(
genv,
recv_vtx,
Expand Down
12 changes: 12 additions & 0 deletions core/src/analyzer/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use super::loops::{process_for_node, process_until_node, process_while_node};
use super::operators::{process_and_node, process_or_node};
use super::parentheses::process_parentheses_node;
use super::returns::process_return_node;
use super::super_calls;

/// Build graph from AST (public API wrapper)
pub struct AstInstaller<'a> {
Expand Down Expand Up @@ -89,6 +90,17 @@ pub(crate) fn install_node(
return process_rescue_modifier_node(genv, lenv, changes, source, &rescue_modifier);
}

// SuperNode: super(args) — explicit arguments
if let Some(super_node) = node.as_super_node() {
return super_calls::process_super_node(genv, lenv, changes, source, &super_node);
}
// ForwardingSuperNode: super — implicit argument forwarding
if let Some(fwd_super_node) = node.as_forwarding_super_node() {
return super_calls::process_forwarding_super_node(
genv, lenv, changes, source, &fwd_super_node,
);
}

if let Some(while_node) = node.as_while_node() {
return process_while_node(genv, lenv, changes, source, &while_node);
}
Expand Down
1 change: 1 addition & 0 deletions core/src/analyzer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ mod operators;
mod parameters;
mod parentheses;
mod returns;
mod super_calls;
mod variables;

pub use install::AstInstaller;
Expand Down
Loading