Skip to content

Commit 1e48303

Browse files
alexcrichtonsunfishcode
authored andcommitted
Represent strings as lists of char internally
1 parent 2bcfe64 commit 1e48303

File tree

8 files changed

+37
-17
lines changed

8 files changed

+37
-17
lines changed

tools/witx/src/ast.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -213,8 +213,8 @@ impl Type {
213213

214214
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
215215
pub enum BuiltinType {
216-
String,
217216
Char8,
217+
Char,
218218
USize,
219219
U8,
220220
U16,

tools/witx/src/coretypes.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,14 @@ impl Type {
3535
pub fn passed_by(&self) -> TypePassedBy {
3636
match self {
3737
Type::Builtin(b) => match b {
38-
BuiltinType::String => TypePassedBy::PointerLengthPair,
3938
BuiltinType::U8
4039
| BuiltinType::U16
4140
| BuiltinType::U32
4241
| BuiltinType::S8
4342
| BuiltinType::S16
4443
| BuiltinType::S32
4544
| BuiltinType::Char8
45+
| BuiltinType::Char
4646
| BuiltinType::USize => TypePassedBy::Value(AtomType::I32),
4747
BuiltinType::U64 | BuiltinType::S64 => TypePassedBy::Value(AtomType::I64),
4848
BuiltinType::F32 => TypePassedBy::Value(AtomType::F32),

tools/witx/src/docs/ast.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,8 @@ impl ToMarkdown for InterfaceFuncParam {
323323
impl BuiltinType {
324324
pub fn type_name(&self) -> &'static str {
325325
match self {
326-
BuiltinType::String => "string",
327326
BuiltinType::Char8 => "char8",
327+
BuiltinType::Char => "char",
328328
BuiltinType::USize => "usize",
329329
BuiltinType::U8 => "u8",
330330
BuiltinType::U16 => "u16",
@@ -345,7 +345,10 @@ impl TypeRef {
345345
match self {
346346
TypeRef::Name(n) => n.name.as_str().to_string(),
347347
TypeRef::Value(ref v) => match &**v {
348-
Type::List(a) => format!("List<{}>", a.type_name()),
348+
Type::List(a) => match &*a.type_() {
349+
Type::Builtin(BuiltinType::Char) => "string".to_string(),
350+
_ => format!("List<{}>", a.type_name()),
351+
},
349352
Type::Pointer(p) => format!("Pointer<{}>", p.type_name()),
350353
Type::ConstPointer(p) => format!("ConstPointer<{}>", p.type_name()),
351354
Type::Builtin(b) => b.type_name().to_string(),

tools/witx/src/docs/md.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,13 @@ impl fmt::Display for MdType {
360360
Self::Record => f.write_fmt(format_args!(": Record"))?,
361361
Self::Variant => f.write_fmt(format_args!(": Variant"))?,
362362
Self::Union => f.write_fmt(format_args!(": Union"))?,
363-
Self::List { r#type } => f.write_fmt(format_args!(": `List<{}>`", r#type))?,
363+
Self::List { r#type } => {
364+
if r#type == "char" {
365+
f.write_str(": `string`")?
366+
} else {
367+
f.write_fmt(format_args!(": `List<{}>`", r#type))?
368+
}
369+
}
364370
Self::Pointer { r#type } => f.write_fmt(format_args!(": `Pointer<{}>`", r#type))?,
365371
Self::ConstPointer { r#type } => {
366372
f.write_fmt(format_args!(": `ConstPointer<{}>`", r#type))?

tools/witx/src/layout.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ impl Type {
6868
Type::Variant(s) => s.mem_size_align(),
6969
Type::Union(u) => u.layout(cache),
7070
Type::Handle(h) => h.mem_size_align(),
71-
Type::List { .. } => BuiltinType::String.mem_size_align(),
71+
Type::List { .. } => SizeAlign { size: 8, align: 4 }, // Pointer and Length
7272
Type::Pointer { .. } | Type::ConstPointer { .. } => BuiltinType::U32.mem_size_align(),
7373
Type::Builtin(b) => b.mem_size_align(),
7474
}
@@ -242,14 +242,15 @@ impl Layout for HandleDatatype {
242242
impl Layout for BuiltinType {
243243
fn mem_size_align(&self) -> SizeAlign {
244244
match self {
245-
BuiltinType::String => SizeAlign { size: 8, align: 4 }, // Pointer and Length
246245
BuiltinType::U8 | BuiltinType::S8 | BuiltinType::Char8 => {
247246
SizeAlign { size: 1, align: 1 }
248247
}
249248
BuiltinType::U16 | BuiltinType::S16 => SizeAlign { size: 2, align: 2 },
250-
BuiltinType::USize | BuiltinType::U32 | BuiltinType::S32 | BuiltinType::F32 => {
251-
SizeAlign { size: 4, align: 4 }
252-
}
249+
BuiltinType::Char
250+
| BuiltinType::USize
251+
| BuiltinType::U32
252+
| BuiltinType::S32
253+
| BuiltinType::F32 => SizeAlign { size: 4, align: 4 },
253254
BuiltinType::U64 | BuiltinType::S64 | BuiltinType::F64 => {
254255
SizeAlign { size: 8, align: 8 }
255256
}

tools/witx/src/parser.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ mod kw {
2020

2121
wast::custom_keyword!(bitflags);
2222
wast::custom_keyword!(char8);
23+
wast::custom_keyword!(char);
2324
wast::custom_keyword!(const_pointer);
2425
wast::custom_keyword!(f32);
2526
wast::custom_keyword!(f64);
@@ -57,12 +58,12 @@ mod annotation {
5758
impl Parse<'_> for BuiltinType {
5859
fn parse(parser: Parser<'_>) -> Result<Self> {
5960
let mut l = parser.lookahead1();
60-
if l.peek::<kw::string>() {
61-
parser.parse::<kw::string>()?;
62-
Ok(BuiltinType::String)
63-
} else if l.peek::<kw::char8>() {
61+
if l.peek::<kw::char8>() {
6462
parser.parse::<kw::char8>()?;
6563
Ok(BuiltinType::Char8)
64+
} else if l.peek::<kw::char>() {
65+
parser.parse::<kw::char>()?;
66+
Ok(BuiltinType::Char)
6667
} else if l.peek::<kw::u8>() {
6768
parser.parse::<kw::u8>()?;
6869
Ok(BuiltinType::U8)
@@ -101,8 +102,8 @@ impl Parse<'_> for BuiltinType {
101102

102103
impl wast::parser::Peek for BuiltinType {
103104
fn peek(cursor: wast::parser::Cursor<'_>) -> bool {
104-
<kw::string as Peek>::peek(cursor)
105-
|| <kw::char8 as Peek>::peek(cursor)
105+
<kw::char8 as Peek>::peek(cursor)
106+
|| <kw::char as Peek>::peek(cursor)
106107
|| <kw::u8 as Peek>::peek(cursor)
107108
|| <kw::u16 as Peek>::peek(cursor)
108109
|| <kw::u32 as Peek>::peek(cursor)
@@ -114,10 +115,12 @@ impl wast::parser::Peek for BuiltinType {
114115
|| <kw::f32 as Peek>::peek(cursor)
115116
|| <kw::f64 as Peek>::peek(cursor)
116117
}
118+
117119
fn display() -> &'static str {
118120
"builtin type"
119121
}
120122
}
123+
121124
#[derive(Debug, Clone, PartialEq, Eq, Default)]
122125
pub struct CommentSyntax<'a> {
123126
pub comments: Vec<&'a str>,
@@ -283,6 +286,7 @@ pub enum TypedefSyntax<'a> {
283286
ConstPointer(Box<TypedefSyntax<'a>>),
284287
Builtin(BuiltinType),
285288
Ident(wast::Id<'a>),
289+
String,
286290
}
287291

288292
impl<'a> Parse<'a> for TypedefSyntax<'a> {
@@ -292,6 +296,9 @@ impl<'a> Parse<'a> for TypedefSyntax<'a> {
292296
Ok(TypedefSyntax::Ident(parser.parse()?))
293297
} else if l.peek::<BuiltinType>() {
294298
Ok(TypedefSyntax::Builtin(parser.parse()?))
299+
} else if l.peek::<kw::string>() {
300+
parser.parse::<kw::string>()?;
301+
Ok(TypedefSyntax::String)
295302
} else if l.peek::<wast::LParen>() {
296303
parser.parens(|parser| {
297304
let mut l = parser.lookahead1();

tools/witx/src/render.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ impl Id {
7777
impl BuiltinType {
7878
pub fn to_sexpr(&self) -> SExpr {
7979
match self {
80-
BuiltinType::String => SExpr::word("string"),
8180
BuiltinType::Char8 => SExpr::word("char8"),
81+
BuiltinType::Char => SExpr::word("char"),
8282
BuiltinType::USize => SExpr::Vec(vec![SExpr::annot("witx"), SExpr::word("usize")]),
8383
BuiltinType::U8 => SExpr::word("u8"),
8484
BuiltinType::U16 => SExpr::word("u16"),

tools/witx/src/validate.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,9 @@ impl DocValidationScope<'_> {
311311
Type::ConstPointer(self.validate_datatype(syntax, false, span)?)
312312
}
313313
TypedefSyntax::Builtin(builtin) => Type::Builtin(*builtin),
314+
TypedefSyntax::String => {
315+
Type::List(TypeRef::Value(Rc::new(Type::Builtin(BuiltinType::Char))))
316+
}
314317
TypedefSyntax::Ident { .. } => unreachable!(),
315318
}))),
316319
}

0 commit comments

Comments
 (0)