]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Support translated string constants
authorJürg Billeter <j@bitron.ch>
Tue, 8 Feb 2011 19:32:35 +0000 (20:32 +0100)
committerJürg Billeter <j@bitron.ch>
Tue, 8 Feb 2011 19:32:35 +0000 (20:32 +0100)
Fixes bug 641543.

codegen/valaccodebasemodule.vala
vala/valaconstant.vala
vala/valastringliteral.vala

index 14258bc23f66d6beb6bca1ee381b0fda267748d7..328de67ef805a017cc6705a2a91709276620a4b1 100644 (file)
@@ -1,6 +1,6 @@
 /* valaccodebasemodule.vala
  *
- * Copyright (C) 2006-2010  Jürg Billeter
+ * Copyright (C) 2006-2011  Jürg Billeter
  * Copyright (C) 2006-2008  Raffaele Sandrini
  *
  * This library is free software; you can redistribute it and/or
@@ -3424,6 +3424,17 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
 
        public override void visit_string_literal (StringLiteral expr) {
                set_cvalue (expr, new CCodeConstant.string (expr.value.replace ("\n", "\\n")));
+
+               if (expr.translate) {
+                       // translated string constant
+
+                       var m = (Method) root_symbol.scope.lookup ("GLib").scope.lookup ("_");
+                       add_symbol_declaration (cfile, m, m.get_cname ());
+
+                       var translate = new CCodeFunctionCall (new CCodeIdentifier ("_"));
+                       translate.add_argument (get_cvalue (expr));
+                       set_cvalue (expr, translate);
+               }
        }
 
        public override void visit_regex_literal (RegexLiteral expr) {
index fab450c17aaeb1450c2dc20a00bcab71deec45dd..c9b880cb5562e05757cc84762319fbb5f52c8683 100644 (file)
@@ -203,6 +203,21 @@ public class Vala.Constant : Symbol, Lockable {
                                        return false;
                                }
 
+                               // support translated string constants for efficiency / convenience
+                               // even though the expression is not a compile-time constant
+                               var call = value as MethodCall;
+                               if (call != null) {
+                                       var method_type = call.call.value_type as MethodType;
+                                       if (method_type != null && method_type.method_symbol.get_full_name () == "GLib._") {
+                                               // first argument is string
+                                               var literal = call.get_argument_list ().get (0) as StringLiteral;
+                                               if (literal != null) {
+                                                       value = literal;
+                                                       literal.translate = true;
+                                               }
+                                       }
+                               }
+
                                if (!value.is_constant ()) {
                                        error = true;
                                        Report.error (value.source_reference, "Value must be constant");
index 69fc4d79b2c4c877d62787fb6ac3b29ef862a863..ffb4285be2a65a7470666cc6da170a636284406c 100644 (file)
@@ -1,6 +1,6 @@
 /* valastringliteral.vala
  *
- * Copyright (C) 2006-2010  Jürg Billeter
+ * Copyright (C) 2006-2011  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
@@ -31,6 +31,8 @@ public class Vala.StringLiteral : Literal {
         */
        public string value { get; set; }
 
+       public bool translate { get; set; }
+
        /**
         * Creates a new string literal.
         *