Skip to content

Commit

Permalink
Avoid creating "fn-" entries in interner
Browse files Browse the repository at this point in the history
  • Loading branch information
muglug committed Mar 21, 2024
1 parent 167c98b commit f76278b
Show file tree
Hide file tree
Showing 27 changed files with 252 additions and 212 deletions.
23 changes: 13 additions & 10 deletions src/analyzer/expr/call/arguments_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::sync::Arc;

use hakana_reflection_info::assertion::Assertion;
use hakana_reflection_info::EFFECT_WRITE_LOCAL;
use hakana_reflection_info::{GenericParent, EFFECT_WRITE_LOCAL};

use hakana_reflection_info::data_flow::node::DataFlowNode;
use hakana_reflection_info::taint::SinkType;
Expand Down Expand Up @@ -520,7 +520,7 @@ pub(crate) fn check_arguments_match(
}

fn adjust_param_type(
class_generic_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
class_generic_params: &IndexMap<StrId, Vec<(GenericParent, Arc<TUnion>)>>,
param_type: &mut TUnion,
codebase: &CodebaseInfo,
mut arg_value_type: TUnion,
Expand Down Expand Up @@ -833,7 +833,7 @@ fn handle_closure_arg(
}

fn map_class_generic_params(
class_generic_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
class_generic_params: &IndexMap<StrId, Vec<(GenericParent, Arc<TUnion>)>>,
param_type: &mut TUnion,
codebase: &CodebaseInfo,
interner: &Interner,
Expand Down Expand Up @@ -1083,7 +1083,7 @@ fn refine_template_result_for_functionlike(
classlike_storage: Option<&ClassLikeInfo>,
calling_classlike_storage: Option<&ClassLikeInfo>,
functionlike_storage: &FunctionLikeInfo,
class_template_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
class_template_params: &IndexMap<StrId, Vec<(GenericParent, Arc<TUnion>)>>,
) {
let template_types = get_template_types_for_call(
codebase,
Expand Down Expand Up @@ -1117,10 +1117,10 @@ pub(crate) fn get_template_types_for_call(
declaring_classlike_storage: Option<&ClassLikeInfo>,
appearing_class_name: Option<&StrId>,
calling_classlike_storage: Option<&ClassLikeInfo>,
existing_template_types: &[(StrId, Vec<(StrId, Arc<TUnion>)>)],
class_template_params: &IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
) -> IndexMap<StrId, FxHashMap<StrId, TUnion>> {
let mut template_types: IndexMap<StrId, Vec<(StrId, Arc<TUnion>)>> =
existing_template_types: &[(StrId, Vec<(GenericParent, Arc<TUnion>)>)],
class_template_params: &IndexMap<StrId, Vec<(GenericParent, Arc<TUnion>)>>,
) -> IndexMap<StrId, FxHashMap<GenericParent, TUnion>> {
let mut template_types: IndexMap<StrId, Vec<(GenericParent, Arc<TUnion>)>> =
IndexMap::from_iter(existing_template_types.to_owned());

if let Some(declaring_classlike_storage) = declaring_classlike_storage {
Expand All @@ -1144,7 +1144,7 @@ pub(crate) fn get_template_types_for_call(
let mut output_type = None;
for atomic_type in &type_.types {
let output_type_candidate = if let TAtomic::TGenericParam {
defining_entity,
defining_entity: GenericParent::ClassLike(defining_entity),
param_name,
..
} = &atomic_type
Expand Down Expand Up @@ -1179,7 +1179,10 @@ pub(crate) fn get_template_types_for_call(
template_types
.entry(*template_name)
.or_insert_with(Vec::new)
.push((declaring_classlike_storage.name, output_type));
.push((
GenericParent::ClassLike(declaring_classlike_storage.name),
output_type,
));
}
}
}
Expand Down
24 changes: 14 additions & 10 deletions src/analyzer/expr/call/class_template_param_collector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use rustc_hash::FxHashMap;

use hakana_reflection_info::{
classlike_info::ClassLikeInfo, codebase_info::CodebaseInfo, t_atomic::TAtomic, t_union::TUnion,
GenericParent,
};
use hakana_type::{add_optional_union_type, get_mixed_any, wrap_atomic};
use indexmap::IndexMap;
Expand All @@ -14,7 +15,7 @@ pub(crate) fn collect(
class_storage: &ClassLikeInfo,
static_class_storage: &ClassLikeInfo,
lhs_type_part: Option<&TAtomic>, // default None
) -> Option<IndexMap<StrId, FxHashMap<StrId, TUnion>>> {
) -> Option<IndexMap<StrId, FxHashMap<GenericParent, TUnion>>> {
let template_types = &class_storage.template_types;

if template_types.is_empty() {
Expand All @@ -38,7 +39,10 @@ pub(crate) fn collect(
class_template_params
.entry(*type_name)
.or_insert_with(FxHashMap::default)
.insert(class_storage.name, type_param.clone());
.insert(
GenericParent::ClassLike(class_storage.name),
type_param.clone(),
);
}
}
}
Expand All @@ -65,7 +69,7 @@ pub(crate) fn collect(
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.insert(
class_storage.name,
GenericParent::ClassLike(class_storage.name),
output_type_extends.unwrap_or(get_mixed_any()),
);
}
Expand All @@ -74,7 +78,7 @@ pub(crate) fn collect(
class_template_params
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.entry(class_storage.name)
.entry(GenericParent::ClassLike(class_storage.name))
.or_insert(get_mixed_any());
}
}
Expand All @@ -90,7 +94,7 @@ pub(crate) fn collect(
class_template_params
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.entry(class_storage.name)
.entry(GenericParent::ClassLike(class_storage.name))
.or_insert(TUnion::new(expand_type(
extended_type,
e,
Expand All @@ -106,7 +110,7 @@ pub(crate) fn collect(
..
}) = lhs_type_part
{
template_classname == self_class_name
template_classname == &GenericParent::ClassLike(*self_class_name)
} else {
false
};
Expand All @@ -115,7 +119,7 @@ pub(crate) fn collect(
class_template_params
.entry(*template_name)
.or_insert_with(FxHashMap::default)
.entry(class_storage.name)
.entry(GenericParent::ClassLike(class_storage.name))
.or_insert((**type_).clone());
}
}
Expand All @@ -135,7 +139,7 @@ pub(crate) fn resolve_template_param(
for type_extends_atomic in &input_type_extends.types {
if let TAtomic::TGenericParam {
param_name,
defining_entity,
defining_entity: GenericParent::ClassLike(defining_entity),
..
} = &type_extends_atomic
{
Expand Down Expand Up @@ -191,14 +195,14 @@ fn expand_type(
input_type_extends: &Arc<TUnion>,
e: &FxHashMap<StrId, IndexMap<StrId, Arc<TUnion>>>,
static_classlike_name: &StrId,
static_template_types: &Vec<(StrId, Vec<(StrId, Arc<TUnion>)>)>,
static_template_types: &Vec<(StrId, Vec<(GenericParent, Arc<TUnion>)>)>,
) -> Vec<TAtomic> {
let mut output_type_extends = Vec::new();

for type_extends_atomic in &input_type_extends.types {
if let Some(extended_type) = if let TAtomic::TGenericParam {
param_name,
defining_entity,
defining_entity: GenericParent::ClassLike(defining_entity),
..
} = type_extends_atomic
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hakana_reflection_info::{
t_atomic::{DictKey, TAtomic},
t_union::TUnion,
};
use hakana_reflection_info::{EFFECT_WRITE_LOCAL, EFFECT_WRITE_PROPS};
use hakana_reflection_info::{GenericParent, EFFECT_WRITE_LOCAL, EFFECT_WRITE_PROPS};
use hakana_str::StrId;
use hakana_type::get_null;
use hakana_type::template::standin_type_replacer;
Expand Down Expand Up @@ -148,7 +148,7 @@ pub(crate) fn analyze(
let template_type = class_template_params
.get(template_name)
.unwrap()
.get(&declaring_method_id.0)
.get(&GenericParent::ClassLike(declaring_method_id.0))
.unwrap();

standin_type_replacer::replace(
Expand Down
27 changes: 10 additions & 17 deletions src/analyzer/expr/call/function_call_return_type_fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use hakana_reflection_info::functionlike_info::FunctionLikeInfo;
use hakana_reflection_info::t_atomic::{DictKey, TAtomic};
use hakana_reflection_info::t_union::TUnion;
use hakana_reflection_info::taint::SinkType;
use hakana_reflection_info::GenericParent;
use hakana_str::{Interner, StrId};
use hakana_type::type_comparator::type_comparison_result::TypeComparisonResult;
use hakana_type::type_comparator::union_type_comparator;
Expand Down Expand Up @@ -76,28 +77,20 @@ pub(crate) fn fetch(
if !function_storage.template_types.is_empty()
&& !function_storage.template_types.is_empty()
{
let interner = statements_analyzer.get_interner();
let fn_id = interner
.get(
format!(
"fn-{}",
match functionlike_id {
FunctionLikeIdentifier::Function(function_id) => function_id.0,
FunctionLikeIdentifier::Method(_, _) => panic!(),
_ => {
panic!()
}
}
)
.as_str(),
)
.unwrap();
let fn_id = match functionlike_id {
FunctionLikeIdentifier::Function(function_id) => function_id,
FunctionLikeIdentifier::Method(_, _) => panic!(),
_ => {
panic!()
}
};

for (template_name, _) in &function_storage.template_types {
if template_result.lower_bounds.get(template_name).is_none() {
template_result.lower_bounds.insert(
*template_name,
FxHashMap::from_iter([(
fn_id,
GenericParent::FunctionLike(*fn_id),
vec![TemplateBound::new(get_nothing(), 1, None, None)],
)]),
);
Expand Down
11 changes: 2 additions & 9 deletions src/analyzer/expr/call/method_call_return_type_fetcher.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::rc::Rc;

use hakana_reflection_info::functionlike_identifier::FunctionLikeIdentifier;
use hakana_reflection_info::GenericParent;
use hakana_str::{Interner, StrId};
use oxidized::{aast, ast_defs};
use rustc_hash::FxHashMap;
Expand Down Expand Up @@ -72,20 +73,12 @@ pub(crate) fn fetch(
let mut template_result = template_result.clone();

if !functionlike_storage.template_types.is_empty() {
let fn_id = format!(
"fn-{}::{}",
declaring_method_id.0 .0, declaring_method_id.1 .0,
);
let fn_id = statements_analyzer
.get_interner()
.get(fn_id.as_str())
.unwrap();
for (template_name, _) in &functionlike_storage.template_types {
template_result
.lower_bounds
.entry(*template_name)
.or_insert(FxHashMap::from_iter([(
fn_id,
GenericParent::FunctionLike(declaring_method_id.1),
vec![TemplateBound::new(get_nothing(), 1, None, None)],
)]));
}
Expand Down
8 changes: 5 additions & 3 deletions src/analyzer/expr/call/new_analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::sync::Arc;

use hakana_reflection_info::classlike_info::Variance;
use hakana_reflection_info::EFFECT_WRITE_GLOBALS;
use hakana_reflection_info::{GenericParent, EFFECT_WRITE_GLOBALS};

use hakana_reflection_info::data_flow::node::DataFlowNode;
use hakana_reflection_info::functionlike_info::FunctionLikeInfo;
Expand Down Expand Up @@ -406,7 +406,9 @@ fn analyze_named_constructor(
let mut placeholder_lower_bounds = vec![];

if let Some(bounds) = template_result.lower_bounds.get(template_name) {
if let Some(bounds) = bounds.get(&classlike_name) {
if let Some(bounds) =
bounds.get(&GenericParent::ClassLike(classlike_name))
{
for bound in bounds {
placeholder_lower_bounds.push(bound.clone());
}
Expand Down Expand Up @@ -476,7 +478,7 @@ fn analyze_named_constructor(

let mut generic_param_type = if let Some(template_bounds) =
if let Some(result_map) = template_result.lower_bounds.get(template_name) {
result_map.get(&classlike_name)
result_map.get(&GenericParent::ClassLike(classlike_name))
} else {
None
} {
Expand Down
9 changes: 5 additions & 4 deletions src/analyzer/expr/call_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::sync::Arc;

use hakana_reflection_info::code_location::HPos;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_reflection_info::{EFFECT_IMPURE, EFFECT_WRITE_PROPS};
use hakana_reflection_info::{GenericParent, EFFECT_IMPURE, EFFECT_WRITE_PROPS};
use hakana_str::StrId;
use hakana_type::template::standin_type_replacer::get_relevant_bounds;
use hakana_type::type_comparator::type_comparison_result::TypeComparisonResult;
Expand Down Expand Up @@ -321,13 +321,13 @@ pub(crate) fn get_generic_param_for_offset(
classlike_name: &StrId,
template_name: &StrId,
template_extended_params: &FxHashMap<StrId, IndexMap<StrId, Arc<TUnion>>>,
found_generic_params: &FxHashMap<StrId, Vec<(StrId, Arc<TUnion>)>>,
found_generic_params: &FxHashMap<StrId, Vec<(GenericParent, Arc<TUnion>)>>,
) -> Arc<TUnion> {
if let Some(found_generic_param) =
if let Some(result_map) = found_generic_params.get(template_name) {
result_map
.iter()
.filter(|(e, _)| e == classlike_name)
.filter(|(e, _)| e == &GenericParent::ClassLike(*classlike_name))
.map(|(_, v)| v)
.next()
} else {
Expand All @@ -345,7 +345,8 @@ pub(crate) fn get_generic_param_for_offset(
..
} = &extended_atomic_type
{
if extended_param_name == template_name && defining_entity == classlike_name
if extended_param_name == template_name
&& defining_entity == &GenericParent::ClassLike(*classlike_name)
{
return get_generic_param_for_offset(
extended_class_name,
Expand Down
10 changes: 8 additions & 2 deletions src/analyzer/expr/fetch/atomic_property_fetch_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
use crate::{scope_context::ScopeContext, statements_analyzer::StatementsAnalyzer};
use hakana_reflection_info::code_location::HPos;
use hakana_reflection_info::issue::{Issue, IssueKind};
use hakana_reflection_info::GenericParent;
use hakana_reflection_info::{
classlike_info::ClassLikeInfo,
codebase_info::CodebaseInfo,
Expand Down Expand Up @@ -334,7 +335,10 @@ pub(crate) fn localize_property_type(
template_types
.entry(*calling_param_name)
.or_insert_with(FxHashMap::default)
.insert(property_class_storage.name, lhs_param_type.clone());
.insert(
GenericParent::ClassLike(property_class_storage.name),
lhs_param_type.clone(),
);
break;
}
}
Expand Down Expand Up @@ -365,7 +369,9 @@ pub(crate) fn localize_property_type(
.entry(type_name)
.or_insert_with(FxHashMap::default)
.insert(
property_declaring_class_storage.name,
GenericParent::ClassLike(
property_declaring_class_storage.name,
),
mapped_param.clone(),
);
}
Expand Down
1 change: 0 additions & 1 deletion src/analyzer/functionlike_analyzer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ impl<'a> FunctionLikeAnalyzer<'a> {
param_name: *param_name,
as_type: Box::new((*first_map_entry.1).clone()),
defining_entity: first_map_entry.0,
from_class: false,
extra_types: None,
})
})
Expand Down
4 changes: 2 additions & 2 deletions src/code_info/classlike_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hash::{FxHashMap, FxHashSet};

use crate::{
code_location::HPos, codebase_info::symbols::SymbolKind, functionlike_info::MetaStart,
t_atomic::TAtomic, t_union::TUnion,
t_atomic::TAtomic, t_union::TUnion, GenericParent,
};
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -131,7 +131,7 @@ pub struct ClassLikeInfo {
* (i.e. the same as the class name). This allows operations with the same-named template defined
* across multiple classes to not run into trouble.
*/
pub template_types: Vec<(StrId, Vec<(StrId, Arc<TUnion>)>)>,
pub template_types: Vec<(StrId, Vec<(GenericParent, Arc<TUnion>)>)>,

/*
* A list of all the templates that are only written in the constructor.
Expand Down
Loading

0 comments on commit f76278b

Please sign in to comment.