blob: 3a4526b4ec72ac26c816f77661def51f02689827 [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.
//! Expression, operator, and cast JSON parsers for `RawCppBundle`.
use super::*;
mod ops_and_casts;
use self::ops_and_casts::{binary_op, cast_kind, unary_op};
fn boxed_expr(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<Box<RawExpr>> {
Ok(Box::new(parse_expr(field(o, key, ctx)?)?))
}
pub fn parse_expr(v: &Value) -> Result<RawExpr> {
let o = obj(v, "expr")?;
let kind = get_str(o, "kind", "expr")?;
let ctx = kind.as_str();
let ty = parse_type(field(o, "ty", ctx)?)?;
let is_lvalue = match get_str(o, "value_category", ctx)?.as_str() {
// C++ xvalues are glvalues: they designate an object.
"lvalue" | "xvalue" => true,
"prvalue" => false,
other => {
return Ok(RawExpr {
ty,
is_lvalue: false,
is_const: false,
kind: RawExprKind::Unsupported { desc: format!("value category `{other}`") },
})
}
};
let is_const = if matches!(
ctx,
"compound_literal" | "materialize_temporary" | "bind_temporary" | "construct" | "init_list"
) {
get_bool(o, "is_const", ctx)?
} else {
get_bool_or(o, "is_const", false, ctx)?
};
let kind = match ctx {
"int_lit" => {
let value = parse_i128(&get_str(o, "value_str", ctx)?, ctx)?;
let bits = get_u64(o, "bits", ctx)?;
let signed = get_bool(o, "signed", ctx)?;
match &ty {
RawType::Int { bits: tb, signed: ts, .. } if *tb == bits && *ts == signed => {}
_ => return schema("int_lit: bits/signed disagree with `ty`"),
}
RawExprKind::IntLit { value }
}
"bool_lit" => RawExprKind::BoolLit { value: get_bool(o, "value", ctx)? },
"var_ref" => RawExprKind::VarRef {
var_id: get_u32(o, "var_id", ctx)?,
is_param: get_bool(o, "is_param", ctx)?,
param_idx: match get_opt(o, "param_idx") {
Some(p) => match p.as_u64().and_then(|n| u32::try_from(n).ok()) {
Some(n) => Some(n),
None => return schema("var_ref: param_idx must be a u32 or null"),
},
None => None,
},
is_ref_binding: get_bool_or(o, "is_ref_binding", false, ctx)?,
},
"global_ref" => RawExprKind::GlobalRef { symbol: get_str(o, "symbol", ctx)? },
"unary_op" => RawExprKind::Unary {
op: unary_op(&get_str(o, "op", ctx)?)?,
sub: boxed_expr(o, "sub", ctx)?,
},
"binary_op" => {
let op = binary_op(&get_str(o, "op", ctx)?)?;
let compute_tys = match op {
BinaryOp::CompoundAssign(_) => Some((
parse_type(field(o, "compute_lhs_ty", ctx)?)?,
parse_type(field(o, "compute_result_ty", ctx)?)?,
)),
_ => None,
};
RawExprKind::Binary {
op,
lhs: boxed_expr(o, "lhs", ctx)?,
rhs: boxed_expr(o, "rhs", ctx)?,
compute_tys,
}
}
"conditional" => RawExprKind::Conditional {
cond: boxed_expr(o, "cond", ctx)?,
then_expr: boxed_expr(o, "then_expr", ctx)?,
else_expr: boxed_expr(o, "else_expr", ctx)?,
},
"cast" => {
let ck = get_str(o, "cast_kind", ctx)?;
let kind = if ck == "derived_to_base" {
match get_opt(o, "base_offset_bytes").and_then(Value::as_u64) {
Some(offset_bytes) => Some(CastKind::DerivedToBase { offset_bytes }),
None => return schema("derived_to_base cast without base_offset_bytes"),
}
} else {
cast_kind(&ck)
};
match kind {
Some(kind) => RawExprKind::Cast { kind, sub: boxed_expr(o, "sub", ctx)? },
None => RawExprKind::Unsupported { desc: format!("cast kind `{ck}`") },
}
}
"array_subscript" => RawExprKind::ArraySubscript {
base: boxed_expr(o, "base", ctx)?,
idx: boxed_expr(o, "idx", ctx)?,
},
"member" => RawExprKind::Member {
base: boxed_expr(o, "base", ctx)?,
is_arrow: get_bool(o, "is_arrow", ctx)?,
field_name: get_str(o, "field_name", ctx)?,
record_id: get_str(o, "record_id", ctx)?,
field_idx: get_u32(o, "field_idx", ctx)?,
is_bitfield: get_bool_or(o, "is_bitfield", false, ctx)?,
},
"call" => {
let mut args = Vec::new();
for a in get_arr(o, "args", ctx)? {
args.push(parse_expr(a)?);
}
RawExprKind::Call {
callee_symbol: match get_opt(o, "callee_symbol") {
Some(s) => match s.as_str() {
Some(s) => Some(s.to_owned()),
None => return schema("call: callee_symbol must be a string or null"),
},
None => None,
},
args,
is_indirect: get_bool(o, "is_indirect", ctx)?,
is_builtin: get_bool_or(o, "is_builtin", false, ctx)?,
implicit_object: match get_opt(o, "implicit_object") {
Some(e) => Some(Box::new(parse_expr(e)?)),
None => None,
},
implicit_object_is_ptr: get_bool_or(o, "implicit_object_is_ptr", false, ctx)?,
is_virtual: get_bool_or(o, "is_virtual", false, ctx)?,
param_is_ref: get_bool_list(o, "param_is_ref", ctx)?,
returns_ref: get_bool_or(o, "returns_ref", false, ctx)?,
trivial_assign: get_bool_or(o, "trivial_assign", false, ctx)?,
}
}
"init_list" => {
let mut inits = Vec::new();
for i in get_arr(o, "inits", ctx)? {
inits.push(parse_expr(i)?);
}
RawExprKind::InitList {
inits,
is_transparent: get_bool_or(o, "is_transparent", false, ctx)?,
has_union_field: get_opt(o, "union_field_idx").is_some(),
array_filler: match get_opt(o, "array_filler") {
Some(f) => Some(Box::new(parse_expr(f)?)),
None => None,
},
}
}
"compound_literal" => RawExprKind::CompoundLiteral {
init: boxed_expr(o, "init", ctx)?,
is_file_scope: get_bool(o, "is_file_scope", ctx)?,
},
"cxx_this" => RawExprKind::CxxThis,
"nullptr_lit" => RawExprKind::NullptrLit,
"implicit_value_init" => RawExprKind::ImplicitValueInit,
"expr_with_cleanups" => RawExprKind::ExprWithCleanups { sub: boxed_expr(o, "sub", ctx)? },
"materialize_temporary" => RawExprKind::MaterializeTemporary {
sub: boxed_expr(o, "sub", ctx)?,
lifetime_extended: get_bool_or(o, "lifetime_extended", false, ctx)?,
dtor_symbol: get_opt_str(o, "dtor_symbol", ctx)?,
},
"bind_temporary" => RawExprKind::BindTemporary {
sub: boxed_expr(o, "sub", ctx)?,
dtor_symbol: get_str(o, "dtor_symbol", ctx)?,
},
"construct" => {
let mut args = Vec::new();
for a in get_arr(o, "args", ctx)? {
args.push(parse_expr(a)?);
}
RawExprKind::Construct {
ctor_symbol: get_str(o, "ctor_symbol", ctx)?,
record_id: get_str(o, "record_id", ctx)?,
args,
param_is_ref: get_bool_list(o, "param_is_ref", ctx)?,
is_trivial: get_bool(o, "is_trivial", ctx)?,
is_copy_or_move: get_bool_or(o, "is_copy_or_move", false, ctx)?,
is_default: get_bool_or(o, "is_default", false, ctx)?,
zero_init: get_bool_or(o, "zero_init", false, ctx)?,
is_array_construct: get_bool_or(o, "is_array_construct", false, ctx)?,
is_complete: get_opt_str(o, "construction_kind", ctx)?.as_deref()
== Some("complete"),
}
}
"builtin_bit_cast" => RawExprKind::BuiltinBitCast {
sub: boxed_expr(o, "sub", ctx)?,
from_ty: parse_type(field(o, "from_ty", ctx)?)?,
},
"sizeof_or_alignof" => {
RawExprKind::SizeofOrAlignof { value_bytes: get_u64(o, "value_bytes", ctx)? }
}
"paren" => RawExprKind::Paren { sub: boxed_expr(o, "sub", ctx)? },
"unsupported_expr" => RawExprKind::Unsupported { desc: get_str(o, "desc", ctx)? },
other => return schema(format!("unknown expr kind `{other}`")),
};
Ok(RawExpr { ty, is_lvalue, is_const, kind })
}