sway_core/language/call_path.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545
use crate::{
engine_threading::{
DebugWithEngines, DisplayWithEngines, EqWithEngines, HashWithEngines, OrdWithEngines,
OrdWithEnginesContext, PartialEqWithEngines, PartialEqWithEnginesContext,
},
parsed::QualifiedPathType,
Engines, Ident, Namespace,
};
use serde::{Deserialize, Serialize};
use std::{
cmp::Ordering,
fmt,
hash::{Hash, Hasher},
sync::Arc,
};
use sway_error::{
error::CompileError,
handler::{ErrorEmitted, Handler},
};
use sway_types::{span::Span, Spanned};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct CallPathTree {
pub qualified_call_path: QualifiedCallPath,
pub children: Vec<CallPathTree>,
}
impl HashWithEngines for CallPathTree {
fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
let CallPathTree {
qualified_call_path,
children,
} = self;
qualified_call_path.hash(state, engines);
children.hash(state, engines);
}
}
impl EqWithEngines for CallPathTree {}
impl PartialEqWithEngines for CallPathTree {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
let CallPathTree {
qualified_call_path,
children,
} = self;
qualified_call_path.eq(&other.qualified_call_path, ctx) && children.eq(&other.children, ctx)
}
}
impl<T: PartialEqWithEngines> EqWithEngines for Vec<T> {}
impl<T: PartialEqWithEngines> PartialEqWithEngines for Vec<T> {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
if self.len() != other.len() {
return false;
}
self.iter().zip(other.iter()).all(|(a, b)| a.eq(b, ctx))
}
}
impl OrdWithEngines for CallPathTree {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
let CallPathTree {
qualified_call_path: l_call_path,
children: l_children,
} = self;
let CallPathTree {
qualified_call_path: r_call_path,
children: r_children,
} = other;
l_call_path
.cmp(r_call_path, ctx)
.then_with(|| l_children.cmp(r_children, ctx))
}
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct QualifiedCallPath {
pub call_path: CallPath,
pub qualified_path_root: Option<Box<QualifiedPathType>>,
}
impl std::convert::From<Ident> for QualifiedCallPath {
fn from(other: Ident) -> Self {
QualifiedCallPath {
call_path: CallPath {
prefixes: vec![],
suffix: other,
callpath_type: CallPathType::Ambiguous,
},
qualified_path_root: None,
}
}
}
impl std::convert::From<CallPath> for QualifiedCallPath {
fn from(other: CallPath) -> Self {
QualifiedCallPath {
call_path: other,
qualified_path_root: None,
}
}
}
impl QualifiedCallPath {
pub fn to_call_path(self, handler: &Handler) -> Result<CallPath, ErrorEmitted> {
if let Some(qualified_path_root) = self.qualified_path_root {
Err(handler.emit_err(CompileError::Internal(
"Unexpected qualified path.",
qualified_path_root.as_trait_span,
)))
} else {
Ok(self.call_path)
}
}
}
impl HashWithEngines for QualifiedCallPath {
fn hash<H: Hasher>(&self, state: &mut H, engines: &Engines) {
let QualifiedCallPath {
call_path,
qualified_path_root,
} = self;
call_path.hash(state);
qualified_path_root.hash(state, engines);
}
}
impl EqWithEngines for QualifiedCallPath {}
impl PartialEqWithEngines for QualifiedCallPath {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
let QualifiedCallPath {
call_path,
qualified_path_root,
} = self;
PartialEqWithEngines::eq(call_path, &other.call_path, ctx)
&& qualified_path_root.eq(&other.qualified_path_root, ctx)
}
}
impl OrdWithEngines for QualifiedCallPath {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
let QualifiedCallPath {
call_path: l_call_path,
qualified_path_root: l_qualified_path_root,
} = self;
let QualifiedCallPath {
call_path: r_call_path,
qualified_path_root: r_qualified_path_root,
} = other;
l_call_path
.cmp(r_call_path)
.then_with(|| l_qualified_path_root.cmp(r_qualified_path_root, ctx))
}
}
impl DisplayWithEngines for QualifiedCallPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
if let Some(qualified_path_root) = &self.qualified_path_root {
write!(
f,
"{}::{}",
engines.help_out(qualified_path_root),
&self.call_path
)
} else {
write!(f, "{}", &self.call_path)
}
}
}
impl DebugWithEngines for QualifiedCallPath {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
write!(f, "{}", engines.help_out(self))
}
}
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub enum CallPathType {
/// An unresolved path on the form `::X::Y::Z`. The path must be resolved relative to the
/// current package root module.
/// The path can be converted to a full path by prepending the package name, so if the path
/// `::X::Y::Z` occurs in package `A`, then the corresponding full path will be `A::X::Y::Z`.
RelativeToPackageRoot,
/// An unresolved path on the form `X::Y::Z`. The path must either be resolved relative to the
/// current module, in which case `X` is either a submodule or a name bound in the current
/// module, or as a full path, in which case `X` is the name of an external package.
/// If the path is resolved relative to the current module, and the current module has a module
/// path `A::B::C`, then the corresponding full path is `A::B::C::X::Y::Z`.
/// If the path is resolved as a full path, then the full path is `X::Y::Z`.
Ambiguous,
/// A full path on the form `X::Y::Z`. The first identifier `X` is the name of either the
/// current package or an external package.
/// After that comes a (possibly empty) series of names of submodules. Then comes the name of an
/// item (a type, a trait, a function, or something else declared in that module). Additionally,
/// there may be additional names such as the name of an enum variant or associated types.
Full,
}
/// In the expression `a::b::c()`, `a` and `b` are the prefixes and `c` is the suffix.
/// `c` can be any type `T`, but in practice `c` is either an `Ident` or a `TypeInfo`.
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub struct CallPath<T = Ident> {
pub prefixes: Vec<Ident>,
pub suffix: T,
pub callpath_type: CallPathType,
}
impl EqWithEngines for CallPath {}
impl PartialEqWithEngines for CallPath {
fn eq(&self, other: &Self, _ctx: &PartialEqWithEnginesContext) -> bool {
self.prefixes == other.prefixes
&& self.suffix == other.suffix
&& self.callpath_type == other.callpath_type
}
}
impl<T: EqWithEngines> EqWithEngines for CallPath<T> {}
impl<T: PartialEqWithEngines> PartialEqWithEngines for CallPath<T> {
fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
self.prefixes == other.prefixes
&& self.suffix.eq(&other.suffix, ctx)
&& self.callpath_type == other.callpath_type
}
}
impl<T: OrdWithEngines> OrdWithEngines for CallPath<T> {
fn cmp(&self, other: &Self, ctx: &OrdWithEnginesContext) -> Ordering {
self.prefixes
.cmp(&other.prefixes)
.then_with(|| self.suffix.cmp(&other.suffix, ctx))
.then_with(|| self.callpath_type.cmp(&other.callpath_type))
}
}
#[derive(Debug, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
pub struct ResolvedCallPath<T, U = Ident> {
pub decl: T,
pub unresolved_call_path: CallPath<U>,
}
impl std::convert::From<Ident> for CallPath {
fn from(other: Ident) -> Self {
CallPath {
prefixes: vec![],
suffix: other,
callpath_type: CallPathType::Ambiguous,
}
}
}
impl<T> fmt::Display for CallPath<T>
where
T: fmt::Display,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
for prefix in self.prefixes.iter() {
write!(f, "{}::", prefix.as_str())?;
}
write!(f, "{}", &self.suffix)
}
}
impl<T: DisplayWithEngines> DisplayWithEngines for CallPath<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
for prefix in self.prefixes.iter() {
write!(f, "{}::", prefix.as_str())?;
}
write!(f, "{}", engines.help_out(&self.suffix))
}
}
impl<T: DisplayWithEngines> DebugWithEngines for CallPath<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>, engines: &Engines) -> fmt::Result {
for prefix in self.prefixes.iter() {
write!(f, "{}::", prefix.as_str())?;
}
write!(f, "{}", engines.help_out(&self.suffix))
}
}
impl<T: Spanned> Spanned for CallPath<T> {
fn span(&self) -> Span {
if self.prefixes.is_empty() {
self.suffix.span()
} else {
let mut prefixes_spans = self
.prefixes
.iter()
.map(|x| x.span())
//LOC below should be removed when #21 goes in
.filter(|x| {
Arc::ptr_eq(x.src(), self.suffix.span().src())
&& x.source_id() == self.suffix.span().source_id()
})
.peekable();
if prefixes_spans.peek().is_some() {
Span::join(Span::join_all(prefixes_spans), &self.suffix.span())
} else {
self.suffix.span()
}
}
}
}
impl CallPath {
pub fn fullpath(path: &[&str]) -> Self {
assert!(!path.is_empty());
CallPath {
prefixes: path
.iter()
.take(path.len() - 1)
.map(|&x| Ident::new_no_span(x.into()))
.collect(),
suffix: path.last().map(|&x| Ident::new_no_span(x.into())).unwrap(),
callpath_type: CallPathType::Full,
}
}
/// Shifts the last prefix into the suffix, and removes the old suffix.
/// Does nothing if prefixes are empty, or if the path is a full path and there is only a single prefix (which must be the package name, which is obligatory for full paths)
pub fn rshift(&self) -> CallPath {
if self.prefixes.is_empty()
|| (matches!(self.callpath_type, CallPathType::Full) && self.prefixes.len() == 1)
{
self.clone()
} else {
CallPath {
prefixes: self.prefixes[0..self.prefixes.len() - 1].to_vec(),
suffix: self.prefixes.last().unwrap().clone(),
callpath_type: self.callpath_type,
}
}
}
/// Removes the first prefix. Does nothing if prefixes are empty.
pub fn lshift(&self) -> CallPath {
if self.prefixes.is_empty() {
self.clone()
} else {
let new_callpath_type = match self.callpath_type {
CallPathType::RelativeToPackageRoot | CallPathType::Ambiguous => {
CallPathType::Ambiguous
}
CallPathType::Full => CallPathType::RelativeToPackageRoot,
};
CallPath {
prefixes: self.prefixes[1..self.prefixes.len()].to_vec(),
suffix: self.suffix.clone(),
callpath_type: new_callpath_type,
}
}
}
pub fn as_vec_string(&self) -> Vec<String> {
self.prefixes
.iter()
.map(|p| p.to_string())
.chain(std::iter::once(self.suffix.to_string()))
.collect::<Vec<_>>()
}
/// Create a full [CallPath] from a given [Ident] and the [Namespace] in which the [Ident] is
/// declared.
///
/// This function is intended to be used while typechecking the identifier declaration, i.e.,
/// before the identifier is added to the environment.
pub fn ident_to_fullpath(suffix: Ident, namespace: &Namespace) -> CallPath {
let mut res: Self = suffix.clone().into();
for mod_path in namespace.current_mod_path() {
res.prefixes.push(mod_path.clone())
}
res.callpath_type = CallPathType::Full;
res
}
/// Convert a given [CallPath] into a call path suitable for a `use` statement.
///
/// For example, given a path `pkga::SOME_CONST` where `pkga` is an _internal_ library of a package named
/// `my_project`, the corresponding call path is `pkga::SOME_CONST`.
///
/// Paths to _external_ libraries such `std::lib1::lib2::my_obj` are left unchanged.
pub fn to_import_path(&self, engines: &Engines, namespace: &Namespace) -> CallPath {
let converted = self.to_fullpath(engines, namespace);
if let Some(first) = converted.prefixes.first() {
if namespace.current_package_name() == first {
return converted.lshift();
}
}
converted
}
}
impl<T: Clone> CallPath<T> {
/// Convert a given [CallPath] to a symbol to a full [CallPath] to a program point in which the
/// symbol can be resolved (assuming the given [CallPath] is a legal Sway path).
///
/// The resulting [CallPath] is not guaranteed to be located in the package where the symbol is
/// declared. To obtain the path to the declaration, use [to_canonical_path].
///
/// The [CallPath] is converted within the current module of the supplied namespace.
///
/// For example, given a path `pkga::SOME_CONST` where `pkga` is an _internal_ module of a
/// package named `my_project`, the corresponding call path is
/// `my_project::pkga::SOME_CONST`. This does not imply that `SOME_CONST` is declared in the
/// `my_project::pkga`, but only that the name `SOME_CONST` is bound in `my_project::pkga`.
///
/// Paths to _external_ libraries such `std::lib1::lib2::my_obj` are considered full already
/// and are left unchanged since `std` is a root of the package `std`.
pub fn to_fullpath(&self, engines: &Engines, namespace: &Namespace) -> CallPath<T> {
self.to_fullpath_from_mod_path(engines, namespace, namespace.current_mod_path())
}
/// Convert a given [CallPath] to a symbol to a full [CallPath] to a program point in which the
/// symbol can be resolved (assuming the given [CallPath] is a legal Sway path).
///
/// The resulting [CallPath] is not guaranteed to be located in the package where the symbol is
/// declared. To obtain the path to the declaration, use [to_canonical_path].
///
/// The [CallPath] is converted within the module given by `mod_path`, which must be a legal
/// path to a module.
///
/// For example, given a path `pkga::SOME_CONST` where `pkga` is an _internal_ module of a
/// package named `my_project`, the corresponding call path is
/// `my_project::pkga::SOME_CONST`. This does not imply that `SOME_CONST` is declared in the
/// `my_project::pkga`, but only that the name `SOME_CONST` is bound in `my_project::pkga`.
///
/// Paths to _external_ libraries such `std::lib1::lib2::my_obj` are considered full already
/// and are left unchanged since `std` is a root of the package `std`.
pub fn to_fullpath_from_mod_path(
&self,
engines: &Engines,
namespace: &Namespace,
mod_path: &Vec<Ident>,
) -> CallPath<T> {
let mod_path_module = namespace.module_from_absolute_path(mod_path);
match self.callpath_type {
CallPathType::Full => self.clone(),
CallPathType::RelativeToPackageRoot => {
let mut prefixes = vec![mod_path[0].clone()];
for ident in self.prefixes.iter() {
prefixes.push(ident.clone());
}
Self {
prefixes,
suffix: self.suffix.clone(),
callpath_type: CallPathType::Full,
}
}
CallPathType::Ambiguous => {
if self.prefixes.is_empty() {
// // Given a path to a symbol that has no prefixes, discover the path to the symbol as a
// // combination of the package name in which the symbol is defined and the path to the
// // current submodule.
CallPath {
prefixes: mod_path.clone(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Full,
}
} else if mod_path_module.is_some()
&& (mod_path_module.unwrap().has_submodule(&self.prefixes[0])
|| namespace.module_has_binding(engines, mod_path, &self.prefixes[0]))
{
// The first identifier in the prefix is a submodule of the current
// module.
//
// The path is a qualified path relative to the current module
//
// Complete the path by prepending the package name and the path to the current module.
CallPath {
prefixes: mod_path.iter().chain(&self.prefixes).cloned().collect(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Full,
}
} else if namespace.package_exists(&self.prefixes[0])
&& namespace.module_is_external(&self.prefixes)
{
// The first identifier refers to an external package. The path is already fully qualified.
CallPath {
prefixes: self.prefixes.clone(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Full,
}
} else {
// The first identifier in the prefix is neither a submodule of the current module nor the name of an external package.
// This is probably an illegal path, so let it fail by assuming it is bound in the current module.
CallPath {
prefixes: mod_path.iter().chain(&self.prefixes).cloned().collect(),
suffix: self.suffix.clone(),
callpath_type: CallPathType::Full,
}
}
}
}
}
}
impl CallPath {
/// Convert a given [CallPath] to a symbol to a full [CallPath] to where the symbol is declared
/// (assuming the given [CallPath] is a legal Sway path).
///
/// The [CallPath] is converted within the current module of the supplied namespace.
///
/// For example, given a path `pkga::SOME_CONST` where `pkga` is an _internal_ module of a
/// package named `my_project`, and `SOME_CONST` is bound in the module `my_project::pkga`, then
/// the corresponding call path is the full callpath to the declaration that `SOME_CONST` is
/// bound to. This does not imply that `SOME_CONST` is declared in the `my_project::pkga`, since
/// the binding may be the result of an import.
///
/// Paths to _external_ libraries such `std::lib1::lib2::my_obj` are considered full already
/// and are left unchanged since `std` is a root of the package `std`.
pub fn to_canonical_path(&self, engines: &Engines, namespace: &Namespace) -> CallPath {
// Generate a full path to a module where the suffix can be resolved
let full_path = self.to_fullpath(engines, namespace);
match namespace.module_from_absolute_path(&full_path.prefixes) {
Some(module) => {
// Resolve the path suffix in the found module
match module.resolve_symbol(&Handler::default(), engines, &full_path.suffix) {
Ok((_, decl_path)) => {
// Replace the resolvable path with the declaration's path
CallPath {
prefixes: decl_path,
suffix: full_path.suffix.clone(),
callpath_type: full_path.callpath_type,
}
}
Err(_) => {
// The symbol does not resolve. The symbol isn't bound, so the best bet is
// the full path.
full_path
}
}
}
None => {
// The resolvable module doesn't exist. The symbol probably doesn't exist, so
// the best bet is the full path.
full_path
}
}
}
}