1 // Copyright (c) Facebook, Inc. and its affiliates.
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
12 use strum::IntoEnumIterator;
16 use crate::state::UnitState;
18 use crate::textual::TextualFile;
20 const UNIT_START_MARKER: &str = "TEXTUAL UNIT START";
21 const UNIT_END_MARKER: &str = "TEXTUAL UNIT END";
23 pub fn textual_writer(
24 w: &mut dyn std::io::Write,
29 let mut txf = TextualFile::new(w, Arc::clone(&unit.strings));
31 let escaped_path = escaper::escape(path.display().to_string());
32 txf.write_comment(&format!("{UNIT_START_MARKER} {escaped_path}"))?;
34 txf.set_attribute(textual::FileAttribute::SourceLanguage("hack".to_string()))?;
35 txf.debug_separator()?;
37 let mut state = UnitState::new(Arc::clone(&unit.strings));
38 check_fatal(path, unit.fatal.as_ref())?;
40 for cls in unit.classes {
41 crate::class::write_class(&mut txf, &mut state, cls)?;
44 for func in unit.functions {
45 crate::func::write_function(&mut txf, &mut state, func)?;
48 let all_builtins: HashMap<&str, hack::Builtin> = hack::Builtin::iter()
49 .map(|b| (b.as_str(), b))
50 .chain(hack::Hhbc::iter().map(|b| (b.as_str(), hack::Builtin::Hhbc(b))))
53 let builtins = txf.write_epilogue(&all_builtins)?;
56 decls::write_decls(&mut txf, &builtins)?;
59 txf.write_comment(&format!("{UNIT_END_MARKER} {escaped_path}"))?;
60 txf.debug_separator()?;
65 fn check_fatal(path: &Path, fatal: Option<&ir::Fatal>) -> Result<()> {
66 if let Some(fatal) = fatal {
67 let err = match fatal.op {
68 ir::FatalOp::Parse => "Parse",
69 ir::FatalOp::Runtime => "Runtime",
70 ir::FatalOp::RuntimeOmitFrame => "Runtime Omit",
75 "{err} error in {}[{}]: {}",