]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
GIR writer: Generate constant c:identifier and value
authorDidier 'Ptitjes <ptitjes@free.fr>
Fri, 20 Mar 2009 19:12:17 +0000 (20:12 +0100)
committerJürg Billeter <j@bitron.ch>
Thu, 30 Apr 2009 20:57:25 +0000 (22:57 +0200)
Signed-off-by: Didier 'Ptitjes <ptitjes@free.fr>
gobject/valagirwriter.vala

index fb0b58d0e58dfd6e78410d97251ed590a39c9870..9395143cef29f0b79d8275885cd2f21cbfe6f958 100644 (file)
@@ -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) {