[vala/wip/attributes: 3/9] Use hashmap for storing code node attributes



commit 2a3e6649f7b4701b4ed8efd0a80e5dbddec93fb4
Author: Luca Bruno <lucabru src gnome org>
Date:   Wed Jul 6 10:23:20 2011 +0200

    Use hashmap for storing code node attributes

 vala/valacodenode.vala    |   12 +++------
 vala/valagenieparser.vala |   53 +++++++++++++++++++++++----------------------
 vala/valaparser.vala      |   50 +++++++++++++++++++++---------------------
 3 files changed, 56 insertions(+), 59 deletions(-)
---
diff --git a/vala/valacodenode.vala b/vala/valacodenode.vala
index 630d021..42f9402 100644
--- a/vala/valacodenode.vala
+++ b/vala/valacodenode.vala
@@ -45,7 +45,7 @@ public abstract class Vala.CodeNode {
 	/**
 	 * Contains all attributes that have been specified for this code node.
 	 */
-	public GLib.List<Attribute> attributes;
+	public Map<string, Attribute> attributes;
 
 	public string type_name {
 		get { return Type.from_instance (this).name (); }
@@ -143,14 +143,10 @@ public abstract class Vala.CodeNode {
 	 * @return     attribute
 	 */
 	public Attribute? get_attribute (string name) {
-		// FIXME: use hash table
-		foreach (Attribute a in attributes) {
-			if (a.name == name) {
-				return a;
-			}
+		if (attributes == null) {
+			return null;
 		}
-
-		return null;
+		return attributes.get (name);
 	}
 
 	/**
diff --git a/vala/valagenieparser.vala b/vala/valagenieparser.vala
index 0de741c..42ec403 100644
--- a/vala/valagenieparser.vala
+++ b/vala/valagenieparser.vala
@@ -2289,16 +2289,20 @@ public class Vala.Genie.Parser : CodeVisitor {
 		}
 	}
 
-	List<Attribute>? parse_attributes (bool parameter) throws ParseError {
+	Map<string, Attribute>? parse_attributes (bool parameter) throws ParseError {
 		if (current () != TokenType.OPEN_BRACKET) {
 			return null;
 		}
-		var attrs = new ArrayList<Attribute> ();
+		var attrs = new HashMap<string, Attribute> ();
 		while (accept (TokenType.OPEN_BRACKET)) {
 			do {
 				var begin = get_location ();
 				string id = parse_identifier ();
-				var attr = new Attribute (id, get_src (begin));
+				var attr = attrs.get (id);
+				if (attr == null) {
+					attr = new Attribute (id, get_src (begin));
+					attrs.set (id, attr);
+				}
 				if (accept (TokenType.OPEN_PARENS)) {
 					if (current () != TokenType.CLOSE_PARENS) {
 						do {
@@ -2309,7 +2313,6 @@ public class Vala.Genie.Parser : CodeVisitor {
 					}
 					expect (TokenType.CLOSE_PARENS);
 				}
-				attrs.add (attr);
 			} while (accept (TokenType.COMMA));
 			expect (TokenType.CLOSE_BRACKET);
 		}
@@ -2318,11 +2321,9 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return attrs;
 	}
 
-	void set_attributes (CodeNode node, List<Attribute>? attributes) {
+	void set_attributes (CodeNode node, Map<string, Attribute>? attributes) {
 		if (attributes != null) {
-			foreach (Attribute attr in (List<Attribute>) attributes) {
-				node.attributes.append (attr);
-			}
+			node.attributes = attributes;
 		}
 	}
 
@@ -2475,7 +2476,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return RecoveryState.EOF;
 	}
 
-	Namespace parse_namespace_declaration (List<Attribute>? attrs) throws ParseError {
+	Namespace parse_namespace_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.NAMESPACE);
 		var sym = parse_symbol_name ();
@@ -2566,7 +2567,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		
 	}
 
-	Symbol parse_class_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_class_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.CLASS);
 
@@ -2703,7 +2704,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		}
 	}
 
-	Constant parse_constant_declaration (List<Attribute>? attrs) throws ParseError {
+	Constant parse_constant_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 
 		expect (TokenType.CONST);
@@ -2742,7 +2743,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return c;
 	}
 
-	Field parse_field_declaration (List<Attribute>? attrs) throws ParseError {
+	Field parse_field_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		string id = parse_identifier ();
 		expect (TokenType.COLON);
@@ -2810,7 +2811,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 	
 	
 
-	Method parse_main_method_declaration (List<Attribute>? attrs) throws ParseError {
+	Method parse_main_method_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var id = "main";
 		var begin = get_location ();
 		DataType type = new VoidType ();
@@ -2842,7 +2843,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return method;
 	}
 
-	Method parse_method_declaration (List<Attribute>? attrs) throws ParseError {
+	Method parse_method_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		DataType type = new VoidType ();
 		expect (TokenType.DEF);
@@ -2992,7 +2993,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return method;
 	}
 
-	Property parse_property_declaration (List<Attribute>? attrs) throws ParseError {
+	Property parse_property_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var readonly = false;
 
@@ -3135,7 +3136,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return prop;
 	}
 
-	Vala.Signal parse_signal_declaration (List<Attribute>? attrs) throws ParseError {
+	Vala.Signal parse_signal_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		DataType type;
 
@@ -3193,7 +3194,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return sig;
 	}
 
-	Constructor parse_constructor_declaration (List<Attribute>? attrs) throws ParseError {
+	Constructor parse_constructor_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 
 		expect (TokenType.INIT);
@@ -3211,7 +3212,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return c;
 	}
 
-	Destructor parse_destructor_declaration (List<Attribute>? attrs) throws ParseError {
+	Destructor parse_destructor_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.FINAL);
 		var d = new Destructor (get_src (begin));
@@ -3220,7 +3221,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return d;
 	}
 
-	Symbol parse_struct_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_struct_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 
 		expect (TokenType.STRUCT);
@@ -3277,7 +3278,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		}
 	}
 
-	Symbol parse_interface_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_interface_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 
 		expect (TokenType.INTERFACE);
@@ -3354,7 +3355,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		}
 	}
 
-	Symbol parse_enum_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_enum_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.ENUM);
 		var flags = parse_type_declaration_modifiers ();
@@ -3412,7 +3413,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return result;
 	}
 
-	Symbol parse_errordomain_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_errordomain_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.ERRORDOMAIN);
 		var flags = parse_type_declaration_modifiers ();
@@ -3582,7 +3583,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return param;
 	}
 
-	CreationMethod parse_creation_method_declaration (List<Attribute>? attrs) throws ParseError {
+	CreationMethod parse_creation_method_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		CreationMethod method;
 
@@ -3632,7 +3633,7 @@ public class Vala.Genie.Parser : CodeVisitor {
 		return method;
 	}
 
-	Symbol parse_delegate_declaration (List<Attribute>? attrs) throws ParseError {
+	Symbol parse_delegate_declaration (Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		DataType type;
 
@@ -3683,8 +3684,8 @@ public class Vala.Genie.Parser : CodeVisitor {
 			d.access = get_access (sym.name);
 		}
 
-		if (!(ModifierFlags.STATIC in flags)) {
-			d.has_target = true;
+		if (ModifierFlags.STATIC in flags) {
+			d.has_target = false;
 		}
 		if (ModifierFlags.EXTERN in flags || scanner.source_file.file_type == SourceFileType.PACKAGE) {
 			d.external = true;
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 60c7a60..79e5477 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2106,16 +2106,20 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	List<Attribute>? parse_attributes () throws ParseError {
+	Map<string, Attribute>? parse_attributes () throws ParseError {
 		if (current () != TokenType.OPEN_BRACKET) {
 			return null;
 		}
-		var attrs = new ArrayList<Attribute> ();
+		var attrs = new HashMap<string, Attribute> (str_hash, str_equal);
 		while (accept (TokenType.OPEN_BRACKET)) {
 			do {
 				var begin = get_location ();
 				string id = parse_identifier ();
-				var attr = new Attribute (id, get_src (begin));
+				var attr = attrs.get (id);
+				if (attr == null) {
+					attr = new Attribute (id, get_src (begin));
+					attrs.set (id, attr);
+				}
 				if (accept (TokenType.OPEN_PARENS)) {
 					if (current () != TokenType.CLOSE_PARENS) {
 						do {
@@ -2126,18 +2130,15 @@ public class Vala.Parser : CodeVisitor {
 					}
 					expect (TokenType.CLOSE_PARENS);
 				}
-				attrs.add (attr);
 			} while (accept (TokenType.COMMA));
 			expect (TokenType.CLOSE_BRACKET);
 		}
 		return attrs;
 	}
 
-	void set_attributes (CodeNode node, List<Attribute>? attributes) {
+	void set_attributes (CodeNode node, Map<string, Attribute>? attributes) {
 		if (attributes != null) {
-			foreach (Attribute attr in (List<Attribute>) attributes) {
-				node.attributes.append (attr);
-			}
+			node.attributes = attributes;
 		}
 	}
 
@@ -2398,7 +2399,7 @@ public class Vala.Parser : CodeVisitor {
 		return RecoveryState.EOF;
 	}
 
-	void parse_namespace_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_namespace_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		expect (TokenType.NAMESPACE);
 		var sym = parse_symbol_name ();
@@ -2449,7 +2450,7 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	void parse_class_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_class_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_type_declaration_modifiers ();
@@ -2504,7 +2505,7 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	void parse_constant_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_constant_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -2539,7 +2540,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_constant (c);
 	}
 
-	void parse_field_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_field_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -2644,7 +2645,7 @@ public class Vala.Parser : CodeVisitor {
 		return map;
 	}
 
-	void parse_method_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_method_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -2741,7 +2742,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_method (method);
 	}
 
-	void parse_property_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_property_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -2883,7 +2884,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_property (prop);
 	}
 
-	void parse_signal_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_signal_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -2919,7 +2920,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_signal (sig);
 	}
 
-	void parse_constructor_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_constructor_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var flags = parse_member_declaration_modifiers ();
 		expect (TokenType.CONSTRUCT);
@@ -2937,7 +2938,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_constructor (c);
 	}
 
-	void parse_destructor_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_destructor_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var flags = parse_member_declaration_modifiers ();
 		expect (TokenType.TILDE);
@@ -2958,7 +2959,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_destructor (d);
 	}
 
-	void parse_struct_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_struct_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_type_declaration_modifiers ();
@@ -2998,7 +2999,7 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	void parse_interface_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_interface_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_type_declaration_modifiers ();
@@ -3041,7 +3042,7 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	void parse_enum_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_enum_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_type_declaration_modifiers ();
@@ -3098,7 +3099,7 @@ public class Vala.Parser : CodeVisitor {
 		}
 	}
 
-	void parse_errordomain_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_errordomain_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_type_declaration_modifiers ();
@@ -3285,7 +3286,7 @@ public class Vala.Parser : CodeVisitor {
 		return param;
 	}
 
-	void parse_creation_method_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_creation_method_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -3355,7 +3356,7 @@ public class Vala.Parser : CodeVisitor {
 		parent.add_method (method);
 	}
 
-	void parse_delegate_declaration (Symbol parent, List<Attribute>? attrs) throws ParseError {
+	void parse_delegate_declaration (Symbol parent, Map<string, Attribute>? attrs) throws ParseError {
 		var begin = get_location ();
 		var access = parse_access_modifier ();
 		var flags = parse_member_declaration_modifiers ();
@@ -3374,8 +3375,7 @@ public class Vala.Parser : CodeVisitor {
 				// TODO enable warning in future releases
 				Report.warning (get_last_src (), "deprecated syntax, use [CCode (has_target = false)]");
 			}
-		} else {
-			d.has_target = true;
+			d.has_target = false;
 		}
 		if (ModifierFlags.EXTERN in flags || scanner.source_file.file_type == SourceFileType.PACKAGE) {
 			d.external = true;



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