diff options
author | Princeton Ferro <princetonferro@gmail.com> | 2021-05-09 15:55:37 -0400 |
---|---|---|
committer | Rico Tzschichholz <ricotz@ubuntu.com> | 2021-05-20 11:59:28 +0200 |
commit | 994b4cb078643d9bb1842fa60ecb69891e1e7b87 (patch) | |
tree | ab33efea49be6482d6b5bd05cdd1bb373768b2d9 /ccode | |
parent | acc059f8ede46b3165bb4eabeb77e5e81d455ce4 (diff) | |
download | vala-994b4cb078643d9bb1842fa60ecb69891e1e7b87.tar.gz |
ccode: Allow appending `#elif`, `#else` cases to CCodeIfSection
Diffstat (limited to 'ccode')
-rw-r--r-- | ccode/valaccodeifsection.vala | 42 |
1 files changed, 35 insertions, 7 deletions
diff --git a/ccode/valaccodeifsection.vala b/ccode/valaccodeifsection.vala index dd51743e0..c89cc26d5 100644 --- a/ccode/valaccodeifsection.vala +++ b/ccode/valaccodeifsection.vala @@ -27,24 +27,52 @@ using GLib; */ public class Vala.CCodeIfSection : CCodeFragment { /** - * The expression + * The conditional expression, or null if there is no condition. */ - public string expression { get; set; } + public string? expression { get; set; } - public CCodeIfSection (string expr) { + CCodeIfSection? else_section; + bool is_else_section; + + public CCodeIfSection (string? expr) { expression = expr; + is_else_section = false; + } + + public unowned CCodeIfSection append_else (string? expr = null) { + else_section = new CCodeIfSection (expr); + else_section.is_else_section = true; + return else_section; } public override void write (CCodeWriter writer) { - writer.write_string ("#if "); - writer.write_string (expression); + if (is_else_section) { + if (expression != null) { + writer.write_string ("#elif "); + writer.write_string (expression); + } else { + writer.write_string ("#else "); + } + } else if (expression != null) { + writer.write_string ("#if "); + writer.write_string (expression); + } + writer.write_newline (); foreach (CCodeNode node in get_children ()) { node.write_combined (writer); } - writer.write_string ("#endif"); - writer.write_newline (); + if (else_section != null) { + else_section.write_combined (writer); + } else { + writer.write_string ("#endif"); + writer.write_newline (); + } } public override void write_declaration (CCodeWriter writer) { } + + public override void write_combined (CCodeWriter writer) { + write (writer); + } } |