Commit 20ab99e1 authored by Didier 'Ptitjes's avatar Didier 'Ptitjes Committed by Jürg Billeter

GIR writer: Generate constant c:identifier and value

Signed-off-by: default avatarDidier 'Ptitjes <ptitjes@free.fr>
parent 43a74d25
......@@ -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) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment