[vala] Derive EnumValue from Constant



commit c866eed90e47fd0fceaae686497f5939ccc32fae
Author: Jürg Billeter <j bitron ch>
Date:   Sun Jul 25 20:12:03 2010 +0200

    Derive EnumValue from Constant

 codegen/valaccodememberaccessmodule.vala |   12 +++---
 codegen/valadovamemberaccessmodule.vala  |   12 +++---
 vala/valaconstant.vala                   |   12 ++++-
 vala/valaenumvalue.vala                  |   63 ++---------------------------
 vala/valagenieparser.vala                |   11 +++--
 vala/valagirparser.vala                  |    2 +-
 vala/valamemberaccess.vala               |    2 +-
 vala/valaparser.vala                     |    9 +++-
 vala/valasemanticanalyzer.vala           |    4 +-
 vapigen/valagidlparser.vala              |    2 +-
 10 files changed, 44 insertions(+), 85 deletions(-)
---
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 6143a4f..57ddcd3 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -172,6 +172,12 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 
 				expr.ccodenode = new CCodeIdentifier (f.get_cname ());
 			}
+		} else if (expr.symbol_reference is EnumValue) {
+			var ev = (EnumValue) expr.symbol_reference;
+
+			generate_enum_declaration ((Enum) ev.parent_symbol, source_declarations);
+
+			expr.ccodenode = new CCodeConstant (ev.get_cname ());
 		} else if (expr.symbol_reference is Constant) {
 			var c = (Constant) expr.symbol_reference;
 
@@ -346,12 +352,6 @@ public class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
 				ccomma.append_expression (ctemp);
 				expr.ccodenode = ccomma;
 			}
