use crate::parsing::resolution::ImportBinding;
use crate::parsing::{InheritanceResolver, ResolutionScope, ScopeLevel, ScopeType};
use crate::{FileId, SymbolId};
use std::collections::HashMap;
pub struct CResolutionContext {
#[allow(dead_code)]
file_id: FileId,
local_scope: HashMap<String, SymbolId>,
module_symbols: HashMap<String, SymbolId>,
imported_symbols: HashMap<String, SymbolId>,
global_symbols: HashMap<String, SymbolId>,
scope_stack: Vec<ScopeType>,
includes: Vec<String>,
import_bindings: HashMap<String, ImportBinding>,
}
impl CResolutionContext {
pub fn new(file_id: FileId) -> Self {
Self {
file_id,
local_scope: HashMap::new(),
module_symbols: HashMap::new(),
imported_symbols: HashMap::new(),
global_symbols: HashMap::new(),
scope_stack: Vec::new(),
includes: Vec::new(),
import_bindings: HashMap::new(),
}
}
pub fn add_include(&mut self, header_path: String) {
self.includes.push(header_path);
}
pub fn add_local(&mut self, name: String, symbol_id: SymbolId) {
self.local_scope.insert(name, symbol_id);
}
pub fn add_module_symbol(&mut self, name: String, symbol_id: SymbolId) {
self.module_symbols.insert(name, symbol_id);
}
pub fn add_import_symbol(&mut self, name: String, symbol_id: SymbolId) {
self.imported_symbols.insert(name, symbol_id);
}
pub fn add_global_symbol(&mut self, name: String, symbol_id: SymbolId) {
self.global_symbols.insert(name, symbol_id);
}
pub fn includes(&self) -> &[String] {
&self.includes
}
}
impl ResolutionScope for CResolutionContext {
fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
self
}
fn add_symbol(&mut self, name: String, symbol_id: SymbolId, scope_level: ScopeLevel) {
match scope_level {
ScopeLevel::Local => {
self.local_scope.insert(name, symbol_id);
}
ScopeLevel::Module => {
self.module_symbols.insert(name, symbol_id);
}
ScopeLevel::Package => {
self.imported_symbols.insert(name, symbol_id);
}
ScopeLevel::Global => {
self.global_symbols.insert(name, symbol_id);
}
}
}
fn resolve(&self, name: &str) -> Option<SymbolId> {
if let Some(&id) = self.local_scope.get(name) {
return Some(id);
}
if let Some(&id) = self.module_symbols.get(name) {
return Some(id);
}
if let Some(&id) = self.imported_symbols.get(name) {
return Some(id);
}
if let Some(&id) = self.global_symbols.get(name) {
return Some(id);
}
None
}
fn clear_local_scope(&mut self) {
self.local_scope.clear();
}
fn enter_scope(&mut self, scope_type: ScopeType) {
self.scope_stack.push(scope_type);
}
fn exit_scope(&mut self) {
self.scope_stack.pop();
if matches!(
self.scope_stack.last(),
None | Some(ScopeType::Module | ScopeType::Global)
) {
self.clear_local_scope();
}
}
fn symbols_in_scope(&self) -> Vec<(String, SymbolId, ScopeLevel)> {
let mut symbols = Vec::new();
for (name, &id) in &self.local_scope {
symbols.push((name.clone(), id, ScopeLevel::Local));
}
for (name, &id) in &self.module_symbols {
symbols.push((name.clone(), id, ScopeLevel::Module));
}
for (name, &id) in &self.imported_symbols {
symbols.push((name.clone(), id, ScopeLevel::Package));
}
for (name, &id) in &self.global_symbols {
symbols.push((name.clone(), id, ScopeLevel::Global));
}
symbols
}
fn populate_imports(&mut self, imports: &[crate::parsing::Import]) {
for import in imports {
self.includes.push(import.path.clone());
}
}
fn register_import_binding(&mut self, binding: ImportBinding) {
self.import_bindings
.insert(binding.exposed_name.clone(), binding);
}
fn import_binding(&self, name: &str) -> Option<ImportBinding> {
self.import_bindings.get(name).cloned()
}
}
pub struct CInheritanceResolver {
composition_map: HashMap<String, Vec<(String, String)>>,
type_methods: HashMap<String, Vec<String>>,
typedef_map: HashMap<String, String>,
}
impl CInheritanceResolver {
pub fn new() -> Self {
Self {
composition_map: HashMap::new(),
type_methods: HashMap::new(),
typedef_map: HashMap::new(),
}
}
pub fn add_typedef(&mut self, alias: String, underlying_type: String) {
self.typedef_map.insert(alias, underlying_type);
}
pub fn resolve_typedef(&self, type_name: &str) -> String {
let mut current = type_name.to_string();
let mut visited = std::collections::HashSet::new();
while let Some(underlying) = self.typedef_map.get(¤t) {
if visited.contains(¤t) {
break; }
visited.insert(current.clone());
current = underlying.clone();
}
current
}
}
impl Default for CInheritanceResolver {
fn default() -> Self {
Self::new()
}
}
impl InheritanceResolver for CInheritanceResolver {
fn add_inheritance(&mut self, child: String, parent: String, kind: &str) {
match kind {
"typedef" => {
self.typedef_map.insert(child, parent);
}
"composition" | "embedded" => {
self.composition_map
.entry(child)
.or_default()
.push((parent, kind.to_string()));
}
_ => {
self.composition_map
.entry(child)
.or_default()
.push((parent, kind.to_string()));
}
}
}
fn resolve_method(&self, type_name: &str, method: &str) -> Option<String> {
let resolved_type = self.resolve_typedef(type_name);
if let Some(methods) = self.type_methods.get(&resolved_type) {
if methods.contains(&method.to_string()) {
return Some(resolved_type);
}
}
if let Some(composed_types) = self.composition_map.get(&resolved_type) {
for (composed_type, _kind) in composed_types {
if let Some(provider) = self.resolve_method(composed_type, method) {
return Some(provider);
}
}
}
None
}
fn get_inheritance_chain(&self, type_name: &str) -> Vec<String> {
let mut chain = Vec::new();
let mut visited = std::collections::HashSet::new();
self.build_composition_chain(type_name, &mut chain, &mut visited);
chain
}
fn is_subtype(&self, child: &str, parent: &str) -> bool {
if child == parent {
return true;
}
let resolved_child = self.resolve_typedef(child);
let resolved_parent = self.resolve_typedef(parent);
if resolved_child == resolved_parent {
return true;
}
let mut visited = std::collections::HashSet::new();
self.is_composed_of_recursive(&resolved_child, &resolved_parent, &mut visited)
}
fn add_type_methods(&mut self, type_name: String, methods: Vec<String>) {
let resolved_type = self.resolve_typedef(&type_name);
self.type_methods.insert(resolved_type, methods);
}
fn get_all_methods(&self, type_name: &str) -> Vec<String> {
let resolved_type = self.resolve_typedef(type_name);
let mut all_methods = std::collections::HashSet::new();
let mut visited = std::collections::HashSet::new();
self.collect_all_methods(&resolved_type, &mut all_methods, &mut visited);
all_methods.into_iter().collect()
}
}
impl CInheritanceResolver {
fn build_composition_chain(
&self,
type_name: &str,
chain: &mut Vec<String>,
visited: &mut std::collections::HashSet<String>,
) {
let resolved_type = self.resolve_typedef(type_name);
if visited.contains(&resolved_type) {
return; }
visited.insert(resolved_type.clone());
if let Some(composed_types) = self.composition_map.get(&resolved_type) {
for (composed_type, _kind) in composed_types {
chain.push(composed_type.clone());
self.build_composition_chain(composed_type, chain, visited);
}
}
}
fn is_composed_of_recursive(
&self,
child: &str,
parent: &str,
visited: &mut std::collections::HashSet<String>,
) -> bool {
if visited.contains(child) {
return false; }
visited.insert(child.to_string());
if let Some(composed_types) = self.composition_map.get(child) {
for (composed_type, _kind) in composed_types {
if composed_type == parent {
return true;
}
if self.is_composed_of_recursive(composed_type, parent, visited) {
return true;
}
}
}
false
}
fn collect_all_methods(
&self,
type_name: &str,
all_methods: &mut std::collections::HashSet<String>,
visited: &mut std::collections::HashSet<String>,
) {
if visited.contains(type_name) {
return; }
visited.insert(type_name.to_string());
if let Some(methods) = self.type_methods.get(type_name) {
all_methods.extend(methods.iter().cloned());
}
if let Some(composed_types) = self.composition_map.get(type_name) {
for (composed_type, _kind) in composed_types {
self.collect_all_methods(composed_type, all_methods, visited);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_c_resolution_basic() {
let file_id = FileId::new(1).unwrap();
let mut context = CResolutionContext::new(file_id);
let symbol_id = SymbolId::new(1).unwrap();
context.add_symbol("test_func".to_string(), symbol_id, ScopeLevel::Module);
assert_eq!(context.resolve("test_func"), Some(symbol_id));
assert_eq!(context.resolve("unknown"), None);
}
#[test]
fn test_scope_precedence() {
let file_id = FileId::new(1).unwrap();
let mut context = CResolutionContext::new(file_id);
let local_id = SymbolId::new(1).unwrap();
let module_id = SymbolId::new(2).unwrap();
context.add_symbol("name".to_string(), module_id, ScopeLevel::Module);
context.add_symbol("name".to_string(), local_id, ScopeLevel::Local);
assert_eq!(context.resolve("name"), Some(local_id));
}
#[test]
fn test_scope_management() {
let file_id = FileId::new(1).unwrap();
let mut context = CResolutionContext::new(file_id);
let symbol_id = SymbolId::new(1).unwrap();
context.add_symbol("local_var".to_string(), symbol_id, ScopeLevel::Local);
assert_eq!(context.resolve("local_var"), Some(symbol_id));
context.enter_scope(ScopeType::Function { hoisting: false });
context.exit_scope();
assert_eq!(context.resolve("local_var"), None);
}
}