vala r848 - in trunk: . compiler vala



Author: juergbi
Date: Thu Jan 17 22:51:29 2008
New Revision: 848
URL: http://svn.gnome.org/viewvc/vala?rev=848&view=rev

Log:
2008-01-17  Juerg Billeter  <j bitron ch>

	* vala/parser.y, vala/valacodecontext.vala, compiler/valacompiler.vala:
	  add [Conditional (condition = "CONDITION")] attribute for methods,
	  fixes bug 434515


Modified:
   trunk/ChangeLog
   trunk/compiler/valacompiler.vala
   trunk/vala/parser.y
   trunk/vala/valacodecontext.vala

Modified: trunk/compiler/valacompiler.vala
==============================================================================
--- trunk/compiler/valacompiler.vala	(original)
+++ trunk/compiler/valacompiler.vala	Thu Jan 17 22:51:29 2008
@@ -48,6 +48,8 @@
 	[NoArrayLength]
 	static string[] cc_options;
 	static bool save_temps;
+	[NoArrayLength]
+	static string[] defines;
 
 	private CodeContext context;
 
@@ -64,6 +66,7 @@
 		{ "debug", 'g', 0, OptionArg.NONE, ref debug, "Produce debug information", null },
 		{ "thread", 0, 0, OptionArg.NONE, ref thread, "Enable multithreading support", null },
 		{ "optimize", 'O', 0, OptionArg.INT, ref optlevel, "Optimization level", "OPTLEVEL" },
+		{ "define", 'D', 0, OptionArg.STRING_ARRAY, out defines, "Define SYMBOL", "SYMBOL..." },
 		{ "disable-assert", 0, 0, OptionArg.NONE, ref disable_assert, "Disable assertions", null },
 		{ "disable-checking", 0, 0, OptionArg.NONE, ref disable_checking, "Disable run-time checks", null },
 		{ "enable-non-null", 0, 0, OptionArg.NONE, ref non_null, "Enable non-null types", null },
@@ -180,6 +183,12 @@
 		context.optlevel = optlevel;
 		context.save_temps = save_temps;
 
+		if (defines != null) {
+			foreach (string define in defines) {
+				context.add_define (define);
+			}
+		}
+
 		context.codegen = new CCodeGenerator ();
 
 		/* default package */

Modified: trunk/vala/parser.y
==============================================================================
--- trunk/vala/parser.y	(original)
+++ trunk/vala/parser.y	Thu Jan 17 22:51:29 2008
@@ -2981,32 +2981,34 @@
 method_declaration
 	: method_header method_body
 	  {
-		ValaCodeNode *n = (ValaCodeNode*)$1;
-		ValaAttribute *a = vala_code_node_get_attribute (n, "Import");
-		gboolean imported;
-		if (a != NULL) {
-			imported = TRUE;
-			g_object_unref (a);
-		} else {
-			imported = FALSE;
-		}
-		$$ = $1;
-		vala_method_set_body ($$, $2);
-		
-		if ($2 != NULL) {
-			g_object_unref ($2);
-			/* method must not be imported, abstract or from a VAPI file */
-			if (imported || vala_method_get_is_abstract ($1) || vala_source_file_get_pkg (current_source_file)) {
-				ValaSourceReference *sr = vala_code_node_get_source_reference (n);
-				vala_report_error (sr, "unexpected method body found");
-				g_object_unref (sr);
+		ValaCodeNode *n = VALA_CODE_NODE ($1);
+		if (n != NULL) {
+			ValaAttribute *a = vala_code_node_get_attribute (n, "Import");
+			gboolean imported;
+			if (a != NULL) {
+				imported = TRUE;
+				g_object_unref (a);
+			} else {
+				imported = FALSE;
 			}
-		} else {
-			/* only imported, abstract and VAPI methods are allowed to have no body */
-			if (!imported && !vala_method_get_is_abstract ($1) && !vala_source_file_get_pkg (current_source_file)) {
-				ValaSourceReference *sr = vala_code_node_get_source_reference (n);
-				vala_report_error (sr, "expected method body got `;'");
-				g_object_unref (sr);
+			$$ = $1;
+			vala_method_set_body ($$, $2);
+		
+			if ($2 != NULL) {
+				g_object_unref ($2);
+				/* method must not be imported, abstract or from a VAPI file */
+				if (imported || vala_method_get_is_abstract ($1) || vala_source_file_get_pkg (current_source_file)) {
+					ValaSourceReference *sr = vala_code_node_get_source_reference (n);
+					vala_report_error (sr, "unexpected method body found");
+					g_object_unref (sr);
+				}
+			} else {
+				/* only imported, abstract and VAPI methods are allowed to have no body */
+				if (!imported && !vala_method_get_is_abstract ($1) && !vala_source_file_get_pkg (current_source_file)) {
+					ValaSourceReference *sr = vala_code_node_get_source_reference (n);
+					vala_report_error (sr, "expected method body got `;'");
+					g_object_unref (sr);
+				}
 			}
 		}
 	  }
@@ -3093,6 +3095,11 @@
 
 		g_object_unref ($5);
 		g_free ($6);
+
+		if (vala_code_context_ignore_node (context, VALA_CODE_NODE ($$))) {
+			g_object_unref ($$);
+			$$ = NULL;
+		}
 	  }
 	| comment opt_attributes opt_access_modifier opt_modifiers identifier opt_name_specifier OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS opt_throws_declaration
 	  {

Modified: trunk/vala/valacodecontext.vala
==============================================================================
--- trunk/vala/valacodecontext.vala	(original)
+++ trunk/vala/valacodecontext.vala	Thu Jan 17 22:51:29 2008
@@ -124,6 +124,8 @@
 
 	private Gee.List<string> packages = new ArrayList<string> (str_equal);
 
+	private Gee.List<string> defines = new ArrayList<string> (str_equal);
+
 	private static bool _non_null = false;
 
 	/**
@@ -365,6 +367,24 @@
 		file.mark = 2;
 	}
 
+	public void add_define (string define) {
+		defines.add (define);
+	}
+
+	public bool ignore_node (CodeNode node) {
+		var attr = node.get_attribute ("Conditional");
+		if (attr == null) {
+			return false;
+		} else {
+			var condition = attr.get_string ("condition");
+			if (condition == null) {
+				return false;
+			} else {
+				return !defines.contains (condition);
+			}
+		}
+	}
+
 	public Namespace! create_namespace (string name, SourceReference source_reference = null) {
 		var node = new Namespace (name, source_reference);
 		node.code_binding = codegen.create_namespace_binding (node);



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