-		} else if (expr.symbol_reference is EnumValue) {
-			var ev = (EnumValue) expr.symbol_reference;
-
-			generate_enum_declaration ((Enum) ev.parent_symbol, source_declarations);
-
-			expr.ccodenode = new CCodeConstant (ev.get_cname ());
 		} else if (expr.symbol_reference is LocalVariable) {
 			var local = (LocalVariable) expr.symbol_reference;
 			if (local.is_result) {
diff --git a/codegen/valadovamemberaccessmodule.vala b/codegen/valadovamemberaccessmodule.vala
index 0d11f22..c6ef7e4 100644
--- a/codegen/valadovamemberaccessmodule.vala
+++ b/codegen/valadovamemberaccessmodule.vala
@@ -131,6 +131,12 @@ internal class Vala.DovaMemberAccessModule : DovaControlFlowModule {
 
 				expr.ccodenode = new CCodeIdentifier (f.get_cname ());
 			}
+		} else if (expr.symbol_reference is EnumValue) {
+			var ev = (EnumValue) expr.symbol_reference;
+
+			generate_enum_declaration ((Enum) ev.parent_symbol, source_declarations);
+
+			expr.ccodenode = new CCodeConstant (ev.get_cname ());
 		} else if (expr.symbol_reference is Constant) {
 			var c = (Constant) expr.symbol_reference;
 
@@ -192,12 +198,6 @@ internal class Vala.DovaMemberAccessModule : DovaControlFlowModule {
 			}
 
 			expr.ccodenode = ccall;
-		} else if (expr.symbol_reference is EnumValue) {
-			var ev = (EnumValue) expr.symbol_reference;
-
-			generate_enum_declaration ((Enum) ev.parent_symbol, source_declarations);
-
-			expr.ccodenode = new CCodeConstant (ev.get_cname ());
 		} else if (expr.symbol_reference is LocalVariable) {
 			var local = (LocalVariable) expr.symbol_reference;
 			if (local.is_result) {
diff --git a/vala/valaconstant.vala b/vala/valaconstant.vala
index 223ea45..02b815f 100644
--- a/vala/valaconstant.vala
+++ b/vala/valaconstant.vala
@@ -67,9 +67,11 @@ public class Vala.Constant : Symbol, Lockable {
 	 * @param source_reference reference to source code
 	 * @return                 newly created constant
 	 */
-	public Constant (string name, DataType type_reference, Expression? value, SourceReference? source_reference, Comment? comment = null) {
+	public Constant (string name, DataType? type_reference, Expression? value, SourceReference? source_reference, Comment? comment = null) {
 		base (name, source_reference, comment);
-		this.type_reference = type_reference;
+		if (type_reference != null) {
+			this.type_reference = type_reference;
+		}
 		this.value = value;
 	}
 
@@ -103,7 +105,7 @@ public class Vala.Constant : Symbol, Lockable {
 	 *
 	 * @return the name to be used in C code by default
 	 */
-	public string get_default_cname () {
+	public virtual string get_default_cname () {
 		if (parent_symbol == null) {
 			// global constant
 			return name;
@@ -112,6 +114,10 @@ public class Vala.Constant : Symbol, Lockable {
 		}
 	}
 
+	public void set_cname (string value) {
+		this.cname = value;
+	}
+
 	public bool get_lock_used () {
 		return lock_used;
 	}
diff --git a/vala/valaenumvalue.vala b/vala/valaenumvalue.vala
index 0c3d0ea..2ade411 100644
--- a/vala/valaenumvalue.vala
+++ b/vala/valaenumvalue.vala
@@ -1,6 +1,6 @@
 /* valaenumvalue.vala
  *
- * Copyright (C) 2006-2009  Jürg Billeter
+ * Copyright (C) 2006-2010  Jürg Billeter
  *
  * This library is free software; you can redistribute it and/or
  * modify it under the terms of the GNU Lesser General Public
@@ -25,25 +25,7 @@ using GLib;
 /**
  * Represents an enum member in the source code.
  */
-public class Vala.EnumValue : Symbol {
-	/**
-	 * Specifies the numerical representation of this enum value.
-	 */
-	public Expression value { get; set; }
-
-	private string cname;
-
-	/**
-	 * Creates a new enum value.
-	 *
-	 * @param name enum value name
-	 * @return     newly created enum value
-	 */
-	public EnumValue (string name, SourceReference? source_reference = null, Comment? comment = null) {
-		base (name, source_reference);
-		this.comment = comment;
-	}
-
+public class Vala.EnumValue : Constant {
 	/**
 	 * Creates a new enum value with the specified numerical representation.
 	 *
@@ -51,9 +33,8 @@ public class Vala.EnumValue : Symbol {
 	 * @param value numerical representation
 	 * @return      newly created enum value
 	 */
-	public EnumValue.with_value (string name, Expression value, SourceReference? source_reference = null, Comment? comment = null) {
-		this (name, source_reference, comment);
-		this.value = value;
+	public EnumValue (string name, Expression? value, SourceReference? source_reference = null, Comment? comment = null) {
+		base (name, null, value, source_reference, comment);
 	}
 	
 	/**
@@ -93,45 +74,11 @@ public class Vala.EnumValue : Symbol {
 		}
 	}
 
-	/**
-	 * Process all associated attributes.
-	 */
-	public void process_attributes () {
-		foreach (Attribute a in attributes) {
-			if (a.name == "CCode" && a.has_argument("cname")) {
-				cname = a.get_string ("cname");
-			} else if (a.name == "Deprecated") {
-				process_deprecated_attribute (a);
-			}
-		}
-	}
-
-	/**
-	 * Returns the name of this enum value as it is used in C code.
-	 *
-	 * @return the name to be used in C code
-	 */
-	public string get_cname () {
-		if (cname == null) {
-			cname = get_default_cname ();
-		}
-		return cname;
-	}
-
-	public string get_default_cname () {
+	public override string get_default_cname () {
 		var en = (Enum) parent_symbol;
 		return "%s%s".printf (en.get_cprefix (), name);
 	}
 
-	/**
-	 * Sets the name of this enum value to be used in C code.
-	 *
-	 * @param cname the name to be used in C code
-	 */
-	public void set_cname (string cname) {
-		this.cname = cname;
-	}
-
 	public override bool check (SemanticAnalyzer analyzer) {
 		if (checked) {
 			return !error;
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index d4d01b9..50e6e7d 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -3382,12 +3382,15 @@ public class Vala.Genie.Parser : CodeVisitor {
 			var value_begin = get_location (); 
 			string id = parse_identifier ();
 			comment = scanner.pop_comment ();
-			var ev = new EnumValue (id, get_src (value_begin), comment);
-			set_attributes (ev, value_attrs);
-			
+
+			Expression value = null;
 			if (accept (TokenType.ASSIGN)) {
-				ev.value = parse_expression ();
+				value = parse_expression ();
 			}
+
+			var ev = new EnumValue (id, value, get_src (value_begin), comment);
+			set_attributes (ev, value_attrs);
+
 			en.add_value (ev);
 			expect (TokenType.EOL);
 		} while (true);
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index bf1d33e..67f8437 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -376,7 +376,7 @@ public class Vala.GirParser : CodeVisitor {
 
 	EnumValue parse_enumeration_member () {
 		start_element ("member");
-		var ev = new EnumValue (string.joinv ("_", reader.get_attribute ("name").up ().split ("-")));
+		var ev = new EnumValue (string.joinv ("_", reader.get_attribute ("name").up ().split ("-")), null);
 		ev.set_cname (reader.get_attribute ("c:identifier"));
 		next ();
 		end_element ("member");
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 82570b0..b6325f2 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -166,7 +166,7 @@ public class Vala.MemberAccess : Expression {
 	}
 
 	public override bool is_constant () {
-		if (symbol_reference is Constant || symbol_reference is EnumValue) {
+		if (symbol_reference is Constant) {
 			return true;
 		} else {
 			return false;
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 67366df..2acbcee 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -3047,11 +3047,14 @@ public class Vala.Parser : CodeVisitor {
 			var value_begin = get_location ();
 			string id = parse_identifier ();
 			comment = scanner.pop_comment ();
-			var ev = new EnumValue (id, get_src (value_begin), comment);
-			set_attributes (ev, value_attrs);
+
+			Expression value = null;
 			if (accept (TokenType.ASSIGN)) {
-				ev.value = parse_expression ();
+				value = parse_expression ();
 			}
+
+			var ev = new EnumValue (id, value, get_src (value_begin), comment);
+			set_attributes (ev, value_attrs);
 			en.add_value (ev);
 		} while (accept (TokenType.COMMA));
 		if (accept (TokenType.SEMICOLON)) {
diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala
index c2eb9c7..4a6ab87 100644
--- a/vala/valasemanticanalyzer.vala
+++ b/vala/valasemanticanalyzer.vala
@@ -261,6 +261,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 				type.value_owned = false;
 			}
 			return type;
+		} else if (sym is EnumValue) {
+			return new EnumValueType ((Enum) sym.parent_symbol);
 		} else if (sym is Constant) {
 			var c = (Constant) sym;
 			return c.type_reference;
@@ -289,8 +291,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
 				type.value_owned = false;
 			}
 			return type;
-		} else if (sym is EnumValue) {
-			return new EnumValueType ((Enum) sym.parent_symbol);
 		} else if (sym is Method) {
 			return new MethodType ((Method) sym);
 		} else if (sym is Signal) {
diff --git a/vapigen/valagidlparser.vala b/vapigen/valagidlparser.vala
index a932ef2..985665e 100644
--- a/vapigen/valagidlparser.vala
+++ b/vapigen/valagidlparser.vala
@@ -1084,7 +1084,7 @@ public class Vala.GIdlParser : CodeVisitor {
 			}
 
 			if (!is_hidden) {
-				var ev = new EnumValue (value2.name.offset (common_prefix.len ()));
+				var ev = new EnumValue (value2.name.offset (common_prefix.len ()), null);
 				en.add_value (ev);
 			}
 		}



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