[vala] Add support for constant members inside enums



commit d78fe510c0cc28cad47af799db65c673dc7510b4
Author: Sebastian Dröge <sebastian droege collabora co uk>
Date:   Thu Apr 8 10:14:26 2010 +0200

    Add support for constant members inside enums

 vala/valacodewriter.vala |    5 ++++-
 vala/valaenum.vala       |   28 ++++++++++++++++++++++++++++
 vala/valaparser.vala     |    2 ++
 3 files changed, 34 insertions(+), 1 deletions(-)
---
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 04b3d5f..8d5f1b3 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -526,7 +526,7 @@ public class Vala.CodeWriter : CodeVisitor {
 		}
 
 		if (!first) {
-			if (en.get_methods ().size > 0) {
+			if (en.get_methods ().size > 0 || en.get_constants ().size > 0) {
 				write_string (";");
 			}
 			write_newline ();
@@ -536,6 +536,9 @@ public class Vala.CodeWriter : CodeVisitor {
 		foreach (Method m in en.get_methods ()) {
 			m.accept (this);
 		}
+		foreach (Constant c in en.get_constants ()) {
+			c.accept (this);
+		}
 		current_scope = current_scope.parent_scope;
 
 		write_end_block ();
diff --git a/vala/valaenum.vala b/vala/valaenum.vala
index 4e8c230..ef8a077 100644
--- a/vala/valaenum.vala
+++ b/vala/valaenum.vala
@@ -38,6 +38,7 @@ public class Vala.Enum : TypeSymbol {
 
 	private List<EnumValue> values = new ArrayList<EnumValue> ();
 	private List<Method> methods = new ArrayList<Method> ();
+	private List<Constant> constants = new ArrayList<Constant> ();
 	private string cname;
 	private string cprefix;
 	private string lower_case_cprefix;
@@ -91,6 +92,16 @@ public class Vala.Enum : TypeSymbol {
 	}
 
 	/**
+	 * Adds the specified constant as a member to this enum.
+	 *
+	 * @param c a constant
+	 */
+	public void add_constant (Constant c) {
+		constants.add (c);
+		scope.add (c.name, c);
+	}
+
+	/**
 	 * Returns a copy of the list of enum values.
 	 *
 	 * @return list of enum values
@@ -113,6 +124,15 @@ public class Vala.Enum : TypeSymbol {
 		return methods;
 	}
 
+	/**
+	 * Returns a copy of the list of constants.
+	 *
+	 * @return list of constants
+	 */
+	public List<Constant> get_constants () {
+		return constants;
+	}
+
 	public override void accept (CodeVisitor visitor) {
 		visitor.visit_enum (this);
 	}
@@ -125,6 +145,10 @@ public class Vala.Enum : TypeSymbol {
 		foreach (Method m in methods) {
 			m.accept (visitor);
 		}
+
+		foreach (Constant c in constants) {
+			c.accept (visitor);
+		}
 	}
 
 	public override string get_cname (bool const_type = false) {
@@ -317,6 +341,10 @@ public class Vala.Enum : TypeSymbol {
 			m.check (analyzer);
 		}
 
+		foreach (Constant c in constants) {
+			c.check (analyzer);
+		}
+
 		analyzer.current_source_file = old_source_file;
 		analyzer.current_symbol = old_symbol;
 
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 15ae81b..23d4b0f 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2943,6 +2943,8 @@ public class Vala.Parser : CodeVisitor {
 				var member_sym = parse_declaration ();
 				if (member_sym is Method) {
 					en.add_method ((Method) member_sym);
+				} else if (member_sym is Constant) {
+					en.add_constant ((Constant) member_sym);
 				} else {
 					Report.error (member_sym.source_reference, "unexpected declaration in enum");
 				}



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]