use anyhow::{Context, Result};
use id_arena::{Arena, Id};
use indexmap::IndexMap;
use semver::Version;
use std::borrow::Cow;
use std::fmt;
use std::path::Path;
#[cfg(feature = "decoding")]
pub mod decoding;
#[cfg(feature = "decoding")]
mod metadata;
#[cfg(feature = "decoding")]
pub use metadata::PackageMetadata;
pub mod abi;
mod ast;
use ast::lex::Span;
pub use ast::SourceMap;
pub use ast::{parse_use_path, ParsedUsePath};
mod sizealign;
pub use sizealign::*;
mod resolve;
pub use resolve::{Package, PackageId, Remap, Resolve};
mod live;
pub use live::LiveTypes;
#[cfg(feature = "serde")]
use serde_derive::Serialize;
#[cfg(feature = "serde")]
mod serde_;
#[cfg(feature = "serde")]
use serde_::*;
pub fn validate_id(s: &str) -> Result<()> {
ast::validate_id(0, s)?;
Ok(())
}
pub type WorldId = Id<World>;
pub type InterfaceId = Id<Interface>;
pub type TypeId = Id<TypeDef>;
#[derive(Clone)]
pub struct UnresolvedPackage {
pub name: PackageName,
pub worlds: Arena<World>,
pub interfaces: Arena<Interface>,
pub types: Arena<TypeDef>,
pub foreign_deps: IndexMap<PackageName, IndexMap<String, AstItem>>,
pub docs: Docs,
package_name_span: Span,
unknown_type_spans: Vec<Span>,
interface_spans: Vec<InterfaceSpan>,
world_spans: Vec<WorldSpan>,
type_spans: Vec<Span>,
foreign_dep_spans: Vec<Span>,
required_resource_types: Vec<(TypeId, Span)>,
}
#[derive(Clone)]
pub struct UnresolvedPackageGroup {
pub main: UnresolvedPackage,
pub nested: Vec<UnresolvedPackage>,
pub source_map: SourceMap,
}
#[derive(Clone)]
struct WorldSpan {
span: Span,
imports: Vec<Span>,
exports: Vec<Span>,
includes: Vec<Span>,
}
#[derive(Clone)]
struct InterfaceSpan {
span: Span,
funcs: Vec<Span>,
}
#[derive(Debug, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum AstItem {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Interface(InterfaceId),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
World(WorldId),
}
#[derive(Debug, Clone, Hash, Eq, PartialEq, Ord, PartialOrd)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(into = "String"))]
pub struct PackageName {
pub namespace: String,
pub name: String,
pub version: Option<Version>,
}
impl From<PackageName> for String {
fn from(name: PackageName) -> String {
name.to_string()
}
}
impl PackageName {
pub fn interface_id(&self, interface: &str) -> String {
let mut s = String::new();
s.push_str(&format!("{}:{}/{interface}", self.namespace, self.name));
if let Some(version) = &self.version {
s.push_str(&format!("@{version}"));
}
s
}
}
impl fmt::Display for PackageName {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.namespace, self.name)?;
if let Some(version) = &self.version {
write!(f, "@{version}")?;
}
Ok(())
}
}
#[derive(Debug)]
struct Error {
span: Span,
msg: String,
highlighted: Option<String>,
}
impl Error {
fn new(span: Span, msg: impl Into<String>) -> Error {
Error {
span,
msg: msg.into(),
highlighted: None,
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.highlighted.as_ref().unwrap_or(&self.msg).fmt(f)
}
}
impl std::error::Error for Error {}
impl UnresolvedPackageGroup {
pub fn parse(path: impl AsRef<Path>, contents: &str) -> Result<UnresolvedPackageGroup> {
let mut map = SourceMap::default();
map.push(path.as_ref(), contents);
map.parse()
}
pub fn parse_path(path: impl AsRef<Path>) -> Result<UnresolvedPackageGroup> {
let path = path.as_ref();
if path.is_dir() {
UnresolvedPackageGroup::parse_dir(path)
} else {
UnresolvedPackageGroup::parse_file(path)
}
}
pub fn parse_file(path: impl AsRef<Path>) -> Result<UnresolvedPackageGroup> {
let path = path.as_ref();
let contents = std::fs::read_to_string(path)
.with_context(|| format!("failed to read file {path:?}"))?;
Self::parse(path, &contents)
}
pub fn parse_dir(path: impl AsRef<Path>) -> Result<UnresolvedPackageGroup> {
let path = path.as_ref();
let mut map = SourceMap::default();
let cx = || format!("failed to read directory {path:?}");
for entry in path.read_dir().with_context(&cx)? {
let entry = entry.with_context(&cx)?;
let path = entry.path();
let ty = entry.file_type().with_context(&cx)?;
if ty.is_dir() {
continue;
}
if ty.is_symlink() {
if path.is_dir() {
continue;
}
}
let filename = match path.file_name().and_then(|s| s.to_str()) {
Some(name) => name,
None => continue,
};
if !filename.ends_with(".wit") {
continue;
}
map.push_file(&path)?;
}
map.parse()
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct World {
pub name: String,
pub imports: IndexMap<WorldKey, WorldItem>,
pub exports: IndexMap<WorldKey, WorldItem>,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_optional_id"))]
pub package: Option<PackageId>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Stability::is_unknown")
)]
pub stability: Stability,
#[cfg_attr(feature = "serde", serde(skip))]
pub includes: Vec<(Stability, WorldId)>,
#[cfg_attr(feature = "serde", serde(skip))]
pub include_names: Vec<Vec<IncludeName>>,
}
#[derive(Debug, Clone)]
pub struct IncludeName {
pub name: String,
pub as_: String,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(into = "String"))]
pub enum WorldKey {
Name(String),
Interface(InterfaceId),
}
impl From<WorldKey> for String {
fn from(key: WorldKey) -> String {
match key {
WorldKey::Name(name) => name,
WorldKey::Interface(id) => format!("interface-{}", id.index()),
}
}
}
impl WorldKey {
#[track_caller]
pub fn unwrap_name(self) -> String {
match self {
WorldKey::Name(name) => name,
WorldKey::Interface(_) => panic!("expected a name, found interface"),
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum WorldItem {
Interface {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
id: InterfaceId,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Stability::is_unknown")
)]
stability: Stability,
},
Function(Function),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Type(TypeId),
}
impl WorldItem {
pub fn stability<'a>(&'a self, resolve: &'a Resolve) -> &'a Stability {
match self {
WorldItem::Interface { stability, .. } => stability,
WorldItem::Function(f) => &f.stability,
WorldItem::Type(id) => &resolve.types[*id].stability,
}
}
}
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Interface {
pub name: Option<String>,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id_map"))]
pub types: IndexMap<String, TypeId>,
pub functions: IndexMap<String, Function>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Stability::is_unknown")
)]
pub stability: Stability,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_optional_id"))]
pub package: Option<PackageId>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct TypeDef {
pub name: Option<String>,
pub kind: TypeDefKind,
pub owner: TypeOwner,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Stability::is_unknown")
)]
pub stability: Stability,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum TypeDefKind {
Record(Record),
Resource,
Handle(Handle),
Flags(Flags),
Tuple(Tuple),
Variant(Variant),
Enum(Enum),
Option(Type),
Result(Result_),
List(Type),
Future(Option<Type>),
Stream(Stream),
Type(Type),
Unknown,
}
impl TypeDefKind {
pub fn as_str(&self) -> &'static str {
match self {
TypeDefKind::Record(_) => "record",
TypeDefKind::Resource => "resource",
TypeDefKind::Handle(handle) => match handle {
Handle::Own(_) => "own",
Handle::Borrow(_) => "borrow",
},
TypeDefKind::Flags(_) => "flags",
TypeDefKind::Tuple(_) => "tuple",
TypeDefKind::Variant(_) => "variant",
TypeDefKind::Enum(_) => "enum",
TypeDefKind::Option(_) => "option",
TypeDefKind::Result(_) => "result",
TypeDefKind::List(_) => "list",
TypeDefKind::Future(_) => "future",
TypeDefKind::Stream(_) => "stream",
TypeDefKind::Type(_) => "type",
TypeDefKind::Unknown => "unknown",
}
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum TypeOwner {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
World(WorldId),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Interface(InterfaceId),
#[cfg_attr(feature = "serde", serde(untagged, serialize_with = "serialize_none"))]
None,
}
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Handle {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Own(TypeId),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Borrow(TypeId),
}
#[derive(Debug, PartialEq, Eq, Hash, Copy, Clone)]
pub enum Type {
Bool,
U8,
U16,
U32,
U64,
S8,
S16,
S32,
S64,
F32,
F64,
Char,
String,
Id(TypeId),
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Int {
U8,
U16,
U32,
U64,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Record {
pub fields: Vec<Field>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Field {
pub name: String,
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub ty: Type,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Flags {
pub flags: Vec<Flag>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Flag {
pub name: String,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
}
#[derive(Debug, Clone, PartialEq)]
pub enum FlagsRepr {
U8,
U16,
U32(usize),
}
impl Flags {
pub fn repr(&self) -> FlagsRepr {
match self.flags.len() {
0 => FlagsRepr::U32(0),
n if n <= 8 => FlagsRepr::U8,
n if n <= 16 => FlagsRepr::U16,
n => FlagsRepr::U32(sizealign::align_to(n, 32) / 32),
}
}
}
impl FlagsRepr {
pub fn count(&self) -> usize {
match self {
FlagsRepr::U8 => 1,
FlagsRepr::U16 => 1,
FlagsRepr::U32(n) => *n,
}
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Tuple {
pub types: Vec<Type>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Variant {
pub cases: Vec<Case>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Case {
pub name: String,
#[cfg_attr(feature = "serde", serde(rename = "type"))]
pub ty: Option<Type>,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
}
impl Variant {
pub fn tag(&self) -> Int {
discriminant_type(self.cases.len())
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Enum {
pub cases: Vec<EnumCase>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct EnumCase {
pub name: String,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
}
impl Enum {
pub fn tag(&self) -> Int {
discriminant_type(self.cases.len())
}
}
fn discriminant_type(num_cases: usize) -> Int {
match num_cases.checked_sub(1) {
None => Int::U8,
Some(n) if n <= u8::max_value() as usize => Int::U8,
Some(n) if n <= u16::max_value() as usize => Int::U16,
Some(n) if n <= u32::max_value() as usize => Int::U32,
_ => panic!("too many cases to fit in a repr"),
}
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Result_ {
pub ok: Option<Type>,
pub err: Option<Type>,
}
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Stream {
pub element: Option<Type>,
pub end: Option<Type>,
}
#[derive(Clone, Default, Debug, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Docs {
pub contents: Option<String>,
}
impl Docs {
pub fn is_empty(&self) -> bool {
self.contents.is_none()
}
}
pub type Params = Vec<(String, Type)>;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(untagged))]
pub enum Results {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_params"))]
Named(Params),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_anon_result"))]
Anon(Type),
}
pub enum ResultsTypeIter<'a> {
Named(std::slice::Iter<'a, (String, Type)>),
Anon(std::iter::Once<&'a Type>),
}
impl<'a> Iterator for ResultsTypeIter<'a> {
type Item = &'a Type;
fn next(&mut self) -> Option<&'a Type> {
match self {
ResultsTypeIter::Named(ps) => ps.next().map(|p| &p.1),
ResultsTypeIter::Anon(ty) => ty.next(),
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
match self {
ResultsTypeIter::Named(ps) => ps.size_hint(),
ResultsTypeIter::Anon(ty) => ty.size_hint(),
}
}
}
impl<'a> ExactSizeIterator for ResultsTypeIter<'a> {}
impl Results {
pub fn empty() -> Results {
Results::Named(Vec::new())
}
pub fn len(&self) -> usize {
match self {
Results::Named(params) => params.len(),
Results::Anon(_) => 1,
}
}
pub fn throws<'a>(&self, resolve: &'a Resolve) -> Option<(Option<&'a Type>, Option<&'a Type>)> {
if self.len() != 1 {
return None;
}
match self.iter_types().next().unwrap() {
Type::Id(id) => match &resolve.types[*id].kind {
TypeDefKind::Result(r) => Some((r.ok.as_ref(), r.err.as_ref())),
_ => None,
},
_ => None,
}
}
pub fn iter_types(&self) -> ResultsTypeIter {
match self {
Results::Named(ps) => ResultsTypeIter::Named(ps.iter()),
Results::Anon(ty) => ResultsTypeIter::Anon(std::iter::once(ty)),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
pub struct Function {
pub name: String,
pub kind: FunctionKind,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_params"))]
pub params: Params,
pub results: Results,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Docs::is_empty"))]
pub docs: Docs,
#[cfg_attr(
feature = "serde",
serde(skip_serializing_if = "Stability::is_unknown")
)]
pub stability: Stability,
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum FunctionKind {
Freestanding,
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Method(TypeId),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Static(TypeId),
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_id"))]
Constructor(TypeId),
}
impl Function {
pub fn item_name(&self) -> &str {
match &self.kind {
FunctionKind::Freestanding => &self.name,
FunctionKind::Method(_) | FunctionKind::Static(_) => {
&self.name[self.name.find('.').unwrap() + 1..]
}
FunctionKind::Constructor(_) => "constructor",
}
}
pub fn core_export_name<'a>(&'a self, interface: Option<&str>) -> Cow<'a, str> {
match interface {
Some(interface) => Cow::Owned(format!("{interface}#{}", self.name)),
None => Cow::Borrowed(&self.name),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(serde_derive::Deserialize, Serialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "lowercase"))]
pub enum Stability {
Stable {
#[cfg_attr(feature = "serde", serde(serialize_with = "serialize_version"))]
#[cfg_attr(feature = "serde", serde(deserialize_with = "deserialize_version"))]
since: Version,
#[cfg_attr(feature = "serde", serde(skip_serializing_if = "Option::is_none"))]
feature: Option<String>,
#[cfg_attr(
feature = "serde",
serde(
skip_serializing_if = "Option::is_none",
default,
serialize_with = "serialize_optional_version",
deserialize_with = "deserialize_optional_version"
)
)]
deprecated: Option<Version>,
},
Unstable {
feature: String,
#[cfg_attr(
feature = "serde",
serde(
skip_serializing_if = "Option::is_none",
default,
serialize_with = "serialize_optional_version",
deserialize_with = "deserialize_optional_version"
)
)]
deprecated: Option<Version>,
},
Unknown,
}
impl Stability {
pub fn is_unknown(&self) -> bool {
matches!(self, Stability::Unknown)
}
}
impl Default for Stability {
fn default() -> Stability {
Stability::Unknown
}
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_discriminant_type() {
assert_eq!(discriminant_type(1), Int::U8);
assert_eq!(discriminant_type(0x100), Int::U8);
assert_eq!(discriminant_type(0x101), Int::U16);
assert_eq!(discriminant_type(0x10000), Int::U16);
assert_eq!(discriminant_type(0x10001), Int::U32);
if let Ok(num_cases) = usize::try_from(0x100000000_u64) {
assert_eq!(discriminant_type(num_cases), Int::U32);
}
}
}