| // 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. | |
| //! Statement AST definition and JSON parser for `RawCppBundle`. | |
| use super::*; | |
| #[derive(Debug, Clone)] | |
| pub enum RawStmt { | |
| Compound(Vec<RawStmt>), | |
| /// Multiple declarations from a single non-block `DeclStmt` (for example | |
| /// `for (int i = 0, j = n; ...)` or a labeled declaration), lowered into | |
| /// the enclosing scope without opening an inner block scope. | |
| DeclGroup(Vec<RawStmt>), | |
| Decl { | |
| var_id: u32, | |
| name: String, | |
| ty: RawType, | |
| init: Option<RawExpr>, | |
| /// Declared object type is `const`-qualified (including arrays of | |
| /// `const` elements). | |
| is_const: bool, | |
| /// C++: declared with reference type; `init` is the bound glvalue. | |
| is_ref: bool, | |
| /// C++: non-trivial destructor run at the end of the scope. | |
| dtor_symbol: Option<String>, | |
| /// C++: named return value optimization candidate. | |
| is_nrvo: bool, | |
| /// A constant-initialized, const-qualified function-local `static` | |
| /// (the extractor admits no other static locals). | |
| is_static_const: bool, | |
| }, | |
| Expr(RawExpr), | |
| If { | |
| cond: RawExpr, | |
| then_branch: Box<RawStmt>, | |
| else_branch: Option<Box<RawStmt>>, | |
| }, | |
| While { | |
| cond: RawExpr, | |
| body: Box<RawStmt>, | |
| }, | |
| DoWhile { | |
| body: Box<RawStmt>, | |
| cond: RawExpr, | |
| }, | |
| For { | |
| init: Option<Box<RawStmt>>, | |
| cond: Option<RawExpr>, | |
| inc: Option<RawExpr>, | |
| body: Box<RawStmt>, | |
| }, | |
| Switch { | |
| cond: RawExpr, | |
| body: Box<RawStmt>, | |
| }, | |
| Case { | |
| value: i128, | |
| sub: Box<RawStmt>, | |
| }, | |
| Default { | |
| sub: Box<RawStmt>, | |
| }, | |
| Break, | |
| Continue, | |
| Goto { | |
| label: String, | |
| }, | |
| Label { | |
| label: String, | |
| sub: Box<RawStmt>, | |
| }, | |
| Return(Option<RawExpr>), | |
| Null, | |
| Unsupported { | |
| desc: String, | |
| }, | |
| } | |
| fn boxed_stmt(o: &Map<String, Value>, key: &str, ctx: &str) -> Result<Box<RawStmt>> { | |
| Ok(Box::new(parse_stmt(field(o, key, ctx)?)?)) | |
| } | |
| pub fn parse_stmt(v: &Value) -> Result<RawStmt> { | |
| let o = obj(v, "stmt")?; | |
| let kind = get_str(o, "kind", "stmt")?; | |
| let ctx = kind.as_str(); | |
| Ok(match ctx { | |
| "compound" => { | |
| let mut stmts = Vec::new(); | |
| for s in get_arr(o, "stmts", ctx)? { | |
| stmts.push(parse_stmt(s)?); | |
| } | |
| RawStmt::Compound(stmts) | |
| } | |
| "decl_group" => { | |
| let mut decls = Vec::new(); | |
| for s in get_arr(o, "decls", ctx)? { | |
| let parsed = parse_stmt(s)?; | |
| if !matches!(parsed, RawStmt::Decl { .. } | RawStmt::Unsupported { .. }) { | |
| return schema("decl_group element must be decl_stmt or unsupported_stmt"); | |
| } | |
| decls.push(parsed); | |
| } | |
| RawStmt::DeclGroup(decls) | |
| } | |
| "decl_stmt" => RawStmt::Decl { | |
| var_id: get_u32(o, "var_id", ctx)?, | |
| name: get_str(o, "name", ctx)?, | |
| ty: parse_type(field(o, "ty", ctx)?)?, | |
| init: match get_opt(o, "init") { | |
| Some(e) => Some(parse_expr(e)?), | |
| None => None, | |
| }, | |
| is_const: get_bool(o, "is_const", ctx)?, | |
| is_ref: get_bool_or(o, "is_ref", false, ctx)?, | |
| dtor_symbol: get_opt_str(o, "dtor_symbol", ctx)?, | |
| is_nrvo: get_bool_or(o, "is_nrvo", false, ctx)?, | |
| is_static_const: get_bool_or(o, "is_static_const", false, ctx)?, | |
| }, | |
| "expr_stmt" => RawStmt::Expr(parse_expr(field(o, "expr", ctx)?)?), | |
| "if" => RawStmt::If { | |
| cond: parse_expr(field(o, "cond", ctx)?)?, | |
| then_branch: boxed_stmt(o, "then_branch", ctx)?, | |
| else_branch: match get_opt(o, "else_branch") { | |
| Some(s) => Some(Box::new(parse_stmt(s)?)), | |
| None => None, | |
| }, | |
| }, | |
| "while" => RawStmt::While { | |
| cond: parse_expr(field(o, "cond", ctx)?)?, | |
| body: boxed_stmt(o, "body", ctx)?, | |
| }, | |
| "do_while" => RawStmt::DoWhile { | |
| body: boxed_stmt(o, "body", ctx)?, | |
| cond: parse_expr(field(o, "cond", ctx)?)?, | |
| }, | |
| "for" => RawStmt::For { | |
| init: match get_opt(o, "init") { | |
| Some(s) => Some(Box::new(parse_stmt(s)?)), | |
| None => None, | |
| }, | |
| cond: match get_opt(o, "cond") { | |
| Some(e) => Some(parse_expr(e)?), | |
| None => None, | |
| }, | |
| inc: match get_opt(o, "inc") { | |
| Some(e) => Some(parse_expr(e)?), | |
| None => None, | |
| }, | |
| body: boxed_stmt(o, "body", ctx)?, | |
| }, | |
| "switch" => RawStmt::Switch { | |
| cond: parse_expr(field(o, "cond", ctx)?)?, | |
| body: boxed_stmt(o, "body", ctx)?, | |
| }, | |
| "case" => RawStmt::Case { | |
| value: parse_i128(&get_str(o, "value_str", ctx)?, ctx)?, | |
| sub: boxed_stmt(o, "sub", ctx)?, | |
| }, | |
| "default" => RawStmt::Default { sub: boxed_stmt(o, "sub", ctx)? }, | |
| "break" => RawStmt::Break, | |
| "continue" => RawStmt::Continue, | |
| "goto" => RawStmt::Goto { label: get_str(o, "label", ctx)? }, | |
| "label" => { | |
| RawStmt::Label { label: get_str(o, "label", ctx)?, sub: boxed_stmt(o, "sub", ctx)? } | |
| } | |
| "return" => RawStmt::Return(match get_opt(o, "expr") { | |
| Some(e) => Some(parse_expr(e)?), | |
| None => None, | |
| }), | |
| "null_stmt" => RawStmt::Null, | |
| "unsupported_stmt" => RawStmt::Unsupported { desc: get_str(o, "desc", ctx)? }, | |
| other => return schema(format!("unknown stmt kind `{other}`")), | |
| }) | |
| } |