[vala] GIR writer: Generate constant c:identifier and value



commit 20ab99e1375a38443a4a24c2501c596ec18bf103
Author: Didier 'Ptitjes <ptitjes free fr>
Date:   Fri Mar 20 20:12:17 2009 +0100

    GIR writer: Generate constant c:identifier and value
    
    Signed-off-by: Didier 'Ptitjes <ptitjes free fr>
---
 gobject/valagirwriter.vala |   50 +++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 49 insertions(+), 1 deletions(-)

diff --git a/gobject/valagirwriter.vala b/gobject/valagirwriter.vala
index fb0b58d..9395143 100644
--- a/gobject/valagirwriter.vala
+++ b/gobject/valagirwriter.vala
@@ -350,8 +350,21 @@ public class Vala.GIRWriter : CodeVisitor {
 			return;
 		}
 
+		//TODO Add better constant evaluation
+		var initializer = c.initializer;
+		string value = literal_expression_to_value_string (initializer);
+
+		write_indent ();
+		stream.printf ("<constant name=\"%s\" c:identifier=\"%s\"", c.name, c.get_cname ());
+		stream.printf (" value=\"%s\"", value);
+		stream.printf (">\n");
+		indent++;
+
+		write_type (initializer.value_type);
+
+		indent--;
 		write_indent ();
-		stream.printf ("<constant name=\"%s\"/>\n", c.get_cname ());
+		stream.printf ("</constant>\n");
 	}
 
 	public override void visit_field (Field f) {
@@ -670,6 +683,41 @@ public class Vala.GIRWriter : CodeVisitor {
 		}
 	}
 
+	private string? literal_expression_to_value_string (Expression literal) {
+		if (literal is StringLiteral) {
+			var lit = literal as StringLiteral;
+			if (lit != null) {
+				return escape_attribute_string (lit.eval ());
+			}
+		} else if (literal is CharacterLiteral) {
+			return "%lc".printf (((CharacterLiteral) literal).get_char ());
+		} else if (literal is BooleanLiteral) {
+			return ((BooleanLiteral) literal).value ? "true" : "false";
+		} else if (literal is RealLiteral) {
+			return ((RealLiteral) literal).value;
+		} else if (literal is IntegerLiteral) {
+			return ((IntegerLiteral) literal).value;
+		} else if (literal is UnaryExpression) {
+			var unary = (UnaryExpression) literal;
+			if (unary.operator == UnaryOperator.MINUS) {
+				if (unary.inner is RealLiteral) {
+					return "-" + ((RealLiteral) unary.inner).value;
+				} else if (unary.inner is IntegerLiteral) {
+					return "-" + ((IntegerLiteral) unary.inner).value;
+				}
+			}
+		}
+		return null;
+	}
+
+	private string escape_attribute_string(string value) {
+		return value.replace ("&", "&amp;")
+			.replace ("<", "&lt;")
+			.replace (">", "&gt;")
+			.replace ("'", "&apos;")
+			.replace ("\"", "&quot;");
+	}
+
 	private bool check_accessibility (Symbol sym) {
 		if (sym.access == SymbolAccessibility.PUBLIC ||
 		    sym.access == SymbolAccessibility.PROTECTED) {



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