>From 7b58ecb6c242e6a4b0ba6867117968b2986d6230 Mon Sep 17 00:00:00 2001 From: Yu Feng Date: Fri, 26 Jun 2009 10:01:29 +0800 Subject: [PATCH] Deduces is_all_constant in switch statements Currently non-constant case labels are not supported. --- vala/valaswitchlabel.vala | 10 +++++++++- vala/valaswitchsection.vala | 12 ++++++++++++ vala/valaswitchstatement.vala | 20 ++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletions(-) diff --git a/vala/valaswitchlabel.vala b/vala/valaswitchlabel.vala index a778ce2..67cb16c 100644 --- a/vala/valaswitchlabel.vala +++ b/vala/valaswitchlabel.vala @@ -65,11 +65,19 @@ public class Vala.SwitchLabel : CodeNode { } } + /* for default label this flag is also true */ + private bool _is_constant = true; + public override bool check (SemanticAnalyzer analyzer) { if (expression != null) { expression.check (analyzer); + _is_constant = expression.is_constant(); } - + return true; } + + public bool is_constant () { + return _is_constant; + } } diff --git a/vala/valaswitchsection.vala b/vala/valaswitchsection.vala index 27c02c2..bacad15 100644 --- a/vala/valaswitchsection.vala +++ b/vala/valaswitchsection.vala @@ -67,6 +67,15 @@ public class Vala.SwitchSection : Block { return false; } + private bool _is_all_constant = true; + /** + * Return if all labels are constant + * + * @return if all labels are constant + */ + public bool is_all_constant () { + return _is_all_constant; + } public override void accept (CodeVisitor visitor) { visitor.visit_switch_section (this); } @@ -90,6 +99,9 @@ public class Vala.SwitchSection : Block { foreach (SwitchLabel label in get_labels ()) { label.check (analyzer); + if(label.is_constant() == false) { + _is_all_constant = false; + } } owner = analyzer.current_symbol.scope; diff --git a/vala/valaswitchstatement.vala b/vala/valaswitchstatement.vala index a66a284..5be6fac 100644 --- a/vala/valaswitchstatement.vala +++ b/vala/valaswitchstatement.vala @@ -43,6 +43,7 @@ public class Vala.SwitchStatement : CodeNode, Statement { private Expression _expression; private Gee.List sections = new ArrayList (); + private bool _is_all_constant = true; /** * Creates a new switch statement. * @@ -74,6 +75,15 @@ public class Vala.SwitchStatement : CodeNode, Statement { return new ReadOnlyList (sections); } + /** + * Returns if all labels can be deduced into a constant + * + * @return if all labels can be deduced into a constant + */ + public bool is_all_constant() { + return _is_all_constant; + } + public override void accept (CodeVisitor visitor) { visitor.visit_switch_statement (this); } @@ -116,8 +126,18 @@ public class Vala.SwitchStatement : CodeNode, Statement { foreach (SwitchSection section in sections) { section.check (analyzer); + if(section.is_all_constant() == false) { + _is_all_constant = false; + } } + if(!_is_all_constant + && !expression.value_type.compatible (analyzer.string_type)) { + Report.error (expression.source_reference, + "Non-constant case labels are not yet supported"); + error = true; + return false; + } return !error; } } -- 1.6.0.6