Skip to content

Commit

Permalink
Represent strings as lists of char internally
Browse files Browse the repository at this point in the history
  • Loading branch information
alexcrichton authored and sunfishcode committed Feb 11, 2021
1 parent 2bcfe64 commit 1e48303
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 17 deletions.
2 changes: 1 addition & 1 deletion tools/witx/src/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,8 @@ impl Type {

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BuiltinType {
String,
Char8,
Char,
USize,
U8,
U16,
Expand Down
2 changes: 1 addition & 1 deletion tools/witx/src/coretypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ impl Type {
pub fn passed_by(&self) -> TypePassedBy {
match self {
Type::Builtin(b) => match b {
BuiltinType::String => TypePassedBy::PointerLengthPair,
BuiltinType::U8
| BuiltinType::U16
| BuiltinType::U32
| BuiltinType::S8
| BuiltinType::S16
| BuiltinType::S32
| BuiltinType::Char8
| BuiltinType::Char
| BuiltinType::USize => TypePassedBy::Value(AtomType::I32),
BuiltinType::U64 | BuiltinType::S64 => TypePassedBy::Value(AtomType::I64),
BuiltinType::F32 => TypePassedBy::Value(AtomType::F32),
Expand Down
7 changes: 5 additions & 2 deletions tools/witx/src/docs/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,8 +323,8 @@ impl ToMarkdown for InterfaceFuncParam {
impl BuiltinType {
pub fn type_name(&self) -> &'static str {
match self {
BuiltinType::String => "string",
BuiltinType::Char8 => "char8",
BuiltinType::Char => "char",
BuiltinType::USize => "usize",
BuiltinType::U8 => "u8",
BuiltinType::U16 => "u16",
Expand All @@ -345,7 +345,10 @@ impl TypeRef {
match self {
TypeRef::Name(n) => n.name.as_str().to_string(),
TypeRef::Value(ref v) => match &**v {
Type::List(a) => format!("List<{}>", a.type_name()),
Type::List(a) => match &*a.type_() {
Type::Builtin(BuiltinType::Char) => "string".to_string(),
_ => format!("List<{}>", a.type_name()),
},
Type::Pointer(p) => format!("Pointer<{}>", p.type_name()),
Type::ConstPointer(p) => format!("ConstPointer<{}>", p.type_name()),
Type::Builtin(b) => b.type_name().to_string(),
Expand Down
8 changes: 7 additions & 1 deletion tools/witx/src/docs/md.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,13 @@ impl fmt::Display for MdType {
Self::Record => f.write_fmt(format_args!(": Record"))?,
Self::Variant => f.write_fmt(format_args!(": Variant"))?,
Self::Union => f.write_fmt(format_args!(": Union"))?,
Self::List { r#type } => f.write_fmt(format_args!(": `List<{}>`", r#type))?,
Self::List { r#type } => {
if r#type == "char" {
f.write_str(": `string`")?
} else {
f.write_fmt(format_args!(": `List<{}>`", r#type))?
}
}
Self::Pointer { r#type } => f.write_fmt(format_args!(": `Pointer<{}>`", r#type))?,
Self::ConstPointer { r#type } => {
f.write_fmt(format_args!(": `ConstPointer<{}>`", r#type))?
Expand Down
11 changes: 6 additions & 5 deletions tools/witx/src/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl Type {
Type::Variant(s) => s.mem_size_align(),
Type::Union(u) => u.layout(cache),
Type::Handle(h) => h.mem_size_align(),
Type::List { .. } => BuiltinType::String.mem_size_align(),
Type::List { .. } => SizeAlign { size: 8, align: 4 }, // Pointer and Length
Type::Pointer { .. } | Type::ConstPointer { .. } => BuiltinType::U32.mem_size_align(),
Type::Builtin(b) => b.mem_size_align(),
}
Expand Down Expand Up @@ -242,14 +242,15 @@ impl Layout for HandleDatatype {
impl Layout for BuiltinType {
fn mem_size_align(&self) -> SizeAlign {
match self {
BuiltinType::String => SizeAlign { size: 8, align: 4 }, // Pointer and Length
BuiltinType::U8 | BuiltinType::S8 | BuiltinType::Char8 => {
SizeAlign { size: 1, align: 1 }
}
BuiltinType::U16 | BuiltinType::S16 => SizeAlign { size: 2, align: 2 },
BuiltinType::USize | BuiltinType::U32 | BuiltinType::S32 | BuiltinType::F32 => {
SizeAlign { size: 4, align: 4 }
}
BuiltinType::Char
| BuiltinType::USize
| BuiltinType::U32
| BuiltinType::S32
| BuiltinType::F32 => SizeAlign { size: 4, align: 4 },
BuiltinType::U64 | BuiltinType::S64 | BuiltinType::F64 => {
SizeAlign { size: 8, align: 8 }
}
Expand Down
19 changes: 13 additions & 6 deletions tools/witx/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ mod kw {

wast::custom_keyword!(bitflags);
wast::custom_keyword!(char8);
wast::custom_keyword!(char);
wast::custom_keyword!(const_pointer);
wast::custom_keyword!(f32);
wast::custom_keyword!(f64);
Expand Down Expand Up @@ -57,12 +58,12 @@ mod annotation {
impl Parse<'_> for BuiltinType {
fn parse(parser: Parser<'_>) -> Result<Self> {
let mut l = parser.lookahead1();
if l.peek::<kw::string>() {
parser.parse::<kw::string>()?;
Ok(BuiltinType::String)
} else if l.peek::<kw::char8>() {
if l.peek::<kw::char8>() {
parser.parse::<kw::char8>()?;
Ok(BuiltinType::Char8)
} else if l.peek::<kw::char>() {
parser.parse::<kw::char>()?;
Ok(BuiltinType::Char)
} else if l.peek::<kw::u8>() {
parser.parse::<kw::u8>()?;
Ok(BuiltinType::U8)
Expand Down Expand Up @@ -101,8 +102,8 @@ impl Parse<'_> for BuiltinType {

impl wast::parser::Peek for BuiltinType {
fn peek(cursor: wast::parser::Cursor<'_>) -> bool {
<kw::string as Peek>::peek(cursor)
|| <kw::char8 as Peek>::peek(cursor)
<kw::char8 as Peek>::peek(cursor)
|| <kw::char as Peek>::peek(cursor)
|| <kw::u8 as Peek>::peek(cursor)
|| <kw::u16 as Peek>::peek(cursor)
|| <kw::u32 as Peek>::peek(cursor)
Expand All @@ -114,10 +115,12 @@ impl wast::parser::Peek for BuiltinType {
|| <kw::f32 as Peek>::peek(cursor)
|| <kw::f64 as Peek>::peek(cursor)
}

fn display() -> &'static str {
"builtin type"
}
}

#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct CommentSyntax<'a> {
pub comments: Vec<&'a str>,
Expand Down Expand Up @@ -283,6 +286,7 @@ pub enum TypedefSyntax<'a> {
ConstPointer(Box<TypedefSyntax<'a>>),
Builtin(BuiltinType),
Ident(wast::Id<'a>),
String,
}

impl<'a> Parse<'a> for TypedefSyntax<'a> {
Expand All @@ -292,6 +296,9 @@ impl<'a> Parse<'a> for TypedefSyntax<'a> {
Ok(TypedefSyntax::Ident(parser.parse()?))
} else if l.peek::<BuiltinType>() {
Ok(TypedefSyntax::Builtin(parser.parse()?))
} else if l.peek::<kw::string>() {
parser.parse::<kw::string>()?;
Ok(TypedefSyntax::String)
} else if l.peek::<wast::LParen>() {
parser.parens(|parser| {
let mut l = parser.lookahead1();
Expand Down
2 changes: 1 addition & 1 deletion tools/witx/src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ impl Id {
impl BuiltinType {
pub fn to_sexpr(&self) -> SExpr {
match self {
BuiltinType::String => SExpr::word("string"),
BuiltinType::Char8 => SExpr::word("char8"),
BuiltinType::Char => SExpr::word("char"),
BuiltinType::USize => SExpr::Vec(vec![SExpr::annot("witx"), SExpr::word("usize")]),
BuiltinType::U8 => SExpr::word("u8"),
BuiltinType::U16 => SExpr::word("u16"),
Expand Down
3 changes: 3 additions & 0 deletions tools/witx/src/validate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,9 @@ impl DocValidationScope<'_> {
Type::ConstPointer(self.validate_datatype(syntax, false, span)?)
}
TypedefSyntax::Builtin(builtin) => Type::Builtin(*builtin),
TypedefSyntax::String => {
Type::List(TypeRef::Value(Rc::new(Type::Builtin(BuiltinType::Char))))
}
TypedefSyntax::Ident { .. } => unreachable!(),
}))),
}
Expand Down

0 comments on commit 1e48303

Please sign in to comment.