From: Didier 'Ptitjes Date: Fri, 20 Mar 2009 19:12:17 +0000 (+0100) Subject: GIR writer: Generate constant c:identifier and value X-Git-Tag: 0.7.2~41 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=20ab99e1375a38443a4a24c2501c596ec18bf103;p=thirdparty%2Fvala.git GIR writer: Generate constant c:identifier and value Signed-off-by: Didier 'Ptitjes --- diff --git a/gobject/valagirwriter.vala b/gobject/valagirwriter.vala index fb0b58d0e..9395143ce 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 ("\n"); + indent++; + + write_type (initializer.value_type); + + indent--; write_indent (); - stream.printf ("\n", c.get_cname ()); + stream.printf ("\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 ("&", "&") + .replace ("<", "<") + .replace (">", ">") + .replace ("'", "'") + .replace ("\"", """); + } + private bool check_accessibility (Symbol sym) { if (sym.access == SymbolAccessibility.PUBLIC || sym.access == SymbolAccessibility.PROTECTED) {