blob: cbd1f971273df50e34cd740695ae5488ba0840b8 [file]
// Copyright 2026 The Fuchsia Authors
//
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
// This file may not be copied, modified, or distributed except according to
// those terms.
//! Top-level bundle, record, and type JSON parsers.
use super::*;
mod function;
use self::function::parse_function;
pub fn parse_bundle(text: &str) -> Result<RawBundle> {
let v: Value = match serde_json::from_str(text) {
Ok(v) => v,
Err(e) => return Err(crate::error::LowerError::Json { detail: e.to_string() }),
};
let o = obj(&v, "bundle")?;
let version = get_u64(o, "schema_version", "bundle")?;
if version != 1 {
return schema(format!("unsupported schema_version {version} (expected 1)"));
}
let lp = obj(field(o, "lang_policy", "bundle")?, "lang_policy")?;
let lang_policy = RawLangPolicy {
is_cplusplus: get_bool(lp, "is_cplusplus", "lang_policy")?,
lang_standard: get_str(lp, "lang_standard", "lang_policy")?,
pointer_width_bits: get_u64(lp, "pointer_width_bits", "lang_policy")?,
char_is_signed: get_bool(lp, "char_is_signed", "lang_policy")?,
strict_aliasing: get_bool(lp, "strict_aliasing", "lang_policy")?,
signed_overflow: get_str(lp, "signed_overflow", "lang_policy")?,
exceptions_enabled: get_bool(lp, "exceptions_enabled", "lang_policy")?,
fast_math: get_bool(lp, "fast_math", "lang_policy")?,
little_endian: get_bool(lp, "little_endian", "lang_policy")?,
};
let mut records = Vec::new();
for r in get_arr(o, "records", "bundle")? {
records.push(parse_record(r)?);
}
let mut functions = Vec::new();
for f in get_arr(o, "functions", "bundle")? {
functions.push(parse_function(f)?);
}
Ok(RawBundle {
tu_main_file: get_str(o, "tu_main_file", "bundle")?,
target_triple: get_str(o, "target_triple", "bundle")?,
lang_policy,
records,
functions,
})
}
fn parse_record(v: &Value) -> Result<RawRecord> {
let o = obj(v, "record")?;
let id = get_str(o, "id", "record")?;
let ctx = format!("record `{id}`");
let mut fields = Vec::new();
for f in get_arr(o, "fields", &ctx)? {
let fo = obj(f, &ctx)?;
let bit_width = match get_opt(fo, "bit_width") {
None => None,
Some(b) => match b.as_u64() {
Some(n) => Some(n),
None => return schema(format!("{ctx}: bit_width must be an integer or null")),
},
};
fields.push(RawField {
name: get_str(fo, "name", &ctx)?,
offset_bits: get_u64(fo, "offset_bits", &ctx)?,
bit_width,
ty: parse_type(field(fo, "ty", &ctx)?)?,
is_const: get_bool(fo, "is_const", &ctx)?,
});
}
let mut base_ids = Vec::new();
if let Some(bases) = get_opt(o, "bases") {
let a = match bases.as_array() {
Some(a) => a,
None => return schema(format!("{ctx}: bases must be an array")),
};
for b in a {
let bo = obj(b, &ctx)?;
base_ids.push(get_str(bo, "record_id", &ctx)?);
}
}
Ok(RawRecord {
is_union: get_bool(o, "is_union", &ctx)?,
size_bytes: get_u64(o, "size_bytes", &ctx)?,
align_bytes: get_u64(o, "align_bytes", &ctx)?,
fields,
base_ids,
is_polymorphic: get_bool_or(o, "is_polymorphic", false, &ctx)?,
is_trivially_copyable: get_bool_or(o, "is_trivially_copyable", true, &ctx)?,
has_mutable: get_bool_or(o, "has_mutable", false, &ctx)?,
has_address_dependent_ctor: get_bool_or(o, "has_address_dependent_ctor", false, &ctx)?,
dtor_symbol: match get_opt(o, "dtor_symbol") {
Some(d) => match d.as_str() {
Some(d) => Some(d.to_owned()),
None => return schema(format!("{ctx}: dtor_symbol must be a string or null")),
},
None => None,
},
id,
})
}
pub fn parse_type(v: &Value) -> Result<RawType> {
let o = obj(v, "type")?;
let kind = get_str(o, "kind", "type")?;
Ok(match kind.as_str() {
"void" => RawType::Void,
"bool" => RawType::Bool,
"int" => RawType::Int {
bits: get_u64(o, "bits", "int type")?,
signed: get_bool(o, "signed", "int type")?,
is_char_like: get_bool_or(o, "is_char_like", false, "int type")?,
},
"ptr" => RawType::Ptr {
pointee: Box::new(parse_type(field(o, "pointee", "ptr type")?)?),
is_const: get_bool(o, "is_const", "ptr type")?,
is_restrict: get_bool(o, "is_restrict", "ptr type")?,
may_alias: get_bool(o, "may_alias", "ptr type")?,
},
"array" => RawType::Array {
elem: Box::new(parse_type(field(o, "elem", "array type")?)?),
len: get_u64(o, "len", "array type")?,
},
"record" => RawType::Record { id: get_str(o, "id", "record type")? },
"nullptr_t" => RawType::NullPtrT,
"ref" => RawType::Ref {
pointee: Box::new(parse_type(field(o, "pointee", "ref type")?)?),
is_const: get_bool(o, "is_const", "ref type")?,
is_rvalue: get_bool(o, "is_rvalue", "ref type")?,
may_alias: get_bool_or(o, "may_alias", false, "ref type")?,
},
"unsupported" => RawType::Unsupported { desc: get_str(o, "desc", "unsupported type")? },
other => return schema(format!("unknown type kind `{other}`")),
})
}