use rustc_hash::FxHashMap as HashMap;
use crate::ast::{Expression, Span};
use crate::types::Symbol;
use crate::types::Type;
pub type ResolvedDefinitions = HashMap<Span, Symbol>;
pub fn resolved_definition<'a>(
expression: &'a Expression,
definitions: &'a ResolvedDefinitions,
) -> Option<&'a str> {
match expression.unwrap_parens() {
Expression::Identifier {
qualified: Some(definition),
..
} => Some(definition),
Expression::DotAccess { span, .. } => definitions.get(span).map(Symbol::as_str),
_ => None,
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ReceiverCoercion {
AutoAddress,
AutoDeref,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DotAccessKind {
StructField { is_exported: bool },
TupleStructField { is_newtype: bool },
TupleElement,
ModuleMember,
EnumVariant,
InstanceMethod { is_exported: bool },
InstanceMethodValue {
is_exported: bool,
is_pointer_receiver: bool,
},
StaticMethod { is_exported: bool },
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeTypeKind {
Slice,
EnumeratedSlice,
Map,
Channel,
Sender,
Receiver,
String,
Array,
}
impl NativeTypeKind {
pub fn from_type(ty: &Type) -> Option<Self> {
let resolved = ty.strip_refs();
if resolved.as_import_namespace().is_some() {
return None;
}
if let Type::Nominal { ref id, .. } = resolved
&& id.as_str().starts_with("go:")
{
return None;
}
let name = resolved.get_name()?;
Self::from_name(name)
}
pub fn from_constructor_path(path: &str) -> Option<Self> {
match path {
"Channel.new" | "Channel.buffered" => Some(Self::Channel),
"Map.new" => Some(Self::Map),
"Slice.new" | "Slice.make" => Some(Self::Slice),
_ => None,
}
}
pub fn is_constructor_method(name: &str) -> bool {
matches!(name, "new" | "buffered" | "make")
}
pub fn from_name(name: &str) -> Option<Self> {
match name {
"Slice" => Some(Self::Slice),
"EnumeratedSlice" => Some(Self::EnumeratedSlice),
"Map" => Some(Self::Map),
"Channel" => Some(Self::Channel),
"Sender" => Some(Self::Sender),
"Receiver" => Some(Self::Receiver),
"string" => Some(Self::String),
"Array" => Some(Self::Array),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CallKind {
Regular,
TupleStructConstructor,
AssertType,
UfcsMethod,
NativeConstructor(NativeTypeKind),
NativeMethod(NativeTypeKind),
NativeMethodIdentifier(NativeTypeKind),
ReceiverMethodUfcs { is_public: bool },
}