vala r1848 - in trunk: . gobject vala



Author: juergbi
Date: Fri Oct 17 11:57:31 2008
New Revision: 1848
URL: http://svn.gnome.org/viewvc/vala?rev=1848&view=rev

Log:
2008-10-17  JÃrg Billeter  <j bitron ch>

	* vala/valabinaryexpression.vala:
	* vala/valaexpression.vala:
	* vala/valaliteral.vala:
	* vala/valamemberaccess.vala:
	* vala/valasemanticanalyzer.vala:
	* gobject/valaccodegenerator.vala:

	Treat the result of two concatenated string constants as constant,
	fixes bug 516287


Modified:
   trunk/ChangeLog
   trunk/gobject/valaccodegenerator.vala
   trunk/vala/valabinaryexpression.vala
   trunk/vala/valaexpression.vala
   trunk/vala/valaliteral.vala
   trunk/vala/valamemberaccess.vala
   trunk/vala/valasemanticanalyzer.vala

Modified: trunk/gobject/valaccodegenerator.vala
==============================================================================
--- trunk/gobject/valaccodegenerator.vala	(original)
+++ trunk/gobject/valaccodegenerator.vala	Fri Oct 17 11:57:31 2008
@@ -3665,13 +3665,32 @@
 		    && !(expr.right.value_type is NullType)
 		    && expr.right.value_type.compatible (string_type)) {
 			if (expr.operator == BinaryOperator.PLUS) {
-				/* string concatenation: convert to g_strconcat (a, b, NULL) */
-				var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strconcat"));
-				ccall.add_argument (cleft);
-				ccall.add_argument (cright);
-				ccall.add_argument (new CCodeConstant("NULL"));
-				expr.ccodenode = ccall;
-				return;
+				// string concatenation
+				if (expr.left.is_constant () && expr.right.is_constant ()) {
+					string left, right;
+
+					if (cleft is CCodeIdentifier) {
+						left = ((CCodeIdentifier) cleft).name;
+					} else if (cleft is CCodeConstant) {
+						left = ((CCodeConstant) cleft).name;
+					}
+					if (cright is CCodeIdentifier) {
+						right = ((CCodeIdentifier) cright).name;
+					} else if (cright is CCodeConstant) {
+						right = ((CCodeConstant) cright).name;
+					}
+
+					expr.ccodenode = new CCodeConstant ("%s %s".printf (left, right));
+					return;
+				} else {
+					// convert to g_strconcat (a, b, NULL)
+					var ccall = new CCodeFunctionCall (new CCodeIdentifier ("g_strconcat"));
+					ccall.add_argument (cleft);
+					ccall.add_argument (cright);
+					ccall.add_argument (new CCodeConstant("NULL"));
+					expr.ccodenode = ccall;
+					return;
+				}
 			} else if (expr.operator == BinaryOperator.EQUALITY
 			           || expr.operator == BinaryOperator.INEQUALITY
 			           || expr.operator == BinaryOperator.LESS_THAN

Modified: trunk/vala/valabinaryexpression.vala
==============================================================================
--- trunk/vala/valabinaryexpression.vala	(original)
+++ trunk/vala/valabinaryexpression.vala	Fri Oct 17 11:57:31 2008
@@ -126,6 +126,10 @@
 		return _left.to_string () + get_operator_string () + _right.to_string ();
 	}
 
+	public override bool is_constant () {
+		return left.is_constant () && right.is_constant ();
+	}
+
 	public override bool is_pure () {
 		return left.is_pure () && right.is_pure ();
 	}

Modified: trunk/vala/valaexpression.vala
==============================================================================
--- trunk/vala/valaexpression.vala	(original)
+++ trunk/vala/valaexpression.vala	Fri Oct 17 11:57:31 2008
@@ -61,6 +61,14 @@
 	public ArrayList<LocalVariable> temp_vars = new ArrayList<LocalVariable> ();
 
 	/**
+	 * Returns whether this expression is constant, i.e. whether this
+	 * expression only consists of literals and other constants.
+	 */
+	public virtual bool is_constant () {
+		return false;
+	}
+
+	/**
 	 * Returns whether this expression is pure, i.e. whether this expression
 	 * is free of side-effects.
 	 */

Modified: trunk/vala/valaliteral.vala
==============================================================================
--- trunk/vala/valaliteral.vala	(original)
+++ trunk/vala/valaliteral.vala	Fri Oct 17 11:57:31 2008
@@ -26,6 +26,10 @@
  * Base class for all literals in the source code.
  */
 public abstract class Vala.Literal : Expression {
+	public override bool is_constant () {
+		return true;
+	}
+
 	public override bool is_pure () {
 		return true;
 	}

Modified: trunk/vala/valamemberaccess.vala
==============================================================================
--- trunk/vala/valamemberaccess.vala	(original)
+++ trunk/vala/valamemberaccess.vala	Fri Oct 17 11:57:31 2008
@@ -161,4 +161,12 @@
 	public override CodeBinding? create_code_binding (CodeGenerator codegen) {
 		return codegen.create_member_access_binding (this);
 	}
+
+	public override bool is_constant () {
+		if (symbol_reference is Constant) {
+			return true;
+		} else {
+			return false;
+		}
+	}
 }

Modified: trunk/vala/valasemanticanalyzer.vala
==============================================================================
--- trunk/vala/valasemanticanalyzer.vala	(original)
+++ trunk/vala/valasemanticanalyzer.vala	Fri Oct 17 11:57:31 2008
@@ -3156,7 +3156,11 @@
 			}
 
 			expr.value_type = string_type.copy ();
-			expr.value_type.value_owned = true;
+			if (expr.left.is_constant () && expr.right.is_constant ()) {
+				expr.value_type.value_owned = false;
+			} else {
+				expr.value_type.value_owned = true;
+			}
 		} else if (expr.operator == BinaryOperator.PLUS
 			   || expr.operator == BinaryOperator.MINUS
 			   || expr.operator == BinaryOperator.MUL



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